Keep the last session file fresh whenever possible 08/325308/3
authorMichal Bloch <m.bloch@samsung.com>
Fri, 6 Jun 2025 16:10:14 +0000 (18:10 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Mon, 9 Jun 2025 13:42:05 +0000 (15:42 +0200)
Change-Id: I89fc04e74d7f18e92b554bf4d3517a48e033b89e

src/service/src/main_restore.cpp

index 81542f2ebd44645bca57dca99f34141bedff1549..247fba88f5bf3ac2e67d24005d97f39d4e366bd7 100644 (file)
@@ -85,7 +85,7 @@ static std::string get_last_subsession_of_user(int uid)
        return last_subsession;
 }
 
-static void restore_user_session(const fs::path& username, int uid)
+static std::string restore_user_session(const fs::path& username, int uid)
 {
        std::string last_subsession;
        try {
@@ -95,17 +95,17 @@ static void restore_user_session(const fs::path& username, int uid)
 
                /* At first boot, the last subsession file won't exist.
                 * This is common and fine - don't rethrow. */
-               return;
+               return SUBSESSION_INITIAL_SID;
        }
 
        if (!subsession_exists(uid, last_subsession)) {
                LOGW("User %s (uid %d) last subsession identified as %s but did not exist - ignoring", username.c_str(), uid, last_subsession.c_str());
-               return;
+               return SUBSESSION_INITIAL_SID;
        }
 
        if (last_subsession == SUBSESSION_INITIAL_SID) {
                LOGI("User %s (uid %d) last subsession was empty - nothing to do", username.c_str(), uid);
-               return;
+               return SUBSESSION_INITIAL_SID;
        }
 
        LOGI("User %s (uid %d) last subsession identified as \"%s\" - switching", username.c_str(), uid, last_subsession.c_str());
@@ -117,14 +117,14 @@ static void restore_user_session(const fs::path& username, int uid)
 
        try {
                switch_user_subsession(uid, SUBSESSION_INITIAL_SID, last_subsession);
-               if (g_sessiond_context)
-                       g_sessiond_context->save_last_subsession(uid, last_subsession);
        } catch (const std::exception &ex) {
                LOGE("Could not switch to last subsession %s of user %s (uid %d)", last_subsession.c_str(), username.c_str(), uid);
        }
 
        if (lock_fd != -1)
                restoration_unlock(lock_fd);
+
+       return last_subsession;
 }
 
 static void remount_image_sessions(int uid) try
@@ -195,7 +195,15 @@ void restore_all_user_sessions(bool restore_only)
                remount_image_sessions(uid);
 
                LOGI("Restoring last session for user %s (uid %d)", username.c_str(), uid);
-               restore_user_session(username, uid);
+               const auto restored_session = restore_user_session(username, uid);
+
+               /* Most of the time we've restored whatever the `.last_subsession` file
+                * told us to restore, so this is a bit redundant, but it's important to
+                * do this to make sure that the entry is always up-to-date. This is not
+                * just for correctness, but also to avoid the slow D-Bus fallback for
+                * GetCurrentSubsession during the performance-sensitive early boot period. */
+               if (!restore_only && g_sessiond_context)
+                       g_sessiond_context->save_last_subsession(uid, restored_session);
        }
 }