Bitcoin Core  27.99.0
P2P Digital Currency
interfaces.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 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/system.h>
6 #include <interfaces/init.h>
7 #include <interfaces/ipc.h>
8 #include <ipc/capnp/protocol.h>
9 #include <ipc/process.h>
10 #include <ipc/protocol.h>
11 #include <logging.h>
12 #include <tinyformat.h>
13 #include <util/fs.h>
14 
15 #include <cstdio>
16 #include <cstdlib>
17 #include <functional>
18 #include <memory>
19 #include <stdexcept>
20 #include <string.h>
21 #include <string>
22 #include <unistd.h>
23 #include <utility>
24 #include <vector>
25 
26 namespace ipc {
27 namespace {
28 class IpcImpl : public interfaces::Ipc
29 {
30 public:
31  IpcImpl(const char* exe_name, const char* process_argv0, interfaces::Init& init)
32  : m_exe_name(exe_name), m_process_argv0(process_argv0), m_init(init),
33  m_protocol(ipc::capnp::MakeCapnpProtocol()), m_process(ipc::MakeProcess())
34  {
35  }
36  std::unique_ptr<interfaces::Init> spawnProcess(const char* new_exe_name) override
37  {
38  int pid;
39  int fd = m_process->spawn(new_exe_name, m_process_argv0, pid);
40  LogPrint(::BCLog::IPC, "Process %s pid %i launched\n", new_exe_name, pid);
41  auto init = m_protocol->connect(fd, m_exe_name);
42  Ipc::addCleanup(*init, [this, new_exe_name, pid] {
43  int status = m_process->waitSpawned(pid);
44  LogPrint(::BCLog::IPC, "Process %s pid %i exited with status %i\n", new_exe_name, pid, status);
45  });
46  return init;
47  }
48  bool startSpawnedProcess(int argc, char* argv[], int& exit_status) override
49  {
50  exit_status = EXIT_FAILURE;
51  int32_t fd = -1;
52  if (!m_process->checkSpawned(argc, argv, fd)) {
53  return false;
54  }
55  m_protocol->serve(fd, m_exe_name, m_init);
57  return true;
58  }
59  void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) override
60  {
61  m_protocol->addCleanup(type, iface, std::move(cleanup));
62  }
63  Context& context() override { return m_protocol->context(); }
64  const char* m_exe_name;
65  const char* m_process_argv0;
66  interfaces::Init& m_init;
67  std::unique_ptr<Protocol> m_protocol;
68  std::unique_ptr<Process> m_process;
69 };
70 } // namespace
71 } // namespace ipc
72 
73 namespace interfaces {
74 std::unique_ptr<Ipc> MakeIpc(const char* exe_name, const char* process_argv0, Init& init)
75 {
76  return std::make_unique<ipc::IpcImpl>(exe_name, process_argv0, init);
77 }
78 } // namespace interfaces
int exit_status
return EXIT_SUCCESS
std::unique_ptr< interfaces::Init > init
Initial interface created when a process is first started, and used to give and get access to other i...
Definition: init.h:30
Interface providing access to interprocess-communication (IPC) functionality.
Definition: ipc.h:45
#define LogPrint(category,...)
Definition: logging.h:264
@ IPC
Definition: logging.h:64
std::unique_ptr< Ipc > MakeIpc(const char *exe_name, const char *process_argv0, Init &init)
Return implementation of Ipc interface.
Definition: interfaces.cpp:74
std::unique_ptr< Protocol > MakeCapnpProtocol()
Definition: protocol.cpp:91
Definition: ipc.h:12
std::unique_ptr< Process > MakeProcess()
Constructor for Process interface.
Definition: process.cpp:60