ARC SDK
Public Member Functions | Static Public Member Functions

This class runs an external executable. More...

#include <arc/Run.h>

Public Member Functions

 Run (const std::string &cmdline)
 Constructor prepares object to run cmdline.
 
 Run (const std::list< std::string > &argv)
 Constructor prepares object to run executable and arguments specified in argv.
 
 ~Run (void)
 Destructor kills running executable and releases associated resources.
 
 operator bool (void)
 Returns true if object is valid.
 
bool operator! (void)
 Returns true if object is invalid.
 
bool Start (void)
 Starts running executable. This method may be called only once. More...
 
bool Wait (int timeout)
 Wait till execution finished or till timeout seconds expires. More...
 
bool Wait (void)
 Wait till execution finished. More...
 
int Result (void)
 Returns exit code of execution. More...
 
bool Running (void)
 Return true if execution is going on.
 
Time RunTime (void)
 Return when executable was started.
 
Time ExitTime (void)
 Return when executable finished executing.
 
int ReadStdout (int timeout, char *buf, int size)
 Read from stdout handle of running executable. More...
 
int ReadStderr (int timeout, char *buf, int size)
 Read from stderr handle of running executable. More...
 
int WriteStdin (int timeout, const char *buf, int size)
 Write to stdin handle of running executable. More...
 
void AssignStdout (std::string &str)
 Associate stdout handle of executable with string. More...
 
void AssignStderr (std::string &str)
 Associate stderr handle of executable with string. More...
 
void AssignStdin (std::string &str)
 Associate stdin handle of executable with string. More...
 
void KeepStdout (bool keep=true)
 Keep stdout same as parent's if keep = true.
 
void KeepStderr (bool keep=true)
 Keep stderr same as parent's if keep = true.
 
void KeepStdin (bool keep=true)
 Keep stdin same as parent's if keep = true.
 
void CloseStdout (void)
 Closes pipe associated with stdout handle.
 
void CloseStderr (void)
 Closes pipe associated with stderr handle.
 
void CloseStdin (void)
 Closes pipe associated with stdin handle.
 
void AssignInitializer (void(*initializer_func)(void *), void *initializer_arg)
 Assign a function to be called just after process is forked but before execution starts.
 
void AssignKicker (void(*kicker_func)(void *), void *kicker_arg)
 Assign a function to be called just after execution ends.
 
void AssignWorkingDirectory (std::string &wd)
 Assign working directory of the running process.
 
void AssignUserId (int uid)
 Assign uid for the process to run under.
 
void AssignGroupId (int gid)
 Assign gid for the process to run under.
 
void Kill (int timeout)
 Kill running executable. More...
 
void Abandon (void)
 Detach this object from running process. More...
 

Static Public Member Functions

static void AfterFork (void)
 Call this method after fork() in child process. More...
 

Detailed Description

This class runs an external executable.

It is possible to read from or write to its standard handles or to redirect them to std::string elements.

Member Function Documentation

void Arc::Run::Abandon ( void  )

Detach this object from running process.

After calling this method instance is not associated with external process anymore. As result destructor will not kill process.

static void Arc::Run::AfterFork ( void  )
static

Call this method after fork() in child process.

It will reinitialize internal structures for new environment. Do not call it in any other case than defined.

void Arc::Run::AssignStderr ( std::string &  str)

Associate stderr handle of executable with string.

This method must be called before Start(). str object must be valid as long as this object exists.

void Arc::Run::AssignStdin ( std::string &  str)

Associate stdin handle of executable with string.

This method must be called before Start(). str object must be valid as long as this object exists.

void Arc::Run::AssignStdout ( std::string &  str)

Associate stdout handle of executable with string.

This method must be called before Start(). str object must be valid as long as this object exists.

void Arc::Run::Kill ( int  timeout)

Kill running executable.

First soft kill signal (SIGTERM) is sent to executable. If after timeout seconds executable is still running it's killed completely. Currently this method does not work for Windows OS

int Arc::Run::ReadStderr ( int  timeout,
char *  buf,
int  size 
)

Read from stderr handle of running executable.

This method may be used while stderr is directed to string, but the result is unpredictable.

Parameters
timeoutupper limit for which method will block in milliseconds. Negative means infinite.
bufbuffer to write the stderr to
sizesize of buf
Returns
number of read bytes.
int Arc::Run::ReadStdout ( int  timeout,
char *  buf,
int  size 
)

Read from stdout handle of running executable.

This method may be used while stdout is directed to string, but the result is unpredictable.

Parameters
timeoutupper limit for which method will block in milliseconds. Negative means infinite.
bufbuffer to write the stdout to
sizesize of buf
Returns
number of read bytes.
int Arc::Run::Result ( void  )
inline

Returns exit code of execution.

If child process was killed then exit code is -1. If code is compiled with support for detecting lost child process this code is -1 also if track of child was lost.

bool Arc::Run::Start ( void  )

Starts running executable. This method may be called only once.

Returns
true if executable started without problems
bool Arc::Run::Wait ( int  timeout)

Wait till execution finished or till timeout seconds expires.

Returns
true if execution is complete.
bool Arc::Run::Wait ( void  )

Wait till execution finished.

Returns
true if execution is complete, false if execution was not started.
int Arc::Run::WriteStdin ( int  timeout,
const char *  buf,
int  size 
)

Write to stdin handle of running executable.

This method may be used while stdin is directed to string, but the result is unpredictable.

Parameters
timeoutupper limit for which method will block in milliseconds. Negative means infinite.
bufbuffer to read the stdin from
sizesize of buf
Returns
number of written bytes.

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