portage.process module

portage.process._configure_loopback_interface()

Configure the loopback interface.

class portage.process._dummy_list(iterable=(), /)

Bases: list

append(object, /)

Append object to the end of the list.

clear()

Remove all items from list.

copy()

Return a shallow copy of the list.

count(value, /)

Return number of occurrences of value.

extend(iterable, /)

Extend list by appending elements from the iterable.

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

insert(index, object, /)

Insert object before index.

pop(index=- 1, /)

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

remove(item)

Remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

Reverse IN PLACE.

sort(*, key=None, reverse=False)

Sort the list in ascending order and return None.

The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).

If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.

The reverse flag can be set to sort in descending order.

portage.process._exec(binary, mycommand, opt_name, fd_pipes, env, gid, groups, uid, umask, cwd, pre_exec, close_fds, unshare_net, unshare_ipc, unshare_mount, unshare_pid, unshare_flags, cgroup)

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 (List) – 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)

  • cwd (String) – Current working directory

  • 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

  • unshare_mount (Boolean) – If True, mount namespace will be unshared and mounts will be private to the namespace

  • unshare_pid (Boolean) – If True, PID ns will be unshared from the spawned process

  • unshare_flags (Integer) – Flags for the unshare(2) function

  • cgroup (String) – CGroup path to bind the process to

Return type

None

Returns

Never returns (calls os.execve)

portage.process._has_ipv6()

Test that both userland and kernel support IPv6, by attempting to create a socket and listen on any unused port of the IPv6 ::1 loopback address.

Return type

bool

Returns

True if IPv6 is supported, False otherwise.

portage.process._setup_pipes(fd_pipes, close_fds=True, inheritable=None)

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.

class portage.process._unshare_validator

Bases: object

In order to prevent failed unshare calls from corrupting the state of an essential process, validate the relevant unshare call in a short-lived subprocess. An unshare call is considered valid if it successfully executes in a short-lived subprocess.

static _run_subproc(subproc_pipe, target, args=(), kwargs={})

Call function and send return value to parent process.

Parameters
  • subproc_pipe (multiprocessing.Connection) – connection to parent process

  • target (callable) – target is the callable object to be invoked

  • args (tuple) – the argument tuple for the target invocation

  • kwargs (dict) – dictionary of keyword arguments for the target invocation

classmethod _validate(flags)

Perform validation.

Parameters

flags (int) – unshare flags

Return type

int

Returns

errno value, or 0 if no error occurred.

static _validate_subproc(unshare, flags)

Perform validation. Calls to this method must be isolated in a subprocess, since the unshare function is called for purposes of validation.

Parameters
  • unshare (callable) – unshare function

  • flags (int) – unshare flags

Return type

int

Returns

errno value, or 0 if no error occurred.

portage.process.atexit_register(func, *args, **kargs)

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.

portage.process.cleanup()
portage.process.find_binary(binary)

Given a binary name, find the binary in PATH

Parameters

binary – Name of the binary to find

:type string :rtype: None or string :return: full path to binary or None if the binary could not be located.

portage.process.get_open_fds()
portage.process.run_exitfuncs()

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).

portage.process.sanitize_fds()

Set the inheritable flag to False for all open file descriptors, except for those corresponding to stdin, stdout, and stderr. This ensures that any unintentionally inherited file descriptors will not be inherited by child processes.

portage.process.spawn(mycommand, env=None, opt_name=None, fd_pipes=None, returnpid=False, uid=None, gid=None, groups=None, umask=None, cwd=None, logfile=None, path_lookup=True, pre_exec=None, close_fds=False, unshare_net=False, unshare_ipc=False, unshare_mount=False, unshare_pid=False, cgroup=None)

Spawns a given command.

Parameters
  • mycommand (String or List (Popen style list)) – the command to execute

  • env (None or Mapping) – If env is not None, it must be a mapping that defines the environment variables for the new process; these are used instead of the default behavior of inheriting the current process’s environment.

  • opt_name (String) – an optional name for the spawn’d process (defaults to the binary name)

  • fd_pipes (Dictionary) – A dict of mapping for pipes, { ‘0’: stdin, ‘1’: stdout } for example (default is {0:stdin, 1:stdout, 2:stderr})

  • 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 cwd: Current working directory :type cwd: String :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 for python3.3 and earlier, and False for python3.4 and later due to non-inheritable file descriptor behavior from PEP 446).

Parameters
  • 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

  • unshare_mount (Boolean) – If True, mount namespace will be unshared and mounts will be private to the namespace

  • unshare_pid (Boolean) – If True, PID ns will be unshared from the spawned process

  • cgroup (String) – CGroup path to bind the process to

logfile requires stdout and stderr to be assigned to this process (ie not pointed

somewhere else.)

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

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

portage.process.spawn_fakeroot(mycommand, fakeroot_state=None, opt_name=None, **keywords)
portage.process.spawn_sandbox(mycommand, opt_name=None, **keywords)