chiark / gitweb /
Prep v234: Apply missing upstream fixes in src/login (3/6)
[elogind.git] / src / login / logind.c
index e0f391f4165b01a021de97b7db8972c82f62ccde..d46358e31ff392090d4e873d756ed627ac5fe6dd 100644 (file)
 #include "def.h"
 #include "dirent-util.h"
 #include "fd-util.h"
 #include "def.h"
 #include "dirent-util.h"
 #include "fd-util.h"
-#include "formats-util.h"
+#include "format-util.h"
 #include "logind.h"
 #include "selinux-util.h"
 #include "signal-util.h"
 #include "strv.h"
 #include "udev-util.h"
 #include "logind.h"
 #include "selinux-util.h"
 #include "signal-util.h"
 #include "strv.h"
 #include "udev-util.h"
-
 /// Additional includes needed by elogind
 #include "cgroup.h"       // From src/core/
 #include "elogind.h"
 /// Additional includes needed by elogind
 #include "cgroup.h"       // From src/core/
 #include "elogind.h"
@@ -52,7 +51,6 @@
 static void manager_free(Manager *m);
 
 static void manager_reset_config(Manager *m) {
 static void manager_free(Manager *m);
 
 static void manager_reset_config(Manager *m) {
-
 #if 0 /// elogind does not support autospawning of vts
         m->n_autovts = 6;
         m->reserve_vt = 6;
 #if 0 /// elogind does not support autospawning of vts
         m->n_autovts = 6;
         m->reserve_vt = 6;
@@ -75,7 +73,7 @@ static void manager_reset_config(Manager *m) {
         m->idle_action = HANDLE_IGNORE;
 
         m->runtime_dir_size = physical_memory_scale(10U, 100U); /* 10% */
         m->idle_action = HANDLE_IGNORE;
 
         m->runtime_dir_size = physical_memory_scale(10U, 100U); /* 10% */
-        m->user_tasks_max = system_tasks_max_scale(33U, 100U); /* 33% */
+        m->user_tasks_max = system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE, 100U); /* 33% */
         m->sessions_max = 8192;
         m->inhibitors_max = 8192;
 
         m->sessions_max = 8192;
         m->inhibitors_max = 8192;
 
@@ -83,7 +81,6 @@ static void manager_reset_config(Manager *m) {
 
         m->kill_only_users = strv_free(m->kill_only_users);
         m->kill_exclude_users = strv_free(m->kill_exclude_users);
 
         m->kill_only_users = strv_free(m->kill_only_users);
         m->kill_exclude_users = strv_free(m->kill_exclude_users);
-
 #if 1 /// elogind needs an Add-On for sleep configuration
         elogind_manager_reset_config(m);
 #endif // 1
 #if 1 /// elogind needs an Add-On for sleep configuration
         elogind_manager_reset_config(m);
 #endif // 1
@@ -122,7 +119,6 @@ static Manager *manager_new(void) {
         if (r < 0)
                 goto fail;
 #endif // 1
         if (r < 0)
                 goto fail;
 #endif // 1
-
         m->udev = udev_new();
         if (!m->udev)
                 goto fail;
         m->udev = udev_new();
         if (!m->udev)
                 goto fail;
@@ -150,7 +146,8 @@ static void manager_free(Manager *m) {
         Inhibitor *i;
         Button *b;
 
         Inhibitor *i;
         Button *b;
 
-        assert(m);
+        if (!m)
+                return;
 
         while ((session = hashmap_first(m->sessions)))
                 session_free(session);
 
         while ((session = hashmap_first(m->sessions)))
                 session_free(session);
@@ -213,7 +210,6 @@ static void manager_free(Manager *m) {
 #if 0 /// elogind does not support autospawning of vts
         safe_close(m->reserve_vt_fd);
 #endif // 0
 #if 0 /// elogind does not support autospawning of vts
         safe_close(m->reserve_vt_fd);
 #endif // 0
-
 #if 1 /// elogind has to free its own data
         elogind_manager_free(m);
 #endif // 1
 #if 1 /// elogind has to free its own data
         elogind_manager_free(m);
 #endif // 1
@@ -440,10 +436,71 @@ static int manager_enumerate_users(Manager *m) {
         return r;
 }
 
         return r;
 }
 
+static int manager_attach_fds(Manager *m) {
+        _cleanup_strv_free_ char **fdnames = NULL;
+        int n, i, fd;
+
+        /* Upon restart, PID1 will send us back all fds of session devices
+         * that we previously opened. Each file descriptor is associated
+         * with a given session. The session ids are passed through FDNAMES. */
+
+        n = sd_listen_fds_with_names(true, &fdnames);
+        if (n <= 0)
+                return n;
+
+        for (i = 0; i < n; i++) {
+                struct stat st;
+                SessionDevice *sd;
+                Session *s;
+                char *id;
+
+                fd = SD_LISTEN_FDS_START + i;
+
+                id = startswith(fdnames[i], "session-");
+                if (!id)
+                        continue;
+
+                s = hashmap_get(m->sessions, id);
+                if (!s) {
+                        /* If the session doesn't exist anymore, the associated session
+                         * device attached to this fd doesn't either. Let's simply close
+                         * this fd. */
+                        log_debug("Failed to attach fd for unknown session: %s", id);
+                        close_nointr(fd);
+                        continue;
+                }
+
+                if (fstat(fd, &st) < 0) {
+                        /* The device is allowed to go away at a random point, in which
+                         * case fstat failing is expected. */
+                        log_debug_errno(errno, "Failed to stat device fd for session %s: %m", id);
+                        close_nointr(fd);
+                        continue;
+                }
+
+                sd = hashmap_get(s->devices, &st.st_rdev);
+                if (!sd) {
+                        /* Weird we got an fd for a session device which wasn't
+                        * recorded in the session state file... */
+                        log_warning("Got fd for missing session device [%u:%u] in session %s",
+                                    major(st.st_rdev), minor(st.st_rdev), s->id);
+                        close_nointr(fd);
+                        continue;
+                }
+
+                log_debug("Attaching fd to session device [%u:%u] for session %s",
+                          major(st.st_rdev), minor(st.st_rdev), s->id);
+
+                session_device_attach_fd(sd, fd, s->was_active);
+        }
+
+        return 0;
+}
+
 static int manager_enumerate_sessions(Manager *m) {
         _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
 static int manager_enumerate_sessions(Manager *m) {
         _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
-        int r = 0;
+        int r = 0, k;
 
         assert(m);
 
 
         assert(m);
 
@@ -458,7 +515,6 @@ static int manager_enumerate_sessions(Manager *m) {
 
         FOREACH_DIRENT(de, d, return -errno) {
                 struct Session *s;
 
         FOREACH_DIRENT(de, d, return -errno) {
                 struct Session *s;
-                int k;
 
                 if (!dirent_is_file(de))
                         continue;
 
                 if (!dirent_is_file(de))
                         continue;
@@ -472,7 +528,6 @@ static int manager_enumerate_sessions(Manager *m) {
                 k = manager_add_session(m, de->d_name, &s);
                 if (k < 0) {
                         log_error_errno(k, "Failed to add session by file name %s: %m", de->d_name);
                 k = manager_add_session(m, de->d_name, &s);
                 if (k < 0) {
                         log_error_errno(k, "Failed to add session by file name %s: %m", de->d_name);
-
                         r = k;
                         continue;
                 }
                         r = k;
                         continue;
                 }
@@ -484,6 +539,12 @@ static int manager_enumerate_sessions(Manager *m) {
                         r = k;
         }
 
                         r = k;
         }
 
+        /* We might be restarted and PID1 could have sent us back the
+         * session device fds we previously saved. */
+        k = manager_attach_fds(m);
+        if (k < 0)
+                log_warning_errno(k, "Failed to reattach session device fds: %m");
+
         return r;
 }
 
         return r;
 }
 
@@ -731,12 +792,14 @@ static int manager_connect_bus(Manager *m) {
         if (r < 0)
                 return log_error_errno(r, "Failed to attach bus to event loop: %m");
 
         if (r < 0)
                 return log_error_errno(r, "Failed to attach bus to event loop: %m");
 
-#if 1 /// elogind has to setup its release agent
+#if 0 /// elogind has to setup its release agent
+        return 0;
+#else
         elogind_bus_setup_system(m);
         r = elogind_setup_cgroups_agent(m);
         elogind_bus_setup_system(m);
         r = elogind_setup_cgroups_agent(m);
-#endif // 1
 
         return r;
 
         return r;
+#endif // 0
 }
 
 static int manager_vt_switch(sd_event_source *src, const struct signalfd_siginfo *si, void *data) {
 }
 
 static int manager_vt_switch(sd_event_source *src, const struct signalfd_siginfo *si, void *data) {
@@ -1048,11 +1111,11 @@ static int manager_parse_config_file(Manager *m) {
 #if 0 /// elogind parses its own config file
         assert(m);
 
 #if 0 /// elogind parses its own config file
         assert(m);
 
-        return config_parse_many(PKGSYSCONFDIR "/logind.conf",
-                                 CONF_PATHS_NULSTR("systemd/logind.conf.d"),
-                                 "Login\0",
-                                 config_item_perf_lookup, logind_gperf_lookup,
-                                 false, m);
+        return config_parse_many_nulstr(PKGSYSCONFDIR "/logind.conf",
+                                        CONF_PATHS_NULSTR("systemd/logind.conf.d"),
+                                        "Login\0",
+                                        config_item_perf_lookup, logind_gperf_lookup,
+                                        false, m);
 #else
          const char* logind_conf = getenv("ELOGIND_CONF_FILE");
 
 #else
          const char* logind_conf = getenv("ELOGIND_CONF_FILE");
 
@@ -1210,11 +1273,11 @@ int main(int argc, char *argv[]) {
         log_set_target(LOG_TARGET_AUTO);
         log_set_facility(LOG_AUTH);
         log_parse_environment();
         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);
 #ifdef ENABLE_DEBUG_ELOGIND
         log_set_max_level(LOG_DEBUG);
+        log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
 #endif // ENABLE_DEBUG_ELOGIND
 #endif // ENABLE_DEBUG_ELOGIND
+        log_open();
 
         umask(0022);
 
 
         umask(0022);
 
@@ -1286,8 +1349,7 @@ finish:
                   "STOPPING=1\n"
                   "STATUS=Shutting down...");
 
                   "STOPPING=1\n"
                   "STATUS=Shutting down...");
 
-        if (m)
-                manager_free(m);
+        manager_free(m);
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }