Skip to content

Commit

Permalink
common: implement win32 subprocess helpers
Browse files Browse the repository at this point in the history
This change adds a Windows implementation for the SubProcess class.

We're also implementing "run_command" for Windows, re-using
the SubProcess class. It might be seen as just a convenience
wrapper on top of that. At the moment, the Linux bits are left
unchanged, although the same implementation might be used for all
platforms.

Signed-off-by: Lucian Petrut <[email protected]>
  • Loading branch information
petrutlucian94 committed Aug 31, 2020
1 parent 242264c commit be15568
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 26 deletions.
7 changes: 4 additions & 3 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ set(common_srcs
Readahead.cc
RefCountedObj.cc
SloppyCRCMap.cc
SubProcess.cc
Thread.cc
Throttle.cc
Timer.cc
Expand Down Expand Up @@ -89,6 +88,7 @@ set(common_srcs
pick_address.cc
random_string.cc
reverse.c
run_cmd.cc
scrub_types.cc
shared_mutex_debug.cc
signal.cc
Expand All @@ -106,14 +106,15 @@ set(common_srcs
if(WIN32)
list(APPEND common_srcs
blkdev_win32.cc
dns_resolve_win32.cc)
dns_resolve_win32.cc
SubProcess_win32.cc)
else()
list(APPEND common_srcs
blkdev.cc
dns_resolve.cc
ipaddr.cc
linux_version.c
run_cmd.cc)
SubProcess.cc)
endif()

set_source_files_properties(${CMAKE_SOURCE_DIR}/src/common/version.cc
Expand Down
22 changes: 0 additions & 22 deletions src/common/SubProcess.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ void SubProcess::close_stderr() {
close(stderr_pipe_in_fd);
}

#ifndef _WIN32
void SubProcess::kill(int signo) const {
ceph_assert(is_spawned());

Expand Down Expand Up @@ -274,7 +273,6 @@ int SubProcess::join() {
errstr << cmd << ": waitpid: unknown status returned\n";
return EXIT_FAILURE;
}
#endif /* _WIN32 */

SubProcessTimed::SubProcessTimed(const char *cmd, std_fd_op stdin_op,
std_fd_op stdout_op, std_fd_op stderr_op,
Expand All @@ -290,7 +288,6 @@ void timeout_sighandler(int sig) {
}
static void dummy_sighandler(int sig) {}

#ifndef _WIN32
void SubProcessTimed::exec() {
ceph_assert(is_child());

Expand Down Expand Up @@ -395,22 +392,3 @@ void SubProcessTimed::exec() {
fail_exit:
_exit(EXIT_FAILURE);
}

#else
int SubProcess::join() {
return EXIT_FAILURE;
}

void SubProcess::kill(int signo) const {
}

int SubProcess::spawn() {
return EXIT_FAILURE;
}

void SubProcess::exec() {
}

void SubProcessTimed::exec() {
}
#endif /* _WIN32 */
20 changes: 19 additions & 1 deletion src/common/SubProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <sstream>
#include <vector>

#include "include/compat.h"

/**
* SubProcess:
* A helper class to spawn a subprocess.
Expand Down Expand Up @@ -85,9 +87,12 @@ class SubProcess {
bool is_child() const { return pid == 0; }
virtual void exec();

private:
void close(int &fd);

#ifdef _WIN32
void close_h(HANDLE &handle);
#endif

protected:
std::string cmd;
std::vector<std::string> cmd_args;
Expand All @@ -99,6 +104,10 @@ class SubProcess {
int stderr_pipe_in_fd;
int pid;
std::ostringstream errstr;

#ifdef _WIN32
HANDLE proc_handle = INVALID_HANDLE_VALUE;
#endif
};

class SubProcessTimed : public SubProcess {
Expand All @@ -107,12 +116,21 @@ class SubProcessTimed : public SubProcess {
std_fd_op stdout_op = CLOSE, std_fd_op stderr_op = CLOSE,
int timeout = 0, int sigkill = SIGKILL);

#ifdef _WIN32
int spawn() override;
int join() override;
#endif

protected:
void exec() override;

private:
int timeout;
int sigkill;

#ifdef _WIN32
std::thread waiter;
#endif
};

void timeout_sighandler(int sig);
Expand Down
Loading

0 comments on commit be15568

Please sign in to comment.