Loading...
Searching...
No Matches
Fork.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Fork.h
//------------------------------------------------------------------------------
// Copyright (C) 1997-2002,2005 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#ifndef IS_FORK_H
#define IS_FORK_H
#include <unistd.h> // fork
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#ifndef WIN32
# include <sys/wait.h>
#endif
#include <list>
using std::list;
/* Sun Solaris 2.6 has wait4(3C) function definition missing
* from its header files. The function, however, is in the
* standard library. Testing this scenario would require
* writing custom m4 macro.
*/
#if defined(__sun)
#include <sys/time.h>
#include <sys/resource.h>
extern "C" pid_t
wait4(pid_t pid, int *statusp, int options, struct rusage *rusage);
#endif
#include "assa/Assure.h"
#include "assa/Singleton.h"
#include "assa/EventHandler.h"
#include "assa/SigHandler.h"
#include "assa/SigAction.h"
namespace ASSA {
class ChildStatusHandler : public EventHandler
{
public:
ChildStatusHandler ()
: m_exit_status (-1), m_caught (false) { /* no-op */ }
int handle_signal (int signum_);
int exit_status () const { return m_exit_status; }
bool caught () const { return m_caught; }
private:
int m_exit_status;
bool m_caught;
};
class Fork {
public:
enum state_t {
KILL_ON_EXIT,
WAIT_ON_EXIT,
LEAVE_ALONE
};
enum wait4status_t {
IGNORE_STATUS,
COLLECT_STATUS
};
#if !defined(WIN32)
Fork (state_t exit_action_ = WAIT_ON_EXIT,
wait4status_t catch_status_ = COLLECT_STATUS);
~Fork() { trace_with_mask("Fork::~Fork",FORK); }
bool isParent() const { return m_pid ? true : false; }
bool isChild() const { return !m_pid ? true : false; }
pid_t getChildPID() const {
trace_with_mask("Fork::getChildPID",FORK);
return m_pid;
}
int get_exit_status () const { return m_chstath.exit_status (); }
#endif // !defined(WIN32)
static int fork_exec (const string& cmd_,
const string& args_,
wait4status_t wait_for_completion_,
bool ignore_output_ = false);
#if !defined(WIN32)
private:
pid_t m_pid;
SigHandler m_local_sh;
ChildStatusHandler m_chstath;
SigAction m_old_disp;
#endif // !defined(WIN32)
};
class fnode_t {
public:
fnode_t (pid_t pid_, Fork::state_t state_)
: m_pid(pid_), m_state(state_)
{
trace_with_mask("fnode_t::fnode_t",FORK);
}
pid_t getPID() const
{
trace_with_mask("fnode_t::getPID",FORK);
return m_pid;
}
bool needKill() const
{
trace_with_mask("fnode_t::needKill",FORK);
return m_state == Fork::KILL_ON_EXIT ? true : false;
}
private:
pid_t m_pid;
Fork::state_t m_state;
};
class ForkList : public Singleton < ForkList >
{
public:
ForkList () { trace_with_mask("ForkList::ForkList",FORK); }
~ForkList();
list< fnode_t* > m_list;
};
} // end namespace ASSA
#endif // IS_FORK_H
Assure.h
A collection of assert function wrappers.
EventHandler.h
An abstract interface for handling I/O events, timers, and such.
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
SigAction.h
SigAction is a C++ wrapper around sigaction structure.
SigHandler.h
Class SigHandler is a UNIX signal handlers manager/dispatcher class.
Singleton.h
Singleton template class allows to turn any new or existing class T into Singleton Pattern.
ASSA::ChildStatusHandler
A helper class of Fork.
Definition Fork.h:59
ASSA::ChildStatusHandler::m_caught
bool m_caught
Definition Fork.h:77
ASSA::ChildStatusHandler::exit_status
int exit_status() const
Definition Fork.h:69
ASSA::ChildStatusHandler::handle_signal
int handle_signal(int signum_)
Signal handler callback.
Definition Fork.cpp:135
ASSA::ChildStatusHandler::m_exit_status
int m_exit_status
Definition Fork.h:76
ASSA::ChildStatusHandler::caught
bool caught() const
Definition Fork.h:73
ASSA::ChildStatusHandler::ChildStatusHandler
ChildStatusHandler()
Definition Fork.h:61
ASSA::EventHandler::EventHandler
EventHandler()
Constructor.
Definition EventHandler.h:165
ASSA::ForkList::m_list
list< fnode_t * > m_list
List of children's data structures.
Definition Fork.h:241
ASSA::ForkList::ForkList
ForkList()
Constructor.
Definition Fork.h:235
ASSA::ForkList::~ForkList
~ForkList()
Destructor. Wipe out childer based on their state.
Definition Fork.cpp:188
ASSA::Fork::m_pid
pid_t m_pid
Child pid.
Definition Fork.h:178
ASSA::Fork::state_t
state_t
Child completion states.
Definition Fork.h:91
ASSA::Fork::KILL_ON_EXIT
@ KILL_ON_EXIT
Kill all childer on exit.
Definition Fork.h:92
ASSA::Fork::WAIT_ON_EXIT
@ WAIT_ON_EXIT
Wait for all children to exit.
Definition Fork.h:93
ASSA::Fork::LEAVE_ALONE
@ LEAVE_ALONE
Ignore all running children on exit.
Definition Fork.h:94
ASSA::Fork::isChild
bool isChild() const
Test whether we are in child section of the code.
Definition Fork.h:136
ASSA::Fork::wait4status_t
wait4status_t
Definition Fork.h:99
ASSA::Fork::COLLECT_STATUS
@ COLLECT_STATUS
Wait for child to complete and collect its exit status.
Definition Fork.h:101
ASSA::Fork::IGNORE_STATUS
@ IGNORE_STATUS
Don't wait for child to complete.
Definition Fork.h:100
ASSA::Fork::~Fork
~Fork()
Destructor.
Definition Fork.h:124
ASSA::Fork::m_old_disp
SigAction m_old_disp
Old signal disposition.
Definition Fork.h:187
ASSA::Fork::m_local_sh
SigHandler m_local_sh
Local signal handler.
Definition Fork.h:181
ASSA::Fork::isParent
bool isParent() const
Test whether we are in parent section of the code.
Definition Fork.h:130
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
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::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::getChildPID
pid_t getChildPID() const
Retrieve child process id.
Definition Fork.h:142
ASSA::Fork::m_chstath
ChildStatusHandler m_chstath
Handler to catch Child's status.
Definition Fork.h:184
ASSA::SigAction
Definition SigAction.h:95
ASSA::SigHandler
Definition SigHandler.h:50
ASSA::Singleton< ForkList >::Singleton
Singleton()
Definition Singleton.h:57
ASSA::fnode_t::getPID
pid_t getPID() const
Retrieve child pid.
Definition Fork.h:205
ASSA::fnode_t::fnode_t
fnode_t(pid_t pid_, Fork::state_t state_)
Constructor.
Definition Fork.h:198
ASSA::fnode_t::m_pid
pid_t m_pid
Child pid.
Definition Fork.h:219
ASSA::fnode_t::needKill
bool needKill() const
Retrieve kill flag.
Definition Fork.h:212
ASSA::fnode_t::m_state
Fork::state_t m_state
Child state {kill, wait}.
Definition Fork.h:222
ASSA
Definition Acceptor.h:40
ASSA::FORK
@ FORK
Class Fork messages.
Definition LogMask.h:47