#include "os_ops.hpp"
#include <sys/mount.h>
-#include <sys/wait.h>
#include <unistd.h>
#include <string_view>
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;
_exit(1);
} else {
- throw_if_child_failed(child_pid, "mkfs.ext2 failed!");
+ OS::throw_if_child_failed(child_pid, "mkfs.ext2 failed!");
}
}
* 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;
_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;
_exit(1);
} else {
- throw_if_child_failed(child_pid, "umount failed!");
+ OS::throw_if_child_failed(child_pid, "umount failed!");
}
}
#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));