MCPcopy Create free account
hub / github.com/3rdparty/libprocess / createChildProcess

Function createChildProcess

src/windows/subprocess.hpp:47–113  ·  view source on GitHub ↗

NOTE: We are expecting that components of `argv` that need to be quoted (for example, paths with spaces in them like `C:\"Program Files"\foo.exe`) to have been already quoted correctly before we generate `command`. Incorrectly-quoted command arguments will probably lead the child process to terminate with an error. See also NOTE on `process::subprocess`.

Source from the content-addressed store, hash-verified

45// Incorrectly-quoted command arguments will probably lead the child process
46// to terminate with an error. See also NOTE on `process::subprocess`.
47inline Try<::internal::windows::ProcessData> createChildProcess(
48 const std::string& path,
49 const std::vector<std::string>& argv,
50 const Option<std::map<std::string, std::string>>& environment,
51 const std::vector<Subprocess::ParentHook>& parent_hooks,
52 const InputFileDescriptors& stdinfds,
53 const OutputFileDescriptors& stdoutfds,
54 const OutputFileDescriptors& stderrfds,
55 const std::vector<int_fd>& whitelist_fds = {})
56{
57 const std::array<int_fd, 3> fds{
58 stdinfds.read, stdoutfds.write, stderrfds.write};
59
60 Try<::internal::windows::ProcessData> process_data =
61 ::internal::windows::create_process(
62 path,
63 argv,
64 environment,
65 true, // Create suspended.
66 fds,
67 whitelist_fds);
68
69 // Close the child-ends of the file descriptors that are created
70 // by this function.
71 foreach (const int_fd& fd, fds) {
72 if (fd.is_valid()) {
73 Try<Nothing> result = os::close(fd);
74 if (result.isError()) {
75 return Error(result.error());
76 }
77 }
78 }
79
80 if (process_data.isError()) {
81 return Error(process_data.error());
82 }
83
84 // Run the parent hooks.
85 const pid_t pid = process_data->pid;
86 foreach (const Subprocess::ParentHook& hook, parent_hooks) {
87 Try<Nothing> parent_setup = hook.parent_setup(pid);
88
89 // If the hook callback fails, we shouldn't proceed with the
90 // execution and hence the child process should be killed.
91 if (parent_setup.isError()) {
92 // Attempt to kill the process. Since it is still in suspended state, we
93 // do not need to kill any descendents. We also can't use `os::kill_job`
94 // because this process is not in a Job Object unless one of the parent
95 // hooks added it.
96 ::TerminateProcess(process_data->process_handle.get_handle(), 1);
97
98 return Error(
99 "Failed to execute Parent Hook in child '" + stringify(pid) +
100 "' with command '" + stringify(argv) + "': " +
101 parent_setup.error());
102 }
103 }
104

Callers 1

subprocessFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected