LCOV - code coverage report
Current view: top level - src/ipc/capnp - protocol.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 80.9 % 68 55
Test Date: 2025-10-25 04:38:23 Functions: 78.6 % 14 11
Branches: 45.8 % 72 33

             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                 :             : 
      34                 :         488 : BCLog::Level ConvertIPCLogLevel(mp::Log level)
      35                 :             : {
      36   [ +  +  -  -  :         488 :     switch (level) {
                   -  + ]
      37                 :             :         case mp::Log::Trace: return BCLog::Level::Trace;
      38                 :         228 :         case mp::Log::Debug: return BCLog::Level::Debug;
      39                 :          68 :         case mp::Log::Info: return BCLog::Level::Info;
      40                 :           0 :         case mp::Log::Warning: return BCLog::Level::Warning;
      41                 :           0 :         case mp::Log::Error: return BCLog::Level::Error;
      42                 :           0 :         case mp::Log::Raise: return BCLog::Level::Error;
      43                 :             :     } // no default case, so the compiler can warn about missing cases
      44                 :             : 
      45                 :             :     // Be conservative and assume that if MP ever adds a new log level, it
      46                 :             :     // should only be shown at our most verbose level.
      47                 :             :     return BCLog::Level::Trace;
      48                 :             : }
      49                 :             : 
      50                 :           2 : mp::Log GetRequestedIPCLogLevel()
      51                 :             : {
      52         [ -  + ]:           2 :     if (LogAcceptCategory(BCLog::IPC, BCLog::Level::Trace)) return mp::Log::Trace;
      53         [ #  # ]:           0 :     if (LogAcceptCategory(BCLog::IPC, BCLog::Level::Debug)) return mp::Log::Debug;
      54                 :             : 
      55                 :             :     // Info, Warning, and Error are logged unconditionally
      56                 :             :     return mp::Log::Info;
      57                 :             : }
      58                 :             : 
      59                 :         244 : void IpcLogFn(mp::LogMessage message)
      60                 :             : {
      61         [ +  - ]:         244 :     LogPrintLevel(BCLog::IPC, ConvertIPCLogLevel(message.level), "%s\n", message.message);
      62   [ -  +  -  - ]:         244 :     if (message.level == mp::Log::Raise) throw Exception(message.message);
      63                 :         244 : }
      64                 :             : 
      65                 :           2 : class CapnpProtocol : public Protocol
      66                 :             : {
      67                 :             : public:
      68                 :           4 :     ~CapnpProtocol() noexcept(true)
      69                 :           2 :     {
      70                 :           2 :         m_loop_ref.reset();
      71         [ +  + ]:           2 :         if (m_loop_thread.joinable()) m_loop_thread.join();
      72         [ -  + ]:           2 :         assert(!m_loop);
      73                 :           4 :     };
      74                 :           6 :     std::unique_ptr<interfaces::Init> connect(int fd, const char* exe_name) override
      75                 :             :     {
      76                 :           6 :         startLoop(exe_name);
      77         [ -  + ]:           6 :         return mp::ConnectStream<messages::Init>(*m_loop, fd);
      78                 :             :     }
      79                 :           2 :     void listen(int listen_fd, const char* exe_name, interfaces::Init& init) override
      80                 :             :     {
      81                 :           2 :         startLoop(exe_name);
      82         [ -  + ]:           2 :         if (::listen(listen_fd, /*backlog=*/5) != 0) {
      83         [ #  # ]:           0 :             throw std::system_error(errno, std::system_category());
      84                 :             :         }
      85                 :           2 :         mp::ListenConnections<messages::Init>(*m_loop, listen_fd, init);
      86                 :           2 :     }
      87                 :           1 :     void serve(int fd, const char* exe_name, interfaces::Init& init, const std::function<void()>& ready_fn = {}) override
      88                 :             :     {
      89         [ -  + ]:           1 :         assert(!m_loop);
      90         [ +  - ]:           1 :         mp::g_thread_context.thread_name = mp::ThreadName(exe_name);
      91         [ +  - ]:           1 :         mp::LogOptions opts = {
      92                 :             :             .log_fn = IpcLogFn,
      93                 :           1 :             .log_level = GetRequestedIPCLogLevel()
      94         [ +  - ]:           1 :         };
      95         [ +  - ]:           1 :         m_loop.emplace(exe_name, std::move(opts), &m_context);
      96   [ +  -  +  - ]:           1 :         if (ready_fn) ready_fn();
      97         [ +  - ]:           1 :         mp::ServeStream<messages::Init>(*m_loop, fd, init);
      98         [ +  - ]:           1 :         m_parent_connection = &m_loop->m_incoming_connections.back();
      99         [ +  - ]:           1 :         m_loop->loop();
     100                 :           1 :         m_loop.reset();
     101                 :           1 :     }
     102                 :           0 :     void disconnectIncoming() override
     103                 :             :     {
     104         [ #  # ]:           0 :         if (!m_loop) return;
     105                 :             :         // Delete incoming connections, except the connection to a parent
     106                 :             :         // process (if there is one), since a parent process should be able to
     107                 :             :         // monitor and control this process, even during shutdown.
     108                 :           0 :         m_loop->sync([&] {
     109         [ #  # ]:           0 :             m_loop->m_incoming_connections.remove_if([this](mp::Connection& c) { return &c != m_parent_connection; });
     110                 :             :         });
     111                 :             :     }
     112                 :           0 :     void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) override
     113                 :             :     {
     114                 :           0 :         mp::ProxyTypeRegister::types().at(type)(iface).cleanup_fns.emplace_back(std::move(cleanup));
     115                 :           0 :     }
     116                 :           0 :     Context& context() override { return m_context; }
     117                 :           8 :     void startLoop(const char* exe_name)
     118                 :             :     {
     119         [ +  + ]:           8 :         if (m_loop) return;
     120                 :           1 :         std::promise<void> promise;
     121                 :           1 :         m_loop_thread = std::thread([&] {
     122         [ +  - ]:           1 :             util::ThreadRename("capnp-loop");
     123         [ +  - ]:           1 :             mp::LogOptions opts = {
     124                 :             :                 .log_fn = IpcLogFn,
     125                 :           1 :                 .log_level = GetRequestedIPCLogLevel()
     126         [ +  - ]:           1 :             };
     127         [ +  - ]:           1 :             m_loop.emplace(exe_name, std::move(opts), &m_context);
     128         [ +  - ]:           1 :             m_loop_ref.emplace(*m_loop);
     129         [ +  - ]:           1 :             promise.set_value();
     130         [ +  - ]:           1 :             m_loop->loop();
     131                 :           1 :             m_loop.reset();
     132         [ +  - ]:           2 :         });
     133   [ +  -  +  - ]:           2 :         promise.get_future().wait();
     134                 :           1 :     }
     135                 :             :     Context m_context;
     136                 :             :     std::thread m_loop_thread;
     137                 :             :     //! EventLoop object which manages I/O events for all connections.
     138                 :             :     std::optional<mp::EventLoop> m_loop;
     139                 :             :     //! Reference to the same EventLoop. Increments the loop’s refcount on
     140                 :             :     //! creation, decrements on destruction. The loop thread exits when the
     141                 :             :     //! refcount reaches 0. Other IPC objects also hold their own EventLoopRef.
     142                 :             :     std::optional<mp::EventLoopRef> m_loop_ref;
     143                 :             :     //! Connection to parent, if this is a child process spawned by a parent process.
     144                 :             :     mp::Connection* m_parent_connection{nullptr};
     145                 :             : };
     146                 :             : } // namespace
     147                 :             : 
     148                 :           2 : std::unique_ptr<Protocol> MakeCapnpProtocol() { return std::make_unique<CapnpProtocol>(); }
     149                 :             : } // namespace capnp
     150                 :             : } // namespace ipc
        

Generated by: LCOV version 2.0-1