summaryrefslogtreecommitdiff
path: root/test/fixtures/util.cpp
blob: 7434393556aed3b4e226df26295ab47cebce4d86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
}

}
}