[Xfce4-commits] [xfce/xfce4-session] 02/02: Add ConsoleKit2 support

noreply at xfce.org noreply at xfce.org
Sun Dec 7 08:59:32 CET 2014


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository xfce/xfce4-session.

commit 2cd4ba484bfec3354a2bbc58f94e93f874757f1e
Author: Eric Koegel <eric.koegel at gmail.com>
Date:   Mon Nov 3 07:45:36 2014 +0300

    Add ConsoleKit2 support
    
    This patch:
    - Moves the shutdown fallback code into its own files
    - Adds support for ConsoleKit2
    - Simplifies the upower version check to one spot. To do that,
      every shutdown backend has its own lock screen function.
    
    Now, either logind or consolekit may be !NULL and upower will
    only be !NULL when using versions prior to 0.99.0. It will attempt
    to use logind first, upower second, consolekit third, and finally
    the internal fallback code.
---
 xfce4-session/Makefile.am              |    2 +
 xfce4-session/xfsm-consolekit.c        |  153 +++++++++++-
 xfce4-session/xfsm-consolekit.h        |   17 ++
 xfce4-session/xfsm-shutdown-fallback.c |  371 +++++++++++++++++++++++++++++
 xfce4-session/xfsm-shutdown-fallback.h |   39 ++++
 xfce4-session/xfsm-shutdown.c          |  398 +++++++++-----------------------
 6 files changed, 695 insertions(+), 285 deletions(-)

diff --git a/xfce4-session/Makefile.am b/xfce4-session/Makefile.am
index 1562e3c..73a29c7 100644
--- a/xfce4-session/Makefile.am
+++ b/xfce4-session/Makefile.am
@@ -57,6 +57,8 @@ xfce4_session_SOURCES =							\
 	xfsm-manager.h							\
 	xfsm-properties.c						\
 	xfsm-properties.h						\
+	xfsm-shutdown-fallback.c				\
+	xfsm-shutdown-fallback.h				\
 	xfsm-shutdown.c							\
 	xfsm-shutdown.h							\
 	xfsm-splash-screen.c						\
diff --git a/xfce4-session/xfsm-consolekit.c b/xfce4-session/xfsm-consolekit.c
index f6cd641..8d99398 100644
--- a/xfce4-session/xfsm-consolekit.c
+++ b/xfce4-session/xfsm-consolekit.c
@@ -24,7 +24,7 @@
 #include <dbus/dbus-glib-lowlevel.h>
 
 #include <xfce4-session/xfsm-consolekit.h>
-
+#include <libxfsm/xfsm-util.h>
 
 
 #define CK_NAME         "org.freedesktop.ConsoleKit"
@@ -259,6 +259,48 @@ xfsm_consolekit_can_method (XfsmConsolekit  *consolekit,
 
 
 static gboolean
+xfsm_consolekit_can_sleep (XfsmConsolekit  *consolekit,
+                           const gchar     *method,
+                           gboolean        *can_method,
+                           gboolean        *auth_method,
+                           GError         **error)
+{
+  gboolean ret;
+  gchar *can_string;
+  g_return_val_if_fail (can_method != NULL, FALSE);
+
+  /* never return true if something fails */
+  *can_method = FALSE;
+
+  if (!xfsm_consolekit_proxy_ensure (consolekit, error))
+    return FALSE;
+
+  ret = dbus_g_proxy_call (consolekit->ck_proxy, method,
+                            error, G_TYPE_INVALID,
+                            G_TYPE_STRING, &can_string,
+                            G_TYPE_INVALID);
+
+  if (ret == FALSE)
+    return FALSE;
+
+  /* If yes or challenge then we can sleep, it just might take a password */
+  if (g_strcmp0 (can_string, "yes") == 0 || g_strcmp0 (can_string, "challenge") == 0)
+    {
+      *can_method = TRUE;
+      *auth_method = TRUE;
+    }
+  else
+    {
+      *can_method = FALSE;
+      *auth_method = FALSE;
+    }
+
+  return TRUE;
+}
+
+
+
+static gboolean
 xfsm_consolekit_try_method (XfsmConsolekit  *consolekit,
                             const gchar     *method,
                             GError         **error)
@@ -272,6 +314,21 @@ xfsm_consolekit_try_method (XfsmConsolekit  *consolekit,
 
 
 
+static gboolean
+xfsm_consolekit_try_sleep (XfsmConsolekit  *consolekit,
+                           const gchar     *method,
+                           GError         **error)
+{
+  if (!xfsm_consolekit_proxy_ensure (consolekit, error))
+    return FALSE;
+
+  return dbus_g_proxy_call (consolekit->ck_proxy, method, error,
+                            G_TYPE_BOOLEAN, TRUE,
+                            G_TYPE_INVALID, G_TYPE_INVALID);
+}
+
+
+
 XfsmConsolekit *
 xfsm_consolekit_get (void)
 {
@@ -338,3 +395,97 @@ xfsm_consolekit_can_shutdown (XfsmConsolekit  *consolekit,
   return xfsm_consolekit_can_method (consolekit, "CanStop",
                                      can_shutdown, error);
 }
+
+
+static gboolean
+lock_screen (GError **error)
+{
+  XfconfChannel *channel;
+  gboolean       ret = TRUE;
+
+  channel = xfsm_open_config ();
+  if (xfconf_channel_get_bool (channel, "/shutdown/LockScreen", FALSE))
+      ret = g_spawn_command_line_async ("xflock4", error);
+
+  return ret;
+}
+
+gboolean
+xfsm_consolekit_try_suspend (XfsmConsolekit  *consolekit,
+                             GError         **error)
+{
+  gboolean can_suspend, auth_suspend;
+
+  g_return_val_if_fail (XFSM_IS_CONSOLEKIT (consolekit), FALSE);
+
+  /* Check if consolekit can suspend before we call lock screen. */
+  if (xfsm_consolekit_can_suspend (consolekit, &can_suspend, &auth_suspend, NULL))
+    {
+      if (!can_suspend)
+        return FALSE;
+    }
+  else
+    {
+      return FALSE;
+    }
+
+  if (!lock_screen (error))
+    return FALSE;
+
+  return xfsm_consolekit_try_sleep (consolekit, "Suspend", error);
+}
+
+
+
+gboolean
+xfsm_consolekit_try_hibernate (XfsmConsolekit  *consolekit,
+                               GError         **error)
+{
+  gboolean can_hibernate, auth_hibernate;
+
+  g_return_val_if_fail (XFSM_IS_CONSOLEKIT (consolekit), FALSE);
+
+  /* Check if consolekit can hibernate before we call lock screen. */
+  if (xfsm_consolekit_can_hibernate (consolekit, &can_hibernate, &auth_hibernate, NULL))
+    {
+      if (!can_hibernate)
+        return FALSE;
+    }
+  else
+    {
+      return FALSE;
+    }
+
+  if (!lock_screen (error))
+    return FALSE;
+
+  return xfsm_consolekit_try_sleep (consolekit, "Hibernate", error);
+}
+
+
+
+gboolean
+xfsm_consolekit_can_suspend (XfsmConsolekit  *consolekit,
+                             gboolean        *can_suspend,
+                             gboolean        *auth_suspend,
+                             GError         **error)
+{
+  g_return_val_if_fail (XFSM_IS_CONSOLEKIT (consolekit), FALSE);
+
+  return xfsm_consolekit_can_sleep (consolekit, "CanSuspend",
+                                    can_suspend, auth_suspend, error);
+}
+
+
+
+gboolean
+xfsm_consolekit_can_hibernate (XfsmConsolekit  *consolekit,
+                               gboolean        *can_hibernate,
+                               gboolean        *auth_hibernate,
+                               GError         **error)
+{
+  g_return_val_if_fail (XFSM_IS_CONSOLEKIT (consolekit), FALSE);
+
+  return xfsm_consolekit_can_sleep (consolekit, "CanHibernate",
+                                    can_hibernate, auth_hibernate, error);
+}
diff --git a/xfce4-session/xfsm-consolekit.h b/xfce4-session/xfsm-consolekit.h
index 051acaf..413ad68 100644
--- a/xfce4-session/xfsm-consolekit.h
+++ b/xfce4-session/xfsm-consolekit.h
@@ -51,4 +51,21 @@ gboolean        xfsm_consolekit_can_shutdown (XfsmConsolekit  *consolekit,
                                               gboolean        *can_shutdown,
                                               GError         **error);
 
+gboolean        xfsm_consolekit_try_suspend  (XfsmConsolekit  *consolekit,
+                                              GError         **error);
+
+gboolean        xfsm_consolekit_try_hibernate(XfsmConsolekit  *consolekit,
+                                              GError         **error);
+
+gboolean        xfsm_consolekit_can_suspend  (XfsmConsolekit  *consolekit,
+                                              gboolean        *can_suspend,
+                                              gboolean        *auth_suspend,
+                                              GError         **error);
+
+gboolean        xfsm_consolekit_can_hibernate(XfsmConsolekit  *consolekit,
+                                              gboolean        *can_hibernate,
+                                              gboolean        *auth_hibernate,
+                                              GError         **error);
+
+
 #endif /* !__XFSM_CONSOLEKIT_HELPER_H__ */
diff --git a/xfce4-session/xfsm-shutdown-fallback.c b/xfce4-session/xfsm-shutdown-fallback.c
new file mode 100644
index 0000000..24437e8
--- /dev/null
+++ b/xfce4-session/xfsm-shutdown-fallback.c
@@ -0,0 +1,371 @@
+/*-
+ * Copyright (c) 2003-2004 Benedikt Meurer <benny at xfce.org>
+ * Copyright (c) 2011      Nick Schermer <nick at xfce.org>
+ * Copyright (c) 2014      Xfce Development Team <xfce4-dev at xfce.org>
+ * All rights reserved.
+ *
+ * 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, 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.
+ *
+ * Parts of this file where taken from gnome-session/logout.c, which
+ * was written by Owen Taylor <otaylor at redhat.com>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+#ifdef HAVE_SIGNAL_H
+#include <signal.h>
+#endif
+#ifdef HAVE_SYS_SYSCTL_H
+#include <sys/sysctl.h>
+#endif
+
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <libxfce4util/libxfce4util.h>
+#include <gtk/gtk.h>
+#ifdef HAVE_UPOWER
+#include <upower.h>
+#endif
+#ifdef HAVE_POLKIT
+#include <polkit/polkit.h>
+#endif
+
+#include <libxfsm/xfsm-util.h>
+#include <xfce4-session/xfsm-shutdown-fallback.h>
+
+
+
+#define POLKIT_AUTH_SHUTDOWN_XFSM  "org.xfce.session.xfsm-shutdown-helper"
+#define POLKIT_AUTH_RESTART_XFSM   "org.xfce.session.xfsm-shutdown-helper"
+#define POLKIT_AUTH_SUSPEND_XFSM   "org.xfce.session.xfsm-shutdown-helper"
+#define POLKIT_AUTH_HIBERNATE_XFSM "org.xfce.session.xfsm-shutdown-helper"
+
+
+
+
+#ifdef BACKEND_TYPE_FREEBSD
+static gchar *
+get_string_sysctl (GError **err, const gchar *format, ...)
+{
+        va_list args;
+        gchar *name;
+        size_t value_len;
+        gchar *str = NULL;
+
+        g_return_val_if_fail(format != NULL, FALSE);
+
+        va_start (args, format);
+        name = g_strdup_vprintf (format, args);
+        va_end (args);
+
+        if (sysctlbyname (name, NULL, &value_len, NULL, 0) == 0) {
+                str = g_new (char, value_len + 1);
+                if (sysctlbyname (name, str, &value_len, NULL, 0) == 0)
+                        str[value_len] = 0;
+                else {
+                        g_free (str);
+                        str = NULL;
+                }
+        }
+
+        if (!str)
+                g_set_error (err, 0, 0, "%s", g_strerror(errno));
+
+        g_free(name);
+        return str;
+}
+
+
+
+static gboolean
+freebsd_supports_sleep_state (const gchar *state)
+{
+  gboolean ret = FALSE;
+  gchar *sleep_states;
+
+  sleep_states = get_string_sysctl (NULL, "hw.acpi.supported_sleep_state");
+  if (sleep_states != NULL)
+    {
+      if (strstr (sleep_states, state) != NULL)
+          ret = TRUE;
+    }
+
+  g_free (sleep_states);
+
+  return ret;
+}
+#endif /* BACKEND_TYPE_FREEBSD */
+
+
+
+#ifdef BACKEND_TYPE_LINUX
+static gboolean
+linux_supports_sleep_state (const gchar *state)
+{
+  gboolean ret = FALSE;
+  gchar *command;
+  GError *error = NULL;
+  gint exit_status;
+
+  /* run script from pm-utils */
+  command = g_strdup_printf ("/usr/bin/pm-is-supported --%s", state);
+
+  ret = g_spawn_command_line_sync (command, NULL, NULL, &exit_status, &error);
+  if (!ret)
+    {
+      g_warning ("failed to run script: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
+  ret = (WIFEXITED(exit_status) && (WEXITSTATUS(exit_status) == EXIT_SUCCESS));
+
+out:
+  g_free (command);
+
+  return ret;
+}
+#endif /* BACKEND_TYPE_LINUX */
+
+
+
+static gboolean
+xfsm_shutdown_fallback_check_auth (const gchar *action_id)
+{
+  gboolean auth_result = FALSE;
+#ifdef HAVE_POLKIT
+  GDBusConnection *bus;
+  PolkitAuthority *polkit;
+  PolkitAuthorizationResult *polkit_result;
+  PolkitSubject *polkit_subject;
+
+  bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
+  polkit = polkit_authority_get_sync (NULL, NULL);
+  if (polkit != NULL && bus != NULL)
+    {
+      polkit_subject = polkit_system_bus_name_new (g_dbus_connection_get_unique_name (bus));
+      polkit_result = polkit_authority_check_authorization_sync (polkit,
+                                                                 polkit_subject,
+                                                                 action_id,
+                                                                 NULL,
+                                                                 POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
+                                                                 NULL,
+                                                                 NULL);
+      if (polkit_result != NULL)
+        {
+          auth_result = polkit_authorization_result_get_is_authorized (polkit_result);
+          g_object_unref (polkit_result);
+        }
+
+        g_object_unref (polkit);
+        g_object_unref (bus);
+      }
+#endif /* HAVE_POLKIT */
+
+  return auth_result;
+}
+
+
+
+static gboolean
+lock_screen (GError **error)
+{
+  XfconfChannel *channel;
+  gboolean       ret = TRUE;
+
+  channel = xfsm_open_config ();
+  if (xfconf_channel_get_bool (channel, "/shutdown/LockScreen", FALSE))
+      ret = g_spawn_command_line_async ("xflock4", error);
+
+  if (ret)
+    {
+      /* sleep 2 seconds so locking has time to startup */
+      g_usleep (G_USEC_PER_SEC * 2);
+    }
+
+  return ret;
+}
+
+
+
+/**
+ * xfsm_shutdown_fallback_try_action:
+ * @type:  The @XfsmShutdownType action to perform (shutdown, suspend, etc).
+ * @error: Returns a @GError if an error was encountered.
+ *
+ * Performs the @XfsmShutdownType action requested.
+ *
+ * Return value: Returns FALSE if an invalid @XfsmShutdownType action was
+ *               requested. Otherwise returns the status of the pkexec
+ *               call to the helper command.
+ **/
+gboolean
+xfsm_shutdown_fallback_try_action (XfsmShutdownType   type,
+                                   GError           **error)
+{
+  const gchar *action;
+  gboolean ret;
+  gint exit_status = 0;
+  gchar *command = NULL;
+
+  if (type == XFSM_SHUTDOWN_SHUTDOWN)
+    action = "shutdown";
+  if (type == XFSM_SHUTDOWN_RESTART)
+    action = "restart";
+  else if (type == XFSM_SHUTDOWN_SUSPEND)
+    {
+      action = "suspend";
+      /* On suspend we try to lock the screen */
+      if (!lock_screen (error))
+        return FALSE;
+    }
+  else if (type == XFSM_SHUTDOWN_HIBERNATE)
+    {
+      action = "hibernate";
+      /* On hibernate we try to lock the screen */
+      if (!lock_screen (error))
+        return FALSE;
+    }
+  else
+      return FALSE;
+
+  command = g_strdup_printf ("pkexec " XFSM_SHUTDOWN_HELPER_CMD " --%s", action);
+  ret = g_spawn_command_line_sync (command, NULL, NULL, &exit_status, error);
+
+  g_free (command);
+  return ret;
+}
+
+
+
+/**
+ * xfsm_shutdown_fallback_can_suspend:
+ *
+ * Return value: Returns whether the *system* is capable suspending.
+ **/
+gboolean
+xfsm_shutdown_fallback_can_suspend (void)
+{
+#ifdef BACKEND_TYPE_FREEBSD
+  return freebsd_supports_sleep_state ("S3");
+#endif
+#ifdef BACKEND_TYPE_LINUX
+  return linux_supports_sleep_state ("suspend");
+#endif
+#ifdef BACKEND_TYPE_OPENBSD
+  return TRUE;
+#endif
+
+  return FALSE;
+}
+
+
+
+/**
+ * xfsm_shutdown_fallback_can_hibernate:
+ *
+ * Return value: Returns whether the *system* is capable hibernating.
+ **/
+gboolean
+xfsm_shutdown_fallback_can_hibernate (void)
+{
+#ifdef BACKEND_TYPE_FREEBSD
+  return freebsd_supports_sleep_state ("S4");
+#endif
+#ifdef BACKEND_TYPE_LINUX
+  return linux_supports_sleep_state ("hibernate");
+#endif
+#ifdef BACKEND_TYPE_OPENBSD
+  return TRUE;
+#endif
+
+  return FALSE;
+}
+
+
+
+/**
+ * xfsm_shutdown_fallback_auth_shutdown:
+ *
+ * Return value: Returns whether the user is authorized to perform a shutdown.
+ **/
+gboolean
+xfsm_shutdown_fallback_auth_shutdown (void)
+{
+  return xfsm_shutdown_fallback_check_auth (POLKIT_AUTH_SHUTDOWN_XFSM);
+}
+
+
+
+/**
+ * xfsm_shutdown_fallback_auth_restart:
+ *
+ * Return value: Returns whether the user is authorized to perform a restart.
+ **/
+gboolean
+xfsm_shutdown_fallback_auth_restart (void)
+{
+  return xfsm_shutdown_fallback_check_auth (POLKIT_AUTH_RESTART_XFSM);
+}
+
+
+
+/**
+ * xfsm_shutdown_fallback_auth_suspend:
+ *
+ * Return value: Returns whether the user is authorized to perform a suspend.
+ **/
+gboolean
+xfsm_shutdown_fallback_auth_suspend (void)
+{
+  return xfsm_shutdown_fallback_check_auth (POLKIT_AUTH_SUSPEND_XFSM);
+}
+
+
+
+/**
+ * xfsm_shutdown_fallback_auth_hibernate:
+ *
+ * Return value: Returns whether the user is authorized to perform a hibernate.
+ **/
+gboolean
+xfsm_shutdown_fallback_auth_hibernate (void)
+{
+  return xfsm_shutdown_fallback_check_auth (POLKIT_AUTH_HIBERNATE_XFSM);
+}
diff --git a/xfce4-session/xfsm-shutdown-fallback.h b/xfce4-session/xfsm-shutdown-fallback.h
new file mode 100644
index 0000000..e4909e7
--- /dev/null
+++ b/xfce4-session/xfsm-shutdown-fallback.h
@@ -0,0 +1,39 @@
+/*-
+ * Copyright (c) 2003-2004 Benedikt Meurer <benny at xfce.org>
+ * Copyright (c) 2011      Nick Schermer <nick at xfce.org>
+ * Copyright (c) 2014      Xfce Development Team <xfce4-dev at xfce.org>
+ * All rights reserved.
+ *
+ * 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, 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 __XFSM_SHUTDOWN_FALLBACK_H__
+#define __XFSM_SHUTDOWN_FALLBACK_H__
+
+#include <xfce4-session/xfsm-shutdown.h>
+
+gboolean xfsm_shutdown_fallback_auth_shutdown  (void);
+gboolean xfsm_shutdown_fallback_auth_restart   (void);
+gboolean xfsm_shutdown_fallback_auth_suspend   (void);
+gboolean xfsm_shutdown_fallback_auth_hibernate (void);
+
+gboolean xfsm_shutdown_fallback_can_suspend    (void);
+gboolean xfsm_shutdown_fallback_can_hibernate  (void);
+
+gboolean xfsm_shutdown_fallback_try_action     (XfsmShutdownType   type,
+                                                GError           **error);
+
+#endif	/* !__XFSM_SHUTDOWN_FALLBACK_H__ */
diff --git a/xfce4-session/xfsm-shutdown.c b/xfce4-session/xfsm-shutdown.c
index e32dedf..d8209f2 100644
--- a/xfce4-session/xfsm-shutdown.c
+++ b/xfce4-session/xfsm-shutdown.c
@@ -77,23 +77,11 @@
 #include <xfce4-session/xfsm-legacy.h>
 #include <xfce4-session/xfsm-upower.h>
 #include <xfce4-session/xfsm-systemd.h>
-
-
-
-#define POLKIT_AUTH_SHUTDOWN_XFSM  "org.xfce.session.xfsm-shutdown-helper"
-#define POLKIT_AUTH_RESTART_XFSM   "org.xfce.session.xfsm-shutdown-helper"
-#define POLKIT_AUTH_SUSPEND_XFSM   "org.xfce.session.xfsm-shutdown-helper"
-#define POLKIT_AUTH_HIBERNATE_XFSM "org.xfce.session.xfsm-shutdown-helper"
+#include <xfce4-session/xfsm-shutdown-fallback.h>
 
 
 
 static void xfsm_shutdown_finalize  (GObject      *object);
-static gboolean xfsm_shutdown_fallback_auth_shutdown  (void);
-static gboolean xfsm_shutdown_fallback_auth_restart   (void);
-static gboolean xfsm_shutdown_fallback_can_hibernate  (void);
-static gboolean xfsm_shutdown_fallback_can_suspend    (void);
-static gboolean xfsm_shutdown_fallback_auth_hibernate (void);
-static gboolean xfsm_shutdown_fallback_auth_suspend   (void);
 
 
 
@@ -140,12 +128,17 @@ xfsm_shutdown_init (XfsmShutdown *shutdown)
 
   shutdown->consolekit = NULL;
   shutdown->systemd = NULL;
+  shutdown->upower = NULL;
   if (LOGIND_RUNNING())
     shutdown->systemd = xfsm_systemd_get ();
   else
     shutdown->consolekit = xfsm_consolekit_get ();
 
+#ifdef HAVE_UPOWER
+#if !UP_CHECK_VERSION(0, 99, 0)
   shutdown->upower = xfsm_upower_get ();
+#endif /* UP_CHECK_VERSION */
+#endif /* HAVE_UPOWER */
 
   /* check kiosk */
   kiosk = xfce_kiosk_new ("xfce4-session");
@@ -239,35 +232,6 @@ xfsm_shutdown_try_type (XfsmShutdown      *shutdown,
 
 
 
-static gboolean
-xfsm_shutdown_fallback_try_action (XfsmShutdown      *shutdown,
-                                   XfsmShutdownType   type,
-                                   GError           **error)
-{
-  const gchar *action;
-  gboolean ret;
-  gint exit_status = 0;
-  gchar *command = NULL;
-
-  if (type == XFSM_SHUTDOWN_SHUTDOWN)
-    action = "shutdown";
-  if (type == XFSM_SHUTDOWN_RESTART)
-    action = "restart";
-  else if (type == XFSM_SHUTDOWN_SUSPEND)
-    action = "suspend";
-  else if (type == XFSM_SHUTDOWN_HIBERNATE)
-    action = "hibernate";
-  else
-      return FALSE;
-
-  command = g_strdup_printf ("pkexec " XFSM_SHUTDOWN_HELPER_CMD " --%s", action);
-  ret = g_spawn_command_line_sync (command, NULL, NULL, &exit_status, error);
-
-  g_free (command);
-  return ret;
-}
-
-
 
 gboolean
 xfsm_shutdown_try_restart (XfsmShutdown  *shutdown,
@@ -279,12 +243,21 @@ xfsm_shutdown_try_restart (XfsmShutdown  *shutdown,
     return FALSE;
 
   if (shutdown->systemd != NULL)
-    return xfsm_systemd_try_restart (shutdown->systemd, error);
-
-  if (shutdown->consolekit != NULL)
-    return xfsm_consolekit_try_restart (shutdown->consolekit, error);
+    {
+      if (xfsm_systemd_try_restart (shutdown->systemd, NULL))
+        {
+          return TRUE;
+        }
+    }
+  else if (shutdown->consolekit != NULL)
+    {
+      if (xfsm_consolekit_try_restart (shutdown->consolekit, NULL))
+        {
+          return TRUE;
+        }
+    }
 
-  return xfsm_shutdown_fallback_try_action (shutdown, XFSM_SHUTDOWN_RESTART, error);
+  return xfsm_shutdown_fallback_try_action (XFSM_SHUTDOWN_RESTART, error);
 }
 
 
@@ -299,12 +272,35 @@ xfsm_shutdown_try_shutdown (XfsmShutdown  *shutdown,
     return FALSE;
 
   if (shutdown->systemd != NULL)
-    return xfsm_systemd_try_shutdown (shutdown->systemd, error);
+    {
+      if (xfsm_systemd_try_shutdown (shutdown->systemd, NULL))
+        {
+          return TRUE;
+        }
+    }
+  else if (shutdown->consolekit != NULL)
+    {
+      if (xfsm_consolekit_try_shutdown (shutdown->consolekit, NULL))
+        {
+          return TRUE;
+        }
+    }
 
-  if (shutdown->consolekit != NULL)
-    return xfsm_consolekit_try_shutdown (shutdown->consolekit, error);
+  return xfsm_shutdown_fallback_try_action (XFSM_SHUTDOWN_SHUTDOWN, error);
+}
+
+
+
+typedef gboolean (*SleepFunc) (gpointer object, GError **);
+
+static gboolean
+try_sleep_method (gpointer  object,
+                  SleepFunc func)
+{
+  if (object == NULL)
+    return FALSE;
 
-  return xfsm_shutdown_fallback_try_action (shutdown, XFSM_SHUTDOWN_SHUTDOWN, error);
+  return func (object, NULL);
 }
 
 
@@ -315,18 +311,21 @@ xfsm_shutdown_try_suspend (XfsmShutdown  *shutdown,
 {
   g_return_val_if_fail (XFSM_IS_SHUTDOWN (shutdown), FALSE);
 
-  if (shutdown->systemd != NULL)
-    return xfsm_systemd_try_suspend (shutdown->systemd, error);
+  /* Try each way to suspend - it will handle NULL.
+   * In the future the upower code can go away once everyone is
+   * running upower 0.99.0+
+   */
 
-#ifdef HAVE_UPOWER
-#if !UP_CHECK_VERSION(0, 99, 0)
-  if (shutdown->upower != NULL)
-    return xfsm_upower_try_suspend (shutdown->upower, error);
-#endif /* UP_CHECK_VERSION */
-#endif /* HAVE_UPOWER */
+  if (try_sleep_method (shutdown->systemd, (SleepFunc)xfsm_systemd_try_suspend))
+    return TRUE;
+
+  if (try_sleep_method (shutdown->upower, (SleepFunc)xfsm_upower_try_suspend))
+    return TRUE;
 
-  xfsm_upower_lock_screen (shutdown->upower, "Suspend", error);
-  return xfsm_shutdown_fallback_try_action (shutdown, XFSM_SHUTDOWN_SUSPEND, error);
+  if (try_sleep_method (shutdown->consolekit, (SleepFunc)xfsm_consolekit_try_suspend))
+    return TRUE;
+
+  return xfsm_shutdown_fallback_try_action (XFSM_SHUTDOWN_SUSPEND, error);
 }
 
 
@@ -337,18 +336,21 @@ xfsm_shutdown_try_hibernate (XfsmShutdown  *shutdown,
 {
   g_return_val_if_fail (XFSM_IS_SHUTDOWN (shutdown), FALSE);
 
-  if (shutdown->systemd != NULL)
-    return xfsm_systemd_try_hibernate (shutdown->systemd, error);
+  /* Try each way to hibernate - it will handle NULL.
+   * In the future the upower code can go away once everyone is
+   * running upower 0.99.0+
+   */
 
-#ifdef HAVE_UPOWER
-#if !UP_CHECK_VERSION(0, 99, 0)
-  if (shutdown->upower != NULL)
-    return xfsm_upower_try_hibernate (shutdown->upower, error);
-#endif /* UP_CHECK_VERSION */
-#endif /* HAVE_UPOWER */
+  if (try_sleep_method (shutdown->systemd, (SleepFunc)xfsm_systemd_try_hibernate))
+    return TRUE;
 
-  xfsm_upower_lock_screen (shutdown->upower, "Hibernate", error);
-  return xfsm_shutdown_fallback_try_action (shutdown, XFSM_SHUTDOWN_HIBERNATE, error);
+  if (try_sleep_method (shutdown->upower, (SleepFunc)xfsm_upower_try_hibernate))
+    return TRUE;
+
+  if (try_sleep_method (shutdown->consolekit, (SleepFunc)xfsm_consolekit_try_hibernate))
+    return TRUE;
+
+  return xfsm_shutdown_fallback_try_action (XFSM_SHUTDOWN_HIBERNATE, error);
 }
 
 
@@ -401,8 +403,7 @@ xfsm_shutdown_can_shutdown (XfsmShutdown  *shutdown,
       if (xfsm_systemd_can_shutdown (shutdown->systemd, can_shutdown, error))
         return TRUE;
     }
-
-  if (shutdown->consolekit != NULL)
+  else if (shutdown->consolekit != NULL)
     {
       if (xfsm_consolekit_can_shutdown (shutdown->consolekit, can_shutdown, error))
         return TRUE;
@@ -429,16 +430,26 @@ xfsm_shutdown_can_suspend (XfsmShutdown  *shutdown,
     }
 
   if (shutdown->systemd != NULL)
-    return xfsm_systemd_can_suspend (shutdown->systemd, can_suspend,
-                                     auth_suspend, error);
-
-#ifdef HAVE_UPOWER
-#if !UP_CHECK_VERSION(0, 99, 0)
-  if (shutdown->upower)
-    return xfsm_upower_can_suspend (shutdown->upower, can_suspend,
-                                    auth_suspend, error);
-#endif /* UP_CHECK_VERSION */
-#endif /* HAVE_UPOWER */
+    {
+      if (xfsm_systemd_can_suspend (shutdown->systemd, can_suspend, auth_suspend, NULL))
+        {
+          return TRUE;
+        }
+    }
+  else if (shutdown->upower != NULL)
+    {
+      if (xfsm_upower_can_suspend (shutdown->upower, can_suspend, auth_suspend, NULL))
+        {
+          return TRUE;
+        }
+    }
+  else if (shutdown->consolekit != NULL)
+    {
+      if (xfsm_consolekit_can_suspend (shutdown->consolekit, can_suspend, auth_suspend, NULL))
+        {
+          return TRUE;
+        }
+    }
 
   *can_suspend = xfsm_shutdown_fallback_can_suspend ();
   *auth_suspend = xfsm_shutdown_fallback_auth_suspend ();
@@ -462,218 +473,37 @@ xfsm_shutdown_can_hibernate (XfsmShutdown  *shutdown,
     }
 
   if (shutdown->systemd != NULL)
-    return xfsm_systemd_can_hibernate (shutdown->systemd, can_hibernate,
-                                       auth_hibernate, error);
-
-#ifdef HAVE_UPOWER
-#if !UP_CHECK_VERSION(0, 99, 0)
-  if (shutdown->upower != NULL)
-    return xfsm_upower_can_hibernate (shutdown->upower, can_hibernate,
-                                      auth_hibernate, error);
-#endif /* UP_CHECK_VERSION */
-#endif /* HAVE_UPOWER */
-
-  *can_hibernate = xfsm_shutdown_fallback_can_hibernate ();
-  *auth_hibernate = xfsm_shutdown_fallback_auth_hibernate ();
-  return TRUE;
-}
-
-
-
-gboolean
-xfsm_shutdown_can_save_session (XfsmShutdown *shutdown)
-{
-  g_return_val_if_fail (XFSM_IS_SHUTDOWN (shutdown), FALSE);
-  return shutdown->kiosk_can_save_session;
-}
-
-
-
-#ifdef BACKEND_TYPE_FREEBSD
-static gchar *
-get_string_sysctl (GError **err, const gchar *format, ...)
-{
-        va_list args;
-        gchar *name;
-        size_t value_len;
-        gchar *str = NULL;
-
-        g_return_val_if_fail(format != NULL, FALSE);
-
-        va_start (args, format);
-        name = g_strdup_vprintf (format, args);
-        va_end (args);
-
-        if (sysctlbyname (name, NULL, &value_len, NULL, 0) == 0) {
-                str = g_new (char, value_len + 1);
-                if (sysctlbyname (name, str, &value_len, NULL, 0) == 0)
-                        str[value_len] = 0;
-                else {
-                        g_free (str);
-                        str = NULL;
-                }
-        }
-
-        if (!str)
-                g_set_error (err, 0, 0, "%s", g_strerror(errno));
-
-        g_free(name);
-        return str;
-}
-
-
-
-static gboolean
-freebsd_supports_sleep_state (const gchar *state)
-{
-  gboolean ret = FALSE;
-  gchar *sleep_states;
-
-  sleep_states = get_string_sysctl (NULL, "hw.acpi.supported_sleep_state");
-  if (sleep_states != NULL)
     {
-      if (strstr (sleep_states, state) != NULL)
-          ret = TRUE;
+      if (xfsm_systemd_can_hibernate (shutdown->systemd, can_hibernate, auth_hibernate, NULL))
+        {
+          return TRUE;
+        }
     }
-
-  g_free (sleep_states);
-
-  return ret;
-}
-#endif /* BACKEND_TYPE_FREEBSD */
-
-
-
-#ifdef BACKEND_TYPE_LINUX
-static gboolean
-linux_supports_sleep_state (const gchar *state)
-{
-  gboolean ret = FALSE;
-  gchar *command;
-  GError *error = NULL;
-  gint exit_status;
-
-  /* run script from pm-utils */
-  command = g_strdup_printf ("/usr/bin/pm-is-supported --%s", state);
-
-  ret = g_spawn_command_line_sync (command, NULL, NULL, &exit_status, &error);
-  if (!ret)
+  else if (shutdown->upower != NULL)
     {
-      g_warning ("failed to run script: %s", error->message);
-      g_error_free (error);
-      goto out;
+      if (xfsm_upower_can_hibernate (shutdown->upower, can_hibernate, auth_hibernate, NULL))
+        {
+          return TRUE;
+        }
     }
-  ret = (WIFEXITED(exit_status) && (WEXITSTATUS(exit_status) == EXIT_SUCCESS));
-
-out:
-  g_free (command);
-
-  return ret;
-}
-#endif /* BACKEND_TYPE_LINUX */
-
-
-
-static gboolean
-xfsm_shutdown_fallback_can_suspend (void)
-{
-#ifdef BACKEND_TYPE_FREEBSD
-  return freebsd_supports_sleep_state ("S3");
-#endif
-#ifdef BACKEND_TYPE_LINUX
-  return linux_supports_sleep_state ("suspend");
-#endif
-#ifdef BACKEND_TYPE_OPENBSD
-  return TRUE;
-#endif
-
-  return FALSE;
-}
-
-
-
-static gboolean
-xfsm_shutdown_fallback_can_hibernate (void)
-{
-#ifdef BACKEND_TYPE_FREEBSD
-  return freebsd_supports_sleep_state ("S4");
-#endif
-#ifdef BACKEND_TYPE_LINUX
-  return linux_supports_sleep_state ("hibernate");
-#endif
-#ifdef BACKEND_TYPE_OPENBSD
-  return TRUE;
-#endif
-
-  return FALSE;
-}
-
-
-
-static gboolean
-xfsm_shutdown_fallback_check_auth (const gchar *action_id)
-{
-  gboolean auth_result = FALSE;
-#ifdef HAVE_POLKIT
-  GDBusConnection *bus;
-  PolkitAuthority *polkit;
-  PolkitAuthorizationResult *polkit_result;
-  PolkitSubject *polkit_subject;
-
-  bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
-  polkit = polkit_authority_get_sync (NULL, NULL);
-  if (polkit != NULL && bus != NULL)
+  else if (shutdown->consolekit != NULL)
     {
-      polkit_subject = polkit_system_bus_name_new (g_dbus_connection_get_unique_name (bus));
-      polkit_result = polkit_authority_check_authorization_sync (polkit,
-                                                                 polkit_subject,
-                                                                 action_id,
-                                                                 NULL,
-                                                                 POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
-                                                                 NULL,
-                                                                 NULL);
-      if (polkit_result != NULL)
+      if (xfsm_consolekit_can_hibernate (shutdown->consolekit, can_hibernate, auth_hibernate, NULL))
         {
-          auth_result = polkit_authorization_result_get_is_authorized (polkit_result);
-          g_object_unref (polkit_result);
+          return TRUE;
         }
+    }
 
-        g_object_unref (polkit);
-        g_object_unref (bus);
-      }
-#endif /* HAVE_POLKIT */
-
-  return auth_result;
-}
-
-
-
-static gboolean
-xfsm_shutdown_fallback_auth_shutdown (void)
-{
-  return xfsm_shutdown_fallback_check_auth (POLKIT_AUTH_SHUTDOWN_XFSM);
-}
-
-
-
-static gboolean
-xfsm_shutdown_fallback_auth_restart (void)
-{
-  return xfsm_shutdown_fallback_check_auth (POLKIT_AUTH_RESTART_XFSM);
-}
-
-
-
-static gboolean
-xfsm_shutdown_fallback_auth_suspend (void)
-{
-  return xfsm_shutdown_fallback_check_auth (POLKIT_AUTH_SUSPEND_XFSM);
+  *can_hibernate = xfsm_shutdown_fallback_can_hibernate ();
+  *auth_hibernate = xfsm_shutdown_fallback_auth_hibernate ();
+  return TRUE;
 }
 
 
 
-static gboolean
-xfsm_shutdown_fallback_auth_hibernate (void)
+gboolean
+xfsm_shutdown_can_save_session (XfsmShutdown *shutdown)
 {
-  return xfsm_shutdown_fallback_check_auth (POLKIT_AUTH_HIBERNATE_XFSM);
+  g_return_val_if_fail (XFSM_IS_SHUTDOWN (shutdown), FALSE);
+  return shutdown->kiosk_can_save_session;
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list