]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-session-device.c
Merge pull request #9199 from poettering/copy-file-atomic
[thirdparty/systemd.git] / src / login / logind-session-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 David Herrmann
6 ***/
7
8 #include <fcntl.h>
9 #include <linux/input.h>
10 #include <string.h>
11 #include <sys/ioctl.h>
12 #include <sys/types.h>
13
14 #include "libudev.h"
15
16 #include "alloc-util.h"
17 #include "bus-util.h"
18 #include "fd-util.h"
19 #include "logind-session-device.h"
20 #include "missing.h"
21 #include "parse-util.h"
22 #include "sd-daemon.h"
23 #include "util.h"
24
25 enum SessionDeviceNotifications {
26 SESSION_DEVICE_RESUME,
27 SESSION_DEVICE_TRY_PAUSE,
28 SESSION_DEVICE_PAUSE,
29 SESSION_DEVICE_RELEASE,
30 };
31
32 static int session_device_notify(SessionDevice *sd, enum SessionDeviceNotifications type) {
33 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
34 _cleanup_free_ char *path = NULL;
35 const char *t = NULL;
36 uint32_t major, minor;
37 int r;
38
39 assert(sd);
40
41 major = major(sd->dev);
42 minor = minor(sd->dev);
43
44 if (!sd->session->controller)
45 return 0;
46
47 path = session_bus_path(sd->session);
48 if (!path)
49 return -ENOMEM;
50
51 r = sd_bus_message_new_signal(
52 sd->session->manager->bus,
53 &m, path,
54 "org.freedesktop.login1.Session",
55 (type == SESSION_DEVICE_RESUME) ? "ResumeDevice" : "PauseDevice");
56 if (!m)
57 return r;
58
59 r = sd_bus_message_set_destination(m, sd->session->controller);
60 if (r < 0)
61 return r;
62
63 switch (type) {
64
65 case SESSION_DEVICE_RESUME:
66 r = sd_bus_message_append(m, "uuh", major, minor, sd->fd);
67 if (r < 0)
68 return r;
69 break;
70
71 case SESSION_DEVICE_TRY_PAUSE:
72 t = "pause";
73 break;
74
75 case SESSION_DEVICE_PAUSE:
76 t = "force";
77 break;
78
79 case SESSION_DEVICE_RELEASE:
80 t = "gone";
81 break;
82
83 default:
84 return -EINVAL;
85 }
86
87 if (t) {
88 r = sd_bus_message_append(m, "uus", major, minor, t);
89 if (r < 0)
90 return r;
91 }
92
93 return sd_bus_send(sd->session->manager->bus, m, NULL);
94 }
95
96 static void sd_eviocrevoke(int fd) {
97 static bool warned = false;
98
99 assert(fd >= 0);
100
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");
105 warned = true;
106 }
107 }
108 }
109
110 static int sd_drmsetmaster(int fd) {
111 assert(fd >= 0);
112
113 if (ioctl(fd, DRM_IOCTL_SET_MASTER, 0) < 0)
114 return -errno;
115
116 return 0;
117 }
118
119 static int sd_drmdropmaster(int fd) {
120 assert(fd >= 0);
121
122 if (ioctl(fd, DRM_IOCTL_DROP_MASTER, 0) < 0)
123 return -errno;
124
125 return 0;
126 }
127
128 static int session_device_open(SessionDevice *sd, bool active) {
129 int fd, r;
130
131 assert(sd);
132 assert(sd->type != DEVICE_TYPE_UNKNOWN);
133 assert(sd->node);
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) {
141
142 case DEVICE_TYPE_DRM:
143 if (active) {
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. */
146 r = sd_drmsetmaster(fd);
147 if (r < 0) {
148 close_nointr(fd);
149 return r;
150 }
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. */
154 (void) sd_drmdropmaster(fd);
155 break;
156
157 case DEVICE_TYPE_EVDEV:
158 if (!active)
159 sd_eviocrevoke(fd);
160 break;
161
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) {
181
182 case DEVICE_TYPE_DRM:
183 if (sd->fd < 0) {
184 log_error("Failed to re-activate DRM fd, as the fd was lost (maybe logind restart went wrong?)");
185 return -EBADF;
186 }
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;
193 break;
194
195 case DEVICE_TYPE_EVDEV:
196 /* Evdev devices are revoked while inactive. Reopen it and we are fine. */
197 r = session_device_open(sd, true);
198 if (r < 0)
199 return r;
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. */
203 safe_close(sd->fd);
204 sd->fd = r;
205 break;
206
207 case DEVICE_TYPE_UNKNOWN:
208 default:
209 /* fallback for devices without synchronizations */
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) {
224
225 case DEVICE_TYPE_DRM:
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
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;
237
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;
245
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
263 if (streq_ptr(subsystem, "drm")) {
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);
304 } else if (sd->type != DEVICE_TYPE_DRM) {
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
347 int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **out) {
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
370 r = hashmap_put(s->devices, &sd->dev, sd);
371 if (r < 0)
372 goto error;
373
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;
389 }
390 sd->fd = r;
391 }
392
393 LIST_PREPEND(sd_by_device, sd->device->session_devices, sd);
394
395 *out = sd;
396 return 0;
397
398 error:
399 hashmap_remove(s->devices, &sd->dev);
400 free(sd->node);
401 free(sd);
402 return r;
403 }
404
405 void session_device_free(SessionDevice *sd) {
406 assert(sd);
407
408 /* Make sure to remove the pushed fd. */
409 if (sd->pushed_fd) {
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);
423 }
424
425 session_device_stop(sd);
426 session_device_notify(sd, SESSION_DEVICE_RELEASE);
427 safe_close(sd->fd);
428
429 LIST_REMOVE(sd_by_device, sd->device->session_devices, sd);
430
431 hashmap_remove(sd->session->devices, &sd->dev);
432
433 free(sd->node);
434 free(sd);
435 }
436
437 void session_device_complete_pause(SessionDevice *sd) {
438 SessionDevice *iter;
439 Iterator i;
440
441 if (!sd->active)
442 return;
443
444 session_device_stop(sd);
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);
453 }
454
455 void session_device_resume_all(Session *s) {
456 SessionDevice *sd;
457 Iterator i;
458
459 assert(s);
460
461 HASHMAP_FOREACH(sd, s->devices, i) {
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;
469
470 session_device_notify(sd, SESSION_DEVICE_RESUME);
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) {
481 if (!sd->active)
482 continue;
483
484 session_device_stop(sd);
485 session_device_notify(sd, SESSION_DEVICE_PAUSE);
486 }
487 }
488
489 unsigned int session_device_try_pause_all(Session *s) {
490 unsigned num_pending = 0;
491 SessionDevice *sd;
492 Iterator i;
493
494 assert(s);
495
496 HASHMAP_FOREACH(sd, s->devices, i) {
497 if (!sd->active)
498 continue;
499
500 session_device_notify(sd, SESSION_DEVICE_TRY_PAUSE);
501 num_pending++;
502 }
503
504 return num_pending;
505 }
506
507 int session_device_save(SessionDevice *sd) {
508 _cleanup_free_ char *m = NULL;
509 const char *id;
510 int r;
511
512 assert(sd);
513
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.
517 *
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;
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;
533
534 r = sd_pid_notify_with_fds(0, false, m, &sd->fd, 1);
535 if (r < 0)
536 return r;
537
538 sd->pushed_fd = true;
539 return 1;
540 }
541
542 void session_device_attach_fd(SessionDevice *sd, int fd, bool active) {
543 assert(fd >= 0);
544 assert(sd);
545 assert(sd->fd < 0);
546 assert(!sd->active);
547
548 sd->fd = fd;
549 sd->pushed_fd = true;
550 sd->active = active;
551 }