Add Windows support for launching new processes.

Windows is simpler than Unix in that it doesn't require fork just
just to set a working directory; it's more complex in that arguments
must be manually quoted rather than passed as an array.
This commit is contained in:
Grant Paul 2016-11-16 02:23:21 -08:00 committed by onhachoe
parent cc30391cde
commit a1f792a720
4 changed files with 127 additions and 3 deletions

View File

@ -10,15 +10,14 @@
#ifndef __process_Launcher_h
#define __process_Launcher_h
#include <process/Context.h>
#include <sstream>
#include <ext/optional>
namespace libutil { class Filesystem; }
namespace process {
class Context;
/*
* Abstract process launcher.
*/

View File

@ -12,6 +12,11 @@
#include <process/Launcher.h>
#include <string>
#include <functional>
#include <unordered_map>
#include <ext/optional>
namespace process {
/*

View File

@ -8,18 +8,38 @@
*/
#include <process/DefaultLauncher.h>
#include <process/Context.h>
#include <libutil/Filesystem.h>
#if _WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
// In most cases, size of pipe will be greater than one page,
#define PIPE_BUFFER_SIZE 4096
using process::DefaultLauncher;
using process::Context;
using libutil::Filesystem;
#if _WIN32
using WideString = std::basic_string<std::remove_const<std::remove_pointer<LPCWSTR>::type>::type>;
static WideString
StringToWideString(std::string const &str)
{
int size = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), NULL, 0);
WideString wide = WideString();
wide.resize(size);
MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &wide[0], size);
return wide;
}
#endif
DefaultLauncher::
DefaultLauncher() :
Launcher()
@ -34,6 +54,103 @@ DefaultLauncher::
ext::optional<int> DefaultLauncher::
launch(Filesystem *filesystem, Context const *context)
{
#if _WIN32
WideString arguments;
for (std::string const &argument : context->commandLineArguments()) {
if (&argument != &context->commandLineArguments().front()) {
arguments += static_cast<wchar_t>(' ');
}
if (!argument.empty() && argument.find_first_of(" \t\n\v\"") == std::string::npos) {
arguments += StringToWideString(argument);
} else {
arguments += static_cast<wchar_t>('"');
WideString wide = StringToWideString(argument);
WideString special = StringToWideString("\"\\");
for (auto it = wide.begin(); it != wide.end(); ++it) {
/* Find the first backslash or quote. */
size_t first_ = wide.find_first_of(special, it - wide.begin());
auto first = (first_ == std::string::npos ? wide.end() : wide.begin() + first_);
/* Copy up to the first special character. */
arguments.insert(arguments.end(), it, first);
if (first == wide.end()) {
/* Nothing special; jump to the end. */
it = std::prev(first);
} else if (*first == static_cast<wchar_t>('"')) {
/* Escape and append the quote itself. */
arguments += static_cast<wchar_t>('\\');
arguments += *first;
/* Move past the quote. */
it = first;
} else if (*first == static_cast<wchar_t>('\\')) {
/* Find next non-backslash character after the first backslash. */
size_t after_ = wide.find_first_not_of(static_cast<wchar_t>('\\'), first - wide.begin());
auto after = (after_ == std::string::npos ? wide.end() : wide.begin() + after_);
if (after == wide.end() || *after == static_cast<wchar_t>('"')) {
/* Duplicate (escape) all backslashes before (possibly ending) quote. */
arguments.insert(arguments.end(), first, after);
arguments.insert(arguments.end(), first, after);
} else {
/* Not before quote, no need to escape backslashes. */
arguments.insert(arguments.end(), first, after);
}
/* Jump to the next non-backslash character. */
it = std::prev(after);
} else {
abort();
}
}
arguments += static_cast<wchar_t>('"');
}
}
WideString environment;
for (auto const &entry : context->environmentVariables()) {
environment += StringToWideString(entry.first);
environment += StringToWideString("=");
environment += StringToWideString(entry.second);
environment += static_cast<wchar_t>('\0');
}
WideString executablePath = StringToWideString(context->executablePath());
WideString currentDirectory = StringToWideString(context->currentDirectory());
STARTUPINFOW startup;
memset(&startup, 0, sizeof(startup));
startup.cb = sizeof(startup);
PROCESS_INFORMATION process;
if (!CreateProcessW(
executablePath.c_str(),
&arguments[0],
nullptr,
nullptr,
FALSE,
CREATE_UNICODE_ENVIRONMENT,
static_cast<LPVOID>(&environment[0]),
currentDirectory.c_str(),
&startup,
&process)) {
return ext::nullopt;
}
if (!CloseHandle(process.hThread)) {
/* Error, but process started. Ignore. */
}
if (!CloseHandle(process.hProcess)) {
/* Error, but process started. Ignore. */
}
return static_cast<int>(process.dwProcessId);
#else
/*
* Extract input data for exec, so no C++ is required after fork.
*/
@ -153,4 +270,5 @@ launch(Filesystem *filesystem, Context const *context)
::waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
#endif
}

View File

@ -8,9 +8,11 @@
*/
#include <process/MemoryLauncher.h>
#include <process/Context.h>
#include <libutil/Filesystem.h>
using process::MemoryLauncher;
using process::Context;
using libutil::Filesystem;
MemoryLauncher::