chiark / gitweb /
logind: kill a session as soon as its pipe fd gets EOF
[elogind.git] / src / logind.c
index 863b1636df89a9fc8d0ee377a12a9737822d779d..8507c2e152a50f05b64b6f395fd2fd26764e3374 100644 (file)
@@ -652,7 +652,13 @@ static int vt_is_busy(int vtnr) {
 
         assert(vtnr >= 1);
 
-        fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
+        /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
+         * we'd open the latter we'd open the foreground tty which
+         * hence would be unconditionally busy. By opening /dev/tty1
+         * we avoid this. Since tty1 is special and needs to be an
+         * explicitly loaded getty or DM this is safe. */
+
+        fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
         if (fd < 0)
                 return -errno;
 
@@ -668,16 +674,61 @@ static int vt_is_busy(int vtnr) {
 
 int manager_spawn_autovt(Manager *m, int vtnr) {
         int r;
+        DBusMessage *message = NULL, *reply = NULL;
+        char *name = NULL;
+        const char *mode = "fail";
+        DBusError error;
 
         assert(m);
 
+        dbus_error_init(&error);
+
         r = vt_is_busy(vtnr);
         if (r != 0)
                 return r;
 
-        /* ... */
+        message = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit");
+        if (!message) {
+                log_error("Could not allocate message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (asprintf(&name, "autovt-getty@tty%i.service", vtnr) < 0) {
+                log_error("Could not allocate service name.");
+                r = -ENOMEM;
+                goto finish;
+        }
 
-        return 0;
+        if (!dbus_message_append_args(message,
+                                      DBUS_TYPE_STRING, &name,
+                                      DBUS_TYPE_STRING, &mode,
+                                      DBUS_TYPE_INVALID)) {
+                log_error("Could not attach target and flag information to message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        reply = dbus_connection_send_with_reply_and_block(m->bus, message, -1, &error);
+        if (!reply) {
+                log_error("Failed to start unit: %s", bus_error_message(&error));
+                goto finish;
+        }
+
+        r = 0;
+
+finish:
+        free(name);
+
+        if (message)
+                dbus_message_unref(message);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
 }
 
 void manager_cgroup_notify_empty(Manager *m, const char *cgroup) {
@@ -720,7 +771,7 @@ static void manager_pipe_notify_eof(Manager *m, int fd) {
         assert(s->pipe_fd == fd);
         session_unset_pipe_fd(s);
 
-        session_add_to_gc_queue(s);
+        session_stop(s);
 }
 
 static int manager_connect_bus(Manager *m) {
@@ -736,7 +787,7 @@ static int manager_connect_bus(Manager *m) {
 
         m->bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
         if (!m->bus) {
-                log_error("Failed to get system D-Bus connection: %s", error.message);
+                log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
                 r = -ECONNREFUSED;
                 goto fail;
         }
@@ -759,7 +810,7 @@ static int manager_connect_bus(Manager *m) {
                            &error);
 
         if (dbus_error_is_set(&error)) {
-                log_error("Failed to register match: %s", error.message);
+                log_error("Failed to register match: %s", bus_error_message(&error));
                 r = -EIO;
                 goto fail;
         }