1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2011 Lennart Poettering
12 #include "alloc-util.h"
13 #include "bus-error.h"
15 #include "cgroup-util.h"
16 //#include "conf-parser.h"
19 #include "parse-util.h"
20 #include "process-util.h"
22 #include "terminal-util.h"
23 #include "udev-util.h"
24 #include "user-util.h"
26 void manager_reset_config(Manager *m) {
32 m->inhibit_delay_max = 5 * USEC_PER_SEC;
33 m->handle_power_key = HANDLE_POWEROFF;
34 m->handle_suspend_key = HANDLE_SUSPEND;
35 m->handle_hibernate_key = HANDLE_HIBERNATE;
36 m->handle_lid_switch = HANDLE_SUSPEND;
37 m->handle_lid_switch_ep = _HANDLE_ACTION_INVALID;
38 m->handle_lid_switch_docked = HANDLE_IGNORE;
39 m->power_key_ignore_inhibited = false;
40 m->suspend_key_ignore_inhibited = false;
41 m->hibernate_key_ignore_inhibited = false;
42 m->lid_switch_ignore_inhibited = true;
44 m->holdoff_timeout_usec = 30 * USEC_PER_SEC;
46 m->idle_action_usec = 30 * USEC_PER_MINUTE;
47 m->idle_action = HANDLE_IGNORE;
49 m->runtime_dir_size = physical_memory_scale(10U, 100U); /* 10% */
50 m->user_tasks_max = system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE, 100U); /* 33% */
51 m->sessions_max = 8192;
52 m->inhibitors_max = 8192;
54 m->kill_user_processes = KILL_USER_PROCESSES;
56 m->kill_only_users = strv_free(m->kill_only_users);
57 m->kill_exclude_users = strv_free(m->kill_exclude_users);
60 int manager_parse_config_file(Manager *m) {
63 return config_parse_many_nulstr(PKGSYSCONFDIR "/logind.conf",
64 CONF_PATHS_NULSTR("elogind/logind.conf.d"),
66 config_item_perf_lookup, logind_gperf_lookup,
67 CONFIG_PARSE_WARN, m);
70 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
76 d = hashmap_get(m->devices, sysfs);
78 /* we support adding master-flags, but not removing them */
79 d->master = d->master || master;
81 d = device_new(m, sysfs, master);
92 int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
98 s = hashmap_get(m->seats, id);
111 int manager_add_session(Manager *m, const char *id, Session **_session) {
117 s = hashmap_get(m->sessions, id);
119 s = session_new(m, id);
130 int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) {
137 u = hashmap_get(m->users, UID_TO_PTR(uid));
139 r = user_new(&u, m, uid, gid, name);
150 int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
158 r = get_user_creds(&name, &uid, &gid, NULL, NULL);
162 return manager_add_user(m, uid, gid, name, _user);
165 int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
173 return errno > 0 ? -errno : -ENOENT;
175 return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
178 int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
184 i = hashmap_get(m->inhibitors, id);
192 i = inhibitor_new(m, id);
202 int manager_add_button(Manager *m, const char *name, Button **_button) {
208 b = hashmap_get(m->buttons, name);
210 b = button_new(m, name);
221 int manager_process_seat_device(Manager *m, struct udev_device *d) {
227 if (streq_ptr(udev_device_get_action(d), "remove")) {
229 device = hashmap_get(m->devices, udev_device_get_syspath(d));
233 seat_add_to_gc_queue(device->seat);
241 sn = udev_device_get_property_value(d, "ID_SEAT");
245 if (!seat_name_is_valid(sn)) {
246 log_warning("Device with invalid seat name %s found, ignoring.", sn);
250 seat = hashmap_get(m->seats, sn);
251 master = udev_device_has_tag(d, "master-of-seat");
253 /* Ignore non-master devices for unknown seats */
254 if (!master && !seat)
257 r = manager_add_device(m, udev_device_get_syspath(d), master, &device);
262 r = manager_add_seat(m, sn, &seat);
271 device_attach(device, seat);
278 int manager_process_button_device(Manager *m, struct udev_device *d) {
285 if (streq_ptr(udev_device_get_action(d), "remove")) {
287 b = hashmap_get(m->buttons, udev_device_get_sysname(d));
296 r = manager_add_button(m, udev_device_get_sysname(d), &b);
300 sn = udev_device_get_property_value(d, "ID_SEAT");
304 button_set_seat(b, sn);
307 if (r < 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
308 * opening the device?) let's close the button again. */
315 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **ret) {
316 #if 0 /// elogind does not support systemd units, but its own session system
317 _cleanup_free_ char *unit = NULL;
319 _cleanup_free_ char *session_name = NULL;
326 if (!pid_is_valid(pid))
329 #if 0 /// elogind does not support systemd units, but its own session system
330 r = cg_pid_get_unit(pid, &unit);
334 s = hashmap_get(m->session_units, unit);
341 log_debug_elogind("Searching session for PID %u", pid);
342 r = cg_pid_get_session(pid, &session_name);
346 s = hashmap_get(m->sessions, session_name);
347 log_debug_elogind("Session Name \"%s\" -> Session \"%s\"",
348 session_name, s && s->id ? s->id : "NULL");
359 int manager_get_user_by_pid(Manager *m, pid_t pid, User **ret) {
360 #if 0 /// elogind does not support systemd units, but its own session system
361 _cleanup_free_ char *unit = NULL;
370 if (!pid_is_valid(pid))
373 #if 0 /// elogind does not support systemd units, but its own session system
374 r = cg_pid_get_slice(pid, &unit);
378 u = hashmap_get(m->user_units, unit);
393 r = manager_get_session_by_pid (m, pid, &s);
403 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
406 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
411 idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0, NULL);
413 HASHMAP_FOREACH(s, m->sessions, i) {
417 ih = session_get_idle_hint(s, &k);
423 if (k.monotonic < ts.monotonic)
429 } else if (idle_hint) {
431 if (k.monotonic > ts.monotonic)
442 bool manager_shall_kill(Manager *m, const char *user) {
446 if (!m->kill_exclude_users && streq(user, "root"))
449 if (strv_contains(m->kill_exclude_users, user))
452 if (!strv_isempty(m->kill_only_users))
453 return strv_contains(m->kill_only_users, user);
455 return m->kill_user_processes;
458 #if 0 /// UNNEEDED by elogind
459 int config_parse_n_autovts(
461 const char *filename,
464 unsigned section_line,
480 r = safe_atou(rvalue, &o);
482 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse number of autovts, ignoring: %s", rvalue);
487 log_syntax(unit, LOG_ERR, filename, line, r, "A maximum of 15 autovts are supported, ignoring: %s", rvalue);
495 static int vt_is_busy(unsigned int vtnr) {
496 struct vt_stat vt_stat;
498 _cleanup_close_ int fd;
502 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
505 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
506 * we'd open the latter we'd open the foreground tty which
507 * hence would be unconditionally busy. By opening /dev/tty1
508 * we avoid this. Since tty1 is special and needs to be an
509 * explicitly loaded getty or DM this is safe. */
511 fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
515 if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
518 r = !!(vt_stat.v_state & (1 << vtnr));
523 int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
524 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
525 char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
531 if (vtnr > m->n_autovts &&
532 vtnr != m->reserve_vt)
535 if (vtnr != m->reserve_vt) {
536 /* If this is the reserved TTY, we'll start the getty
537 * on it in any case, but otherwise only if it is not
540 r = vt_is_busy(vtnr);
547 snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
548 r = sd_bus_call_method(
550 "org.freedesktop.systemd1",
551 "/org/freedesktop/systemd1",
552 "org.freedesktop.systemd1.Manager",
558 log_error("Failed to start %s: %s", name, bus_error_message(&error, r));
564 static bool manager_is_docked(Manager *m) {
568 HASHMAP_FOREACH(b, m->buttons, i)
575 static int manager_count_external_displays(Manager *m) {
576 _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
577 struct udev_list_entry *item = NULL, *first = NULL;
581 e = udev_enumerate_new(m->udev);
585 r = udev_enumerate_add_match_subsystem(e, "drm");
589 r = udev_enumerate_scan_devices(e);
593 first = udev_enumerate_get_list_entry(e);
594 udev_list_entry_foreach(item, first) {
595 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
596 struct udev_device *p;
597 const char *status, *enabled, *dash, *nn, *i;
598 bool external = false;
600 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
604 p = udev_device_get_parent(d);
608 /* If the parent shares the same subsystem as the
609 * device we are looking at then it is a connector,
610 * which is what we are interested in. */
611 if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
614 nn = udev_device_get_sysname(d);
618 /* Ignore internal displays: the type is encoded in
619 * the sysfs name, as the second dash separated item
620 * (the first is the card name, the last the connector
621 * number). We implement a whitelist of external
622 * displays here, rather than a whitelist, to ensure
623 * we don't block suspends too eagerly. */
624 dash = strchr(nn, '-');
629 FOREACH_STRING(i, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
630 "Composite-", "SVIDEO-", "Component-",
631 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
633 if (startswith(dash, i)) {
641 /* Ignore ports that are not enabled */
642 enabled = udev_device_get_sysattr_value(d, "enabled");
645 if (!streq_ptr(enabled, "enabled"))
648 /* We count any connector which is not explicitly
649 * "disconnected" as connected. */
650 status = udev_device_get_sysattr_value(d, "status");
651 if (!streq_ptr(status, "disconnected"))
658 bool manager_is_docked_or_external_displays(Manager *m) {
661 /* If we are docked don't react to lid closing */
662 if (manager_is_docked(m)) {
663 log_debug("System is docked.");
667 /* If we have more than one display connected,
668 * assume that we are docked. */
669 n = manager_count_external_displays(m);
671 log_warning_errno(n, "Display counting failed: %m");
673 log_debug("External (%i) displays connected.", n);
680 bool manager_is_on_external_power(void) {
683 /* For now we only check for AC power, but 'external power' can apply
684 * to anything that isn't an internal battery */
687 log_warning_errno(r, "Failed to read AC power status: %m");
694 bool manager_all_buttons_ignored(Manager *m) {
697 if (m->handle_power_key != HANDLE_IGNORE)
699 if (m->handle_suspend_key != HANDLE_IGNORE)
701 if (m->handle_hibernate_key != HANDLE_IGNORE)
703 if (m->handle_lid_switch != HANDLE_IGNORE)
705 if (m->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
706 m->handle_lid_switch_ep != HANDLE_IGNORE)
708 if (m->handle_lid_switch_docked != HANDLE_IGNORE)