Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_INTERFACES_IPC_H6#define BITCOIN_INTERFACES_IPC_H8#include <functional>9#include <memory>10#include <string>11#include <typeindex>12#include <utility>14namespace ipc {15struct Context;16} // namespace ipc18namespace interfaces {19class Init;21//! Interface providing access to interprocess-communication (IPC)22//! functionality. The IPC implementation is responsible for establishing23//! connections between a controlling process and a process being controlled.24//! When a connection is established, the process being controlled returns an25//! interfaces::Init pointer to the controlling process, which the controlling26//! process can use to get access to other interfaces and functionality.27//!28//! When spawning a new process, the steps are:29//!30//! 1. The controlling process calls interfaces::Ipc::spawnProcess(), which31//! calls ipc::Process::spawn(), which spawns a new process and returns a32//! socketpair file descriptor for communicating with it.33//! interfaces::Ipc::spawnProcess() then calls ipc::Protocol::connect()34//! passing the socketpair descriptor, which returns a local proxy35//! interfaces::Init implementation calling remote interfaces::Init methods.36//! 2. The spawned process calls interfaces::Ipc::startSpawnProcess(), which37//! calls ipc::Process::checkSpawned() to read command line arguments and38//! determine whether it is a spawned process and what socketpair file39//! descriptor it should use. It then calls ipc::Protocol::serve() to handle40//! incoming requests from the socketpair and invoke interfaces::Init41//! interface methods, and exit when the socket is closed.42//! 3. The controlling process calls local proxy interfaces::Init object methods43//! to make other proxy objects calling other remote interfaces. It can also44//! destroy the initial interfaces::Init object to close the connection and45//! shut down the spawned process.46//!47//! When connecting to an existing process, the steps are similar to spawning a48//! new process, except a socket is created instead of a socketpair, and49//! destroying an Init interface doesn't end the process, since there can be50//! multiple connections.51class Ipc52{53public:54virtual ~Ipc() = default;56//! Spawn a child process returning pointer to its Init interface.57virtual std::unique_ptr<Init> spawnProcess(const char* exe_name) = 0;59//! If this is a spawned process, block and handle requests from the parent60//! process by forwarding them to this process's Init interface, then return61//! true. If this is not a spawned child process, return false.62virtual bool startSpawnedProcess(int argc, char* argv[], int& exit_status) = 0;64//! Connect to a socket address and return a pointer to its Init interface.65//! Returns a non-null pointer if the connection was established, returns66//! null if address is empty ("") or disabled ("0") or if a connection was67//! refused but not required ("auto"), and throws an exception if there was68//! an unexpected error.69virtual std::unique_ptr<Init> connectAddress(std::string& address) = 0;71//! Listen on a socket address exposing this process's init interface to72//! clients. Throws an exception if there was an error.73virtual void listenAddress(std::string& address) = 0;75//! Disconnect any incoming connections that are still connected.76virtual void disconnectIncoming() = 0;78//! Add cleanup callback to remote interface that will run when the79//! interface is deleted.80template<typename Interface>81void addCleanup(Interface& iface, std::function<void()> cleanup)82{83addCleanup(typeid(Interface), &iface, std::move(cleanup));84}86//! IPC context struct accessor (see struct definition for more description).87virtual ipc::Context& context() = 0;89protected:90//! Internal implementation of public addCleanup method (above) as a91//! type-erased virtual function, since template functions can't be virtual.92virtual void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) = 0;93};95//! Return implementation of Ipc interface.96std::unique_ptr<Ipc> MakeIpc(const char* exe_name, const char* process_argv0, Init& init);97} // namespace interfaces99#endif // BITCOIN_INTERFACES_IPC_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.