You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by an...@apache.org on 2018/02/21 22:57:12 UTC

[1/5] mesos git commit: Added fetcher tests for `stderr` output.

Repository: mesos
Updated Branches:
  refs/heads/master 49c642e98 -> 9635d4a2d


Added fetcher tests for `stderr` output.

The fetcher is supposed to log its `stderr` output to a redirected file
called `stderr` in the given sandbox directory. There previously existed
a bug on Windows due to incorrect handle inheritance where this
redirection failed silently, leaving the log empty. These unit tests
assert that the correct content is logged to the `stderr` file for both
a success and failure fetch scenario.

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


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

Branch: refs/heads/master
Commit: 9635d4a2d12fc77935c3d5d166469258634c6b7e
Parents: b1e9d42
Author: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Authored: Mon Feb 12 20:36:40 2018 -0800
Committer: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Committed: Wed Feb 21 14:00:11 2018 -0800

----------------------------------------------------------------------
 src/tests/fetcher_tests.cpp | 88 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/9635d4a2/src/tests/fetcher_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/fetcher_tests.cpp b/src/tests/fetcher_tests.cpp
index 1b4ba1d..311be6a 100644
--- a/src/tests/fetcher_tests.cpp
+++ b/src/tests/fetcher_tests.cpp
@@ -129,6 +129,94 @@ TEST_F(FetcherTest, FileURI)
 }
 
 
+TEST_F(FetcherTest, LogSuccessToStderr)
+{
+  // Valid test file with data.
+  string fromDir = path::join(os::getcwd(), "from");
+  ASSERT_SOME(os::mkdir(fromDir));
+  string testFile = path::join(fromDir, "test");
+  EXPECT_SOME(os::write(testFile, "data"));
+
+  string localFile = path::join(os::getcwd(), "test");
+  EXPECT_FALSE(os::exists(localFile));
+
+  // Fetcher stderr file.
+  string stderrFile = path::join(os::getcwd(), "stderr");
+  EXPECT_FALSE(os::exists(stderrFile));
+
+  slave::Flags flags;
+  flags.launcher_dir = getLauncherDir();
+
+  ContainerID containerId;
+  containerId.set_value(id::UUID::random().toString());
+
+  CommandInfo commandInfo;
+  CommandInfo::URI* uri = commandInfo.add_uris();
+  uri->set_value(uri::from_path(testFile));
+
+  Fetcher fetcher(flags);
+
+  Future<Nothing> fetch =
+    fetcher.fetch(containerId, commandInfo, os::getcwd(), None());
+  AWAIT_READY(fetch);
+
+  EXPECT_TRUE(os::exists(localFile));
+
+  // Fetcher created log file.
+  EXPECT_TRUE(os::exists(stderrFile));
+
+  // Fetcher logged success to stderr.
+  const Try<string> stderrContent = os::read(stderrFile);
+  EXPECT_SOME(stderrContent);
+  EXPECT_TRUE(strings::contains(
+      stderrContent.get(), "Fetching directly into the sandbox directory"));
+  EXPECT_TRUE(strings::contains(
+      stderrContent.get(), "Successfully fetched all URIs into"));
+}
+
+
+TEST_F(FetcherTest, LogFailureToStderr)
+{
+  // Purposefully invalid file to fetch.
+  string testFile = path::join(os::getcwd(), "test");
+  EXPECT_FALSE(os::exists(testFile));
+
+  string localFile = path::join(os::getcwd(), "test");
+  EXPECT_FALSE(os::exists(localFile));
+
+  // Fetcher stderr file.
+  string stderrFile = path::join(os::getcwd(), "stderr");
+  EXPECT_FALSE(os::exists(stderrFile));
+
+  slave::Flags flags;
+  flags.launcher_dir = getLauncherDir();
+
+  ContainerID containerId;
+  containerId.set_value(id::UUID::random().toString());
+
+  CommandInfo commandInfo;
+  CommandInfo::URI* uri = commandInfo.add_uris();
+  uri->set_value(uri::from_path(testFile));
+
+  Fetcher fetcher(flags);
+
+  Future<Nothing> fetch =
+    fetcher.fetch(containerId, commandInfo, os::getcwd(), None());
+  AWAIT_FAILED(fetch);
+
+  // Failed to fetch.
+  EXPECT_FALSE(os::exists(localFile));
+
+  // Fetcher created log file.
+  EXPECT_TRUE(os::exists(stderrFile));
+
+  // Fetcher logged failure to stderr.
+  Try<string> stderrContent = os::read(stderrFile);
+  EXPECT_SOME(stderrContent);
+  EXPECT_TRUE(strings::contains(stderrContent.get(), "Failed to fetch"));
+}
+
+
 // TODO(coffler): Test uses os::getuid(), which does not exist on Windows.
 //     Disable test until privilege model is worked out on Windows.
 #ifndef __WINDOWS__


[4/5] mesos git commit: Windows: Updated `internal::process:createChildProcess`.

Posted by an...@apache.org.
Windows: Updated `internal::process:createChildProcess`.

The interface of `internal::windows:create_process` was changed to
accept a `std::array<os::WindowsFD, 3>` instead of a `std::tuple`.

Furthermore, the error handling in `subprocess` was incorrect. We only
need to check the success of `createChildProcess`; we do not need to
check if `pid == -1` on Windows. We also now return the actual error
from `create_process` if it errored, instead of the whole
`Try<process_data>`.

The TODO about closing the child-ends of the file descriptors was
resolved. We now close these in `createChildProcess`, immediately after
`create_process`. This means we only have to do it once.

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


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

Branch: refs/heads/master
Commit: b1e9d4232e5931bcd0e1cd568a280a07828ff205
Parents: f946e53
Author: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Authored: Thu Feb 8 10:41:19 2018 -0800
Committer: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Committed: Wed Feb 21 14:00:11 2018 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/src/subprocess.cpp         | 21 +++------------
 3rdparty/libprocess/src/subprocess_windows.hpp | 29 ++++++++++++++-------
 2 files changed, 24 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/b1e9d423/3rdparty/libprocess/src/subprocess.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/subprocess.cpp b/3rdparty/libprocess/src/subprocess.cpp
index 785e2e1..8983263 100644
--- a/3rdparty/libprocess/src/subprocess.cpp
+++ b/3rdparty/libprocess/src/subprocess.cpp
@@ -426,26 +426,13 @@ Try<Subprocess> subprocess(
           stderrfds);
 
     if (process_data.isError()) {
-      process::internal::close(stdinfds, stdoutfds, stderrfds);
-      return Error(
-          "Could not launch child process: " + process_data.error());
+      // NOTE: `createChildProcess` either succeeds entirely or returns an
+      // `Error`. There is no need to check the value of the PID.
+      return Error(process_data.error());
     }
 
-    if (process_data.get().pid == -1) {
-      // Save the errno as 'close' below might overwrite it.
-      ErrnoError error("Failed to clone");
-      process::internal::close(stdinfds, stdoutfds, stderrfds);
-      return error;
-    }
-
-    // Close the child-ends of the file descriptors that are created
-    // by this function.
-    // TODO(jieyu): We should move the closing of FDs to
-    // 'createChildProcess' to be consistent with the posix path.
-    internal::close({stdinfds.read, stdoutfds.write, stderrfds.write});
-
     process.data->process_data = process_data.get();
-    process.data->pid = process_data.get().pid;
+    process.data->pid = process_data->pid;
 #endif // __WINDOWS__
   }
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/b1e9d423/3rdparty/libprocess/src/subprocess_windows.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/subprocess_windows.hpp b/3rdparty/libprocess/src/subprocess_windows.hpp
index 0183bb4..4cc5675 100644
--- a/3rdparty/libprocess/src/subprocess_windows.hpp
+++ b/3rdparty/libprocess/src/subprocess_windows.hpp
@@ -15,8 +15,8 @@
 
 #include <signal.h>
 
+#include <array>
 #include <string>
-#include <tuple>
 
 #include <glog/logging.h>
 
@@ -53,41 +53,52 @@ inline Try<::internal::windows::ProcessData> createChildProcess(
     const OutputFileDescriptors stdoutfds,
     const OutputFileDescriptors stderrfds)
 {
+  const std::array<os::WindowsFD, 3> fds{
+    stdinfds.read, stdoutfds.write, stderrfds.write};
+
   Try<::internal::windows::ProcessData> process_data =
     ::internal::windows::create_process(
         path,
         argv,
         environment,
         true, // Create suspended.
-        std::make_tuple(stdinfds.read, stdoutfds.write, stderrfds.write));
+        fds);
+
+  // Close the child-ends of the file descriptors that are created
+  // by this function.
+  foreach (const os::WindowsFD& fd, fds) {
+    if (fd >= 0) {
+      os::close(fd);
+    }
+  }
 
   if (process_data.isError()) {
-    return process_data;
+    return Error(process_data.error());
   }
 
   // Run the parent hooks.
-  const pid_t pid = process_data.get().pid;
+  const pid_t pid = process_data->pid;
   foreach (const Subprocess::ParentHook& hook, parent_hooks) {
-    Try<Nothing> parentSetup = hook.parent_setup(pid);
+    Try<Nothing> parent_setup = hook.parent_setup(pid);
 
     // If the hook callback fails, we shouldn't proceed with the
     // execution and hence the child process should be killed.
-    if (parentSetup.isError()) {
+    if (parent_setup.isError()) {
       // Attempt to kill the process. Since it is still in suspended state, we
       // do not need to kill any descendents. We also can't use `os::kill_job`
       // because this process is not in a Job Object unless one of the parent
       // hooks added it.
-      ::TerminateProcess(process_data.get().process_handle.get_handle(), 1);
+      ::TerminateProcess(process_data->process_handle.get_handle(), 1);
 
       return Error(
           "Failed to execute Parent Hook in child '" + stringify(pid) +
           "' with command '" + stringify(argv) + "': " +
-          parentSetup.error());
+          parent_setup.error());
     }
   }
 
   // Start child process.
-  if (::ResumeThread(process_data.get().thread_handle.get_handle()) == -1) {
+  if (::ResumeThread(process_data->thread_handle.get_handle()) == -1) {
     return WindowsError(
         "Failed to resume child process with command '" +
         stringify(argv) + "'");


[3/5] mesos git commit: Windows: Disabled `O_CLOEXEC` semantic mapping on Windows.

Posted by an...@apache.org.
Windows: Disabled `O_CLOEXEC` semantic mapping on Windows.

Previously we mapped `O_CLOEXEC` to `O_NOINHERIT`, which is close to the
intended semantics, but not close enough. Instead, we want all Windows
handles by default to be non-inheritable, and not tie any Windows
inheritance semantics to the close-on-exec semantics. So, we define
`O_CLOEXEC` on Windows to be `0`, and remove the logging from the
`os::cloexec` family of functions because do not ever intend to
implement them.

We achieve the "close" part of the semantics by making all handles
non-inheritable, as the intent is to avoid leaking handles.

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


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

Branch: refs/heads/master
Commit: faba7dfae00a5ae79015a16c7bb69bce9b84ed1e
Parents: 49c642e
Author: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Authored: Mon Jan 22 12:25:06 2018 -0800
Committer: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Committed: Wed Feb 21 14:00:11 2018 -0800

----------------------------------------------------------------------
 3rdparty/stout/include/stout/os/open.hpp          |  6 +++++-
 3rdparty/stout/include/stout/os/windows/fcntl.hpp | 11 ++++-------
 2 files changed, 9 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/faba7dfa/3rdparty/stout/include/stout/os/open.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/open.hpp b/3rdparty/stout/include/stout/os/open.hpp
index 6711f22..4dc5b08 100644
--- a/3rdparty/stout/include/stout/os/open.hpp
+++ b/3rdparty/stout/include/stout/os/open.hpp
@@ -32,6 +32,7 @@
 
 #ifndef O_CLOEXEC
 #error "missing O_CLOEXEC support on this platform"
+// NOTE: On Windows, `fnctl.hpp` defines `O_CLOEXEC` to a no-op.
 #endif
 
 namespace os {
@@ -43,7 +44,10 @@ inline Try<int_fd> open(const std::string& path, int oflag, mode_t mode = 0)
   // By default, Windows will perform "text translation" meaning that it will
   // automatically write CR/LF instead of LF line feeds. To prevent this, and
   // use the POSIX semantics, we open with `O_BINARY`.
-  int_fd fd = ::_wopen(longpath.data(), oflag | O_BINARY, mode);
+  //
+  // Also by default, we will mimic the Windows (non-CRT) APIs and make all
+  // opened handles non-inheritable.
+  int_fd fd = ::_wopen(longpath.data(), oflag | O_BINARY | O_NOINHERIT, mode);
 #else
   int_fd fd = ::open(path.c_str(), oflag, mode);
 #endif // __WINDOWS__

http://git-wip-us.apache.org/repos/asf/mesos/blob/faba7dfa/3rdparty/stout/include/stout/os/windows/fcntl.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/windows/fcntl.hpp b/3rdparty/stout/include/stout/os/windows/fcntl.hpp
index 5800ec9..bf8c38a 100644
--- a/3rdparty/stout/include/stout/os/windows/fcntl.hpp
+++ b/3rdparty/stout/include/stout/os/windows/fcntl.hpp
@@ -28,29 +28,26 @@
 #define O_CREAT _O_CREAT
 #define O_TRUNC _O_TRUNC
 #define O_APPEND _O_APPEND
-#define O_CLOEXEC _O_NOINHERIT
+// NOTE: Windows does not support the semantics of close-on-exec. Instead, by
+// default we set all handles to be non-inheritable.
+#define O_CLOEXEC 0
 
 namespace os {
 
-// NOTE: This is not supported on Windows.
 inline Try<Nothing> cloexec(const WindowsFD& fd)
 {
-  VLOG(2) << "`os::cloexec` has been called, but is a no-op on Windows";
   return Nothing();
 }
 
 
-// NOTE: This is not supported on Windows.
 inline Try<Nothing> unsetCloexec(const WindowsFD& fd)
 {
-  VLOG(2) << "`os::unsetCloexec` has been called, but is a no-op on Windows";
   return Nothing();
 }
 
 
 inline Try<bool> isCloexec(const WindowsFD& fd)
 {
-  VLOG(2) << "`os::isCloexec` has been called, but is a stub on Windows";
   return true;
 }
 
@@ -67,7 +64,7 @@ inline Try<Nothing> nonblock(const WindowsFD& fd)
       const u_long non_block_mode = 1;
       u_long mode = non_block_mode;
 
-      int result = ioctlsocket(fd, FIONBIO, &mode);
+      int result = ::ioctlsocket(fd, FIONBIO, &mode);
       if (result != NO_ERROR) {
         return WindowsSocketError();
       }


[2/5] mesos git commit: Windows: Fixed handle inheritance in `create_process` wrapper.

Posted by an...@apache.org.
Windows: Fixed handle inheritance in `create_process` wrapper.

The primary change in this patch is that `create_process` now enables
inheritance for the `pipe` handles passed before starting the child
process. This is required, otherwise the child process will behave
incorrectly (for example, it will write to `stdout` but that will go
nowhere, as the redirection silently failed). After the process is
created, inheritance is disabled to prevent further calls to
`create_process` from inheriting the wrong handles.

The `std::tuple<os::WindowsFD, ...>` type was changed to a
`std::array<os::WindowsFD, 3>` as it is significantly easier to work
with (it supports iteration).

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


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

Branch: refs/heads/master
Commit: f946e53a0ac0fbe924e055b05c124228a45e52cc
Parents: c422c97
Author: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Authored: Thu Feb 8 10:11:43 2018 -0800
Committer: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Committed: Wed Feb 21 14:00:11 2018 -0800

----------------------------------------------------------------------
 .../stout/include/stout/os/windows/shell.hpp    | 84 ++++++++++++++------
 1 file changed, 58 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/f946e53a/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 542039c..1696d08 100644
--- a/3rdparty/stout/include/stout/os/windows/shell.hpp
+++ b/3rdparty/stout/include/stout/os/windows/shell.hpp
@@ -17,24 +17,23 @@
 #include <stdarg.h> // For va_list, va_start, etc.
 
 #include <algorithm>
+#include <array>
 #include <map>
-#include <ostream>
 #include <string>
-#include <tuple>
 #include <vector>
 
 #include <stout/error.hpp>
 #include <stout/foreach.hpp>
 #include <stout/option.hpp>
 #include <stout/os.hpp>
-#include <stout/os/int_fd.hpp>
 #include <stout/try.hpp>
-#include <stout/unimplemented.hpp>
-
 #include <stout/windows.hpp>
 
-namespace internal {
+#include <stout/os/windows/fd.hpp>
+
+#include <stout/internal/windows/inherit.hpp>
 
+namespace internal {
 namespace windows {
 
 // Retrieves system environment in a `std::map`, ignoring
@@ -208,12 +207,14 @@ inline std::wstring stringify_args(const std::vector<std::string>& argv)
   return command;
 }
 
+
 struct ProcessData {
   SharedHandle process_handle;
   SharedHandle thread_handle;
   pid_t pid;
 };
 
+
 // Provides an interface for creating a child process on Windows.
 //
 // The `command` argument is given for compatibility, and is ignored. This is
@@ -227,6 +228,10 @@ struct ProcessData {
 // The caller can specify explicit `stdin`, `stdout`, and `stderr` handles,
 // in that order, for the process via the `pipes` argument.
 //
+// NOTE: If `pipes` are specified, they will be temporarily set to
+// inheritable, and then set to uninheritable. This is a side effect
+// on each `HANDLE`.
+//
 // The return value is a `ProcessData` struct, with the process and thread
 // handles each saved in a `SharedHandle`, ensuring they are closed when struct
 // goes out of scope.
@@ -235,10 +240,10 @@ inline Try<ProcessData> create_process(
     const std::vector<std::string>& argv,
     const Option<std::map<std::string, std::string>>& environment,
     const bool create_suspended = false,
-    const Option<std::tuple<int_fd, int_fd, int_fd>> pipes = None())
+    const Option<std::array<os::WindowsFD, 3>> pipes = None())
 {
   // TODO(andschwa): Assert that `command` and `argv[0]` are the same.
-  std::wstring arg_string = stringify_args(argv);
+  const std::wstring arg_string = stringify_args(argv);
   std::vector<wchar_t> arg_buffer(arg_string.begin(), arg_string.end());
   arg_buffer.push_back(L'\0');
 
@@ -249,7 +254,7 @@ inline Try<ProcessData> create_process(
   }
 
   // Construct the environment that will be passed to `::CreateProcessW`.
-  Option<std::wstring> env_string = create_process_env(environment);
+  const Option<std::wstring> env_string = create_process_env(environment);
   std::vector<wchar_t> env_buffer;
   if (env_string.isSome()) {
     // This string contains the necessary null characters.
@@ -258,11 +263,9 @@ inline Try<ProcessData> create_process(
 
   wchar_t* process_env = env_buffer.empty() ? nullptr : env_buffer.data();
 
-  PROCESS_INFORMATION process_info;
-  memset(&process_info, 0, sizeof(PROCESS_INFORMATION));
+  PROCESS_INFORMATION process_info = {};
 
-  STARTUPINFOW startup_info;
-  memset(&startup_info, 0, sizeof(STARTUPINFOW));
+  STARTUPINFOW startup_info = {};
   startup_info.cb = sizeof(STARTUPINFOW);
 
   // Hook up the stdin/out/err pipes and use the `STARTF_USESTDHANDLES`
@@ -272,42 +275,71 @@ inline Try<ProcessData> create_process(
   // [1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx
   // [2] https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
   if (pipes.isSome()) {
+    // Each of these handles must be inheritable.
+    foreach (const os::WindowsFD& fd, pipes.get()) {
+      const Try<Nothing> inherit = set_inherit(fd, true);
+      if (inherit.isError()) {
+        return Error(inherit.error());
+      }
+    }
+
+    startup_info.dwFlags |= STARTF_USESTDHANDLES;
     startup_info.hStdInput = std::get<0>(pipes.get());
     startup_info.hStdOutput = std::get<1>(pipes.get());
     startup_info.hStdError = std::get<2>(pipes.get());
-    startup_info.dwFlags |= STARTF_USESTDHANDLES;
   }
 
-  BOOL create_process_result = ::CreateProcessW(
+  const BOOL result = ::CreateProcessW(
       // This is replaced by the first token of `arg_buffer` string.
       static_cast<LPCWSTR>(nullptr),
       static_cast<LPWSTR>(arg_buffer.data()),
       static_cast<LPSECURITY_ATTRIBUTES>(nullptr),
       static_cast<LPSECURITY_ATTRIBUTES>(nullptr),
-      TRUE, // Inherited parent process handles.
+      TRUE, // Inherit parent process handles (such as those in `pipes`).
       creation_flags,
       static_cast<LPVOID>(process_env),
-      static_cast<LPCWSTR>(nullptr), // Inherited working directory.
+      static_cast<LPCWSTR>(nullptr), // Inherit working directory.
       &startup_info,
       &process_info);
 
-  if (!create_process_result) {
+  // NOTE: The MSDN documentation for `CreateProcess` states that it
+  // returns before the process has "finished initialization," but is
+  // not clear on precisely what initialization entails. It would seem
+  // that this does not affect inherited handles, as it stands to
+  // reason that the system call to `CreateProcess` causes inheritable
+  // handles to become inherited, and not some "initialization" of the
+  // child process. However, if an inheritance race condition
+  // manifests, this assumption should be re-evaluated.
+
+  if (pipes.isSome()) {
+    // These handles should no longer be inheritable. This prevents other child
+    // processes from accidentally inheriting the wrong handles.
+    //
+    // NOTE: This is explicit, and does not take into account the
+    // previous inheritance semantics of each `HANDLE`. It is assumed
+    // that users of this function send non-inheritable handles.
+    foreach (const os::WindowsFD& fd, pipes.get()) {
+      const Try<Nothing> inherit = set_inherit(fd, false);
+      if (inherit.isError()) {
+        return Error(inherit.error());
+      }
+    }
+  }
+
+  if (result == FALSE) {
     return WindowsError(
         "Failed to call `CreateProcess`: " + stringify(arg_string));
   }
 
-  return ProcessData{
-    SharedHandle{process_info.hProcess, ::CloseHandle},
-    SharedHandle{process_info.hThread, ::CloseHandle},
-    static_cast<pid_t>(process_info.dwProcessId)};
+  return ProcessData{SharedHandle{process_info.hProcess, ::CloseHandle},
+                     SharedHandle{process_info.hThread, ::CloseHandle},
+                     static_cast<pid_t>(process_info.dwProcessId)};
 }
 
 } // namespace windows {
-
 } // namespace internal {
 
 namespace os {
-
 namespace Shell {
 
 // Canonical constants used as platform-dependent args to `exec` calls.
@@ -346,11 +378,11 @@ inline int spawn(
 
   // Wait for the process synchronously.
   ::WaitForSingleObject(
-      process_data.get().process_handle.get_handle(), INFINITE);
+      process_data->process_handle.get_handle(), INFINITE);
 
   DWORD status;
   if (!::GetExitCodeProcess(
-           process_data.get().process_handle.get_handle(),
+           process_data->process_handle.get_handle(),
            &status)) {
     LOG(WARNING) << "Failed to `GetExitCodeProcess`: " << command;
     return -1;


[5/5] mesos git commit: Windows: Added `internal::windows::set_inherit(WindowsFD, bool)`.

Posted by an...@apache.org.
Windows: Added `internal::windows::set_inherit(WindowsFD, bool)`.

This function is used on Windows to explicitly enable or disable
inheritance on a handle.

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


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

Branch: refs/heads/master
Commit: c422c97fb6af049bdaa4214c46aac5b4f37bbcac
Parents: faba7df
Author: Akash Gupta <ak...@hotmail.com>
Authored: Thu Feb 1 14:34:05 2018 -0800
Committer: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Committed: Wed Feb 21 14:00:11 2018 -0800

----------------------------------------------------------------------
 .../include/stout/internal/windows/inherit.hpp  | 47 ++++++++++++++++++++
 1 file changed, 47 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/c422c97f/3rdparty/stout/include/stout/internal/windows/inherit.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/internal/windows/inherit.hpp b/3rdparty/stout/include/stout/internal/windows/inherit.hpp
new file mode 100644
index 0000000..6da6f8e
--- /dev/null
+++ b/3rdparty/stout/include/stout/internal/windows/inherit.hpp
@@ -0,0 +1,47 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//  http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef __STOUT_INTERNAL_WINDOWS_INHERIT_HPP__
+#define __STOUT_INTERNAL_WINDOWS_INHERIT_HPP__
+
+#include <stout/error.hpp>
+#include <stout/nothing.hpp>
+#include <stout/try.hpp>
+
+#include <stout/os/windows/fd.hpp>
+#include <stout/windows.hpp>
+
+
+namespace internal {
+namespace windows {
+
+// This function enables or disables inheritance for a Windows file handle.
+//
+// NOTE: By default, handles on Windows are not inheritable, so this is
+// primarily used to enable inheritance when passing handles to child processes,
+// and subsequently disable inheritance.
+inline Try<Nothing> set_inherit(const os::WindowsFD& fd, const bool inherit)
+{
+  const BOOL result = ::SetHandleInformation(
+      fd, HANDLE_FLAG_INHERIT, inherit ? HANDLE_FLAG_INHERIT : 0);
+
+  if (result == FALSE) {
+    return WindowsError();
+  }
+
+  return Nothing();
+}
+
+} // namespace windows {
+} // namespace internal {
+
+#endif // __STOUT_INTERNAL_WINDOWS_INHERIT_HPP__