1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 David Herrmann
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <linux/input.h>
26 #include <linux/ioctl.h>
28 #include <sys/ioctl.h>
30 #include <sys/types.h>
36 #include "logind-session-device.h"
38 enum SessionDeviceNotifications {
39 SESSION_DEVICE_RESUME,
40 SESSION_DEVICE_TRY_PAUSE,
42 SESSION_DEVICE_RELEASE,
45 static int session_device_notify(SessionDevice *sd, enum SessionDeviceNotifications type) {
46 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
47 _cleanup_free_ char *path = NULL;
49 uint32_t major, minor;
54 major = major(sd->dev);
55 minor = minor(sd->dev);
57 if (!sd->session->controller)
60 path = session_bus_path(sd->session);
64 r = sd_bus_message_new_signal(
65 sd->session->manager->bus, path,
66 "org.freedesktop.login1.Session",
67 (type == SESSION_DEVICE_RESUME) ? "ResumeDevice" : "PauseDevice",
72 r = sd_bus_message_set_destination(m, sd->session->controller);
77 case SESSION_DEVICE_RESUME:
78 r = sd_bus_message_append(m, "uuh", major, minor, sd->fd);
82 case SESSION_DEVICE_TRY_PAUSE:
85 case SESSION_DEVICE_PAUSE:
88 case SESSION_DEVICE_RELEASE:
96 r = sd_bus_message_append(m, "uus", major, minor, t);
101 return sd_bus_send(sd->session->manager->bus, m, NULL);
104 static int sd_eviocrevoke(int fd) {
110 r = ioctl(fd, EVIOCREVOKE, 1);
113 if (r == -EINVAL && !warned) {
115 log_warning("kernel does not support evdev-revocation");
122 static int sd_drmsetmaster(int fd) {
127 r = ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
134 static int sd_drmdropmaster(int fd) {
139 r = ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
146 static int session_device_open(SessionDevice *sd, bool active) {
149 assert(sd->type != DEVICE_TYPE_UNKNOWN);
151 /* open device and try to get an udev_device from it */
152 fd = open(sd->node, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
157 case DEVICE_TYPE_DRM:
159 /* Weird legacy DRM semantics might return an error
160 * even though we're master. No way to detect that so
161 * fail at all times and let caller retry in inactive
163 r = sd_drmsetmaster(fd);
169 /* DRM-Master is granted to the first user who opens a
170 * device automatically (ughh, racy!). Hence, we just
171 * drop DRM-Master in case we were the first. */
172 sd_drmdropmaster(fd);
175 case DEVICE_TYPE_EVDEV:
179 case DEVICE_TYPE_UNKNOWN:
181 /* fallback for devices wihout synchronizations */
188 static int session_device_start(SessionDevice *sd) {
192 assert(session_is_active(sd->session));
198 case DEVICE_TYPE_DRM:
199 /* Device is kept open. Simply call drmSetMaster() and hope
200 * there is no-one else. In case it fails, we keep the device
201 * paused. Maybe at some point we have a drmStealMaster(). */
202 r = sd_drmsetmaster(sd->fd);
206 case DEVICE_TYPE_EVDEV:
207 /* Evdev devices are revoked while inactive. Reopen it and we
209 r = session_device_open(sd, true);
212 close_nointr(sd->fd);
215 case DEVICE_TYPE_UNKNOWN:
217 /* fallback for devices wihout synchronizations */
225 static void session_device_stop(SessionDevice *sd) {
232 case DEVICE_TYPE_DRM:
233 /* On DRM devices we simply drop DRM-Master but keep it open.
234 * This allows the user to keep resources allocated. The
235 * CAP_SYS_ADMIN restriction to DRM-Master prevents users from
236 * circumventing this. */
237 sd_drmdropmaster(sd->fd);
239 case DEVICE_TYPE_EVDEV:
240 /* Revoke access on evdev file-descriptors during deactivation.
241 * This will basically prevent any operations on the fd and
242 * cannot be undone. Good side is: it needs no CAP_SYS_ADMIN
243 * protection this way. */
244 sd_eviocrevoke(sd->fd);
246 case DEVICE_TYPE_UNKNOWN:
248 /* fallback for devices without synchronization */
255 static DeviceType detect_device_type(struct udev_device *dev) {
256 const char *sysname, *subsystem;
259 sysname = udev_device_get_sysname(dev);
260 subsystem = udev_device_get_subsystem(dev);
261 type = DEVICE_TYPE_UNKNOWN;
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;
274 static int session_device_verify(SessionDevice *sd) {
275 struct udev_device *dev, *p = NULL;
276 const char *sp, *node;
279 dev = udev_device_new_from_devnum(sd->session->manager->udev, 'c', sd->dev);
283 sp = udev_device_get_syspath(dev);
284 node = udev_device_get_devnode(dev);
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) {
295 } else if (sd->type == DEVICE_TYPE_EVDEV) {
296 /* for evdev devices we need the parent node as device */
298 dev = udev_device_get_parent_with_subsystem_devtype(p, "input", NULL);
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. */
312 /* search for an existing seat device and return it if available */
313 sd->device = hashmap_get(sd->session->manager->devices, sp);
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);
322 /* if it's still not available, then the device is invalid */
323 sd->device = hashmap_get(sd->session->manager->devices, sp);
330 if (sd->device->seat != sd->session->seat) {
335 sd->node = strdup(node);
343 udev_device_unref(p ? : dev);
347 int session_device_new(Session *s, dev_t dev, SessionDevice **out) {
357 sd = new0(SessionDevice, 1);
364 sd->type = DEVICE_TYPE_UNKNOWN;
366 r = session_device_verify(sd);
370 r = hashmap_put(s->devices, &sd->dev, sd);
376 /* Open the device for the first time. We need a valid fd to pass back
377 * to the caller. If the session is not active, this _might_ immediately
378 * revoke access and thus invalidate the fd. But this is still needed
379 * to pass a valid fd back. */
380 sd->active = session_is_active(s);
381 r = session_device_open(sd, sd->active);
383 /* EINVAL _may_ mean a master is active; retry inactive */
384 if (sd->active && r == -EINVAL) {
386 r = session_device_open(sd, false);
393 LIST_PREPEND(sd_by_device, sd->device->session_devices, sd);
399 hashmap_remove(s->devices, &sd->dev);
405 void session_device_free(SessionDevice *sd) {
408 session_device_stop(sd);
409 session_device_notify(sd, SESSION_DEVICE_RELEASE);
410 close_nointr(sd->fd);
412 LIST_REMOVE(sd_by_device, sd->device->session_devices, sd);
414 hashmap_remove(sd->session->devices, &sd->dev);
420 void session_device_complete_pause(SessionDevice *sd) {
427 session_device_stop(sd);
429 /* if not all devices are paused, wait for further completion events */
430 HASHMAP_FOREACH(iter, sd->session->devices, i)
434 /* complete any pending session switch */
435 seat_complete_switch(sd->session->seat);
438 void session_device_resume_all(Session *s) {
445 HASHMAP_FOREACH(sd, s->devices, i) {
447 r = session_device_start(sd);
449 session_device_notify(sd, SESSION_DEVICE_RESUME);
454 void session_device_pause_all(Session *s) {
460 HASHMAP_FOREACH(sd, s->devices, i) {
462 session_device_stop(sd);
463 session_device_notify(sd, SESSION_DEVICE_PAUSE);
468 unsigned int session_device_try_pause_all(Session *s) {
471 unsigned int num_pending = 0;
475 HASHMAP_FOREACH(sd, s->devices, i) {
477 session_device_notify(sd, SESSION_DEVICE_TRY_PAUSE);