4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Asger Alstrup
9 * Interface cleaned up by
10 * \author Angus Leeming
12 * Full author contact details are available in file CREDITS
14 * An instance of Class Forkedcall represents a single child process.
16 * Class Forkedcall uses fork() and execvp() to lauch the child process.
18 * Once launched, control is returned immediately to the parent process
19 * but a Signal can be emitted upon completion of the child.
21 * The child process is not killed when the Forkedcall instance goes out of
22 * scope, but it can be killed by an explicit invocation of the kill() member
35 #include <boost/shared_ptr.hpp>
36 #include <boost/signals/signal2.hpp>
37 #include <boost/function/function0.hpp>
39 #include <sys/types.h>
54 virtual ~ForkedProcess() {}
56 virtual ForkedProcess * clone() const = 0;
58 /** A SignalType signal is can be emitted once the forked process
59 * has finished. It passes:
60 * the PID of the child and;
61 * the return value from the child.
63 * We use a signal rather than simply a callback function so that
64 * we can return easily to C++ methods, rather than just globally
65 * accessible functions.
67 typedef boost::signal2<void, pid_t, int> SignalType;
69 /** The signal is connected in the calling routine to the desired
70 * slot. We pass a shared_ptr rather than a reference to the signal
71 * because it is eminently possible for the instance of the calling
72 * class (and hence the signal) to be destructed before the forked
75 * It doesn't matter if the slot disappears, SigC takes care of that.
77 typedef boost::shared_ptr<SignalType> SignalTypePtr;
79 /** Invoking the following methods makes sense only if the command
80 * is running asynchronously!
83 /** gets the PID of the child process.
86 pid_t pid() const { return pid_; }
93 /** Set the return value of the child process.
96 void setRetValue(int r) { retval_ = r; }
98 /// Returns the identifying command (for display in the GUI perhaps).
99 string const & command() const { return command_; }
101 /** Kill child prematurely.
102 * First, a SIGHUP is sent to the child.
103 * If that does not end the child process within "tolerance"
104 * seconds, the SIGKILL signal is sent to the child.
105 * When the child is dead, the callback is called.
107 void kill(int tolerance = 5);
110 /** Wait for child process to finish.
111 * Returns returncode from child.
114 /** Do not wait for child process to finish.
115 * Returns returncode from child.
117 int runNonBlocking();
119 /// Callback function
120 SignalTypePtr signal_;
122 /// identifying command (for display in the GUI perhaps).
125 /// Process ID of child
128 /// Return value from child
131 /// generate child in background
132 virtual int generateChild() = 0;
134 /// Wait for child process to finish. Updates returncode from child.
139 class Forkedcall : public ForkedProcess {
142 virtual ForkedProcess * clone() const {
143 return 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, string const & what);
161 int startscript(string const & what, SignalTypePtr);
165 virtual int generateChild();
168 #endif // FORKEDCALL_H