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 "support/shared_ptr.h"
18 #include <boost/signal.hpp>
20 #ifdef HAVE_SYS_TYPES_H
21 # include <sys/types.h>
42 virtual ~ForkedProcess() {}
44 virtual shared_ptr<ForkedProcess> clone() const = 0;
46 /** A SignalType signal 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 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);
100 /// Returns true if this is a child process
101 static bool iAmAChild() { return IAmAChild; }
104 /** Spawn the child process.
105 * Returns returncode from child.
107 int run(Starttype type);
109 /// implement our own version of fork()
110 /// it just returns -1 if ::fork() is not defined
111 /// otherwise, it forks and sets the global child-process
112 /// boolean IAmAChild
115 /// Callback function
116 SignalTypePtr signal_;
118 /// identifying command (for display in the GUI perhaps).
119 std::string command_;
121 /// Process ID of child
124 /// Return value from child
127 /// generate child in background
128 virtual int generateChild() = 0;
131 static bool IAmAChild;
133 /// Wait for child process to finish. Updates returncode from child.
139 * An instance of class ForkedCall represents a single child process.
141 * Class ForkedCall uses fork() and execvp() to lauch the child process.
143 * Once launched, control is returned immediately to the parent process
144 * but a Signal can be emitted upon completion of the child.
146 * The child process is not killed when the ForkedCall instance goes out of
147 * scope, but it can be killed by an explicit invocation of the kill() member
151 class ForkedCall : public ForkedProcess {
154 virtual shared_ptr<ForkedProcess> clone() const {
155 return shared_ptr<ForkedProcess>(new ForkedCall(*this));
158 /** Start the child process.
160 * The command "what" is passed to execvp() for execution.
162 * There are two startScript commands available. They differ in that
163 * the second receives a signal that is executed on completion of
164 * the command. This makes sense only for a command executed
165 * in the background, ie DontWait.
167 * The other startscript command can be executed either blocking
168 * or non-blocking, but no signal will be emitted on finishing.
170 int startScript(Starttype, std::string const & what);
173 int startScript(std::string const & what, SignalTypePtr);
177 virtual int generateChild();
182 * This interfaces a queue of forked processes. In order not to
183 * hose the system with multiple processes running simultaneously, you can
184 * request the addition of your process to this queue and it will be
185 * executed when its turn comes.
189 namespace ForkedCallQueue {
191 ForkedCall::SignalTypePtr add(std::string const & process);
192 /// Query whether the queue is running a forked process now.
199 * Control of child processes launched using fork() and execvp().
202 namespace ForkedCallsController {
204 /// Add a new child process to the list of controlled processes.
205 void addCall(ForkedProcess const &);
207 /** Those child processes that are found to have finished are removed
208 * from the list and their callback function is passed the final
211 void handleCompletedProcesses();
213 /** Kill this process prematurely and remove it from the list.
214 * The process is killed within tolerance secs.
215 * See forkedcall.[Ch] for details.
217 void kill(pid_t, int tolerance = 5);
219 } // namespace ForkedCallsController
223 // a wrapper for GetLastError() and FormatMessage().
224 std::string const getChildErrorMessage();
227 } // namespace support
230 #endif // FORKEDCALLS_H