FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
portal.hpp
Go to the documentation of this file.
1
8
9#include <filesystem>
10#include <memory>
11
12#include "../std/expected.hpp"
13#include "../lib/env.hpp"
14#include "../lib/subprocess.hpp"
16
26namespace ns_portal
27{
28
29namespace
30{
31
32namespace fs = std::filesystem;
33
34} // anonymous namespace
35
38
40
47class Portal
48{
49 private:
50 std::unique_ptr<ns_subprocess::Child> m_child;
51 Portal();
52
53 public:
54 friend Value<std::unique_ptr<Portal>> spawn(Daemon const& daemon, Logs const& logs);
55 friend constexpr std::unique_ptr<Portal> std::make_unique<Portal>();
56};
57
58
62inline Portal::Portal()
63 : m_child(nullptr)
64{
65}
66
74[[nodiscard]] inline Value<std::unique_ptr<Portal>> spawn(Daemon const& daemon, Logs const& logs)
75{
76 auto portal = std::make_unique<Portal>();
77 // Path to daemon
78 fs::path path_bin_daemon = daemon.get_path_bin_daemon();
79 return_if(portal == nullptr, Error("E::Could not create portal object"));
80 return_if(not Try(fs::exists(path_bin_daemon)), Error("E::Daemon not found in {}", path_bin_daemon));
81 // Create a portal that uses the reference file to create an unique communication key
82 // Spawn process to background
83 portal->m_child = ns_subprocess::Subprocess(path_bin_daemon)
84 .with_var("FIM_DAEMON_CFG", Pop(ns_daemon::serialize(daemon)))
85 .with_var("FIM_DAEMON_LOG", Pop(ns_daemon::ns_log::serialize(logs)))
87 .spawn();
88 return portal;
89}
90
91} // namespace ns_portal
92
93/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
friend Value< std::unique_ptr< Portal > > spawn(Daemon const &daemon, Logs const &logs)
Spawns a new portal daemon instance.
Definition portal.hpp:74
Subprocess & with_var(K &&k, V &&v)
Adds or replaces a single environment variable.
std::unique_ptr< Child > spawn()
Spawns (forks) the child process and begins execution.
Subprocess & with_daemon()
Enable daemon mode using double fork pattern.
Defines a class that manages FlatImage's portal configuration.
Enhanced error handling framework built on std::expected.
A library for manipulating environment variables.
Value< std::string > serialize(Logs const &logs) noexcept
Serializes a Logs class into a json string.
Definition daemon.hpp:122
Portal daemon configuration and management.
Value< std::string > serialize(Daemon const &daemon) noexcept
Serializes a Daemon class into a json string.
Definition daemon.hpp:210
Portal IPC system for host-container communication.
Value< std::unique_ptr< Portal > > spawn(Daemon const &daemon, Logs const &logs)
Spawns a new portal daemon instance.
Definition portal.hpp:74
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44
A library to spawn sub-processes in linux.