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