mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-23 12:33:26 -05:00

Follow-up for PR #30856, commit 0dd66251
.
-BEGIN VERIFY SCRIPT-
sed -i "s|config/bitcoin-config\.h|bitcoin-build-config.h|g" $(git grep -l config/bitcoin-config\.h)
sed -i "s|bitcoin-config\.h|bitcoin-build-config.h|g" $(git grep -l "bitcoin-config\.h" ./src ./test ./cmake)
git mv ./cmake/bitcoin-config.h.in ./cmake/bitcoin-build-config.h.in
-END VERIFY SCRIPT-
109 lines
2.1 KiB
C++
109 lines
2.1 KiB
C++
// Copyright (c) 2021-2022 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
#include <util/tokenpipe.h>
|
|
|
|
#include <bitcoin-build-config.h> // IWYU pragma: keep
|
|
|
|
#ifndef WIN32
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <optional>
|
|
#include <unistd.h>
|
|
|
|
TokenPipeEnd TokenPipe::TakeReadEnd()
|
|
{
|
|
TokenPipeEnd res(m_fds[0]);
|
|
m_fds[0] = -1;
|
|
return res;
|
|
}
|
|
|
|
TokenPipeEnd TokenPipe::TakeWriteEnd()
|
|
{
|
|
TokenPipeEnd res(m_fds[1]);
|
|
m_fds[1] = -1;
|
|
return res;
|
|
}
|
|
|
|
TokenPipeEnd::TokenPipeEnd(int fd) : m_fd(fd)
|
|
{
|
|
}
|
|
|
|
TokenPipeEnd::~TokenPipeEnd()
|
|
{
|
|
Close();
|
|
}
|
|
|
|
int TokenPipeEnd::TokenWrite(uint8_t token)
|
|
{
|
|
while (true) {
|
|
ssize_t result = write(m_fd, &token, 1);
|
|
if (result < 0) {
|
|
// Failure. It's possible that the write was interrupted by a signal,
|
|
// in that case retry.
|
|
if (errno != EINTR) {
|
|
return TS_ERR;
|
|
}
|
|
} else if (result == 0) {
|
|
return TS_EOS;
|
|
} else { // ==1
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
int TokenPipeEnd::TokenRead()
|
|
{
|
|
uint8_t token;
|
|
while (true) {
|
|
ssize_t result = read(m_fd, &token, 1);
|
|
if (result < 0) {
|
|
// Failure. Check if the read was interrupted by a signal,
|
|
// in that case retry.
|
|
if (errno != EINTR) {
|
|
return TS_ERR;
|
|
}
|
|
} else if (result == 0) {
|
|
return TS_EOS;
|
|
} else { // ==1
|
|
return token;
|
|
}
|
|
}
|
|
return token;
|
|
}
|
|
|
|
void TokenPipeEnd::Close()
|
|
{
|
|
if (m_fd != -1) close(m_fd);
|
|
m_fd = -1;
|
|
}
|
|
|
|
std::optional<TokenPipe> TokenPipe::Make()
|
|
{
|
|
int fds[2] = {-1, -1};
|
|
#if HAVE_O_CLOEXEC && HAVE_DECL_PIPE2
|
|
if (pipe2(fds, O_CLOEXEC) != 0) {
|
|
return std::nullopt;
|
|
}
|
|
#else
|
|
if (pipe(fds) != 0) {
|
|
return std::nullopt;
|
|
}
|
|
#endif
|
|
return TokenPipe(fds);
|
|
}
|
|
|
|
TokenPipe::~TokenPipe()
|
|
{
|
|
Close();
|
|
}
|
|
|
|
void TokenPipe::Close()
|
|
{
|
|
if (m_fds[0] != -1) close(m_fds[0]);
|
|
if (m_fds[1] != -1) close(m_fds[1]);
|
|
m_fds[0] = m_fds[1] = -1;
|
|
}
|
|
|
|
#endif // WIN32
|