Extract uid/username getting from sessiond restoration 67/324667/2
authorMichal Bloch <m.bloch@samsung.com>
Thu, 22 May 2025 11:07:15 +0000 (13:07 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 22 May 2025 14:03:29 +0000 (16:03 +0200)
Will be used for image remounting as well.

Change-Id: I78a740a9089ba280344aa2dab4ab196234c19607

src/service/src/main_restore.cpp

index 6d431e268d52673d9420959132cf1af3def13591..b791e003faeaaa3205c17b2beaacd6b91c0729c6 100644 (file)
@@ -76,27 +76,16 @@ static void restoration_unlock(int fd)
        close(fd);
 }
 
-static void restore_user_session(const fs::path& user_home_dir)
+static void restore_user_session(const fs::path& username, int uid)
 {
-       /* 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 = -1;
-
        std::string last_subsession;
        try {
-               uid = OS::get_uid_from_name(username.native());
                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);
-
-               /* Don't rethrow since the most common errors are fine:
-                *  - name doesn't necessarily map to an existing UID,
-                *    this can happen e.g. when you install debug rpm
-                *    packages which leaves some build artifacts under
-                *    the `abuild` folder which isn't a user on target.
-                *  - at first boot, the last subsession file won't exist. */
+               LOGE("Could not retrieve last subsession of user %s (uid %d): %s", username.c_str(), uid, ex.what());
+
+               /* At first boot, the last subsession file won't exist.
+                * This is common and fine - don't rethrow. */
                return;
        }
 
@@ -140,8 +129,21 @@ void restore_all_user_sessions()
                        continue;
 
                const auto& username = entry.path().filename();
-               LOGI("Restoring last session for user %s", username.c_str());
-               restore_user_session(entry.path());
+               int uid;
+               try {
+                       /* This has to be under a try clause because user
+                        * name doesn't necessarily map to an existing UID,
+                        * this can happen e.g. when you install debug rpm
+                        * packages which leaves some build artifacts under
+                        * the `abuild` folder which isn't a user on target. */
+                       uid = OS::get_uid_from_name(username.native());
+               } catch (const std::exception &ex) {
+                       LOGW("Could not get uid of user '%s', skipping", username.c_str());
+                       continue;
+               }
+
+               LOGI("Restoring last session for user %s (uid %d)", username.c_str(), uid);
+               restore_user_session(username, uid);
        }
 }