Coverage Report

Created: 2026-07-14 18:13

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