Package portage :: Module process
[hide private]

Module process

source code

Classes [hide private]
  basestring
str(object='') -> string
  _dummy_list
Functions [hide private]
 
_get_open_fds() source code
 
get_open_fds() source code
 
spawn_bash(mycommand, debug=False, opt_name=None, **keywords)
Spawns a bash shell running a specific commands
source code
 
spawn_sandbox(mycommand, opt_name=None, **keywords) source code
 
spawn_fakeroot(mycommand, fakeroot_state=None, opt_name=None, **keywords) source code
 
atexit_register(func, *args, **kargs)
Wrapper around atexit.register that is needed in order to track what is registered.
source code
 
run_exitfuncs()
This should behave identically to the routine performed by the atexit module at exit time.
source code
 
cleanup() source code
 
spawn(mycommand, env={}, opt_name=None, fd_pipes=None, returnpid=False, uid=None, gid=None, groups=None, umask=None, logfile=None, path_lookup=True, pre_exec=None, close_fds=True, unshare_net=False, unshare_ipc=False, cgroup=None)
Spawns a given command.
source code
None
_exec(binary, mycommand, opt_name, fd_pipes, env, gid, groups, uid, umask, pre_exec, close_fds, unshare_net, unshare_ipc, cgroup)
Execute a given binary with options
source code
 
_setup_pipes(fd_pipes, close_fds=True, inheritable=None)
Setup pipes for a forked process.
source code
None or string
find_binary(binary)
Given a binary name, find the binary in PATH
source code
Variables [hide private]
  max_fd_limit = 1024
  _set_inheritable = None
hash(x)
  _FD_CLOEXEC = 1
  _fd_dir = '/proc/self/fd'
  sandbox_capable = True
  fakeroot_capable = True
  _exithandlers = []
  spawned_pids = []
  __package__ = 'portage'

Imports: atexit, errno, fcntl, platform, signal, socket, struct, sys, traceback, _os, os, _encodings, _unicode_encode, portage, BASH_BINARY, SANDBOX_BINARY, FAKEROOT_BINARY, CommandNotFound, find_library, LoadLibrary, ctypes, resource, dump_traceback, writemsg


Function Details [hide private]

spawn_bash(mycommand, debug=False, opt_name=None, **keywords)

source code 

Spawns a bash shell running a specific commands

Parameters:
  • mycommand (String) - The command for bash to run
  • debug (Boolean) - Turn bash debugging on (set -x)
  • opt_name (String) - Name of the spawned process (detaults to binary name)
  • keywords (Dictionary) - Extra Dictionary arguments to pass to spawn

atexit_register(func, *args, **kargs)

source code 

Wrapper around atexit.register that is needed in order to track what is registered. For example, when portage restarts itself via os.execv, the atexit module does not work so we have to do it manually by calling the run_exitfuncs() function in this module.

run_exitfuncs()

source code 

This should behave identically to the routine performed by the atexit module at exit time. It's only necessary to call this function when atexit will not work (because of os.execv, for example).

spawn(mycommand, env={}, opt_name=None, fd_pipes=None, returnpid=False, uid=None, gid=None, groups=None, umask=None, logfile=None, path_lookup=True, pre_exec=None, close_fds=True, unshare_net=False, unshare_ipc=False, cgroup=None)

source code 

Spawns a given command.

@param mycommand: the command to execute
@type mycommand: String or List (Popen style list)
@param env: A dict of Key=Value pairs for env variables
@type env: Dictionary
@param opt_name: an optional name for the spawn'd process (defaults to the binary name)
@type opt_name: String
@param fd_pipes: A dict of mapping for pipes, { '0': stdin, '1': stdout } for example
        (default is {0:stdin, 1:stdout, 2:stderr})
@type fd_pipes: Dictionary
@param returnpid: Return the Process IDs for a successful spawn.
NOTE: This requires the caller clean up all the PIDs, otherwise spawn will clean them.
@type returnpid: Boolean
@param uid: User ID to spawn as; useful for dropping privilages
@type uid: Integer
@param gid: Group ID to spawn as; useful for dropping privilages
@type gid: Integer
@param groups: Group ID's to spawn in: useful for having the process run in multiple group contexts.
@type groups: List
@param umask: An integer representing the umask for the process (see man chmod for umask details)
@type umask: Integer
@param logfile: name of a file to use for logging purposes
@type logfile: String
@param path_lookup: If the binary is not fully specified then look for it in PATH
@type path_lookup: Boolean
@param pre_exec: A function to be called with no arguments just prior to the exec call.
@type pre_exec: callable
@param close_fds: If True, then close all file descriptors except those
        referenced by fd_pipes (default is True).
@type close_fds: Boolean
@param unshare_net: If True, networking will be unshared from the spawned process
@type unshare_net: Boolean
@param unshare_ipc: If True, IPC will be unshared from the spawned process
@type unshare_ipc: Boolean
@param cgroup: CGroup path to bind the process to
@type cgroup: String

logfile requires stdout and stderr to be assigned to this process (ie not pointed
   somewhere else.)

_exec(binary, mycommand, opt_name, fd_pipes, env, gid, groups, uid, umask, pre_exec, close_fds, unshare_net, unshare_ipc, cgroup)

source code 

Execute a given binary with options

Parameters:
  • binary (String) - Name of program to execute
  • mycommand (String) - Options for program
  • opt_name (String) - Name of process (defaults to binary)
  • fd_pipes (Dictionary) - Mapping pipes to destination; { 0:0, 1:1, 2:2 }
  • env (Dictionary) - Key,Value mapping for Environmental Variables
  • gid (Integer) - Group ID to run the process under
  • groups (Integer) - Groups the Process should be in.
  • uid (Integer) - User ID to run the process under
  • umask (Integer) - an int representing a unix umask (see man chmod for umask details)
  • pre_exec (callable) - A function to be called with no arguments just prior to the exec call.
  • unshare_net (Boolean) - If True, networking will be unshared from the spawned process
  • unshare_ipc (Boolean) - If True, IPC will be unshared from the spawned process
  • cgroup (String) - CGroup path to bind the process to
Returns: None
Never returns (calls os.execve)

_setup_pipes(fd_pipes, close_fds=True, inheritable=None)

source code 
Setup pipes for a forked process.

Even when close_fds is False, file descriptors referenced as
values in fd_pipes are automatically closed if they do not also
occur as keys in fd_pipes. It is assumed that the caller will
explicitly add them to the fd_pipes keys if they are intended
to remain open. This allows for convenient elimination of
unnecessary duplicate file descriptors.

WARNING: When not followed by exec, the close_fds behavior
can trigger interference from destructors that close file
descriptors. This interference happens when the garbage
collector intermittently executes such destructors after their
corresponding file descriptors have been re-used, leading
to intermittent "[Errno 9] Bad file descriptor" exceptions in
forked processes. This problem has been observed with PyPy 1.8,
and also with CPython under some circumstances (as triggered
by xmpppy in bug #374335). In order to close a safe subset of
file descriptors, see portage.locks._close_fds().

NOTE: When not followed by exec, even when close_fds is False,
it's still possible for dup2() calls to cause interference in a
way that's similar to the way that close_fds interferes (since
dup2() has to close the target fd if it happens to be open).
It's possible to avoid such interference by using allocated
file descriptors as the keys in fd_pipes. For example:

        pr, pw = os.pipe()
        fd_pipes[pw] = pw

By using the allocated pw file descriptor as the key in fd_pipes,
it's not necessary for dup2() to close a file descriptor (it
actually does nothing in this case), which avoids possible
interference.

find_binary(binary)

source code 

Given a binary name, find the binary in PATH

Parameters:
  • binary - Name of the binary to find @type string
Returns: None or string
full path to binary or None if the binary could not be located.