chiark / gitweb /
logind: allow users to override their own suspend/sleep inhibitors
[elogind.git] / src / login / logind-dbus.c
index ae9671bb1ef3d9b7a19c041290560b5cabb67cde..650be3465618e9a871e37722a9fbb7e63884e012 100644 (file)
@@ -31,6 +31,8 @@
 #include "path-util.h"
 #include "polkit.h"
 #include "special.h"
+#include "systemd/sd-id128.h"
+#include "systemd/sd-messages.h"
 
 #define BUS_MANAGER_INTERFACE                                           \
         " <interface name=\"org.freedesktop.login1.Manager\">\n"        \
@@ -720,10 +722,20 @@ static int bus_manager_inhibit(Manager *m, DBusConnection *connection, DBusMessa
                 goto fail;
         }
 
+        /* Delay is only supported for shutdown/sleep */
+        if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP))) {
+                r = -EINVAL;
+                goto fail;
+        }
+
         r = verify_polkit(connection, message,
-                          m == INHIBIT_BLOCK ?
-                          "org.freedesktop.login1.inhibit-block" :
-                          "org.freedesktop.login1.inhibit-delay", false, NULL, error);
+                          w == INHIBIT_SHUTDOWN         ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
+                          w == INHIBIT_SLEEP            ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep"    : "org.freedesktop.login1.inhibit-delay-sleep") :
+                          w == INHIBIT_IDLE             ? "org.freedesktop.login1.inhibit-block-idle" :
+                          w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
+                          w == INHIBIT_HANDLE_SLEEP_KEY ? "org.freedesktop.login1.inhibit-handle-sleep-key" :
+                                                          "org.freedesktop.login1.inhibit-handle-lid-switch",
+                          false, NULL, error);
         if (r < 0)
                 goto fail;
 
@@ -944,24 +956,17 @@ static int flush_devices(Manager *m) {
 }
 
 static int have_multiple_sessions(
-                DBusConnection *connection,
                 Manager *m,
-                DBusMessage *message,
-                DBusError *error) {
+                uid_t uid) {
 
         Session *session;
         Iterator i;
-        unsigned long ul;
 
         assert(m);
 
-        ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), error);
-        if (ul == (unsigned long) -1)
-                return -EIO;
-
         /* Check for other users' sessions. Greeter sessions do not count. */
         HASHMAP_FOREACH(session, m->sessions, i)
-                if (session->class == SESSION_USER && session->user->uid != ul)
+                if (session->class == SESSION_USER && session->user->uid != uid)
                         return true;
 
         return false;
@@ -1048,6 +1053,7 @@ static int bus_manager_can_shutdown_or_sleep(
         const char *result;
         DBusMessage *reply = NULL;
         int r;
+        unsigned long ul;
 
         assert(m);
         assert(connection);
@@ -1071,12 +1077,16 @@ static int bus_manager_can_shutdown_or_sleep(
                 }
         }
 
-        r = have_multiple_sessions(connection, m, message, error);
+        ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), error);
+        if (ul == (unsigned long) -1)
+                return -EIO;
+
+        r = have_multiple_sessions(m, (uid_t) ul);
         if (r < 0)
                 return r;
 
         multiple_sessions = r > 0;
-        blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL);
+        blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, (uid_t) ul);
 
         if (multiple_sessions) {
                 r = verify_polkit(connection, message, action_multiple_sessions, false, &challenge, error);
@@ -1138,6 +1148,42 @@ finish:
         return 0;
 }
 
+static int bus_manager_log_shutdown(
+                Manager *m,
+                InhibitWhat w,
+                const char *unit_name) {
+
+        const char *p, *q;
+
+        assert(m);
+        assert(unit_name);
+
+        if (w != INHIBIT_SHUTDOWN)
+                return 0;
+
+        if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
+                p = "MESSAGE=System is powering down.";
+                q = "SHUTDOWN=power-off";
+        } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
+                p = "MESSAGE=System is halting.";
+                q = "SHUTDOWN=halt";
+        } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
+                p = "MESSAGE=System is rebooting.";
+                q = "SHUTDOWN=reboot";
+        } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
+                p = "MESSAGE=System is rebooting with kexec.";
+                q = "SHUTDOWN=kexec";
+        } else {
+                p = "MESSAGE=System is shutting down.";
+                q = NULL;
+        }
+
+        return log_struct(LOG_NOTICE,
+                          "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_SHUTDOWN),
+                          p,
+                          q, NULL);
+}
+
 int bus_manager_shutdown_or_sleep_now_or_later(
                 Manager *m,
                 const char *unit_name,
@@ -1154,16 +1200,19 @@ int bus_manager_shutdown_or_sleep_now_or_later(
 
         delayed =
                 m->inhibit_delay_max > 0 &&
-                manager_is_inhibited(m, w, INHIBIT_DELAY, NULL);
+                manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0);
 
         if (delayed)
                 /* Shutdown is delayed, keep in mind what we
                  * want to do, and start a timeout */
                 r = delay_shutdown_or_sleep(m, w, unit_name);
-        else
+        else {
+                bus_manager_log_shutdown(m, w, unit_name);
+
                 /* Shutdown is not delayed, execute it
                  * immediately */
                 r = send_start_unit(m->bus, unit_name, error);
+        }
 
         return r;
 }
@@ -1185,6 +1234,7 @@ static int bus_manager_do_shutdown_or_sleep(
         bool multiple_sessions, blocked;
         DBusMessage *reply = NULL;
         int r;
+        unsigned long ul;
 
         assert(m);
         assert(connection);
@@ -1214,12 +1264,16 @@ static int bus_manager_do_shutdown_or_sleep(
                         return -ENOTSUP;
         }
 
-        r = have_multiple_sessions(connection, m, message, error);
+        ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), error);
+        if (ul == (unsigned long) -1)
+                return -EIO;
+
+        r = have_multiple_sessions(m, (uid_t) ul);
         if (r < 0)
                 return r;
 
         multiple_sessions = r > 0;
-        blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL);
+        blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, (uid_t) ul);
 
         if (multiple_sessions) {
                 r = verify_polkit(connection, message, action_multiple_sessions, interactive, NULL, error);
@@ -2252,10 +2306,12 @@ int manager_dispatch_delayed(Manager *manager) {
         /* Continue delay? */
         delayed =
                 manager->delayed_timestamp + manager->inhibit_delay_max > now(CLOCK_MONOTONIC) &&
-                manager_is_inhibited(manager, manager->delayed_what, INHIBIT_DELAY, NULL);
+                manager_is_inhibited(manager, manager->delayed_what, INHIBIT_DELAY, NULL, false, false, 0);
         if (delayed)
                 return 0;
 
+        bus_manager_log_shutdown(manager, manager->delayed_what, manager->delayed_unit);
+
         /* Reset delay data */
         unit_name = manager->delayed_unit;
         manager->delayed_unit = NULL;