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