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