1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 David Herrmann
7 #include <linux/input.h>
10 #include <sys/types.h>
12 #if 0 /// elogind needs the systems udev header
18 #include "alloc-util.h"
21 #include "logind-session-device.h"
23 #include "parse-util.h"
24 #include "sd-daemon.h"
27 enum SessionDeviceNotifications {
28 SESSION_DEVICE_RESUME,
29 SESSION_DEVICE_TRY_PAUSE,
31 SESSION_DEVICE_RELEASE,
34 static int session_device_notify(SessionDevice *sd, enum SessionDeviceNotifications type) {
35 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
36 _cleanup_free_ char *path = NULL;
38 uint32_t major, minor;
43 major = major(sd->dev);
44 minor = minor(sd->dev);
46 if (!sd->session->controller)
49 path = session_bus_path(sd->session);
53 r = sd_bus_message_new_signal(
54 sd->session->manager->bus,
56 "org.freedesktop.login1.Session",
57 (type == SESSION_DEVICE_RESUME) ? "ResumeDevice" : "PauseDevice");
61 r = sd_bus_message_set_destination(m, sd->session->controller);
67 case SESSION_DEVICE_RESUME:
68 r = sd_bus_message_append(m, "uuh", major, minor, sd->fd);
73 case SESSION_DEVICE_TRY_PAUSE:
77 case SESSION_DEVICE_PAUSE:
81 case SESSION_DEVICE_RELEASE:
90 r = sd_bus_message_append(m, "uus", major, minor, t);
95 return sd_bus_send(sd->session->manager->bus, m, NULL);
98 static void sd_eviocrevoke(int fd) {
99 static bool warned = false;
103 if (ioctl(fd, EVIOCREVOKE, NULL) < 0) {
105 if (errno == EINVAL && !warned) {
106 log_warning_errno(errno, "Kernel does not support evdev-revocation: %m");
112 static int sd_drmsetmaster(int fd) {
115 if (ioctl(fd, DRM_IOCTL_SET_MASTER, 0) < 0)
121 static int sd_drmdropmaster(int fd) {
124 if (ioctl(fd, DRM_IOCTL_DROP_MASTER, 0) < 0)
130 static int session_device_open(SessionDevice *sd, bool active) {
134 assert(sd->type != DEVICE_TYPE_UNKNOWN);
137 /* open device and try to get an udev_device from it */
138 fd = open(sd->node, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
144 case DEVICE_TYPE_DRM:
146 /* Weird legacy DRM semantics might return an error even though we're master. No way to detect
147 * that so fail at all times and let caller retry in inactive state. */
148 r = sd_drmsetmaster(fd);
154 /* DRM-Master is granted to the first user who opens a device automatically (ughh,
155 * racy!). Hence, we just drop DRM-Master in case we were the first. */
156 (void) sd_drmdropmaster(fd);
159 case DEVICE_TYPE_EVDEV:
164 case DEVICE_TYPE_UNKNOWN:
166 /* fallback for devices wihout synchronizations */
173 static int session_device_start(SessionDevice *sd) {
177 assert(session_is_active(sd->session));
184 case DEVICE_TYPE_DRM:
186 log_error("Failed to re-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)");
190 /* Device is kept open. Simply call drmSetMaster() and hope there is no-one else. In case it fails, we
191 * keep the device paused. Maybe at some point we have a drmStealMaster(). */
192 r = sd_drmsetmaster(sd->fd);
197 case DEVICE_TYPE_EVDEV:
198 /* Evdev devices are revoked while inactive. Reopen it and we are fine. */
199 r = session_device_open(sd, true);
203 /* For evdev devices, the file descriptor might be left uninitialized. This might happen while resuming
204 * into a session and logind has been restarted right before. */
209 case DEVICE_TYPE_UNKNOWN:
211 /* fallback for devices without synchronizations */
219 static void session_device_stop(SessionDevice *sd) {
227 case DEVICE_TYPE_DRM:
229 log_error("Failed to de-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)");
233 /* On DRM devices we simply drop DRM-Master but keep it open.
234 * This allows the user to keep resources allocated. The
235 * CAP_SYS_ADMIN restriction to DRM-Master prevents users from
236 * circumventing this. */
237 sd_drmdropmaster(sd->fd);
240 case DEVICE_TYPE_EVDEV:
241 /* Revoke access on evdev file-descriptors during deactivation.
242 * This will basically prevent any operations on the fd and
243 * cannot be undone. Good side is: it needs no CAP_SYS_ADMIN
244 * protection this way. */
245 sd_eviocrevoke(sd->fd);
248 case DEVICE_TYPE_UNKNOWN:
250 /* fallback for devices without synchronization */
257 static DeviceType detect_device_type(struct udev_device *dev) {
258 const char *sysname, *subsystem;
261 sysname = udev_device_get_sysname(dev);
262 subsystem = udev_device_get_subsystem(dev);
263 type = DEVICE_TYPE_UNKNOWN;
265 if (streq_ptr(subsystem, "drm")) {
266 if (startswith(sysname, "card"))
267 type = DEVICE_TYPE_DRM;
268 } else if (streq_ptr(subsystem, "input")) {
269 if (startswith(sysname, "event"))
270 type = DEVICE_TYPE_EVDEV;
276 static int session_device_verify(SessionDevice *sd) {
277 struct udev_device *dev, *p = NULL;
278 const char *sp, *node;
281 dev = udev_device_new_from_devnum(sd->session->manager->udev, 'c', sd->dev);
285 sp = udev_device_get_syspath(dev);
286 node = udev_device_get_devnode(dev);
292 /* detect device type so we can find the correct sysfs parent */
293 sd->type = detect_device_type(dev);
294 if (sd->type == DEVICE_TYPE_UNKNOWN) {
297 } else if (sd->type == DEVICE_TYPE_EVDEV) {
298 /* for evdev devices we need the parent node as device */
300 dev = udev_device_get_parent_with_subsystem_devtype(p, "input", NULL);
305 sp = udev_device_get_syspath(dev);
306 } else if (sd->type != DEVICE_TYPE_DRM) {
307 /* Prevent opening unsupported devices. Especially devices of
308 * subsystem "input" must be opened via the evdev node as
309 * we require EVIOCREVOKE. */
314 /* search for an existing seat device and return it if available */
315 sd->device = hashmap_get(sd->session->manager->devices, sp);
317 /* The caller might have gotten the udev event before we were
318 * able to process it. Hence, fake the "add" event and let the
319 * logind-manager handle the new device. */
320 r = manager_process_seat_device(sd->session->manager, dev);
324 /* if it's still not available, then the device is invalid */
325 sd->device = hashmap_get(sd->session->manager->devices, sp);
332 if (sd->device->seat != sd->session->seat) {
337 sd->node = strdup(node);
345 udev_device_unref(p ? : dev);
349 int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **out) {
359 sd = new0(SessionDevice, 1);
366 sd->type = DEVICE_TYPE_UNKNOWN;
368 r = session_device_verify(sd);
372 r = hashmap_put(s->devices, &sd->dev, sd);
377 /* Open the device for the first time. We need a valid fd to pass back
378 * to the caller. If the session is not active, this _might_ immediately
379 * revoke access and thus invalidate the fd. But this is still needed
380 * to pass a valid fd back. */
381 sd->active = session_is_active(s);
382 r = session_device_open(sd, sd->active);
384 /* EINVAL _may_ mean a master is active; retry inactive */
385 if (sd->active && r == -EINVAL) {
387 r = session_device_open(sd, false);
395 LIST_PREPEND(sd_by_device, sd->device->session_devices, sd);
401 hashmap_remove(s->devices, &sd->dev);
407 void session_device_free(SessionDevice *sd) {
410 /* Make sure to remove the pushed fd. */
412 _cleanup_free_ char *m = NULL;
416 /* Session ID does not contain separators. */
417 id = sd->session->id;
418 assert(*(id + strcspn(id, "-\n")) == '\0');
420 r = asprintf(&m, "FDSTOREREMOVE=1\n"
421 "FDNAME=session-%s-device-%u-%u\n",
422 id, major(sd->dev), minor(sd->dev));
424 (void) sd_notify(false, m);
427 session_device_stop(sd);
428 session_device_notify(sd, SESSION_DEVICE_RELEASE);
431 LIST_REMOVE(sd_by_device, sd->device->session_devices, sd);
433 hashmap_remove(sd->session->devices, &sd->dev);
439 void session_device_complete_pause(SessionDevice *sd) {
446 session_device_stop(sd);
448 /* if not all devices are paused, wait for further completion events */
449 HASHMAP_FOREACH(iter, sd->session->devices, i)
453 /* complete any pending session switch */
454 seat_complete_switch(sd->session->seat);
457 void session_device_resume_all(Session *s) {
463 HASHMAP_FOREACH(sd, s->devices, i) {
467 if (session_device_start(sd) < 0)
469 if (session_device_save(sd) < 0)
472 session_device_notify(sd, SESSION_DEVICE_RESUME);
476 void session_device_pause_all(Session *s) {
482 HASHMAP_FOREACH(sd, s->devices, i) {
486 session_device_stop(sd);
487 session_device_notify(sd, SESSION_DEVICE_PAUSE);
491 unsigned int session_device_try_pause_all(Session *s) {
492 unsigned num_pending = 0;
498 HASHMAP_FOREACH(sd, s->devices, i) {
502 session_device_notify(sd, SESSION_DEVICE_TRY_PAUSE);
509 int session_device_save(SessionDevice *sd) {
510 _cleanup_free_ char *m = NULL;
516 /* Store device fd in PID1. It will send it back to us on restart so revocation will continue to work. To make
517 * things simple, send fds for all type of devices even if they don't support the revocation mechanism so we
518 * don't have to handle them differently later.
520 * Note: for device supporting revocation, PID1 will drop a stored fd automatically if the corresponding device
526 /* Session ID does not contain separators. */
527 id = sd->session->id;
528 assert(*(id + strcspn(id, "-\n")) == '\0');
530 r = asprintf(&m, "FDSTORE=1\n"
531 "FDNAME=session-%s-device-%u-%u\n",
532 id, major(sd->dev), minor(sd->dev));
536 r = sd_pid_notify_with_fds(0, false, m, &sd->fd, 1);
540 sd->pushed_fd = true;
544 void session_device_attach_fd(SessionDevice *sd, int fd, bool active) {
551 sd->pushed_fd = true;