Check for mountpoint before mounting 78/320378/2
authorMichal Bloch <m.bloch@samsung.com>
Thu, 27 Feb 2025 12:20:43 +0000 (13:20 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 27 Feb 2025 15:28:30 +0000 (16:28 +0100)
Change-Id: I335f3d0ae03a473eca929ebf1bd1d34d11c035f7

src/service/src/dir_backend_fixed_size.cpp
src/service/src/os_ops.cpp
src/service/src/os_ops.hpp

index 2e921b6d15cee0a154db32b7d52b108f605a9bdc..a8e8a3321b835980ec6f9555b4b04e896e6c577f 100644 (file)
@@ -25,6 +25,7 @@
 #include <dlog.h>
 
 #include "dir_backend_fixed_size.hpp"
+#include "os_ops.hpp"
 
 #include <sys/mount.h>
 #include <sys/wait.h>
@@ -219,5 +220,11 @@ void DirBackendFixedSize::SwitchSubsessionAway (const std::filesystem::path& sub
 void DirBackendFixedSize::SwitchSubsessionInto (const std::filesystem::path& subsession_path) const
 {
        const auto image_path = GetImagePathFromSubsessionPath(subsession_path);
+
+       if (OS::is_mountpoint(subsession_path)) {
+               LOGW("Session image already mounted (maybe earlier sessiond instance died?), reusing");
+               return;
+       }
+
        do_mount(image_path, subsession_path);
 }
index fa0861bec99e877214a71702b543f2b3019d816b..15343f8a6839da58db12131652d661f0b86fd2cb 100644 (file)
 
 #include "os_ops.hpp"
 
+#include <optional>
+
 #include <grp.h>
 #include <pwd.h>
 #include <sys/smack.h>
 #include <sys/stat.h>
+#include <sys/sysmacros.h>
 #include <unistd.h>
 
 namespace fs = std::filesystem;
@@ -179,3 +182,30 @@ void OS::set_ownership_and_perms(fs::path src_path, fs::path dest_path, fs::perm
        if (!is_symlink(info))
                fs::permissions(dest_path, permissions);
 }
+
+static std::optional <dev_t> get_stat_dev(fs::path path)
+{
+       struct stat info;
+       const int ret = lstat(path.c_str(), &info);
+       if (ret != 0) {
+               if (errno == ENOENT)
+                       return {};
+               else
+                       throw std::system_error(errno, std::system_category(), "Couldn't lstat("s + path.native() + ")");
+       }
+       return info.st_dev;
+}
+
+bool OS::is_mountpoint(fs::path path)
+{
+       const auto path_dev = get_stat_dev(path);
+       if (!path_dev)
+               return false;
+
+       const auto parent_dev = get_stat_dev(path.parent_path());
+       if (!parent_dev)
+               return false; // somewhat worrisome
+
+       return major(*path_dev) != major(*parent_dev)
+           || minor(*path_dev) != minor(*parent_dev);
+}
index 76ca4db483081281837d492b1c9c37d708a16c80..04bf02302caedf3ca991a45a929d7c0e7f3ce511 100644 (file)
@@ -21,4 +21,6 @@ namespace OS {
 
        void set_ownership_and_perms(fs::path src_path, fs::path dest_path, fs::perms permissions,
                const int session_uid, const int gid, bool copy_perms_from_skel);
+
+       bool is_mountpoint(fs::path path);
 }