2002-02-27 09:59:52 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
|
|
|
* \file forkedcontr.h
|
2002-09-25 10:03:41 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-02-27 09:59:52 +00:00
|
|
|
*
|
|
|
|
* \author Asger Alstrup Nielsen
|
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-09-25 10:03:41 +00:00
|
|
|
*
|
2002-02-27 09:59:52 +00:00
|
|
|
* A class for the control of child processes launched using
|
|
|
|
* fork() and execvp().
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FORKEDCONTR_H
|
|
|
|
#define FORKEDCONTR_H
|
|
|
|
|
2004-03-23 14:39:41 +00:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2004-11-07 13:22:51 +00:00
|
|
|
|
2004-03-24 17:38:54 +00:00
|
|
|
#include <csignal>
|
|
|
|
//#include <sys/types.h> // needed for pid_t
|
2002-05-29 16:21:03 +00:00
|
|
|
#include <list>
|
2004-03-24 17:38:54 +00:00
|
|
|
#include <vector>
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace support {
|
|
|
|
|
|
|
|
class ForkedProcess;
|
|
|
|
|
2004-03-22 14:10:20 +00:00
|
|
|
class ForkedcallsController {
|
2002-02-27 09:59:52 +00:00
|
|
|
public:
|
|
|
|
/// Get hold of the only controller that can exist inside the process.
|
2002-03-21 17:09:55 +00:00
|
|
|
static ForkedcallsController & get();
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2004-03-24 17:38:54 +00:00
|
|
|
/// Are there any completed child processes to be cleaned-up after?
|
|
|
|
bool processesCompleted() const { return current_child != -1; }
|
|
|
|
|
|
|
|
/** Those child processes that are found to have finished are removed
|
|
|
|
* from the list and their callback function is passed the final
|
|
|
|
* return state.
|
2004-04-03 08:37:12 +00:00
|
|
|
*/
|
2004-03-24 17:38:54 +00:00
|
|
|
void handleCompletedProcesses();
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
/// Add a new child process to the list of controlled processes.
|
2002-10-31 12:42:26 +00:00
|
|
|
void addCall(ForkedProcess const &);
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
/** Kill this process prematurely and remove it from the list.
|
|
|
|
* The process is killed within tolerance secs.
|
|
|
|
* See forkedcall.[Ch] for details.
|
|
|
|
*/
|
|
|
|
void kill(pid_t, int tolerance = 5);
|
|
|
|
|
2004-03-24 17:38:54 +00:00
|
|
|
struct Data {
|
|
|
|
Data() : pid(0), status(0) {}
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** These data are used by the SIGCHLD handler to populate a list
|
|
|
|
* of child processes that have completed and been reaped.
|
|
|
|
* The associated signals are then emitted within the main LyX
|
|
|
|
* event loop.
|
|
|
|
*/
|
|
|
|
std::vector<Data> reaped_children;
|
|
|
|
sig_atomic_t current_child;
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
private:
|
2004-03-22 14:10:20 +00:00
|
|
|
ForkedcallsController();
|
2002-02-27 09:59:52 +00:00
|
|
|
ForkedcallsController(ForkedcallsController const &);
|
2004-03-22 14:10:20 +00:00
|
|
|
~ForkedcallsController();
|
|
|
|
|
2004-03-23 14:39:41 +00:00
|
|
|
typedef boost::shared_ptr<ForkedProcess> ForkedProcessPtr;
|
|
|
|
typedef std::list<ForkedProcessPtr> ListType;
|
2004-03-24 17:38:54 +00:00
|
|
|
typedef ListType::iterator iterator;
|
|
|
|
|
|
|
|
iterator find_pid(pid_t);
|
|
|
|
|
|
|
|
/// The child processes
|
2002-02-27 09:59:52 +00:00
|
|
|
ListType forkedCalls;
|
|
|
|
|
2004-03-24 17:38:54 +00:00
|
|
|
/// Used to block SIGCHLD signals.
|
|
|
|
sigset_t newMask, oldMask;
|
2002-02-27 09:59:52 +00:00
|
|
|
};
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
} // namespace support
|
|
|
|
} // namespace lyx
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#endif // FORKEDCONTR_H
|