chiark / gitweb /
Prep v239: Update manager_parse_config_file() that got moved to logind-core.c
[elogind.git] / src / login / logind.c
index 5008a273031026289be7bb30aa4b084ecf402e9b..b605367007390cb5c9e1fbc3cc033d4865c9f6b3 100644 (file)
@@ -1,9 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2011 Lennart Poettering
-***/
 
 #include <errno.h>
 #include <fcntl.h>
 #include "bus-error.h"
 #include "bus-util.h"
 //#include "cgroup-util.h"
-#include "conf-parser.h"
 #include "def.h"
 #include "dirent-util.h"
 #include "fd-util.h"
 #include "format-util.h"
 #include "fs-util.h"
 #include "logind.h"
-//#include "parse-util.h"
+#include "parse-util.h"
 //#include "process-util.h"
 #include "selinux-util.h"
 #include "signal-util.h"
 #include "process-util.h"
 #include "cgroup-util.h"
 
-static void manager_free(Manager *m);
+static Manager* manager_unref(Manager *m);
+DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref);
 
 #if 0 /// elogind does not support autospawning of vts
 #endif // 0
-static Manager *manager_new(void) {
-        Manager *m;
+static int manager_new(Manager **ret) {
+        _cleanup_(manager_unrefp) Manager *m = NULL;
         int r;
 
+        assert(ret);
+
         m = new0(Manager, 1);
         if (!m)
-                return NULL;
+                return -ENOMEM;
 
         m->console_active_fd = -1;
 #if 0 /// UNNEEDED by elogind
@@ -72,33 +69,38 @@ static Manager *manager_new(void) {
         m->session_units = hashmap_new(&string_hash_ops);
 
         if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units)
-                goto fail;
+                return -ENOMEM;
 
 #if 1 /// elogind needs some more data
         r = elogind_manager_new(m);
         if (r < 0)
-                goto fail;
+                return r;
 #endif // 1
         m->udev = udev_new();
         if (!m->udev)
-                goto fail;
+                return -errno;
 
         r = sd_event_default(&m->event);
         if (r < 0)
-                goto fail;
+                return r;
+
+        r = sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
+        if (r < 0)
+                return r;
 
-        sd_event_set_watchdog(m->event, true);
+        r = sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
+        if (r < 0)
+                return r;
 
-        manager_reset_config(m);
+        (void) sd_event_set_watchdog(m->event, true);
 
-        return m;
+        manager_reset_config(m);
 
-fail:
-        manager_free(m);
-        return NULL;
+        *ret = TAKE_PTR(m);
+        return 0;
 }
 
-static void manager_free(Manager *m) {
+static Manager* manager_unref(Manager *m) {
         Session *session;
         User *u;
         Device *d;
@@ -107,7 +109,7 @@ static void manager_free(Manager *m) {
         Button *b;
 
         if (!m)
-                return;
+                return NULL;
 
         while ((session = hashmap_first(m->sessions)))
                 session_free(session);
@@ -183,12 +185,13 @@ static void manager_free(Manager *m) {
 #if 0 /// UNNEEDED by elogind
         free(m->action_job);
 #endif // 0
-        free(m);
+
+        return mfree(m);
 }
 
 static int manager_enumerate_devices(Manager *m) {
         struct udev_list_entry *item = NULL, *first = NULL;
-        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
+        _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
         int r;
 
         assert(m);
@@ -214,7 +217,7 @@ static int manager_enumerate_devices(Manager *m) {
 
         first = udev_enumerate_get_list_entry(e);
         udev_list_entry_foreach(item, first) {
-                _cleanup_udev_device_unref_ struct udev_device *d = NULL;
+                _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
                 int k;
 
                 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
@@ -230,7 +233,7 @@ static int manager_enumerate_devices(Manager *m) {
 }
 
 static int manager_enumerate_buttons(Manager *m) {
-        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
+        _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
         struct udev_list_entry *item = NULL, *first = NULL;
         int r;
 
@@ -263,7 +266,7 @@ static int manager_enumerate_buttons(Manager *m) {
 
         first = udev_enumerate_get_list_entry(e);
         udev_list_entry_foreach(item, first) {
-                _cleanup_udev_device_unref_ struct udev_device *d = NULL;
+                _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
                 int k;
 
                 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
@@ -408,14 +411,18 @@ static int parse_fdname(const char *fdname, char **session_id, dev_t *dev) {
 
         if (!streq(parts[0], "session"))
                 return -EINVAL;
+
         id = strdup(parts[1]);
         if (!id)
                 return -ENOMEM;
 
         if (!streq(parts[2], "device"))
                 return -EINVAL;
-        r = safe_atou(parts[3], &major) ||
-            safe_atou(parts[4], &minor);
+
+        r = safe_atou(parts[3], &major);
+        if (r < 0)
+                return r;
+        r = safe_atou(parts[4], &minor);
         if (r < 0)
                 return r;
 
@@ -586,7 +593,7 @@ static int manager_enumerate_inhibitors(Manager *m) {
 }
 
 static int manager_dispatch_seat_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
-        _cleanup_udev_device_unref_ struct udev_device *d = NULL;
+        _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
         Manager *m = userdata;
 
         assert(m);
@@ -600,7 +607,7 @@ static int manager_dispatch_seat_udev(sd_event_source *s, int fd, uint32_t reven
 }
 
 static int manager_dispatch_device_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
-        _cleanup_udev_device_unref_ struct udev_device *d = NULL;
+        _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
         Manager *m = userdata;
 
         assert(m);
@@ -615,7 +622,7 @@ static int manager_dispatch_device_udev(sd_event_source *s, int fd, uint32_t rev
 
 #if 0 /// UNNEEDED by elogind
 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;
+        _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
         Manager *m = userdata;
         const char *name;
 
@@ -638,7 +645,7 @@ static int manager_dispatch_vcsa_udev(sd_event_source *s, int fd, uint32_t reven
 #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;
+        _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
         Manager *m = userdata;
 
         assert(m);
@@ -1100,18 +1107,6 @@ static int manager_dispatch_idle_action(sd_event_source *s, uint64_t t, void *us
         return 0;
 }
 
-#if 0 /// elogind parses its own config file
-#else
-         const char* logind_conf = getenv("ELOGIND_CONF_FILE");
-
-         assert(m);
-
-         if (!logind_conf)
-                 logind_conf = PKGSYSCONFDIR "/logind.conf";
-
-         return config_parse(NULL, logind_conf, NULL, "Login\0Sleep\0",
-                             config_item_perf_lookup, logind_gperf_lookup,
-#endif // 0
 static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
         Manager *m = userdata;
         int r;
@@ -1140,8 +1135,6 @@ static int manager_startup(Manager *m) {
 
         assert(m);
 
-        assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGHUP, -1) >= 0);
-
         r = sd_event_add_signal(m->event, NULL, SIGHUP, manager_dispatch_reload_signal, m);
         if (r < 0)
                 return log_error_errno(r, "Failed to register SIGHUP handler: %m");
@@ -1254,7 +1247,7 @@ static int manager_run(Manager *m) {
 }
 
 int main(int argc, char *argv[]) {
-        Manager *m = NULL;
+        _cleanup_(manager_unrefp) Manager *m = NULL;
         int r;
 
 #if 1 /// perform extra checks for elogind startup
@@ -1288,15 +1281,22 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        /* Always create the directories people can create inotify
-         * watches in. Note that some applications might check for the
-         * existence of /run/systemd/seats/ to determine whether
-         * logind is available, so please always make sure this check
-         * stays in. */
 #if 0 /// elogind can not rely on systemd to help, so we need a bit more effort than this
         mkdir_label("/run/systemd/seats", 0755);
         mkdir_label("/run/systemd/users", 0755);
         mkdir_label("/run/systemd/sessions", 0755);
+        /* Always create the directories people can create inotify watches in. Note that some applications might check
+         * for the existence of /run/systemd/seats/ to determine whether logind is available, so please always make
+         * sure these directories are created early on and unconditionally. */
+        (void) mkdir_label("/run/systemd/seats", 0755);
+        (void) mkdir_label("/run/systemd/users", 0755);
+        (void) mkdir_label("/run/systemd/sessions", 0755);
+
+        assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGHUP, SIGTERM, SIGINT, -1) >= 0);
+
+        r = manager_new(&m);
+        if (r < 0) {
+                log_error_errno(r, "Failed to allocate manager object: %m");
 #else
         r = mkdir_label("/run/systemd", 0755);
         if ( (r < 0) && (-EEXIST != r) )
@@ -1314,14 +1314,10 @@ int main(int argc, char *argv[]) {
         if ( r < 0 && (-EEXIST != r) )
                 return log_error_errno(r, "Failed to create /run/systemd/machines : %m");
 #endif // 0
-
-        m = manager_new();
-        if (!m) {
-                r = log_oom();
                 goto finish;
         }
 
-        manager_parse_config_file(m);
+        (void) manager_parse_config_file(m);
 
 #if 1 /// elogind needs an Add-On for sleep configuration
         elogind_manager_reset_config(m);
@@ -1334,20 +1330,18 @@ int main(int argc, char *argv[]) {
 
         log_debug("elogind running as pid "PID_FMT, getpid_cached());
 
-        sd_notify(false,
-                  "READY=1\n"
-                  "STATUS=Processing requests...");
+        (void) sd_notify(false,
+                         "READY=1\n"
+                         "STATUS=Processing requests...");
 
         r = manager_run(m);
 
         log_debug("elogind stopped as pid "PID_FMT, getpid_cached());
 
-finish:
-        sd_notify(false,
-                  "STOPPING=1\n"
-                  "STATUS=Shutting down...");
-
-        manager_free(m);
+        (void) sd_notify(false,
+                         "STOPPING=1\n"
+                         "STATUS=Shutting down...");
 
+finish:
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }