1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include "sd-device.h"
14 #include "alloc-util.h"
15 #include "bus-error.h"
17 #include "cgroup-util.h"
18 #include "conf-parser.h"
19 #include "device-util.h"
20 #include "errno-util.h"
22 #include "limits-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "process-util.h"
28 #include "terminal-util.h"
29 #include "udev-util.h"
30 #include "user-util.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_suspend_key
= HANDLE_SUSPEND
;
43 m
->handle_hibernate_key
= HANDLE_HIBERNATE
;
44 m
->handle_lid_switch
= HANDLE_SUSPEND
;
45 m
->handle_lid_switch_ep
= _HANDLE_ACTION_INVALID
;
46 m
->handle_lid_switch_docked
= HANDLE_IGNORE
;
47 m
->power_key_ignore_inhibited
= false;
48 m
->suspend_key_ignore_inhibited
= false;
49 m
->hibernate_key_ignore_inhibited
= false;
50 m
->lid_switch_ignore_inhibited
= true;
52 m
->holdoff_timeout_usec
= 30 * USEC_PER_SEC
;
54 m
->idle_action_usec
= 30 * USEC_PER_MINUTE
;
55 m
->idle_action
= HANDLE_IGNORE
;
57 m
->runtime_dir_size
= physical_memory_scale(10U, 100U); /* 10% */
58 m
->user_tasks_max
= system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE
, 100U); /* 33% */
59 m
->sessions_max
= 8192;
60 m
->inhibitors_max
= 8192;
62 m
->kill_user_processes
= KILL_USER_PROCESSES
;
64 m
->kill_only_users
= strv_free(m
->kill_only_users
);
65 m
->kill_exclude_users
= strv_free(m
->kill_exclude_users
);
68 int manager_parse_config_file(Manager
*m
) {
71 return config_parse_many_nulstr(PKGSYSCONFDIR
"/logind.conf",
72 CONF_PATHS_NULSTR("systemd/logind.conf.d"),
74 config_item_perf_lookup
, logind_gperf_lookup
,
75 CONFIG_PARSE_WARN
, m
);
78 int manager_add_device(Manager
*m
, const char *sysfs
, bool master
, Device
**ret_device
) {
84 d
= hashmap_get(m
->devices
, sysfs
);
86 /* we support adding master-flags, but not removing them */
87 d
->master
= d
->master
|| master
;
89 d
= device_new(m
, sysfs
, master
);
100 int manager_add_seat(Manager
*m
, const char *id
, Seat
**ret_seat
) {
107 s
= hashmap_get(m
->seats
, id
);
109 r
= seat_new(&s
, m
, id
);
120 int manager_add_session(Manager
*m
, const char *id
, Session
**ret_session
) {
127 s
= hashmap_get(m
->sessions
, id
);
129 r
= session_new(&s
, m
, id
);
140 int manager_add_user(
154 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
156 r
= user_new(&u
, m
, uid
, gid
, name
, home
);
167 int manager_add_user_by_name(
172 const char *home
= NULL
;
180 r
= get_user_creds(&name
, &uid
, &gid
, &home
, NULL
, 0);
184 return manager_add_user(m
, uid
, gid
, name
, home
, ret_user
);
187 int manager_add_user_by_uid(Manager
*m
, uid_t uid
, User
**ret_user
) {
195 return errno_or_else(ENOENT
);
197 return manager_add_user(m
, uid
, p
->pw_gid
, p
->pw_name
, p
->pw_dir
, ret_user
);
200 int manager_add_inhibitor(Manager
*m
, const char* id
, Inhibitor
**ret
) {
207 i
= hashmap_get(m
->inhibitors
, id
);
209 r
= inhibitor_new(&i
, m
, id
);
220 int manager_add_button(Manager
*m
, const char *name
, Button
**ret_button
) {
226 b
= hashmap_get(m
->buttons
, name
);
228 b
= button_new(m
, name
);
239 int manager_process_seat_device(Manager
*m
, sd_device
*d
) {
245 if (device_for_action(d
, DEVICE_ACTION_REMOVE
)) {
248 r
= sd_device_get_syspath(d
, &syspath
);
252 device
= hashmap_get(m
->devices
, syspath
);
256 seat_add_to_gc_queue(device
->seat
);
260 const char *sn
, *syspath
;
264 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
267 if (!seat_name_is_valid(sn
)) {
268 log_device_warning(d
, "Device with invalid seat name %s found, ignoring.", sn
);
272 seat
= hashmap_get(m
->seats
, sn
);
273 master
= sd_device_has_tag(d
, "master-of-seat") > 0;
275 /* Ignore non-master devices for unknown seats */
276 if (!master
&& !seat
)
279 r
= sd_device_get_syspath(d
, &syspath
);
283 r
= manager_add_device(m
, syspath
, master
, &device
);
288 r
= manager_add_seat(m
, sn
, &seat
);
297 device_attach(device
, seat
);
304 int manager_process_button_device(Manager
*m
, sd_device
*d
) {
311 r
= sd_device_get_sysname(d
, &sysname
);
315 if (device_for_action(d
, DEVICE_ACTION_REMOVE
)) {
317 b
= hashmap_get(m
->buttons
, sysname
);
326 r
= manager_add_button(m
, sysname
, &b
);
330 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
333 button_set_seat(b
, sn
);
336 if (r
< 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
337 * opening the device?) let's close the button again. */
344 int manager_get_session_by_pid(Manager
*m
, pid_t pid
, Session
**ret
) {
345 _cleanup_free_
char *unit
= NULL
;
351 if (!pid_is_valid(pid
))
354 s
= hashmap_get(m
->sessions_by_leader
, PID_TO_PTR(pid
));
356 r
= cg_pid_get_unit(pid
, &unit
);
358 s
= hashmap_get(m
->session_units
, unit
);
367 int manager_get_user_by_pid(Manager
*m
, pid_t pid
, User
**ret
) {
368 _cleanup_free_
char *unit
= NULL
;
374 if (!pid_is_valid(pid
))
377 r
= cg_pid_get_slice(pid
, &unit
);
379 u
= hashmap_get(m
->user_units
, unit
);
387 int manager_get_idle_hint(Manager
*m
, dual_timestamp
*t
) {
390 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
395 idle_hint
= !manager_is_inhibited(m
, INHIBIT_IDLE
, INHIBIT_BLOCK
, t
, false, false, 0, NULL
);
397 HASHMAP_FOREACH(s
, m
->sessions
, i
) {
401 ih
= session_get_idle_hint(s
, &k
);
407 if (k
.monotonic
< ts
.monotonic
)
413 } else if (idle_hint
) {
415 if (k
.monotonic
> ts
.monotonic
)
426 bool manager_shall_kill(Manager
*m
, const char *user
) {
430 if (!m
->kill_exclude_users
&& streq(user
, "root"))
433 if (strv_contains(m
->kill_exclude_users
, user
))
436 if (!strv_isempty(m
->kill_only_users
))
437 return strv_contains(m
->kill_only_users
, user
);
439 return m
->kill_user_processes
;
442 int config_parse_n_autovts(
444 const char *filename
,
447 unsigned section_line
,
463 r
= safe_atou(rvalue
, &o
);
465 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse number of autovts, ignoring: %s", rvalue
);
470 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "A maximum of 15 autovts are supported, ignoring: %s", rvalue
);
478 static int vt_is_busy(unsigned vtnr
) {
479 struct vt_stat vt_stat
;
481 _cleanup_close_
int fd
;
485 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
488 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
489 * we'd open the latter we'd open the foreground tty which
490 * hence would be unconditionally busy. By opening /dev/tty1
491 * we avoid this. Since tty1 is special and needs to be an
492 * explicitly loaded getty or DM this is safe. */
494 fd
= open_terminal("/dev/tty1", O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
498 if (ioctl(fd
, VT_GETSTATE
, &vt_stat
) < 0)
501 r
= !!(vt_stat
.v_state
& (1 << vtnr
));
506 int manager_spawn_autovt(Manager
*m
, unsigned vtnr
) {
507 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
508 char name
[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned)];
514 if (vtnr
> m
->n_autovts
&&
515 vtnr
!= m
->reserve_vt
)
518 if (vtnr
!= m
->reserve_vt
) {
519 /* If this is the reserved TTY, we'll start the getty
520 * on it in any case, but otherwise only if it is not
523 r
= vt_is_busy(vtnr
);
530 snprintf(name
, sizeof(name
), "autovt@tty%u.service", vtnr
);
531 r
= sd_bus_call_method(
533 "org.freedesktop.systemd1",
534 "/org/freedesktop/systemd1",
535 "org.freedesktop.systemd1.Manager",
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
) {
550 HASHMAP_FOREACH(b
, m
->buttons
, i
)
557 static bool manager_is_docked(Manager
*m
) {
561 HASHMAP_FOREACH(b
, m
->buttons
, i
)
568 static int manager_count_external_displays(Manager
*m
) {
569 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
573 r
= sd_device_enumerator_new(&e
);
577 r
= sd_device_enumerator_allow_uninitialized(e
);
581 r
= sd_device_enumerator_add_match_subsystem(e
, "drm", true);
585 FOREACH_DEVICE(e
, d
) {
586 const char *status
, *enabled
, *dash
, *nn
, *subsys
;
589 if (sd_device_get_parent(d
, &p
) < 0)
592 /* If the parent shares the same subsystem as the
593 * device we are looking at then it is a connector,
594 * which is what we are interested in. */
595 if (sd_device_get_subsystem(p
, &subsys
) < 0 || !streq(subsys
, "drm"))
598 if (sd_device_get_sysname(d
, &nn
) < 0)
601 /* Ignore internal displays: the type is encoded in the sysfs name, as the second dash separated item
602 * (the first is the card name, the last the connector number). We implement a blacklist of external
603 * displays here, rather than a whitelist of internal ones, to ensure we don't block suspends too
605 dash
= strchr(nn
, '-');
610 if (!STARTSWITH_SET(dash
,
611 "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
612 "Composite-", "SVIDEO-", "Component-",
613 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-"))
616 /* Ignore ports that are not enabled */
617 if (sd_device_get_sysattr_value(d
, "enabled", &enabled
) < 0 || !streq(enabled
, "enabled"))
620 /* We count any connector which is not explicitly
621 * "disconnected" as connected. */
622 if (sd_device_get_sysattr_value(d
, "status", &status
) < 0 || !streq(status
, "disconnected"))
629 bool manager_is_docked_or_external_displays(Manager
*m
) {
632 /* If we are docked don't react to lid closing */
633 if (manager_is_docked(m
)) {
634 log_debug("System is docked.");
638 /* If we have more than one display connected,
639 * assume that we are docked. */
640 n
= manager_count_external_displays(m
);
642 log_warning_errno(n
, "Display counting failed: %m");
644 log_debug("External (%i) displays connected.", n
);
651 bool manager_is_on_external_power(void) {
654 /* For now we only check for AC power, but 'external power' can apply to anything that isn't an internal
658 log_warning_errno(r
, "Failed to read AC power status: %m");
660 return r
!= 0; /* Treat failure as 'on AC' */
663 bool manager_all_buttons_ignored(Manager
*m
) {
666 if (m
->handle_power_key
!= HANDLE_IGNORE
)
668 if (m
->handle_suspend_key
!= HANDLE_IGNORE
)
670 if (m
->handle_hibernate_key
!= HANDLE_IGNORE
)
672 if (m
->handle_lid_switch
!= HANDLE_IGNORE
)
674 if (!IN_SET(m
->handle_lid_switch_ep
, _HANDLE_ACTION_INVALID
, HANDLE_IGNORE
))
676 if (m
->handle_lid_switch_docked
!= HANDLE_IGNORE
)
682 int manager_read_utmp(Manager
*m
) {
688 if (utmpxname(_PATH_UTMPX
) < 0)
689 return log_error_errno(errno
, "Failed to set utmp path to " _PATH_UTMPX
": %m");
694 _cleanup_free_
char *t
= NULL
;
703 log_warning_errno(errno
, "Failed to read " _PATH_UTMPX
", ignoring: %m");
708 if (u
->ut_type
!= USER_PROCESS
)
711 if (!pid_is_valid(u
->ut_pid
))
714 t
= strndup(u
->ut_line
, sizeof(u
->ut_line
));
720 c
= path_startswith(t
, "/dev/");
722 r
= free_and_strdup(&t
, c
);
732 s
= hashmap_get(m
->sessions_by_leader
, PID_TO_PTR(u
->ut_pid
));
736 if (s
->tty_validity
== TTY_FROM_UTMP
&& !streq_ptr(s
->tty
, t
)) {
737 /* This may happen on multiplexed SSH connection (i.e. 'SSH connection sharing'). In
738 * this case PAM and utmp sessions don't match. In such a case let's invalidate the TTY
739 * information and never acquire it again. */
741 s
->tty
= mfree(s
->tty
);
742 s
->tty_validity
= TTY_UTMP_INCONSISTENT
;
743 log_debug("Session '%s' has inconsistent TTY information, dropping TTY information.", s
->id
);
747 /* Never override what we figured out once */
748 if (s
->tty
|| s
->tty_validity
>= 0)
751 s
->tty
= TAKE_PTR(t
);
752 s
->tty_validity
= TTY_FROM_UTMP
;
753 log_debug("Acquired TTY information '%s' from utmp for session '%s'.", s
->tty
, s
->id
);
764 static int manager_dispatch_utmp(sd_event_source
*s
, const struct inotify_event
*event
, void *userdata
) {
765 Manager
*m
= userdata
;
769 /* If there's indication the file itself might have been removed or became otherwise unavailable, then let's
770 * reestablish the watch on whatever there's now. */
771 if ((event
->mask
& (IN_ATTRIB
|IN_DELETE_SELF
|IN_MOVE_SELF
|IN_Q_OVERFLOW
|IN_UNMOUNT
)) != 0)
772 manager_connect_utmp(m
);
774 (void) manager_read_utmp(m
);
779 void manager_connect_utmp(Manager
*m
) {
781 sd_event_source
*s
= NULL
;
786 /* Watch utmp for changes via inotify. We do this to deal with tools such as ssh, which will register the PAM
787 * session early, and acquire a TTY only much later for the connection. Thus during PAM the TTY won't be known
788 * yet. ssh will register itself with utmp when it finally acquired the TTY. Hence, let's make use of this, and
789 * watch utmp for the TTY asynchronously. We use the PAM session's leader PID as key, to find the right entry.
791 * Yes, relying on utmp is pretty ugly, but it's good enough for informational purposes, as well as idle
792 * detection (which, for tty sessions, relies on the TTY used) */
794 r
= sd_event_add_inotify(m
->event
, &s
, _PATH_UTMPX
, IN_MODIFY
|IN_MOVE_SELF
|IN_DELETE_SELF
|IN_ATTRIB
, manager_dispatch_utmp
, m
);
796 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
, "Failed to create inotify watch on " _PATH_UTMPX
", ignoring: %m");
798 r
= sd_event_source_set_priority(s
, SD_EVENT_PRIORITY_IDLE
);
800 log_warning_errno(r
, "Failed to adjust utmp event source priority, ignoring: %m");
802 (void) sd_event_source_set_description(s
, "utmp");
805 sd_event_source_unref(m
->utmp_event_source
);
806 m
->utmp_event_source
= s
;
810 void manager_reconnect_utmp(Manager
*m
) {
814 if (m
->utmp_event_source
)
817 manager_connect_utmp(m
);