process-cpp 3.0.0
A simple convenience library for handling processes in C++11.
core::posix Namespace Reference

Namespaces

namespace  backtrace
 
namespace  exit
 
namespace  linux
 
namespace  this_process
 
namespace  wait
 

Classes

class  ChildProcess
 The Process class models a child process of this process. More...
 
class  Process
 The Process class models a process and possible operations on it. More...
 
class  ProcessGroup
 The ProcessGroup class models a signalable group of process. More...
 
class  Signalable
 The Signalable class abstracts the ability of an entity to be delivered a posix signal. More...
 
class  SignalTrap
 The SignalTrap class encapsulates functionality to trap and handle signals. More...
 

Enumerations

enum class  Signal {
  unknown = 0 ,
  sig_hup = SIGHUP ,
  sig_int = SIGINT ,
  sig_quit = SIGQUIT ,
  sig_ill = SIGILL ,
  sig_abrt = SIGABRT ,
  sig_fpe = SIGFPE ,
  sig_kill = SIGKILL ,
  sig_segv = SIGSEGV ,
  sig_pipe = SIGPIPE ,
  sig_alrm = SIGALRM ,
  sig_term = SIGTERM ,
  sig_usr1 = SIGUSR1 ,
  sig_usr2 = SIGUSR2 ,
  sig_chld = SIGCHLD ,
  sig_cont = SIGCONT ,
  sig_stop = SIGSTOP ,
  sig_tstp = SIGTSTP ,
  sig_ttin = SIGTTIN ,
  sig_ttou = SIGTTOU
}
 The Signal enum collects the most common POSIX signals. More...
 
enum class  StandardStream : std::uint8_t {
  empty = 0 ,
  stdin = 1 << 0 ,
  stdout = 1 << 1 ,
  stderr = 1 << 2
}
 The StandardStream enum wraps the POSIX standard streams. More...
 

Functions

CORE_POSIX_DLL_PUBLIC ChildProcess exec (const std::string &fn, const std::vector< std::string > &argv, const std::map< std::string, std::string > &env, const StandardStream &flags)
 exec execve's the executable with the provided arguments and environment.
 
CORE_POSIX_DLL_PUBLIC ChildProcess exec (const std::string &fn, const std::vector< std::string > &argv, const std::map< std::string, std::string > &env, const StandardStream &flags, const std::function< void()> &child_setup)
 exec execve's the executable with the provided arguments and environment.
 
CORE_POSIX_DLL_PUBLIC ChildProcess fork (const std::function< posix::exit::Status()> &main, const StandardStream &flags)
 fork forks a new process and executes the provided main function in the newly forked process.
 
CORE_POSIX_DLL_PUBLIC ChildProcess vfork (const std::function< posix::exit::Status()> &main, const StandardStream &flags)
 fork vforks a new process and executes the provided main function in the newly forked process.
 
CORE_POSIX_DLL_PUBLIC std::shared_ptr< SignalTraptrap_signals_for_process (std::initializer_list< core::posix::Signal > blocked_signals)
 Traps the specified signals for the entire process.
 
CORE_POSIX_DLL_PUBLIC std::shared_ptr< SignalTraptrap_signals_for_all_subsequent_threads (std::initializer_list< core::posix::Signal > blocked_signals)
 Traps the specified signals for the current thread, and inherits the respective signal mask to all child-threads.
 
CORE_POSIX_DLL_PUBLIC StandardStream operator| (StandardStream l, StandardStream r)
 
CORE_POSIX_DLL_PUBLIC StandardStream operator& (StandardStream l, StandardStream r)
 
bool is_child (pid_t pid)
 

Enumeration Type Documentation

◆ Signal

The Signal enum collects the most common POSIX signals.

Enumerator
unknown 
sig_hup 
sig_int 
sig_quit 
sig_ill 
sig_abrt 
sig_fpe 
sig_kill 
sig_segv 
sig_pipe 
sig_alrm 
sig_term 
sig_usr1 
sig_usr2 
sig_chld 
sig_cont 
sig_stop 
sig_tstp 
sig_ttin 
sig_ttou 

Definition at line 38 of file signal.h.

◆ StandardStream

enum class core::posix::StandardStream : std::uint8_t
strong

The StandardStream enum wraps the POSIX standard streams.

Enumerator
empty 
stdin 
stdout 
stderr 

Definition at line 38 of file standard_stream.h.

Function Documentation

◆ exec() [1/2]

ChildProcess core::posix::exec ( const std::string & fn,
const std::vector< std::string > & argv,
const std::map< std::string, std::string > & env,
const StandardStream & flags )

exec execve's the executable with the provided arguments and environment.

Exceptions
std::system_errorin case of errors.
Parameters
fnThe executable to run.
argvVector of command line arguments
envEnvironment that the new process should run under
flagsSpecifies which standard streams should be redirected.
Returns
An instance of ChildProcess corresponding to the newly exec'd process.

Definition at line 33 of file exec.cpp.

References exec().

Referenced by exec(), TEST(), TEST(), TEST(), and TEST().

+ Here is the call graph for this function:

◆ exec() [2/2]

ChildProcess core::posix::exec ( const std::string & fn,
const std::vector< std::string > & argv,
const std::map< std::string, std::string > & env,
const StandardStream & flags,
const std::function< void()> & child_setup )

exec execve's the executable with the provided arguments and environment.

Exceptions
std::system_errorin case of errors.
Parameters
fnThe executable to run.
argvVector of command line arguments
envEnvironment that the new process should run under
flagsSpecifies which standard streams should be redirected.
child_setupFunction to run in the child just before exec().
Returns
An instance of ChildProcess corresponding to the newly exec'd process.

Definition at line 42 of file exec.cpp.

References fork().

+ Here is the call graph for this function:

◆ fork()

ChildProcess core::posix::fork ( const std::function< posix::exit::Status()> & main,
const StandardStream & flags )

fork forks a new process and executes the provided main function in the newly forked process.

Exceptions
std::system_errorin case of errors.
Parameters
[in]mainThe main function of the newly forked process.
[in]flagsSpecify which standard streams should be redirected to the parent process.
Returns
An instance of ChildProcess in case of success.

Definition at line 57 of file fork.cpp.

Referenced by exec(), core::testing::fork_and_run(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST_F().

◆ is_child()

bool core::posix::is_child ( pid_t pid)

Definition at line 55 of file fork.cpp.

◆ operator&()

◆ operator|()

◆ trap_signals_for_all_subsequent_threads()

std::shared_ptr< core::posix::SignalTrap > core::posix::trap_signals_for_all_subsequent_threads ( std::initializer_list< core::posix::Signal > blocked_signals)

Traps the specified signals for the current thread, and inherits the respective signal mask to all child-threads.

Definition at line 210 of file signal.cpp.

References impl::SignalTrap::thread.

Referenced by TESTP().

◆ trap_signals_for_process()

std::shared_ptr< core::posix::SignalTrap > core::posix::trap_signals_for_process ( std::initializer_list< core::posix::Signal > blocked_signals)

Traps the specified signals for the entire process.

Definition at line 202 of file signal.cpp.

References impl::SignalTrap::process.

◆ vfork()

ChildProcess core::posix::vfork ( const std::function< posix::exit::Status()> & main,
const StandardStream & flags )

fork vforks a new process and executes the provided main function in the newly forked process.

Exceptions
std::system_errorin case of errors.
Parameters
[in]mainThe main function of the newly forked process.
[in]flagsSpecify which standard streams should be redirected to the parent process.
Returns
An instance of ChildProcess in case of success.

Definition at line 125 of file fork.cpp.