libassa  3.5.1
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
ASSA::Fork Class Reference

Fork class is a simple wrapper around C library function fork(). More...

#include <Fork.h>

Public Types

enum  state_t { KILL_ON_EXIT , WAIT_ON_EXIT , LEAVE_ALONE }
 Child completion states. More...
 
enum  wait4status_t { IGNORE_STATUS , COLLECT_STATUS }
 

Public Member Functions

 Fork (state_t exit_action_=WAIT_ON_EXIT, wait4status_t catch_status_=COLLECT_STATUS)
 Fork the current process in two immediately. More...
 
 ~Fork ()
 Destructor. More...
 
bool isParent () const
 Test whether we are in parent section of the code. More...
 
bool isChild () const
 Test whether we are in child section of the code. More...
 
pid_t getChildPID () const
 Retrieve child process id. More...
 
int get_exit_status () const
 Retrieve exit status of a child process if the constructor's parameter catch_status_ was set to TRUE. More...
 

Static Public Member Functions

static int fork_exec (const string &cmd_, const string &args_, wait4status_t wait_for_completion_, bool ignore_output_=false)
 Execute an external command. More...
 

Private Attributes

pid_t m_pid
 Child pid. More...
 
SigHandler m_local_sh
 Local signal handler. More...
 
ChildStatusHandler m_chstath
 Handler to catch Child's status. More...
 
SigAction m_old_disp
 Old signal disposition. More...
 

Detailed Description

Fork class is a simple wrapper around C library function fork().

Main advantage of using Fork over fork() is that child termination process is handles internally by Fork class static destructor.

Definition at line 86 of file Fork.h.

Member Enumeration Documentation

◆ state_t

Child completion states.

Enumerator
KILL_ON_EXIT 

Kill all childer on exit.

WAIT_ON_EXIT 

Wait for all children to exit.

LEAVE_ALONE 

Ignore all running children on exit.

Definition at line 91 of file Fork.h.

91  {
92  KILL_ON_EXIT,
93  WAIT_ON_EXIT,
95  };
@ KILL_ON_EXIT
Kill all childer on exit.
Definition: Fork.h:92
@ WAIT_ON_EXIT
Wait for all children to exit.
Definition: Fork.h:93
@ LEAVE_ALONE
Ignore all running children on exit.
Definition: Fork.h:94

◆ wait4status_t

Enumerator
IGNORE_STATUS 

Don't wait for child to complete.

COLLECT_STATUS 

Wait for child to complete and collect its exit status.

Definition at line 99 of file Fork.h.

99  {
100  IGNORE_STATUS,
103  };
@ COLLECT_STATUS
Wait for child to complete and collect its exit status.
Definition: Fork.h:101
@ IGNORE_STATUS
Don't wait for child to complete.
Definition: Fork.h:100

Constructor & Destructor Documentation

◆ Fork()

Fork::Fork ( Fork::state_t  exit_action_ = WAIT_ON_EXIT,
Fork::wait4status_t  catch_status_ = COLLECT_STATUS 
)

Fork the current process in two immediately.

Parameters
exit_action_Specify (default=WAIT_ON_EXIT) whether to wait for the child to finish or kill it with SIGTERM on process exit.
catch_status_If true (default=COLLECT_STATUS), pause for the child to exit and collect its exit status.

Definition at line 159 of file Fork.cpp.

161 {
162  trace_with_mask("Fork::Fork",FORK);
163 
164  if (catch_status_ == COLLECT_STATUS) {
165  m_local_sh.install (SIGCHLD, &m_chstath, 0, 0, &m_old_disp);
166  }
167 
168  if ((m_pid = fork()) < 0) {
169  EL((ASSAERR,"failed to fork() - out of swap space?\n"));
170  exit (1); // die right here
171  }
172 
173  if (m_pid) { // The Parent
174  if (exit_action_ != LEAVE_ALONE) {
175  ForkList::get_instance()->m_list.push_back (new fnode_t (m_pid, exit_action_));
176  }
177  if (catch_status_ == COLLECT_STATUS) {
178  if (! m_chstath.caught ()) {
179  pause ();
180  }
181  m_local_sh.remove (SIGCHLD, &m_chstath, &m_old_disp, 0);
182  }
183  }
184 }
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
bool caught() const
Definition: Fork.h:73
list< fnode_t * > m_list
List of children's data structures.
Definition: Fork.h:241
pid_t m_pid
Child pid.
Definition: Fork.h:178
SigAction m_old_disp
Old signal disposition.
Definition: Fork.h:187
SigHandler m_local_sh
Local signal handler.
Definition: Fork.h:181
ChildStatusHandler m_chstath
Handler to catch Child's status.
Definition: Fork.h:184
virtual int remove(int signum_, EventHandler *eh_=0, SigAction *new_disp_=0, SigAction *old_disp_=0)
Remove EventHandler associated with signum_.
Definition: SigHandler.cpp:107
virtual int install(int signum_, EventHandler *new_hand_, SigAction *new_disp_=0, EventHandler **old_hand_=0, SigAction *old_disp_=0)
Add new signal handler and new disposition for the signal.
Definition: SigHandler.cpp:70
static ForkList * get_instance()
Return an instance of templated class T.
Definition: Singleton.h:47
forknode_t class.
Definition: Fork.h:195
@ FORK
Class Fork messages
Definition: LogMask.h:47
@ ASSAERR
ASSA and system errors
Definition: LogMask.h:34

References ASSA::ASSAERR, ASSA::ChildStatusHandler::caught(), COLLECT_STATUS, EL, ASSA::FORK, ASSA::Singleton< ForkList >::get_instance(), ASSA::SigHandler::install(), LEAVE_ALONE, m_chstath, ASSA::ForkList::m_list, m_local_sh, m_old_disp, m_pid, ASSA::SigHandler::remove(), and trace_with_mask.

◆ ~Fork()

ASSA::Fork::~Fork ( )
inline

Destructor.

Doesn't really do anything. All children will be terminated according to state set when process terminates.

Definition at line 124 of file Fork.h.

124 { trace_with_mask("Fork::~Fork",FORK); }

References ASSA::FORK, and trace_with_mask.

Member Function Documentation

◆ fork_exec()

int Fork::fork_exec ( const string &  cmd_,
const string &  args_,
Fork::wait4status_t  wait_for_completion_,
bool  ignore_output_ = false 
)
static

Execute an external command.

Conveniently wraps fork()/execvp()/wait() sequence of calls.

Parameters
cmd_Command to execute.
args_Command arguments as one string.
wait_for_completion_If set to true, blocks until child exits; false otherwise.
ignore_output_Discard child's output to stdout/stderr.
Returns
If wait_for_completion_ is false, returns child PID; If wait_for_completion_ is true, returns command exit status: 0 returned means that command succeeded; 1 that it failed; -1 that wait(2) failed (where it shouldn't).

Close all file descriptors and reduped stdout/stderr to /dev/null

Definition at line 62 of file Fork.cpp.

67 {
68  trace_with_mask("Fork[static]::fork_exec",FORK);
69 
70  DL((FORK,"exec \"%s %s\")\n", cmd_.c_str (), args_.c_str ()));
71  if (cmd_.size () == 0) {
72  return -1;
73  }
74 
75 #if defined(WIN32)
76 
77  return -1; // NOT IMPLEMENTED YET
78 
79 #else
80 
81  Fork f (Fork::LEAVE_ALONE, wait_for_completion_);
82 
83  if (f.isChild ()) {
84  string arg_list (cmd_);
85  arg_list += " " + args_;
86  int argc = 0;
87  char** argv = 0;
88  CmdLineOpts::str_to_argv (arg_list, argc, argv);
89 
93  if (ignore_output_) {
94  for (int i = 0; i < 1024; i++) {
95  (void) close(i);
96  }
97  pid_t nullfd = open("/dev/null", O_WRONLY | O_CREAT, 0666);
98  if (nullfd == -1) {
99  syslog (LOG_ERR,"failed to open \"/dev/null\"");
100  _exit (-1);
101  }
102 
103  (void) dup2 (nullfd, 1);
104  (void) dup2 (nullfd, 2);
105  (void) close (nullfd);
106  }
107 
108  execvp (cmd_.c_str (), argv);
109 
110  EL((ASSAERR,"fork_exec (\"%s\") failed\n", cmd_.c_str ()));
111  _exit (-1);
112  }
113 
114  if (! wait_for_completion_) {
115  return f.getChildPID ();
116  }
117 
118  return f.get_exit_status ();
119 
120 #endif // defined(WIN32)
121 }
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
static void str_to_argv(const string &src_, int &argc_, char **&argv_)
Static function.
Fork class is a simple wrapper around C library function fork().
Definition: Fork.h:86

References ASSA::ASSAERR, DL, EL, ASSA::FORK, get_exit_status(), getChildPID(), isChild(), LEAVE_ALONE, ASSA::CmdLineOpts::str_to_argv(), and trace_with_mask.

◆ get_exit_status()

int ASSA::Fork::get_exit_status ( ) const
inline

Retrieve exit status of a child process if the constructor's parameter catch_status_ was set to TRUE.

Definition at line 151 of file Fork.h.

151 { return m_chstath.exit_status (); }
int exit_status() const
Definition: Fork.h:69

References ASSA::ChildStatusHandler::exit_status(), and m_chstath.

Referenced by fork_exec().

◆ getChildPID()

pid_t ASSA::Fork::getChildPID ( ) const
inline

Retrieve child process id.

Returns
child pid

Definition at line 142 of file Fork.h.

142  {
143  trace_with_mask("Fork::getChildPID",FORK);
144  return m_pid;
145  }

References ASSA::FORK, m_pid, and trace_with_mask.

Referenced by fork_exec(), and ASSA::Pipe::open().

◆ isChild()

bool ASSA::Fork::isChild ( ) const
inline

Test whether we are in child section of the code.

Returns
true if it is parent code, false otherwise

Definition at line 136 of file Fork.h.

136 { return !m_pid ? true : false; }

References m_pid.

Referenced by ASSA::GenServer::become_daemon(), fork_exec(), and ASSA::Pipe::open().

◆ isParent()

bool ASSA::Fork::isParent ( ) const
inline

Test whether we are in parent section of the code.

Returns
true if it is parent code, false otherwise

Definition at line 130 of file Fork.h.

130 { return m_pid ? true : false; }

References m_pid.

Member Data Documentation

◆ m_chstath

ChildStatusHandler ASSA::Fork::m_chstath
private

Handler to catch Child's status.

Definition at line 184 of file Fork.h.

Referenced by Fork(), and get_exit_status().

◆ m_local_sh

SigHandler ASSA::Fork::m_local_sh
private

Local signal handler.

Definition at line 181 of file Fork.h.

Referenced by Fork().

◆ m_old_disp

SigAction ASSA::Fork::m_old_disp
private

Old signal disposition.

Definition at line 187 of file Fork.h.

Referenced by Fork().

◆ m_pid

pid_t ASSA::Fork::m_pid
private

Child pid.

Definition at line 178 of file Fork.h.

Referenced by Fork(), getChildPID(), isChild(), and isParent().


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