Python fcntl.LOCK_SH Examples

The following are 30 code examples of fcntl.LOCK_SH(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module fcntl , or try the search function .
Example #1
Source File: schema.py    From conary with Apache License 2.0 6 votes vote down vote up
def _shareLock(db):
    """
    Take a share lock on the database syslock when an optional migration might
    run. If it conflicts due to an ongoing update then bail out.
    """
    if db.database == ':memory:':
        # Nothing to lock
        return None, True
    lockPath = os.path.join(os.path.dirname(db.database), 'syslock')
    try:
        lockFile = open(lockPath, 'r+')
        fcntl.lockf(lockFile.fileno(), fcntl.LOCK_SH | fcntl.LOCK_NB)
    except IOError as err:
        if err.args[0] in (errno.EAGAIN, errno.EACCES, errno.EROFS):
            # Busy or no write access; skip optional migrations
            return None, False
        elif err.args[0] == errno.ENOENT:
            # Database has never been locked. Probably running in a testsuite,
            # so proceed anyway.
            return None, True
        raise
    return lockFile, True 
Example #2
Source File: cacheprediction.py    From kerncraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self, kernel, machine, cores=1, symbolic=False):
        """Initialize layer condition based predictor from kernel and machine object."""
        CachePredictor.__init__(self, kernel, machine, cores=cores)
        if isinstance(kernel, KernelCode):
            # Make use of caching for symbolic LC representation:
            file_name = 'LC_analysis.pickle.lzma'
            file_path = kernel.get_intermediate_location(
                file_name, machine_and_compiler_dependent=False, other_dependencies=[str(cores)])
            lock_mode, lock_fp = kernel.lock_intermediate(file_path)
            if lock_mode == fcntl.LOCK_SH:
                # use cache
                self.results = compress_pickle.load(file_path)
                lock_fp.close()  # release lock
            else:  # lock_mode == fcntl.LOCK_EX
                # needs update
                self.build_symbolic_LCs()
                compress_pickle.dump(self.results, file_path)
                lock_fp.close()  # release lock
        else:
            # No caching support without filename for kernel code
            self.build_symbolic_LCs()

        if not symbolic:
            self.desymbolize() 
Example #3
Source File: docker.py    From deimos with Apache License 2.0 6 votes vote down vote up
def wait(self, wait_pb, *args):
        log.info(" ".join(args))
        container_id = wait_pb.container_id.value
        state = deimos.state.State(self.state_root, mesos_id=container_id)
        self.state = state
        deimos.sig.install(self.stop_docker_and_resume)
        state.await_launch()
        try:  # Wait for the observe lock so observe completes first
            state.lock("observe", LOCK_SH, seconds=None)
            state.lock("wait", LOCK_SH, seconds=None)
        except IOError as e:                       # Allows for signal recovery
            if e.errno != errno.EINTR:
                raise e
            state.lock("observe", LOCK_SH, seconds=1)
            state.lock("wait", LOCK_SH, seconds=1)
        termination = (state.exit() if state.exit() is not None else 64) << 8
        recordio.write(Termination,
                       killed=False,
                       message="",
                       status=termination)
        if state.exit() is not None:
            return state.exit()
        raise Err("Wait lock is not held nor is exit file present") 
Example #4
Source File: docker.py    From deimos with Apache License 2.0 6 votes vote down vote up
def containers(self, *args):
        log.info(" ".join(args))
        data = Run(data=True)(deimos.docker.docker("ps", "--no-trunc", "-q"))
        mesos_ids = []
        for line in data.splitlines():
            cid = line.strip()
            state = deimos.state.State(self.state_root, docker_id=cid)
            if not state.exists():
                continue
            try:
                state.lock("wait", LOCK_SH | LOCK_NB)
            except deimos.flock.Err:     # LOCK_EX held, so launch() is running
                mesos_ids += [state.mesos_container_id()]
        containers = Containers()
        for mesos_id in mesos_ids:
            container = containers.containers.add()
            container.value = mesos_id
        recordio.writeProto(containers)
        return 0 
Example #5
Source File: kernel.py    From kerncraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_kernel_header(self, name='kernel'):
        """
        Generate and store kernel.h

        :return: tuple of filename of header and file pointer of lockfile
        """
        file_name = 'kernel.h'
        file_path = self.get_intermediate_location(
            file_name, machine_and_compiler_dependent=False)
        lock_mode, lock_fp = self.lock_intermediate(file_path)
        if lock_mode == fcntl.LOCK_SH:
            # use cache
            with open(file_path) as f:
                code = f.read()
        else:  # lock_mode == fcntl.LOCK_EX
            # needs update
            func_decl = self._build_kernel_function_declaration(name=name)
            scalar_decls = self.get_scalar_declarations()
            code = CGenerator().visit(
                c_ast.FileAST(ext=[func_decl]+scalar_decls))
            with open(file_path, 'w') as f:
                f.write(code)
            fcntl.flock(lock_fp, fcntl.LOCK_SH)  # degrade to shared lock

        return file_name, lock_fp 
Example #6
Source File: lock.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def AcquireFileLock(target_file, flags):
  """ Lock the target file. Note that if |target_file| is closed, the lock is
    automatically released.
  Args:
    target_file: file handle of the file to acquire lock.
    flags: can be any of the type LOCK_EX, LOCK_SH, LOCK_NB, or a bitwise
      OR combination of flags.
  """
  assert flags in (
      LOCK_EX, LOCK_SH, LOCK_NB, LOCK_EX | LOCK_NB, LOCK_SH | LOCK_NB)
  if os.name == 'nt':
    _LockImplWin(target_file, flags)
  elif os.name == 'posix':
    _LockImplPosix(target_file, flags)
  else:
    raise NotImplementedError('%s is not supported' % os.name) 
Example #7
Source File: lock.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def AcquireFileLock(target_file, flags):
  """ Lock the target file. Note that if |target_file| is closed, the lock is
    automatically released.
  Args:
    target_file: file handle of the file to acquire lock.
    flags: can be any of the type LOCK_EX, LOCK_SH, LOCK_NB, or a bitwise
      OR combination of flags.
  """
  assert flags in (
      LOCK_EX, LOCK_SH, LOCK_NB, LOCK_EX | LOCK_NB, LOCK_SH | LOCK_NB)
  if os.name == 'nt':
    _LockImplWin(target_file, flags)
  elif os.name == 'posix':
    _LockImplPosix(target_file, flags)
  else:
    raise NotImplementedError('%s is not supported' % os.name) 
Example #8
Source File: Util.py    From securedrop-workstation with GNU Affero General Public License v3.0 6 votes vote down vote up
def can_obtain_lock(basename):
    """
    We temporarily obtain a shared, nonblocking lock to a lockfile to determine
    whether the associated process is currently running. Returns True if it is
    safe to continue execution (no lock conflict), False if not.

    `basename` is the basename of a lockfile situated in the LOCK_DIRECTORY.
    """
    lock_file = os.path.join(LOCK_DIRECTORY, basename)
    try:
        lh = open(lock_file, "r")
    except FileNotFoundError:  # noqa: F821
        # Process may not have run during this session, safe to continue
        return True

    try:
        # Obtain a nonblocking, shared lock
        fcntl.lockf(lh, fcntl.LOCK_SH | fcntl.LOCK_NB)
    except IOError:
        sdlog.error(LOCK_ERROR.format(lock_file))
        return False

    return True 
Example #9
Source File: lock.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def AcquireFileLock(target_file, flags):
  """ Lock the target file. Note that if |target_file| is closed, the lock is
    automatically released.
  Args:
    target_file: file handle of the file to acquire lock.
    flags: can be any of the type LOCK_EX, LOCK_SH, LOCK_NB, or a bitwise
      OR combination of flags.
  """
  assert flags in (
      LOCK_EX, LOCK_SH, LOCK_NB, LOCK_EX | LOCK_NB, LOCK_SH | LOCK_NB)
  if os.name == 'nt':
    _LockImplWin(target_file, flags)
  elif os.name == 'posix':
    _LockImplPosix(target_file, flags)
  else:
    raise NotImplementedError('%s is not supported' % os.name) 
Example #10
Source File: fs.py    From canari3 with GNU General Public License v3.0 5 votes vote down vote up
def locksh(self, nb=False):
        flags = LOCK_SH
        if nb:
            flags |= LOCK_NB
        flock(self, flags)
        self.locked = True 
Example #11
Source File: Certificate.py    From bot-sdk-python with Apache License 2.0 5 votes vote down vote up
def get_file_content_safety(filename):
    """
    获取文件内容
    :param filename
    :return content
    """
    with open(filename, 'r') as f:
        fcntl.flock(f, fcntl.LOCK_SH)
        content = f.read()
        fcntl.flock(f, fcntl.LOCK_UN)
        return content 
Example #12
Source File: utils.py    From anime-dl with MIT License 5 votes vote down vote up
def _lock_file(f, exclusive):
            fcntl.flock(f, fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH) 
Example #13
Source File: portalocker.py    From pydal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, filename, mode="rb"):
        self.filename = filename
        self.mode = mode
        self.file = None
        if "r" in mode:
            self.file = open_file(filename, mode)
            lock(self.file, LOCK_SH)
        elif "w" in mode or "a" in mode:
            self.file = open_file(filename, mode.replace("w", "a"))
            lock(self.file, LOCK_EX)
            if "a" not in mode:
                self.file.seek(0)
                self.file.truncate(0)
        else:
            raise RuntimeError("invalid LockedFile(...,mode)") 
Example #14
Source File: sandbox.py    From catnip with Apache License 2.0 5 votes vote down vote up
def _GetSandboxStatus(self, sandbox_id):
    # Throws KeyError when the user does not exist.
    pwd.getpwnam('catnip%d' % sandbox_id)
    try:
      with util.Lock(os.path.join(SANDBOX_LOCK_BASEDIR, '%d' % sandbox_id),
                     fcntl.LOCK_SH|fcntl.LOCK_NB):
        pass
    except util.LockFailed:
      return {'status': 'running'}
    else:
      return {'status': 'ready'} 
Example #15
Source File: fs.py    From canari3 with GNU General Public License v3.0 5 votes vote down vote up
def flock(file_, flags):
        hfile = msvcrt.get_osfhandle(file_.fileno())
        overlapped = OVERLAPPED()
        if flags & LOCK_UN and UnlockFileEx(hfile, 0, 0, 0xFFFF0000, byref(overlapped)):
            return
        elif (not flags or flags & (LOCK_EX | LOCK_NB | LOCK_SH)) and \
                LockFileEx(hfile, flags, 0, 0, 0xFFFF0000, byref(overlapped)):
            return
        raise IOError(GetLastError()) 
Example #16
Source File: sandbox.py    From catnip with Apache License 2.0 5 votes vote down vote up
def _SandboxLock(self):
    @contextlib.contextmanager
    def Locker():
      self._log.info('locking sandbox %d', self._params.cpu)
      with util.Lock(SANDBOX_MASTER_LOCK_FILE, fcntl.LOCK_SH):
        if not os.path.exists(SANDBOX_SETUP_FILE):
          raise InternalError('Sandbox is not ready')
        if not self._params.ignore_health:
          try:
            with util.Lock(
                SANDBOX_HEALTH_CHECK_LOCK_FILE, fcntl.LOCK_SH|fcntl.LOCK_NB):
              with open(SANDBOX_HEALTH_FILE) as f:
                health = (f.readline().strip() == 'healthy')
          except util.LockFailed:
            # A health checker is running.
            health = None
          except IOError:
            # No health checker has run yet.
            health = None
          if health is None:
            raise InternalError('Sandbox is not ready')
          if health is False:
            raise InternalError('Sandbox is unhealthy')
        lock_mode = fcntl.LOCK_SH if self._params.share_cpu else fcntl.LOCK_EX
        if not self._params.block:
          lock_mode |= fcntl.LOCK_NB
        try:
          with util.Lock(self._sandbox_lock_file, lock_mode):
            self._log.info('locked sandbox %d', self._params.cpu)
            yield
            self._log.info('unlocking sandbox %d', self._params.cpu)
        except util.LockFailed:
          raise InternalError('Sandbox is locked')
      self._log.info('unlocked sandbox %d', self._params.cpu)
    return Locker() 
Example #17
Source File: sandbox.py    From catnip with Apache License 2.0 5 votes vote down vote up
def GetStatus(self):
    with util.Lock(SANDBOX_MASTER_LOCK_FILE, fcntl.LOCK_SH):
      status = {}
      status['status'] = self._GetMasterStatus()
      status['sandbox'] = []
      try:
        for sandbox_id in xrange(256):
          status['sandbox'].append(self._GetSandboxStatus(sandbox_id))
      except KeyError:
        pass
    return status 
Example #18
Source File: cache.py    From CrisisLex with MIT License 5 votes vote down vote up
def _lock_file_posix(self, path, exclusive=True):
        lock_path = path + '.lock'
        if exclusive is True:
            f_lock = open(lock_path, 'w')
            fcntl.lockf(f_lock, fcntl.LOCK_EX)
        else:
            f_lock = open(lock_path, 'r')
            fcntl.lockf(f_lock, fcntl.LOCK_SH)
        if os.path.exists(lock_path) is False:
            f_lock.close()
            return None
        return f_lock 
Example #19
Source File: accessfs.py    From allura with Apache License 2.0 5 votes vote down vote up
def lock(self, cmd, owner, **kw):
        # The code here is much rather just a demonstration of the locking
        # API than something which actually was seen to be useful.

        # Advisory file locking is pretty messy in Unix, and the Python
        # interface to this doesn't make it better.
        # We can't do fcntl(2)/F_GETLK from Python in a platfrom independent
        # way. The following implementation *might* work under Linux.
        #
        # if cmd == fcntl.F_GETLK:
        #     import struct
        #
        #     lockdata = struct.pack('hhQQi', kw['l_type'], os.SEEK_SET,
        #                            kw['l_start'], kw['l_len'], kw['l_pid'])
        #     ld2 = fcntl.fcntl(self.fd, fcntl.F_GETLK, lockdata)
        #     flockfields = ('l_type', 'l_whence', 'l_start', 'l_len', 'l_pid')
        #     uld2 = struct.unpack('hhQQi', ld2)
        #     res = {}
        #     for i in xrange(len(uld2)):
        #          res[flockfields[i]] = uld2[i]
        #
        #     return fuse.Flock(**res)

        # Convert fcntl-ish lock parameters to Python's weird
        # lockf(3)/flock(2) medley locking API...
        op = {fcntl.F_UNLCK: fcntl.LOCK_UN,
              fcntl.F_RDLCK: fcntl.LOCK_SH,
              fcntl.F_WRLCK: fcntl.LOCK_EX}[kw['l_type']]
        if cmd == fcntl.F_GETLK:
            return -errno.EOPNOTSUPP
        elif cmd == fcntl.F_SETLK:
            if op != fcntl.LOCK_UN:
                op |= fcntl.LOCK_NB
        elif cmd == fcntl.F_SETLKW:
            pass
        else:
            return -errno.EINVAL

        fcntl.lockf(self.fd, op, kw['l_start'], kw['l_len']) 
Example #20
Source File: synchronization.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def do_acquire_read_lock(self, wait):
        filedescriptor = self._open(os.O_CREAT | os.O_RDONLY)
        if not wait:
            try:
                fcntl.flock(filedescriptor, fcntl.LOCK_SH | fcntl.LOCK_NB)
                return True
            except IOError:
                os.close(filedescriptor)
                self._filedescriptor.remove()
                return False
        else:
            fcntl.flock(filedescriptor, fcntl.LOCK_SH)
            return True 
Example #21
Source File: common.py    From VxWireguard-Generator with MIT License 5 votes vote down vote up
def _open_file(self, conf_name: str, writable: bool = False) -> None:
        if self._conf_file is not None and self._conf_name == conf_name and self._writable >= writable:
            self._conf_file.seek(0)
            return
        self._close_file()
        if writable:
            conf_file = open(conf_name + '.conf', 'w+')
        else:
            conf_file = open(conf_name + '.conf', 'r')
        try:
            try:
                if writable:
                    fcntl.lockf(conf_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
                else:
                    fcntl.lockf(conf_file, fcntl.LOCK_SH | fcntl.LOCK_NB)
            except OSError as e:
                if e.errno in (errno.EACCES, errno.EAGAIN):
                    print('The configuration file is being used by another process, waiting.', end='', file=sys.stderr, flush=True)
                    if writable:
                        fcntl.lockf(conf_file, fcntl.LOCK_EX)
                    else:
                        fcntl.lockf(conf_file, fcntl.LOCK_SH)
                    print(file=sys.stderr, flush=True)
                else:
                    raise
        except Exception as e:
            conf_file.close()
            raise
        self._conf_file = conf_file
        self._conf_name = conf_name
        self._writable = writable 
Example #22
Source File: portalocker.py    From termite-visualizations with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, filename, mode='rb'):
        self.filename = filename
        self.mode = mode
        self.file = None
        if 'r' in mode:
            self.file = open(filename, mode)
            lock(self.file, LOCK_SH)
        elif 'w' in mode or 'a' in mode:
            self.file = open(filename, mode.replace('w', 'a'))
            lock(self.file, LOCK_EX)
            if not 'a' in mode:
                self.file.seek(0)
                self.file.truncate()
        else:
            raise RuntimeError("invalid LockedFile(...,mode)") 
Example #23
Source File: kernel.py    From kerncraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def lock_intermediate(self, file_path):
        """
        Lock intermediate. Depending on state, readable or writable.

        A sepeate file_path+'.lock' file is used. It is the callees responsibility to close the lock

        :param file_path: path to baser lock file on.

        :return: tuple: (acquired lock mode, lock file pointer)

        lock modes are: fcntl.LOCK_SH, which means file_path is good for read-only access
                        fcntl.LOCK_EX, which means file_path is good for write access and MUST be 
                        create/updated.
        """
        lock_filename = file_path + '.lock'
        # 1. Open lockfile (create and write)
        lock_fp = open(lock_filename, 'w')
        # 2. Acquire SH lock (blocking)
        fcntl.flock(lock_fp, fcntl.LOCK_SH)
        # 3. Check existence and freshness
        if self._check_freshness(file_path):
            # -> READ MODE
            return (fcntl.LOCK_SH, lock_fp)
        # 4. Release SH lock (to allow other processes already awaiting an exclusive lock to enter)
        fcntl.flock(lock_fp, fcntl.LOCK_UN)
        # 5. Acquire EX lock (blocking)
        fcntl.flock(lock_fp, fcntl.LOCK_EX)
        # 6. Check if file is now fresh (things may have changed!)
        if self._check_freshness(file_path):
            # Acquire SH lock (this will replace EX lock in-place)
            fcntl.flock(lock_fp, fcntl.LOCK_SH)
            # -> READ MODE
            return (fcntl.LOCK_SH, lock_fp)
        # else: -> WRITE MODE
        return (fcntl.LOCK_EX, lock_fp) 
Example #24
Source File: kernel.py    From kerncraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def assemble_to_object(self, in_filename, verbose=False):
        """
        Assemble *in_filename* assembly into *out_filename* object.

        Returns tuple of filename to binary file and shared lock file pointer
        """
        # Build file name
        file_base_name = os.path.splitext(os.path.basename(in_filename))[0]
        out_filename = self.get_intermediate_location(file_base_name + '.o')
        lock_mode, lock_fp = self.lock_intermediate(out_filename)

        if lock_mode == fcntl.LOCK_SH:
            # use cached version
            pass
        else:  # lock_mode == fcntl.LOCK_EX
            # needs update
            compiler, compiler_args = self._machine.get_compiler()

            # Compile to object file
            compiler_args.append('-c')

            cmd = [compiler] + [in_filename] + compiler_args + ['-o', out_filename]

            if verbose:
                print('Executing (assemble_to_object): ', ' '.join(cmd))

            try:
                # Assemble all to a binary
                subprocess.check_output(cmd)
                fcntl.flock(lock_fp, fcntl.LOCK_SH)  # degrade to shared lock
            except subprocess.CalledProcessError as e:
                print("Assembly failed:", e, file=sys.stderr)
                sys.exit(1)

        return out_filename, lock_fp 
Example #25
Source File: cache.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def _lock_file_posix(self, path, exclusive=True):
        lock_path = path + '.lock'
        if exclusive is True:
            f_lock = open(lock_path, 'w')
            fcntl.lockf(f_lock, fcntl.LOCK_EX)
        else:
            f_lock = open(lock_path, 'r')
            fcntl.lockf(f_lock, fcntl.LOCK_SH)
        if os.path.exists(lock_path) is False:
            f_lock.close()
            return None
        return f_lock 
Example #26
Source File: utils.py    From tvalacarta with GNU General Public License v3.0 5 votes vote down vote up
def _lock_file(f, exclusive):
            fcntl.flock(f, fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH) 
Example #27
Source File: cache.py    From locality-sensitive-hashing with MIT License 5 votes vote down vote up
def _lock_file_posix(self, path, exclusive=True):
        lock_path = path + '.lock'
        if exclusive is True:
            f_lock = open(lock_path, 'w')
            fcntl.lockf(f_lock, fcntl.LOCK_EX)
        else:
            f_lock = open(lock_path, 'r')
            fcntl.lockf(f_lock, fcntl.LOCK_SH)
        if os.path.exists(lock_path) is False:
            f_lock.close()
            return None
        return f_lock 
Example #28
Source File: docker.py    From deimos with Apache License 2.0 5 votes vote down vote up
def observe(self, *args):
        log.info(" ".join(args))
        state = deimos.state.State(self.state_root, mesos_id=args[0])
        self.state = state
        deimos.sig.install(self.stop_docker_and_resume)
        state.await_launch()
        try:  # Take the wait lock to block calls to wait()
            state.lock("wait", LOCK_SH, seconds=None)
        except IOError as e:                       # Allows for signal recovery
            if e.errno != errno.EINTR:
                raise e
            state.lock("wait", LOCK_SH, seconds=1)
        if state.exit() is not None:
            return state.exit()
        raise Err("Wait lock is not held nor is exit file present") 
Example #29
Source File: flock.py    From deimos with Apache License 2.0 5 votes vote down vote up
def format_lock_flags(flags):
    tokens = [("EX", fcntl.LOCK_EX), ("SH", fcntl.LOCK_SH),
               ("UN", fcntl.LOCK_UN), ("NB", fcntl.LOCK_NB)]
    return "|".join(s for s, flag in tokens if (flags & flag) != 0) 
Example #30
Source File: state.py    From deimos with Apache License 2.0 5 votes vote down vote up
def await_launch(self):
        lk_l = self.lock("launch", LOCK_SH)
        self.ids(3)
        if self.cid() is None:
            lk_l.unlock()
            self.await_cid()
            lk_l = self.lock("launch", LOCK_SH)
        return lk_l