Bitcoin Core 32.99.0
P2P Digital Currency
interfaces.cpp
Go to the documentation of this file.
1// Copyright (c) 2021-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <common/args.h>
6#include <common/system.h>
7#include <interfaces/init.h>
8#include <interfaces/ipc.h>
10#include <ipc/process.h>
11#include <ipc/protocol.h>
12#include <tinyformat.h>
13#include <util/fs.h>
14#include <util/log.h>
15
16#include <csignal>
17#include <cstdio>
18#include <cstdlib>
19#include <cstring>
20#include <functional>
21#include <memory>
22#include <optional>
23#include <stdexcept>
24#include <string>
25#include <utility>
26#include <vector>
27
28#ifndef WIN32
29#include <unistd.h>
30#endif
31
32namespace ipc {
33namespace {
34#ifndef WIN32
35std::string g_ignore_ctrl_c;
36
37void HandleCtrlC(int)
38{
39 // (void)! needed to suppress -Wunused-result warning from GCC
40 (void)!write(STDOUT_FILENO, g_ignore_ctrl_c.data(), g_ignore_ctrl_c.size());
41}
42#endif
43
44void IgnoreCtrlC(std::string message)
45{
46#ifndef WIN32
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);
53#endif
54}
55
56class IpcImpl : public interfaces::Ipc
57{
58public:
59 IpcImpl(const char* exe_name, const char* process_argv0, interfaces::Init& init)
60 : m_exe_name(exe_name), m_process_argv0(process_argv0), m_init(init),
61 m_protocol(ipc::capnp::MakeCapnpProtocol(exe_name)), m_process(ipc::MakeProcess())
62 {
63 }
64 std::unique_ptr<interfaces::Init> spawnProcess(const char* new_exe_name) override
65 {
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);
72 });
73 return init;
74 }
75 bool startSpawnedProcess(int argc, char* argv[], int& exit_status) override
76 {
77 exit_status = EXIT_FAILURE;
78 std::optional<mp::SocketId> socket{m_process->checkSpawned(argc, argv)};
79 if (!socket) {
80 return false;
81 }
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); } );
85 return true;
86 }
87 std::unique_ptr<interfaces::Init> connectAddress(std::string& address) override
88 {
89 if (address.empty() || address == "0") return nullptr;
90 mp::SocketId fd;
91 if (address == "auto") {
92 // Treat "auto" the same as "unix" except don't treat it an as error
93 // if the connection is not accepted. Just return null so the caller
94 // can work offline without a connection, or spawn a new
95 // bitcoin-node process and connect to it.
96 address = "unix";
97 try {
98 fd = m_process->connect(gArgs.GetDataDirNet(), "bitcoin-node", address);
99 } catch (const std::system_error& e) {
100 // If connection type is auto and socket path isn't accepting connections, or doesn't exist, catch the error and return null;
101 if (e.code() == std::errc::connection_refused || e.code() == std::errc::no_such_file_or_directory || e.code() == std::errc::not_a_directory) {
102 return nullptr;
103 }
104 throw;
105 } catch (const std::invalid_argument&) {
106 // Catch 'Unix address path "..." exceeded maximum socket path length' error
107 return nullptr;
108 }
109 } else {
110 fd = m_process->connect(gArgs.GetDataDirNet(), "bitcoin-node", address);
111 }
112 return m_protocol->connect(m_protocol->makeStream(fd));
113 }
114 void listenAddress(std::string& address) override
115 {
116 mp::SocketId fd = m_process->bind(gArgs.GetDataDirNet(), m_exe_name, address);
117 m_protocol->listen(fd, m_init);
118 }
119 void disconnectIncoming() override
120 {
121 m_protocol->disconnectIncoming();
122 }
123 void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) override
124 {
125 m_protocol->addCleanup(type, iface, std::move(cleanup));
126 }
127 Context& context() override { return m_protocol->context(); }
128 const char* m_exe_name;
129 const char* m_process_argv0;
130 interfaces::Init& m_init;
131 std::unique_ptr<Protocol> m_protocol;
132 std::unique_ptr<Process> m_process;
133};
134} // namespace
135} // namespace ipc
136
137namespace interfaces {
138std::unique_ptr<Ipc> MakeIpc(const char* exe_name, const char* process_argv0, Init& init)
139{
140 return std::make_unique<ipc::IpcImpl>(exe_name, process_argv0, init);
141}
142} // namespace interfaces
ArgsManager gArgs
Definition: args.cpp:38
int exit_status
return EXIT_SUCCESS
std::unique_ptr< interfaces::Init > init
fs::path GetDataDirNet() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path with appended network identifier.
Definition: args.cpp:328
Initial interface created when a process is first started, and used to give and get access to other i...
Definition: init.h:39
Interface providing access to interprocess-communication (IPC) functionality.
Definition: ipc.h:52
const char * m_exe_name
Definition: protocol.cpp:146
#define LogDebug(category,...)
Definition: log.h:143
Definition: basic.cpp:11
std::unique_ptr< Ipc > MakeIpc(const char *exe_name, const char *process_argv0, Init &init)
Return implementation of Ipc interface.
Definition: interfaces.cpp:138
std::unique_ptr< Protocol > MakeCapnpProtocol(const char *exe_name)
Definition: protocol.cpp:160
Definition: ipc.h:14
std::unique_ptr< Process > MakeProcess()
Constructor for Process interface.
Definition: process.cpp:157
int SocketId
Definition: util.h:27
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
WalletContext context