* 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;
--- /dev/null
+/* 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());
+ }
+}
+
--- /dev/null
+/* 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();