libassa  3.5.1
Fork.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // Fork.h
4 //------------------------------------------------------------------------------
5 // Copyright (C) 1997-2002,2005 Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 #ifndef IS_FORK_H
13 #define IS_FORK_H
14 
15 #include <unistd.h> // fork
16 #include <stdlib.h>
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <signal.h>
20 
21 #ifndef WIN32
22 # include <sys/wait.h>
23 #endif
24 
25 #include <list>
26 using std::list;
27 
28 /* Sun Solaris 2.6 has wait4(3C) function definition missing
29  * from its header files. The function, however, is in the
30  * standard library. Testing this scenario would require
31  * writing custom m4 macro.
32  */
33 #if defined(__sun)
34 #include <sys/time.h>
35 #include <sys/resource.h>
36 extern "C" pid_t
37 wait4(pid_t pid, int *statusp, int options, struct rusage *rusage);
38 #endif
39 
40 
41 #include "assa/Assure.h"
42 #include "assa/Singleton.h"
43 #include "assa/EventHandler.h"
44 #include "assa/SigHandler.h"
45 #include "assa/SigAction.h"
46 
47 namespace ASSA {
48 
59 {
60 public:
62  : m_exit_status (-1), m_caught (false) { /* no-op */ }
63 
64  int handle_signal (int signum_);
65 
69  int exit_status () const { return m_exit_status; }
70 
73  bool caught () const { return m_caught; }
74 
75 private:
77  bool m_caught;
78 };
79 
86 class Fork {
87 public:
91  enum state_t {
95  };
96 
103  };
104 
105 #if !defined(WIN32)
106 
116  Fork (state_t exit_action_ = WAIT_ON_EXIT,
117  wait4status_t catch_status_ = COLLECT_STATUS);
118 
124  ~Fork() { trace_with_mask("Fork::~Fork",FORK); }
125 
130  bool isParent() const { return m_pid ? true : false; }
131 
136  bool isChild() const { return !m_pid ? true : false; }
137 
142  pid_t getChildPID() const {
143  trace_with_mask("Fork::getChildPID",FORK);
144  return m_pid;
145  }
146 
151  int get_exit_status () const { return m_chstath.exit_status (); }
152 
153 #endif // !defined(WIN32)
154 
170  static int fork_exec (const string& cmd_,
171  const string& args_,
172  wait4status_t wait_for_completion_,
173  bool ignore_output_ = false);
174 
175 #if !defined(WIN32)
176 private:
178  pid_t m_pid;
179 
182 
185 
188 
189 #endif // !defined(WIN32)
190 
191 };
192 
195 class fnode_t {
196 public:
198  fnode_t (pid_t pid_, Fork::state_t state_)
199  : m_pid(pid_), m_state(state_)
200  {
201  trace_with_mask("fnode_t::fnode_t",FORK);
202  }
203 
205  pid_t getPID() const
206  {
207  trace_with_mask("fnode_t::getPID",FORK);
208  return m_pid;
209  }
210 
212  bool needKill() const
213  {
214  trace_with_mask("fnode_t::needKill",FORK);
215  return m_state == Fork::KILL_ON_EXIT ? true : false;
216  }
217 private:
219  pid_t m_pid;
220 
223 };
224 
231 class ForkList : public Singleton < ForkList >
232 {
233 public:
235  ForkList () { trace_with_mask("ForkList::ForkList",FORK); }
236 
238  ~ForkList();
239 
241  list< fnode_t* > m_list;
242 };
243 
244 } // end namespace ASSA
245 
246 #endif // IS_FORK_H
247 
248 
249 
250 
251 
ASSA::fnode_t
forknode_t class.
Definition: Fork.h:195
ASSA::fnode_t::needKill
bool needKill() const
Retrieve kill flag.
Definition: Fork.h:212
SigHandler.h
ASSA::ForkList::m_list
list< fnode_t * > m_list
List of children's data structures.
Definition: Fork.h:241
ASSA::SigAction
Definition: SigAction.h:94
ASSA::ChildStatusHandler
A helper class of Fork.
Definition: Fork.h:58
ASSA::Fork::~Fork
~Fork()
Destructor.
Definition: Fork.h:124
ASSA::Fork::m_local_sh
SigHandler m_local_sh
Local signal handler.
Definition: Fork.h:181
ASSA::Fork
Fork class is a simple wrapper around C library function fork(). Main advantage of using Fork over fo...
Definition: Fork.h:86
ASSA::Fork::fork_exec
static int fork_exec(const string &cmd_, const string &args_, wait4status_t wait_for_completion_, bool ignore_output_=false)
Execute an external command.
Definition: Fork.cpp:63
ASSA::FORK
@ FORK
Class Fork messages
Definition: LogMask.h:47
ASSA::fnode_t::getPID
pid_t getPID() const
Retrieve child pid.
Definition: Fork.h:205
ASSA::SigHandler
Definition: SigHandler.h:49
ASSA::Fork::COLLECT_STATUS
@ COLLECT_STATUS
Wait for child to complete and collect its exit status.
Definition: Fork.h:101
ASSA::ChildStatusHandler::exit_status
int exit_status() const
Definition: Fork.h:69
ASSA::fnode_t::m_state
Fork::state_t m_state
Child state {kill, wait}.
Definition: Fork.h:222
ASSA::fnode_t::fnode_t
fnode_t(pid_t pid_, Fork::state_t state_)
Constructor.
Definition: Fork.h:198
ASSA::Fork::m_pid
pid_t m_pid
Child pid.
Definition: Fork.h:178
ASSA::ChildStatusHandler::ChildStatusHandler
ChildStatusHandler()
Definition: Fork.h:61
ASSA::Fork::state_t
state_t
Definition: Fork.h:91
ASSA::ChildStatusHandler::caught
bool caught() const
Definition: Fork.h:73
SigAction.h
ASSA::EventHandler
EventHandler class.
Definition: EventHandler.h:102
ASSA::Fork::wait4status_t
wait4status_t
Definition: Fork.h:99
EventHandler.h
ASSA::Fork::isChild
bool isChild() const
Test whether we are in child section of the code.
Definition: Fork.h:136
ASSA::Fork::isParent
bool isParent() const
Test whether we are in parent section of the code.
Definition: Fork.h:130
ASSA::ForkList::ForkList
ForkList()
Constructor.
Definition: Fork.h:235
ASSA::Fork::WAIT_ON_EXIT
@ WAIT_ON_EXIT
Wait for all children to exit.
Definition: Fork.h:93
ASSA::ForkList::~ForkList
~ForkList()
Destructor. Wipe out childer based on their state.
Definition: Fork.cpp:188
ASSA::ChildStatusHandler::handle_signal
int handle_signal(int signum_)
Signal handler callback.
Definition: Fork.cpp:135
ASSA::ForkList
ForkList is a singleton class that keeps a list of all forked children.
Definition: Fork.h:231
ASSA::ChildStatusHandler::m_caught
bool m_caught
Definition: Fork.h:77
Assure.h
Singleton.h
ASSA::Singleton
Definition: Singleton.h:42
ASSA::Fork::IGNORE_STATUS
@ IGNORE_STATUS
Don't wait for child to complete.
Definition: Fork.h:100
ASSA::fnode_t::m_pid
pid_t m_pid
Child pid.
Definition: Fork.h:219
ASSA::Fork::get_exit_status
int get_exit_status() const
Retrieve exit status of a child process if the constructor's parameter catch_status_ was set to TRUE.
Definition: Fork.h:151
ASSA::Fork::m_chstath
ChildStatusHandler m_chstath
Handler to catch Child's status.
Definition: Fork.h:184
ASSA::Fork::KILL_ON_EXIT
@ KILL_ON_EXIT
Kill all childer on exit.
Definition: Fork.h:92
ASSA
Definition: Acceptor.h:40
ASSA::Fork::m_old_disp
SigAction m_old_disp
Old signal disposition.
Definition: Fork.h:187
ASSA::Fork::Fork
Fork(state_t exit_action_=WAIT_ON_EXIT, wait4status_t catch_status_=COLLECT_STATUS)
Fork the current process in two immediately.
Definition: Fork.cpp:160
ASSA::Fork::getChildPID
pid_t getChildPID() const
Retrieve child process id.
Definition: Fork.h:142
trace_with_mask
#define trace_with_mask(s, m)
Definition: Logger.h:437
ASSA::ChildStatusHandler::m_exit_status
int m_exit_status
Definition: Fork.h:76
ASSA::Fork::LEAVE_ALONE
@ LEAVE_ALONE
Ignore all running children on exit.
Definition: Fork.h:94