#[repr(transparent)]
pub struct Subprocess { /* private fields */ }
Expand description

Subprocess allows the creation of and interaction with child processes.

Processes can be communicated with using standard GIO-style APIs (ie: InputStream, OutputStream). There are GIO-style APIs to wait for process termination (ie: cancellable and with an asynchronous variant).

There is an API to force a process to terminate, as well as a race-free API for sending UNIX signals to a subprocess.

One major advantage that GIO brings over the core GLib library is comprehensive API for asynchronous I/O, such OutputStreamExt::splice_async(). This makes GSubprocess significantly more powerful and flexible than equivalent APIs in some other languages such as the subprocess.py included with Python. For example, using Subprocess one could create two child processes, reading standard output from the first, processing it, and writing to the input stream of the second, all without blocking the main loop.

A powerful communicate() API is provided similar to the communicate() method of subprocess.py. This enables very easy interaction with a subprocess that has been opened with pipes.

Subprocess defaults to tight control over the file descriptors open in the child process, avoiding dangling-fd issues that are caused by a simple fork()/exec(). The only open file descriptors in the spawned process are ones that were explicitly specified by the Subprocess API (unless SubprocessFlags::INHERIT_FDS was specified).

Subprocess will quickly reap all child processes as they exit, avoiding “zombie processes” remaining around for long periods of time. wait() can be used to wait for this to happen, but it will happen even without the call being explicitly made.

As a matter of principle, Subprocess has no API that accepts shell-style space-separated strings. It will, however, match the typical shell behaviour of searching the PATH for executables that do not contain a directory separator in their name. By default, the PATH of the current process is used. You can specify SubprocessFlags::SEARCH_PATH_FROM_ENVP to use the PATH of the launcher environment instead.

Subprocess attempts to have a very simple API for most uses (ie: spawning a subprocess with arguments and support for most typical kinds of input and output redirection). See g_subprocess_new(). The SubprocessLauncher API is provided for more complicated cases (advanced types of redirection, environment variable manipulation, change of working directory, child setup functions, etc).

A typical use of Subprocess will involve calling g_subprocess_new(), followed by wait_async() or wait(). After the process exits, the status can be checked using functions such as has_exited() (which are similar to the familiar WIFEXITED-style POSIX macros).

Implements

glib::ObjectExt, InitableExt

Implementations

Asynchronous version of communicate_utf8(). Complete invocation with g_subprocess_communicate_utf8_finish().

stdin_buf

Input data, or None

cancellable

Cancellable

callback

Callback

Create a new process with the given flags and argument list.

The argument list is expected to be None-terminated.

argv

commandline arguments for the subprocess

flags

flags that define the behaviour of the subprocess

Returns

A newly created Subprocess, or None on error (and error will be set)

Communicate with the subprocess until it terminates, and all input and output has been completed.

If stdin_buf is given, the subprocess must have been created with SubprocessFlags::STDIN_PIPE. The given data is fed to the stdin of the subprocess and the pipe is closed (ie: EOF).

At the same time (as not to cause blocking when dealing with large amounts of data), if SubprocessFlags::STDOUT_PIPE or SubprocessFlags::STDERR_PIPE were used, reads from those streams. The data that was read is returned in stdout and/or the stderr.

If the subprocess was created with SubprocessFlags::STDOUT_PIPE, stdout_buf will contain the data read from stdout. Otherwise, for subprocesses not created with SubprocessFlags::STDOUT_PIPE, stdout_buf will be set to None. Similar provisions apply to stderr_buf and SubprocessFlags::STDERR_PIPE.

As usual, any output variable may be given as None to ignore it.

If you desire the stdout and stderr data to be interleaved, create the subprocess with SubprocessFlags::STDOUT_PIPE and SubprocessFlags::STDERR_MERGE. The merged result will be returned in stdout_buf and stderr_buf will be set to None.

In case of any error (including cancellation), false will be returned with error set. Some or all of the stdin data may have been written. Any stdout or stderr data that has been read will be discarded. None of the out variables (aside from error) will have been set to anything in particular and should not be inspected.

In the case that true is returned, the subprocess has exited and the exit status inspection APIs (eg: has_exited(), exit_status()) may be used.

You should not attempt to use any of the subprocess pipes after starting this function, since they may be left in strange states, even if the operation was cancelled. You should especially not attempt to interact with the pipes while the operation is in progress (either from another thread or if using the asynchronous version).

stdin_buf

data to send to the stdin of the subprocess, or None

cancellable

a Cancellable

Returns

true if successful

stdout_buf

data read from the subprocess stdout

stderr_buf

data read from the subprocess stderr

Asynchronous version of communicate(). Complete invocation with g_subprocess_communicate_finish().

stdin_buf

Input data, or None

cancellable

Cancellable

callback

Callback

Like communicate(), but validates the output of the process as UTF-8, and returns it as a regular NUL terminated string.

On error, stdout_buf and stderr_buf will be set to undefined values and should not be used.

stdin_buf

data to send to the stdin of the subprocess, or None

cancellable

a Cancellable

Returns
stdout_buf

data read from the subprocess stdout

stderr_buf

data read from the subprocess stderr

Use an operating-system specific method to attempt an immediate, forceful termination of the process. There is no mechanism to determine whether or not the request itself was successful; however, you can use wait() to monitor the status of the process after calling this function.

On Unix, this function sends SIGKILL.

Check the exit status of the subprocess, given that it exited normally. This is the value passed to the exit() system call or the return value from main.

This is equivalent to the system WEXITSTATUS macro.

It is an error to call this function before wait() and unless has_exited() returned true.

Returns

the exit status

On UNIX, returns the process ID as a decimal string. On Windows, returns the result of GetProcessId() also as a string. If the subprocess has terminated, this will return None.

Returns

the subprocess identifier, or None if the subprocess has terminated

Check if the given subprocess exited normally (ie: by way of exit() or return from main()).

This is equivalent to the system WIFEXITED macro.

It is an error to call this function before wait() has returned.

Returns

true if the case of a normal exit

Check if the given subprocess terminated in response to a signal.

This is equivalent to the system WIFSIGNALED macro.

It is an error to call this function before wait() has returned.

Returns

true if the case of termination due to a signal

Gets the raw status code of the process, as from waitpid().

This value has no particular meaning, but it can be used with the macros defined by the system headers such as WIFEXITED. It can also be used with g_spawn_check_wait_status().

It is more likely that you want to use has_exited() followed by exit_status().

It is an error to call this function before wait() has returned.

Returns

the (meaningless) waitpid() exit status from the kernel

Gets the InputStream from which to read the stderr output of self.

The process must have been created with SubprocessFlags::STDERR_PIPE, otherwise None will be returned.

Returns

the stderr pipe

Gets the OutputStream that you can write to in order to give data to the stdin of self.

The process must have been created with SubprocessFlags::STDIN_PIPE and not SubprocessFlags::STDIN_INHERIT, otherwise None will be returned.

Returns

the stdout pipe

Gets the InputStream from which to read the stdout output of self.

The process must have been created with SubprocessFlags::STDOUT_PIPE, otherwise None will be returned.

Returns

the stdout pipe

Checks if the process was “successful”. A process is considered successful if it exited cleanly with an exit status of 0, either by way of the exit() system call or return from main().

It is an error to call this function before wait() has returned.

Returns

true if the process exited cleanly with a exit status of 0

Get the signal number that caused the subprocess to terminate, given that it terminated due to a signal.

This is equivalent to the system WTERMSIG macro.

It is an error to call this function before wait() and unless has_signaled() returned true.

Returns

the signal causing termination

Available on non-Windows only.

Sends the UNIX signal signal_num to the subprocess, if it is still running.

This API is race-free. If the subprocess has terminated, it will not be signalled.

This API is not available on Windows.

signal_num

the signal number to send

Synchronously wait for the subprocess to terminate.

After the process terminates you can query its exit status with functions such as has_exited() and exit_status().

This function does not fail in the case of the subprocess having abnormal termination. See wait_check() for that.

Cancelling cancellable doesn’t kill the subprocess. Call force_exit() if it is desirable.

cancellable

a Cancellable

Returns

true on success, false if cancellable was cancelled

Wait for the subprocess to terminate.

This is the asynchronous version of wait().

cancellable

a Cancellable, or None

callback

a GAsyncReadyCallback to call when the operation is complete

Combines wait() with g_spawn_check_wait_status().

cancellable

a Cancellable

Returns

true on success, false if process exited abnormally, or cancellable was cancelled

Combines wait_async() with g_spawn_check_wait_status().

This is the asynchronous version of wait_check().

cancellable

a Cancellable, or None

callback

a GAsyncReadyCallback to call when the operation is complete

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Returns the type identifier of Self.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Upcasts an object to a superclass or interface T. Read more

Upcasts an object to a reference of its superclass or interface T. Read more

Tries to downcast to a subclass or interface implementor T. Read more

Tries to downcast to a reference of its subclass or interface implementor T. Read more

Tries to cast to an object of type T. This handles upcasting, downcasting and casting between interface and interface implementors. All checks are performed at runtime, while downcast and upcast will do many checks at compile-time already. Read more

Tries to cast to reference to an object of type T. This handles upcasting, downcasting and casting between interface and interface implementors. All checks are performed at runtime, while downcast and upcast will do many checks at compile-time already. Read more

Casts to T unconditionally. Read more

Casts to &T unconditionally. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Returns true if the object is an instance of (can be cast to) T.

Returns the type of the object.

Returns the ObjectClass of the object. Read more

Returns the class of the object.

Returns the class of the object in the given type T. Read more

Returns the interface T of the object. Read more

Similar to Self::set_property but fails instead of panicking.

Sets the property property_name of the object to value value. Read more

Similar to Self::set_property but fails instead of panicking.

Sets the property property_name of the object to value value. Read more

Similar to Self::set_properties but fails instead of panicking.

Sets multiple properties of the object at once. Read more

Similar to Self::set_properties_from_value but fails instead of panicking.

Sets multiple properties of the object at once. Read more

Similar to Self::property but fails instead of panicking.

Gets the property property_name of the object and cast it to the type V. Read more

Similar to Self::property_value but fails instead of panicking.

Gets the property property_name of the object. Read more

Check if the object has a property property_name of the given type_. Read more

Get the type of the property property_name of this object. Read more

Get the ParamSpec of the property property_name of this object.

Return all ParamSpec of the properties of this object.

Freeze all property notifications until the return guard object is dropped. Read more

Set arbitrary data on this object with the given key. Read more

Return previously set arbitrary data of this object with the given key. Read more

Retrieve previously set arbitrary data of this object with the given key. Read more

Set arbitrary data on this object with the given key. Read more

Return previously set arbitrary data of this object with the given key. Read more

Retrieve previously set arbitrary data of this object with the given key. Read more

Block a given signal handler. Read more

Unblock a given signal handler.

Stop emission of the currently emitted signal.

Stop emission of the currently emitted signal by the (possibly detailed) signal name.

Similar to Self::connect but fails instead of panicking.

Connect to the signal signal_name on this object. Read more

Similar to Self::connect_id but fails instead of panicking.

Connect to the signal signal_id on this object. Read more

Similar to Self::connect_local but fails instead of panicking.

Connect to the signal signal_name on this object. Read more

Similar to Self::connect_local_id but fails instead of panicking.

Connect to the signal signal_id on this object. Read more

Similar to Self::connect_unsafe but fails instead of panicking.

Connect to the signal signal_name on this object. Read more

Similar to Self::connect_unsafe_id but fails instead of panicking.

Similar to Self::connect_closure but fails instead of panicking.

Connect a closure to the signal signal_name on this object. Read more

Similar to Self::connect_closure_id but fails instead of panicking.

Connect a closure to the signal signal_id on this object. Read more

Limits the lifetime of closure to the lifetime of the object. When the object’s reference count drops to zero, the closure will be invalidated. An invalidated closure will ignore any calls to Closure::invoke. Read more

Connect to the signal signal_id on this object. Read more

Similar to Self::emit but fails instead of panicking.

Emit signal by signal id. Read more

Similar to Self::emit_with_values but fails instead of panicking.

Same as Self::emit but takes Value for the arguments.

Similar to Self::emit_by_name but fails instead of panicking.

Emit signal by its name. Read more

Similar to Self::emit_by_name_with_values but fails instead of panicking.

Emit signal by its name. Read more

Similar to Self::emit_with_details but fails instead of panicking.

Emit signal by signal id with details. Read more

Similar to Self::emit_with_details_and_values but fails instead of panicking.

Emit signal by signal id with details. Read more

Disconnect a previously connected signal handler.

Connect to the notify signal of the object. Read more

Connect to the notify signal of the object. Read more

Connect to the notify signal of the object. Read more

Notify that the given property has changed its value. Read more

Notify that the given property has changed its value. Read more

Downgrade this object to a weak reference.

Bind property source_property on this object to the target_property on the target object. Read more

Returns the strong reference count of this object.

Ensures that the type has been registered with the type system.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.