1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/input.h>
11 #include "alloc-util.h"
14 #include "logind-session-device.h"
16 #include "parse-util.h"
17 #include "sd-daemon.h"
20 enum SessionDeviceNotifications
{
21 SESSION_DEVICE_RESUME
,
22 SESSION_DEVICE_TRY_PAUSE
,
24 SESSION_DEVICE_RELEASE
,
27 static int session_device_notify(SessionDevice
*sd
, enum SessionDeviceNotifications type
) {
28 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
29 _cleanup_free_
char *path
= NULL
;
31 uint32_t major
, minor
;
36 major
= major(sd
->dev
);
37 minor
= minor(sd
->dev
);
39 if (!sd
->session
->controller
)
42 path
= session_bus_path(sd
->session
);
46 r
= sd_bus_message_new_signal(
47 sd
->session
->manager
->bus
,
49 "org.freedesktop.login1.Session",
50 (type
== SESSION_DEVICE_RESUME
) ? "ResumeDevice" : "PauseDevice");
54 r
= sd_bus_message_set_destination(m
, sd
->session
->controller
);
60 case SESSION_DEVICE_RESUME
:
61 r
= sd_bus_message_append(m
, "uuh", major
, minor
, sd
->fd
);
66 case SESSION_DEVICE_TRY_PAUSE
:
70 case SESSION_DEVICE_PAUSE
:
74 case SESSION_DEVICE_RELEASE
:
83 r
= sd_bus_message_append(m
, "uus", major
, minor
, t
);
88 return sd_bus_send(sd
->session
->manager
->bus
, m
, NULL
);
91 static void sd_eviocrevoke(int fd
) {
92 static bool warned
= false;
96 if (ioctl(fd
, EVIOCREVOKE
, NULL
) < 0) {
98 if (errno
== EINVAL
&& !warned
) {
99 log_warning_errno(errno
, "Kernel does not support evdev-revocation: %m");
105 static int sd_drmsetmaster(int fd
) {
108 if (ioctl(fd
, DRM_IOCTL_SET_MASTER
, 0) < 0)
114 static int sd_drmdropmaster(int fd
) {
117 if (ioctl(fd
, DRM_IOCTL_DROP_MASTER
, 0) < 0)
123 static int session_device_open(SessionDevice
*sd
, bool active
) {
127 assert(sd
->type
!= DEVICE_TYPE_UNKNOWN
);
130 /* open device and try to get an udev_device from it */
131 fd
= open(sd
->node
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
137 case DEVICE_TYPE_DRM
:
139 /* Weird legacy DRM semantics might return an error even though we're master. No way to detect
140 * that so fail at all times and let caller retry in inactive state. */
141 r
= sd_drmsetmaster(fd
);
147 /* DRM-Master is granted to the first user who opens a device automatically (ughh,
148 * racy!). Hence, we just drop DRM-Master in case we were the first. */
149 (void) sd_drmdropmaster(fd
);
152 case DEVICE_TYPE_EVDEV
:
157 case DEVICE_TYPE_UNKNOWN
:
159 /* fallback for devices wihout synchronizations */
166 static int session_device_start(SessionDevice
*sd
) {
170 assert(session_is_active(sd
->session
));
177 case DEVICE_TYPE_DRM
:
179 return log_error_errno(SYNTHETIC_ERRNO(EBADF
),
180 "Failed to re-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)");
182 /* Device is kept open. Simply call drmSetMaster() and hope there is no-one else. In case it fails, we
183 * keep the device paused. Maybe at some point we have a drmStealMaster(). */
184 r
= sd_drmsetmaster(sd
->fd
);
189 case DEVICE_TYPE_EVDEV
:
190 /* Evdev devices are revoked while inactive. Reopen it and we are fine. */
191 r
= session_device_open(sd
, true);
195 /* For evdev devices, the file descriptor might be left uninitialized. This might happen while resuming
196 * into a session and logind has been restarted right before. */
201 case DEVICE_TYPE_UNKNOWN
:
203 /* fallback for devices without synchronizations */
211 static void session_device_stop(SessionDevice
*sd
) {
219 case DEVICE_TYPE_DRM
:
221 log_error("Failed to de-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)");
225 /* On DRM devices we simply drop DRM-Master but keep it open.
226 * This allows the user to keep resources allocated. The
227 * CAP_SYS_ADMIN restriction to DRM-Master prevents users from
228 * circumventing this. */
229 sd_drmdropmaster(sd
->fd
);
232 case DEVICE_TYPE_EVDEV
:
233 /* Revoke access on evdev file-descriptors during deactivation.
234 * This will basically prevent any operations on the fd and
235 * cannot be undone. Good side is: it needs no CAP_SYS_ADMIN
236 * protection this way. */
237 sd_eviocrevoke(sd
->fd
);
240 case DEVICE_TYPE_UNKNOWN
:
242 /* fallback for devices without synchronization */
249 static DeviceType
detect_device_type(sd_device
*dev
) {
250 const char *sysname
, *subsystem
;
251 DeviceType type
= DEVICE_TYPE_UNKNOWN
;
253 if (sd_device_get_sysname(dev
, &sysname
) < 0 ||
254 sd_device_get_subsystem(dev
, &subsystem
) < 0)
257 if (streq(subsystem
, "drm")) {
258 if (startswith(sysname
, "card"))
259 type
= DEVICE_TYPE_DRM
;
260 } else if (streq(subsystem
, "input")) {
261 if (startswith(sysname
, "event"))
262 type
= DEVICE_TYPE_EVDEV
;
268 static int session_device_verify(SessionDevice
*sd
) {
269 _cleanup_(sd_device_unrefp
) sd_device
*p
= NULL
;
270 const char *sp
, *node
;
274 r
= sd_device_new_from_devnum(&p
, 'c', sd
->dev
);
280 if (sd_device_get_syspath(dev
, &sp
) < 0 ||
281 sd_device_get_devname(dev
, &node
) < 0)
284 /* detect device type so we can find the correct sysfs parent */
285 sd
->type
= detect_device_type(dev
);
286 if (sd
->type
== DEVICE_TYPE_UNKNOWN
)
289 else if (sd
->type
== DEVICE_TYPE_EVDEV
) {
290 /* for evdev devices we need the parent node as device */
291 if (sd_device_get_parent_with_subsystem_devtype(p
, "input", NULL
, &dev
) < 0)
293 if (sd_device_get_syspath(dev
, &sp
) < 0)
296 } else if (sd
->type
!= DEVICE_TYPE_DRM
)
297 /* Prevent opening unsupported devices. Especially devices of
298 * subsystem "input" must be opened via the evdev node as
299 * we require EVIOCREVOKE. */
302 /* search for an existing seat device and return it if available */
303 sd
->device
= hashmap_get(sd
->session
->manager
->devices
, sp
);
305 /* The caller might have gotten the udev event before we were
306 * able to process it. Hence, fake the "add" event and let the
307 * logind-manager handle the new device. */
308 r
= manager_process_seat_device(sd
->session
->manager
, dev
);
312 /* if it's still not available, then the device is invalid */
313 sd
->device
= hashmap_get(sd
->session
->manager
->devices
, sp
);
318 if (sd
->device
->seat
!= sd
->session
->seat
)
321 sd
->node
= strdup(node
);
328 int session_device_new(Session
*s
, dev_t dev
, bool open_device
, SessionDevice
**out
) {
338 sd
= new0(SessionDevice
, 1);
345 sd
->type
= DEVICE_TYPE_UNKNOWN
;
347 r
= session_device_verify(sd
);
351 r
= hashmap_put(s
->devices
, &sd
->dev
, sd
);
356 /* Open the device for the first time. We need a valid fd to pass back
357 * to the caller. If the session is not active, this _might_ immediately
358 * revoke access and thus invalidate the fd. But this is still needed
359 * to pass a valid fd back. */
360 sd
->active
= session_is_active(s
);
361 r
= session_device_open(sd
, sd
->active
);
363 /* EINVAL _may_ mean a master is active; retry inactive */
364 if (sd
->active
&& r
== -EINVAL
) {
366 r
= session_device_open(sd
, false);
374 LIST_PREPEND(sd_by_device
, sd
->device
->session_devices
, sd
);
380 hashmap_remove(s
->devices
, &sd
->dev
);
386 void session_device_free(SessionDevice
*sd
) {
389 /* Make sure to remove the pushed fd. */
391 _cleanup_free_
char *m
= NULL
;
395 /* Session ID does not contain separators. */
396 id
= sd
->session
->id
;
397 assert(*(id
+ strcspn(id
, "-\n")) == '\0');
399 r
= asprintf(&m
, "FDSTOREREMOVE=1\n"
400 "FDNAME=session-%s-device-%u-%u\n",
401 id
, major(sd
->dev
), minor(sd
->dev
));
403 (void) sd_notify(false, m
);
406 session_device_stop(sd
);
407 session_device_notify(sd
, SESSION_DEVICE_RELEASE
);
410 LIST_REMOVE(sd_by_device
, sd
->device
->session_devices
, sd
);
412 hashmap_remove(sd
->session
->devices
, &sd
->dev
);
418 void session_device_complete_pause(SessionDevice
*sd
) {
425 session_device_stop(sd
);
427 /* if not all devices are paused, wait for further completion events */
428 HASHMAP_FOREACH(iter
, sd
->session
->devices
, i
)
432 /* complete any pending session switch */
433 seat_complete_switch(sd
->session
->seat
);
436 void session_device_resume_all(Session
*s
) {
442 HASHMAP_FOREACH(sd
, s
->devices
, i
) {
446 if (session_device_start(sd
) < 0)
448 if (session_device_save(sd
) < 0)
451 session_device_notify(sd
, SESSION_DEVICE_RESUME
);
455 void session_device_pause_all(Session
*s
) {
461 HASHMAP_FOREACH(sd
, s
->devices
, i
) {
465 session_device_stop(sd
);
466 session_device_notify(sd
, SESSION_DEVICE_PAUSE
);
470 unsigned session_device_try_pause_all(Session
*s
) {
471 unsigned num_pending
= 0;
477 HASHMAP_FOREACH(sd
, s
->devices
, i
) {
481 session_device_notify(sd
, SESSION_DEVICE_TRY_PAUSE
);
488 int session_device_save(SessionDevice
*sd
) {
489 _cleanup_free_
char *m
= NULL
;
495 /* Store device fd in PID1. It will send it back to us on restart so revocation will continue to work. To make
496 * things simple, send fds for all type of devices even if they don't support the revocation mechanism so we
497 * don't have to handle them differently later.
499 * Note: for device supporting revocation, PID1 will drop a stored fd automatically if the corresponding device
505 /* Session ID does not contain separators. */
506 id
= sd
->session
->id
;
507 assert(*(id
+ strcspn(id
, "-\n")) == '\0');
509 r
= asprintf(&m
, "FDSTORE=1\n"
510 "FDNAME=session-%s-device-%u-%u\n",
511 id
, major(sd
->dev
), minor(sd
->dev
));
515 r
= sd_pid_notify_with_fds(0, false, m
, &sd
->fd
, 1);
519 sd
->pushed_fd
= true;
523 void session_device_attach_fd(SessionDevice
*sd
, int fd
, bool active
) {
530 sd
->pushed_fd
= true;