-rw-r--r-- | libopie/oprocess.cpp | 94 | ||||
-rw-r--r-- | libopie/oprocess.h | 72 |
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 | |||
@@ -49,112 +49,132 @@ | |||
49 | #define _MAY_INCLUDE_KPROCESSCONTROLLER_ | 49 | #define _MAY_INCLUDE_KPROCESSCONTROLLER_ |
50 | #include "oprocctrl.h" | 50 | #include "oprocctrl.h" |
51 | 51 | ||
52 | //#include <config.h> | 52 | //#include <config.h> |
53 | 53 | ||
54 | #include <qfile.h> | 54 | #include <qfile.h> |
55 | #include <qsocketnotifier.h> | 55 | #include <qsocketnotifier.h> |
56 | #include <qregexp.h> | 56 | #include <qregexp.h> |
57 | 57 | ||
58 | #include <sys/time.h> | 58 | #include <sys/time.h> |
59 | #include <sys/types.h> | 59 | #include <sys/types.h> |
60 | #include <sys/stat.h> | 60 | #include <sys/stat.h> |
61 | #include <sys/socket.h> | 61 | #include <sys/socket.h> |
62 | 62 | ||
63 | #include <errno.h> | 63 | #include <errno.h> |
64 | #include <fcntl.h> | 64 | #include <fcntl.h> |
65 | #include <stdlib.h> | 65 | #include <stdlib.h> |
66 | #include <signal.h> | 66 | #include <signal.h> |
67 | #include <stdio.h> | 67 | #include <stdio.h> |
68 | #include <string.h> | 68 | #include <string.h> |
69 | #include <unistd.h> | 69 | #include <unistd.h> |
70 | #ifdef HAVE_SYS_SELECT_H | 70 | #ifdef HAVE_SYS_SELECT_H |
71 | #include <sys/select.h> | 71 | #include <sys/select.h> |
72 | #endif | 72 | #endif |
73 | #ifdef HAVE_INITGROUPS | 73 | #ifdef HAVE_INITGROUPS |
74 | #include <grp.h> | 74 | #include <grp.h> |
75 | #endif | 75 | #endif |
76 | #include <pwd.h> | 76 | #include <pwd.h> |
77 | 77 | ||
78 | #include <qapplication.h> | 78 | #include <qapplication.h> |
79 | #include <qmap.h> | 79 | #include <qmap.h> |
80 | //#include <kdebug.h> | 80 | //#include <kdebug.h> |
81 | 81 | ||
82 | ///////////////////////////// | 82 | ///////////////////////////// |
83 | // public member functions // | 83 | // public member functions // |
84 | ///////////////////////////// | 84 | ///////////////////////////// |
85 | 85 | ||
86 | class OProcessPrivate { | 86 | class OProcessPrivate { |
87 | public: | 87 | public: |
88 | OProcessPrivate() : useShell(false) { } | 88 | OProcessPrivate() : useShell(false) { } |
89 | 89 | ||
90 | bool useShell; | 90 | bool useShell; |
91 | QMap<QString,QString> env; | 91 | QMap<QString,QString> env; |
92 | QString wd; | 92 | QString wd; |
93 | QCString shell; | 93 | QCString shell; |
94 | }; | 94 | }; |
95 | 95 | ||
96 | 96 | ||
97 | OProcess::OProcess() | 97 | OProcess::OProcess(QObject *parent, const char *name) |
98 | : QObject(), | 98 | : QObject(parent, name) |
99 | run_mode(NotifyOnExit), | ||
100 | runs(false), | ||
101 | pid_(0), | ||
102 | status(0), | ||
103 | keepPrivs(false), | ||
104 | innot(0), | ||
105 | outnot(0), | ||
106 | errnot(0), | ||
107 | communication(NoCommunication), | ||
108 | input_data(0), | ||
109 | input_sent(0), | ||
110 | input_total(0), | ||
111 | d(0) | ||
112 | { | 99 | { |
100 | init ( ); | ||
101 | } | ||
102 | |||
103 | OProcess::OProcess(const QString &arg0, QObject *parent, const char *name) | ||
104 | : QObject(parent, name) | ||
105 | { | ||
106 | init ( ); | ||
107 | *this << arg0; | ||
108 | } | ||
109 | |||
110 | OProcess::OProcess(const QStringList &args, QObject *parent, const char *name) | ||
111 | : QObject(parent, name) | ||
112 | { | ||
113 | init ( ); | ||
114 | *this << args; | ||
115 | } | ||
116 | |||
117 | void OProcess::init ( ) | ||
118 | { | ||
119 | run_mode = NotifyOnExit; | ||
120 | runs = false; | ||
121 | pid_ = 0; | ||
122 | status = 0; | ||
123 | keepPrivs = false; | ||
124 | innot = 0; | ||
125 | outnot = 0; | ||
126 | errnot = 0; | ||
127 | communication = NoCommunication; | ||
128 | input_data = 0; | ||
129 | input_sent = 0; | ||
130 | input_total = 0; | ||
131 | d = 0; | ||
132 | |||
113 | if (0 == OProcessController::theOProcessController) { | 133 | if (0 == OProcessController::theOProcessController) { |
114 | (void) new OProcessController(); | 134 | (void) new OProcessController(); |
115 | CHECK_PTR(OProcessController::theOProcessController); | 135 | CHECK_PTR(OProcessController::theOProcessController); |
116 | } | 136 | } |
117 | 137 | ||
118 | OProcessController::theOProcessController->addOProcess(this); | 138 | OProcessController::theOProcessController->addOProcess(this); |
119 | out[0] = out[1] = -1; | 139 | out[0] = out[1] = -1; |
120 | in[0] = in[1] = -1; | 140 | in[0] = in[1] = -1; |
121 | err[0] = err[1] = -1; | 141 | err[0] = err[1] = -1; |
122 | } | 142 | } |
123 | 143 | ||
124 | void | 144 | void |
125 | OProcess::setEnvironment(const QString &name, const QString &value) | 145 | OProcess::setEnvironment(const QString &name, const QString &value) |
126 | { | 146 | { |
127 | if (!d) | 147 | if (!d) |
128 | d = new OProcessPrivate; | 148 | d = new OProcessPrivate; |
129 | d->env.insert(name, value); | 149 | d->env.insert(name, value); |
130 | } | 150 | } |
131 | 151 | ||
132 | void | 152 | void |
133 | OProcess::setWorkingDirectory(const QString &dir) | 153 | OProcess::setWorkingDirectory(const QString &dir) |
134 | { | 154 | { |
135 | if (!d) | 155 | if (!d) |
136 | d = new OProcessPrivate; | 156 | d = new OProcessPrivate; |
137 | d->wd = dir; | 157 | d->wd = dir; |
138 | } | 158 | } |
139 | 159 | ||
140 | void | 160 | void |
141 | OProcess::setupEnvironment() | 161 | OProcess::setupEnvironment() |
142 | { | 162 | { |
143 | if (d) | 163 | if (d) |
144 | { | 164 | { |
145 | QMap<QString,QString>::Iterator it; | 165 | QMap<QString,QString>::Iterator it; |
146 | for(it = d->env.begin(); it != d->env.end(); ++it) | 166 | for(it = d->env.begin(); it != d->env.end(); ++it) |
147 | setenv(QFile::encodeName(it.key()).data(), | 167 | setenv(QFile::encodeName(it.key()).data(), |
148 | QFile::encodeName(it.data()).data(), 1); | 168 | QFile::encodeName(it.data()).data(), 1); |
149 | if (!d->wd.isEmpty()) | 169 | if (!d->wd.isEmpty()) |
150 | chdir(QFile::encodeName(d->wd).data()); | 170 | chdir(QFile::encodeName(d->wd).data()); |
151 | } | 171 | } |
152 | } | 172 | } |
153 | 173 | ||
154 | void | 174 | void |
155 | OProcess::setRunPrivileged(bool keepPrivileges) | 175 | OProcess::setRunPrivileged(bool keepPrivileges) |
156 | { | 176 | { |
157 | keepPrivs = keepPrivileges; | 177 | keepPrivs = keepPrivileges; |
158 | } | 178 | } |
159 | 179 | ||
160 | bool | 180 | bool |
@@ -427,96 +447,110 @@ bool OProcess::isRunning() const | |||
427 | 447 | ||
428 | 448 | ||
429 | 449 | ||
430 | pid_t OProcess::pid() const | 450 | pid_t OProcess::pid() const |
431 | { | 451 | { |
432 | return pid_; | 452 | return pid_; |
433 | } | 453 | } |
434 | 454 | ||
435 | 455 | ||
436 | 456 | ||
437 | bool OProcess::normalExit() const | 457 | bool OProcess::normalExit() const |
438 | { | 458 | { |
439 | int _status = status; | 459 | int _status = status; |
440 | return (pid_ != 0) && (!runs) && (WIFEXITED((_status))); | 460 | return (pid_ != 0) && (!runs) && (WIFEXITED((_status))); |
441 | } | 461 | } |
442 | 462 | ||
443 | 463 | ||
444 | 464 | ||
445 | int OProcess::exitStatus() const | 465 | int OProcess::exitStatus() const |
446 | { | 466 | { |
447 | int _status = status; | 467 | int _status = status; |
448 | return WEXITSTATUS((_status)); | 468 | return WEXITSTATUS((_status)); |
449 | } | 469 | } |
450 | 470 | ||
451 | 471 | ||
452 | 472 | ||
453 | bool OProcess::writeStdin(const char *buffer, int buflen) | 473 | bool OProcess::writeStdin(const char *buffer, int buflen) |
454 | { | 474 | { |
455 | bool rv; | 475 | bool rv; |
456 | 476 | ||
457 | // if there is still data pending, writing new data | 477 | // if there is still data pending, writing new data |
458 | // to stdout is not allowed (since it could also confuse | 478 | // to stdout is not allowed (since it could also confuse |
459 | // kprocess... | 479 | // kprocess... |
460 | if (0 != input_data) | 480 | if (0 != input_data) |
461 | return false; | 481 | return false; |
462 | 482 | ||
463 | if (runs && (communication & Stdin)) { | 483 | if (runs && (communication & Stdin)) { |
464 | input_data = buffer; | 484 | input_data = buffer; |
465 | input_sent = 0; | 485 | input_sent = 0; |
466 | input_total = buflen; | 486 | input_total = buflen; |
467 | slotSendData(0); | 487 | slotSendData(0); |
468 | innot->setEnabled(true); | 488 | innot->setEnabled(true); |
469 | rv = true; | 489 | rv = true; |
470 | } else | 490 | } else |
471 | rv = false; | 491 | rv = false; |
472 | return rv; | 492 | return rv; |
473 | } | 493 | } |
474 | 494 | ||
495 | void OProcess::flushStdin ( ) | ||
496 | { | ||
497 | if ( !input_data || ( input_sent == input_total )) | ||
498 | return; | ||
499 | |||
500 | int d1, d2; | ||
501 | |||
502 | do { | ||
503 | d1 = input_total - input_sent; | ||
504 | slotSendData ( 0 ); | ||
505 | d2 = input_total - input_sent; | ||
506 | } while ( d2 <= d1 ); | ||
507 | } | ||
508 | |||
475 | void OProcess::suspend() | 509 | void OProcess::suspend() |
476 | { | 510 | { |
477 | if ((communication & Stdout) && outnot) | 511 | if ((communication & Stdout) && outnot) |
478 | outnot->setEnabled(false); | 512 | outnot->setEnabled(false); |
479 | } | 513 | } |
480 | 514 | ||
481 | void OProcess::resume() | 515 | void OProcess::resume() |
482 | { | 516 | { |
483 | if ((communication & Stdout) && outnot) | 517 | if ((communication & Stdout) && outnot) |
484 | outnot->setEnabled(true); | 518 | outnot->setEnabled(true); |
485 | } | 519 | } |
486 | 520 | ||
487 | bool OProcess::closeStdin() | 521 | bool OProcess::closeStdin() |
488 | { | 522 | { |
489 | bool rv; | 523 | bool rv; |
490 | 524 | ||
491 | if (communication & Stdin) { | 525 | if (communication & Stdin) { |
492 | communication = (Communication) (communication & ~Stdin); | 526 | communication = (Communication) (communication & ~Stdin); |
493 | delete innot; | 527 | delete innot; |
494 | innot = 0; | 528 | innot = 0; |
495 | close(in[1]); | 529 | close(in[1]); |
496 | rv = true; | 530 | rv = true; |
497 | } else | 531 | } else |
498 | rv = false; | 532 | rv = false; |
499 | return rv; | 533 | return rv; |
500 | } | 534 | } |
501 | 535 | ||
502 | bool OProcess::closeStdout() | 536 | bool OProcess::closeStdout() |
503 | { | 537 | { |
504 | bool rv; | 538 | bool rv; |
505 | 539 | ||
506 | if (communication & Stdout) { | 540 | if (communication & Stdout) { |
507 | communication = (Communication) (communication & ~Stdout); | 541 | communication = (Communication) (communication & ~Stdout); |
508 | delete outnot; | 542 | delete outnot; |
509 | outnot = 0; | 543 | outnot = 0; |
510 | close(out[0]); | 544 | close(out[0]); |
511 | rv = true; | 545 | rv = true; |
512 | } else | 546 | } else |
513 | rv = false; | 547 | rv = false; |
514 | return rv; | 548 | return rv; |
515 | } | 549 | } |
516 | 550 | ||
517 | bool OProcess::closeStderr() | 551 | bool OProcess::closeStderr() |
518 | { | 552 | { |
519 | bool rv; | 553 | bool rv; |
520 | 554 | ||
521 | if (communication & Stderr) { | 555 | if (communication & Stderr) { |
522 | communication = static_cast<Communication>(communication & ~Stderr); | 556 | communication = static_cast<Communication>(communication & ~Stderr); |
@@ -843,80 +877,50 @@ QString OProcess::quote(const QString &arg) | |||
843 | QString::fromLatin1("'\"'\"'")); | 877 | QString::fromLatin1("'\"'\"'")); |
844 | res.prepend('\''); | 878 | res.prepend('\''); |
845 | res.append('\''); | 879 | res.append('\''); |
846 | return res; | 880 | return res; |
847 | } | 881 | } |
848 | 882 | ||
849 | QCString OProcess::searchShell() | 883 | QCString OProcess::searchShell() |
850 | { | 884 | { |
851 | QCString tmpShell = QCString(getenv("SHELL")).stripWhiteSpace(); | 885 | QCString tmpShell = QCString(getenv("SHELL")).stripWhiteSpace(); |
852 | if (!isExecutable(tmpShell)) | 886 | if (!isExecutable(tmpShell)) |
853 | { | 887 | { |
854 | tmpShell = "/bin/sh"; | 888 | tmpShell = "/bin/sh"; |
855 | } | 889 | } |
856 | 890 | ||
857 | return tmpShell; | 891 | return tmpShell; |
858 | } | 892 | } |
859 | 893 | ||
860 | bool OProcess::isExecutable(const QCString &filename) | 894 | bool OProcess::isExecutable(const QCString &filename) |
861 | { | 895 | { |
862 | struct stat fileinfo; | 896 | struct stat fileinfo; |
863 | 897 | ||
864 | if (filename.isEmpty()) return false; | 898 | if (filename.isEmpty()) return false; |
865 | 899 | ||
866 | // CC: we've got a valid filename, now let's see whether we can execute that file | 900 | // CC: we've got a valid filename, now let's see whether we can execute that file |
867 | 901 | ||
868 | if (-1 == stat(filename.data(), &fileinfo)) return false; | 902 | if (-1 == stat(filename.data(), &fileinfo)) return false; |
869 | // CC: return false if the file does not exist | 903 | // CC: return false if the file does not exist |
870 | 904 | ||
871 | // CC: anyway, we cannot execute directories, block/character devices, fifos or sockets | 905 | // CC: anyway, we cannot execute directories, block/character devices, fifos or sockets |
872 | if ( (S_ISDIR(fileinfo.st_mode)) || | 906 | if ( (S_ISDIR(fileinfo.st_mode)) || |
873 | (S_ISCHR(fileinfo.st_mode)) || | 907 | (S_ISCHR(fileinfo.st_mode)) || |
874 | (S_ISBLK(fileinfo.st_mode)) || | 908 | (S_ISBLK(fileinfo.st_mode)) || |
875 | #ifdef S_ISSOCK | 909 | #ifdef S_ISSOCK |
876 | // CC: SYSVR4 systems don't have that macro | 910 | // CC: SYSVR4 systems don't have that macro |
877 | (S_ISSOCK(fileinfo.st_mode)) || | 911 | (S_ISSOCK(fileinfo.st_mode)) || |
878 | #endif | 912 | #endif |
879 | (S_ISFIFO(fileinfo.st_mode)) || | 913 | (S_ISFIFO(fileinfo.st_mode)) || |
880 | (S_ISDIR(fileinfo.st_mode)) ) { | 914 | (S_ISDIR(fileinfo.st_mode)) ) { |
881 | return false; | 915 | return false; |
882 | } | 916 | } |
883 | 917 | ||
884 | // CC: now check for permission to execute the file | 918 | // CC: now check for permission to execute the file |
885 | if (access(filename.data(), X_OK) != 0) return false; | 919 | if (access(filename.data(), X_OK) != 0) return false; |
886 | 920 | ||
887 | // CC: we've passed all the tests... | 921 | // CC: we've passed all the tests... |
888 | return true; | 922 | return true; |
889 | } | 923 | } |
890 | 924 | ||
891 | void OProcess::virtual_hook( int, void* ) | ||
892 | { /*BASE::virtual_hook( id, data );*/ } | ||
893 | |||
894 | |||
895 | /////////////////////////// | ||
896 | // CC: Class KShellProcess | ||
897 | /////////////////////////// | ||
898 | |||
899 | KShellProcess::KShellProcess(const char *shellname): | ||
900 | OProcess() | ||
901 | { | ||
902 | setUseShell(true, shellname); | ||
903 | } | ||
904 | |||
905 | |||
906 | KShellProcess::~KShellProcess() { | ||
907 | } | ||
908 | |||
909 | QString KShellProcess::quote(const QString &arg) | ||
910 | { | ||
911 | return OProcess::quote(arg); | ||
912 | } | ||
913 | |||
914 | bool KShellProcess::start(RunMode runmode, Communication comm) | ||
915 | { | ||
916 | return OProcess::start(runmode, comm); | ||
917 | } | ||
918 | 925 | ||
919 | void KShellProcess::virtual_hook( int id, void* data ) | ||
920 | { OProcess::virtual_hook( id, data ); } | ||
921 | 926 | ||
922 | //#include "kprocess.moc" | ||
diff --git a/libopie/oprocess.h b/libopie/oprocess.h index fd726b4..bf5fe0e 100644 --- a/libopie/oprocess.h +++ b/libopie/oprocess.h | |||
@@ -142,97 +142,99 @@ class OProcessPrivate; | |||
142 | * | 142 | * |
143 | *@author Christian Czezakte e9025461@student.tuwien.ac.at | 143 | *@author Christian Czezakte e9025461@student.tuwien.ac.at |
144 | * | 144 | * |
145 | * | 145 | * |
146 | **/ | 146 | **/ |
147 | class OProcess : public QObject | 147 | class OProcess : public QObject |
148 | { | 148 | { |
149 | Q_OBJECT | 149 | Q_OBJECT |
150 | 150 | ||
151 | public: | 151 | public: |
152 | 152 | ||
153 | /** | 153 | /** |
154 | * Modes in which the communication channel can be opened. | 154 | * Modes in which the communication channel can be opened. |
155 | * | 155 | * |
156 | * If communication for more than one channel is required, | 156 | * If communication for more than one channel is required, |
157 | * the values have to be or'ed together, for example to get | 157 | * the values have to be or'ed together, for example to get |
158 | * communication with stdout as well as with stdin, you would | 158 | * communication with stdout as well as with stdin, you would |
159 | * specify @p Stdin @p | @p Stdout | 159 | * specify @p Stdin @p | @p Stdout |
160 | * | 160 | * |
161 | * If @p NoRead is specified in conjunction with @p Stdout, | 161 | * If @p NoRead is specified in conjunction with @p Stdout, |
162 | * no data is actually read from @p Stdout but only | 162 | * no data is actually read from @p Stdout but only |
163 | * the signal @ref childOutput(int fd) is emitted. | 163 | * the signal @ref childOutput(int fd) is emitted. |
164 | */ | 164 | */ |
165 | enum Communication { NoCommunication = 0, Stdin = 1, Stdout = 2, Stderr = 4, | 165 | enum Communication { NoCommunication = 0, Stdin = 1, Stdout = 2, Stderr = 4, |
166 | AllOutput = 6, All = 7, | 166 | AllOutput = 6, All = 7, |
167 | NoRead }; | 167 | NoRead }; |
168 | 168 | ||
169 | /** | 169 | /** |
170 | * Run-modes for a child process. | 170 | * Run-modes for a child process. |
171 | */ | 171 | */ |
172 | enum RunMode { | 172 | enum RunMode { |
173 | /** | 173 | /** |
174 | * The application does not receive notifications from the subprocess when | 174 | * The application does not receive notifications from the subprocess when |
175 | * it is finished or aborted. | 175 | * it is finished or aborted. |
176 | */ | 176 | */ |
177 | DontCare, | 177 | DontCare, |
178 | /** | 178 | /** |
179 | * The application is notified when the subprocess dies. | 179 | * The application is notified when the subprocess dies. |
180 | */ | 180 | */ |
181 | NotifyOnExit, | 181 | NotifyOnExit, |
182 | /** | 182 | /** |
183 | * The application is suspended until the started process is finished. | 183 | * The application is suspended until the started process is finished. |
184 | */ | 184 | */ |
185 | Block }; | 185 | Block }; |
186 | 186 | ||
187 | /** | 187 | /** |
188 | * Constructor | 188 | * Constructor |
189 | */ | 189 | */ |
190 | OProcess(); | 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 | ||
@@ -315,96 +317,98 @@ public: | |||
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 | /** |
@@ -686,120 +690,58 @@ protected: | |||
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 | ||
709 | private: | 713 | private: |
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 | ||
734 | protected: | ||
735 | virtual void virtual_hook( int id, void* data ); | ||
736 | private: | ||
737 | OProcessPrivate *d; | ||
738 | }; | ||
739 | |||
740 | class 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 | */ | ||
753 | class KShellProcess: public OProcess | ||
754 | { | ||
755 | Q_OBJECT | ||
756 | |||
757 | public: | ||
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 | |||
788 | private: | 738 | private: |
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 | |||
796 | protected: | ||
797 | virtual void virtual_hook( int id, void* data ); | ||
798 | private: | ||
799 | KShellProcessPrivate *d; | ||
800 | }; | 742 | }; |
801 | 743 | ||
802 | 744 | ||
803 | 745 | ||
804 | #endif | 746 | #endif |
805 | 747 | ||