From 023333688f73ed26097c693d43f76e4b7d5698e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=BCller?= Date: Fri, 7 Jun 2013 18:38:42 +0200 Subject: [PATCH] Add shutdown/reboot functionality for systemd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Upstream-Status: Submitted [2] [1] http://git.xfce.org/xfce/xfce4-session/commit/?id=ae28aef315a7a6b90f1649ce6d1f30b842791cbf [2] https://bugzilla.xfce.org/show_bug.cgi?id=10167 Signed-off-by: Andreas Müller --- configure.ac | 2 + src/Makefile.am | 14 +++- src/xfpm-manager.c | 29 ++++++- src/xfpm-power.c | 66 ++++++++++++++- src/xfpm-systemd.c | 245 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/xfpm-systemd.h | 60 +++++++++++++ 6 files changed, 411 insertions(+), 5 deletions(-) create mode 100644 src/xfpm-systemd.c create mode 100644 src/xfpm-systemd.h diff --git a/configure.ac b/configure.ac index 633b340..1db2036 100644 --- a/configure.ac +++ b/configure.ac @@ -95,6 +95,8 @@ if test "x$ac_cv_enable_polkit" = "xno"; then polkit="no" else AC_MSG_RESULT([yes]) + m4_define([polkit_minimum_version], [0.100]) + XDT_CHECK_PACKAGE([SYSTEMD], [polkit-gobject-1], [polkit_minimum_version]) AC_DEFINE(ENABLE_POLKIT, 1 , [PolicyKit support]) polkit="yes" fi diff --git a/src/Makefile.am b/src/Makefile.am index 0435bed..9b7fdd0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,8 +18,6 @@ xfce4_power_manager_SOURCES = \ xfpm-xfconf.h \ xfpm-disks.c \ xfpm-disks.h \ - xfpm-console-kit.c \ - xfpm-console-kit.h \ egg-idletime.c \ egg-idletime.h \ xfpm-backlight.c \ @@ -39,6 +37,16 @@ xfce4_power_manager_SOURCES = \ xfpm-errors.c \ xfpm-errors.h +if ENABLE_POLKIT +xfce4_power_manager_SOURCES += \ + xfpm-systemd.c \ + xfpm-systemd.h +else +xfce4_power_manager_SOURCES += \ + xfpm-console-kit.c \ + xfpm-console-kit.h +endif + xfce4_power_manager_CFLAGS = \ -I$(top_srcdir) \ -I$(top_srcdir)/common \ @@ -52,6 +60,7 @@ xfce4_power_manager_CFLAGS = \ $(LIBXFCE4UI_CFLAGS) \ $(XFCONF_CFLAGS) \ $(LIBNOTIFY_CFLAGS) \ + $(SYSTEMD_CFLAGS) \ $(XRANDR_CFLAGS) \ $(DPMS_CFLAGS) \ $(PLATFORM_CPPFLAGS) \ @@ -70,6 +79,7 @@ xfce4_power_manager_LDADD = \ $(LIBXFCE4UI_LIBS) \ $(XFCONF_LIBS) \ $(LIBNOTIFY_LIBS) \ + $(SYSTEMD_LIBS) \ $(XRANDR_LIBS) \ $(DPMS_LIBS) diff --git a/src/xfpm-manager.c b/src/xfpm-manager.c index 472ee3e..9bfb53c 100644 --- a/src/xfpm-manager.c +++ b/src/xfpm-manager.c @@ -43,7 +43,6 @@ #include "xfpm-disks.h" #include "xfpm-dpms.h" #include "xfpm-manager.h" -#include "xfpm-console-kit.h" #include "xfpm-button.h" #include "xfpm-backlight.h" #include "xfpm-inhibit.h" @@ -58,6 +57,12 @@ #include "xfpm-enum-types.h" #include "xfpm-dbus-monitor.h" +#ifdef ENABLE_POLKIT +#include "xfpm-systemd.h" +#else +#include "xfpm-console-kit.h" +#endif + static void xfpm_manager_finalize (GObject *object); static void xfpm_manager_dbus_class_init (XfpmManagerClass *klass); @@ -80,7 +85,11 @@ struct XfpmManagerPrivate XfpmButton *button; XfpmXfconf *conf; XfpmBacklight *backlight; +#ifdef ENABLE_POLKIT + XfpmSystemd *systemd; +#else XfpmConsoleKit *console; +#endif XfpmDBusMonitor *monitor; XfpmDisks *disks; XfpmInhibit *inhibit; @@ -131,7 +140,11 @@ xfpm_manager_finalize (GObject *object) g_object_unref (manager->priv->button); g_object_unref (manager->priv->conf); g_object_unref (manager->priv->client); +#ifdef ENABLE_POLKIT + g_object_unref (manager->priv->systemd); +#else g_object_unref (manager->priv->console); +#endif g_object_unref (manager->priv->monitor); g_object_unref (manager->priv->disks); g_object_unref (manager->priv->inhibit); @@ -201,7 +214,11 @@ static void xfpm_manager_shutdown (XfpmManager *manager) { GError *error = NULL; +#ifdef ENABLE_POLKIT + xfpm_systemd_shutdown (manager->priv->systemd, &error ); +#else xfpm_console_kit_shutdown (manager->priv->console, &error ); +#endif if ( error ) { @@ -521,7 +538,11 @@ void xfpm_manager_start (XfpmManager *manager) manager->priv->power = xfpm_power_get (); manager->priv->button = xfpm_button_new (); manager->priv->conf = xfpm_xfconf_new (); +#ifdef ENABLE_POLKIT + manager->priv->systemd = xfpm_systemd_new (); +#else manager->priv->console = xfpm_console_kit_new (); +#endif manager->priv->monitor = xfpm_dbus_monitor_new (); manager->priv->disks = xfpm_disks_new (); manager->priv->inhibit = xfpm_inhibit_new (); @@ -603,9 +624,15 @@ GHashTable *xfpm_manager_get_config (XfpmManager *manager) hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); +#ifdef ENABLE_POLKIT + g_object_get (G_OBJECT (manager->priv->systemd), + "can-shutdown", &can_shutdown, + NULL); +#else g_object_get (G_OBJECT (manager->priv->console), "can-shutdown", &can_shutdown, NULL); +#endif g_object_get (G_OBJECT (manager->priv->power), "auth-suspend", &auth_suspend, diff --git a/src/xfpm-power.c b/src/xfpm-power.c index 796bef8..391c7fc 100644 --- a/src/xfpm-power.c +++ b/src/xfpm-power.c @@ -39,7 +39,6 @@ #include "xfpm-xfconf.h" #include "xfpm-notify.h" #include "xfpm-errors.h" -#include "xfpm-console-kit.h" #include "xfpm-inhibit.h" #include "xfpm-polkit.h" #include "xfpm-network-manager.h" @@ -51,6 +50,12 @@ #include "xfpm-enum-types.h" #include "egg-idletime.h" +#ifdef ENABLE_POLKIT +#include "xfpm-systemd.h" +#else +#include "xfpm-console-kit.h" +#endif + static void xfpm_power_finalize (GObject *object); static void xfpm_power_get_property (GObject *object, @@ -75,7 +80,11 @@ struct XfpmPowerPrivate GHashTable *hash; +#ifdef ENABLE_POLKIT + XfpmSystemd *systemd; +#else XfpmConsoleKit *console; +#endif XfpmInhibit *inhibit; XfpmXfconf *conf; GtkStatusIcon *adapter_icon; @@ -680,9 +689,15 @@ xfpm_power_add_actions_to_notification (XfpmPower *power, NotifyNotification *n) { gboolean can_shutdown; +#ifdef ENABLE_POLKIT + g_object_get (G_OBJECT (power->priv->systemd), + "can-shutdown", &can_shutdown, + NULL); +#else g_object_get (G_OBJECT (power->priv->console), "can-shutdown", &can_shutdown, NULL); +#endif if ( power->priv->can_hibernate && power->priv->auth_hibernate ) { @@ -756,9 +771,15 @@ xfpm_power_show_critical_action_gtk (XfpmPower *power) const gchar *message; gboolean can_shutdown; +#ifdef ENABLE_POLKIT + g_object_get (G_OBJECT (power->priv->systemd), + "can-shutdown", &can_shutdown, + NULL); +#else g_object_get (G_OBJECT (power->priv->console), "can-shutdown", &can_shutdown, NULL); +#endif message = _("System is running on low power. "\ "Save your work to avoid losing data"); @@ -1327,7 +1348,11 @@ xfpm_power_init (XfpmPower *power) power->priv->inhibit = xfpm_inhibit_new (); power->priv->notify = xfpm_notify_new (); power->priv->conf = xfpm_xfconf_new (); +#ifdef ENABLE_POLKIT + power->priv->systemd = xfpm_systemd_new (); +#else power->priv->console = xfpm_console_kit_new (); +#endif g_signal_connect_swapped (power->priv->conf, "notify::" SHOW_TRAY_ICON_CFG, G_CALLBACK (xfpm_power_refresh_adaptor_visible), power); @@ -1445,7 +1470,11 @@ xfpm_power_finalize (GObject *object) g_object_unref (power->priv->inhibit); g_object_unref (power->priv->notify); g_object_unref (power->priv->conf); +#ifdef ENABLE_POLKIT + g_object_unref (power->priv->systemd); +#else g_object_unref (power->priv->console); +#endif xfpm_power_hide_adapter_icon (power); @@ -1607,9 +1636,15 @@ static gboolean xfpm_power_dbus_shutdown (XfpmPower *power, { gboolean can_reboot; +#ifdef ENABLE_POLKIT + g_object_get (G_OBJECT (power->priv->systemd), + "can-shutdown", &can_reboot, + NULL); +#else g_object_get (G_OBJECT (power->priv->console), "can-shutdown", &can_reboot, NULL); +#endif if ( !can_reboot) { @@ -1618,7 +1653,11 @@ static gboolean xfpm_power_dbus_shutdown (XfpmPower *power, return FALSE; } +#ifdef ENABLE_POLKIT + xfpm_systemd_shutdown (power->priv->systemd, error); +#else xfpm_console_kit_shutdown (power->priv->console, error); +#endif return TRUE; } @@ -1628,9 +1667,15 @@ static gboolean xfpm_power_dbus_reboot (XfpmPower *power, { gboolean can_reboot; +#ifdef ENABLE_POLKIT + g_object_get (G_OBJECT (power->priv->systemd), + "can-restart", &can_reboot, + NULL); +#else g_object_get (G_OBJECT (power->priv->console), "can-restart", &can_reboot, NULL); +#endif if ( !can_reboot) { @@ -1639,7 +1684,11 @@ static gboolean xfpm_power_dbus_reboot (XfpmPower *power, return FALSE; } +#ifdef ENABLE_POLKIT + xfpm_systemd_reboot (power->priv->systemd, error); +#else xfpm_console_kit_reboot (power->priv->console, error); +#endif return TRUE; } @@ -1694,10 +1743,16 @@ static gboolean xfpm_power_dbus_can_reboot (XfpmPower * power, gboolean * OUT_can_reboot, GError ** error) { +#ifdef ENABLE_POLKIT + g_object_get (G_OBJECT (power->priv->systemd), + "can-reboot", OUT_can_reboot, + NULL); +#else g_object_get (G_OBJECT (power->priv->console), "can-reboot", OUT_can_reboot, NULL); - +#endif + return TRUE; } @@ -1705,9 +1760,16 @@ static gboolean xfpm_power_dbus_can_shutdown (XfpmPower * power, gboolean * OUT_can_shutdown, GError ** error) { +#ifdef ENABLE_POLKIT + g_object_get (G_OBJECT (power->priv->systemd), + "can-shutdown", OUT_can_shutdown, + NULL); +#else g_object_get (G_OBJECT (power->priv->console), "can-shutdown", OUT_can_shutdown, NULL); +#endif + return TRUE; } diff --git a/src/xfpm-systemd.c b/src/xfpm-systemd.c new file mode 100644 index 0000000..cc1cba1 --- /dev/null +++ b/src/xfpm-systemd.c @@ -0,0 +1,245 @@ +/* + * * Copyright (C) 2009-2011 Ali + * * Copyright (C) 2013 Andreas Müller + * + * Licensed under the GNU General Public License Version 2 + * + * 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. + * + * You should have received a copy of the GNU 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 + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#include +#include + +#include "xfpm-systemd.h" + + +static void xfpm_systemd_finalize (GObject *object); + +static void xfpm_systemd_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); + +#define XFPM_SYSTEMD_GET_PRIVATE(o) \ +(G_TYPE_INSTANCE_GET_PRIVATE ((o), XFPM_TYPE_SYSTEMD, XfpmSystemdPrivate)) + +struct XfpmSystemdPrivate +{ + gboolean can_shutdown; + gboolean can_restart; + + PolkitAuthority *authority; + PolkitSubject *subject; +}; + +enum +{ + PROP_0, + PROP_CAN_RESTART, + PROP_CAN_SHUTDOWN +}; + +G_DEFINE_TYPE (XfpmSystemd, xfpm_systemd, G_TYPE_OBJECT) + +#define SYSTEMD_DBUS_NAME "org.freedesktop.login1" +#define SYSTEMD_DBUS_PATH "/org/freedesktop/login1" +#define SYSTEMD_DBUS_INTERFACE "org.freedesktop.login1.Manager" +#define SYSTEMD_REBOOT_ACTION "Reboot" +#define SYSTEMD_POWEROFF_ACTION "PowerOff" +#define SYSTEMD_REBOOT_TEST "org.freedesktop.login1.reboot" +#define SYSTEMD_POWEROFF_TEST "org.freedesktop.login1.power-off" + +static void +xfpm_systemd_class_init (XfpmSystemdClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = xfpm_systemd_finalize; + + object_class->get_property = xfpm_systemd_get_property; + + g_object_class_install_property (object_class, + PROP_CAN_RESTART, + g_param_spec_boolean ("can-restart", + NULL, NULL, + FALSE, + G_PARAM_READABLE)); + + g_object_class_install_property (object_class, + PROP_CAN_SHUTDOWN, + g_param_spec_boolean ("can-shutdown", + NULL, NULL, + FALSE, + G_PARAM_READABLE)); + + g_type_class_add_private (klass, sizeof (XfpmSystemdPrivate)); +} + +static gboolean +xfpm_systemd_can_method (XfpmSystemd *systemd, + gboolean *can_method, + const gchar *method) +{ + PolkitAuthorizationResult *res; + GError *local_error = NULL; + + *can_method = FALSE; + res = polkit_authority_check_authorization_sync (systemd->priv->authority, + systemd->priv->subject, + method, + NULL, + POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE, + NULL, + &local_error); + if ( local_error ) + { + g_critical ("Unable to get %s : %s", method, local_error->message); + g_error_free (local_error); + } + + if(res) + { + *can_method = polkit_authorization_result_get_is_authorized (res) || + polkit_authorization_result_get_is_challenge (res); + g_object_unref (G_OBJECT (res)); + } +} + +static void +xfpm_systemd_init (XfpmSystemd *systemd) +{ + PolkitAuthorizationResult *res; + + systemd->priv = XFPM_SYSTEMD_GET_PRIVATE (systemd); + systemd->priv->authority = polkit_authority_get_sync (NULL, NULL); + systemd->priv->subject = polkit_unix_process_new (getpid()); + systemd->priv->can_shutdown = FALSE; + systemd->priv->can_restart = FALSE; + + if(systemd->priv->authority && systemd->priv->subject) + { + xfpm_systemd_can_method (systemd, + &systemd->priv->can_shutdown, + SYSTEMD_POWEROFF_TEST); + xfpm_systemd_can_method (systemd, + &systemd->priv->can_restart, + SYSTEMD_REBOOT_TEST); + } +} + +static void xfpm_systemd_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + XfpmSystemd *systemd; + systemd = XFPM_SYSTEMD (object); + + switch (prop_id) + { + case PROP_CAN_SHUTDOWN: + g_value_set_boolean (value, systemd->priv->can_shutdown); + break; + case PROP_CAN_RESTART: + g_value_set_boolean (value, systemd->priv->can_restart); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +xfpm_systemd_finalize (GObject *object) +{ + XfpmSystemd *systemd; + + systemd = XFPM_SYSTEMD (object); + + if(systemd->priv->authority) + g_object_unref (G_OBJECT (systemd->priv->authority)); + if(systemd->priv->subject) + g_object_unref (G_OBJECT (systemd->priv->subject)); + + G_OBJECT_CLASS (xfpm_systemd_parent_class)->finalize (object); +} + +XfpmSystemd * +xfpm_systemd_new (void) +{ + static gpointer systemd_obj = NULL; + + if ( G_LIKELY (systemd_obj != NULL ) ) + { + g_object_ref (systemd_obj); + } + else + { + systemd_obj = g_object_new (XFPM_TYPE_SYSTEMD, NULL); + g_object_add_weak_pointer (systemd_obj, &systemd_obj); + } + + return XFPM_SYSTEMD (systemd_obj); +} + +static void +xfpm_systemd_try_method (XfpmSystemd *systemd, + const gchar *method, + GError **error) +{ + GDBusConnection *bus; + GError *local_error = NULL; + + bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error); + if (G_LIKELY (bus != NULL)) + { + g_dbus_connection_call_sync (bus, + SYSTEMD_DBUS_NAME, + SYSTEMD_DBUS_PATH, + SYSTEMD_DBUS_INTERFACE, + method, + g_variant_new ("(b)", TRUE), + NULL, 0, G_MAXINT, NULL, + &local_error); + g_object_unref (G_OBJECT (bus)); + + if (local_error != NULL) + { + g_propagate_error (error, local_error); + } + } +} + +void xfpm_systemd_shutdown (XfpmSystemd *systemd, GError **error) +{ + xfpm_systemd_try_method (systemd, + SYSTEMD_POWEROFF_ACTION, + error); +} + +void xfpm_systemd_reboot (XfpmSystemd *systemd, GError **error) +{ + xfpm_systemd_try_method (systemd, + SYSTEMD_REBOOT_ACTION, + error); +} diff --git a/src/xfpm-systemd.h b/src/xfpm-systemd.h new file mode 100644 index 0000000..2890e08 --- /dev/null +++ b/src/xfpm-systemd.h @@ -0,0 +1,60 @@ +/* + * * Copyright (C) 2009-2011 Ali + * * Copyright (C) 2013 Andreas Müller + * + * Licensed under the GNU General Public License Version 2 + * + * 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. + * + * You should have received a copy of the GNU 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 + */ + +#ifndef __XFPM_SYSTEMD_H +#define __XFPM_SYSTEMD_H + +#include + +G_BEGIN_DECLS + +#define XFPM_TYPE_SYSTEMD (xfpm_systemd_get_type () ) +#define XFPM_SYSTEMD(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), XFPM_TYPE_SYSTEMD, XfpmSystemd)) +#define XFPM_IS_SYSTEMD(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), XFPM_TYPE_SYSTEMD)) + +typedef struct XfpmSystemdPrivate XfpmSystemdPrivate; + +typedef struct +{ + GObject parent; + XfpmSystemdPrivate *priv; + +} XfpmSystemd; + +typedef struct +{ + GObjectClass parent_class; + +} XfpmSystemdClass; + +GType XFPM_SYSTEMD_get_type (void) G_GNUC_CONST; + +XfpmSystemd *XFPM_SYSTEMD_new (void); + +void XFPM_SYSTEMD_shutdown (XfpmSystemd *systemd, + GError **error); + +void XFPM_SYSTEMD_reboot (XfpmSystemd *systemd, + GError **error); + +G_END_DECLS + +#endif /* __XFPM_SYSTEMD_H */ -- 1.7.6.5