25#include <sys/socket.h>
33class ProcessImpl :
public Process
36 std::tuple<mp::ProcessId, mp::SocketId> spawn(
const std::string&
new_exe_name,
const fs::path&
argv0_path)
override
40 path.remove_filename();
46 std::optional<mp::SocketId> checkSpawned(
int argc,
char*
argv[])
override
62 }
catch (
const std::exception&
e) {
63 throw std::runtime_error(
strprintf(
"Invalid -ipcchild value '%s' (%s)",
argv[2],
e.what()));
68 std::string& address)
override;
78 if (address ==
"unix" || address.starts_with(
"unix:")) {
80 if (address.size() <= 5) {
87 if (
path_str.size() >=
sizeof(addr.sun_path)) {
88 error =
strprintf(
"Unix address path %s exceeded maximum socket path length", fs::quoted(fs::PathToString(path)));
91 memset(&addr, 0,
sizeof(addr));
97 error =
strprintf(
"Unrecognized address '%s'", address);
103 std::string& address)
108 throw std::invalid_argument(error);
113 throw std::system_error(
errno, std::system_category());
115 if (::connect(fd, (
struct sockaddr*)&addr,
sizeof(addr)) == 0) {
119 if (::close(fd) != 0) {
122 throw std::system_error(
connect_error, std::system_category());
130 throw std::invalid_argument(error);
133 if (addr.sun_family ==
AF_UNIX) {
134 fs::path path = addr.sun_path;
135 if (path.has_parent_path()) fs::create_directories(path.parent_path());
136 if (fs::symlink_status(path).type() == fs::file_type::socket) {
143 throw std::system_error(
errno, std::system_category());
146 if (::bind(fd, (
struct sockaddr*)&addr,
sizeof(addr)) == 0) {
150 if (::close(fd) != 0) {
153 throw std::system_error(
bind_error, std::system_category());
157std::unique_ptr<Process>
MakeProcess() {
return std::make_unique<ProcessImpl>(); }
static std::string PathToString(const path &path)
Convert path object to a byte string.
static path PathFromString(const std::string &string)
Convert byte string to path object.
std::unique_ptr< Process > MakeProcess()
Constructor for Process interface.
int WaitProcess(int pid)
Wait for a process to exit and return its exit code.
int SpawnProcess(int &pid, FdToArgsFn &&fd_to_args)
Spawn a new process that communicates with the current process over a socket pair.
SocketId StartSpawned(const std::string &connect_info)
constexpr SocketId SocketError
std::string_view RemovePrefixView(std::string_view str LIFETIMEBOUND, std::string_view prefix)
std::string SysErrorString(int err)
Return system error string from errno value.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.