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"
35 #include "terminal-util.h"
36 #include "udev-util.h"
37 #include "user-util.h"
39 int manager_add_device(Manager
*m
, const char *sysfs
, bool master
, Device
**_device
) {
45 d
= hashmap_get(m
->devices
, sysfs
);
47 /* we support adding master-flags, but not removing them */
48 d
->master
= d
->master
|| master
;
50 d
= device_new(m
, sysfs
, master
);
61 int manager_add_seat(Manager
*m
, const char *id
, Seat
**_seat
) {
67 s
= hashmap_get(m
->seats
, id
);
80 int manager_add_session(Manager
*m
, const char *id
, Session
**_session
) {
86 s
= hashmap_get(m
->sessions
, id
);
88 s
= session_new(m
, id
);
99 int manager_add_user(Manager
*m
, uid_t uid
, gid_t gid
, const char *name
, User
**_user
) {
106 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
108 r
= user_new(&u
, m
, uid
, gid
, name
);
119 int manager_add_user_by_name(Manager
*m
, const char *name
, User
**_user
) {
127 r
= get_user_creds(&name
, &uid
, &gid
, NULL
, NULL
);
131 return manager_add_user(m
, uid
, gid
, name
, _user
);
134 int manager_add_user_by_uid(Manager
*m
, uid_t uid
, User
**_user
) {
142 return errno
> 0 ? -errno
: -ENOENT
;
144 return manager_add_user(m
, uid
, p
->pw_gid
, p
->pw_name
, _user
);
147 int manager_add_inhibitor(Manager
*m
, const char* id
, Inhibitor
**_inhibitor
) {
153 i
= hashmap_get(m
->inhibitors
, id
);
161 i
= inhibitor_new(m
, id
);
171 int manager_add_button(Manager
*m
, const char *name
, Button
**_button
) {
177 b
= hashmap_get(m
->buttons
, name
);
179 b
= button_new(m
, name
);
190 int manager_process_seat_device(Manager
*m
, struct udev_device
*d
) {
196 if (streq_ptr(udev_device_get_action(d
), "remove")) {
198 device
= hashmap_get(m
->devices
, udev_device_get_syspath(d
));
202 seat_add_to_gc_queue(device
->seat
);
210 sn
= udev_device_get_property_value(d
, "ID_SEAT");
214 if (!seat_name_is_valid(sn
)) {
215 log_warning("Device with invalid seat name %s found, ignoring.", sn
);
219 seat
= hashmap_get(m
->seats
, sn
);
220 master
= udev_device_has_tag(d
, "master-of-seat");
222 /* Ignore non-master devices for unknown seats */
223 if (!master
&& !seat
)
226 r
= manager_add_device(m
, udev_device_get_syspath(d
), master
, &device
);
231 r
= manager_add_seat(m
, sn
, &seat
);
240 device_attach(device
, seat
);
247 int manager_process_button_device(Manager
*m
, struct udev_device
*d
) {
254 if (streq_ptr(udev_device_get_action(d
), "remove")) {
256 b
= hashmap_get(m
->buttons
, udev_device_get_sysname(d
));
265 r
= manager_add_button(m
, udev_device_get_sysname(d
), &b
);
269 sn
= udev_device_get_property_value(d
, "ID_SEAT");
273 button_set_seat(b
, sn
);
276 if (r
< 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
277 * opening the device?) let's close the button again. */
284 int manager_get_session_by_pid(Manager
*m
, pid_t pid
, Session
**session
) {
285 _cleanup_free_
char *unit
= NULL
;
291 if (!pid_is_valid(pid
))
294 r
= cg_pid_get_unit(pid
, &unit
);
298 s
= hashmap_get(m
->session_units
, unit
);
307 int manager_get_user_by_pid(Manager
*m
, pid_t pid
, User
**user
) {
308 _cleanup_free_
char *unit
= NULL
;
315 if (!pid_is_valid(pid
))
318 r
= cg_pid_get_slice(pid
, &unit
);
322 u
= hashmap_get(m
->user_units
, unit
);
330 int manager_get_idle_hint(Manager
*m
, dual_timestamp
*t
) {
333 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
338 idle_hint
= !manager_is_inhibited(m
, INHIBIT_IDLE
, INHIBIT_BLOCK
, t
, false, false, 0, NULL
);
340 HASHMAP_FOREACH(s
, m
->sessions
, i
) {
344 ih
= session_get_idle_hint(s
, &k
);
350 if (k
.monotonic
< ts
.monotonic
)
356 } else if (idle_hint
) {
358 if (k
.monotonic
> ts
.monotonic
)
369 bool manager_shall_kill(Manager
*m
, const char *user
) {
373 if (!m
->kill_exclude_users
&& streq(user
, "root"))
376 if (strv_contains(m
->kill_exclude_users
, user
))
379 if (!strv_isempty(m
->kill_only_users
))
380 return strv_contains(m
->kill_only_users
, user
);
382 return m
->kill_user_processes
;
385 int config_parse_n_autovts(
387 const char *filename
,
390 unsigned section_line
,
406 r
= safe_atou(rvalue
, &o
);
408 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse number of autovts, ignoring: %s", rvalue
);
413 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "A maximum of 15 autovts are supported, ignoring: %s", rvalue
);
421 static int vt_is_busy(unsigned int vtnr
) {
422 struct vt_stat vt_stat
;
424 _cleanup_close_
int fd
;
428 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
431 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
432 * we'd open the latter we'd open the foreground tty which
433 * hence would be unconditionally busy. By opening /dev/tty1
434 * we avoid this. Since tty1 is special and needs to be an
435 * explicitly loaded getty or DM this is safe. */
437 fd
= open_terminal("/dev/tty1", O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
441 if (ioctl(fd
, VT_GETSTATE
, &vt_stat
) < 0)
444 r
= !!(vt_stat
.v_state
& (1 << vtnr
));
449 int manager_spawn_autovt(Manager
*m
, unsigned int vtnr
) {
450 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
451 char name
[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
457 if (vtnr
> m
->n_autovts
&&
458 vtnr
!= m
->reserve_vt
)
461 if (vtnr
!= m
->reserve_vt
) {
462 /* If this is the reserved TTY, we'll start the getty
463 * on it in any case, but otherwise only if it is not
466 r
= vt_is_busy(vtnr
);
473 snprintf(name
, sizeof(name
), "autovt@tty%u.service", vtnr
);
474 r
= sd_bus_call_method(
476 "org.freedesktop.systemd1",
477 "/org/freedesktop/systemd1",
478 "org.freedesktop.systemd1.Manager",
484 log_error("Failed to start %s: %s", name
, bus_error_message(&error
, r
));
489 static bool manager_is_docked(Manager
*m
) {
493 HASHMAP_FOREACH(b
, m
->buttons
, i
)
500 static int manager_count_external_displays(Manager
*m
) {
501 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*e
= NULL
;
502 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
506 e
= udev_enumerate_new(m
->udev
);
510 r
= udev_enumerate_add_match_subsystem(e
, "drm");
514 r
= udev_enumerate_scan_devices(e
);
518 first
= udev_enumerate_get_list_entry(e
);
519 udev_list_entry_foreach(item
, first
) {
520 _cleanup_udev_device_unref_
struct udev_device
*d
= NULL
;
521 struct udev_device
*p
;
522 const char *status
, *enabled
, *dash
, *nn
, *i
;
523 bool external
= false;
525 d
= udev_device_new_from_syspath(m
->udev
, udev_list_entry_get_name(item
));
529 p
= udev_device_get_parent(d
);
533 /* If the parent shares the same subsystem as the
534 * device we are looking at then it is a connector,
535 * which is what we are interested in. */
536 if (!streq_ptr(udev_device_get_subsystem(p
), "drm"))
539 nn
= udev_device_get_sysname(d
);
543 /* Ignore internal displays: the type is encoded in
544 * the sysfs name, as the second dash separated item
545 * (the first is the card name, the last the connector
546 * number). We implement a whitelist of external
547 * displays here, rather than a whitelist, to ensure
548 * we don't block suspends too eagerly. */
549 dash
= strchr(nn
, '-');
554 FOREACH_STRING(i
, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
555 "Composite-", "SVIDEO-", "Component-",
556 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
558 if (startswith(dash
, i
)) {
566 /* Ignore ports that are not enabled */
567 enabled
= udev_device_get_sysattr_value(d
, "enabled");
570 if (!streq_ptr(enabled
, "enabled"))
573 /* We count any connector which is not explicitly
574 * "disconnected" as connected. */
575 status
= udev_device_get_sysattr_value(d
, "status");
576 if (!streq_ptr(status
, "disconnected"))
583 bool manager_is_docked_or_external_displays(Manager
*m
) {
586 /* If we are docked don't react to lid closing */
587 if (manager_is_docked(m
)) {
588 log_debug("System is docked.");
592 /* If we have more than one display connected,
593 * assume that we are docked. */
594 n
= manager_count_external_displays(m
);
596 log_warning_errno(n
, "Display counting failed: %m");
598 log_debug("External (%i) displays connected.", n
);