glibmm 2.82.0
|
Child processes. More...
#include <giomm/subprocess.h>
Public Types | |
enum class | Flags { Flags::NONE = 0x0 , Flags::STDIN_PIPE = (1u << 0) , Flags::STDIN_INHERIT = (1u << 1) , Flags::STDOUT_PIPE = (1u << 2) , Flags::STDOUT_SILENCE = (1u << 3) , Flags::STDERR_PIPE = (1u << 4) , Flags::STDERR_SILENCE = (1u << 5) , Flags::STDERR_MERGE = (1u << 6) , Flags::INHERIT_FDS = (1u << 7) , Flags::SEARCH_PATH_FROM_ENVP = (1u << 8) } |
Flags to define the behaviour of a Subprocess. More... | |
Public Types inherited from Glib::Object | |
using | DestroyNotify = void(*)(gpointer data) |
Public Member Functions | |
Subprocess (Subprocess &&src) noexcept | |
Subprocess & | operator= (Subprocess &&src) noexcept |
~Subprocess () noexcept override | |
GSubprocess * | gobj () |
Provides access to the underlying C GObject. | |
const GSubprocess * | gobj () const |
Provides access to the underlying C GObject. | |
GSubprocess * | gobj_copy () |
Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs. | |
Glib::RefPtr< OutputStream > | get_stdin_pipe () |
Gets the OutputStream that you can write to in order to give data to the stdin of subprocess. | |
Glib::RefPtr< const OutputStream > | get_stdin_pipe () const |
Gets the OutputStream that you can write to in order to give data to the stdin of subprocess. | |
Glib::RefPtr< InputStream > | get_stdout_pipe () |
Gets the InputStream from which to read the stdout output of subprocess. | |
Glib::RefPtr< const InputStream > | get_stdout_pipe () const |
Gets the InputStream from which to read the stdout output of subprocess. | |
Glib::RefPtr< InputStream > | get_stderr_pipe () |
Gets the InputStream from which to read the stderr output of subprocess. | |
Glib::RefPtr< const InputStream > | get_stderr_pipe () const |
Gets the InputStream from which to read the stderr output of subprocess. | |
Glib::ustring | get_identifier () const |
On UNIX, returns the process ID as a decimal string. | |
void | send_signal (int signal_num) |
Sends the UNIX signal signal_num to the subprocess, if it is still running. | |
void | force_exit () |
Use an operating-system specific method to attempt an immediate, forceful termination of the process. | |
void | wait (const Glib::RefPtr< Cancellable > &cancellable={}) const |
Synchronously wait for the subprocess to terminate. | |
void | wait_async (const SlotAsyncReady &slot, const Glib::RefPtr< Cancellable > &cancellable={}) const |
Wait for the subprocess to terminate. | |
void | wait_finish (const Glib::RefPtr< AsyncResult > &result) const |
Collects the result of a previous call to g_subprocess_wait_async(). | |
void | wait_check (const Glib::RefPtr< Cancellable > &cancellable={}) const |
Combines g_subprocess_wait() with Glib::spawn_check_wait_status(). | |
void | wait_check_async (const SlotAsyncReady &slot, const Glib::RefPtr< Cancellable > &cancellable={}) const |
Combines g_subprocess_wait_async() with Glib::spawn_check_wait_status(). | |
void | wait_check_finish (const Glib::RefPtr< AsyncResult > &result) const |
Collects the result of a previous call to g_subprocess_wait_check_async(). | |
int | get_status () const |
Gets the raw status code of the process, as from waitpid(). | |
bool | get_successful () const |
Checks if the process was "successful". | |
bool | get_if_exited () const |
Check if the given subprocess exited normally (ie: by way of exit() or return from main()). | |
int | get_exit_status () const |
Check the exit status of the subprocess, given that it exited normally. | |
bool | get_if_signaled () const |
Check if the given subprocess terminated in response to a signal. | |
int | get_term_sig () const |
Get the signal number that caused the subprocess to terminate, given that it terminated due to a signal. | |
std::pair< Glib::RefPtr< Glib::Bytes >, Glib::RefPtr< Glib::Bytes > > | communicate (const Glib::RefPtr< const Glib::Bytes > &stdin_buf, const Glib::RefPtr< Cancellable > &cancellable={}) |
Communicate with the subprocess until it terminates, and all input and output has been completed. | |
void | communicate_async (const Glib::RefPtr< const Glib::Bytes > &stdin_buf, const SlotAsyncReady &slot, const Glib::RefPtr< Cancellable > &cancellable={}) |
Asynchronous version of g_subprocess_communicate(). | |
std::pair< Glib::RefPtr< Glib::Bytes >, Glib::RefPtr< Glib::Bytes > > | communicate_finish (const Glib::RefPtr< AsyncResult > &result) |
Complete an invocation of communicate_async(). | |
std::pair< Glib::ustring, Glib::ustring > | communicate_utf8 (const Glib::ustring &stdin_buf, const Glib::RefPtr< Cancellable > &cancellable={}) |
Like communicate(), but validates the output of the process as UTF-8, and returns it as a regular Glib::ustring. | |
void | communicate_utf8_async (const Glib::ustring &stdin_buf, const SlotAsyncReady &slot, const Glib::RefPtr< Cancellable > &cancellable={}) |
Asynchronous version of g_subprocess_communicate_utf8(). | |
std::pair< Glib::ustring, Glib::ustring > | communicate_utf8_finish (const Glib::RefPtr< AsyncResult > &result) |
Complete an invocation of communicate_utf8_async(). | |
Public Member Functions inherited from Glib::Object | |
Object (const Object &)=delete | |
Object & | operator= (const Object &)=delete |
Object (Object &&src) noexcept | |
Object & | operator= (Object &&src) noexcept |
void * | get_data (const QueryQuark & key) |
void | set_data (const Quark & key, void *data) |
void | set_data_with_c_callback (const Quark & key, void *data, GDestroyNotify notify) |
void | set_data (const Quark & key, void *data, DestroyNotify notify) |
Prefer set_data_with_c_callback() with a callback with C linkage. | |
void | remove_data (const QueryQuark &quark) |
void * | steal_data (const QueryQuark &quark) |
Public Member Functions inherited from Glib::ObjectBase | |
ObjectBase (const ObjectBase &)=delete | |
ObjectBase & | operator= (const ObjectBase &)=delete |
void | set_property_value (const Glib::ustring & property_name, const Glib::ValueBase & value) |
You probably want to use a specific property_*() accessor method instead. | |
void | get_property_value (const Glib::ustring & property_name, Glib::ValueBase & value) const |
You probably want to use a specific property_*() accessor method instead. | |
template<class PropertyType > | |
void | set_property (const Glib::ustring & property_name, const PropertyType & value) |
You probably want to use a specific property_*() accessor method instead. | |
template<class PropertyType > | |
void | get_property (const Glib::ustring & property_name, PropertyType & value) const |
You probably want to use a specific property_*() accessor method instead. | |
template<class PropertyType > | |
PropertyType | get_property (const Glib::ustring & property_name) const |
You probably want to use a specific property_*() accessor method instead. | |
sigc::connection | connect_property_changed (const Glib::ustring & property_name, const sigc::slot< void()> &slot) |
You can use the signal_changed() signal of the property proxy instead. | |
sigc::connection | connect_property_changed (const Glib::ustring & property_name, sigc::slot< void()> &&slot) |
You can use the signal_changed() signal of the property proxy instead. | |
void | freeze_notify () |
Increases the freeze count on object. | |
void | thaw_notify () |
Reverts the effect of a previous call to freeze_notify(). | |
virtual void | reference () const |
Increment the reference count for this object. | |
virtual void | unreference () const |
Decrement the reference count for this object. | |
GObject * | gobj () |
Provides access to the underlying C GObject. | |
const GObject * | gobj () const |
Provides access to the underlying C GObject. | |
GObject * | gobj_copy () const |
Give a ref-ed copy to someone. Use for direct struct access. | |
Public Member Functions inherited from Gio::Initable | |
Initable (Initable &&src) noexcept | |
Initable & | operator= (Initable &&src) noexcept |
~Initable () noexcept override | |
GInitable * | gobj () |
Provides access to the underlying C GObject. | |
const GInitable * | gobj () const |
Provides access to the underlying C GObject. | |
Public Member Functions inherited from Glib::Interface | |
Interface () | |
A Default constructor. | |
Interface (Interface &&src) noexcept | |
Interface & | operator= (Interface &&src) noexcept |
Interface (const Glib::Interface_Class &interface_class) | |
Called by constructors of derived classes. | |
Interface (GObject *castitem) | |
Called by constructors of derived classes. | |
~Interface () noexcept override | |
Interface (const Interface &)=delete | |
Interface & | operator= (const Interface &)=delete |
GObject * | gobj () |
const GObject * | gobj () const |
Static Public Member Functions | |
static GType | get_type () |
Get the GType for this class, for use with the underlying GObject type system. | |
static Glib::RefPtr< Subprocess > | create (const std::vector< std::string > &argv, Flags flags=Flags::NONE) |
Create a new process with the given flags and argument list. | |
Static Public Member Functions inherited from Gio::Initable | |
static void | add_interface (GType gtype_implementer) |
static GType | get_type () |
Get the GType for this class, for use with the underlying GObject type system. | |
Protected Member Functions | |
Subprocess (const std::vector< std::string > &argv, Flags flags=Flags::NONE) | |
Protected Member Functions inherited from Glib::Object | |
Object () | |
Object (const Glib::ConstructParams &construct_params) | |
Object (GObject *castitem) | |
~Object () noexcept override | |
Protected Member Functions inherited from Glib::ObjectBase | |
ObjectBase () | |
This default constructor is called implicitly from the constructor of user-derived classes, even if, for instance, Gtk::Button calls a different ObjectBase constructor. | |
ObjectBase (const char *custom_type_name) | |
A derived constructor always overrides this choice. | |
ObjectBase (const std::type_info &custom_type_info) | |
This constructor is a special feature to allow creation of derived types on the fly, without having to use g_object_new() manually. | |
ObjectBase (ObjectBase &&src) noexcept | |
ObjectBase & | operator= (ObjectBase &&src) noexcept |
virtual | ~ObjectBase () noexcept=0 |
void | initialize (GObject *castitem) |
void | initialize_move (GObject *castitem, Glib::ObjectBase *previous_wrapper) |
Protected Member Functions inherited from Gio::Initable | |
Initable () | |
You should derive from this class to use it. | |
void | init (const Glib::RefPtr< Cancellable > &cancellable) |
Initializes the object implementing the interface. | |
void | init () |
A init() convenience overload. | |
virtual bool | init_vfunc (const Glib::RefPtr< Cancellable > &cancellable, GError **error) |
Related Symbols | |
(Note that these are not member symbols.) | |
Glib::RefPtr< Gio::Subprocess > | wrap (GSubprocess *object, bool take_copy=false) |
A Glib::wrap() method for this object. | |
Related Symbols inherited from Glib::Object | |
Glib::RefPtr< Glib::Object > | wrap (GObject *object, bool take_copy=false) |
Related Symbols inherited from Gio::Initable | |
Glib::RefPtr< Gio::Initable > | wrap (GInitable *object, bool take_copy=false) |
A Glib::wrap() method for this object. | |
Child processes.
Gio::Subprocess allows the creation of and interaction with child processes.
Processes can be communicated with using standard GIO-style APIs (Gio::InputStream, Gio::OutputStream). There are GIO-style APIs to wait for process termination (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 as Gio::OutputStream::splice_async(). This makes Gio::Subprocess significantly more powerful and flexible than equivalent APIs in some other languages such as the subprocess.py
included with Python. For example, using Gio::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.
Gio::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 Gio::Subprocess API (unless Gio::Subprocess::Flags::INHERIT_FDS was specified).
Gio::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, Gio::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 Gio::Subprocess::Flags::SEARCH_PATH_FROM_ENVP to use the PATH
of the launcher environment instead.
Gio::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 create(). The Gio::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 Gio::Subprocess will involve calling create(), followed by wait_async() or wait(). After the process exits, the status can be checked using functions such as get_if_exited() (which are similar to the familiar WIFEXITED-style POSIX macros).
|
noexcept |
|
overridenoexcept |
|
explicitprotected |
std::pair< Glib::RefPtr< Glib::Bytes >, Glib::RefPtr< Glib::Bytes > > Gio::Subprocess::communicate | ( | const Glib::RefPtr< const Glib::Bytes > & | stdin_buf, |
const Glib::RefPtr< Cancellable > & | cancellable = {} |
||
) |
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 Gio::Subprocess::Flags::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 Gio::Subprocess::Flags::STDOUT_PIPE or Gio::Subprocess::Flags::STDERR_PIPE were used, reads from those streams. The data that was read is returned in stdout_buf and/or the stderr_buf.
If the subprocess was created with Gio::Subprocess::Flags::STDOUT_PIPE, stdout_buf will contain the data read from stdout. Otherwise, for subprocesses not created with Gio::Subprocess::Flags::STDOUT_PIPE, stdout_buf will be set to an empty RefPtr. Similar provisions apply to stderr_buf and Gio::Subprocess::Flags::STDERR_PIPE.
If you desire the stdout and stderr data to be interleaved, create the subprocess with Gio::Subprocess::Flags::STDOUT_PIPE and Gio::Subprocess::Flags::STDERR_MERGE. The merged result will be returned in stdout_buf, and stderr_buf will be set to an empty RefPtr.
In case of any error (including cancellation), an exception will be thrown.
After a normal return (no exception thrown), the subprocess has exited and the exit status inspection APIs (eg: get_if_exited(), get_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 an empty RefPtr. |
cancellable | A Cancellable. |
Glib::Error |
void Gio::Subprocess::communicate_async | ( | const Glib::RefPtr< const Glib::Bytes > & | stdin_buf, |
const SlotAsyncReady & | slot, | ||
const Glib::RefPtr< Cancellable > & | cancellable = {} |
||
) |
Asynchronous version of g_subprocess_communicate().
Complete invocation with g_subprocess_communicate_finish().
stdin_buf | Input data, or nullptr . |
cancellable | Cancellable. |
slot | Callback. |
std::pair< Glib::RefPtr< Glib::Bytes >, Glib::RefPtr< Glib::Bytes > > Gio::Subprocess::communicate_finish | ( | const Glib::RefPtr< AsyncResult > & | result | ) |
Complete an invocation of communicate_async().
result | Result. |
Glib::Error |
std::pair< Glib::ustring, Glib::ustring > Gio::Subprocess::communicate_utf8 | ( | const Glib::ustring & | stdin_buf, |
const Glib::RefPtr< Cancellable > & | cancellable = {} |
||
) |
Like communicate(), but validates the output of the process as UTF-8, and returns it as a regular Glib::ustring.
On error, an exception is thrown.
stdin_buf | Data to send to the stdin of the subprocess, or an empty string. |
cancellable | A Cancellable. |
Glib::Error |
void Gio::Subprocess::communicate_utf8_async | ( | const Glib::ustring & | stdin_buf, |
const SlotAsyncReady & | slot, | ||
const Glib::RefPtr< Cancellable > & | cancellable = {} |
||
) |
Asynchronous version of g_subprocess_communicate_utf8().
Complete invocation with g_subprocess_communicate_utf8_finish().
stdin_buf | Input data, or nullptr . |
cancellable | Cancellable. |
slot | Callback. |
std::pair< Glib::ustring, Glib::ustring > Gio::Subprocess::communicate_utf8_finish | ( | const Glib::RefPtr< AsyncResult > & | result | ) |
Complete an invocation of communicate_utf8_async().
result | Result. |
Glib::Error |
|
static |
Create a new process with the given flags and argument list.
argv | Commandline arguments for the subprocess. |
flags | Flags that define the behaviour of the subprocess. |
Glib::Error |
void Gio::Subprocess::force_exit | ( | ) |
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 g_subprocess_wait() to monitor the status of the process after calling this function.
On Unix, this function sends S::IGKILL.
int Gio::Subprocess::get_exit_status | ( | ) | const |
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 g_subprocess_wait() and unless g_subprocess_get_if_exited() returned true
.
Glib::ustring Gio::Subprocess::get_identifier | ( | ) | const |
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 nullptr
.
nullptr
if the subprocess has terminated. bool Gio::Subprocess::get_if_exited | ( | ) | const |
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 g_subprocess_wait() has returned.
true
if the case of a normal exit. bool Gio::Subprocess::get_if_signaled | ( | ) | const |
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 g_subprocess_wait() has returned.
true
if the case of termination due to a signal. int Gio::Subprocess::get_status | ( | ) | const |
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 Glib::spawn_check_wait_status().
It is more likely that you want to use g_subprocess_get_if_exited() followed by g_subprocess_get_exit_status().
It is an error to call this function before g_subprocess_wait() has returned.
Glib::RefPtr< InputStream > Gio::Subprocess::get_stderr_pipe | ( | ) |
Gets the InputStream from which to read the stderr output of subprocess.
The process must have been created with Gio::Subprocess::Flags::STDERR_PIPE, otherwise nullptr
will be returned.
Glib::RefPtr< const InputStream > Gio::Subprocess::get_stderr_pipe | ( | ) | const |
Gets the InputStream from which to read the stderr output of subprocess.
The process must have been created with Gio::Subprocess::Flags::STDERR_PIPE, otherwise nullptr
will be returned.
Glib::RefPtr< OutputStream > Gio::Subprocess::get_stdin_pipe | ( | ) |
Gets the OutputStream that you can write to in order to give data to the stdin of subprocess.
The process must have been created with Gio::Subprocess::Flags::STDIN_PIPE and not Gio::Subprocess::Flags::STDIN_INHERIT, otherwise nullptr
will be returned.
Glib::RefPtr< const OutputStream > Gio::Subprocess::get_stdin_pipe | ( | ) | const |
Gets the OutputStream that you can write to in order to give data to the stdin of subprocess.
The process must have been created with Gio::Subprocess::Flags::STDIN_PIPE and not Gio::Subprocess::Flags::STDIN_INHERIT, otherwise nullptr
will be returned.
Glib::RefPtr< InputStream > Gio::Subprocess::get_stdout_pipe | ( | ) |
Gets the InputStream from which to read the stdout output of subprocess.
The process must have been created with Gio::Subprocess::Flags::STDOUT_PIPE, otherwise nullptr
will be returned.
Glib::RefPtr< const InputStream > Gio::Subprocess::get_stdout_pipe | ( | ) | const |
Gets the InputStream from which to read the stdout output of subprocess.
The process must have been created with Gio::Subprocess::Flags::STDOUT_PIPE, otherwise nullptr
will be returned.
bool Gio::Subprocess::get_successful | ( | ) | const |
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 g_subprocess_wait() has returned.
true
if the process exited cleanly with a exit status of 0. int Gio::Subprocess::get_term_sig | ( | ) | const |
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 g_subprocess_wait() and unless g_subprocess_get_if_signaled() returned true
.
Get the GType for this class, for use with the underlying GObject type system.
|
inline |
Provides access to the underlying C GObject.
|
inline |
Provides access to the underlying C GObject.
GSubprocess * Gio::Subprocess::gobj_copy | ( | ) |
Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.
|
noexcept |
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. |
void Gio::Subprocess::wait | ( | const Glib::RefPtr< Cancellable > & | cancellable = {} | ) | const |
Synchronously wait for the subprocess to terminate.
After the process terminates you can query its exit status with functions such as g_subprocess_get_if_exited() and g_subprocess_get_exit_status().
This function does not fail in the case of the subprocess having abnormal termination. See g_subprocess_wait_check() for that.
Cancelling cancellable doesn't kill the subprocess. Call g_subprocess_force_exit() if it is desirable.
cancellable | A Cancellable. |
Glib::Error |
void Gio::Subprocess::wait_async | ( | const SlotAsyncReady & | slot, |
const Glib::RefPtr< Cancellable > & | cancellable = {} |
||
) | const |
Wait for the subprocess to terminate.
This is the asynchronous version of g_subprocess_wait().
cancellable | A Cancellable, or nullptr . |
slot | A SlotAsyncReady to call when the operation is complete. |
void Gio::Subprocess::wait_check | ( | const Glib::RefPtr< Cancellable > & | cancellable = {} | ) | const |
Combines g_subprocess_wait() with Glib::spawn_check_wait_status().
cancellable | A Cancellable. |
Glib::Error |
void Gio::Subprocess::wait_check_async | ( | const SlotAsyncReady & | slot, |
const Glib::RefPtr< Cancellable > & | cancellable = {} |
||
) | const |
Combines g_subprocess_wait_async() with Glib::spawn_check_wait_status().
This is the asynchronous version of g_subprocess_wait_check().
cancellable | A Cancellable, or nullptr . |
slot | A SlotAsyncReady to call when the operation is complete. |
void Gio::Subprocess::wait_check_finish | ( | const Glib::RefPtr< AsyncResult > & | result | ) | const |
Collects the result of a previous call to g_subprocess_wait_check_async().
result | The AsyncResult passed to your SlotAsyncReady. |
Glib::Error |
void Gio::Subprocess::wait_finish | ( | const Glib::RefPtr< AsyncResult > & | result | ) | const |
Collects the result of a previous call to g_subprocess_wait_async().
result | The AsyncResult passed to your SlotAsyncReady. |
Glib::Error |
|
related |
A Glib::wrap() method for this object.
object | The C instance. |
take_copy | False if the result should take ownership of the C instance. True if it should take a new copy or ref. |