/* SPDX-License-Identifier: LGPL-2.1+ */
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
#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 "fs-util.h"
#include "logind.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);
-static void manager_reset_config(Manager *m) {
#if 0 /// elogind does not support autospawning of vts
- 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->handle_suspend_key = HANDLE_SUSPEND;
- m->handle_hibernate_key = HANDLE_HIBERNATE;
- m->handle_lid_switch = HANDLE_SUSPEND;
- m->handle_lid_switch_ep = _HANDLE_ACTION_INVALID;
- m->handle_lid_switch_docked = HANDLE_IGNORE;
- m->power_key_ignore_inhibited = false;
- m->suspend_key_ignore_inhibited = false;
- m->hibernate_key_ignore_inhibited = false;
- m->lid_switch_ignore_inhibited = true;
-
- m->holdoff_timeout_usec = 30 * USEC_PER_SEC;
-
- m->idle_action_usec = 30 * USEC_PER_MINUTE;
- m->idle_action = HANDLE_IGNORE;
-
- m->runtime_dir_size = physical_memory_scale(10U, 100U); /* 10% */
- m->user_tasks_max = system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE, 100U); /* 33% */
- m->sessions_max = 8192;
- m->inhibitors_max = 8192;
-
- m->kill_user_processes = KILL_USER_PROCESSES;
-
- m->kill_only_users = strv_free(m->kill_only_users);
- m->kill_exclude_users = strv_free(m->kill_exclude_users);
-}
-
-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
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;
- sd_event_set_watchdog(m->event, true);
+ r = sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
+ if (r < 0)
+ return r;
- manager_reset_config(m);
+ r = sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
+ if (r < 0)
+ return r;
- return m;
+ (void) sd_event_set_watchdog(m->event, true);
-fail:
- manager_free(m);
- return NULL;
+ manager_reset_config(m);
+
+ *ret = TAKE_PTR(m);
+ return 0;
}
-static void manager_free(Manager *m) {
+static Manager* manager_unref(Manager *m) {
Session *session;
User *u;
Device *d;
Button *b;
if (!m)
- return;
+ return NULL;
while ((session = hashmap_first(m->sessions)))
session_free(session);
#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);
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));
}
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;
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));
return r;
}
+static int parse_fdname(const char *fdname, char **session_id, dev_t *dev) {
+ _cleanup_strv_free_ char **parts = NULL;
+ _cleanup_free_ char *id = NULL;
+ unsigned int major, minor;
+ int r;
+
+ parts = strv_split(fdname, "-");
+ if (!parts)
+ return -ENOMEM;
+ if (strv_length(parts) != 5)
+ return -EINVAL;
+
+ 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);
+ if (r < 0)
+ return r;
+ r = safe_atou(parts[4], &minor);
+ if (r < 0)
+ return r;
+
+ *dev = makedev(major, minor);
+ *session_id = TAKE_PTR(id);
+
+ return 0;
+}
+
static int manager_attach_fds(Manager *m) {
_cleanup_strv_free_ char **fdnames = NULL;
int n, i, fd;
return n;
for (i = 0; i < n; i++) {
+ _cleanup_free_ char *id = NULL;
+ dev_t dev;
struct stat st;
SessionDevice *sd;
Session *s;
- char *id;
+ int r;
fd = SD_LISTEN_FDS_START + i;
- id = startswith(fdnames[i], "session-");
- if (!id)
+ r = parse_fdname(fdnames[i], &id, &dev);
+ if (r < 0) {
+ log_debug_errno(r, "Failed to parse fd name %s: %m", fdnames[i]);
+ close_nointr(fd);
continue;
+ }
s = hashmap_get(m->sessions, id);
if (!s) {
continue;
}
- sd = hashmap_get(s->devices, &st.st_rdev);
+ if (!S_ISCHR(st.st_mode) || st.st_rdev != dev) {
+ log_debug("Device fd doesn't point to the expected character device node");
+ close_nointr(fd);
+ continue;
+ }
+
+ sd = hashmap_get(s->devices, &dev);
if (!sd) {
/* Weird, we got an fd for a session device which wasn't
- * recorded in the session state file... */
+ * 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);
+ major(dev), minor(dev), 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);
+ major(dev), minor(dev), s->id);
session_device_attach_fd(sd, fd, s->was_active);
}
}
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);
}
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);
#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;
#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);
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Subscribe",
-#endif // 0
NULL, NULL,
NULL);
+#endif // 0
if (r < 0)
return log_error_errno(r, "Failed to enable subscription: %m");
return 0;
}
-static int manager_parse_config_file(Manager *m) {
-#if 0 /// elogind parses its own config file
- assert(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,
-#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
- CONFIG_PARSE_WARN, m);
-}
-
static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
Manager *m = userdata;
int r;
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");
user_start(user);
HASHMAP_FOREACH(session, m->sessions, i)
- session_start(session);
+ session_start(session, NULL);
HASHMAP_FOREACH(inhibitor, m->inhibitors, i)
inhibitor_start(inhibitor);
}
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
log_set_target(LOG_TARGET_AUTO);
log_set_facility(LOG_AUTH);
log_parse_environment();
-#ifdef ENABLE_DEBUG_ELOGIND
+#if ENABLE_DEBUG_ELOGIND
log_set_max_level(LOG_DEBUG);
log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
#endif // ENABLE_DEBUG_ELOGIND
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) )
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);
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;
}