-rw-r--r-- | libopie/oprocess.h | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/libopie/oprocess.h b/libopie/oprocess.h index bf5fe0e..f2853b0 100644 --- a/libopie/oprocess.h +++ b/libopie/oprocess.h | |||
@@ -1,113 +1,113 @@ | |||
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 | ||
41 | class QSocketNotifier; | 41 | class QSocketNotifier; |
42 | class OProcessPrivate; | 42 | class 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 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. |
@@ -127,129 +127,135 @@ class OProcessPrivate; | |||
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 | **/ |
147 | class OProcess : public QObject | 147 | class OProcess : public QObject |
148 | { | 148 | { |
149 | Q_OBJECT | 149 | Q_OBJECT |
150 | 150 | ||
151 | public: | 151 | public: |
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 | /** | ||
192 | * Constructor | ||
193 | */ | ||
191 | OProcess(const QString &arg0, QObject *parent = 0, const char *name = 0); | 194 | OProcess(const QString &arg0, QObject *parent = 0, const char *name = 0); |
195 | /** | ||
196 | * Constructor | ||
197 | */ | ||
192 | OProcess(const QStringList &args, QObject *parent = 0, const char *name = 0); | 198 | OProcess(const QStringList &args, QObject *parent = 0, const char *name = 0); |
193 | 199 | ||
194 | /** | 200 | /** |
195 | *Destructor: | 201 | *Destructor: |
196 | * | 202 | * |
197 | * If the process is running when the destructor for this class | 203 | * If the process is running when the destructor for this class |
198 | * is called, the child process is killed with a SIGKILL, but | 204 | * is called, the child process is killed with a SIGKILL, but |
199 | * only if the run mode is not of type @p DontCare. | 205 | * only if the run mode is not of type @p DontCare. |
200 | * Processes started as @p DontCare keep running anyway. | 206 | * Processes started as @p DontCare keep running anyway. |
201 | */ | 207 | */ |
202 | virtual ~OProcess(); | 208 | virtual ~OProcess(); |
203 | 209 | ||
204 | /** | 210 | /** |
205 | @deprecated | 211 | @deprecated |
206 | 212 | ||
207 | The use of this function is now deprecated. -- Please use the | 213 | The use of this function is now deprecated. -- Please use the |
208 | "operator<<" instead of "setExecutable". | 214 | "operator<<" instead of "setExecutable". |
209 | 215 | ||
210 | Sets the executable to be started with this OProcess object. | 216 | Sets the executable to be started with this OProcess object. |
211 | Returns false if the process is currently running (in that | 217 | Returns false if the process is currently running (in that |
212 | case the executable remains unchanged.) | 218 | case the executable remains unchanged.) |
213 | 219 | ||
214 | @see operator<< | 220 | @see operator<< |
215 | 221 | ||
216 | */ | 222 | */ |
217 | bool setExecutable(const QString& proc); | 223 | bool setExecutable(const QString& proc); |
218 | 224 | ||
219 | 225 | ||
220 | /** | 226 | /** |
221 | * 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. |
222 | * | 228 | * |
223 | * 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: |
224 | * <pre> | 230 | * <pre> |
225 | * OProcess p; | 231 | * OProcess p; |
226 | * ... | 232 | * ... |
227 | * p << "ls" << "-l" << "/usr/local/bin" | 233 | * p << "ls" << "-l" << "/usr/local/bin" |
228 | * </pre> | 234 | * </pre> |
229 | * | 235 | * |
230 | **/ | 236 | **/ |
231 | OProcess &operator<<(const QString& arg); | 237 | OProcess &operator<<(const QString& arg); |
232 | /** | 238 | /** |
233 | * 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. |
234 | */ | 240 | */ |
235 | OProcess &operator<<(const char * arg); | 241 | OProcess &operator<<(const char * arg); |
236 | /** | 242 | /** |
237 | * 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. |
238 | */ | 244 | */ |
239 | OProcess &operator<<(const QCString & arg); | 245 | OProcess &operator<<(const QCString & arg); |
240 | 246 | ||
241 | /** | 247 | /** |
242 | * 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, |
243 | * in a single method call, or add a list of arguments. | 249 | * in a single method call, or add a list of arguments. |
244 | **/ | 250 | **/ |
245 | OProcess &operator<<(const QStringList& args); | 251 | OProcess &operator<<(const QStringList& args); |
246 | 252 | ||
247 | /** | 253 | /** |
248 | * 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 |
249 | * the "operator<<". | 255 | * the "operator<<". |
250 | */ | 256 | */ |
251 | void clearArguments(); | 257 | void clearArguments(); |
252 | 258 | ||
253 | /** | 259 | /** |
254 | * Starts the process. | 260 | * Starts the process. |
255 | * For a detailed description of the | 261 | * For a detailed description of the |
@@ -349,269 +355,269 @@ public: | |||
349 | * @ref writeStdin() is still in progress. | 355 | * @ref writeStdin() is still in progress. |
350 | * | 356 | * |
351 | * Please note that the data is sent to the client asynchronously, | 357 | * Please note that the data is sent to the client asynchronously, |
352 | * so when this function returns, the data might not have been | 358 | * so when this function returns, the data might not have been |
353 | * processed by the child process. | 359 | * processed by the child process. |
354 | * | 360 | * |
355 | * If all the data has been sent to the client, the signal | 361 | * If all the data has been sent to the client, the signal |
356 | * @ref wroteStdin() will be emitted. | 362 | * @ref wroteStdin() will be emitted. |
357 | * | 363 | * |
358 | * Please note that you must not free "buffer" or call @ref writeStdin() | 364 | * Please note that you must not free "buffer" or call @ref writeStdin() |
359 | * again until either a @ref wroteStdin() signal indicates that the | 365 | * again until either a @ref wroteStdin() signal indicates that the |
360 | * data has been sent or a @ref processHasExited() signal shows that | 366 | * data has been sent or a @ref processHasExited() signal shows that |
361 | * the child process is no longer alive... | 367 | * the child process is no longer alive... |
362 | **/ | 368 | **/ |
363 | bool writeStdin(const char *buffer, int buflen); | 369 | bool writeStdin(const char *buffer, int buflen); |
364 | 370 | ||
365 | void flushStdin(); | 371 | void flushStdin(); |
366 | 372 | ||
367 | /** | 373 | /** |
368 | * This causes the stdin file descriptor of the child process to be | 374 | * This causes the stdin file descriptor of the child process to be |
369 | * closed indicating an "EOF" to the child. | 375 | * closed indicating an "EOF" to the child. |
370 | * | 376 | * |
371 | * @return @p false if no communication to the process's stdin | 377 | * @return @p false if no communication to the process's stdin |
372 | * had been specified in the call to @ref start(). | 378 | * had been specified in the call to @ref start(). |
373 | */ | 379 | */ |
374 | bool closeStdin(); | 380 | bool closeStdin(); |
375 | 381 | ||
376 | /** | 382 | /** |
377 | * This causes the stdout file descriptor of the child process to be | 383 | * This causes the stdout file descriptor of the child process to be |
378 | * closed. | 384 | * closed. |
379 | * | 385 | * |
380 | * @return @p false if no communication to the process's stdout | 386 | * @return @p false if no communication to the process's stdout |
381 | * had been specified in the call to @ref start(). | 387 | * had been specified in the call to @ref start(). |
382 | */ | 388 | */ |
383 | bool closeStdout(); | 389 | bool closeStdout(); |
384 | 390 | ||
385 | /** | 391 | /** |
386 | * This causes the stderr file descriptor of the child process to be | 392 | * This causes the stderr file descriptor of the child process to be |
387 | * closed. | 393 | * closed. |
388 | * | 394 | * |
389 | * @return @p false if no communication to the process's stderr | 395 | * @return @p false if no communication to the process's stderr |
390 | * had been specified in the call to @ref start(). | 396 | * had been specified in the call to @ref start(). |
391 | */ | 397 | */ |
392 | bool closeStderr(); | 398 | bool closeStderr(); |
393 | 399 | ||
394 | /** | 400 | /** |
395 | * Lets you see what your arguments are for debugging. | 401 | * Lets you see what your arguments are for debugging. |
396 | */ | 402 | */ |
397 | 403 | ||
398 | const QValueList<QCString> &args() { return arguments; } | 404 | const QValueList<QCString> &args() { return arguments; } |
399 | 405 | ||
400 | /** | 406 | /** |
401 | * Controls whether the started process should drop any | 407 | * Controls whether the started process should drop any |
402 | * setuid/segid privileges or whether it should keep them | 408 | * setuid/segid privileges or whether it should keep them |
403 | * | 409 | * |
404 | * The default is @p false : drop privileges | 410 | * The default is @p false : drop privileges |
405 | */ | 411 | */ |
406 | void setRunPrivileged(bool keepPrivileges); | 412 | void setRunPrivileged(bool keepPrivileges); |
407 | 413 | ||
408 | /** | 414 | /** |
409 | * Returns whether the started process will drop any | 415 | * Returns whether the started process will drop any |
410 | * setuid/segid privileges or whether it will keep them | 416 | * setuid/segid privileges or whether it will keep them |
411 | */ | 417 | */ |
412 | bool runPrivileged() const; | 418 | bool runPrivileged() const; |
413 | 419 | ||
414 | /** | 420 | /** |
415 | * Modifies the environment of the process to be started. | 421 | * Modifies the environment of the process to be started. |
416 | * This function must be called before starting the process. | 422 | * This function must be called before starting the process. |
417 | */ | 423 | */ |
418 | void setEnvironment(const QString &name, const QString &value); | 424 | void setEnvironment(const QString &name, const QString &value); |
419 | 425 | ||
420 | /** | 426 | /** |
421 | * Changes the current working directory (CWD) of the process | 427 | * Changes the current working directory (CWD) of the process |
422 | * to be started. | 428 | * to be started. |
423 | * This function must be called before starting the process. | 429 | * This function must be called before starting the process. |
424 | */ | 430 | */ |
425 | void setWorkingDirectory(const QString &dir); | 431 | void setWorkingDirectory(const QString &dir); |
426 | 432 | ||
427 | /** | 433 | /** |
428 | * Specify whether to start the command via a shell or directly. | 434 | * Specify whether to start the command via a shell or directly. |
429 | * The default is to start the command directly. | 435 | * The default is to start the command directly. |
430 | * If @p useShell is true @p shell will be used as shell, or | 436 | * If @p useShell is true @p shell will be used as shell, or |
431 | * if shell is empty, the standard shell is used. | 437 | * if shell is empty, the standard shell is used. |
432 | * @p quote A flag indicating whether to quote the arguments. | 438 | * @p quote A flag indicating whether to quote the arguments. |
433 | * | 439 | * |
434 | * When using a shell, the caller should make sure that all filenames etc. | 440 | * When using a shell, the caller should make sure that all filenames etc. |
435 | * are properly quoted when passed as argument. | 441 | * are properly quoted when passed as argument. |
436 | * @see quote() | 442 | * @see quote() |
437 | */ | 443 | */ |
438 | void setUseShell(bool useShell, const char *shell = 0); | 444 | void setUseShell(bool useShell, const char *shell = 0); |
439 | 445 | ||
440 | /** | 446 | /** |
441 | * This function can be used to quote an argument string such that | 447 | * This function can be used to quote an argument string such that |
442 | * the shell processes it properly. This is e. g. necessary for | 448 | * the shell processes it properly. This is e. g. necessary for |
443 | * user-provided file names which may contain spaces or quotes. | 449 | * user-provided file names which may contain spaces or quotes. |
444 | * It also prevents expansion of wild cards and environment variables. | 450 | * It also prevents expansion of wild cards and environment variables. |
445 | */ | 451 | */ |
446 | static QString quote(const QString &arg); | 452 | static QString quote(const QString &arg); |
447 | 453 | ||
448 | /** | 454 | /** |
449 | * Detaches OProcess from child process. All communication is closed. | 455 | * Detaches OProcess from child process. All communication is closed. |
450 | * No exit notification is emitted any more for the child process. | 456 | * No exit notification is emitted any more for the child process. |
451 | * Deleting the OProcess will no longer kill the child process. | 457 | * Deleting the OProcess will no longer kill the child process. |
452 | * Note that the current process remains the parent process of the | 458 | * Note that the current process remains the parent process of the |
453 | * child process. | 459 | * child process. |
454 | */ | 460 | */ |
455 | void detach(); | 461 | void detach(); |
456 | 462 | ||
457 | 463 | ||
458 | 464 | ||
459 | signals: | 465 | signals: |
460 | 466 | ||
461 | /** | 467 | /** |
462 | * Emitted after the process has terminated when | 468 | * Emitted after the process has terminated when |
463 | * the process was run in the @p NotifyOnExit (==default option to | 469 | * the process was run in the @p NotifyOnExit (==default option to |
464 | * @ref start()) or the @ref Block mode. | 470 | * @ref start()) or the @ref Block mode. |
465 | **/ | 471 | **/ |
466 | void processExited(OProcess *proc); | 472 | void processExited(OProcess *proc); |
467 | 473 | ||
468 | 474 | ||
469 | /** | 475 | /** |
470 | * Emitted, when output from the child process has | 476 | * Emitted, when output from the child process has |
471 | * been received on stdout. | 477 | * been received on stdout. |
472 | * | 478 | * |
473 | * To actually get | 479 | * To actually get |
474 | * these signals, the respective communication link (stdout/stderr) | 480 | * these signals, the respective communication link (stdout/stderr) |
475 | * has to be turned on in @ref start(). | 481 | * has to be turned on in @ref start(). |
476 | * | 482 | * |
477 | * @param buffer The data received. | 483 | * @param buffer The data received. |
478 | * @param buflen The number of bytes that are available. | 484 | * @param buflen The number of bytes that are available. |
479 | * | 485 | * |
480 | * You should copy the information contained in @p buffer to your private | 486 | * You should copy the information contained in @p buffer to your private |
481 | * data structures before returning from this slot. | 487 | * data structures before returning from this slot. |
482 | **/ | 488 | **/ |
483 | void receivedStdout(OProcess *proc, char *buffer, int buflen); | 489 | void receivedStdout(OProcess *proc, char *buffer, int buflen); |
484 | 490 | ||
485 | /** | 491 | /** |
486 | * Emitted when output from the child process has | 492 | * Emitted when output from the child process has |
487 | * been received on stdout. | 493 | * been received on stdout. |
488 | * | 494 | * |
489 | * To actually get these signals, the respective communications link | 495 | * To actually get these signals, the respective communications link |
490 | * (stdout/stderr) has to be turned on in @ref start() and the | 496 | * (stdout/stderr) has to be turned on in @ref start() and the |
491 | * @p NoRead flag should have been passed. | 497 | * @p NoRead flag should have been passed. |
492 | * | 498 | * |
493 | * You will need to explicitly call resume() after your call to start() | 499 | * You will need to explicitly call resume() after your call to start() |
494 | * to begin processing data from the child process's stdout. This is | 500 | * to begin processing data from the child process's stdout. This is |
495 | * to ensure that this signal is not emitted when no one is connected | 501 | * to ensure that this signal is not emitted when no one is connected |
496 | * to it, otherwise this signal will not be emitted. | 502 | * to it, otherwise this signal will not be emitted. |
497 | * | 503 | * |
498 | * The data still has to be read from file descriptor @p fd. | 504 | * The data still has to be read from file descriptor @p fd. |
499 | **/ | 505 | **/ |
500 | void receivedStdout(int fd, int &len); | 506 | void receivedStdout(int fd, int &len); |
501 | 507 | ||
502 | 508 | ||
503 | /** | 509 | /** |
504 | * Emitted, when output from the child process has | 510 | * Emitted, when output from the child process has |
505 | * been received on stderr. | 511 | * been received on stderr. |
506 | * To actually get | 512 | * To actually get |
507 | * these signals, the respective communication link (stdout/stderr) | 513 | * these signals, the respective communication link (stdout/stderr) |
508 | * has to be turned on in @ref start(). | 514 | * has to be turned on in @ref start(). |
509 | * | 515 | * |
510 | * @param buffer The data received. | 516 | * @param buffer The data received. |
511 | * @param buflen The number of bytes that are available. | 517 | * @param buflen The number of bytes that are available. |
512 | * | 518 | * |
513 | * You should copy the information contained in @p buffer to your private | 519 | * You should copy the information contained in @p buffer to your private |
514 | * data structures before returning from this slot. | 520 | * data structures before returning from this slot. |
515 | */ | 521 | */ |
516 | void receivedStderr(OProcess *proc, char *buffer, int buflen); | 522 | void receivedStderr(OProcess *proc, char *buffer, int buflen); |
517 | 523 | ||
518 | /** | 524 | /** |
519 | * Emitted after all the data that has been | 525 | * Emitted after all the data that has been |
520 | * specified by a prior call to @ref writeStdin() has actually been | 526 | * specified by a prior call to @ref writeStdin() has actually been |
521 | * written to the child process. | 527 | * written to the child process. |
522 | **/ | 528 | **/ |
523 | void wroteStdin(OProcess *proc); | 529 | void wroteStdin(OProcess *proc); |
524 | 530 | ||
525 | 531 | ||
526 | protected slots: | 532 | protected slots: |
527 | 533 | ||
528 | /** | 534 | /** |
529 | * This slot gets activated when data from the child's stdout arrives. | 535 | * This slot gets activated when data from the child's stdout arrives. |
530 | * It usually calls "childOutput" | 536 | * It usually calls "childOutput" |
531 | */ | 537 | */ |
532 | void slotChildOutput(int fdno); | 538 | void slotChildOutput(int fdno); |
533 | 539 | ||
534 | /** | 540 | /** |
535 | * This slot gets activated when data from the child's stderr arrives. | 541 | * This slot gets activated when data from the child's stderr arrives. |
536 | * It usually calls "childError" | 542 | * It usually calls "childError" |
537 | */ | 543 | */ |
538 | void slotChildError(int fdno); | 544 | void slotChildError(int fdno); |
539 | /* | 545 | /* |
540 | Slot functions for capturing stdout and stderr of the child | 546 | Slot functions for capturing stdout and stderr of the child |
541 | */ | 547 | */ |
542 | 548 | ||
543 | /** | 549 | /** |
544 | * Called when another bulk of data can be sent to the child's | 550 | * Called when another bulk of data can be sent to the child's |
545 | * stdin. If there is no more data to be sent to stdin currently | 551 | * stdin. If there is no more data to be sent to stdin currently |
546 | * available, this function must disable the QSocketNotifier "innot". | 552 | * available, this function must disable the QSocketNotifier "innot". |
547 | */ | 553 | */ |
548 | void slotSendData(int dummy); | 554 | void slotSendData(int dummy); |
549 | 555 | ||
550 | protected: | 556 | protected: |
551 | 557 | ||
552 | /** | 558 | /** |
553 | * Sets up the environment according to the data passed via | 559 | * Sets up the environment according to the data passed via |
554 | * setEnvironment(...) | 560 | * setEnvironment(...) |
555 | */ | 561 | */ |
556 | void setupEnvironment(); | 562 | void setupEnvironment(); |
557 | 563 | ||
558 | /** | 564 | /** |
559 | * The list of the process' command line arguments. The first entry | 565 | * The list of the process' command line arguments. The first entry |
560 | * in this list is the executable itself. | 566 | * in this list is the executable itself. |
561 | */ | 567 | */ |
562 | QValueList<QCString> arguments; | 568 | QValueList<QCString> arguments; |
563 | /** | 569 | /** |
564 | * How to run the process (Block, NotifyOnExit, DontCare). You should | 570 | * How to run the process (Block, NotifyOnExit, DontCare). You should |
565 | * not modify this data member directly from derived classes. | 571 | * not modify this data member directly from derived classes. |
566 | */ | 572 | */ |
567 | RunMode run_mode; | 573 | RunMode run_mode; |
568 | /** | 574 | /** |
569 | * true if the process is currently running. You should not | 575 | * true if the process is currently running. You should not |
570 | * modify this data member directly from derived classes. For | 576 | * modify this data member directly from derived classes. For |
571 | * reading the value of this data member, please use "isRunning()" | 577 | * reading the value of this data member, please use "isRunning()" |
572 | * since "runs" will probably be made private in later versions | 578 | * since "runs" will probably be made private in later versions |
573 | * of OProcess. | 579 | * of OProcess. |
574 | */ | 580 | */ |
575 | bool runs; | 581 | bool runs; |
576 | 582 | ||
577 | /** | 583 | /** |
578 | * The PID of the currently running process (see "getPid()"). | 584 | * The PID of the currently running process (see "getPid()"). |
579 | * You should not modify this data member in derived classes. | 585 | * You should not modify this data member in derived classes. |
580 | * Please use "getPid()" instead of directly accessing this | 586 | * Please use "getPid()" instead of directly accessing this |
581 | * member function since it will probably be made private in | 587 | * member function since it will probably be made private in |
582 | * later versions of OProcess. | 588 | * later versions of OProcess. |
583 | */ | 589 | */ |
584 | pid_t pid_; | 590 | pid_t pid_; |
585 | 591 | ||
586 | /** | 592 | /** |
587 | * The process' exit status as returned by "waitpid". You should not | 593 | * The process' exit status as returned by "waitpid". You should not |
588 | * modify the value of this data member from derived classes. You should | 594 | * modify the value of this data member from derived classes. You should |
589 | * rather use @ref exitStatus than accessing this data member directly | 595 | * rather use @ref exitStatus than accessing this data member directly |
590 | * since it will probably be made private in further versions of | 596 | * since it will probably be made private in further versions of |
591 | * OProcess. | 597 | * OProcess. |
592 | */ | 598 | */ |
593 | int status; | 599 | int status; |
594 | 600 | ||
595 | 601 | ||
596 | /** | 602 | /** |
597 | * See setRunPrivileged() | 603 | * See setRunPrivileged() |
598 | */ | 604 | */ |
599 | bool keepPrivs; | 605 | bool keepPrivs; |
600 | 606 | ||
601 | /* | 607 | /* |
602 | Functions for setting up the sockets for communication. | 608 | Functions for setting up the sockets for communication. |
603 | setupCommunication | 609 | setupCommunication |
604 | -- is called from "start" before "fork"ing. | 610 | -- is called from "start" before "fork"ing. |
605 | commSetupDoneP | 611 | commSetupDoneP |
606 | -- completes communication socket setup in the parent | 612 | -- completes communication socket setup in the parent |
607 | commSetupDoneC | 613 | commSetupDoneC |
608 | -- completes communication setup in the child process | 614 | -- completes communication setup in the child process |
609 | commClose | 615 | commClose |
610 | -- frees all allocated communication resources in the parent | 616 | -- frees all allocated communication resources in the parent |
611 | after the process has exited | 617 | after the process has exited |
612 | */ | 618 | */ |
613 | 619 | ||
614 | /** | 620 | /** |
615 | * This function is called from "OProcess::start" right before a "fork" takes | 621 | * This function is called from "OProcess::start" right before a "fork" takes |
616 | * place. According to | 622 | * place. According to |
617 | * the "comm" parameter this function has to initialize the "in", "out" and | 623 | * the "comm" parameter this function has to initialize the "in", "out" and |
@@ -651,97 +657,97 @@ protected: | |||
651 | /** | 657 | /** |
652 | * Immediately called after a process has exited. This function normally | 658 | * Immediately called after a process has exited. This function normally |
653 | * calls commClose to close all open communication channels to this | 659 | * calls commClose to close all open communication channels to this |
654 | * process and emits the "processExited" signal (if the process was | 660 | * process and emits the "processExited" signal (if the process was |
655 | * not running in the "DontCare" mode). | 661 | * not running in the "DontCare" mode). |
656 | */ | 662 | */ |
657 | virtual void processHasExited(int state); | 663 | virtual void processHasExited(int state); |
658 | 664 | ||
659 | /** | 665 | /** |
660 | * Should clean up the communication links to the child after it has | 666 | * Should clean up the communication links to the child after it has |
661 | * exited. Should be called from "processHasExited". | 667 | * exited. Should be called from "processHasExited". |
662 | */ | 668 | */ |
663 | virtual void commClose(); | 669 | virtual void commClose(); |
664 | 670 | ||
665 | 671 | ||
666 | /** | 672 | /** |
667 | * the socket descriptors for stdin/stdout/stderr. | 673 | * the socket descriptors for stdin/stdout/stderr. |
668 | */ | 674 | */ |
669 | int out[2]; | 675 | int out[2]; |
670 | int in[2]; | 676 | int in[2]; |
671 | int err[2]; | 677 | int err[2]; |
672 | 678 | ||
673 | /** | 679 | /** |
674 | * The socket notifiers for the above socket descriptors. | 680 | * The socket notifiers for the above socket descriptors. |
675 | */ | 681 | */ |
676 | QSocketNotifier *innot; | 682 | QSocketNotifier *innot; |
677 | QSocketNotifier *outnot; | 683 | QSocketNotifier *outnot; |
678 | QSocketNotifier *errnot; | 684 | QSocketNotifier *errnot; |
679 | 685 | ||
680 | /** | 686 | /** |
681 | * Lists the communication links that are activated for the child | 687 | * Lists the communication links that are activated for the child |
682 | * process. Should not be modified from derived classes. | 688 | * process. Should not be modified from derived classes. |
683 | */ | 689 | */ |
684 | Communication communication; | 690 | Communication communication; |
685 | 691 | ||
686 | /** | 692 | /** |
687 | * Called by "slotChildOutput" this function copies data arriving from the | 693 | * Called by "slotChildOutput" this function copies data arriving from the |
688 | * child process's stdout to the respective buffer and emits the signal | 694 | * child process's stdout to the respective buffer and emits the signal |
689 | * "@ref receivedStderr". | 695 | * "@ref receivedStderr". |
690 | */ | 696 | */ |
691 | int childOutput(int fdno); | 697 | int childOutput(int fdno); |
692 | 698 | ||
693 | /** | 699 | /** |
694 | * Called by "slotChildOutput" this function copies data arriving from the | 700 | * Called by "slotChildOutput" this function copies data arriving from the |
695 | * child process's stdout to the respective buffer and emits the signal | 701 | * child process's stdout to the respective buffer and emits the signal |
696 | * "@ref receivedStderr" | 702 | * "@ref receivedStderr" |
697 | */ | 703 | */ |
698 | int childError(int fdno); | 704 | int childError(int fdno); |
699 | 705 | ||
700 | // information about the data that has to be sent to the child: | 706 | // information about the data that has to be sent to the child: |
701 | 707 | ||
702 | const char *input_data; // the buffer holding the data | 708 | const char *input_data; // the buffer holding the data |
703 | int input_sent; // # of bytes already transmitted | 709 | int input_sent; // # of bytes already transmitted |
704 | int input_total; // total length of input_data | 710 | int input_total; // total length of input_data |
705 | 711 | ||
706 | /** | 712 | /** |
707 | * @ref OProcessController is a friend of OProcess because it has to have | 713 | * @ref OProcessController is a friend of OProcess because it has to have |
708 | * access to various data members. | 714 | * access to various data members. |
709 | */ | 715 | */ |
710 | friend class OProcessController; | 716 | friend class OProcessController; |
711 | 717 | ||
712 | 718 | ||
713 | private: | 719 | private: |
714 | /** | 720 | /** |
715 | * Searches for a valid shell. | 721 | * Searches for a valid shell. |
716 | * Here is the algorithm used for finding an executable shell: | 722 | * Here is the algorithm used for finding an executable shell: |
717 | * | 723 | * |
718 | * @li Try the executable pointed to by the "SHELL" environment | 724 | * @li Try the executable pointed to by the "SHELL" environment |
719 | * variable with white spaces stripped off | 725 | * variable with white spaces stripped off |
720 | * | 726 | * |
721 | * @li If your process runs with uid != euid or gid != egid, a shell | 727 | * @li If your process runs with uid != euid or gid != egid, a shell |
722 | * not listed in /etc/shells will not used. | 728 | * not listed in /etc/shells will not used. |
723 | * | 729 | * |
724 | * @li If no valid shell could be found, "/bin/sh" is used as a last resort. | 730 | * @li If no valid shell could be found, "/bin/sh" is used as a last resort. |
725 | */ | 731 | */ |
726 | QCString searchShell(); | 732 | QCString searchShell(); |
727 | 733 | ||
728 | /** | 734 | /** |
729 | * Used by @ref searchShell in order to find out whether the shell found | 735 | * Used by @ref searchShell in order to find out whether the shell found |
730 | * is actually executable at all. | 736 | * is actually executable at all. |
731 | */ | 737 | */ |
732 | bool isExecutable(const QCString &filename); | 738 | bool isExecutable(const QCString &filename); |
733 | 739 | ||
734 | // Disallow assignment and copy-construction | 740 | // Disallow assignment and copy-construction |
735 | OProcess( const OProcess& ); | 741 | OProcess( const OProcess& ); |
736 | OProcess& operator= ( const OProcess& ); | 742 | OProcess& operator= ( const OProcess& ); |
737 | 743 | ||
738 | private: | 744 | private: |
739 | void init ( ); | 745 | void init ( ); |
740 | 746 | ||
741 | OProcessPrivate *d; | 747 | OProcessPrivate *d; |
742 | }; | 748 | }; |
743 | 749 | ||
744 | 750 | ||
745 | 751 | ||
746 | #endif | 752 | #endif |
747 | 753 | ||