Ejemplo n.º 1
0
    def _try_lock(self, pid):
        fd = os.open(self.target, os.O_CREAT | os.O_RDWR, 0o644)

        try:
            try:
                fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
            except OSError as e:
                if e.errno == errno.EWOULDBLOCK:
                    return -1
                raise

            old_pid = os.read(fd, 20)
            if len(old_pid) == 0:
                # empty file, write our pid
                os.write(fd, str(pid).encode('utf-8'))
                return pid

            try:
                old_pid = int(old_pid)
            except ValueError:
                msg = _('Malformed lock file found: %s.\n'
                        'Ensure no other dnf/yum process is running and '
                        'remove the lock file manually or run '
                        'systemd-tmpfiles --remove dnf.conf.') % (self.target)
                raise LockError(msg)

            if old_pid == pid:
                # already locked by this process
                return pid

            if not os.access('/proc/%d/stat' % old_pid, os.F_OK):
                # locked by a dead process, write our pid
                os.lseek(fd, 0, os.SEEK_SET)
                os.ftruncate(fd, 0)
                os.write(fd, str(pid).encode('utf-8'))
                return pid

            try:
                with open('/proc/%d/status' % old_pid) as f:
                    if not re.findall(r'Name:(.*)(dnf|yum)', f.read(), re.I):
                        os.write(fd, str(pid).encode('utf-8'))
                        return pid
            except OSError as e:
                msg = _('Malformed lock file found: %s.\n'
                        'But pid in lock file is invalid '
                        'Ensure no other dnf/yum process is running and '
                        'remove the lock file manually.') % (self.target)
                raise LockError(msg)

            return old_pid

        finally:
            os.close(fd)
Ejemplo n.º 2
0
 def _try_read_lock(self):
     try:
         with open(self.target, 'r') as f:
             return int(f.readline())
     except IOError:
         return -1
     except ValueError:
         time.sleep(2)
         try:
             with open(self.target, 'r') as f:
                 return int(f.readline())
         except IOError:
             return -1
         except ValueError:
             msg = _('Malformed lock file found: %s.\n'
                     'Ensure no other dnf process is running and '
                     'remove the lock file manually or run '
                     'systemd-tmpfiles --remove dnf.conf.' % (self.target))
             raise LockError(msg)