33namespace fs = std::filesystem;
38std::optional<pid_t> opt_child = std::nullopt;
49 kill(opt_child.value(), sig);
102 logger(
"D::Sending message through pipe: {}", path_fifo_daemon);
108 , std::chrono::seconds(SECONDS_TIMEOUT)
109 , std::span(data.c_str(), data.length())
112 return (
static_cast<size_t>(size_writen) != data.length())?
113 Error(
"E::Could not write data to daemon({}): {}", size_writen, strerror(errno))
130 , std::chrono::seconds(SECONDS_TIMEOUT)
131 , std::span<pid_t>(&pid_child, 1)
133 return_if(bytes_read !=
sizeof(pid_child), Error(
"E::{}", strerror(errno)));
135 return_if(pid_child < 0, Error(
"E::Could not start PID, program not found?"));
137 opt_child = pid_child;
138 logger(
"D::Child pid: {}", pid_child);
141 std::jthread thread_stdin([pid_child, message]
145 std::jthread thread_stdout([pid_child, message]
149 std::jthread thread_stderr([pid_child, message]
153 logger(
"D::Connected to stdin/stdout/stderr fifos");
158 , std::chrono::seconds{SECONDS_TIMEOUT}
159 , std::span<int>(&code_exit, 1)
161 return_if(bytes_exit !=
sizeof(code_exit), Error(
"E::Incorrect number of bytes '{}' read", bytes_exit));
174 , fs::path
const& path_dir_fifo
175 , std::vector<std::string>
const& cmd
178 using namespace std::chrono_literals;
180 auto environment = std::ranges::subrange(environ, std::unreachable_sentinel)
181 | std::views::take_while([](
char* p) {
return p !=
nullptr; })
182 | std::ranges::to<std::vector<std::string>>();
208 std::vector<std::string> args(argv+1, argv+argc);
210 return_if(args.empty(), EXIT_FAILURE,
"E::No arguments for dispatcher");
221 , arg_cfg.get_path_dir_fifo()
223 ) ,
"E::Failure to dispatch process request");
Defines a class that manages FlatImage's portal dispatcher configuration.
Enhanced error handling framework built on std::expected.
constexpr auto __expected_fn
Lambda helper for Pop macro error returns.
File descriptor redirection helpers.
Value< void > redirect_file_to_fd(pid_t ppid, fs::path const &path_file, int fd_dst)
Redirects the output of a file to a file descriptor.
Value< void > redirect_fd_to_file(pid_t ppid, int fd_src, fs::path const &path_file)
Redirects the output of a file descriptor to a file.
Linux FIFO related operation wrappers.
Value< fs::path > create(fs::path const &path_file_fifo)
Create a fifo object.
A library for manipulating environment variables.
A library with helpers for linux operations.
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.
Portal command dispatcher configuration.
Value< Dispatcher > deserialize(std::string_view str_raw_json) noexcept
Deserializes a json string into a Dispatcher class.
Portal IPC message serialization and deserialization.
Value< std::string > serialize(Message const &message) noexcept
Serializes a Message class into a json string.
Value< std::string > get_expected(std::string_view name)
Get the value of an environment variable.
bool exists(std::string_view name, std::string_view value)
Checks if variable exists and equals value.
Value< fs::path > create_directories(fs::path const &p)
Creates directories recursively.
ssize_t open_write_with_timeout(fs::path const &path_file_src, std::chrono::milliseconds const &timeout, std::span< Data > buf)
Opens and writes to the given input file.
ssize_t open_read_with_timeout(fs::path const &path_file_src, std::chrono::milliseconds const &timeout, std::span< Data > buf)
Opens and reads from the given input file.
void set_level(Level level)
Sets the logging verbosity (CRITICAL,ERROR,INFO,DEBUG)
void set_sink_file(fs::path const &path_file_sink)
Sets the sink file of the logger.
Constants for portal configuration.
int main()
Entry point for the portal daemon.
Value< void > send_message(ns_message::Message const &message, fs::path const &path_fifo_daemon)
Sends a message to the portal daemon.
Value< int > process_request(fs::path const &path_fifo_daemon, fs::path const &path_dir_fifo, std::vector< std::string > const &cmd)
Sends a request to the daemon to create a new process.
void signal_handler(int sig)
Forwards received signal to requested child process.
void register_signals()
Registers signal handlers for dispatcher cleanup.
Value< int > process_wait(ns_message::Message const &message)
Waits for the requested process to finish.
Value< void > fifo_create(ns_message::Message const &msg)
Creates FIFOs for child process I/O.
Enhanced expected type with integrated logging capabilities.