From 1fe1bdfacb2ca6f46b8dbc4c768b56238860f9e9 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 12 Jan 2017 15:34:24 +1300 Subject: Use power management functions from ConsoleKit2 if available. Suspend and hibernate functionality was removed from upower 0.99.0, so systems not using systemd had now suspend/hibernate functionality. Support for this was added into ConsoleKit2. Most systems will either be systemd or ConsoleKit2 now, so we try the following: 1. Power management in logind 2. Power management in ConsoleKit 3. upower for suspend/resume (really only here to not break backwards compatibility) Based on a patch for Gentoo by Fitzcarraldo. --- liblightdm-gobject/power.c | 102 ++++++++++++++-------- tests/Makefile.am | 2 + tests/scripts/power-no-login1-or-console-kit.conf | 65 ++++++++++++++ tests/scripts/power-no-login1.conf | 8 +- tests/src/test-runner.c | 32 +++++++ tests/test-power-no-login1-or-console-kit | 2 + 6 files changed, 169 insertions(+), 42 deletions(-) create mode 100644 tests/scripts/power-no-login1-or-console-kit.conf create mode 100755 tests/test-power-no-login1-or-console-kit diff --git a/liblightdm-gobject/power.c b/liblightdm-gobject/power.c index b46af0f8..b81165de 100644 --- a/liblightdm-gobject/power.c +++ b/liblightdm-gobject/power.c @@ -75,6 +75,36 @@ login1_call_function (const gchar *function, GVariant *parameters, GError **erro return r; } +static GVariant * +ck_call_function (const gchar *function, GVariant *parameters, GError **error) +{ + GVariant *r; + + if (!ck_proxy) + { + ck_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.freedesktop.ConsoleKit", + "/org/freedesktop/ConsoleKit/Manager", + "org.freedesktop.ConsoleKit.Manager", + NULL, + error); + if (!ck_proxy) + return FALSE; + } + + r = g_dbus_proxy_call_sync (ck_proxy, + function, + parameters, + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + error); + + return r; +} + /** * lightdm_get_can_suspend: * @@ -98,7 +128,13 @@ lightdm_get_can_suspend (void) can_suspend = g_strcmp0 (result, "yes") == 0; } } - else + if (!r) + { + r = ck_call_function ("CanSuspend", NULL, NULL); + if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)"))) + g_variant_get (r, "(b)", &can_suspend); + } + if (!r) { r = upower_call_function ("SuspendAllowed", NULL); if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)"))) @@ -128,7 +164,14 @@ lightdm_suspend (GError **error) if (!result) { if (error) - g_debug ("Can't suspend using logind; falling back to UPower: %s", (*error)->message); + g_debug ("Can't suspend using logind; falling back to ConsoleKit: %s", (*error)->message); + g_clear_error (error); + result = ck_call_function ("Suspend", g_variant_new ("(b)", FALSE), error); + } + if (!result) + { + if (error) + g_debug ("Can't suspend using logind or ConsoleKit; falling back to UPower: %s", (*error)->message); g_clear_error (error); result = upower_call_function ("Suspend", error); } @@ -163,7 +206,13 @@ lightdm_get_can_hibernate (void) can_hibernate = g_strcmp0 (result, "yes") == 0; } } - else + if (!r) + { + r = ck_call_function ("CanHibernate", NULL, NULL); + if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)"))) + g_variant_get (r, "(b)", &can_hibernate); + } + if (!r) { r = upower_call_function ("HibernateAllowed", NULL); if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)"))) @@ -193,7 +242,14 @@ lightdm_hibernate (GError **error) if (!result) { if (error) - g_debug ("Can't hibernate using logind; falling back to UPower: %s", (*error)->message); + g_debug ("Can't hibernate using logind; falling back to ConsoleKit: %s", (*error)->message); + g_clear_error (error); + result = ck_call_function ("Hibernate", g_variant_new ("(b)", FALSE), error); + } + if (!result) + { + if (error) + g_debug ("Can't hibernate using logind or ConsoleKit; falling back to UPower: %s", (*error)->message); g_clear_error (error); result = upower_call_function ("Hibernate", error); } @@ -205,36 +261,6 @@ lightdm_hibernate (GError **error) return hibernated; } -static GVariant * -ck_call_function (const gchar *function, GError **error) -{ - GVariant *r; - - if (!ck_proxy) - { - ck_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.ConsoleKit", - "/org/freedesktop/ConsoleKit/Manager", - "org.freedesktop.ConsoleKit.Manager", - NULL, - error); - if (!ck_proxy) - return FALSE; - } - - r = g_dbus_proxy_call_sync (ck_proxy, - function, - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - error); - - return r; -} - /** * lightdm_get_can_restart: * @@ -260,7 +286,7 @@ lightdm_get_can_restart (void) } else { - r = ck_call_function ("CanRestart", NULL); + r = ck_call_function ("CanRestart", NULL, NULL); if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)"))) g_variant_get (r, "(b)", &can_restart); } @@ -288,7 +314,7 @@ lightdm_restart (GError **error) if (!r) { g_clear_error (error); - r = ck_call_function ("Restart", error); + r = ck_call_function ("Restart", NULL, error); } restarted = r != NULL; if (r) @@ -322,7 +348,7 @@ lightdm_get_can_shutdown (void) } else { - r = ck_call_function ("CanStop", NULL); + r = ck_call_function ("CanStop", NULL, NULL); if (r && g_variant_is_of_type (r, G_VARIANT_TYPE ("(b)"))) g_variant_get (r, "(b)", &can_shutdown); } @@ -350,7 +376,7 @@ lightdm_shutdown (GError **error) if (!r) { g_clear_error (error); - r = ck_call_function ("Stop", error); + r = ck_call_function ("Stop", NULL, error); } shutdown = r != NULL; if (r) diff --git a/tests/Makefile.am b/tests/Makefile.am index a4ba67ce..f28f2e5c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -196,6 +196,7 @@ TESTS = \ test-gobject-power \ test-power-no-console-kit \ test-power-no-login1 \ + test-power-no-login1-or-console-kit \ test-power-no-services \ test-open-file-descriptors \ test-xdmcp-server-open-file-descriptors \ @@ -487,6 +488,7 @@ EXTRA_DIST = \ scripts/power-no-console-kit.conf \ scripts/power-no-services.conf \ scripts/power-no-login1.conf \ + scripts/power-no-login1-or-console-kit.conf \ scripts/plymouth-active-vt.conf \ scripts/plymouth-inactive-vt.conf \ scripts/plymouth-no-seat.conf \ diff --git a/tests/scripts/power-no-login1-or-console-kit.conf b/tests/scripts/power-no-login1-or-console-kit.conf new file mode 100644 index 00000000..d7077a2d --- /dev/null +++ b/tests/scripts/power-no-login1-or-console-kit.conf @@ -0,0 +1,65 @@ +# +# Check can do power operations from the greeter when no login1 or ConsoleKit service (falls back to upower for some) +# + +[test-runner-config] +disable-login1=true +disable-console-kit=true + +#?*START-DAEMON +#?RUNNER DAEMON-START + +# X server starts +#?XSERVER-0 START VT=7 SEAT=seat0 + +# Daemon connects when X server is ready +#?*XSERVER-0 INDICATE-READY +#?XSERVER-0 INDICATE-READY +#?XSERVER-0 ACCEPT-CONNECT + +# Greeter starts +#?GREETER-X-0 START XDG_SEAT=seat0 XDG_VTNR=7 XDG_SESSION_CLASS=greeter +#?XSERVER-0 ACCEPT-CONNECT +#?GREETER-X-0 CONNECT-XSERVER +#?GREETER-X-0 CONNECT-TO-DAEMON +#?GREETER-X-0 CONNECTED-TO-DAEMON + +# See if can suspend +#?*GREETER-X-0 GET-CAN-SUSPEND +#?UPOWER SUSPEND-ALLOWED +#?GREETER-X-0 CAN-SUSPEND ALLOWED=TRUE + +# Suspend +#?*GREETER-X-0 SUSPEND +#?UPOWER SUSPEND + +# See if can hibernate +#?*GREETER-X-0 GET-CAN-HIBERNATE +#?UPOWER HIBERNATE-ALLOWED +#?GREETER-X-0 CAN-HIBERNATE ALLOWED=TRUE + +# Hibernate +#?*GREETER-X-0 HIBERNATE +#?UPOWER HIBERNATE + +# See if can restart +#?*GREETER-X-0 GET-CAN-RESTART +#?GREETER-X-0 CAN-RESTART ALLOWED=FALSE + +# Restart +#?*GREETER-X-0 RESTART +#?GREETER-X-0 FAIL-RESTART + +# See if can shutdown +#?*GREETER-X-0 GET-CAN-SHUTDOWN +#?GREETER-X-0 CAN-SHUTDOWN ALLOWED=FALSE + +# Shutdown +#?*GREETER-X-0 SHUTDOWN +#?GREETER-X-0 FAIL-SHUTDOWN + +# Cleanup +#?*STOP-DAEMON +#?GREETER-X-0 TERMINATE SIGNAL=15 +#?XSERVER-0 TERMINATE SIGNAL=15 +#?RUNNER DAEMON-EXIT STATUS=0 diff --git a/tests/scripts/power-no-login1.conf b/tests/scripts/power-no-login1.conf index 79df9b4c..ed9d2e21 100644 --- a/tests/scripts/power-no-login1.conf +++ b/tests/scripts/power-no-login1.conf @@ -26,21 +26,21 @@ disable-login1=true # See if can suspend #?*GREETER-X-0 GET-CAN-SUSPEND -#?UPOWER SUSPEND-ALLOWED +#?CONSOLE-KIT CAN-SUSPEND #?GREETER-X-0 CAN-SUSPEND ALLOWED=TRUE # Suspend #?*GREETER-X-0 SUSPEND -#?UPOWER SUSPEND +#?CONSOLE-KIT SUSPEND # See if can hibernate #?*GREETER-X-0 GET-CAN-HIBERNATE -#?UPOWER HIBERNATE-ALLOWED +#?CONSOLE-KIT CAN-HIBERNATE #?GREETER-X-0 CAN-HIBERNATE ALLOWED=TRUE # Hibernate #?*GREETER-X-0 HIBERNATE -#?UPOWER HIBERNATE +#?CONSOLE-KIT HIBERNATE # See if can restart #?*GREETER-X-0 GET-CAN-RESTART diff --git a/tests/src/test-runner.c b/tests/src/test-runner.c index 234356eb..503b04a7 100644 --- a/tests/src/test-runner.c +++ b/tests/src/test-runner.c @@ -1238,6 +1238,16 @@ handle_ck_call (GDBusConnection *connection, check_status ("CONSOLE-KIT CAN-STOP"); g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE)); } + else if (strcmp (method_name, "CanSuspend") == 0) + { + check_status ("CONSOLE-KIT CAN-SUSPEND"); + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE)); + } + else if (strcmp (method_name, "CanHibernate") == 0) + { + check_status ("CONSOLE-KIT CAN-HIBERNATE"); + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE)); + } else if (strcmp (method_name, "CloseSession") == 0) g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE)); else if (strcmp (method_name, "OpenSession") == 0) @@ -1281,6 +1291,16 @@ handle_ck_call (GDBusConnection *connection, check_status ("CONSOLE-KIT STOP"); g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); } + else if (strcmp (method_name, "Suspend") == 0) + { + check_status ("CONSOLE-KIT SUSPEND"); + g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); + } + else if (strcmp (method_name, "Hibernate") == 0) + { + check_status ("CONSOLE-KIT HIBERNATE"); + g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); + } else g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name); } @@ -1337,6 +1357,12 @@ ck_name_acquired_cb (GDBusConnection *connection, " " " " " " + " " + " " + " " + " " + " " + " " " " " " " " @@ -1354,6 +1380,12 @@ ck_name_acquired_cb (GDBusConnection *connection, " " " " " " + " " + " " + " " + " " + " " + " " " " " " " " diff --git a/tests/test-power-no-login1-or-console-kit b/tests/test-power-no-login1-or-console-kit new file mode 100755 index 00000000..e71f25cb --- /dev/null +++ b/tests/test-power-no-login1-or-console-kit @@ -0,0 +1,2 @@ +#!/bin/sh +./src/dbus-env ./src/test-runner power-no-login1-or-console-kit test-gobject-greeter -- cgit v1.2.1