glibmm 2.80.0
|
Environment options for launching a child process. More...
#include <giomm/subprocesslauncher.h>
Public Member Functions | |
SubprocessLauncher (SubprocessLauncher &&src) noexcept | |
SubprocessLauncher & | operator= (SubprocessLauncher &&src) noexcept |
~SubprocessLauncher () noexcept override | |
GSubprocessLauncher * | gobj () |
Provides access to the underlying C GObject. | |
const GSubprocessLauncher * | gobj () const |
Provides access to the underlying C GObject. | |
GSubprocessLauncher * | 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< Subprocess > | spawn (const std::vector< std::string > &argv) |
Creates a Subprocess given a provided array of arguments. | |
void | inherit_environ () |
Inherit the parent process’ environment. | |
void | set_environ (const std::vector< std::string > &env) |
Replace the entire environment of processes launched from this launcher with the given 'environ' variable. | |
void | setenv (const std::string &variable, const std::string & value, bool overwrite) |
Sets the environment variable variable in the environment of processes launched from this launcher. | |
void | unsetenv (const std::string &variable) |
Removes the environment variable variable from the environment of processes launched from this launcher. | |
std::string | getenv (const std::string &variable) |
Returns the value of the environment variable variable in the environment of processes launched from this launcher. | |
void | set_cwd (const std::string &cwd) |
Sets the current working directory that processes will be launched with. | |
void | set_flags (Subprocess::Flags flags) |
Sets the flags on the launcher. | |
void | set_stdin_file_path (const std::string & path) |
Sets the file path to use as the stdin for spawned processes. | |
void | take_stdin_fd (int fd) |
Sets the file descriptor to use as the stdin for spawned processes. | |
void | set_stdout_file_path (const std::string & path) |
Sets the file path to use as the stdout for spawned processes. | |
void | take_stdout_fd (int fd) |
Sets the file descriptor to use as the stdout for spawned processes. | |
void | set_stderr_file_path (const std::string & path) |
Sets the file path to use as the stderr for spawned processes. | |
void | take_stderr_fd (int fd) |
Sets the file descriptor to use as the stderr for spawned processes. | |
void | take_fd (int source_fd, int target_fd) |
Transfer an arbitrary file descriptor from parent process to the child. | |
void | close () |
Closes all the file descriptors previously passed to the object with g_subprocess_launcher_take_fd(), g_subprocess_launcher_take_stderr_fd(), etc. | |
void | set_child_setup (const Glib::SlotSpawnChildSetup &child_setup={}) |
Sets up a child setup function. | |
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. | |
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< SubprocessLauncher > | create (Subprocess::Flags flags=Subprocess::Flags::NONE) |
Creates a new SubprocessLauncher. | |
Related Symbols | |
(Note that these are not member symbols.) | |
Glib::RefPtr< Gio::SubprocessLauncher > | wrap (GSubprocessLauncher *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) |
Additional Inherited Members | |
Public Types inherited from Glib::Object | |
using | DestroyNotify = void(*)(gpointer data) |
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) |
Environment options for launching a child process.
This class contains a set of options for launching child processes, such as where its standard input and output will be directed, the argument list, the environment, and more.
While the Gio::Subprocess class has high level functions covering popular cases, use of this class allows access to more advanced options. It can also be used to launch multiple subprocesses with a similar configuration.
|
noexcept |
|
overridenoexcept |
void Gio::SubprocessLauncher::close | ( | ) |
Closes all the file descriptors previously passed to the object with g_subprocess_launcher_take_fd(), g_subprocess_launcher_take_stderr_fd(), etc.
After calling this method, any subsequent calls to g_subprocess_launcher_spawn() or g_subprocess_launcher_spawnv() will return Gio::Error::CLOSED. This method is idempotent if called more than once.
This function is called automatically when the SubprocessLauncher is disposed, but is provided separately so that garbage collected language bindings can call it earlier to guarantee when FDs are closed.
|
static |
Creates a new SubprocessLauncher.
The launcher is created with the default options. A copy of the environment of the calling process is made at the time of this call and will be used as the environment that the process is launched in.
flags | SubprocessFlags. |
Get the GType for this class, for use with the underlying GObject type system.
std::string Gio::SubprocessLauncher::getenv | ( | const std::string & | variable | ) |
Returns the value of the environment variable variable in the environment of processes launched from this launcher.
On UNIX, the returned string can be an arbitrary byte string. On Windows, it will be UTF-8.
variable | The environment variable to get. |
nullptr
if unset.
|
inline |
Provides access to the underlying C GObject.
|
inline |
Provides access to the underlying C GObject.
GSubprocessLauncher * Gio::SubprocessLauncher::gobj_copy | ( | ) |
Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.
void Gio::SubprocessLauncher::inherit_environ | ( | ) |
Inherit the parent process’ environment.
|
noexcept |
void Gio::SubprocessLauncher::set_child_setup | ( | const Glib::SlotSpawnChildSetup & | child_setup = {} | ) |
Sets up a child setup function.
The child setup function will be called after fork() but before exec() on the child's side.
A copy of the child_setup slot is stored. The copy will not be automatically deleted on the child's side of the fork(). It will only be deleted when the last reference on the SubprocessLauncher is dropped or when a new child setup slot is given.
An empty slot can be given as child_setup to disable the functionality.
Child setup functions are only available on UNIX.
child_setup | A Glib::SlotSpawnChildSetup to use as the child setup function. |
void Gio::SubprocessLauncher::set_cwd | ( | const std::string & | cwd | ) |
Sets the current working directory that processes will be launched with.
By default processes are launched with the current working directory of the launching process at the time of launch.
cwd | The cwd for launched processes. |
void Gio::SubprocessLauncher::set_environ | ( | const std::vector< std::string > & | env | ) |
Replace the entire environment of processes launched from this launcher with the given 'environ' variable.
Typically you will build this variable by using Glib::listenv() to copy the process 'environ' and using the functions Glib::environ_setenv(), Glib::environ_unsetenv(), etc.
As an alternative, you can use g_subprocess_launcher_setenv(), g_subprocess_launcher_unsetenv(), etc.
Pass an empty array to set an empty environment. Pass nullptr
to inherit the parent process’ environment. As of GLib 2.54, the parent process’ environment will be copied when g_subprocess_launcher_set_environ() is called. Previously, it was copied when the subprocess was executed. This means the copied environment may now be modified (using g_subprocess_launcher_setenv(), etc.) before launching the subprocess.
On UNIX, all strings in this array can be arbitrary byte strings. On Windows, they should be in UTF-8.
env | The replacement environment. |
void Gio::SubprocessLauncher::set_flags | ( | Subprocess::Flags | flags | ) |
Sets the flags on the launcher.
The default flags are Gio::Subprocess::Flags::NONE.
You may not set flags that specify conflicting options for how to handle a particular stdio stream (eg: specifying both Gio::Subprocess::Flags::STDIN_PIPE and Gio::Subprocess::Flags::STDIN_INHERIT).
You may also not set a flag that conflicts with a previous call to a function like g_subprocess_launcher_set_stdin_file_path() or g_subprocess_launcher_take_stdout_fd().
flags | SubprocessFlags. |
void Gio::SubprocessLauncher::set_stderr_file_path | ( | const std::string & | path | ) |
Sets the file path to use as the stderr for spawned processes.
If path is nullptr
then any previously given path is unset.
The file will be created or truncated when the process is spawned, as would be the case if using '2>' at the shell.
If you want to send both stdout and stderr to the same file then use Gio::Subprocess::Flags::STDERR_MERGE.
You may not set a stderr file path if a stderr fd is already set or if the launcher flags contain any flags directing stderr elsewhere.
This feature is only available on UNIX.
path | A filename or nullptr . |
void Gio::SubprocessLauncher::set_stdin_file_path | ( | const std::string & | path | ) |
Sets the file path to use as the stdin for spawned processes.
If path is nullptr
then any previously given path is unset.
The file must exist or spawning the process will fail.
You may not set a stdin file path if a stdin fd is already set or if the launcher flags contain any flags directing stdin elsewhere.
This feature is only available on UNIX.
path | A filename or nullptr . |
void Gio::SubprocessLauncher::set_stdout_file_path | ( | const std::string & | path | ) |
Sets the file path to use as the stdout for spawned processes.
If path is nullptr
then any previously given path is unset.
The file will be created or truncated when the process is spawned, as would be the case if using '>' at the shell.
You may not set a stdout file path if a stdout fd is already set or if the launcher flags contain any flags directing stdout elsewhere.
This feature is only available on UNIX.
path | A filename or nullptr . |
void Gio::SubprocessLauncher::setenv | ( | const std::string & | variable, |
const std::string & | value, | ||
bool | overwrite | ||
) |
Sets the environment variable variable in the environment of processes launched from this launcher.
On UNIX, both the variable's name and value can be arbitrary byte strings, except that the variable's name cannot contain '='. On Windows, they should be in UTF-8.
variable | The environment variable to set, must not contain '='. |
value | The new value for the variable. |
overwrite | Whether to change the variable if it already exists. |
Glib::RefPtr< Subprocess > Gio::SubprocessLauncher::spawn | ( | const std::vector< std::string > & | argv | ) |
Creates a Subprocess given a provided array of arguments.
argv | Command line arguments. |
nullptr
on error (and error will be set).Glib::Error |
Transfer an arbitrary file descriptor from parent process to the child.
This function takes ownership of the source_fd; it will be closed in the parent when self is freed.
By default, all file descriptors from the parent will be closed. This function allows you to create (for example) a custom pipe()
or socketpair()
before launching the process, and choose the target descriptor in the child.
An example use case is GNUPG, which has a command line argument --passphrase-fd
providing a file descriptor number where it expects the passphrase to be written.
source_fd | File descriptor in parent process. |
target_fd | Target descriptor for child process. |
Sets the file descriptor to use as the stderr for spawned processes.
If fd is -1 then any previously given fd is unset.
Note that the default behaviour is to pass stderr through to the stderr of the parent process.
The passed fd belongs to the SubprocessLauncher. It will be automatically closed when the launcher is finalized. The file descriptor will also be closed on the child side when executing the spawned process.
You may not set a stderr fd if a stderr file path is already set or if the launcher flags contain any flags directing stderr elsewhere.
This feature is only available on UNIX.
fd | A file descriptor, or -1. |
Sets the file descriptor to use as the stdin for spawned processes.
If fd is -1 then any previously given fd is unset.
Note that if your intention is to have the stdin of the calling process inherited by the child then Gio::Subprocess::Flags::STDIN_INHERIT is a better way to go about doing that.
The passed fd is noted but will not be touched in the current process. It is therefore necessary that it be kept open by the caller until the subprocess is spawned. The file descriptor will also not be explicitly closed on the child side, so it must be marked O_CLOEXEC if that's what you want.
You may not set a stdin fd if a stdin file path is already set or if the launcher flags contain any flags directing stdin elsewhere.
This feature is only available on UNIX.
fd | A file descriptor, or -1. |
Sets the file descriptor to use as the stdout for spawned processes.
If fd is -1 then any previously given fd is unset.
Note that the default behaviour is to pass stdout through to the stdout of the parent process.
The passed fd is noted but will not be touched in the current process. It is therefore necessary that it be kept open by the caller until the subprocess is spawned. The file descriptor will also not be explicitly closed on the child side, so it must be marked O_CLOEXEC if that's what you want.
You may not set a stdout fd if a stdout file path is already set or if the launcher flags contain any flags directing stdout elsewhere.
This feature is only available on UNIX.
fd | A file descriptor, or -1. |
void Gio::SubprocessLauncher::unsetenv | ( | const std::string & | variable | ) |
Removes the environment variable variable from the environment of processes launched from this launcher.
On UNIX, the variable's name can be an arbitrary byte string not containing '='. On Windows, it should be in UTF-8.
variable | The environment variable to unset, must not contain '='. |
|
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. |