]> git.lyx.org Git - lyx.git/blob - src/support/ForkedCalls.h
Transfer tempName() implementation to FileName.
[lyx.git] / src / support / ForkedCalls.h
1 // -*- C++ -*-
2 /**
3  * \file ForkedCalls.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Asger Alstrup
8  * \author Angus Leeming
9  * \author Alfredo Braunstein
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef FORKEDCALLS_H
15 #define FORKEDCALLS_H
16
17 #include <boost/shared_ptr.hpp>
18 #include <boost/signal.hpp>
19
20 #ifdef HAVE_SYS_TYPES_H
21 # include <sys/types.h>
22 #endif
23
24 #include <string>
25
26 namespace lyx {
27 namespace support {
28
29 class ForkedProcess {
30 public:
31         ///
32         enum Starttype {
33                 ///
34                 Wait,
35                 ///
36                 DontWait
37         };
38
39         ///
40         ForkedProcess();
41         ///
42         virtual ~ForkedProcess() {}
43         ///
44         virtual boost::shared_ptr<ForkedProcess> clone() const = 0;
45
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.
50          *
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.
54          */
55         typedef boost::signal<void(pid_t, int)> SignalType;
56
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
61          *  call is complete.
62          *
63          *  It doesn't matter if the slot disappears, SigC takes care of that.
64          */
65         typedef boost::shared_ptr<SignalType> SignalTypePtr;
66
67         /** Invoking the following methods makes sense only if the command
68          *  is running asynchronously!
69          */
70
71         /** gets the PID of the child process.
72          *  Used by the timer.
73          */
74         pid_t pid() const { return pid_; }
75
76         /** Emit the signal.
77          *  Used by the timer.
78          */
79         void emitSignal();
80
81         /** Set the return value of the child process.
82          *  Used by the timer.
83          */
84         void setRetValue(int r) { retval_ = r; }
85
86         /// Returns the identifying command (for display in the GUI perhaps).
87         std::string const & command() const { return command_; }
88
89         /// is the process running ?
90         bool running() const;
91
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.
97          */
98         void kill(int tolerance = 5);
99
100 protected:
101         /** Spawn the child process.
102          *  Returns returncode from child.
103          */
104         int run(Starttype type);
105
106         /// Callback function
107         SignalTypePtr signal_;
108
109         /// identifying command (for display in the GUI perhaps).
110         std::string command_;
111
112         /// Process ID of child
113         pid_t pid_;
114
115         /// Return value from child
116         int retval_;
117 private:
118         /// generate child in background
119         virtual int generateChild() = 0;
120
121         /// Wait for child process to finish. Updates returncode from child.
122         int waitForChild();
123 };
124
125
126 /* 
127  * An instance of class ForkedCall represents a single child process.
128  *
129  * Class ForkedCall uses fork() and execvp() to lauch the child process.
130  *
131  * Once launched, control is returned immediately to the parent process
132  * but a Signal can be emitted upon completion of the child.
133  *
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
136  * function.
137  */
138
139 class ForkedCall : public ForkedProcess {
140 public:
141         ///
142         virtual boost::shared_ptr<ForkedProcess> clone() const {
143                 return boost::shared_ptr<ForkedProcess>(new ForkedCall(*this));
144         }
145
146         /** Start the child process.
147          *
148          *  The command "what" is passed to execvp() for execution.
149          *
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.
154          *
155          *  The other startscript command can be executed either blocking
156          *  or non-blocking, but no signal will be emitted on finishing.
157          */
158         int startScript(Starttype, std::string const & what);
159
160         ///
161         int startScript(std::string const & what, SignalTypePtr);
162
163 private:
164         ///
165         virtual int generateChild();
166 };
167
168
169 /**
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.
174  *
175  */
176
177 namespace ForkedCallQueue {
178
179 ForkedCall::SignalTypePtr add(std::string const & process);
180 /// Query whether the queue is running a forked process now.
181 bool running();
182
183 }
184
185
186 /**
187  * Control of child processes launched using fork() and execvp().
188  */
189
190 namespace ForkedCallsController {
191
192 /// Add a new child process to the list of controlled processes.
193 void addCall(ForkedProcess const &);
194
195 /** Those child processes that are found to have finished are removed
196  *  from the list and their callback function is passed the final
197  *  return state.
198  */
199 void handleCompletedProcesses();
200
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.
204  */
205 void kill(pid_t, int tolerance = 5);
206
207 } // namespace ForkedCallsController
208
209
210 #if defined(_WIN32)
211 // a wrapper for GetLastError() and FormatMessage().
212 std::string const getChildErrorMessage();
213 #endif
214
215 } // namespace support
216 } // namespace lyx
217
218 #endif // FORKEDCALLS_H