2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
26 #include "alloc-util.h"
27 #include "bus-error.h"
29 #include "cgroup-util.h"
32 #include "parse-util.h"
34 #include "terminal-util.h"
35 #include "udev-util.h"
36 #include "user-util.h"
38 int manager_add_device(Manager
*m
, const char *sysfs
, bool master
, Device
**_device
) {
44 d
= hashmap_get(m
->devices
, sysfs
);
46 /* we support adding master-flags, but not removing them */
47 d
->master
= d
->master
|| master
;
49 d
= device_new(m
, sysfs
, master
);
60 int manager_add_seat(Manager
*m
, const char *id
, Seat
**_seat
) {
66 s
= hashmap_get(m
->seats
, id
);
79 int manager_add_session(Manager
*m
, const char *id
, Session
**_session
) {
85 s
= hashmap_get(m
->sessions
, id
);
87 s
= session_new(m
, id
);
98 int manager_add_user(Manager
*m
, uid_t uid
, gid_t gid
, const char *name
, User
**_user
) {
105 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
107 r
= user_new(&u
, m
, uid
, gid
, name
);
118 int manager_add_user_by_name(Manager
*m
, const char *name
, User
**_user
) {
126 r
= get_user_creds(&name
, &uid
, &gid
, NULL
, NULL
);
130 return manager_add_user(m
, uid
, gid
, name
, _user
);
133 int manager_add_user_by_uid(Manager
*m
, uid_t uid
, User
**_user
) {
141 return errno
> 0 ? -errno
: -ENOENT
;
143 return manager_add_user(m
, uid
, p
->pw_gid
, p
->pw_name
, _user
);
146 int manager_add_inhibitor(Manager
*m
, const char* id
, Inhibitor
**_inhibitor
) {
152 i
= hashmap_get(m
->inhibitors
, id
);
160 i
= inhibitor_new(m
, id
);
170 int manager_add_button(Manager
*m
, const char *name
, Button
**_button
) {
176 b
= hashmap_get(m
->buttons
, name
);
178 b
= button_new(m
, name
);
189 int manager_process_seat_device(Manager
*m
, struct udev_device
*d
) {
195 if (streq_ptr(udev_device_get_action(d
), "remove")) {
197 device
= hashmap_get(m
->devices
, udev_device_get_syspath(d
));
201 seat_add_to_gc_queue(device
->seat
);
209 sn
= udev_device_get_property_value(d
, "ID_SEAT");
213 if (!seat_name_is_valid(sn
)) {
214 log_warning("Device with invalid seat name %s found, ignoring.", sn
);
218 seat
= hashmap_get(m
->seats
, sn
);
219 master
= udev_device_has_tag(d
, "master-of-seat");
221 /* Ignore non-master devices for unknown seats */
222 if (!master
&& !seat
)
225 r
= manager_add_device(m
, udev_device_get_syspath(d
), master
, &device
);
230 r
= manager_add_seat(m
, sn
, &seat
);
239 device_attach(device
, seat
);
246 int manager_process_button_device(Manager
*m
, struct udev_device
*d
) {
253 if (streq_ptr(udev_device_get_action(d
), "remove")) {
255 b
= hashmap_get(m
->buttons
, udev_device_get_sysname(d
));
264 r
= manager_add_button(m
, udev_device_get_sysname(d
), &b
);
268 sn
= udev_device_get_property_value(d
, "ID_SEAT");
272 button_set_seat(b
, sn
);
275 if (r
< 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
276 * opening the device?) let's close the button again. */
283 int manager_get_session_by_pid(Manager
*m
, pid_t pid
, Session
**session
) {
284 _cleanup_free_
char *unit
= NULL
;
290 if (!pid_is_valid(pid
))
293 r
= cg_pid_get_unit(pid
, &unit
);
297 s
= hashmap_get(m
->session_units
, unit
);
306 int manager_get_user_by_pid(Manager
*m
, pid_t pid
, User
**user
) {
307 _cleanup_free_
char *unit
= NULL
;
314 if (!pid_is_valid(pid
))
317 r
= cg_pid_get_slice(pid
, &unit
);
321 u
= hashmap_get(m
->user_units
, unit
);
329 int manager_get_idle_hint(Manager
*m
, dual_timestamp
*t
) {
332 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
337 idle_hint
= !manager_is_inhibited(m
, INHIBIT_IDLE
, INHIBIT_BLOCK
, t
, false, false, 0, NULL
);
339 HASHMAP_FOREACH(s
, m
->sessions
, i
) {
343 ih
= session_get_idle_hint(s
, &k
);
349 if (k
.monotonic
< ts
.monotonic
)
355 } else if (idle_hint
) {
357 if (k
.monotonic
> ts
.monotonic
)
368 bool manager_shall_kill(Manager
*m
, const char *user
) {
372 if (!m
->kill_exclude_users
&& streq(user
, "root"))
375 if (strv_contains(m
->kill_exclude_users
, user
))
378 if (!strv_isempty(m
->kill_only_users
))
379 return strv_contains(m
->kill_only_users
, user
);
381 return m
->kill_user_processes
;
384 int config_parse_n_autovts(
386 const char *filename
,
389 unsigned section_line
,
405 r
= safe_atou(rvalue
, &o
);
407 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse number of autovts, ignoring: %s", rvalue
);
412 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "A maximum of 15 autovts are supported, ignoring: %s", rvalue
);
420 static int vt_is_busy(unsigned int vtnr
) {
421 struct vt_stat vt_stat
;
423 _cleanup_close_
int fd
;
427 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
430 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
431 * we'd open the latter we'd open the foreground tty which
432 * hence would be unconditionally busy. By opening /dev/tty1
433 * we avoid this. Since tty1 is special and needs to be an
434 * explicitly loaded getty or DM this is safe. */
436 fd
= open_terminal("/dev/tty1", O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
440 if (ioctl(fd
, VT_GETSTATE
, &vt_stat
) < 0)
443 r
= !!(vt_stat
.v_state
& (1 << vtnr
));
448 int manager_spawn_autovt(Manager
*m
, unsigned int vtnr
) {
449 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
450 char name
[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
456 if (vtnr
> m
->n_autovts
&&
457 vtnr
!= m
->reserve_vt
)
460 if (vtnr
!= m
->reserve_vt
) {
461 /* If this is the reserved TTY, we'll start the getty
462 * on it in any case, but otherwise only if it is not
465 r
= vt_is_busy(vtnr
);
472 snprintf(name
, sizeof(name
), "autovt@tty%u.service", vtnr
);
473 r
= sd_bus_call_method(
475 "org.freedesktop.systemd1",
476 "/org/freedesktop/systemd1",
477 "org.freedesktop.systemd1.Manager",
483 log_error("Failed to start %s: %s", name
, bus_error_message(&error
, r
));
488 static bool manager_is_docked(Manager
*m
) {
492 HASHMAP_FOREACH(b
, m
->buttons
, i
)
499 static int manager_count_external_displays(Manager
*m
) {
500 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*e
= NULL
;
501 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
505 e
= udev_enumerate_new(m
->udev
);
509 r
= udev_enumerate_add_match_subsystem(e
, "drm");
513 r
= udev_enumerate_scan_devices(e
);
517 first
= udev_enumerate_get_list_entry(e
);
518 udev_list_entry_foreach(item
, first
) {
519 _cleanup_udev_device_unref_
struct udev_device
*d
= NULL
;
520 struct udev_device
*p
;
521 const char *status
, *enabled
, *dash
, *nn
, *i
;
522 bool external
= false;
524 d
= udev_device_new_from_syspath(m
->udev
, udev_list_entry_get_name(item
));
528 p
= udev_device_get_parent(d
);
532 /* If the parent shares the same subsystem as the
533 * device we are looking at then it is a connector,
534 * which is what we are interested in. */
535 if (!streq_ptr(udev_device_get_subsystem(p
), "drm"))
538 nn
= udev_device_get_sysname(d
);
542 /* Ignore internal displays: the type is encoded in
543 * the sysfs name, as the second dash separated item
544 * (the first is the card name, the last the connector
545 * number). We implement a whitelist of external
546 * displays here, rather than a whitelist, to ensure
547 * we don't block suspends too eagerly. */
548 dash
= strchr(nn
, '-');
553 FOREACH_STRING(i
, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
554 "Composite-", "SVIDEO-", "Component-",
555 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
557 if (startswith(dash
, i
)) {
565 /* Ignore ports that are not enabled */
566 enabled
= udev_device_get_sysattr_value(d
, "enabled");
569 if (!streq_ptr(enabled
, "enabled"))
572 /* We count any connector which is not explicitly
573 * "disconnected" as connected. */
574 status
= udev_device_get_sysattr_value(d
, "status");
575 if (!streq_ptr(status
, "disconnected"))
582 bool manager_is_docked_or_external_displays(Manager
*m
) {
585 /* If we are docked don't react to lid closing */
586 if (manager_is_docked(m
)) {
587 log_debug("System is docked.");
591 /* If we have more than one display connected,
592 * assume that we are docked. */
593 n
= manager_count_external_displays(m
);
595 log_warning_errno(n
, "Display counting failed: %m");
597 log_debug("External (%i) displays connected.", n
);