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>
28 #include "alloc-util.h"
31 #include "logind-session-device.h"
35 enum SessionDeviceNotifications
{
36 SESSION_DEVICE_RESUME
,
37 SESSION_DEVICE_TRY_PAUSE
,
39 SESSION_DEVICE_RELEASE
,
42 static int session_device_notify(SessionDevice
*sd
, enum SessionDeviceNotifications type
) {
43 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
44 _cleanup_free_
char *path
= NULL
;
46 uint32_t major
, minor
;
51 major
= major(sd
->dev
);
52 minor
= minor(sd
->dev
);
54 if (!sd
->session
->controller
)
57 path
= session_bus_path(sd
->session
);
61 r
= sd_bus_message_new_signal(
62 sd
->session
->manager
->bus
,
64 "org.freedesktop.login1.Session",
65 (type
== SESSION_DEVICE_RESUME
) ? "ResumeDevice" : "PauseDevice");
69 r
= sd_bus_message_set_destination(m
, sd
->session
->controller
);
74 case SESSION_DEVICE_RESUME
:
75 r
= sd_bus_message_append(m
, "uuh", major
, minor
, sd
->fd
);
79 case SESSION_DEVICE_TRY_PAUSE
:
82 case SESSION_DEVICE_PAUSE
:
85 case SESSION_DEVICE_RELEASE
:
93 r
= sd_bus_message_append(m
, "uus", major
, minor
, t
);
98 return sd_bus_send(sd
->session
->manager
->bus
, m
, NULL
);
101 static int sd_eviocrevoke(int fd
) {
107 r
= ioctl(fd
, EVIOCREVOKE
, NULL
);
110 if (r
== -EINVAL
&& !warned
) {
112 log_warning("kernel does not support evdev-revocation");
119 static int sd_drmsetmaster(int fd
) {
124 r
= ioctl(fd
, DRM_IOCTL_SET_MASTER
, 0);
131 static int sd_drmdropmaster(int fd
) {
136 r
= ioctl(fd
, DRM_IOCTL_DROP_MASTER
, 0);
143 static int session_device_open(SessionDevice
*sd
, bool active
) {
146 assert(sd
->type
!= DEVICE_TYPE_UNKNOWN
);
148 /* open device and try to get an udev_device from it */
149 fd
= open(sd
->node
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
154 case DEVICE_TYPE_DRM
:
156 /* Weird legacy DRM semantics might return an error
157 * even though we're master. No way to detect that so
158 * fail at all times and let caller retry in inactive
160 r
= sd_drmsetmaster(fd
);
166 /* DRM-Master is granted to the first user who opens a
167 * device automatically (ughh, racy!). Hence, we just
168 * drop DRM-Master in case we were the first. */
169 sd_drmdropmaster(fd
);
172 case DEVICE_TYPE_EVDEV
:
176 case DEVICE_TYPE_UNKNOWN
:
178 /* fallback for devices wihout synchronizations */
185 static int session_device_start(SessionDevice
*sd
) {
189 assert(session_is_active(sd
->session
));
195 case DEVICE_TYPE_DRM
:
196 /* Device is kept open. Simply call drmSetMaster() and hope
197 * there is no-one else. In case it fails, we keep the device
198 * paused. Maybe at some point we have a drmStealMaster(). */
199 r
= sd_drmsetmaster(sd
->fd
);
203 case DEVICE_TYPE_EVDEV
:
204 /* Evdev devices are revoked while inactive. Reopen it and we
206 r
= session_device_open(sd
, true);
209 close_nointr(sd
->fd
);
212 case DEVICE_TYPE_UNKNOWN
:
214 /* fallback for devices wihout synchronizations */
222 static void session_device_stop(SessionDevice
*sd
) {
229 case DEVICE_TYPE_DRM
:
230 /* On DRM devices we simply drop DRM-Master but keep it open.
231 * This allows the user to keep resources allocated. The
232 * CAP_SYS_ADMIN restriction to DRM-Master prevents users from
233 * circumventing this. */
234 sd_drmdropmaster(sd
->fd
);
236 case DEVICE_TYPE_EVDEV
:
237 /* Revoke access on evdev file-descriptors during deactivation.
238 * This will basically prevent any operations on the fd and
239 * cannot be undone. Good side is: it needs no CAP_SYS_ADMIN
240 * protection this way. */
241 sd_eviocrevoke(sd
->fd
);
243 case DEVICE_TYPE_UNKNOWN
:
245 /* fallback for devices without synchronization */
252 static DeviceType
detect_device_type(struct udev_device
*dev
) {
253 const char *sysname
, *subsystem
;
256 sysname
= udev_device_get_sysname(dev
);
257 subsystem
= udev_device_get_subsystem(dev
);
258 type
= DEVICE_TYPE_UNKNOWN
;
260 if (streq_ptr(subsystem
, "drm")) {
261 if (startswith(sysname
, "card"))
262 type
= DEVICE_TYPE_DRM
;
263 } else if (streq_ptr(subsystem
, "input")) {
264 if (startswith(sysname
, "event"))
265 type
= DEVICE_TYPE_EVDEV
;
271 static int session_device_verify(SessionDevice
*sd
) {
272 struct udev_device
*dev
, *p
= NULL
;
273 const char *sp
, *node
;
276 dev
= udev_device_new_from_devnum(sd
->session
->manager
->udev
, 'c', sd
->dev
);
280 sp
= udev_device_get_syspath(dev
);
281 node
= udev_device_get_devnode(dev
);
287 /* detect device type so we can find the correct sysfs parent */
288 sd
->type
= detect_device_type(dev
);
289 if (sd
->type
== DEVICE_TYPE_UNKNOWN
) {
292 } else if (sd
->type
== DEVICE_TYPE_EVDEV
) {
293 /* for evdev devices we need the parent node as device */
295 dev
= udev_device_get_parent_with_subsystem_devtype(p
, "input", NULL
);
300 sp
= udev_device_get_syspath(dev
);
301 } else if (sd
->type
!= DEVICE_TYPE_DRM
) {
302 /* Prevent opening unsupported devices. Especially devices of
303 * subsystem "input" must be opened via the evdev node as
304 * we require EVIOCREVOKE. */
309 /* search for an existing seat device and return it if available */
310 sd
->device
= hashmap_get(sd
->session
->manager
->devices
, sp
);
312 /* The caller might have gotten the udev event before we were
313 * able to process it. Hence, fake the "add" event and let the
314 * logind-manager handle the new device. */
315 r
= manager_process_seat_device(sd
->session
->manager
, dev
);
319 /* if it's still not available, then the device is invalid */
320 sd
->device
= hashmap_get(sd
->session
->manager
->devices
, sp
);
327 if (sd
->device
->seat
!= sd
->session
->seat
) {
332 sd
->node
= strdup(node
);
340 udev_device_unref(p
? : dev
);
344 int session_device_new(Session
*s
, dev_t dev
, SessionDevice
**out
) {
354 sd
= new0(SessionDevice
, 1);
361 sd
->type
= DEVICE_TYPE_UNKNOWN
;
363 r
= session_device_verify(sd
);
367 r
= hashmap_put(s
->devices
, &sd
->dev
, sd
);
373 /* Open the device for the first time. We need a valid fd to pass back
374 * to the caller. If the session is not active, this _might_ immediately
375 * revoke access and thus invalidate the fd. But this is still needed
376 * to pass a valid fd back. */
377 sd
->active
= session_is_active(s
);
378 r
= session_device_open(sd
, sd
->active
);
380 /* EINVAL _may_ mean a master is active; retry inactive */
381 if (sd
->active
&& r
== -EINVAL
) {
383 r
= session_device_open(sd
, false);
390 LIST_PREPEND(sd_by_device
, sd
->device
->session_devices
, sd
);
396 hashmap_remove(s
->devices
, &sd
->dev
);
402 void session_device_free(SessionDevice
*sd
) {
405 session_device_stop(sd
);
406 session_device_notify(sd
, SESSION_DEVICE_RELEASE
);
407 close_nointr(sd
->fd
);
409 LIST_REMOVE(sd_by_device
, sd
->device
->session_devices
, sd
);
411 hashmap_remove(sd
->session
->devices
, &sd
->dev
);
417 void session_device_complete_pause(SessionDevice
*sd
) {
424 session_device_stop(sd
);
426 /* if not all devices are paused, wait for further completion events */
427 HASHMAP_FOREACH(iter
, sd
->session
->devices
, i
)
431 /* complete any pending session switch */
432 seat_complete_switch(sd
->session
->seat
);
435 void session_device_resume_all(Session
*s
) {
442 HASHMAP_FOREACH(sd
, s
->devices
, i
) {
444 r
= session_device_start(sd
);
446 session_device_notify(sd
, SESSION_DEVICE_RESUME
);
451 void session_device_pause_all(Session
*s
) {
457 HASHMAP_FOREACH(sd
, s
->devices
, i
) {
459 session_device_stop(sd
);
460 session_device_notify(sd
, SESSION_DEVICE_PAUSE
);
465 unsigned int session_device_try_pause_all(Session
*s
) {
468 unsigned int num_pending
= 0;
472 HASHMAP_FOREACH(sd
, s
->devices
, i
) {
474 session_device_notify(sd
, SESSION_DEVICE_TRY_PAUSE
);