Windows: Fixed `os::dup()` to use `DuplicateHandle()`.

Note that for now we need to keep the original CRT code, as it can't
be removed until `FD_CRT` is removed too.

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


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

Branch: refs/heads/master
Commit: fc2e3e99e601f0b1ca3290806955232d7544e08e
Parents: 37e2844
Author: Andrew Schwartzmeyer <[email protected]>
Authored: Mon Mar 19 13:39:08 2018 -0700
Committer: Andrew Schwartzmeyer <[email protected]>
Committed: Tue May 1 18:36:04 2018 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/stout/os/windows/dup.hpp | 24 ++++++++++++++++----
 1 file changed, 19 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/fc2e3e99/3rdparty/stout/include/stout/os/windows/dup.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/windows/dup.hpp 
b/3rdparty/stout/include/stout/os/windows/dup.hpp
index 75ef9d8..54b78b1 100644
--- a/3rdparty/stout/include/stout/os/windows/dup.hpp
+++ b/3rdparty/stout/include/stout/os/windows/dup.hpp
@@ -25,11 +25,8 @@ namespace os {
 inline Try<int_fd> dup(const int_fd& fd)
 {
   switch (fd.type()) {
-    case WindowsFD::FD_CRT:
-    case WindowsFD::FD_HANDLE: {
-      // TODO(andschwa): Replace this with `::DuplicateHandle` after figuring
-      // out how to make it compatible with handles to stdin/stdout/stderr, as
-      // well as defining sane inheritance semantics.
+    // TODO(andschwa): Remove this when `FD_CRT` is removed, MESOS-8675.
+    case WindowsFD::FD_CRT: {
       int result = ::_dup(fd.crt());
       if (result == -1) {
         return ErrnoError();
@@ -37,6 +34,23 @@ inline Try<int_fd> dup(const int_fd& fd)
 
       return result;
     }
+    case WindowsFD::FD_HANDLE: {
+      HANDLE duplicate = INVALID_HANDLE_VALUE;
+      const BOOL result = ::DuplicateHandle(
+          ::GetCurrentProcess(),  // Source process == current.
+          fd,                     // Handle to duplicate.
+          ::GetCurrentProcess(),  // Target process == current.
+          &duplicate,
+          0,                      // Ignored (DUPLICATE_SAME_ACCESS).
+          FALSE,                  // Non-inheritable handle.
+          DUPLICATE_SAME_ACCESS); // Same access level as source.
+
+      if (result == FALSE) {
+        return WindowsError();
+      }
+
+      return duplicate;
+    }
     case WindowsFD::FD_SOCKET: {
       WSAPROTOCOL_INFOW info;
       const int result =

Reply via email to