[tracker] Use g_unix_signal_add() for signal handlers



commit 963544e4192761d54f334a2eda8e0a33f191df7e
Author: Martin Kampas <martin kampas tieto com>
Date:   Mon Oct 27 07:37:50 2014 +0100

    Use g_unix_signal_add() for signal handlers
    
    Identified by functional-tests/17-ontology-changes timeouting randomly.
    
    Documentation for g_unix_signal_source_new() explains why it is not safe
    to call g_main_loop_quit() from a regular UNIX signal handler.
    
    Intentionally removed the (main_loop != NULL) tests - this cannot happen.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=739234

 src/miners/apps/tracker-main.c        |   30 +++++++++++-------------------
 src/miners/fs/tracker-main.c          |   30 +++++++++++-------------------
 src/miners/user-guides/tracker-main.c |   30 +++++++++++-------------------
 src/tracker-extract/tracker-main.c    |   28 ++++++++++++----------------
 src/tracker-store/tracker-main.vala   |   20 +++++++-------------
 src/tracker/tracker-daemon.c          |   22 +++++++++-------------
 6 files changed, 61 insertions(+), 99 deletions(-)
---
diff --git a/src/miners/apps/tracker-main.c b/src/miners/apps/tracker-main.c
index a6ef75b..6d4f012 100644
--- a/src/miners/apps/tracker-main.c
+++ b/src/miners/apps/tracker-main.c
@@ -21,10 +21,10 @@
 
 #include <stdlib.h>
 #include <locale.h>
-#include <signal.h>
 #include <errno.h>
 
 #include <glib.h>
+#include <glib-unix.h>
 #include <glib-object.h>
 #include <glib/gi18n.h>
 
@@ -67,9 +67,11 @@ static GOptionEntry entries[] = {
        { NULL }
 };
 
-static void
-signal_handler (int signo)
+static gboolean
+signal_handler (gpointer user_data)
 {
+       int signo = GPOINTER_TO_INT (user_data);
+
        static gboolean in_loop = FALSE;
 
        /* Die if we get re-entrant signals handler calls */
@@ -81,11 +83,8 @@ signal_handler (int signo)
        case SIGTERM:
        case SIGINT:
                in_loop = TRUE;
-               if (main_loop != NULL) {
-                       g_main_loop_quit (main_loop);
-               } else {
-                       exit (0);
-               }
+               g_main_loop_quit (main_loop);
+
                /* Fall through */
        default:
                if (g_strsignal (signo)) {
@@ -96,23 +95,16 @@ signal_handler (int signo)
                }
                break;
        }
+
+       return G_SOURCE_CONTINUE;
 }
 
 static void
 initialize_signal_handler (void)
 {
 #ifndef G_OS_WIN32
-       struct sigaction act;
-       sigset_t         empty_mask;
-
-       sigemptyset (&empty_mask);
-       act.sa_handler = signal_handler;
-       act.sa_mask    = empty_mask;
-       act.sa_flags   = 0;
-
-       sigaction (SIGTERM, &act, NULL);
-       sigaction (SIGINT,  &act, NULL);
-       sigaction (SIGHUP,  &act, NULL);
+       g_unix_signal_add (SIGTERM, signal_handler, GINT_TO_POINTER (SIGTERM));
+       g_unix_signal_add (SIGINT, signal_handler, GINT_TO_POINTER (SIGINT));
 #endif /* G_OS_WIN32 */
 }
 
diff --git a/src/miners/fs/tracker-main.c b/src/miners/fs/tracker-main.c
index 2661b88..31b7bc2 100644
--- a/src/miners/fs/tracker-main.c
+++ b/src/miners/fs/tracker-main.c
@@ -22,12 +22,12 @@
 #include <string.h>
 #include <stdlib.h>
 #include <locale.h>
-#include <signal.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 #include <glib.h>
+#include <glib-unix.h>
 #include <glib-object.h>
 #include <glib/gi18n.h>
 
@@ -120,9 +120,11 @@ sanity_check_option_values (TrackerConfig *config)
        }
 }
 
-static void
-signal_handler (int signo)
+static gboolean
+signal_handler (gpointer user_data)
 {
+       int signo = GPOINTER_TO_INT (user_data);
+
        static gboolean in_loop = FALSE;
 
        /* Die if we get re-entrant signals handler calls */
@@ -134,11 +136,8 @@ signal_handler (int signo)
        case SIGTERM:
        case SIGINT:
                in_loop = TRUE;
-               if (main_loop != NULL) {
-                       g_main_loop_quit (main_loop);
-               } else {
-                       exit (0);
-               }
+               g_main_loop_quit (main_loop);
+
                /* Fall through */
        default:
                if (g_strsignal (signo)) {
@@ -149,23 +148,16 @@ signal_handler (int signo)
                }
                break;
        }
+
+       return G_SOURCE_CONTINUE;
 }
 
 static void
 initialize_signal_handler (void)
 {
 #ifndef G_OS_WIN32
-       struct sigaction act;
-       sigset_t         empty_mask;
-
-       sigemptyset (&empty_mask);
-       act.sa_handler = signal_handler;
-       act.sa_mask    = empty_mask;
-       act.sa_flags   = 0;
-
-       sigaction (SIGTERM, &act, NULL);
-       sigaction (SIGINT,  &act, NULL);
-       sigaction (SIGHUP,  &act, NULL);
+       g_unix_signal_add (SIGTERM, signal_handler, GINT_TO_POINTER (SIGTERM));
+       g_unix_signal_add (SIGINT, signal_handler, GINT_TO_POINTER (SIGINT));
 #endif /* G_OS_WIN32 */
 }
 
diff --git a/src/miners/user-guides/tracker-main.c b/src/miners/user-guides/tracker-main.c
index b596225..55920d9 100644
--- a/src/miners/user-guides/tracker-main.c
+++ b/src/miners/user-guides/tracker-main.c
@@ -21,10 +21,10 @@
 
 #include <stdlib.h>
 #include <locale.h>
-#include <signal.h>
 #include <errno.h>
 
 #include <glib.h>
+#include <glib-unix.h>
 #include <glib-object.h>
 #include <glib/gi18n.h>
 
@@ -67,9 +67,11 @@ static GOptionEntry entries[] = {
        { NULL }
 };
 
-static void
-signal_handler (int signo)
+static gboolean
+signal_handler (gpointer user_data)
 {
+       int signo = GPOINTER_TO_INT (user_data);
+
        static gboolean in_loop = FALSE;
 
        /* Die if we get re-entrant signals handler calls */
@@ -81,11 +83,8 @@ signal_handler (int signo)
        case SIGTERM:
        case SIGINT:
                in_loop = TRUE;
-               if (main_loop != NULL) {
-                       g_main_loop_quit (main_loop);
-               } else {
-                       exit (0);
-               }
+               g_main_loop_quit (main_loop);
+
                /* Fall through */
        default:
                if (g_strsignal (signo)) {
@@ -96,23 +95,16 @@ signal_handler (int signo)
                }
                break;
        }
+
+       return G_SOURCE_CONTINUE;
 }
 
 static void
 initialize_signal_handler (void)
 {
 #ifndef G_OS_WIN32
-       struct sigaction act;
-       sigset_t         empty_mask;
-
-       sigemptyset (&empty_mask);
-       act.sa_handler = signal_handler;
-       act.sa_mask    = empty_mask;
-       act.sa_flags   = 0;
-
-       sigaction (SIGTERM, &act, NULL);
-       sigaction (SIGINT,  &act, NULL);
-       sigaction (SIGHUP,  &act, NULL);
+       g_unix_signal_add (SIGTERM, signal_handler, GINT_TO_POINTER (SIGTERM));
+       g_unix_signal_add (SIGINT, signal_handler, GINT_TO_POINTER (SIGINT));
 #endif /* G_OS_WIN32 */
 }
 
diff --git a/src/tracker-extract/tracker-main.c b/src/tracker-extract/tracker-main.c
index 22af4b2..a02cb0e 100644
--- a/src/tracker-extract/tracker-main.c
+++ b/src/tracker-extract/tracker-main.c
@@ -24,12 +24,12 @@
 #include <time.h>
 #include <stdlib.h>
 #include <locale.h>
-#include <signal.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 #include <glib-object.h>
+#include <glib-unix.h>
 #include <glib/gi18n.h>
 #include <glib/gprintf.h>
 #include <gio/gio.h>
@@ -150,9 +150,11 @@ initialize_directories (void)
        g_free (user_data_dir);
 }
 
-static void
-signal_handler (int signo)
+static gboolean
+signal_handler (gpointer user_data)
 {
+       int signo = GPOINTER_TO_INT (user_data);
+
        static gboolean in_loop = FALSE;
 
        /* Die if we get re-entrant signals handler calls */
@@ -176,23 +178,16 @@ signal_handler (int signo)
                }
                break;
        }
+
+       return G_SOURCE_CONTINUE;
 }
 
 static void
 initialize_signal_handler (void)
 {
 #ifndef G_OS_WIN32
-       struct sigaction act;
-       sigset_t         empty_mask;
-
-       sigemptyset (&empty_mask);
-       act.sa_handler = signal_handler;
-       act.sa_mask    = empty_mask;
-       act.sa_flags   = 0;
-
-       sigaction (SIGTERM, &act, NULL);
-       sigaction (SIGINT,  &act, NULL);
-       sigaction (SIGHUP,  &act, NULL);
+       g_unix_signal_add (SIGTERM, signal_handler, GINT_TO_POINTER (SIGTERM));
+       g_unix_signal_add (SIGINT, signal_handler, GINT_TO_POINTER (SIGINT));
 #endif /* G_OS_WIN32 */
 }
 
@@ -385,10 +380,11 @@ main (int argc, char *argv[])
        controller = tracker_extract_controller_new (decorator);
        tracker_miner_start (TRACKER_MINER (decorator));
 
-       initialize_signal_handler ();
-
        /* Main loop */
        main_loop = g_main_loop_new (NULL, FALSE);
+
+       initialize_signal_handler ();
+
        g_main_loop_run (main_loop);
 
        my_main_loop = main_loop;
diff --git a/src/tracker-store/tracker-main.vala b/src/tracker-store/tracker-main.vala
index 8d991c6..4685867 100644
--- a/src/tracker-store/tracker-main.vala
+++ b/src/tracker-store/tracker-main.vala
@@ -77,7 +77,7 @@ License which can be viewed at:
 
        static bool in_loop = false;
 
-       static void signal_handler (int signo) {
+       static bool signal_handler (int signo) {
                /* Die if we get re-entrant signals handler calls */
                if (in_loop) {
                        Process.exit (1);
@@ -101,20 +101,13 @@ License which can be viewed at:
                        }
                        break;
                }
+
+               return true;
        }
 
        static void initialize_signal_handler () {
-               var empty_mask = Posix.sigset_t ();
-               Posix.sigemptyset (empty_mask);
-
-               var act = Posix.sigaction_t ();
-               act.sa_handler = signal_handler;
-               act.sa_mask = empty_mask;
-               act.sa_flags = 0;
-
-               Posix.sigaction (Posix.SIGTERM, act, null);
-               Posix.sigaction (Posix.SIGINT, act, null);
-               Posix.sigaction (Posix.SIGHUP, act, null);
+               Unix.signal_add (Posix.SIGTERM, () => signal_handler (Posix.SIGTERM));
+               Unix.signal_add (Posix.SIGINT, () => signal_handler (Posix.SIGINT));
        }
 
        static void initialize_priority () {
@@ -298,9 +291,10 @@ License which can be viewed at:
                 * doing what they do and shutdown.
                 */
                if (!shutdown) {
+                       main_loop = new MainLoop ();
+
                        initialize_signal_handler ();
 
-                       main_loop = new MainLoop ();
                        main_loop.run ();
                }
 
diff --git a/src/tracker/tracker-daemon.c b/src/tracker/tracker-daemon.c
index f37f1a1..7cda3e3 100644
--- a/src/tracker/tracker-daemon.c
+++ b/src/tracker/tracker-daemon.c
@@ -27,6 +27,7 @@
 #endif
 
 #include <glib.h>
+#include <glib-unix.h>
 #include <glib/gi18n.h>
 #include <glib/gprintf.h>
 
@@ -206,9 +207,11 @@ parse_watch (const gchar  *option_name,
        return TRUE;
 }
 
-static void
-signal_handler (int signo)
+static gboolean
+signal_handler (gpointer user_data)
 {
+       int signo = GPOINTER_TO_INT (user_data);
+
        static gboolean in_loop = FALSE;
 
        /* Die if we get re-entrant signals handler calls */
@@ -232,22 +235,15 @@ signal_handler (int signo)
                }
                break;
        }
+
+       return G_SOURCE_CONTINUE;
 }
 
 static void
 initialize_signal_handler (void)
 {
-       struct sigaction act;
-       sigset_t empty_mask;
-
-       sigemptyset (&empty_mask);
-       act.sa_handler = signal_handler;
-       act.sa_mask = empty_mask;
-       act.sa_flags = 0;
-
-       sigaction (SIGTERM, &act, NULL);
-       sigaction (SIGINT, &act, NULL);
-       sigaction (SIGHUP, &act, NULL);
+       g_unix_signal_add (SIGTERM, signal_handler, GINT_TO_POINTER (SIGTERM));
+       g_unix_signal_add (SIGINT, signal_handler, GINT_TO_POINTER (SIGINT));
 }
 
 static gboolean


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