libcosmos
Linux C++ System Programming Library
|
Sub process creation facility. More...
#include <ChildCloner.hxx>
Public Types | |
typedef std::function< void(const ChildCloner &)> | Callback |
callback function type used in setPostForkCB(). | |
Public Member Functions | |
ChildCloner ()=default | |
Creates an instance with default settings. | |
ChildCloner (const StringViewVector args) | |
Creates an instance configured with the provided arguments. | |
bool | hasExe () const |
Returns whether currently an executable is set. | |
auto & | getExe () |
Returns the currently set executable name. | |
const auto & | getExe () const |
void | setExe (const std::string_view exe) |
Sets the path to the executable and argv0. | |
const auto & | getArgs () const |
Returns the currently configured argument vector. | |
StringVector & | getArgs () |
void | setArgs (const StringVector &sv) |
Sets the argument vector to be used including argv0. | |
void | setArgsFromView (const StringViewVector &svv) |
void | clearArgs () |
Clears any currently set parameters. | |
void | setCWD (const std::string_view cwd) |
Set an explicit working directory the child process. | |
const auto & | getCWD () const |
Returns the currently set CWD for sub process execution. | |
void | setInheritCWD () |
Clear a previously configured CWD and inherit it from the parent. | |
void | setEnv (const StringVector &vars) |
Sets explicit environment variables for the child process. | |
void | setInheritEnv () |
Clears any previously set environment variables and let's to-be-started child processes inherit the parent's environment. | |
void | setStdErr (FileDescriptor fd) |
Redirect the child's stderr to the given file descriptor. | |
void | setStdOut (FileDescriptor fd) |
void | setStdIn (FileDescriptor fd) |
void | addInheritFD (FileDescriptor fd) |
Adds a file descriptor to inherit to the child process. | |
void | resetStdFiles () |
Restore the default inheritance behaviour for stdin/stderr/stdout. | |
template<typename SCHED_SETTING > | |
void | setSchedulerSettings (const SCHED_SETTING &ss) |
Sets scheduler type and settings. | |
void | setInheritSchedulerSettings () |
clear previously set scheduler settings and inherit them from the parent instead | |
void | setPostForkCB (Callback cb) |
Sets a callback function to be invoked in the child process context. | |
void | resetPostForkCB () |
Removes a previously stored post fork callback. | |
SubProc | run () |
Clone a new process and execute the currently configured program. | |
Protected Member Functions | |
void | postFork () |
Performs settings needed after forking i.e. in the child process but before exec()'ing. | |
void | resetSignals () |
restore a default signal mask in child process context. | |
void | redirectFD (FileDescriptor orig, FileDescriptor redirect) |
Redirects the given orig file descriptor to redirect (used in child context). | |
void | setArgv0 () |
sets argv0 from the current executable name. | |
void | setExeFromArgv0 () |
Protected Attributes | |
std::string | m_executable |
Path to the child process executable to run. | |
StringVector | m_argv |
Argument vector including argv0 denoting the executable name (which can be different than m_executable. | |
std::string | m_cwd |
Path to an explicit working directory, if any. | |
std::optional< StringVector > | m_env |
Explicit environment child environment variables, if any. | |
std::optional< SchedulerSettingsVariant > | m_sched_settings |
Scheduler policy settings, if any. | |
FileDescriptor | m_stdout |
File descriptor to use as child's stdin. | |
FileDescriptor | m_stderr |
File descriptor to use as child's stderr. | |
FileDescriptor | m_stdin |
File descriptor to use as child's stdin. | |
std::vector< FileDescriptor > | m_inherit_fds |
Additional file descriptors to inherit to the child process. | |
Callback | m_post_fork_cb = nullptr |
Friends | |
std::ostream & | operator<< (std::ostream &o, const ChildCloner &proc) |
Outputs a summary of the ChildCloner's configuration. | |
Sub process creation facility.
This type allows to configure and create child processes. This is a rather heavy weight type that can be reused to create multiple child processes. The SubProc type returned from wait() is rather lightweight in contrast.
By default, created child processes will inherit the current process's stdout, stderr and stdin file descriptors. You can redirect the child's stdout, stderr and stdin file descriptors via the setStdErr(), setStdOut() and setStdIn() member functions. It is expected that all file descriptors used have the O_CLOEXEC flag set. The implementation will take care to unset this flag appropriately in a manner that allows the file descriptors to be inherited to the child but at the same time won't influence other threads in the current process (to avoid races if multiple threads invoke clone()).
Furthermore the child's environment variables, current working directory, scheduling policy and command line arguments can be configured.
For advanced usage a post fork callback can be installed that performs actions before the child process is replaced by the new target executable.
Definition at line 40 of file ChildCloner.hxx.
std::function<void (const ChildCloner&)> cosmos::ChildCloner::Callback |
callback function type used in setPostForkCB().
Definition at line 44 of file ChildCloner.hxx.
|
inlineexplicit |
Creates an instance configured with the provided arguments.
This is a convenience constructor for simple execution of child processes without special settings. The executable path is taken from args[0]
.
Definition at line 57 of file ChildCloner.hxx.
|
inline |
Adds a file descriptor to inherit to the child process.
Beyond the stdin, stdout and stderr file descriptor additional descriptors can be inherited into the child process context. The fd
should have the O_CLOEXEC flag set. The implementation will adjust this flag appropriately to allow the fd
to be inherited across execution of the new child process image.
The file descriptor number of fd
will not be change in the child process. Therefore it must not be number 0, 1 or 2 (stdin, stdout, stderr), since these are already covered by the setStdErr(), setStdOut() and setStdIn() functions.
The ownership of fd
remains with the caller. The caller must ensure that the file descriptor stays valid until run() is invoked. Otherwise the child process execution / descriptor inheritance will fail. The implementation will not alter the fd
in the current process's context.
The child process must be instructed which FD to use and for which purpose. Some programs support command line arguments or evaluate environment variables to get this knowledge. Some programs may also be hardcoded to use certain file descriptor numbers.
Definition at line 184 of file ChildCloner.hxx.
|
inline |
Clears any currently set parameters.
Clears all currently set arguments but keeps the executable and argv0.
Definition at line 113 of file ChildCloner.hxx.
|
inline |
|
inline |
Returns the currently configured argument vector.
This vector is by convention including the executable name as first argument (argv0). You may change this argument using this function for special use cases (e.g. programs that behave differently depending on argv0).
Definition at line 85 of file ChildCloner.hxx.
|
inline |
Returns the currently set CWD for sub process execution.
Definition at line 127 of file ChildCloner.hxx.
|
inline |
Returns the currently set executable name.
Definition at line 65 of file ChildCloner.hxx.
|
inline |
Definition at line 66 of file ChildCloner.hxx.
|
inline |
|
protected |
Performs settings needed after forking i.e. in the child process but before exec()'ing.
Definition at line 195 of file ChildCloner.cxx.
|
protected |
Redirects the given orig
file descriptor to redirect
(used in child context).
[in] | orig | The file descriptor that should be replaced by redirect |
Definition at line 227 of file ChildCloner.cxx.
|
inline |
Removes a previously stored post fork callback.
Definition at line 232 of file ChildCloner.hxx.
|
protected |
restore a default signal mask in child process context.
Definition at line 186 of file ChildCloner.cxx.
|
inline |
Restore the default inheritance behaviour for stdin/stderr/stdout.
Any previously set file descriptor overrides will be reset and the child process will inherit the parent process's std file descriptors.
Definition at line 197 of file ChildCloner.hxx.
SubProc cosmos::ChildCloner::run | ( | ) |
Clone a new process and execute the currently configured program.
All settings made via member functions will come into effect. The configured executable will be invoked and passed the configured arguments.
The returned object is a move-only type that can be used to control the new sub process, communicate with it and evaluate its exit state.
It is mandatory to join the child process via SubProc::wait() before the SubProc object is destroyed.
Definition at line 77 of file ChildCloner.cxx.
|
inline |
Sets the argument vector to be used including argv0.
This also sets a new executable path from sv[0], or clears the executable, if sv
is empty.
Definition at line 95 of file ChildCloner.hxx.
|
inline |
Definition at line 101 of file ChildCloner.hxx.
|
inlineprotected |
sets argv0 from the current executable name.
Definition at line 264 of file ChildCloner.hxx.
|
inline |
Set an explicit working directory the child process.
If cwd
is empty then the parent process's CWD is inherited to the child.
Definition at line 124 of file ChildCloner.hxx.
|
inline |
Sets explicit environment variables for the child process.
By default the parent process's environment is inherited to the child (see also setInheritEnv()).
Each entry in the provided vector should be of the form "name=value". The provided variables will make up the complete child process environment.
Definition at line 141 of file ChildCloner.hxx.
|
inline |
Sets the path to the executable and argv0.
The actual executable path and argv0 will always be the same. You can change argv0, if necessary via getArgs().
Definition at line 73 of file ChildCloner.hxx.
|
inlineprotected |
Definition at line 271 of file ChildCloner.hxx.
|
inline |
Clear a previously configured CWD and inherit it from the parent.
Definition at line 130 of file ChildCloner.hxx.
|
inline |
Clears any previously set environment variables and let's to-be-started child processes inherit the parent's environment.
Definition at line 145 of file ChildCloner.hxx.
|
inline |
clear previously set scheduler settings and inherit them from the parent instead
Definition at line 213 of file ChildCloner.hxx.
|
inline |
Sets a callback function to be invoked in the child process context.
This function will be invoked in the child process after the clone happened but before the new program is executed. It can be used to perform custom child process setup, but care should be taken not to interfere with the SubProc's internal child process setup.
This callback is invoked before any redirections or other settings are performed by the implementation.
Be aware that any exceptions thrown from this callback will prevent the child process from executing, but you will not be notified about this apart from premature exit of the child process.
Definition at line 229 of file ChildCloner.hxx.
|
inline |
Sets scheduler type and settings.
By default the parent's scheduling settings will be inherited. If you want to explicitly change scheduling settings then apply the appropriate settings here.
Definition at line 210 of file ChildCloner.hxx.
|
inline |
Redirect the child's stderr to the given file descriptor.
This only affects yet to be started child processes. The file descriptor is expected to have the close-on-exec flag set, the inheritance to the child process will be performed appropriately by the implementation.
Definition at line 154 of file ChildCloner.hxx.
|
inline |
|
inline |
|
friend |
Outputs a summary of the ChildCloner's configuration.
Definition at line 236 of file ChildCloner.cxx.
|
protected |
Argument vector including argv0 denoting the executable name (which can be different than m_executable.
Definition at line 283 of file ChildCloner.hxx.
|
protected |
Path to an explicit working directory, if any.
Definition at line 285 of file ChildCloner.hxx.
|
protected |
Explicit environment child environment variables, if any.
Definition at line 287 of file ChildCloner.hxx.
|
protected |
Path to the child process executable to run.
Definition at line 281 of file ChildCloner.hxx.
|
protected |
Additional file descriptors to inherit to the child process.
Definition at line 298 of file ChildCloner.hxx.
|
protected |
Definition at line 300 of file ChildCloner.hxx.
|
protected |
Scheduler policy settings, if any.
Definition at line 289 of file ChildCloner.hxx.
|
protected |
File descriptor to use as child's stderr.
Definition at line 294 of file ChildCloner.hxx.
|
protected |
File descriptor to use as child's stdin.
Definition at line 296 of file ChildCloner.hxx.
|
protected |
File descriptor to use as child's stdin.
Definition at line 292 of file ChildCloner.hxx.