Line data Source code
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 1 : inline ThreadContext& CurrentThread()
58 : {
59 1 : return g_thread_context;
60 : }
61 : #endif
62 : } // namespace mp
63 :
64 : #endif // BITCOIN_IPC_UTIL_H
|