def METHOD_NAME(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
        return old_pid
    finally:
        os.close(fd)