35std::string g_ignore_ctrl_c;
40 (void)!write(STDOUT_FILENO, g_ignore_ctrl_c.data(), g_ignore_ctrl_c.size());
44void IgnoreCtrlC(std::string message)
47 g_ignore_ctrl_c = std::move(message);
48 struct sigaction sa{};
49 sa.sa_handler = HandleCtrlC;
50 sigemptyset(&sa.sa_mask);
51 sa.sa_flags = SA_RESTART;
52 sigaction(SIGINT, &sa,
nullptr);
60 :
m_exe_name(exe_name), m_process_argv0(process_argv0), m_init(
init),
64 std::unique_ptr<interfaces::Init> spawnProcess(
const char* new_exe_name)
override
66 const auto [pid, socket] = m_process->spawn(new_exe_name, m_process_argv0);
67 LogDebug(::BCLog::IPC,
"Process %s pid %i launched\n", new_exe_name, pid);
68 auto init = m_protocol->connect(m_protocol->makeStream(socket));
69 Ipc::addCleanup(*
init, [
this, new_exe_name, pid] {
70 int status = m_process->waitSpawned(pid);
71 LogDebug(::BCLog::IPC,
"Process %s pid %i exited with status %i\n", new_exe_name, pid, status);
75 bool startSpawnedProcess(
int argc,
char* argv[],
int&
exit_status)
override
78 std::optional<mp::SocketId> socket{m_process->checkSpawned(argc, argv)};
82 IgnoreCtrlC(
strprintf(
"[%s] SIGINT received — waiting for parent to shut down.\n",
m_exe_name));
83 m_protocol->serve(m_init, [&] {
return m_protocol->makeStream(*socket); } );
87 std::unique_ptr<interfaces::Init> connectAddress(std::string& address)
override
89 if (address.empty() || address ==
"0")
return nullptr;
91 if (address ==
"auto") {
99 }
catch (
const std::system_error& e) {
101 if (e.code() == std::errc::connection_refused || e.code() == std::errc::no_such_file_or_directory || e.code() == std::errc::not_a_directory) {
105 }
catch (
const std::invalid_argument&) {
112 return m_protocol->connect(m_protocol->makeStream(fd));
114 void listenAddress(std::string& address)
override
117 m_protocol->listen(fd, m_init);
119 void disconnectIncoming()
override
121 m_protocol->disconnectIncoming();
123 void addCleanup(std::type_index type,
void* iface, std::function<
void()> cleanup)
override
125 m_protocol->addCleanup(type, iface, std::move(cleanup));
127 Context&
context()
override {
return m_protocol->context(); }
129 const char* m_process_argv0;
131 std::unique_ptr<Protocol> m_protocol;
132 std::unique_ptr<Process> m_process;
138std::unique_ptr<Ipc>
MakeIpc(
const char* exe_name,
const char* process_argv0,
Init&
init)
140 return std::make_unique<ipc::IpcImpl>(exe_name, process_argv0,
init);
std::unique_ptr< interfaces::Init > init
fs::path GetDataDirNet() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path with appended network identifier.
Initial interface created when a process is first started, and used to give and get access to other i...
Interface providing access to interprocess-communication (IPC) functionality.
#define LogDebug(category,...)
std::unique_ptr< Ipc > MakeIpc(const char *exe_name, const char *process_argv0, Init &init)
Return implementation of Ipc interface.
std::unique_ptr< Protocol > MakeCapnpProtocol(const char *exe_name)
std::unique_ptr< Process > MakeProcess()
Constructor for Process interface.