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"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "process-util.h"
26 #include "terminal-util.h"
27 #include "user-util.h"
29 void manager_reset_config(Manager
*m
) {
35 m
->inhibit_delay_max
= 5 * USEC_PER_SEC
;
36 m
->user_stop_delay
= 10 * USEC_PER_SEC
;
38 m
->handle_power_key
= HANDLE_POWEROFF
;
39 m
->handle_suspend_key
= HANDLE_SUSPEND
;
40 m
->handle_hibernate_key
= HANDLE_HIBERNATE
;
41 m
->handle_lid_switch
= HANDLE_SUSPEND
;
42 m
->handle_lid_switch_ep
= _HANDLE_ACTION_INVALID
;
43 m
->handle_lid_switch_docked
= HANDLE_IGNORE
;
44 m
->power_key_ignore_inhibited
= false;
45 m
->suspend_key_ignore_inhibited
= false;
46 m
->hibernate_key_ignore_inhibited
= false;
47 m
->lid_switch_ignore_inhibited
= true;
49 m
->holdoff_timeout_usec
= 30 * USEC_PER_SEC
;
51 m
->idle_action_usec
= 30 * USEC_PER_MINUTE
;
52 m
->idle_action
= HANDLE_IGNORE
;
54 m
->runtime_dir_size
= physical_memory_scale(10U, 100U); /* 10% */
55 m
->user_tasks_max
= system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE
, 100U); /* 33% */
56 m
->sessions_max
= 8192;
57 m
->inhibitors_max
= 8192;
59 m
->kill_user_processes
= KILL_USER_PROCESSES
;
61 m
->kill_only_users
= strv_free(m
->kill_only_users
);
62 m
->kill_exclude_users
= strv_free(m
->kill_exclude_users
);
65 int manager_parse_config_file(Manager
*m
) {
68 return config_parse_many_nulstr(PKGSYSCONFDIR
"/logind.conf",
69 CONF_PATHS_NULSTR("systemd/logind.conf.d"),
71 config_item_perf_lookup
, logind_gperf_lookup
,
72 CONFIG_PARSE_WARN
, m
);
75 int manager_add_device(Manager
*m
, const char *sysfs
, bool master
, Device
**_device
) {
81 d
= hashmap_get(m
->devices
, sysfs
);
83 /* we support adding master-flags, but not removing them */
84 d
->master
= d
->master
|| master
;
86 d
= device_new(m
, sysfs
, master
);
97 int manager_add_seat(Manager
*m
, const char *id
, Seat
**_seat
) {
104 s
= hashmap_get(m
->seats
, id
);
106 r
= seat_new(&s
, m
, id
);
117 int manager_add_session(Manager
*m
, const char *id
, Session
**_session
) {
124 s
= hashmap_get(m
->sessions
, id
);
126 r
= session_new(&s
, m
, id
);
137 int manager_add_user(
151 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
153 r
= user_new(&u
, m
, uid
, gid
, name
, home
);
164 int manager_add_user_by_name(
169 const char *home
= NULL
;
177 r
= get_user_creds(&name
, &uid
, &gid
, &home
, NULL
, 0);
181 return manager_add_user(m
, uid
, gid
, name
, home
, _user
);
184 int manager_add_user_by_uid(Manager
*m
, uid_t uid
, User
**_user
) {
192 return errno
> 0 ? -errno
: -ENOENT
;
194 return manager_add_user(m
, uid
, p
->pw_gid
, p
->pw_name
, p
->pw_dir
, _user
);
197 int manager_add_inhibitor(Manager
*m
, const char* id
, Inhibitor
**_inhibitor
) {
203 i
= hashmap_get(m
->inhibitors
, id
);
211 i
= inhibitor_new(m
, id
);
221 int manager_add_button(Manager
*m
, const char *name
, Button
**_button
) {
227 b
= hashmap_get(m
->buttons
, name
);
229 b
= button_new(m
, name
);
240 int manager_process_seat_device(Manager
*m
, sd_device
*d
) {
247 if (sd_device_get_property_value(d
, "ACTION", &action
) >= 0 &&
248 streq(action
, "remove")) {
251 r
= sd_device_get_syspath(d
, &syspath
);
255 device
= hashmap_get(m
->devices
, syspath
);
259 seat_add_to_gc_queue(device
->seat
);
263 const char *sn
, *syspath
;
267 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
270 if (!seat_name_is_valid(sn
)) {
271 log_device_warning(d
, "Device with invalid seat name %s found, ignoring.", sn
);
275 seat
= hashmap_get(m
->seats
, sn
);
276 master
= sd_device_has_tag(d
, "master-of-seat") > 0;
278 /* Ignore non-master devices for unknown seats */
279 if (!master
&& !seat
)
282 r
= sd_device_get_syspath(d
, &syspath
);
286 r
= manager_add_device(m
, syspath
, master
, &device
);
291 r
= manager_add_seat(m
, sn
, &seat
);
300 device_attach(device
, seat
);
307 int manager_process_button_device(Manager
*m
, sd_device
*d
) {
308 const char *action
, *sysname
;
314 r
= sd_device_get_sysname(d
, &sysname
);
318 if (sd_device_get_property_value(d
, "ACTION", &action
) >= 0 &&
319 streq(action
, "remove")) {
321 b
= hashmap_get(m
->buttons
, sysname
);
330 r
= manager_add_button(m
, sysname
, &b
);
334 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
337 button_set_seat(b
, sn
);
340 if (r
< 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
341 * opening the device?) let's close the button again. */
348 int manager_get_session_by_pid(Manager
*m
, pid_t pid
, Session
**ret
) {
349 _cleanup_free_
char *unit
= NULL
;
355 if (!pid_is_valid(pid
))
358 s
= hashmap_get(m
->sessions_by_leader
, PID_TO_PTR(pid
));
360 r
= cg_pid_get_unit(pid
, &unit
);
364 s
= hashmap_get(m
->session_units
, unit
);
380 int manager_get_user_by_pid(Manager
*m
, pid_t pid
, User
**ret
) {
381 _cleanup_free_
char *unit
= NULL
;
387 if (!pid_is_valid(pid
))
390 r
= cg_pid_get_slice(pid
, &unit
);
394 u
= hashmap_get(m
->user_units
, unit
);
410 int manager_get_idle_hint(Manager
*m
, dual_timestamp
*t
) {
413 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
418 idle_hint
= !manager_is_inhibited(m
, INHIBIT_IDLE
, INHIBIT_BLOCK
, t
, false, false, 0, NULL
);
420 HASHMAP_FOREACH(s
, m
->sessions
, i
) {
424 ih
= session_get_idle_hint(s
, &k
);
430 if (k
.monotonic
< ts
.monotonic
)
436 } else if (idle_hint
) {
438 if (k
.monotonic
> ts
.monotonic
)
449 bool manager_shall_kill(Manager
*m
, const char *user
) {
453 if (!m
->kill_exclude_users
&& streq(user
, "root"))
456 if (strv_contains(m
->kill_exclude_users
, user
))
459 if (!strv_isempty(m
->kill_only_users
))
460 return strv_contains(m
->kill_only_users
, user
);
462 return m
->kill_user_processes
;
465 int config_parse_n_autovts(
467 const char *filename
,
470 unsigned section_line
,
486 r
= safe_atou(rvalue
, &o
);
488 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse number of autovts, ignoring: %s", rvalue
);
493 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "A maximum of 15 autovts are supported, ignoring: %s", rvalue
);
501 static int vt_is_busy(unsigned vtnr
) {
502 struct vt_stat vt_stat
;
504 _cleanup_close_
int fd
;
508 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
511 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
512 * we'd open the latter we'd open the foreground tty which
513 * hence would be unconditionally busy. By opening /dev/tty1
514 * we avoid this. Since tty1 is special and needs to be an
515 * explicitly loaded getty or DM this is safe. */
517 fd
= open_terminal("/dev/tty1", O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
521 if (ioctl(fd
, VT_GETSTATE
, &vt_stat
) < 0)
524 r
= !!(vt_stat
.v_state
& (1 << vtnr
));
529 int manager_spawn_autovt(Manager
*m
, unsigned vtnr
) {
530 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
531 char name
[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned)];
537 if (vtnr
> m
->n_autovts
&&
538 vtnr
!= m
->reserve_vt
)
541 if (vtnr
!= m
->reserve_vt
) {
542 /* If this is the reserved TTY, we'll start the getty
543 * on it in any case, but otherwise only if it is not
546 r
= vt_is_busy(vtnr
);
553 snprintf(name
, sizeof(name
), "autovt@tty%u.service", vtnr
);
554 r
= sd_bus_call_method(
556 "org.freedesktop.systemd1",
557 "/org/freedesktop/systemd1",
558 "org.freedesktop.systemd1.Manager",
564 return log_error_errno(r
, "Failed to start %s: %s", name
, bus_error_message(&error
, r
));
569 bool manager_is_lid_closed(Manager
*m
) {
573 HASHMAP_FOREACH(b
, m
->buttons
, i
)
580 static bool manager_is_docked(Manager
*m
) {
584 HASHMAP_FOREACH(b
, m
->buttons
, i
)
591 static int manager_count_external_displays(Manager
*m
) {
592 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
596 r
= sd_device_enumerator_new(&e
);
600 r
= sd_device_enumerator_allow_uninitialized(e
);
604 r
= sd_device_enumerator_add_match_subsystem(e
, "drm", true);
608 FOREACH_DEVICE(e
, d
) {
610 const char *status
, *enabled
, *dash
, *nn
, *i
, *subsys
;
611 bool external
= false;
613 if (sd_device_get_parent(d
, &p
) < 0)
616 /* If the parent shares the same subsystem as the
617 * device we are looking at then it is a connector,
618 * which is what we are interested in. */
619 if (sd_device_get_subsystem(p
, &subsys
) < 0 || !streq(subsys
, "drm"))
622 if (sd_device_get_sysname(d
, &nn
) < 0)
625 /* Ignore internal displays: the type is encoded in the sysfs name, as the second dash separated item
626 * (the first is the card name, the last the connector number). We implement a blacklist of external
627 * displays here, rather than a whitelist of internal ones, to ensure we don't block suspends too
629 dash
= strchr(nn
, '-');
634 FOREACH_STRING(i
, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
635 "Composite-", "SVIDEO-", "Component-",
636 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
638 if (startswith(dash
, i
)) {
646 /* Ignore ports that are not enabled */
647 if (sd_device_get_sysattr_value(d
, "enabled", &enabled
) < 0 || !streq(enabled
, "enabled"))
650 /* We count any connector which is not explicitly
651 * "disconnected" as connected. */
652 if (sd_device_get_sysattr_value(d
, "status", &status
) < 0 || !streq(status
, "disconnected"))
659 bool manager_is_docked_or_external_displays(Manager
*m
) {
662 /* If we are docked don't react to lid closing */
663 if (manager_is_docked(m
)) {
664 log_debug("System is docked.");
668 /* If we have more than one display connected,
669 * assume that we are docked. */
670 n
= manager_count_external_displays(m
);
672 log_warning_errno(n
, "Display counting failed: %m");
674 log_debug("External (%i) displays connected.", n
);
681 bool manager_is_on_external_power(void) {
684 /* For now we only check for AC power, but 'external power' can apply to anything that isn't an internal
688 log_warning_errno(r
, "Failed to read AC power status: %m");
690 return r
!= 0; /* Treat failure as 'on AC' */
693 bool manager_all_buttons_ignored(Manager
*m
) {
696 if (m
->handle_power_key
!= HANDLE_IGNORE
)
698 if (m
->handle_suspend_key
!= HANDLE_IGNORE
)
700 if (m
->handle_hibernate_key
!= HANDLE_IGNORE
)
702 if (m
->handle_lid_switch
!= HANDLE_IGNORE
)
704 if (m
->handle_lid_switch_ep
!= _HANDLE_ACTION_INVALID
&&
705 m
->handle_lid_switch_ep
!= HANDLE_IGNORE
)
707 if (m
->handle_lid_switch_docked
!= HANDLE_IGNORE
)
713 int manager_read_utmp(Manager
*m
) {
719 if (utmpxname(_PATH_UTMPX
) < 0)
720 return log_error_errno(errno
, "Failed to set utmp path to " _PATH_UTMPX
": %m");
725 _cleanup_free_
char *t
= NULL
;
734 log_warning_errno(errno
, "Failed to read " _PATH_UTMPX
", ignoring: %m");
739 if (u
->ut_type
!= USER_PROCESS
)
742 if (!pid_is_valid(u
->ut_pid
))
745 t
= strndup(u
->ut_line
, sizeof(u
->ut_line
));
751 c
= path_startswith(t
, "/dev/");
753 r
= free_and_strdup(&t
, c
);
763 s
= hashmap_get(m
->sessions_by_leader
, PID_TO_PTR(u
->ut_pid
));
767 if (s
->tty_validity
== TTY_FROM_UTMP
&& !streq_ptr(s
->tty
, t
)) {
768 /* This may happen on multiplexed SSH connection (i.e. 'SSH connection sharing'). In
769 * this case PAM and utmp sessions don't match. In such a case let's invalidate the TTY
770 * information and never acquire it again. */
772 s
->tty
= mfree(s
->tty
);
773 s
->tty_validity
= TTY_UTMP_INCONSISTENT
;
774 log_debug("Session '%s' has inconsistent TTY information, dropping TTY information.", s
->id
);
778 /* Never override what we figured out once */
779 if (s
->tty
|| s
->tty_validity
>= 0)
782 s
->tty
= TAKE_PTR(t
);
783 s
->tty_validity
= TTY_FROM_UTMP
;
784 log_debug("Acquired TTY information '%s' from utmp for session '%s'.", s
->tty
, s
->id
);
795 static int manager_dispatch_utmp(sd_event_source
*s
, const struct inotify_event
*event
, void *userdata
) {
796 Manager
*m
= userdata
;
800 /* If there's indication the file itself might have been removed or became otherwise unavailable, then let's
801 * reestablish the watch on whatever there's now. */
802 if ((event
->mask
& (IN_ATTRIB
|IN_DELETE_SELF
|IN_MOVE_SELF
|IN_Q_OVERFLOW
|IN_UNMOUNT
)) != 0)
803 manager_connect_utmp(m
);
805 (void) manager_read_utmp(m
);
810 void manager_connect_utmp(Manager
*m
) {
812 sd_event_source
*s
= NULL
;
817 /* Watch utmp for changes via inotify. We do this to deal with tools such as ssh, which will register the PAM
818 * session early, and acquire a TTY only much later for the connection. Thus during PAM the TTY won't be known
819 * yet. ssh will register itself with utmp when it finally acquired the TTY. Hence, let's make use of this, and
820 * watch utmp for the TTY asynchronously. We use the PAM session's leader PID as key, to find the right entry.
822 * Yes, relying on utmp is pretty ugly, but it's good enough for informational purposes, as well as idle
823 * detection (which, for tty sessions, relies on the TTY used) */
825 r
= sd_event_add_inotify(m
->event
, &s
, _PATH_UTMPX
, IN_MODIFY
|IN_MOVE_SELF
|IN_DELETE_SELF
|IN_ATTRIB
, manager_dispatch_utmp
, m
);
827 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
, "Failed to create inotify watch on " _PATH_UTMPX
", ignoring: %m");
829 r
= sd_event_source_set_priority(s
, SD_EVENT_PRIORITY_IDLE
);
831 log_warning_errno(r
, "Failed to adjust utmp event source priority, ignoring: %m");
833 (void) sd_event_source_set_description(s
, "utmp");
836 sd_event_source_unref(m
->utmp_event_source
);
837 m
->utmp_event_source
= s
;
841 void manager_reconnect_utmp(Manager
*m
) {
845 if (m
->utmp_event_source
)
848 manager_connect_utmp(m
);