nidas v1.2.3
|
Process provides an encapsulation of a spawned process, allowing the parent process to perform I/O with the spawned process, send it signals and wait for it to finish. More...
#include <Process.h>
Public Member Functions | |
Process (pid_t pid) | |
Construct a Process. | |
Process () | |
Default constructor. | |
Process (const Process &x) | |
Copy constructor. | |
Process & | operator= (const Process &x) |
Assignment operator. | |
~Process () | |
Destructor, which does very little. | |
void | kill (int signal) |
Send a signal to the process. | |
int | wait (bool hang, int *status) |
Do a system wait on a process. | |
int | getInFd () const |
Get the file descriptor of the pipe that is connected to the standard in of the Process. | |
std::ostream & | inStream () |
Get the ostream of the pipe that is connected to the standard in of the Process. | |
void | closeIn () |
Close the file descriptor and ostream of the pipe connected to the standard in of the Process. | |
int | getOutFd () const |
Get the file descriptor of the pipe that is connected to the standard out of the Process. | |
std::istream & | outStream () |
Get the istream of the pipe that is connected to the standard out of the Process. | |
void | closeOut () |
Close the file descriptor and istream of the pipe connected to the standard out of the Process. | |
int | getErrFd () const |
Get the file descriptor of the pipe that is connected to the standard error of the Process. | |
std::istream & | errStream () |
Get the istream of the pipe that is connected to the standard error of the Process. | |
void | closeErr () |
Close the file descriptor and istream of the pipe connected to the standard error of the Process. | |
pid_t | getPid () const |
The process id of the Process. | |
Static Public Member Functions | |
static Process | spawn (const std::string &cmd, const std::vector< std::string > &args, const std::map< std::string, std::string > &env, int niceval=0) |
Fork and execute a command and associated arguments and environment. | |
static Process | spawn (const std::string &cmd, const std::vector< std::string > &args, int niceval=0) |
Fork and execute a command and associated arguments. | |
static Process | spawn (const std::string &cmd) |
Execute a command by forking and executing command from the shell command line using "sh -c cmd". | |
static pid_t | checkPidFile (const std::string &pidFile) |
Check if another process is running, using the named process id file. | |
static void | removePidFile () |
Remove the pid file. | |
static void | addEffectiveCapability (int cap) |
Add an effective capability to this process. | |
static void | clearEffectiveCapability (int cap) |
static bool | getEffectiveCapability (int cap) |
Check if this process has an effective capability. | |
static std::string | expandEnvVars (std::string input) |
static bool | getEnvVar (const std::string &name, std::string &value) |
Get an environment variable given a variable name like "HOST", without the '$', or any brackets, '{' or '}'. | |
static void | setEnvVar (const std::string &name, const std::string &value) |
static void | clearEnv () |
Remove any environment settings made by setEnvVar and release any memory allocated to put those settings in the environment. | |
static unsigned long | getVMemSize () |
Return the virtual memory size in bytes of the current process, as read from the /proc/pid/stat file. | |
static unsigned long | getMaxRSS () |
Return the maximum resident set size of the current process, in bytes. | |
Private Member Functions | |
void | setInFd (int val) |
void | setOutFd (int val) |
void | setErrFd (int val) |
Private Attributes | |
pid_t | _pid |
int | _infd |
File descriptor that is connected via a pipe to the standard in file descriptor of a spawned process. | |
auto_ptr< __gnu_cxx::stdio_filebuf< char > > | _inbuf_ap |
GNU extension filebuf which is needed to create a ostream from a file descriptor. | |
auto_ptr< std::ostream > | _instream_ap |
int | _outfd |
File descriptor that is connected via a pipe to the standard out file descriptor of a spawned process. | |
auto_ptr< __gnu_cxx::stdio_filebuf< char > > | _outbuf_ap |
auto_ptr< std::istream > | _outstream_ap |
int | _errfd |
File descriptor that is connected via a pipe to the standard error file descriptor of a spawned process. | |
auto_ptr< __gnu_cxx::stdio_filebuf< char > > | _errbuf_ap |
auto_ptr< std::istream > | _errstream_ap |
Static Private Attributes | |
static std::string | _pidFile |
static std::map< std::string, char * > | _environment |
static Mutex | _envLock |
Process provides an encapsulation of a spawned process, allowing the parent process to perform I/O with the spawned process, send it signals and wait for it to finish.
Static methods are provided for spawning (forking and executing) a process. A static method also exists to atomically check and create a process id file.
Process::Process | ( | pid_t | pid | ) |
Construct a Process.
@ param pid: pid should be > 0 and be a valid process id. Unix system calls support doing kills and waits on a pid less than or equal to 0, but those capabilities are not supported in this class. I/O via the getXXFd() or xxStream() methods is not possible with a process created with this constructor. kill() and wait() are supported. In order to use the Process methods to perform I/O with a process, it must be started with one of the spawn methods.
Process::Process | ( | ) |
Default constructor.
Does not point to any process.
Process::Process | ( | const Process & | x | ) |
Copy constructor.
The new process will inherit the file descriptors and iostreams from x. The file descriptors and iostreams of the copied Process, x, are no longer valid after this copy.
Process::~Process | ( | ) |
Destructor, which does very little.
It does not do a kill or wait on the process, or close any file descriptors that are connected to the process.
|
static |
Add an effective capability to this process.
See man 7 capabilities. If a process has been started with an effective uid of 0(root), one should call prctl(PR_SET_KEEPCAPS,1,0,0,0) prior to doing setuid to a uid > 0. Then use this method to add a capability.
Exception |
|
static |
Check if another process is running, using the named process id file.
A process id file typically has a name like /var/run/xxxx.pid, or /tmp/xxxx.pid. This function is useful when there should only be one instance of a process running on a system - for example a daemon which listens on a socket port. The process name, "xxxx" should not be used by any other process on the system.
If the return is 0, it means that the pidFile either didn't exist, was empty, didn't contain a number, or a current process with the given number doesn't exist. In this case, the file is created if necessary, and will contain the process id of the current process. checkPidFile also calls atexit() to schedule the removePidFile() function to be run when the current process exits. One typically does not have to call removePidFile explicitly.
IOException |
References _pidFile, fd, kill(), and removePidFile().
|
static |
Exception |
|
static |
Remove any environment settings made by setEnvVar and release any memory allocated to put those settings in the environment.
References _environment, and _envLock.
void Process::closeErr | ( | ) |
Close the file descriptor and istream of the pipe connected to the standard error of the Process.
References _errbuf_ap, _errfd, and _errstream_ap.
Referenced by wait().
void Process::closeIn | ( | ) |
void Process::closeOut | ( | ) |
Close the file descriptor and istream of the pipe connected to the standard out of the Process.
References _outbuf_ap, _outfd, and _outstream_ap.
Referenced by wait().
|
inline |
Get the istream of the pipe that is connected to the standard error of the Process.
This istream is only valid after a spawn() of a Process. The error output of the Process can be read from this istream.
References _errstream_ap.
|
static |
References getEnvVar().
|
static |
|
static |
Get an environment variable given a variable name like "HOST", without the '$', or any brackets, '{' or '}'.
References _envLock.
Referenced by expandEnvVars(), nidas::util::UTime::getTZ(), and nidas::util::UTime::setTZ().
|
inline |
|
inline |
|
static |
Return the maximum resident set size of the current process, in bytes.
This is the portion of the process's memory that is held in RAM. This value is determined with the getrusage(RUSAGE_SELF,...) system call.
References ELOG, nidas::util::Exception::errnoToString(), and usage().
|
inline |
|
inline |
The process id of the Process.
References _pid.
Referenced by nidas::core::FsMount::cancel().
|
static |
Return the virtual memory size in bytes of the current process, as read from the /proc/pid/stat file.
Do man proc for info.
References ELOG, nidas::util::Exception::errnoToString(), fclose(), and fp.
|
inline |
Get the ostream of the pipe that is connected to the standard in of the Process.
This ostream is only valid after a spawn() of a Process.
References _instream_ap.
void Process::kill | ( | int | signal | ) |
Send a signal to the process.
IOException |
Referenced by nidas::core::FsMount::cancel(), checkPidFile(), kill(), and Process().
Assignment operator.
The assigned process (on LHS of =) will inherit the file descriptors and iostreams from x. The file descriptors and iostreams of the RHS Process, x, are no longer valid after this assignment.
References _errbuf_ap, _errfd, _errstream_ap, _inbuf_ap, _infd, _instream_ap, _outbuf_ap, _outfd, _outstream_ap, and _pid.
|
inline |
Get the istream of the pipe that is connected to the standard out of the Process.
This istream is only valid after a spawn() of a Process. The output of the Process can be read from this istream.
References _outstream_ap.
Referenced by nidas::core::FsMount::autoMount(), nidas::core::FsMount::mount(), and nidas::core::FsMount::unmount().
|
static |
Remove the pid file.
Note that checkPidFile schedules this function to be called when the process exits.
References _pidFile.
Referenced by checkPidFile().
|
static |
References _environment, and _envLock.
Referenced by nidas::util::UTime::setTZ().
|
private |
References _errbuf_ap, _errfd, and _errstream_ap.
|
private |
References _inbuf_ap, _infd, and _instream_ap.
|
private |
References _outbuf_ap, _outfd, and _outstream_ap.
|
static |
Execute a command by forking and executing command from the shell command line using "sh -c cmd".
Return the Process.
IOException |
|
static |
Fork and execute a command and associated arguments and environment.
Return the Process object that can be used to communicate with or control the sub-process.
cmd | The name of the command. spawn uses the execvp system call, which means that if the command name does not contain a slash (/) character, then a search is done for the command name using the PATH environment variable. |
args | Arguments that are passed to the command. The zero'th argument should be the short name of the command, as is required by the exec system calls. |
env | Environment variables to be added or changed in the subprocess. The map keys are the names of the environment variables. Note that by default, the current environment is copied to the subprocess, so you only need to specify variables that you wish to be added or changed. |
niceval | The desired nice value, typically a positive value so that the spawned process has a higher nice value (lower priority). If niceval is negative and the user does not have sufficient permisions to lower the nice value, the error is silently ignored. |
IOException |
Referenced by spawn(), and nidas::util::svnStatus().
|
static |
Fork and execute a command and associated arguments.
Return the Process object that can be used to communicate with or control the sub-process. spawn uses the execvp system call, which means that if the command name does not contain a slash (/) character, then a search is done for the command name using the PATH environment variable.
IOException |
References spawn().
int Process::wait | ( | bool | hang, |
int * | status ) |
Do a system wait on a process.
hang | If true, wait for process state to change. If hang=false, don't wait, return 0 if process state has not changed. |
status | status of process. See man 2 waitpid. After a successfull wait, any file descriptors connected to the process will be closed. |
IOException |
References _pid, closeErr(), closeIn(), and closeOut().
Referenced by nidas::core::FsMount::autoMount(), nidas::core::FsMount::cancel(), nidas::core::FsMount::mount(), and nidas::core::FsMount::unmount().
|
staticprivate |
Referenced by clearEnv(), and setEnvVar().
|
staticprivate |
Referenced by clearEnv(), getEnvVar(), and setEnvVar().
|
mutableprivate |
Referenced by closeErr(), operator=(), and setErrFd().
|
mutableprivate |
File descriptor that is connected via a pipe to the standard error file descriptor of a spawned process.
Referenced by closeErr(), getErrFd(), operator=(), and setErrFd().
|
mutableprivate |
Referenced by closeErr(), errStream(), operator=(), and setErrFd().
|
mutableprivate |
GNU extension filebuf which is needed to create a ostream from a file descriptor.
Referenced by closeIn(), operator=(), and setInFd().
|
mutableprivate |
File descriptor that is connected via a pipe to the standard in file descriptor of a spawned process.
It is declared mutable to allow invalidating x._infd in the copy constructor and assignment operators that take a reference to a const Process argument.
Referenced by closeIn(), getInFd(), operator=(), and setInFd().
|
mutableprivate |
Referenced by closeIn(), inStream(), operator=(), and setInFd().
|
mutableprivate |
Referenced by closeOut(), operator=(), and setOutFd().
|
mutableprivate |
File descriptor that is connected via a pipe to the standard out file descriptor of a spawned process.
Referenced by closeOut(), getOutFd(), operator=(), and setOutFd().
|
mutableprivate |
Referenced by closeOut(), operator=(), outStream(), and setOutFd().
|
private |
Referenced by getPid(), kill(), operator=(), Process(), and wait().
|
staticprivate |
Referenced by checkPidFile(), and removePidFile().