1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
27 #include "alloc-util.h"
28 #include "bus-error.h"
30 #include "cgroup-util.h"
33 #include "parse-util.h"
34 #include "process-util.h"
36 #include "terminal-util.h"
37 #include "udev-util.h"
38 #include "user-util.h"
40 int manager_add_device(Manager
*m
, const char *sysfs
, bool master
, Device
**_device
) {
46 d
= hashmap_get(m
->devices
, sysfs
);
48 /* we support adding master-flags, but not removing them */
49 d
->master
= d
->master
|| master
;
51 d
= device_new(m
, sysfs
, master
);
62 int manager_add_seat(Manager
*m
, const char *id
, Seat
**_seat
) {
68 s
= hashmap_get(m
->seats
, id
);
81 int manager_add_session(Manager
*m
, const char *id
, Session
**_session
) {
87 s
= hashmap_get(m
->sessions
, id
);
89 s
= session_new(m
, id
);
100 int manager_add_user(Manager
*m
, uid_t uid
, gid_t gid
, const char *name
, User
**_user
) {
107 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
109 r
= user_new(&u
, m
, uid
, gid
, name
);
120 int manager_add_user_by_name(Manager
*m
, const char *name
, User
**_user
) {
128 r
= get_user_creds(&name
, &uid
, &gid
, NULL
, NULL
);
132 return manager_add_user(m
, uid
, gid
, name
, _user
);
135 int manager_add_user_by_uid(Manager
*m
, uid_t uid
, User
**_user
) {
143 return errno
> 0 ? -errno
: -ENOENT
;
145 return manager_add_user(m
, uid
, p
->pw_gid
, p
->pw_name
, _user
);
148 int manager_add_inhibitor(Manager
*m
, const char* id
, Inhibitor
**_inhibitor
) {
154 i
= hashmap_get(m
->inhibitors
, id
);
162 i
= inhibitor_new(m
, id
);
172 int manager_add_button(Manager
*m
, const char *name
, Button
**_button
) {
178 b
= hashmap_get(m
->buttons
, name
);
180 b
= button_new(m
, name
);
191 int manager_process_seat_device(Manager
*m
, struct udev_device
*d
) {
197 if (streq_ptr(udev_device_get_action(d
), "remove")) {
199 device
= hashmap_get(m
->devices
, udev_device_get_syspath(d
));
203 seat_add_to_gc_queue(device
->seat
);
211 sn
= udev_device_get_property_value(d
, "ID_SEAT");
215 if (!seat_name_is_valid(sn
)) {
216 log_warning("Device with invalid seat name %s found, ignoring.", sn
);
220 seat
= hashmap_get(m
->seats
, sn
);
221 master
= udev_device_has_tag(d
, "master-of-seat");
223 /* Ignore non-master devices for unknown seats */
224 if (!master
&& !seat
)
227 r
= manager_add_device(m
, udev_device_get_syspath(d
), master
, &device
);
232 r
= manager_add_seat(m
, sn
, &seat
);
241 device_attach(device
, seat
);
248 int manager_process_button_device(Manager
*m
, struct udev_device
*d
) {
255 if (streq_ptr(udev_device_get_action(d
), "remove")) {
257 b
= hashmap_get(m
->buttons
, udev_device_get_sysname(d
));
266 r
= manager_add_button(m
, udev_device_get_sysname(d
), &b
);
270 sn
= udev_device_get_property_value(d
, "ID_SEAT");
274 button_set_seat(b
, sn
);
277 if (r
< 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
278 * opening the device?) let's close the button again. */
285 int manager_get_session_by_pid(Manager
*m
, pid_t pid
, Session
**session
) {
286 _cleanup_free_
char *unit
= NULL
;
292 if (!pid_is_valid(pid
))
295 r
= cg_pid_get_unit(pid
, &unit
);
299 s
= hashmap_get(m
->session_units
, unit
);
308 int manager_get_user_by_pid(Manager
*m
, pid_t pid
, User
**user
) {
309 _cleanup_free_
char *unit
= NULL
;
316 if (!pid_is_valid(pid
))
319 r
= cg_pid_get_slice(pid
, &unit
);
323 u
= hashmap_get(m
->user_units
, unit
);
331 int manager_get_idle_hint(Manager
*m
, dual_timestamp
*t
) {
334 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
339 idle_hint
= !manager_is_inhibited(m
, INHIBIT_IDLE
, INHIBIT_BLOCK
, t
, false, false, 0, NULL
);
341 HASHMAP_FOREACH(s
, m
->sessions
, i
) {
345 ih
= session_get_idle_hint(s
, &k
);
351 if (k
.monotonic
< ts
.monotonic
)
357 } else if (idle_hint
) {
359 if (k
.monotonic
> ts
.monotonic
)
370 bool manager_shall_kill(Manager
*m
, const char *user
) {
374 if (!m
->kill_exclude_users
&& streq(user
, "root"))
377 if (strv_contains(m
->kill_exclude_users
, user
))
380 if (!strv_isempty(m
->kill_only_users
))
381 return strv_contains(m
->kill_only_users
, user
);
383 return m
->kill_user_processes
;
386 int config_parse_n_autovts(
388 const char *filename
,
391 unsigned section_line
,
407 r
= safe_atou(rvalue
, &o
);
409 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse number of autovts, ignoring: %s", rvalue
);
414 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "A maximum of 15 autovts are supported, ignoring: %s", rvalue
);
422 static int vt_is_busy(unsigned int vtnr
) {
423 struct vt_stat vt_stat
;
425 _cleanup_close_
int fd
;
429 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
432 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
433 * we'd open the latter we'd open the foreground tty which
434 * hence would be unconditionally busy. By opening /dev/tty1
435 * we avoid this. Since tty1 is special and needs to be an
436 * explicitly loaded getty or DM this is safe. */
438 fd
= open_terminal("/dev/tty1", O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
442 if (ioctl(fd
, VT_GETSTATE
, &vt_stat
) < 0)
445 r
= !!(vt_stat
.v_state
& (1 << vtnr
));
450 int manager_spawn_autovt(Manager
*m
, unsigned int vtnr
) {
451 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
452 char name
[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
458 if (vtnr
> m
->n_autovts
&&
459 vtnr
!= m
->reserve_vt
)
462 if (vtnr
!= m
->reserve_vt
) {
463 /* If this is the reserved TTY, we'll start the getty
464 * on it in any case, but otherwise only if it is not
467 r
= vt_is_busy(vtnr
);
474 snprintf(name
, sizeof(name
), "autovt@tty%u.service", vtnr
);
475 r
= sd_bus_call_method(
477 "org.freedesktop.systemd1",
478 "/org/freedesktop/systemd1",
479 "org.freedesktop.systemd1.Manager",
485 log_error("Failed to start %s: %s", name
, bus_error_message(&error
, r
));
490 static bool manager_is_docked(Manager
*m
) {
494 HASHMAP_FOREACH(b
, m
->buttons
, i
)
501 static int manager_count_external_displays(Manager
*m
) {
502 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*e
= NULL
;
503 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
507 e
= udev_enumerate_new(m
->udev
);
511 r
= udev_enumerate_add_match_subsystem(e
, "drm");
515 r
= udev_enumerate_scan_devices(e
);
519 first
= udev_enumerate_get_list_entry(e
);
520 udev_list_entry_foreach(item
, first
) {
521 _cleanup_udev_device_unref_
struct udev_device
*d
= NULL
;
522 struct udev_device
*p
;
523 const char *status
, *enabled
, *dash
, *nn
, *i
;
524 bool external
= false;
526 d
= udev_device_new_from_syspath(m
->udev
, udev_list_entry_get_name(item
));
530 p
= udev_device_get_parent(d
);
534 /* If the parent shares the same subsystem as the
535 * device we are looking at then it is a connector,
536 * which is what we are interested in. */
537 if (!streq_ptr(udev_device_get_subsystem(p
), "drm"))
540 nn
= udev_device_get_sysname(d
);
544 /* Ignore internal displays: the type is encoded in
545 * the sysfs name, as the second dash separated item
546 * (the first is the card name, the last the connector
547 * number). We implement a whitelist of external
548 * displays here, rather than a whitelist, to ensure
549 * we don't block suspends too eagerly. */
550 dash
= strchr(nn
, '-');
555 FOREACH_STRING(i
, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
556 "Composite-", "SVIDEO-", "Component-",
557 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
559 if (startswith(dash
, i
)) {
567 /* Ignore ports that are not enabled */
568 enabled
= udev_device_get_sysattr_value(d
, "enabled");
571 if (!streq_ptr(enabled
, "enabled"))
574 /* We count any connector which is not explicitly
575 * "disconnected" as connected. */
576 status
= udev_device_get_sysattr_value(d
, "status");
577 if (!streq_ptr(status
, "disconnected"))
584 bool manager_is_docked_or_external_displays(Manager
*m
) {
587 /* If we are docked don't react to lid closing */
588 if (manager_is_docked(m
)) {
589 log_debug("System is docked.");
593 /* If we have more than one display connected,
594 * assume that we are docked. */
595 n
= manager_count_external_displays(m
);
597 log_warning_errno(n
, "Display counting failed: %m");
599 log_debug("External (%i) displays connected.", n
);