• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kextprocess

KExtProcess::ExtProcess

KExtProcess::ExtProcess Class Reference

#include <extprocess.h>

Inheritance diagram for KExtProcess::ExtProcess:

Inheritance graph
[legend]

List of all members.


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
Basically, this class distinguishes three different ways of running child processes:

  • DontCare -- The child process is invoked and both the child process and the parent process continue concurrently.
The process is started in an own session (see setsid(2)).

  • NotifyOnExit -- The child process is invoked and both the child and the parent process run concurrently.
When the child process exits, the KProcess instance corresponding to it emits the Qt signal processExited(). Since this signal is not emitted from within a UN*X signal handler, arbitrary function calls can be made.

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.
KProcess also provides several functions for determining the exit status and the pid of the child process it represents.

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 129 of file extprocess.h.


Public Types

enum  {
  PrioLowest = 20, PrioLow = 10, PrioLower = 5, PrioNormal = 0,
  PrioHigher = -5, PrioHigh = -10, PrioHighest = -19
}
enum  Communication {
  NoCommunication = 0, Stdin = 1, Stdout = 2, Stderr = 4,
  AllOutput = 6, All = 7, NoRead
}
enum  RunMode { DontCare, NotifyOnExit, Block, OwnGroup }

Signals

void processExited (KExtProcess::ExtProcess *proc)
void receivedStderr (KExtProcess::ExtProcess *proc, char *buffer, int buflen)
void receivedStdout (KExtProcess::ExtProcess *proc, char *buffer, int buflen)
void wroteStdin (KExtProcess::ExtProcess *proc)

Public Member Functions

const QValueList< QCString > & arguments () const
void chainAfter (ExtProcess *parentProcess)
 ExtProcess (QObject *parent=0, const char *name=0)
bool isChainedProcess () const
bool isNull () const
ExtProcess & operator<< (const QString &arg)
ExtProcess * parentProcess () const
virtual bool start (RunMode runmode=NotifyOnExit, Communication comm=NoCommunication)=0
virtual bool writeStdin (const char *buffer, int buflen)=0
virtual ~ExtProcess ()

Static Public Member Functions

static QString quote (const QString &arg)

Member Enumeration Documentation

anonymous enum

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. More or less intuitive constants for use with setPriority().

Enumerator:
PrioLowest 
PrioLow 
PrioLower 
PrioNormal 
PrioHigher 
PrioHigh 
PrioHighest 

Definition at line 546 of file extprocess.h.

enum KExtProcess::ExtProcess::Communication

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 Stdin | Stdout

If NoRead is specified in conjunction with Stdout, no data is actually read from Stdout but only the signal receivedStdout(int fd, int &len) is emitted.

Enumerator:
NoCommunication 
Stdin 
Stdout 
Stderr 
AllOutput 
All 
NoRead 

Definition at line 147 of file extprocess.h.

enum KExtProcess::ExtProcess::RunMode

Run-modes for a child process.

Enumerator:
DontCare  The application does not receive notifications from the subprocess when it is finished or aborted.
NotifyOnExit  The application is notified when the subprocess dies.
Block  The application is suspended until the started process is finished.
OwnGroup  Same as NotifyOnExit, but the process is run in an own session, just like with DontCare.

Definition at line 158 of file extprocess.h.


Constructor & Destructor Documentation

KExtProcess::ExtProcess::ExtProcess ( QObject *  parent = 0,
const char *  name = 0 
)

Constructor.

Definition at line 41 of file extprocess.cpp.

KExtProcess::ExtProcess::~ExtProcess (  )  [virtual]

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 DontCare. Processes started as DontCare keep running anyway.

Definition at line 51 of file extprocess.cpp.


Member Function Documentation

const QValueList< QCString > & KExtProcess::ExtProcess::arguments (  )  const

Shuts down the Stdin communication link.

If no pty is used, this causes "EOF" to be indicated on the child's stdin file descriptor.

Returns:
false if no Stdin communication link exists (any more). 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.

false if no Stdout communication link exists (any more). 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.

false if no Stderr communication link exists (any more). 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.

Returns:
false if the pty is not open (any more). Calls the above four close* functions in a row. Lets you see what your arguments are for debugging.

the list of arguments

Definition at line 68 of file extprocess.cpp.

void KExtProcess::ExtProcess::chainAfter ( ExtProcess *  parentProcess  ) 

Sets the scheduling priority of the process.

Parameters:
prio the new priority in the range -20 (high) to 19 (low).
Returns:
false on error; see setpriority(2) for possible reasons.
Since:
3.2 Chain the process with another process. By chaining processes you can run applications that are not reachable in a single step, e.g. 'SSH to server, then su to root', or SSH-hopping to distant servers.
Parameters:
parentProcess is the process that should be used for all communication rather than allocating a LocalProcess. Pass a null pointer to disable chaining again.
Do *NOT* call this method after calling start()!

FIXME: Not all ExtProcesses can be used as startpoint or endpoint. Currently LocalProcess by its very nature can only be a startpoint, and it's conceivable that in the future some processes can only be endpoints. As soon as KExtProcess has access to the meta-information that describes these capabilities we should also check them and do some error reporting.

Definition at line 104 of file extprocess.cpp.

bool KExtProcess::ExtProcess::isChainedProcess (  )  const

Convenience method that returns true when the process is chained.

Definition at line 99 of file extprocess.cpp.

bool KExtProcess::ExtProcess::isNull (  )  const

Returns true when the process is a NullProcess.

A null process can't actually run anything and simulates immediate failure. ProcessFactory returns null processes in most error conditions. Use isNull() to check for them.

Definition at line 62 of file extprocess.cpp.

ExtProcess & KExtProcess::ExtProcess::operator<< ( const QString &  arg  ) 

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:

  KProcess p;
  ...
  p << "ls" << "-l" << "/usr/local/bin"

Parameters:
arg the argument to add
Returns:
a reference to this KProcess

Definition at line 56 of file extprocess.cpp.

KExtProcess::ExtProcess * KExtProcess::ExtProcess::parentProcess (  )  const

Return the parent process when chaining, or a null pointer when this process is not chained.

Definition at line 94 of file extprocess.cpp.

void KExtProcess::ExtProcess::processExited ( KExtProcess::ExtProcess *  proc  )  [signal]

Emitted after the process has terminated when the process was run in the NotifyOnExit (==default option to start() ) or the Block mode.

Parameters:
proc a pointer to the process that has exited

QString KExtProcess::ExtProcess::quote ( const QString &  arg  )  [static]

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

Parameters:
keepPrivileges true to keep the privileges Returns whether the started process will drop any setuid/setgid privileges or whether it will keep them.
Returns:
true if the process runs privileged Adds the variable name to the process' environment. This function must be called before starting the process.
Parameters:
name the name of the environment variable
value the new value for the environment variable Changes the current working directory (CWD) of the process to be started. This function must be called before starting the process.
dir the new directory Specify whether to start the command via a shell or directly. The default is to start the command directly. If useShell is true shell will be used as shell, or if shell is empty, /bin/sh will be used.
When using a shell, the caller should make sure that all filenames etc. are properly quoted when passed as argument.
See also:
quote()
Parameters:
useShell true if the command should be started via a shell
shell the path to the shell that will execute the process, or 0 to use /bin/sh. Use getenv("SHELL") to use the user's default shell, but note that doing so is usually a bad idea for shell compatibility reasons.
Since:
3.1 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.
Parameters:
arg the argument to quote
Returns:
the quoted argument
Since:
3.1

Definition at line 89 of file extprocess.cpp.

void KExtProcess::ExtProcess::receivedStderr ( KExtProcess::ExtProcess *  proc,
char *  buffer,
int  buflen 
) [signal]

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 fd.

Parameters:
fd the file descriptor that provides the data
len the number of bytes that have been read from fd must be written here 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 buffer to your private data structures before returning from the slot.

Parameters:
proc a pointer to the process that has received the data
buffer The data received.
buflen The number of bytes that are available.

void KExtProcess::ExtProcess::receivedStdout ( KExtProcess::ExtProcess *  proc,
char *  buffer,
int  buflen 
) [signal]

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().

Parameters:
proc a pointer to the process that has received the output
buffer The data received.
buflen The number of bytes that are available.
You should copy the information contained in buffer to your private data structures before returning from the slot. Example:
     QString myBuf = QString::fromLatin1(buffer, buflen);

virtual bool KExtProcess::ExtProcess::start ( RunMode  runmode = NotifyOnExit,
Communication  comm = NoCommunication 
) [pure virtual]

Similar to previous method, takes a char *, supposed to be in locale 8 bit already.

Similar to previous method, takes a QCString, supposed to be in locale 8 bit already.

Parameters:
arg the argument to add
Returns:
a reference to this KProcess Sets the executable and the command line argument list for this process, in a single method call, or add a list of arguments.
Parameters:
args the arguments to add
Returns:
a reference to this KProcess Clear a command line argument list that has been set by using operator<<. 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:

  • The process is already running.
  • The command line argument list is empty.
  • The the comm parameter is incompatible with the selected pty usage.
  • The starting of the process failed (could not fork).
  • The executable was not found.
Parameters:
runmode The Run-mode for the process.
comm Specifies which communication links should be established to the child process (stdin/stdout/stderr). By default, no communication takes place and the respective communication signals will never get emitted.
Returns:
true on success, false on error (see above for error conditions)

Implemented in KExtProcess::LocalProcess, KExtProcess::NullProcess, and KExtProcess::ShellBasedProcess.

virtual bool KExtProcess::ExtProcess::writeStdin ( const char *  buffer,
int  buflen 
) [pure virtual]

Stop the process (by sending it a signal).

Parameters:
signo The signal to send. The default is SIGTERM.
Returns:
true if the signal was delivered successfully. Checks whether the process is running.

true if the process is (still) considered to be running 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.

Returns:
the pid of the process or 0 if no process has been started yet. Suspend processing of data from stdout of the child process. Resume processing of data from stdout of the child process. 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.
Parameters:
timeout timeout in seconds. -1 means wait indefinitely.
Returns:
true if the process exited, false if the timeout hit.
Since:
3.2 Checks whether the process exited cleanly.
Returns:
true if the process has already finished and has exited "voluntarily", ie: it has not been killed by a signal. Checks whether the process was killed by a signal.

true if the process has already finished and has not exited "voluntarily", ie: it has been killed by a signal.

Since:
3.2 Checks whether a killed process dumped core.
Returns:
true if signalled() returns true and the process dumped core. Note that on systems that don't define the WCOREDUMP macro, the return value is always false.
Since:
3.2 Returns the exit status of the process.
Returns:
the exit status of the process. Note that this value is not valid if normalExit() returns false. Returns the signal the process was killed by.

the signal number that caused the process to exit. Note that this value is not valid if signalled() returns false.

Since:
3.2 Transmit data to the child process' stdin.
This function may return false in the following cases:

  • The process is not currently running. This implies that you cannot use this function in Block mode.
  • Communication to stdin has not been requested in the start() call.
  • Transmission of data to the child process by a previous call to writeStdin() is still in progress.
Please note that the data is sent to the client asynchronously, so when this function returns, the data might not have been processed by the child process. That means that you must not free 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.

Parameters:
buffer the buffer to write
buflen the length of the buffer
Returns:
false if an error has occurred

Implemented in KExtProcess::LocalProcess, KExtProcess::NullProcess, and KExtProcess::ShellBasedProcess.

void KExtProcess::ExtProcess::wroteStdin ( KExtProcess::ExtProcess *  proc  )  [signal]

Emitted after all the data that has been specified by a prior call to writeStdin() has actually been written to the child process.

Parameters:
proc a pointer to the process


The documentation for this class was generated from the following files:
  • extprocess.h
  • extprocess.cpp

kextprocess

Skip menu "kextprocess"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kextprocess
  • shaman
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal