LCOV - code coverage report
Current view: top level - src/ipc/capnp - protocol.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 82.7 % 52 43
Test Date: 2025-08-25 05:11:47 Functions: 75.0 % 12 9
Branches: 44.4 % 36 16

             Branch data     Line data    Source code
       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 <interfaces/init.h>
       6                 :             : #include <ipc/capnp/context.h>
       7                 :             : #include <ipc/capnp/init.capnp.h>
       8                 :             : #include <ipc/capnp/init.capnp.proxy.h>
       9                 :             : #include <ipc/capnp/protocol.h>
      10                 :             : #include <ipc/exception.h>
      11                 :             : #include <ipc/protocol.h>
      12                 :             : #include <kj/async.h>
      13                 :             : #include <logging.h>
      14                 :             : #include <mp/proxy-io.h>
      15                 :             : #include <mp/proxy-types.h>
      16                 :             : #include <mp/util.h>
      17                 :             : #include <util/threadnames.h>
      18                 :             : 
      19                 :             : #include <cassert>
      20                 :             : #include <cerrno>
      21                 :             : #include <future>
      22                 :             : #include <memory>
      23                 :             : #include <mutex>
      24                 :             : #include <optional>
      25                 :             : #include <string>
      26                 :             : #include <sys/socket.h>
      27                 :             : #include <system_error>
      28                 :             : #include <thread>
      29                 :             : 
      30                 :             : namespace ipc {
      31                 :             : namespace capnp {
      32                 :             : namespace {
      33                 :         148 : void IpcLogFn(bool raise, std::string message)
      34                 :             : {
      35         [ +  - ]:         148 :     LogDebug(BCLog::IPC, "%s\n", message);
      36   [ -  +  -  - ]:         148 :     if (raise) throw Exception(message);
      37                 :         148 : }
      38                 :             : 
      39                 :           2 : class CapnpProtocol : public Protocol
      40                 :             : {
      41                 :             : public:
      42                 :           4 :     ~CapnpProtocol() noexcept(true)
      43                 :           2 :     {
      44                 :           2 :         m_loop_ref.reset();
      45         [ +  + ]:           2 :         if (m_loop_thread.joinable()) m_loop_thread.join();
      46         [ -  + ]:           2 :         assert(!m_loop);
      47                 :           4 :     };
      48                 :           6 :     std::unique_ptr<interfaces::Init> connect(int fd, const char* exe_name) override
      49                 :             :     {
      50                 :           6 :         startLoop(exe_name);
      51         [ -  + ]:           6 :         return mp::ConnectStream<messages::Init>(*m_loop, fd);
      52                 :             :     }
      53                 :           2 :     void listen(int listen_fd, const char* exe_name, interfaces::Init& init) override
      54                 :             :     {
      55                 :           2 :         startLoop(exe_name);
      56         [ -  + ]:           2 :         if (::listen(listen_fd, /*backlog=*/5) != 0) {
      57         [ #  # ]:           0 :             throw std::system_error(errno, std::system_category());
      58                 :             :         }
      59                 :           2 :         mp::ListenConnections<messages::Init>(*m_loop, listen_fd, init);
      60                 :           2 :     }
      61                 :           1 :     void serve(int fd, const char* exe_name, interfaces::Init& init, const std::function<void()>& ready_fn = {}) override
      62                 :             :     {
      63         [ -  + ]:           1 :         assert(!m_loop);
      64         [ +  - ]:           1 :         mp::g_thread_context.thread_name = mp::ThreadName(exe_name);
      65                 :           1 :         m_loop.emplace(exe_name, &IpcLogFn, &m_context);
      66         [ +  - ]:           1 :         if (ready_fn) ready_fn();
      67                 :           1 :         mp::ServeStream<messages::Init>(*m_loop, fd, init);
      68                 :           1 :         m_parent_connection = &m_loop->m_incoming_connections.back();
      69                 :           1 :         m_loop->loop();
      70                 :           1 :         m_loop.reset();
      71                 :           1 :     }
      72                 :           0 :     void disconnectIncoming() override
      73                 :             :     {
      74         [ #  # ]:           0 :         if (!m_loop) return;
      75                 :             :         // Delete incoming connections, except the connection to a parent
      76                 :             :         // process (if there is one), since a parent process should be able to
      77                 :             :         // monitor and control this process, even during shutdown.
      78                 :           0 :         m_loop->sync([&] {
      79         [ #  # ]:           0 :             m_loop->m_incoming_connections.remove_if([this](mp::Connection& c) { return &c != m_parent_connection; });
      80                 :             :         });
      81                 :             :     }
      82                 :           0 :     void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) override
      83                 :             :     {
      84                 :           0 :         mp::ProxyTypeRegister::types().at(type)(iface).cleanup_fns.emplace_back(std::move(cleanup));
      85                 :           0 :     }
      86                 :           0 :     Context& context() override { return m_context; }
      87                 :           8 :     void startLoop(const char* exe_name)
      88                 :             :     {
      89         [ +  + ]:           8 :         if (m_loop) return;
      90                 :           1 :         std::promise<void> promise;
      91                 :           1 :         m_loop_thread = std::thread([&] {
      92         [ +  - ]:           1 :             util::ThreadRename("capnp-loop");
      93                 :           1 :             m_loop.emplace(exe_name, &IpcLogFn, &m_context);
      94                 :           1 :             m_loop_ref.emplace(*m_loop);
      95                 :           1 :             promise.set_value();
      96                 :           1 :             m_loop->loop();
      97                 :           1 :             m_loop.reset();
      98         [ +  - ]:           2 :         });
      99   [ +  -  +  - ]:           2 :         promise.get_future().wait();
     100                 :           1 :     }
     101                 :             :     Context m_context;
     102                 :             :     std::thread m_loop_thread;
     103                 :             :     //! EventLoop object which manages I/O events for all connections.
     104                 :             :     std::optional<mp::EventLoop> m_loop;
     105                 :             :     //! Reference to the same EventLoop. Increments the loop’s refcount on
     106                 :             :     //! creation, decrements on destruction. The loop thread exits when the
     107                 :             :     //! refcount reaches 0. Other IPC objects also hold their own EventLoopRef.
     108                 :             :     std::optional<mp::EventLoopRef> m_loop_ref;
     109                 :             :     //! Connection to parent, if this is a child process spawned by a parent process.
     110                 :             :     mp::Connection* m_parent_connection{nullptr};
     111                 :             : };
     112                 :             : } // namespace
     113                 :             : 
     114                 :           2 : std::unique_ptr<Protocol> MakeCapnpProtocol() { return std::make_unique<CapnpProtocol>(); }
     115                 :             : } // namespace capnp
     116                 :             : } // namespace ipc
        

Generated by: LCOV version 2.0-1