2 This file is part of systemd.
4 Copyright 2013 David Herrmann
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/input.h>
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
26 #if 0 /// elogind needs the systems udev header
32 #include "alloc-util.h"
35 #include "logind-session-device.h"
39 enum SessionDeviceNotifications {
40 SESSION_DEVICE_RESUME,
41 SESSION_DEVICE_TRY_PAUSE,
43 SESSION_DEVICE_RELEASE,
46 static int session_device_notify(SessionDevice *sd, enum SessionDeviceNotifications type) {
47 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
48 _cleanup_free_ char *path = NULL;
50 uint32_t major, minor;
55 major = major(sd->dev);
56 minor = minor(sd->dev);
58 if (!sd->session->controller)
61 path = session_bus_path(sd->session);
65 r = sd_bus_message_new_signal(
66 sd->session->manager->bus,
68 "org.freedesktop.login1.Session",
69 (type == SESSION_DEVICE_RESUME) ? "ResumeDevice" : "PauseDevice");
73 r = sd_bus_message_set_destination(m, sd->session->controller);
78 case SESSION_DEVICE_RESUME:
79 r = sd_bus_message_append(m, "uuh", major, minor, sd->fd);
83 case SESSION_DEVICE_TRY_PAUSE:
86 case SESSION_DEVICE_PAUSE:
89 case SESSION_DEVICE_RELEASE:
97 r = sd_bus_message_append(m, "uus", major, minor, t);
102 return sd_bus_send(sd->session->manager->bus, m, NULL);
105 static int sd_eviocrevoke(int fd) {
111 r = ioctl(fd, EVIOCREVOKE, NULL);
114 if (r == -EINVAL && !warned) {
116 log_warning("kernel does not support evdev-revocation");
123 static int sd_drmsetmaster(int fd) {
128 r = ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
135 static int sd_drmdropmaster(int fd) {
140 r = ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
147 static int session_device_open(SessionDevice *sd, bool active) {
150 assert(sd->type != DEVICE_TYPE_UNKNOWN);
152 /* open device and try to get an udev_device from it */
153 fd = open(sd->node, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
158 case DEVICE_TYPE_DRM:
160 /* Weird legacy DRM semantics might return an error
161 * even though we're master. No way to detect that so
162 * fail at all times and let caller retry in inactive
164 r = sd_drmsetmaster(fd);
170 /* DRM-Master is granted to the first user who opens a
171 * device automatically (ughh, racy!). Hence, we just
172 * drop DRM-Master in case we were the first. */
173 sd_drmdropmaster(fd);
176 case DEVICE_TYPE_EVDEV:
180 case DEVICE_TYPE_UNKNOWN:
182 /* fallback for devices wihout synchronizations */
189 static int session_device_start(SessionDevice *sd) {
193 assert(session_is_active(sd->session));
199 case DEVICE_TYPE_DRM:
200 /* Device is kept open. Simply call drmSetMaster() and hope
201 * there is no-one else. In case it fails, we keep the device
202 * paused. Maybe at some point we have a drmStealMaster(). */
203 r = sd_drmsetmaster(sd->fd);
207 case DEVICE_TYPE_EVDEV:
208 /* Evdev devices are revoked while inactive. Reopen it and we
210 r = session_device_open(sd, true);
213 close_nointr(sd->fd);
216 case DEVICE_TYPE_UNKNOWN:
218 /* fallback for devices wihout synchronizations */
226 static void session_device_stop(SessionDevice *sd) {
233 case DEVICE_TYPE_DRM:
234 /* On DRM devices we simply drop DRM-Master but keep it open.
235 * This allows the user to keep resources allocated. The
236 * CAP_SYS_ADMIN restriction to DRM-Master prevents users from
237 * circumventing this. */
238 sd_drmdropmaster(sd->fd);
240 case DEVICE_TYPE_EVDEV:
241 /* Revoke access on evdev file-descriptors during deactivation.
242 * This will basically prevent any operations on the fd and
243 * cannot be undone. Good side is: it needs no CAP_SYS_ADMIN
244 * protection this way. */
245 sd_eviocrevoke(sd->fd);
247 case DEVICE_TYPE_UNKNOWN:
249 /* fallback for devices without synchronization */
256 static DeviceType detect_device_type(struct udev_device *dev) {
257 const char *sysname, *subsystem;
260 sysname = udev_device_get_sysname(dev);
261 subsystem = udev_device_get_subsystem(dev);
262 type = DEVICE_TYPE_UNKNOWN;
264 if (streq_ptr(subsystem, "drm")) {
265 if (startswith(sysname, "card"))
266 type = DEVICE_TYPE_DRM;
267 } else if (streq_ptr(subsystem, "input")) {
268 if (startswith(sysname, "event"))
269 type = DEVICE_TYPE_EVDEV;
275 static int session_device_verify(SessionDevice *sd) {
276 struct udev_device *dev, *p = NULL;
277 const char *sp, *node;
280 dev = udev_device_new_from_devnum(sd->session->manager->udev, 'c', sd->dev);
284 sp = udev_device_get_syspath(dev);
285 node = udev_device_get_devnode(dev);
291 /* detect device type so we can find the correct sysfs parent */
292 sd->type = detect_device_type(dev);
293 if (sd->type == DEVICE_TYPE_UNKNOWN) {
296 } else if (sd->type == DEVICE_TYPE_EVDEV) {
297 /* for evdev devices we need the parent node as device */
299 dev = udev_device_get_parent_with_subsystem_devtype(p, "input", NULL);
304 sp = udev_device_get_syspath(dev);
305 } else if (sd->type != DEVICE_TYPE_DRM) {
306 /* Prevent opening unsupported devices. Especially devices of
307 * subsystem "input" must be opened via the evdev node as
308 * we require EVIOCREVOKE. */
313 /* search for an existing seat device and return it if available */
314 sd->device = hashmap_get(sd->session->manager->devices, sp);
316 /* The caller might have gotten the udev event before we were
317 * able to process it. Hence, fake the "add" event and let the
318 * logind-manager handle the new device. */
319 r = manager_process_seat_device(sd->session->manager, dev);
323 /* if it's still not available, then the device is invalid */
324 sd->device = hashmap_get(sd->session->manager->devices, sp);
331 if (sd->device->seat != sd->session->seat) {
336 sd->node = strdup(node);
344 udev_device_unref(p ? : dev);
348 int session_device_new(Session *s, dev_t dev, SessionDevice **out) {
358 sd = new0(SessionDevice, 1);
365 sd->type = DEVICE_TYPE_UNKNOWN;
367 r = session_device_verify(sd);
371 r = hashmap_put(s->devices, &sd->dev, sd);
377 /* Open the device for the first time. We need a valid fd to pass back
378 * to the caller. If the session is not active, this _might_ immediately
379 * revoke access and thus invalidate the fd. But this is still needed
380 * to pass a valid fd back. */
381 sd->active = session_is_active(s);
382 r = session_device_open(sd, sd->active);
384 /* EINVAL _may_ mean a master is active; retry inactive */
385 if (sd->active && r == -EINVAL) {
387 r = session_device_open(sd, false);
394 LIST_PREPEND(sd_by_device, sd->device->session_devices, sd);
400 hashmap_remove(s->devices, &sd->dev);
406 void session_device_free(SessionDevice *sd) {
409 session_device_stop(sd);
410 session_device_notify(sd, SESSION_DEVICE_RELEASE);
411 close_nointr(sd->fd);
413 LIST_REMOVE(sd_by_device, sd->device->session_devices, sd);
415 hashmap_remove(sd->session->devices, &sd->dev);
421 void session_device_complete_pause(SessionDevice *sd) {
428 session_device_stop(sd);
430 /* if not all devices are paused, wait for further completion events */
431 HASHMAP_FOREACH(iter, sd->session->devices, i)
435 /* complete any pending session switch */
436 seat_complete_switch(sd->session->seat);
439 void session_device_resume_all(Session *s) {
446 HASHMAP_FOREACH(sd, s->devices, i) {
448 r = session_device_start(sd);
450 session_device_notify(sd, SESSION_DEVICE_RESUME);
455 void session_device_pause_all(Session *s) {
461 HASHMAP_FOREACH(sd, s->devices, i) {
463 session_device_stop(sd);
464 session_device_notify(sd, SESSION_DEVICE_PAUSE);
469 unsigned int session_device_try_pause_all(Session *s) {
472 unsigned int num_pending = 0;
476 HASHMAP_FOREACH(sd, s->devices, i) {
478 session_device_notify(sd, SESSION_DEVICE_TRY_PAUSE);