]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-session-device.c
19ad889b3a6b271d186a3f7f66133ee95a1ec5e6
[thirdparty/systemd.git] / src / login / logind-session-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2013 David Herrmann
4 ***/
5
6 #include <fcntl.h>
7 #include <linux/input.h>
8 #include <string.h>
9 #include <sys/ioctl.h>
10 #include <sys/types.h>
11
12 #include "libudev.h"
13
14 #include "alloc-util.h"
15 #include "bus-util.h"
16 #include "fd-util.h"
17 #include "logind-session-device.h"
18 #include "missing.h"
19 #include "parse-util.h"
20 #include "sd-daemon.h"
21 #include "util.h"
22
23 enum SessionDeviceNotifications {
24 SESSION_DEVICE_RESUME,
25 SESSION_DEVICE_TRY_PAUSE,
26 SESSION_DEVICE_PAUSE,
27 SESSION_DEVICE_RELEASE,
28 };
29
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;
33 const char *t = NULL;
34 uint32_t major, minor;
35 int r;
36
37 assert(sd);
38
39 major = major(sd->dev);
40 minor = minor(sd->dev);
41
42 if (!sd->session->controller)
43 return 0;
44
45 path = session_bus_path(sd->session);
46 if (!path)
47 return -ENOMEM;
48
49 r = sd_bus_message_new_signal(
50 sd->session->manager->bus,
51 &m, path,
52 "org.freedesktop.login1.Session",
53 (type == SESSION_DEVICE_RESUME) ? "ResumeDevice" : "PauseDevice");
54 if (!m)
55 return r;
56
57 r = sd_bus_message_set_destination(m, sd->session->controller);
58 if (r < 0)
59 return r;
60
61 switch (type) {
62
63 case SESSION_DEVICE_RESUME:
64 r = sd_bus_message_append(m, "uuh", major, minor, sd->fd);
65 if (r < 0)
66 return r;
67 break;
68
69 case SESSION_DEVICE_TRY_PAUSE:
70 t = "pause";
71 break;
72
73 case SESSION_DEVICE_PAUSE:
74 t = "force";
75 break;
76
77 case SESSION_DEVICE_RELEASE:
78 t = "gone";
79 break;
80
81 default:
82 return -EINVAL;
83 }
84
85 if (t) {
86 r = sd_bus_message_append(m, "uus", major, minor, t);
87 if (r < 0)
88 return r;
89 }
90
91 return sd_bus_send(sd->session->manager->bus, m, NULL);
92 }
93
94 static void sd_eviocrevoke(int fd) {
95 static bool warned = false;
96
97 assert(fd >= 0);
98
99 if (ioctl(fd, EVIOCREVOKE, NULL) < 0) {
100
101 if (errno == EINVAL && !warned) {
102 log_warning_errno(errno, "Kernel does not support evdev-revocation: %m");
103 warned = true;
104 }
105 }
106 }
107
108 static int sd_drmsetmaster(int fd) {
109 assert(fd >= 0);
110
111 if (ioctl(fd, DRM_IOCTL_SET_MASTER, 0) < 0)
112 return -errno;
113
114 return 0;
115 }
116
117 static int sd_drmdropmaster(int fd) {
118 assert(fd >= 0);
119
120 if (ioctl(fd, DRM_IOCTL_DROP_MASTER, 0) < 0)
121 return -errno;
122
123 return 0;
124 }
125
126 static int session_device_open(SessionDevice *sd, bool active) {
127 int fd, r;
128
129 assert(sd);
130 assert(sd->type != DEVICE_TYPE_UNKNOWN);
131 assert(sd->node);
132
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);
135 if (fd < 0)
136 return -errno;
137
138 switch (sd->type) {
139
140 case DEVICE_TYPE_DRM:
141 if (active) {
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);
145 if (r < 0) {
146 close_nointr(fd);
147 return r;
148 }
149 } else
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);
153 break;
154
155 case DEVICE_TYPE_EVDEV:
156 if (!active)
157 sd_eviocrevoke(fd);
158 break;
159
160 case DEVICE_TYPE_UNKNOWN:
161 default:
162 /* fallback for devices wihout synchronizations */
163 break;
164 }
165
166 return fd;
167 }
168
169 static int session_device_start(SessionDevice *sd) {
170 int r;
171
172 assert(sd);
173 assert(session_is_active(sd->session));
174
175 if (sd->active)
176 return 0;
177
178 switch (sd->type) {
179
180 case DEVICE_TYPE_DRM:
181 if (sd->fd < 0) {
182 log_error("Failed to re-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)");
183 return -EBADF;
184 }
185
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);
189 if (r < 0)
190 return r;
191 break;
192
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);
196 if (r < 0)
197 return r;
198
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. */
201 safe_close(sd->fd);
202 sd->fd = r;
203 break;
204
205 case DEVICE_TYPE_UNKNOWN:
206 default:
207 /* fallback for devices without synchronizations */
208 break;
209 }
210
211 sd->active = true;
212 return 0;
213 }
214
215 static void session_device_stop(SessionDevice *sd) {
216 assert(sd);
217
218 if (!sd->active)
219 return;
220
221 switch (sd->type) {
222
223 case DEVICE_TYPE_DRM:
224 if (sd->fd < 0) {
225 log_error("Failed to de-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)");
226 return;
227 }
228
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);
234 break;
235
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);
242 break;
243
244 case DEVICE_TYPE_UNKNOWN:
245 default:
246 /* fallback for devices without synchronization */
247 break;
248 }
249
250 sd->active = false;
251 }
252
253 static DeviceType detect_device_type(struct udev_device *dev) {
254 const char *sysname, *subsystem;
255 DeviceType type;
256
257 sysname = udev_device_get_sysname(dev);
258 subsystem = udev_device_get_subsystem(dev);
259 type = DEVICE_TYPE_UNKNOWN;
260
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;
267 }
268
269 return type;
270 }
271
272 static int session_device_verify(SessionDevice *sd) {
273 struct udev_device *dev, *p = NULL;
274 const char *sp, *node;
275 int r;
276
277 dev = udev_device_new_from_devnum(sd->session->manager->udev, 'c', sd->dev);
278 if (!dev)
279 return -ENODEV;
280
281 sp = udev_device_get_syspath(dev);
282 node = udev_device_get_devnode(dev);
283 if (!node) {
284 r = -EINVAL;
285 goto err_dev;
286 }
287
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) {
291 r = -ENODEV;
292 goto err_dev;
293 } else if (sd->type == DEVICE_TYPE_EVDEV) {
294 /* for evdev devices we need the parent node as device */
295 p = dev;
296 dev = udev_device_get_parent_with_subsystem_devtype(p, "input", NULL);
297 if (!dev) {
298 r = -ENODEV;
299 goto err_dev;
300 }
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. */
306 r = -ENODEV;
307 goto err_dev;
308 }
309
310 /* search for an existing seat device and return it if available */
311 sd->device = hashmap_get(sd->session->manager->devices, sp);
312 if (!sd->device) {
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);
317 if (r < 0)
318 goto err_dev;
319
320 /* if it's still not available, then the device is invalid */
321 sd->device = hashmap_get(sd->session->manager->devices, sp);
322 if (!sd->device) {
323 r = -ENODEV;
324 goto err_dev;
325 }
326 }
327
328 if (sd->device->seat != sd->session->seat) {
329 r = -EPERM;
330 goto err_dev;
331 }
332
333 sd->node = strdup(node);
334 if (!sd->node) {
335 r = -ENOMEM;
336 goto err_dev;
337 }
338
339 r = 0;
340 err_dev:
341 udev_device_unref(p ? : dev);
342 return r;
343 }
344
345 int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **out) {
346 SessionDevice *sd;
347 int r;
348
349 assert(s);
350 assert(out);
351
352 if (!s->seat)
353 return -EPERM;
354
355 sd = new0(SessionDevice, 1);
356 if (!sd)
357 return -ENOMEM;
358
359 sd->session = s;
360 sd->dev = dev;
361 sd->fd = -1;
362 sd->type = DEVICE_TYPE_UNKNOWN;
363
364 r = session_device_verify(sd);
365 if (r < 0)
366 goto error;
367
368 r = hashmap_put(s->devices, &sd->dev, sd);
369 if (r < 0)
370 goto error;
371
372 if (open_device) {
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);
379 if (r < 0) {
380 /* EINVAL _may_ mean a master is active; retry inactive */
381 if (sd->active && r == -EINVAL) {
382 sd->active = false;
383 r = session_device_open(sd, false);
384 }
385 if (r < 0)
386 goto error;
387 }
388 sd->fd = r;
389 }
390
391 LIST_PREPEND(sd_by_device, sd->device->session_devices, sd);
392
393 *out = sd;
394 return 0;
395
396 error:
397 hashmap_remove(s->devices, &sd->dev);
398 free(sd->node);
399 free(sd);
400 return r;
401 }
402
403 void session_device_free(SessionDevice *sd) {
404 assert(sd);
405
406 /* Make sure to remove the pushed fd. */
407 if (sd->pushed_fd) {
408 _cleanup_free_ char *m = NULL;
409 const char *id;
410 int r;
411
412 /* Session ID does not contain separators. */
413 id = sd->session->id;
414 assert(*(id + strcspn(id, "-\n")) == '\0');
415
416 r = asprintf(&m, "FDSTOREREMOVE=1\n"
417 "FDNAME=session-%s-device-%u-%u\n",
418 id, major(sd->dev), minor(sd->dev));
419 if (r >= 0)
420 (void) sd_notify(false, m);
421 }
422
423 session_device_stop(sd);
424 session_device_notify(sd, SESSION_DEVICE_RELEASE);
425 safe_close(sd->fd);
426
427 LIST_REMOVE(sd_by_device, sd->device->session_devices, sd);
428
429 hashmap_remove(sd->session->devices, &sd->dev);
430
431 free(sd->node);
432 free(sd);
433 }
434
435 void session_device_complete_pause(SessionDevice *sd) {
436 SessionDevice *iter;
437 Iterator i;
438
439 if (!sd->active)
440 return;
441
442 session_device_stop(sd);
443
444 /* if not all devices are paused, wait for further completion events */
445 HASHMAP_FOREACH(iter, sd->session->devices, i)
446 if (iter->active)
447 return;
448
449 /* complete any pending session switch */
450 seat_complete_switch(sd->session->seat);
451 }
452
453 void session_device_resume_all(Session *s) {
454 SessionDevice *sd;
455 Iterator i;
456
457 assert(s);
458
459 HASHMAP_FOREACH(sd, s->devices, i) {
460 if (sd->active)
461 continue;
462
463 if (session_device_start(sd) < 0)
464 continue;
465 if (session_device_save(sd) < 0)
466 continue;
467
468 session_device_notify(sd, SESSION_DEVICE_RESUME);
469 }
470 }
471
472 void session_device_pause_all(Session *s) {
473 SessionDevice *sd;
474 Iterator i;
475
476 assert(s);
477
478 HASHMAP_FOREACH(sd, s->devices, i) {
479 if (!sd->active)
480 continue;
481
482 session_device_stop(sd);
483 session_device_notify(sd, SESSION_DEVICE_PAUSE);
484 }
485 }
486
487 unsigned int session_device_try_pause_all(Session *s) {
488 unsigned num_pending = 0;
489 SessionDevice *sd;
490 Iterator i;
491
492 assert(s);
493
494 HASHMAP_FOREACH(sd, s->devices, i) {
495 if (!sd->active)
496 continue;
497
498 session_device_notify(sd, SESSION_DEVICE_TRY_PAUSE);
499 num_pending++;
500 }
501
502 return num_pending;
503 }
504
505 int session_device_save(SessionDevice *sd) {
506 _cleanup_free_ char *m = NULL;
507 const char *id;
508 int r;
509
510 assert(sd);
511
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.
515 *
516 * Note: for device supporting revocation, PID1 will drop a stored fd automatically if the corresponding device
517 * is revoked. */
518
519 if (sd->pushed_fd)
520 return 0;
521
522 /* Session ID does not contain separators. */
523 id = sd->session->id;
524 assert(*(id + strcspn(id, "-\n")) == '\0');
525
526 r = asprintf(&m, "FDSTORE=1\n"
527 "FDNAME=session-%s-device-%u-%u\n",
528 id, major(sd->dev), minor(sd->dev));
529 if (r < 0)
530 return r;
531
532 r = sd_pid_notify_with_fds(0, false, m, &sd->fd, 1);
533 if (r < 0)
534 return r;
535
536 sd->pushed_fd = true;
537 return 1;
538 }
539
540 void session_device_attach_fd(SessionDevice *sd, int fd, bool active) {
541 assert(fd >= 0);
542 assert(sd);
543 assert(sd->fd < 0);
544 assert(!sd->active);
545
546 sd->fd = fd;
547 sd->pushed_fd = true;
548 sd->active = active;
549 }