chiark / gitweb /
logind: introduce session-devices
[elogind.git] / src / login / logind-session.c
index a726fb1bedcae8cf195985109ef77094eb972257..fcc1901ed626c9f2c21d6762fb42273bbac59ace 100644 (file)
@@ -41,6 +41,7 @@ Session* session_new(Manager *m, const char *id) {
 
         assert(m);
         assert(id);
+        assert(session_id_valid(id));
 
         s = new0(Session, 1);
         if (!s)
@@ -52,9 +53,17 @@ Session* session_new(Manager *m, const char *id) {
                 return NULL;
         }
 
+        s->devices = hashmap_new(trivial_hash_func, trivial_compare_func);
+        if (!s->devices) {
+                free(s->state_file);
+                free(s);
+                return NULL;
+        }
+
         s->id = path_get_file_name(s->state_file);
 
         if (hashmap_put(m->sessions, s->id, s) < 0) {
+                hashmap_free(s->devices);
                 free(s->state_file);
                 free(s);
                 return NULL;
@@ -67,11 +76,20 @@ Session* session_new(Manager *m, const char *id) {
 }
 
 void session_free(Session *s) {
+        SessionDevice *sd;
+
         assert(s);
 
         if (s->in_gc_queue)
                 LIST_REMOVE(Session, gc_queue, s->manager->session_gc_queue, s);
 
+        session_drop_controller(s);
+
+        while ((sd = hashmap_first(s->devices)))
+                session_device_free(sd);
+
+        hashmap_free(s->devices);
+
         if (s->user) {
                 LIST_REMOVE(Session, sessions_by_user, s->user->sessions, s);
 
@@ -188,7 +206,7 @@ int session_save(Session *s) {
         if (s->service)
                 fprintf(f, "SERVICE=%s\n", s->service);
 
-        if (s->seat && seat_can_multi_session(s->seat))
+        if (s->seat && seat_has_vts(s->seat))
                 fprintf(f, "VTNR=%i\n", s->vtnr);
 
         if (s->leader > 0)
@@ -298,7 +316,7 @@ int session_load(Session *s) {
                         seat_attach_session(o, s);
         }
 
-        if (vtnr && s->seat && seat_can_multi_session(s->seat)) {
+        if (vtnr && s->seat && seat_has_vts(s->seat)) {
                 int v;
 
                 k = safe_atoi(vtnr, &v);
@@ -357,12 +375,10 @@ int session_load(Session *s) {
 }
 
 int session_activate(Session *s) {
-        int r;
-
         assert(s);
         assert(s->user);
 
-        if (s->vtnr < 0)
+        if (s->vtnr <= 0)
                 return -ENOTSUP;
 
         if (!s->seat)
@@ -371,13 +387,9 @@ int session_activate(Session *s) {
         if (s->seat->active == s)
                 return 0;
 
-        assert(seat_is_vtconsole(s->seat));
+        assert(seat_has_vts(s->seat));
 
-        r = chvt(s->vtnr);
-        if (r < 0)
-                return r;
-
-        return seat_set_active(s->seat, s);
+        return chvt(s->vtnr);
 }
 
 static int session_link_x11_socket(Session *s) {
@@ -615,6 +627,7 @@ int session_stop(Session *s) {
 
 int session_finalize(Session *s) {
         int r = 0;
+        SessionDevice *sd;
 
         assert(s);
 
@@ -630,6 +643,10 @@ int session_finalize(Session *s) {
                            "MESSAGE=Removed session %s.", s->id,
                            NULL);
 
+        /* Kill session devices */
+        while ((sd = hashmap_first(s->devices)))
+                session_device_free(sd);
+
         /* Remove X11 symlink */
         session_unlink_x11_socket(s);
 
@@ -917,6 +934,59 @@ int session_kill(Session *s, KillWho who, int signo) {
         return manager_kill_unit(s->manager, s->scope, who, signo, NULL);
 }
 
+bool session_is_controller(Session *s, const char *sender)
+{
+        assert(s);
+
+        return streq_ptr(s->controller, sender);
+}
+
+int session_set_controller(Session *s, const char *sender, bool force) {
+        char *t;
+        int r;
+
+        assert(s);
+        assert(sender);
+
+        if (session_is_controller(s, sender))
+                return 0;
+        if (s->controller && !force)
+                return -EBUSY;
+
+        t = strdup(sender);
+        if (!t)
+                return -ENOMEM;
+
+        r = manager_watch_busname(s->manager, sender);
+        if (r) {
+                free(t);
+                return r;
+        }
+
+        session_drop_controller(s);
+
+        s->controller = t;
+        return 0;
+}
+
+void session_drop_controller(Session *s) {
+        SessionDevice *sd;
+
+        assert(s);
+
+        if (!s->controller)
+                return;
+
+        manager_drop_busname(s->manager, s->controller);
+        free(s->controller);
+        s->controller = NULL;
+
+        /* Drop all devices as they're now unused. Do that after the controller
+         * is released to avoid sending out useles dbus signals. */
+        while ((sd = hashmap_first(s->devices)))
+                session_device_free(sd);
+}
+
 static const char* const session_state_table[_SESSION_STATE_MAX] = {
         [SESSION_OPENING] = "opening",
         [SESSION_ONLINE] = "online",