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