diff options
Diffstat (limited to 'test/fixtures/util.cpp')
-rw-r--r-- | test/fixtures/util.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/test/fixtures/util.cpp b/test/fixtures/util.cpp new file mode 100644 index 0000000000..7434393556 --- /dev/null +++ b/test/fixtures/util.cpp @@ -0,0 +1,35 @@ +#include "util.hpp" + +#include <csignal> + +namespace mbgl { +namespace test { + +pid_t startServer(const char *executable) { + const std::string parent_pid = std::to_string(getpid()); + pid_t pid = fork(); + if (pid < 0) { + throw std::runtime_error("Cannot create server process"); + } else if (pid == 0) { + char *args[] = { const_cast<char *>(executable), + const_cast<char *>(parent_pid.c_str()), + nullptr }; + int ret = execv(executable, args); + // This call should not return. In case execve failed, we exit anyway. + if (ret < 0) { + fprintf(stderr, "Failed to start server: %s\n", strerror(errno)); + } + exit(0); + } else { + // Wait until the server process sends SIGCONT. + raise(SIGSTOP); + } + return pid; +} + +void stopServer(pid_t pid) { + kill(pid, SIGTERM); +} + +} +} |