Package ganeti :: Package utils :: Module process
[hide private]
[frames] | no frames]

Module process

source code

Utility functions for processes.

Classes [hide private]
  RunResult
Holds the result of running external programs.
Functions [hide private]
 
DisableFork()
Disables the use of fork(2).
source code
 
_BuildCmdEnvironment(env, reset)
Builds the environment for an external program.
source code
RunResult
RunCmd(cmd, env=None, output=None, cwd="/", reset_env=False, interactive=False, timeout=None, noclose_fds=None)
Execute a (shell) command.
source code
 
SetupDaemonEnv(cwd="/", umask=077)
Setup a daemon's environment.
source code
 
SetupDaemonFDs(output_file, output_fd)
Setups up a daemon's file descriptors.
source code
int
StartDaemon(cmd, env=None, cwd="/", output=None, output_fd=None, pidfile=None)
Start a daemon process after forking twice.
source code
 
_StartDaemonChild(errpipe_read, errpipe_write, pidpipe_read, pidpipe_write, args, env, cwd, output, fd_output, pidfile)
Child process for starting daemon.
source code
 
WriteErrorToFD(fd, err)
Possibly write an error message to a fd.
source code
 
_CheckIfAlive(child)
Raises utils_retry.RetryAgain if child is still alive.
source code
 
_WaitForProcess(child, timeout)
Waits for the child to terminate or until we reach timeout.
source code
tuple
_RunCmdPipe(cmd, env, via_shell, cwd, interactive, timeout, noclose_fds, _linger_timeout=constants.CHILD_LINGER_TIMEOUT)
Run a command and return its output.
source code
int
_RunCmdFile(cmd, env, via_shell, output, cwd, noclose_fds)
Run a command and save its output to a file.
source code
list of tuples
RunParts(dir_name, env=None, reset_env=False)
Run Scripts or programs in a directory
source code
string
_GetProcStatusPath(pid)
Returns the path for a PID's proc status file.
source code
boolean
IsProcessAlive(pid)
Check if a given pid exists on the system.
source code
set
_ParseSigsetT(sigset)
Parse a rendered sigset_t value.
source code
string
_GetProcStatusField(pstatus, field)
Retrieves a field from the contents of a proc status file.
source code
bool
IsProcessHandlingSignal(pid, signum, status_path=None)
Checks whether a process is handling a signal.
source code
tuple; (int, callable)
Daemonize(logfile)
Daemonize the current process.
source code
 
KillProcess(pid, signal_=signal.SIGTERM, timeout=30, waitpid=False)
Kill a process given by its pid.
source code
bool
RunInSeparateProcess(fn, *args)
Runs a function in a separate process.
source code
 
CloseFDs(noclose_fds=None)
Close file descriptors.
source code
Variables [hide private]
  _no_fork = False
when set to True, RunCmd is disabled

Imports: os, sys, subprocess, errno, select, logging, signal, resource, StringIO, errors, constants, compat, utils_retry, utils_wrapper, utils_text, utils_io, utils_algo


Function Details [hide private]

RunCmd(cmd, env=None, output=None, cwd="/", reset_env=False, interactive=False, timeout=None, noclose_fds=None)

source code 

Execute a (shell) command.

The command should not read from its standard input, as it will be closed.

Parameters:
  • cmd (string or list) - Command to run
  • env (dict) - Additional environment variables
  • output (str) - if desired, the output of the command can be saved in a file instead of the RunResult instance; this parameter denotes the file name (if not None)
  • cwd (string) - if specified, will be used as the working directory for the command; the default will be /
  • reset_env (boolean) - whether to reset or keep the default os environment
  • interactive (boolean) - weather we pipe stdin, stdout and stderr (default behaviour) or run the command interactive
  • timeout (int) - If not None, timeout in seconds until child process gets killed
  • noclose_fds (list) - list of additional (fd >=3) file descriptors to leave open for the child process
Returns: RunResult
RunResult instance
Raises:

SetupDaemonEnv(cwd="/", umask=077)

source code 

Setup a daemon's environment.

This should be called between the first and second fork, due to setsid usage.

Parameters:
  • cwd - the directory to which to chdir
  • umask - the umask to setup

SetupDaemonFDs(output_file, output_fd)

source code 

Setups up a daemon's file descriptors.

Parameters:
  • output_file - if not None, the file to which to redirect stdout/stderr
  • output_fd - if not None, the file descriptor for stdout/stderr

StartDaemon(cmd, env=None, cwd="/", output=None, output_fd=None, pidfile=None)

source code 

Start a daemon process after forking twice.

Parameters:
  • cmd (string or list) - Command to run
  • env (dict) - Additional environment variables
  • cwd (string) - Working directory for the program
  • output (string) - Path to file in which to save the output
  • output_fd (int) - File descriptor for output
  • pidfile (string) - Process ID file
Returns: int
Daemon process ID
Raises:

WriteErrorToFD(fd, err)

source code 

Possibly write an error message to a fd.

Parameters:
  • fd (None or int (file descriptor)) - if not None, the error will be written to this fd
  • err - string, the error message

_CheckIfAlive(child)

source code 

Raises utils_retry.RetryAgain if child is still alive.

Raises:

_RunCmdPipe(cmd, env, via_shell, cwd, interactive, timeout, noclose_fds, _linger_timeout=constants.CHILD_LINGER_TIMEOUT)

source code 

Run a command and return its output.

Parameters:
  • cmd (string or list) - Command to run
  • env (dict) - The environment to use
  • via_shell (bool) - if we should run via the shell
  • cwd (string) - the working directory for the program
  • interactive (boolean) - Run command interactive (without piping)
  • timeout (int) - Timeout after the programm gets terminated
  • noclose_fds (list) - list of additional (fd >=3) file descriptors to leave open for the child process
Returns: tuple
(out, err, status)

_RunCmdFile(cmd, env, via_shell, output, cwd, noclose_fds)

source code 

Run a command and save its output to a file.

Parameters:
  • cmd (string or list) - Command to run
  • env (dict) - The environment to use
  • via_shell (bool) - if we should run via the shell
  • output (str) - the filename in which to save the output
  • cwd (string) - the working directory for the program
  • noclose_fds (list) - list of additional (fd >=3) file descriptors to leave open for the child process
Returns: int
the exit status

RunParts(dir_name, env=None, reset_env=False)

source code 

Run Scripts or programs in a directory

Parameters:
  • dir_name (string) - absolute path to a directory
  • env (dict) - The environment to use
  • reset_env (boolean) - whether to reset or keep the default os environment
Returns: list of tuples
list of (name, (one of RUNDIR_STATUS), RunResult)

_GetProcStatusPath(pid)

source code 

Returns the path for a PID's proc status file.

Parameters:
  • pid (int) - Process ID
Returns: string

IsProcessAlive(pid)

source code 

Check if a given pid exists on the system.

Parameters:
  • pid (int) - the process ID to check
Returns: boolean
True if the process exists

Note: zombie status is not handled, so zombie processes will be returned as alive

_ParseSigsetT(sigset)

source code 

Parse a rendered sigset_t value.

This is the opposite of the Linux kernel's fs/proc/array.c:render_sigset_t function.

Parameters:
  • sigset (string) - Rendered signal set from /proc/$pid/status
Returns: set
Set of all enabled signal numbers

_GetProcStatusField(pstatus, field)

source code 

Retrieves a field from the contents of a proc status file.

Parameters:
  • pstatus (string) - Contents of /proc/$pid/status
  • field (string) - Name of field whose value should be returned
Returns: string

IsProcessHandlingSignal(pid, signum, status_path=None)

source code 

Checks whether a process is handling a signal.

Parameters:
  • pid (int) - Process ID
  • signum (int) - Signal number
Returns: bool

Daemonize(logfile)

source code 

Daemonize the current process.

This detaches the current process from the controlling terminal and runs it in the background as a daemon.

Parameters:
  • logfile (str) - the logfile to which we should redirect stdout/stderr
Returns: tuple; (int, callable)
File descriptor of pipe(2) which must be closed to notify parent process and a callable to reopen log files

KillProcess(pid, signal_=signal.SIGTERM, timeout=30, waitpid=False)

source code 

Kill a process given by its pid.

Parameters:
  • pid (int) - The PID to terminate.
  • signal_ (int) - The signal to send, by default SIGTERM
  • timeout (int) - The timeout after which, if the process is still alive, a SIGKILL will be sent. If not positive, no such checking will be done
  • waitpid (boolean) - If true, we should waitpid on this process after sending signals, since it's our own child and otherwise it would remain as zombie

RunInSeparateProcess(fn, *args)

source code 

Runs a function in a separate process.

Note: Only boolean return values are supported.

Parameters:
  • fn (callable) - Function to be called
Returns: bool
Function's result

CloseFDs(noclose_fds=None)

source code 

Close file descriptors.

This closes all file descriptors above 2 (i.e. except stdin/out/err).

Parameters:
  • noclose_fds (list or None) - if given, it denotes a list of file descriptor that should not be closed