From 4e3690f0e1a362e9e7932730101b700a2c0da79b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=BCller?= Date: Mon, 30 Dec 2013 13:34:16 +0300 Subject: [PATCH] Add shutdown/reboot functionality for systemd (Bug 10167) based on the xfce4-session implementation [1] [1] http://git.xfce.org/xfce/xfce4-session/commit/?id=ae28aef315a7a6b90f1649ce6d1f30b842791cbf Guido Berhoerster assisted by allowing polkit support without needing systemd. --- configure.ac.in | 4 +- src/Makefile.am | 8 ++ src/xfpm-manager.c | 33 +++++++- src/xfpm-power.c | 74 +++++++++++++++- src/xfpm-systemd.c | 245 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/xfpm-systemd.h | 62 ++++++++++++++ 6 files changed, 423 insertions(+), 3 deletions(-) create mode 100644 src/xfpm-systemd.c create mode 100644 src/xfpm-systemd.h diff --git a/configure.ac.in b/configure.ac.in index 633b340..5d59fc4 100644 --- a/configure.ac.in +++ b/configure.ac.in @@ -82,7 +82,7 @@ XDT_CHECK_PACKAGE([XRANDR],[xrandr], [xrandr_minimum_version]) XDT_CHECK_PACKAGE([X11], [x11], [x11_minimum_version]) #=======================================================# -# Polkit? # +# Polkit? / Systemd through Polkit? # #=======================================================# AC_ARG_ENABLE([polkit], [AC_HELP_STRING([--disable-polkit], @@ -95,6 +95,8 @@ if test "x$ac_cv_enable_polkit" = "xno"; then polkit="no" else AC_MSG_RESULT([yes]) + XDT_CHECK_OPTIONAL_PACKAGE([SYSTEMD], [polkit-gobject-1], [0.100], + [systemd], [Systemd support (through polkit)]) AC_DEFINE(ENABLE_POLKIT, 1 , [PolicyKit support]) polkit="yes" fi diff --git a/src/Makefile.am b/src/Makefile.am index 0435bed..3476fa4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -52,6 +52,7 @@ xfce4_power_manager_CFLAGS = \ $(LIBXFCE4UI_CFLAGS) \ $(XFCONF_CFLAGS) \ $(LIBNOTIFY_CFLAGS) \ + $(SYSTEMD_CFLAGS) \ $(XRANDR_CFLAGS) \ $(DPMS_CFLAGS) \ $(PLATFORM_CPPFLAGS) \ @@ -70,6 +71,7 @@ xfce4_power_manager_LDADD = \ $(LIBXFCE4UI_LIBS) \ $(XFCONF_LIBS) \ $(LIBNOTIFY_LIBS) \ + $(SYSTEMD_LIBS) \ $(XRANDR_LIBS) \ $(DPMS_LIBS) @@ -100,6 +102,12 @@ xfce4_power_information_LDADD = \ $(LIBXFCE4UI_LIBS) \ $(top_builddir)/libdbus/libxfpmdbus.la +if HAVE_SYSTEMD +xfce4_power_manager_SOURCES += \ + xfpm-systemd.c \ + xfpm-systemd.h +endif + if ENABLE_POLKIT sbin_PROGRAMS = xfpm-power-backlight-helper diff --git a/src/xfpm-manager.c b/src/xfpm-manager.c index 853d78c..2c73f14 100644 --- a/src/xfpm-manager.c +++ b/src/xfpm-manager.c @@ -58,6 +58,10 @@ #include "xfpm-enum-types.h" #include "xfpm-dbus-monitor.h" +#ifdef HAVE_SYSTEMD +#include "xfpm-systemd.h" +#endif + static void xfpm_manager_finalize (GObject *object); static void xfpm_manager_dbus_class_init (XfpmManagerClass *klass); @@ -81,6 +85,9 @@ struct XfpmManagerPrivate XfpmXfconf *conf; XfpmBacklight *backlight; XfpmConsoleKit *console; +#ifdef HAVE_SYSTEMD + XfpmSystemd *systemd; +#endif XfpmDBusMonitor *monitor; XfpmDisks *disks; XfpmInhibit *inhibit; @@ -131,7 +138,12 @@ xfpm_manager_finalize (GObject *object) g_object_unref (manager->priv->button); g_object_unref (manager->priv->conf); g_object_unref (manager->priv->client); - g_object_unref (manager->priv->console); +#ifdef HAVE_SYSTEMD + if ( manager->priv->systemd != NULL ) + g_object_unref (manager->priv->systemd); +#endif + if ( manager->priv->console != NULL ) + g_object_unref (manager->priv->console); g_object_unref (manager->priv->monitor); g_object_unref (manager->priv->disks); g_object_unref (manager->priv->inhibit); @@ -201,6 +213,11 @@ static void xfpm_manager_shutdown (XfpmManager *manager) { GError *error = NULL; +#ifdef HAVE_SYSTEMD + if ( LOGIND_RUNNING () ) + xfpm_systemd_shutdown (manager->priv->systemd, &error ); + else +#endif xfpm_console_kit_shutdown (manager->priv->console, &error ); if ( error ) @@ -521,6 +538,13 @@ 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 HAVE_SYSTEMD + manager->priv->console = NULL; + manager->priv->systemd = NULL; + if ( LOGIND_RUNNING () ) + manager->priv->systemd = xfpm_systemd_new (); + else +#endif manager->priv->console = xfpm_console_kit_new (); manager->priv->monitor = xfpm_dbus_monitor_new (); manager->priv->disks = xfpm_disks_new (); @@ -603,6 +627,13 @@ GHashTable *xfpm_manager_get_config (XfpmManager *manager) hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); +#ifdef HAVE_SYSTEMD + if ( LOGIND_RUNNING () ) + g_object_get (G_OBJECT (manager->priv->systemd), + "can-shutdown", &can_shutdown, + NULL); + else +#endif g_object_get (G_OBJECT (manager->priv->console), "can-shutdown", &can_shutdown, NULL); diff --git a/src/xfpm-power.c b/src/xfpm-power.c index d703f8e..b83fcb2 100644 --- a/src/xfpm-power.c +++ b/src/xfpm-power.c @@ -51,6 +51,10 @@ #include "xfpm-enum-types.h" #include "egg-idletime.h" +#ifdef HAVE_SYSTEMD +#include "xfpm-systemd.h" +#endif + static void xfpm_power_finalize (GObject *object); static void xfpm_power_get_property (GObject *object, @@ -75,6 +79,9 @@ struct XfpmPowerPrivate GHashTable *hash; +#ifdef HAVE_SYSTEMD + XfpmSystemd *systemd; +#endif XfpmConsoleKit *console; XfpmInhibit *inhibit; XfpmXfconf *conf; @@ -680,6 +687,13 @@ xfpm_power_add_actions_to_notification (XfpmPower *power, NotifyNotification *n) { gboolean can_shutdown; +#ifdef HAVE_SYSTEMD + if ( LOGIND_RUNNING () ) + g_object_get (G_OBJECT (power->priv->systemd), + "can-shutdown", &can_shutdown, + NULL); + else +#endif g_object_get (G_OBJECT (power->priv->console), "can-shutdown", &can_shutdown, NULL); @@ -756,6 +770,13 @@ xfpm_power_show_critical_action_gtk (XfpmPower *power) const gchar *message; gboolean can_shutdown; +#ifdef HAVE_SYSTEMD + if ( LOGIND_RUNNING () ) + g_object_get (G_OBJECT (power->priv->systemd), + "can-shutdown", &can_shutdown, + NULL); + else +#endif g_object_get (G_OBJECT (power->priv->console), "can-shutdown", &can_shutdown, NULL); @@ -1327,6 +1348,13 @@ xfpm_power_init (XfpmPower *power) power->priv->inhibit = xfpm_inhibit_new (); power->priv->notify = xfpm_notify_new (); power->priv->conf = xfpm_xfconf_new (); +#ifdef HAVE_SYSTEMD + power->priv->systemd = NULL; + power->priv->console = NULL; + if ( LOGIND_RUNNING () ) + power->priv->systemd = xfpm_systemd_new (); + else +#endif power->priv->console = xfpm_console_kit_new (); g_signal_connect_swapped (power->priv->conf, "notify::" SHOW_TRAY_ICON_CFG, @@ -1445,7 +1473,12 @@ xfpm_power_finalize (GObject *object) g_object_unref (power->priv->inhibit); g_object_unref (power->priv->notify); g_object_unref (power->priv->conf); - g_object_unref (power->priv->console); +#ifdef HAVE_SYSTEMD + if ( power->priv->systemd != NULL ) + g_object_unref (power->priv->systemd); +#endif + if ( power->priv->console != NULL ) + g_object_unref (power->priv->console); xfpm_power_hide_adapter_icon (power); @@ -1607,6 +1640,13 @@ static gboolean xfpm_power_dbus_shutdown (XfpmPower *power, { gboolean can_reboot; +#ifdef HAVE_SYSTEMD + if ( LOGIND_RUNNING () ) + g_object_get (G_OBJECT (power->priv->systemd), + "can-shutdown", &can_reboot, + NULL); + else +#endif g_object_get (G_OBJECT (power->priv->console), "can-shutdown", &can_reboot, NULL); @@ -1618,6 +1658,11 @@ static gboolean xfpm_power_dbus_shutdown (XfpmPower *power, return FALSE; } +#ifdef HAVE_SYSTEMD + if ( LOGIND_RUNNING () ) + xfpm_systemd_shutdown (power->priv->systemd, error); + else +#endif xfpm_console_kit_shutdown (power->priv->console, error); return TRUE; @@ -1628,6 +1673,13 @@ static gboolean xfpm_power_dbus_reboot (XfpmPower *power, { gboolean can_reboot; +#ifdef HAVE_SYSTEMD + if ( LOGIND_RUNNING () ) + g_object_get (G_OBJECT (power->priv->systemd), + "can-restart", &can_reboot, + NULL); + else +#endif g_object_get (G_OBJECT (power->priv->console), "can-restart", &can_reboot, NULL); @@ -1639,8 +1691,14 @@ static gboolean xfpm_power_dbus_reboot (XfpmPower *power, return FALSE; } +#ifdef HAVE_SYSTEMD + if ( LOGIND_RUNNING () ) + xfpm_systemd_reboot (power->priv->systemd, error); + else +#endif xfpm_console_kit_reboot (power->priv->console, error); + return TRUE; } @@ -1694,6 +1752,13 @@ static gboolean xfpm_power_dbus_can_reboot (XfpmPower * power, gboolean * OUT_can_reboot, GError ** error) { +#ifdef HAVE_SYSTEMD + if ( LOGIND_RUNNING () ) + g_object_get (G_OBJECT (power->priv->systemd), + "can-reboot", OUT_can_reboot, + NULL); + else +#endif g_object_get (G_OBJECT (power->priv->console), "can-reboot", OUT_can_reboot, NULL); @@ -1705,6 +1770,13 @@ static gboolean xfpm_power_dbus_can_shutdown (XfpmPower * power, gboolean * OUT_can_shutdown, GError ** error) { +#ifdef HAVE_SYSTEMD + if ( LOGIND_RUNNING () ) + g_object_get (G_OBJECT (power->priv->systemd), + "can-shutdown", OUT_can_shutdown, + NULL); + else +#endif g_object_get (G_OBJECT (power->priv->console), "can-shutdown", OUT_can_shutdown, NULL); diff --git a/src/xfpm-systemd.c b/src/xfpm-systemd.c new file mode 100644 index 0000000..eb0f545 --- /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)); + } + + return TRUE; +} + +static void +xfpm_systemd_init (XfpmSystemd *systemd) +{ + 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..7ade4f4 --- /dev/null +++ b/src/xfpm-systemd.h @@ -0,0 +1,62 @@ +/* + * * 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 LOGIND_RUNNING() (access ("/run/systemd/seats/", F_OK) >= 0) + +#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.8.3.2