1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "bus-error.h"
14 #include "cgroup-util.h"
15 #include "conf-parser.h"
16 #include "device-util.h"
19 #include "parse-util.h"
20 #include "process-util.h"
22 #include "terminal-util.h"
23 #include "user-util.h"
25 void manager_reset_config(Manager
*m
) {
31 m
->inhibit_delay_max
= 5 * USEC_PER_SEC
;
32 m
->handle_power_key
= HANDLE_POWEROFF
;
33 m
->handle_suspend_key
= HANDLE_SUSPEND
;
34 m
->handle_hibernate_key
= HANDLE_HIBERNATE
;
35 m
->handle_lid_switch
= HANDLE_SUSPEND
;
36 m
->handle_lid_switch_ep
= _HANDLE_ACTION_INVALID
;
37 m
->handle_lid_switch_docked
= HANDLE_IGNORE
;
38 m
->power_key_ignore_inhibited
= false;
39 m
->suspend_key_ignore_inhibited
= false;
40 m
->hibernate_key_ignore_inhibited
= false;
41 m
->lid_switch_ignore_inhibited
= true;
43 m
->holdoff_timeout_usec
= 30 * USEC_PER_SEC
;
45 m
->idle_action_usec
= 30 * USEC_PER_MINUTE
;
46 m
->idle_action
= HANDLE_IGNORE
;
48 m
->runtime_dir_size
= physical_memory_scale(10U, 100U); /* 10% */
49 m
->user_tasks_max
= system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE
, 100U); /* 33% */
50 m
->sessions_max
= 8192;
51 m
->inhibitors_max
= 8192;
53 m
->kill_user_processes
= KILL_USER_PROCESSES
;
55 m
->kill_only_users
= strv_free(m
->kill_only_users
);
56 m
->kill_exclude_users
= strv_free(m
->kill_exclude_users
);
59 int manager_parse_config_file(Manager
*m
) {
62 return config_parse_many_nulstr(PKGSYSCONFDIR
"/logind.conf",
63 CONF_PATHS_NULSTR("systemd/logind.conf.d"),
65 config_item_perf_lookup
, logind_gperf_lookup
,
66 CONFIG_PARSE_WARN
, m
);
69 int manager_add_device(Manager
*m
, const char *sysfs
, bool master
, Device
**_device
) {
75 d
= hashmap_get(m
->devices
, sysfs
);
77 /* we support adding master-flags, but not removing them */
78 d
->master
= d
->master
|| master
;
80 d
= device_new(m
, sysfs
, master
);
91 int manager_add_seat(Manager
*m
, const char *id
, Seat
**_seat
) {
97 s
= hashmap_get(m
->seats
, id
);
110 int manager_add_session(Manager
*m
, const char *id
, Session
**_session
) {
116 s
= hashmap_get(m
->sessions
, id
);
118 s
= session_new(m
, id
);
129 int manager_add_user(Manager
*m
, uid_t uid
, gid_t gid
, const char *name
, User
**_user
) {
136 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
138 r
= user_new(&u
, m
, uid
, gid
, name
);
149 int manager_add_user_by_name(Manager
*m
, const char *name
, User
**_user
) {
157 r
= get_user_creds(&name
, &uid
, &gid
, NULL
, NULL
, 0);
161 return manager_add_user(m
, uid
, gid
, name
, _user
);
164 int manager_add_user_by_uid(Manager
*m
, uid_t uid
, User
**_user
) {
172 return errno
> 0 ? -errno
: -ENOENT
;
174 return manager_add_user(m
, uid
, p
->pw_gid
, p
->pw_name
, _user
);
177 int manager_add_inhibitor(Manager
*m
, const char* id
, Inhibitor
**_inhibitor
) {
183 i
= hashmap_get(m
->inhibitors
, id
);
191 i
= inhibitor_new(m
, id
);
201 int manager_add_button(Manager
*m
, const char *name
, Button
**_button
) {
207 b
= hashmap_get(m
->buttons
, name
);
209 b
= button_new(m
, name
);
220 int manager_process_seat_device(Manager
*m
, sd_device
*d
) {
227 if (sd_device_get_property_value(d
, "ACTION", &action
) >= 0 &&
228 streq(action
, "remove")) {
231 r
= sd_device_get_syspath(d
, &syspath
);
235 device
= hashmap_get(m
->devices
, syspath
);
239 seat_add_to_gc_queue(device
->seat
);
243 const char *sn
, *syspath
;
247 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
250 if (!seat_name_is_valid(sn
)) {
251 log_warning("Device with invalid seat name %s found, ignoring.", sn
);
255 seat
= hashmap_get(m
->seats
, sn
);
256 master
= sd_device_has_tag(d
, "master-of-seat") > 0;
258 /* Ignore non-master devices for unknown seats */
259 if (!master
&& !seat
)
262 r
= sd_device_get_syspath(d
, &syspath
);
266 r
= manager_add_device(m
, syspath
, master
, &device
);
271 r
= manager_add_seat(m
, sn
, &seat
);
280 device_attach(device
, seat
);
287 int manager_process_button_device(Manager
*m
, sd_device
*d
) {
288 const char *action
, *sysname
;
294 r
= sd_device_get_sysname(d
, &sysname
);
298 if (sd_device_get_property_value(d
, "ACTION", &action
) >= 0 &&
299 streq(action
, "remove")) {
301 b
= hashmap_get(m
->buttons
, sysname
);
310 r
= manager_add_button(m
, sysname
, &b
);
314 if (sd_device_get_property_value(d
, "ID_SEAT", &sn
) < 0 || isempty(sn
))
317 button_set_seat(b
, sn
);
320 if (r
< 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
321 * opening the device?) let's close the button again. */
328 int manager_get_session_by_pid(Manager
*m
, pid_t pid
, Session
**ret
) {
329 _cleanup_free_
char *unit
= NULL
;
335 if (!pid_is_valid(pid
))
338 r
= cg_pid_get_unit(pid
, &unit
);
342 s
= hashmap_get(m
->session_units
, unit
);
357 int manager_get_user_by_pid(Manager
*m
, pid_t pid
, User
**ret
) {
358 _cleanup_free_
char *unit
= NULL
;
364 if (!pid_is_valid(pid
))
367 r
= cg_pid_get_slice(pid
, &unit
);
371 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 int 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 int 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 int)];
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 static bool manager_is_docked(Manager
*m
) {
550 HASHMAP_FOREACH(b
, m
->buttons
, i
)
557 static int manager_count_external_displays(Manager
*m
) {
558 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
562 r
= sd_device_enumerator_new(&e
);
566 r
= sd_device_enumerator_allow_uninitialized(e
);
570 r
= sd_device_enumerator_add_match_subsystem(e
, "drm", true);
574 FOREACH_DEVICE(e
, d
) {
576 const char *status
, *enabled
, *dash
, *nn
, *i
, *subsys
;
577 bool external
= false;
579 if (sd_device_get_parent(d
, &p
) < 0)
582 /* If the parent shares the same subsystem as the
583 * device we are looking at then it is a connector,
584 * which is what we are interested in. */
585 if (sd_device_get_subsystem(p
, &subsys
) < 0 || !streq(subsys
, "drm"))
588 if (sd_device_get_sysname(d
, &nn
) < 0)
591 /* Ignore internal displays: the type is encoded in the sysfs name, as the second dash separated item
592 * (the first is the card name, the last the connector number). We implement a blacklist of external
593 * displays here, rather than a whitelist of internal ones, to ensure we don't block suspends too
595 dash
= strchr(nn
, '-');
600 FOREACH_STRING(i
, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
601 "Composite-", "SVIDEO-", "Component-",
602 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
604 if (startswith(dash
, i
)) {
612 /* Ignore ports that are not enabled */
613 if (sd_device_get_sysattr_value(d
, "enabled", &enabled
) < 0 || !streq(enabled
, "enabled"))
616 /* We count any connector which is not explicitly
617 * "disconnected" as connected. */
618 if (sd_device_get_sysattr_value(d
, "status", &status
) < 0 || !streq(status
, "disconnected"))
625 bool manager_is_docked_or_external_displays(Manager
*m
) {
628 /* If we are docked don't react to lid closing */
629 if (manager_is_docked(m
)) {
630 log_debug("System is docked.");
634 /* If we have more than one display connected,
635 * assume that we are docked. */
636 n
= manager_count_external_displays(m
);
638 log_warning_errno(n
, "Display counting failed: %m");
640 log_debug("External (%i) displays connected.", n
);
647 bool manager_is_on_external_power(void) {
650 /* For now we only check for AC power, but 'external power' can apply
651 * to anything that isn't an internal battery */
654 log_warning_errno(r
, "Failed to read AC power status: %m");
661 bool manager_all_buttons_ignored(Manager
*m
) {
664 if (m
->handle_power_key
!= HANDLE_IGNORE
)
666 if (m
->handle_suspend_key
!= HANDLE_IGNORE
)
668 if (m
->handle_hibernate_key
!= HANDLE_IGNORE
)
670 if (m
->handle_lid_switch
!= HANDLE_IGNORE
)
672 if (m
->handle_lid_switch_ep
!= _HANDLE_ACTION_INVALID
&&
673 m
->handle_lid_switch_ep
!= HANDLE_IGNORE
)
675 if (m
->handle_lid_switch_docked
!= HANDLE_IGNORE
)