[rygel] core: Handle POSIX signals in Vala, no need for C



commit 63091b52369c0d1de6d358d7d41542d792746c1a
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Mon Jun 21 01:45:59 2010 +0300

    core: Handle POSIX signals in Vala, no need for C

 configure.ac                        |    3 ++
 src/rygel/Makefile.am               |    3 +-
 src/rygel/cstuff.c                  |   34 -------------------
 src/rygel/cstuff.h                  |   10 -----
 src/rygel/cstuff.vapi               |   12 -------
 src/rygel/rygel-main.vala           |   21 +++++++++---
 src/rygel/rygel-signal-handler.vala |   63 +++++++++++++++++++++++++++++++++++
 7 files changed, 84 insertions(+), 62 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index ff16535..dda5449 100644
--- a/configure.ac
+++ b/configure.ac
@@ -111,6 +111,9 @@ if test x$enable_vala = xyes ; then
     AC_CHECK_FILE("${VAPIDIR}/sqlite3.vapi",
                   true,
                   [AC_MSG_ERROR("Unable to find Vala bindings for sqlite3")])
+    AC_CHECK_FILE("${VAPIDIR}/posix.vapi",
+                  true,
+                  [AC_MSG_ERROR("Unable to find Vala bindings for POSIX")])
 else
     VAPIDIR=`echo ${datadir}/vala/vapi`
 fi
diff --git a/src/rygel/Makefile.am b/src/rygel/Makefile.am
index 4da1cc0..b1e1c50 100644
--- a/src/rygel/Makefile.am
+++ b/src/rygel/Makefile.am
@@ -37,6 +37,7 @@ rygel_SOURCES = $(VAPI_SOURCE_FILES) \
 		rygel-dbus-service.vala \
 		rygel-root-device.vala \
 		rygel-root-device-factory.vala \
+		rygel-signal-handler.vala \
 		rygel-main.vala
 
 VAPI_SOURCE_FILES = rygel-configuration.vala \
@@ -103,7 +104,7 @@ rygel_VALAFLAGS = \
 	-H rygel.h -C --library=rygel-1.0 --vapidir=$(srcdir) --thread \
 	--pkg cstuff --pkg rygel-build-config --pkg gupnp-1.0 \
 	--pkg gupnp-av-1.0 --pkg dbus-glib-1 --pkg gstreamer-0.10 \
-	--pkg gio-2.0 --pkg gee-1.0 -g
+	--pkg gio-2.0 --pkg gee-1.0 --pkg posix -g
 
 rygel_LDADD = librygel-configuration.a \
 	      $(LIBGUPNP_LIBS) \
diff --git a/src/rygel/cstuff.c b/src/rygel/cstuff.c
index 93246e1..7b4ab52 100644
--- a/src/rygel/cstuff.c
+++ b/src/rygel/cstuff.c
@@ -23,13 +23,8 @@
  */
 
 #include <cstuff.h>
-#include <signal.h>
 #include <string.h>
 
-static ApplicationExitCb on_app_exit = NULL;
-static gpointer data;
-static struct sigaction sig_action;
-
 /* Copy-paste from gupnp. */
 xmlNode *
 get_xml_element (xmlNode *node,
@@ -74,32 +69,3 @@ generate_random_udn (void)
 
         return default_value;
 }
-
-static void
-signal_handler (int signum)
-{
-        gboolean restart;
-
-        restart = (signum == SIGHUP);
-
-        on_app_exit (restart, data);
-}
-
-void
-on_application_exit (ApplicationExitCb app_exit_cb,
-                     gpointer          user_data)
-{
-        on_app_exit = app_exit_cb;
-        data = user_data;
-
-        /* Hook the handler for SIGTERM */
-        memset (&sig_action, 0, sizeof (sig_action));
-        sig_action.sa_handler = signal_handler;
-        sigaction (SIGINT, &sig_action, NULL);
-        sigaction (SIGHUP, &sig_action, NULL);
-}
-
-void
-restart_application (const char **args) {
-        execvp (args[0], args);
-}
diff --git a/src/rygel/cstuff.h b/src/rygel/cstuff.h
index ede0d40..1637a05 100644
--- a/src/rygel/cstuff.h
+++ b/src/rygel/cstuff.h
@@ -29,9 +29,6 @@
 #include <glib.h>
 #include <uuid/uuid.h>
 
-typedef void (* ApplicationExitCb)      (gboolean restart,
-                                         gpointer user_data);
-
 G_GNUC_INTERNAL xmlNode *
 get_xml_element                         (xmlNode *node,
                                          ...);
@@ -39,12 +36,5 @@ get_xml_element                         (xmlNode *node,
 G_GNUC_INTERNAL char *
 generate_random_udn                     (void);
 
-G_GNUC_INTERNAL void
-on_application_exit                     (ApplicationExitCb app_exit_cb,
-                                         gpointer          user_data);
-
-G_GNUC_INTERNAL void
-restart_application                     (const char **args);
-
 #endif /* __CSTUFF_H__ */
 
diff --git a/src/rygel/cstuff.vapi b/src/rygel/cstuff.vapi
index 788300c..9c1487b 100644
--- a/src/rygel/cstuff.vapi
+++ b/src/rygel/cstuff.vapi
@@ -33,17 +33,5 @@ namespace CStuff {
                                                           ...);
         [CCode (cname = "generate_random_udn", cheader_filename = "cstuff.h")]
         public static string generate_random_udn ();
-
-        public delegate void ApplicationExitCb (bool restart);
-
-        [CCode (cname = "on_application_exit", cheader_filename = "cstuff.h")]
-        public static void on_application_exit
-                                        (ApplicationExitCb app_exit_cb);
-
-        [CCode (cname = "restart_application", cheader_filename = "cstuff.h")]
-        public static void restart_application (
-                                        [CCode (array_length = false,
-                                         array_null_terminated = true)]
-                                        string[] args);
     }
 }
diff --git a/src/rygel/rygel-main.vala b/src/rygel/rygel-main.vala
index 1f397a7..8a1180b 100644
--- a/src/rygel/rygel-main.vala
+++ b/src/rygel/rygel-main.vala
@@ -21,14 +21,19 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-using CStuff;
 using Gee;
 using GUPnP;
 
+namespace Posix {
+    [CCode (cheader_filename = "unistd.h")]
+    public extern int execvp (string file, string[] argv);
+}
+
 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;
@@ -56,7 +61,9 @@ public class Rygel.Main : Object {
 
         this.plugin_loader.plugin_available.connect (this.on_plugin_loaded);
 
-        Utils.on_application_exit (this.application_exit_cb);
+        this.signal_handler = SignalHandler.get_default ();
+        this.signal_handler.exit.connect (this.application_exit_cb);
+        this.signal_handler.restart.connect (this.application_restart_cb);
     }
 
     public void exit (int exit_code) {
@@ -84,9 +91,13 @@ public class Rygel.Main : Object {
         return this.exit_code;
     }
 
-    private void application_exit_cb (bool restart) {
-        this.restart = restart;
+    private void application_restart_cb () {
+        this.restart = true;
+
+        this.exit (0);
+    }
 
+    private void application_exit_cb () {
         this.exit (0);
     }
 
@@ -232,7 +243,7 @@ public class Rygel.Main : Object {
         int exit_code = main.run ();
 
         if (main.restart) {
-            Utils.restart_application (original_args);
+            Posix.execvp (original_args[0], original_args);
         }
 
         return exit_code;
diff --git a/src/rygel/rygel-signal-handler.vala b/src/rygel/rygel-signal-handler.vala
new file mode 100644
index 0000000..2055970
--- /dev/null
+++ b/src/rygel/rygel-signal-handler.vala
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2008,2010 Nokia Corporation.
+ * Copyright (C) 2008 Zeeshan Ali (Khattak) <zeeshanak gnome org>.
+ *
+ * Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+using Posix;
+
+/**
+ * Handles Posix signals.
+ */
+public class Rygel.SignalHandler : GLib.Object {
+    private static SignalHandler handler;
+
+    public signal void exit ();
+    public signal void restart ();
+
+    private sigaction_t action;
+
+    public static SignalHandler get_default () {
+        if (handler == null) {
+            handler = new SignalHandler ();
+        }
+
+        return handler;
+    }
+
+    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);
+    }
+
+    private static void signal_handler (int signum) {
+        if (signum == SIGHUP) {
+            handler.restart ();
+        } else {
+            handler.exit ();
+        }
+    }
+}
+



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