Utility functions for processes.
Class |
|
Holds the result of running external programs. |
Function |
|
Close file descriptors. |
Function |
|
Daemonize the current process. |
Function |
|
Disables the use of fork(2). |
Function |
|
Returns the command line of a pid as a list of arguments. |
Function |
|
Determines whether a daemon is alive |
Function |
|
Check if a given pid exists on the system. |
Function |
|
Checks whether a process is handling a signal. |
Function |
|
Kill a process given by its pid. |
Function |
|
Execute a (shell) command. |
Function |
|
Runs a function in a separate process. |
Function |
|
Run Scripts or programs in a directory |
Function |
|
Setup a daemon's environment. |
Function |
|
Setups up a daemon's file descriptors. |
Function |
|
Start a daemon process after forking twice. |
Function |
|
Possibly write an error message to a fd. |
Function | _ |
Builds the environment for an external program. |
Function | _ |
Raises utils_retry.RetryAgain if child is still alive. |
Function | _ |
Retrieves a field from the contents of a proc status file. |
Function | _ |
Returns the path for a PID's proc status file. |
Function | _ |
Parse a rendered sigset_t value. |
Function | _ |
Run a command and save its output to a file. |
Function | _ |
Run a command and return its output. |
Function | _ |
Child process for starting daemon. |
Function | _ |
Waits for the child to terminate or until we reach timeout. |
Constant | _TIMEOUT |
Undocumented |
Constant | _TIMEOUT |
Undocumented |
Constant | _TIMEOUT |
Undocumented |
Variable | _no |
Undocumented |
Close file descriptors.
This closes all file descriptors above 2 (i.e. except stdin/out/err).
Parameters | |
noclose | if given, it denotes a list of file descriptor that should not be closed |
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 |
Returns the command line of a pid as a list of arguments.
Parameters | |
pid:int | Process ID |
Returns | |
list of string | Undocumented |
Raises | |
EnvironmentError | If the process does not exist |
Determines whether a daemon is alive
Parameters | |
name:string | daemon name |
Returns | |
boolean | True if daemon is running, False otherwise |
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 |
Checks whether a process is handling a signal.
Parameters | |
pid:int | Process ID |
signum:int | Signal number |
status | Undocumented |
Returns | |
bool | Undocumented |
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 |
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 | whether to reset or keep the default os environment |
interactive:boolean | whether 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 | list of additional (fd >=3) file descriptors to leave open for the child process |
input | File descriptor for process' standard input |
postfork | Callback run after fork but before timeout |
Returns | |
RunResult | RunResult instance |
Raises | |
errors.ProgrammerError | if we call this when forks are disabled |
Runs a function in a separate process.
Note: Only boolean return values are supported.
Parameters | |
fn:callable | Function to be called |
*args | Undocumented |
Returns | |
bool | Function's result |
Run Scripts or programs in a directory
Parameters | |
dir | absolute path to a directory |
env:dict | The environment to use |
reset | whether to reset or keep the default os environment |
Returns | |
list of tuples | list of (name, (one of RUNDIR_STATUS), RunResult) |
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 |
Setups up a daemon's file descriptors.
Parameters | |
output | if not None, the file to which to redirect stdout/stderr |
output | if not None, the file descriptor for stdout/stderr |
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 | File descriptor for output |
pidfile:string | Process ID file |
Returns | |
int | Daemon process ID |
Raises | |
errors.ProgrammerError | if we call this when forks are disabled |
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 |
Raises utils_retry.RetryAgain
if child is still alive.
Raises | |
utils_retry.RetryAgain | If child is still alive |
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 | Undocumented |
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 |
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 | 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 | list of additional (fd >=3) file descriptors to leave open for the child process |
Returns | |
int | the exit status |
Run a command and return its output.
Parameters | |
cmd:string or list | Command to run |
env:dict | The environment to use |
via | 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 | list of additional (fd >=3) file descriptors to leave open for the child process |
input | File descriptor for process' standard input |
postfork | Function run after fork but before timeout |
_linger | Undocumented |
Returns | |
tuple | (out, err, status) |
Child process for starting daemon.