FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
ns_subprocess::ns_pipe Namespace Reference

Inter-process pipe management. More...

Functions

void write_pipe (pid_t child_pid, int pipe_fd, std::istream &stream)
 Write from an input stream to a pipe file descriptor.
 
void read_pipe (pid_t child_pid, int pipe_fd, std::ostream &stream, std::filesystem::path const &path_file_log)
 Read from a pipe file descriptor and write to an output stream.
 
template<typename Stream>
bool is_standard_stream (Stream &stream)
 Check if a stream is a standard stream (std::cin, std::cout, or std::cerr)
 
template<typename Stream>
void pipes_child (bool is_istream, int pipe[2], Stream &stream, int fileno)
 Setup pipe for child process (unified for stdin/stdout/stderr)
 
template<typename Stream>
std::optional< std::jthread > pipes_parent (pid_t child_pid, bool is_istream, int pipe[2], Stream &stream, std::filesystem::path const &path_file_log)
 Setup pipe for parent process (unified for stdin/stdout/stderr)
 
std::vector< std::jthread > setup (pid_t pid, int pipestdin[2], int pipestdout[2], int pipestderr[2], std::istream &stdin, std::ostream &stdout, std::ostream &stderr, std::filesystem::path const &path_file_log)
 Handle pipe setup for both parent and child processes.
 

Detailed Description

Inter-process pipe management.

Manages Unix pipe creation, configuration, and cleanup for inter-process communication. Handles file descriptor management with RAII semantics, read/write end access, and automatic closing on destruction for safe stdio redirection in child processes.

Function Documentation

◆ is_standard_stream()

template<typename Stream>
bool ns_subprocess::ns_pipe::is_standard_stream ( Stream & stream)

Check if a stream is a standard stream (std::cin, std::cout, or std::cerr)

Template Parameters
StreamThe stream type (std::istream or std::ostream)
Parameters
streamThe stream reference to check
Returns
true if the stream is std::cin, std::cout, or std::cerr

Definition at line 92 of file pipe.hpp.

Here is the caller graph for this function:

◆ pipes_child()

template<typename Stream>
void ns_subprocess::ns_pipe::pipes_child ( bool is_istream,
int pipe[2],
Stream & stream,
int fileno )

Setup pipe for child process (unified for stdin/stdout/stderr)

Template Parameters
StreamThe stream type (std::istream or std::ostream)
Parameters
is_istreamTrue for input streams (stdin), false for output streams (stdout/stderr)
pipePipe array [read_end, write_end]
streamThe stream reference to check
filenoThe file descriptor to redirect to (STDIN_FILENO, STDOUT_FILENO, or STDERR_FILENO)

Definition at line 115 of file pipe.hpp.

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

◆ pipes_parent()

template<typename Stream>
std::optional< std::jthread > ns_subprocess::ns_pipe::pipes_parent ( pid_t child_pid,
bool is_istream,
int pipe[2],
Stream & stream,
std::filesystem::path const & path_file_log )

Setup pipe for parent process (unified for stdin/stdout/stderr)

Template Parameters
StreamThe stream type (std::istream or std::ostream)
Parameters
child_pidPID of child process (needed for stdin writer)
is_istreamTrue for input streams (stdin), false for output streams (stdout/stderr)
pipePipe array [read_end, write_end]
streamThe stream reference to use
path_file_logOptional log file path (unused for stdin)
Returns
std::optional<std::jthread> The created thread, or std::nullopt if no thread was created

Definition at line 149 of file pipe.hpp.

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

◆ read_pipe()

void ns_subprocess::ns_pipe::read_pipe ( pid_t child_pid,
int pipe_fd,
std::ostream & stream,
std::filesystem::path const & path_file_log )
inline

Read from a pipe file descriptor and write to an output stream.

Reads data from the pipe and writes it to the output stream. Handles line splitting and filtering of empty/whitespace-only lines. Replaces carriage returns with newlines to handle Windows-style line endings and progress updates.

Parameters
child_pidPID of the child process to monitor
pipe_fdFile descriptor of the pipe read end
streamOutput stream to write to
path_file_logLog file path for logging output

Definition at line 72 of file pipe.hpp.

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

◆ setup()

std::vector< std::jthread > ns_subprocess::ns_pipe::setup ( pid_t pid,
int pipestdin[2],
int pipestdout[2],
int pipestderr[2],
std::istream & stdin,
std::ostream & stdout,
std::ostream & stderr,
std::filesystem::path const & path_file_log )
inline

Handle pipe setup for both parent and child processes.

This function manages the pipe setup after fork():

  • For parent (pid > 0): Creates threads for reading/writing pipes
  • For child (pid == 0): Redirects stdin/stdout/stderr to pipes via dup2()

Standard streams (std::cin, std::cout, std::cerr) are not redirected, allowing terminal access.

Parameters
pidProcess ID from fork() (0 for child, >0 for parent)
pipestdinStdin pipe array [read_end, write_end]
pipestdoutStdout pipe array [read_end, write_end]
pipestderrStderr pipe array [read_end, write_end]
stdinInput stream to read from (for child's stdin)
stdoutOutput stream to write to (for child's stdout)
stderrError stream to write to (for child's stderr)
path_file_logLog file path for pipe reader threads
Returns
std::vector<std::jthread> Vector of created threads (empty for child process)

Definition at line 204 of file pipe.hpp.

Here is the call graph for this function:

◆ write_pipe()

void ns_subprocess::ns_pipe::write_pipe ( pid_t child_pid,
int pipe_fd,
std::istream & stream )
inline

Write from an input stream to a pipe file descriptor.

Reads lines from the input stream and writes them to the pipe. Continues until the child process exits or stream errors occur.

Parameters
child_pidPID of the child process to monitor
pipe_fdFile descriptor of the pipe write end
streamInput stream to read from

Definition at line 53 of file pipe.hpp.

Here is the caller graph for this function: