Bitcoin Core 31.99.0
P2P Digital Currency
tokenpipe.cpp
Go to the documentation of this file.
1// Copyright (c) 2021-present 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#include <bitcoin-build-config.h> // IWYU pragma: keep
6
7#include <util/tokenpipe.h>
8
9#ifndef WIN32
10
11#include <cerrno>
12#include <optional>
13
14#include <fcntl.h>
15#include <sys/types.h>
16#include <unistd.h>
17
19{
20 TokenPipeEnd res(m_fds[0]);
21 m_fds[0] = -1;
22 return res;
23}
24
26{
27 TokenPipeEnd res(m_fds[1]);
28 m_fds[1] = -1;
29 return res;
30}
31
33{
34}
35
37{
38 Close();
39}
40
41int TokenPipeEnd::TokenWrite(uint8_t token)
42{
43 while (true) {
44 ssize_t result = write(m_fd, &token, 1);
45 if (result < 0) {
46 // Failure. It's possible that the write was interrupted by a signal,
47 // in that case retry.
48 if (errno != EINTR) {
49 return TS_ERR;
50 }
51 } else if (result == 0) {
52 return TS_EOS;
53 } else { // ==1
54 return 0;
55 }
56 }
57}
58
60{
61 uint8_t token;
62 while (true) {
63 ssize_t result = read(m_fd, &token, 1);
64 if (result < 0) {
65 // Failure. Check if the read was interrupted by a signal,
66 // in that case retry.
67 if (errno != EINTR) {
68 return TS_ERR;
69 }
70 } else if (result == 0) {
71 return TS_EOS;
72 } else { // ==1
73 return token;
74 }
75 }
76 return token;
77}
78
80{
81 if (m_fd != -1) close(m_fd);
82 m_fd = -1;
83}
84
85std::optional<TokenPipe> TokenPipe::Make()
86{
87 int fds[2] = {-1, -1};
88#if HAVE_O_CLOEXEC && HAVE_DECL_PIPE2
89 if (pipe2(fds, O_CLOEXEC) != 0) {
90 return std::nullopt;
91 }
92#else
93 if (pipe(fds) != 0) {
94 return std::nullopt;
95 }
96#endif
97 return TokenPipe(fds);
98}
99
101{
102 Close();
103}
104
106{
107 if (m_fds[0] != -1) close(m_fds[0]);
108 if (m_fds[1] != -1) close(m_fds[1]);
109 m_fds[0] = m_fds[1] = -1;
110}
111
112#endif // WIN32
One end of a token pipe.
Definition: tokenpipe.h:15
TokenPipeEnd(int fd=-1)
Definition: tokenpipe.cpp:32
@ TS_ERR
I/O error.
Definition: tokenpipe.h:25
@ TS_EOS
Unexpected end of stream.
Definition: tokenpipe.h:26
int TokenWrite(uint8_t token)
Write token to endpoint.
Definition: tokenpipe.cpp:41
void Close()
Explicit close function.
Definition: tokenpipe.cpp:79
int TokenRead()
Read token from endpoint.
Definition: tokenpipe.cpp:59
void Close()
Close and end of the pipe that hasn't been moved out.
Definition: tokenpipe.cpp:105
TokenPipeEnd TakeReadEnd()
Take the read end of this pipe.
Definition: tokenpipe.cpp:18
TokenPipeEnd TakeWriteEnd()
Take the write end of this pipe.
Definition: tokenpipe.cpp:25
int m_fds[2]
Definition: tokenpipe.h:78
static std::optional< TokenPipe > Make()
Create a new pipe.
Definition: tokenpipe.cpp:85
TokenPipe(int fds[2])
Definition: tokenpipe.h:80