|
| 1 | +// Copyright (c) 2021 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 <fs.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/system.h> |
| 14 | + |
| 15 | +#include <functional> |
| 16 | +#include <memory> |
| 17 | +#include <stdexcept> |
| 18 | +#include <stdio.h> |
| 19 | +#include <stdlib.h> |
| 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); |
| 56 | + exit_status = EXIT_SUCCESS; |
| 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 | + const char* m_exe_name; |
| 64 | + const char* m_process_argv0; |
| 65 | + interfaces::Init& m_init; |
| 66 | + std::unique_ptr<Protocol> m_protocol; |
| 67 | + std::unique_ptr<Process> m_process; |
| 68 | +}; |
| 69 | +} // namespace |
| 70 | +} // namespace ipc |
| 71 | + |
| 72 | +namespace interfaces { |
| 73 | +std::unique_ptr<Ipc> MakeIpc(const char* exe_name, const char* process_argv0, Init& init) |
| 74 | +{ |
| 75 | + return std::make_unique<ipc::IpcImpl>(exe_name, process_argv0, init); |
| 76 | +} |
| 77 | +} // namespace interfaces |
0 commit comments