chiark / gitweb /
Major cleanup of all leftovers after rebasing on master.
[elogind.git] / src / login / logind.c
index 09007c2..0b7d965 100644 (file)
 #include "conf-parser.h"
 #include "bus-util.h"
 #include "bus-error.h"
-#include "logind.h"
 #include "udev-util.h"
 #include "formats-util.h"
+#include "signal-util.h"
 #include "label.h"
-#include "label.h"
+#include "logind.h"
 #include "cgroup.h"
+#include "mount-setup.h"
+#include "virt.h"
 
 static void manager_free(Manager *m);
 
@@ -94,11 +96,35 @@ static Manager *manager_new(void) {
         if (!m->kill_exclude_users)
                 goto fail;
 
+        /* If elogind should be its own controller, mount its cgroup */
+        if (streq(ELOGIND_CGROUP_CONTROLLER, "name=elogind")) {
+                r = mount_setup(true);
+                if (r < 0)
+                        goto fail;
+        }
+
         /* Make cgroups */
         r = manager_setup_cgroup(m);
         if (r < 0)
                 goto fail;
 
+        m->suspend_mode = NULL;
+        m->suspend_state = strv_new("mem", "standby", "freeze", NULL);
+        if (!m->suspend_state)
+                goto fail;
+        m->hibernate_mode = strv_new("platform", "shutdown", NULL);
+        if (!m->hibernate_mode)
+                goto fail;
+        m->hibernate_state = strv_new("disk", NULL);
+        if (!m->hibernate_state)
+                goto fail;
+        m->hybrid_sleep_mode = strv_new("suspend", "platform", "shutdown", NULL);
+        if (!m->hybrid_sleep_mode)
+                goto fail;
+        m->hybrid_sleep_state = strv_new("disk", NULL);
+        if (!m->hybrid_sleep_state)
+                goto fail;
+
         m->udev = udev_new();
         if (!m->udev)
                 goto fail;
@@ -177,7 +203,7 @@ static void manager_free(Manager *m) {
                 udev_unref(m->udev);
 
         if (m->unlink_nologin)
-                unlink("/run/nologin");
+                (void) unlink("/run/nologin");
 
         bus_verify_polkit_async_registry_free(m->polkit_registry);
 
@@ -189,6 +215,12 @@ static void manager_free(Manager *m) {
         safe_close(m->reserve_vt_fd);
 #endif // 0
 
+        /* Avoid the creation of new processes forked by the
+         * kernel; at this point, we will not listen to the
+         * signals anyway */
+        if (detect_container(NULL) <= 0)
+                (void) cg_uninstall_release_agent(ELOGIND_CGROUP_CONTROLLER);
+
         manager_shutdown_cgroup(m, true);
 
         strv_free(m->kill_only_users);
@@ -197,6 +229,14 @@ static void manager_free(Manager *m) {
         free(m->scheduled_shutdown_type);
         free(m->scheduled_shutdown_tty);
         free(m->wall_message);
+
+        strv_free(m->suspend_mode);
+        strv_free(m->suspend_state);
+        strv_free(m->hibernate_mode);
+        strv_free(m->hibernate_state);
+        strv_free(m->hybrid_sleep_mode);
+        strv_free(m->hybrid_sleep_state);
+
         free(m);
 }
 
@@ -603,6 +643,34 @@ static int manager_reserve_vt(Manager *m) {
 }
 #endif // 0
 
+static int signal_agent_released(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+        Manager *m = userdata;
+        Session *s;
+        const char *cgroup;
+        int r;
+
+        assert(message);
+        assert(m);
+
+        r = sd_bus_message_read(message, "s", &cgroup);
+        if (r < 0) {
+                bus_log_parse_error(r);
+                return 0;
+        }
+
+        s = hashmap_get(m->sessions, cgroup);
+
+        if (!s) {
+                log_warning("Session not found: %s", cgroup);
+                return 0;
+        }
+
+        session_finalize(s);
+        session_free(s);
+
+        return 0;
+}
+
 static int manager_connect_bus(Manager *m) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         int r;
@@ -618,6 +686,14 @@ static int manager_connect_bus(Manager *m) {
         if (r < 0)
                 return log_error_errno(r, "Failed to add manager object vtable: %m");
 
+        /* elogind relies on signals from its release agent */
+        r = sd_bus_add_match(m->bus, NULL,
+                             "type='signal',"
+                             "interface='org.freedesktop.elogind.Agent',"
+                             "member='Released',"
+                             "path='/org/freedesktop/elogind/agent'",
+                             signal_agent_released, m);
+
         r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/seat", "org.freedesktop.login1.Seat", seat_vtable, seat_object_find, m);
         if (r < 0)
                 return log_error_errno(r, "Failed to add seat object vtable: %m");
@@ -642,7 +718,7 @@ static int manager_connect_bus(Manager *m) {
         if (r < 0)
                 return log_error_errno(r, "Failed to add user enumerator: %m");
 
-/// elogind does not support systemd action jobs
+/// elogind does not support systemd as PID 1
 #if 0
         r = sd_bus_add_match(m->bus,
                              NULL,
@@ -654,7 +730,6 @@ static int manager_connect_bus(Manager *m) {
                              match_job_removed, m);
         if (r < 0)
                 log_warning_errno(r, "Failed to add match for JobRemoved: %m");
-#endif // 0
 
         r = sd_bus_add_match(m->bus,
                              NULL,
@@ -698,6 +773,7 @@ static int manager_connect_bus(Manager *m) {
                         NULL, NULL);
         if (r < 0)
                 log_notice("Failed to enable subscription: %s", bus_error_message(&error, r));
+#endif // 0
 
         r = sd_bus_request_name(m->bus, "org.freedesktop.login1", 0);
         if (r < 0)
@@ -793,13 +869,8 @@ static int manager_connect_console(Manager *m) {
                 return -EINVAL;
         }
 
-        r = ignore_signals(SIGRTMIN + 1, -1);
-        if (r < 0)
-                return log_error_errno(r, "Cannot ignore SIGRTMIN + 1: %m");
-
-        r = sigprocmask_many(SIG_BLOCK, SIGRTMIN, -1);
-        if (r < 0)
-                return log_error_errno(r, "Cannot block SIGRTMIN: %m");
+        assert_se(ignore_signals(SIGRTMIN + 1, -1) >= 0);
+        assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN, -1) >= 0);
 
         r = sd_event_add_signal(m->event, NULL, SIGRTMIN, manager_vt_switch, m);
         if (r < 0)
@@ -1126,6 +1197,12 @@ static int manager_run(Manager *m) {
 
                 manager_gc(m, true);
 
+                r = manager_dispatch_delayed(m, false);
+                if (r < 0)
+                        return r;
+                if (r > 0)
+                        continue;
+
                 r = sd_event_run(m->event, (uint64_t) -1);
                 if (r < 0)
                         return r;
@@ -1133,24 +1210,45 @@ static int manager_run(Manager *m) {
 }
 
 static int manager_parse_config_file(Manager *m) {
+        const char *unit = NULL, *logind_conf, *sections;
+        FILE *file = NULL;
+        bool relaxed = false, allow_include = false, warn = true;
+
         assert(m);
 
+/// elogind parses its own config file
+#if 0
         return config_parse_many("/etc/systemd/logind.conf",
                                  CONF_DIRS_NULSTR("systemd/logind.conf"),
                                  "Login\0",
                                  config_item_perf_lookup, logind_gperf_lookup,
                                  false, m);
+#endif // 0
+
+        logind_conf = getenv("ELOGIND_CONF_FILE");
+        if (!logind_conf)
+                logind_conf = PKGSYSCONFDIR "/logind.conf";
+        sections = "Login\0Sleep\0";
+
+        return config_parse(unit, logind_conf, file, sections,
+                            config_item_perf_lookup, logind_gperf_lookup,
+                            relaxed, allow_include, warn, m);
 }
 
 int main(int argc, char *argv[]) {
         Manager *m = NULL;
         int r;
 
+        elogind_set_program_name(argv[0]);
         log_set_target(LOG_TARGET_AUTO);
         log_set_facility(LOG_AUTH);
         log_parse_environment();
         log_open();
 
+#ifdef ENABLE_DEBUG_ELOGIND
+        log_set_max_level(LOG_DEBUG);
+#endif // ENABLE_DEBUG_ELOGIND
+
         umask(0022);
 
         if (argc != 1) {