chiark / gitweb /
Beginnings of handling suspend/etc within logind
[elogind.git] / src / login / logind-session.c
index f03c2e565d6f69a6e10cd176ae8a53d00b88535d..c7c88f863bf6d59e19024dd6203c82188092e92c 100644 (file)
 #include "audit.h"
 #include "bus-util.h"
 #include "bus-error.h"
-#include "cgroup-util.h"
-#include "def.h"
 #include "logind-session.h"
+#include "formats-util.h"
+#include "terminal-util.h"
+
+#define RELEASE_USEC (20*USEC_PER_SEC)
 
 static void session_remove_fifo(Session *s);
 
@@ -250,7 +252,7 @@ int session_save(Session *s) {
                 fprintf(f, "VTNR=%u\n", s->vtnr);
 
         if (!s->vtnr)
-                fprintf(f, "POS=%u\n", s->pos);
+                fprintf(f, "POSITION=%u\n", s->position);
 
         if (s->leader > 0)
                 fprintf(f, "LEADER="PID_FMT"\n", s->leader);
@@ -288,7 +290,7 @@ int session_load(Session *s) {
                 *seat = NULL,
                 *vtnr = NULL,
                 *state = NULL,
-                *pos = NULL,
+                *position = NULL,
                 *leader = NULL,
                 *type = NULL,
                 *class = NULL,
@@ -313,7 +315,7 @@ int session_load(Session *s) {
                            "DESKTOP",        &s->desktop,
                            "VTNR",           &vtnr,
                            "STATE",          &state,
-                           "POS",            &pos,
+                           "POSITION",       &position,
                            "LEADER",         &leader,
                            "TYPE",           &type,
                            "CLASS",          &class,
@@ -372,10 +374,10 @@ int session_load(Session *s) {
         if (!s->seat || !seat_has_vts(s->seat))
                 s->vtnr = 0;
 
-        if (pos && s->seat) {
+        if (position && s->seat) {
                 unsigned int npos;
 
-                safe_atou(pos, &npos);
+                safe_atou(position, &npos);
                 seat_claim_position(s->seat, s, npos);
         }
 
@@ -477,25 +479,6 @@ int session_activate(Session *s) {
         return 0;
 }
 
-static int session_start_cgroup(Session *s) {
-        int r;
-
-        assert(s);
-        assert(s->user);
-        assert(s->leader > 0);
-
-        /* First, create our own group */
-        r = cg_create(SYSTEMD_CGROUP_CONTROLLER, s->id);
-        if (r < 0)
-                return log_error_errno(r, "Failed to create cgroup %s: %m", s->id);
-
-        r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, s->id, s->leader);
-        if (r < 0)
-                log_warning_errno(r, "Failed to attach PID %d to cgroup %s: %m", s->leader, s->id);
-
-        return 0;
-}
-
 int session_start(Session *s) {
         int r;
 
@@ -511,10 +494,6 @@ int session_start(Session *s) {
         if (r < 0)
                 return r;
 
-        r = session_start_cgroup(s);
-        if (r < 0)
-                return r;
-
         log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO,
                    LOG_MESSAGE_ID(SD_MESSAGE_SESSION_START),
                    "SESSION_ID=%s", s->id,
@@ -552,23 +531,8 @@ int session_start(Session *s) {
         return 0;
 }
 
-static int session_stop_cgroup(Session *s, bool force) {
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        int r;
-
-        assert(s);
-
-        if (force || manager_shall_kill(s->manager, s->user->name)) {
-                r = session_kill(s, KILL_ALL, SIGTERM);
-                if (r < 0)
-                        return r;
-        }
-
-        return 0;
-}
-
 int session_stop(Session *s, bool force) {
-        int r;
+        int r = 0;
 
         assert(s);
 
@@ -577,12 +541,12 @@ int session_stop(Session *s, bool force) {
 
         s->timer_event_source = sd_event_source_unref(s->timer_event_source);
 
+        if (s->seat)
+                seat_evict_position(s->seat, s);
+
         /* We are going down, don't care about FIFOs anymore */
         session_remove_fifo(s);
 
-        /* Kill cgroup */
-        r = session_stop_cgroup(s, force);
-
         s->stopping = true;
 
         user_elect_display(s->user);
@@ -613,6 +577,9 @@ int session_finalize(Session *s) {
 
         s->timer_event_source = sd_event_source_unref(s->timer_event_source);
 
+        if (s->seat)
+                seat_evict_position(s->seat, s);
+
         /* Kill session devices */
         while ((sd = hashmap_first(s->devices)))
                 session_device_free(sd);
@@ -640,6 +607,16 @@ int session_finalize(Session *s) {
         return r;
 }
 
+static int release_timeout_callback(sd_event_source *es, uint64_t usec, void *userdata) {
+        Session *s = userdata;
+
+        assert(es);
+        assert(s);
+
+        session_stop(s, false);
+        return 0;
+}
+
 int session_release(Session *s) {
         assert(s);
 
@@ -649,10 +626,11 @@ int session_release(Session *s) {
         if (s->timer_event_source)
                 return 0;
 
-        /* In systemd, session release is triggered by user jobs
-           dying.  In elogind we don't have that so go ahead and stop
-           now.  */
-        return session_stop(s, false);
+        return sd_event_add_time(s->manager->event,
+                                 &s->timer_event_source,
+                                 CLOCK_MONOTONIC,
+                                 now(CLOCK_MONOTONIC) + RELEASE_USEC, 0,
+                                 release_timeout_callback, s);
 }
 
 bool session_is_active(Session *s) {
@@ -857,9 +835,6 @@ bool session_check_gc(Session *s, bool drop_not_started) {
                         return true;
         }
 
-        if (cg_is_empty_recursive (SYSTEMD_CGROUP_CONTROLLER, s->id, false) > 0)
-                return true;
-
         return false;
 }
 
@@ -892,23 +867,8 @@ SessionState session_get_state(Session *s) {
 int session_kill(Session *s, KillWho who, int signo) {
         assert(s);
 
-        if (who == KILL_LEADER) {
-                if (s->leader <= 0)
-                        return -ESRCH;
-
-                /* FIXME: verify that leader is in cgroup?  */
-
-                if (kill(s->leader, signo) < 0) {
-                        return log_error_errno(errno, "Failed to kill process leader %d for session %s: %m", s->leader, s->id);
-                }
-                return 0;
-        } else {
-                bool sigcont = false;
-                bool ignore_self = true;
-                bool rem = true;
-                return cg_kill_recursive (SYSTEMD_CGROUP_CONTROLLER, s->id, signo,
-                                          sigcont, ignore_self, rem, NULL);
-        }
+        /* No way to kill the session without cgroups.  */
+        return -ESRCH;
 }
 
 static int session_open_vt(Session *s) {