1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
11 #include "battery-util.h"
12 #include "bus-error.h"
13 #include "bus-locator.h"
15 #include "cgroup-util.h"
16 #include "conf-parser.h"
17 #include "device-util.h"
18 #include "efi-loader.h"
19 #include "errno-util.h"
21 #include "limits-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "stdio-util.h"
28 #include "terminal-util.h"
29 #include "udev-util.h"
30 #include "user-util.h"
32 #include "utmp-wtmp.h"
34 void manager_reset_config(Manager
*m
) {
40 m
->inhibit_delay_max
= 5 * USEC_PER_SEC
;
41 m
->user_stop_delay
= 10 * USEC_PER_SEC
;
43 m
->handle_action_sleep_mask
= HANDLE_ACTION_SLEEP_MASK_DEFAULT
;
45 m
->handle_power_key
= HANDLE_POWEROFF
;
46 m
->handle_power_key_long_press
= HANDLE_IGNORE
;
47 m
->handle_reboot_key
= HANDLE_REBOOT
;
48 m
->handle_reboot_key_long_press
= HANDLE_POWEROFF
;
49 m
->handle_suspend_key
= HANDLE_SUSPEND
;
50 m
->handle_suspend_key_long_press
= HANDLE_HIBERNATE
;
51 m
->handle_hibernate_key
= HANDLE_HIBERNATE
;
52 m
->handle_hibernate_key_long_press
= HANDLE_IGNORE
;
54 m
->handle_lid_switch
= HANDLE_SUSPEND
;
55 m
->handle_lid_switch_ep
= _HANDLE_ACTION_INVALID
;
56 m
->handle_lid_switch_docked
= HANDLE_IGNORE
;
58 m
->power_key_ignore_inhibited
= false;
59 m
->suspend_key_ignore_inhibited
= false;
60 m
->hibernate_key_ignore_inhibited
= false;
61 m
->lid_switch_ignore_inhibited
= true;
62 m
->reboot_key_ignore_inhibited
= false;
64 m
->holdoff_timeout_usec
= 30 * USEC_PER_SEC
;
66 m
->idle_action_usec
= 30 * USEC_PER_MINUTE
;
67 m
->idle_action
= HANDLE_IGNORE
;
69 m
->runtime_dir_size
= physical_memory_scale(10U, 100U); /* 10% */
70 m
->runtime_dir_inodes
= DIV_ROUND_UP(m
->runtime_dir_size
, 4096); /* 4k per inode */
71 m
->sessions_max
= 8192;
72 m
->inhibitors_max
= 8192;
74 m
->kill_user_processes
= KILL_USER_PROCESSES
;
76 m
->kill_only_users
= strv_free(m
->kill_only_users
);
77 m
->kill_exclude_users
= strv_free(m
->kill_exclude_users
);
79 m
->stop_idle_session_usec
= USEC_INFINITY
;
82 int manager_parse_config_file(Manager
*m
) {
85 return config_parse_config_file("logind.conf", "Login\0",
86 config_item_perf_lookup
, logind_gperf_lookup
,
87 CONFIG_PARSE_WARN
, m
);
90 int manager_add_device(Manager
*m
, const char *sysfs
, bool master
, Device
**ret_device
) {
96 d
= hashmap_get(m
->devices
, sysfs
);
98 /* we support adding master-flags, but not removing them */
99 d
->master
= d
->master
|| master
;
101 d
= device_new(m
, sysfs
, master
);
112 int manager_add_seat(Manager
*m
, const char *id
, Seat
**ret_seat
) {
119 s
= hashmap_get(m
->seats
, id
);
121 r
= seat_new(&s
, m
, id
);
132 int manager_add_session(Manager
*m
, const char *id
, Session
**ret_session
) {
139 s
= hashmap_get(m
->sessions
, id
);
141 r
= session_new(&s
, m
, id
);
152 int manager_add_user(
163 u
= hashmap_get(m
->users
, UID_TO_PTR(ur
->uid
));
165 r
= user_new(&u
, m
, ur
);
176 int manager_add_user_by_name(
181 _cleanup_(user_record_unrefp
) UserRecord
*ur
= NULL
;
187 r
= userdb_by_name(name
, USERDB_SUPPRESS_SHADOW
, &ur
);
191 return manager_add_user(m
, ur
, ret_user
);
194 int manager_add_user_by_uid(
199 _cleanup_(user_record_unrefp
) UserRecord
*ur
= NULL
;
203 assert(uid_is_valid(uid
));
205 r
= userdb_by_uid(uid
, USERDB_SUPPRESS_SHADOW
, &ur
);
209 return manager_add_user(m
, ur
, ret_user
);
212 int manager_add_inhibitor(Manager
*m
, const char* id
, Inhibitor
**ret
) {
219 i
= hashmap_get(m
->inhibitors
, id
);
221 r
= inhibitor_new(&i
, m
, id
);
232 int manager_add_button(Manager
*m
, const char *name
, Button
**ret_button
) {
238 b
= hashmap_get(m
->buttons
, name
);
240 b
= button_new(m
, name
);
251 int manager_process_seat_device(Manager
*m
, sd_device
*d
) {
257 if (device_for_action(d
, SD_DEVICE_REMOVE
) ||
258 sd_device_has_current_tag(d
, "seat") <= 0) {
261 r
= sd_device_get_syspath(d
, &syspath
);
265 device
= hashmap_get(m
->devices
, syspath
);
269 seat_add_to_gc_queue(device
->seat
);
273 const char *sn
, *syspath
;
277 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
280 if (!seat_name_is_valid(sn
)) {
281 log_device_warning(d
, "Device with invalid seat name %s found, ignoring.", sn
);
285 seat
= hashmap_get(m
->seats
, sn
);
286 master
= sd_device_has_current_tag(d
, "master-of-seat") > 0;
288 /* Ignore non-master devices for unknown seats */
289 if (!master
&& !seat
)
292 r
= sd_device_get_syspath(d
, &syspath
);
296 r
= manager_add_device(m
, syspath
, master
, &device
);
301 r
= manager_add_seat(m
, sn
, &seat
);
310 device_attach(device
, seat
);
317 int manager_process_button_device(Manager
*m
, sd_device
*d
) {
324 r
= sd_device_get_sysname(d
, &sysname
);
328 if (device_for_action(d
, SD_DEVICE_REMOVE
) ||
329 sd_device_has_current_tag(d
, "power-switch") <= 0)
331 button_free(hashmap_get(m
->buttons
, sysname
));
336 r
= manager_add_button(m
, sysname
, &b
);
340 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
343 button_set_seat(b
, sn
);
346 if (r
< 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
347 * opening the device?) let's close the button again. */
354 int manager_get_session_by_pidref(Manager
*m
, const PidRef
*pid
, Session
**ret
) {
355 _cleanup_free_
char *unit
= NULL
;
361 if (!pidref_is_set(pid
))
364 s
= hashmap_get(m
->sessions_by_leader
, pid
);
366 r
= pidref_verify(pid
);
370 r
= cg_pidref_get_unit(pid
, &unit
);
374 s
= hashmap_get(m
->session_units
, unit
);
383 int manager_get_user_by_pid(Manager
*m
, pid_t pid
, User
**ret
) {
384 _cleanup_free_
char *unit
= NULL
;
390 if (!pid_is_valid(pid
))
393 r
= cg_pid_get_slice(pid
, &unit
);
395 u
= hashmap_get(m
->user_units
, unit
);
403 int manager_get_idle_hint(Manager
*m
, dual_timestamp
*t
) {
406 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
410 idle_hint
= !manager_is_inhibited(m
, INHIBIT_IDLE
, INHIBIT_BLOCK
, t
, false, false, 0, NULL
);
412 HASHMAP_FOREACH(s
, m
->sessions
) {
416 if (!SESSION_CLASS_CAN_IDLE(s
->class))
419 ih
= session_get_idle_hint(s
, &k
);
425 if (k
.monotonic
< ts
.monotonic
)
431 } else if (idle_hint
) {
433 if (k
.monotonic
> ts
.monotonic
)
444 bool manager_shall_kill(Manager
*m
, const char *user
) {
448 if (!m
->kill_exclude_users
&& streq(user
, "root"))
451 if (strv_contains(m
->kill_exclude_users
, user
))
454 if (!strv_isempty(m
->kill_only_users
))
455 return strv_contains(m
->kill_only_users
, user
);
457 return m
->kill_user_processes
;
460 int config_parse_n_autovts(
462 const char *filename
,
465 unsigned section_line
,
472 unsigned *n
= ASSERT_PTR(data
);
480 r
= safe_atou(rvalue
, &o
);
482 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
483 "Failed to parse number of autovts, ignoring: %s", rvalue
);
488 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
489 "A maximum of 15 autovts are supported, ignoring: %s", rvalue
);
497 static int vt_is_busy(unsigned vtnr
) {
498 struct vt_stat vt_stat
;
500 _cleanup_close_
int fd
= -EBADF
;
504 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
507 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
508 * we'd open the latter we'd open the foreground tty which
509 * hence would be unconditionally busy. By opening /dev/tty1
510 * we avoid this. Since tty1 is special and needs to be an
511 * explicitly loaded getty or DM this is safe. */
513 fd
= open_terminal("/dev/tty1", O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
517 if (ioctl(fd
, VT_GETSTATE
, &vt_stat
) < 0)
520 r
= !!(vt_stat
.v_state
& (1 << vtnr
));
525 int manager_spawn_autovt(Manager
*m
, unsigned vtnr
) {
526 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
527 char name
[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned)];
533 if (vtnr
> m
->n_autovts
&&
534 vtnr
!= m
->reserve_vt
)
537 if (vtnr
!= m
->reserve_vt
) {
538 /* If this is the reserved TTY, we'll start the getty
539 * on it in any case, but otherwise only if it is not
542 r
= vt_is_busy(vtnr
);
549 xsprintf(name
, "autovt@tty%u.service", vtnr
);
550 r
= bus_call_method(m
->bus
, bus_systemd_mgr
, "StartUnit", &error
, NULL
, "ss", name
, "fail");
552 return log_error_errno(r
, "Failed to start %s: %s", name
, bus_error_message(&error
, r
));
557 bool manager_is_lid_closed(Manager
*m
) {
560 HASHMAP_FOREACH(b
, m
->buttons
)
567 static bool manager_is_docked(Manager
*m
) {
570 HASHMAP_FOREACH(b
, m
->buttons
)
577 static int manager_count_external_displays(Manager
*m
) {
578 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
581 r
= sd_device_enumerator_new(&e
);
585 r
= sd_device_enumerator_allow_uninitialized(e
);
589 r
= sd_device_enumerator_add_match_subsystem(e
, "drm", true);
593 FOREACH_DEVICE(e
, d
) {
594 const char *status
, *enabled
, *dash
, *nn
;
597 if (sd_device_get_parent(d
, &p
) < 0)
600 /* If the parent shares the same subsystem as the
601 * device we are looking at then it is a connector,
602 * which is what we are interested in. */
603 if (!device_in_subsystem(p
, "drm"))
606 if (sd_device_get_sysname(d
, &nn
) < 0)
609 /* Ignore internal displays: the type is encoded in the sysfs name, as the second dash
610 * separated item (the first is the card name, the last the connector number). We implement a
611 * deny list of external displays here, rather than an allow list of internal ones, to ensure
612 * we don't block suspends too eagerly. */
613 dash
= strchr(nn
, '-');
618 if (!STARTSWITH_SET(dash
,
619 "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
620 "Composite-", "SVIDEO-", "Component-",
621 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-"))
624 /* Ignore ports that are not enabled */
625 if (sd_device_get_sysattr_value(d
, "enabled", &enabled
) < 0 || !streq(enabled
, "enabled"))
628 /* We count any connector which is not explicitly
629 * "disconnected" as connected. */
630 if (sd_device_get_sysattr_value(d
, "status", &status
) < 0 || !streq(status
, "disconnected"))
637 bool manager_is_docked_or_external_displays(Manager
*m
) {
640 /* If we are docked don't react to lid closing */
641 if (manager_is_docked(m
)) {
642 log_debug("System is docked.");
646 /* If we have more than one display connected,
647 * assume that we are docked. */
648 n
= manager_count_external_displays(m
);
650 log_warning_errno(n
, "Display counting failed: %m");
652 log_debug("External (%i) displays connected.", n
);
659 bool manager_is_on_external_power(void) {
662 /* For now we only check for AC power, but 'external power' can apply to anything that isn't an internal
666 log_warning_errno(r
, "Failed to read AC power status: %m");
668 return r
!= 0; /* Treat failure as 'on AC' */
671 bool manager_all_buttons_ignored(Manager
*m
) {
674 if (m
->handle_power_key
!= HANDLE_IGNORE
)
676 if (m
->handle_power_key_long_press
!= HANDLE_IGNORE
)
678 if (m
->handle_suspend_key
!= HANDLE_IGNORE
)
680 if (m
->handle_suspend_key_long_press
!= HANDLE_IGNORE
)
682 if (m
->handle_hibernate_key
!= HANDLE_IGNORE
)
684 if (m
->handle_hibernate_key_long_press
!= HANDLE_IGNORE
)
686 if (m
->handle_reboot_key
!= HANDLE_IGNORE
)
688 if (m
->handle_reboot_key_long_press
!= HANDLE_IGNORE
)
690 if (m
->handle_lid_switch
!= HANDLE_IGNORE
)
692 if (!IN_SET(m
->handle_lid_switch_ep
, _HANDLE_ACTION_INVALID
, HANDLE_IGNORE
))
694 if (m
->handle_lid_switch_docked
!= HANDLE_IGNORE
)
700 int manager_read_utmp(Manager
*m
) {
703 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
707 if (utmpxname(_PATH_UTMPX
) < 0)
708 return log_error_errno(errno
, "Failed to set utmp path to " _PATH_UTMPX
": %m");
710 utmpx
= utxent_start();
713 _cleanup_free_
char *t
= NULL
;
722 log_debug_errno(errno
, _PATH_UTMPX
" does not exist, ignoring.");
724 log_warning_errno(errno
, "Failed to read " _PATH_UTMPX
", ignoring: %m");
728 if (u
->ut_type
!= USER_PROCESS
)
731 if (!pid_is_valid(u
->ut_pid
))
734 t
= strndup(u
->ut_line
, sizeof(u
->ut_line
));
738 c
= path_startswith(t
, "/dev/");
740 r
= free_and_strdup(&t
, c
);
748 if (manager_get_session_by_pidref(m
, &PIDREF_MAKE_FROM_PID(u
->ut_pid
), &s
) <= 0)
751 if (s
->tty_validity
== TTY_FROM_UTMP
&& !streq_ptr(s
->tty
, t
)) {
752 /* This may happen on multiplexed SSH connection (i.e. 'SSH connection sharing'). In
753 * this case PAM and utmp sessions don't match. In such a case let's invalidate the TTY
754 * information and never acquire it again. */
756 s
->tty
= mfree(s
->tty
);
757 s
->tty_validity
= TTY_UTMP_INCONSISTENT
;
758 log_debug("Session '%s' has inconsistent TTY information, dropping TTY information.", s
->id
);
762 /* Never override what we figured out once */
763 if (s
->tty
|| s
->tty_validity
>= 0)
766 s
->tty
= TAKE_PTR(t
);
767 s
->tty_validity
= TTY_FROM_UTMP
;
768 log_debug("Acquired TTY information '%s' from utmp for session '%s'.", s
->tty
, s
->id
);
777 static int manager_dispatch_utmp(sd_event_source
*s
, const struct inotify_event
*event
, void *userdata
) {
778 Manager
*m
= ASSERT_PTR(userdata
);
780 /* If there's indication the file itself might have been removed or became otherwise unavailable, then let's
781 * reestablish the watch on whatever there's now. */
782 if ((event
->mask
& (IN_ATTRIB
|IN_DELETE_SELF
|IN_MOVE_SELF
|IN_Q_OVERFLOW
|IN_UNMOUNT
)) != 0)
783 manager_connect_utmp(m
);
785 (void) manager_read_utmp(m
);
790 void manager_connect_utmp(Manager
*m
) {
792 sd_event_source
*s
= NULL
;
797 /* Watch utmp for changes via inotify. We do this to deal with tools such as ssh, which will register the PAM
798 * session early, and acquire a TTY only much later for the connection. Thus during PAM the TTY won't be known
799 * yet. ssh will register itself with utmp when it finally acquired the TTY. Hence, let's make use of this, and
800 * watch utmp for the TTY asynchronously. We use the PAM session's leader PID as key, to find the right entry.
802 * Yes, relying on utmp is pretty ugly, but it's good enough for informational purposes, as well as idle
803 * detection (which, for tty sessions, relies on the TTY used) */
805 r
= sd_event_add_inotify(m
->event
, &s
, _PATH_UTMPX
, IN_MODIFY
|IN_MOVE_SELF
|IN_DELETE_SELF
|IN_ATTRIB
, manager_dispatch_utmp
, m
);
807 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
, "Failed to create inotify watch on " _PATH_UTMPX
", ignoring: %m");
809 r
= sd_event_source_set_priority(s
, SD_EVENT_PRIORITY_IDLE
);
811 log_warning_errno(r
, "Failed to adjust utmp event source priority, ignoring: %m");
813 (void) sd_event_source_set_description(s
, "utmp");
816 sd_event_source_unref(m
->utmp_event_source
);
817 m
->utmp_event_source
= s
;
821 void manager_reconnect_utmp(Manager
*m
) {
825 if (m
->utmp_event_source
)
828 manager_connect_utmp(m
);
832 int manager_read_efi_boot_loader_entries(Manager
*m
) {
837 if (m
->efi_boot_loader_entries_set
)
840 r
= efi_loader_get_entries(&m
->efi_boot_loader_entries
);
842 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
)) {
843 log_debug_errno(r
, "Boot loader reported no entries.");
844 m
->efi_boot_loader_entries_set
= true;
847 return log_error_errno(r
, "Failed to determine entries reported by boot loader: %m");
850 m
->efi_boot_loader_entries_set
= true;