BoosterSeat  0.1
A C++ library that includes common utilities that are used in other projects.
bst::Process Class Reference

A class to handle linux process calls. More...

#include <process.hpp>

Collaboration diagram for bst::Process:

Public Member Functions

 Process (const std::string &cmd, std::vector< std::string > argv)
 
 ~Process ()
 
void closeInStream ()
 
int getExitCode ()
 
const std::string & getStderr ()
 
const std::string & getStdout ()
 
void waitToComplete (unsigned int timeout=1000)
 Waits for the process to complete. If the process does not complete within the timeout, a std::system_error is thrown. More...
 
void writeToInStream (const std::string &s)
 

Private Member Functions

void assertComplete ()
 
void child (const std::vector< std::string > &argv)
 
void run (const std::string &cmd, std::vector< std::string > argv)
 

Private Attributes

int err_pipe_ [2]
 The file descriptors for error pipe. More...
 
std::istream * err_stream_
 The error stream for the child process. More...
 
int exit_code_ = 0
 The exit code of the process. More...
 
bool have_exit_code_ = false
 Set to true when the process has completed and an exit code has been retrieved. This is used internally to check if the process has completed. More...
 
int in_pipe_ [2]
 The file descriptors for input pipe. More...
 
std::ostream * in_stream_
 The input stream for the child process. More...
 
int out_pipe_ [2]
 The file descriptors for output pipe. More...
 
std::istream * out_stream_
 The output stream for the child process. More...
 
__gnu_cxx::stdio_filebuf< char > * p_err_filebuf_
 The filebuf for error pipe. More...
 
__gnu_cxx::stdio_filebuf< char > * p_in_filebuf_
 The filebuf for input pipe. More...
 
__gnu_cxx::stdio_filebuf< char > * p_out_filebuf_
 The filebuf for output pipe. More...
 
pid_t pid_
 The process id of the child process. More...
 
std::string stderr_string_
 The stderr of the process. More...
 
std::string stdout_string_
 The stdout of the process. More...
 

Detailed Description

A class to handle linux process calls.

This is a rework of the project cpp-subprocess

Constructor & Destructor Documentation

◆ Process()

bst::Process::Process ( const std::string &  cmd,
std::vector< std::string >  argv 
)

References err_pipe_, in_pipe_, out_pipe_, and run().

Here is the call graph for this function:

◆ ~Process()

bst::Process::~Process ( )

Member Function Documentation

◆ writeToInStream()

void bst::Process::writeToInStream ( const std::string &  s)

References in_stream_.

Referenced by TEST().

Here is the caller graph for this function:

◆ closeInStream()

void bst::Process::closeInStream ( )

References p_in_filebuf_.

◆ getStdout()

const std::string & bst::Process::getStdout ( )

References assertComplete(), and stdout_string_.

Referenced by getOutput().

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

◆ getStderr()

const std::string & bst::Process::getStderr ( )

References assertComplete(), and stderr_string_.

Referenced by getOutput().

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

◆ getExitCode()

int bst::Process::getExitCode ( )

References assertComplete(), and exit_code_.

Referenced by getOutput().

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

◆ waitToComplete()

void bst::Process::waitToComplete ( unsigned int  timeout = 1000)

Waits for the process to complete. If the process does not complete within the timeout, a std::system_error is thrown.

Parameters
timeoutThe number of milliseconds to wait for the process to exit. Default is 1000ms.

References err_stream_, exit_code_, have_exit_code_, out_stream_, pid_, bst::PROCESS_TIMEOUT, bst::sleep(), stderr_string_, and stdout_string_.

Referenced by TEST().

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

◆ assertComplete()

void bst::Process::assertComplete ( )
private

References have_exit_code_, and bst::PROCESS_NOT_COMPLETE.

Referenced by getExitCode(), getStderr(), and getStdout().

Here is the caller graph for this function:

◆ run()

void bst::Process::run ( const std::string &  cmd,
std::vector< std::string >  argv 
)
private

References child(), err_pipe_, err_stream_, in_pipe_, in_stream_, bst::K_PIPE_READ, bst::K_PIPE_WRITE, out_pipe_, out_stream_, p_err_filebuf_, p_in_filebuf_, p_out_filebuf_, and pid_.

Referenced by Process().

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

◆ child()

void bst::Process::child ( const std::vector< std::string > &  argv)
private

References err_pipe_, in_pipe_, bst::K_PIPE_READ, bst::K_PIPE_WRITE, and out_pipe_.

Referenced by run().

Here is the caller graph for this function:

Member Data Documentation

◆ pid_

pid_t bst::Process::pid_
private

The process id of the child process.

Referenced by run(), and waitToComplete().

◆ in_pipe_

int bst::Process::in_pipe_[2]
private

The file descriptors for input pipe.

Referenced by Process(), child(), and run().

◆ out_pipe_

int bst::Process::out_pipe_[2]
private

The file descriptors for output pipe.

Referenced by Process(), child(), and run().

◆ err_pipe_

int bst::Process::err_pipe_[2]
private

The file descriptors for error pipe.

Referenced by Process(), child(), and run().

◆ p_in_filebuf_

__gnu_cxx::stdio_filebuf<char>* bst::Process::p_in_filebuf_
private

The filebuf for input pipe.

Referenced by ~Process(), closeInStream(), and run().

◆ p_out_filebuf_

__gnu_cxx::stdio_filebuf<char>* bst::Process::p_out_filebuf_
private

The filebuf for output pipe.

Referenced by ~Process(), and run().

◆ p_err_filebuf_

__gnu_cxx::stdio_filebuf<char>* bst::Process::p_err_filebuf_
private

The filebuf for error pipe.

Referenced by ~Process(), and run().

◆ in_stream_

std::ostream* bst::Process::in_stream_
private

The input stream for the child process.

Referenced by ~Process(), run(), and writeToInStream().

◆ out_stream_

std::istream* bst::Process::out_stream_
private

The output stream for the child process.

Referenced by ~Process(), run(), and waitToComplete().

◆ err_stream_

std::istream* bst::Process::err_stream_
private

The error stream for the child process.

Referenced by ~Process(), run(), and waitToComplete().

◆ have_exit_code_

bool bst::Process::have_exit_code_ = false
private

Set to true when the process has completed and an exit code has been retrieved. This is used internally to check if the process has completed.

Referenced by assertComplete(), and waitToComplete().

◆ exit_code_

int bst::Process::exit_code_ = 0
private

The exit code of the process.

Referenced by getExitCode(), and waitToComplete().

◆ stdout_string_

std::string bst::Process::stdout_string_
private

The stdout of the process.

Referenced by getStdout(), and waitToComplete().

◆ stderr_string_

std::string bst::Process::stderr_string_
private

The stderr of the process.

Referenced by getStderr(), and waitToComplete().