chiark / gitweb /
Update some message formats
[elogind.git] / src / login / logind-session.c
index cd87088456782b07d2b72756ca83a882d03277f4..bec59c044bb8639174b3def54e8def1700db73c4 100644 (file)
 #include "bus-error.h"
 #include "logind-session.h"
 
-static unsigned devt_hash_func(const void *p) {
+static unsigned long devt_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
         uint64_t u = *(const dev_t*)p;
 
-        return uint64_hash_func(&u);
+        return uint64_hash_func(&u, hash_key);
 }
 
 static int devt_compare_func(const void *_a, const void *_b) {
@@ -79,7 +79,7 @@ Session* session_new(Manager *m, const char *id) {
                 return NULL;
         }
 
-        s->id = path_get_file_name(s->state_file);
+        s->id = basename(s->state_file);
 
         if (hashmap_put(m->sessions, s->id, s) < 0) {
                 hashmap_free(s->devices);
@@ -125,6 +125,7 @@ void session_free(Session *s) {
                 if (s->seat->pending_switch == s)
                         s->seat->pending_switch = NULL;
 
+                seat_evict_position(s->seat, s);
                 LIST_REMOVE(sessions_by_seat, s->seat->sessions, s);
         }
 
@@ -142,6 +143,7 @@ void session_free(Session *s) {
         free(s->remote_host);
         free(s->remote_user);
         free(s->service);
+        free(s->desktop);
 
         hashmap_remove(s->manager->sessions, s->id);
 
@@ -184,12 +186,12 @@ int session_save(Session *s) {
 
         fprintf(f,
                 "# This is private data. Do not parse.\n"
-                "UID=%lu\n"
+                "UID="UID_FMT"\n"
                 "USER=%s\n"
                 "ACTIVE=%i\n"
                 "STATE=%s\n"
                 "REMOTE=%i\n",
-                (unsigned long) s->user->uid,
+                s->user->uid,
                 s->user->name,
                 session_is_active(s),
                 session_state_to_string(session_get_state(s)),
@@ -228,21 +230,27 @@ int session_save(Session *s) {
         if (s->service)
                 fprintf(f, "SERVICE=%s\n", s->service);
 
+        if (s->desktop)
+                fprintf(f, "DESKTOP=%s\n", s->desktop);
+
         if (s->seat && seat_has_vts(s->seat))
                 fprintf(f, "VTNR=%u\n", s->vtnr);
 
+        if (!s->vtnr)
+                fprintf(f, "POS=%u\n", s->pos);
+
         if (s->leader > 0)
-                fprintf(f, "LEADER=%lu\n", (unsigned long) s->leader);
+                fprintf(f, "LEADER="PID_FMT"\n", s->leader);
 
         if (s->audit_id > 0)
                 fprintf(f, "AUDIT=%"PRIu32"\n", s->audit_id);
 
         if (dual_timestamp_is_set(&s->timestamp))
                 fprintf(f,
-                        "REALTIME=%llu\n"
-                        "MONOTONIC=%llu\n",
-                        (unsigned long long) s->timestamp.realtime,
-                        (unsigned long long) s->timestamp.monotonic);
+                        "REALTIME="USEC_FMT"\n"
+                        "MONOTONIC="USEC_FMT"\n",
+                        s->timestamp.realtime,
+                        s->timestamp.monotonic);
 
         if (s->controller)
                 fprintf(f, "CONTROLLER=%s\n", s->controller);
@@ -257,7 +265,7 @@ int session_save(Session *s) {
 
 finish:
         if (r < 0)
-                log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
+                log_error("Failed to save session data %s: %s", s->state_file, strerror(-r));
 
         return r;
 }
@@ -266,6 +274,7 @@ int session_load(Session *s) {
         _cleanup_free_ char *remote = NULL,
                 *seat = NULL,
                 *vtnr = NULL,
+                *pos = NULL,
                 *leader = NULL,
                 *type = NULL,
                 *class = NULL,
@@ -289,7 +298,9 @@ int session_load(Session *s) {
                            "REMOTE_HOST",    &s->remote_host,
                            "REMOTE_USER",    &s->remote_user,
                            "SERVICE",        &s->service,
+                           "DESKTOP",        &s->desktop,
                            "VTNR",           &vtnr,
+                           "POS",            &pos,
                            "LEADER",         &leader,
                            "TYPE",           &type,
                            "CLASS",          &class,
@@ -334,20 +345,27 @@ int session_load(Session *s) {
                         s->remote = k;
         }
 
+        if (vtnr)
+                safe_atou(vtnr, &s->vtnr);
+
         if (seat && !s->seat) {
                 Seat *o;
 
                 o = hashmap_get(s->manager->seats, seat);
                 if (o)
-                        seat_attach_session(o, s);
+                        r = seat_attach_session(o, s);
+                if (!o || r < 0)
+                        log_error("Cannot attach session %s to seat %s", s->id, seat);
         }
 
-        if (vtnr && s->seat && seat_has_vts(s->seat)) {
-                unsigned int v;
+        if (!s->seat || !seat_has_vts(s->seat))
+                s->vtnr = 0;
 
-                k = safe_atou(vtnr, &v);
-                if (k >= 0 && v >= 1)
-                        s->vtnr = v;
+        if (pos && s->seat) {
+                unsigned int npos;
+
+                safe_atou(pos, &npos);
+                seat_claim_position(s->seat, s, npos);
         }
 
         if (leader) {
@@ -444,72 +462,6 @@ int session_activate(Session *s) {
         return 0;
 }
 
-static int session_link_x11_socket(Session *s) {
-        _cleanup_free_ char *t = NULL, *f = NULL;
-        char *c;
-        size_t k;
-
-        assert(s);
-        assert(s->user);
-        assert(s->user->runtime_path);
-
-        if (s->user->display)
-                return 0;
-
-        if (!s->display || !display_is_local(s->display))
-                return 0;
-
-        k = strspn(s->display+1, "0123456789");
-        f = new(char, sizeof("/tmp/.X11-unix/X") + k);
-        if (!f)
-                return log_oom();
-
-        c = stpcpy(f, "/tmp/.X11-unix/X");
-        memcpy(c, s->display+1, k);
-        c[k] = 0;
-
-        if (access(f, F_OK) < 0) {
-                log_warning("Session %s has display %s with non-existing socket %s.", s->id, s->display, f);
-                return -ENOENT;
-        }
-
-        /* Note that this cannot be in a subdir to avoid
-         * vulnerabilities since we are privileged but the runtime
-         * path is owned by the user */
-
-        t = strappend(s->user->runtime_path, "/X11-display");
-        if (!t)
-                return log_oom();
-
-        if (link(f, t) < 0) {
-                if (errno == EEXIST) {
-                        unlink(t);
-
-                        if (link(f, t) >= 0)
-                                goto done;
-                }
-
-                if (symlink(f, t) < 0) {
-
-                        if (errno == EEXIST) {
-                                unlink(t);
-
-                                if (symlink(f, t) >= 0)
-                                        goto done;
-                        }
-
-                        log_error("Failed to link %s to %s: %m", f, t);
-                        return -errno;
-                }
-        }
-
-done:
-        log_info("Linked %s to %s.", f, t);
-        s->user->display = s;
-
-        return 0;
-}
-
 static int session_start_scope(Session *s) {
         int r;
 
@@ -573,7 +525,7 @@ int session_start(Session *s) {
         if (r < 0)
                 return r;
 
-        log_struct(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG,
+        log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO,
                    MESSAGE_ID(SD_MESSAGE_SESSION_START),
                    "SESSION_ID=%s", s->id,
                    "USER_ID=%s", s->user->name,
@@ -581,9 +533,6 @@ int session_start(Session *s) {
                    "MESSAGE=New session %s of user %s.", s->id, s->user->name,
                    NULL);
 
-        /* Create X11 symlink */
-        session_link_x11_socket(s);
-
         if (!dual_timestamp_is_set(&s->timestamp))
                 dual_timestamp_get(&s->timestamp);
 
@@ -634,26 +583,6 @@ static int session_stop_scope(Session *s) {
         return 0;
 }
 
-static int session_unlink_x11_socket(Session *s) {
-        _cleanup_free_ char *t = NULL;
-        int r;
-
-        assert(s);
-        assert(s->user);
-
-        if (s->user->display != s)
-                return 0;
-
-        s->user->display = NULL;
-
-        t = strappend(s->user->runtime_path, "/X11-display");
-        if (!t)
-                return log_oom();
-
-        r = unlink(t);
-        return r < 0 ? -errno : 0;
-}
-
 int session_stop(Session *s) {
         int r;
 
@@ -681,7 +610,7 @@ int session_finalize(Session *s) {
                 return -ESTALE;
 
         if (s->started)
-                log_struct(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG,
+                log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO,
                            MESSAGE_ID(SD_MESSAGE_SESSION_STOP),
                            "SESSION_ID=%s", s->id,
                            "USER_ID=%s", s->user->name,
@@ -693,9 +622,6 @@ int session_finalize(Session *s) {
         while ((sd = hashmap_first(s->devices)))
                 session_device_free(sd);
 
-        /* Remove X11 symlink */
-        session_unlink_x11_socket(s);
-
         unlink(s->state_file);
         session_add_to_gc_queue(s);
         user_add_to_gc_queue(s->user);
@@ -954,9 +880,6 @@ void session_add_to_gc_queue(Session *s) {
 SessionState session_get_state(Session *s) {
         assert(s);
 
-        if (s->closing)
-                return SESSION_CLOSING;
-
         if (s->scope_job)
                 return SESSION_OPENING;
 
@@ -1159,7 +1082,8 @@ DEFINE_STRING_TABLE_LOOKUP(session_state, SessionState);
 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
         [SESSION_TTY] = "tty",
         [SESSION_X11] = "x11",
-        [SESSION_UNSPECIFIED] = "unspecified"
+        [SESSION_WAYLAND] = "wayland",
+        [SESSION_UNSPECIFIED] = "unspecified",
 };
 
 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);