From 983e3c994750e1fdeb439ce6dd9d430e2f6f3f84 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Thu, 18 Jun 2009 23:22:07 -0400 Subject: core: gracefully handle SIGTERM and SIGINT by shutting down cleanly --- src/main.c | 32 +++++++++++++++++++++++++++----- src/mm-manager.c | 2 +- 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 99257067..36691150 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,18 @@ /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details: + * + * Copyright (C) 2008 - 2009 Novell, Inc. + * Copyright (C) 2009 Red Hat, Inc. + */ #include #include @@ -10,11 +24,17 @@ #define HAL_DBUS_SERVICE "org.freedesktop.Hal" +static GMainLoop *loop = NULL; + static void mm_signal_handler (int signo) { if (signo == SIGUSR1) mm_options_set_debug (!mm_options_debug ()); + else if (signo == SIGINT || signo == SIGTERM) { + g_message ("Caught signal %d, shutting down...", signo); + g_main_loop_quit (loop); + } } static void @@ -27,7 +47,9 @@ setup_signals (void) action.sa_handler = mm_signal_handler; action.sa_mask = mask; action.sa_flags = 0; - sigaction (SIGUSR1, &action, NULL); + sigaction (SIGUSR1, &action, NULL); + sigaction (SIGTERM, &action, NULL); + sigaction (SIGINT, &action, NULL); } static void @@ -88,8 +110,6 @@ logging_shutdown (void) static void destroy_cb (DBusGProxy *proxy, gpointer user_data) { - GMainLoop *loop = (GMainLoop *) user_data; - g_message ("disconnected from the system bus, exiting."); g_main_loop_quit (loop); } @@ -146,9 +166,9 @@ main (int argc, char *argv[]) { DBusGConnection *bus; DBusGProxy *proxy; - GMainLoop *loop; MMManager *manager; GError *err = NULL; + guint id; mm_options_parse (argc, argv); g_type_init (); @@ -175,10 +195,12 @@ main (int argc, char *argv[]) g_idle_add (start_manager, manager); loop = g_main_loop_new (NULL, FALSE); - g_signal_connect (proxy, "destroy", G_CALLBACK (destroy_cb), loop); + id = g_signal_connect (proxy, "destroy", G_CALLBACK (destroy_cb), loop); g_main_loop_run (loop); + g_signal_handler_disconnect (proxy, id); + g_object_unref (manager); g_object_unref (proxy); dbus_g_connection_unref (bus); diff --git a/src/mm-manager.c b/src/mm-manager.c index beed08cc..3d3b1ac1 100644 --- a/src/mm-manager.c +++ b/src/mm-manager.c @@ -140,7 +140,7 @@ mm_manager_new (DBusGConnection *bus) if (manager) { MMManagerPrivate *priv = MM_MANAGER_GET_PRIVATE (manager); - priv->connection = bus; + priv->connection = dbus_g_connection_ref (bus); dbus_g_connection_register_g_object (priv->connection, MM_DBUS_PATH, G_OBJECT (manager)); -- cgit v1.2.3-70-g09d2