LCOV - code coverage report
Current view: top level - src/util - tokenpipe.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 55 65 84.6 %
Date: 2022-04-21 14:51:19 Functions: 10 10 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 10 20 50.0 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2021 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                 :            : #include <util/tokenpipe.h>
#       5                 :            : 
#       6                 :            : #include <config/bitcoin-config.h>
#       7                 :            : 
#       8                 :            : #ifndef WIN32
#       9                 :            : 
#      10                 :            : #include <errno.h>
#      11                 :            : #include <fcntl.h>
#      12                 :            : #include <optional>
#      13                 :            : #include <unistd.h>
#      14                 :            : 
#      15                 :            : TokenPipeEnd TokenPipe::TakeReadEnd()
#      16                 :        802 : {
#      17                 :        802 :     TokenPipeEnd res(m_fds[0]);
#      18                 :        802 :     m_fds[0] = -1;
#      19                 :        802 :     return res;
#      20                 :        802 : }
#      21                 :            : 
#      22                 :            : TokenPipeEnd TokenPipe::TakeWriteEnd()
#      23                 :        802 : {
#      24                 :        802 :     TokenPipeEnd res(m_fds[1]);
#      25                 :        802 :     m_fds[1] = -1;
#      26                 :        802 :     return res;
#      27                 :        802 : }
#      28                 :            : 
#      29                 :            : TokenPipeEnd::TokenPipeEnd(int fd) : m_fd(fd)
#      30                 :       4076 : {
#      31                 :       4076 : }
#      32                 :            : 
#      33                 :            : TokenPipeEnd::~TokenPipeEnd()
#      34                 :       2420 : {
#      35                 :       2420 :     Close();
#      36                 :       2420 : }
#      37                 :            : 
#      38                 :            : int TokenPipeEnd::TokenWrite(uint8_t token)
#      39                 :        732 : {
#      40                 :        732 :     while (true) {
#      41                 :        732 :         ssize_t result = write(m_fd, &token, 1);
#      42         [ -  + ]:        732 :         if (result < 0) {
#      43                 :            :             // Failure. It's possible that the write was interrupted by a signal,
#      44                 :            :             // in that case retry.
#      45         [ #  # ]:          0 :             if (errno != EINTR) {
#      46                 :          0 :                 return TS_ERR;
#      47                 :          0 :             }
#      48         [ -  + ]:        732 :         } else if (result == 0) {
#      49                 :          0 :             return TS_EOS;
#      50                 :        732 :         } else { // ==1
#      51                 :        732 :             return 0;
#      52                 :        732 :         }
#      53                 :        732 :     }
#      54                 :        732 : }
#      55                 :            : 
#      56                 :            : int TokenPipeEnd::TokenRead()
#      57                 :        719 : {
#      58                 :        719 :     uint8_t token;
#      59                 :        724 :     while (true) {
#      60                 :        724 :         ssize_t result = read(m_fd, &token, 1);
#      61         [ +  + ]:        724 :         if (result < 0) {
#      62                 :            :             // Failure. Check if the read was interrupted by a signal,
#      63                 :            :             // in that case retry.
#      64         [ -  + ]:          5 :             if (errno != EINTR) {
#      65                 :          0 :                 return TS_ERR;
#      66                 :          0 :             }
#      67         [ -  + ]:        719 :         } else if (result == 0) {
#      68                 :          0 :             return TS_EOS;
#      69                 :        719 :         } else { // ==1
#      70                 :        719 :             return token;
#      71                 :        719 :         }
#      72                 :        724 :     }
#      73                 :          0 :     return token;
#      74                 :        719 : }
#      75                 :            : 
#      76                 :            : void TokenPipeEnd::Close()
#      77                 :       4024 : {
#      78         [ -  + ]:       4024 :     if (m_fd != -1) close(m_fd);
#      79                 :       4024 :     m_fd = -1;
#      80                 :       4024 : }
#      81                 :            : 
#      82                 :            : std::optional<TokenPipe> TokenPipe::Make()
#      83                 :        802 : {
#      84                 :        802 :     int fds[2] = {-1, -1};
#      85                 :        802 : #if HAVE_O_CLOEXEC && HAVE_DECL_PIPE2
#      86         [ -  + ]:        802 :     if (pipe2(fds, O_CLOEXEC) != 0) {
#      87                 :          0 :         return std::nullopt;
#      88                 :          0 :     }
#      89                 :            : #else
#      90                 :            :     if (pipe(fds) != 0) {
#      91                 :            :         return std::nullopt;
#      92                 :            :     }
#      93                 :            : #endif
#      94                 :        802 :     return TokenPipe(fds);
#      95                 :        802 : }
#      96                 :            : 
#      97                 :            : TokenPipe::~TokenPipe()
#      98                 :       1604 : {
#      99                 :       1604 :     Close();
#     100                 :       1604 : }
#     101                 :            : 
#     102                 :            : void TokenPipe::Close()
#     103                 :       1604 : {
#     104         [ -  + ]:       1604 :     if (m_fds[0] != -1) close(m_fds[0]);
#     105         [ -  + ]:       1604 :     if (m_fds[1] != -1) close(m_fds[1]);
#     106                 :       1604 :     m_fds[0] = m_fds[1] = -1;
#     107                 :       1604 : }
#     108                 :            : 
#     109                 :            : #endif // WIN32

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a