1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 David Herrmann
7 #include <linux/input.h>
10 #include <sys/types.h>
14 #include "alloc-util.h"
17 #include "logind-session-device.h"
19 #include "parse-util.h"
20 #include "sd-daemon.h"
23 enum SessionDeviceNotifications
{
24 SESSION_DEVICE_RESUME
,
25 SESSION_DEVICE_TRY_PAUSE
,
27 SESSION_DEVICE_RELEASE
,
30 static int session_device_notify(SessionDevice
*sd
, enum SessionDeviceNotifications type
) {
31 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
32 _cleanup_free_
char *path
= NULL
;
34 uint32_t major
, minor
;
39 major
= major(sd
->dev
);
40 minor
= minor(sd
->dev
);
42 if (!sd
->session
->controller
)
45 path
= session_bus_path(sd
->session
);
49 r
= sd_bus_message_new_signal(
50 sd
->session
->manager
->bus
,
52 "org.freedesktop.login1.Session",
53 (type
== SESSION_DEVICE_RESUME
) ? "ResumeDevice" : "PauseDevice");
57 r
= sd_bus_message_set_destination(m
, sd
->session
->controller
);
63 case SESSION_DEVICE_RESUME
:
64 r
= sd_bus_message_append(m
, "uuh", major
, minor
, sd
->fd
);
69 case SESSION_DEVICE_TRY_PAUSE
:
73 case SESSION_DEVICE_PAUSE
:
77 case SESSION_DEVICE_RELEASE
:
86 r
= sd_bus_message_append(m
, "uus", major
, minor
, t
);
91 return sd_bus_send(sd
->session
->manager
->bus
, m
, NULL
);
94 static void sd_eviocrevoke(int fd
) {
95 static bool warned
= false;
99 if (ioctl(fd
, EVIOCREVOKE
, NULL
) < 0) {
101 if (errno
== EINVAL
&& !warned
) {
102 log_warning_errno(errno
, "Kernel does not support evdev-revocation: %m");
108 static int sd_drmsetmaster(int fd
) {
111 if (ioctl(fd
, DRM_IOCTL_SET_MASTER
, 0) < 0)
117 static int sd_drmdropmaster(int fd
) {
120 if (ioctl(fd
, DRM_IOCTL_DROP_MASTER
, 0) < 0)
126 static int session_device_open(SessionDevice
*sd
, bool active
) {
130 assert(sd
->type
!= DEVICE_TYPE_UNKNOWN
);
133 /* open device and try to get an udev_device from it */
134 fd
= open(sd
->node
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
140 case DEVICE_TYPE_DRM
:
142 /* Weird legacy DRM semantics might return an error even though we're master. No way to detect
143 * that so fail at all times and let caller retry in inactive state. */
144 r
= sd_drmsetmaster(fd
);
150 /* DRM-Master is granted to the first user who opens a device automatically (ughh,
151 * racy!). Hence, we just drop DRM-Master in case we were the first. */
152 (void) sd_drmdropmaster(fd
);
155 case DEVICE_TYPE_EVDEV
:
160 case DEVICE_TYPE_UNKNOWN
:
162 /* fallback for devices wihout synchronizations */
169 static int session_device_start(SessionDevice
*sd
) {
173 assert(session_is_active(sd
->session
));
180 case DEVICE_TYPE_DRM
:
182 log_error("Failed to re-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)");
186 /* Device is kept open. Simply call drmSetMaster() and hope there is no-one else. In case it fails, we
187 * keep the device paused. Maybe at some point we have a drmStealMaster(). */
188 r
= sd_drmsetmaster(sd
->fd
);
193 case DEVICE_TYPE_EVDEV
:
194 /* Evdev devices are revoked while inactive. Reopen it and we are fine. */
195 r
= session_device_open(sd
, true);
199 /* For evdev devices, the file descriptor might be left uninitialized. This might happen while resuming
200 * into a session and logind has been restarted right before. */
205 case DEVICE_TYPE_UNKNOWN
:
207 /* fallback for devices without synchronizations */
215 static void session_device_stop(SessionDevice
*sd
) {
223 case DEVICE_TYPE_DRM
:
225 log_error("Failed to de-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)");
229 /* On DRM devices we simply drop DRM-Master but keep it open.
230 * This allows the user to keep resources allocated. The
231 * CAP_SYS_ADMIN restriction to DRM-Master prevents users from
232 * circumventing this. */
233 sd_drmdropmaster(sd
->fd
);
236 case DEVICE_TYPE_EVDEV
:
237 /* Revoke access on evdev file-descriptors during deactivation.
238 * This will basically prevent any operations on the fd and
239 * cannot be undone. Good side is: it needs no CAP_SYS_ADMIN
240 * protection this way. */
241 sd_eviocrevoke(sd
->fd
);
244 case DEVICE_TYPE_UNKNOWN
:
246 /* fallback for devices without synchronization */
253 static DeviceType
detect_device_type(struct udev_device
*dev
) {
254 const char *sysname
, *subsystem
;
257 sysname
= udev_device_get_sysname(dev
);
258 subsystem
= udev_device_get_subsystem(dev
);
259 type
= DEVICE_TYPE_UNKNOWN
;
261 if (streq_ptr(subsystem
, "drm")) {
262 if (startswith(sysname
, "card"))
263 type
= DEVICE_TYPE_DRM
;
264 } else if (streq_ptr(subsystem
, "input")) {
265 if (startswith(sysname
, "event"))
266 type
= DEVICE_TYPE_EVDEV
;
272 static int session_device_verify(SessionDevice
*sd
) {
273 struct udev_device
*dev
, *p
= NULL
;
274 const char *sp
, *node
;
277 dev
= udev_device_new_from_devnum(sd
->session
->manager
->udev
, 'c', sd
->dev
);
281 sp
= udev_device_get_syspath(dev
);
282 node
= udev_device_get_devnode(dev
);
288 /* detect device type so we can find the correct sysfs parent */
289 sd
->type
= detect_device_type(dev
);
290 if (sd
->type
== DEVICE_TYPE_UNKNOWN
) {
293 } else if (sd
->type
== DEVICE_TYPE_EVDEV
) {
294 /* for evdev devices we need the parent node as device */
296 dev
= udev_device_get_parent_with_subsystem_devtype(p
, "input", NULL
);
301 sp
= udev_device_get_syspath(dev
);
302 } else if (sd
->type
!= DEVICE_TYPE_DRM
) {
303 /* Prevent opening unsupported devices. Especially devices of
304 * subsystem "input" must be opened via the evdev node as
305 * we require EVIOCREVOKE. */
310 /* search for an existing seat device and return it if available */
311 sd
->device
= hashmap_get(sd
->session
->manager
->devices
, sp
);
313 /* The caller might have gotten the udev event before we were
314 * able to process it. Hence, fake the "add" event and let the
315 * logind-manager handle the new device. */
316 r
= manager_process_seat_device(sd
->session
->manager
, dev
);
320 /* if it's still not available, then the device is invalid */
321 sd
->device
= hashmap_get(sd
->session
->manager
->devices
, sp
);
328 if (sd
->device
->seat
!= sd
->session
->seat
) {
333 sd
->node
= strdup(node
);
341 udev_device_unref(p
? : dev
);
345 int session_device_new(Session
*s
, dev_t dev
, bool open_device
, SessionDevice
**out
) {
355 sd
= new0(SessionDevice
, 1);
362 sd
->type
= DEVICE_TYPE_UNKNOWN
;
364 r
= session_device_verify(sd
);
368 r
= hashmap_put(s
->devices
, &sd
->dev
, sd
);
373 /* Open the device for the first time. We need a valid fd to pass back
374 * to the caller. If the session is not active, this _might_ immediately
375 * revoke access and thus invalidate the fd. But this is still needed
376 * to pass a valid fd back. */
377 sd
->active
= session_is_active(s
);
378 r
= session_device_open(sd
, sd
->active
);
380 /* EINVAL _may_ mean a master is active; retry inactive */
381 if (sd
->active
&& r
== -EINVAL
) {
383 r
= session_device_open(sd
, false);
391 LIST_PREPEND(sd_by_device
, sd
->device
->session_devices
, sd
);
397 hashmap_remove(s
->devices
, &sd
->dev
);
403 void session_device_free(SessionDevice
*sd
) {
406 /* Make sure to remove the pushed fd. */
408 _cleanup_free_
char *m
= NULL
;
412 /* Session ID does not contain separators. */
413 id
= sd
->session
->id
;
414 assert(*(id
+ strcspn(id
, "-\n")) == '\0');
416 r
= asprintf(&m
, "FDSTOREREMOVE=1\n"
417 "FDNAME=session-%s-device-%u-%u\n",
418 id
, major(sd
->dev
), minor(sd
->dev
));
420 (void) sd_notify(false, m
);
423 session_device_stop(sd
);
424 session_device_notify(sd
, SESSION_DEVICE_RELEASE
);
427 LIST_REMOVE(sd_by_device
, sd
->device
->session_devices
, sd
);
429 hashmap_remove(sd
->session
->devices
, &sd
->dev
);
435 void session_device_complete_pause(SessionDevice
*sd
) {
442 session_device_stop(sd
);
444 /* if not all devices are paused, wait for further completion events */
445 HASHMAP_FOREACH(iter
, sd
->session
->devices
, i
)
449 /* complete any pending session switch */
450 seat_complete_switch(sd
->session
->seat
);
453 void session_device_resume_all(Session
*s
) {
459 HASHMAP_FOREACH(sd
, s
->devices
, i
) {
463 if (session_device_start(sd
) < 0)
465 if (session_device_save(sd
) < 0)
468 session_device_notify(sd
, SESSION_DEVICE_RESUME
);
472 void session_device_pause_all(Session
*s
) {
478 HASHMAP_FOREACH(sd
, s
->devices
, i
) {
482 session_device_stop(sd
);
483 session_device_notify(sd
, SESSION_DEVICE_PAUSE
);
487 unsigned int session_device_try_pause_all(Session
*s
) {
488 unsigned num_pending
= 0;
494 HASHMAP_FOREACH(sd
, s
->devices
, i
) {
498 session_device_notify(sd
, SESSION_DEVICE_TRY_PAUSE
);
505 int session_device_save(SessionDevice
*sd
) {
506 _cleanup_free_
char *m
= NULL
;
512 /* Store device fd in PID1. It will send it back to us on restart so revocation will continue to work. To make
513 * things simple, send fds for all type of devices even if they don't support the revocation mechanism so we
514 * don't have to handle them differently later.
516 * Note: for device supporting revocation, PID1 will drop a stored fd automatically if the corresponding device
522 /* Session ID does not contain separators. */
523 id
= sd
->session
->id
;
524 assert(*(id
+ strcspn(id
, "-\n")) == '\0');
526 r
= asprintf(&m
, "FDSTORE=1\n"
527 "FDNAME=session-%s-device-%u-%u\n",
528 id
, major(sd
->dev
), minor(sd
->dev
));
532 r
= sd_pid_notify_with_fds(0, false, m
, &sd
->fd
, 1);
536 sd
->pushed_fd
= true;
540 void session_device_attach_fd(SessionDevice
*sd
, int fd
, bool active
) {
547 sd
->pushed_fd
= true;