1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "bus-error.h"
12 #include "cgroup-util.h"
13 //#include "conf-parser.h"
16 #include "parse-util.h"
17 #include "process-util.h"
19 #include "terminal-util.h"
20 #include "udev-util.h"
21 #include "user-util.h"
23 void manager_reset_config(Manager *m) {
29 m->inhibit_delay_max = 5 * USEC_PER_SEC;
30 m->handle_power_key = HANDLE_POWEROFF;
31 m->handle_suspend_key = HANDLE_SUSPEND;
32 m->handle_hibernate_key = HANDLE_HIBERNATE;
33 m->handle_lid_switch = HANDLE_SUSPEND;
34 m->handle_lid_switch_ep = _HANDLE_ACTION_INVALID;
35 m->handle_lid_switch_docked = HANDLE_IGNORE;
36 m->power_key_ignore_inhibited = false;
37 m->suspend_key_ignore_inhibited = false;
38 m->hibernate_key_ignore_inhibited = false;
39 m->lid_switch_ignore_inhibited = true;
41 m->holdoff_timeout_usec = 30 * USEC_PER_SEC;
43 m->idle_action_usec = 30 * USEC_PER_MINUTE;
44 m->idle_action = HANDLE_IGNORE;
46 m->runtime_dir_size = physical_memory_scale(10U, 100U); /* 10% */
47 m->user_tasks_max = system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE, 100U); /* 33% */
48 m->sessions_max = 8192;
49 m->inhibitors_max = 8192;
51 m->kill_user_processes = KILL_USER_PROCESSES;
53 m->kill_only_users = strv_free(m->kill_only_users);
54 m->kill_exclude_users = strv_free(m->kill_exclude_users);
57 int manager_parse_config_file(Manager *m) {
60 return config_parse_many_nulstr(PKGSYSCONFDIR "/logind.conf",
61 CONF_PATHS_NULSTR("elogind/logind.conf.d"),
63 config_item_perf_lookup, logind_gperf_lookup,
64 CONFIG_PARSE_WARN, m);
67 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
73 d = hashmap_get(m->devices, sysfs);
75 /* we support adding master-flags, but not removing them */
76 d->master = d->master || master;
78 d = device_new(m, sysfs, master);
89 int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
95 s = hashmap_get(m->seats, id);
108 int manager_add_session(Manager *m, const char *id, Session **_session) {
114 s = hashmap_get(m->sessions, id);
116 s = session_new(m, id);
127 int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) {
134 u = hashmap_get(m->users, UID_TO_PTR(uid));
136 r = user_new(&u, m, uid, gid, name);
147 int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
155 r = get_user_creds(&name, &uid, &gid, NULL, NULL);
159 return manager_add_user(m, uid, gid, name, _user);
162 int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
170 return errno > 0 ? -errno : -ENOENT;
172 return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
175 int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
181 i = hashmap_get(m->inhibitors, id);
189 i = inhibitor_new(m, id);
199 int manager_add_button(Manager *m, const char *name, Button **_button) {
205 b = hashmap_get(m->buttons, name);
207 b = button_new(m, name);
218 int manager_process_seat_device(Manager *m, struct udev_device *d) {
224 if (streq_ptr(udev_device_get_action(d), "remove")) {
226 device = hashmap_get(m->devices, udev_device_get_syspath(d));
230 seat_add_to_gc_queue(device->seat);
238 sn = udev_device_get_property_value(d, "ID_SEAT");
242 if (!seat_name_is_valid(sn)) {
243 log_warning("Device with invalid seat name %s found, ignoring.", sn);
247 seat = hashmap_get(m->seats, sn);
248 master = udev_device_has_tag(d, "master-of-seat");
250 /* Ignore non-master devices for unknown seats */
251 if (!master && !seat)
254 r = manager_add_device(m, udev_device_get_syspath(d), master, &device);
259 r = manager_add_seat(m, sn, &seat);
268 device_attach(device, seat);
275 int manager_process_button_device(Manager *m, struct udev_device *d) {
282 if (streq_ptr(udev_device_get_action(d), "remove")) {
284 b = hashmap_get(m->buttons, udev_device_get_sysname(d));
293 r = manager_add_button(m, udev_device_get_sysname(d), &b);
297 sn = udev_device_get_property_value(d, "ID_SEAT");
301 button_set_seat(b, sn);
304 if (r < 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
305 * opening the device?) let's close the button again. */
312 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **ret) {
313 #if 0 /// elogind does not support systemd units, but its own session system
314 _cleanup_free_ char *unit = NULL;
316 _cleanup_free_ char *session_name = NULL;
323 if (!pid_is_valid(pid))
326 #if 0 /// elogind does not support systemd units, but its own session system
327 r = cg_pid_get_unit(pid, &unit);
331 s = hashmap_get(m->session_units, unit);
338 log_debug_elogind("Searching session for PID %u", pid);
339 r = cg_pid_get_session(pid, &session_name);
343 s = hashmap_get(m->sessions, session_name);
344 log_debug_elogind("Session Name \"%s\" -> Session \"%s\"",
345 session_name, s && s->id ? s->id : "NULL");
356 int manager_get_user_by_pid(Manager *m, pid_t pid, User **ret) {
357 #if 0 /// elogind does not support systemd units, but its own session system
358 _cleanup_free_ char *unit = NULL;
367 if (!pid_is_valid(pid))
370 #if 0 /// elogind does not support systemd units, but its own session system
371 r = cg_pid_get_slice(pid, &unit);
375 u = hashmap_get(m->user_units, unit);
390 r = manager_get_session_by_pid (m, pid, &s);
400 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
403 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
408 idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0, NULL);
410 HASHMAP_FOREACH(s, m->sessions, i) {
414 ih = session_get_idle_hint(s, &k);
420 if (k.monotonic < ts.monotonic)
426 } else if (idle_hint) {
428 if (k.monotonic > ts.monotonic)
439 bool manager_shall_kill(Manager *m, const char *user) {
443 if (!m->kill_exclude_users && streq(user, "root"))
446 if (strv_contains(m->kill_exclude_users, user))
449 if (!strv_isempty(m->kill_only_users))
450 return strv_contains(m->kill_only_users, user);
452 return m->kill_user_processes;
455 #if 0 /// UNNEEDED by elogind
456 int config_parse_n_autovts(
458 const char *filename,
461 unsigned section_line,
477 r = safe_atou(rvalue, &o);
479 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse number of autovts, ignoring: %s", rvalue);
484 log_syntax(unit, LOG_ERR, filename, line, r, "A maximum of 15 autovts are supported, ignoring: %s", rvalue);
492 static int vt_is_busy(unsigned int vtnr) {
493 struct vt_stat vt_stat;
495 _cleanup_close_ int fd;
499 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
502 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
503 * we'd open the latter we'd open the foreground tty which
504 * hence would be unconditionally busy. By opening /dev/tty1
505 * we avoid this. Since tty1 is special and needs to be an
506 * explicitly loaded getty or DM this is safe. */
508 fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
512 if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
515 r = !!(vt_stat.v_state & (1 << vtnr));
520 int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
521 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
522 char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
528 if (vtnr > m->n_autovts &&
529 vtnr != m->reserve_vt)
532 if (vtnr != m->reserve_vt) {
533 /* If this is the reserved TTY, we'll start the getty
534 * on it in any case, but otherwise only if it is not
537 r = vt_is_busy(vtnr);
544 snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
545 r = sd_bus_call_method(
547 "org.freedesktop.systemd1",
548 "/org/freedesktop/systemd1",
549 "org.freedesktop.systemd1.Manager",
555 log_error("Failed to start %s: %s", name, bus_error_message(&error, r));
561 static bool manager_is_docked(Manager *m) {
565 HASHMAP_FOREACH(b, m->buttons, i)
572 static int manager_count_external_displays(Manager *m) {
573 _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
574 struct udev_list_entry *item = NULL, *first = NULL;
578 e = udev_enumerate_new(m->udev);
582 r = udev_enumerate_add_match_subsystem(e, "drm");
586 r = udev_enumerate_scan_devices(e);
590 first = udev_enumerate_get_list_entry(e);
591 udev_list_entry_foreach(item, first) {
592 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
593 struct udev_device *p;
594 const char *status, *enabled, *dash, *nn, *i;
595 bool external = false;
597 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
601 p = udev_device_get_parent(d);
605 /* If the parent shares the same subsystem as the
606 * device we are looking at then it is a connector,
607 * which is what we are interested in. */
608 if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
611 nn = udev_device_get_sysname(d);
615 /* Ignore internal displays: the type is encoded in
616 * the sysfs name, as the second dash separated item
617 * (the first is the card name, the last the connector
618 * number). We implement a whitelist of external
619 * displays here, rather than a whitelist, to ensure
620 * we don't block suspends too eagerly. */
621 dash = strchr(nn, '-');
626 FOREACH_STRING(i, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
627 "Composite-", "SVIDEO-", "Component-",
628 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
630 if (startswith(dash, i)) {
638 /* Ignore ports that are not enabled */
639 enabled = udev_device_get_sysattr_value(d, "enabled");
642 if (!streq_ptr(enabled, "enabled"))
645 /* We count any connector which is not explicitly
646 * "disconnected" as connected. */
647 status = udev_device_get_sysattr_value(d, "status");
648 if (!streq_ptr(status, "disconnected"))
655 bool manager_is_docked_or_external_displays(Manager *m) {
658 /* If we are docked don't react to lid closing */
659 if (manager_is_docked(m)) {
660 log_debug("System is docked.");
664 /* If we have more than one display connected,
665 * assume that we are docked. */
666 n = manager_count_external_displays(m);
668 log_warning_errno(n, "Display counting failed: %m");
670 log_debug("External (%i) displays connected.", n);
677 bool manager_is_on_external_power(void) {
680 /* For now we only check for AC power, but 'external power' can apply
681 * to anything that isn't an internal battery */
684 log_warning_errno(r, "Failed to read AC power status: %m");
691 bool manager_all_buttons_ignored(Manager *m) {
694 if (m->handle_power_key != HANDLE_IGNORE)
696 if (m->handle_suspend_key != HANDLE_IGNORE)
698 if (m->handle_hibernate_key != HANDLE_IGNORE)
700 if (m->handle_lid_switch != HANDLE_IGNORE)
702 if (m->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
703 m->handle_lid_switch_ep != HANDLE_IGNORE)
705 if (m->handle_lid_switch_docked != HANDLE_IGNORE)