/tmp/bitcoin/src/ipc/util.h
Line | Count | Source |
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 | 11 | { |
33 | 11 | return socket; |
34 | 11 | } |
35 | | |
36 | | inline std::array<SocketId, 2> SocketPair() |
37 | 1 | { |
38 | 1 | int pair[2]; |
39 | 1 | KJ_SYSCALL(socketpair(AF_UNIX, SOCK_STREAM, 0, pair)); |
40 | 1 | return {pair[0], pair[1]}; |
41 | 1 | } |
42 | | |
43 | | inline std::tuple<ProcessId, SocketId> SpawnProcess(const std::function<std::vector<std::string>(std::string)>& spawn_argv) |
44 | 0 | { |
45 | 0 | ProcessId pid; |
46 | 0 | SocketId socket = SpawnProcess(pid, [&](int fd) { return spawn_argv(strprintf("%d", fd)); }); |
47 | 0 | return {pid, socket}; |
48 | 0 | } |
49 | | |
50 | | inline SocketId StartSpawned(const std::string& connect_info) |
51 | 0 | { |
52 | 0 | auto socket = ToIntegral<SocketId>(connect_info); |
53 | 0 | if (!socket) throw std::invalid_argument(strprintf("Invalid socket descriptor '%s'", connect_info)); |
54 | 0 | return *socket; |
55 | 0 | } |
56 | | |
57 | | inline ThreadContext& CurrentThread() |
58 | 1 | { |
59 | 1 | return g_thread_context; |
60 | 1 | } |
61 | | #endif |
62 | | } // namespace mp |
63 | | |
64 | | #endif // BITCOIN_IPC_UTIL_H |