[rygel] core: Simplify and fix (unix) signal handling



commit b6c7ba96ec1f2c17914ec454a6f845f22cc83714
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Tue Jun 22 01:15:53 2010 +0300

    core: Simplify and fix (unix) signal handling
    
    We aren't allowed to do a lot in unix signal handlers so we shouldn't do
    things like emitting signals.

 src/rygel/rygel-main.vala           |   27 +++++++++++----------------
 src/rygel/rygel-signal-handler.vala |   32 +++++++++++++-------------------
 2 files changed, 24 insertions(+), 35 deletions(-)
---
diff --git a/src/rygel/rygel-main.vala b/src/rygel/rygel-main.vala
index 8a1180b..bb56471 100644
--- a/src/rygel/rygel-main.vala
+++ b/src/rygel/rygel-main.vala
@@ -33,7 +33,6 @@ public class Rygel.Main : Object {
     private static int PLUGIN_TIMEOUT = 5;
 
     private PluginLoader plugin_loader;
-    private SignalHandler signal_handler;
     private ContextManager context_manager;
     private ArrayList <RootDeviceFactory> factories;
     private ArrayList <RootDevice> root_devices;
@@ -44,7 +43,7 @@ public class Rygel.Main : Object {
     private MainLoop main_loop;
 
     private int exit_code;
-    public bool restart;
+    public bool need_restart;
 
     private Main () throws GLib.Error {
         Environment.set_application_name (_(BuildConfig.PACKAGE_NAME));
@@ -61,15 +60,21 @@ public class Rygel.Main : Object {
 
         this.plugin_loader.plugin_available.connect (this.on_plugin_loaded);
 
-        this.signal_handler = SignalHandler.get_default ();
-        this.signal_handler.exit.connect (this.application_exit_cb);
-        this.signal_handler.restart.connect (this.application_restart_cb);
+        SignalHandler.setup (this);
     }
 
     public void exit (int exit_code) {
         this.exit_code = exit_code;
 
         this.main_loop.quit ();
+
+        SignalHandler.cleanup ();
+    }
+
+    public void restart () {
+        this.need_restart = true;
+
+        this.exit (0);
     }
 
     private int run () {
@@ -91,16 +96,6 @@ public class Rygel.Main : Object {
         return this.exit_code;
     }
 
-    private void application_restart_cb () {
-        this.restart = true;
-
-        this.exit (0);
-    }
-
-    private void application_exit_cb () {
-        this.exit (0);
-    }
-
     private void on_plugin_loaded (PluginLoader plugin_loader,
                                    Plugin       plugin) {
         var iterator = this.factories.iterator ();
@@ -242,7 +237,7 @@ public class Rygel.Main : Object {
 
         int exit_code = main.run ();
 
-        if (main.restart) {
+        if (main.need_restart) {
             Posix.execvp (original_args[0], original_args);
         }
 
diff --git a/src/rygel/rygel-signal-handler.vala b/src/rygel/rygel-signal-handler.vala
index 2055970..37419d7 100644
--- a/src/rygel/rygel-signal-handler.vala
+++ b/src/rygel/rygel-signal-handler.vala
@@ -27,36 +27,30 @@ using Posix;
  * Handles Posix signals.
  */
 public class Rygel.SignalHandler : GLib.Object {
-    private static SignalHandler handler;
+    private static Main main = null;
+    private static sigaction_t action;
 
-    public signal void exit ();
-    public signal void restart ();
+    public static void setup (Main _main) {
+        main = _main;
 
-    private sigaction_t action;
+        action = sigaction_t ();
 
-    public static SignalHandler get_default () {
-        if (handler == null) {
-            handler = new SignalHandler ();
-        }
+        action.sa_handler = signal_handler;
 
-        return handler;
+        /* Hook the handler for SIGTERM */
+        sigaction (SIGINT, action, null);
+        sigaction (SIGHUP, action, null);
     }
 
-    private SignalHandler () {
-        this.action = sigaction_t ();
-
-        this.action.sa_handler = this.signal_handler;
-
-        /* Hook the handler for SIGTERM */
-        sigaction (SIGINT, this.action, null);
-        sigaction (SIGHUP, this.action, null);
+    public static void cleanup () {
+        main = null;
     }
 
     private static void signal_handler (int signum) {
         if (signum == SIGHUP) {
-            handler.restart ();
+            main.restart ();
         } else {
-            handler.exit ();
+            main.exit (0);
         }
     }
 }



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