diff options
author | Chase Douglas <chase.douglas@canonical.com> | 2012-04-18 10:24:50 -0700 |
---|---|---|
committer | Chase Douglas <chase.douglas@canonical.com> | 2012-04-18 10:24:50 -0700 |
commit | 49f6f2deb544163823ecc8ec127162c0774acc00 (patch) | |
tree | 74a0f2784c0ac89de6aad1d66e33c8590a70d209 | |
parent | 5b1b93962317140c480bbf34a6f988568bfd51d1 (diff) |
Kill dummy Xorg server when a terminating signal arrives
This only takes effect if the program links in the provided main()
function. If you provide your own main() you must handle signals
yourself.
Signed-off-by: Chase Douglas <chase.douglas@canonical.com>
-rw-r--r-- | src/xorg-gtest_main.cpp | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/src/xorg-gtest_main.cpp b/src/xorg-gtest_main.cpp index 5e5a748..8953afa 100644 --- a/src/xorg-gtest_main.cpp +++ b/src/xorg-gtest_main.cpp @@ -27,6 +27,8 @@ #include <getopt.h> +#include <csignal> + #include <gtest/gtest.h> #include "xorg/gtest/environment.h" @@ -53,12 +55,64 @@ const struct option longopts[] = { } // namespace +xorg::testing::Environment* environment = NULL; + +static void signal_handler(int signum) { + if (environment) + environment->Kill(); + + /* This will call the default handler because we used SA_RESETHAND */ + raise(signum); +} + +static void setup_signal_handlers() { + static const int signals[] = { + SIGHUP, + SIGTERM, + SIGQUIT, + SIGILL, + SIGABRT, + SIGFPE, + SIGSEGV, + SIGPIPE, + SIGALRM, + SIGTERM, + SIGUSR1, + SIGUSR2, + SIGBUS, + SIGPOLL, + SIGPROF, + SIGSYS, + SIGTRAP, + SIGVTALRM, + SIGXCPU, + SIGXFSZ, + SIGIOT, + SIGSTKFLT, + SIGIO, + SIGPWR, + SIGUNUSED, + }; + + struct sigaction action; + action.sa_handler = signal_handler; + sigemptyset(&action.sa_mask); + action.sa_flags = SA_RESETHAND; + + for (unsigned i = 0; i < sizeof(signals) / sizeof(signals[0]); ++i) + if (sigaction(signals[i], &action, NULL)) + std::cerr << "Warning: Failed to set signal handler for signal " + << signals[i] << "\n"; +} + int main(int argc, char *argv[]) { std::string xorg_conf_path; std::string xorg_log_file_path; int xorg_display = -1; std::string server; + setup_signal_handlers(); + testing::InitGoogleTest(&argc, argv); /* Reset getopt state */ @@ -110,7 +164,7 @@ int main(int argc, char *argv[]) { } if (!no_dummy_server) { - xorg::testing::Environment* environment = new xorg::testing::Environment; + environment = new xorg::testing::Environment; if (xorg_conf_specified) environment->set_conf_file(xorg_conf_path); |