Merge pull request #173 from kiwix/subprocess_windows

Subprocess windows
This commit is contained in:
Matthieu Gautier 2018-10-31 14:04:21 +01:00 committed by GitHub
commit b977b08683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 22 additions and 16 deletions

View File

@ -19,8 +19,10 @@ libzim_dep = dependency('libzim', version : '>=4.0.0', static:static_deps)
pugixml_dep = dependency('pugixml', static:static_deps)
libcurl_dep = dependency('libcurl', static:static_deps)
extra_cflags = ''
if target_machine.system() == 'windows' and static_deps
add_project_arguments('-DCURL_STATICLIB', language : 'cpp')
extra_cflags += '-DCURL_STATICLIB'
endif
@ -112,7 +114,6 @@ if xapian_dep.found()
pkg_requires += ['xapian-core']
endif
extra_cflags = ''
if has_ctpp2_dep
extra_libs += ctpp2_link_args
if ctpp2_include_path != ''

View File

@ -36,7 +36,11 @@ Aria2::Aria2():
std::string rpc_secret = "--rpc-secret=" + m_secret;
m_secret = "token:"+m_secret;
#ifdef _WIN32
callCmd.push_back("aria2c.exe");
#else
callCmd.push_back("aria2c");
#endif
callCmd.push_back("--enable-rpc");
callCmd.push_back(rpc_secret.c_str());
callCmd.push_back(rpc_port.c_str());
@ -57,7 +61,6 @@ Aria2::Aria2():
callCmd.push_back("--max-concurrent-downloads=42");
callCmd.push_back("--rpc-max-request-size=6M");
callCmd.push_back("--file-allocation=none");
callCmd.push_back(NULL);
mp_aria = Subprocess::run(callCmd);
mp_curl = curl_easy_init();
curl_easy_setopt(mp_curl, CURLOPT_URL, "http://localhost/rpc");

View File

@ -13,6 +13,7 @@
#include <memory>
#include <curl/curl.h>
#include <pthread.h>
namespace kiwix {

View File

@ -8,7 +8,7 @@
# include "subprocess_unix.h"
#endif
Subprocess::Subprocess(std::unique_ptr<SubprocessImpl> impl, const commandLine_t& commandLine) :
Subprocess::Subprocess(std::unique_ptr<SubprocessImpl> impl, commandLine_t& commandLine) :
mp_impl(std::move(impl))
{
mp_impl->run(commandLine);
@ -19,7 +19,7 @@ Subprocess::~Subprocess()
mp_impl->kill();
}
std::unique_ptr<Subprocess> Subprocess::run(const commandLine_t& commandLine)
std::unique_ptr<Subprocess> Subprocess::run(commandLine_t& commandLine)
{
#ifdef _WIN32
auto impl = std::unique_ptr<SubprocessImpl>(new WinImpl);

View File

@ -11,7 +11,7 @@ typedef std::vector<const char *> commandLine_t;
class SubprocessImpl
{
public:
virtual void run(const commandLine_t& commandLine) = 0;
virtual void run(commandLine_t& commandLine) = 0;
virtual bool kill() = 0;
virtual bool isRunning() = 0;
virtual ~SubprocessImpl() = default;
@ -22,10 +22,10 @@ class Subprocess
private:
// Impl depends of the system (window, unix, ...)
std::unique_ptr<SubprocessImpl> mp_impl;
Subprocess(std::unique_ptr<SubprocessImpl> impl, const commandLine_t& commandLine);
Subprocess(std::unique_ptr<SubprocessImpl> impl, commandLine_t& commandLine);
public:
static std::unique_ptr<Subprocess> run(const commandLine_t& commandLine);
static std::unique_ptr<Subprocess> run(commandLine_t& commandLine);
~Subprocess();
bool isRunning();

View File

@ -55,7 +55,7 @@ void* UnixImpl::waitForPID(void* _self)
return self;
}
void UnixImpl::run(const commandLine_t& commandLine)
void UnixImpl::run(commandLine_t& commandLine)
{
const char* binary = commandLine[0];
int pid = fork();
@ -64,6 +64,7 @@ void UnixImpl::run(const commandLine_t& commandLine)
std::cerr << "cannot fork" << std::endl;
break;
case 0:
commandLine.push_back(NULL);
if (execvp(binary, const_cast<char* const*>(commandLine.data()))) {
perror("Cannot launch\n");
exit(-1);

View File

@ -18,7 +18,7 @@ class UnixImpl : public SubprocessImpl
UnixImpl();
virtual ~UnixImpl();
void run(const commandLine_t& commandLine);
void run(commandLine_t& commandLine);
bool kill();
bool isRunning();

View File

@ -50,20 +50,19 @@ std::unique_ptr<wchar_t[]> toWideChar(const std::string& value)
}
void WinImpl::run(const commandLine_t& commandLine)
void WinImpl::run(commandLine_t& commandLine)
{
STARTUPINFOW startInfo = {0};
PROCESS_INFORMATION procInfo;
startInfo.cb = sizeof(startInfo);
const char* binary = commandLine[0];
std::cerr << "running " << binary << std::endl;
std::ostringstream oss;
for(auto& item: commandLine) {
oss << item << " ";
}
auto wCommandLine = toWideChar(oss.str());
if (CreateProcessW(
toWideChar(binary).get(),
toWideChar(oss.str()).get(),
NULL,
wCommandLine.get(),
NULL,
NULL,
false,

View File

@ -3,6 +3,7 @@
#include "subprocess.h"
#include <windows.h>
#include <synchapi.h>
class WinImpl : public SubprocessImpl
@ -17,7 +18,7 @@ class WinImpl : public SubprocessImpl
WinImpl();
virtual ~WinImpl();
void run(const commandLine_t& commandLine);
void run(commandLine_t& commandLine);
bool kill();
bool isRunning();