]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
118ecf32 DH |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 David Herrmann | |
118ecf32 DH |
6 | ***/ |
7 | ||
118ecf32 | 8 | #include <fcntl.h> |
118ecf32 | 9 | #include <linux/input.h> |
118ecf32 DH |
10 | #include <string.h> |
11 | #include <sys/ioctl.h> | |
118ecf32 | 12 | #include <sys/types.h> |
118ecf32 | 13 | |
b4bbcaa9 TA |
14 | #include "libudev.h" |
15 | ||
b5efdb8a | 16 | #include "alloc-util.h" |
cc377381 | 17 | #include "bus-util.h" |
3ffd4af2 | 18 | #include "fd-util.h" |
cc377381 | 19 | #include "logind-session-device.h" |
3ffd4af2 | 20 | #include "missing.h" |
aed24c4c FB |
21 | #include "parse-util.h" |
22 | #include "sd-daemon.h" | |
3ffd4af2 | 23 | #include "util.h" |
118ecf32 DH |
24 | |
25 | enum SessionDeviceNotifications { | |
26 | SESSION_DEVICE_RESUME, | |
27 | SESSION_DEVICE_TRY_PAUSE, | |
28 | SESSION_DEVICE_PAUSE, | |
29 | SESSION_DEVICE_RELEASE, | |
30 | }; | |
31 | ||
cc377381 | 32 | static int session_device_notify(SessionDevice *sd, enum SessionDeviceNotifications type) { |
4afd3348 | 33 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
118ecf32 DH |
34 | _cleanup_free_ char *path = NULL; |
35 | const char *t = NULL; | |
081dfa85 | 36 | uint32_t major, minor; |
cc377381 | 37 | int r; |
118ecf32 DH |
38 | |
39 | assert(sd); | |
40 | ||
081dfa85 DH |
41 | major = major(sd->dev); |
42 | minor = minor(sd->dev); | |
43 | ||
118ecf32 | 44 | if (!sd->session->controller) |
cc377381 | 45 | return 0; |
118ecf32 DH |
46 | |
47 | path = session_bus_path(sd->session); | |
48 | if (!path) | |
cc377381 | 49 | return -ENOMEM; |
118ecf32 | 50 | |
cc377381 | 51 | r = sd_bus_message_new_signal( |
151b9b96 LP |
52 | sd->session->manager->bus, |
53 | &m, path, | |
cc377381 | 54 | "org.freedesktop.login1.Session", |
151b9b96 | 55 | (type == SESSION_DEVICE_RESUME) ? "ResumeDevice" : "PauseDevice"); |
118ecf32 | 56 | if (!m) |
cc377381 | 57 | return r; |
118ecf32 | 58 | |
cc377381 LP |
59 | r = sd_bus_message_set_destination(m, sd->session->controller); |
60 | if (r < 0) | |
61 | return r; | |
118ecf32 DH |
62 | |
63 | switch (type) { | |
864fe630 | 64 | |
118ecf32 | 65 | case SESSION_DEVICE_RESUME: |
cc377381 LP |
66 | r = sd_bus_message_append(m, "uuh", major, minor, sd->fd); |
67 | if (r < 0) | |
68 | return r; | |
118ecf32 | 69 | break; |
864fe630 | 70 | |
118ecf32 DH |
71 | case SESSION_DEVICE_TRY_PAUSE: |
72 | t = "pause"; | |
73 | break; | |
864fe630 | 74 | |
118ecf32 DH |
75 | case SESSION_DEVICE_PAUSE: |
76 | t = "force"; | |
77 | break; | |
864fe630 | 78 | |
118ecf32 DH |
79 | case SESSION_DEVICE_RELEASE: |
80 | t = "gone"; | |
81 | break; | |
864fe630 | 82 | |
118ecf32 | 83 | default: |
cc377381 | 84 | return -EINVAL; |
118ecf32 DH |
85 | } |
86 | ||
cc377381 LP |
87 | if (t) { |
88 | r = sd_bus_message_append(m, "uus", major, minor, t); | |
89 | if (r < 0) | |
90 | return r; | |
91 | } | |
118ecf32 | 92 | |
cc377381 | 93 | return sd_bus_send(sd->session->manager->bus, m, NULL); |
118ecf32 DH |
94 | } |
95 | ||
a3ddf73c | 96 | static void sd_eviocrevoke(int fd) { |
5d5330a8 | 97 | static bool warned = false; |
118ecf32 DH |
98 | |
99 | assert(fd >= 0); | |
100 | ||
5d5330a8 LP |
101 | if (ioctl(fd, EVIOCREVOKE, NULL) < 0) { |
102 | ||
103 | if (errno == EINVAL && !warned) { | |
104 | log_warning_errno(errno, "Kernel does not support evdev-revocation: %m"); | |
118ecf32 | 105 | warned = true; |
118ecf32 DH |
106 | } |
107 | } | |
118ecf32 DH |
108 | } |
109 | ||
110 | static int sd_drmsetmaster(int fd) { | |
118ecf32 DH |
111 | assert(fd >= 0); |
112 | ||
864fe630 | 113 | if (ioctl(fd, DRM_IOCTL_SET_MASTER, 0) < 0) |
118ecf32 DH |
114 | return -errno; |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
119 | static int sd_drmdropmaster(int fd) { | |
118ecf32 DH |
120 | assert(fd >= 0); |
121 | ||
864fe630 | 122 | if (ioctl(fd, DRM_IOCTL_DROP_MASTER, 0) < 0) |
118ecf32 DH |
123 | return -errno; |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
128 | static int session_device_open(SessionDevice *sd, bool active) { | |
c2e5d024 | 129 | int fd, r; |
118ecf32 | 130 | |
864fe630 | 131 | assert(sd); |
118ecf32 | 132 | assert(sd->type != DEVICE_TYPE_UNKNOWN); |
864fe630 | 133 | assert(sd->node); |
118ecf32 DH |
134 | |
135 | /* open device and try to get an udev_device from it */ | |
136 | fd = open(sd->node, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
137 | if (fd < 0) | |
138 | return -errno; | |
139 | ||
140 | switch (sd->type) { | |
864fe630 | 141 | |
118ecf32 | 142 | case DEVICE_TYPE_DRM: |
c2e5d024 | 143 | if (active) { |
864fe630 LP |
144 | /* Weird legacy DRM semantics might return an error even though we're master. No way to detect |
145 | * that so fail at all times and let caller retry in inactive state. */ | |
c2e5d024 DH |
146 | r = sd_drmsetmaster(fd); |
147 | if (r < 0) { | |
d1107170 | 148 | close_nointr(fd); |
c2e5d024 DH |
149 | return r; |
150 | } | |
864fe630 LP |
151 | } else |
152 | /* DRM-Master is granted to the first user who opens a device automatically (ughh, | |
153 | * racy!). Hence, we just drop DRM-Master in case we were the first. */ | |
4804600b | 154 | (void) sd_drmdropmaster(fd); |
118ecf32 | 155 | break; |
864fe630 | 156 | |
118ecf32 DH |
157 | case DEVICE_TYPE_EVDEV: |
158 | if (!active) | |
159 | sd_eviocrevoke(fd); | |
160 | break; | |
864fe630 | 161 | |
118ecf32 DH |
162 | case DEVICE_TYPE_UNKNOWN: |
163 | default: | |
164 | /* fallback for devices wihout synchronizations */ | |
165 | break; | |
166 | } | |
167 | ||
168 | return fd; | |
169 | } | |
170 | ||
171 | static int session_device_start(SessionDevice *sd) { | |
172 | int r; | |
173 | ||
174 | assert(sd); | |
175 | assert(session_is_active(sd->session)); | |
176 | ||
177 | if (sd->active) | |
178 | return 0; | |
179 | ||
180 | switch (sd->type) { | |
864fe630 | 181 | |
118ecf32 | 182 | case DEVICE_TYPE_DRM: |
4d3900f1 | 183 | if (sd->fd < 0) { |
f2705337 AJ |
184 | log_error("Failed to re-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)"); |
185 | return -EBADF; | |
4d3900f1 | 186 | } |
f2705337 AJ |
187 | |
188 | /* Device is kept open. Simply call drmSetMaster() and hope there is no-one else. In case it fails, we | |
189 | * keep the device paused. Maybe at some point we have a drmStealMaster(). */ | |
190 | r = sd_drmsetmaster(sd->fd); | |
191 | if (r < 0) | |
192 | return r; | |
118ecf32 | 193 | break; |
864fe630 | 194 | |
118ecf32 | 195 | case DEVICE_TYPE_EVDEV: |
864fe630 | 196 | /* Evdev devices are revoked while inactive. Reopen it and we are fine. */ |
118ecf32 DH |
197 | r = session_device_open(sd, true); |
198 | if (r < 0) | |
199 | return r; | |
864fe630 LP |
200 | |
201 | /* For evdev devices, the file descriptor might be left uninitialized. This might happen while resuming | |
202 | * into a session and logind has been restarted right before. */ | |
aed24c4c | 203 | safe_close(sd->fd); |
118ecf32 DH |
204 | sd->fd = r; |
205 | break; | |
864fe630 | 206 | |
118ecf32 DH |
207 | case DEVICE_TYPE_UNKNOWN: |
208 | default: | |
340aff15 | 209 | /* fallback for devices without synchronizations */ |
118ecf32 DH |
210 | break; |
211 | } | |
212 | ||
213 | sd->active = true; | |
214 | return 0; | |
215 | } | |
216 | ||
217 | static void session_device_stop(SessionDevice *sd) { | |
218 | assert(sd); | |
219 | ||
220 | if (!sd->active) | |
221 | return; | |
222 | ||
223 | switch (sd->type) { | |
340aff15 | 224 | |
118ecf32 | 225 | case DEVICE_TYPE_DRM: |
f2705337 AJ |
226 | if (sd->fd < 0) { |
227 | log_error("Failed to de-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)"); | |
228 | return; | |
229 | } | |
230 | ||
118ecf32 DH |
231 | /* On DRM devices we simply drop DRM-Master but keep it open. |
232 | * This allows the user to keep resources allocated. The | |
233 | * CAP_SYS_ADMIN restriction to DRM-Master prevents users from | |
234 | * circumventing this. */ | |
235 | sd_drmdropmaster(sd->fd); | |
236 | break; | |
340aff15 | 237 | |
118ecf32 DH |
238 | case DEVICE_TYPE_EVDEV: |
239 | /* Revoke access on evdev file-descriptors during deactivation. | |
240 | * This will basically prevent any operations on the fd and | |
241 | * cannot be undone. Good side is: it needs no CAP_SYS_ADMIN | |
242 | * protection this way. */ | |
243 | sd_eviocrevoke(sd->fd); | |
244 | break; | |
340aff15 | 245 | |
118ecf32 DH |
246 | case DEVICE_TYPE_UNKNOWN: |
247 | default: | |
248 | /* fallback for devices without synchronization */ | |
249 | break; | |
250 | } | |
251 | ||
252 | sd->active = false; | |
253 | } | |
254 | ||
255 | static DeviceType detect_device_type(struct udev_device *dev) { | |
256 | const char *sysname, *subsystem; | |
257 | DeviceType type; | |
258 | ||
259 | sysname = udev_device_get_sysname(dev); | |
260 | subsystem = udev_device_get_subsystem(dev); | |
261 | type = DEVICE_TYPE_UNKNOWN; | |
262 | ||
3a83f522 | 263 | if (streq_ptr(subsystem, "drm")) { |
118ecf32 DH |
264 | if (startswith(sysname, "card")) |
265 | type = DEVICE_TYPE_DRM; | |
266 | } else if (streq_ptr(subsystem, "input")) { | |
267 | if (startswith(sysname, "event")) | |
268 | type = DEVICE_TYPE_EVDEV; | |
269 | } | |
270 | ||
271 | return type; | |
272 | } | |
273 | ||
274 | static int session_device_verify(SessionDevice *sd) { | |
275 | struct udev_device *dev, *p = NULL; | |
276 | const char *sp, *node; | |
277 | int r; | |
278 | ||
279 | dev = udev_device_new_from_devnum(sd->session->manager->udev, 'c', sd->dev); | |
280 | if (!dev) | |
281 | return -ENODEV; | |
282 | ||
283 | sp = udev_device_get_syspath(dev); | |
284 | node = udev_device_get_devnode(dev); | |
285 | if (!node) { | |
286 | r = -EINVAL; | |
287 | goto err_dev; | |
288 | } | |
289 | ||
290 | /* detect device type so we can find the correct sysfs parent */ | |
291 | sd->type = detect_device_type(dev); | |
292 | if (sd->type == DEVICE_TYPE_UNKNOWN) { | |
293 | r = -ENODEV; | |
294 | goto err_dev; | |
295 | } else if (sd->type == DEVICE_TYPE_EVDEV) { | |
296 | /* for evdev devices we need the parent node as device */ | |
297 | p = dev; | |
298 | dev = udev_device_get_parent_with_subsystem_devtype(p, "input", NULL); | |
299 | if (!dev) { | |
300 | r = -ENODEV; | |
301 | goto err_dev; | |
302 | } | |
303 | sp = udev_device_get_syspath(dev); | |
3a83f522 | 304 | } else if (sd->type != DEVICE_TYPE_DRM) { |
118ecf32 DH |
305 | /* Prevent opening unsupported devices. Especially devices of |
306 | * subsystem "input" must be opened via the evdev node as | |
307 | * we require EVIOCREVOKE. */ | |
308 | r = -ENODEV; | |
309 | goto err_dev; | |
310 | } | |
311 | ||
312 | /* search for an existing seat device and return it if available */ | |
313 | sd->device = hashmap_get(sd->session->manager->devices, sp); | |
314 | if (!sd->device) { | |
315 | /* The caller might have gotten the udev event before we were | |
316 | * able to process it. Hence, fake the "add" event and let the | |
317 | * logind-manager handle the new device. */ | |
318 | r = manager_process_seat_device(sd->session->manager, dev); | |
319 | if (r < 0) | |
320 | goto err_dev; | |
321 | ||
322 | /* if it's still not available, then the device is invalid */ | |
323 | sd->device = hashmap_get(sd->session->manager->devices, sp); | |
324 | if (!sd->device) { | |
325 | r = -ENODEV; | |
326 | goto err_dev; | |
327 | } | |
328 | } | |
329 | ||
330 | if (sd->device->seat != sd->session->seat) { | |
331 | r = -EPERM; | |
332 | goto err_dev; | |
333 | } | |
334 | ||
335 | sd->node = strdup(node); | |
336 | if (!sd->node) { | |
337 | r = -ENOMEM; | |
338 | goto err_dev; | |
339 | } | |
340 | ||
341 | r = 0; | |
342 | err_dev: | |
343 | udev_device_unref(p ? : dev); | |
344 | return r; | |
345 | } | |
346 | ||
aed24c4c | 347 | int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **out) { |
118ecf32 DH |
348 | SessionDevice *sd; |
349 | int r; | |
350 | ||
351 | assert(s); | |
352 | assert(out); | |
353 | ||
354 | if (!s->seat) | |
355 | return -EPERM; | |
356 | ||
357 | sd = new0(SessionDevice, 1); | |
358 | if (!sd) | |
359 | return -ENOMEM; | |
360 | ||
361 | sd->session = s; | |
362 | sd->dev = dev; | |
363 | sd->fd = -1; | |
364 | sd->type = DEVICE_TYPE_UNKNOWN; | |
365 | ||
366 | r = session_device_verify(sd); | |
367 | if (r < 0) | |
368 | goto error; | |
369 | ||
831dedef | 370 | r = hashmap_put(s->devices, &sd->dev, sd); |
e38aa664 | 371 | if (r < 0) |
118ecf32 | 372 | goto error; |
118ecf32 | 373 | |
aed24c4c FB |
374 | if (open_device) { |
375 | /* Open the device for the first time. We need a valid fd to pass back | |
376 | * to the caller. If the session is not active, this _might_ immediately | |
377 | * revoke access and thus invalidate the fd. But this is still needed | |
378 | * to pass a valid fd back. */ | |
379 | sd->active = session_is_active(s); | |
380 | r = session_device_open(sd, sd->active); | |
381 | if (r < 0) { | |
382 | /* EINVAL _may_ mean a master is active; retry inactive */ | |
383 | if (sd->active && r == -EINVAL) { | |
384 | sd->active = false; | |
385 | r = session_device_open(sd, false); | |
386 | } | |
387 | if (r < 0) | |
388 | goto error; | |
c2e5d024 | 389 | } |
aed24c4c | 390 | sd->fd = r; |
c2e5d024 | 391 | } |
118ecf32 | 392 | |
71fda00f | 393 | LIST_PREPEND(sd_by_device, sd->device->session_devices, sd); |
118ecf32 DH |
394 | |
395 | *out = sd; | |
396 | return 0; | |
397 | ||
398 | error: | |
831dedef | 399 | hashmap_remove(s->devices, &sd->dev); |
118ecf32 DH |
400 | free(sd->node); |
401 | free(sd); | |
402 | return r; | |
403 | } | |
404 | ||
405 | void session_device_free(SessionDevice *sd) { | |
406 | assert(sd); | |
407 | ||
1bef256c | 408 | /* Make sure to remove the pushed fd. */ |
4050e479 | 409 | if (sd->pushed_fd) { |
1bef256c AJ |
410 | _cleanup_free_ char *m = NULL; |
411 | const char *id; | |
412 | int r; | |
413 | ||
414 | /* Session ID does not contain separators. */ | |
415 | id = sd->session->id; | |
416 | assert(*(id + strcspn(id, "-\n")) == '\0'); | |
417 | ||
418 | r = asprintf(&m, "FDSTOREREMOVE=1\n" | |
419 | "FDNAME=session-%s-device-%u-%u\n", | |
420 | id, major(sd->dev), minor(sd->dev)); | |
421 | if (r >= 0) | |
422 | (void) sd_notify(false, m); | |
4050e479 LP |
423 | } |
424 | ||
118ecf32 DH |
425 | session_device_stop(sd); |
426 | session_device_notify(sd, SESSION_DEVICE_RELEASE); | |
4d219f53 | 427 | safe_close(sd->fd); |
118ecf32 | 428 | |
71fda00f | 429 | LIST_REMOVE(sd_by_device, sd->device->session_devices, sd); |
118ecf32 | 430 | |
831dedef | 431 | hashmap_remove(sd->session->devices, &sd->dev); |
118ecf32 DH |
432 | |
433 | free(sd->node); | |
434 | free(sd); | |
435 | } | |
436 | ||
437 | void session_device_complete_pause(SessionDevice *sd) { | |
d7bd01b5 DH |
438 | SessionDevice *iter; |
439 | Iterator i; | |
440 | ||
118ecf32 DH |
441 | if (!sd->active) |
442 | return; | |
443 | ||
444 | session_device_stop(sd); | |
d7bd01b5 DH |
445 | |
446 | /* if not all devices are paused, wait for further completion events */ | |
447 | HASHMAP_FOREACH(iter, sd->session->devices, i) | |
448 | if (iter->active) | |
449 | return; | |
450 | ||
451 | /* complete any pending session switch */ | |
452 | seat_complete_switch(sd->session->seat); | |
118ecf32 DH |
453 | } |
454 | ||
455 | void session_device_resume_all(Session *s) { | |
456 | SessionDevice *sd; | |
457 | Iterator i; | |
118ecf32 DH |
458 | |
459 | assert(s); | |
460 | ||
461 | HASHMAP_FOREACH(sd, s->devices, i) { | |
d7ba71f4 LP |
462 | if (sd->active) |
463 | continue; | |
464 | ||
465 | if (session_device_start(sd) < 0) | |
466 | continue; | |
467 | if (session_device_save(sd) < 0) | |
468 | continue; | |
340aff15 | 469 | |
d7ba71f4 | 470 | session_device_notify(sd, SESSION_DEVICE_RESUME); |
118ecf32 DH |
471 | } |
472 | } | |
473 | ||
474 | void session_device_pause_all(Session *s) { | |
475 | SessionDevice *sd; | |
476 | Iterator i; | |
477 | ||
478 | assert(s); | |
479 | ||
480 | HASHMAP_FOREACH(sd, s->devices, i) { | |
d7ba71f4 LP |
481 | if (!sd->active) |
482 | continue; | |
483 | ||
484 | session_device_stop(sd); | |
485 | session_device_notify(sd, SESSION_DEVICE_PAUSE); | |
118ecf32 DH |
486 | } |
487 | } | |
d7bd01b5 DH |
488 | |
489 | unsigned int session_device_try_pause_all(Session *s) { | |
d7ba71f4 | 490 | unsigned num_pending = 0; |
d7bd01b5 DH |
491 | SessionDevice *sd; |
492 | Iterator i; | |
d7bd01b5 DH |
493 | |
494 | assert(s); | |
495 | ||
496 | HASHMAP_FOREACH(sd, s->devices, i) { | |
d7ba71f4 LP |
497 | if (!sd->active) |
498 | continue; | |
499 | ||
500 | session_device_notify(sd, SESSION_DEVICE_TRY_PAUSE); | |
501 | num_pending++; | |
d7bd01b5 DH |
502 | } |
503 | ||
504 | return num_pending; | |
505 | } | |
aed24c4c FB |
506 | |
507 | int session_device_save(SessionDevice *sd) { | |
1bef256c AJ |
508 | _cleanup_free_ char *m = NULL; |
509 | const char *id; | |
aed24c4c FB |
510 | int r; |
511 | ||
512 | assert(sd); | |
513 | ||
4050e479 LP |
514 | /* Store device fd in PID1. It will send it back to us on restart so revocation will continue to work. To make |
515 | * things simple, send fds for all type of devices even if they don't support the revocation mechanism so we | |
516 | * don't have to handle them differently later. | |
aed24c4c | 517 | * |
4050e479 LP |
518 | * Note: for device supporting revocation, PID1 will drop a stored fd automatically if the corresponding device |
519 | * is revoked. */ | |
520 | ||
521 | if (sd->pushed_fd) | |
522 | return 0; | |
1bef256c AJ |
523 | |
524 | /* Session ID does not contain separators. */ | |
525 | id = sd->session->id; | |
526 | assert(*(id + strcspn(id, "-\n")) == '\0'); | |
527 | ||
528 | r = asprintf(&m, "FDSTORE=1\n" | |
529 | "FDNAME=session-%s-device-%u-%u\n", | |
530 | id, major(sd->dev), minor(sd->dev)); | |
531 | if (r < 0) | |
532 | return r; | |
4050e479 LP |
533 | |
534 | r = sd_pid_notify_with_fds(0, false, m, &sd->fd, 1); | |
aed24c4c | 535 | if (r < 0) |
4050e479 | 536 | return r; |
aed24c4c | 537 | |
4050e479 LP |
538 | sd->pushed_fd = true; |
539 | return 1; | |
aed24c4c FB |
540 | } |
541 | ||
542 | void session_device_attach_fd(SessionDevice *sd, int fd, bool active) { | |
4c9cb12c | 543 | assert(fd >= 0); |
aed24c4c FB |
544 | assert(sd); |
545 | assert(sd->fd < 0); | |
546 | assert(!sd->active); | |
547 | ||
548 | sd->fd = fd; | |
f8f9419e | 549 | sd->pushed_fd = true; |
aed24c4c FB |
550 | sd->active = active; |
551 | } |