Bitcoin Core 31.99.0
P2P Digital Currency
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
20namespace 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
25class EventLoop;
26using ProcessId = int;
27using SocketId = int;
28constexpr SocketId SocketError{-1};
29
32{
33 return socket;
34}
35
36inline 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
43inline 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
50inline 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
58{
59 return g_thread_context;
60}
61#endif
62} // namespace mp
63
64#endif // BITCOIN_IPC_UTIL_H
Event loop implementation.
Definition: proxy-io.h:242
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
SocketId Stream
Definition: util.h:30
int ProcessId
Definition: util.h:26
ThreadContext & CurrentThread()
Definition: util.h:57
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
Stream MakeStream(EventLoop &, SocketId socket)
Definition: util.h:31
int SocketId
Definition: util.h:27
thread_local ThreadContext g_thread_context
Definition: proxy.cpp:44
SocketId StartSpawned(const std::string &connect_info)
Definition: util.h:50
std::array< SocketId, 2 > SocketPair()
Definition: util.h:36
constexpr SocketId SocketError
Definition: util.h:28
The thread_local ThreadContext g_thread_context struct provides information about individual threads ...
Definition: proxy-io.h:706
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
Major and minor version numbers.