chiark / gitweb /
logind: save/restore session devices and their respective file descriptors
[elogind.git] / src / login / logind-session-device.c
index e92bb54fffa85489e608c1b3b2714c5942ba2504..915f87dd5fd11894898d9f63ce686794ac885b9a 100644 (file)
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
 /***
   This file is part of systemd.
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <assert.h>
 #include <fcntl.h>
-#include <libudev.h>
 #include <linux/input.h>
-#include <linux/ioctl.h>
 #include <string.h>
 #include <sys/ioctl.h>
-#include <sys/stat.h>
 #include <sys/types.h>
-#include <unistd.h>
 
-#include "dbus-common.h"
+#if 0 /// elogind needs the systems udev header
+#include "libudev.h"
+#else
+#include <libudev.h>
+#endif // 0
+
+#include "alloc-util.h"
+#include "bus-util.h"
+#include "fd-util.h"
 #include "logind-session-device.h"
-#include "util.h"
 #include "missing.h"
+#include "util.h"
 
 enum SessionDeviceNotifications {
         SESSION_DEVICE_RESUME,
@@ -42,37 +43,42 @@ enum SessionDeviceNotifications {
         SESSION_DEVICE_RELEASE,
 };
 
-static void session_device_notify(SessionDevice *sd, enum SessionDeviceNotifications type) {
-        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
+static int session_device_notify(SessionDevice *sd, enum SessionDeviceNotifications type) {
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
         _cleanup_free_ char *path = NULL;
         const char *t = NULL;
+        uint32_t major, minor;
+        int r;
 
         assert(sd);
 
+        major = major(sd->dev);
+        minor = minor(sd->dev);
+
         if (!sd->session->controller)
-                return;
+                return 0;
 
         path = session_bus_path(sd->session);
         if (!path)
-                return;
+                return -ENOMEM;
 
-        m = dbus_message_new_signal(path,
-                                    "org.freedesktop.login1.Session",
-                                    (type == SESSION_DEVICE_RESUME) ? "ResumeDevice" : "PauseDevice");
+        r = sd_bus_message_new_signal(
+                        sd->session->manager->bus,
+                        &m, path,
+                        "org.freedesktop.login1.Session",
+                        (type == SESSION_DEVICE_RESUME) ? "ResumeDevice" : "PauseDevice");
         if (!m)
-                return;
+                return r;
 
-        if (!dbus_message_set_destination(m, sd->session->controller))
-                return;
+        r = sd_bus_message_set_destination(m, sd->session->controller);
+        if (r < 0)
+                return r;
 
         switch (type) {
         case SESSION_DEVICE_RESUME:
-                if (!dbus_message_append_args(m,
-                                              DBUS_TYPE_UINT32, major(sd->dev),
-                                              DBUS_TYPE_UINT32, minor(sd->dev),
-                                              DBUS_TYPE_UNIX_FD, &sd->fd,
-                                              DBUS_TYPE_INVALID))
-                        return;
+                r = sd_bus_message_append(m, "uuh", major, minor, sd->fd);
+                if (r < 0)
+                        return r;
                 break;
         case SESSION_DEVICE_TRY_PAUSE:
                 t = "pause";
@@ -84,17 +90,16 @@ static void session_device_notify(SessionDevice *sd, enum SessionDeviceNotificat
                 t = "gone";
                 break;
         default:
-                return;
+                return -EINVAL;
         }
 
-        if (t && !dbus_message_append_args(m,
-                                           DBUS_TYPE_UINT32, major(sd->dev),
-                                           DBUS_TYPE_UINT32, minor(sd->dev),
-                                           DBUS_TYPE_STRING, &t,
-                                           DBUS_TYPE_INVALID))
-                return;
+        if (t) {
+                r = sd_bus_message_append(m, "uus", major, minor, t);
+                if (r < 0)
+                        return r;
+        }
 
-        dbus_connection_send(sd->session->manager->bus, m, NULL);
+        return sd_bus_send(sd->session->manager->bus, m, NULL);
 }
 
 static int sd_eviocrevoke(int fd) {
@@ -103,7 +108,7 @@ static int sd_eviocrevoke(int fd) {
 
         assert(fd >= 0);
 
-        r = ioctl(fd, EVIOCREVOKE, 1);
+        r = ioctl(fd, EVIOCREVOKE, NULL);
         if (r < 0) {
                 r = -errno;
                 if (r == -EINVAL && !warned) {
@@ -140,7 +145,7 @@ static int sd_drmdropmaster(int fd) {
 }
 
 static int session_device_open(SessionDevice *sd, bool active) {
-        int fd;
+        int fd, r;
 
         assert(sd->type != DEVICE_TYPE_UNKNOWN);
 
@@ -151,9 +156,17 @@ static int session_device_open(SessionDevice *sd, bool active) {
 
         switch (sd->type) {
         case DEVICE_TYPE_DRM:
-                if (active)
-                        sd_drmsetmaster(fd);
-                else {
+                if (active) {
+                        /* Weird legacy DRM semantics might return an error
+                         * even though we're master. No way to detect that so
+                         * fail at all times and let caller retry in inactive
+                         * state. */
+                        r = sd_drmsetmaster(fd);
+                        if (r < 0) {
+                                close_nointr(fd);
+                                return r;
+                        }
+                } else {
                         /* DRM-Master is granted to the first user who opens a
                          * device automatically (ughh, racy!). Hence, we just
                          * drop DRM-Master in case we were the first. */
@@ -164,7 +177,6 @@ static int session_device_open(SessionDevice *sd, bool active) {
                 if (!active)
                         sd_eviocrevoke(fd);
                 break;
-        case DEVICE_TYPE_FBDEV:
         case DEVICE_TYPE_UNKNOWN:
         default:
                 /* fallback for devices wihout synchronizations */
@@ -198,17 +210,12 @@ static int session_device_start(SessionDevice *sd) {
                 r = session_device_open(sd, true);
                 if (r < 0)
                         return r;
-                close_nointr_nofail(sd->fd);
+                /* For evdev devices, the file descriptor might be left
+                 * uninitialized. This might happen while resuming into a
+                 * session and logind has been restarted right before. */
+                safe_close(sd->fd);
                 sd->fd = r;
                 break;
-        case DEVICE_TYPE_FBDEV:
-                /* fbdev devices have no way to synchronize access. Moreover,
-                 * they mostly operate through mmaps() without any pageflips
-                 * and modesetting, so there is no way for us to prevent access
-                 * but tear down mmaps.
-                 * That would be quite expensive to do on a per-fd context. So
-                 * ignore legcy fbdev and let its users feel the pain they asked
-                 * for when deciding for fbdev. */
         case DEVICE_TYPE_UNKNOWN:
         default:
                 /* fallback for devices wihout synchronizations */
@@ -240,7 +247,6 @@ static void session_device_stop(SessionDevice *sd) {
                  * protection this way. */
                 sd_eviocrevoke(sd->fd);
                 break;
-        case DEVICE_TYPE_FBDEV:
         case DEVICE_TYPE_UNKNOWN:
         default:
                 /* fallback for devices without synchronization */
@@ -258,10 +264,7 @@ static DeviceType detect_device_type(struct udev_device *dev) {
         subsystem = udev_device_get_subsystem(dev);
         type = DEVICE_TYPE_UNKNOWN;
 
-        if (streq_ptr(subsystem, "graphics")) {
-                if (!streq(sysname, "fbcon") && startswith(sysname, "fb"))
-                        type = DEVICE_TYPE_FBDEV;
-        } else if (streq_ptr(subsystem, "drm")) {
+        if (streq_ptr(subsystem, "drm")) {
                 if (startswith(sysname, "card"))
                         type = DEVICE_TYPE_DRM;
         } else if (streq_ptr(subsystem, "input")) {
@@ -302,8 +305,7 @@ static int session_device_verify(SessionDevice *sd) {
                         goto err_dev;
                 }
                 sp = udev_device_get_syspath(dev);
-        } else if (sd->type != DEVICE_TYPE_FBDEV &&
-                   sd->type != DEVICE_TYPE_DRM) {
+        } else if (sd->type != DEVICE_TYPE_DRM) {
                 /* Prevent opening unsupported devices. Especially devices of
                  * subsystem "input" must be opened via the evdev node as
                  * we require EVIOCREVOKE. */
@@ -346,7 +348,7 @@ err_dev:
         return r;
 }
 
-int session_device_new(Session *s, dev_t dev, SessionDevice **out) {
+int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **out) {
         SessionDevice *sd;
         int r;
 
@@ -369,30 +371,38 @@ int session_device_new(Session *s, dev_t dev, SessionDevice **out) {
         if (r < 0)
                 goto error;
 
-        assert_cc(sizeof(unsigned long) >= sizeof(dev_t));
-
-        r = hashmap_put(s->devices, ULONG_TO_PTR((unsigned long)sd->dev), sd);
+        r = hashmap_put(s->devices, &sd->dev, sd);
         if (r < 0) {
                 r = -ENOMEM;
                 goto error;
         }
 
-        /* Open the device for the first time. We need a valid fd to pass back
-         * to the caller. If the session is not active, this _might_ immediately
-         * revoke access and thus invalidate the fd. But this is still needed
-         * to pass a valid fd back. */
-        sd->active = session_is_active(s);
-        sd->fd = session_device_open(sd, sd->active);
-        if (sd->fd < 0)
-                goto error;
+        if (open_device) {
+                /* Open the device for the first time. We need a valid fd to pass back
+                 * to the caller. If the session is not active, this _might_ immediately
+                 * revoke access and thus invalidate the fd. But this is still needed
+                 * to pass a valid fd back. */
+                sd->active = session_is_active(s);
+                r = session_device_open(sd, sd->active);
+                if (r < 0) {
+                        /* EINVAL _may_ mean a master is active; retry inactive */
+                        if (sd->active && r == -EINVAL) {
+                                sd->active = false;
+                                r = session_device_open(sd, false);
+                        }
+                        if (r < 0)
+                                goto error;
+                }
+                sd->fd = r;
+        }
 
-        LIST_PREPEND(SessionDevice, sd_by_device, sd->device->session_devices, sd);
+        LIST_PREPEND(sd_by_device, sd->device->session_devices, sd);
 
         *out = sd;
         return 0;
 
 error:
-        hashmap_remove(s->devices, ULONG_TO_PTR((unsigned long)sd->dev));
+        hashmap_remove(s->devices, &sd->dev);
         free(sd->node);
         free(sd);
         return r;
@@ -403,11 +413,11 @@ void session_device_free(SessionDevice *sd) {
 
         session_device_stop(sd);
         session_device_notify(sd, SESSION_DEVICE_RELEASE);
-        close_nointr_nofail(sd->fd);
+        close_nointr(sd->fd);
 
-        LIST_REMOVE(SessionDevice, sd_by_device, sd->device->session_devices, sd);
+        LIST_REMOVE(sd_by_device, sd->device->session_devices, sd);
 
-        hashmap_remove(sd->session->devices, ULONG_TO_PTR((unsigned long)sd->dev));
+        hashmap_remove(sd->session->devices, &sd->dev);
 
         free(sd->node);
         free(sd);
@@ -434,15 +444,16 @@ void session_device_complete_pause(SessionDevice *sd) {
 void session_device_resume_all(Session *s) {
         SessionDevice *sd;
         Iterator i;
-        int r;
 
         assert(s);
 
         HASHMAP_FOREACH(sd, s->devices, i) {
                 if (!sd->active) {
-                        r = session_device_start(sd);
-                        if (!r)
-                                session_device_notify(sd, SESSION_DEVICE_RESUME);
+                        if (session_device_start(sd) < 0)
+                                continue;
+                        if (session_device_save(sd) < 0)
+                                continue;
+                        session_device_notify(sd, SESSION_DEVICE_RESUME);
                 }
         }
 }
@@ -477,3 +488,36 @@ unsigned int session_device_try_pause_all(Session *s) {
 
         return num_pending;
 }
+
+int session_device_save(SessionDevice *sd) {
+        _cleanup_free_ char *state = NULL;
+        int r;
+
+        assert(sd);
+
+        /* Store device fd in PID1. It will send it back to us on
+         * restart so revocation will continue to work. To make things
+         * simple, send fds for all type of devices even if they don't
+         * support the revocation mechanism so we don't have to handle
+         * them differently later.
+         *
+         * Note: for device supporting revocation, PID1 will drop a
+         * stored fd automatically if the corresponding device is
+         * revoked. */
+        r = asprintf(&state, "FDSTORE=1\n"
+                             "FDNAME=session-%s", sd->session->id);
+        if (r < 0)
+                return -ENOMEM;
+
+        return sd_pid_notify_with_fds(0, false, state, &sd->fd, 1);
+}
+
+void session_device_attach_fd(SessionDevice *sd, int fd, bool active) {
+        assert(fd > 0);
+        assert(sd);
+        assert(sd->fd < 0);
+        assert(!sd->active);
+
+        sd->fd = fd;
+        sd->active = active;
+}