LCOV - code coverage report
Current view: top level - corosio/native/detail/select - select_traits.hpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 68.7 % 67 46 21
Test Date: 2026-04-09 23:21:11 Functions: 90.0 % 10 9 1

           TLA  Line data    Source code
       1                 : //
       2                 : // Copyright (c) 2026 Michael Vandeberg
       3                 : //
       4                 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
       5                 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
       6                 : //
       7                 : // Official repository: https://github.com/cppalliance/corosio
       8                 : //
       9                 : 
      10                 : #ifndef BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP
      11                 : #define BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP
      12                 : 
      13                 : #include <boost/corosio/detail/platform.hpp>
      14                 : 
      15                 : #if BOOST_COROSIO_HAS_SELECT
      16                 : 
      17                 : #include <boost/corosio/native/detail/make_err.hpp>
      18                 : #include <boost/corosio/native/detail/reactor/reactor_descriptor_state.hpp>
      19                 : 
      20                 : #include <system_error>
      21                 : 
      22                 : #include <errno.h>
      23                 : #include <fcntl.h>
      24                 : #include <netinet/in.h>
      25                 : #include <sys/select.h>
      26                 : #include <sys/socket.h>
      27                 : #include <unistd.h>
      28                 : 
      29                 : /* select backend traits.
      30                 : 
      31                 :    Captures the platform-specific behavior of the portable select() backend:
      32                 :    manual fcntl for O_NONBLOCK/FD_CLOEXEC, FD_SETSIZE validation,
      33                 :    conditional SO_NOSIGPIPE, sendmsg(MSG_NOSIGNAL) where available,
      34                 :    and accept()+fcntl for accepted connections.
      35                 : */
      36                 : 
      37                 : namespace boost::corosio::detail {
      38                 : 
      39                 : class select_scheduler;
      40                 : struct select_descriptor_state;
      41                 : 
      42                 : struct select_traits
      43                 : {
      44                 :     using scheduler_type    = select_scheduler;
      45                 :     using desc_state_type   = select_descriptor_state;
      46                 : 
      47                 :     static constexpr bool needs_write_notification = true;
      48                 : 
      49                 :     /// No extra per-socket state or lifecycle hooks needed for select.
      50                 :     struct stream_socket_hook
      51                 :     {
      52 HIT          28 :         std::error_code on_set_option(
      53                 :             int fd, int level, int optname,
      54                 :             void const* data, std::size_t size) noexcept
      55                 :         {
      56              28 :             if (::setsockopt(
      57                 :                     fd, level, optname, data,
      58              28 :                     static_cast<socklen_t>(size)) != 0)
      59 MIS           0 :                 return make_err(errno);
      60 HIT          28 :             return {};
      61                 :         }
      62 MIS           0 :         static void pre_shutdown(int) noexcept {}
      63 HIT       10877 :         static void pre_destroy(int) noexcept {}
      64                 :     };
      65                 : 
      66                 :     struct write_policy
      67                 :     {
      68          103585 :         static ssize_t write(int fd, iovec* iovecs, int count) noexcept
      69                 :         {
      70          103585 :             msghdr msg{};
      71          103585 :             msg.msg_iov    = iovecs;
      72          103585 :             msg.msg_iovlen = static_cast<std::size_t>(count);
      73                 : 
      74                 : #ifdef MSG_NOSIGNAL
      75          103585 :             constexpr int send_flags = MSG_NOSIGNAL;
      76                 : #else
      77                 :             constexpr int send_flags = 0;
      78                 : #endif
      79                 : 
      80                 :             ssize_t n;
      81                 :             do
      82                 :             {
      83          103585 :                 n = ::sendmsg(fd, &msg, send_flags);
      84                 :             }
      85          103585 :             while (n < 0 && errno == EINTR);
      86          103585 :             return n;
      87                 :         }
      88                 :     };
      89                 : 
      90                 :     struct accept_policy
      91                 :     {
      92            7234 :         static int do_accept(int fd, sockaddr_storage& peer) noexcept
      93                 :         {
      94            7234 :             socklen_t addrlen = sizeof(peer);
      95                 :             int new_fd;
      96                 :             do
      97                 :             {
      98            7234 :                 new_fd = ::accept(
      99                 :                     fd, reinterpret_cast<sockaddr*>(&peer), &addrlen);
     100                 :             }
     101            7234 :             while (new_fd < 0 && errno == EINTR);
     102                 : 
     103            7234 :             if (new_fd < 0)
     104            3617 :                 return new_fd;
     105                 : 
     106            3617 :             if (new_fd >= FD_SETSIZE)
     107                 :             {
     108 MIS           0 :                 ::close(new_fd);
     109               0 :                 errno = EINVAL;
     110               0 :                 return -1;
     111                 :             }
     112                 : 
     113 HIT        3617 :             int flags = ::fcntl(new_fd, F_GETFL, 0);
     114            3617 :             if (flags == -1)
     115                 :             {
     116 MIS           0 :                 int err = errno;
     117               0 :                 ::close(new_fd);
     118               0 :                 errno = err;
     119               0 :                 return -1;
     120                 :             }
     121                 : 
     122 HIT        3617 :             if (::fcntl(new_fd, F_SETFL, flags | O_NONBLOCK) == -1)
     123                 :             {
     124 MIS           0 :                 int err = errno;
     125               0 :                 ::close(new_fd);
     126               0 :                 errno = err;
     127               0 :                 return -1;
     128                 :             }
     129                 : 
     130 HIT        3617 :             if (::fcntl(new_fd, F_SETFD, FD_CLOEXEC) == -1)
     131                 :             {
     132 MIS           0 :                 int err = errno;
     133               0 :                 ::close(new_fd);
     134               0 :                 errno = err;
     135               0 :                 return -1;
     136                 :             }
     137                 : 
     138                 : #ifdef SO_NOSIGPIPE
     139                 :             int one = 1;
     140                 :             if (::setsockopt(
     141                 :                     new_fd, SOL_SOCKET, SO_NOSIGPIPE,
     142                 :                     &one, sizeof(one)) == -1)
     143                 :             {
     144                 :                 int err = errno;
     145                 :                 ::close(new_fd);
     146                 :                 errno = err;
     147                 :                 return -1;
     148                 :             }
     149                 : #endif
     150                 : 
     151 HIT        3617 :             return new_fd;
     152                 :         }
     153                 :     };
     154                 : 
     155                 :     /// Create a plain socket (no atomic flags — select is POSIX-portable).
     156            3760 :     static int create_socket(int family, int type, int protocol) noexcept
     157                 :     {
     158            3760 :         return ::socket(family, type, protocol);
     159                 :     }
     160                 : 
     161                 :     /// Set O_NONBLOCK, FD_CLOEXEC; check FD_SETSIZE; optionally SO_NOSIGPIPE.
     162                 :     /// Caller is responsible for closing fd on error.
     163            3760 :     static std::error_code set_fd_options(int fd) noexcept
     164                 :     {
     165            3760 :         int flags = ::fcntl(fd, F_GETFL, 0);
     166            3760 :         if (flags == -1)
     167 MIS           0 :             return make_err(errno);
     168 HIT        3760 :         if (::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
     169 MIS           0 :             return make_err(errno);
     170 HIT        3760 :         if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
     171 MIS           0 :             return make_err(errno);
     172                 : 
     173 HIT        3760 :         if (fd >= FD_SETSIZE)
     174 MIS           0 :             return make_err(EMFILE);
     175                 : 
     176                 : #ifdef SO_NOSIGPIPE
     177                 :         {
     178                 :             int one = 1;
     179                 :             ::setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one));
     180                 :         }
     181                 : #endif
     182                 : 
     183 HIT        3760 :         return {};
     184                 :     }
     185                 : 
     186                 :     /// Apply protocol-specific options after socket creation.
     187                 :     /// For IP sockets, sets IPV6_V6ONLY on AF_INET6.
     188                 :     static std::error_code
     189            3677 :     configure_ip_socket(int fd, int family) noexcept
     190                 :     {
     191            3677 :         if (family == AF_INET6)
     192                 :         {
     193              13 :             int one = 1;
     194              13 :             ::setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
     195                 :         }
     196                 : 
     197            3677 :         return set_fd_options(fd);
     198                 :     }
     199                 : 
     200                 :     /// Apply protocol-specific options for acceptor sockets.
     201                 :     /// For IP acceptors, sets IPV6_V6ONLY=0 (dual-stack).
     202                 :     static std::error_code
     203              63 :     configure_ip_acceptor(int fd, int family) noexcept
     204                 :     {
     205              63 :         if (family == AF_INET6)
     206                 :         {
     207               8 :             int val = 0;
     208               8 :             ::setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
     209                 :         }
     210                 : 
     211              63 :         return set_fd_options(fd);
     212                 :     }
     213                 : 
     214                 :     /// Apply options for local (unix) sockets.
     215                 :     static std::error_code
     216              20 :     configure_local_socket(int fd) noexcept
     217                 :     {
     218              20 :         return set_fd_options(fd);
     219                 :     }
     220                 : };
     221                 : 
     222                 : } // namespace boost::corosio::detail
     223                 : 
     224                 : #endif // BOOST_COROSIO_HAS_SELECT
     225                 : 
     226                 : #endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP
        

Generated by: LCOV version 2.3