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

Function subprocess

src/subprocess.cpp:315–466  ·  view source on GitHub ↗

Executes a subprocess. NOTE: On Windows, components of the `path` and `argv` that need to be quoted are expected to have been quoted before they are passed to `subprocess. For example, either of these may contain paths with spaces in them, like `C:\"Program Files"\foo.exe`, where notably the character sequence `\"` is not escaped quote, but instead a path separator and the start of a path compone

Source from the content-addressed store, hash-verified

313// escape it. Therefore, incorrectly-quoted command arguments will probably
314// lead the child process to terminate with an error.
315Try<Subprocess> subprocess(
316 const string& path,
317 vector<string> argv,
318 const Subprocess::IO& in,
319 const Subprocess::IO& out,
320 const Subprocess::IO& err,
321 const flags::FlagsBase* flags,
322 const Option<map<string, string>>& environment,
323 const Option<lambda::function<
324 pid_t(const lambda::function<int()>&)>>& _clone,
325 const vector<Subprocess::ParentHook>& parent_hooks,
326 const vector<Subprocess::ChildHook>& child_hooks,
327 const vector<int_fd>& whitelist_fds)
328{
329 // TODO(hausdorff): We should error out on Windows here if we are passing
330 // parameters that aren't used.
331
332 // File descriptors for redirecting stdin/stdout/stderr.
333 // These file descriptors are used for different purposes depending
334 // on the specified I/O modes.
335 // See `Subprocess::PIPE`, `Subprocess::PATH`, and `Subprocess::FD`.
336 InputFileDescriptors stdinfds;
337 OutputFileDescriptors stdoutfds;
338 OutputFileDescriptors stderrfds;
339
340 // Prepare the file descriptor(s) for stdin.
341 Try<InputFileDescriptors> input = in.input();
342 if (input.isError()) {
343 return Error(input.error());
344 }
345
346 stdinfds = input.get();
347
348 // Prepare the file descriptor(s) for stdout.
349 Try<OutputFileDescriptors> output = out.output();
350 if (output.isError()) {
351 process::internal::close(stdinfds, stdoutfds, stderrfds);
352 return Error(output.error());
353 }
354
355 stdoutfds = output.get();
356
357 // Prepare the file descriptor(s) for stderr.
358 output = err.output();
359 if (output.isError()) {
360 process::internal::close(stdinfds, stdoutfds, stderrfds);
361 return Error(output.error());
362 }
363
364 stderrfds = output.get();
365
366#ifndef __WINDOWS__
367 // TODO(jieyu): Consider using O_CLOEXEC for atomic close-on-exec.
368 Try<Nothing> cloexec = internal::cloexec(stdinfds, stdoutfds, stderrfds);
369 if (cloexec.isError()) {
370 process::internal::close(stdinfds, stdoutfds, stderrfds);
371 return Error("Failed to cloexec: " + cloexec.error());
372 }

Callers 2

SetUpMethod · 0.50
TEST_FFunction · 0.50

Calls 8

closeFunction · 0.85
cloexecFunction · 0.85
cloneChildFunction · 0.85
createChildProcessFunction · 0.85
reapFunction · 0.85
bindFunction · 0.85
futureMethod · 0.80
getMethod · 0.45

Tested by 2

SetUpMethod · 0.40
TEST_FFunction · 0.40