From 4cb4011915c2638e3426afd78c0677bcd28d1dbc Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 12 Oct 2009 18:18:29 -0400 Subject: [PATCH 03/21] Unify the restart and stop code paths --- src/ck-manager.c | 147 +++++++++++++++++++----------------------------------- 1 files changed, 51 insertions(+), 96 deletions(-) diff --git a/src/ck-manager.c b/src/ck-manager.c index 99693a3..fdc290a 100644 --- a/src/ck-manager.c +++ b/src/ck-manager.c @@ -83,6 +83,8 @@ struct CkManagerPrivate gboolean system_idle_hint; GTimeVal system_idle_since_hint; + + gboolean restart; }; enum { @@ -456,29 +458,9 @@ log_system_stop_event (CkManager *manager) memset (&event, 0, sizeof (CkLogEvent)); - event.type = CK_LOG_EVENT_SYSTEM_STOP; - g_get_current_time (&event.timestamp); - - error = NULL; - res = ck_event_logger_queue_event (manager->priv->logger, &event, &error); - if (! res) { - g_debug ("Unable to log event: %s", error->message); - g_error_free (error); - } - - /* FIXME: in this case we should block and wait for log to flush */ -} - -static void -log_system_restart_event (CkManager *manager) -{ - CkLogEvent event; - gboolean res; - GError *error; - - memset (&event, 0, sizeof (CkLogEvent)); - - event.type = CK_LOG_EVENT_SYSTEM_RESTART; + event.type = manager->priv->restart + ? CK_LOG_EVENT_SYSTEM_RESTART + : CK_LOG_EVENT_SYSTEM_STOP; g_get_current_time (&event.timestamp); error = NULL; @@ -1072,27 +1054,29 @@ out: #endif static void -do_restart (CkManager *manager, - DBusGMethodInvocation *context) +do_stop (CkManager *manager, + DBusGMethodInvocation *context) { GError *error; gboolean res; - g_debug ("ConsoleKit preforming Restart"); + g_debug ("ConsoleKit preforming %s", manager->priv->restart ? "Restart" : "Stop"); - log_system_restart_event (manager); + log_system_stop_event (manager); error = NULL; - res = g_spawn_command_line_async (PREFIX "/lib/ConsoleKit/scripts/ck-system-restart", + res = g_spawn_command_line_async (manager->priv->restart + ? PREFIX "/lib/ConsoleKit/scripts/ck-system-restart" + : PREFIX "/lib/ConsoleKit/scripts/ck-system-stop", &error); if (! res) { GError *new_error; - g_warning ("Unable to restart system: %s", error->message); + g_warning ("Unable to restart/stop system: %s", error->message); new_error = g_error_new (CK_MANAGER_ERROR, CK_MANAGER_ERROR_GENERAL, - "Unable to restart system: %s", error->message); + "Unable to restart/stop system: %s", error->message); dbus_g_method_return_error (context, new_error); g_error_free (new_error); @@ -1102,31 +1086,32 @@ do_restart (CkManager *manager, } } -/* - Example: - dbus-send --system --dest=org.freedesktop.ConsoleKit \ - --type=method_call --print-reply --reply-timeout=2000 \ - /org/freedesktop/ConsoleKit/Manager \ - org.freedesktop.ConsoleKit.Manager.Restart -*/ -gboolean -ck_manager_restart (CkManager *manager, - DBusGMethodInvocation *context) +static gboolean +restart_or_stop (CkManager *manager, + DBusGMethodInvocation *context) { const char *action; if (get_system_num_users (manager) > 1) { - action = "org.freedesktop.consolekit.system.restart-multiple-users"; + if (manager->priv->restart) { + action = "org.freedesktop.consolekit.system.restart-multiple-users"; + } else { + action = "org.freedesktop.consolekit.system.stop-multiple-users"; + } } else { - action = "org.freedesktop.consolekit.system.restart"; + if (manager->priv->restart) { + action = "org.freedesktop.consolekit.system.restart"; + } else { + action = "org.freedesktop.consolekit.system.stop"; + } } - g_debug ("ConsoleKit Restart: %s", action); + g_debug ("ConsoleKit Stop: %s", action); #if defined HAVE_POLKIT - check_polkit_permissions (manager, context, action, do_restart); + check_polkit_permissions (manager, context, action, do_stop); #elif defined ENABLE_RBAC_SHUTDOWN - check_rbac_permissions (manager, context, do_restart); + check_rbac_permissions (manager, context, do_stop); #else g_warning ("Compiled without PolicyKit or RBAC support!"); #endif @@ -1134,72 +1119,42 @@ ck_manager_restart (CkManager *manager, return TRUE; } +/* + Example: + dbus-send --system --dest=org.freedesktop.ConsoleKit \ + --type=method_call --print-reply --reply-timeout=2000 \ + /org/freedesktop/ConsoleKit/Manager \ + org.freedesktop.ConsoleKit.Manager.Restart +*/ gboolean -ck_manager_can_restart (CkManager *manager, +ck_manager_restart (CkManager *manager, DBusGMethodInvocation *context) - { - const char *action; - - action = "org.freedesktop.consolekit.system.restart"; - -#if defined HAVE_POLKIT - get_polkit_permissions (manager, action, context); -#else - dbus_g_method_return (context, TRUE); -#endif - - return TRUE; + manager->priv->restart = TRUE; + return restart_or_stop (manager, context); } -static void -do_stop (CkManager *manager, - DBusGMethodInvocation *context) +gboolean +ck_manager_stop (CkManager *manager, + DBusGMethodInvocation *context) { - GError *error; - gboolean res; - - g_debug ("Stopping system"); - - log_system_stop_event (manager); - - error = NULL; - res = g_spawn_command_line_async (PREFIX "/lib/ConsoleKit/scripts/ck-system-stop", - &error); - if (! res) { - GError *new_error; - - g_warning ("Unable to stop system: %s", error->message); - - new_error = g_error_new (CK_MANAGER_ERROR, - CK_MANAGER_ERROR_GENERAL, - "Unable to stop system: %s", error->message); - dbus_g_method_return_error (context, new_error); - g_error_free (new_error); - g_error_free (error); - } else { - dbus_g_method_return (context); - } + manager->priv->restart = FALSE; + return restart_or_stop (manager, context); } gboolean -ck_manager_stop (CkManager *manager, - DBusGMethodInvocation *context) +ck_manager_can_restart (CkManager *manager, + DBusGMethodInvocation *context) + { const char *action; - if (get_system_num_users (manager) > 1) { - action = "org.freedesktop.consolekit.system.stop-multiple-users"; - } else { - action = "org.freedesktop.consolekit.system.stop"; - } + action = "org.freedesktop.consolekit.system.restart"; #if defined HAVE_POLKIT - check_polkit_permissions (manager, context, action, do_stop); -#elif defined ENABLE_RBAC_SHUTDOWN - check_rbac_permissions (manager, context, do_stop); + get_polkit_permissions (manager, action, context); #else - g_warning ("Compiled without PolicyKit or RBAC support!"); + dbus_g_method_return (context, TRUE); #endif return TRUE; -- 1.6.5.rc2