qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] catch signals


From: Gerd Hoffmann
Subject: [Qemu-devel] [PATCH] catch signals
Date: Tue, 05 Aug 2008 18:09:39 +0200
User-agent: Thunderbird 2.0.0.14 (X11/20080501)

  Hi,

This patch adds a signal handler to qemu, so fatal signals don't kill
off qemu.  Instead a shutdown request is issued, like it is done when
you close the SDL window.  qemu cleans up nicely then, cespecially it
calls all atexit handlers.

please apply,
  Gerd

-- 
http://kraxel.fedorapeople.org/xenner/
>From 886775f5229d83d73f3d162b919ea68b386b2a60 Mon Sep 17 00:00:00 2001
From: Gerd Hoffmann <address@hidden>
Date: Tue, 5 Aug 2008 17:53:36 +0200
Subject: [PATCH] catch signals, so atexit handlers are called correctly.


Signed-off-by: Gerd Hoffmann <address@hidden>
---
 curses.c |    2 --
 sdl.c    |    5 -----
 vl.c     |   39 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/curses.c b/curses.c
index 87aa9b3..03580fb 100644
--- a/curses.c
+++ b/curses.c
@@ -346,8 +346,6 @@ void curses_display_init(DisplayState *ds, int full_screen)
     atexit(curses_atexit);
 
 #ifndef _WIN32
-    signal(SIGINT, SIG_DFL);
-    signal(SIGQUIT, SIG_DFL);
 #if defined(SIGWINCH) && defined(KEY_RESIZE)
     /* some curses implementations provide a handler, but we
      * want to be sure this is handled regardless of the library */
diff --git a/sdl.c b/sdl.c
index 0edc4a0..9ac5725 100644
--- a/sdl.c
+++ b/sdl.c
@@ -636,11 +636,6 @@ void sdl_display_init(DisplayState *ds, int full_screen, 
int no_frame)
         fprintf(stderr, "Could not initialize SDL - exiting\n");
         exit(1);
     }
-#ifndef _WIN32
-    /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
-    signal(SIGINT, SIG_DFL);
-    signal(SIGQUIT, SIG_DFL);
-#endif
 
     ds->dpy_update = sdl_update;
     ds->dpy_resize = sdl_resize;
diff --git a/vl.c b/vl.c
index e929370..c70b984 100644
--- a/vl.c
+++ b/vl.c
@@ -8141,6 +8141,40 @@ static BOOL WINAPI qemu_ctrl_handler(DWORD type)
 
 #define MAX_NET_CLIENTS 32
 
+#ifndef _WIN32
+static void termsig_handler(int signal)
+{
+    switch (signal) {
+    case SIGSEGV:
+    case SIGBUS:
+        /* returning from signal handler most likely isn't going to work */
+        fprintf(stderr, "qemu: got signal %d (%s), taking emergency exit\n",
+                signal, strsignal(signal));
+        exit(1);
+        break;
+    default:
+        qemu_system_shutdown_request();
+        vm_start();    /* In case we're paused */
+        break;
+    }
+}
+
+static void termsig_setup(void)
+{
+    struct sigaction act;
+
+    memset(&act, 0, sizeof(act));
+    act.sa_flags   = SA_RESETHAND;
+    act.sa_handler = termsig_handler;
+
+    sigaction(SIGINT,  &act, NULL);
+    sigaction(SIGTERM, &act, NULL);
+    sigaction(SIGQUIT, &act, NULL);
+    sigaction(SIGSEGV, &act, NULL);
+    sigaction(SIGBUS,  &act, NULL);
+}
+#endif
+
 int main(int argc, char **argv)
 {
 #ifdef CONFIG_GDBSTUB
@@ -9031,6 +9065,11 @@ int main(int argc, char **argv)
 #endif
     }
 
+#ifndef _WIN32
+    /* must be after terminal init, SDL changes signal handlers */
+    termsig_setup();
+#endif
+
     /* Maintain compatibility with multiple stdio monitors */
 
     has_monitor = 0;
-- 
1.5.5.1


reply via email to

[Prev in Thread] Current Thread [Next in Thread]