chiark / gitweb /
logind: check whether newly created session is active
[elogind.git] / src / logind-session.c
index 74f8ad1b0c60b8048c7d6634eb495f0fcdcc06cb..8e35e09069c4b503035375238d2eee4ae2dca04d 100644 (file)
@@ -22,6 +22,7 @@
 #include <errno.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/epoll.h>
 
 #include "logind-session.h"
 #include "strv.h"
@@ -83,6 +84,9 @@ void session_free(Session *s) {
                 LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s);
         }
 
+        if (s->cgroup_path)
+                hashmap_remove(s->manager->cgroups, s->cgroup_path);
+
         free(s->cgroup_path);
         strv_free(s->controllers);
 
@@ -94,8 +98,7 @@ void session_free(Session *s) {
 
         hashmap_remove(s->manager->sessions, s->id);
 
-        if (s->pipe_fd >= 0)
-                close_nointr_nofail(s->pipe_fd);
+        session_unset_pipe_fd(s);
 
         free(s->state_file);
         free(s);
@@ -108,6 +111,9 @@ int session_save(Session *s) {
 
         assert(s);
 
+        if (!s->started)
+                return 0;
+
         r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0);
         if (r < 0)
                 goto finish;
@@ -133,6 +139,11 @@ int session_save(Session *s) {
                 s->remote,
                 s->kill_processes);
 
+        if (s->type >= 0)
+                fprintf(f,
+                        "TYPE=%s\n",
+                        session_type_to_string(s->type));
+
         if (s->cgroup_path)
                 fprintf(f,
                         "CGROUP=%s\n",
@@ -207,7 +218,8 @@ int session_load(Session *s) {
                 *seat = NULL,
                 *vtnr = NULL,
                 *leader = NULL,
-                *audit_id = NULL;
+                *audit_id = NULL,
+                *type = NULL;
 
         int k, r;
 
@@ -225,6 +237,7 @@ int session_load(Session *s) {
                            "SERVICE",        &s->service,
                            "VTNR",           &vtnr,
                            "LEADER",         &leader,
+                           "TYPE",           &type,
                            NULL);
 
         if (r < 0)
@@ -269,6 +282,14 @@ int session_load(Session *s) {
                 }
         }
 
+        if (type) {
+                SessionType t;
+
+                t = session_type_from_string(type);
+                if (t >= 0)
+                        s->type = t;
+        }
+
 finish:
         free(remote);
         free(kill_processes);
@@ -307,14 +328,6 @@ int session_activate(Session *s) {
         return seat_apply_acls(s->seat, old_active);
 }
 
-bool x11_display_is_local(const char *display) {
-        assert(display);
-
-        return
-                display[0] == ':' &&
-                display[1] >= '0' &&
-                display[1] <= '9';
-}
 
 static int session_link_x11_socket(Session *s) {
         char *t, *f, *c;
@@ -327,7 +340,7 @@ static int session_link_x11_socket(Session *s) {
         if (s->user->display)
                 return 0;
 
-        if (!s->display || !x11_display_is_local(s->display))
+        if (!s->display || !display_is_local(s->display))
                 return 0;
 
         k = strspn(s->display+1, "0123456789");
@@ -395,9 +408,11 @@ static int session_create_one_group(Session *s, const char *controller, const ch
         assert(controller);
         assert(path);
 
-        if (s->leader > 0)
+        if (s->leader > 0) {
                 r = cg_create_and_attach(controller, path, s->leader);
-        else
+                if (r < 0)
+                        r = cg_create(controller, path);
+        } else
                 r = cg_create(controller, path);
 
         if (r < 0)
@@ -429,9 +444,9 @@ static int session_create_cgroup(Session *s) {
 
         r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p);
         if (r < 0) {
+                log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
                 free(p);
                 s->cgroup_path = NULL;
-                log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
                 return r;
         }
 
@@ -468,6 +483,8 @@ static int session_create_cgroup(Session *s) {
                 }
         }
 
+        hashmap_put(s->manager->cgroups, s->cgroup_path, s);
+
         return 0;
 }
 
@@ -494,13 +511,16 @@ int session_start(Session *s) {
         /* Create X11 symlink */
         session_link_x11_socket(s);
 
-        /* Save session data */
-        session_save(s);
-
         dual_timestamp_get(&s->timestamp);
 
+        if (s->seat)
+                seat_read_active_vt(s->seat);
+
         s->started = true;
 
+        /* Save session data */
+        session_save(s);
+
         session_send_signal(s, true);
 
         if (s->seat) {
@@ -562,6 +582,8 @@ static int session_kill_cgroup(Session *s) {
         STRV_FOREACH(k, s->user->manager->controllers)
                 cg_trim(*k, s->cgroup_path, true);
 
+        hashmap_remove(s->manager->cgroups, s->cgroup_path);
+
         free(s->cgroup_path);
         s->cgroup_path = NULL;
 
@@ -722,6 +744,45 @@ void session_set_idle_hint(Session *s, bool b) {
                              "IdleSinceHintMonotonic\0");
 }
 
+int session_set_pipe_fd(Session *s, int fd) {
+        struct epoll_event ev;
+        int r;
+
+        assert(s);
+        assert(fd >= 0);
+        assert(s->pipe_fd < 0);
+
+        r = hashmap_put(s->manager->pipe_fds, INT_TO_PTR(fd + 1), s);
+        if (r < 0)
+                return r;
+
+        zero(ev);
+        ev.events = 0;
+        ev.data.u32 = FD_PIPE_BASE + fd;
+
+        if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
+                assert_se(hashmap_remove(s->manager->pipe_fds, INT_TO_PTR(fd + 1)) == s);
+                return -errno;
+        }
+
+        s->pipe_fd = fd;
+        return 0;
+}
+
+void session_unset_pipe_fd(Session *s) {
+        assert(s);
+
+        if (s->pipe_fd < 0)
+                return;
+
+        assert_se(hashmap_remove(s->manager->pipe_fds, INT_TO_PTR(s->pipe_fd + 1)) == s);
+
+        assert_se(epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_DEL, s->pipe_fd, NULL) == 0);
+
+        close_nointr_nofail(s->pipe_fd);
+        s->pipe_fd = -1;
+}
+
 int session_check_gc(Session *s) {
         int r;
 
@@ -763,7 +824,7 @@ void session_add_to_gc_queue(Session *s) {
 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
         [SESSION_TTY] = "tty",
         [SESSION_X11] = "x11",
-        [SESSION_OTHER] = "other"
+        [SESSION_UNSPECIFIED] = "unspecified"
 };
 
 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);