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/06/28 18:22:22 UTC

[08/16] mesos git commit: Organized POSIX and Windows libprocess implementations.

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/posix/poll_socket.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/posix/poll_socket.cpp b/3rdparty/libprocess/src/posix/poll_socket.cpp
new file mode 100644
index 0000000..74acb69
--- /dev/null
+++ b/3rdparty/libprocess/src/posix/poll_socket.cpp
@@ -0,0 +1,278 @@
+// 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
+
+
+#ifdef __WINDOWS__
+#include <stout/windows.hpp>
+#else
+#include <netinet/tcp.h>
+#endif // __WINDOWS__
+
+#include <process/io.hpp>
+#include <process/loop.hpp>
+#include <process/network.hpp>
+#include <process/socket.hpp>
+
+#include <stout/os/sendfile.hpp>
+#include <stout/os/strerror.hpp>
+#include <stout/os.hpp>
+
+#include "config.hpp"
+#include "poll_socket.hpp"
+
+using std::string;
+
+namespace process {
+namespace network {
+namespace internal {
+
+Try<std::shared_ptr<SocketImpl>> PollSocketImpl::create(int_fd s)
+{
+  return std::make_shared<PollSocketImpl>(s);
+}
+
+
+Try<Nothing> PollSocketImpl::listen(int backlog)
+{
+  if (::listen(get(), backlog) < 0) {
+    return ErrnoError();
+  }
+  return Nothing();
+}
+
+
+Future<std::shared_ptr<SocketImpl>> PollSocketImpl::accept()
+{
+  // Need to hold a copy of `this` so that the underlying socket
+  // doesn't end up getting reused before we return from the call to
+  // `io::poll` and end up accepting a socket incorrectly.
+  auto self = shared(this);
+
+  return io::poll(get(), io::READ)
+    .then([self]() -> Future<std::shared_ptr<SocketImpl>> {
+      Try<int_fd> accepted = network::accept(self->get());
+      if (accepted.isError()) {
+        return Failure(accepted.error());
+      }
+
+      int_fd s = accepted.get();
+      Try<Nothing> nonblock = os::nonblock(s);
+      if (nonblock.isError()) {
+        os::close(s);
+        return Failure("Failed to accept, nonblock: " + nonblock.error());
+      }
+
+      Try<Nothing> cloexec = os::cloexec(s);
+      if (cloexec.isError()) {
+        os::close(s);
+        return Failure("Failed to accept, cloexec: " + cloexec.error());
+      }
+
+      Try<Address> address = network::address(s);
+      if (address.isError()) {
+        os::close(s);
+        return Failure("Failed to get address: " + address.error());
+      }
+
+      // Turn off Nagle (TCP_NODELAY) so pipelined requests don't wait.
+      // NOTE: We cast to `char*` here because the function prototypes
+      // on Windows use `char*` instead of `void*`.
+      if (address->family() == Address::Family::INET4 ||
+          address->family() == Address::Family::INET6) {
+        int on = 1;
+        if (::setsockopt(
+                s,
+                SOL_TCP,
+                TCP_NODELAY,
+                reinterpret_cast<const char*>(&on),
+                sizeof(on)) < 0) {
+          const string error = os::strerror(errno);
+          os::close(s);
+          return Failure(
+              "Failed to turn off the Nagle algorithm: " + stringify(error));
+        }
+      }
+
+      Try<std::shared_ptr<SocketImpl>> impl = create(s);
+      if (impl.isError()) {
+        os::close(s);
+        return Failure("Failed to create socket: " + impl.error());
+      }
+
+      return impl.get();
+    });
+}
+
+
+Future<Nothing> PollSocketImpl::connect(const Address& address)
+{
+  Try<Nothing, SocketError> connect = network::connect(get(), address);
+  if (connect.isError()) {
+    if (net::is_inprogress_error(connect.error().code)) {
+      // Need to hold a copy of `this` so that the underlying socket
+      // doesn't end up getting reused before we return from the call
+      // to `io::poll` and end up connecting incorrectly.
+      auto self = shared(this);
+
+      return io::poll(get(), io::WRITE)
+        .then([self, address]() -> Future<Nothing> {
+          // Now check that a successful connection was made.
+          int opt;
+          socklen_t optlen = sizeof(opt);
+
+          // NOTE: We cast to `char*` here because the function
+          // prototypes on Windows use `char*` instead of `void*`.
+          if (::getsockopt(
+                  self->get(),
+                  SOL_SOCKET,
+                  SO_ERROR,
+                  reinterpret_cast<char*>(&opt),
+                  &optlen) < 0) {
+            return Failure(SocketError(
+                "Failed to get status of connect to " + stringify(address)));
+          }
+
+          if (opt != 0) {
+            return Failure(SocketError(
+                opt,
+                "Failed to connect to " +
+                stringify(address)));
+          }
+
+          return Nothing();
+        });
+    }
+
+    return Failure(connect.error());
+  }
+
+  return Nothing();
+}
+
+
+Future<size_t> PollSocketImpl::recv(char* data, size_t size)
+{
+  // Need to hold a copy of `this` so that the underlying socket
+  // doesn't end up getting reused before we return from the call to
+  // `io::read` and end up reading data incorrectly.
+  auto self = shared(this);
+
+  return io::read(get(), data, size)
+    .then([self](size_t length) {
+      return length;
+    });
+}
+
+
+Future<size_t> PollSocketImpl::send(const char* data, size_t size)
+{
+  CHECK(size > 0); // TODO(benh): Just return 0 if `size` is 0?
+
+  // Need to hold a copy of `this` so that the underlying socket
+  // doesn't end up getting reused before we return.
+  auto self = shared(this);
+
+  // TODO(benh): Reuse `io::write`? Or is `net::send` and
+  // `MSG_NOSIGNAL` critical here?
+  return loop(
+      None(),
+      [self, data, size]() -> Future<Option<size_t>> {
+        while (true) {
+          ssize_t length = net::send(self->get(), data, size, MSG_NOSIGNAL);
+
+          if (length < 0) {
+#ifdef __WINDOWS__
+            int error = WSAGetLastError();
+#else
+            int error = errno;
+#endif // __WINDOWS__
+
+            if (net::is_restartable_error(error)) {
+              // Interrupted, try again now.
+              continue;
+            } else if (!net::is_retryable_error(error)) {
+              // TODO(benh): Confirm that `os::strerror` does the
+              // right thing for `error` on Windows.
+              VLOG(1) << "Socket error while sending: " << os::strerror(error);
+              return Failure(os::strerror(error));
+            }
+
+            return None();
+          }
+
+          return length;
+        }
+      },
+      [self](const Option<size_t>& length) -> Future<ControlFlow<size_t>> {
+        // Retry after we've polled if we don't yet have a result.
+        if (length.isNone()) {
+          return io::poll(self->get(), io::WRITE)
+            .then([](short event) -> ControlFlow<size_t> {
+              CHECK_EQ(io::WRITE, event);
+              return Continue();
+            });
+        }
+        return Break(length.get());
+      });
+}
+
+
+Future<size_t> PollSocketImpl::sendfile(int_fd fd, off_t offset, size_t size)
+{
+  CHECK(size > 0); // TODO(benh): Just return 0 if `size` is 0?
+
+  // Need to hold a copy of `this` so that the underlying socket
+  // doesn't end up getting reused before we return.
+  auto self = shared(this);
+
+  return loop(
+      None(),
+      [self, fd, offset, size]() -> Future<Option<size_t>> {
+        while (true) {
+          Try<ssize_t, SocketError> length = os::sendfile(
+              self->get(),
+              fd,
+              offset,
+              size);
+
+          if (length.isSome()) {
+            CHECK(length.get() >= 0);
+            return length.get();
+          }
+
+          if (net::is_restartable_error(length.error().code)) {
+            // Interrupted, try again now.
+            continue;
+          } else if (!net::is_retryable_error(length.error().code)) {
+            VLOG(1) << length.error().message;
+            return Failure(length.error());
+          }
+
+          return None();
+        }
+      },
+      [self](const Option<size_t>& length) -> Future<ControlFlow<size_t>> {
+        // Retry after we've polled if we don't yet have a result.
+        if (length.isNone()) {
+          return io::poll(self->get(), io::WRITE)
+            .then([](short event) -> ControlFlow<size_t> {
+              CHECK_EQ(io::WRITE, event);
+              return Continue();
+            });
+        }
+        return Break(length.get());
+      });
+}
+
+} // namespace internal {
+} // namespace network {
+} // namespace process {

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/posix/subprocess.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/posix/subprocess.cpp b/3rdparty/libprocess/src/posix/subprocess.cpp
new file mode 100644
index 0000000..01e3272
--- /dev/null
+++ b/3rdparty/libprocess/src/posix/subprocess.cpp
@@ -0,0 +1,102 @@
+// 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
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <string>
+
+#include <glog/logging.h>
+
+#include <process/future.hpp>
+#include <process/reap.hpp>
+#include <process/subprocess.hpp>
+
+#include <stout/error.hpp>
+#include <stout/lambda.hpp>
+#include <stout/foreach.hpp>
+#include <stout/option.hpp>
+#include <stout/os.hpp>
+#include <stout/os/strerror.hpp>
+#include <stout/strings.hpp>
+#include <stout/try.hpp>
+
+using std::map;
+using std::string;
+using std::vector;
+
+namespace process {
+
+using InputFileDescriptors = Subprocess::IO::InputFileDescriptors;
+using OutputFileDescriptors = Subprocess::IO::OutputFileDescriptors;
+
+
+Subprocess::IO Subprocess::PIPE()
+{
+  return Subprocess::IO(
+      []() -> Try<InputFileDescriptors> {
+        int pipefd[2];
+        if (::pipe(pipefd) == -1) {
+          return ErrnoError("Failed to create pipe");
+        }
+
+        InputFileDescriptors fds;
+        fds.read = pipefd[0];
+        fds.write = pipefd[1];
+        return fds;
+      },
+      []() -> Try<OutputFileDescriptors> {
+        int pipefd[2];
+        if (::pipe(pipefd) == -1) {
+          return ErrnoError("Failed to create pipe");
+        }
+
+        OutputFileDescriptors fds;
+        fds.read = pipefd[0];
+        fds.write = pipefd[1];
+        return fds;
+      });
+}
+
+
+Subprocess::IO Subprocess::PATH(const string& path)
+{
+  return Subprocess::IO(
+      [path]() -> Try<InputFileDescriptors> {
+        Try<int> open = os::open(path, O_RDONLY | O_CLOEXEC);
+        if (open.isError()) {
+          return Error("Failed to open '" + path + "': " + open.error());
+        }
+
+        InputFileDescriptors fds;
+        fds.read = open.get();
+        return fds;
+      },
+      [path]() -> Try<OutputFileDescriptors> {
+        Try<int> open = os::open(
+            path,
+            O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC,
+            S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+        if (open.isError()) {
+          return Error("Failed to open '" + path + "': " + open.error());
+        }
+
+        OutputFileDescriptors fds;
+        fds.write = open.get();
+        return fds;
+      });
+}
+
+}  // namespace process {

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/posix/subprocess.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/posix/subprocess.hpp b/3rdparty/libprocess/src/posix/subprocess.hpp
new file mode 100644
index 0000000..007058b
--- /dev/null
+++ b/3rdparty/libprocess/src/posix/subprocess.hpp
@@ -0,0 +1,355 @@
+// 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 __PROCESS_POSIX_SUBPROCESS_HPP__
+#define __PROCESS_POSIX_SUBPROCESS_HPP__
+
+#ifdef __linux__
+#include <sys/prctl.h>
+#endif // __linux__
+#include <sys/types.h>
+
+#include <string>
+
+#include <glog/logging.h>
+
+#include <process/subprocess.hpp>
+
+#include <stout/check.hpp>
+#include <stout/error.hpp>
+#include <stout/exit.hpp>
+#include <stout/foreach.hpp>
+#include <stout/hashset.hpp>
+#include <stout/nothing.hpp>
+#include <stout/lambda.hpp>
+#include <stout/none.hpp>
+#include <stout/option.hpp>
+#include <stout/os.hpp>
+#include <stout/try.hpp>
+#include <stout/unreachable.hpp>
+
+#include <stout/os/close.hpp>
+#include <stout/os/environment.hpp>
+#include <stout/os/fcntl.hpp>
+#include <stout/os/signals.hpp>
+#include <stout/os/strerror.hpp>
+
+namespace process {
+namespace internal {
+
+static void close(std::initializer_list<int_fd> fds);
+
+
+static void close(
+    const Subprocess::IO::InputFileDescriptors& stdinfds,
+    const Subprocess::IO::OutputFileDescriptors& stdoutfds,
+    const Subprocess::IO::OutputFileDescriptors& stderrfds);
+
+
+inline pid_t defaultClone(const lambda::function<int()>& func)
+{
+  pid_t pid = ::fork();
+  if (pid == -1) {
+    return -1;
+  } else if (pid == 0) {
+    // Child.
+    ::exit(func());
+    UNREACHABLE();
+  } else {
+    // Parent.
+    return pid;
+  }
+}
+
+
+// This function will invoke `os::cloexec` on all specified file
+// descriptors that are valid (i.e., not `None` and >= 0).
+inline Try<Nothing> cloexec(
+    const InputFileDescriptors& stdinfds,
+    const OutputFileDescriptors& stdoutfds,
+    const OutputFileDescriptors& stderrfds)
+{
+  hashset<int> fds = {
+    stdinfds.read,
+    stdinfds.write.getOrElse(-1),
+    stdoutfds.read.getOrElse(-1),
+    stdoutfds.write,
+    stderrfds.read.getOrElse(-1),
+    stderrfds.write
+  };
+
+  foreach (int fd, fds) {
+    if (fd >= 0) {
+      Try<Nothing> cloexec = os::cloexec(fd);
+      if (cloexec.isError()) {
+        return Error(cloexec.error());
+      }
+    }
+  }
+
+  return Nothing();
+}
+
+
+// The main entry of the child process.
+//
+// NOTE: This function has to be async signal safe.
+inline int childMain(
+    const std::string& path,
+    char** argv,
+    char** envp,
+    const InputFileDescriptors& stdinfds,
+    const OutputFileDescriptors& stdoutfds,
+    const OutputFileDescriptors& stderrfds,
+    bool blocking,
+    int pipes[2],
+    const std::vector<Subprocess::ChildHook>& child_hooks)
+{
+  // Close parent's end of the pipes.
+  if (stdinfds.write.isSome()) {
+    ::close(stdinfds.write.get());
+  }
+  if (stdoutfds.read.isSome()) {
+    ::close(stdoutfds.read.get());
+  }
+  if (stderrfds.read.isSome()) {
+    ::close(stderrfds.read.get());
+  }
+
+  // Currently we will block the child's execution of the new process
+  // until all the parent hooks (if any) have executed.
+  if (blocking) {
+    ::close(pipes[1]);
+  }
+
+  // Redirect I/O for stdin/stdout/stderr.
+  while (::dup2(stdinfds.read, STDIN_FILENO) == -1 && errno == EINTR);
+  while (::dup2(stdoutfds.write, STDOUT_FILENO) == -1 && errno == EINTR);
+  while (::dup2(stderrfds.write, STDERR_FILENO) == -1 && errno == EINTR);
+
+  // Close the copies. We need to make sure that we do not close the
+  // file descriptor assigned to stdin/stdout/stderr in case the
+  // parent has closed stdin/stdout/stderr when calling this
+  // function (in that case, a dup'ed file descriptor may have the
+  // same file descriptor number as stdin/stdout/stderr).
+  //
+  // We also need to ensure that we don't "double close" any file
+  // descriptors in the case where one of stdinfds.read,
+  // stdoutfds.write, or stdoutfds.write are equal.
+  if (stdinfds.read != STDIN_FILENO &&
+      stdinfds.read != STDOUT_FILENO &&
+      stdinfds.read != STDERR_FILENO) {
+    ::close(stdinfds.read);
+  }
+  if (stdoutfds.write != STDIN_FILENO &&
+      stdoutfds.write != STDOUT_FILENO &&
+      stdoutfds.write != STDERR_FILENO &&
+      stdoutfds.write != stdinfds.read) {
+    ::close(stdoutfds.write);
+  }
+  if (stderrfds.write != STDIN_FILENO &&
+      stderrfds.write != STDOUT_FILENO &&
+      stderrfds.write != STDERR_FILENO &&
+      stderrfds.write != stdinfds.read &&
+      stderrfds.write != stdoutfds.write) {
+    ::close(stderrfds.write);
+  }
+
+  if (blocking) {
+    // Do a blocking read on the pipe until the parent signals us to
+    // continue.
+    char dummy;
+    ssize_t length;
+    while ((length = ::read(pipes[0], &dummy, sizeof(dummy))) == -1 &&
+          errno == EINTR);
+
+    if (length != sizeof(dummy)) {
+      ABORT("Failed to synchronize with parent");
+    }
+
+    // Now close the pipe as we don't need it anymore.
+    ::close(pipes[0]);
+  }
+
+  // Run the child hooks.
+  foreach (const Subprocess::ChildHook& hook, child_hooks) {
+    Try<Nothing> callback = hook();
+
+    // If the callback failed, we should abort execution.
+    if (callback.isError()) {
+      ABORT("Failed to execute Subprocess::ChildHook: " + callback.error());
+    }
+  }
+
+  os::execvpe(path.c_str(), argv, envp);
+
+  SAFE_EXIT(
+      errno, "Failed to os::execvpe on path '%s': %d", path.c_str(), errno);
+}
+
+
+inline Try<pid_t> cloneChild(
+    const std::string& path,
+    std::vector<std::string> argv,
+    const Option<std::map<std::string, std::string>>& environment,
+    const Option<lambda::function<
+        pid_t(const lambda::function<int()>&)>>& _clone,
+    const std::vector<Subprocess::ParentHook>& parent_hooks,
+    const std::vector<Subprocess::ChildHook>& child_hooks,
+    const InputFileDescriptors stdinfds,
+    const OutputFileDescriptors stdoutfds,
+    const OutputFileDescriptors stderrfds)
+{
+  // The real arguments that will be passed to 'os::execvpe'. We need
+  // to construct them here before doing the clone as it might not be
+  // async signal safe to perform the memory allocation.
+  char** _argv = new char*[argv.size() + 1];
+  for (size_t i = 0; i < argv.size(); i++) {
+    _argv[i] = (char*) argv[i].c_str();
+  }
+  _argv[argv.size()] = nullptr;
+
+  // Like above, we need to construct the environment that we'll pass
+  // to 'os::execvpe' as it might not be async-safe to perform the
+  // memory allocations.
+  char** envp = os::raw::environment();
+
+  if (environment.isSome()) {
+    // NOTE: We add 1 to the size for a `nullptr` terminator.
+    envp = new char*[environment->size() + 1];
+
+    size_t index = 0;
+    foreachpair (
+        const std::string& key,
+        const std::string& value, environment.get()) {
+      std::string entry = key + "=" + value;
+      envp[index] = new char[entry.size() + 1];
+      strncpy(envp[index], entry.c_str(), entry.size() + 1);
+      ++index;
+    }
+
+    envp[index] = nullptr;
+  }
+
+  // Determine the function to clone the child process. If the user
+  // does not specify the clone function, we will use the default.
+  lambda::function<pid_t(const lambda::function<int()>&)> clone =
+    (_clone.isSome() ? _clone.get() : defaultClone);
+
+  // Currently we will block the child's execution of the new process
+  // until all the `parent_hooks` (if any) have executed.
+  std::array<int, 2> pipes;
+  const bool blocking = !parent_hooks.empty();
+
+  if (blocking) {
+    // We assume this should not fail under reasonable conditions so we
+    // use CHECK.
+    Try<std::array<int, 2>> pipe = os::pipe();
+    CHECK_SOME(pipe);
+
+    pipes = pipe.get();
+  }
+
+  // Now, clone the child process.
+  pid_t pid = clone(lambda::bind(
+      &childMain,
+      path,
+      _argv,
+      envp,
+      stdinfds,
+      stdoutfds,
+      stderrfds,
+      blocking,
+      pipes.data(),
+      child_hooks));
+
+  delete[] _argv;
+
+  // Need to delete 'envp' if we had environment variables passed to
+  // us and we needed to allocate the space.
+  if (environment.isSome()) {
+    CHECK_NE(os::raw::environment(), envp);
+
+    // We ignore the last 'envp' entry since it is nullptr.
+    for (size_t index = 0; index < environment->size(); index++) {
+      delete[] envp[index];
+    }
+
+    delete[] envp;
+  }
+
+  if (pid == -1) {
+    // Save the errno as 'close' below might overwrite it.
+    ErrnoError error("Failed to clone");
+    internal::close(stdinfds, stdoutfds, stderrfds);
+
+    if (blocking) {
+      os::close(pipes[0]);
+      os::close(pipes[1]);
+    }
+
+    return error;
+  }
+
+  // Close the child-ends of the file descriptors that are created by
+  // this function.
+  internal::close({stdinfds.read, stdoutfds.write, stderrfds.write});
+
+  if (blocking) {
+    os::close(pipes[0]);
+
+    // Run the parent hooks.
+    foreach (const Subprocess::ParentHook& hook, parent_hooks) {
+      Try<Nothing> parentSetup = 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()) {
+        LOG(WARNING)
+          << "Failed to execute Subprocess::ParentHook in parent for child '"
+          << pid << "': " << parentSetup.error();
+
+        os::close(pipes[1]);
+
+        // Ensure the child is killed.
+        ::kill(pid, SIGKILL);
+
+        return Error(
+            "Failed to execute Subprocess::ParentHook in parent for child '" +
+            stringify(pid) + "': " + parentSetup.error());
+      }
+    }
+
+    // Now that we've executed the parent hooks, we can signal the child to
+    // continue by writing to the pipe.
+    char dummy;
+    ssize_t length;
+    while ((length = ::write(pipes[1], &dummy, sizeof(dummy))) == -1 &&
+           errno == EINTR);
+
+    os::close(pipes[1]);
+
+    if (length != sizeof(dummy)) {
+      // Ensure the child is killed.
+      ::kill(pid, SIGKILL);
+
+      return Error("Failed to synchronize child process");
+    }
+  }
+
+  return pid;
+}
+
+}  // namespace internal {
+}  // namespace process {
+
+#endif // __PROCESS_POSIX_SUBPROCESS_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/socket.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/socket.cpp b/3rdparty/libprocess/src/socket.cpp
index 3031cd8..606a1c4 100644
--- a/3rdparty/libprocess/src/socket.cpp
+++ b/3rdparty/libprocess/src/socket.cpp
@@ -27,7 +27,7 @@
 #include <stout/unreachable.hpp>
 
 #ifdef USE_SSL_SOCKET
-#include "libevent_ssl_socket.hpp"
+#include "posix/libevent/libevent_ssl_socket.hpp"
 #endif
 #include "poll_socket.hpp"
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/subprocess.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/subprocess.cpp b/3rdparty/libprocess/src/subprocess.cpp
index 0b2c02a..c0640de 100644
--- a/3rdparty/libprocess/src/subprocess.cpp
+++ b/3rdparty/libprocess/src/subprocess.cpp
@@ -47,9 +47,9 @@
 #include <stout/os/signals.hpp>
 
 #ifdef __WINDOWS__
-#include "subprocess_windows.hpp"
+#include "windows/subprocess.hpp"
 #else
-#include "subprocess_posix.hpp"
+#include "posix/subprocess.hpp"
 #endif // __WINDOWS__
 
 using std::map;

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/subprocess_posix.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/subprocess_posix.cpp b/3rdparty/libprocess/src/subprocess_posix.cpp
deleted file mode 100644
index 01e3272..0000000
--- a/3rdparty/libprocess/src/subprocess_posix.cpp
+++ /dev/null
@@ -1,102 +0,0 @@
-// 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
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <string>
-
-#include <glog/logging.h>
-
-#include <process/future.hpp>
-#include <process/reap.hpp>
-#include <process/subprocess.hpp>
-
-#include <stout/error.hpp>
-#include <stout/lambda.hpp>
-#include <stout/foreach.hpp>
-#include <stout/option.hpp>
-#include <stout/os.hpp>
-#include <stout/os/strerror.hpp>
-#include <stout/strings.hpp>
-#include <stout/try.hpp>
-
-using std::map;
-using std::string;
-using std::vector;
-
-namespace process {
-
-using InputFileDescriptors = Subprocess::IO::InputFileDescriptors;
-using OutputFileDescriptors = Subprocess::IO::OutputFileDescriptors;
-
-
-Subprocess::IO Subprocess::PIPE()
-{
-  return Subprocess::IO(
-      []() -> Try<InputFileDescriptors> {
-        int pipefd[2];
-        if (::pipe(pipefd) == -1) {
-          return ErrnoError("Failed to create pipe");
-        }
-
-        InputFileDescriptors fds;
-        fds.read = pipefd[0];
-        fds.write = pipefd[1];
-        return fds;
-      },
-      []() -> Try<OutputFileDescriptors> {
-        int pipefd[2];
-        if (::pipe(pipefd) == -1) {
-          return ErrnoError("Failed to create pipe");
-        }
-
-        OutputFileDescriptors fds;
-        fds.read = pipefd[0];
-        fds.write = pipefd[1];
-        return fds;
-      });
-}
-
-
-Subprocess::IO Subprocess::PATH(const string& path)
-{
-  return Subprocess::IO(
-      [path]() -> Try<InputFileDescriptors> {
-        Try<int> open = os::open(path, O_RDONLY | O_CLOEXEC);
-        if (open.isError()) {
-          return Error("Failed to open '" + path + "': " + open.error());
-        }
-
-        InputFileDescriptors fds;
-        fds.read = open.get();
-        return fds;
-      },
-      [path]() -> Try<OutputFileDescriptors> {
-        Try<int> open = os::open(
-            path,
-            O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC,
-            S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
-
-        if (open.isError()) {
-          return Error("Failed to open '" + path + "': " + open.error());
-        }
-
-        OutputFileDescriptors fds;
-        fds.write = open.get();
-        return fds;
-      });
-}
-
-}  // namespace process {

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/subprocess_posix.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/subprocess_posix.hpp b/3rdparty/libprocess/src/subprocess_posix.hpp
deleted file mode 100644
index 007058b..0000000
--- a/3rdparty/libprocess/src/subprocess_posix.hpp
+++ /dev/null
@@ -1,355 +0,0 @@
-// 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 __PROCESS_POSIX_SUBPROCESS_HPP__
-#define __PROCESS_POSIX_SUBPROCESS_HPP__
-
-#ifdef __linux__
-#include <sys/prctl.h>
-#endif // __linux__
-#include <sys/types.h>
-
-#include <string>
-
-#include <glog/logging.h>
-
-#include <process/subprocess.hpp>
-
-#include <stout/check.hpp>
-#include <stout/error.hpp>
-#include <stout/exit.hpp>
-#include <stout/foreach.hpp>
-#include <stout/hashset.hpp>
-#include <stout/nothing.hpp>
-#include <stout/lambda.hpp>
-#include <stout/none.hpp>
-#include <stout/option.hpp>
-#include <stout/os.hpp>
-#include <stout/try.hpp>
-#include <stout/unreachable.hpp>
-
-#include <stout/os/close.hpp>
-#include <stout/os/environment.hpp>
-#include <stout/os/fcntl.hpp>
-#include <stout/os/signals.hpp>
-#include <stout/os/strerror.hpp>
-
-namespace process {
-namespace internal {
-
-static void close(std::initializer_list<int_fd> fds);
-
-
-static void close(
-    const Subprocess::IO::InputFileDescriptors& stdinfds,
-    const Subprocess::IO::OutputFileDescriptors& stdoutfds,
-    const Subprocess::IO::OutputFileDescriptors& stderrfds);
-
-
-inline pid_t defaultClone(const lambda::function<int()>& func)
-{
-  pid_t pid = ::fork();
-  if (pid == -1) {
-    return -1;
-  } else if (pid == 0) {
-    // Child.
-    ::exit(func());
-    UNREACHABLE();
-  } else {
-    // Parent.
-    return pid;
-  }
-}
-
-
-// This function will invoke `os::cloexec` on all specified file
-// descriptors that are valid (i.e., not `None` and >= 0).
-inline Try<Nothing> cloexec(
-    const InputFileDescriptors& stdinfds,
-    const OutputFileDescriptors& stdoutfds,
-    const OutputFileDescriptors& stderrfds)
-{
-  hashset<int> fds = {
-    stdinfds.read,
-    stdinfds.write.getOrElse(-1),
-    stdoutfds.read.getOrElse(-1),
-    stdoutfds.write,
-    stderrfds.read.getOrElse(-1),
-    stderrfds.write
-  };
-
-  foreach (int fd, fds) {
-    if (fd >= 0) {
-      Try<Nothing> cloexec = os::cloexec(fd);
-      if (cloexec.isError()) {
-        return Error(cloexec.error());
-      }
-    }
-  }
-
-  return Nothing();
-}
-
-
-// The main entry of the child process.
-//
-// NOTE: This function has to be async signal safe.
-inline int childMain(
-    const std::string& path,
-    char** argv,
-    char** envp,
-    const InputFileDescriptors& stdinfds,
-    const OutputFileDescriptors& stdoutfds,
-    const OutputFileDescriptors& stderrfds,
-    bool blocking,
-    int pipes[2],
-    const std::vector<Subprocess::ChildHook>& child_hooks)
-{
-  // Close parent's end of the pipes.
-  if (stdinfds.write.isSome()) {
-    ::close(stdinfds.write.get());
-  }
-  if (stdoutfds.read.isSome()) {
-    ::close(stdoutfds.read.get());
-  }
-  if (stderrfds.read.isSome()) {
-    ::close(stderrfds.read.get());
-  }
-
-  // Currently we will block the child's execution of the new process
-  // until all the parent hooks (if any) have executed.
-  if (blocking) {
-    ::close(pipes[1]);
-  }
-
-  // Redirect I/O for stdin/stdout/stderr.
-  while (::dup2(stdinfds.read, STDIN_FILENO) == -1 && errno == EINTR);
-  while (::dup2(stdoutfds.write, STDOUT_FILENO) == -1 && errno == EINTR);
-  while (::dup2(stderrfds.write, STDERR_FILENO) == -1 && errno == EINTR);
-
-  // Close the copies. We need to make sure that we do not close the
-  // file descriptor assigned to stdin/stdout/stderr in case the
-  // parent has closed stdin/stdout/stderr when calling this
-  // function (in that case, a dup'ed file descriptor may have the
-  // same file descriptor number as stdin/stdout/stderr).
-  //
-  // We also need to ensure that we don't "double close" any file
-  // descriptors in the case where one of stdinfds.read,
-  // stdoutfds.write, or stdoutfds.write are equal.
-  if (stdinfds.read != STDIN_FILENO &&
-      stdinfds.read != STDOUT_FILENO &&
-      stdinfds.read != STDERR_FILENO) {
-    ::close(stdinfds.read);
-  }
-  if (stdoutfds.write != STDIN_FILENO &&
-      stdoutfds.write != STDOUT_FILENO &&
-      stdoutfds.write != STDERR_FILENO &&
-      stdoutfds.write != stdinfds.read) {
-    ::close(stdoutfds.write);
-  }
-  if (stderrfds.write != STDIN_FILENO &&
-      stderrfds.write != STDOUT_FILENO &&
-      stderrfds.write != STDERR_FILENO &&
-      stderrfds.write != stdinfds.read &&
-      stderrfds.write != stdoutfds.write) {
-    ::close(stderrfds.write);
-  }
-
-  if (blocking) {
-    // Do a blocking read on the pipe until the parent signals us to
-    // continue.
-    char dummy;
-    ssize_t length;
-    while ((length = ::read(pipes[0], &dummy, sizeof(dummy))) == -1 &&
-          errno == EINTR);
-
-    if (length != sizeof(dummy)) {
-      ABORT("Failed to synchronize with parent");
-    }
-
-    // Now close the pipe as we don't need it anymore.
-    ::close(pipes[0]);
-  }
-
-  // Run the child hooks.
-  foreach (const Subprocess::ChildHook& hook, child_hooks) {
-    Try<Nothing> callback = hook();
-
-    // If the callback failed, we should abort execution.
-    if (callback.isError()) {
-      ABORT("Failed to execute Subprocess::ChildHook: " + callback.error());
-    }
-  }
-
-  os::execvpe(path.c_str(), argv, envp);
-
-  SAFE_EXIT(
-      errno, "Failed to os::execvpe on path '%s': %d", path.c_str(), errno);
-}
-
-
-inline Try<pid_t> cloneChild(
-    const std::string& path,
-    std::vector<std::string> argv,
-    const Option<std::map<std::string, std::string>>& environment,
-    const Option<lambda::function<
-        pid_t(const lambda::function<int()>&)>>& _clone,
-    const std::vector<Subprocess::ParentHook>& parent_hooks,
-    const std::vector<Subprocess::ChildHook>& child_hooks,
-    const InputFileDescriptors stdinfds,
-    const OutputFileDescriptors stdoutfds,
-    const OutputFileDescriptors stderrfds)
-{
-  // The real arguments that will be passed to 'os::execvpe'. We need
-  // to construct them here before doing the clone as it might not be
-  // async signal safe to perform the memory allocation.
-  char** _argv = new char*[argv.size() + 1];
-  for (size_t i = 0; i < argv.size(); i++) {
-    _argv[i] = (char*) argv[i].c_str();
-  }
-  _argv[argv.size()] = nullptr;
-
-  // Like above, we need to construct the environment that we'll pass
-  // to 'os::execvpe' as it might not be async-safe to perform the
-  // memory allocations.
-  char** envp = os::raw::environment();
-
-  if (environment.isSome()) {
-    // NOTE: We add 1 to the size for a `nullptr` terminator.
-    envp = new char*[environment->size() + 1];
-
-    size_t index = 0;
-    foreachpair (
-        const std::string& key,
-        const std::string& value, environment.get()) {
-      std::string entry = key + "=" + value;
-      envp[index] = new char[entry.size() + 1];
-      strncpy(envp[index], entry.c_str(), entry.size() + 1);
-      ++index;
-    }
-
-    envp[index] = nullptr;
-  }
-
-  // Determine the function to clone the child process. If the user
-  // does not specify the clone function, we will use the default.
-  lambda::function<pid_t(const lambda::function<int()>&)> clone =
-    (_clone.isSome() ? _clone.get() : defaultClone);
-
-  // Currently we will block the child's execution of the new process
-  // until all the `parent_hooks` (if any) have executed.
-  std::array<int, 2> pipes;
-  const bool blocking = !parent_hooks.empty();
-
-  if (blocking) {
-    // We assume this should not fail under reasonable conditions so we
-    // use CHECK.
-    Try<std::array<int, 2>> pipe = os::pipe();
-    CHECK_SOME(pipe);
-
-    pipes = pipe.get();
-  }
-
-  // Now, clone the child process.
-  pid_t pid = clone(lambda::bind(
-      &childMain,
-      path,
-      _argv,
-      envp,
-      stdinfds,
-      stdoutfds,
-      stderrfds,
-      blocking,
-      pipes.data(),
-      child_hooks));
-
-  delete[] _argv;
-
-  // Need to delete 'envp' if we had environment variables passed to
-  // us and we needed to allocate the space.
-  if (environment.isSome()) {
-    CHECK_NE(os::raw::environment(), envp);
-
-    // We ignore the last 'envp' entry since it is nullptr.
-    for (size_t index = 0; index < environment->size(); index++) {
-      delete[] envp[index];
-    }
-
-    delete[] envp;
-  }
-
-  if (pid == -1) {
-    // Save the errno as 'close' below might overwrite it.
-    ErrnoError error("Failed to clone");
-    internal::close(stdinfds, stdoutfds, stderrfds);
-
-    if (blocking) {
-      os::close(pipes[0]);
-      os::close(pipes[1]);
-    }
-
-    return error;
-  }
-
-  // Close the child-ends of the file descriptors that are created by
-  // this function.
-  internal::close({stdinfds.read, stdoutfds.write, stderrfds.write});
-
-  if (blocking) {
-    os::close(pipes[0]);
-
-    // Run the parent hooks.
-    foreach (const Subprocess::ParentHook& hook, parent_hooks) {
-      Try<Nothing> parentSetup = 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()) {
-        LOG(WARNING)
-          << "Failed to execute Subprocess::ParentHook in parent for child '"
-          << pid << "': " << parentSetup.error();
-
-        os::close(pipes[1]);
-
-        // Ensure the child is killed.
-        ::kill(pid, SIGKILL);
-
-        return Error(
-            "Failed to execute Subprocess::ParentHook in parent for child '" +
-            stringify(pid) + "': " + parentSetup.error());
-      }
-    }
-
-    // Now that we've executed the parent hooks, we can signal the child to
-    // continue by writing to the pipe.
-    char dummy;
-    ssize_t length;
-    while ((length = ::write(pipes[1], &dummy, sizeof(dummy))) == -1 &&
-           errno == EINTR);
-
-    os::close(pipes[1]);
-
-    if (length != sizeof(dummy)) {
-      // Ensure the child is killed.
-      ::kill(pid, SIGKILL);
-
-      return Error("Failed to synchronize child process");
-    }
-  }
-
-  return pid;
-}
-
-}  // namespace internal {
-}  // namespace process {
-
-#endif // __PROCESS_POSIX_SUBPROCESS_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/subprocess_windows.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/subprocess_windows.cpp b/3rdparty/libprocess/src/subprocess_windows.cpp
deleted file mode 100644
index c497a03..0000000
--- a/3rdparty/libprocess/src/subprocess_windows.cpp
+++ /dev/null
@@ -1,101 +0,0 @@
-// 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
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-
-#include <string>
-
-#include <glog/logging.h>
-
-#include <process/future.hpp>
-#include <process/reap.hpp>
-#include <process/subprocess.hpp>
-
-#include <stout/error.hpp>
-#include <stout/foreach.hpp>
-#include <stout/lambda.hpp>
-#include <stout/option.hpp>
-#include <stout/os.hpp>
-#include <stout/strings.hpp>
-#include <stout/try.hpp>
-#include <stout/windows.hpp>
-
-#include <stout/os/int_fd.hpp>
-#include <stout/os/open.hpp>
-#include <stout/os/pipe.hpp>
-#include <stout/os/strerror.hpp>
-
-using std::array;
-using std::string;
-
-namespace process {
-
-using InputFileDescriptors = Subprocess::IO::InputFileDescriptors;
-using OutputFileDescriptors = Subprocess::IO::OutputFileDescriptors;
-
-// Opens a pair of file handles. On success, the first handle returned receives
-// the 'read' handle of the pipe, while the second receives the 'write' handle.
-// The pipe handles can then be passed to a child process, as shown in [1].
-//
-// [1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
-Subprocess::IO Subprocess::PIPE()
-{
-  return Subprocess::IO(
-      []() -> Try<InputFileDescriptors> {
-        // Create STDIN pipe and set the 'read' component to be not overlapped,
-        // because we're sending it to the child process.
-        const Try<array<int_fd, 2>> pipefd = os::pipe(false, true);
-        if (pipefd.isError()) {
-          return Error(pipefd.error());
-        }
-
-        return InputFileDescriptors{pipefd.get()[0], pipefd.get()[1]};
-      },
-      []() -> Try<OutputFileDescriptors> {
-        // Create STDOUT pipe and set the 'write' component to be not
-        // overlapped, because we're sending it to the child process.
-        const Try<array<int_fd, 2>> pipefd = os::pipe(true, false);
-        if (pipefd.isError()) {
-          return Error(pipefd.error());
-        }
-
-        return OutputFileDescriptors{pipefd.get()[0], pipefd.get()[1]};
-      });
-}
-
-
-Subprocess::IO Subprocess::PATH(const string& path)
-{
-  return Subprocess::IO(
-      [path]() -> Try<InputFileDescriptors> {
-        const Try<int_fd> open = os::open(path, O_RDONLY);
-
-        if (open.isError()) {
-          return Error(open.error());
-        }
-
-        return InputFileDescriptors{open.get(), None()};
-      },
-      [path]() -> Try<OutputFileDescriptors> {
-        const Try<int_fd> open = os::open(path, O_WRONLY | O_CREAT | O_APPEND);
-
-        if (open.isError()) {
-          return Error(open.error());
-        }
-
-        return OutputFileDescriptors{None(), open.get()};
-      });
-}
-
-}  // namespace process {

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/subprocess_windows.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/subprocess_windows.hpp b/3rdparty/libprocess/src/subprocess_windows.hpp
deleted file mode 100644
index 1bbb8af..0000000
--- a/3rdparty/libprocess/src/subprocess_windows.hpp
+++ /dev/null
@@ -1,118 +0,0 @@
-// 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 __PROCESS_WINDOWS_SUBPROCESS_HPP__
-#define __PROCESS_WINDOWS_SUBPROCESS_HPP__
-
-#include <signal.h>
-
-#include <array>
-#include <string>
-
-#include <glog/logging.h>
-
-#include <process/subprocess.hpp>
-
-#include <stout/error.hpp>
-#include <stout/foreach.hpp>
-#include <stout/hashset.hpp>
-#include <stout/option.hpp>
-#include <stout/os.hpp>
-#include <stout/os/shell.hpp>
-#include <stout/try.hpp>
-#include <stout/windows.hpp>
-
-#include <stout/os/close.hpp>
-#include <stout/os/environment.hpp>
-
-#include <userEnv.h>
-
-namespace process {
-namespace internal {
-
-// NOTE: We are expecting that components of `argv` that need to be quoted
-// (for example, paths with spaces in them like `C:\"Program Files"\foo.exe`)
-// to have been already quoted correctly before we generate `command`.
-// Incorrectly-quoted command arguments will probably lead the child process
-// to terminate with an error. See also NOTE on `process::subprocess`.
-inline Try<::internal::windows::ProcessData> createChildProcess(
-    const std::string& path,
-    const std::vector<std::string>& argv,
-    const Option<std::map<std::string, std::string>>& environment,
-    const std::vector<Subprocess::ParentHook>& parent_hooks,
-    const InputFileDescriptors& stdinfds,
-    const OutputFileDescriptors& stdoutfds,
-    const OutputFileDescriptors& stderrfds,
-    const std::vector<int_fd>& whitelist_fds = {})
-{
-  const std::array<int_fd, 3> fds{
-    stdinfds.read, stdoutfds.write, stderrfds.write};
-
-  Try<::internal::windows::ProcessData> process_data =
-    ::internal::windows::create_process(
-        path,
-        argv,
-        environment,
-        true, // Create suspended.
-        fds,
-        whitelist_fds);
-
-  // Close the child-ends of the file descriptors that are created
-  // by this function.
-  foreach (const int_fd& fd, fds) {
-    if (fd.is_valid()) {
-      Try<Nothing> result = os::close(fd);
-      if (result.isError()) {
-        return Error(result.error());
-      }
-    }
-  }
-
-  if (process_data.isError()) {
-    return Error(process_data.error());
-  }
-
-  // Run the parent hooks.
-  const pid_t pid = process_data->pid;
-  foreach (const Subprocess::ParentHook& hook, parent_hooks) {
-    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 (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->process_handle.get_handle(), 1);
-
-      return Error(
-          "Failed to execute Parent Hook in child '" + stringify(pid) +
-          "' with command '" + stringify(argv) + "': " +
-          parent_setup.error());
-    }
-  }
-
-  // Start child process.
-  if (::ResumeThread(process_data->thread_handle.get_handle()) == -1) {
-    return WindowsError(
-        "Failed to resume child process with command '" +
-        stringify(argv) + "'");
-  }
-
-  return process_data;
-}
-
-}  // namespace internal {
-}  // namespace process {
-
-#endif // __PROCESS_WINDOWS_SUBPROCESS_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/windows/subprocess.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/windows/subprocess.cpp b/3rdparty/libprocess/src/windows/subprocess.cpp
new file mode 100644
index 0000000..c497a03
--- /dev/null
+++ b/3rdparty/libprocess/src/windows/subprocess.cpp
@@ -0,0 +1,101 @@
+// 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
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+#include <string>
+
+#include <glog/logging.h>
+
+#include <process/future.hpp>
+#include <process/reap.hpp>
+#include <process/subprocess.hpp>
+
+#include <stout/error.hpp>
+#include <stout/foreach.hpp>
+#include <stout/lambda.hpp>
+#include <stout/option.hpp>
+#include <stout/os.hpp>
+#include <stout/strings.hpp>
+#include <stout/try.hpp>
+#include <stout/windows.hpp>
+
+#include <stout/os/int_fd.hpp>
+#include <stout/os/open.hpp>
+#include <stout/os/pipe.hpp>
+#include <stout/os/strerror.hpp>
+
+using std::array;
+using std::string;
+
+namespace process {
+
+using InputFileDescriptors = Subprocess::IO::InputFileDescriptors;
+using OutputFileDescriptors = Subprocess::IO::OutputFileDescriptors;
+
+// Opens a pair of file handles. On success, the first handle returned receives
+// the 'read' handle of the pipe, while the second receives the 'write' handle.
+// The pipe handles can then be passed to a child process, as shown in [1].
+//
+// [1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
+Subprocess::IO Subprocess::PIPE()
+{
+  return Subprocess::IO(
+      []() -> Try<InputFileDescriptors> {
+        // Create STDIN pipe and set the 'read' component to be not overlapped,
+        // because we're sending it to the child process.
+        const Try<array<int_fd, 2>> pipefd = os::pipe(false, true);
+        if (pipefd.isError()) {
+          return Error(pipefd.error());
+        }
+
+        return InputFileDescriptors{pipefd.get()[0], pipefd.get()[1]};
+      },
+      []() -> Try<OutputFileDescriptors> {
+        // Create STDOUT pipe and set the 'write' component to be not
+        // overlapped, because we're sending it to the child process.
+        const Try<array<int_fd, 2>> pipefd = os::pipe(true, false);
+        if (pipefd.isError()) {
+          return Error(pipefd.error());
+        }
+
+        return OutputFileDescriptors{pipefd.get()[0], pipefd.get()[1]};
+      });
+}
+
+
+Subprocess::IO Subprocess::PATH(const string& path)
+{
+  return Subprocess::IO(
+      [path]() -> Try<InputFileDescriptors> {
+        const Try<int_fd> open = os::open(path, O_RDONLY);
+
+        if (open.isError()) {
+          return Error(open.error());
+        }
+
+        return InputFileDescriptors{open.get(), None()};
+      },
+      [path]() -> Try<OutputFileDescriptors> {
+        const Try<int_fd> open = os::open(path, O_WRONLY | O_CREAT | O_APPEND);
+
+        if (open.isError()) {
+          return Error(open.error());
+        }
+
+        return OutputFileDescriptors{None(), open.get()};
+      });
+}
+
+}  // namespace process {

http://git-wip-us.apache.org/repos/asf/mesos/blob/78244b53/3rdparty/libprocess/src/windows/subprocess.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/windows/subprocess.hpp b/3rdparty/libprocess/src/windows/subprocess.hpp
new file mode 100644
index 0000000..1bbb8af
--- /dev/null
+++ b/3rdparty/libprocess/src/windows/subprocess.hpp
@@ -0,0 +1,118 @@
+// 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 __PROCESS_WINDOWS_SUBPROCESS_HPP__
+#define __PROCESS_WINDOWS_SUBPROCESS_HPP__
+
+#include <signal.h>
+
+#include <array>
+#include <string>
+
+#include <glog/logging.h>
+
+#include <process/subprocess.hpp>
+
+#include <stout/error.hpp>
+#include <stout/foreach.hpp>
+#include <stout/hashset.hpp>
+#include <stout/option.hpp>
+#include <stout/os.hpp>
+#include <stout/os/shell.hpp>
+#include <stout/try.hpp>
+#include <stout/windows.hpp>
+
+#include <stout/os/close.hpp>
+#include <stout/os/environment.hpp>
+
+#include <userEnv.h>
+
+namespace process {
+namespace internal {
+
+// NOTE: We are expecting that components of `argv` that need to be quoted
+// (for example, paths with spaces in them like `C:\"Program Files"\foo.exe`)
+// to have been already quoted correctly before we generate `command`.
+// Incorrectly-quoted command arguments will probably lead the child process
+// to terminate with an error. See also NOTE on `process::subprocess`.
+inline Try<::internal::windows::ProcessData> createChildProcess(
+    const std::string& path,
+    const std::vector<std::string>& argv,
+    const Option<std::map<std::string, std::string>>& environment,
+    const std::vector<Subprocess::ParentHook>& parent_hooks,
+    const InputFileDescriptors& stdinfds,
+    const OutputFileDescriptors& stdoutfds,
+    const OutputFileDescriptors& stderrfds,
+    const std::vector<int_fd>& whitelist_fds = {})
+{
+  const std::array<int_fd, 3> fds{
+    stdinfds.read, stdoutfds.write, stderrfds.write};
+
+  Try<::internal::windows::ProcessData> process_data =
+    ::internal::windows::create_process(
+        path,
+        argv,
+        environment,
+        true, // Create suspended.
+        fds,
+        whitelist_fds);
+
+  // Close the child-ends of the file descriptors that are created
+  // by this function.
+  foreach (const int_fd& fd, fds) {
+    if (fd.is_valid()) {
+      Try<Nothing> result = os::close(fd);
+      if (result.isError()) {
+        return Error(result.error());
+      }
+    }
+  }
+
+  if (process_data.isError()) {
+    return Error(process_data.error());
+  }
+
+  // Run the parent hooks.
+  const pid_t pid = process_data->pid;
+  foreach (const Subprocess::ParentHook& hook, parent_hooks) {
+    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 (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->process_handle.get_handle(), 1);
+
+      return Error(
+          "Failed to execute Parent Hook in child '" + stringify(pid) +
+          "' with command '" + stringify(argv) + "': " +
+          parent_setup.error());
+    }
+  }
+
+  // Start child process.
+  if (::ResumeThread(process_data->thread_handle.get_handle()) == -1) {
+    return WindowsError(
+        "Failed to resume child process with command '" +
+        stringify(argv) + "'");
+  }
+
+  return process_data;
+}
+
+}  // namespace internal {
+}  // namespace process {
+
+#endif // __PROCESS_WINDOWS_SUBPROCESS_HPP__