Restore last subsession at sessiond start 52/322552/4
authorMichal Bloch <m.bloch@samsung.com>
Thu, 10 Apr 2025 19:55:32 +0000 (21:55 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Mon, 14 Apr 2025 11:11:46 +0000 (13:11 +0200)
And add a restore-only mode in case work needs to be done
such as for fixed-size backend that needs to be remounted.

Change-Id: Iee7d831889ecac7e1a913e7acb61eb9a34ba82a5

src/service/CMakeLists.txt
src/service/src/main.cpp
src/service/src/main_context.hpp
src/service/src/main_restore.cpp [new file with mode: 0644]
src/service/src/main_restore.hpp [new file with mode: 0644]

index 8eb9cff28c4bead442a35878b83b0dfa26de0de3..13ee438037c0c9fb914f61fd4c7a08e025879e08 100644 (file)
@@ -9,6 +9,7 @@ pkg_check_modules(DEPS REQUIRED IMPORTED_TARGET
 set(
        sessiond_SRCS
        src/main.cpp
+       src/main_restore.cpp
        src/fs_helpers.cpp
        src/os_ops.cpp
        src/plugin.cpp
index 19a0c73da4b20ee5932e9ddced71865a5feb8dd6..07a3ff92c571780a8f634511bdb05ca444f1154e 100644 (file)
  * THE SOFTWARE. */
 
 #include "main_context.hpp"
+#include "main_restore.hpp"
 #include <stdexcept>
 
 std::unique_ptr <sessiond_context> g_sessiond_context;
 
-int main() try {
-       g_sessiond_context = std::make_unique <sessiond_context> ();
-       g_sessiond_context->run();
+bool isRestoreOnly(int argc, char **argv)
+{
+       if (argc < 2)
+               return false;
+
+       return std::string_view(argv[1]) == "--restore-only";
+}
+
+int main(int argc, char **argv) try {
+       if (isRestoreOnly(argc, argv)) {
+               restore_all_user_sessions();
+       } else {
+               g_sessiond_context = std::make_unique <sessiond_context> ();
+               restore_all_user_sessions();
+               g_sessiond_context->run();
+       }
 } catch (const std::exception &ex) {
        LOGE("Exception %s caught in top scope! Bailing out...", ex.what());
        return EXIT_FAILURE;
index e38d0b2cd33e2f128b37fbac1e1a2c25faea544d..149af64407b6169c85a7576291b2d952ae5d72cd 100644 (file)
@@ -36,6 +36,8 @@
 #include "sessiond-internal.h"
 #include "globals.hpp"
 #include "fs_helpers.hpp"
+#include "main_restore.hpp"
+#include "os_ops.hpp"
 #include "tuple_g_variant_helpers.hpp"
 #include "tuple_hash.hpp"
 #include "wait_manager.hpp"
diff --git a/src/service/src/main_restore.cpp b/src/service/src/main_restore.cpp
new file mode 100644 (file)
index 0000000..aee9852
--- /dev/null
@@ -0,0 +1,108 @@
+/* MIT License
+ *
+ * Copyright (c) 2025 Samsung Electronics Co., Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is furnished
+ * to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE. */
+
+#include "main_restore.hpp"
+
+#include "fs_helpers.hpp"
+#include "main_context.hpp"
+#include "os_ops.hpp"
+
+#include <filesystem>
+#include <fstream>
+
+#undef LOG_TAG
+#define LOG_TAG "SESSIOND"
+#include <dlog.h>
+
+/* TODO: without some sort of lock, restoration is racy, since in case the "real" sessiond
+ * manages to start before the restore-only version is done, they could both be trying to
+ * mount the same fixed-size subsession. */
+static int restoration_lock(int uid)
+{
+       return -1;
+}
+
+static void restoration_unlock(int fd)
+{
+}
+
+static void restore_user_session(const fs::path& user_home_dir)
+{
+       /* Somewhere below we get username back from uid,
+        * so it would be good to refactor it so as to be
+        * able to take either arg and avoid the syscall. */
+       const auto username = user_home_dir.filename();
+       int uid = OS::get_uid_from_name(username.native());
+
+       std::string last_subsession;
+       try {
+               std::ifstream(get_last_subsession_path_by_user_id(uid), std::ios::in) >> last_subsession;
+       } catch (const std::exception &ex) {
+               LOGE("Could not retrieve last subsession of user %s (uid %d)", username.c_str(), uid);
+               return; // FIXME the file not existing is actually fine (1st run) but actual errors are probably not
+       }
+
+       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;
+       }
+
+       if (last_subsession == SUBSESSION_INITIAL_SID) {
+               LOGI("User %s (uid %d) last subsession was empty - nothing to do", username.c_str(), uid);
+               return;
+       }
+
+       LOGI("User %s (uid %d) last subsession identified as \"%s\" - switching", username.c_str(), uid, last_subsession.c_str());
+
+       const int lock_fd = restoration_lock(uid);
+       if (lock_fd == -1) {
+               LOGW("Could not ensure thread-safety of restoration, but continuing anyway because should be mostly safe regardless");
+       }
+
+       try {
+               switch_user_subsession(uid, SUBSESSION_INITIAL_SID, last_subsession);
+               if (g_sessiond_context)
+                       g_sessiond_context->last_subsession_per_session[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);
+}
+
+void restore_all_user_sessions()
+{
+       /* In theory this should live among OS or FS helpers, but
+        * this happens at early boot so we care about performance
+        * enough to skip abstractions and do things directly. */
+
+       for (auto const& entry : fs::directory_iterator("/opt/usr/home")) {
+               if (!fs::is_directory(entry.status()))
+                       continue;
+
+               const auto& username = entry.path().filename();
+               LOGI("Restoring last session for user %s", username.c_str());
+               restore_user_session(entry.path());
+       }
+}
+
diff --git a/src/service/src/main_restore.hpp b/src/service/src/main_restore.hpp
new file mode 100644 (file)
index 0000000..146b410
--- /dev/null
@@ -0,0 +1,25 @@
+/* MIT License
+ *
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is furnished
+ * to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE. */
+
+#pragma once
+
+void restore_all_user_sessions();