[PATCH xorg-gtest 2/3] Kill dummy Xorg server when a terminating signal arrives

Chase Douglas chase.douglas at canonical.com
Wed Apr 18 10:39:04 PDT 2012


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 at canonical.com>
---
 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);
-- 
1.7.9.1



More information about the xorg-devel mailing list