chiark / gitweb /
consolidate TODO
[elogind.git] / src / logind-session.c
index 35213098466c89588984ed0eb3866e7f577c8a07..011fc8f5b55597082f350c54e9ba4ed44509b959 100644 (file)
@@ -297,7 +297,19 @@ int session_load(Session *s) {
                         s->type = t;
         }
 
-        session_open_fifo(s);
+        if (s->fifo_path) {
+                int fd;
+
+                /* If we open an unopened pipe for reading we will not
+                   get an EOF. to trigger an EOF we hence open it for
+                   reading, but close it right-away which then will
+                   trigger the EOF. */
+
+                fd = session_create_fifo(s);
+                if (fd >= 0)
+                        close_nointr_nofail(fd);
+        }
+
 
 finish:
         free(remote);
@@ -368,13 +380,15 @@ static int session_link_x11_socket(Session *s) {
                 return -ENOENT;
         }
 
-        t = strappend(s->user->runtime_path, "/display");
+        t = strappend(s->user->runtime_path, "/X11/display");
         if (!t) {
                 log_error("Out of memory");
                 free(f);
                 return -ENOMEM;
         }
 
+        mkdir_parents(t, 0755);
+
         if (link(f, t) < 0) {
                 if (errno == EEXIST) {
                         unlink(t);
@@ -574,7 +588,7 @@ static bool session_shall_kill(Session *s) {
         return strv_contains(s->manager->kill_only_users, s->user->name);
 }
 
-static int session_kill_cgroup(Session *s) {
+static int session_terminate_cgroup(Session *s) {
         int r;
         char **k;
 
@@ -626,7 +640,7 @@ static int session_unlink_x11_socket(Session *s) {
 
         s->user->display = NULL;
 
-        t = strappend(s->user->runtime_path, "/display");
+        t = strappend(s->user->runtime_path, "/X11/display");
         if (!t) {
                 log_error("Out of memory");
                 return -ENOMEM;
@@ -647,7 +661,7 @@ int session_stop(Session *s) {
                 log_info("Removed session %s.", s->id);
 
         /* Kill cgroup */
-        k = session_kill_cgroup(s);
+        k = session_terminate_cgroup(s);
         if (k < 0)
                 r = k;
 
@@ -768,42 +782,17 @@ void session_set_idle_hint(Session *s, bool b) {
                              "IdleSinceHintMonotonic\0");
 }
 
-int session_open_fifo(Session *s) {
-        struct epoll_event ev;
-        int r;
-
-        assert(s);
-
-        if (s->fifo_fd >= 0)
-                return 0;
-
-        if (!s->fifo_path)
-                return -EINVAL;
-
-        s->fifo_fd = open(s->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY);
-        if (s->fifo_fd < 0)
-                return -errno;
-
-        r = hashmap_put(s->manager->fifo_fds, INT_TO_PTR(s->fifo_fd + 1), s);
-        if (r < 0)
-                return r;
-
-        zero(ev);
-        ev.events = 0;
-        ev.data.u32 = FD_FIFO_BASE + s->fifo_fd;
-
-        if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, s->fifo_fd, &ev) < 0)
-                return -errno;
-
-        return 0;
-}
-
 int session_create_fifo(Session *s) {
         int r;
 
         assert(s);
 
+        /* Create FIFO */
         if (!s->fifo_path) {
+                r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0);
+                if (r < 0)
+                        return r;
+
                 if (asprintf(&s->fifo_path, "/run/systemd/sessions/%s.ref", s->id) < 0)
                         return -ENOMEM;
 
@@ -812,9 +801,24 @@ int session_create_fifo(Session *s) {
         }
 
         /* Open reading side */
-        r = session_open_fifo(s);
-        if (r < 0)
-                return r;
+        if (s->fifo_fd < 0) {
+                struct epoll_event ev;
+
+                s->fifo_fd = open(s->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY);
+                if (s->fifo_fd < 0)
+                        return -errno;
+
+                r = hashmap_put(s->manager->fifo_fds, INT_TO_PTR(s->fifo_fd + 1), s);
+                if (r < 0)
+                        return r;
+
+                zero(ev);
+                ev.events = 0;
+                ev.data.u32 = FD_FIFO_BASE + s->fifo_fd;
+
+                if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, s->fifo_fd, &ev) < 0)
+                        return -errno;
+        }
 
         /* Open writing side */
         r = open(s->fifo_path, O_WRONLY|O_CLOEXEC|O_NDELAY);
@@ -882,6 +886,47 @@ void session_add_to_gc_queue(Session *s) {
         s->in_gc_queue = true;
 }
 
+int session_kill(Session *s, KillWho who, int signo) {
+        int r = 0;
+        Set *pid_set = NULL;
+
+        assert(s);
+
+        if (!s->cgroup_path)
+                return -ESRCH;
+
+        if (s->leader <= 0 && who == KILL_LEADER)
+                return -ESRCH;
+
+        if (s->leader > 0)
+                if (kill(s->leader, signo) < 0)
+                        r = -errno;
+
+        if (who == KILL_ALL) {
+                int q;
+
+                pid_set = set_new(trivial_hash_func, trivial_compare_func);
+                if (!pid_set)
+                        return -ENOMEM;
+
+                if (s->leader > 0) {
+                        q = set_put(pid_set, LONG_TO_PTR(s->leader));
+                        if (q < 0)
+                                r = q;
+                }
+
+                q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, signo, false, true, false, pid_set);
+                if (q < 0)
+                        if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
+                                r = q;
+        }
+
+        if (pid_set)
+                set_free(pid_set);
+
+        return r;
+}
+
 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
         [SESSION_TTY] = "tty",
         [SESSION_X11] = "x11",
@@ -889,3 +934,10 @@ static const char* const session_type_table[_SESSION_TYPE_MAX] = {
 };
 
 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);
+
+static const char* const kill_who_table[_KILL_WHO_MAX] = {
+        [KILL_LEADER] = "leader",
+        [KILL_ALL] = "all"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);