You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2014/04/10 19:10:00 UTC

git commit: Made os::system async signal safe.

Repository: mesos
Updated Branches:
  refs/heads/master 9beace423 -> d916668d4


Made os::system async signal safe.

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


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

Branch: refs/heads/master
Commit: d916668d48a85d79a4961a95d7df42c785758a97
Parents: 9beace4
Author: Jie Yu <yu...@gmail.com>
Authored: Tue Apr 8 11:07:26 2014 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Thu Apr 10 10:09:25 2014 -0700

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/os.hpp         | 26 +++++++++++++++++++-
 .../3rdparty/stout/tests/os_tests.cpp           | 13 ++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/d916668d/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
index 8eb3523..3875661 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
@@ -46,6 +46,7 @@
 #endif // __linux__
 #include <sys/types.h>
 #include <sys/utsname.h>
+#include <sys/wait.h>
 
 #include <list>
 #include <set>
@@ -547,9 +548,32 @@ inline Try<Nothing> rmdir(const std::string& directory, bool recursive = true)
 }
 
 
+// Executes a command by calling "/bin/sh -c <command>", and returns
+// after the command has been completed. Returns 0 if succeeds, and
+// return -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 system(const std::string& command)
 {
-  return ::system(command.c_str());
+  pid_t pid = ::fork();
+
+  if (pid == -1) {
+    return -1;
+  } else if (pid == 0) {
+    // In child process.
+    ::execl("/bin/sh", "sh", "-c", command.c_str(), (char*) NULL);
+    ::exit(127);
+  } else {
+    // In parent process.
+    int status;
+    while (::waitpid(pid, &status, 0) == -1) {
+      if (errno != EINTR) {
+        return -1;
+      }
+    }
+
+    return status;
+  }
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/d916668d/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
index d61db30..94eb256 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
@@ -129,6 +129,19 @@ TEST_F(OsTest, rmdir)
 }
 
 
+TEST_F(OsTest, system)
+{
+  EXPECT_EQ(0, os::system("exit 0"));
+  EXPECT_EQ(0, os::system("sleep 0"));
+  EXPECT_NE(0, os::system("exit 1"));
+  EXPECT_NE(0, os::system("invalid.command"));
+
+  // Note that ::system returns 0 for the following two cases as well.
+  EXPECT_EQ(0, os::system(""));
+  EXPECT_EQ(0, os::system(" "));
+}
+
+
 TEST_F(OsTest, nonblock)
 {
   int pipes[2];