Fork a child process or execute an external program. More...
#include <exec.h>
Public Member Functions | |
execute () | |
C'tor. | |
~execute () | |
D'tor. | |
void | fork (void) |
Fork a child process. | |
void | clear (void) |
Reset the execute class to default values, kill the child processif one is running. | |
bool | is_child (void) |
Returns true if called by the child. | |
bool | is_parent (void) |
Returns true if called by the parent. | |
pid_t | my_pid (void) |
Returns the PID. | |
void | exit (int code=0) |
Called by the child to exit with a particular code. | |
void | reroute_stdio (void) |
Called by the child to reroute the child's stdin, stdout, and stderr to the parent. | |
pid_t | child_pid (void) |
Returns the child's PID. | |
void | signal_child (int signal_no) |
Send a signal to the child. | |
void | hup_child (void) |
Send a HUP signal to the child. | |
void | kill_child (void) |
Send a KILL signal to the child. | |
void | wait (void) |
Wait for the child to exit. | |
bool | child_started (void) const |
Returns true if the child has been started. | |
bool | child_running (void) |
Returns true if the child is running. | |
bool | child_exited (void) |
Returns true of the child has existed. | |
bool | child_exited_normally (void) |
Returns true if the child has exited normally. | |
bool | child_signaled (void) |
Returns true if the child was signaled. | |
bool | child_exited_success (void) |
Returns true if the child returned exit code 0 and no caught signals. | |
int | child_exit_code (void) |
Return the child's exit code. | |
int | child_signal_no (void) |
If the child was signaled, return the signal number. | |
void | exec (const std::string command) |
Execute a command, rerouting stdin, stdout, and stderr to parent. | |
void | exec (const std::string binary, const std::vector< std::string > argv) |
Execute a command, rerouting stdin, stdout, and stderr to parent. | |
int | in_fd (void) |
Return a file descriptor for I/O between parent and child. | |
int | out_fd (void) |
Return a file descriptor for I/O between parent a child. | |
int | err_fd (void) |
Return a file descriptor for I/O between parent and child. | |
bool | in_ready (void) |
Check I/O for input. | |
bool | out_ready (void) |
Check I/O for output. | |
bool | err_ready (void) |
Check I/O for output. | |
bool | in_eof (void) |
Check for input EOF. | |
bool | out_eof (void) |
Check for output EOF. | |
bool | err_eof (void) |
Check for err EOF. | |
int | in_read (char *buf, const int len) |
Allow child to read input from in_fd(). | |
int | in_write (const char *buf, const int len) |
Allow parent to write output to in_fd(). | |
int | out_read (char *buf, const int len) |
Allow parent to read out_fd(). | |
int | out_write (const char *buf, const int len) |
Allow child to write to out_fd(). | |
int | err_read (char *buf, const int len) |
Allow parent to read from err_fd(). | |
int | err_write (const char *buf, const int len) |
Allow child to write to err_fd(). | |
void | print (std::ostream &out) |
Dump execute object information -- used for debugging. | |
Private Member Functions | |
pid_t | check_child_ (void) |
Check the child's status. | |
bool | check_write_ready_ (int fd) |
Return true if the file descriptor is ready to be written to. | |
bool | check_read_ready_ (int fd) |
Return true if the file descriptor has input ready to be read. | |
Private Attributes | |
int | m_fd1 [2] |
int | m_fd2 [2] |
int | m_fd3 [2] |
pid_t | m_pid |
int | m_status |
bool | m_in_eof |
bool | m_out_eof |
bool | m_err_eof |
bool | m_child_started |
Fork a child process or execute an external program.
Definition at line 21 of file exec.h.
execute::execute | ( | ) |
execute::~execute | ( | ) |
pid_t execute::check_child_ | ( | void | ) | [private] |
Check the child's status.
Definition at line 203 of file exec.cc.
References m_pid, and m_status.
Referenced by child_exit_code(), child_exited(), child_running(), child_signal_no(), and child_signaled().
bool execute::check_read_ready_ | ( | int | fd | ) | [private] |
Return true if the file descriptor has input ready to be read.
Definition at line 375 of file exec.cc.
Referenced by err_ready(), in_ready(), and out_ready().
bool execute::check_write_ready_ | ( | int | fd | ) | [private] |
Return true if the file descriptor is ready to be written to.
Definition at line 360 of file exec.cc.
Referenced by err_ready(), in_ready(), and out_ready().
int execute::child_exit_code | ( | void | ) |
Return the child's exit code.
Definition at line 273 of file exec.cc.
References check_child_(), and m_status.
Referenced by child_exited_normally(), child_exited_success(), job_archiver::mf_do_chores(), print(), job_archiver::process(), test1(), test10(), test11(), test12(), test13(), test2(), test3(), test4(), test7(), test8(), and test9().
bool execute::child_exited | ( | void | ) |
Returns true of the child has existed.
Definition at line 231 of file exec.cc.
References check_child_(), and pid().
Referenced by child_exited_normally(), child_exited_success(), child_signal_no(), print(), test1(), test10(), test11(), test12(), test13(), test2(), test3(), test4(), test5(), test6(), test7(), test8(), and test9().
bool execute::child_exited_normally | ( | void | ) |
Returns true if the child has exited normally.
Definition at line 242 of file exec.cc.
References child_exit_code(), and child_exited().
Referenced by job_archiver::mf_do_chores(), print(), job_archiver::process(), test1(), test10(), test11(), test12(), test13(), test2(), test3(), test4(), test7(), test8(), and test9().
bool execute::child_exited_success | ( | void | ) |
Returns true if the child returned exit code 0 and no caught signals.
Definition at line 263 of file exec.cc.
References child_exit_code(), child_exited(), and child_signaled().
Referenced by print(), test1(), test2(), test3(), test4(), test5(), test6(), and test7().
pid_t execute::child_pid | ( | void | ) |
Returns the child's PID.
Definition at line 170 of file exec.cc.
References m_pid.
Referenced by job_archiver::start().
bool execute::child_running | ( | void | ) |
Returns true if the child is running.
Definition at line 220 of file exec.cc.
References check_child_(), and pid().
Referenced by clear(), job_archiver::end(), err_read(), err_write(), in_read(), in_write(), job_archiver::mf_process_rsync_io(), out_read(), out_write(), print(), job_archiver::process(), test1(), test10(), test11(), test12(), test13(), test2(), test3(), test4(), test5(), test6(), test7(), test8(), and test9().
int execute::child_signal_no | ( | void | ) |
If the child was signaled, return the signal number.
Definition at line 284 of file exec.cc.
References check_child_(), child_exited(), and m_status.
Referenced by job_archiver::mf_do_chores(), print(), job_archiver::process(), test1(), test10(), test11(), test12(), test13(), test2(), test3(), test4(), test6(), test7(), test8(), and test9().
bool execute::child_signaled | ( | void | ) |
Returns true if the child was signaled.
Definition at line 252 of file exec.cc.
References check_child_(), and m_status.
Referenced by child_exited_success(), job_archiver::mf_do_chores(), print(), job_archiver::process(), test1(), test10(), test11(), test12(), test13(), test2(), test3(), test4(), test7(), test8(), and test9().
bool execute::child_started | ( | void | ) | const |
Returns true if the child has been started.
Definition at line 214 of file exec.cc.
References m_child_started.
Referenced by err_read(), err_write(), in_read(), in_write(), out_read(), out_write(), and test1().
void execute::clear | ( | void | ) |
Reset the execute class to default values, kill the child processif one is running.
Definition at line 46 of file exec.cc.
References child_running(), is_parent(), m_child_started, m_err_eof, m_fd1, m_fd2, m_fd3, m_in_eof, m_out_eof, m_pid, m_status, and signal_child().
Referenced by job_archiver::end(), execute(), fork(), and ~execute().
bool execute::err_eof | ( | void | ) |
Check for err EOF.
Definition at line 533 of file exec.cc.
References m_err_eof.
Referenced by job_archiver::mf_process_child_io(), and test13().
int execute::err_fd | ( | void | ) |
Return a file descriptor for I/O between parent and child.
If called by the parent, a readable file descriptor is returned. If called by the child, a writeable file descriptor is returned.
If reroute_stdio() was called by the child, then the returned file descriptior is the same as that used by the child for stderr.
Definition at line 347 of file exec.cc.
References is_parent(), and m_fd3.
Referenced by err_read(), err_ready(), err_write(), job_archiver::mf_process_rsync_io(), print(), test10(), test11(), test12(), test13(), test2(), test3(), test8(), and test9().
int execute::err_read | ( | char * | buf, | |
const int | len | |||
) |
Allow parent to read from err_fd().
Definition at line 607 of file exec.cc.
References child_running(), child_started(), err_fd(), and m_err_eof.
Referenced by job_archiver::mf_process_child_io(), and test13().
bool execute::err_ready | ( | void | ) |
Check I/O for output.
If called by the parent, check if output from child is ready to be read. If called by the child, check if output to parent is ready to be written to.
If reroute_stdio() was called by the child, then this pipe is the same as used by the child for stderr.
Definition at line 508 of file exec.cc.
References check_read_ready_(), check_write_ready_(), err_fd(), and is_parent().
Referenced by job_archiver::mf_process_child_io(), job_archiver::mf_process_rsync_io(), print(), test13(), test3(), test8(), and test9().
int execute::err_write | ( | const char * | buf, | |
const int | len | |||
) |
Allow child to write to err_fd().
Definition at line 624 of file exec.cc.
References child_running(), child_started(), err_fd(), and m_err_eof.
void execute::exec | ( | const std::string | binary, | |
const std::vector< std::string > | argv | |||
) |
Execute a command, rerouting stdin, stdout, and stderr to parent.
Definition at line 412 of file exec.cc.
References exit(), fork(), is_parent(), and reroute_stdio().
void execute::exec | ( | const std::string | command | ) |
Execute a command, rerouting stdin, stdout, and stderr to parent.
Definition at line 390 of file exec.cc.
References exit(), fork(), is_parent(), and reroute_stdio().
Referenced by job_archiver::mf_do_chores(), test8(), and test9().
void execute::exit | ( | int | code = 0 |
) |
Called by the child to exit with a particular code.
Definition at line 130 of file exec.cc.
References is_child().
Referenced by exec(), reroute_stdio(), job_archiver::start(), test1(), test10(), test11(), test12(), test13(), test2(), test3(), test4(), test5(), test6(), and test7().
void execute::fork | ( | void | ) |
Fork a child process.
Definition at line 72 of file exec.cc.
References _signal_handler(), clear(), ERROR, m_child_started, m_fd1, m_fd2, m_fd3, and m_pid.
Referenced by exec(), job_archiver::start(), test1(), test10(), test11(), test12(), test13(), test2(), test3(), test4(), test5(), test6(), and test7().
void execute::hup_child | ( | void | ) |
Send a HUP signal to the child.
Definition at line 182 of file exec.cc.
References signal_child().
bool execute::in_eof | ( | void | ) |
int execute::in_fd | ( | void | ) |
Return a file descriptor for I/O between parent and child.
If called by the parent, a writeable file descriptor is returned. If called by the child, a readable file descriptor is returned.
If retroute_stdio() was called by the child, then the returned file descriptor is the same as that used by the child for stdin.
Definition at line 307 of file exec.cc.
References is_parent(), and m_fd1.
Referenced by in_read(), in_ready(), in_write(), print(), test10(), test11(), test12(), test13(), test2(), and test3().
int execute::in_read | ( | char * | buf, | |
const int | len | |||
) |
Allow child to read input from in_fd().
Definition at line 539 of file exec.cc.
References child_running(), child_started(), in_fd(), and m_in_eof.
bool execute::in_ready | ( | void | ) |
Check I/O for input.
If called by the parent, check if ready to write to child's input. If called by the child, check if input is ready to be read.
If reroute_stdio() was called by the child, then this pipe is the same as used by the child for stdin.
Definition at line 468 of file exec.cc.
References check_read_ready_(), check_write_ready_(), in_fd(), and is_parent().
Referenced by print(), test13(), test3(), test8(), and test9().
int execute::in_write | ( | const char * | buf, | |
const int | len | |||
) |
Allow parent to write output to in_fd().
Definition at line 556 of file exec.cc.
References child_running(), child_started(), in_fd(), and m_in_eof.
Referenced by test13().
bool execute::is_child | ( | void | ) |
Returns true if called by the child.
Definition at line 101 of file exec.cc.
References m_pid.
Referenced by exit(), is_parent(), print(), reroute_stdio(), job_archiver::start(), test1(), test13(), test2(), test3(), test5(), and test6().
bool execute::is_parent | ( | void | ) |
Returns true if called by the parent.
Definition at line 110 of file exec.cc.
References is_child().
Referenced by clear(), err_fd(), err_ready(), exec(), in_fd(), in_ready(), out_fd(), out_ready(), print(), test10(), test11(), test12(), test13(), test2(), test3(), test4(), test7(), and wait().
void execute::kill_child | ( | void | ) |
Send a KILL signal to the child.
Definition at line 188 of file exec.cc.
References signal_child().
Referenced by job_archiver::end(), job_archiver::mf_process_rsync_io(), and test6().
pid_t execute::my_pid | ( | void | ) |
Returns the PID.
Definition at line 120 of file exec.cc.
References pid().
Referenced by job_archiver::clear(), and print().
bool execute::out_eof | ( | void | ) |
Check for output EOF.
Definition at line 527 of file exec.cc.
References m_out_eof.
Referenced by job_archiver::mf_process_child_io(), and test13().
int execute::out_fd | ( | void | ) |
Return a file descriptor for I/O between parent a child.
If called by the parent, a readable file descriptor is returned. If called by the child, a writable file descriptor is returned.
If reroute_stdio() was called by the child, then the returned file descriptor is the same as that used by the child for stdout.
Definition at line 327 of file exec.cc.
References is_parent(), and m_fd2.
Referenced by job_archiver::mf_process_rsync_io(), out_read(), out_ready(), out_write(), print(), test10(), test11(), test12(), test13(), test2(), test3(), test8(), and test9().
int execute::out_read | ( | char * | buf, | |
const int | len | |||
) |
Allow parent to read out_fd().
Definition at line 573 of file exec.cc.
References child_running(), child_started(), m_out_eof, and out_fd().
Referenced by job_archiver::mf_process_child_io(), and test13().
bool execute::out_ready | ( | void | ) |
Check I/O for output.
If called by the parent, check if output from child is ready to be read. If called by the child, check if output to parent is ready to be written to.
If reroute_stdio() was called by the child, then this pipe is the same as used by the child for stdout.
Definition at line 488 of file exec.cc.
References check_read_ready_(), check_write_ready_(), is_parent(), and out_fd().
Referenced by job_archiver::mf_process_child_io(), job_archiver::mf_process_rsync_io(), print(), test13(), test3(), test8(), and test9().
int execute::out_write | ( | const char * | buf, | |
const int | len | |||
) |
Allow child to write to out_fd().
Definition at line 590 of file exec.cc.
References child_running(), child_started(), m_out_eof, and out_fd().
void execute::print | ( | std::ostream & | out | ) |
Dump execute object information -- used for debugging.
Definition at line 641 of file exec.cc.
References child_exit_code(), child_exited(), child_exited_normally(), child_exited_success(), child_running(), child_signal_no(), child_signaled(), err_fd(), err_ready(), in_fd(), in_ready(), is_child(), is_parent(), my_pid(), out_fd(), and out_ready().
Referenced by operator<<().
void execute::reroute_stdio | ( | void | ) |
Called by the child to reroute the child's stdin, stdout, and stderr to the parent.
Definition at line 138 of file exec.cc.
References ERROR_INSTANCE, exit(), is_child(), m_fd1, m_fd2, and m_fd3.
Referenced by exec(), and job_archiver::start().
void execute::signal_child | ( | int | signal_no | ) |
Send a signal to the child.
Definition at line 176 of file exec.cc.
References m_pid.
Referenced by clear(), hup_child(), kill_child(), and test7().
void execute::wait | ( | void | ) |
bool execute::m_child_started [private] |
Definition at line 84 of file exec.h.
Referenced by child_started(), clear(), and fork().
bool execute::m_err_eof [private] |
Definition at line 83 of file exec.h.
Referenced by clear(), err_eof(), err_read(), and err_write().
int execute::m_fd1[2] [private] |
int execute::m_fd2[2] [private] |
int execute::m_fd3[2] [private] |
bool execute::m_in_eof [private] |
bool execute::m_out_eof [private] |
Definition at line 82 of file exec.h.
Referenced by clear(), out_eof(), out_read(), and out_write().
pid_t execute::m_pid [private] |
Definition at line 79 of file exec.h.
Referenced by check_child_(), child_pid(), clear(), fork(), is_child(), signal_child(), and wait().
int execute::m_status [private] |
Definition at line 80 of file exec.h.
Referenced by check_child_(), child_exit_code(), child_signal_no(), child_signaled(), clear(), and wait().