stc
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
stc::Unix::Process Class Reference

#include <Process.hpp>

Public Member Functions

 Process (const std::vector< std::string > &command, const std::optional< Environment > &env=std::nullopt, const Config &config={})
 
 Process (const std::vector< std::string > &command, const Pipes &pipes, const std::optional< Environment > &env=std::nullopt, const Config &config={})
 
 Process (const std::vector< std::string > &command, const std::shared_ptr< PTY > &pty, const std::optional< Environment > &env=std::nullopt, const Config &config={})
 
virtual ~Process ()
 
std::string getStdoutBuffer (bool reset=false)
 
std::string getStderrBuffer (bool reset=false)
 
void resetBuffers ()
 
ssize_t writeToStdin (const std::string &data)
 
int block ()
 
void signal (int sig)
 
void stop ()
 
void sigkill ()
 
void closeStdin ()
 
std::optional< bool > hasExitedNormally ()
 
std::optional< int > getExitCode ()
 

Protected Member Functions

bool waitPid (int opts=0)
 
char *const * createEnviron (const std::optional< Environment > &env)
 
void doSpawnCommand (const std::vector< std::string > &command, const std::function< void()> &readImpl, const std::function< void()> &prepDuping, const std::optional< Environment > &env)
 
void run (const std::function< void()> &readImpl)
 

Protected Attributes

std::optional< decltype(fork())> pid = std::nullopt
 
std::optional< std::variant< Pipes, std::shared_ptr< PTY > > > interface
 
std::stringstream stdoutBuff
 
std::stringstream stderrBuff
 
std::mutex lock
 
std::thread inputCollector
 
std::atomic< int > statusCode = -1
 
std::atomic< std::optional< bool > > exitedNormally
 
bool running = true
 
Config config
 

Constructor & Destructor Documentation

◆ Process() [1/3]

stc::Unix::Process::Process ( const std::vector< std::string > &  command,
const std::optional< Environment > &  env = std::nullopt,
const Config config = {} 
)
inline

◆ Process() [2/3]

stc::Unix::Process::Process ( const std::vector< std::string > &  command,
const Pipes pipes,
const std::optional< Environment > &  env = std::nullopt,
const Config config = {} 
)
inline

◆ Process() [3/3]

stc::Unix::Process::Process ( const std::vector< std::string > &  command,
const std::shared_ptr< PTY > &  pty,
const std::optional< Environment > &  env = std::nullopt,
const Config config = {} 
)
inline

◆ ~Process()

virtual stc::Unix::Process::~Process ( )
inlinevirtual

Member Function Documentation

◆ block()

int stc::Unix::Process::block ( )
inline

Waits for the process to exit. This is a blocking function call.

Returns
the exit code for the process.

◆ closeStdin()

void stc::Unix::Process::closeStdin ( )
inline

◆ createEnviron()

char *const * stc::Unix::Process::createEnviron ( const std::optional< Environment > &  env)
inlineprotected

Converts an optional<Environment> to environ, or the provided Environment merged with environ (if extendEnviron == true).

This function should never be called outside the subprocess. It calls new std::vector without cleaning it up to build the environment. This is an intentional memory leak because it does not matter since the memory is disappeared shortly after due to exec(). Calling this anywhere else will cause that memory leak to be a problem.

◆ doSpawnCommand()

void stc::Unix::Process::doSpawnCommand ( const std::vector< std::string > &  command,
const std::function< void()> &  readImpl,
const std::function< void()> &  prepDuping,
const std::optional< Environment > &  env 
)
inlineprotected

◆ getExitCode()

std::optional< int > stc::Unix::Process::getExitCode ( )
inline

◆ getStderrBuffer()

std::string stc::Unix::Process::getStderrBuffer ( bool  reset = false)
inline

Returns the stderr output.

Note that:

  • If stdout == stderr, stderr may actually be contained in getStderrBuffer instead.
  • If using a PTY, this method returns nothing
  • If not using any pipes, or not capturing stderr, this will always be empty
Parameters
resetWhether or not to reset the buffer. This can be useful if you want to progressively get output.

◆ getStdoutBuffer()

std::string stc::Unix::Process::getStdoutBuffer ( bool  reset = false)
inline

Returns the stdout output.

Note that:

  • If stdout == stderr, stdout may actually be contained in getStderrBuffer instead.
  • If using a PTY, this also includes some input, as defined by weird PTY internal bullshit that I don't understand
  • If not using any pipes, or not capturing stdout, this will always be empty
Parameters
resetWhether or not to reset the buffer. This can be useful if you want to progressively get output.

◆ hasExitedNormally()

std::optional< bool > stc::Unix::Process::hasExitedNormally ( )
inline
Returns
whether or not the process exited normally, i.e. its end wasn't caused by a signal. This does not imply that the return code is 0, only that it wasn't stopped or terminated. Returns std::nullopt if the process hasn't exited yet.

◆ resetBuffers()

void stc::Unix::Process::resetBuffers ( )
inline

Wipes the content of both stdoutBuff and stderrBuff without returning the contents. To also get the content of the buffers, use getStderrBuffer and getStdoutBuffer, and pass reset = true.

◆ run()

void stc::Unix::Process::run ( const std::function< void()> &  readImpl)
inlineprotected

◆ sigkill()

void stc::Unix::Process::sigkill ( )
inline

Sends sigkill to the process. You should prefer using stop() over this if possible, as sigkill skips cleanup in the child process, which isn't always acceptable.

◆ signal()

void stc::Unix::Process::signal ( int  sig)
inline

◆ stop()

void stc::Unix::Process::stop ( )
inline

Sends sigterm to the process.

◆ waitPid()

bool stc::Unix::Process::waitPid ( int  opts = 0)
inlineprotected

◆ writeToStdin()

ssize_t stc::Unix::Process::writeToStdin ( const std::string &  data)
inline

Used to write to stdin.

Returns
the number of bytes written. This does not have to be used for anything, as it's mainly intended for use in tests of stc, but it's there if you need it for something.
Exceptions
std::runtime_errorif not using pipe or PTY mode, or if using pipe mode and the stdin pipe is null

Member Data Documentation

◆ config

Config stc::Unix::Process::config
protected

◆ exitedNormally

std::atomic<std::optional<bool> > stc::Unix::Process::exitedNormally
protected

◆ inputCollector

std::thread stc::Unix::Process::inputCollector
protected

◆ interface

std::optional< std::variant<Pipes, std::shared_ptr<PTY> > > stc::Unix::Process::interface
protected

◆ lock

std::mutex stc::Unix::Process::lock
protected

◆ pid

std::optional<decltype(fork())> stc::Unix::Process::pid = std::nullopt
protected

◆ running

bool stc::Unix::Process::running = true
protected

◆ statusCode

std::atomic<int> stc::Unix::Process::statusCode = -1
protected

◆ stderrBuff

std::stringstream stc::Unix::Process::stderrBuff
protected

◆ stdoutBuff

std::stringstream stc::Unix::Process::stdoutBuff
protected

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