File 1003-logind-store-a-timestamp-when-the-ACPI-power-button-.patch of Package systemd-mini.20455

From 43abadc2cb8d298839eabd13926a22ec10287039 Mon Sep 17 00:00:00 2001
From: Federico Mena Quintero <federico@gnome.org>
Date: Thu, 16 Jun 2016 17:38:44 -0500
Subject: [PATCH 1003/1003] logind: store a timestamp when the ACPI power
 button is pressed

When we get a D-Bus call to shutdown, we'll use the corresponding
timestamp to see if this call is due to GDM responding to the ACPI
power button itself, or just due to any random program calling the
D-Bus shutdown method.

[federico: fixes bsc#981830]
[federico: fixes bsc#888612]
[fbui: fixes bsc#1072933]
---
 src/login/logind-action.c |  4 ++++
 src/login/logind-dbus.c   | 22 +++++++++++++++++-----
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/src/login/logind-action.c b/src/login/logind-action.c
index 140953eec1..6a8b1dcbd1 100644
--- a/src/login/logind-action.c
+++ b/src/login/logind-action.c
@@ -7,6 +7,7 @@
 #include "bus-util.h"
 #include "conf-parser.h"
 #include "format-util.h"
+#include "fs-util.h"
 #include "logind-action.h"
 #include "logind-dbus.h"
 #include "logind-session-dbus.h"
@@ -81,6 +82,9 @@ int manager_handle_action(
 
         /* If the key handling is inhibited, don't do anything */
         if (inhibit_key > 0) {
+                if (inhibit_key == INHIBIT_HANDLE_POWER_KEY)
+                        (void) touch("/run/systemd/acpi-shutdown");
+
                 if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
                         log_debug("Refusing %s operation, %s is inhibited.",
                                   handle_action_to_string(handle),
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
index 75a48300ad..9339bb8cfb 100644
--- a/src/login/logind-dbus.c
+++ b/src/login/logind-dbus.c
@@ -1802,12 +1802,16 @@ static int verify_shutdown_creds(
                 const char *action,
                 const char *action_multiple_sessions,
                 const char *action_ignore_inhibit,
+                bool is_sleep,
                 sd_bus_error *error) {
 
         _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
         bool multiple_sessions, blocked;
+        bool shutdown_through_acpi;
         uid_t uid;
         int r;
+        int fd;
+        struct stat buf;
 
         assert(m);
         assert(message);
@@ -1829,7 +1833,15 @@ static int verify_shutdown_creds(
         multiple_sessions = r > 0;
         blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
 
-        if (multiple_sessions && action_multiple_sessions) {
+        fd = open("/run/systemd/acpi-shutdown", O_NOFOLLOW | O_PATH | O_CLOEXEC);
+        if (fd >= 0) {
+                shutdown_through_acpi = ((fstat(fd, &buf) == 0) && (time(NULL) - buf.st_mtime <= 65) && !is_sleep);
+                unlink("/run/systemd/acpi-shutdown");
+                close(fd);
+        } else
+                shutdown_through_acpi = false;
+
+        if (multiple_sessions && action_multiple_sessions && !shutdown_through_acpi) {
                 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
                 if (r < 0)
                         return r;
@@ -1837,7 +1849,7 @@ static int verify_shutdown_creds(
                         return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
         }
 
-        if (blocked && action_ignore_inhibit) {
+        if (blocked && action_ignore_inhibit && !shutdown_through_acpi) {
                 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
                 if (r < 0)
                         return r;
@@ -1845,7 +1857,7 @@ static int verify_shutdown_creds(
                         return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
         }
 
-        if (!multiple_sessions && !blocked && action) {
+        if (!multiple_sessions && !blocked && action && !shutdown_through_acpi) {
                 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
                 if (r < 0)
                         return r;
@@ -1897,7 +1909,7 @@ static int method_do_shutdown_or_sleep(
         }
 
         r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
-                                  action_ignore_inhibit, error);
+                                  action_ignore_inhibit, sleep_verb != NULL, error);
         if (r != 0)
                 return r;
 
@@ -2191,7 +2203,7 @@ static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
 
         r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
-                                  action, action_multiple_sessions, action_ignore_inhibit, error);
+                                  action, action_multiple_sessions, action_ignore_inhibit, false, error);
         if (r != 0)
                 return r;
 
-- 
2.26.2

openSUSE Build Service is sponsored by