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) pugixml_dep = dependency('pugixml', static:static_deps)
libcurl_dep = dependency('libcurl', static:static_deps) libcurl_dep = dependency('libcurl', static:static_deps)
extra_cflags = ''
if target_machine.system() == 'windows' and static_deps if target_machine.system() == 'windows' and static_deps
add_project_arguments('-DCURL_STATICLIB', language : 'cpp') add_project_arguments('-DCURL_STATICLIB', language : 'cpp')
extra_cflags += '-DCURL_STATICLIB'
endif endif
@ -112,11 +114,10 @@ if xapian_dep.found()
pkg_requires += ['xapian-core'] pkg_requires += ['xapian-core']
endif endif
extra_cflags = ''
if has_ctpp2_dep if has_ctpp2_dep
extra_libs += ctpp2_link_args extra_libs += ctpp2_link_args
if ctpp2_include_path != '' if ctpp2_include_path != ''
extra_cflags = '-I'+ctpp2_include_path extra_cflags = ' -I'+ctpp2_include_path
endif endif
endif endif

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,7 +18,7 @@ class UnixImpl : public SubprocessImpl
UnixImpl(); UnixImpl();
virtual ~UnixImpl(); virtual ~UnixImpl();
void run(const commandLine_t& commandLine); void run(commandLine_t& commandLine);
bool kill(); bool kill();
bool isRunning(); 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}; STARTUPINFOW startInfo = {0};
PROCESS_INFORMATION procInfo; PROCESS_INFORMATION procInfo;
startInfo.cb = sizeof(startInfo); startInfo.cb = sizeof(startInfo);
const char* binary = commandLine[0];
std::cerr << "running " << binary << std::endl;
std::ostringstream oss; std::ostringstream oss;
for(auto& item: commandLine) { for(auto& item: commandLine) {
oss << item << " "; oss << item << " ";
} }
auto wCommandLine = toWideChar(oss.str());
if (CreateProcessW( if (CreateProcessW(
toWideChar(binary).get(), NULL,
toWideChar(oss.str()).get(), wCommandLine.get(),
NULL, NULL,
NULL, NULL,
false, false,

View File

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