34namespace fs = std::filesystem;
42volatile std::sig_atomic_t G_CONTINUE = 1;
66 signal(SIGPIPE, SIG_IGN);
70 logger(
"D::Started host daemon");
82 logger(
"D::Initialized portal daemon in {} mode", args_cfg.get_mode().lower());
86 int fd_fifo = ::open(path_fifo_in.c_str(), O_RDONLY | O_NONBLOCK);
89 ,
"E::Could not open file '{}': {}", path_fifo_in, strerror(errno)
91 logger(
"D::Listening fifo {}", path_fifo_in);
94 [[maybe_unused]]
int fd_dummy = ::open(path_fifo_in.c_str(), O_WRONLY);
95 return_if(fd_dummy < 0
97 ,
"E::Could not open dummy writer in '{}', {}", path_fifo_in, strerror(errno)
101 pid_t pid_reference = args_cfg.get_pid_reference();
106 [[maybe_unused]]
auto thread_sig = std::jthread([pid_reference,pid=getpid()]
108 while(kill(pid_reference, 0) == 0)
110 std::this_thread::sleep_for(std::chrono::milliseconds{100});
116 for(
char buffer[16384]; G_CONTINUE;)
118 ssize_t bytes_read = ::read(fd_fifo, &buffer, SIZE_BUFFER_READ);
127 else if (bytes_read < 0)
129 if (errno == EAGAIN or errno == EWOULDBLOCK)
131 std::this_thread::sleep_for(std::chrono::milliseconds{100});
140 std::string_view msg{buffer,
static_cast<size_t>(bytes_read)};
141 logger(
"D::Recovered message: {}", msg);
144 continue_if(not message,
"E::Could not parse message: {}", message.error());
146 if(pid_t pid = fork(); pid < 0)
148 logger(
"E::Could not fork child");
157 logger(
"D::Portal daemon shutdown, G_CONTINUE={}", G_CONTINUE);
Defines a class that manages FlatImage's portal configuration.
Enhanced error handling framework built on std::expected.
constexpr auto __expected_fn
Lambda helper for Pop macro error returns.
Linux FIFO related operation wrappers.
Value< fs::path > create(fs::path const &path_file_fifo)
Create a fifo object.
void cleanup(int sig)
Signal handler for parent process exit detection.
A library for manipulating environment variables.
A library for file logging.
#define logger(fmt,...)
Compile-time log level dispatch macro with automatic location capture.
Simplified macros for common control flow patterns with optional logging.
Defines a class that manages portal daemon message serialization/deserialization.
Value< Logs > deserialize(std::string_view str_raw_json) noexcept
Deserializes a json string into a Logs class.
Portal daemon configuration and management.
Value< Daemon > deserialize(std::string_view str_raw_json) noexcept
Deserializes a json string into a Daemon class.
Portal IPC message serialization and deserialization.
Value< Message > deserialize(std::string_view str_raw_json) noexcept
Deserializes a json string into a Message class.
Value< std::string > get_expected(std::string_view name)
Get the value of an environment variable.
void set_sink_file(fs::path const &path_file_sink)
Sets the sink file of the logger.
Value< void > spawn(std::vector< std::string > const &vec_argv, ns_db::ns_portal::ns_message::Message const &message)
Forks a child process and waits for it to complete.
Spawns a child process and connects its I/O to pipes.
Constants for portal configuration.
void cleanup(int sig)
Signal handler for daemon cleanup.
int main()
Entry point for the portal daemon.