|
FlatImage
A configurable Linux containerization system
|
Public Member Functions | |
| template<ns_concept::StringRepresentable T> | |
| Subprocess (T &&t) | |
| Construct a new Subprocess object with a program path. | |
| ~Subprocess () | |
| Destroy the Subprocess object. | |
| Subprocess (Subprocess const &)=delete | |
| Subprocess & | operator= (Subprocess const &)=delete |
| Subprocess (Subprocess &&)=delete | |
| Subprocess & | operator= (Subprocess &&)=delete |
| Subprocess & | env_clear () |
| Clears all environment variables before starting the process. | |
| template<ns_concept::StringRepresentable K, ns_concept::StringRepresentable V> | |
| Subprocess & | with_var (K &&k, V &&v) |
| Adds or replaces a single environment variable. | |
| template<ns_concept::StringRepresentable K> | |
| Subprocess & | rm_var (K &&k) |
| Removes an environment variable from the process environment. | |
| template<typename... Args> | |
| Subprocess & | with_args (Args &&... args) |
| Arguments forwarded as the process' arguments. | |
| template<typename... Args> | |
| Subprocess & | with_env (Args &&... args) |
| Includes environment variables with the format 'NAME=VALUE' in the environment. | |
| Subprocess & | with_die_on_pid (pid_t pid) |
| Configures the child process to die when the specified PID dies. | |
| Subprocess & | with_stdio (Stream mode) |
| Sets the stdio redirection mode for the child process. | |
| Subprocess & | with_log_file (std::filesystem::path const &path) |
| Configures logging output for child process stdout/stderr. | |
| Subprocess & | with_log_level (ns_log::Level const &level) |
| Sets the logging level for the pipe reader processes. | |
| Subprocess & | with_streams (std::istream &stdin_stream, std::ostream &stdout_stream, std::ostream &stderr_stream) |
| Configures stream handlers for stdin, stdout, and stderr of the child process. | |
| template<typename F> | |
| Subprocess & | with_callback_child (F &&f) |
| Sets a callback to run in the child process after fork() but before execve() | |
| template<typename F> | |
| Subprocess & | with_callback_parent (F &&f) |
| Sets a callback to run in the parent process after fork() | |
| Subprocess & | with_daemon () |
| Enable daemon mode using double fork pattern. | |
| std::unique_ptr< Child > | spawn () |
| Spawns (forks) the child process and begins execution. | |
Definition at line 108 of file subprocess.hpp.
|
nodiscard |
Construct a new Subprocess object with a program path.
Creates a subprocess object configured to run the specified program. The environment is inherited from the parent process by default.
| T | Type that is string representable |
| t | The program path or name to execute |
Definition at line 204 of file subprocess.hpp.
|
inline |
Destroy the Subprocess object.
Note: After spawn() is called, the Child handle takes ownership of the child process. The Subprocess destructor does not wait for the child since that responsibility has been transferred to Child.
Definition at line 243 of file subprocess.hpp.
|
inlinenodiscard |
Clears all environment variables before starting the process.
Removes all inherited environment variables, creating a clean slate. Useful for when you need precise control over the environment.
Definition at line 270 of file subprocess.hpp.
|
nodiscard |
Removes an environment variable from the process environment.
Deletes the specified environment variable if it exists. If the variable doesn't exist, this is a no-op (safe to call multiple times).
| K | Type that is string representable |
| k | The name of the variable to remove |
Definition at line 342 of file subprocess.hpp.
|
inlinenodiscard |
Spawns (forks) the child process and begins execution.
Creates a child process via fork() and executes the configured program. When daemon mode is enabled via with_daemon(), performs a double fork pattern to fully detach the process from the terminal and parent.
The parent process returns a Child handle immediately while the child runs asynchronously. Call wait() on the returned handle to synchronize and retrieve the exit code.
Use with_log_file() before spawn() to capture child's stdout/stderr to log files.
Definition at line 948 of file subprocess.hpp.
|
nodiscard |
Arguments forwarded as the process' arguments.
Accepts any number of arguments (including zero). Each argument can be:
| Args | Types of arguments (variadic, can be string, iterable, or string representable) |
| args | Arguments to forward to the process |
Definition at line 389 of file subprocess.hpp.
|
nodiscard |
Sets a callback to run in the child process after fork() but before execve()
The callback runs in the child process context, allowing you to for example:
IMPORTANT: The callback runs AFTER stdio redirection and die_on_pid setup, but BEFORE execve(). Any errors should use _exit() not exit() to avoid flushing parent's buffers.
| F | Function type compatible with std::function<void(ArgsCallbackChild)> |
| f | Callback function that receives ArgsCallbackChild |
Definition at line 812 of file subprocess.hpp.
|
inlinenodiscard |
Sets a callback to run in the parent process after fork()
The callback runs in the parent process context after the child has been forked, allowing you to:
The callback receives ArgsCallbackParent containing the child's PID.
IMPORTANT: The callback runs AFTER the child has been forked and BEFORE the Child handle is returned. Any exceptions or errors will prevent the Child handle from being created.
| F | Function type compatible with std::function<void(ArgsCallbackParent)> |
| f | Callback function that receives ArgsCallbackParent |
Definition at line 860 of file subprocess.hpp.
|
inlinenodiscard |
Enable daemon mode using double fork pattern.
When enabled, the spawn() method will perform a double fork:
This detaches the process from the terminal and parent process.
Definition at line 890 of file subprocess.hpp.
|
inlinenodiscard |
Configures the child process to die when the specified PID dies.
Uses prctl(PR_SET_PDEATHSIG) to ensure the child process receives SIGKILL when the parent process with the given PID terminates. This prevents orphaned processes and ensures cleanup even if the parent crashes.
| pid | The PID that the child should monitor (typically parent's PID) |
Definition at line 512 of file subprocess.hpp.
|
nodiscard |
Includes environment variables with the format 'NAME=VALUE' in the environment.
Accepts any number of environment variable entries (including zero). Each entry can be:
Automatically removes duplicate keys - if the same KEY appears multiple times, only the last value is kept.
| Args | Types of arguments (variadic, can be string, iterable, or string representable) |
| args | Environment variables to include (format: "KEY=VALUE") |
Definition at line 448 of file subprocess.hpp.
|
inlinenodiscard |
Configures logging output for child process stdout/stderr.
Automatically sets up pipe redirection (Stream::Pipe mode) and configures the log sink for the pipe reader threads. All child process output (stdout and stderr) is captured and logged to the specified file.
The child's stdout/stderr are redirected to pipes, and those pipes are read by detached threads that write directly to log files. This works even if the child process calls execve(), as the pipe readers remain in the parent.
The log file path is used directly by ns_log::set_sink_file() in the pipe reader threads.
If stdout/stderr stream handlers are registered via with_streams(), they will receive the child's output in addition to the log file.
| path | Path for log file (e.g., "/tmp/app.log") |
Definition at line 584 of file subprocess.hpp.
|
inlinenodiscard |
Sets the logging level for the pipe reader processes.
Configures the verbosity level for the pipe reader processes that capture the child's stdout/stderr. The log level determines which messages are written to the log files:
This is applied in the forked pipe reader processes (not the main child process), so it affects logging from the subprocess library's pipe handling mechanism.
| level | The logging level to set (ns_log::Level enum) |
Definition at line 628 of file subprocess.hpp.
|
inlinenodiscard |
Sets the stdio redirection mode for the child process.
Controls how the child's stdin, stdout, and stderr are handled:
| mode | Stream redirection mode |
Definition at line 550 of file subprocess.hpp.
|
inlinenodiscard |
Configures stream handlers for stdin, stdout, and stderr of the child process.
Sets up the stream redirection for all three standard I/O streams at once. The streams are used when Stream::Pipe mode is active via with_stdio(Stream::Pipe).
| stdin_stream | Input stream for the child process's stdin |
| stdout_stream | Output stream for the child process's stdout |
| stderr_stream | Output stream for the child process's stderr |
Definition at line 762 of file subprocess.hpp.
|
nodiscard |
Adds or replaces a single environment variable.
Sets an environment variable by key-value pair. If the key already exists, it is replaced with the new value. More convenient than with_env() when you have separate key and value.
| K | Type that is string representable for the key |
| V | Type that is string representable for the value |
| k | The name of the environment variable |
| v | The value of the environment variable |
Definition at line 308 of file subprocess.hpp.