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