]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-session-device.c
codespell: fix spelling errors
[thirdparty/systemd.git] / src / login / logind-session-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <linux/input.h>
5 #include <string.h>
6 #include <sys/ioctl.h>
7 #include <sys/types.h>
8
9 #include "sd-device.h"
10
11 #include "alloc-util.h"
12 #include "bus-util.h"
13 #include "fd-util.h"
14 #include "logind-session-device.h"
15 #include "missing.h"
16 #include "parse-util.h"
17 #include "sd-daemon.h"
18 #include "util.h"
19
20 enum SessionDeviceNotifications {
21 SESSION_DEVICE_RESUME,
22 SESSION_DEVICE_TRY_PAUSE,
23 SESSION_DEVICE_PAUSE,
24 SESSION_DEVICE_RELEASE,
25 };
26
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;
30 const char *t = NULL;
31 uint32_t major, minor;
32 int r;
33
34 assert(sd);
35
36 major = major(sd->dev);
37 minor = minor(sd->dev);
38
39 if (!sd->session->controller)
40 return 0;
41
42 path = session_bus_path(sd->session);
43 if (!path)
44 return -ENOMEM;
45
46 r = sd_bus_message_new_signal(
47 sd->session->manager->bus,
48 &m, path,
49 "org.freedesktop.login1.Session",
50 (type == SESSION_DEVICE_RESUME) ? "ResumeDevice" : "PauseDevice");
51 if (!m)
52 return r;
53
54 r = sd_bus_message_set_destination(m, sd->session->controller);
55 if (r < 0)
56 return r;
57
58 switch (type) {
59
60 case SESSION_DEVICE_RESUME:
61 r = sd_bus_message_append(m, "uuh", major, minor, sd->fd);
62 if (r < 0)
63 return r;
64 break;
65
66 case SESSION_DEVICE_TRY_PAUSE:
67 t = "pause";
68 break;
69
70 case SESSION_DEVICE_PAUSE:
71 t = "force";
72 break;
73
74 case SESSION_DEVICE_RELEASE:
75 t = "gone";
76 break;
77
78 default:
79 return -EINVAL;
80 }
81
82 if (t) {
83 r = sd_bus_message_append(m, "uus", major, minor, t);
84 if (r < 0)
85 return r;
86 }
87
88 return sd_bus_send(sd->session->manager->bus, m, NULL);
89 }
90
91 static void sd_eviocrevoke(int fd) {
92 static bool warned = false;
93
94 assert(fd >= 0);
95
96 if (ioctl(fd, EVIOCREVOKE, NULL) < 0) {
97
98 if (errno == EINVAL && !warned) {
99 log_warning_errno(errno, "Kernel does not support evdev-revocation: %m");
100 warned = true;
101 }
102 }
103 }
104
105 static int sd_drmsetmaster(int fd) {
106 assert(fd >= 0);
107
108 if (ioctl(fd, DRM_IOCTL_SET_MASTER, 0) < 0)
109 return -errno;
110
111 return 0;
112 }
113
114 static int sd_drmdropmaster(int fd) {
115 assert(fd >= 0);
116
117 if (ioctl(fd, DRM_IOCTL_DROP_MASTER, 0) < 0)
118 return -errno;
119
120 return 0;
121 }
122
123 static int session_device_open(SessionDevice *sd, bool active) {
124 int fd, r;
125
126 assert(sd);
127 assert(sd->type != DEVICE_TYPE_UNKNOWN);
128 assert(sd->node);
129
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);
132 if (fd < 0)
133 return -errno;
134
135 switch (sd->type) {
136
137 case DEVICE_TYPE_DRM:
138 if (active) {
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);
142 if (r < 0) {
143 close_nointr(fd);
144 return r;
145 }
146 } else
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);
150 break;
151
152 case DEVICE_TYPE_EVDEV:
153 if (!active)
154 sd_eviocrevoke(fd);
155 break;
156
157 case DEVICE_TYPE_UNKNOWN:
158 default:
159 /* fallback for devices without synchronizations */
160 break;
161 }
162
163 return fd;
164 }
165
166 static int session_device_start(SessionDevice *sd) {
167 int r;
168
169 assert(sd);
170 assert(session_is_active(sd->session));
171
172 if (sd->active)
173 return 0;
174
175 switch (sd->type) {
176
177 case DEVICE_TYPE_DRM:
178 if (sd->fd < 0)
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?)");
181
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);
185 if (r < 0)
186 return r;
187 break;
188
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);
192 if (r < 0)
193 return r;
194
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. */
197 safe_close(sd->fd);
198 sd->fd = r;
199 break;
200
201 case DEVICE_TYPE_UNKNOWN:
202 default:
203 /* fallback for devices without synchronizations */
204 break;
205 }
206
207 sd->active = true;
208 return 0;
209 }
210
211 static void session_device_stop(SessionDevice *sd) {
212 assert(sd);
213
214 if (!sd->active)
215 return;
216
217 switch (sd->type) {
218
219 case DEVICE_TYPE_DRM:
220 if (sd->fd < 0) {
221 log_error("Failed to de-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)");
222 return;
223 }
224
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);
230 break;
231
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);
238 break;
239
240 case DEVICE_TYPE_UNKNOWN:
241 default:
242 /* fallback for devices without synchronization */
243 break;
244 }
245
246 sd->active = false;
247 }
248
249 static DeviceType detect_device_type(sd_device *dev) {
250 const char *sysname, *subsystem;
251 DeviceType type = DEVICE_TYPE_UNKNOWN;
252
253 if (sd_device_get_sysname(dev, &sysname) < 0 ||
254 sd_device_get_subsystem(dev, &subsystem) < 0)
255 return type;
256
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;
263 }
264
265 return type;
266 }
267
268 static int session_device_verify(SessionDevice *sd) {
269 _cleanup_(sd_device_unrefp) sd_device *p = NULL;
270 const char *sp, *node;
271 sd_device *dev;
272 int r;
273
274 r = sd_device_new_from_devnum(&p, 'c', sd->dev);
275 if (r < 0)
276 return r;
277
278 dev = p;
279
280 if (sd_device_get_syspath(dev, &sp) < 0 ||
281 sd_device_get_devname(dev, &node) < 0)
282 return -EINVAL;
283
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)
287 return -ENODEV;
288
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)
292 return -ENODEV;
293 if (sd_device_get_syspath(dev, &sp) < 0)
294 return -ENODEV;
295
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. */
300 return -ENODEV;
301
302 /* search for an existing seat device and return it if available */
303 sd->device = hashmap_get(sd->session->manager->devices, sp);
304 if (!sd->device) {
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);
309 if (r < 0)
310 return r;
311
312 /* if it's still not available, then the device is invalid */
313 sd->device = hashmap_get(sd->session->manager->devices, sp);
314 if (!sd->device)
315 return -ENODEV;
316 }
317
318 if (sd->device->seat != sd->session->seat)
319 return -EPERM;
320
321 sd->node = strdup(node);
322 if (!sd->node)
323 return -ENOMEM;
324
325 return 0;
326 }
327
328 int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **out) {
329 SessionDevice *sd;
330 int r;
331
332 assert(s);
333 assert(out);
334
335 if (!s->seat)
336 return -EPERM;
337
338 sd = new0(SessionDevice, 1);
339 if (!sd)
340 return -ENOMEM;
341
342 sd->session = s;
343 sd->dev = dev;
344 sd->fd = -1;
345 sd->type = DEVICE_TYPE_UNKNOWN;
346
347 r = session_device_verify(sd);
348 if (r < 0)
349 goto error;
350
351 r = hashmap_put(s->devices, &sd->dev, sd);
352 if (r < 0)
353 goto error;
354
355 if (open_device) {
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);
362 if (r < 0) {
363 /* EINVAL _may_ mean a master is active; retry inactive */
364 if (sd->active && r == -EINVAL) {
365 sd->active = false;
366 r = session_device_open(sd, false);
367 }
368 if (r < 0)
369 goto error;
370 }
371 sd->fd = r;
372 }
373
374 LIST_PREPEND(sd_by_device, sd->device->session_devices, sd);
375
376 *out = sd;
377 return 0;
378
379 error:
380 hashmap_remove(s->devices, &sd->dev);
381 free(sd->node);
382 free(sd);
383 return r;
384 }
385
386 void session_device_free(SessionDevice *sd) {
387 assert(sd);
388
389 /* Make sure to remove the pushed fd. */
390 if (sd->pushed_fd)
391 (void) sd_notifyf(false,
392 "FDSTOREREMOVE=1\n"
393 "FDNAME=session-%s-device-%u-%u",
394 sd->session->id, major(sd->dev), minor(sd->dev));
395
396 session_device_stop(sd);
397 session_device_notify(sd, SESSION_DEVICE_RELEASE);
398 safe_close(sd->fd);
399
400 LIST_REMOVE(sd_by_device, sd->device->session_devices, sd);
401
402 hashmap_remove(sd->session->devices, &sd->dev);
403
404 free(sd->node);
405 free(sd);
406 }
407
408 void session_device_complete_pause(SessionDevice *sd) {
409 SessionDevice *iter;
410 Iterator i;
411
412 if (!sd->active)
413 return;
414
415 session_device_stop(sd);
416
417 /* if not all devices are paused, wait for further completion events */
418 HASHMAP_FOREACH(iter, sd->session->devices, i)
419 if (iter->active)
420 return;
421
422 /* complete any pending session switch */
423 seat_complete_switch(sd->session->seat);
424 }
425
426 void session_device_resume_all(Session *s) {
427 SessionDevice *sd;
428 Iterator i;
429
430 assert(s);
431
432 HASHMAP_FOREACH(sd, s->devices, i) {
433 if (sd->active)
434 continue;
435
436 if (session_device_start(sd) < 0)
437 continue;
438 if (session_device_save(sd) < 0)
439 continue;
440
441 session_device_notify(sd, SESSION_DEVICE_RESUME);
442 }
443 }
444
445 void session_device_pause_all(Session *s) {
446 SessionDevice *sd;
447 Iterator i;
448
449 assert(s);
450
451 HASHMAP_FOREACH(sd, s->devices, i) {
452 if (!sd->active)
453 continue;
454
455 session_device_stop(sd);
456 session_device_notify(sd, SESSION_DEVICE_PAUSE);
457 }
458 }
459
460 unsigned session_device_try_pause_all(Session *s) {
461 unsigned num_pending = 0;
462 SessionDevice *sd;
463 Iterator i;
464
465 assert(s);
466
467 HASHMAP_FOREACH(sd, s->devices, i) {
468 if (!sd->active)
469 continue;
470
471 session_device_notify(sd, SESSION_DEVICE_TRY_PAUSE);
472 num_pending++;
473 }
474
475 return num_pending;
476 }
477
478 int session_device_save(SessionDevice *sd) {
479 _cleanup_free_ char *m = NULL;
480 const char *id;
481 int r;
482
483 assert(sd);
484
485 /* Store device fd in PID1. It will send it back to us on restart so revocation will continue to work. To make
486 * things simple, send fds for all type of devices even if they don't support the revocation mechanism so we
487 * don't have to handle them differently later.
488 *
489 * Note: for device supporting revocation, PID1 will drop a stored fd automatically if the corresponding device
490 * is revoked. */
491
492 if (sd->pushed_fd)
493 return 0;
494
495 /* Session ID does not contain separators. */
496 id = sd->session->id;
497 assert(*(id + strcspn(id, "-\n")) == '\0');
498
499 r = asprintf(&m, "FDSTORE=1\n"
500 "FDNAME=session-%s-device-%u-%u\n",
501 id, major(sd->dev), minor(sd->dev));
502 if (r < 0)
503 return r;
504
505 r = sd_pid_notify_with_fds(0, false, m, &sd->fd, 1);
506 if (r < 0)
507 return r;
508
509 sd->pushed_fd = true;
510 return 1;
511 }
512
513 void session_device_attach_fd(SessionDevice *sd, int fd, bool active) {
514 assert(fd >= 0);
515 assert(sd);
516 assert(sd->fd < 0);
517 assert(!sd->active);
518
519 sd->fd = fd;
520 sd->pushed_fd = true;
521 sd->active = active;
522 }