FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
ns_subprocess::Child Class Reference

Handle to a spawned child process. More...

#include <child.hpp>

Public Member Functions

 ~Child ()
 Destructor automatically waits for the child process.
 
 Child (Child const &)=delete
 
Childoperator= (Child const &)=delete
 
 Child (Child &&)=delete
 
Childoperator= (Child &&)=delete
 
Value< int > wait ()
 Waits for the child process to finish and returns exit code.
 
std::optional< pid_t > get_pid () const
 Gets the PID of the child process.
 
void kill (int signal)
 Sends a signal to the child process.
 

Friends

class Subprocess
 

Detailed Description

Handle to a spawned child process.

This class represents a running child process and provides methods to wait for it and retrieve its exit code. Returned by Subprocess::spawn().

The Child class uses RAII to ensure proper cleanup - the destructor automatically waits for the child process if it hasn't been waited on explicitly. This prevents zombie processes.

Note
This class is non-copyable and non-moveable. It automatically waits for the process in its destructor.
// Basic usage - direct chaining
auto result = Subprocess("/bin/ls")
.with_args("-la")
.spawn()
->wait();

Definition at line 53 of file child.hpp.

Constructor & Destructor Documentation

◆ ~Child()

ns_subprocess::Child::~Child ( )
inline

Destructor automatically waits for the child process.

Ensures child processes are properly cleaned up even if wait() is not called. This prevents zombie processes and resource leaks.

{
auto child = Subprocess("/bin/sleep").with_args("5").spawn();
// Destructor automatically waits for sleep to finish
} // wait() called here

Definition at line 101 of file child.hpp.

Here is the call graph for this function:

Member Function Documentation

◆ get_pid()

std::optional< pid_t > ns_subprocess::Child::get_pid ( ) const
inlinenodiscard

Gets the PID of the child process.

Returns the process ID if the child is still valid (hasn't been waited on), or std::nullopt if wait() has already been called.

Returns
std::optional<pid_t> The PID if still running, nullopt otherwise
auto child = Subprocess("/bin/sleep").with_args("1").spawn();
if (auto pid = child->get_pid()) {
std::cout << "Child PID: " << *pid << "\n";
}
child->wait();
if (auto pid = child->get_pid()) {
// Won't execute - PID is invalid after wait
} else {
std::cout << "Process has finished\n";
}

Definition at line 202 of file child.hpp.

◆ kill()

void ns_subprocess::Child::kill ( int signal)
inline

Sends a signal to the child process.

Sends the specified signal to the child process if it's still valid. Does nothing if the process has already been waited on.

Parameters
signalThe signal number to send (e.g., SIGTERM, SIGKILL)
auto child = Subprocess("/bin/sleep").with_args("100").spawn();
// Let it run for a bit
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Terminate it early
child->kill(SIGTERM);
// Wait for cleanup
child->wait();

Definition at line 228 of file child.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ wait()

Value< int > ns_subprocess::Child::wait ( )
inlinenodiscard

Waits for the child process to finish and returns exit code.

Blocks until the child process terminates, then returns its exit code. Also cleans up any pipe reader processes created by Stream::Pipe mode. After wait() returns, the PID is invalidated (get_pid() returns nullopt).

Returns
Value<int> The exit code (typically 0-255) on success, or error message on failure. Exit code range depends on the child process (WEXITSTATUS returns low 8 bits).
// Check exit code
auto child = Subprocess("/bin/false").spawn();
auto result = child->wait();
if (result) {
std::cout << "Exit code: " << *result << "\n"; // Exit code: 1
} else {
std::cerr << "Error: " << result.error() << "\n";
}
// Success/failure handling
auto child = Subprocess("/usr/bin/make").with_args("all").spawn();
if (auto code = child->wait(); code && *code == 0) {
std::cout << "Build succeeded\n";
} else {
std::cerr << "Build failed\n";
}
// Multiple waits (safe but second returns error)
auto child = Subprocess("/bin/true").spawn();
auto result1 = child->wait(); // Returns exit code
auto result2 = child->wait(); // Returns error (already waited)

Definition at line 149 of file child.hpp.

Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ Subprocess

friend class Subprocess
friend

Definition at line 60 of file child.hpp.


The documentation for this class was generated from the following file: