Repository: mesos
Updated Branches:
  refs/heads/1.0.x d6132c58c -> 7b322c9ef


Added 'os::spawn()' to stout as 'argv' counterpart to 'os::system()'.

Review: https://reviews.apache.org/r/52184/

*** Modified for 1.0.x ***


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/d73c57e2
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/d73c57e2
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/d73c57e2

Branch: refs/heads/1.0.x
Commit: d73c57e24aa6de7e342306f52032c0a7a9d26150
Parents: d6132c5
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 21:21:11 2016 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/stout/os/posix/shell.hpp | 31 ++++++++++++++++++++
 .../stout/include/stout/os/windows/shell.hpp    | 17 +++++++++++
 2 files changed, 48 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/d73c57e2/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 1d73ae5..928c230 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 {
 
@@ -144,6 +145,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/d73c57e2/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 c6e141a..dcec3a7 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,21 @@ 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,
+// the exit code is propagated back through the parent via `exit()`.
 template<typename... T>
 inline int execlp(const char* file, T... t)
 {

Reply via email to