1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
10 #include <sys/ioctl.h>
11 #include <sys/types.h>
14 #include "alloc-util.h"
15 #include "bus-error.h"
17 #include "cgroup-util.h"
20 #include "parse-util.h"
21 #include "process-util.h"
23 #include "terminal-util.h"
24 #include "udev-util.h"
25 #include "user-util.h"
27 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
33 d = hashmap_get(m->devices, sysfs);
35 /* we support adding master-flags, but not removing them */
36 d->master = d->master || master;
38 d = device_new(m, sysfs, master);
49 int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
55 s = hashmap_get(m->seats, id);
68 int manager_add_session(Manager *m, const char *id, Session **_session) {
74 s = hashmap_get(m->sessions, id);
76 s = session_new(m, id);
87 int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) {
94 u = hashmap_get(m->users, UID_TO_PTR(uid));
96 r = user_new(&u, m, uid, gid, name);
107 int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
115 r = get_user_creds(&name, &uid, &gid, NULL, NULL);
119 return manager_add_user(m, uid, gid, name, _user);
122 int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
130 return errno > 0 ? -errno : -ENOENT;
132 return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
135 int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
141 i = hashmap_get(m->inhibitors, id);
149 i = inhibitor_new(m, id);
159 int manager_add_button(Manager *m, const char *name, Button **_button) {
165 b = hashmap_get(m->buttons, name);
167 b = button_new(m, name);
178 int manager_process_seat_device(Manager *m, struct udev_device *d) {
184 if (streq_ptr(udev_device_get_action(d), "remove")) {
186 device = hashmap_get(m->devices, udev_device_get_syspath(d));
190 seat_add_to_gc_queue(device->seat);
198 sn = udev_device_get_property_value(d, "ID_SEAT");
202 if (!seat_name_is_valid(sn)) {
203 log_warning("Device with invalid seat name %s found, ignoring.", sn);
207 seat = hashmap_get(m->seats, sn);
208 master = udev_device_has_tag(d, "master-of-seat");
210 /* Ignore non-master devices for unknown seats */
211 if (!master && !seat)
214 r = manager_add_device(m, udev_device_get_syspath(d), master, &device);
219 r = manager_add_seat(m, sn, &seat);
228 device_attach(device, seat);
235 int manager_process_button_device(Manager *m, struct udev_device *d) {
242 if (streq_ptr(udev_device_get_action(d), "remove")) {
244 b = hashmap_get(m->buttons, udev_device_get_sysname(d));
253 r = manager_add_button(m, udev_device_get_sysname(d), &b);
257 sn = udev_device_get_property_value(d, "ID_SEAT");
261 button_set_seat(b, sn);
264 if (r < 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
265 * opening the device?) let's close the button again. */
272 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **ret) {
273 #if 0 /// elogind does not support systemd units, but its own session system
274 _cleanup_free_ char *unit = NULL;
276 _cleanup_free_ char *session_name = NULL;
283 if (!pid_is_valid(pid))
286 #if 0 /// elogind does not support systemd units, but its own session system
287 r = cg_pid_get_unit(pid, &unit);
291 s = hashmap_get(m->session_units, unit);
298 log_debug_elogind("Searching session for PID %u", pid);
299 r = cg_pid_get_session(pid, &session_name);
303 s = hashmap_get(m->sessions, session_name);
304 log_debug_elogind("Session Name \"%s\" -> Session \"%s\"",
305 session_name, s && s->id ? s->id : "NULL");
316 int manager_get_user_by_pid(Manager *m, pid_t pid, User **ret) {
317 #if 0 /// elogind does not support systemd units, but its own session system
318 _cleanup_free_ char *unit = NULL;
327 if (!pid_is_valid(pid))
330 #if 0 /// elogind does not support systemd units, but its own session system
331 r = cg_pid_get_slice(pid, &unit);
335 u = hashmap_get(m->user_units, unit);
350 r = manager_get_session_by_pid (m, pid, &s);
360 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
363 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
368 idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0, NULL);
370 HASHMAP_FOREACH(s, m->sessions, i) {
374 ih = session_get_idle_hint(s, &k);
380 if (k.monotonic < ts.monotonic)
386 } else if (idle_hint) {
388 if (k.monotonic > ts.monotonic)
399 bool manager_shall_kill(Manager *m, const char *user) {
403 if (!m->kill_exclude_users && streq(user, "root"))
406 if (strv_contains(m->kill_exclude_users, user))
409 if (!strv_isempty(m->kill_only_users))
410 return strv_contains(m->kill_only_users, user);
412 return m->kill_user_processes;
415 #if 0 /// UNNEEDED by elogind
416 int config_parse_n_autovts(
418 const char *filename,
421 unsigned section_line,
437 r = safe_atou(rvalue, &o);
439 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse number of autovts, ignoring: %s", rvalue);
444 log_syntax(unit, LOG_ERR, filename, line, r, "A maximum of 15 autovts are supported, ignoring: %s", rvalue);
452 static int vt_is_busy(unsigned int vtnr) {
453 struct vt_stat vt_stat;
455 _cleanup_close_ int fd;
459 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
462 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
463 * we'd open the latter we'd open the foreground tty which
464 * hence would be unconditionally busy. By opening /dev/tty1
465 * we avoid this. Since tty1 is special and needs to be an
466 * explicitly loaded getty or DM this is safe. */
468 fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
472 if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
475 r = !!(vt_stat.v_state & (1 << vtnr));
480 int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
481 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
482 char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
488 if (vtnr > m->n_autovts &&
489 vtnr != m->reserve_vt)
492 if (vtnr != m->reserve_vt) {
493 /* If this is the reserved TTY, we'll start the getty
494 * on it in any case, but otherwise only if it is not
497 r = vt_is_busy(vtnr);
504 snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
505 r = sd_bus_call_method(
507 "org.freedesktop.systemd1",
508 "/org/freedesktop/systemd1",
509 "org.freedesktop.systemd1.Manager",
515 log_error("Failed to start %s: %s", name, bus_error_message(&error, r));
521 static bool manager_is_docked(Manager *m) {
525 HASHMAP_FOREACH(b, m->buttons, i)
532 static int manager_count_external_displays(Manager *m) {
533 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
534 struct udev_list_entry *item = NULL, *first = NULL;
538 e = udev_enumerate_new(m->udev);
542 r = udev_enumerate_add_match_subsystem(e, "drm");
546 r = udev_enumerate_scan_devices(e);
550 first = udev_enumerate_get_list_entry(e);
551 udev_list_entry_foreach(item, first) {
552 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
553 struct udev_device *p;
554 const char *status, *enabled, *dash, *nn, *i;
555 bool external = false;
557 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
561 p = udev_device_get_parent(d);
565 /* If the parent shares the same subsystem as the
566 * device we are looking at then it is a connector,
567 * which is what we are interested in. */
568 if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
571 nn = udev_device_get_sysname(d);
575 /* Ignore internal displays: the type is encoded in
576 * the sysfs name, as the second dash separated item
577 * (the first is the card name, the last the connector
578 * number). We implement a whitelist of external
579 * displays here, rather than a whitelist, to ensure
580 * we don't block suspends too eagerly. */
581 dash = strchr(nn, '-');
586 FOREACH_STRING(i, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
587 "Composite-", "SVIDEO-", "Component-",
588 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
590 if (startswith(dash, i)) {
598 /* Ignore ports that are not enabled */
599 enabled = udev_device_get_sysattr_value(d, "enabled");
602 if (!streq_ptr(enabled, "enabled"))
605 /* We count any connector which is not explicitly
606 * "disconnected" as connected. */
607 status = udev_device_get_sysattr_value(d, "status");
608 if (!streq_ptr(status, "disconnected"))
615 bool manager_is_docked_or_external_displays(Manager *m) {
618 /* If we are docked don't react to lid closing */
619 if (manager_is_docked(m)) {
620 log_debug("System is docked.");
624 /* If we have more than one display connected,
625 * assume that we are docked. */
626 n = manager_count_external_displays(m);
628 log_warning_errno(n, "Display counting failed: %m");
630 log_debug("External (%i) displays connected.", n);
637 bool manager_is_on_external_power(void) {
640 /* For now we only check for AC power, but 'external power' can apply
641 * to anything that isn't an internal battery */
644 log_warning_errno(r, "Failed to read AC power status: %m");
651 bool manager_all_buttons_ignored(Manager *m) {
654 if (m->handle_power_key != HANDLE_IGNORE)
656 if (m->handle_suspend_key != HANDLE_IGNORE)
658 if (m->handle_hibernate_key != HANDLE_IGNORE)
660 if (m->handle_lid_switch != HANDLE_IGNORE)
662 if (m->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
663 m->handle_lid_switch_ep != HANDLE_IGNORE)
665 if (m->handle_lid_switch_docked != HANDLE_IGNORE)