-rw-r--r-- | libopie/oprocess.cpp | 94 |
1 files changed, 49 insertions, 45 deletions
diff --git a/libopie/oprocess.cpp b/libopie/oprocess.cpp index f3e52bd..5db2b6c 100644 --- a/libopie/oprocess.cpp +++ b/libopie/oprocess.cpp | |||
@@ -1,922 +1,926 @@ | |||
1 | /* | 1 | /* |
2 | 2 | ||
3 | $Id$ | 3 | $Id$ |
4 | 4 | ||
5 | This file is part of the KDE libraries | 5 | This file is part of the KDE libraries |
6 | Copyright (C) 1997 Christian Czezatke (e9025461@student.tuwien.ac.at) | 6 | Copyright (C) 1997 Christian Czezatke (e9025461@student.tuwien.ac.at) |
7 | 7 | ||
8 | This library is free software; you can redistribute it and/or | 8 | This library is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU Library General Public | 9 | modify it under the terms of the GNU Library General Public |
10 | License as published by the Free Software Foundation; either | 10 | License as published by the Free Software Foundation; either |
11 | version 2 of the License, or (at your option) any later version. | 11 | version 2 of the License, or (at your option) any later version. |
12 | 12 | ||
13 | This library is distributed in the hope that it will be useful, | 13 | This library is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | Library General Public License for more details. | 16 | Library General Public License for more details. |
17 | 17 | ||
18 | You should have received a copy of the GNU Library General Public License | 18 | You should have received a copy of the GNU Library General Public License |
19 | along with this library; see the file COPYING.LIB. If not, write to | 19 | along with this library; see the file COPYING.LIB. If not, write to |
20 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 20 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. | 21 | Boston, MA 02111-1307, USA. |
22 | 22 | ||
23 | */ | 23 | */ |
24 | 24 | ||
25 | 25 | ||
26 | // | 26 | // |
27 | // KPROCESS -- A class for handling child processes in KDE without | 27 | // KPROCESS -- A class for handling child processes in KDE without |
28 | // having to take care of Un*x specific implementation details | 28 | // having to take care of Un*x specific implementation details |
29 | // | 29 | // |
30 | // version 0.3.1, Jan 8th 1998 | 30 | // version 0.3.1, Jan 8th 1998 |
31 | // | 31 | // |
32 | // (C) Christian Czezatke | 32 | // (C) Christian Czezatke |
33 | // e9025461@student.tuwien.ac.at | 33 | // e9025461@student.tuwien.ac.at |
34 | // | 34 | // |
35 | // Changes: | 35 | // Changes: |
36 | // | 36 | // |
37 | // March 2nd, 1998: Changed parameter list for KShellProcess: | 37 | // March 2nd, 1998: Changed parameter list for KShellProcess: |
38 | // Arguments are now placed in a single string so that | 38 | // Arguments are now placed in a single string so that |
39 | // <shell> -c <commandstring> is passed to the shell | 39 | // <shell> -c <commandstring> is passed to the shell |
40 | // to make the use of "operator<<" consistent with KProcess | 40 | // to make the use of "operator<<" consistent with KProcess |
41 | // | 41 | // |
42 | // | 42 | // |
43 | // Ported by Holger Freyther | 43 | // Ported by Holger Freyther |
44 | // <zekce> Harlekin: oprocess and say it was ported to Qt by the Opie developers an Qt 2 | 44 | // <zekce> Harlekin: oprocess and say it was ported to Qt by the Opie developers an Qt 2 |
45 | 45 | ||
46 | 46 | ||
47 | 47 | ||
48 | #include "oprocess.h" | 48 | #include "oprocess.h" |
49 | #define _MAY_INCLUDE_KPROCESSCONTROLLER_ | 49 | #define _MAY_INCLUDE_KPROCESSCONTROLLER_ |
50 | #include "oprocctrl.h" | 50 | #include "oprocctrl.h" |
51 | 51 | ||
52 | //#include <config.h> | 52 | //#include <config.h> |
53 | 53 | ||
54 | #include <qfile.h> | 54 | #include <qfile.h> |
55 | #include <qsocketnotifier.h> | 55 | #include <qsocketnotifier.h> |
56 | #include <qregexp.h> | 56 | #include <qregexp.h> |
57 | 57 | ||
58 | #include <sys/time.h> | 58 | #include <sys/time.h> |
59 | #include <sys/types.h> | 59 | #include <sys/types.h> |
60 | #include <sys/stat.h> | 60 | #include <sys/stat.h> |
61 | #include <sys/socket.h> | 61 | #include <sys/socket.h> |
62 | 62 | ||
63 | #include <errno.h> | 63 | #include <errno.h> |
64 | #include <fcntl.h> | 64 | #include <fcntl.h> |
65 | #include <stdlib.h> | 65 | #include <stdlib.h> |
66 | #include <signal.h> | 66 | #include <signal.h> |
67 | #include <stdio.h> | 67 | #include <stdio.h> |
68 | #include <string.h> | 68 | #include <string.h> |
69 | #include <unistd.h> | 69 | #include <unistd.h> |
70 | #ifdef HAVE_SYS_SELECT_H | 70 | #ifdef HAVE_SYS_SELECT_H |
71 | #include <sys/select.h> | 71 | #include <sys/select.h> |
72 | #endif | 72 | #endif |
73 | #ifdef HAVE_INITGROUPS | 73 | #ifdef HAVE_INITGROUPS |
74 | #include <grp.h> | 74 | #include <grp.h> |
75 | #endif | 75 | #endif |
76 | #include <pwd.h> | 76 | #include <pwd.h> |
77 | 77 | ||
78 | #include <qapplication.h> | 78 | #include <qapplication.h> |
79 | #include <qmap.h> | 79 | #include <qmap.h> |
80 | //#include <kdebug.h> | 80 | //#include <kdebug.h> |
81 | 81 | ||
82 | ///////////////////////////// | 82 | ///////////////////////////// |
83 | // public member functions // | 83 | // public member functions // |
84 | ///////////////////////////// | 84 | ///////////////////////////// |
85 | 85 | ||
86 | class OProcessPrivate { | 86 | class OProcessPrivate { |
87 | public: | 87 | public: |
88 | OProcessPrivate() : useShell(false) { } | 88 | OProcessPrivate() : useShell(false) { } |
89 | 89 | ||
90 | bool useShell; | 90 | bool useShell; |
91 | QMap<QString,QString> env; | 91 | QMap<QString,QString> env; |
92 | QString wd; | 92 | QString wd; |
93 | QCString shell; | 93 | QCString shell; |
94 | }; | 94 | }; |
95 | 95 | ||
96 | 96 | ||
97 | OProcess::OProcess() | 97 | OProcess::OProcess(QObject *parent, const char *name) |
98 | : QObject(), | 98 | : QObject(parent, name) |
99 | run_mode(NotifyOnExit), | ||
100 | runs(false), | ||
101 | pid_(0), | ||
102 | status(0), | ||
103 | keepPrivs(false), | ||
104 | innot(0), | ||
105 | outnot(0), | ||
106 | errnot(0), | ||
107 | communication(NoCommunication), | ||
108 | input_data(0), | ||
109 | input_sent(0), | ||
110 | input_total(0), | ||
111 | d(0) | ||
112 | { | 99 | { |
100 | init ( ); | ||
101 | } | ||
102 | |||
103 | OProcess::OProcess(const QString &arg0, QObject *parent, const char *name) | ||
104 | : QObject(parent, name) | ||
105 | { | ||
106 | init ( ); | ||
107 | *this << arg0; | ||
108 | } | ||
109 | |||
110 | OProcess::OProcess(const QStringList &args, QObject *parent, const char *name) | ||
111 | : QObject(parent, name) | ||
112 | { | ||
113 | init ( ); | ||
114 | *this << args; | ||
115 | } | ||
116 | |||
117 | void OProcess::init ( ) | ||
118 | { | ||
119 | run_mode = NotifyOnExit; | ||
120 | runs = false; | ||
121 | pid_ = 0; | ||
122 | status = 0; | ||
123 | keepPrivs = false; | ||
124 | innot = 0; | ||
125 | outnot = 0; | ||
126 | errnot = 0; | ||
127 | communication = NoCommunication; | ||
128 | input_data = 0; | ||
129 | input_sent = 0; | ||
130 | input_total = 0; | ||
131 | d = 0; | ||
132 | |||
113 | if (0 == OProcessController::theOProcessController) { | 133 | if (0 == OProcessController::theOProcessController) { |
114 | (void) new OProcessController(); | 134 | (void) new OProcessController(); |
115 | CHECK_PTR(OProcessController::theOProcessController); | 135 | CHECK_PTR(OProcessController::theOProcessController); |
116 | } | 136 | } |
117 | 137 | ||
118 | OProcessController::theOProcessController->addOProcess(this); | 138 | OProcessController::theOProcessController->addOProcess(this); |
119 | out[0] = out[1] = -1; | 139 | out[0] = out[1] = -1; |
120 | in[0] = in[1] = -1; | 140 | in[0] = in[1] = -1; |
121 | err[0] = err[1] = -1; | 141 | err[0] = err[1] = -1; |
122 | } | 142 | } |
123 | 143 | ||
124 | void | 144 | void |
125 | OProcess::setEnvironment(const QString &name, const QString &value) | 145 | OProcess::setEnvironment(const QString &name, const QString &value) |
126 | { | 146 | { |
127 | if (!d) | 147 | if (!d) |
128 | d = new OProcessPrivate; | 148 | d = new OProcessPrivate; |
129 | d->env.insert(name, value); | 149 | d->env.insert(name, value); |
130 | } | 150 | } |
131 | 151 | ||
132 | void | 152 | void |
133 | OProcess::setWorkingDirectory(const QString &dir) | 153 | OProcess::setWorkingDirectory(const QString &dir) |
134 | { | 154 | { |
135 | if (!d) | 155 | if (!d) |
136 | d = new OProcessPrivate; | 156 | d = new OProcessPrivate; |
137 | d->wd = dir; | 157 | d->wd = dir; |
138 | } | 158 | } |
139 | 159 | ||
140 | void | 160 | void |
141 | OProcess::setupEnvironment() | 161 | OProcess::setupEnvironment() |
142 | { | 162 | { |
143 | if (d) | 163 | if (d) |
144 | { | 164 | { |
145 | QMap<QString,QString>::Iterator it; | 165 | QMap<QString,QString>::Iterator it; |
146 | for(it = d->env.begin(); it != d->env.end(); ++it) | 166 | for(it = d->env.begin(); it != d->env.end(); ++it) |
147 | setenv(QFile::encodeName(it.key()).data(), | 167 | setenv(QFile::encodeName(it.key()).data(), |
148 | QFile::encodeName(it.data()).data(), 1); | 168 | QFile::encodeName(it.data()).data(), 1); |
149 | if (!d->wd.isEmpty()) | 169 | if (!d->wd.isEmpty()) |
150 | chdir(QFile::encodeName(d->wd).data()); | 170 | chdir(QFile::encodeName(d->wd).data()); |
151 | } | 171 | } |
152 | } | 172 | } |
153 | 173 | ||
154 | void | 174 | void |
155 | OProcess::setRunPrivileged(bool keepPrivileges) | 175 | OProcess::setRunPrivileged(bool keepPrivileges) |
156 | { | 176 | { |
157 | keepPrivs = keepPrivileges; | 177 | keepPrivs = keepPrivileges; |
158 | } | 178 | } |
159 | 179 | ||
160 | bool | 180 | bool |
161 | OProcess::runPrivileged() const | 181 | OProcess::runPrivileged() const |
162 | { | 182 | { |
163 | return keepPrivs; | 183 | return keepPrivs; |
164 | } | 184 | } |
165 | 185 | ||
166 | 186 | ||
167 | OProcess::~OProcess() | 187 | OProcess::~OProcess() |
168 | { | 188 | { |
169 | // destroying the OProcess instance sends a SIGKILL to the | 189 | // destroying the OProcess instance sends a SIGKILL to the |
170 | // child process (if it is running) after removing it from the | 190 | // child process (if it is running) after removing it from the |
171 | // list of valid processes (if the process is not started as | 191 | // list of valid processes (if the process is not started as |
172 | // "DontCare") | 192 | // "DontCare") |
173 | 193 | ||
174 | OProcessController::theOProcessController->removeOProcess(this); | 194 | OProcessController::theOProcessController->removeOProcess(this); |
175 | // this must happen before we kill the child | 195 | // this must happen before we kill the child |
176 | // TODO: block the signal while removing the current process from the process list | 196 | // TODO: block the signal while removing the current process from the process list |
177 | 197 | ||
178 | if (runs && (run_mode != DontCare)) | 198 | if (runs && (run_mode != DontCare)) |
179 | kill(SIGKILL); | 199 | kill(SIGKILL); |
180 | 200 | ||
181 | // Clean up open fd's and socket notifiers. | 201 | // Clean up open fd's and socket notifiers. |
182 | closeStdin(); | 202 | closeStdin(); |
183 | closeStdout(); | 203 | closeStdout(); |
184 | closeStderr(); | 204 | closeStderr(); |
185 | 205 | ||
186 | // TODO: restore SIGCHLD and SIGPIPE handler if this is the last OProcess | 206 | // TODO: restore SIGCHLD and SIGPIPE handler if this is the last OProcess |
187 | delete d; | 207 | delete d; |
188 | } | 208 | } |
189 | 209 | ||
190 | void OProcess::detach() | 210 | void OProcess::detach() |
191 | { | 211 | { |
192 | OProcessController::theOProcessController->removeOProcess(this); | 212 | OProcessController::theOProcessController->removeOProcess(this); |
193 | 213 | ||
194 | runs = false; | 214 | runs = false; |
195 | pid_ = 0; | 215 | pid_ = 0; |
196 | 216 | ||
197 | // Clean up open fd's and socket notifiers. | 217 | // Clean up open fd's and socket notifiers. |
198 | closeStdin(); | 218 | closeStdin(); |
199 | closeStdout(); | 219 | closeStdout(); |
200 | closeStderr(); | 220 | closeStderr(); |
201 | } | 221 | } |
202 | 222 | ||
203 | bool OProcess::setExecutable(const QString& proc) | 223 | bool OProcess::setExecutable(const QString& proc) |
204 | { | 224 | { |
205 | if (runs) return false; | 225 | if (runs) return false; |
206 | 226 | ||
207 | if (proc.isEmpty()) return false; | 227 | if (proc.isEmpty()) return false; |
208 | 228 | ||
209 | if (!arguments.isEmpty()) | 229 | if (!arguments.isEmpty()) |
210 | arguments.remove(arguments.begin()); | 230 | arguments.remove(arguments.begin()); |
211 | arguments.prepend(QFile::encodeName(proc)); | 231 | arguments.prepend(QFile::encodeName(proc)); |
212 | 232 | ||
213 | return true; | 233 | return true; |
214 | } | 234 | } |
215 | 235 | ||
216 | OProcess &OProcess::operator<<(const QStringList& args) | 236 | OProcess &OProcess::operator<<(const QStringList& args) |
217 | { | 237 | { |
218 | QStringList::ConstIterator it = args.begin(); | 238 | QStringList::ConstIterator it = args.begin(); |
219 | for ( ; it != args.end() ; ++it ) | 239 | for ( ; it != args.end() ; ++it ) |
220 | arguments.append(QFile::encodeName(*it)); | 240 | arguments.append(QFile::encodeName(*it)); |
221 | return *this; | 241 | return *this; |
222 | } | 242 | } |
223 | 243 | ||
224 | OProcess &OProcess::operator<<(const QCString& arg) | 244 | OProcess &OProcess::operator<<(const QCString& arg) |
225 | { | 245 | { |
226 | return operator<< (arg.data()); | 246 | return operator<< (arg.data()); |
227 | } | 247 | } |
228 | 248 | ||
229 | OProcess &OProcess::operator<<(const char* arg) | 249 | OProcess &OProcess::operator<<(const char* arg) |
230 | { | 250 | { |
231 | arguments.append(arg); | 251 | arguments.append(arg); |
232 | return *this; | 252 | return *this; |
233 | } | 253 | } |
234 | 254 | ||
235 | OProcess &OProcess::operator<<(const QString& arg) | 255 | OProcess &OProcess::operator<<(const QString& arg) |
236 | { | 256 | { |
237 | arguments.append(QFile::encodeName(arg)); | 257 | arguments.append(QFile::encodeName(arg)); |
238 | return *this; | 258 | return *this; |
239 | } | 259 | } |
240 | 260 | ||
241 | void OProcess::clearArguments() | 261 | void OProcess::clearArguments() |
242 | { | 262 | { |
243 | arguments.clear(); | 263 | arguments.clear(); |
244 | } | 264 | } |
245 | 265 | ||
246 | bool OProcess::start(RunMode runmode, Communication comm) | 266 | bool OProcess::start(RunMode runmode, Communication comm) |
247 | { | 267 | { |
248 | uint i; | 268 | uint i; |
249 | uint n = arguments.count(); | 269 | uint n = arguments.count(); |
250 | char **arglist; | 270 | char **arglist; |
251 | 271 | ||
252 | if (runs || (0 == n)) { | 272 | if (runs || (0 == n)) { |
253 | return false; // cannot start a process that is already running | 273 | return false; // cannot start a process that is already running |
254 | // or if no executable has been assigned | 274 | // or if no executable has been assigned |
255 | } | 275 | } |
256 | run_mode = runmode; | 276 | run_mode = runmode; |
257 | status = 0; | 277 | status = 0; |
258 | 278 | ||
259 | QCString shellCmd; | 279 | QCString shellCmd; |
260 | if (d && d->useShell) | 280 | if (d && d->useShell) |
261 | { | 281 | { |
262 | if (d->shell.isEmpty()) | 282 | if (d->shell.isEmpty()) |
263 | { | 283 | { |
264 | qWarning( "Could not find a valid shell" ); | 284 | qWarning( "Could not find a valid shell" ); |
265 | return false; | 285 | return false; |
266 | } | 286 | } |
267 | 287 | ||
268 | arglist = static_cast<char **>(malloc( (4)*sizeof(char *))); | 288 | arglist = static_cast<char **>(malloc( (4)*sizeof(char *))); |
269 | for (i=0; i < n; i++) { | 289 | for (i=0; i < n; i++) { |
270 | shellCmd += arguments[i]; | 290 | shellCmd += arguments[i]; |
271 | shellCmd += " "; // CC: to separate the arguments | 291 | shellCmd += " "; // CC: to separate the arguments |
272 | } | 292 | } |
273 | 293 | ||
274 | arglist[0] = d->shell.data(); | 294 | arglist[0] = d->shell.data(); |
275 | arglist[1] = (char *) "-c"; | 295 | arglist[1] = (char *) "-c"; |
276 | arglist[2] = shellCmd.data(); | 296 | arglist[2] = shellCmd.data(); |
277 | arglist[3] = 0; | 297 | arglist[3] = 0; |
278 | } | 298 | } |
279 | else | 299 | else |
280 | { | 300 | { |
281 | arglist = static_cast<char **>(malloc( (n+1)*sizeof(char *))); | 301 | arglist = static_cast<char **>(malloc( (n+1)*sizeof(char *))); |
282 | for (i=0; i < n; i++) | 302 | for (i=0; i < n; i++) |
283 | arglist[i] = arguments[i].data(); | 303 | arglist[i] = arguments[i].data(); |
284 | arglist[n]= 0; | 304 | arglist[n]= 0; |
285 | } | 305 | } |
286 | 306 | ||
287 | if (!setupCommunication(comm)) | 307 | if (!setupCommunication(comm)) |
288 | qWarning( "Could not setup Communication!"); | 308 | qWarning( "Could not setup Communication!"); |
289 | 309 | ||
290 | // We do this in the parent because if we do it in the child process | 310 | // We do this in the parent because if we do it in the child process |
291 | // gdb gets confused when the application runs from gdb. | 311 | // gdb gets confused when the application runs from gdb. |
292 | uid_t uid = getuid(); | 312 | uid_t uid = getuid(); |
293 | gid_t gid = getgid(); | 313 | gid_t gid = getgid(); |
294 | #ifdef HAVE_INITGROUPS | 314 | #ifdef HAVE_INITGROUPS |
295 | struct passwd *pw = getpwuid(uid); | 315 | struct passwd *pw = getpwuid(uid); |
296 | #endif | 316 | #endif |
297 | 317 | ||
298 | int fd[2]; | 318 | int fd[2]; |
299 | if (0 > pipe(fd)) | 319 | if (0 > pipe(fd)) |
300 | { | 320 | { |
301 | fd[0] = fd[1] = 0; // Pipe failed.. continue | 321 | fd[0] = fd[1] = 0; // Pipe failed.. continue |
302 | } | 322 | } |
303 | 323 | ||
304 | runs = true; | 324 | runs = true; |
305 | 325 | ||
306 | QApplication::flushX(); | 326 | QApplication::flushX(); |
307 | 327 | ||
308 | // WABA: Note that we use fork() and not vfork() because | 328 | // WABA: Note that we use fork() and not vfork() because |
309 | // vfork() has unclear semantics and is not standardized. | 329 | // vfork() has unclear semantics and is not standardized. |
310 | pid_ = fork(); | 330 | pid_ = fork(); |
311 | 331 | ||
312 | if (0 == pid_) { | 332 | if (0 == pid_) { |
313 | if (fd[0]) | 333 | if (fd[0]) |
314 | close(fd[0]); | 334 | close(fd[0]); |
315 | if (!runPrivileged()) | 335 | if (!runPrivileged()) |
316 | { | 336 | { |
317 | setgid(gid); | 337 | setgid(gid); |
318 | #if defined( HAVE_INITGROUPS) | 338 | #if defined( HAVE_INITGROUPS) |
319 | if(pw) | 339 | if(pw) |
320 | initgroups(pw->pw_name, pw->pw_gid); | 340 | initgroups(pw->pw_name, pw->pw_gid); |
321 | #endif | 341 | #endif |
322 | setuid(uid); | 342 | setuid(uid); |
323 | } | 343 | } |
324 | // The child process | 344 | // The child process |
325 | if(!commSetupDoneC()) | 345 | if(!commSetupDoneC()) |
326 | qWarning( "Could not finish comm setup in child!" ); | 346 | qWarning( "Could not finish comm setup in child!" ); |
327 | 347 | ||
328 | setupEnvironment(); | 348 | setupEnvironment(); |
329 | 349 | ||
330 | // Matthias | 350 | // Matthias |
331 | if (run_mode == DontCare) | 351 | if (run_mode == DontCare) |
332 | setpgid(0,0); | 352 | setpgid(0,0); |
333 | // restore default SIGPIPE handler (Harri) | 353 | // restore default SIGPIPE handler (Harri) |
334 | struct sigaction act; | 354 | struct sigaction act; |
335 | sigemptyset(&(act.sa_mask)); | 355 | sigemptyset(&(act.sa_mask)); |
336 | sigaddset(&(act.sa_mask), SIGPIPE); | 356 | sigaddset(&(act.sa_mask), SIGPIPE); |
337 | act.sa_handler = SIG_DFL; | 357 | act.sa_handler = SIG_DFL; |
338 | act.sa_flags = 0; | 358 | act.sa_flags = 0; |
339 | sigaction(SIGPIPE, &act, 0L); | 359 | sigaction(SIGPIPE, &act, 0L); |
340 | 360 | ||
341 | // We set the close on exec flag. | 361 | // We set the close on exec flag. |
342 | // Closing of fd[1] indicates that the execvp succeeded! | 362 | // Closing of fd[1] indicates that the execvp succeeded! |
343 | if (fd[1]) | 363 | if (fd[1]) |
344 | fcntl(fd[1], F_SETFD, FD_CLOEXEC); | 364 | fcntl(fd[1], F_SETFD, FD_CLOEXEC); |
345 | execvp(arglist[0], arglist); | 365 | execvp(arglist[0], arglist); |
346 | char resultByte = 1; | 366 | char resultByte = 1; |
347 | if (fd[1]) | 367 | if (fd[1]) |
348 | write(fd[1], &resultByte, 1); | 368 | write(fd[1], &resultByte, 1); |
349 | _exit(-1); | 369 | _exit(-1); |
350 | } else if (-1 == pid_) { | 370 | } else if (-1 == pid_) { |
351 | // forking failed | 371 | // forking failed |
352 | 372 | ||
353 | runs = false; | 373 | runs = false; |
354 | free(arglist); | 374 | free(arglist); |
355 | return false; | 375 | return false; |
356 | } else { | 376 | } else { |
357 | if (fd[1]) | 377 | if (fd[1]) |
358 | close(fd[1]); | 378 | close(fd[1]); |
359 | // the parent continues here | 379 | // the parent continues here |
360 | 380 | ||
361 | // Discard any data for stdin that might still be there | 381 | // Discard any data for stdin that might still be there |
362 | input_data = 0; | 382 | input_data = 0; |
363 | 383 | ||
364 | // Check whether client could be started. | 384 | // Check whether client could be started. |
365 | if (fd[0]) for(;;) | 385 | if (fd[0]) for(;;) |
366 | { | 386 | { |
367 | char resultByte; | 387 | char resultByte; |
368 | int n = ::read(fd[0], &resultByte, 1); | 388 | int n = ::read(fd[0], &resultByte, 1); |
369 | if (n == 1) | 389 | if (n == 1) |
370 | { | 390 | { |
371 | // Error | 391 | // Error |
372 | runs = false; | 392 | runs = false; |
373 | close(fd[0]); | 393 | close(fd[0]); |
374 | free(arglist); | 394 | free(arglist); |
375 | pid_ = 0; | 395 | pid_ = 0; |
376 | return false; | 396 | return false; |
377 | } | 397 | } |
378 | if (n == -1) | 398 | if (n == -1) |
379 | { | 399 | { |
380 | if ((errno == ECHILD) || (errno == EINTR)) | 400 | if ((errno == ECHILD) || (errno == EINTR)) |
381 | continue; // Ignore | 401 | continue; // Ignore |
382 | } | 402 | } |
383 | break; // success | 403 | break; // success |
384 | } | 404 | } |
385 | if (fd[0]) | 405 | if (fd[0]) |
386 | close(fd[0]); | 406 | close(fd[0]); |
387 | 407 | ||
388 | if (!commSetupDoneP()) // finish communication socket setup for the parent | 408 | if (!commSetupDoneP()) // finish communication socket setup for the parent |
389 | qWarning( "Could not finish comm setup in parent!" ); | 409 | qWarning( "Could not finish comm setup in parent!" ); |
390 | 410 | ||
391 | if (run_mode == Block) { | 411 | if (run_mode == Block) { |
392 | commClose(); | 412 | commClose(); |
393 | 413 | ||
394 | // The SIGCHLD handler of the process controller will catch | 414 | // The SIGCHLD handler of the process controller will catch |
395 | // the exit and set the status | 415 | // the exit and set the status |
396 | while(runs) | 416 | while(runs) |
397 | { | 417 | { |
398 | OProcessController::theOProcessController-> | 418 | OProcessController::theOProcessController-> |
399 | slotDoHousekeeping(0); | 419 | slotDoHousekeeping(0); |
400 | } | 420 | } |
401 | runs = FALSE; | 421 | runs = FALSE; |
402 | emit processExited(this); | 422 | emit processExited(this); |
403 | } | 423 | } |
404 | } | 424 | } |
405 | free(arglist); | 425 | free(arglist); |
406 | return true; | 426 | return true; |
407 | } | 427 | } |
408 | 428 | ||
409 | 429 | ||
410 | 430 | ||
411 | bool OProcess::kill(int signo) | 431 | bool OProcess::kill(int signo) |
412 | { | 432 | { |
413 | bool rv=false; | 433 | bool rv=false; |
414 | 434 | ||
415 | if (0 != pid_) | 435 | if (0 != pid_) |
416 | rv= (-1 != ::kill(pid_, signo)); | 436 | rv= (-1 != ::kill(pid_, signo)); |
417 | // probably store errno somewhere... | 437 | // probably store errno somewhere... |
418 | return rv; | 438 | return rv; |
419 | } | 439 | } |
420 | 440 | ||
421 | 441 | ||
422 | 442 | ||
423 | bool OProcess::isRunning() const | 443 | bool OProcess::isRunning() const |
424 | { | 444 | { |
425 | return runs; | 445 | return runs; |
426 | } | 446 | } |
427 | 447 | ||
428 | 448 | ||
429 | 449 | ||
430 | pid_t OProcess::pid() const | 450 | pid_t OProcess::pid() const |
431 | { | 451 | { |
432 | return pid_; | 452 | return pid_; |
433 | } | 453 | } |
434 | 454 | ||
435 | 455 | ||
436 | 456 | ||
437 | bool OProcess::normalExit() const | 457 | bool OProcess::normalExit() const |
438 | { | 458 | { |
439 | int _status = status; | 459 | int _status = status; |
440 | return (pid_ != 0) && (!runs) && (WIFEXITED((_status))); | 460 | return (pid_ != 0) && (!runs) && (WIFEXITED((_status))); |
441 | } | 461 | } |
442 | 462 | ||
443 | 463 | ||
444 | 464 | ||
445 | int OProcess::exitStatus() const | 465 | int OProcess::exitStatus() const |
446 | { | 466 | { |
447 | int _status = status; | 467 | int _status = status; |
448 | return WEXITSTATUS((_status)); | 468 | return WEXITSTATUS((_status)); |
449 | } | 469 | } |
450 | 470 | ||
451 | 471 | ||
452 | 472 | ||
453 | bool OProcess::writeStdin(const char *buffer, int buflen) | 473 | bool OProcess::writeStdin(const char *buffer, int buflen) |
454 | { | 474 | { |
455 | bool rv; | 475 | bool rv; |
456 | 476 | ||
457 | // if there is still data pending, writing new data | 477 | // if there is still data pending, writing new data |
458 | // to stdout is not allowed (since it could also confuse | 478 | // to stdout is not allowed (since it could also confuse |
459 | // kprocess... | 479 | // kprocess... |
460 | if (0 != input_data) | 480 | if (0 != input_data) |
461 | return false; | 481 | return false; |
462 | 482 | ||
463 | if (runs && (communication & Stdin)) { | 483 | if (runs && (communication & Stdin)) { |
464 | input_data = buffer; | 484 | input_data = buffer; |
465 | input_sent = 0; | 485 | input_sent = 0; |
466 | input_total = buflen; | 486 | input_total = buflen; |
467 | slotSendData(0); | 487 | slotSendData(0); |
468 | innot->setEnabled(true); | 488 | innot->setEnabled(true); |
469 | rv = true; | 489 | rv = true; |
470 | } else | 490 | } else |
471 | rv = false; | 491 | rv = false; |
472 | return rv; | 492 | return rv; |
473 | } | 493 | } |
474 | 494 | ||
495 | void OProcess::flushStdin ( ) | ||
496 | { | ||
497 | if ( !input_data || ( input_sent == input_total )) | ||
498 | return; | ||
499 | |||
500 | int d1, d2; | ||
501 | |||
502 | do { | ||
503 | d1 = input_total - input_sent; | ||
504 | slotSendData ( 0 ); | ||
505 | d2 = input_total - input_sent; | ||
506 | } while ( d2 <= d1 ); | ||
507 | } | ||
508 | |||
475 | void OProcess::suspend() | 509 | void OProcess::suspend() |
476 | { | 510 | { |
477 | if ((communication & Stdout) && outnot) | 511 | if ((communication & Stdout) && outnot) |
478 | outnot->setEnabled(false); | 512 | outnot->setEnabled(false); |
479 | } | 513 | } |
480 | 514 | ||
481 | void OProcess::resume() | 515 | void OProcess::resume() |
482 | { | 516 | { |
483 | if ((communication & Stdout) && outnot) | 517 | if ((communication & Stdout) && outnot) |
484 | outnot->setEnabled(true); | 518 | outnot->setEnabled(true); |
485 | } | 519 | } |
486 | 520 | ||
487 | bool OProcess::closeStdin() | 521 | bool OProcess::closeStdin() |
488 | { | 522 | { |
489 | bool rv; | 523 | bool rv; |
490 | 524 | ||
491 | if (communication & Stdin) { | 525 | if (communication & Stdin) { |
492 | communication = (Communication) (communication & ~Stdin); | 526 | communication = (Communication) (communication & ~Stdin); |
493 | delete innot; | 527 | delete innot; |
494 | innot = 0; | 528 | innot = 0; |
495 | close(in[1]); | 529 | close(in[1]); |
496 | rv = true; | 530 | rv = true; |
497 | } else | 531 | } else |
498 | rv = false; | 532 | rv = false; |
499 | return rv; | 533 | return rv; |
500 | } | 534 | } |
501 | 535 | ||
502 | bool OProcess::closeStdout() | 536 | bool OProcess::closeStdout() |
503 | { | 537 | { |
504 | bool rv; | 538 | bool rv; |
505 | 539 | ||
506 | if (communication & Stdout) { | 540 | if (communication & Stdout) { |
507 | communication = (Communication) (communication & ~Stdout); | 541 | communication = (Communication) (communication & ~Stdout); |
508 | delete outnot; | 542 | delete outnot; |
509 | outnot = 0; | 543 | outnot = 0; |
510 | close(out[0]); | 544 | close(out[0]); |
511 | rv = true; | 545 | rv = true; |
512 | } else | 546 | } else |
513 | rv = false; | 547 | rv = false; |
514 | return rv; | 548 | return rv; |
515 | } | 549 | } |
516 | 550 | ||
517 | bool OProcess::closeStderr() | 551 | bool OProcess::closeStderr() |
518 | { | 552 | { |
519 | bool rv; | 553 | bool rv; |
520 | 554 | ||
521 | if (communication & Stderr) { | 555 | if (communication & Stderr) { |
522 | communication = static_cast<Communication>(communication & ~Stderr); | 556 | communication = static_cast<Communication>(communication & ~Stderr); |
523 | delete errnot; | 557 | delete errnot; |
524 | errnot = 0; | 558 | errnot = 0; |
525 | close(err[0]); | 559 | close(err[0]); |
526 | rv = true; | 560 | rv = true; |
527 | } else | 561 | } else |
528 | rv = false; | 562 | rv = false; |
529 | return rv; | 563 | return rv; |
530 | } | 564 | } |
531 | 565 | ||
532 | 566 | ||
533 | ///////////////////////////// | 567 | ///////////////////////////// |
534 | // protected slots // | 568 | // protected slots // |
535 | ///////////////////////////// | 569 | ///////////////////////////// |
536 | 570 | ||
537 | 571 | ||
538 | 572 | ||
539 | void OProcess::slotChildOutput(int fdno) | 573 | void OProcess::slotChildOutput(int fdno) |
540 | { | 574 | { |
541 | if (!childOutput(fdno)) | 575 | if (!childOutput(fdno)) |
542 | closeStdout(); | 576 | closeStdout(); |
543 | } | 577 | } |
544 | 578 | ||
545 | 579 | ||
546 | void OProcess::slotChildError(int fdno) | 580 | void OProcess::slotChildError(int fdno) |
547 | { | 581 | { |
548 | if (!childError(fdno)) | 582 | if (!childError(fdno)) |
549 | closeStderr(); | 583 | closeStderr(); |
550 | } | 584 | } |
551 | 585 | ||
552 | 586 | ||
553 | void OProcess::slotSendData(int) | 587 | void OProcess::slotSendData(int) |
554 | { | 588 | { |
555 | if (input_sent == input_total) { | 589 | if (input_sent == input_total) { |
556 | innot->setEnabled(false); | 590 | innot->setEnabled(false); |
557 | input_data = 0; | 591 | input_data = 0; |
558 | emit wroteStdin(this); | 592 | emit wroteStdin(this); |
559 | } else | 593 | } else |
560 | input_sent += ::write(in[1], input_data+input_sent, input_total-input_sent); | 594 | input_sent += ::write(in[1], input_data+input_sent, input_total-input_sent); |
561 | } | 595 | } |
562 | 596 | ||
563 | 597 | ||
564 | 598 | ||
565 | ////////////////////////////// | 599 | ////////////////////////////// |
566 | // private member functions // | 600 | // private member functions // |
567 | ////////////////////////////// | 601 | ////////////////////////////// |
568 | 602 | ||
569 | 603 | ||
570 | 604 | ||
571 | void OProcess::processHasExited(int state) | 605 | void OProcess::processHasExited(int state) |
572 | { | 606 | { |
573 | if (runs) | 607 | if (runs) |
574 | { | 608 | { |
575 | runs = false; | 609 | runs = false; |
576 | status = state; | 610 | status = state; |
577 | 611 | ||
578 | commClose(); // cleanup communication sockets | 612 | commClose(); // cleanup communication sockets |
579 | 613 | ||
580 | // also emit a signal if the process was run Blocking | 614 | // also emit a signal if the process was run Blocking |
581 | if (DontCare != run_mode) | 615 | if (DontCare != run_mode) |
582 | { | 616 | { |
583 | emit processExited(this); | 617 | emit processExited(this); |
584 | } | 618 | } |
585 | } | 619 | } |
586 | } | 620 | } |
587 | 621 | ||
588 | 622 | ||
589 | 623 | ||
590 | int OProcess::childOutput(int fdno) | 624 | int OProcess::childOutput(int fdno) |
591 | { | 625 | { |
592 | if (communication & NoRead) { | 626 | if (communication & NoRead) { |
593 | int len = -1; | 627 | int len = -1; |
594 | emit receivedStdout(fdno, len); | 628 | emit receivedStdout(fdno, len); |
595 | errno = 0; // Make sure errno doesn't read "EAGAIN" | 629 | errno = 0; // Make sure errno doesn't read "EAGAIN" |
596 | return len; | 630 | return len; |
597 | } | 631 | } |
598 | else | 632 | else |
599 | { | 633 | { |
600 | char buffer[1024]; | 634 | char buffer[1024]; |
601 | int len; | 635 | int len; |
602 | 636 | ||
603 | len = ::read(fdno, buffer, 1024); | 637 | len = ::read(fdno, buffer, 1024); |
604 | 638 | ||
605 | if ( 0 < len) { | 639 | if ( 0 < len) { |
606 | emit receivedStdout(this, buffer, len); | 640 | emit receivedStdout(this, buffer, len); |
607 | } | 641 | } |
608 | return len; | 642 | return len; |
609 | } | 643 | } |
610 | } | 644 | } |
611 | 645 | ||
612 | 646 | ||
613 | 647 | ||
614 | int OProcess::childError(int fdno) | 648 | int OProcess::childError(int fdno) |
615 | { | 649 | { |
616 | char buffer[1024]; | 650 | char buffer[1024]; |
617 | int len; | 651 | int len; |
618 | 652 | ||
619 | len = ::read(fdno, buffer, 1024); | 653 | len = ::read(fdno, buffer, 1024); |
620 | 654 | ||
621 | if ( 0 < len) | 655 | if ( 0 < len) |
622 | emit receivedStderr(this, buffer, len); | 656 | emit receivedStderr(this, buffer, len); |
623 | return len; | 657 | return len; |
624 | } | 658 | } |
625 | 659 | ||
626 | 660 | ||
627 | 661 | ||
628 | int OProcess::setupCommunication(Communication comm) | 662 | int OProcess::setupCommunication(Communication comm) |
629 | { | 663 | { |
630 | int ok; | 664 | int ok; |
631 | 665 | ||
632 | communication = comm; | 666 | communication = comm; |
633 | 667 | ||
634 | ok = 1; | 668 | ok = 1; |
635 | if (comm & Stdin) | 669 | if (comm & Stdin) |
636 | ok &= socketpair(AF_UNIX, SOCK_STREAM, 0, in) >= 0; | 670 | ok &= socketpair(AF_UNIX, SOCK_STREAM, 0, in) >= 0; |
637 | 671 | ||
638 | if (comm & Stdout) | 672 | if (comm & Stdout) |
639 | ok &= socketpair(AF_UNIX, SOCK_STREAM, 0, out) >= 0; | 673 | ok &= socketpair(AF_UNIX, SOCK_STREAM, 0, out) >= 0; |
640 | 674 | ||
641 | if (comm & Stderr) | 675 | if (comm & Stderr) |
642 | ok &= socketpair(AF_UNIX, SOCK_STREAM, 0, err) >= 0; | 676 | ok &= socketpair(AF_UNIX, SOCK_STREAM, 0, err) >= 0; |
643 | 677 | ||
644 | return ok; | 678 | return ok; |
645 | } | 679 | } |
646 | 680 | ||
647 | 681 | ||
648 | 682 | ||
649 | int OProcess::commSetupDoneP() | 683 | int OProcess::commSetupDoneP() |
650 | { | 684 | { |
651 | int ok = 1; | 685 | int ok = 1; |
652 | 686 | ||
653 | if (communication != NoCommunication) { | 687 | if (communication != NoCommunication) { |
654 | if (communication & Stdin) | 688 | if (communication & Stdin) |
655 | close(in[0]); | 689 | close(in[0]); |
656 | if (communication & Stdout) | 690 | if (communication & Stdout) |
657 | close(out[1]); | 691 | close(out[1]); |
658 | if (communication & Stderr) | 692 | if (communication & Stderr) |
659 | close(err[1]); | 693 | close(err[1]); |
660 | 694 | ||
661 | // Don't create socket notifiers and set the sockets non-blocking if | 695 | // Don't create socket notifiers and set the sockets non-blocking if |
662 | // blocking is requested. | 696 | // blocking is requested. |
663 | if (run_mode == Block) return ok; | 697 | if (run_mode == Block) return ok; |
664 | 698 | ||
665 | if (communication & Stdin) { | 699 | if (communication & Stdin) { |
666 | // ok &= (-1 != fcntl(in[1], F_SETFL, O_NONBLOCK)); | 700 | // ok &= (-1 != fcntl(in[1], F_SETFL, O_NONBLOCK)); |
667 | innot = new QSocketNotifier(in[1], QSocketNotifier::Write, this); | 701 | innot = new QSocketNotifier(in[1], QSocketNotifier::Write, this); |
668 | CHECK_PTR(innot); | 702 | CHECK_PTR(innot); |
669 | innot->setEnabled(false); // will be enabled when data has to be sent | 703 | innot->setEnabled(false); // will be enabled when data has to be sent |
670 | QObject::connect(innot, SIGNAL(activated(int)), | 704 | QObject::connect(innot, SIGNAL(activated(int)), |
671 | this, SLOT(slotSendData(int))); | 705 | this, SLOT(slotSendData(int))); |
672 | } | 706 | } |
673 | 707 | ||
674 | if (communication & Stdout) { | 708 | if (communication & Stdout) { |
675 | // ok &= (-1 != fcntl(out[0], F_SETFL, O_NONBLOCK)); | 709 | // ok &= (-1 != fcntl(out[0], F_SETFL, O_NONBLOCK)); |
676 | outnot = new QSocketNotifier(out[0], QSocketNotifier::Read, this); | 710 | outnot = new QSocketNotifier(out[0], QSocketNotifier::Read, this); |
677 | CHECK_PTR(outnot); | 711 | CHECK_PTR(outnot); |
678 | QObject::connect(outnot, SIGNAL(activated(int)), | 712 | QObject::connect(outnot, SIGNAL(activated(int)), |
679 | this, SLOT(slotChildOutput(int))); | 713 | this, SLOT(slotChildOutput(int))); |
680 | if (communication & NoRead) | 714 | if (communication & NoRead) |
681 | suspend(); | 715 | suspend(); |
682 | } | 716 | } |
683 | 717 | ||
684 | if (communication & Stderr) { | 718 | if (communication & Stderr) { |
685 | // ok &= (-1 != fcntl(err[0], F_SETFL, O_NONBLOCK)); | 719 | // ok &= (-1 != fcntl(err[0], F_SETFL, O_NONBLOCK)); |
686 | errnot = new QSocketNotifier(err[0], QSocketNotifier::Read, this ); | 720 | errnot = new QSocketNotifier(err[0], QSocketNotifier::Read, this ); |
687 | CHECK_PTR(errnot); | 721 | CHECK_PTR(errnot); |
688 | QObject::connect(errnot, SIGNAL(activated(int)), | 722 | QObject::connect(errnot, SIGNAL(activated(int)), |
689 | this, SLOT(slotChildError(int))); | 723 | this, SLOT(slotChildError(int))); |
690 | } | 724 | } |
691 | } | 725 | } |
692 | return ok; | 726 | return ok; |
693 | } | 727 | } |
694 | 728 | ||
695 | 729 | ||
696 | 730 | ||
697 | int OProcess::commSetupDoneC() | 731 | int OProcess::commSetupDoneC() |
698 | { | 732 | { |
699 | int ok = 1; | 733 | int ok = 1; |
700 | struct linger so; | 734 | struct linger so; |
701 | memset(&so, 0, sizeof(so)); | 735 | memset(&so, 0, sizeof(so)); |
702 | 736 | ||
703 | if (communication & Stdin) | 737 | if (communication & Stdin) |
704 | close(in[1]); | 738 | close(in[1]); |
705 | if (communication & Stdout) | 739 | if (communication & Stdout) |
706 | close(out[0]); | 740 | close(out[0]); |
707 | if (communication & Stderr) | 741 | if (communication & Stderr) |
708 | close(err[0]); | 742 | close(err[0]); |
709 | 743 | ||
710 | if (communication & Stdin) | 744 | if (communication & Stdin) |
711 | ok &= dup2(in[0], STDIN_FILENO) != -1; | 745 | ok &= dup2(in[0], STDIN_FILENO) != -1; |
712 | else { | 746 | else { |
713 | int null_fd = open( "/dev/null", O_RDONLY ); | 747 | int null_fd = open( "/dev/null", O_RDONLY ); |
714 | ok &= dup2( null_fd, STDIN_FILENO ) != -1; | 748 | ok &= dup2( null_fd, STDIN_FILENO ) != -1; |
715 | close( null_fd ); | 749 | close( null_fd ); |
716 | } | 750 | } |
717 | if (communication & Stdout) { | 751 | if (communication & Stdout) { |
718 | ok &= dup2(out[1], STDOUT_FILENO) != -1; | 752 | ok &= dup2(out[1], STDOUT_FILENO) != -1; |
719 | ok &= !setsockopt(out[1], SOL_SOCKET, SO_LINGER, (char*)&so, sizeof(so)); | 753 | ok &= !setsockopt(out[1], SOL_SOCKET, SO_LINGER, (char*)&so, sizeof(so)); |
720 | } | 754 | } |
721 | else { | 755 | else { |
722 | int null_fd = open( "/dev/null", O_WRONLY ); | 756 | int null_fd = open( "/dev/null", O_WRONLY ); |
723 | ok &= dup2( null_fd, STDOUT_FILENO ) != -1; | 757 | ok &= dup2( null_fd, STDOUT_FILENO ) != -1; |
724 | close( null_fd ); | 758 | close( null_fd ); |
725 | } | 759 | } |
726 | if (communication & Stderr) { | 760 | if (communication & Stderr) { |
727 | ok &= dup2(err[1], STDERR_FILENO) != -1; | 761 | ok &= dup2(err[1], STDERR_FILENO) != -1; |
728 | ok &= !setsockopt(err[1], SOL_SOCKET, SO_LINGER, reinterpret_cast<char *>(&so), sizeof(so)); | 762 | ok &= !setsockopt(err[1], SOL_SOCKET, SO_LINGER, reinterpret_cast<char *>(&so), sizeof(so)); |
729 | } | 763 | } |
730 | else { | 764 | else { |
731 | int null_fd = open( "/dev/null", O_WRONLY ); | 765 | int null_fd = open( "/dev/null", O_WRONLY ); |
732 | ok &= dup2( null_fd, STDERR_FILENO ) != -1; | 766 | ok &= dup2( null_fd, STDERR_FILENO ) != -1; |
733 | close( null_fd ); | 767 | close( null_fd ); |
734 | } | 768 | } |
735 | return ok; | 769 | return ok; |
736 | } | 770 | } |
737 | 771 | ||
738 | 772 | ||
739 | 773 | ||
740 | void OProcess::commClose() | 774 | void OProcess::commClose() |
741 | { | 775 | { |
742 | if (NoCommunication != communication) { | 776 | if (NoCommunication != communication) { |
743 | bool b_in = (communication & Stdin); | 777 | bool b_in = (communication & Stdin); |
744 | bool b_out = (communication & Stdout); | 778 | bool b_out = (communication & Stdout); |
745 | bool b_err = (communication & Stderr); | 779 | bool b_err = (communication & Stderr); |
746 | if (b_in) | 780 | if (b_in) |
747 | delete innot; | 781 | delete innot; |
748 | 782 | ||
749 | if (b_out || b_err) { | 783 | if (b_out || b_err) { |
750 | // If both channels are being read we need to make sure that one socket buffer | 784 | // If both channels are being read we need to make sure that one socket buffer |
751 | // doesn't fill up whilst we are waiting for data on the other (causing a deadlock). | 785 | // doesn't fill up whilst we are waiting for data on the other (causing a deadlock). |
752 | // Hence we need to use select. | 786 | // Hence we need to use select. |
753 | 787 | ||
754 | // Once one or other of the channels has reached EOF (or given an error) go back | 788 | // Once one or other of the channels has reached EOF (or given an error) go back |
755 | // to the usual mechanism. | 789 | // to the usual mechanism. |
756 | 790 | ||
757 | int fds_ready = 1; | 791 | int fds_ready = 1; |
758 | fd_set rfds; | 792 | fd_set rfds; |
759 | 793 | ||
760 | int max_fd = 0; | 794 | int max_fd = 0; |
761 | if (b_out) { | 795 | if (b_out) { |
762 | fcntl(out[0], F_SETFL, O_NONBLOCK); | 796 | fcntl(out[0], F_SETFL, O_NONBLOCK); |
763 | if (out[0] > max_fd) | 797 | if (out[0] > max_fd) |
764 | max_fd = out[0]; | 798 | max_fd = out[0]; |
765 | delete outnot; | 799 | delete outnot; |
766 | outnot = 0; | 800 | outnot = 0; |
767 | } | 801 | } |
768 | if (b_err) { | 802 | if (b_err) { |
769 | fcntl(err[0], F_SETFL, O_NONBLOCK); | 803 | fcntl(err[0], F_SETFL, O_NONBLOCK); |
770 | if (err[0] > max_fd) | 804 | if (err[0] > max_fd) |
771 | max_fd = err[0]; | 805 | max_fd = err[0]; |
772 | delete errnot; | 806 | delete errnot; |
773 | errnot = 0; | 807 | errnot = 0; |
774 | } | 808 | } |
775 | 809 | ||
776 | 810 | ||
777 | while (b_out || b_err) { | 811 | while (b_out || b_err) { |
778 | // * If the process is still running we block until we | 812 | // * If the process is still running we block until we |
779 | // receive data. (p_timeout = 0, no timeout) | 813 | // receive data. (p_timeout = 0, no timeout) |
780 | // * If the process has already exited, we only check | 814 | // * If the process has already exited, we only check |
781 | // the available data, we don't wait for more. | 815 | // the available data, we don't wait for more. |
782 | // (p_timeout = &timeout, timeout immediately) | 816 | // (p_timeout = &timeout, timeout immediately) |
783 | struct timeval timeout; | 817 | struct timeval timeout; |
784 | timeout.tv_sec = 0; | 818 | timeout.tv_sec = 0; |
785 | timeout.tv_usec = 0; | 819 | timeout.tv_usec = 0; |
786 | struct timeval *p_timeout = runs ? 0 : &timeout; | 820 | struct timeval *p_timeout = runs ? 0 : &timeout; |
787 | 821 | ||
788 | FD_ZERO(&rfds); | 822 | FD_ZERO(&rfds); |
789 | if (b_out) | 823 | if (b_out) |
790 | FD_SET(out[0], &rfds); | 824 | FD_SET(out[0], &rfds); |
791 | 825 | ||
792 | if (b_err) | 826 | if (b_err) |
793 | FD_SET(err[0], &rfds); | 827 | FD_SET(err[0], &rfds); |
794 | 828 | ||
795 | fds_ready = select(max_fd+1, &rfds, 0, 0, p_timeout); | 829 | fds_ready = select(max_fd+1, &rfds, 0, 0, p_timeout); |
796 | if (fds_ready <= 0) break; | 830 | if (fds_ready <= 0) break; |
797 | 831 | ||
798 | if (b_out && FD_ISSET(out[0], &rfds)) { | 832 | if (b_out && FD_ISSET(out[0], &rfds)) { |
799 | int ret = 1; | 833 | int ret = 1; |
800 | while (ret > 0) ret = childOutput(out[0]); | 834 | while (ret > 0) ret = childOutput(out[0]); |
801 | if ((ret == -1 && errno != EAGAIN) || ret == 0) | 835 | if ((ret == -1 && errno != EAGAIN) || ret == 0) |
802 | b_out = false; | 836 | b_out = false; |
803 | } | 837 | } |
804 | 838 | ||
805 | if (b_err && FD_ISSET(err[0], &rfds)) { | 839 | if (b_err && FD_ISSET(err[0], &rfds)) { |
806 | int ret = 1; | 840 | int ret = 1; |
807 | while (ret > 0) ret = childError(err[0]); | 841 | while (ret > 0) ret = childError(err[0]); |
808 | if ((ret == -1 && errno != EAGAIN) || ret == 0) | 842 | if ((ret == -1 && errno != EAGAIN) || ret == 0) |
809 | b_err = false; | 843 | b_err = false; |
810 | } | 844 | } |
811 | } | 845 | } |
812 | } | 846 | } |
813 | 847 | ||
814 | if (b_in) { | 848 | if (b_in) { |
815 | communication = (Communication) (communication & ~Stdin); | 849 | communication = (Communication) (communication & ~Stdin); |
816 | close(in[1]); | 850 | close(in[1]); |
817 | } | 851 | } |
818 | if (b_out) { | 852 | if (b_out) { |
819 | communication = (Communication) (communication & ~Stdout); | 853 | communication = (Communication) (communication & ~Stdout); |
820 | close(out[0]); | 854 | close(out[0]); |
821 | } | 855 | } |
822 | if (b_err) { | 856 | if (b_err) { |
823 | communication = (Communication) (communication & ~Stderr); | 857 | communication = (Communication) (communication & ~Stderr); |
824 | close(err[0]); | 858 | close(err[0]); |
825 | } | 859 | } |
826 | } | 860 | } |
827 | } | 861 | } |
828 | 862 | ||
829 | void OProcess::setUseShell(bool useShell, const char *shell) | 863 | void OProcess::setUseShell(bool useShell, const char *shell) |
830 | { | 864 | { |
831 | if (!d) | 865 | if (!d) |
832 | d = new OProcessPrivate; | 866 | d = new OProcessPrivate; |
833 | d->useShell = useShell; | 867 | d->useShell = useShell; |
834 | d->shell = shell; | 868 | d->shell = shell; |
835 | if (d->shell.isEmpty()) | 869 | if (d->shell.isEmpty()) |
836 | d->shell = searchShell(); | 870 | d->shell = searchShell(); |
837 | } | 871 | } |
838 | 872 | ||
839 | QString OProcess::quote(const QString &arg) | 873 | QString OProcess::quote(const QString &arg) |
840 | { | 874 | { |
841 | QString res = arg; | 875 | QString res = arg; |
842 | res.replace(QRegExp(QString::fromLatin1("\'")), | 876 | res.replace(QRegExp(QString::fromLatin1("\'")), |
843 | QString::fromLatin1("'\"'\"'")); | 877 | QString::fromLatin1("'\"'\"'")); |
844 | res.prepend('\''); | 878 | res.prepend('\''); |
845 | res.append('\''); | 879 | res.append('\''); |
846 | return res; | 880 | return res; |
847 | } | 881 | } |
848 | 882 | ||
849 | QCString OProcess::searchShell() | 883 | QCString OProcess::searchShell() |
850 | { | 884 | { |
851 | QCString tmpShell = QCString(getenv("SHELL")).stripWhiteSpace(); | 885 | QCString tmpShell = QCString(getenv("SHELL")).stripWhiteSpace(); |
852 | if (!isExecutable(tmpShell)) | 886 | if (!isExecutable(tmpShell)) |
853 | { | 887 | { |
854 | tmpShell = "/bin/sh"; | 888 | tmpShell = "/bin/sh"; |
855 | } | 889 | } |
856 | 890 | ||
857 | return tmpShell; | 891 | return tmpShell; |
858 | } | 892 | } |
859 | 893 | ||
860 | bool OProcess::isExecutable(const QCString &filename) | 894 | bool OProcess::isExecutable(const QCString &filename) |
861 | { | 895 | { |
862 | struct stat fileinfo; | 896 | struct stat fileinfo; |
863 | 897 | ||
864 | if (filename.isEmpty()) return false; | 898 | if (filename.isEmpty()) return false; |
865 | 899 | ||
866 | // CC: we've got a valid filename, now let's see whether we can execute that file | 900 | // CC: we've got a valid filename, now let's see whether we can execute that file |
867 | 901 | ||
868 | if (-1 == stat(filename.data(), &fileinfo)) return false; | 902 | if (-1 == stat(filename.data(), &fileinfo)) return false; |
869 | // CC: return false if the file does not exist | 903 | // CC: return false if the file does not exist |
870 | 904 | ||
871 | // CC: anyway, we cannot execute directories, block/character devices, fifos or sockets | 905 | // CC: anyway, we cannot execute directories, block/character devices, fifos or sockets |
872 | if ( (S_ISDIR(fileinfo.st_mode)) || | 906 | if ( (S_ISDIR(fileinfo.st_mode)) || |
873 | (S_ISCHR(fileinfo.st_mode)) || | 907 | (S_ISCHR(fileinfo.st_mode)) || |
874 | (S_ISBLK(fileinfo.st_mode)) || | 908 | (S_ISBLK(fileinfo.st_mode)) || |
875 | #ifdef S_ISSOCK | 909 | #ifdef S_ISSOCK |
876 | // CC: SYSVR4 systems don't have that macro | 910 | // CC: SYSVR4 systems don't have that macro |
877 | (S_ISSOCK(fileinfo.st_mode)) || | 911 | (S_ISSOCK(fileinfo.st_mode)) || |
878 | #endif | 912 | #endif |
879 | (S_ISFIFO(fileinfo.st_mode)) || | 913 | (S_ISFIFO(fileinfo.st_mode)) || |
880 | (S_ISDIR(fileinfo.st_mode)) ) { | 914 | (S_ISDIR(fileinfo.st_mode)) ) { |
881 | return false; | 915 | return false; |
882 | } | 916 | } |
883 | 917 | ||
884 | // CC: now check for permission to execute the file | 918 | // CC: now check for permission to execute the file |
885 | if (access(filename.data(), X_OK) != 0) return false; | 919 | if (access(filename.data(), X_OK) != 0) return false; |
886 | 920 | ||
887 | // CC: we've passed all the tests... | 921 | // CC: we've passed all the tests... |
888 | return true; | 922 | return true; |
889 | } | 923 | } |
890 | 924 | ||
891 | void OProcess::virtual_hook( int, void* ) | ||
892 | { /*BASE::virtual_hook( id, data );*/ } | ||
893 | |||
894 | |||
895 | /////////////////////////// | ||
896 | // CC: Class KShellProcess | ||
897 | /////////////////////////// | ||
898 | |||
899 | KShellProcess::KShellProcess(const char *shellname): | ||
900 | OProcess() | ||
901 | { | ||
902 | setUseShell(true, shellname); | ||
903 | } | ||
904 | |||
905 | |||
906 | KShellProcess::~KShellProcess() { | ||
907 | } | ||
908 | |||
909 | QString KShellProcess::quote(const QString &arg) | ||
910 | { | ||
911 | return OProcess::quote(arg); | ||
912 | } | ||
913 | |||
914 | bool KShellProcess::start(RunMode runmode, Communication comm) | ||
915 | { | ||
916 | return OProcess::start(runmode, comm); | ||
917 | } | ||
918 | 925 | ||
919 | void KShellProcess::virtual_hook( int id, void* data ) | ||
920 | { OProcess::virtual_hook( id, data ); } | ||
921 | 926 | ||
922 | //#include "kprocess.moc" | ||