Bitcoin Core
31.99.0
P2P Digital Currency
src
ipc
util.h
Go to the documentation of this file.
1
// Copyright (c) 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
#ifndef BITCOIN_IPC_UTIL_H
6
#define BITCOIN_IPC_UTIL_H
7
8
#include <
tinyformat.h
>
9
#include <
util/strencodings.h
>
10
11
#include <array>
12
#include <cstdint>
13
#include <functional>
14
#include <kj/debug.h>
15
#include <
mp/proxy-io.h
>
16
#include <
mp/util.h
>
17
#include <
mp/version.h
>
18
#include <sys/socket.h>
19
20
namespace
mp
{
21
// Definitions that can be deleted when libmultiprocess subtree is updated to
22
// v14. Having these allows Bitcoin Core changes to be decoupled from
23
// libmultiprocess changes so they don't have to be reviewed in a single PR.
24
#if MP_MAJOR_VERSION < 14
25
class
EventLoop;
26
using
ProcessId
= int;
27
using
SocketId
= int;
28
constexpr
SocketId
SocketError
{-1};
29
30
using
Stream
=
SocketId
;
31
inline
Stream
MakeStream
(
EventLoop
&,
SocketId
socket)
32
{
33
return
socket;
34
}
35
36
inline
std::array<SocketId, 2>
SocketPair
()
37
{
38
int
pair[2];
39
KJ_SYSCALL(socketpair(AF_UNIX, SOCK_STREAM, 0, pair));
40
return
{pair[0], pair[1]};
41
}
42
43
inline
std::tuple<ProcessId, SocketId>
SpawnProcess
(
const
std::function<std::vector<std::string>(std::string)>& spawn_argv)
44
{
45
ProcessId
pid;
46
SocketId
socket =
SpawnProcess
(pid, [&](
int
fd) {
return
spawn_argv(
strprintf
(
"%d"
, fd)); });
47
return
{pid, socket};
48
}
49
50
inline
SocketId
StartSpawned
(
const
std::string& connect_info)
51
{
52
auto
socket = ToIntegral<SocketId>(connect_info);
53
if
(!socket)
throw
std::invalid_argument(
strprintf
(
"Invalid socket descriptor '%s'"
, connect_info));
54
return
*socket;
55
}
56
57
inline
ThreadContext
&
CurrentThread
()
58
{
59
return
g_thread_context
;
60
}
61
#endif
62
}
// namespace mp
63
64
#endif
// BITCOIN_IPC_UTIL_H
mp::EventLoop
Event loop implementation.
Definition:
proxy-io.h:242
util.h
mp
Functions to serialize / deserialize common bitcoin types.
Definition:
common-types.h:57
mp::Stream
SocketId Stream
Definition:
util.h:30
mp::ProcessId
int ProcessId
Definition:
util.h:26
mp::CurrentThread
ThreadContext & CurrentThread()
Definition:
util.h:57
mp::SpawnProcess
int SpawnProcess(int &pid, FdToArgsFn &&fd_to_args)
Spawn a new process that communicates with the current process over a socket pair.
Definition:
util.cpp:119
mp::MakeStream
Stream MakeStream(EventLoop &, SocketId socket)
Definition:
util.h:31
mp::SocketId
int SocketId
Definition:
util.h:27
mp::g_thread_context
thread_local ThreadContext g_thread_context
Definition:
proxy.cpp:44
mp::StartSpawned
SocketId StartSpawned(const std::string &connect_info)
Definition:
util.h:50
mp::SocketPair
std::array< SocketId, 2 > SocketPair()
Definition:
util.h:36
mp::SocketError
constexpr SocketId SocketError
Definition:
util.h:28
proxy-io.h
strencodings.h
mp::ThreadContext
The thread_local ThreadContext g_thread_context struct provides information about individual threads ...
Definition:
proxy-io.h:706
tinyformat.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition:
tinyformat.h:1172
version.h
Major and minor version numbers.
Generated on Thu Jul 30 2026 20:00:35 for Bitcoin Core by
1.9.4