summaryrefslogtreecommitdiff
path: root/libopie/oprocess.h
Unidiff
Diffstat (limited to 'libopie/oprocess.h') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie/oprocess.h6
1 files changed, 0 insertions, 6 deletions
diff --git a/libopie/oprocess.h b/libopie/oprocess.h
index f2853b0..af7cddb 100644
--- a/libopie/oprocess.h
+++ b/libopie/oprocess.h
@@ -1,753 +1,747 @@
1/* This file is part of the KDE libraries 1/* This file is part of the KDE libraries
2 Copyright (C) 1997 Christian Czezakte (e9025461@student.tuwien.ac.at) 2 Copyright (C) 1997 Christian Czezakte (e9025461@student.tuwien.ac.at)
3 3
4 This library is free software; you can redistribute it and/or 4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public 5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either 6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version. 7 version 2 of the License, or (at your option) any later version.
8 8
9 This library is distributed in the hope that it will be useful, 9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details. 12 Library General Public License for more details.
13 13
14 You should have received a copy of the GNU Library General Public License 14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to 15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. 17 Boston, MA 02111-1307, USA.
18*/ 18*/
19// 19//
20// KPROCESS -- A class for handling child processes in KDE without 20// KPROCESS -- A class for handling child processes in KDE without
21// having to take care of Un*x specific implementation details 21// having to take care of Un*x specific implementation details
22// 22//
23// version 0.3.1, Jan 8th 1998 23// version 0.3.1, Jan 8th 1998
24// 24//
25// (C) Christian Czezatke 25// (C) Christian Czezatke
26// e9025461@student.tuwien.ac.at 26// e9025461@student.tuwien.ac.at
27// Ported by Holger Freyther to the Open Palmtop Integrated Environment 27// Ported by Holger Freyther to the Open Palmtop Integrated Environment
28// 28//
29 29
30#ifndef __kprocess_h__ 30#ifndef __kprocess_h__
31#define __kprocess_h__ 31#define __kprocess_h__
32 32
33#include <sys/types.h> // for pid_t 33#include <sys/types.h> // for pid_t
34#include <sys/wait.h> 34#include <sys/wait.h>
35#include <signal.h> 35#include <signal.h>
36#include <unistd.h> 36#include <unistd.h>
37#include <qvaluelist.h> 37#include <qvaluelist.h>
38#include <qcstring.h> 38#include <qcstring.h>
39#include <qobject.h> 39#include <qobject.h>
40 40
41class QSocketNotifier; 41class QSocketNotifier;
42class OProcessPrivate; 42class OProcessPrivate;
43 43
44/** 44/**
45 * Child process invocation, monitoring and control. 45 * Child process invocation, monitoring and control.
46 * 46 *
47 * @sect General usage and features 47 * @sect General usage and features
48 * 48 *
49 *This class allows a KDE and OPIE application to start child processes without having 49 *This class allows a KDE and OPIE application to start child processes without having
50 *to worry about UN*X signal handling issues and zombie process reaping. 50 *to worry about UN*X signal handling issues and zombie process reaping.
51 * 51 *
52 *@see KProcIO 52 *@see KProcIO
53 * 53 *
54 *Basically, this class distinguishes three different ways of running 54 *Basically, this class distinguishes three different ways of running
55 *child processes: 55 *child processes:
56 * 56 *
57 *@li OProcess::DontCare -- The child process is invoked and both the child 57 *@li OProcess::DontCare -- The child process is invoked and both the child
58 *process and the parent process continue concurrently. 58 *process and the parent process continue concurrently.
59 * 59 *
60 *Starting a DontCare child process means that the application is 60 *Starting a DontCare child process means that the application is
61 *not interested in any notification to determine whether the 61 *not interested in any notification to determine whether the
62 *child process has already exited or not. 62 *child process has already exited or not.
63 * 63 *
64 *@li OProcess::NotifyOnExit -- The child process is invoked and both the 64 *@li OProcess::NotifyOnExit -- The child process is invoked and both the
65 *child and the parent process run concurrently. 65 *child and the parent process run concurrently.
66 * 66 *
67 *When the child process exits, the OProcess instance 67 *When the child process exits, the OProcess instance
68 *corresponding to it emits the Qt signal @ref processExited(). 68 *corresponding to it emits the Qt signal @ref processExited().
69 * 69 *
70 *Since this signal is @em not emitted from within a UN*X 70 *Since this signal is @em not emitted from within a UN*X
71 *signal handler, arbitrary function calls can be made. 71 *signal handler, arbitrary function calls can be made.
72 * 72 *
73 *Be aware: When the OProcess objects gets destructed, the child 73 *Be aware: When the OProcess objects gets destructed, the child
74 *process will be killed if it is still running! 74 *process will be killed if it is still running!
75 *This means in particular, that you cannot use a OProcess on the stack 75 *This means in particular, that you cannot use a OProcess on the stack
76 *with OProcess::NotifyOnExit. 76 *with OProcess::NotifyOnExit.
77 * 77 *
78 *@li OProcess::Block -- The child process starts and the parent process 78 *@li OProcess::Block -- The child process starts and the parent process
79 *is suspended until the child process exits. (@em Really not recommended 79 *is suspended until the child process exits. (@em Really not recommended
80 *for programs with a GUI.) 80 *for programs with a GUI.)
81 * 81 *
82 *OProcess also provides several functions for determining the exit status 82 *OProcess also provides several functions for determining the exit status
83 *and the pid of the child process it represents. 83 *and the pid of the child process it represents.
84 * 84 *
85 *Furthermore it is possible to supply command-line arguments to the process 85 *Furthermore it is possible to supply command-line arguments to the process
86 *in a clean fashion (no null -- terminated stringlists and such...) 86 *in a clean fashion (no null -- terminated stringlists and such...)
87 * 87 *
88 *A small usage example: 88 *A small usage example:
89 *<pre> 89 *<pre>
90 *OProcess *proc = new OProcess; 90 *OProcess *proc = new OProcess;
91 * 91 *
92 **proc << "my_executable"; 92 **proc << "my_executable";
93 **proc << "These" << "are" << "the" << "command" << "line" << "args"; 93 **proc << "These" << "are" << "the" << "command" << "line" << "args";
94 *QApplication::connect(proc, SIGNAL(processExited(OProcess *)), 94 *QApplication::connect(proc, SIGNAL(processExited(OProcess *)),
95 * pointer_to_my_object, SLOT(my_objects_slot(OProcess *))); 95 * pointer_to_my_object, SLOT(my_objects_slot(OProcess *)));
96 *proc->start(); 96 *proc->start();
97 *</pre> 97 *</pre>
98 * 98 *
99 *This will start "my_executable" with the commandline arguments "These"... 99 *This will start "my_executable" with the commandline arguments "These"...
100 * 100 *
101 *When the child process exits, the respective Qt signal will be emitted. 101 *When the child process exits, the respective Qt signal will be emitted.
102 * 102 *
103 *@sect Communication with the child process 103 *@sect Communication with the child process
104 * 104 *
105 *OProcess supports communication with the child process through 105 *OProcess supports communication with the child process through
106 *stdin/stdout/stderr. 106 *stdin/stdout/stderr.
107 * 107 *
108 *The following functions are provided for getting data from the child 108 *The following functions are provided for getting data from the child
109 *process or sending data to the child's stdin (For more information, 109 *process or sending data to the child's stdin (For more information,
110 *have a look at the documentation of each function): 110 *have a look at the documentation of each function):
111 * 111 *
112 *@li bool @ref writeStdin(char *buffer, int buflen); 112 *@li bool @ref writeStdin(char *buffer, int buflen);
113 *@li -- Transmit data to the child process's stdin. 113 *@li -- Transmit data to the child process's stdin.
114 * 114 *
115 *@li bool @ref closeStdin(); 115 *@li bool @ref closeStdin();
116 *@li -- Closes the child process's stdin (which causes it to see an feof(stdin)). 116 *@li -- Closes the child process's stdin (which causes it to see an feof(stdin)).
117 *Returns false if you try to close stdin for a process that has been started 117 *Returns false if you try to close stdin for a process that has been started
118 *without a communication channel to stdin. 118 *without a communication channel to stdin.
119 * 119 *
120 *@li bool @ref closeStdout(); 120 *@li bool @ref closeStdout();
121 *@li -- Closes the child process's stdout. 121 *@li -- Closes the child process's stdout.
122 *Returns false if you try to close stdout for a process that has been started 122 *Returns false if you try to close stdout for a process that has been started
123 *without a communication channel to stdout. 123 *without a communication channel to stdout.
124 * 124 *
125 *@li bool @ref closeStderr(); 125 *@li bool @ref closeStderr();
126 *@li -- Closes the child process's stderr. 126 *@li -- Closes the child process's stderr.
127 *Returns false if you try to close stderr for a process that has been started 127 *Returns false if you try to close stderr for a process that has been started
128 *without a communication channel to stderr. 128 *without a communication channel to stderr.
129 * 129 *
130 * 130 *
131 *@sect QT signals: 131 *@sect QT signals:
132 * 132 *
133 *@li void @ref receivedStdout(OProcess *proc, char *buffer, int buflen); 133 *@li void @ref receivedStdout(OProcess *proc, char *buffer, int buflen);
134 *@li void @ref receivedStderr(OProcess *proc, char *buffer, int buflen); 134 *@li void @ref receivedStderr(OProcess *proc, char *buffer, int buflen);
135 *@li -- Indicates that new data has arrived from either the 135 *@li -- Indicates that new data has arrived from either the
136 *child process's stdout or stderr. 136 *child process's stdout or stderr.
137 * 137 *
138 *@li void @ref wroteStdin(OProcess *proc); 138 *@li void @ref wroteStdin(OProcess *proc);
139 *@li -- Indicates that all data that has been sent to the child process 139 *@li -- Indicates that all data that has been sent to the child process
140 *by a prior call to @ref writeStdin() has actually been transmitted to the 140 *by a prior call to @ref writeStdin() has actually been transmitted to the
141 *client . 141 *client .
142 * 142 *
143 *@author Christian Czezakte e9025461@student.tuwien.ac.at 143 *@author Christian Czezakte e9025461@student.tuwien.ac.at
144 * 144 *
145 * 145 *
146 **/ 146 **/
147class OProcess : public QObject 147class OProcess : public QObject
148{ 148{
149 Q_OBJECT 149 Q_OBJECT
150 150
151public: 151public:
152 152
153 /** 153 /**
154 * Modes in which the communication channel can be opened. 154 * Modes in which the communication channel can be opened.
155 * 155 *
156 * If communication for more than one channel is required, 156 * If communication for more than one channel is required,
157 * the values have to be or'ed together, for example to get 157 * the values have to be or'ed together, for example to get
158 * communication with stdout as well as with stdin, you would 158 * communication with stdout as well as with stdin, you would
159 * specify @p Stdin @p | @p Stdout 159 * specify @p Stdin @p | @p Stdout
160 * 160 *
161 * If @p NoRead is specified in conjunction with @p Stdout, 161 * If @p NoRead is specified in conjunction with @p Stdout,
162 * no data is actually read from @p Stdout but only 162 * no data is actually read from @p Stdout but only
163 * the signal @ref childOutput(int fd) is emitted. 163 * the signal @ref childOutput(int fd) is emitted.
164 */ 164 */
165 enum Communication { NoCommunication = 0, Stdin = 1, Stdout = 2, Stderr = 4, 165 enum Communication { NoCommunication = 0, Stdin = 1, Stdout = 2, Stderr = 4,
166 AllOutput = 6, All = 7, 166 AllOutput = 6, All = 7,
167 NoRead }; 167 NoRead };
168 168
169 /** 169 /**
170 * Run-modes for a child process. 170 * Run-modes for a child process.
171 */ 171 */
172 enum RunMode { 172 enum RunMode {
173 /** 173 /**
174 * The application does not receive notifications from the subprocess when 174 * The application does not receive notifications from the subprocess when
175 * it is finished or aborted. 175 * it is finished or aborted.
176 */ 176 */
177 DontCare, 177 DontCare,
178 /** 178 /**
179 * The application is notified when the subprocess dies. 179 * The application is notified when the subprocess dies.
180 */ 180 */
181 NotifyOnExit, 181 NotifyOnExit,
182 /** 182 /**
183 * The application is suspended until the started process is finished. 183 * The application is suspended until the started process is finished.
184 */ 184 */
185 Block }; 185 Block };
186 186
187 /** 187 /**
188 * Constructor 188 * Constructor
189 */ 189 */
190 OProcess(QObject *parent = 0, const char *name = 0); 190 OProcess(QObject *parent = 0, const char *name = 0);
191 /** 191 /**
192 * Constructor 192 * Constructor
193 */ 193 */
194 OProcess(const QString &arg0, QObject *parent = 0, const char *name = 0); 194 OProcess(const QString &arg0, QObject *parent = 0, const char *name = 0);
195 /** 195 /**
196 * Constructor 196 * Constructor
197 */ 197 */
198 OProcess(const QStringList &args, QObject *parent = 0, const char *name = 0); 198 OProcess(const QStringList &args, QObject *parent = 0, const char *name = 0);
199 199
200 /** 200 /**
201 *Destructor: 201 *Destructor:
202 * 202 *
203 * If the process is running when the destructor for this class 203 * If the process is running when the destructor for this class
204 * is called, the child process is killed with a SIGKILL, but 204 * is called, the child process is killed with a SIGKILL, but
205 * only if the run mode is not of type @p DontCare. 205 * only if the run mode is not of type @p DontCare.
206 * Processes started as @p DontCare keep running anyway. 206 * Processes started as @p DontCare keep running anyway.
207 */ 207 */
208 virtual ~OProcess(); 208 virtual ~OProcess();
209 209
210 /** 210 /**
211 @deprecated 211 @deprecated
212 212
213 The use of this function is now deprecated. -- Please use the 213 The use of this function is now deprecated. -- Please use the
214 "operator<<" instead of "setExecutable". 214 "operator<<" instead of "setExecutable".
215 215
216 Sets the executable to be started with this OProcess object. 216 Sets the executable to be started with this OProcess object.
217 Returns false if the process is currently running (in that 217 Returns false if the process is currently running (in that
218 case the executable remains unchanged.) 218 case the executable remains unchanged.)
219 219
220 @see operator<< 220 @see operator<<
221 221
222 */ 222 */
223 bool setExecutable(const QString& proc); 223 bool setExecutable(const QString& proc);
224 224
225 225
226 /** 226 /**
227 * Sets the executable and the command line argument list for this process. 227 * Sets the executable and the command line argument list for this process.
228 * 228 *
229 * For example, doing an "ls -l /usr/local/bin" can be achieved by: 229 * For example, doing an "ls -l /usr/local/bin" can be achieved by:
230 * <pre> 230 * <pre>
231 * OProcess p; 231 * OProcess p;
232 * ... 232 * ...
233 * p << "ls" << "-l" << "/usr/local/bin" 233 * p << "ls" << "-l" << "/usr/local/bin"
234 * </pre> 234 * </pre>
235 * 235 *
236 **/ 236 **/
237 OProcess &operator<<(const QString& arg); 237 OProcess &operator<<(const QString& arg);
238 /** 238 /**
239 * Similar to previous method, takes a char *, supposed to be in locale 8 bit already. 239 * Similar to previous method, takes a char *, supposed to be in locale 8 bit already.
240 */ 240 */
241 OProcess &operator<<(const char * arg); 241 OProcess &operator<<(const char * arg);
242 /** 242 /**
243 * Similar to previous method, takes a QCString, supposed to be in locale 8 bit already. 243 * Similar to previous method, takes a QCString, supposed to be in locale 8 bit already.
244 */ 244 */
245 OProcess &operator<<(const QCString & arg); 245 OProcess &operator<<(const QCString & arg);
246 246
247 /** 247 /**
248 * Sets the executable and the command line argument list for this process, 248 * Sets the executable and the command line argument list for this process,
249 * in a single method call, or add a list of arguments. 249 * in a single method call, or add a list of arguments.
250 **/ 250 **/
251 OProcess &operator<<(const QStringList& args); 251 OProcess &operator<<(const QStringList& args);
252 252
253 /** 253 /**
254 * Clear a command line argument list that has been set by using 254 * Clear a command line argument list that has been set by using
255 * the "operator<<". 255 * the "operator<<".
256 */ 256 */
257 void clearArguments(); 257 void clearArguments();
258 258
259 /** 259 /**
260 * Starts the process. 260 * Starts the process.
261 * For a detailed description of the 261 * For a detailed description of the
262 * various run modes and communication semantics, have a look at the 262 * various run modes and communication semantics, have a look at the
263 * general description of the OProcess class. 263 * general description of the OProcess class.
264 * 264 *
265 * The following problems could cause this function to 265 * The following problems could cause this function to
266 * return false: 266 * return false:
267 * 267 *
268 * @li The process is already running. 268 * @li The process is already running.
269 * @li The command line argument list is empty. 269 * @li The command line argument list is empty.
270 * @li The starting of the process failed (could not fork). 270 * @li The starting of the process failed (could not fork).
271 * @li The executable was not found. 271 * @li The executable was not found.
272 * 272 *
273 * @param comm Specifies which communication links should be 273 * @param comm Specifies which communication links should be
274 * established to the child process (stdin/stdout/stderr). By default, 274 * established to the child process (stdin/stdout/stderr). By default,
275 * no communication takes place and the respective communication 275 * no communication takes place and the respective communication
276 * signals will never get emitted. 276 * signals will never get emitted.
277 * 277 *
278 * @return true on success, false on error 278 * @return true on success, false on error
279 * (see above for error conditions) 279 * (see above for error conditions)
280 **/ 280 **/
281 virtual bool start(RunMode runmode = NotifyOnExit, 281 virtual bool start(RunMode runmode = NotifyOnExit,
282 Communication comm = NoCommunication); 282 Communication comm = NoCommunication);
283 283
284 /** 284 /**
285 * Stop the process (by sending it a signal). 285 * Stop the process (by sending it a signal).
286 * 286 *
287 * @param signoThe signal to send. The default is SIGTERM. 287 * @param signoThe signal to send. The default is SIGTERM.
288 * @return @p true if the signal was delivered successfully. 288 * @return @p true if the signal was delivered successfully.
289 */ 289 */
290 virtual bool kill(int signo = SIGTERM); 290 virtual bool kill(int signo = SIGTERM);
291 291
292 /** 292 /**
293 @return @p true if the process is (still) considered to be running 293 @return @p true if the process is (still) considered to be running
294 */ 294 */
295 bool isRunning() const; 295 bool isRunning() const;
296 296
297 /** Returns the process id of the process. 297 /** Returns the process id of the process.
298 * 298 *
299 * If it is called after 299 * If it is called after
300 * the process has exited, it returns the process id of the last 300 * the process has exited, it returns the process id of the last
301 * child process that was created by this instance of OProcess. 301 * child process that was created by this instance of OProcess.
302 * 302 *
303 * Calling it before any child process has been started by this 303 * Calling it before any child process has been started by this
304 * OProcess instance causes pid() to return 0. 304 * OProcess instance causes pid() to return 0.
305 **/ 305 **/
306 pid_t pid() const; 306 pid_t pid() const;
307 307
308 /** 308 /**
309 * Use pid().
310 * @deprecated
311 */
312 pid_t getPid() const { return pid(); }
313
314 /**
315 * Suspend processing of data from stdout of the child process. 309 * Suspend processing of data from stdout of the child process.
316 */ 310 */
317 void suspend(); 311 void suspend();
318 312
319 /** 313 /**
320 * Resume processing of data from stdout of the child process. 314 * Resume processing of data from stdout of the child process.
321 */ 315 */
322 void resume(); 316 void resume();
323 317
324 /** 318 /**
325 * @return @p true if the process has already finished and has exited 319 * @return @p true if the process has already finished and has exited
326 * "voluntarily", ie: it has not been killed by a signal. 320 * "voluntarily", ie: it has not been killed by a signal.
327 * 321 *
328 * Note that you should check @ref OProcess::exitStatus() to determine 322 * Note that you should check @ref OProcess::exitStatus() to determine
329 * whether the process completed its task successful or not. 323 * whether the process completed its task successful or not.
330 */ 324 */
331 bool normalExit() const; 325 bool normalExit() const;
332 326
333 /** 327 /**
334 * Returns the exit status of the process. 328 * Returns the exit status of the process.
335 * 329 *
336 * Please use 330 * Please use
337 * @ref OProcess::normalExit() to check whether the process has exited 331 * @ref OProcess::normalExit() to check whether the process has exited
338 * cleanly (i.e., @ref OProcess::normalExit() returns @p true) before calling 332 * cleanly (i.e., @ref OProcess::normalExit() returns @p true) before calling
339 * this function because if the process did not exit normally, 333 * this function because if the process did not exit normally,
340 * it does not have a valid exit status. 334 * it does not have a valid exit status.
341 */ 335 */
342 int exitStatus() const; 336 int exitStatus() const;
343 337
344 338
345 /** 339 /**
346 * Transmit data to the child process's stdin. 340 * Transmit data to the child process's stdin.
347 * 341 *
348 * OProcess::writeStdin may return false in the following cases: 342 * OProcess::writeStdin may return false in the following cases:
349 * 343 *
350 * @li The process is not currently running. 344 * @li The process is not currently running.
351 * 345 *
352 * @li Communication to stdin has not been requested in the @ref start() call. 346 * @li Communication to stdin has not been requested in the @ref start() call.
353 * 347 *
354 * @li Transmission of data to the child process by a previous call to 348 * @li Transmission of data to the child process by a previous call to
355 * @ref writeStdin() is still in progress. 349 * @ref writeStdin() is still in progress.
356 * 350 *
357 * Please note that the data is sent to the client asynchronously, 351 * Please note that the data is sent to the client asynchronously,
358 * so when this function returns, the data might not have been 352 * so when this function returns, the data might not have been
359 * processed by the child process. 353 * processed by the child process.
360 * 354 *
361 * If all the data has been sent to the client, the signal 355 * If all the data has been sent to the client, the signal
362 * @ref wroteStdin() will be emitted. 356 * @ref wroteStdin() will be emitted.
363 * 357 *
364 * Please note that you must not free "buffer" or call @ref writeStdin() 358 * Please note that you must not free "buffer" or call @ref writeStdin()
365 * again until either a @ref wroteStdin() signal indicates that the 359 * again until either a @ref wroteStdin() signal indicates that the
366 * data has been sent or a @ref processHasExited() signal shows that 360 * data has been sent or a @ref processHasExited() signal shows that
367 * the child process is no longer alive... 361 * the child process is no longer alive...
368 **/ 362 **/
369 bool writeStdin(const char *buffer, int buflen); 363 bool writeStdin(const char *buffer, int buflen);
370 364
371 void flushStdin(); 365 void flushStdin();
372 366
373 /** 367 /**
374 * This causes the stdin file descriptor of the child process to be 368 * This causes the stdin file descriptor of the child process to be
375 * closed indicating an "EOF" to the child. 369 * closed indicating an "EOF" to the child.
376 * 370 *
377 * @return @p false if no communication to the process's stdin 371 * @return @p false if no communication to the process's stdin
378 * had been specified in the call to @ref start(). 372 * had been specified in the call to @ref start().
379 */ 373 */
380 bool closeStdin(); 374 bool closeStdin();
381 375
382 /** 376 /**
383 * This causes the stdout file descriptor of the child process to be 377 * This causes the stdout file descriptor of the child process to be
384 * closed. 378 * closed.
385 * 379 *
386 * @return @p false if no communication to the process's stdout 380 * @return @p false if no communication to the process's stdout
387 * had been specified in the call to @ref start(). 381 * had been specified in the call to @ref start().
388 */ 382 */
389 bool closeStdout(); 383 bool closeStdout();
390 384
391 /** 385 /**
392 * This causes the stderr file descriptor of the child process to be 386 * This causes the stderr file descriptor of the child process to be
393 * closed. 387 * closed.
394 * 388 *
395 * @return @p false if no communication to the process's stderr 389 * @return @p false if no communication to the process's stderr
396 * had been specified in the call to @ref start(). 390 * had been specified in the call to @ref start().
397 */ 391 */
398 bool closeStderr(); 392 bool closeStderr();
399 393
400 /** 394 /**
401 * Lets you see what your arguments are for debugging. 395 * Lets you see what your arguments are for debugging.
402 */ 396 */
403 397
404 const QValueList<QCString> &args() { return arguments; } 398 const QValueList<QCString> &args() { return arguments; }
405 399
406 /** 400 /**
407 * Controls whether the started process should drop any 401 * Controls whether the started process should drop any
408 * setuid/segid privileges or whether it should keep them 402 * setuid/segid privileges or whether it should keep them
409 * 403 *
410 * The default is @p false : drop privileges 404 * The default is @p false : drop privileges
411 */ 405 */
412 void setRunPrivileged(bool keepPrivileges); 406 void setRunPrivileged(bool keepPrivileges);
413 407
414 /** 408 /**
415 * Returns whether the started process will drop any 409 * Returns whether the started process will drop any
416 * setuid/segid privileges or whether it will keep them 410 * setuid/segid privileges or whether it will keep them
417 */ 411 */
418 bool runPrivileged() const; 412 bool runPrivileged() const;
419 413
420 /** 414 /**
421 * Modifies the environment of the process to be started. 415 * Modifies the environment of the process to be started.
422 * This function must be called before starting the process. 416 * This function must be called before starting the process.
423 */ 417 */
424 void setEnvironment(const QString &name, const QString &value); 418 void setEnvironment(const QString &name, const QString &value);
425 419
426 /** 420 /**
427 * Changes the current working directory (CWD) of the process 421 * Changes the current working directory (CWD) of the process
428 * to be started. 422 * to be started.
429 * This function must be called before starting the process. 423 * This function must be called before starting the process.
430 */ 424 */
431 void setWorkingDirectory(const QString &dir); 425 void setWorkingDirectory(const QString &dir);
432 426
433 /** 427 /**
434 * Specify whether to start the command via a shell or directly. 428 * Specify whether to start the command via a shell or directly.
435 * The default is to start the command directly. 429 * The default is to start the command directly.
436 * If @p useShell is true @p shell will be used as shell, or 430 * If @p useShell is true @p shell will be used as shell, or
437 * if shell is empty, the standard shell is used. 431 * if shell is empty, the standard shell is used.
438 * @p quote A flag indicating whether to quote the arguments. 432 * @p quote A flag indicating whether to quote the arguments.
439 * 433 *
440 * When using a shell, the caller should make sure that all filenames etc. 434 * When using a shell, the caller should make sure that all filenames etc.
441 * are properly quoted when passed as argument. 435 * are properly quoted when passed as argument.
442 * @see quote() 436 * @see quote()
443 */ 437 */
444 void setUseShell(bool useShell, const char *shell = 0); 438 void setUseShell(bool useShell, const char *shell = 0);
445 439
446 /** 440 /**
447 * This function can be used to quote an argument string such that 441 * This function can be used to quote an argument string such that
448 * the shell processes it properly. This is e. g. necessary for 442 * the shell processes it properly. This is e. g. necessary for
449 * user-provided file names which may contain spaces or quotes. 443 * user-provided file names which may contain spaces or quotes.
450 * It also prevents expansion of wild cards and environment variables. 444 * It also prevents expansion of wild cards and environment variables.
451 */ 445 */
452 static QString quote(const QString &arg); 446 static QString quote(const QString &arg);
453 447
454 /** 448 /**
455 * Detaches OProcess from child process. All communication is closed. 449 * Detaches OProcess from child process. All communication is closed.
456 * No exit notification is emitted any more for the child process. 450 * No exit notification is emitted any more for the child process.
457 * Deleting the OProcess will no longer kill the child process. 451 * Deleting the OProcess will no longer kill the child process.
458 * Note that the current process remains the parent process of the 452 * Note that the current process remains the parent process of the
459 * child process. 453 * child process.
460 */ 454 */
461 void detach(); 455 void detach();
462 456
463 457
464 458
465signals: 459signals:
466 460
467 /** 461 /**
468 * Emitted after the process has terminated when 462 * Emitted after the process has terminated when
469 * the process was run in the @p NotifyOnExit (==default option to 463 * the process was run in the @p NotifyOnExit (==default option to
470 * @ref start()) or the @ref Block mode. 464 * @ref start()) or the @ref Block mode.
471 **/ 465 **/
472 void processExited(OProcess *proc); 466 void processExited(OProcess *proc);
473 467
474 468
475 /** 469 /**
476 * Emitted, when output from the child process has 470 * Emitted, when output from the child process has
477 * been received on stdout. 471 * been received on stdout.
478 * 472 *
479 * To actually get 473 * To actually get
480 * these signals, the respective communication link (stdout/stderr) 474 * these signals, the respective communication link (stdout/stderr)
481 * has to be turned on in @ref start(). 475 * has to be turned on in @ref start().
482 * 476 *
483 * @param buffer The data received. 477 * @param buffer The data received.
484 * @param buflen The number of bytes that are available. 478 * @param buflen The number of bytes that are available.
485 * 479 *
486 * You should copy the information contained in @p buffer to your private 480 * You should copy the information contained in @p buffer to your private
487 * data structures before returning from this slot. 481 * data structures before returning from this slot.
488 **/ 482 **/
489 void receivedStdout(OProcess *proc, char *buffer, int buflen); 483 void receivedStdout(OProcess *proc, char *buffer, int buflen);
490 484
491 /** 485 /**
492 * Emitted when output from the child process has 486 * Emitted when output from the child process has
493 * been received on stdout. 487 * been received on stdout.
494 * 488 *
495 * To actually get these signals, the respective communications link 489 * To actually get these signals, the respective communications link
496 * (stdout/stderr) has to be turned on in @ref start() and the 490 * (stdout/stderr) has to be turned on in @ref start() and the
497 * @p NoRead flag should have been passed. 491 * @p NoRead flag should have been passed.
498 * 492 *
499 * You will need to explicitly call resume() after your call to start() 493 * You will need to explicitly call resume() after your call to start()
500 * to begin processing data from the child process's stdout. This is 494 * to begin processing data from the child process's stdout. This is
501 * to ensure that this signal is not emitted when no one is connected 495 * to ensure that this signal is not emitted when no one is connected
502 * to it, otherwise this signal will not be emitted. 496 * to it, otherwise this signal will not be emitted.
503 * 497 *
504 * The data still has to be read from file descriptor @p fd. 498 * The data still has to be read from file descriptor @p fd.
505 **/ 499 **/
506 void receivedStdout(int fd, int &len); 500 void receivedStdout(int fd, int &len);
507 501
508 502
509 /** 503 /**
510 * Emitted, when output from the child process has 504 * Emitted, when output from the child process has
511 * been received on stderr. 505 * been received on stderr.
512 * To actually get 506 * To actually get
513 * these signals, the respective communication link (stdout/stderr) 507 * these signals, the respective communication link (stdout/stderr)
514 * has to be turned on in @ref start(). 508 * has to be turned on in @ref start().
515 * 509 *
516 * @param buffer The data received. 510 * @param buffer The data received.
517 * @param buflen The number of bytes that are available. 511 * @param buflen The number of bytes that are available.
518 * 512 *
519 * You should copy the information contained in @p buffer to your private 513 * You should copy the information contained in @p buffer to your private
520 * data structures before returning from this slot. 514 * data structures before returning from this slot.
521 */ 515 */
522 void receivedStderr(OProcess *proc, char *buffer, int buflen); 516 void receivedStderr(OProcess *proc, char *buffer, int buflen);
523 517
524 /** 518 /**
525 * Emitted after all the data that has been 519 * Emitted after all the data that has been
526 * specified by a prior call to @ref writeStdin() has actually been 520 * specified by a prior call to @ref writeStdin() has actually been
527 * written to the child process. 521 * written to the child process.
528 **/ 522 **/
529 void wroteStdin(OProcess *proc); 523 void wroteStdin(OProcess *proc);
530 524
531 525
532protected slots: 526protected slots:
533 527
534 /** 528 /**
535 * This slot gets activated when data from the child's stdout arrives. 529 * This slot gets activated when data from the child's stdout arrives.
536 * It usually calls "childOutput" 530 * It usually calls "childOutput"
537 */ 531 */
538 void slotChildOutput(int fdno); 532 void slotChildOutput(int fdno);
539 533
540 /** 534 /**
541 * This slot gets activated when data from the child's stderr arrives. 535 * This slot gets activated when data from the child's stderr arrives.
542 * It usually calls "childError" 536 * It usually calls "childError"
543 */ 537 */
544 void slotChildError(int fdno); 538 void slotChildError(int fdno);
545 /* 539 /*
546 Slot functions for capturing stdout and stderr of the child 540 Slot functions for capturing stdout and stderr of the child
547 */ 541 */
548 542
549 /** 543 /**
550 * Called when another bulk of data can be sent to the child's 544 * Called when another bulk of data can be sent to the child's
551 * stdin. If there is no more data to be sent to stdin currently 545 * stdin. If there is no more data to be sent to stdin currently
552 * available, this function must disable the QSocketNotifier "innot". 546 * available, this function must disable the QSocketNotifier "innot".
553 */ 547 */
554 void slotSendData(int dummy); 548 void slotSendData(int dummy);
555 549
556protected: 550protected:
557 551
558 /** 552 /**
559 * Sets up the environment according to the data passed via 553 * Sets up the environment according to the data passed via
560 * setEnvironment(...) 554 * setEnvironment(...)
561 */ 555 */
562 void setupEnvironment(); 556 void setupEnvironment();
563 557
564 /** 558 /**
565 * The list of the process' command line arguments. The first entry 559 * The list of the process' command line arguments. The first entry
566 * in this list is the executable itself. 560 * in this list is the executable itself.
567 */ 561 */
568 QValueList<QCString> arguments; 562 QValueList<QCString> arguments;
569 /** 563 /**
570 * How to run the process (Block, NotifyOnExit, DontCare). You should 564 * How to run the process (Block, NotifyOnExit, DontCare). You should
571 * not modify this data member directly from derived classes. 565 * not modify this data member directly from derived classes.
572 */ 566 */
573 RunMode run_mode; 567 RunMode run_mode;
574 /** 568 /**
575 * true if the process is currently running. You should not 569 * true if the process is currently running. You should not
576 * modify this data member directly from derived classes. For 570 * modify this data member directly from derived classes. For
577 * reading the value of this data member, please use "isRunning()" 571 * reading the value of this data member, please use "isRunning()"
578 * since "runs" will probably be made private in later versions 572 * since "runs" will probably be made private in later versions
579 * of OProcess. 573 * of OProcess.
580 */ 574 */
581 bool runs; 575 bool runs;
582 576
583 /** 577 /**
584 * The PID of the currently running process (see "getPid()"). 578 * The PID of the currently running process (see "getPid()").
585 * You should not modify this data member in derived classes. 579 * You should not modify this data member in derived classes.
586 * Please use "getPid()" instead of directly accessing this 580 * Please use "getPid()" instead of directly accessing this
587 * member function since it will probably be made private in 581 * member function since it will probably be made private in
588 * later versions of OProcess. 582 * later versions of OProcess.
589 */ 583 */
590 pid_t pid_; 584 pid_t pid_;
591 585
592 /** 586 /**
593 * The process' exit status as returned by "waitpid". You should not 587 * The process' exit status as returned by "waitpid". You should not
594 * modify the value of this data member from derived classes. You should 588 * modify the value of this data member from derived classes. You should
595 * rather use @ref exitStatus than accessing this data member directly 589 * rather use @ref exitStatus than accessing this data member directly
596 * since it will probably be made private in further versions of 590 * since it will probably be made private in further versions of
597 * OProcess. 591 * OProcess.
598 */ 592 */
599 int status; 593 int status;
600 594
601 595
602 /** 596 /**
603 * See setRunPrivileged() 597 * See setRunPrivileged()
604 */ 598 */
605 bool keepPrivs; 599 bool keepPrivs;
606 600
607 /* 601 /*
608 Functions for setting up the sockets for communication. 602 Functions for setting up the sockets for communication.
609 setupCommunication 603 setupCommunication
610 -- is called from "start" before "fork"ing. 604 -- is called from "start" before "fork"ing.
611 commSetupDoneP 605 commSetupDoneP
612 -- completes communication socket setup in the parent 606 -- completes communication socket setup in the parent
613 commSetupDoneC 607 commSetupDoneC
614 -- completes communication setup in the child process 608 -- completes communication setup in the child process
615 commClose 609 commClose
616 -- frees all allocated communication resources in the parent 610 -- frees all allocated communication resources in the parent
617 after the process has exited 611 after the process has exited
618 */ 612 */
619 613
620 /** 614 /**
621 * This function is called from "OProcess::start" right before a "fork" takes 615 * This function is called from "OProcess::start" right before a "fork" takes
622 * place. According to 616 * place. According to
623 * the "comm" parameter this function has to initialize the "in", "out" and 617 * the "comm" parameter this function has to initialize the "in", "out" and
624 * "err" data member of OProcess. 618 * "err" data member of OProcess.
625 * 619 *
626 * This function should return 0 if setting the needed communication channels 620 * This function should return 0 if setting the needed communication channels
627 * was successful. 621 * was successful.
628 * 622 *
629 * The default implementation is to create UNIX STREAM sockets for the communication, 623 * The default implementation is to create UNIX STREAM sockets for the communication,
630 * but you could overload this function and establish a TCP/IP communication for 624 * but you could overload this function and establish a TCP/IP communication for
631 * network communication, for example. 625 * network communication, for example.
632 */ 626 */
633 virtual int setupCommunication(Communication comm); 627 virtual int setupCommunication(Communication comm);
634 628
635 /** 629 /**
636 * Called right after a (successful) fork on the parent side. This function 630 * Called right after a (successful) fork on the parent side. This function
637 * will usually do some communications cleanup, like closing the reading end 631 * will usually do some communications cleanup, like closing the reading end
638 * of the "stdin" communication channel. 632 * of the "stdin" communication channel.
639 * 633 *
640 * Furthermore, it must also create the QSocketNotifiers "innot", "outnot" and 634 * Furthermore, it must also create the QSocketNotifiers "innot", "outnot" and
641 * "errnot" and connect their Qt slots to the respective OProcess member functions. 635 * "errnot" and connect their Qt slots to the respective OProcess member functions.
642 * 636 *
643 * For a more detailed explanation, it is best to have a look at the default 637 * For a more detailed explanation, it is best to have a look at the default
644 * implementation of "setupCommunication" in kprocess.cpp. 638 * implementation of "setupCommunication" in kprocess.cpp.
645 */ 639 */
646 virtual int commSetupDoneP(); 640 virtual int commSetupDoneP();
647 641
648 /** 642 /**
649 * Called right after a (successful) fork, but before an "exec" on the child 643 * Called right after a (successful) fork, but before an "exec" on the child
650 * process' side. It usually just closes the unused communication ends of 644 * process' side. It usually just closes the unused communication ends of
651 * "in", "out" and "err" (like the writing end of the "in" communication 645 * "in", "out" and "err" (like the writing end of the "in" communication
652 * channel. 646 * channel.
653 */ 647 */
654 virtual int commSetupDoneC(); 648 virtual int commSetupDoneC();
655 649
656 650
657 /** 651 /**
658 * Immediately called after a process has exited. This function normally 652 * Immediately called after a process has exited. This function normally
659 * calls commClose to close all open communication channels to this 653 * calls commClose to close all open communication channels to this
660 * process and emits the "processExited" signal (if the process was 654 * process and emits the "processExited" signal (if the process was
661 * not running in the "DontCare" mode). 655 * not running in the "DontCare" mode).
662 */ 656 */
663 virtual void processHasExited(int state); 657 virtual void processHasExited(int state);
664 658
665 /** 659 /**
666 * Should clean up the communication links to the child after it has 660 * Should clean up the communication links to the child after it has
667 * exited. Should be called from "processHasExited". 661 * exited. Should be called from "processHasExited".
668 */ 662 */
669 virtual void commClose(); 663 virtual void commClose();
670 664
671 665
672 /** 666 /**
673 * the socket descriptors for stdin/stdout/stderr. 667 * the socket descriptors for stdin/stdout/stderr.
674 */ 668 */
675 int out[2]; 669 int out[2];
676 int in[2]; 670 int in[2];
677 int err[2]; 671 int err[2];
678 672
679 /** 673 /**
680 * The socket notifiers for the above socket descriptors. 674 * The socket notifiers for the above socket descriptors.
681 */ 675 */
682 QSocketNotifier *innot; 676 QSocketNotifier *innot;
683 QSocketNotifier *outnot; 677 QSocketNotifier *outnot;
684 QSocketNotifier *errnot; 678 QSocketNotifier *errnot;
685 679
686 /** 680 /**
687 * Lists the communication links that are activated for the child 681 * Lists the communication links that are activated for the child
688 * process. Should not be modified from derived classes. 682 * process. Should not be modified from derived classes.
689 */ 683 */
690 Communication communication; 684 Communication communication;
691 685
692 /** 686 /**
693 * Called by "slotChildOutput" this function copies data arriving from the 687 * Called by "slotChildOutput" this function copies data arriving from the
694 * child process's stdout to the respective buffer and emits the signal 688 * child process's stdout to the respective buffer and emits the signal
695 * "@ref receivedStderr". 689 * "@ref receivedStderr".
696 */ 690 */
697 int childOutput(int fdno); 691 int childOutput(int fdno);
698 692
699 /** 693 /**
700 * Called by "slotChildOutput" this function copies data arriving from the 694 * Called by "slotChildOutput" this function copies data arriving from the
701 * child process's stdout to the respective buffer and emits the signal 695 * child process's stdout to the respective buffer and emits the signal
702 * "@ref receivedStderr" 696 * "@ref receivedStderr"
703 */ 697 */
704 int childError(int fdno); 698 int childError(int fdno);
705 699
706 // information about the data that has to be sent to the child: 700 // information about the data that has to be sent to the child:
707 701
708 const char *input_data; // the buffer holding the data 702 const char *input_data; // the buffer holding the data
709 int input_sent; // # of bytes already transmitted 703 int input_sent; // # of bytes already transmitted
710 int input_total; // total length of input_data 704 int input_total; // total length of input_data
711 705
712 /** 706 /**
713 * @ref OProcessController is a friend of OProcess because it has to have 707 * @ref OProcessController is a friend of OProcess because it has to have
714 * access to various data members. 708 * access to various data members.
715 */ 709 */
716 friend class OProcessController; 710 friend class OProcessController;
717 711
718 712
719private: 713private:
720 /** 714 /**
721 * Searches for a valid shell. 715 * Searches for a valid shell.
722 * Here is the algorithm used for finding an executable shell: 716 * Here is the algorithm used for finding an executable shell:
723 * 717 *
724 * @li Try the executable pointed to by the "SHELL" environment 718 * @li Try the executable pointed to by the "SHELL" environment
725 * variable with white spaces stripped off 719 * variable with white spaces stripped off
726 * 720 *
727 * @li If your process runs with uid != euid or gid != egid, a shell 721 * @li If your process runs with uid != euid or gid != egid, a shell
728 * not listed in /etc/shells will not used. 722 * not listed in /etc/shells will not used.
729 * 723 *
730 * @li If no valid shell could be found, "/bin/sh" is used as a last resort. 724 * @li If no valid shell could be found, "/bin/sh" is used as a last resort.
731 */ 725 */
732 QCString searchShell(); 726 QCString searchShell();
733 727
734 /** 728 /**
735 * Used by @ref searchShell in order to find out whether the shell found 729 * Used by @ref searchShell in order to find out whether the shell found
736 * is actually executable at all. 730 * is actually executable at all.
737 */ 731 */
738 bool isExecutable(const QCString &filename); 732 bool isExecutable(const QCString &filename);
739 733
740 // Disallow assignment and copy-construction 734 // Disallow assignment and copy-construction
741 OProcess( const OProcess& ); 735 OProcess( const OProcess& );
742 OProcess& operator= ( const OProcess& ); 736 OProcess& operator= ( const OProcess& );
743 737
744private: 738private:
745 void init ( ); 739 void init ( );
746 740
747 OProcessPrivate *d; 741 OProcessPrivate *d;
748}; 742};
749 743
750 744
751 745
752#endif 746#endif
753 747