4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Asger Alstrup
8 * \author Angus Leeming
9 * \author Alfredo Braunstein
11 * Full author contact details are available in file CREDITS.
17 #include <boost/shared_ptr.hpp>
18 #include <boost/signal.hpp>
20 #ifdef HAVE_SYS_TYPES_H
21 # include <sys/types.h>
42 virtual ~ForkedProcess() {}
44 virtual boost::shared_ptr<ForkedProcess> clone() const = 0;
46 /** A SignalType signal is can be emitted once the forked process
47 * has finished. It passes:
48 * the PID of the child and;
49 * the return value from the child.
51 * We use a signal rather than simply a callback function so that
52 * we can return easily to C++ methods, rather than just globally
53 * accessible functions.
55 typedef boost::signal<void(pid_t, int)> SignalType;
57 /** The signal is connected in the calling routine to the desired
58 * slot. We pass a shared_ptr rather than a reference to the signal
59 * because it is eminently possible for the instance of the calling
60 * class (and hence the signal) to be destructed before the forked
63 * It doesn't matter if the slot disappears, SigC takes care of that.
65 typedef boost::shared_ptr<SignalType> SignalTypePtr;
67 /** Invoking the following methods makes sense only if the command
68 * is running asynchronously!
71 /** gets the PID of the child process.
74 pid_t pid() const { return pid_; }
81 /** Set the return value of the child process.
84 void setRetValue(int r) { retval_ = r; }
86 /// Returns the identifying command (for display in the GUI perhaps).
87 std::string const & command() const { return command_; }
89 /// is the process running ?
92 /** Kill child prematurely.
93 * First, a SIGHUP is sent to the child.
94 * If that does not end the child process within "tolerance"
95 * seconds, the SIGKILL signal is sent to the child.
96 * When the child is dead, the callback is called.
98 void kill(int tolerance = 5);
101 /** Spawn the child process.
102 * Returns returncode from child.
104 int run(Starttype type);
106 /// Callback function
107 SignalTypePtr signal_;
109 /// identifying command (for display in the GUI perhaps).
110 std::string command_;
112 /// Process ID of child
115 /// Return value from child
118 /// generate child in background
119 virtual int generateChild() = 0;
121 /// Wait for child process to finish. Updates returncode from child.
127 * An instance of class ForkedCall represents a single child process.
129 * Class ForkedCall uses fork() and execvp() to lauch the child process.
131 * Once launched, control is returned immediately to the parent process
132 * but a Signal can be emitted upon completion of the child.
134 * The child process is not killed when the ForkedCall instance goes out of
135 * scope, but it can be killed by an explicit invocation of the kill() member
139 class ForkedCall : public ForkedProcess {
142 virtual boost::shared_ptr<ForkedProcess> clone() const {
143 return boost::shared_ptr<ForkedProcess>(new ForkedCall(*this));
146 /** Start the child process.
148 * The command "what" is passed to execvp() for execution.
150 * There are two startScript commands available. They differ in that
151 * the second receives a signal that is executed on completion of
152 * the command. This makes sense only for a command executed
153 * in the background, ie DontWait.
155 * The other startscript command can be executed either blocking
156 * or non-blocking, but no signal will be emitted on finishing.
158 int startScript(Starttype, std::string const & what);
161 int startScript(std::string const & what, SignalTypePtr);
165 virtual int generateChild();
170 * This interfaces a queue of forked processes. In order not to
171 * hose the system with multiple processes running simultaneously, you can
172 * request the addition of your process to this queue and it will be
173 * executed when its turn comes.
177 namespace ForkedCallQueue {
179 ForkedCall::SignalTypePtr add(std::string const & process);
180 /// Query whether the queue is running a forked process now.
187 * Control of child processes launched using fork() and execvp().
190 namespace ForkedCallsController {
192 /// Add a new child process to the list of controlled processes.
193 void addCall(ForkedProcess const &);
195 /** Those child processes that are found to have finished are removed
196 * from the list and their callback function is passed the final
199 void handleCompletedProcesses();
201 /** Kill this process prematurely and remove it from the list.
202 * The process is killed within tolerance secs.
203 * See forkedcall.[Ch] for details.
205 void kill(pid_t, int tolerance = 5);
207 } // namespace ForkedCallsController
211 // a wrapper for GetLastError() and FormatMessage().
212 std::string const getChildErrorMessage();
215 } // namespace support
218 #endif // FORKEDCALLS_H