chiark / gitweb /
logind: add LockSessions() call to lock all local sessions at once
[elogind.git] / src / login / logind-dbus.c
index e089eebed96d33b24a3ba4b4e3668724c51e1623..86ca654518e3e853ec4731db9bcb65c8ff839a45 100644 (file)
@@ -98,6 +98,7 @@
         "  <method name=\"UnlockSession\">\n"                           \
         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
         "  </method>\n"                                                 \
+        "  <method name=\"LockSessions\"/>\n"                           \
         "  <method name=\"KillSession\">\n"                             \
         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
         "   <arg name=\"who\" type=\"s\" direction=\"in\"/>\n"          \
         "  <property name=\"BlockInhibited\" type=\"s\" access=\"read\"/>\n" \
         "  <property name=\"DelayInhibited\" type=\"s\" access=\"read\"/>\n" \
         "  <property name=\"InhibitDelayMaxUSec\" type=\"t\" access=\"read\"/>\n" \
+        "  <property name=\"HandlePowerKey\" type=\"s\" access=\"read\"/>\n" \
+        "  <property name=\"HandleSleepKey\" type=\"s\" access=\"read\"/>\n" \
+        "  <property name=\"HandleLidSwitch\" type=\"s\" access=\"read\"/>\n" \
+        "  <property name=\"PreparingForShutdown\" type=\"b\" access=\"read\"/>\n" \
+        "  <property name=\"PreparingForSleep\" type=\"b\" access=\"read\"/>\n" \
         " </interface>\n"
 
 #define INTROSPECTION_BEGIN                                             \
@@ -270,6 +276,22 @@ static int bus_manager_append_inhibited(DBusMessageIter *i, const char *property
         return 0;
 }
 
+static int bus_manager_append_preparing(DBusMessageIter *i, const char *property, void *data) {
+        Manager *m = data;
+        dbus_bool_t b;
+
+        assert(i);
+        assert(property);
+
+        if (streq(property, "PreparingForShutdown"))
+                b = !!(m->delayed_what & INHIBIT_SHUTDOWN);
+        else
+                b = !!(m->delayed_what & INHIBIT_SLEEP);
+
+        dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b);
+        return 0;
+}
+
 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
         Session *session = NULL;
         User *user = NULL;
@@ -871,7 +893,7 @@ static int attach_device(Manager *m, const char *seat, const char *sysfs) {
                 goto finish;
         }
 
-        mkdir_p("/etc/udev/rules.d", 0755);
+        mkdir_p_label("/etc/udev/rules.d", 0755);
         r = write_one_line_file_atomic(file, rule);
         if (r < 0)
                 goto finish;
@@ -927,27 +949,20 @@ static int have_multiple_sessions(
                 DBusMessage *message,
                 DBusError *error) {
 
-        Session *s;
+        Session *session;
+        Iterator i;
+        unsigned long ul;
 
         assert(m);
 
-        if (hashmap_size(m->sessions) > 1)
-                return true;
-
-        /* Hmm, there's only one session, but let's make sure it
-         * actually belongs to the user who is asking. If not, better
-         * be safe than sorry. */
-
-        s = hashmap_first(m->sessions);
-        if (s) {
-                unsigned long ul;
-
-                ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), error);
-                if (ul == (unsigned long) -1)
-                        return -EIO;
+        ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), error);
+        if (ul == (unsigned long) -1)
+                return -EIO;
 
-                return s->user->uid != ul;
-        }
+        /* 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)
+                        return true;
 
         return false;
 }
@@ -1065,8 +1080,10 @@ static int bus_manager_can_shutdown_or_sleep(
                 if (r < 0)
                         return r;
 
-                result = "na";
-                goto finish;
+                if (r == 0) {
+                        result = "na";
+                        goto finish;
+                }
         }
 
         r = have_multiple_sessions(connection, m, message, error);
@@ -1083,11 +1100,11 @@ static int bus_manager_can_shutdown_or_sleep(
 
                 if (r > 0)
                         result = "yes";
-                        else if (challenge)
-                                result = "challenge";
-                        else
-                                result = "no";
-                }
+                else if (challenge)
+                        result = "challenge";
+                else
+                        result = "no";
+        }
 
         if (blocked) {
                 r = verify_polkit(connection, message, action_ignore_inhibit, false, &challenge, error);
@@ -1136,6 +1153,36 @@ finish:
         return 0;
 }
 
+int bus_manager_shutdown_or_sleep_now_or_later(
+                Manager *m,
+                const char *unit_name,
+                InhibitWhat w,
+                DBusError *error) {
+
+        bool delayed;
+        int r;
+
+        assert(m);
+        assert(unit_name);
+        assert(w >= 0);
+        assert(w <= _INHIBIT_WHAT_MAX);
+
+        delayed =
+                m->inhibit_delay_max > 0 &&
+                manager_is_inhibited(m, w, INHIBIT_DELAY, NULL);
+
+        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
+                /* Shutdown is not delayed, execute it
+                 * immediately */
+                r = send_start_unit(m->bus, unit_name, error);
+
+        return r;
+}
+
 static int bus_manager_do_shutdown_or_sleep(
                 Manager *m,
                 DBusConnection *connection,
@@ -1150,7 +1197,7 @@ static int bus_manager_do_shutdown_or_sleep(
                 DBusMessage **_reply) {
 
         dbus_bool_t interactive;
-        bool multiple_sessions, blocked, delayed;
+        bool multiple_sessions, blocked;
         DBusMessage *reply = NULL;
         int r;
 
@@ -1207,19 +1254,7 @@ static int bus_manager_do_shutdown_or_sleep(
                         return r;
         }
 
-        delayed =
-                m->inhibit_delay_max > 0 &&
-                manager_is_inhibited(m, w, INHIBIT_DELAY, NULL);
-
-        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
-                /* Shutdown is not delayed, execute it
-                 * immediately */
-                r = send_start_unit(connection, unit_name, error);
-
+        r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
         if (r < 0)
                 return r;
 
@@ -1231,6 +1266,8 @@ static int bus_manager_do_shutdown_or_sleep(
         return 0;
 }
 
+static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_handle_button, handle_button, HandleButton);
+
 static const BusProperty bus_login_manager_properties[] = {
         { "ControlGroupHierarchy",  bus_property_append_string,         "s",  offsetof(Manager, cgroup_path),        true },
         { "Controllers",            bus_property_append_strv,           "as", offsetof(Manager, controllers),        true },
@@ -1245,6 +1282,11 @@ static const BusProperty bus_login_manager_properties[] = {
         { "BlockInhibited",         bus_manager_append_inhibited,       "s",  0 },
         { "DelayInhibited",         bus_manager_append_inhibited,       "s",  0 },
         { "InhibitDelayMaxUSec",    bus_property_append_usec,           "t",  offsetof(Manager, inhibit_delay_max)   },
+        { "HandlePowerKey",         bus_manager_append_handle_button,   "s",  offsetof(Manager, handle_power_key)    },
+        { "HandleSleepKey",         bus_manager_append_handle_button,   "s",  offsetof(Manager, handle_sleep_key)    },
+        { "HandleLidSwitch",        bus_manager_append_handle_button,   "s",  offsetof(Manager, handle_lid_switch)   },
+        { "PreparingForShutdown",   bus_manager_append_preparing,       "b",  0 },
+        { "PreparingForSleep",      bus_manager_append_preparing,       "b",  0 },
         { NULL, }
 };
 
@@ -1704,6 +1746,18 @@ static DBusHandlerResult manager_message_handler(
                 if (!reply)
                         goto oom;
 
+        } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSessions")) {
+                Session *session;
+                Iterator i;
+
+                HASHMAP_FOREACH(session, m->sessions, i)
+                        if (session_send_lock(session, true) < 0)
+                                goto oom;
+
+                reply = dbus_message_new_method_return(message);
+                if (!reply)
+                        goto oom;
+
         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
                 const char *swho;
                 int32_t signo;
@@ -1864,9 +1918,9 @@ static DBusHandlerResult manager_message_handler(
                 if (r < 0)
                         return bus_send_error_reply(connection, message, &error, r);
 
-                mkdir_p("/var/lib/systemd", 0755);
+                mkdir_p_label("/var/lib/systemd", 0755);
 
-                r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
+                r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0);
                 if (r < 0)
                         return bus_send_error_reply(connection, message, &error, r);