You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by jo...@apache.org on 2016/06/15 17:07:21 UTC
[3/7] mesos git commit: Agent: Split executor process launching logic
into Posix and Windows.
Agent: Split executor process launching logic into Posix and Windows.
Review: https://reviews.apache.org/r/47603/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/e8219784
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/e8219784
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/e8219784
Branch: refs/heads/master
Commit: e8219784b19e57afad301666451d4817f8e335c9
Parents: 4e22c2f
Author: Alex Clemmer <cl...@gmail.com>
Authored: Wed Jun 15 10:02:00 2016 +0200
Committer: Joris Van Remoortere <jo...@gmail.com>
Committed: Wed Jun 15 19:06:59 2016 +0200
----------------------------------------------------------------------
src/Makefile.am | 6 +-
src/exec/exec.cpp | 13 +-
src/launcher/executor.cpp | 248 +++++++++------------------------
src/launcher/executor.hpp | 29 ++++
src/launcher/posix/executor.cpp | 247 ++++++++++++++++++++++++++++++++
src/launcher/posix/executor.hpp | 44 ++++++
src/launcher/windows/executor.cpp | 115 +++++++++++++++
src/launcher/windows/executor.hpp | 41 ++++++
8 files changed, 555 insertions(+), 188 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/e8219784/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index f672fc9..45662cd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -879,6 +879,8 @@ libmesos_no_3rdparty_la_SOURCES += \
hook/manager.hpp \
internal/devolve.hpp \
internal/evolve.hpp \
+ launcher/posix/flags.hpp \
+ launcher/windows/flags.hpp \
local/flags.hpp \
local/local.hpp \
logging/flags.hpp \
@@ -1291,7 +1293,9 @@ mesos_fetcher_CPPFLAGS = $(MESOS_CPPFLAGS)
mesos_fetcher_LDADD = libmesos.la $(LDADD)
pkglibexec_PROGRAMS += mesos-executor
-mesos_executor_SOURCES = launcher/executor.cpp
+mesos_executor_SOURCES = \
+ launcher/executor.cpp \
+ launcher/posix/executor.cpp
mesos_executor_CPPFLAGS = $(MESOS_CPPFLAGS)
mesos_executor_LDADD = libmesos.la $(LDADD)
http://git-wip-us.apache.org/repos/asf/mesos/blob/e8219784/src/exec/exec.cpp
----------------------------------------------------------------------
diff --git a/src/exec/exec.cpp b/src/exec/exec.cpp
index 15ebb74..f78e8b6 100644
--- a/src/exec/exec.cpp
+++ b/src/exec/exec.cpp
@@ -68,6 +68,7 @@ using std::string;
using process::Latch;
using process::wait; // Necessary on some OS's to disambiguate.
+using mesos::Executor; // Necessary on some OS's to disambiguate.
namespace mesos {
namespace internal {
@@ -99,7 +100,17 @@ protected:
// TODO(vinod): Invoke killtree without killing ourselves.
// Kill the process group (including ourself).
+#ifndef __WINDOWS__
killpg(0, SIGKILL);
+#else
+ LOG(WARNING) << "Shutting down process group. Windows does not support "
+ "`killpg`, so we simply call `exit` on the assumption "
+ "that the process was generated with the "
+ "`WindowsContainerizer`, which uses the 'close on exit' "
+ "feature of job objects to make sure all child processes "
+ "are killed when a parent process exits";
+ exit(0);
+#endif // __WINDOWS__
// The signal might not get delivered immediately, so sleep for a
// few seconds. Worst case scenario, exit abnormally.
@@ -586,7 +597,7 @@ private:
// Implementation of C++ API.
-MesosExecutorDriver::MesosExecutorDriver(Executor* _executor)
+MesosExecutorDriver::MesosExecutorDriver(mesos::Executor* _executor)
: executor(_executor),
process(nullptr),
status(DRIVER_NOT_STARTED)
http://git-wip-us.apache.org/repos/asf/mesos/blob/e8219784/src/launcher/executor.cpp
----------------------------------------------------------------------
diff --git a/src/launcher/executor.cpp b/src/launcher/executor.cpp
index 346aa30..04fc4d1 100644
--- a/src/launcher/executor.cpp
+++ b/src/launcher/executor.cpp
@@ -17,7 +17,9 @@
#include <signal.h>
#include <stdio.h>
+#ifndef __WINDOWS__
#include <sys/wait.h>
+#endif // __WINDOWS__
#include <iostream>
#include <list>
@@ -41,6 +43,10 @@
#include <process/time.hpp>
#include <process/timer.hpp>
+#ifdef __WINDOWS__
+#include <process/windows/winsock.hpp> // WSAStartup code.
+#endif // __WINDOWS__
+
#include <stout/duration.hpp>
#include <stout/flags.hpp>
#include <stout/json.hpp>
@@ -51,6 +57,12 @@
#include <stout/path.hpp>
#include <stout/protobuf.hpp>
#include <stout/strings.hpp>
+#ifdef __WINDOWS__
+#include <stout/windows.hpp>
+#endif // __WINDOWS__
+
+#include <stout/os/kill.hpp>
+#include <stout/os/killtree.hpp>
#include "common/http.hpp"
#include "common/status_utils.hpp"
@@ -63,6 +75,8 @@
#include "executor/v0_v1executor.hpp"
+#include "launcher/executor.hpp"
+
#include "logging/logging.hpp"
#include "messages/messages.hpp"
@@ -101,6 +115,7 @@ using mesos::v1::executor::Mesos;
using mesos::v1::executor::MesosBase;
using mesos::v1::executor::V0ToV1Adapter;
+
namespace mesos {
namespace v1 {
namespace internal {
@@ -138,9 +153,21 @@ public:
taskCommand(_taskCommand),
frameworkId(_frameworkId),
executorId(_executorId),
- task(None()) {}
+ task(None())
+ {
+#ifdef __WINDOWS__
+ process_handle = INVALID_HANDLE_VALUE;
+#endif
+ }
- virtual ~CommandExecutor() = default;
+ virtual ~CommandExecutor()
+ {
+#ifdef __WINDOWS__
+ if (process_handle != INVALID_HANDLE_VALUE) {
+ ::CloseHandle(process_handle);
+ }
+#endif // __WINDOWS__
+ }
void connected()
{
@@ -361,51 +388,6 @@ protected:
cout << "Starting task " << task->task_id() << endl;
- // TODO(benh): Clean this up with the new 'Fork' abstraction.
- // Use pipes to determine which child has successfully changed
- // session. This is needed as the setsid call can fail from other
- // processes having the same group id.
- int pipes[2];
- if (pipe(pipes) < 0) {
- perror("Failed to create a pipe");
- abort();
- }
-
- // Set the FD_CLOEXEC flags on these pipes.
- Try<Nothing> cloexec = os::cloexec(pipes[0]);
- if (cloexec.isError()) {
- cerr << "Failed to cloexec(pipe[0]): " << cloexec.error() << endl;
- abort();
- }
-
- cloexec = os::cloexec(pipes[1]);
- if (cloexec.isError()) {
- cerr << "Failed to cloexec(pipe[1]): " << cloexec.error() << endl;
- abort();
- }
-
- if (rootfs.isSome()) {
- // The command executor is responsible for chrooting into the
- // root filesystem and changing the user before exec-ing the
- // user process.
-#ifdef __linux__
- Result<string> user = os::user();
- if (user.isError()) {
- cerr << "Failed to get current user: " << user.error() << endl;
- abort();
- } else if (user.isNone()) {
- cerr << "Current username is not found" << endl;
- abort();
- } else if (user.get() != "root") {
- cerr << "The command executor requires root with rootfs" << endl;
- abort();
- }
-#else
- cerr << "Not expecting root volume with non-linux platform." << endl;
- abort();
-#endif // __linux__
- }
-
// Prepare the argv before fork as it's not async signal safe.
char **argv = new char*[command.arguments().size() + 1];
for (int i = 0; i < command.arguments().size(); i++) {
@@ -413,148 +395,35 @@ protected:
}
argv[command.arguments().size()] = nullptr;
- // Prepare the command log message.
- string commandString;
- if (override.isSome()) {
- char** argv = override.get();
- // argv is guaranteed to be nullptr terminated and we rely on
- // that fact to print command to be executed.
- for (int i = 0; argv[i] != nullptr; i++) {
- commandString += string(argv[i]) + " ";
- }
- } else if (command.shell()) {
- commandString = "sh -c '" + command.value() + "'";
- } else {
- commandString =
- "[" + command.value() + ", " +
- strings::join(", ", command.arguments()) + "]";
- }
-
- if ((pid = fork()) == -1) {
- cerr << "Failed to fork to run " << commandString << ": "
- << os::strerror(errno) << endl;
- abort();
- }
-
- // TODO(jieyu): Make the child process async signal safe.
- if (pid == 0) {
- // In child process, we make cleanup easier by putting process
- // into it's own session.
- os::close(pipes[0]);
-
- // NOTE: We setsid() in a loop because setsid() might fail if another
- // process has the same process group id as the calling process.
- while ((pid = setsid()) == -1) {
- perror("Could not put command in its own session, setsid");
-
- cout << "Forking another process and retrying" << endl;
-
- if ((pid = fork()) == -1) {
- perror("Failed to fork to launch command");
- abort();
- }
-
- if (pid > 0) {
- // In parent process. It is ok to suicide here, because
- // we're not watching this process.
- exit(0);
- }
- }
-
- if (write(pipes[1], &pid, sizeof(pid)) != sizeof(pid)) {
- perror("Failed to write PID on pipe");
- abort();
- }
-
- os::close(pipes[1]);
-
- if (rootfs.isSome()) {
-#ifdef __linux__
- if (user.isSome()) {
- // This is a work around to fix the problem that after we chroot
- // os::su call afterwards failed because the linker may not be
- // able to find the necessary library in the rootfs.
- // We call os::su before chroot here to force the linker to load
- // into memory.
- // We also assume it's safe to su to "root" user since
- // filesystem/linux.cpp checks for root already.
- os::su("root");
- }
-
- Try<Nothing> chroot = fs::chroot::enter(rootfs.get());
- if (chroot.isError()) {
- cerr << "Failed to enter chroot '" << rootfs.get()
- << "': " << chroot.error() << endl;;
- abort();
- }
-
- // Determine the current working directory for the executor.
- string cwd;
- if (workingDirectory.isSome()) {
- cwd = workingDirectory.get();
- } else {
- CHECK_SOME(sandboxDirectory);
- cwd = sandboxDirectory.get();
- }
-
- Try<Nothing> chdir = os::chdir(cwd);
- if (chdir.isError()) {
- cerr << "Failed to chdir into current working directory '"
- << cwd << "': " << chdir.error() << endl;
- abort();
- }
-
- if (user.isSome()) {
- Try<Nothing> su = os::su(user.get());
- if (su.isError()) {
- cerr << "Failed to change user to '" << user.get() << "': "
- << su.error() << endl;
- abort();
- }
- }
+#ifndef __WINDOWS__
+ pid = launchTaskPosix(
+ task.get(),
+ command,
+ user,
+ argv,
+ override,
+ rootfs,
+ sandboxDirectory,
+ workingDirectory);
#else
- cerr << "Rootfs is only supported on Linux" << endl;
- abort();
-#endif // __linux__
- }
-
- cout << commandString << endl;
-
- // The child has successfully setsid, now run the command.
- if (override.isNone()) {
- if (command.shell()) {
- execlp(
- "sh",
- "sh",
- "-c",
- command.value().c_str(),
- (char*) nullptr);
- } else {
- execvp(command.value().c_str(), argv);
- }
- } else {
- char** argv = override.get();
- execvp(argv[0], argv);
- }
+ // A Windows process is started using the `CREATE_SUSPENDED` flag
+ // and is part of a job object. While the process handle is kept
+ // open the reap function will work.
+ PROCESS_INFORMATION processInformation = launchTaskWindows(
+ task.get(),
+ command,
+ argv,
+ override,
+ rootfs);
- perror("Failed to exec");
- abort();
- }
+ pid = processInformation.dwProcessId;
+ ::ResumeThread(processInformation.hThread);
+ CloseHandle(processInformation.hThread);
+ process_handle = processInformation.hProcess;
+#endif
delete[] argv;
- // In parent process.
- os::close(pipes[1]);
-
- // Get the child's pid via the pipe.
- if (read(pipes[0], &pid, sizeof(pid)) == -1) {
- cerr << "Failed to get child PID from pipe, read: "
- << os::strerror(errno) << endl;
- abort();
- }
-
- os::close(pipes[0]);
-
cout << "Forked command at " << pid << endl;
if (task->has_health_check()) {
@@ -703,7 +572,7 @@ private:
// Send SIGTERM directly to process 'pid' as it may not have
// received signal before os::killtree() failed.
- ::kill(pid, SIGTERM);
+ os::kill(pid, SIGTERM);
} else {
cout << "Sent SIGTERM to the following process trees:\n"
<< stringify(trees.get()) << endl;
@@ -807,7 +676,7 @@ private:
// Process 'pid' may not have received signal before
// os::killtree() failed. To make sure process 'pid' is reaped
// we send SIGKILL directly.
- ::kill(pid, SIGKILL);
+ os::kill(pid, SIGKILL);
} else {
cout << "Killed the following process trees:\n" << stringify(trees.get())
<< endl;
@@ -909,6 +778,9 @@ private:
Option<Timer> killGracePeriodTimer;
pid_t pid;
+#ifdef __WINDOWS__
+ HANDLE process_handle;
+#endif
pid_t healthPid;
Duration shutdownGracePeriod;
Option<KillPolicy> killPolicy;
@@ -991,6 +863,10 @@ int main(int argc, char** argv)
FrameworkID frameworkId;
ExecutorID executorId;
+#ifdef __WINDOWS__
+ process::Winsock winsock;
+#endif
+
// Load flags from command line.
Try<flags::Warnings> load = flags.load(None(), &argc, &argv);
http://git-wip-us.apache.org/repos/asf/mesos/blob/e8219784/src/launcher/executor.hpp
----------------------------------------------------------------------
diff --git a/src/launcher/executor.hpp b/src/launcher/executor.hpp
new file mode 100644
index 0000000..217680d
--- /dev/null
+++ b/src/launcher/executor.hpp
@@ -0,0 +1,29 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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 __LAUNCHER_EXECUTOR_HPP__
+#define __LAUNCHER_EXECUTOR_HPP__
+
+
+// For readability, we minimize the number of #ifdef blocks in the code by
+// splitting platform specifc system calls into separate directories.
+#ifdef __WINDOWS__
+#include "launcher/windows/executor.hpp"
+#else
+#include "launcher/posix/executor.hpp"
+#endif // __WINDOWS__
+
+#endif // __LAUNCHER_EXECUTOR_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/e8219784/src/launcher/posix/executor.cpp
----------------------------------------------------------------------
diff --git a/src/launcher/posix/executor.cpp b/src/launcher/posix/executor.cpp
new file mode 100644
index 0000000..9b34623
--- /dev/null
+++ b/src/launcher/posix/executor.cpp
@@ -0,0 +1,247 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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 "launcher/posix/executor.hpp"
+
+#include <iostream>
+
+#include <stout/os.hpp>
+#include <stout/strings.hpp>
+
+#ifdef __linux__
+#include "linux/fs.hpp"
+#endif
+
+#ifdef __linux__
+namespace fs = mesos::internal::fs;
+#endif
+
+using std::cout;
+using std::cerr;
+using std::endl;
+using std::string;
+using std::vector;
+
+namespace mesos {
+namespace v1 {
+namespace internal {
+
+pid_t launchTaskPosix(
+ const mesos::v1::TaskInfo& task,
+ const mesos::v1::CommandInfo& command,
+ const Option<string>& user,
+ char** argv,
+ Option<char**>& override,
+ Option<string>& rootfs,
+ Option<string>& sandboxDirectory,
+ Option<string>& workingDirectory)
+{
+ // TODO(benh): Clean this up with the new 'Fork' abstraction.
+ // Use pipes to determine which child has successfully changed
+ // session. This is needed as the setsid call can fail from other
+ // processes having the same group id.
+ int _pipes[2];
+
+ Try<Nothing> pipes = os::pipe(_pipes);
+ if (pipes.isError())
+ {
+ cerr << "Failed to create pipe: " << pipes.error() << endl;
+ abort();
+ }
+ // Set the FD_CLOEXEC flags on these pipes.
+ Try<Nothing> cloexec = os::cloexec(_pipes[0]);
+ if (cloexec.isError()) {
+ cerr << "Failed to cloexec(pipe[0]): " << cloexec.error() << endl;
+ abort();
+ }
+
+ cloexec = os::cloexec(_pipes[1]);
+ if (cloexec.isError()) {
+ cerr << "Failed to cloexec(pipe[1]): " << cloexec.error() << endl;
+ abort();
+ }
+
+ if (rootfs.isSome()) {
+ // The command executor is responsible for chrooting into the
+ // root filesystem and changing the user before exec-ing the
+ // user process.
+#ifdef __linux__
+ Result<string> user = os::user();
+ if (user.isError()) {
+ cerr << "Failed to get current user: " << user.error() << endl;
+ abort();
+ } else if (user.isNone()) {
+ cerr << "Current username is not found" << endl;
+ abort();
+ } else if (user.get() != "root") {
+ cerr << "The command executor requires root with rootfs" << endl;
+ abort();
+ }
+#else
+ cerr << "Not expecting root volume with non-linux platform." << endl;
+ abort();
+#endif // __linux__
+ }
+
+ // Prepare the command log message.
+ string commandString;
+ if (override.isSome()) {
+ char** argv = override.get();
+ // argv is guaranteed to be nullptr terminated and we rely on
+ // that fact to print command to be executed.
+ for (int i = 0; argv[i] != nullptr; i++) {
+ commandString += string(argv[i]) + " ";
+ }
+ } else if (command.shell()) {
+ commandString = string(os::Shell::arg0) + " " +
+ string(os::Shell::arg1) + " '" +
+ command.value() + "'";
+ } else {
+ commandString =
+ "[" + command.value() + ", " +
+ strings::join(", ", command.arguments()) + "]";
+ }
+
+ pid_t pid;
+ if ((pid = fork()) == -1) {
+ cerr << "Failed to fork to run " << commandString << ": "
+ << os::strerror(errno) << endl;
+ abort();
+ }
+
+ // TODO(jieyu): Make the child process async signal safe.
+ if (pid == 0) {
+ // In child process, we make cleanup easier by putting process
+ // into it's own session.
+ os::close(_pipes[0]);
+
+ // NOTE: We setsid() in a loop because setsid() might fail if another
+ // process has the same process group id as the calling process.
+ while ((pid = setsid()) == -1) {
+ perror("Could not put command in its own session, setsid");
+
+ cout << "Forking another process and retrying" << endl;
+
+ if ((pid = fork()) == -1) {
+ perror("Failed to fork to launch command");
+ abort();
+ }
+
+ if (pid > 0) {
+ // In parent process. It is ok to suicide here, because
+ // we're not watching this process.
+ exit(0);
+ }
+ }
+
+ if (write(_pipes[1], &pid, sizeof(pid)) != sizeof(pid)) {
+ perror("Failed to write PID on pipe");
+ abort();
+ }
+
+ os::close(_pipes[1]);
+
+ if (rootfs.isSome()) {
+#ifdef __linux__
+ if (user.isSome()) {
+ // This is a work around to fix the problem that after we chroot
+ // os::su call afterwards failed because the linker may not be
+ // able to find the necessary library in the rootfs.
+ // We call os::su before chroot here to force the linker to load
+ // into memory.
+ // We also assume it's safe to su to "root" user since
+ // filesystem/linux.cpp checks for root already.
+ os::su("root");
+ }
+
+ Try<Nothing> chroot = fs::chroot::enter(rootfs.get());
+ if (chroot.isError()) {
+ cerr << "Failed to enter chroot '" << rootfs.get()
+ << "': " << chroot.error() << endl;
+ abort();
+ }
+
+ // Determine the current working directory for the executor.
+ string cwd;
+ if (workingDirectory.isSome()) {
+ cwd = workingDirectory.get();
+ } else {
+ CHECK_SOME(sandboxDirectory);
+ cwd = sandboxDirectory.get();
+ }
+
+ Try<Nothing> chdir = os::chdir(cwd);
+ if (chdir.isError()) {
+ cerr << "Failed to chdir into current working directory '"
+ << cwd << "': " << chdir.error() << endl;
+ abort();
+ }
+
+ if (user.isSome()) {
+ Try<Nothing> su = os::su(user.get());
+ if (su.isError()) {
+ cerr << "Failed to change user to '" << user.get() << "': "
+ << su.error() << endl;
+ abort();
+ }
+ }
+#else
+ cerr << "Rootfs is only supported on Linux" << endl;
+ abort();
+#endif // __linux__
+ }
+
+ cout << commandString << endl;
+
+ // The child has successfully setsid, now run the command.
+ if (override.isNone()) {
+ if (command.shell()) {
+ execlp(
+ os::Shell::name,
+ os::Shell::arg0,
+ os::Shell::arg1,
+ command.value().c_str(),
+ (char*) nullptr);
+ } else {
+ execvp(command.value().c_str(), argv);
+ }
+ } else {
+ char** argv = override.get();
+ execvp(argv[0], argv);
+ }
+
+ perror("Failed to exec");
+ abort();
+ }
+
+ // In parent process.
+ os::close(_pipes[1]);
+
+ // Get the child's pid via the pipe.
+ if (read(_pipes[0], &pid, sizeof(pid)) == -1) {
+ cerr << "Failed to get child PID from pipe, read: "
+ << os::strerror(errno) << endl;
+ abort();
+ }
+
+ os::close(_pipes[0]);
+
+ return pid;
+}
+
+} // namespace internal {
+} // namespace v1 {
+} // namespace mesos {
http://git-wip-us.apache.org/repos/asf/mesos/blob/e8219784/src/launcher/posix/executor.hpp
----------------------------------------------------------------------
diff --git a/src/launcher/posix/executor.hpp b/src/launcher/posix/executor.hpp
new file mode 100644
index 0000000..7607245
--- /dev/null
+++ b/src/launcher/posix/executor.hpp
@@ -0,0 +1,44 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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 __LAUNCHER_POSIX_EXECUTOR_HPP__
+#define __LAUNCHER_POSIX_EXECUTOR_HPP__
+
+#include <string>
+
+#include <mesos/v1/mesos.hpp>
+
+#include <stout/option.hpp>
+
+namespace mesos {
+namespace v1 {
+namespace internal {
+
+pid_t launchTaskPosix(
+ const mesos::v1::TaskInfo& task,
+ const mesos::v1::CommandInfo& command,
+ const Option<std::string>& user,
+ char** argv,
+ Option<char**>& override,
+ Option<std::string>& rootfs,
+ Option<std::string>& sandboxDirectory,
+ Option<std::string>& workingDirectory);
+
+} // namespace internal {
+} // namespace v1 {
+} // namespace mesos {
+
+#endif // __LAUNCHER_POSIX_EXECUTOR_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/e8219784/src/launcher/windows/executor.cpp
----------------------------------------------------------------------
diff --git a/src/launcher/windows/executor.cpp b/src/launcher/windows/executor.cpp
new file mode 100644
index 0000000..81ae4b0
--- /dev/null
+++ b/src/launcher/windows/executor.cpp
@@ -0,0 +1,115 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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 "launcher/windows/executor.hpp"
+
+#include <iostream>
+
+#include <stout/os.hpp>
+#include <stout/strings.hpp>
+
+#include <stout/os/close.hpp>
+
+using std::cout;
+using std::cerr;
+using std::endl;
+using std::string;
+using std::vector;
+
+namespace mesos {
+namespace v1 {
+namespace internal {
+
+PROCESS_INFORMATION launchTaskWindows(
+ const TaskInfo& task,
+ const CommandInfo& command,
+ char** argv,
+ Option<char**>& override,
+ Option<string>& rootfs)
+{
+ PROCESS_INFORMATION processInfo;
+ ::ZeroMemory(&processInfo, sizeof(PROCESS_INFORMATION));
+
+ STARTUPINFO startupInfo;
+ ::ZeroMemory(&startupInfo, sizeof(STARTUPINFO));
+ startupInfo.cb = sizeof(STARTUPINFO);
+
+ string executable;
+ string commandLine = command.value();
+
+ if (override.isNone()) {
+ if (command.shell()) {
+ // Use Windows shell (`cmd.exe`). Look for it in the system folder.
+ char systemDir[MAX_PATH];
+ if (!::GetSystemDirectory(systemDir, MAX_PATH)) {
+ // No way to recover from this, safe to exit the process.
+ abort();
+ }
+
+ executable = path::join(systemDir, os::Shell::name);
+
+ // `cmd.exe` needs to be used in conjunction with the `/c` parameter.
+ // For compliance with C-style applications, `cmd.exe` should be passed
+ // as `argv[0]`.
+ // TODO(alexnaparu): Quotes are probably needed after `/c`.
+ commandLine =
+ strings::join(" ", os::Shell::arg0, os::Shell::arg1, commandLine);
+ } else {
+ // Not a shell command, execute as-is.
+ executable = command.value();
+ commandLine = os::stringify_args(argv);
+ }
+ } else {
+ // Convert all arguments to a single space-separated string.
+ commandLine = os::stringify_args(override.get());
+ }
+
+ cout << commandLine << endl;
+
+ // There are many wrappers on `CreateProcess` that are more user-friendly,
+ // but they don't return the PID of the child process.
+ BOOL createProcessResult = ::CreateProcess(
+ executable.empty() ? nullptr : executable.c_str(), // Module to load.
+ (LPSTR) commandLine.c_str(), // Command line.
+ nullptr, // Default security attributes.
+ nullptr, // Default primary thread security attributes.
+ TRUE, // Inherited parent process handles.
+ CREATE_SUSPENDED, // Create suspended so we can wrap in job object.
+ nullptr, // Use parent's environment.
+ nullptr, // Use parent's current directory.
+ &startupInfo, // STARTUPINFO pointer.
+ &processInfo); // PROCESS_INFORMATION pointer.
+
+ if (!createProcessResult) {
+ cerr << "launchTaskWindows: CreateProcess failed with error code"
+ << GetLastError() << endl;
+
+ abort();
+ }
+
+ Try<HANDLE> job = os::create_job(processInfo.dwProcessId);
+ // The job handle is not closed. The job lifetime is equal or lower
+ // than the process lifetime.
+ if (job.isError()) {
+ abort();
+ }
+
+ return processInfo;
+}
+
+} // namespace internal {
+} // namespace v1 {
+} // namespace mesos {
http://git-wip-us.apache.org/repos/asf/mesos/blob/e8219784/src/launcher/windows/executor.hpp
----------------------------------------------------------------------
diff --git a/src/launcher/windows/executor.hpp b/src/launcher/windows/executor.hpp
new file mode 100644
index 0000000..0635a2a
--- /dev/null
+++ b/src/launcher/windows/executor.hpp
@@ -0,0 +1,41 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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 __LAUNCHER_EXECUTOR_HPP__
+#define __LAUNCHER_EXECUTOR_HPP__
+
+#include <string>
+
+#include <mesos/v1/mesos.hpp>
+
+#include <stout/option.hpp>
+
+namespace mesos {
+namespace v1 {
+namespace internal {
+
+PROCESS_INFORMATION launchTaskWindows(
+ const TaskInfo& task,
+ const CommandInfo& command,
+ char** argv,
+ Option<char**>& override,
+ Option<std::string>& rootfs);
+
+} // namespace internal {
+} // namespace v1 {
+} // namespace mesos {
+
+#endif // __LAUNCHER_EXECUTOR_HPP__