### Define Process Start Directory Source: https://github.com/boostorg/process/blob/develop/doc/reference/start_dir.adoc Initializes the starting directory for a subprocess. Requires a `filesystem::path` object. ```cpp /// Initializer for the starting directory of a subprocess to be launched. struct process_start_dir { filesystem::path start_dir; process_start_dir(filesystem::path start_dir); { } }; ``` -------------------------------- ### Process V1 Constructor Example Source: https://github.com/boostorg/process/blob/develop/doc/version2.adoc Illustrates the DSL-like constructor used in Boost.Process V1 for defining process settings. ```cpp child c{exe="test", args+="--help", std_in < null(), env["FOO"] += "BAR"}; ``` -------------------------------- ### Create Process with Minimized Window Source: https://github.com/boostorg/process/blob/develop/doc/reference/windows/show_window.adoc Example of creating a new process and displaying its window minimized using the `show_window_minimized` flag. ```cpp process p{"C:\\not-a-virus.exe", {}, process::windows::show_window_minimized}; ``` -------------------------------- ### Launch a Process Source: https://github.com/boostorg/process/blob/develop/doc/quickstart.adoc Launches a new process with the specified executor, executable path, and arguments. Initializers can be provided for custom setup. ```cpp boost::process::processخانه proc(ex, "executable", "arg1", "arg2"); ``` -------------------------------- ### Process V2 Constructor Example Source: https://github.com/boostorg/process/blob/develop/doc/version2.adoc Demonstrates the simplified interface of Boost.Process V2, using explicit initializers for process components like environment and I/O. ```cpp extern std::unordered_map my_env; extern asio::io_context ctx; process proc(ctx, "./test", {"--help"}, process_stdio{nullptr, {}, {}}, process_environment(my_env)); ``` -------------------------------- ### process_start_dir Structure Source: https://github.com/boostorg/process/blob/develop/doc/reference/start_dir.adoc Defines the starting directory for a subprocess. ```APIDOC ## `process_start_dir` Structure ### Description Initializes the starting directory of a subprocess to be launched. ### Fields - **start_dir** (filesystem::path) - The path for the starting directory. ### Constructor - **process_start_dir(filesystem::path start_dir)**: Constructor that takes the starting directory path. ``` -------------------------------- ### Execute Process with Shell Parsing Source: https://github.com/boostorg/process/blob/develop/doc/reference/shell.adoc Example of using the shell utility to parse a command line, extract the executable and arguments, perform a security check, and then launch the process. ```cpp asio::io_context ctx; auto cmd = shell("my-app --help"); auto exe = cmd.exe(); check_if_malicious(exe); process proc{ctx, exe, cmd.args()}; ``` -------------------------------- ### Create a Popen Stream Source: https://github.com/boostorg/process/blob/develop/doc/stdio.adoc Utilize the popen class to start a process and connect pipes for stdin and stdout, enabling stream-like interaction with the child process. ```cpp auto pclose_handle = process::popen( io_context, "cat", process::std_out.pipe().close_native_handle()); std::cout << pclose_handle.read(); ``` -------------------------------- ### Process Environment Source: https://github.com/boostorg/process/blob/develop/doc/reference/ext.adoc Get the current environment variables of a process. ```APIDOC ## Get Process Environment ### Description Retrieves the environment variables of a process. ### Method Various (function overloads) ### Endpoint N/A (C++ functions) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Example usage with a process handle // env_view env_vars = process::ext::env(process_handle, ec); // Example usage with a process ID // env_view env_vars = process::ext::env(pid, ec); ``` ### Response #### Success Response (200) - **env_view** (object) - A view of the process's environment variables. #### Response Example ```json { "env": { "PATH": "/usr/local/bin:/usr/bin:/bin", "HOME": "/home/user" } } ``` ### Notes - Overloads are available for `basic_process_handle` and `pid_type`. - Error handling is provided via `error_code` parameter or exceptions. - May fail with "operation_not_supported" on some platforms. ``` -------------------------------- ### Example Usage of check_exit_code Source: https://github.com/boostorg/process/blob/develop/doc/reference/exit_code.adoc Demonstrates how to use the `check_exit_code` helper function within an asynchronous wait operation to handle process exit statuses. ```cpp process proc{co_await this_coro::executor, "exit", {"1"}}; co_await proc.async_wait( asio::deferred( [&proc](error_code ec, int) { return asio::deferred.values( check_exit_code(ec, proc.native_exit_code()) ); } ) ); ``` -------------------------------- ### Process Executable Source: https://github.com/boostorg/process/blob/develop/doc/reference/ext.adoc Get the executable path of a process. ```APIDOC ## Get Process Executable ### Description Retrieves the path to the executable file of a process. ### Method Various (function overloads) ### Endpoint N/A (C++ functions) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Example usage with a process handle // filesystem::path exe_path = process::ext::exe(process_handle, ec); // Example usage with a process ID // filesystem::path exe_path = process::ext::exe(pid, ec); ``` ### Response #### Success Response (200) - **filesystem::path** (string) - The path to the executable file. #### Response Example ```json { "exe": "/usr/bin/my_process" } ``` ### Notes - Overloads are available for `basic_process_handle` and `pid_type`. - Error handling is provided via `error_code` parameter or exceptions. - May fail with "operation_not_supported" on some platforms. - On Windows, overloads taking a `HANDLE` are also available. ``` -------------------------------- ### Start Process in Specific Directory with process_start_dir Source: https://github.com/boostorg/process/blob/develop/doc/start_dir.adoc Use `process_start_dir` to execute a process in a directory other than the current one. Be cautious with relative paths as they may fail on POSIX systems due to the directory change occurring before `execve`. ```cpp #include int main() { boost::process::system("ls", boost::process::start_dir("/home")); } ``` -------------------------------- ### Process Current Working Directory Source: https://github.com/boostorg/process/blob/develop/doc/reference/ext.adoc Get the current working directory of a process. ```APIDOC ## Get Process Current Working Directory ### Description Retrieves the current working directory of a process. ### Method Various (function overloads) ### Endpoint N/A (C++ functions) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Example usage with a process handle // filesystem::path cwd_path = process::ext::cwd(process_handle, ec); // Example usage with a process ID // filesystem::path cwd_path = process::ext::cwd(pid, ec); ``` ### Response #### Success Response (200) - **filesystem::path** (string) - The path to the current working directory. #### Response Example ```json { "cwd": "/home/user/my_app" } ``` ### Notes - Overloads are available for `basic_process_handle` and `pid_type`. - Error handling is provided via `error_code` parameter or exceptions. - May fail with "operation_not_supported" on some platforms. ``` -------------------------------- ### Implicit Pipe Construction for Subprocess Stdin Source: https://github.com/boostorg/process/blob/develop/doc/reference/stdio.adoc This example shows implicit construction of a readable pipe for the subprocess's stdin. The `process_stdio` initializer creates the other side of the pipe and connects them. ```cpp asio::io_context ctx; asio::writable_pipe wp{ctx}; // create a readable pipe internally and connect it to wp process proc{ctx, "/bin/bash", {}, process_stdio{.in=wp}}; ``` -------------------------------- ### Explicit Pipe Construction for Subprocess Stdin Source: https://github.com/boostorg/process/blob/develop/doc/reference/stdio.adoc This example demonstrates explicit construction of a readable pipe for the subprocess's stdin. It explicitly creates, connects, and assigns the pipe, then closes the parent's end. ```cpp // create it explicitly { // the pipe the child process reads from asio::readable_pipe rp{ctx}; asio::connect_pipe(rp, wp); // `rp.native_handle()` will be assigned to the child processes stdin process proc{ctx, "/bin/bash", {}, process_stdio{.in=rp}}; rp.close(); // close it so the pipe closes when the `proc exits. } ``` -------------------------------- ### Get Process Command Line Source: https://github.com/boostorg/process/blob/develop/doc/reference/ext.adoc Retrieves the command line used to launch a process. Overloads are available for process handles and PIDs, with and without error code handling. ```cpp // Get the cmd line used to launche the process template shell cmd(basic_process_handle & handle, error_code & ec); template shell cmd(basic_process_handle & handle); shell cmd(pid_type pid, error_code & ec); shell cmd(pid_type pid); ``` -------------------------------- ### Custom Initializer for Windows Launchers Source: https://github.com/boostorg/process/blob/develop/doc/launcher.adoc Defines a `custom_initializer` struct for Windows systems to hook into the process creation lifecycle. It includes callbacks for setup, error handling, and success, operating with `std::wstring` for command lines. ```cpp struct custom_initializer { // called before a call to CreateProcess. A returned error will cancel the launch. template error_code on_setup(Launcher & launcher, const filesystem::path &executable, std::wstring &cmd_line); // called for every initializer if an error occurred during setup or process creation template void on_error(Launcher & launcher, const filesystem::path &executable, std::wstring &cmd_line, const error_code & ec); // called after successful process creation template void on_success(Launcher & launcher, const filesystem::path &executable, std::wstring &cmd_line); }; ``` -------------------------------- ### Get Native Process Handle Source: https://github.com/boostorg/process/blob/develop/doc/reference/process.adoc Retrieves the native operating system handle for the process. This might be undefined on POSIX systems that only support signals. ```cpp native_handle_type native_handle() {return process_handle_.native_handle(); } ``` -------------------------------- ### Get Process Executable Path Source: https://github.com/boostorg/process/blob/develop/doc/reference/ext.adoc Fetches the path to the executable file of a process. Available for process handles and PIDs, with and without error code handling. ```cpp // Get the executable of the process. template filesystem::path exe(basic_process_handle & handle, error_code & ec); template filesystem::path exe(basic_process_handle & handle) filesystem::path exe(pid_type pid, error_code & ec); filesystem::path exe(pid_type pid); ``` -------------------------------- ### Custom Initializer for POSIX Launchers Source: https://github.com/boostorg/process/blob/develop/doc/launcher.adoc Defines a `custom_initializer` struct for POSIX systems to hook into the process creation lifecycle. It includes callbacks for setup, error handling, success, and fork/exec specific events. ```cpp struct custom_initializer { // called before a call to fork. A returned error will cancel the launch. template error_code on_setup(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line)); // called for every initializer if an error occurred during setup or process creation template void on_error(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line), const error_code & ec); // called after successful process creation template void on_success(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line)); // called for every initializer if an error occurred when forking, in addition to on_error. template void on_fork_error(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line), const error_code & ec); // called before a call to execve. A returned error will cancel the launch. Called from the child process. template error_code on_exec_setup(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line)); // called after a failed call to execve from the child process. template void on_exec_error(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line)); }; ``` -------------------------------- ### Shell Utility Class Overview Source: https://github.com/boostorg/process/blob/develop/doc/reference/shell.adoc This utility class parses command lines into tokens and allows users to execute processes based on textual inputs. In v1, this was possible directly when starting a process, but has been removed based on the security risks associated with this. By making the shell parsing explicit, it encourages a user to run a sanity check on the executable before launching it. ```APIDOC ## Shell Utility Class This utility class parses command lines into tokens and allows users to execute processes based on textual inputs. By making the shell parsing explicit, it encourages a user to run a sanity check on the executable before launching it. ### Example Usage ```cpp asio::io_context ctx; auto cmd = shell("my-app --help"); auto exe = cmd.exe(); check_if_malicious(exe); process proc{ctx, exe, cmd.args()}; ``` ### `shell` Struct Definition ```cpp /// Utility to parse commands struct shell { shell() = default; template shell(basic_string_view input); shell(basic_cstring_ref input); shell(const shell &) = delete; shell(shell && lhs) noexcept; shell& operator=(const shell &) = delete; shell& operator=(shell && lhs) noexcept; // the length of the parsed shell, including the executable int argc() const ; char_type** argv() const; char_type** begin() const; char_type** end() const; bool empty() const; std::size_t size() const; // Native representation of the arguments to be used - excluding the executable args_type args() const; template filesystem::path exe(Environment && env = environment::current()) const; }; ``` ### Member Functions - **`argc()`** (int): Returns the number of arguments, including the executable. - **`argv()`** (char_type**): Returns a C-style array of argument strings. - **`begin()`** (char_type**): Returns an iterator to the beginning of the argument list. - **`end()`** (char_type**): Returns an iterator to the end of the argument list. - **`empty()`** (bool): Returns true if the shell command is empty, false otherwise. - **`size()`** (std::size_t): Returns the number of arguments. - **`args()`** (args_type): Returns the native representation of the arguments, excluding the executable. - **`exe(Environment && env = environment::current())`** (filesystem::path): Returns the path to the executable. Accepts an optional environment view. ``` -------------------------------- ### Shared Pipe for Subprocess Stdout and Stderr Source: https://github.com/boostorg/process/blob/develop/doc/reference/stdio.adoc This example shows how to use a single readable pipe for both the stdout and stderr of a subprocess. The parent process reads from this pipe, and both child streams write to it. ```cpp // the pipe the parent process reads from and both // stderr & stdout of the child process write to asio::readable_pipe rp{ctx}; asio::writable_pipe wp{ctx}; asio::connect_pipe(rp, wp); process proc{ctx, "/bin/bash", {}, process_stdio{.out=wp, .err=wp}}; wp.close(); // close it so the pipe closes when the `proc exits. ``` -------------------------------- ### Connect Asio Pipes to Subprocess Stdout/Stderr Source: https://github.com/boostorg/process/blob/develop/doc/stdio.adoc Use asio pipes for subprocess I/O. Readable pipes connect to 'out' and 'err'. Ensure proper setup for inter-process communication. ```cpp auto readable_pipe = process::pipe(io_context); child.set_stdin(process::stream_behavior(readable_pipe.native_handle())); child.set_stdout(process::stream_behavior(readable_pipe.native_handle())); ``` -------------------------------- ### Create a process using default_launcher Source: https://github.com/boostorg/process/blob/develop/doc/reference/default_launcher.adoc Instantiate a process using the default_launcher, which is the standard approach. This can be done directly or by calling the default_launcher object. ```cpp asio::io_context ctx; process proc(ctx.get_executor(), "test", {}); // equivalent to process proc = default_launcher()(ctx.get_executor(), "test", {}); ``` -------------------------------- ### Get Process ID Source: https://github.com/boostorg/process/blob/develop/doc/reference/process.adoc Retrieves the unique identifier (PID) of the process. ```cpp pid_type id() const; ``` -------------------------------- ### Launch Process and Interact with Pipes Source: https://github.com/boostorg/process/blob/develop/doc/reference/popen.adoc Launches a process using `popen` and demonstrates writing to its stdin and reading from its stdout. ```cpp popen proc(executor, find_executable("addr2line"), {argv[0]}); asio::write(proc, asio::buffer("main\n")); std::string line; asio::read_until(proc, asio::dynamic_buffer(line), '\n'); ``` -------------------------------- ### Environment Variable Access Source: https://github.com/boostorg/process/blob/develop/doc/reference/environment.adoc Functions for getting, setting, and unsetting environment variables for the current process. ```APIDOC ## Get Environment Variable ### Description Retrieves the value of an environment variable. ### Overloads - `value get(const key & k, error_code & ec)` - `value get(const key & k)` - `value get(basic_cstring_ref> k, error_code & ec)` - `value get(basic_cstring_ref> k)` - `value get(const char_type * c, error_code & ec)` - `value get(const char_type * c)` ### Parameters - `k` (const key &): The key of the environment variable. - `k` (basic_cstring_ref<...>): A C-string reference to the key. - `c` (const char_type *): A C-style string representing the key. - `ec` (error_code &): An error code to store any encountered error. ### Returns A `value` object representing the environment variable's value. ## Set Environment Variable ### Description Sets or modifies an environment variable. ### Overloads - `void set(const key & k, value_view vw, error_code & ec)` - `void set(const key & k, value_view vw)` - `void set(basic_cstring_ref> k, value_view vw, error_code & ec)` - `void set(basic_cstring_ref> k, value_view vw)` - `void set(const char_type * k, value_view vw, error_code & ec)` - `void set(const char_type * k, value_view vw)` - `template void set(const key & k, const Char * vw, error_code & ec)` - `template void set(const key & k, const Char * vw)` - `template void set(basic_cstring_ref> k, const Char * vw, error_code & ec)` - `template void set(basic_cstring_ref> k, const Char * vw)` - `template void set(const char_type * k, const Char * vw, error_code & ec)` - `template void set(const char_type * k, const Char * vw)` ### Parameters - `k` (const key &): The key of the environment variable. - `k` (basic_cstring_ref<...>): A C-string reference to the key. - `k` (const char_type *): A C-style string representing the key. - `vw` (value_view): The value to set for the environment variable. - `vw` (const Char *): A C-style string representing the value. - `ec` (error_code &): An error code to store any encountered error. ## Unset Environment Variable ### Description Removes an environment variable from the current process's environment. ### Overloads - `void unset(const key & k, error_code & ec)` - `void unset(const key & k)` ### Parameters - `k` (const key &): The key of the environment variable to remove. - `ec` (error_code &): An error code to store any encountered error. ``` -------------------------------- ### Boost.Process Library Overview Source: https://github.com/boostorg/process/blob/develop/doc/index.adoc This section provides an overview of the Boost.Process library, its version, and the included documentation modules. ```APIDOC ## Boost.Process Library Version 2.0, 19.11.2024 This library provides functionalities for launching and managing child processes. ### Included Modules: - Quick Start Guide - Launcher - Initializers (start_dir, stdio, env) - Reference (bind_launcher, cstring_ref, default_launcher, environment, error, execute, exit_code, ext, pid, popen, process, process_handle, shell, start_dir, stdio, ext, posix/bind_fd, windows/creation_flags, windows/show_window) - Version 2.0 specific information - Acknowledgements ``` -------------------------------- ### Get Environment Variable Source: https://github.com/boostorg/process/blob/develop/doc/reference/environment.adoc Retrieve the value of an environment variable. Overloads are provided for different key types and error handling. ```cpp value get(const key & k, error_code & ec); value get(const key & k); value get(basic_cstring_ref> k, error_code & ec); value get(basic_cstring_ref> k); value get(const char_type * c, error_code & ec); value get(const char_type * c); ``` -------------------------------- ### Get Process Exit Code Source: https://github.com/boostorg/process/blob/develop/doc/quickstart.adoc Retrieves the exit code of a completed process. This can also be obtained from the result of a wait operation. ```cpp auto exit_code = proc.exit_code(); ``` -------------------------------- ### Use Process Creation Flags Source: https://github.com/boostorg/process/blob/develop/doc/reference/windows/creation_flags.adoc Demonstrates how to use the `create_new_console` flag when creating a new process. This flag ensures that the new process is launched with its own console window. ```cpp process p{"C:\\not-a-virus.exe", {}, process::windows::create_new_console}; ``` -------------------------------- ### Boost.Process Reference - Core Components Source: https://github.com/boostorg/process/blob/develop/doc/index.adoc Reference documentation for core components of the Boost.Process library. ```APIDOC ## Reference - Core Components This section details the core components and functionalities of the Boost.Process library. ### `bind_launcher.adoc` Details on binding launchers. ### `cstring_ref.adoc` Reference for C-string related functionalities. ### `default_launcher.adoc` Information about the default launcher. ### `environment.adoc` Documentation on managing environment variables. ### `error.adoc` Information regarding error handling. ### `execute.adoc` Details on executing processes. ### `exit_code.adoc` Information about exit codes. ### `ext.adoc` Extended functionalities. ### `pid.adoc` Details on process IDs. ### `popen.adoc` Information about popen-like functionalities. ### `process.adoc` Reference for the `process` class. ### `process_handle.adoc` Details on process handles. ### `shell.adoc` Information about shell integration. ### `start_dir.adoc` Reference for setting the start directory. ### `stdio.adoc` Reference for standard input/output/error stream handling. ``` -------------------------------- ### Invoke a Windows Launcher Source: https://github.com/boostorg/process/blob/develop/doc/launcher.adoc Demonstrates how to invoke a Windows launcher, such as `windows::as_user_launcher`, to create a new process. Requires an `io_context` and an `error_code` for error handling. ```cpp auto l = windows::as_user_launcher((HANDLE)0xDEADBEEF); asio::io_context ctx; boost::system::error_code ec; auto proc = l(ctx, ec, "C:\\User\\boost\\Downloads\\totally_not_a_virus.exe", {}); ``` -------------------------------- ### Default Launcher API Source: https://github.com/boostorg/process/blob/develop/doc/reference/default_launcher.adoc The default_launcher is the standard way of creating a process. It has four overloads to accommodate different initialization scenarios. ```APIDOC ## Default Launcher Overloads ### Description Provides four overloads for creating a process using the default launcher. ### Method N/A (This describes function overloads, not an HTTP endpoint) ### Endpoint N/A ### Parameters #### Overload 1 - **ctx** (ExecutionContext &) - Description not provided - **path** (filesystem::path) - Description not provided - **args** (Args &&) - Description not provided - **inits** (Inits && ...) - Description not provided #### Overload 2 - **exec** (Executor &) - Description not provided - **path** (filesystem::path) - Description not provided - **args** (Args &&) - Description not provided - **inits** (Inits && ...) - Description not provided #### Overload 3 - **ctx** (ExecutionContext &) - Description not provided - **err** (error_code&) - Description not provided - **path** (filesystem::path) - Description not provided - **args** (Args &&) - Description not provided - **inits** (Inits && ...) - Description not provided #### Overload 4 - **exec** (Executor &) - Description not provided - **err** (error_code&) - Description not provided - **path** (filesystem::path) - Description not provided - **args** (Args &&) - Description not provided - **inits** (Inits && ...) - Description not provided ### Request Example ```cpp asio::io_context ctx; process proc(ctx.get_executor(), "test", {}); // equivalent to process proc = default_launcher()(ctx.get_executor(), "test", {}); ``` ### Response #### Success Response - **basic_process** (basic_process or basic_process) - Description not provided #### Response Example N/A ``` -------------------------------- ### Get Process Exit Code Source: https://github.com/boostorg/process/blob/develop/doc/reference/process.adoc Returns the evaluated exit code of the process. If the process has already completed, the exit code will be stored internally. ```cpp int exit_code() const; ``` -------------------------------- ### bind_launcher Source: https://github.com/boostorg/process/blob/develop/doc/reference/bind_launcher.adoc Creates a new launcher with the bound initializer. ```APIDOC ## `bind_launcher` ### Description Creates a new launcher with the bound initializer. ### Method N/A (Function Signature) ### Endpoint N/A ### Parameters #### Template Parameters - **Launcher** (typename) - The type of the launcher. - **Init** (typename...) - The types of the initializers. #### Function Parameters - **launcher** (Launcher &&) - The launcher to bind initializers to. - **init** (Init &&...) - The initializers to bind. ### Return Value - `auto` - A new launcher with bound initializers. ``` -------------------------------- ### Get Current Environment Variables Source: https://github.com/boostorg/process/blob/develop/doc/env.adoc Retrieves the current process's environment variables. This is useful for inspecting or modifying the environment before launching a subprocess. ```cpp auto env = environment::current(); for (const auto& var : env) { std::cout << var.name << "," << var.value << std::endl; } ``` -------------------------------- ### Boost.Process Initializers Source: https://github.com/boostorg/process/blob/develop/doc/index.adoc Details on the initializers used for configuring child processes. ```APIDOC ## Initializers Initializers are used to configure the environment and standard streams for launched processes. ### `start_dir.adoc` Provides functionality to set the working directory for the child process. ### `stdio.adoc` Defines how standard input, output, and error streams are handled for the child process. ### `env.adoc` Allows for setting environment variables for the child process. ``` -------------------------------- ### Create Default Launcher with Bound Initializers Source: https://github.com/boostorg/process/blob/develop/doc/reference/bind_launcher.adoc This function is a convenience wrapper around `bind_launcher`, using `default_launcher` as the first parameter. It's useful for quickly creating a default launcher with bound parameters. ```cpp template auto bind_default_launcher(Init && ... init); ``` -------------------------------- ### Get Process Environment Source: https://github.com/boostorg/process/blob/develop/doc/reference/ext.adoc Retrieves the environment variables of a process. Functions are provided for process handles and PIDs, with and without explicit error code parameters. ```cpp // Get the current environment of the process. template env_view cwd(basic_process_handle & handle, error_code & ec); template env_view cwd(basic_process_handle & handle) env_view env(pid_type pid, error_code & ec); env_view env(pid_type pid); ``` -------------------------------- ### default_launcher Overloads Source: https://github.com/boostorg/process/blob/develop/doc/reference/default_launcher.adoc The default_launcher supports multiple overloads to accommodate different initialization requirements, including context, executor, paths, arguments, and error codes. ```cpp (ExecutionContext &, filesystem::path, Args && args, Inits && ... inits) -> basic_process (Executor &, filesystem::path, Args && args, Inits && ... inits) -> basic_process; (ExecutionContext &, error_code&, filesystem::path, Args && args, Inits && ... inits) -> basic_process; (Executor &, error_code&, filesystem::path, Args && args, Inits && ... inits) -> basic_process ``` -------------------------------- ### Process Command Line Source: https://github.com/boostorg/process/blob/develop/doc/reference/ext.adoc Obtain the command line used to launch a process. ```APIDOC ## Get Process Command Line ### Description Retrieves the command line arguments used to launch a process. ### Method Various (function overloads) ### Endpoint N/A (C++ functions) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Example usage with a process handle // shell cmd_line = process::ext::cmd(process_handle, ec); // Example usage with a process ID // shell cmd_line = process::ext::cmd(pid, ec); ``` ### Response #### Success Response (200) - **shell** (string) - The command line string. #### Response Example ```json { "cmd_line": "/usr/bin/my_process --arg1 value1" } ``` ### Notes - Overloads are available for `basic_process_handle` and `pid_type`. - Error handling is provided via `error_code` parameter or exceptions. - May fail with "operation_not_supported" on some platforms. ``` -------------------------------- ### Get Process Current Working Directory Source: https://github.com/boostorg/process/blob/develop/doc/reference/ext.adoc Obtains the current working directory of a process. Supports both process handles and PIDs, with options for error code reporting. ```cpp // Get the current working directory of the process. template filesystem::path cwd(basic_process_handle & handle, error_code & ec); template filesystem::path cwd(basic_process_handle & handle) filesystem::path cwd(pid_type pid, error_code & ec); filesystem::path cwd(pid_type pid); ``` -------------------------------- ### Create Launcher with Bound Initializer Source: https://github.com/boostorg/process/blob/develop/doc/reference/bind_launcher.adoc Use this function to create a new launcher with specific initializers bound to it. The template parameters allow for generic launcher and initializer types. ```cpp template auto bind_launcher(Launcher && launcher, Init && ... init); ``` -------------------------------- ### Get Native Process Exit Code Source: https://github.com/boostorg/process/blob/develop/doc/reference/process.adoc Returns the native type representing the process's exit code. This might be undefined on POSIX systems that only support signals. ```cpp native_exit_code_type native_exit_code() const; ``` -------------------------------- ### Boost.Process Reference - Platform Specifics Source: https://github.com/boostorg/process/blob/develop/doc/index.adoc Platform-specific functionalities for Boost.Process. ```APIDOC ## Reference - Platform Specifics This section covers platform-specific features of the Boost.Process library. ### POSIX Specifics #### `bind_fd.adoc` Details on binding file descriptors on POSIX systems. ### Windows Specifics #### `creation_flags.adoc` Information on creation flags for Windows process creation. #### `show_window.adoc` Details on controlling window visibility for processes on Windows. ``` -------------------------------- ### bind_default_launcher Source: https://github.com/boostorg/process/blob/develop/doc/reference/bind_launcher.adoc Calls bind_launcher with the default_launcher as the first parameter. ```APIDOC ## `bind_default_launcher` ### Description Calls `bind_launcher` with the `default_launcher` as the first parameter. Returns a new launcher with bound parameters. ### Method N/A (Function Signature) ### Endpoint N/A ### Parameters #### Template Parameters - **Init** (typename...) - The types of the initializers. #### Function Parameters - **init** (Init &&...) - The initializers to bind to the default launcher. ### Return Value - `auto` - A new launcher with bound parameters. ``` -------------------------------- ### Asynchronous Read Operation Source: https://github.com/boostorg/process/blob/develop/doc/reference/popen.adoc Starts an asynchronous read operation from the process's standard output. The operation completes when data is read or an error occurs, using a completion token to handle the result. ```cpp template > auto async_read_some(const MutableBufferSequence& buffers, BOOST_ASIO_MOVE_ARG(ReadToken) token = net::default_completion_token_t()); ``` -------------------------------- ### Environment Utilities Source: https://github.com/boostorg/process/blob/develop/doc/reference/environment.adoc Utility functions for finding the home directory and searching for executables within the environment. ```APIDOC ## `home()` Function ### Description Finds the home folder in an environment-like type. ### Parameters - `env` (Environment &&): The environment to search. Defaults to the current process environment. ### Requirements for `Environment` Type - Must be a range with a value type `T`. - `std::get<0>(value)` must return a type comparable to `key_view`. - `std::get<1>(value)` must return a type convertible to `filesystem::path`. ### Returns A `filesystem::path` to the home directory, or an empty path if it cannot be found. ## `find_executable()` Function ### Description Finds an executable `name` in an environment-like type. ### Parameters - `name` (BOOST_PROCESS_V2_NAMESPACE::filesystem::path): The name of the executable to find. - `env` (Environment &&): The environment to search. Defaults to the current process environment. ### Returns A `filesystem::path` to the executable if found, otherwise an empty path. ``` -------------------------------- ### Define Process Show Window Flags Source: https://github.com/boostorg/process/blob/develop/doc/reference/windows/show_window.adoc Defines templated structures for setting `wShowWindow` flags, allowing explicit control over how a new process window is displayed. ```cpp /// A templated initializer to set wShowWindow flags. template struct process_show_window; //Hides the window and activates another window. constexpr static process_show_window show_window_hide; //Activates the window and displays it as a maximized window. constexpr static process_show_window show_window_maximized; //Activates the window and displays it as a minimized window. constexpr static process_show_window show_window_minimized; //Displays the window as a minimized window. This value is similar to `minimized`, except the window is not activated. constexpr static process_show_window show_window_minimized_not_active; //Displays a window in its most recent size and position. This value is similar to show_normal`, except that the window is not activated. constexpr static process_show_window show_window_not_active; //Activates 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. constexpr static process_show_window show_window_normal; ``` -------------------------------- ### key Structure Source: https://github.com/boostorg/process/blob/develop/doc/reference/environment.adoc Details the constructors and type aliases for the key structure, used for environment keys. ```APIDOC ## key Structure ### Description Represents a key within an environment. ### Type Aliases - `using value_type = char_type;` - `using traits_type = key_char_traits;` - `using string_type = std::basic_string;` - `using string_view_type = basic_string_view;` ### Constructors - `key();` - `key( const key& p ) = default;` - `key( key&& p ) noexcept = default;` - `key( const string_type& source );` - `key( string_type&& source );` - `key( const value_type * raw );` - `key( value_type * raw );` - `explicit key(key_view kv);` - `template< class Source > key( const Source& source);` ``` -------------------------------- ### Initialize Subprocess stdio with Null Error Stream Source: https://github.com/boostorg/process/blob/develop/doc/reference/stdio.adoc Demonstrates initializing subprocess stdio with a null error stream, compatible with C++17 designated initializers and older C++ standards. ```cpp asio::io_context ctx; /// C++17 v2::process proc17(ctx, "/bin/bash", {}, v2::process_stdio{.err=nullptr}); /// C++11 & C++14 v2::process proc17(ctx, "/bin/bash", {}, v2::process_stdio{ {}, {}, nullptr}); ``` -------------------------------- ### Process ID Type and Functions Source: https://github.com/boostorg/process/blob/develop/doc/reference/pid.adoc Defines `pid_type` for process identifiers and provides functions to get the current process ID, list all PIDs, and find parent/child PIDs. Use the overloads with `error_code` for error handling. ```cpp //An integral type representing a process id. typedef implementation_defined pid_type; // Get the process id of the current process. pid_type current_pid(); // List all available pids. std::vector all_pids(boost::system::error_code & ec); std::vector all_pids(); // return parent pid of pid. pid_type parent_pid(pid_type pid, boost::system::error_code & ec); pid_type parent_pid(pid_type pid); // return child pids of pid. std::vector child_pids(pid_type pid, boost::system::error_code & ec); std::vector child_pids(pid_type pid); ``` -------------------------------- ### Reusing Process Environment Source: https://github.com/boostorg/process/blob/develop/doc/reference/environment.adoc Demonstrates reusing a process_environment object across multiple process creations. Note that the OS may modify the internal state. ```cpp auto exe = find_executable("printenv"); process_environment env = {"FOO=BAR", "BAR=FOO"}; process proc1(executor, exe, {"FOO"}, env); process proc2(executor, exe, {"BAR"}, env); ``` -------------------------------- ### popen Class Overview Source: https://github.com/boostorg/process/blob/develop/doc/reference/popen.adoc The `popen` class is designed to launch a process and establish connections to its standard input and output streams via pipes. ```APIDOC ## `popen` Class ### Description The `popen` class launches a process and connects its standard input and standard output to pipes, allowing for inter-process communication. ### Constructors Several constructors are available to initialize and launch a `popen` object, with options for specifying the executor, executable path, arguments, and various initialization properties. - `explicit basic_popen(executor_type exec);` - `template explicit basic_popen(ExecutionContext & context);` - `template explicit basic_popen(executor_type executor, const filesystem::path& exe, std::initializer_list args, Inits&&... inits);` - `template explicit basic_popen(Launcher && launcher, executor_type executor, const filesystem::path& exe, std::initializer_list args, Inits&&... inits);` - `template explicit basic_popen(executor_type executor, const filesystem::path& exe, Args&& args, Inits&&... inits);` - `template explicit basic_popen(Launcher && launcher, executor_type executor, const filesystem::path& exe, Args&& args, Inits&&... inits);` - `template explicit basic_popen(ExecutionContext & context, const filesystem::path& exe, std::initializer_list args, Inits&&... inits);` - `template explicit basic_popen(Launcher && launcher, ExecutionContext & context, const filesystem::path& exe, std::initializer_list args, Inits&&... inits);` - `template explicit basic_popen(ExecutionContext & context, const filesystem::path& exe, Args&& args, Inits&&... inits);` - `template explicit basic_popen(Launcher && launcher, ExecutionContext & context, const filesystem::path& exe, Args&& args, Inits&&... inits);` ### Member Types - `using stdin_type = net::basic_writable_pipe;`: Type for the stdin pipe on the parent process side. - `using stdout_type = net::basic_readable_pipe;`: Type for the stdout pipe on the parent process side. ### Member Functions - `stdin_type & get_stdin();`: Get the stdin pipe. - `const stdin_type & get_stdin() const;`: Get the stdin pipe (const version). - `stdout_type & get_stdout();`: Get the stdout pipe. - `const stdout_type & get_stdout() const;`: Get the stdout pipe (const version). - `template std::size_t write_some(const ConstBufferSequence& buffers);`: Write data to the stdin pipe. ### Example Usage ```cpp // Launch a process and interact with its stdin and stdout popen proc(executor, find_executable("addr2line"), {argv[0]}); asio::write(proc, asio::buffer("main\n")); std::string line; asio::read_until(proc, asio::dynamic_buffer(line), '\n'); ``` ``` -------------------------------- ### Find Executable Source: https://github.com/boostorg/process/blob/develop/doc/reference/environment.adoc Search for an executable file in the environment's PATH. Requires a name and an environment to search within. ```cpp template filesystem::path find_executable(BOOST_PROCESS_V2_NAMESPACE::filesystem::path name, Environment && env = current()); ``` -------------------------------- ### Bind null device as file descriptor Source: https://github.com/boostorg/process/blob/develop/doc/reference/posix/bind_fd.adoc Inherit a null device as a set descriptor. This passes a null device as 42 to the child process. ```cpp process p{"test", {}, posix::bind_fd(42, nullptr)}; ``` -------------------------------- ### Bind newly opened file as file descriptor Source: https://github.com/boostorg/process/blob/develop/doc/reference/posix/bind_fd.adoc Inherit a newly opened file as a set descriptor. This passes a descriptor to "extra-output.txt" as 42 to the child process. Default flags are O_RDWR | O_CREAT. ```cpp process p{"test", {}, posix::bind_fd(42, "extra-output.txt")}; ``` -------------------------------- ### key_view Structure Definition Source: https://github.com/boostorg/process/blob/develop/doc/reference/environment.adoc Defines the `key_view` structure, which provides a view for environment keys. It includes constructors, assignment operators, swap functionality, and methods for accessing the native string representation, comparing keys, and converting to different string types. ```cpp // A view type for a key of an environment struct key_view { using value_type = char_type; using traits_type = key_char_traits; using string_view_type = basic_string_view; using string_type = std::basic_string>; key_view() noexcept = default; key_view( const key_view& p ) = default; key_view( key_view&& p ) noexcept = default; template key_view( const Source& source ); key_view( const char_type * p); key_view( char_type * p); ~key_view() = default; key_view& operator=( const key_view& p ) = default; key_view& operator=( key_view&& p ) noexcept = default; key_view& operator=( string_view_type source ); void swap( key_view& other ) noexcept; string_view_type native() const noexcept; operator string_view_type() const; int compare( const key_view& p ) const noexcept; int compare( string_view_type str ) const; int compare( const value_type* s ) const; template< class CharT, class Traits = std::char_traits, class Alloc = std::allocator > std::basic_string basic_string( const Alloc& alloc = Alloc()) const; std::string string() const; std::wstring wstring() const; string_type native_string() const; friend bool operator==(key_view l, key_view r); friend bool operator!=(key_view l, key_view r); friend bool operator<=(key_view l, key_view r); friend bool operator>=(key_view l, key_view r); friend bool operator< (key_view l, key_view r); friend bool operator> (key_view l, key_view r); bool empty() const; template< class CharT, class Traits > friend std::basic_ostream& operator<<( std::basic_ostream& os, const key_view& p ); template< class CharT, class Traits > friend std::basic_istream& operator>>( std::basic_istream& is, key_view& p ); const value_type * data() const; std::size_t size() const; }; ``` -------------------------------- ### Find Home Directory Source: https://github.com/boostorg/process/blob/develop/doc/reference/environment.adoc Locate the user's home directory within a given environment. Defaults to the current process environment. ```cpp template inline filesystem::path home(Environment && env = current()); ``` -------------------------------- ### Initialize Subprocess stdio Source: https://github.com/boostorg/process/blob/develop/doc/reference/stdio.adoc Use `v2::process_stdio{}` to initialize the stdio of a subprocess. By default, stdio handles are inherited from the parent process. ```cpp asio::io_context ctx; v2::process proc(ctx, "/bin/bash", {}, v2::process_stdio{}); ```