4 * Copyright 2002 the LyX Team
5 * Read the file COPYING
7 * \author Asger Alstrup
9 * Interface cleaned up by
10 * \author Angus Leeming <a.leeming@ic.ac.uk>
12 * An instance of Class Forkedcall represents a single child process.
14 * Class Forkedcall uses fork() and execvp() to lauch the child process.
16 * Once launched, control is returned immediately to the parent process
17 * but a Signal can be emitted upon completion of the child.
19 * The child process is not killed when the Forkedcall instance goes out of
20 * scope, but it can be killed by an explicit invocation of the kill() member
33 #include <boost/shared_ptr.hpp>
35 #include <sigc++/signal_system.h>
37 #include <sys/types.h>
52 /** Start the child process.
54 * The command "what" is passed to fork() for execution.
56 * There are two startscript commands available. They differ in that
57 * the second receives a signal that is executed on completion of
58 * the command. This makes sense only for a command executed
59 * in the background, ie DontWait.
61 * The other startscript command can be executed either blocking
62 * or non-blocking, but no signal will be emitted on finishing.
64 int startscript(Starttype, string const & what);
66 /** A SignalType signal is can be emitted once the forked process
67 * has finished. It passes:
68 * the commandline string;
69 * the PID of the child and;
70 * the return value from the child.
72 * We use a signal rather than simply a callback function so that
73 * we can return easily to C++ methods, rather than just globally
74 * accessible functions.
76 typedef SigC::Signal3<void, string const &, pid_t, int> SignalType;
78 /** The signal is connected in the calling routine to the desired
79 * slot. We pass a shared_ptr rather than a reference to the signal
80 * because it is eminently possible for the instance of the calling
81 * class (and hence the signal) to be destructed before the forked
84 * It doesn't matter if the slot disappears, SigC takes care of that.
86 typedef boost::shared_ptr<SignalType> SignalTypePtr;
89 int startscript(string const & what, SignalTypePtr);
91 /** Invoking the following methods makes sense only if the command
92 * is running asynchronously!
95 /** gets the PID of the child process.
98 pid_t pid() const { return pid_; }
105 /** Set the return value of the child process.
108 void setRetValue(int r) { retval_ = r; }
110 /** Kill child prematurely.
111 * First, a SIGHUP is sent to the child.
112 * If that does not end the child process within "tolerance"
113 * seconds, the SIGKILL signal is sent to the child.
114 * When the child is dead, the callback is called.
116 void kill(int tolerance = 5);
118 string const & command() const { return command_; }
121 /// Callback function
122 SignalTypePtr signal_;
127 /// Process ID of child
130 /// Return value from child
134 pid_t generateChild();
136 /// Wait for child process to finish. Updates returncode from child.
140 #endif // FORKEDCALL_H