This header provides the args property. It also provides the alternative name argv . namespace boost { namespace process { unspecified args; unspecified argv; } } unspecifiedThe args property allows to explicitly set arguments for the execution. The name of the executable will always be the first element in the arg-vector. Details Operations Setting values To set a the argument vector the following syntax can be used. args = value; args(value); std::initializer_list is among the allowed types, so the following syntax is also possible. args = {value1, value2}; args({value1, value2}); Below the possible types for value are listed, with char_type being either char or wchar_t. value std::basic_string<char_type> const char_type * std::initializer_list<const char_type *> std::vector<std::basic_string<char_type>> Additionally any range of std::basic_string<char_type> can be passed. Appending values To append a the argument vector the following syntax can be used. args += value; std::initializer_list is among the allowed types, so the following syntax is also possible. args += {value1, value2}; Below the possible types for value are listed, with char_type being either char or wchar_t. value std::basic_string<char_type> const char_type * std::initializer_list<const char_type *> std::vector<std::basic_string<char_type>> Additionally any range of std::basic_string<char_type> can be passed. Example The overload form is used when more than one string is passed, from the second one forward. I.e. the following expressions have the same results: spawn("gcc", "--version"); spawn("gcc", args ="--version"); spawn("gcc", args+="--version"); spawn("gcc", args ={"--version"}); spawn("gcc", args+={"--version"}); A string will be parsed and set in quotes if it has none and contains spaces. unspecifiedAlias for args .
The header which provides the basic asynchrounous features. It provides the on_exit property, which allows callbacks when the process exits. It also implements the necessary traits for passing an boost::asio::io_context, which is needed for asynchronous communication.It also pulls the boost::asio::buffer into the boost::process namespace for convenience. namespace boost { namespace process { unspecified buffer; unspecified on_exit; } } unspecifiedWhen an io_context is passed, the on_exit property can be used, to be notified when the child process exits.The following syntax is validon_exit=function; on_exit(function); with function being a callable object with the signature (int, const std::error_code&) or an std::future<int>.Example io_context ios; child c("ls", on_exit=[](int exit, const std::error_code& ec_in){}); std::future<int> exit_code; chlid c2("ls", on_exit=exit_code); The handler is not invoked when the launch fails. When used ignore_error it might get invoked on error. on_exit uses boost::asio::signal_set to listen for SIGCHLD on posix, and so has the same restrictions as that class (do not register a handler for SIGCHLD except by using boost::asio::signal_set).
Class implementing and asnychronous I/O-Object for use with boost.asio. It is based on the corresponding I/O Object, that is either boost::asio::windows::stream_handle or boost::asio::posix::stream_descriptor.It can be used directly with boost::asio::async_read or async_write.The object is copyable, but that does invoke a handle duplicate. Typedef for the native handle representation. This is the handle on the system, not the boost.asio class. platform_specific Typedef for the handle representation of boost.asio. platform_specific basic_pipe< CharT, Traits >Explicit cast to basic_pipe. voidCancel the current asynchronous operations. voidClose the pipe handles. voidstd::error_code &Close the pipe handles. While passing an error_code boolCheck if the pipes are open. voidAsync close, i.e. close after current operation is completed.There is no guarantee that this will indeed read the entire pipe-buffer std::size_tconst MutableBufferSequence &Read some data from the handle.See the boost.asio documentation for more details. std::size_tconst MutableBufferSequence &Write some data to the handle.See the boost.asio documentation for more details. native_handleGet the native handle of the source. native_handleGet the native handle of the sink. unspecifiedconst MutableBufferSequence &ReadHandler &&Start an asynchronous read.See the boost.asio documentation for more details. unspecifiedconst ConstBufferSequence &WriteHandler &&Start an asynchronous write.See the boost.asio documentation for more details. const handle_type &Get the asio handle of the pipe sink. const handle_type &Get the asio handle of the pipe source. handle_type &&Get the asio handle of the pipe sink. Qualified as rvalue. handle_type &&Get the asio handle of the pipe source. Qualified as rvalue. handle_type::boost::asio::io_context &Move the source out of this class and change the io_context. Qualified as rvalue. Will always move. handle_type::boost::asio::io_context &Move the sink out of this class and change the io_context. Qualified as rvalue. Will always move handle_type::boost::asio::io_context &Copy the source out of this class and change the io_context. Will always copy. handle_type::boost::asio::io_context &Copy the sink out of this class and change the io_context. Will always copy boost::asio::io_context &Construct a new async_pipe, does automatically open the pipe. Initializes source and sink with the same io_context. Windows creates a named pipe here, where the name is automatically generated. boost::asio::io_context &boost::asio::io_context &Construct a new async_pipe, does automatically open the pipe. Windows creates a named pipe here, where the name is automatically generated. boost::asio::io_context &const std::string &Construct a new async_pipe, does automatically open. Initializes source and sink with the same io_context.Windows restricts possible names. boost::asio::io_context &boost::asio::io_context &const std::string &Construct a new async_pipe, does automatically open.Windows restricts possible names. const async_pipe &Copy-Constructor of the async pipe. Windows requires a named pipe for this, if a the wrong type is used an exception is thrown. async_pipe &&Move-Constructor of the async pipe. boost::asio::io_context &const basic_pipe< CharT, Traits > &Construct the async-pipe from a pipe. Windows requires a named pipe for this, if a the wrong type is used an exception is thrown. boost::asio::io_context &boost::asio::io_context &const basic_pipe< CharT, Traits > &Construct the async-pipe from a pipe, with two different io_context objects. Windows requires a named pipe for this, if a the wrong type is used an exception is thrown. async_pipe &const basic_pipe< CharT, Traits > &Assign a basic_pipe. Windows requires a named pipe for this, if a the wrong type is used an exception is thrown. async_pipe &const async_pipe &Copy Assign a pipe. Duplicates the handles. async_pipe &async_pipe &&Move assign a pipe Destructor. Closes the pipe handles.
Defines the asynchrounous version of the system function. unspecifiedboost::asio::io_context &A reference to an io_context ExitHandler &&The exit-handler for the signature void(boost::system::error_code, int)Args &&...This function provides an asynchronous interface to process launching.It uses the same properties and parameters as the other launching function, but is similar to the asynchronous functions in boost.asioIt uses asio::async_result to determine the return value (from the second parameter, exit_handler). This function does not allow custom error handling, since those are done through the exit_handler.
Defines a child process class. The main class to hold a child process. It is simliar to std::thread, in that it has a join and detach function.The destructor will call terminate on the process if not joined or detached without any warning. voidDetach the child, i.e. let it run after this handle dies. voidJoin the child. This just calls wait, but that way the naming is similar to std::thread boolCheck if the child is joinable. native_handle_tGet the native handle for the child process. intGet the exit_code. The return value is without any meaning if the child wasn't waited for or if it was terminated. pid_tGet the Process Identifier. intGet the native, uninterpreted exit code. The return value is without any meaning if the child wasn't waited for or if it was terminated. boolCheck if the child process is running. boolstd::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. voidWait for the child process to exit. voidstd::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. boolconst std::chrono::duration< Rep, Period > &Wait for the child process to exit for a period of time. True if child exited while waiting. boolconst std::chrono::duration< Rep, Period > &std::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. boolconst std::chrono::time_point< Clock, Duration > &Wait for the child process to exit until a point in time. True if child exited while waiting. boolconst std::chrono::time_point< Clock, Duration > &std::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. boolCheck if this handle holds a child process. That does not mean, that the process is still running. It only means, that the handle does or did exist. boolSame as valid, for convenience. boolCheck if the the chlid process is in any process group. boolstd::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. voidTerminate the child process.This function will cause the child process to unconditionally and immediately exit. It is implement with SIGKILL on posix and TerminateProcess on windows. voidstd::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. pid_t &Construct the child from a pid.There is no guarantee that this will work. The process need the right access rights, which are very platform specific. child &&Move-Constructor. Args &&...Construct a child from a property list and launch it The standard version is to create a subprocess, which will spawn the process. Construct an empty child. child &child &&Move assign. Destructor. Will call terminate (without warning) when the child was neither joined nor detached. Typedef for the type of an pid_t. unspecified
This header provides the cmd property. namespace boost { namespace process { unspecified cmd; } } unspecifiedThe cmd property allows to explicitly set commands for the execution.The overload form applies when only one string is passed to a launching function. The string will be internally parsed and split at spaces.The following expressions are valid, with value being either a C-String or a std::basic_string with char or wchar_t.cmd="value"; cmd(value); The property can only be used for assignments.
This header which provides the env property. It allows the modification of the environment the child process will run in, in a functional style. namespace boost { namespace process { unspecified env; } } For additional information see the platform documentations: windows posix unspecifiedThe env property provides a functional way to modify the environment used by the child process. If none is passed the environment is inherited from the father process. Appending means that the environment will be interpreted as a ';' or ':' separated list as used in PATH.On both posix and windows the environment variables can be lists of strings, separated by ';'. This is typically used for the PATH variable.By default the environment will be inherited from the launching process, which is also true if environment are modified with this initializer. Details Operations Setting variables To set a variable id the value value the following syntax can be used. env[id] = value; env(id, value); std::initializer_list is among the allowed types, so the following syntax is also possible. env[id] = {value1, value2}; env(id, {value1, value2}); Creates the variable if it does not exist. The following lists contain possible value types, with char_type being either char or wchar_t for both id and value. id std::basic_string<char_type> const char_type * value std::basic_string<char_type> const char_type * std::initializer_list<const char_type *> std::vector<std::basic_string<char_type>> Using std::vector or std::initializer_list Append variables Appending means, that a variable will be interpreted as a To append a variable id the value value the following syntax can be used: env[id] += value; std::initializer_list is among the allowed types, so the following syntax is also possible. env[id] += {value1, value2}; Creates the variable if it does not exist. The following lists contain possible value types, with char_type being either char or wchar_t for both id and value. id std::basic_string<char_type> const char_type * value std::basic_string<char_type> const char_type * std::initializer_list<const char_type *> std::vector<std::basic_string<char_type>> Reset variables Reseting signle variables can be done in the following way: env[id] = boost::none; env(id, boost::none); This does not set the value empty, but removes it from the list. The following lists contain possible value types, with char_type being either char or wchar_t: id std::basic_string<char_type> const char_type * Initialize the environment The whole environment can be initialized from an object of type boost::process::environment env=env; env(env); The passed environment can also be default-constructed to get an empty environment. id std::basic_string<char_type> const char_type * value boost::process::basic_environment<char_type> Example spawn("b2", env["PATH"]+="F:/boost", env["SOME_VAR"]=boost::none, env["NEW_VAR"]="VALUE"); If the overload style should be done by passing an instance of boost::process::environment the above example would look like this. environment e = this_process::environment(); e["PATH"] += "F:/boost"; e.erase("SOME_VAR"); e["NEW_VAR"] = "VALUE"; spawn("b2", e); Passing an empty environment will cause undefined behaviour.
Type definition to hold a seperate environment. Template representation of environments. It takes a character type (char or wchar_t) as template parameter to implement the environment Proxy class used for read access to members by [] or .at() Holds a reference to the environment it was created from. Char const value_type * std::basic_string< value_type > boost::iterator_range< pointer > Environment std::vector< string_type >Split the entry by ";" or ":" and return it as a vector. Used by PATH. Get the value as string. Get the name of this entry. const const_entry &Copy Constructor. boolCheck if the entry is empty. const_entry &const const_entry &Move Constructor. Proxy class used for read and write access to members by [] or .at() Holds a reference to the environment it was created from. Char const value_type * std::basic_string< value_type > boost::iterator_range< pointer > Environment std::vector< string_type >Split the entry by ";" or ":" and return it as a vector. Used by PATH. Get the value as string. Get the name of this entry. const entry &Copy Constructor. boolCheck if the entry is empty. voidconst string_type &Assign a string to the value. voidconst std::vector< string_type > &Assign a set of strings to the entry; they will be separated by ';' or ':'. voidconst string_type &Append a string to the end of the entry, it will separated by ';' or ':'. voidReset the value. entry &const string_type &Append a string to the end of the entry, it will separated by ';' or ':'. entry &const entry &Move Constructor. entry &const string_type &Assign a string to the entry. entry &const std::vector< string_type > &Assign a set of strings to the entry; they will be separated by ';' or ':'. std::basic_string< Char > boost::transform_iterator< entry_maker, Char ** > boost::transform_iterator< const_entry_maker, Char ** > std::size_t unspecified unspecified iteratorReturns an iterator to the beginning. const_iteratorReturns an iterator to the beginning. const_iteratorReturns an iterator to the beginning. iteratorReturns an iterator to the end. const_iteratorReturns an iterator to the end. const_iteratorReturns an iterator to the end. iteratorconst string_type &Find a variable by its name. const_iteratorconst string_type &Find a variable by its name. std::size_tconst string_type &Number of variables. voidconst string_type &Erase variable by id. std::pair< iterator, bool >const string_type &const string_type &Emplace an environment variable. boolCheck if environment has entries. std::size_tGet the number of variables. voidClear the environment. Use with care, passed environment cannot be empty. entry_typeconst string_type &Get the entry with the key. Throws if it does not exist. const_entry_typeconst string_type &Get the entry with the key. Throws if it does not exist. entry_typeconst string_type &Get the entry with the given key. It creates the entry if it doesn't exist. Default constructor. const basic_environment &Copy constructor. basic_environment &&Move constructor. basic_environment &const basic_environment &Copy assignment. basic_environment &basic_environment &&Move assignment. Definition of the environment for the current process. Template representation of the environment of this process. It takes a template as template parameter to implement the environment. All instances of this class refer to the same environment, but might not get updated if another one makes changes. Proxy class used for read access to members by [] or .at() Holds a reference to the environment it was created from. Char const value_type * std::basic_string< value_type > boost::iterator_range< pointer > Environment std::vector< string_type >Split the entry by ";" or ":" and return it as a vector. Used by PATH. Get the value as string. Get the name of this entry. const const_entry &Copy Constructor. boolCheck if the entry is empty. const_entry &const const_entry &Move Constructor. Proxy class used for read and write access to members by [] or .at() Holds a reference to the environment it was created from. Char const value_type * std::basic_string< value_type > boost::iterator_range< pointer > Environment std::vector< string_type >Split the entry by ";" or ":" and return it as a vector. Used by PATH. Get the value as string. Get the name of this entry. const entry &Copy Constructor. boolCheck if the entry is empty. voidconst string_type &Assign a string to the value. voidconst std::vector< string_type > &Assign a set of strings to the entry; they will be separated by ';' or ':'. voidconst string_type &Append a string to the end of the entry, it will separated by ';' or ':'. voidReset the value. entry &const string_type &Append a string to the end of the entry, it will separated by ';' or ':'. entry &const entry &Move Constructor. entry &const string_type &Assign a string to the entry. entry &const std::vector< string_type > &Assign a set of strings to the entry; they will be separated by ';' or ':'. std::basic_string< Char > boost::transform_iterator< entry_maker, Char ** > boost::transform_iterator< const_entry_maker, Char ** > std::size_t unspecified unspecified iteratorReturns an iterator to the beginning. const_iteratorReturns an iterator to the beginning. const_iteratorReturns an iterator to the beginning. iteratorReturns an iterator to the end. const_iteratorReturns an iterator to the end. const_iteratorReturns an iterator to the end. iteratorconst string_type &Find a variable by its name. const_iteratorconst string_type &Find a variable by its name. std::size_tconst string_type &Number of variables. voidconst string_type &Erase variable by id. std::pair< iterator, bool >const string_type &const string_type &Emplace an environment variable. boolCheck if environment has entries. std::size_tGet the number of variables. entry_typeconst string_type &Get the entry with the key. Throws if it does not exist. const_entry_typeconst string_type &Get the entry with the key. Throws if it does not exist. entry_typeconst string_type &Get the entry with the given key. It creates the entry if it doesn't exist. Default constructor. basic_native_environment &&Move constructor. basic_native_environment &basic_native_environment &&Move assignment. Definition of the environment for the current process. basic_native_environment< char > Definition of the environment for the current process. basic_native_environment< wchar_t > Type definition to hold a seperate environment. basic_environment< char > Type definition to hold a seperate environment. basic_environment< wchar_t > intGet the process id of the current process. native_handle_typeGet the native handle of the current process. native_environmentGet the enviroment of the current process. wnative_environmentGet the enviroment of the current process. std::vector< boost::filesystem::path >Get the path environment variable of the current process runs.
Header which provides the error properties. It allows to explicitly set the error handling, the properties are: namespace boost { namespace process { unspecified ignore_error; unspecified throw_on_error; unspecified error; unspecified error_ref; unspecified error_code; } } For error there are two aliases: error_ref and error_code unspecifiedThe ignore_error property will disable any error handling. This can be useful on linux, where error handling will require a pipe. unspecifiedThe throw_on_error property will enable the exception when launching a process. It is unnecessary by default, but may be used, when an additional error_code is provided. unspecifiedThe error property will set the executor to handle any errors by setting an std::error_code.std::error_code ec; system("gcc", error(ec)); The following syntax is valid:error(ec); error=ec; The overload version is achieved by just passing an object of std::error_code to the function. unspecifiedAlias for error . unspecifiedAlias for error .
system_errorThe exception usually thrown by boost.process. It merely inherits std::system_error but can then be distinguished in the catch-block from other system errors.
Header which provides the exe property. namespace boost { namespace process { unspecified exe; } } unspecifiedThe exe property allows to explicitly set the executable.The overload form applies when to the first, when several strings are passed to a launching function.The following expressions are valid, with value being either a C-String or a std::basic_string with char or wchar_t or a boost::filesystem::path.exe="value"; exe(value); The property can only be used for assignments.
This header which provides the types and functions provided for custom extensions. Please refer to the tutorial for more details. boost::process::extend::handlerboost::process::extend::require_io_contextInheriting this class will tell the launching function, that an event handler shall be invoked when the process exits. This automatically does also inherit require_io_context.You must add the following function to your implementation:template<typename Executor> std::function<void(int, const std::error_code&)> on_exit_handler(Executor & exec) { auto handler_ = this->handler; return [handler_](int exit_code, const std::error_code & ec) { handler_(static_cast<int>(exit_code), ec); }; } The callback will be obtained by calling this function on setup and it will be invoked when the process exits.Cannot be used with boost::process::spawn This class is the base for every initializer, to be used for extensions.The usage is done through compile-time polymorphism, so that the required functions can be overloaded.None of the function need to be const. voidExecutor &This function is invoked before the process launch. It is not required to be const. voidExecutor &const std::error_code &This function is invoked if an error occured while trying to launch the process. It is not required to be const. voidExecutor &This function is invoked if the process was successfully launched. It is not required to be const. voidExecutor &const std::error_code &This function is invoked if an error occured during the call of fork. This function will only be called on posix. voidExecutor &This function is invoked if the call of fork was successful, before calling execve. This function will only be called on posix. It will be invoked from the new process. voidExecutor &const std::error_code &This function is invoked if the call of execve failed. This function will only be called on posix. It will be invoked from the new process. The posix executor type. This type represents the posix executor and can be used for overloading in a custom handler. It is an alias for the implementation on posix, and a forward-declaration on windows. As information for extension development, here is the structure of the process launching (in pseudo-code and uml) for (auto & s : seq) s.on_setup(*this); if (error()) { for (auto & s : seq) s.on_error(*this, error()); return child(); } pid = fork() on_setup(*this); if (pid == -1) //fork error { set_error(get_last_error()); for (auto & s : seq) s.on_fork_error(*this, error()); for (auto & s : seq) s.on_error(*this, error()); return child() } else if (pid == 0) //child process { for (auto & s : seq) s.on_exec_setup(*this); execve(exe, cmd_line, env); auto ec = get_last_error(); for (auto & s : seq) s.on_exec_error(*this); unspecified();//here the error is send to the father process interally std::exit(EXIT_FAILURE); return child(); //for C++ compliance } child c(pid, exit_code); unspecified();//here, we read the the error from the child process if (error()) for (auto & s : seq) s.on_error(*this, error()); else for (auto & s : seq) s.on_success(*this); //now we check again, because a on_success handler might've errored. if (error()) { for (auto & s : seq) s.on_error(*this, error()); return child(); } else return c; The sequence if when no error occurs. The sequence if the execution fails. The sequence if the fork fails. Error handling if execve fails is done through a pipe, unless ignore_error is used. Sequence &A reference to the actual initializer-sequence. const char *A pointer to the name of the executable. char *const *A pointer to the argument-vector. char **A pointer to the environment variables, as default it is set to environ pid_tThe pid of the process - it will be -1 before invoking fork, and after forking either 0 for the new process or a positive value if in the current process. */. std::shared_ptr< std::atomic< int > >This shared-pointer holds the exit code. It's done this way, so it can be shared between an asio::io_context and child. const std::error_code &This function returns a const reference to the error state of the executor. voidconst std::error_code &const std::string &This function can be used to report an error to the executor. This will be handled according to the configuration of the executor, i.e. it might throw an exception. This is the required way to handle errors in initializers. voidconst std::error_code &const char *This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Inheriting the class will tell the launching process that an io_context is needed. This should always be used when get_io_context is used. The windows executor type. This type represents the posix executor and can be used for overloading in a custom handler.It is an alias for the implementation on posix, and a forward-declaration on windows. As information for extension development, here is the structure of the process launching (in pseudo-code and uml) for (auto & s : seq) s.on_setup(*this); if (error()) { for (auto & s : seq) s.on_error(*this, error()); return child(); } int err_code = CreateProcess( exe, cmd_line, proc_attrs, thread_attrs, creation_flags, env, work_dir, startup_info, proc_info); child c(proc_info, exit_code); if (error()) for (auto & s : seq) s.on_error(*this, error()); else for (auto & s : seq) s.on_success(*this); //now we check again, because a on_success handler might've errored. if (error()) { for (auto & s : seq) s.on_error(*this, error()); return child(); } else return c; The sequence for windows process creation. The type of the startup-info, depending on the char-type. unspecified The type of the extended startup-info, depending the char-type; only defined with winapi-version equal or higher than 6. unspecified Sequence &A reference to the actual initializer-sequence. const Char *A pointer to the name of the executable. It's null by default. char Char *A pointer to the argument-vector. Must be set by some initializer. char Char *A pointer to the environment variables. It's null by default. const Char *A pointer to the working directory. It's null by default. unspecifiedA pointer to the process-attributes of type SECURITY_ATTRIBUTES. It's null by default. unspecifiedA pointer to the thread-attributes of type SECURITY_ATTRIBUTES. It' null by default. unspecifiedA logical bool value setting whether handles shall be inherited or not. unspecifiedThe element holding the process-information after process creation. The type is PROCESS_INFORMATION std::shared_ptr< std::atomic< int > >This shared-pointer holds the exit code. It's done this way, so it can be shared between an asio::io_context and child. unspecifiedThe creation flags of the process. startup_info_tThis element is an instance or a reference (if startup_info_ex exists) to the startup-info for the process. startup_info_ex_tThis element is the instance of the extended startup-info. It is only available with a winapi-version equal or highter than 6. const std::error_code &This function returns a const reference to the error state of the executor. voidconst std::error_code &const std::string &This function can be used to report an error to the executor. This will be handled according to the configuration of the executor, i.e. it might throw an exception. This is the required way to handle errors in initializers. voidconst std::error_code &const char *This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. voidThis function switches the information, so that the extended structure is used. It's only defined with winapi-version equal or higher than 6. unspecifiedThis handler is invoked before the process in launched, to setup parameters. The required signature is void(Exec &), where Exec is a template parameter. unspecifiedThis handler is invoked if an error occured. The required signature is void(auto & exec, const std::error_code&), where Exec is a template parameter. unspecifiedThis handler is invoked if launching the process has succeeded. The required signature is void(auto & exec), where Exec is a template parameter. unspecifiedThis handler is invoked if the fork failed. The required signature is void(auto & exec), where Exec is a template parameter. Only available on posix. unspecifiedThis handler is invoked if the fork succeeded. The required signature is void(Exec &), where Exec is a template parameter. Only available on posix. unspecifiedThis handler is invoked if the exec call errored. The required signature is void(auto & exec), where Exec is a template parameter. Only available on posix. std::error_codeHelper function to get the last error code system-independent. voidconst std::string &A message to add to the error code. voidHelper function to get and throw the last system error. boost::process::process_error asio::io_context &const Sequence &The Sequence of the initializer. This function gets the io_context from the initializer sequence.Yields a compile-time error if no io_context is provided.
Defines a group process class. For additional information see the platform specific implementations: windows - job object posix - process group Represents a process group.Groups are movable but non-copyable. The destructor automatically closes handles to the group process.The group will have the same interface as std::thread.If the destructor is called without a previous detach or wait, the group will be terminated. If a default-constructed group is used before being used in a process launch, the behaviour is undefined. Waiting for groups is currently broken on windows and will most likely result in a dead-lock. unspecified Native representation of the handle. group_handle::handle_t voidDetach the group. voidJoin the child. This just calls wait, but that way the naming is similar to std::thread boolCheck if the child is joinable. native_handle_tObtain the native handle of the group. voidWait for the process group to exit. voidstd::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. boolconst std::chrono::duration< Rep, Period > &Wait for the process group to exit for period of time. True if all child processes exited while waiting. boolconst std::chrono::duration< Rep, Period > &std::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. boolconst std::chrono::time_point< Clock, Duration > &Wait for the process group to exit until a point in time. True if all child processes exited while waiting. boolconst std::chrono::time_point< Clock, Duration > &std::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. boolCheck if the group has a valid handle. boolConvenience to call valid. voidTerminate the process group, i.e. all processes in the group. voidstd::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. voidconst child &Assign a child process to the group. voidconst child &std::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. boolconst child &Check if the child process is in the group. boolconst child &std::error_code &This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. group_handle && native_handle_t &Construct the group from a native_handle. const group & group &&Move constructor. Default constructor. group &const group & group &group &&Move assign. DestructorIf the destructor is called without a previous detach or wait, the group will be terminated.
Defines functions to obtain handles of the current process and limit the amount for inherited ones. unspecifiedThe limit_handles property sets all properties to be inherited only expcitly. It closes all unused file-descriptors on posix after the fork and removes the inherit flags on windows.This is executed after the fork on posix. system("gcc", limit_handles); Since limit also closes the standard handles unless they are explicitly redirected they can be ignored by limit_handles in the following way.system("gcc", limit_handles.allowStd()) The native type for handles. Definition of the native handle type. unspecified std::vector< native_handle_type >std::vector< native_handle_type >std::error_code &Get a snapshot of all handles of the process (i.e. file descriptors on posix and handles on windows) of the current process.This function might not work on certain posix systems. On Windows version older than windows 8 this function will iterate all the system handles, meaning it might be quite slow. This functionality is utterly prone to race conditions, since other threads might open or close handles. The list of all open handles of the current process boolnative_handle_typeboolnative_handle_typestd::error_code &Determines if a given handle is a a stream-handle, i.e. any handle that can be used with read and write functions. Stream handles include pipes, regular files and sockets. Indicates if it's a stream handle.
Header which provides the io properties. It provides the following properties: namespace boost { namespace process { unspecified close; unspecified null; unspecified std_in; unspecified std_out; unspecified std_err; } } File I/O The library allows full redirection of streams to files as shown below.boost::filesystem::path log = "my_log_file.txt"; boost::filesystem::path input = "input.txt"; boost::filesystem::path output = "output.txt"; system("my_prog", std_out>output, std_in<input, std_err>log); Synchronous Pipe I/O Another way is to communicate through pipes.pstream str; child c("my_prog", std_out > str); int i; str >> i; Note that the pipe may also be used between several processes, like this:pipe p; child c1("nm", "a.out", std_out>p); child c2("c++filt", std_in<p); Asynchronous I/O Utilizing boost.asio asynchronous I/O is provided.boost::asio::io_context ios; std::future<std::string> output; system("ls", std_out > output, ios); auto res = fut.get(); boost/process/async.hpp must also be included for this to work. Closing Stream can be closed, so nothing can be read or written.system("foo", std_in.close()); Null Streams can be redirected to null, which means, that written date will be discarded and read data will only contain EOF.system("b2", std_out > null); unspecifiedThis constant is a utility to allow syntax like std_out > close for closing I/O streams. unspecifiedThis constant is a utility to redirect streams to the null-device. unspecifiedThis property allows to set the input stream for the child process. Details File Input The file I/O simple redirects the stream to a file, for which the possible types are boost::filesystem::path std::basic_string<char_type> const char_type* FILE* with char_type being either char or wchar_t. FILE* is explicitly added, so the process can easily redirect the output stream of the child to another output stream of the process. That is: system("ls", std_in < stdin); If the launching and the child process use the input, this leads to undefined behaviour. A syntax like system("ls", std_out > std::cerr) is not possible, due to the C++ implementation not providing access to the handle. The valid expressions for this property are std_in < file; std_in = file; Pipe Input As explained in the corresponding section, the boost.process library provides a async_pipe class which can be used to communicate with child processes. Technically the async_pipe works synchronous here, since no asio implementation is used by the library here. The async-operation will then however not end if the process is finished, since the pipe remains open. You can use the async_close function with on_exit to fix that. Valid expressions with pipes are these: std_in < pipe; std_in = pipe; Where the valid types for pipe are the following: basic_pipe async_pipe basic_opstream basic_pstream Note that the pipe may also be used between several processes, like this: pipe p; child c1("nm", "a.out", std_out>p); child c2("c++filt", std_in<p); Asynchronous Pipe Input Asynchronous Pipe I/O classifies communication which has automatically handling of the asynchronous operations by the process library. This means, that a pipe will be constructed, the async_read/-write will be automatically started, and that the end of the child process will also close the pipe. Valid types for pipe I/O are the following: boost::asio::const_buffer Constructed with boost::asio::buffer boost::asio::mutable_buffer Constructed with boost::asio::buffer boost::asio::streambuf Valid expressions with pipes are these: std_in < buffer; std_in = buffer; std_out > buffer; std_out = buffer; std_err > buffer; std_err = buffer; (std_out & std_err) > buffer; (std_out & std_err) = buffer; It is also possible to get a future for std_in, by chaining another std::future<void> onto it, so you can wait for the input to be completed. It looks like this: std::future<void> fut; boost::asio::io_context ios; std::string data; child c("prog", std_in < buffer(data) > fut, ios); fut.get(); boost::asio::buffer is also available in the boost::process namespace. This feature requires boost/process/async.hpp to be included and a reference to boost::asio::io_context to be passed to the launching function. Close The input stream can be closed, so it cannot be read from. This will lead to an error when attempted. This can be achieved by the following syntax. std_in < close; std_in = close; std_in.close(); Null The input stream can be redirected to read from the null-device, which means that only EOF is read. The syntax to achieve that has the following variants: std_in < null; std_in = null; std_in.null(); unspecifiedThis property allows to set the output stream for the child process.The Semantic is the same as for std_err std_err and std_out can be combined into one stream, with the operator &, i.e. std_out & std_err. Details File Input The file I/O simple redirects the stream to a file, for which the possible types are boost::filesystem::path std::basic_string<char_type> const char_type* FILE* with char_type being either char or wchar_t. FILE* is explicitly added, so the process can easily redirect the output stream of the child to another output stream of the process. That is: system("ls", std_out < stdin); If the launching and the child process use the input, this leads to undefined behaviour. A syntax like system("ls", std_out > std::cerr) is not possible, due to the C++ implementation not providing access to the handle. The valid expressions for this property are std_out < file; std_out = file; Pipe Output As explained in the corresponding section, the boost.process library provides a async_pipe class which can be used to communicate with child processes. Technically the async_pipe works like a synchronous pipe here, since no asio implementation is used by the library here. The asynchronous operation will then however not end if the process is finished, since the pipe remains open. You can use the async_close function with on_exit to fix that. Valid expressions with pipes are these: std_out > pipe; std_out = pipe; Where the valid types for pipe are the following: basic_pipe async_pipe basic_ipstream basic_pstream Note that the pipe may also be used between several processes, like this: pipe p; child c1("nm", "a.out", std_out>p); child c2("c++filt", std_in<p); Asynchronous Pipe Output Asynchronous Pipe I/O classifies communication which has automatically handling of the async operations by the process library. This means, that a pipe will be constructed, the async_read/-write will be automatically started, and that the end of the child process will also close the pipe. Valid types for pipe I/O are the following: boost::asio::mutable_buffer Constructed with boost::asio::buffer boost::asio::streambuf std::future<std::vector<char>> std::future<std::string> Valid expressions with pipes are these: std_out > buffer; std_out = buffer; std_err > buffer; std_err = buffer; (std_out & std_err) > buffer; (std_out & std_err) = buffer; boost::asio::buffer is also available in the boost::process namespace. This feature requires boost/process/async.hpp to be included and a reference to boost::asio::io_context to be passed to the launching function. Close The out stream can be closed, so it cannot be write from. This will lead to an error when attempted. This can be achieved by the following syntax. std_out > close; std_out = close; std_out.close(); Null The output stream can be redirected to write to the null-device, which means that all output is discarded. The syntax to achieve that has the following variants: std_out > null; std_out = null; std_out.null(); unspecifiedThis property allows setting the stderr stream. The semantic and syntax is the same as for std_out .
The internally used type for code conversion. std::codecvt< wchar_t, char, std::mbstate_t > const std::error_category &Internally used error cateory for code conversion. const codecvt_type &Get a reference to the currently used code converter. std::localeconst std::locale &Set the locale of the library.
std::basic_istream< CharT, Traits >Implementation of a reading pipe stream. basic_pipe< CharT, Traits > CharT Traits Traits::int_type Traits::pos_type Traits::off_type basic_pipebuf< CharT, Traits > *Get access to the underlying stream_buf. voidpipe_type &&Set the pipe of the streambuf. voidconst pipe_type &Set the pipe of the streambuf. pipe_type &Get a reference to the pipe. const pipe_type &Get a const reference to the pipe. pipe_type &&Get a rvalue reference to the pipe. Qualified as rvalue. boolCheck if the pipe is open. voidOpen a new pipe. voidconst std::string &Open a new named pipe. voidFlush the buffer & close the pipe. Default constructor. const basic_ipstream &Copy constructor. basic_ipstream &&Move constructor. pipe_type &&Move construct from a pipe. const pipe_type &Copy construct from a pipe. basic_ipstream &const basic_ipstream &Copy assignment. basic_ipstream &basic_ipstream &&Move assignment. basic_ipstream &pipe_type &&Move assignment of a pipe. basic_ipstream &const pipe_type &Copy assignment of a pipe. std::basic_ostream< CharT, Traits >Implementation of a write pipe stream. basic_pipe< CharT, Traits > CharT Traits Traits::int_type Traits::pos_type Traits::off_type basic_pipebuf< CharT, Traits > *Get access to the underlying stream_buf. voidpipe_type &&Set the pipe of the streambuf. voidconst pipe_type &Set the pipe of the streambuf. pipe_type &Get a reference to the pipe. const pipe_type &Get a const reference to the pipe. pipe_type &&Get a rvalue reference to the pipe. Qualified as rvalue. voidOpen a new pipe. voidconst std::string &Open a new named pipe. voidFlush the buffer & close the pipe. Default constructor. const basic_opstream &Copy constructor. basic_opstream &&Move constructor. pipe_type &&Move construct from a pipe. const pipe_type &Copy construct from a pipe. basic_opstream &const basic_opstream &Copy assignment. basic_opstream &basic_opstream &&Move assignment. basic_opstream &pipe_type &&Move assignment of a pipe. basic_opstream &const pipe_type &Copy assignment of a pipe. Class implementation of a pipe. CharT Traits Traits::int_type Traits::pos_type Traits::off_type unspecified native_handleGet the native handle of the source. native_handleGet the native handle of the sink. voidnative_handleAssign a new value to the source voidnative_handleAssign a new value to the sink int_typeconst char_type *int_typeWrite data to the pipe. int_typechar_type *int_typeRead data from the pipe. boolCheck if the pipe is open. voidClose the pipe. Default construct the pipe. Will be opened. const std::string &Construct a named pipe. const basic_pipe &Copy construct the pipe. Duplicated the handles. basic_pipe &&Move construct the pipe. basic_pipe &const basic_pipe &Copy assign the pipe. Duplicated the handles. basic_pipe &basic_pipe &&Move assign the pipe. Destructor closes the handles. std::basic_streambuf< CharT, Traits >Implementation of the stream buffer for a pipe. basic_pipe< CharT, Traits > CharT Traits Traits::int_type Traits::pos_type Traits::off_type constexpr int int_typeint_typetraits_type::eof()Writes characters to the associated output sequence from the put area. intSynchronizes the buffers with the associated character sequence. int_typeReads characters from the associated input sequence to the get area. voidpipe_type &&Set the pipe of the streambuf. voidconst pipe_type &Set the pipe of the streambuf. pipe_type &Get a reference to the pipe. const pipe_type &Get a const reference to the pipe. pipe_type &&Get a rvalue reference to the pipe. Qualified as rvalue. boolCheck if the pipe is open. basic_pipebuf< CharT, Traits > *Open a new pipe. basic_pipebuf< CharT, Traits > *const std::string &Open a new named pipe. basic_pipebuf< CharT, Traits > *Flush the buffer & close the pipe. Default constructor, will also construct the pipe. const basic_pipebuf &Copy Constructor. basic_pipebuf &&Move Constructor. Destructor -> writes the frest of the data. pipe_type &&Move construct from a pipe. const pipe_type &Construct from a pipe. basic_pipebuf &const basic_pipebuf &Copy assign. basic_pipebuf &basic_pipebuf &&Move assign. basic_pipebuf &pipe_type &&Move assign a pipe. basic_pipebuf &const pipe_type &Copy assign a pipe. bool std::basic_iostream< CharT, Traits >Implementation of a read-write pipe stream. basic_pipe< CharT, Traits > CharT Traits Traits::int_type Traits::pos_type Traits::off_type basic_pipebuf< CharT, Traits > *Get access to the underlying stream_buf. voidpipe_type &&Set the pipe of the streambuf. voidconst pipe_type &Set the pipe of the streambuf. pipe_type &Get a reference to the pipe. const pipe_type &Get a const reference to the pipe. pipe_type &&Get a rvalue reference to the pipe. Qualified as rvalue. voidOpen a new pipe. voidconst std::string &Open a new named pipe. voidFlush the buffer & close the pipe. Default constructor. const basic_pstream &Copy constructor. basic_pstream &&Move constructor. pipe_type &&Move construct from a pipe. const pipe_type &Copy construct from a pipe. basic_pstream &const basic_pstream &Copy assignment. basic_pstream &basic_pstream &&Move assignment. basic_pstream &pipe_type &&Move assignment of a pipe. basic_pstream &const pipe_type &Copy assignment of a pipe. basic_pipe< char > basic_pipe< wchar_t > basic_pipebuf< char > basic_pipebuf< wchar_t > basic_ipstream< char > basic_ipstream< wchar_t > basic_opstream< char > basic_opstream< wchar_t > basic_pstream< char > basic_pstream< wchar_t >
Header which provides the posix extensions. namespace boost { namespace process { namespace posix { unspecified fd; unspecified sig; unspecified use_vfork; } } } Only available on posix. See the documentation of fork, execve and vfork. unspecifiedThis property lets you modify file-descriptors other than the standard ones (0,1,2).It provides the functions bind, which implements dup2 and close.Close can also be called with a range of file-descriptors to be closed. unspecifiedThis property lets you modify the handling of SIGCHLD for this call. It will be reset afterwards.It can be set to default, by the expression sig.dfl(), set to ignore with sig.ign() or assigned a custom handler. A custom handler must have the type sighandler_tand can be assigned with the following syntax:sig = handler; sig(handler); spawn will automatically use sig.ign(), which will override if you pass a custom handler. unspecifiedThis property will replace the usage of fork by vfork. vfork is no longer an official part of the posix standard.
Defines a function to search for an executable in path. boost::filesystem::pathconst boost::filesystem::path &The base of the filename to findconst std::vector< boost::filesystem::path >::boost::this_process::path()the set of paths to search, defaults to the "PATH" environment variable.Searches for an executable in path.filename must be a basename including the file extension. It must not include any directory separators (like a slash). On Windows the file extension may be omitted. The function will then try the various file extensions for executables on Windows to find filename. the absolute path to the executable filename or an empty string if filename isn't found
Header which provides the shell property. This provides the property to launch a process through the system shell. It also allows the user to obtain the shell-path via shell(). namespace boost { namespace process { unspecified shell; } } unspecifiedThe shell property enables to launch a program through the shell of the system.system("gcc", shell); The shell argument goes without any expression. The operator() is overloaded, to obtain the path of the system shell.auto shell_cmd = shell(); //avoid exceptions std::error_code ec; shell_cmd = shell(ec); Launching through the shell will NOT provide proper error handling, i.e. you will get an error via the return code. Executing shell commands that incorporate unsanitized input from an untrusted source makes a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution. For this reason, the use of shell is strongly discouraged in cases where the command string is constructed from external input:
Defines the spawn function. voidArgs &&...Launch a process and detach it. Returns no handle.This function starts a process and immediately detaches it. It thereby prevents the system from creating a zombie process, but will also cause the system to be unable to wait for the child to exit.This will set SIGCHLD to SIGIGN on posix. This function does not allow asynchronous operations, since it cannot wait for the end of the process. It will fail to compile if a reference to boost::asio::io_context is passed.
Header which provides the start_dir property, which allows to set the directory the process shall be started in. namespace boost { namespace process { unspecified start_dir; } } unspecifiedTo set the start dir, the start_dir property is provided.The valid operations are the following:start_dir=path start_dir(path) It can be used with std::string, std::wstring and boost::filesystem::path.
Defines a system function. intArgs &&...Launches a process and waits for its exit. It works as std::system, though it allows all the properties boost.process provides. It will execute the process and wait for it's exit; then return the exit_code.int ret = system("ls"); Using this function with synchronous pipes leads to many potential deadlocks. When using this function with an asynchronous properties and NOT passing an io_context object, the system function will create one and run it. When the io_context is passed to the function, the system function will check if it is active, and call the io_context::run function if not.
Header which provides the windows extensions. namespace boost { namespace process { namespace windows { unspecified hide; unspecified maximized; unspecified minimized; unspecified minimized_not_active; unspecified not_active; unspecified show; unspecified show_normal; unspecified create_no_window; } } } Only available on windows. See the parameter documentation of ShowWindow for more details. unspecifiedHides the window and activates another window. unspecifiedActivates the window and displays it as a maximized window. unspecifiedActivates the window and displays it as a minimized window. unspecifiedDisplays the window as a minimized window. This value is similar to minimized, except the window is not activated. unspecifiedDisplays a window in its most recent size and position. This value is similar to show_normal`, except that the window is not activated. unspecifiedActivates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. unspecifiedActivates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. unspecifiedAdds the CREATE_NO_WINDOW flag.