userver: userver/engine/subprocess/process_starter.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
process_starter.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/subprocess/process_starter.hpp
4/// @brief @copybrief engine::subprocess::ProcessStarter
5
6#include <optional>
7#include <string>
8#include <vector>
9
10#include <userver/engine/subprocess/child_process.hpp>
11#include <userver/engine/subprocess/environment_variables.hpp>
12#include <userver/engine/task/task_processor_fwd.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace engine::ev {
17class ThreadControl;
18} // namespace engine::ev
19
20namespace engine::subprocess {
21
22/// @brief Structure of settings for executing commands in the OS.
23struct ExecOptions final {
24 /// EnvironmentVariables for the environment in execution, or `std::nullopt`
25 /// to use GetCurrentEnvironmentVariables()
26 std::optional<EnvironmentVariables> env{};
27 /// EnvironmentVariablesUpdate for the update environment before execution, or
28 /// `std::nullopt` to leave `env` as is
30 /// File path to be redirected stdout, or `std::nullopt` to use the service's
31 /// stdout
32 std::optional<std::string> stdout_file{};
33 /// File path to be redirected stderr, or `std::nullopt` to use the service's
34 /// stderr
35 std::optional<std::string> stderr_file{};
36 /// If `false`, `executable_path` is treated as absolute path or a relative path.
37 /// If `true`, and `executable_path` does not contain `/`, and PATH in environment
38 /// variables, then it will be searched in the colon-separated list of
39 /// directory pathnames specified in the PATH environment variable.
40 /// If `true`, and `executable_path` contains `/`, `executable_path` is treated as absolute
41 /// path or a relative path.
42 bool use_path{false};
43};
44
45/// @ingroup userver_clients
46///
47/// @brief Creates a new OS subprocess and executes a command in it.
49public:
50 /// @param task_processor will be used for executing asynchronous fork + exec.
51 /// `main-task-processor is OK for this purpose.
52 explicit ProcessStarter(TaskProcessor& task_processor);
53
54 /// @param executable_path the absolute path or relative path. If `use_path` is
55 /// `true`, and `executable_path` does not contains `/`, then it will be searched in
56 /// the colon-separated list of directory pathnames specified in the PATH
57 /// environment variable. More details @ref ExecOptions::use_path
58 /// @param args exact args passed to the executable
59 /// @param options @ref ExecOptions settings
60 /// @throws std::runtime_error if `use_path` is `true`, `executable_path` contains `/`
61 /// and PATH not in environment variables
62 ChildProcess
63 Exec(const std::string& executable_path, const std::vector<std::string>& args, ExecOptions&& options = {});
64
65 /// @overload
66 /// @param executable_path the absolute path or relative path
67 /// @param args exact args passed to the executable
68 /// @param env redefines all environment variables
69 /// @deprecated Use the `Exec` overload taking @ref ExecOptions
70 ChildProcess Exec(
71 const std::string& executable_path,
72 const std::vector<std::string>& args,
73 const EnvironmentVariables& env,
74 // TODO: use something like pipes instead of path to files
75 const std::optional<std::string>& stdout_file = std::nullopt,
76 const std::optional<std::string>& stderr_file = std::nullopt
77 );
78
79 /// @overload
80 /// @param executable_path the absolute path or relative path
81 /// @param args exact args passed to the executable
82 /// @param env_update variables to add to the current environment, overwriting
83 /// existing ones
84 /// @deprecated Use the `Exec` overload taking @ref ExecOptions
85 ChildProcess Exec(
86 const std::string& executable_path,
87 const std::vector<std::string>& args,
89 // TODO: use something like pipes instead of path to files
90 const std::optional<std::string>& stdout_file = std::nullopt,
91 const std::optional<std::string>& stderr_file = std::nullopt
92 );
93
94private:
95 ev::ThreadControl& thread_control_;
96};
97
98} // namespace engine::subprocess
99
100USERVER_NAMESPACE_END