Coverage Report

Created: 2026-05-30 09:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/util/tokenpipe.cpp
Line
Count
Source
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
18
TokenPipeEnd TokenPipe::TakeReadEnd()
19
1.87k
{
20
1.87k
    TokenPipeEnd res(m_fds[0]);
21
1.87k
    m_fds[0] = -1;
22
1.87k
    return res;
23
1.87k
}
24
25
TokenPipeEnd TokenPipe::TakeWriteEnd()
26
1.87k
{
27
1.87k
    TokenPipeEnd res(m_fds[1]);
28
1.87k
    m_fds[1] = -1;
29
1.87k
    return res;
30
1.87k
}
31
32
8.65k
TokenPipeEnd::TokenPipeEnd(int fd) : m_fd(fd)
33
8.65k
{
34
8.65k
}
35
36
TokenPipeEnd::~TokenPipeEnd()
37
6.25k
{
38
6.25k
    Close();
39
6.25k
}
40
41
int TokenPipeEnd::TokenWrite(uint8_t token)
42
1.00k
{
43
1.00k
    while (true) {
44
1.00k
        ssize_t result = write(m_fd, &token, 1);
45
1.00k
        if (result < 0) {
46
            // Failure. It's possible that the write was interrupted by a signal,
47
            // in that case retry.
48
0
            if (errno != EINTR) {
49
0
                return TS_ERR;
50
0
            }
51
1.00k
        } else if (result == 0) {
52
0
            return TS_EOS;
53
1.00k
        } else { // ==1
54
1.00k
            return 0;
55
1.00k
        }
56
1.00k
    }
57
1.00k
}
58
59
int TokenPipeEnd::TokenRead()
60
1.00k
{
61
1.00k
    uint8_t token;
62
1.00k
    while (true) {
63
1.00k
        ssize_t result = read(m_fd, &token, 1);
64
1.00k
        if (result < 0) {
65
            // Failure. Check if the read was interrupted by a signal,
66
            // in that case retry.
67
9
            if (errno != EINTR) {
68
0
                return TS_ERR;
69
0
            }
70
1.00k
        } else if (result == 0) {
71
0
            return TS_EOS;
72
1.00k
        } else { // ==1
73
1.00k
            return token;
74
1.00k
        }
75
1.00k
    }
76
0
    return token;
77
1.00k
}
78
79
void TokenPipeEnd::Close()
80
10.0k
{
81
10.0k
    if (m_fd != -1) close(m_fd);
82
10.0k
    m_fd = -1;
83
10.0k
}
84
85
std::optional<TokenPipe> TokenPipe::Make()
86
1.87k
{
87
1.87k
    int fds[2] = {-1, -1};
88
1.87k
#if HAVE_O_CLOEXEC && HAVE_DECL_PIPE2
89
1.87k
    if (pipe2(fds, O_CLOEXEC) != 0) {
90
0
        return std::nullopt;
91
0
    }
92
#else
93
    if (pipe(fds) != 0) {
94
        return std::nullopt;
95
    }
96
#endif
97
1.87k
    return TokenPipe(fds);
98
1.87k
}
99
100
TokenPipe::~TokenPipe()
101
3.75k
{
102
3.75k
    Close();
103
3.75k
}
104
105
void TokenPipe::Close()
106
3.75k
{
107
3.75k
    if (m_fds[0] != -1) close(m_fds[0]);
108
3.75k
    if (m_fds[1] != -1) close(m_fds[1]);
109
3.75k
    m_fds[0] = m_fds[1] = -1;
110
3.75k
}
111
112
#endif // WIN32