LCOV - code coverage report
Current view: top level - src/util - tokenpipe.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 64.4 % 45 29
Test Date: 2024-08-28 04:44:32 Functions: 80.0 % 10 8
Branches: 25.0 % 20 5

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

Generated by: LCOV version 2.0-1