libassa 3.5.1
Loading...
Searching...
No Matches
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>
26using 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>
36extern "C" pid_t
37wait4(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
47namespace ASSA {
48
59{
60public:
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
75private:
78};
79
86class Fork {
87public:
96
104
105#if !defined(WIN32)
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
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_,
173 bool ignore_output_ = false);
174
175#if !defined(WIN32)
176private:
179
182
185
188
189#endif // !defined(WIN32)
190
191};
192
195class fnode_t {
196public:
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 }
217private:
220
223};
224
231class ForkList : public Singleton < ForkList >
232{
233public:
235 ForkList () { trace_with_mask("ForkList::ForkList",FORK); }
236
238 ~ForkList();
239
242};
243
244} // end namespace ASSA
245
246#endif // IS_FORK_H
247
248
249
250
251
A collection of assert function wrappers.
An abstract interface for handling I/O events, timers, and such.
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
SigAction is a C++ wrapper around sigaction structure.
Class SigHandler is a UNIX signal handlers manager/dispatcher class.
Singleton template class allows to turn any new or existing class T into Singleton Pattern.
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
A helper class of Fork.
Definition Fork.h:59
int exit_status() const
Definition Fork.h:69
int handle_signal(int signum_)
Signal handler callback.
Definition Fork.cpp:135
bool caught() const
Definition Fork.h:73
EventHandler class.
ForkList is a singleton class that keeps a list of all forked children.
Definition Fork.h:232
list< fnode_t * > m_list
List of children's data structures.
Definition Fork.h:241
ForkList()
Constructor.
Definition Fork.h:235
~ForkList()
Destructor. Wipe out childer based on their state.
Definition Fork.cpp:188
Fork class is a simple wrapper around C library function fork().
Definition Fork.h:86
pid_t m_pid
Child pid.
Definition Fork.h:178
state_t
Child completion states.
Definition Fork.h:91
@ 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
bool isChild() const
Test whether we are in child section of the code.
Definition Fork.h:136
wait4status_t
Definition Fork.h:99
@ 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
~Fork()
Destructor.
Definition Fork.h:124
SigAction m_old_disp
Old signal disposition.
Definition Fork.h:187
SigHandler m_local_sh
Local signal handler.
Definition Fork.h:181
bool isParent() const
Test whether we are in parent section of the code.
Definition Fork.h:130
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
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
pid_t getChildPID() const
Retrieve child process id.
Definition Fork.h:142
ChildStatusHandler m_chstath
Handler to catch Child's status.
Definition Fork.h:184
forknode_t class.
Definition Fork.h:195
pid_t getPID() const
Retrieve child pid.
Definition Fork.h:205
fnode_t(pid_t pid_, Fork::state_t state_)
Constructor.
Definition Fork.h:198
pid_t m_pid
Child pid.
Definition Fork.h:219
bool needKill() const
Retrieve kill flag.
Definition Fork.h:212
Fork::state_t m_state
Child state {kill, wait}.
Definition Fork.h:222
@ FORK
Class Fork messages
Definition LogMask.h:47