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"
33 #include "terminal-util.h"
34 #include "udev-util.h"
35 #include "user-util.h"
37 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
43 d = hashmap_get(m->devices, sysfs);
45 /* we support adding master-flags, but not removing them */
46 d->master = d->master || master;
48 d = device_new(m, sysfs, master);
59 int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
65 s = hashmap_get(m->seats, id);
78 int manager_add_session(Manager *m, const char *id, Session **_session) {
84 s = hashmap_get(m->sessions, id);
86 s = session_new(m, id);
97 int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) {
104 u = hashmap_get(m->users, UID_TO_PTR(uid));
106 r = user_new(&u, m, uid, gid, name);
117 int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
125 r = get_user_creds(&name, &uid, &gid, NULL, NULL);
129 return manager_add_user(m, uid, gid, name, _user);
132 int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
140 return errno > 0 ? -errno : -ENOENT;
142 return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
145 int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
151 i = hashmap_get(m->inhibitors, id);
159 i = inhibitor_new(m, id);
169 int manager_add_button(Manager *m, const char *name, Button **_button) {
175 b = hashmap_get(m->buttons, name);
177 b = button_new(m, name);
188 int manager_process_seat_device(Manager *m, struct udev_device *d) {
194 if (streq_ptr(udev_device_get_action(d), "remove")) {
196 device = hashmap_get(m->devices, udev_device_get_syspath(d));
200 seat_add_to_gc_queue(device->seat);
208 sn = udev_device_get_property_value(d, "ID_SEAT");
212 if (!seat_name_is_valid(sn)) {
213 log_warning("Device with invalid seat name %s found, ignoring.", sn);
217 seat = hashmap_get(m->seats, sn);
218 master = udev_device_has_tag(d, "master-of-seat");
220 /* Ignore non-master devices for unknown seats */
221 if (!master && !seat)
224 r = manager_add_device(m, udev_device_get_syspath(d), master, &device);
229 r = manager_add_seat(m, sn, &seat);
238 device_attach(device, seat);
245 int manager_process_button_device(Manager *m, struct udev_device *d) {
252 if (streq_ptr(udev_device_get_action(d), "remove")) {
254 b = hashmap_get(m->buttons, udev_device_get_sysname(d));
263 r = manager_add_button(m, udev_device_get_sysname(d), &b);
267 sn = udev_device_get_property_value(d, "ID_SEAT");
271 button_set_seat(b, sn);
278 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
279 #if 0 /// elogind does not support systemd units, but its own session system
280 _cleanup_free_ char *unit = NULL;
282 _cleanup_free_ char *session_name = NULL;
292 #if 0 /// 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 #if 0 /// elogind does not support systemd units, but its own session system
318 _cleanup_free_ char *unit = NULL;
331 #if 0 /// elogind does not support systemd units, but its own session system
332 r = cg_pid_get_slice(pid, &unit);
336 u = hashmap_get(m->user_units, unit);
342 r = manager_get_session_by_pid (m, pid, &s);
351 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
354 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
359 idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0, NULL);
361 HASHMAP_FOREACH(s, m->sessions, i) {
365 ih = session_get_idle_hint(s, &k);
371 if (k.monotonic < ts.monotonic)
377 } else if (idle_hint) {
379 if (k.monotonic > ts.monotonic)
390 bool manager_shall_kill(Manager *m, const char *user) {
394 if (!m->kill_exclude_users && streq(user, "root"))
397 if (strv_contains(m->kill_exclude_users, user))
400 if (!strv_isempty(m->kill_only_users))
401 return strv_contains(m->kill_only_users, user);
403 return m->kill_user_processes;
406 #if 0 /// UNNEEDED by elogind
407 static int vt_is_busy(unsigned int vtnr) {
408 struct vt_stat vt_stat;
410 _cleanup_close_ int fd;
414 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
415 * we'd open the latter we'd open the foreground tty which
416 * hence would be unconditionally busy. By opening /dev/tty1
417 * we avoid this. Since tty1 is special and needs to be an
418 * explicitly loaded getty or DM this is safe. */
420 fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
424 if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
427 r = !!(vt_stat.v_state & (1 << vtnr));
432 int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
433 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
434 char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
440 if (vtnr > m->n_autovts &&
441 vtnr != m->reserve_vt)
444 if (vtnr != m->reserve_vt) {
445 /* If this is the reserved TTY, we'll start the getty
446 * on it in any case, but otherwise only if it is not
449 r = vt_is_busy(vtnr);
456 snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
457 r = sd_bus_call_method(
459 "org.freedesktop.systemd1",
460 "/org/freedesktop/systemd1",
461 "org.freedesktop.systemd1.Manager",
467 log_error("Failed to start %s: %s", name, bus_error_message(&error, r));
473 static bool manager_is_docked(Manager *m) {
477 HASHMAP_FOREACH(b, m->buttons, i)
484 static int manager_count_external_displays(Manager *m) {
485 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
486 struct udev_list_entry *item = NULL, *first = NULL;
490 e = udev_enumerate_new(m->udev);
494 r = udev_enumerate_add_match_subsystem(e, "drm");
498 r = udev_enumerate_scan_devices(e);
502 first = udev_enumerate_get_list_entry(e);
503 udev_list_entry_foreach(item, first) {
504 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
505 struct udev_device *p;
506 const char *status, *enabled, *dash, *nn, *i;
507 bool external = false;
509 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
513 p = udev_device_get_parent(d);
517 /* If the parent shares the same subsystem as the
518 * device we are looking at then it is a connector,
519 * which is what we are interested in. */
520 if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
523 nn = udev_device_get_sysname(d);
527 /* Ignore internal displays: the type is encoded in
528 * the sysfs name, as the second dash separated item
529 * (the first is the card name, the last the connector
530 * number). We implement a whitelist of external
531 * displays here, rather than a whitelist, to ensure
532 * we don't block suspends too eagerly. */
533 dash = strchr(nn, '-');
538 FOREACH_STRING(i, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
539 "Composite-", "SVIDEO-", "Component-",
540 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
542 if (startswith(dash, i)) {
550 /* Ignore ports that are not enabled */
551 enabled = udev_device_get_sysattr_value(d, "enabled");
554 if (!streq_ptr(enabled, "enabled"))
557 /* We count any connector which is not explicitly
558 * "disconnected" as connected. */
559 status = udev_device_get_sysattr_value(d, "status");
560 if (!streq_ptr(status, "disconnected"))
567 bool manager_is_docked_or_external_displays(Manager *m) {
570 /* If we are docked don't react to lid closing */
571 if (manager_is_docked(m)) {
572 log_debug("System is docked.");
576 /* If we have more than one display connected,
577 * assume that we are docked. */
578 n = manager_count_external_displays(m);
580 log_warning_errno(n, "Display counting failed: %m");
582 log_debug("External (%i) displays connected.", n);