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_power_key
= HANDLE_POWEROFF
;
44 m
->handle_power_key_long_press
= HANDLE_IGNORE
;
45 m
->handle_reboot_key
= HANDLE_REBOOT
;
46 m
->handle_reboot_key_long_press
= HANDLE_POWEROFF
;
47 m
->handle_suspend_key
= HANDLE_SUSPEND
;
48 m
->handle_suspend_key_long_press
= HANDLE_HIBERNATE
;
49 m
->handle_hibernate_key
= HANDLE_HIBERNATE
;
50 m
->handle_hibernate_key_long_press
= HANDLE_IGNORE
;
52 m
->handle_lid_switch
= HANDLE_SUSPEND
;
53 m
->handle_lid_switch_ep
= _HANDLE_ACTION_INVALID
;
54 m
->handle_lid_switch_docked
= HANDLE_IGNORE
;
56 m
->power_key_ignore_inhibited
= false;
57 m
->suspend_key_ignore_inhibited
= false;
58 m
->hibernate_key_ignore_inhibited
= false;
59 m
->lid_switch_ignore_inhibited
= true;
60 m
->reboot_key_ignore_inhibited
= false;
62 m
->holdoff_timeout_usec
= 30 * USEC_PER_SEC
;
64 m
->idle_action_usec
= 30 * USEC_PER_MINUTE
;
65 m
->idle_action
= HANDLE_IGNORE
;
67 m
->runtime_dir_size
= physical_memory_scale(10U, 100U); /* 10% */
68 m
->runtime_dir_inodes
= DIV_ROUND_UP(m
->runtime_dir_size
, 4096); /* 4k per inode */
69 m
->sessions_max
= 8192;
70 m
->inhibitors_max
= 8192;
72 m
->kill_user_processes
= KILL_USER_PROCESSES
;
74 m
->kill_only_users
= strv_free(m
->kill_only_users
);
75 m
->kill_exclude_users
= strv_free(m
->kill_exclude_users
);
77 m
->stop_idle_session_usec
= USEC_INFINITY
;
80 int manager_parse_config_file(Manager
*m
) {
83 return config_parse_config_file("logind.conf", "Login\0",
84 config_item_perf_lookup
, logind_gperf_lookup
,
85 CONFIG_PARSE_WARN
, m
);
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 button_free(hashmap_get(m
->buttons
, sysname
));
334 r
= manager_add_button(m
, sysname
, &b
);
338 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
341 button_set_seat(b
, sn
);
344 if (r
< 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
345 * opening the device?) let's close the button again. */
352 int manager_get_session_by_pid(Manager
*m
, pid_t pid
, Session
**ret
) {
353 _cleanup_free_
char *unit
= NULL
;
359 if (!pid_is_valid(pid
))
362 s
= hashmap_get(m
->sessions_by_leader
, PID_TO_PTR(pid
));
364 r
= cg_pid_get_unit(pid
, &unit
);
366 s
= hashmap_get(m
->session_units
, unit
);
375 int manager_get_user_by_pid(Manager
*m
, pid_t pid
, User
**ret
) {
376 _cleanup_free_
char *unit
= NULL
;
382 if (!pid_is_valid(pid
))
385 r
= cg_pid_get_slice(pid
, &unit
);
387 u
= hashmap_get(m
->user_units
, unit
);
395 int manager_get_idle_hint(Manager
*m
, dual_timestamp
*t
) {
398 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
402 idle_hint
= !manager_is_inhibited(m
, INHIBIT_IDLE
, INHIBIT_BLOCK
, t
, false, false, 0, NULL
);
404 HASHMAP_FOREACH(s
, m
->sessions
) {
408 ih
= session_get_idle_hint(s
, &k
);
414 if (k
.monotonic
< ts
.monotonic
)
420 } else if (idle_hint
) {
422 if (k
.monotonic
> ts
.monotonic
)
433 bool manager_shall_kill(Manager
*m
, const char *user
) {
437 if (!m
->kill_exclude_users
&& streq(user
, "root"))
440 if (strv_contains(m
->kill_exclude_users
, user
))
443 if (!strv_isempty(m
->kill_only_users
))
444 return strv_contains(m
->kill_only_users
, user
);
446 return m
->kill_user_processes
;
449 int config_parse_n_autovts(
451 const char *filename
,
454 unsigned section_line
,
461 unsigned *n
= ASSERT_PTR(data
);
469 r
= safe_atou(rvalue
, &o
);
471 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
472 "Failed to parse number of autovts, ignoring: %s", rvalue
);
477 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
478 "A maximum of 15 autovts are supported, ignoring: %s", rvalue
);
486 static int vt_is_busy(unsigned vtnr
) {
487 struct vt_stat vt_stat
;
489 _cleanup_close_
int fd
= -EBADF
;
493 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
496 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
497 * we'd open the latter we'd open the foreground tty which
498 * hence would be unconditionally busy. By opening /dev/tty1
499 * we avoid this. Since tty1 is special and needs to be an
500 * explicitly loaded getty or DM this is safe. */
502 fd
= open_terminal("/dev/tty1", O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
506 if (ioctl(fd
, VT_GETSTATE
, &vt_stat
) < 0)
509 r
= !!(vt_stat
.v_state
& (1 << vtnr
));
514 int manager_spawn_autovt(Manager
*m
, unsigned vtnr
) {
515 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
516 char name
[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned)];
522 if (vtnr
> m
->n_autovts
&&
523 vtnr
!= m
->reserve_vt
)
526 if (vtnr
!= m
->reserve_vt
) {
527 /* If this is the reserved TTY, we'll start the getty
528 * on it in any case, but otherwise only if it is not
531 r
= vt_is_busy(vtnr
);
538 xsprintf(name
, "autovt@tty%u.service", vtnr
);
539 r
= bus_call_method(m
->bus
, bus_systemd_mgr
, "StartUnit", &error
, NULL
, "ss", name
, "fail");
541 return log_error_errno(r
, "Failed to start %s: %s", name
, bus_error_message(&error
, r
));
546 bool manager_is_lid_closed(Manager
*m
) {
549 HASHMAP_FOREACH(b
, m
->buttons
)
556 static bool manager_is_docked(Manager
*m
) {
559 HASHMAP_FOREACH(b
, m
->buttons
)
566 static int manager_count_external_displays(Manager
*m
) {
567 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
571 r
= sd_device_enumerator_new(&e
);
575 r
= sd_device_enumerator_allow_uninitialized(e
);
579 r
= sd_device_enumerator_add_match_subsystem(e
, "drm", true);
583 FOREACH_DEVICE(e
, d
) {
584 const char *status
, *enabled
, *dash
, *nn
, *subsys
;
587 if (sd_device_get_parent(d
, &p
) < 0)
590 /* If the parent shares the same subsystem as the
591 * device we are looking at then it is a connector,
592 * which is what we are interested in. */
593 if (sd_device_get_subsystem(p
, &subsys
) < 0 || !streq(subsys
, "drm"))
596 if (sd_device_get_sysname(d
, &nn
) < 0)
599 /* Ignore internal displays: the type is encoded in the sysfs name, as the second dash
600 * separated item (the first is the card name, the last the connector number). We implement a
601 * deny list of external displays here, rather than an allow list of internal ones, to ensure
602 * we don't block suspends too eagerly. */
603 dash
= strchr(nn
, '-');
608 if (!STARTSWITH_SET(dash
,
609 "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
610 "Composite-", "SVIDEO-", "Component-",
611 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-"))
614 /* Ignore ports that are not enabled */
615 if (sd_device_get_sysattr_value(d
, "enabled", &enabled
) < 0 || !streq(enabled
, "enabled"))
618 /* We count any connector which is not explicitly
619 * "disconnected" as connected. */
620 if (sd_device_get_sysattr_value(d
, "status", &status
) < 0 || !streq(status
, "disconnected"))
627 bool manager_is_docked_or_external_displays(Manager
*m
) {
630 /* If we are docked don't react to lid closing */
631 if (manager_is_docked(m
)) {
632 log_debug("System is docked.");
636 /* If we have more than one display connected,
637 * assume that we are docked. */
638 n
= manager_count_external_displays(m
);
640 log_warning_errno(n
, "Display counting failed: %m");
642 log_debug("External (%i) displays connected.", n
);
649 bool manager_is_on_external_power(void) {
652 /* For now we only check for AC power, but 'external power' can apply to anything that isn't an internal
656 log_warning_errno(r
, "Failed to read AC power status: %m");
658 return r
!= 0; /* Treat failure as 'on AC' */
661 bool manager_all_buttons_ignored(Manager
*m
) {
664 if (m
->handle_power_key
!= HANDLE_IGNORE
)
666 if (m
->handle_power_key_long_press
!= HANDLE_IGNORE
)
668 if (m
->handle_suspend_key
!= HANDLE_IGNORE
)
670 if (m
->handle_suspend_key_long_press
!= HANDLE_IGNORE
)
672 if (m
->handle_hibernate_key
!= HANDLE_IGNORE
)
674 if (m
->handle_hibernate_key_long_press
!= HANDLE_IGNORE
)
676 if (m
->handle_reboot_key
!= HANDLE_IGNORE
)
678 if (m
->handle_reboot_key_long_press
!= HANDLE_IGNORE
)
680 if (m
->handle_lid_switch
!= HANDLE_IGNORE
)
682 if (!IN_SET(m
->handle_lid_switch_ep
, _HANDLE_ACTION_INVALID
, HANDLE_IGNORE
))
684 if (m
->handle_lid_switch_docked
!= HANDLE_IGNORE
)
690 int manager_read_utmp(Manager
*m
) {
693 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
697 if (utmpxname(_PATH_UTMPX
) < 0)
698 return log_error_errno(errno
, "Failed to set utmp path to " _PATH_UTMPX
": %m");
700 utmpx
= utxent_start();
703 _cleanup_free_
char *t
= NULL
;
712 log_debug_errno(errno
, _PATH_UTMPX
" does not exist, ignoring.");
714 log_warning_errno(errno
, "Failed to read " _PATH_UTMPX
", ignoring: %m");
718 if (u
->ut_type
!= USER_PROCESS
)
721 if (!pid_is_valid(u
->ut_pid
))
724 t
= strndup(u
->ut_line
, sizeof(u
->ut_line
));
728 c
= path_startswith(t
, "/dev/");
730 r
= free_and_strdup(&t
, c
);
738 s
= hashmap_get(m
->sessions_by_leader
, PID_TO_PTR(u
->ut_pid
));
742 if (s
->tty_validity
== TTY_FROM_UTMP
&& !streq_ptr(s
->tty
, t
)) {
743 /* This may happen on multiplexed SSH connection (i.e. 'SSH connection sharing'). In
744 * this case PAM and utmp sessions don't match. In such a case let's invalidate the TTY
745 * information and never acquire it again. */
747 s
->tty
= mfree(s
->tty
);
748 s
->tty_validity
= TTY_UTMP_INCONSISTENT
;
749 log_debug("Session '%s' has inconsistent TTY information, dropping TTY information.", s
->id
);
753 /* Never override what we figured out once */
754 if (s
->tty
|| s
->tty_validity
>= 0)
757 s
->tty
= TAKE_PTR(t
);
758 s
->tty_validity
= TTY_FROM_UTMP
;
759 log_debug("Acquired TTY information '%s' from utmp for session '%s'.", s
->tty
, s
->id
);
768 static int manager_dispatch_utmp(sd_event_source
*s
, const struct inotify_event
*event
, void *userdata
) {
769 Manager
*m
= ASSERT_PTR(userdata
);
771 /* If there's indication the file itself might have been removed or became otherwise unavailable, then let's
772 * reestablish the watch on whatever there's now. */
773 if ((event
->mask
& (IN_ATTRIB
|IN_DELETE_SELF
|IN_MOVE_SELF
|IN_Q_OVERFLOW
|IN_UNMOUNT
)) != 0)
774 manager_connect_utmp(m
);
776 (void) manager_read_utmp(m
);
781 void manager_connect_utmp(Manager
*m
) {
783 sd_event_source
*s
= NULL
;
788 /* Watch utmp for changes via inotify. We do this to deal with tools such as ssh, which will register the PAM
789 * session early, and acquire a TTY only much later for the connection. Thus during PAM the TTY won't be known
790 * yet. ssh will register itself with utmp when it finally acquired the TTY. Hence, let's make use of this, and
791 * watch utmp for the TTY asynchronously. We use the PAM session's leader PID as key, to find the right entry.
793 * Yes, relying on utmp is pretty ugly, but it's good enough for informational purposes, as well as idle
794 * detection (which, for tty sessions, relies on the TTY used) */
796 r
= sd_event_add_inotify(m
->event
, &s
, _PATH_UTMPX
, IN_MODIFY
|IN_MOVE_SELF
|IN_DELETE_SELF
|IN_ATTRIB
, manager_dispatch_utmp
, m
);
798 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
, "Failed to create inotify watch on " _PATH_UTMPX
", ignoring: %m");
800 r
= sd_event_source_set_priority(s
, SD_EVENT_PRIORITY_IDLE
);
802 log_warning_errno(r
, "Failed to adjust utmp event source priority, ignoring: %m");
804 (void) sd_event_source_set_description(s
, "utmp");
807 sd_event_source_unref(m
->utmp_event_source
);
808 m
->utmp_event_source
= s
;
812 void manager_reconnect_utmp(Manager
*m
) {
816 if (m
->utmp_event_source
)
819 manager_connect_utmp(m
);
823 int manager_read_efi_boot_loader_entries(Manager
*m
) {
828 if (m
->efi_boot_loader_entries_set
)
831 r
= efi_loader_get_entries(&m
->efi_boot_loader_entries
);
832 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
)) {
833 log_debug_errno(r
, "Boot loader reported no entries.");
834 m
->efi_boot_loader_entries_set
= true;
838 return log_error_errno(r
, "Failed to determine entries reported by boot loader: %m");
840 m
->efi_boot_loader_entries_set
= true;