Exemple #1
0
def WritePidFile(pidfile):
    """Write the current process pidfile.

  @type pidfile: string
  @param pidfile: the path to the file to be written
  @raise errors.LockError: if the pid file already exists and
      points to a live process
  @rtype: int
  @return: the file descriptor of the lock file; do not close this unless
      you want to unlock the pid file

  """
    # We don't rename nor truncate the file to not drop locks under
    # existing processes
    fd_pidfile = os.open(pidfile, os.O_RDWR | os.O_CREAT, 0o600)

    # Lock the PID file (and fail if not possible to do so). Any code
    # wanting to send a signal to the daemon should try to lock the PID
    # file before reading it. If acquiring the lock succeeds, the daemon is
    # no longer running and the signal should not be sent.
    try:
        filelock.LockFile(fd_pidfile)
    except errors.LockError:
        msg = ["PID file '%s' is already locked by another process" % pidfile]
        # Try to read PID file
        pid = _ParsePidFileContents(os.read(fd_pidfile, 100))
        if pid > 0:
            msg.append(", PID read from file is %s" % pid)
        raise errors.PidFileLockError("".join(msg))

    os.write(fd_pidfile, "%d\n" % os.getpid())

    return fd_pidfile
Exemple #2
0
def ReadLockedPidFile(path):
    """Reads a locked PID file.

  This can be used together with L{utils.process.StartDaemon}.

  @type path: string
  @param path: Path to PID file
  @return: PID as integer or, if file was unlocked or couldn't be opened, None

  """
    try:
        fd = os.open(path, os.O_RDONLY)
    except EnvironmentError as err:
        if err.errno == errno.ENOENT:
            # PID file doesn't exist
            return None
        raise

    try:
        try:
            # Try to acquire lock
            filelock.LockFile(fd)
        except errors.LockError:
            # Couldn't lock, daemon is running
            return int(os.read(fd, 100))
    finally:
        os.close(fd)

    return None
Exemple #3
0
def SafeWriteFile(file_name, file_id, **kwargs):
    """Wraper over L{WriteFile} that locks the target file.

  By keeping the target file locked during WriteFile, we ensure that
  cooperating writers will safely serialise access to the file.

  @type file_name: str
  @param file_name: the target filename
  @type file_id: tuple
  @param file_id: a result from L{GetFileID}

  """
    fd = os.open(file_name, os.O_RDONLY | os.O_CREAT)
    try:
        filelock.LockFile(fd)
        if file_id is not None:
            disk_id = GetFileID(fd=fd)
            if not VerifyFileID(disk_id, file_id):
                raise errors.LockError(
                    "Cannot overwrite file %s, it has been modified"
                    " since last written" % file_name)
        return WriteFile(file_name, **kwargs)
    finally:
        os.close(fd)
Exemple #4
0
  @param path: Path to PID file
  @return: PID as integer or, if file was unlocked or couldn't be opened, None

  """
    try:
        fd = os.open(path, os.O_RDONLY)
    except EnvironmentError, err:
        if err.errno == errno.ENOENT:
            # PID file doesn't exist
            return None
        raise

    try:
        try:
            # Try to acquire lock
            filelock.LockFile(fd)
        except errors.LockError:
            # Couldn't lock, daemon is running
            return int(os.read(fd, 100))
    finally:
        os.close(fd)

    return None


def _SplitSshKey(key):
    """Splits a line for SSH's C{authorized_keys} file.

  If the line has no options (e.g. no C{command="..."}), only the significant
  parts, the key type and its hash, are used. Otherwise the whole line is used
  (split at whitespace).