Move fork helpers to OS namespace 46/320946/1
authorMichal Bloch <m.bloch@samsung.com>
Tue, 11 Mar 2025 12:53:06 +0000 (13:53 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 11 Mar 2025 17:54:42 +0000 (18:54 +0100)
Change-Id: I264f830c8c7e92bdcd29c48b0b57da32aa381d65

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

index 03ef138850572930a64a74943cb111fd797f61aa..f20aeb49ffe8a80ed56ec3a675739f85149d1857 100644 (file)
@@ -28,7 +28,6 @@
 #include "os_ops.hpp"
 
 #include <sys/mount.h>
-#include <sys/wait.h>
 #include <unistd.h>
 
 #include <string_view>
@@ -44,34 +43,9 @@ fs::path DirBackendFixedSize::GetImagePathFromSubsessionPath(fs::path subsession
        return subsession_path;
 }
 
-/* TODO: perhaps there should also be a "do_exec" wrapper which would
- * accept a child lambda and do the throwing fork and the waitpid */
-
-static int throwing_fork()
-{
-       const int child_pid = fork();
-       if (child_pid == -1) {
-               const auto err = errno;
-               throw std::system_error (err, std::generic_category (), "fork() failed!");
-       }
-       return child_pid;
-}
-
-static void throw_if_child_failed(int child_pid, std::string_view message_on_fail)
-{
-       int status;
-       const int ret = waitpid(child_pid, &status, 0);
-       if (ret == -1) {
-               const auto err = errno;
-               throw std::system_error (err, std::generic_category (), "waitpid() failed!");
-       }
-       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
-               throw std::runtime_error (std::string(message_on_fail));
-}
-
 static void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB)
 {
-       const auto child_pid = throwing_fork();
+       const auto child_pid = OS::throwing_fork();
        if (child_pid == 0) {
                const auto mkfs = "/usr/sbin/mkfs.ext2"sv;
 
@@ -95,7 +69,7 @@ static void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_
 
                _exit(1);
        } else {
-               throw_if_child_failed(child_pid, "mkfs.ext2 failed!");
+               OS::throw_if_child_failed(child_pid, "mkfs.ext2 failed!");
        }
 }
 
@@ -105,7 +79,7 @@ static void do_mount(const fs::path& image_path, const fs::path& mount_path)
         * in making it work properly, such as setting up a loop device,
         * that the mount binary does for us. */
 
-       const auto child_pid = throwing_fork();
+       const auto child_pid = OS::throwing_fork();
        if (child_pid == 0) {
                const auto mount_exe = "/usr/bin/mount"sv;
 
@@ -119,13 +93,13 @@ static void do_mount(const fs::path& image_path, const fs::path& mount_path)
 
                _exit(1);
        } else {
-               throw_if_child_failed(child_pid, "mount failed!");
+               OS::throw_if_child_failed(child_pid, "mount failed!");
        }
 }
 
 static void do_umount(const fs::path& path)
 {
-       const auto child_pid = throwing_fork();
+       const auto child_pid = OS::throwing_fork();
        if (child_pid == 0) {
                const auto umount_exe = "/usr/bin/umount"sv;
 
@@ -137,7 +111,7 @@ static void do_umount(const fs::path& path)
 
                _exit(1);
        } else {
-               throw_if_child_failed(child_pid, "umount failed!");
+               OS::throw_if_child_failed(child_pid, "umount failed!");
        }
 }
 
index b2e27b7eb6e5fc1575360092b42ad12a44e2fe28..92bce5032cc65b1fd8682c9f7c9eb5f27aa641e5 100644 (file)
 #include <sys/smack.h>
 #include <sys/stat.h>
 #include <sys/sysmacros.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
 namespace fs = std::filesystem;
 using namespace std::string_literals;
 
+
+/* TODO: perhaps there should also be a "do_exec" wrapper which would
+ * accept a child lambda and do the throwing fork and the waitpid */
+
+int OS::throwing_fork()
+{
+       const int child_pid = fork();
+       if (child_pid == -1) {
+               const auto err = errno;
+               throw std::system_error (err, std::generic_category (), "fork() failed!");
+       }
+       return child_pid;
+}
+
+void OS::throw_if_child_failed(int child_pid, std::string_view message_on_fail)
+{
+       int status;
+       const int ret = waitpid(child_pid, &status, 0);
+       if (ret == -1) {
+               const auto err = errno;
+               throw std::system_error (err, std::generic_category (), "waitpid() failed!");
+       }
+       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+               throw std::runtime_error (std::string(message_on_fail));
+}
+
 std::string OS::get_home_dir_by_user_id(const int uid)
 {
        auto max_buf_size = static_cast<ssize_t>(sysconf(_SC_GETPW_R_SIZE_MAX));
index 6649604f7073c5c3d48173a371d76e6b2a6132d2..a0569d17685e236da192de06cf18b266452a5970 100644 (file)
@@ -9,6 +9,10 @@
 namespace fs = std::filesystem;
 
 namespace OS {
+       int throwing_fork();
+
+       void throw_if_child_failed(int child_pid, std::string_view message_on_fail);
+
        std::string get_home_dir_by_user_id(const int uid);
 
        int get_gid_from_name(std::string_view group_name);