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