Repository: mesos Updated Branches: refs/heads/master 0c6c5f8a3 -> a67b37cc8
Added 'os::spawn()' to stout as 'argv' counterpart to 'os::system()'. Review: https://reviews.apache.org/r/52184/ Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/4390d2fd Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/4390d2fd Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/4390d2fd Branch: refs/heads/master Commit: 4390d2fd56da2124c9762190fc931c3fed433df4 Parents: 0c6c5f8 Author: Kevin Klues <klue...@gmail.com> Authored: Thu Sep 22 20:45:52 2016 -0700 Committer: Jie Yu <yujie....@gmail.com> Committed: Thu Sep 22 20:45:52 2016 -0700 ---------------------------------------------------------------------- 3rdparty/stout/include/stout/os/posix/shell.hpp | 31 ++++++++++++++++++++ .../stout/include/stout/os/windows/shell.hpp | 13 ++++++++ 2 files changed, 44 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/4390d2fd/3rdparty/stout/include/stout/os/posix/shell.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/posix/shell.hpp b/3rdparty/stout/include/stout/os/posix/shell.hpp index df8fb4c..e7047ac 100644 --- a/3rdparty/stout/include/stout/os/posix/shell.hpp +++ b/3rdparty/stout/include/stout/os/posix/shell.hpp @@ -27,6 +27,7 @@ #include <stout/format.hpp> #include <stout/try.hpp> +#include <stout/os/raw/argv.hpp> namespace os { @@ -147,6 +148,36 @@ inline int system(const std::string& command) } } +// Executes a command by calling "<command> <arguments...>", and +// returns after the command has been completed. Returns 0 if +// succeeds, and -1 on error (e.g., fork/exec/waitpid failed). This +// function is async signal safe. We return int instead of returning a +// Try because Try involves 'new', which is not async signal safe. +inline int spawn( + const std::string& command, + const std::vector<std::string>& arguments) +{ + pid_t pid = ::fork(); + + if (pid == -1) { + return -1; + } else if (pid == 0) { + // In child process. + ::execvp(command.c_str(), os::raw::Argv(arguments)); + ::exit(127); + } else { + // In parent process. + int status; + while (::waitpid(pid, &status, 0) == -1) { + if (errno != EINTR) { + return -1; + } + } + + return status; + } +} + template<typename... T> inline int execlp(const char* file, T... t) http://git-wip-us.apache.org/repos/asf/mesos/blob/4390d2fd/3rdparty/stout/include/stout/os/windows/shell.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/windows/shell.hpp b/3rdparty/stout/include/stout/os/windows/shell.hpp index 8fb48c1..9f03385 100644 --- a/3rdparty/stout/include/stout/os/windows/shell.hpp +++ b/3rdparty/stout/include/stout/os/windows/shell.hpp @@ -21,6 +21,8 @@ #include <stout/try.hpp> +#include <stout/os/raw/argv.hpp> + namespace os { namespace Shell { @@ -104,6 +106,17 @@ inline int system(const std::string& command) } +// Executes a command by calling "<command> <arguments...>", and +// returns after the command has been completed. Returns 0 if +// succeeds, and -1 on error. +inline int spawn( + const std::string& command, + const std::vector<std::string>& arguments) +{ + return ::_spawnvp(_P_WAIT, command.c_str(), os::raw::Argv(arguments)); +} + + // On Windows, the `_spawnlp` call creates a new process. // In order to emulate the semantics of `execlp`, we spawn with `_P_WAIT`, // which forces the parent process to block on the child. When the child exits,