26namespace fs = std::filesystem;
57 , std::chrono::seconds(SECONDS_TIMEOUT)
58 , std::span<pid_t const>(&value, 1)
61 return_if(bytes_written !=
sizeof(value), Error(
"E::Failed to write pid: {}", strerror(errno)));
86 auto program = Try(vec_argv.at(0));
88 if(not path_bin_program)
90 write_fifo(-1, message.get_pid()).discard(
"C::Failed to write pid to fifo");
91 write_fifo(1, message.get_exit()).discard(
"C::Failed to write exit code to fifo");
92 return Error(
"E::Could not find program '{}'", program);
94 path_bin_program.value();
96 std::vector<std::string> args(vec_argv.begin()+1, vec_argv.end());
100 .
with_env(message.get_environment())
105 , std::chrono::seconds(SECONDS_TIMEOUT)
108 if (fd_stdin < 0 or dup2(fd_stdin, STDIN_FILENO) < 0) {
109 logger(
"E::Failed to open/redirect stdin FIFO: {}", strerror(errno));
112 if (fd_stdin != STDIN_FILENO) close(fd_stdin);
116 , std::chrono::seconds(SECONDS_TIMEOUT)
119 if (fd_stdout < 0 or dup2(fd_stdout, STDOUT_FILENO) < 0) {
120 logger(
"E::Failed to open/redirect stdout FIFO: {}", strerror(errno));
123 if (fd_stdout != STDOUT_FILENO) close(fd_stdout);
127 , std::chrono::seconds(SECONDS_TIMEOUT)
130 if (fd_stderr < 0 or dup2(fd_stderr, STDERR_FILENO) < 0) {
131 logger(
"E::Failed to open/redirect stderr FIFO: {}", strerror(errno));
134 if (fd_stderr != STDERR_FILENO) close(fd_stderr);
139 pid_t pid_child = child->get_pid().value_or(-1);
140 write_fifo(pid_child, message.get_pid()).discard(
"C::Failed to write pid to fifo");
142 int code = Pop(child->wait(),
"E::Child exited abnormally");
143 logger(
"D::Exit code: {}", code);
145 write_fifo(code, message.get_exit()).discard(
"C::Failed to write exit code to fifo");
163 Try(fs::create_directories(path_file_log.parent_path()));
166 auto vec_argv = message.get_command();
168 if ( vec_argv.empty() ) {
return Error(
"E::Empty command"); }
170 Pop(
spawn(vec_argv, message));
Subprocess & with_die_on_pid(pid_t pid)
Configures the child process to die when the specified PID dies.
Subprocess & with_callback_child(F &&f)
Sets a callback to run in the child process after fork() but before execve()
Subprocess & with_env(Args &&... args)
Includes environment variables with the format 'NAME=VALUE' in the environment.
Subprocess & with_args(Args &&... args)
Arguments forwarded as the process' arguments.
Defines a class that manages FlatImage's portal configuration.
Enhanced error handling framework built on std::expected.
A library for manipulating environment variables.
A library with helpers for linux operations.
#define logger(fmt,...)
Compile-time log level dispatch macro with automatic location capture.
Defines a class that manages portal daemon message serialization/deserialization.
Portal daemon configuration and management.
Portal IPC message serialization and deserialization.
Value< fs::path > search_path(std::string const &query)
Search the directories in the PATH variable for the given input file name.
fs::path placeholders_replace(fs::path const &path, Args &&... args)
Replace placeholders in a path by traversing components.
int open_with_timeout(fs::path const &path_file_src, std::chrono::milliseconds timeout, int oflag)
Opens a given file with a timeout.
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.
void set_sink_file(fs::path const &path_file_sink)
Sets the sink file of the logger.
Child process spawning and I/O management for portal IPC.
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.
Value< void > write_fifo(int const value, fs::path const &path_fifo)
Writes a value to a fifo given a file path.
Constants for portal configuration.
Enhanced expected type with integrated logging capabilities.
Arguments passed to child callback.
Arguments passed to parent callback.
A library to spawn sub-processes in linux.