1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
27 #include "alloc-util.h"
28 #include "bus-error.h"
30 #include "cgroup-util.h"
33 #include "parse-util.h"
34 //#include "process-util.h"
36 #include "terminal-util.h"
37 #include "udev-util.h"
38 #include "user-util.h"
40 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
46 d = hashmap_get(m->devices, sysfs);
48 /* we support adding master-flags, but not removing them */
49 d->master = d->master || master;
51 d = device_new(m, sysfs, master);
62 int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
68 s = hashmap_get(m->seats, id);
81 int manager_add_session(Manager *m, const char *id, Session **_session) {
87 s = hashmap_get(m->sessions, id);
89 s = session_new(m, id);
100 int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) {
107 u = hashmap_get(m->users, UID_TO_PTR(uid));
109 r = user_new(&u, m, uid, gid, name);
120 int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
128 r = get_user_creds(&name, &uid, &gid, NULL, NULL);
132 return manager_add_user(m, uid, gid, name, _user);
135 int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
143 return errno > 0 ? -errno : -ENOENT;
145 return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
148 int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
154 i = hashmap_get(m->inhibitors, id);
162 i = inhibitor_new(m, id);
172 int manager_add_button(Manager *m, const char *name, Button **_button) {
178 b = hashmap_get(m->buttons, name);
180 b = button_new(m, name);
191 int manager_process_seat_device(Manager *m, struct udev_device *d) {
197 if (streq_ptr(udev_device_get_action(d), "remove")) {
199 device = hashmap_get(m->devices, udev_device_get_syspath(d));
203 seat_add_to_gc_queue(device->seat);
211 sn = udev_device_get_property_value(d, "ID_SEAT");
215 if (!seat_name_is_valid(sn)) {
216 log_warning("Device with invalid seat name %s found, ignoring.", sn);
220 seat = hashmap_get(m->seats, sn);
221 master = udev_device_has_tag(d, "master-of-seat");
223 /* Ignore non-master devices for unknown seats */
224 if (!master && !seat)
227 r = manager_add_device(m, udev_device_get_syspath(d), master, &device);
232 r = manager_add_seat(m, sn, &seat);
241 device_attach(device, seat);
248 int manager_process_button_device(Manager *m, struct udev_device *d) {
255 if (streq_ptr(udev_device_get_action(d), "remove")) {
257 b = hashmap_get(m->buttons, udev_device_get_sysname(d));
266 r = manager_add_button(m, udev_device_get_sysname(d), &b);
270 sn = udev_device_get_property_value(d, "ID_SEAT");
274 button_set_seat(b, sn);
277 if (r < 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
278 * opening the device?) let's close the button again. */
285 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
286 #if 0 /// elogind does not support systemd units, but its own session system
287 _cleanup_free_ char *unit = NULL;
289 _cleanup_free_ char *session_name = NULL;
296 if (!pid_is_valid(pid))
299 #if 0 /// elogind does not support systemd units, but its own session system
300 r = cg_pid_get_unit(pid, &unit);
304 s = hashmap_get(m->session_units, unit);
306 log_debug_elogind("Searching session for PID %u", pid);
307 r = cg_pid_get_session(pid, &session_name);
311 s = hashmap_get(m->sessions, session_name);
312 log_debug_elogind("Session Name \"%s\" -> Session \"%s\"",
313 session_name, s && s->id ? s->id : "NULL");
323 int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
324 #if 0 /// elogind does not support systemd units, but its own session system
325 _cleanup_free_ char *unit = NULL;
335 if (!pid_is_valid(pid))
338 #if 0 /// elogind does not support systemd units, but its own session system
339 r = cg_pid_get_slice(pid, &unit);
343 u = hashmap_get(m->user_units, unit);
349 r = manager_get_session_by_pid (m, pid, &s);
358 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
361 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
366 idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0, NULL);
368 HASHMAP_FOREACH(s, m->sessions, i) {
372 ih = session_get_idle_hint(s, &k);
378 if (k.monotonic < ts.monotonic)
384 } else if (idle_hint) {
386 if (k.monotonic > ts.monotonic)
397 bool manager_shall_kill(Manager *m, const char *user) {
401 if (!m->kill_exclude_users && streq(user, "root"))
404 if (strv_contains(m->kill_exclude_users, user))
407 if (!strv_isempty(m->kill_only_users))
408 return strv_contains(m->kill_only_users, user);
410 return m->kill_user_processes;
413 #if 0 /// UNNEEDED by elogind
414 int config_parse_n_autovts(
416 const char *filename,
419 unsigned section_line,
435 r = safe_atou(rvalue, &o);
437 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse number of autovts, ignoring: %s", rvalue);
442 log_syntax(unit, LOG_ERR, filename, line, r, "A maximum of 15 autovts are supported, ignoring: %s", rvalue);
450 static int vt_is_busy(unsigned int vtnr) {
451 struct vt_stat vt_stat;
453 _cleanup_close_ int fd;
457 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
460 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
461 * we'd open the latter we'd open the foreground tty which
462 * hence would be unconditionally busy. By opening /dev/tty1
463 * we avoid this. Since tty1 is special and needs to be an
464 * explicitly loaded getty or DM this is safe. */
466 fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
470 if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
473 r = !!(vt_stat.v_state & (1 << vtnr));
478 int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
479 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
480 char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
486 if (vtnr > m->n_autovts &&
487 vtnr != m->reserve_vt)
490 if (vtnr != m->reserve_vt) {
491 /* If this is the reserved TTY, we'll start the getty
492 * on it in any case, but otherwise only if it is not
495 r = vt_is_busy(vtnr);
502 snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
503 r = sd_bus_call_method(
505 "org.freedesktop.systemd1",
506 "/org/freedesktop/systemd1",
507 "org.freedesktop.systemd1.Manager",
513 log_error("Failed to start %s: %s", name, bus_error_message(&error, r));
519 static bool manager_is_docked(Manager *m) {
523 HASHMAP_FOREACH(b, m->buttons, i)
530 static int manager_count_external_displays(Manager *m) {
531 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
532 struct udev_list_entry *item = NULL, *first = NULL;
536 e = udev_enumerate_new(m->udev);
540 r = udev_enumerate_add_match_subsystem(e, "drm");
544 r = udev_enumerate_scan_devices(e);
548 first = udev_enumerate_get_list_entry(e);
549 udev_list_entry_foreach(item, first) {
550 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
551 struct udev_device *p;
552 const char *status, *enabled, *dash, *nn, *i;
553 bool external = false;
555 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
559 p = udev_device_get_parent(d);
563 /* If the parent shares the same subsystem as the
564 * device we are looking at then it is a connector,
565 * which is what we are interested in. */
566 if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
569 nn = udev_device_get_sysname(d);
573 /* Ignore internal displays: the type is encoded in
574 * the sysfs name, as the second dash separated item
575 * (the first is the card name, the last the connector
576 * number). We implement a whitelist of external
577 * displays here, rather than a whitelist, to ensure
578 * we don't block suspends too eagerly. */
579 dash = strchr(nn, '-');
584 FOREACH_STRING(i, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
585 "Composite-", "SVIDEO-", "Component-",
586 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
588 if (startswith(dash, i)) {
596 /* Ignore ports that are not enabled */
597 enabled = udev_device_get_sysattr_value(d, "enabled");
600 if (!streq_ptr(enabled, "enabled"))
603 /* We count any connector which is not explicitly
604 * "disconnected" as connected. */
605 status = udev_device_get_sysattr_value(d, "status");
606 if (!streq_ptr(status, "disconnected"))
613 bool manager_is_docked_or_external_displays(Manager *m) {
616 /* If we are docked don't react to lid closing */
617 if (manager_is_docked(m)) {
618 log_debug("System is docked.");
622 /* If we have more than one display connected,
623 * assume that we are docked. */
624 n = manager_count_external_displays(m);
626 log_warning_errno(n, "Display counting failed: %m");
628 log_debug("External (%i) displays connected.", n);
635 bool manager_is_on_external_power(void) {
638 /* For now we only check for AC power, but 'external power' can apply
639 * to anything that isn't an internal battery */
642 log_warning_errno(r, "Failed to read AC power status: %m");
649 bool manager_all_buttons_ignored(Manager *m) {
652 if (m->handle_power_key != HANDLE_IGNORE)
654 if (m->handle_suspend_key != HANDLE_IGNORE)
656 if (m->handle_hibernate_key != HANDLE_IGNORE)
658 if (m->handle_lid_switch != HANDLE_IGNORE)
660 if (m->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
661 m->handle_lid_switch_ep != HANDLE_IGNORE)
663 if (m->handle_lid_switch_docked != HANDLE_IGNORE)