chiark / gitweb /
Prep v231.2: login1.policy vendor is the project, not it's description.
[elogind.git] / src / login / logind-dbus.c
index c81592c2a1c3de40d28275dc78786165dff45df3..cb67c668fa9982d262ab68ecf361017b8cc7bcab 100644 (file)
 #include "path-util.h"
 #include "process-util.h"
 #include "selinux-util.h"
-//#include "sleep-config.h"
+#include "sleep-config.h"
 //#include "special.h"
 #include "strv.h"
 #include "terminal-util.h"
 #include "udev-util.h"
 #include "unit-name.h"
 #include "user-util.h"
-//#include "utmp-wtmp.h"
+#include "utmp-wtmp.h"
+
+/// Additional includes needed by elogind
+#include "update-utmp.h"
+
+#if 1 /// elogind needs this prototype
+static int send_prepare_for(Manager *m, InhibitWhat w, bool _active);
+#endif // 1
 
 int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
         _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
@@ -283,6 +290,24 @@ static int property_get_current_sessions(
         return sd_bus_message_append(reply, "t", (uint64_t) hashmap_size(m->sessions));
 }
 
+static int property_get_current_inhibitors(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        Manager *m = userdata;
+
+        assert(bus);
+        assert(reply);
+        assert(m);
+
+        return sd_bus_message_append(reply, "t", (uint64_t) hashmap_size(m->inhibitors));
+}
+
 static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
         _cleanup_free_ char *p = NULL;
         Manager *m = userdata;
@@ -852,13 +877,17 @@ static int method_create_session(sd_bus_message *message, void *userdata, sd_bus
 
         session->create_message = sd_bus_message_ref(message);
 
-        /* Here upstream systemd starts cgroups and the user systemd,
-           and arranges to reply asynchronously.  We reply
-           directly.  */
+#if 0 /// UNNEEDED by elogind
+        /* Now, let's wait until the slice unit and stuff got
+         * created. We send the reply back from
+         * session_send_create_reply(). */
+#else
+        /* We reply directly. */
 
         r = session_send_create_reply(session, NULL);
         if (r < 0)
                 goto fail;
+#endif // 0
 
         return 1;
 
@@ -893,7 +922,9 @@ static int method_release_session(sd_bus_message *message, void *userdata, sd_bu
         if (r < 0)
                 return r;
 
+#if 1 /// elogind must queue this session
         session_add_to_gc_queue(session);
+#endif // 1
 
         return sd_bus_reply_method_return(message, NULL);
 }
@@ -1526,6 +1557,9 @@ static int execute_shutdown_or_sleep(
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
         char *c = NULL;
         const char *p;
+#else
+        /* needed for update-utmp */
+        char** argv_utmp = NULL;
 #endif // 0
         int r;
 
@@ -1540,39 +1574,70 @@ static int execute_shutdown_or_sleep(
                 log_info("Running in dry run, suppressing action.");
                 reset_scheduled_shutdown(m);
         } else {
-        r = sd_bus_call_method(
-                        m->bus,
-                        "org.freedesktop.systemd1",
-                        "/org/freedesktop/systemd1",
-                        "org.freedesktop.systemd1.Manager",
-                        "StartUnit",
-                        error,
-                        &reply,
-                        "ss", unit_name, "replace-irreversibly");
+                r = sd_bus_call_method(
+                                m->bus,
+                                "org.freedesktop.systemd1",
+                                "/org/freedesktop/systemd1",
+                                "org.freedesktop.systemd1.Manager",
+                                "StartUnit",
+                                error,
+                                &reply,
+                                "ss", unit_name, "replace-irreversibly");
+                if (r < 0)
+                        return r;
+
+                r = sd_bus_message_read(reply, "o", &p);
+                if (r < 0)
+                        return r;
+
+                c = strdup(p);
+                if (!c)
+                        return -ENOMEM;
+
+                m->action_unit = unit_name;
+                free(m->action_job);
+                m->action_job = c;
+                m->action_what = w;
+        }
 #else
+        if (IN_SET(action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
+
+                /* As we have no systemd update-utmp daemon running, we have to
+                 * set the relevant utmp/wtmp entries ourselves.
+                 */
+
+                if (strv_extend(&argv_utmp, "elogind") < 0)
+                        return log_oom();
+
+                if (HANDLE_REBOOT == action) {
+                        if (strv_extend(&argv_utmp, "reboot") < 0)
+                                return log_oom();
+                } else {
+                        if (strv_extend(&argv_utmp, "shutdown") < 0)
+                                return log_oom();
+                }
+
+                 /* This comes from our patched update-utmp/update-utmp.c */
+                update_utmp(2, argv_utmp, m->bus);
+                strv_free(argv_utmp);
+        }
+
+        /* Now perform the requested action */
         r = shutdown_or_sleep(m, action);
 
         /* no more pending actions, whether this failed or not */
         m->pending_action = HANDLE_IGNORE;
-        m->action_what    = 0;
-#endif // 0
-        if (r < 0)
-                return r;
-
-#if 0 /// elogind neither needs a dbus reply, nor supports systemd action jobs
-        r = sd_bus_message_read(reply, "o", &p);
         if (r < 0)
                 return r;
 
-        c = strdup(p);
-        if (!c)
-                return -ENOMEM;
-        }
-
-        m->action_unit = unit_name;
-        free(m->action_job);
-        m->action_job = c;
-        m->action_what = w;
+        /* As elogind can not rely on a systemd manager to call all
+         * sleeping processes to wake up, we have to tell them all
+         * by ourselves. */
+        if (w == INHIBIT_SLEEP) {
+                send_prepare_for(m, w, false);
+                m->action_what = 0;
+        } else
+                m->action_what = w;
 #endif // 0
 
         /* Make sure the lid switch is ignored for a while */
@@ -1942,8 +2007,8 @@ static int update_schedule_file(Manager *m) {
         return 0;
 
 fail:
-                (void) unlink(temp_path);
-                (void) unlink("/run/systemd/shutdown/scheduled");
+        (void) unlink(temp_path);
+        (void) unlink("/run/systemd/shutdown/scheduled");
 
         return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
 }
@@ -2071,11 +2136,9 @@ static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_
                 }
         }
 
-#if 0 /// elogind does not support utmp-wtmp
         r = manager_setup_wall_message_timer(m);
         if (r < 0)
                 return r;
-#endif // 0
 
         if (!isempty(type)) {
                 r = update_schedule_file(m);
@@ -2097,7 +2160,6 @@ static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userd
         cancelled = m->scheduled_shutdown_type != NULL;
         reset_scheduled_shutdown(m);
 
-#if 0 /// elogind does not support utmp-wtmp
         if (cancelled) {
                 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
                 const char *tty = NULL;
@@ -2113,7 +2175,6 @@ static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userd
                 utmp_wall("The system shutdown has been cancelled",
                           uid_to_name(uid), tty, logind_wall_tty_filter, m);
         }
-#endif // 0
 
         return sd_bus_reply_method_return(message, "b", cancelled);
 }
@@ -2324,7 +2385,7 @@ static int property_get_reboot_to_firmware_setup(
 
         return sd_bus_message_append(reply, "b", r > 0);
 #else
-        return sd_bus_message_append(reply, "b", -EOPNOTSUPP);
+        return sd_bus_message_append(reply, "b", false);
 #endif // 0
 }
 
@@ -2404,7 +2465,7 @@ static int method_can_reboot_to_firmware_setup(
 
         return sd_bus_reply_method_return(message, "s", result);
 #else
-        return sd_bus_reply_method_return(message, "s", "na");
+        return sd_bus_reply_method_return(message, "s", "no");
 #endif // 0
 }
 
@@ -2522,6 +2583,9 @@ static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error
         if (r < 0)
                 return r;
 
+        if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
+                return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
+
         do {
                 id = mfree(id);
 
@@ -2592,8 +2656,13 @@ const sd_bus_vtable manager_vtable[] = {
         SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
         SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
         SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
+        SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("RuntimeDirectorySize", "t", bus_property_get_size, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_current_inhibitors, 0, 0),
         SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_current_sessions, 0, 0),
+        SD_BUS_PROPERTY("UserTasksMax", "t", NULL, offsetof(Manager, user_tasks_max), SD_BUS_VTABLE_PROPERTY_CONST),
 
         SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),