1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
29 #include "cgroup-util.h"
31 #include "bus-error.h"
32 #include "udev-util.h"
34 #include "terminal-util.h"
36 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
42 d = hashmap_get(m->devices, sysfs);
44 /* we support adding master-flags, but not removing them */
45 d->master = d->master || master;
47 d = device_new(m, sysfs, master);
58 int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
64 s = hashmap_get(m->seats, id);
77 int manager_add_session(Manager *m, const char *id, Session **_session) {
83 s = hashmap_get(m->sessions, id);
85 s = session_new(m, id);
96 int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) {
102 u = hashmap_get(m->users, UID_TO_PTR(uid));
104 u = user_new(m, uid, gid, name);
115 int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
123 r = get_user_creds(&name, &uid, &gid, NULL, NULL);
127 return manager_add_user(m, uid, gid, name, _user);
130 int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
138 return errno ? -errno : -ENOENT;
140 return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
143 int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
149 i = hashmap_get(m->inhibitors, id);
157 i = inhibitor_new(m, id);
167 int manager_add_button(Manager *m, const char *name, Button **_button) {
173 b = hashmap_get(m->buttons, name);
175 b = button_new(m, name);
186 int manager_process_seat_device(Manager *m, struct udev_device *d) {
192 if (streq_ptr(udev_device_get_action(d), "remove")) {
194 device = hashmap_get(m->devices, udev_device_get_syspath(d));
198 seat_add_to_gc_queue(device->seat);
206 sn = udev_device_get_property_value(d, "ID_SEAT");
210 if (!seat_name_is_valid(sn)) {
211 log_warning("Device with invalid seat name %s found, ignoring.", sn);
215 seat = hashmap_get(m->seats, sn);
216 master = udev_device_has_tag(d, "master-of-seat");
218 /* Ignore non-master devices for unknown seats */
219 if (!master && !seat)
222 r = manager_add_device(m, udev_device_get_syspath(d), master, &device);
227 r = manager_add_seat(m, sn, &seat);
236 device_attach(device, seat);
243 int manager_process_button_device(Manager *m, struct udev_device *d) {
250 if (streq_ptr(udev_device_get_action(d), "remove")) {
252 b = hashmap_get(m->buttons, udev_device_get_sysname(d));
261 r = manager_add_button(m, udev_device_get_sysname(d), &b);
265 sn = udev_device_get_property_value(d, "ID_SEAT");
269 button_set_seat(b, sn);
276 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
277 /// elogind does not support systemd units, but its own session system
279 _cleanup_free_ char *unit = NULL;
281 _cleanup_free_ char *session_name = NULL;
291 /// elogind does not support systemd units, but its own session system
293 r = cg_pid_get_unit(pid, &unit);
297 s = hashmap_get(m->session_units, unit);
299 log_debug_elogind("Searching session for PID %u", pid);
300 r = cg_pid_get_session(pid, &session_name);
304 s = hashmap_get(m->sessions, session_name);
305 log_debug_elogind("Session Name \"%s\" -> Session \"%s\"",
306 session_name, s && s->id ? s->id : "NULL");
316 int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
317 /// elogind does not support systemd units, but its own session system
319 _cleanup_free_ char *unit = NULL;
332 /// elogind does not support systemd units, but its own session system
334 r = cg_pid_get_slice(pid, &unit);
338 u = hashmap_get(m->user_units, unit);
344 r = manager_get_session_by_pid (m, pid, &s);
354 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
357 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
362 idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0, NULL);
364 HASHMAP_FOREACH(s, m->sessions, i) {
368 ih = session_get_idle_hint(s, &k);
374 if (k.monotonic < ts.monotonic)
380 } else if (idle_hint) {
382 if (k.monotonic > ts.monotonic)
393 bool manager_shall_kill(Manager *m, const char *user) {
397 if (!m->kill_user_processes)
400 if (strv_contains(m->kill_exclude_users, user))
403 if (strv_isempty(m->kill_only_users))
406 return strv_contains(m->kill_only_users, user);
409 /// UNNEEDED by elogind
411 static int vt_is_busy(unsigned int vtnr) {
412 struct vt_stat vt_stat;
414 _cleanup_close_ int fd;
418 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
419 * we'd open the latter we'd open the foreground tty which
420 * hence would be unconditionally busy. By opening /dev/tty1
421 * we avoid this. Since tty1 is special and needs to be an
422 * explicitly loaded getty or DM this is safe. */
424 fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
428 if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
431 r = !!(vt_stat.v_state & (1 << vtnr));
436 int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
437 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
438 char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
444 if (vtnr > m->n_autovts &&
445 vtnr != m->reserve_vt)
448 if (vtnr != m->reserve_vt) {
449 /* If this is the reserved TTY, we'll start the getty
450 * on it in any case, but otherwise only if it is not
453 r = vt_is_busy(vtnr);
460 snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
461 r = sd_bus_call_method(
463 "org.freedesktop.systemd1",
464 "/org/freedesktop/systemd1",
465 "org.freedesktop.systemd1.Manager",
471 log_error("Failed to start %s: %s", name, bus_error_message(&error, r));
477 static bool manager_is_docked(Manager *m) {
481 HASHMAP_FOREACH(b, m->buttons, i)
488 static int manager_count_external_displays(Manager *m) {
489 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
490 struct udev_list_entry *item = NULL, *first = NULL;
494 e = udev_enumerate_new(m->udev);
498 r = udev_enumerate_add_match_subsystem(e, "drm");
502 r = udev_enumerate_scan_devices(e);
506 first = udev_enumerate_get_list_entry(e);
507 udev_list_entry_foreach(item, first) {
508 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
509 struct udev_device *p;
510 const char *status, *enabled, *dash, *nn, *i;
511 bool external = false;
513 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
517 p = udev_device_get_parent(d);
521 /* If the parent shares the same subsystem as the
522 * device we are looking at then it is a connector,
523 * which is what we are interested in. */
524 if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
527 nn = udev_device_get_sysname(d);
531 /* Ignore internal displays: the type is encoded in
532 * the sysfs name, as the second dash seperated item
533 * (the first is the card name, the last the connector
534 * number). We implement a whitelist of external
535 * displays here, rather than a whitelist, to ensure
536 * we don't block suspends too eagerly. */
537 dash = strchr(nn, '-');
542 FOREACH_STRING(i, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
543 "Composite-", "SVIDEO-", "Component-",
544 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
546 if (startswith(dash, i)) {
554 /* Ignore ports that are not enabled */
555 enabled = udev_device_get_sysattr_value(d, "enabled");
558 if (!streq_ptr(enabled, "enabled"))
561 /* We count any connector which is not explicitly
562 * "disconnected" as connected. */
563 status = udev_device_get_sysattr_value(d, "status");
564 if (!streq_ptr(status, "disconnected"))
571 bool manager_is_docked_or_external_displays(Manager *m) {
574 /* If we are docked don't react to lid closing */
575 if (manager_is_docked(m)) {
576 log_debug("System is docked.");
580 /* If we have more than one display connected,
581 * assume that we are docked. */
582 n = manager_count_external_displays(m);
584 log_warning_errno(n, "Display counting failed: %m");
586 log_debug("External (%i) displays connected.", n);