1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
11 #include "bus-error.h"
13 #include "cgroup-util.h"
14 #include "conf-parser.h"
15 #include "device-util.h"
16 #include "efi-loader.h"
17 #include "errno-util.h"
19 #include "limits-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "stdio-util.h"
26 #include "terminal-util.h"
27 #include "udev-util.h"
28 #include "user-util.h"
30 #include "utmp-wtmp.h"
32 void manager_reset_config(Manager
*m
) {
38 m
->inhibit_delay_max
= 5 * USEC_PER_SEC
;
39 m
->user_stop_delay
= 10 * USEC_PER_SEC
;
41 m
->handle_power_key
= HANDLE_POWEROFF
;
42 m
->handle_power_key_long_press
= HANDLE_IGNORE
;
43 m
->handle_reboot_key
= HANDLE_REBOOT
;
44 m
->handle_reboot_key_long_press
= HANDLE_POWEROFF
;
45 m
->handle_suspend_key
= HANDLE_SUSPEND
;
46 m
->handle_suspend_key_long_press
= HANDLE_HIBERNATE
;
47 m
->handle_hibernate_key
= HANDLE_HIBERNATE
;
48 m
->handle_hibernate_key_long_press
= HANDLE_IGNORE
;
50 m
->handle_lid_switch
= HANDLE_SUSPEND
;
51 m
->handle_lid_switch_ep
= _HANDLE_ACTION_INVALID
;
52 m
->handle_lid_switch_docked
= HANDLE_IGNORE
;
54 m
->power_key_ignore_inhibited
= false;
55 m
->suspend_key_ignore_inhibited
= false;
56 m
->hibernate_key_ignore_inhibited
= false;
57 m
->lid_switch_ignore_inhibited
= true;
58 m
->reboot_key_ignore_inhibited
= false;
60 m
->holdoff_timeout_usec
= 30 * USEC_PER_SEC
;
62 m
->idle_action_usec
= 30 * USEC_PER_MINUTE
;
63 m
->idle_action
= HANDLE_IGNORE
;
65 m
->runtime_dir_size
= physical_memory_scale(10U, 100U); /* 10% */
66 m
->runtime_dir_inodes
= DIV_ROUND_UP(m
->runtime_dir_size
, 4096); /* 4k per inode */
67 m
->sessions_max
= 8192;
68 m
->inhibitors_max
= 8192;
70 m
->kill_user_processes
= KILL_USER_PROCESSES
;
72 m
->kill_only_users
= strv_free(m
->kill_only_users
);
73 m
->kill_exclude_users
= strv_free(m
->kill_exclude_users
);
76 int manager_parse_config_file(Manager
*m
) {
79 return config_parse_many_nulstr(
80 PKGSYSCONFDIR
"/logind.conf",
81 CONF_PATHS_NULSTR("systemd/logind.conf.d"),
83 config_item_perf_lookup
, logind_gperf_lookup
,
88 int manager_add_device(Manager
*m
, const char *sysfs
, bool master
, Device
**ret_device
) {
94 d
= hashmap_get(m
->devices
, sysfs
);
96 /* we support adding master-flags, but not removing them */
97 d
->master
= d
->master
|| master
;
99 d
= device_new(m
, sysfs
, master
);
110 int manager_add_seat(Manager
*m
, const char *id
, Seat
**ret_seat
) {
117 s
= hashmap_get(m
->seats
, id
);
119 r
= seat_new(&s
, m
, id
);
130 int manager_add_session(Manager
*m
, const char *id
, Session
**ret_session
) {
137 s
= hashmap_get(m
->sessions
, id
);
139 r
= session_new(&s
, m
, id
);
150 int manager_add_user(
161 u
= hashmap_get(m
->users
, UID_TO_PTR(ur
->uid
));
163 r
= user_new(&u
, m
, ur
);
174 int manager_add_user_by_name(
179 _cleanup_(user_record_unrefp
) UserRecord
*ur
= NULL
;
185 r
= userdb_by_name(name
, USERDB_SUPPRESS_SHADOW
, &ur
);
189 return manager_add_user(m
, ur
, ret_user
);
192 int manager_add_user_by_uid(
197 _cleanup_(user_record_unrefp
) UserRecord
*ur
= NULL
;
201 assert(uid_is_valid(uid
));
203 r
= userdb_by_uid(uid
, USERDB_SUPPRESS_SHADOW
, &ur
);
207 return manager_add_user(m
, ur
, ret_user
);
210 int manager_add_inhibitor(Manager
*m
, const char* id
, Inhibitor
**ret
) {
217 i
= hashmap_get(m
->inhibitors
, id
);
219 r
= inhibitor_new(&i
, m
, id
);
230 int manager_add_button(Manager
*m
, const char *name
, Button
**ret_button
) {
236 b
= hashmap_get(m
->buttons
, name
);
238 b
= button_new(m
, name
);
249 int manager_process_seat_device(Manager
*m
, sd_device
*d
) {
255 if (device_for_action(d
, SD_DEVICE_REMOVE
) ||
256 sd_device_has_current_tag(d
, "seat") <= 0) {
259 r
= sd_device_get_syspath(d
, &syspath
);
263 device
= hashmap_get(m
->devices
, syspath
);
267 seat_add_to_gc_queue(device
->seat
);
271 const char *sn
, *syspath
;
275 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
278 if (!seat_name_is_valid(sn
)) {
279 log_device_warning(d
, "Device with invalid seat name %s found, ignoring.", sn
);
283 seat
= hashmap_get(m
->seats
, sn
);
284 master
= sd_device_has_current_tag(d
, "master-of-seat") > 0;
286 /* Ignore non-master devices for unknown seats */
287 if (!master
&& !seat
)
290 r
= sd_device_get_syspath(d
, &syspath
);
294 r
= manager_add_device(m
, syspath
, master
, &device
);
299 r
= manager_add_seat(m
, sn
, &seat
);
308 device_attach(device
, seat
);
315 int manager_process_button_device(Manager
*m
, sd_device
*d
) {
322 r
= sd_device_get_sysname(d
, &sysname
);
326 if (device_for_action(d
, SD_DEVICE_REMOVE
) ||
327 sd_device_has_current_tag(d
, "power-switch") <= 0) {
329 b
= hashmap_get(m
->buttons
, sysname
);
338 r
= manager_add_button(m
, sysname
, &b
);
342 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
345 button_set_seat(b
, sn
);
348 if (r
< 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
349 * opening the device?) let's close the button again. */
356 int manager_get_session_by_pid(Manager
*m
, pid_t pid
, Session
**ret
) {
357 _cleanup_free_
char *unit
= NULL
;
363 if (!pid_is_valid(pid
))
366 s
= hashmap_get(m
->sessions_by_leader
, PID_TO_PTR(pid
));
368 r
= cg_pid_get_unit(pid
, &unit
);
370 s
= hashmap_get(m
->session_units
, unit
);
379 int manager_get_user_by_pid(Manager
*m
, pid_t pid
, User
**ret
) {
380 _cleanup_free_
char *unit
= NULL
;
386 if (!pid_is_valid(pid
))
389 r
= cg_pid_get_slice(pid
, &unit
);
391 u
= hashmap_get(m
->user_units
, unit
);
399 int manager_get_idle_hint(Manager
*m
, dual_timestamp
*t
) {
402 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
406 idle_hint
= !manager_is_inhibited(m
, INHIBIT_IDLE
, INHIBIT_BLOCK
, t
, false, false, 0, NULL
);
408 HASHMAP_FOREACH(s
, m
->sessions
) {
412 ih
= session_get_idle_hint(s
, &k
);
418 if (k
.monotonic
< ts
.monotonic
)
424 } else if (idle_hint
) {
426 if (k
.monotonic
> ts
.monotonic
)
437 bool manager_shall_kill(Manager
*m
, const char *user
) {
441 if (!m
->kill_exclude_users
&& streq(user
, "root"))
444 if (strv_contains(m
->kill_exclude_users
, user
))
447 if (!strv_isempty(m
->kill_only_users
))
448 return strv_contains(m
->kill_only_users
, user
);
450 return m
->kill_user_processes
;
453 int config_parse_n_autovts(
455 const char *filename
,
458 unsigned section_line
,
474 r
= safe_atou(rvalue
, &o
);
476 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
477 "Failed to parse number of autovts, ignoring: %s", rvalue
);
482 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
483 "A maximum of 15 autovts are supported, ignoring: %s", rvalue
);
491 static int vt_is_busy(unsigned vtnr
) {
492 struct vt_stat vt_stat
;
494 _cleanup_close_
int fd
= -1;
498 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
501 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
502 * we'd open the latter we'd open the foreground tty which
503 * hence would be unconditionally busy. By opening /dev/tty1
504 * we avoid this. Since tty1 is special and needs to be an
505 * explicitly loaded getty or DM this is safe. */
507 fd
= open_terminal("/dev/tty1", O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
511 if (ioctl(fd
, VT_GETSTATE
, &vt_stat
) < 0)
514 r
= !!(vt_stat
.v_state
& (1 << vtnr
));
519 int manager_spawn_autovt(Manager
*m
, unsigned vtnr
) {
520 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
521 char name
[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned)];
527 if (vtnr
> m
->n_autovts
&&
528 vtnr
!= m
->reserve_vt
)
531 if (vtnr
!= m
->reserve_vt
) {
532 /* If this is the reserved TTY, we'll start the getty
533 * on it in any case, but otherwise only if it is not
536 r
= vt_is_busy(vtnr
);
543 xsprintf(name
, "autovt@tty%u.service", vtnr
);
544 r
= sd_bus_call_method(
546 "org.freedesktop.systemd1",
547 "/org/freedesktop/systemd1",
548 "org.freedesktop.systemd1.Manager",
554 return log_error_errno(r
, "Failed to start %s: %s", name
, bus_error_message(&error
, r
));
559 bool manager_is_lid_closed(Manager
*m
) {
562 HASHMAP_FOREACH(b
, m
->buttons
)
569 static bool manager_is_docked(Manager
*m
) {
572 HASHMAP_FOREACH(b
, m
->buttons
)
579 static int manager_count_external_displays(Manager
*m
) {
580 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
584 r
= sd_device_enumerator_new(&e
);
588 r
= sd_device_enumerator_allow_uninitialized(e
);
592 r
= sd_device_enumerator_add_match_subsystem(e
, "drm", true);
596 FOREACH_DEVICE(e
, d
) {
597 const char *status
, *enabled
, *dash
, *nn
, *subsys
;
600 if (sd_device_get_parent(d
, &p
) < 0)
603 /* If the parent shares the same subsystem as the
604 * device we are looking at then it is a connector,
605 * which is what we are interested in. */
606 if (sd_device_get_subsystem(p
, &subsys
) < 0 || !streq(subsys
, "drm"))
609 if (sd_device_get_sysname(d
, &nn
) < 0)
612 /* Ignore internal displays: the type is encoded in the sysfs name, as the second dash
613 * separated item (the first is the card name, the last the connector number). We implement a
614 * deny list of external displays here, rather than an allow list of internal ones, to ensure
615 * we don't block suspends too eagerly. */
616 dash
= strchr(nn
, '-');
621 if (!STARTSWITH_SET(dash
,
622 "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
623 "Composite-", "SVIDEO-", "Component-",
624 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-"))
627 /* Ignore ports that are not enabled */
628 if (sd_device_get_sysattr_value(d
, "enabled", &enabled
) < 0 || !streq(enabled
, "enabled"))
631 /* We count any connector which is not explicitly
632 * "disconnected" as connected. */
633 if (sd_device_get_sysattr_value(d
, "status", &status
) < 0 || !streq(status
, "disconnected"))
640 bool manager_is_docked_or_external_displays(Manager
*m
) {
643 /* If we are docked don't react to lid closing */
644 if (manager_is_docked(m
)) {
645 log_debug("System is docked.");
649 /* If we have more than one display connected,
650 * assume that we are docked. */
651 n
= manager_count_external_displays(m
);
653 log_warning_errno(n
, "Display counting failed: %m");
655 log_debug("External (%i) displays connected.", n
);
662 bool manager_is_on_external_power(void) {
665 /* For now we only check for AC power, but 'external power' can apply to anything that isn't an internal
669 log_warning_errno(r
, "Failed to read AC power status: %m");
671 return r
!= 0; /* Treat failure as 'on AC' */
674 bool manager_all_buttons_ignored(Manager
*m
) {
677 if (m
->handle_power_key
!= HANDLE_IGNORE
)
679 if (m
->handle_power_key_long_press
!= HANDLE_IGNORE
)
681 if (m
->handle_suspend_key
!= HANDLE_IGNORE
)
683 if (m
->handle_suspend_key_long_press
!= HANDLE_IGNORE
)
685 if (m
->handle_hibernate_key
!= HANDLE_IGNORE
)
687 if (m
->handle_hibernate_key_long_press
!= HANDLE_IGNORE
)
689 if (m
->handle_reboot_key
!= HANDLE_IGNORE
)
691 if (m
->handle_reboot_key_long_press
!= HANDLE_IGNORE
)
693 if (m
->handle_lid_switch
!= HANDLE_IGNORE
)
695 if (!IN_SET(m
->handle_lid_switch_ep
, _HANDLE_ACTION_INVALID
, HANDLE_IGNORE
))
697 if (m
->handle_lid_switch_docked
!= HANDLE_IGNORE
)
703 int manager_read_utmp(Manager
*m
) {
706 _cleanup_(utxent_cleanup
) bool utmpx
= false;
710 if (utmpxname(_PATH_UTMPX
) < 0)
711 return log_error_errno(errno
, "Failed to set utmp path to " _PATH_UTMPX
": %m");
713 utmpx
= utxent_start();
716 _cleanup_free_
char *t
= NULL
;
725 log_warning_errno(errno
, "Failed to read " _PATH_UTMPX
", ignoring: %m");
729 if (u
->ut_type
!= USER_PROCESS
)
732 if (!pid_is_valid(u
->ut_pid
))
735 t
= strndup(u
->ut_line
, sizeof(u
->ut_line
));
739 c
= path_startswith(t
, "/dev/");
741 r
= free_and_strdup(&t
, c
);
749 s
= hashmap_get(m
->sessions_by_leader
, PID_TO_PTR(u
->ut_pid
));
753 if (s
->tty_validity
== TTY_FROM_UTMP
&& !streq_ptr(s
->tty
, t
)) {
754 /* This may happen on multiplexed SSH connection (i.e. 'SSH connection sharing'). In
755 * this case PAM and utmp sessions don't match. In such a case let's invalidate the TTY
756 * information and never acquire it again. */
758 s
->tty
= mfree(s
->tty
);
759 s
->tty_validity
= TTY_UTMP_INCONSISTENT
;
760 log_debug("Session '%s' has inconsistent TTY information, dropping TTY information.", s
->id
);
764 /* Never override what we figured out once */
765 if (s
->tty
|| s
->tty_validity
>= 0)
768 s
->tty
= TAKE_PTR(t
);
769 s
->tty_validity
= TTY_FROM_UTMP
;
770 log_debug("Acquired TTY information '%s' from utmp for session '%s'.", s
->tty
, s
->id
);
779 static int manager_dispatch_utmp(sd_event_source
*s
, const struct inotify_event
*event
, void *userdata
) {
780 Manager
*m
= userdata
;
784 /* If there's indication the file itself might have been removed or became otherwise unavailable, then let's
785 * reestablish the watch on whatever there's now. */
786 if ((event
->mask
& (IN_ATTRIB
|IN_DELETE_SELF
|IN_MOVE_SELF
|IN_Q_OVERFLOW
|IN_UNMOUNT
)) != 0)
787 manager_connect_utmp(m
);
789 (void) manager_read_utmp(m
);
794 void manager_connect_utmp(Manager
*m
) {
796 sd_event_source
*s
= NULL
;
801 /* Watch utmp for changes via inotify. We do this to deal with tools such as ssh, which will register the PAM
802 * session early, and acquire a TTY only much later for the connection. Thus during PAM the TTY won't be known
803 * yet. ssh will register itself with utmp when it finally acquired the TTY. Hence, let's make use of this, and
804 * watch utmp for the TTY asynchronously. We use the PAM session's leader PID as key, to find the right entry.
806 * Yes, relying on utmp is pretty ugly, but it's good enough for informational purposes, as well as idle
807 * detection (which, for tty sessions, relies on the TTY used) */
809 r
= sd_event_add_inotify(m
->event
, &s
, _PATH_UTMPX
, IN_MODIFY
|IN_MOVE_SELF
|IN_DELETE_SELF
|IN_ATTRIB
, manager_dispatch_utmp
, m
);
811 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
, "Failed to create inotify watch on " _PATH_UTMPX
", ignoring: %m");
813 r
= sd_event_source_set_priority(s
, SD_EVENT_PRIORITY_IDLE
);
815 log_warning_errno(r
, "Failed to adjust utmp event source priority, ignoring: %m");
817 (void) sd_event_source_set_description(s
, "utmp");
820 sd_event_source_unref(m
->utmp_event_source
);
821 m
->utmp_event_source
= s
;
825 void manager_reconnect_utmp(Manager
*m
) {
829 if (m
->utmp_event_source
)
832 manager_connect_utmp(m
);
836 int manager_read_efi_boot_loader_entries(Manager
*m
) {
841 if (m
->efi_boot_loader_entries_set
)
844 r
= efi_loader_get_entries(&m
->efi_boot_loader_entries
);
845 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
)) {
846 log_debug_errno(r
, "Boot loader reported no entries.");
847 m
->efi_boot_loader_entries_set
= true;
851 return log_error_errno(r
, "Failed to determine entries reported by boot loader: %m");
853 m
->efi_boot_loader_entries_set
= true;