]> git.lyx.org Git - lyx.git/blob - src/support/forkedcall.h
Create a grfx::Loader class and so move large chunks of code out of
[lyx.git] / src / support / forkedcall.h
1 // -*- C++ -*-
2 /**
3  *  \file forkedcall.h
4  *  Copyright 2002 the LyX Team
5  *  Read the file COPYING
6  *
7  * \author Asger Alstrup
8  *
9  * Interface cleaned up by
10  * \author Angus Leeming <a.leeming@ic.ac.uk>
11  *
12  * An instance of Class Forkedcall represents a single child process.
13  *
14  * Class Forkedcall uses fork() and execvp() to lauch the child process.
15  *
16  * Once launched, control is returned immediately to the parent process
17  * but a Signal can be emitted upon completion of the child.
18  *
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
21  * function.
22  */
23
24 #ifndef FORKEDCALL_H
25 #define FORKEDCALL_H
26
27 #ifdef __GNUG__
28 #pragma interface
29 #endif
30
31 #include "LString.h"
32
33 #include <boost/shared_ptr.hpp>
34 #include <boost/signals/signal3.hpp>
35
36 #include <sys/types.h>
37
38 class Forkedcall {
39 public:
40         ///
41         enum Starttype {
42                 ///
43                 Wait,
44                 ///
45                 DontWait
46         };
47
48         ///
49         Forkedcall();
50
51         /** Start the child process.
52          *
53          *  The command "what" is passed to fork() for execution.
54          *
55          *  There are two startscript commands available. They differ in that
56          *  the second receives a signal that is executed on completion of
57          *  the command. This makes sense only for a command executed
58          *  in the background, ie DontWait.
59          *
60          *  The other startscript command can be executed either blocking
61          *  or non-blocking, but no signal will be emitted on finishing.
62          */
63         int startscript(Starttype, string const & what);
64
65         /** A SignalType signal is can be emitted once the forked process
66          *  has finished. It passes:
67          *  the commandline string;
68          *  the PID of the child and;
69          *  the return value from the child.
70          *
71          *  We use a signal rather than simply a callback function so that
72          *  we can return easily to C++ methods, rather than just globally
73          *  accessible functions.
74          */
75         typedef boost::signal3<void, string const &, pid_t, int> SignalType;
76
77         /** The signal is connected in the calling routine to the desired
78          *  slot. We pass a shared_ptr rather than a reference to the signal
79          *  because it is eminently possible for the instance of the calling
80          *  class (and hence the signal) to be destructed before the forked
81          *  call is complete.
82          *
83          *  It doesn't matter if the slot disappears, SigC takes care of that.
84          */
85         typedef boost::shared_ptr<SignalType> SignalTypePtr;
86
87         ///
88         int startscript(string const & what, SignalTypePtr);
89
90         /** Invoking the following methods makes sense only if the command
91          *  is running asynchronously!
92          */
93
94         /** gets the PID of the child process.
95          *  Used by the timer.
96          */
97         pid_t pid() const { return pid_; }
98
99         /** Emit the signal.
100          *  Used by the timer.
101          */
102         void emitSignal();
103
104         /** Set the return value of the child process.
105          *  Used by the timer.
106          */
107         void setRetValue(int r) { retval_ = r; }
108
109         /** Kill child prematurely.
110          *  First, a SIGHUP is sent to the child.
111          *  If that does not end the child process within "tolerance"
112          *  seconds, the SIGKILL signal is sent to the child.
113          *  When the child is dead, the callback is called.
114          */
115         void kill(int tolerance = 5);
116         ///
117         string const & command() const { return command_; }
118
119 private:
120         /// Callback function
121         SignalTypePtr signal_;
122
123         /// Commmand line
124         string command_;
125
126         /// Process ID of child
127         pid_t pid_;
128
129         /// Return value from child
130         int retval_;
131
132         ///
133         pid_t generateChild();
134
135         /// Wait for child process to finish. Updates returncode from child.
136         int waitForChild();
137 };
138
139 #endif // FORKEDCALL_H