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"
35 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
41 d = hashmap_get(m->devices, sysfs);
43 /* we support adding master-flags, but not removing them */
44 d->master = d->master || master;
46 d = device_new(m, sysfs, master);
57 int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
63 s = hashmap_get(m->seats, id);
76 int manager_add_session(Manager *m, const char *id, Session **_session) {
82 s = hashmap_get(m->sessions, id);
84 s = session_new(m, id);
95 int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) {
101 u = hashmap_get(m->users, UID_TO_PTR(uid));
103 u = user_new(m, uid, gid, name);
114 int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
122 r = get_user_creds(&name, &uid, &gid, NULL, NULL);
126 return manager_add_user(m, uid, gid, name, _user);
129 int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
137 return errno ? -errno : -ENOENT;
139 return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
142 int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
148 i = hashmap_get(m->inhibitors, id);
156 i = inhibitor_new(m, id);
166 int manager_add_button(Manager *m, const char *name, Button **_button) {
172 b = hashmap_get(m->buttons, name);
174 b = button_new(m, name);
185 int manager_watch_busname(Manager *m, const char *name) {
192 if (set_get(m->busnames, (char*) name))
199 r = set_put(m->busnames, n);
208 void manager_drop_busname(Manager *m, const char *name) {
215 /* keep it if the name still owns a controller */
216 HASHMAP_FOREACH(session, m->sessions, i)
217 if (session_is_controller(session, name))
220 free(set_remove(m->busnames, (char*) name));
223 int manager_process_seat_device(Manager *m, struct udev_device *d) {
229 if (streq_ptr(udev_device_get_action(d), "remove")) {
231 device = hashmap_get(m->devices, udev_device_get_syspath(d));
235 seat_add_to_gc_queue(device->seat);
243 sn = udev_device_get_property_value(d, "ID_SEAT");
247 if (!seat_name_is_valid(sn)) {
248 log_warning("Device with invalid seat name %s found, ignoring.", sn);
252 seat = hashmap_get(m->seats, sn);
253 master = udev_device_has_tag(d, "master-of-seat");
255 /* Ignore non-master devices for unknown seats */
256 if (!master && !seat)
259 r = manager_add_device(m, udev_device_get_syspath(d), master, &device);
264 r = manager_add_seat(m, sn, &seat);
273 device_attach(device, seat);
280 int manager_process_button_device(Manager *m, struct udev_device *d) {
287 if (streq_ptr(udev_device_get_action(d), "remove")) {
289 b = hashmap_get(m->buttons, udev_device_get_sysname(d));
298 r = manager_add_button(m, udev_device_get_sysname(d), &b);
302 sn = udev_device_get_property_value(d, "ID_SEAT");
306 button_set_seat(b, sn);
313 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
314 _cleanup_free_ char *session_name = NULL;
324 r = cg_pid_get_session(pid, &session_name);
328 s = hashmap_get(m->sessions, session_name);
336 int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
343 r = manager_get_session_by_pid (m, pid, &s);
351 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
354 dual_timestamp ts = { 0, 0 };
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_user_processes)
397 if (strv_contains(m->kill_exclude_users, user))
400 if (strv_isempty(m->kill_only_users))
403 return strv_contains(m->kill_only_users, user);
406 static int vt_is_busy(unsigned int vtnr) {
407 struct vt_stat vt_stat;
409 _cleanup_close_ int fd;
413 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
414 * we'd open the latter we'd open the foreground tty which
415 * hence would be unconditionally busy. By opening /dev/tty1
416 * we avoid this. Since tty1 is special and needs to be an
417 * explicitly loaded getty or DM this is safe. */
419 fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
423 if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
426 r = !!(vt_stat.v_state & (1 << vtnr));
431 bool manager_is_docked(Manager *m) {
435 HASHMAP_FOREACH(b, m->buttons, i)
442 int manager_count_displays(Manager *m) {
443 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
444 struct udev_list_entry *item = NULL, *first = NULL;
448 e = udev_enumerate_new(m->udev);
452 r = udev_enumerate_add_match_subsystem(e, "drm");
456 r = udev_enumerate_scan_devices(e);
460 first = udev_enumerate_get_list_entry(e);
461 udev_list_entry_foreach(item, first) {
462 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
463 struct udev_device *p;
466 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
470 p = udev_device_get_parent(d);
474 /* If the parent shares the same subsystem as the
475 * device we are looking at then it is a connector,
476 * which is what we are interested in. */
477 if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
480 /* We count any connector which is not explicitly
481 * "disconnected" as connected. */
482 status = udev_device_get_sysattr_value(d, "status");
483 if (!streq_ptr(status, "disconnected"))
490 bool manager_is_docked_or_multiple_displays(Manager *m) {
493 /* If we are docked don't react to lid closing */
494 if (manager_is_docked(m)) {
495 log_debug("System is docked.");
499 /* If we have more than one display connected,
500 * assume that we are docked. */
501 n = manager_count_displays(m);
503 log_warning_errno(n, "Display counting failed: %m");
505 log_debug("Multiple (%i) displays connected.", n);