Commit 963544e4 authored by Martin Kampas's avatar Martin Kampas Committed by Martyn Russell

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
parent cc84fe20
......@@ -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 */
}
......
......@@ -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 */
}
......
......@@ -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 */
}
......
......@@ -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;
......
......@@ -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 ();
}
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment