SlHelpers
SlHelpers::Process Class Reference

Creates a process and executes a program. More...

#include <Process.h>

Public Types

enum  Error {
  UnknownError, BusyError, PipeError, SpawnError,
  WaitPidError, ReadError, WriteError
}
 Errors returned by lastErrorNo()
 

Public Member Functions

bool run (const std::filesystem::path &program, const std::vector< std::string > &args={}, std::string *out=nullptr)
 Spawns a process, executes program with args, and waits for its termination. More...
 
bool spawn (const std::filesystem::path &program, const std::vector< std::string > &args={}, bool captureStdout=false)
 Forks a process and executes program with args. More...
 
bool readAll (std::string &out)
 Read everything from process' stdout. More...
 
int readPipe () const
 Get the stdout pipe of the process. More...
 
bool waitForFinished ()
 Wait until the underlying process is finished (or dead) More...
 
bool kill (int sig)
 Send a signal sig to the process.
 
pid_t pid () const
 Return the process ID for the child process. More...
 
bool signalled () const
 Was the child signalled? More...
 
unsigned int exitStatus () const
 Exit status of the children (aka WEXITSTATUS()) More...
 
const std::string & lastError () const
 Return the last error string if some.
 
Error lastErrorNo () const
 Return the last error number.
 

Detailed Description

Creates a process and executes a program.

Member Function Documentation

◆ exitStatus()

unsigned int SlHelpers::Process::exitStatus ( ) const
inline

Exit status of the children (aka WEXITSTATUS())

Returns
Exit status.

◆ pid()

pid_t SlHelpers::Process::pid ( ) const
inline

Return the process ID for the child process.

Returns
PID

◆ readAll()

bool SlHelpers::Process::readAll ( std::string &  out)

Read everything from process' stdout.

Parameters
outWhere to read to
Returns
true on success.

◆ readPipe()

int SlHelpers::Process::readPipe ( ) const
inline

Get the stdout pipe of the process.

Returns
Pipe file descriptor.

◆ run()

bool SlHelpers::Process::run ( const std::filesystem::path &  program,
const std::vector< std::string > &  args = {},
std::string *  out = nullptr 
)

Spawns a process, executes program with args, and waits for its termination.

Parameters
programProgram to execute
argsArguments passed to the program (do not add program itself)
outstdout of the program
Returns
zero on success

If out is non-NULL, the stdout of the program is filled in there. run() combines spawn(), readAll(), and waitForFinished().

◆ signalled()

bool SlHelpers::Process::signalled ( ) const
inline

Was the child signalled?

Returns
true if signalled.

◆ spawn()

bool SlHelpers::Process::spawn ( const std::filesystem::path &  program,
const std::vector< std::string > &  args = {},
bool  captureStdout = false 
)

Forks a process and executes program with args.

Parameters
programProgram to run
argsArguments to pass to program
captureStdoutStdout of the program. If nullptr, output is discarded
Returns
true on success.

◆ waitForFinished()

bool SlHelpers::Process::waitForFinished ( )

Wait until the underlying process is finished (or dead)

Returns
true on success.

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