chiark / gitweb /
Major cleanup of all leftovers after rebasing on master.
[elogind.git] / src / login / logind.c
index 23fb55a..0b7d965 100644 (file)
 #include "conf-parser.h"
 #include "bus-util.h"
 #include "bus-error.h"
 #include "conf-parser.h"
 #include "bus-util.h"
 #include "bus-error.h"
-#include "logind.h"
 #include "udev-util.h"
 #include "formats-util.h"
 #include "udev-util.h"
 #include "formats-util.h"
+#include "signal-util.h"
+#include "label.h"
+#include "logind.h"
+#include "cgroup.h"
+#include "mount-setup.h"
+#include "virt.h"
 
 static void manager_free(Manager *m);
 
 
 static void manager_free(Manager *m);
 
@@ -44,11 +49,17 @@ static Manager *manager_new(void) {
         if (!m)
                 return NULL;
 
         if (!m)
                 return NULL;
 
+        m->pin_cgroupfs_fd = -1;
+
         m->console_active_fd = -1;
         m->console_active_fd = -1;
+
+/// elogind does not support autospawning of vts
+#if 0
         m->reserve_vt_fd = -1;
 
         m->n_autovts = 6;
         m->reserve_vt = 6;
         m->reserve_vt_fd = -1;
 
         m->n_autovts = 6;
         m->reserve_vt = 6;
+#endif // 0
         m->remove_ipc = true;
         m->inhibit_delay_max = 5 * USEC_PER_SEC;
         m->handle_power_key = HANDLE_POWEROFF;
         m->remove_ipc = true;
         m->inhibit_delay_max = 5 * USEC_PER_SEC;
         m->handle_power_key = HANDLE_POWEROFF;
@@ -75,16 +86,45 @@ static Manager *manager_new(void) {
         m->user_units = hashmap_new(&string_hash_ops);
         m->session_units = hashmap_new(&string_hash_ops);
 
         m->user_units = hashmap_new(&string_hash_ops);
         m->session_units = hashmap_new(&string_hash_ops);
 
-        m->busnames = set_new(&string_hash_ops);
+        m->running_as = MANAGER_SYSTEM;
+        m->test_run   = false;
 
 
-        if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->busnames ||
-            !m->user_units || !m->session_units)
+        if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units)
                 goto fail;
 
         m->kill_exclude_users = strv_new("root", NULL);
         if (!m->kill_exclude_users)
                 goto fail;
 
                 goto fail;
 
         m->kill_exclude_users = strv_new("root", NULL);
         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;
         m->udev = udev_new();
         if (!m->udev)
                 goto fail;
@@ -140,8 +180,6 @@ static void manager_free(Manager *m) {
         hashmap_free(m->user_units);
         hashmap_free(m->session_units);
 
         hashmap_free(m->user_units);
         hashmap_free(m->session_units);
 
-        set_free_free(m->busnames);
-
         sd_event_source_unref(m->idle_action_event_source);
         sd_event_source_unref(m->inhibit_timeout_source);
         sd_event_source_unref(m->scheduled_shutdown_timeout_source);
         sd_event_source_unref(m->idle_action_event_source);
         sd_event_source_unref(m->inhibit_timeout_source);
         sd_event_source_unref(m->scheduled_shutdown_timeout_source);
@@ -157,16 +195,11 @@ static void manager_free(Manager *m) {
 
         safe_close(m->console_active_fd);
 
 
         safe_close(m->console_active_fd);
 
-        if (m->udev_seat_monitor)
                 udev_monitor_unref(m->udev_seat_monitor);
                 udev_monitor_unref(m->udev_seat_monitor);
-        if (m->udev_device_monitor)
                 udev_monitor_unref(m->udev_device_monitor);
                 udev_monitor_unref(m->udev_device_monitor);
-        if (m->udev_vcsa_monitor)
                 udev_monitor_unref(m->udev_vcsa_monitor);
                 udev_monitor_unref(m->udev_vcsa_monitor);
-        if (m->udev_button_monitor)
                 udev_monitor_unref(m->udev_button_monitor);
 
                 udev_monitor_unref(m->udev_button_monitor);
 
-        if (m->udev)
                 udev_unref(m->udev);
 
         if (m->unlink_nologin)
                 udev_unref(m->udev);
 
         if (m->unlink_nologin)
@@ -177,7 +210,18 @@ static void manager_free(Manager *m) {
         sd_bus_unref(m->bus);
         sd_event_unref(m->event);
 
         sd_bus_unref(m->bus);
         sd_event_unref(m->event);
 
+/// elogind does not support autospawning of vts
+#if 0
         safe_close(m->reserve_vt_fd);
         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);
         strv_free(m->kill_exclude_users);
 
         strv_free(m->kill_only_users);
         strv_free(m->kill_exclude_users);
@@ -185,7 +229,14 @@ static void manager_free(Manager *m) {
         free(m->scheduled_shutdown_type);
         free(m->scheduled_shutdown_tty);
         free(m->wall_message);
         free(m->scheduled_shutdown_type);
         free(m->scheduled_shutdown_tty);
         free(m->wall_message);
-        free(m->action_job);
+
+        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);
 }
 
         free(m);
 }
 
@@ -516,6 +567,8 @@ static int manager_dispatch_device_udev(sd_event_source *s, int fd, uint32_t rev
         return 0;
 }
 
         return 0;
 }
 
+/// UNNEEDED by elogind
+#if 0
 static int manager_dispatch_vcsa_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
         _cleanup_udev_device_unref_ struct udev_device *d = NULL;
         Manager *m = userdata;
 static int manager_dispatch_vcsa_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
         _cleanup_udev_device_unref_ struct udev_device *d = NULL;
         Manager *m = userdata;
@@ -537,6 +590,7 @@ static int manager_dispatch_vcsa_udev(sd_event_source *s, int fd, uint32_t reven
 
         return 0;
 }
 
         return 0;
 }
+#endif // 0
 
 static int manager_dispatch_button_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
         _cleanup_udev_device_unref_ struct udev_device *d = NULL;
 
 static int manager_dispatch_button_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
         _cleanup_udev_device_unref_ struct udev_device *d = NULL;
@@ -563,6 +617,8 @@ static int manager_dispatch_console(sd_event_source *s, int fd, uint32_t revents
         return 0;
 }
 
         return 0;
 }
 
+/// UNNEEDED by elogind
+#if 0
 static int manager_reserve_vt(Manager *m) {
         _cleanup_free_ char *p = NULL;
 
 static int manager_reserve_vt(Manager *m) {
         _cleanup_free_ char *p = NULL;
 
@@ -585,6 +641,35 @@ static int manager_reserve_vt(Manager *m) {
 
         return 0;
 }
 
         return 0;
 }
+#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;
 
 static int manager_connect_bus(Manager *m) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
@@ -601,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");
 
         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");
         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");
@@ -625,17 +718,8 @@ static int manager_connect_bus(Manager *m) {
         if (r < 0)
                 return log_error_errno(r, "Failed to add user enumerator: %m");
 
         if (r < 0)
                 return log_error_errno(r, "Failed to add user enumerator: %m");
 
-        r = sd_bus_add_match(m->bus,
-                             NULL,
-                             "type='signal',"
-                             "sender='org.freedesktop.DBus',"
-                             "interface='org.freedesktop.DBus',"
-                             "member='NameOwnerChanged',"
-                             "path='/org/freedesktop/DBus'",
-                             match_name_owner_changed, m);
-        if (r < 0)
-                return log_error_errno(r, "Failed to add match for NameOwnerChanged: %m");
-
+/// elogind does not support systemd as PID 1
+#if 0
         r = sd_bus_add_match(m->bus,
                              NULL,
                              "type='signal',"
         r = sd_bus_add_match(m->bus,
                              NULL,
                              "type='signal',"
@@ -645,7 +729,7 @@ static int manager_connect_bus(Manager *m) {
                              "path='/org/freedesktop/systemd1'",
                              match_job_removed, m);
         if (r < 0)
                              "path='/org/freedesktop/systemd1'",
                              match_job_removed, m);
         if (r < 0)
-                return log_error_errno(r, "Failed to add match for JobRemoved: %m");
+                log_warning_errno(r, "Failed to add match for JobRemoved: %m");
 
         r = sd_bus_add_match(m->bus,
                              NULL,
 
         r = sd_bus_add_match(m->bus,
                              NULL,
@@ -656,7 +740,7 @@ static int manager_connect_bus(Manager *m) {
                              "path='/org/freedesktop/systemd1'",
                              match_unit_removed, m);
         if (r < 0)
                              "path='/org/freedesktop/systemd1'",
                              match_unit_removed, m);
         if (r < 0)
-                return log_error_errno(r, "Failed to add match for UnitRemoved: %m");
+                log_warning_errno(r, "Failed to add match for UnitRemoved: %m");
 
         r = sd_bus_add_match(m->bus,
                              NULL,
 
         r = sd_bus_add_match(m->bus,
                              NULL,
@@ -666,7 +750,7 @@ static int manager_connect_bus(Manager *m) {
                              "member='PropertiesChanged'",
                              match_properties_changed, m);
         if (r < 0)
                              "member='PropertiesChanged'",
                              match_properties_changed, m);
         if (r < 0)
-                return log_error_errno(r, "Failed to add match for PropertiesChanged: %m");
+                log_warning_errno(r, "Failed to add match for PropertiesChanged: %m");
 
         r = sd_bus_add_match(m->bus,
                              NULL,
 
         r = sd_bus_add_match(m->bus,
                              NULL,
@@ -677,7 +761,7 @@ static int manager_connect_bus(Manager *m) {
                              "path='/org/freedesktop/systemd1'",
                              match_reloading, m);
         if (r < 0)
                              "path='/org/freedesktop/systemd1'",
                              match_reloading, m);
         if (r < 0)
-                return log_error_errno(r, "Failed to add match for Reloading: %m");
+                log_warning_errno(r, "Failed to add match for Reloading: %m");
 
         r = sd_bus_call_method(
                         m->bus,
 
         r = sd_bus_call_method(
                         m->bus,
@@ -687,10 +771,9 @@ static int manager_connect_bus(Manager *m) {
                         "Subscribe",
                         &error,
                         NULL, NULL);
                         "Subscribe",
                         &error,
                         NULL, NULL);
-        if (r < 0) {
-                log_error("Failed to enable subscription: %s", bus_error_message(&error, r));
-                return r;
-        }
+        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)
 
         r = sd_bus_request_name(m->bus, "org.freedesktop.login1", 0);
         if (r < 0)
@@ -786,13 +869,8 @@ static int manager_connect_console(Manager *m) {
                 return -EINVAL;
         }
 
                 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)
 
         r = sd_event_add_signal(m->event, NULL, SIGRTMIN, manager_vt_switch, m);
         if (r < 0)
@@ -879,6 +957,8 @@ static int manager_connect_udev(Manager *m) {
         }
 
         /* Don't bother watching VCSA devices, if nobody cares */
         }
 
         /* Don't bother watching VCSA devices, if nobody cares */
+/// elogind does not support autospawning of vts
+#if 0
         if (m->n_autovts > 0 && m->console_active_fd >= 0) {
 
                 m->udev_vcsa_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
         if (m->n_autovts > 0 && m->console_active_fd >= 0) {
 
                 m->udev_vcsa_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
@@ -897,6 +977,7 @@ static int manager_connect_udev(Manager *m) {
                 if (r < 0)
                         return r;
         }
                 if (r < 0)
                         return r;
         }
+#endif // 0
 
         return 0;
 }
 
         return 0;
 }
@@ -927,8 +1008,8 @@ static void manager_gc(Manager *m, bool drop_not_started) {
                     session_get_state(session) != SESSION_CLOSING)
                         session_stop(session, false);
 
                     session_get_state(session) != SESSION_CLOSING)
                         session_stop(session, false);
 
-                /* Normally, this should make the session busy again,
-                 * if it doesn't then let's get rid of it
+                /* Normally, this should make the session referenced
+                 * again, if it doesn't then let's get rid of it
                  * immediately */
                 if (!session_check_gc(session, drop_not_started)) {
                         session_finalize(session);
                  * immediately */
                 if (!session_check_gc(session, drop_not_started)) {
                         session_finalize(session);
@@ -1076,7 +1157,10 @@ static int manager_startup(Manager *m) {
         manager_gc(m, false);
 
         /* Reserve the special reserved VT */
         manager_gc(m, false);
 
         /* Reserve the special reserved VT */
+/// elogind does not support autospawning of vts
+#if 0
         manager_reserve_vt(m);
         manager_reserve_vt(m);
+#endif // 0
 
         /* And start everything */
         HASHMAP_FOREACH(seat, m->seats, i)
 
         /* And start everything */
         HASHMAP_FOREACH(seat, m->seats, i)
@@ -1113,6 +1197,12 @@ static int manager_run(Manager *m) {
 
                 manager_gc(m, true);
 
 
                 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;
                 r = sd_event_run(m->event, (uint64_t) -1);
                 if (r < 0)
                         return r;
@@ -1120,24 +1210,45 @@ static int manager_run(Manager *m) {
 }
 
 static int manager_parse_config_file(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);
 
         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);
         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;
 
 }
 
 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();
 
         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) {
         umask(0022);
 
         if (argc != 1) {
@@ -1151,9 +1262,21 @@ int main(int argc, char *argv[]) {
          * existence of /run/systemd/seats/ to determine whether
          * logind is available, so please always make sure this check
          * stays in. */
          * existence of /run/systemd/seats/ to determine whether
          * logind is available, so please always make sure this check
          * stays in. */
-        mkdir_label("/run/systemd/seats", 0755);
-        mkdir_label("/run/systemd/users", 0755);
-        mkdir_label("/run/systemd/sessions", 0755);
+        r = mkdir_label("/run/systemd", 0755);
+        if ( (r < 0) && (-EEXIST != r) )
+                return log_error_errno(r, "Failed to create /run/systemd : %m");
+        r = mkdir_label("/run/systemd/seats", 0755);
+        if ( r < 0 && (-EEXIST != r) )
+                return log_error_errno(r, "Failed to create /run/systemd/seats : %m");
+        r = mkdir_label("/run/systemd/users", 0755);
+        if ( r < 0 && (-EEXIST != r) )
+                return log_error_errno(r, "Failed to create /run/systemd/users : %m");
+        r = mkdir_label("/run/systemd/sessions", 0755);
+        if ( r < 0 && (-EEXIST != r) )
+                return log_error_errno(r, "Failed to create /run/systemd/sessions : %m");
+        r = mkdir_label("/run/systemd/machines", 0755);
+        if ( r < 0 && (-EEXIST != r) )
+                return log_error_errno(r, "Failed to create /run/systemd/machines : %m");
 
         m = manager_new();
         if (!m) {
 
         m = manager_new();
         if (!m) {
@@ -1184,7 +1307,6 @@ finish:
                   "STOPPING=1\n"
                   "STATUS=Shutting down...");
 
                   "STOPPING=1\n"
                   "STATUS=Shutting down...");
 
-        if (m)
                 manager_free(m);
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
                 manager_free(m);
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;