KProcess Class Reference
Child process invocation, monitoring and control. More...
#include <kprocess.h>
Inheritance diagram for KProcess:

Public Types | |
enum | Communication { NoCommunication = 0, Stdin = 1, Stdout = 2, Stderr = 4, AllOutput = 6, All = 7, NoRead } |
enum | RunMode { DontCare, NotifyOnExit, Block, OwnGroup } |
enum | { PrioLowest = 20, PrioLow = 10, PrioLower = 5, PrioNormal = 0, PrioHigher = -5, PrioHigh = -10, PrioHighest = -19 } |
Signals | |
void | processExited (KProcess *proc) |
void | receivedStdout (KProcess *proc, char *buffer, int buflen) |
void | receivedStdout (int fd, int &len) |
void | receivedStderr (KProcess *proc, char *buffer, int buflen) |
void | wroteStdin (KProcess *proc) |
Public Member Functions | |
KProcess (QObject *parent, const char *name=0) | |
KProcess () | |
virtual | ~KProcess () |
bool | setExecutable (const QString &proc) KDE_DEPRECATED |
KProcess & | operator<< (const QString &arg) |
KProcess & | operator<< (const char *arg) |
KProcess & | operator<< (const QCString &arg) |
KProcess & | operator<< (const QStringList &args) |
void | clearArguments () |
virtual bool | start (RunMode runmode=NotifyOnExit, Communication comm=NoCommunication) |
virtual bool | kill (int signo=SIGTERM) |
bool | isRunning () const |
pid_t | pid () const |
KDE_DEPRECATED pid_t | getPid () const |
void | suspend () |
void | resume () |
bool | wait (int timeout=-1) |
bool | normalExit () const |
bool | signalled () const |
bool | coreDumped () const |
int | exitStatus () const |
int | exitSignal () const |
bool | writeStdin (const char *buffer, int buflen) |
bool | closeStdin () |
bool | closeStdout () |
bool | closeStderr () |
bool | closePty () |
void | closeAll () |
const QValueList< QCString > & | args () |
void | setRunPrivileged (bool keepPrivileges) |
bool | runPrivileged () const |
void | setEnvironment (const QString &name, const QString &value) |
void | setWorkingDirectory (const QString &dir) |
void | setUseShell (bool useShell, const char *shell=0) |
void | detach () |
void | setUsePty (Communication comm, bool addUtmp) |
KPty * | pty () const |
bool | setPriority (int prio) |
Static Public Member Functions | |
QString | quote (const QString &arg) |
Protected Slots | |
void | slotChildOutput (int fdno) |
void | slotChildError (int fdno) |
void | slotSendData (int dummy) |
Protected Member Functions | |
void | setupEnvironment () |
virtual int | setupCommunication (Communication comm) |
virtual int | commSetupDoneP () |
virtual int | commSetupDoneC () |
virtual void | processHasExited (int state) |
virtual void | commClose () |
void | setBinaryExecutable (const char *filename) |
int | childOutput (int fdno) |
int | childError (int fdno) |
virtual void | virtual_hook (int id, void *data) |
Protected Attributes | |
QValueList< QCString > | arguments |
RunMode | run_mode |
bool | runs |
pid_t | pid_ |
int | status |
bool | keepPrivs |
int | out [2] |
int | in [2] |
int | err [2] |
QSocketNotifier * | innot |
QSocketNotifier * | outnot |
QSocketNotifier * | errnot |
Communication | communication |
const char * | input_data |
int | input_sent |
int | input_total |
Friends | |
class | KProcessController |
Detailed Description
Child process invocation, monitoring and control.
General usage and features:
This class allows a KDE application to start child processes without having to worry about UN*X signal handling issues and zombie process reaping.
- See also:
- KProcIO
- DontCare -- The child process is invoked and both the child process and the parent process continue concurrently.
- NotifyOnExit -- The child process is invoked and both the child and the parent process run concurrently.
Be aware: When the KProcess object gets destructed, the child process will be killed if it is still running! This means in particular, that it usually makes no sense to use a KProcess on the stack with NotifyOnExit.
- OwnGroup -- like NotifyOnExit, but the child process is started in an own process group (and an own session, FWIW). The behavior of kill() changes to killing the whole process group - this makes this mode useful for implementing primitive job management. It can be used to work around broken wrapper scripts that don't propagate signals to the "real" program. However, use this with care, as you disturb the shell's job management if your program is started from the command line.
- Block -- The child process starts and the parent process is suspended until the child process exits. (Really not recommended for programs with a GUI.) In this mode the parent can read the child's output, but can't send it any input.
Furthermore it is possible to supply command-line arguments to the process in a clean fashion (no null-terminated stringlists and such...)
A small usage example:
KProcess *proc = new KProcess; *proc << "my_executable"; *proc << "These" << "are" << "the" << "command" << "line" << "args"; QApplication::connect(proc, SIGNAL(processExited(KProcess *)), pointer_to_my_object, SLOT(my_objects_slot(KProcess *))); proc->start();
This will start "my_executable" with the commandline arguments "These"...
When the child process exits, the slot will be invoked.
Communication with the child process:
KProcess supports communication with the child process through stdin/stdout/stderr.
The following functions are provided for getting data from the child process or sending data to the child's stdin (For more information, have a look at the documentation of each function):
- writeStdin() -- Transmit data to the child process' stdin. When all data was sent, the signal wroteStdin() is emitted.
- When data arrives at stdout or stderr, the signal receivedStdout() resp. receivedStderr() is emitted.
- You can shut down individual communication channels with closeStdin(), closeStdout(), and closeStderr(), resp.
- Author:
- Christian Czezatke e9025461@student.tuwien.ac.at
Definition at line 124 of file kprocess.h.
Member Enumeration Documentation
|
Modes in which the communication channel can be opened.
If communication for more than one channel is required, the values have to be or'ed together, for example to get communication with stdout as well as with stdin, you would specify
If Definition at line 142 of file kprocess.h. Referenced by closeStderr(), closeStdin(), closeStdout(), and setupCommunication(). |
|
Run-modes for a child process.
Definition at line 152 of file kprocess.h. |
|
More or less intuitive constants for use with setPriority().
Definition at line 550 of file kprocess.h. |
Constructor & Destructor Documentation
|
Constructor
Definition at line 112 of file kprocess.cpp. References KProcessController::addKProcess(), err, in, out, and KProcessController::ref(). |
|
Constructor.
Definition at line 137 of file kprocess.cpp. References KProcessController::addKProcess(), err, in, out, and KProcessController::ref(). |
|
Destructor:.
If the process is running when the destructor for this class is called, the child process is killed with a SIGKILL, but only if the run mode is not of type Definition at line 215 of file kprocess.cpp. References KProcessController::deref(), detach(), kill(), KProcessController::removeKProcess(), and run_mode. |
Member Function Documentation
|
Definition at line 243 of file kprocess.cpp. |
|
Sets the executable and the command line argument list for this process. For example, doing an "ls -l /usr/local/bin" can be achieved by:
Definition at line 275 of file kprocess.cpp. References arguments. Referenced by operator<<(). |
|
Similar to previous method, takes a char *, supposed to be in locale 8 bit already.
Definition at line 269 of file kprocess.cpp. References arguments. |
|
Similar to previous method, takes a QCString, supposed to be in locale 8 bit already.
Definition at line 264 of file kprocess.cpp. References operator<<(). |
|
Sets the executable and the command line argument list for this process, in a single method call, or add a list of arguments.
Definition at line 256 of file kprocess.cpp. References arguments. |
|
Clear a command line argument list that has been set by using operator<<.
Definition at line 281 of file kprocess.cpp. References arguments. Referenced by KProcIO::resetAll(). |
|
Starts the process. For a detailed description of the various run modes and communication semantics, have a look at the general description of the KProcess class. Note that if you use setUsePty( Stdout | Stderr, <bool> ), you cannot use Stdout | Stderr here - instead, use Stdout only to receive the mixed output. The following problems could cause this function to return false:
Reimplemented in KShellProcess. Definition at line 286 of file kprocess.cpp. References arguments, Block, commClose(), commSetupDoneC(), commSetupDoneP(), endl(), input_data, OwnGroup, pid_, processExited(), KProcessController::rescheduleCheck(), run_mode, runPrivileged(), runs, setupCommunication(), setupEnvironment(), and KProcessController::unscheduleCheck(). Referenced by KConfigBackEnd::checkConfigFilesWritable(), KProcIO::start(), and KShellProcess::start(). |
|
Stop the process (by sending it a signal).
Definition at line 476 of file kprocess.cpp. References OwnGroup, pid_, run_mode, and runs. Referenced by KProcIO::resetAll(), and ~KProcess(). |
|
Checks whether the process is running.
Definition at line 485 of file kprocess.cpp. References runs. Referenced by KProcIO::resetAll(). |
|
Returns the process id of the process. If it is called after the process has exited, it returns the process id of the last child process that was created by this instance of KProcess. Calling it before any child process has been started by this KProcess instance causes pid() to return 0.
Definition at line 492 of file kprocess.cpp. References pid_. Referenced by getPid(). |
|
Definition at line 306 of file kprocess.h. References pid(). |
|
Suspend processing of data from stdout of the child process.
Definition at line 626 of file kprocess.cpp. References outnot. Referenced by commSetupDoneP(). |
|
Resume processing of data from stdout of the child process.
Definition at line 632 of file kprocess.cpp. References outnot. |
|
Suspend execution of the current thread until the child process dies or the timeout hits. This function is not recommended for programs with a GUI.
Definition at line 509 of file kprocess.cpp. References KProcessController::notifierFd(), pid_, processHasExited(), KProcessController::rescheduleCheck(), runs, and KProcessController::unscheduleCheck(). |
|
Checks whether the process exited cleanly.
Definition at line 572 of file kprocess.cpp. |
|
Checks whether the process was killed by a signal.
Definition at line 578 of file kprocess.cpp. Referenced by coreDumped(). |
|
Checks whether a killed process dumped core.
Definition at line 584 of file kprocess.cpp. References signalled(). |
|
Returns the exit status of the process.
Definition at line 594 of file kprocess.cpp. |
|
Returns the signal the process was killed by.
Definition at line 600 of file kprocess.cpp. |
|
Transmit data to the child process' stdin. This function may return false in the following cases:
buffer or call writeStdin() again until either a wroteStdin() signal indicates that the data has been sent or a processExited() signal shows that the child process is no longer alive.If all the data has been sent to the client, the signal wroteStdin() will be emitted.
Definition at line 606 of file kprocess.cpp. References communication, innot, input_data, input_sent, input_total, and slotSendData(). Referenced by KProcIO::writeStdin(). |
|
Shuts down the Stdin communication link. If no pty is used, this causes "EOF" to be indicated on the child's stdin file descriptor.
Definition at line 638 of file kprocess.cpp. References Communication, communication, in, and innot. Referenced by closeAll(), KProcIO::closeWhenDone(), commClose(), and slotSendData(). |
|
Shuts down the Stdout communication link. If no pty is used, any further attempts by the child to write to its stdout file descriptor will cause it to receive a SIGPIPE.
Definition at line 652 of file kprocess.cpp. References Communication, communication, out, and outnot. Referenced by closeAll(), commClose(), and slotChildOutput(). |
|
Shuts down the Stderr communication link. If no pty is used, any further attempts by the child to write to its stderr file descriptor will cause it to receive a SIGPIPE.
Definition at line 666 of file kprocess.cpp. References Communication, communication, err, and errnot. Referenced by closeAll(), commClose(), and slotChildError(). |
|
Deletes the optional utmp entry and closes the pty. Make sure to shut down any communication links that are using the pty before calling this function.
Definition at line 680 of file kprocess.cpp. Referenced by closeAll(), and commClose(). |
|
Calls the above four close* functions in a row.
Definition at line 691 of file kprocess.cpp. References closePty(), closeStderr(), closeStdin(), and closeStdout(). |
|
Lets you see what your arguments are for debugging.
Definition at line 450 of file kprocess.h. References arguments. |
|
Controls whether the started process should drop any setuid/setgid privileges or whether it should keep them. Note that this function is mostly a dummy, as the KDE libraries currently refuse to run with setuid/setgid privileges. The default is false: drop privileges
Definition at line 190 of file kprocess.cpp. References keepPrivs. |
|
Returns whether the started process will drop any setuid/setgid privileges or whether it will keep them.
Definition at line 196 of file kprocess.cpp. References keepPrivs. Referenced by start(). |
|
Adds the variable This function must be called before starting the process.
Definition at line 163 of file kprocess.cpp. |
|
Changes the current working directory (CWD) of the process to be started. This function must be called before starting the process.
Definition at line 169 of file kprocess.cpp. |
|
Specify whether to start the command via a shell or directly.
The default is to start the command directly. If When using a shell, the caller should make sure that all filenames etc. are properly quoted when passed as argument.
Definition at line 739 of file kprocess.cpp. Referenced by KShellProcess::KShellProcess(). |
|
This function can be used to quote an argument string such that the shell processes it properly. This is e. g. necessary for user-provided file names which may contain spaces or quotes. It also prevents expansion of wild cards and environment variables.
Reimplemented in KShellProcess. Definition at line 781 of file kprocess.cpp. Referenced by KShellProcess::quote(), and KTempDir::unlink(). |
|
Detaches KProcess from child process. All communication is closed. No exit notification is emitted any more for the child process. Deleting the KProcess will no longer kill the child process. Note that the current process remains the parent process of the child process. Definition at line 228 of file kprocess.cpp. References KProcessController::addProcess(), commClose(), pid_, and runs. Referenced by ~KProcess(). |
|
Specify whether to create a pty (pseudo-terminal) for running the command. This function should be called before starting the process.
Definition at line 763 of file kprocess.cpp. |
|
Obtains the pty object used by this process. The return value is valid only after setUsePty() was used to associate at least one standard I/O stream to a pty. The pty is open only while the process is running.
Definition at line 776 of file kprocess.cpp. |
|
Sets the scheduling priority of the process.
Definition at line 202 of file kprocess.cpp. |
|
Emitted after the process has terminated when the process was run in the
Referenced by processHasExited(), and start(). |
|
Emitted, when output from the child process has been received on stdout. To actually get this signal, the Stdout communication link has to be turned on in start().
buffer to your private data structures before returning from the slot. Example:
Referenced by childOutput(). |
|
Emitted when output from the child process has been received on stdout. To actually get this signal, the Stdout communication link has to be turned on in start() and the NoRead flag must have been passed. You will need to explicitly call resume() after your call to start() to begin processing data from the child process' stdout. This is to ensure that this signal is not emitted when no one is connected to it, otherwise this signal will not be emitted.
The data still has to be read from file descriptor
|
|
Emitted, when output from the child process has been received on stderr. To actually get this signal, the Stderr communication link has to be turned on in start().
You should copy the information contained in
Referenced by childError(). |
|
Emitted after all the data that has been specified by a prior call to writeStdin() has actually been written to the child process.
Referenced by KProcIO::resetAll(), slotSendData(), and KProcIO::start(). |
|
This slot gets activated when data from the child's stdout arrives. It usually calls childOutput().
Definition at line 705 of file kprocess.cpp. References childOutput(), and closeStdout(). Referenced by commClose(), and commSetupDoneP(). |
|
This slot gets activated when data from the child's stderr arrives. It usually calls childError().
Definition at line 712 of file kprocess.cpp. References childError(), and closeStderr(). Referenced by commClose(), and commSetupDoneP(). |
|
Called when another bulk of data can be sent to the child's stdin. If there is no more data to be sent to stdin currently available, this function must disable the QSocketNotifier innot.
Definition at line 719 of file kprocess.cpp. References closeStdin(), endl(), in, innot, input_data, input_sent, input_total, and wroteStdin(). Referenced by commSetupDoneP(), and writeStdin(). |
|
Sets up the environment according to the data passed via setEnvironment().
Definition at line 175 of file kprocess.cpp. Referenced by start(). |
|
This function is called from start() right before a fork() takes place.
According to the This function should return 1 if setting the needed communication channels was successful. The default implementation is to create UNIX STREAM sockets for the communication, but you could reimplement this function to establish a TCP/IP communication for network communication, for example. Definition at line 846 of file kprocess.cpp. References Communication, communication, endl(), err, in, and out. Referenced by start(). |
|
Called right after a (successful) fork() on the parent side. This function will usually do some communications cleanup, like closing in[0], out[1] and out[1]. Furthermore, it must also create the QSocketNotifiers innot, outnot and errnot and connect their Qt signals to the respective KProcess slots. For a more detailed explanation, it is best to have a look at the default implementation in kprocess.cpp. Definition at line 912 of file kprocess.cpp. References communication, err, errnot, in, innot, NotifyOnExit, out, outnot, OwnGroup, run_mode, slotChildError(), slotChildOutput(), slotSendData(), and suspend(). Referenced by start(). |
|
Called right after a (successful) fork(), but before an exec() on the child process' side. It usually duplicates the in[0], out[1] and err[1] file handles to the respective standard I/O handles. Definition at line 957 of file kprocess.cpp. References communication, err, in, and out. Referenced by start(). |
|
Immediately called after a successfully started process in NotifyOnExit mode has exited. This function normally calls commClose() and emits the processExited() signal.
Definition at line 793 of file kprocess.cpp. References commClose(), processExited(), run_mode, and runs. Referenced by wait(). |
|
Cleans up the communication links to the child after it has exited. This function should act upon the values of pid() and runs. See the kprocess.cpp source for details.
Definition at line 1001 of file kprocess.cpp. References closePty(), closeStderr(), closeStdin(), closeStdout(), communication, err, KProcessController::notifierFd(), out, pid_, runs, slotChildError(), and slotChildOutput(). Referenced by detach(), processHasExited(), and start(). |
|
Specify the actual executable that should be started (first argument to execve) Normally the the first argument is the executable but you can override that with this function.
Definition at line 238 of file kprocess.cpp. |
|
Called by slotChildOutput() this function copies data arriving from the child process' stdout to the respective buffer and emits the signal receivedStdout().
Definition at line 808 of file kprocess.cpp. References communication, and receivedStdout(). Referenced by slotChildOutput(). |
|
Called by slotChildError() this function copies data arriving from the child process' stderr to the respective buffer and emits the signal receivedStderr().
Definition at line 831 of file kprocess.cpp. References receivedStderr(). Referenced by slotChildError(). |
Friends And Related Function Documentation
|
KProcessController is a friend of KProcess because it has to have access to various data members.
Definition at line 862 of file kprocess.h. |
Member Data Documentation
|
The list of the process' command line arguments. The first entry in this list is the executable itself. Definition at line 673 of file kprocess.h. Referenced by args(), clearArguments(), operator<<(), setExecutable(), and start(). |
|
How to run the process (Block, NotifyOnExit, DontCare). You should not modify this data member directly from derived classes. Definition at line 678 of file kprocess.h. Referenced by commSetupDoneP(), kill(), processHasExited(), start(), and ~KProcess(). |
|
true if the process is currently running. You should not modify this data member directly from derived classes. Please use isRunning() for reading the value of this data member since it will probably be made private in later versions of KProcess. Definition at line 685 of file kprocess.h. Referenced by commClose(), detach(), isRunning(), kill(), normalExit(), processHasExited(), setExecutable(), setPriority(), signalled(), start(), and wait(). |
|
The PID of the currently running process. You should not modify this data member in derived classes. Please use pid() instead of directly accessing this member since it will probably be made private in later versions of KProcess. Definition at line 694 of file kprocess.h. Referenced by commClose(), detach(), kill(), normalExit(), pid(), setPriority(), signalled(), start(), and wait(). |
|
The process' exit status as returned by waitpid(). You should not modify the value of this data member from derived classes. You should rather use exitStatus() than accessing this data member directly since it will probably be made private in further versions of KProcess. Definition at line 703 of file kprocess.h. |
|
If false, the child process' effective uid & gid will be reset to the real values.
Definition at line 711 of file kprocess.h. Referenced by runPrivileged(), and setRunPrivileged(). |
|
The socket descriptors for stdout.
Definition at line 802 of file kprocess.h. Referenced by closeStdout(), commClose(), commSetupDoneC(), commSetupDoneP(), KProcess(), and setupCommunication(). |
|
The socket descriptors for stdin.
Definition at line 806 of file kprocess.h. Referenced by closeStdin(), commSetupDoneC(), commSetupDoneP(), KProcess(), setupCommunication(), and slotSendData(). |
|
The socket descriptors for stderr.
Definition at line 810 of file kprocess.h. Referenced by closeStderr(), commClose(), commSetupDoneC(), commSetupDoneP(), KProcess(), and setupCommunication(). |
|
The socket notifier for in[1].
Definition at line 815 of file kprocess.h. Referenced by closeStdin(), commSetupDoneP(), slotSendData(), and writeStdin(). |
|
The socket notifier for out[0].
Definition at line 819 of file kprocess.h. Referenced by closeStdout(), commSetupDoneP(), resume(), and suspend(). |
|
The socket notifier for err[0].
Definition at line 823 of file kprocess.h. Referenced by closeStderr(), and commSetupDoneP(). |
|
Lists the communication links that are activated for the child process. Should not be modified from derived classes. Definition at line 829 of file kprocess.h. Referenced by childOutput(), closeStderr(), closeStdin(), closeStdout(), commClose(), commSetupDoneC(), commSetupDoneP(), setupCommunication(), and writeStdin(). |
|
The buffer holding the data that has to be sent to the child.
Definition at line 848 of file kprocess.h. Referenced by slotSendData(), start(), and writeStdin(). |
|
The number of bytes already transmitted.
Definition at line 852 of file kprocess.h. Referenced by slotSendData(), and writeStdin(). |
|
The total length of input_data.
Definition at line 856 of file kprocess.h. Referenced by slotSendData(), and writeStdin(). |
The documentation for this class was generated from the following files: