include/boost/corosio/native/detail/select/select_traits.hpp
68.7% Lines (46/67)
90.0% List of functions (9/10)
Functions (10)
Function
Calls
Lines
Blocks
boost::corosio::detail::select_traits::stream_socket_hook::on_set_option(int, int, int, void const*, unsigned long)
:52
28x
80.0%
86.0%
boost::corosio::detail::select_traits::stream_socket_hook::pre_shutdown(int)
:62
0
0.0%
0.0%
boost::corosio::detail::select_traits::stream_socket_hook::pre_destroy(int)
:63
4038x
100.0%
100.0%
boost::corosio::detail::select_traits::write_policy::write(int, iovec*, int)
:68
131116x
100.0%
100.0%
boost::corosio::detail::select_traits::accept_policy::do_accept(int, sockaddr_storage&, unsigned int&)
:92
2674x
44.4%
65.0%
boost::corosio::detail::select_traits::create_socket(int, int, int)
:157
1481x
100.0%
100.0%
boost::corosio::detail::select_traits::set_fd_options(int)
:164
1495x
63.6%
73.0%
boost::corosio::detail::select_traits::configure_ip_socket(int, int)
:190
1398x
100.0%
100.0%
boost::corosio::detail::select_traits::configure_ip_acceptor(int, int)
:204
63x
100.0%
100.0%
boost::corosio::detail::select_traits::configure_local_socket(int)
:217
34x
100.0%
100.0%
| Line | TLA | Hits | 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 | 28x | std::error_code on_set_option( | |
| 53 | int fd, int level, int optname, | ||
| 54 | void const* data, std::size_t size) noexcept | ||
| 55 | { | ||
| 56 | 28x | if (::setsockopt( | |
| 57 | fd, level, optname, data, | ||
| 58 | 28x | static_cast<socklen_t>(size)) != 0) | |
| 59 | ✗ | return make_err(errno); | |
| 60 | 28x | return {}; | |
| 61 | } | ||
| 62 | ✗ | static void pre_shutdown(int) noexcept {} | |
| 63 | 4038x | static void pre_destroy(int) noexcept {} | |
| 64 | }; | ||
| 65 | |||
| 66 | struct write_policy | ||
| 67 | { | ||
| 68 | 131116x | static ssize_t write(int fd, iovec* iovecs, int count) noexcept | |
| 69 | { | ||
| 70 | 131116x | msghdr msg{}; | |
| 71 | 131116x | msg.msg_iov = iovecs; | |
| 72 | 131116x | msg.msg_iovlen = static_cast<std::size_t>(count); | |
| 73 | |||
| 74 | #ifdef MSG_NOSIGNAL | ||
| 75 | 131116x | 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 | 131116x | n = ::sendmsg(fd, &msg, send_flags); | |
| 84 | } | ||
| 85 | 131116x | while (n < 0 && errno == EINTR); | |
| 86 | 131116x | return n; | |
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | struct accept_policy | ||
| 91 | { | ||
| 92 | 2674x | static int do_accept( | |
| 93 | int fd, sockaddr_storage& peer, socklen_t& addrlen) noexcept | ||
| 94 | { | ||
| 95 | 2674x | addrlen = sizeof(peer); | |
| 96 | int new_fd; | ||
| 97 | do | ||
| 98 | { | ||
| 99 | 2674x | new_fd = ::accept( | |
| 100 | fd, reinterpret_cast<sockaddr*>(&peer), &addrlen); | ||
| 101 | } | ||
| 102 | 2674x | while (new_fd < 0 && errno == EINTR); | |
| 103 | |||
| 104 | 2674x | if (new_fd < 0) | |
| 105 | 1337x | return new_fd; | |
| 106 | |||
| 107 | 1337x | if (new_fd >= FD_SETSIZE) | |
| 108 | { | ||
| 109 | ✗ | ::close(new_fd); | |
| 110 | ✗ | errno = EINVAL; | |
| 111 | ✗ | return -1; | |
| 112 | } | ||
| 113 | |||
| 114 | 1337x | int flags = ::fcntl(new_fd, F_GETFL, 0); | |
| 115 | 1337x | if (flags == -1) | |
| 116 | { | ||
| 117 | ✗ | int err = errno; | |
| 118 | ✗ | ::close(new_fd); | |
| 119 | ✗ | errno = err; | |
| 120 | ✗ | return -1; | |
| 121 | } | ||
| 122 | |||
| 123 | 1337x | if (::fcntl(new_fd, F_SETFL, flags | O_NONBLOCK) == -1) | |
| 124 | { | ||
| 125 | ✗ | int err = errno; | |
| 126 | ✗ | ::close(new_fd); | |
| 127 | ✗ | errno = err; | |
| 128 | ✗ | return -1; | |
| 129 | } | ||
| 130 | |||
| 131 | 1337x | if (::fcntl(new_fd, F_SETFD, FD_CLOEXEC) == -1) | |
| 132 | { | ||
| 133 | ✗ | int err = errno; | |
| 134 | ✗ | ::close(new_fd); | |
| 135 | ✗ | errno = err; | |
| 136 | ✗ | return -1; | |
| 137 | } | ||
| 138 | |||
| 139 | #ifdef SO_NOSIGPIPE | ||
| 140 | int one = 1; | ||
| 141 | if (::setsockopt( | ||
| 142 | new_fd, SOL_SOCKET, SO_NOSIGPIPE, | ||
| 143 | &one, sizeof(one)) == -1) | ||
| 144 | { | ||
| 145 | int err = errno; | ||
| 146 | ::close(new_fd); | ||
| 147 | errno = err; | ||
| 148 | return -1; | ||
| 149 | } | ||
| 150 | #endif | ||
| 151 | |||
| 152 | 1337x | return new_fd; | |
| 153 | } | ||
| 154 | }; | ||
| 155 | |||
| 156 | /// Create a plain socket (no atomic flags — select is POSIX-portable). | ||
| 157 | 1481x | static int create_socket(int family, int type, int protocol) noexcept | |
| 158 | { | ||
| 159 | 1481x | return ::socket(family, type, protocol); | |
| 160 | } | ||
| 161 | |||
| 162 | /// Set O_NONBLOCK, FD_CLOEXEC; check FD_SETSIZE; optionally SO_NOSIGPIPE. | ||
| 163 | /// Caller is responsible for closing fd on error. | ||
| 164 | 1495x | static std::error_code set_fd_options(int fd) noexcept | |
| 165 | { | ||
| 166 | 1495x | int flags = ::fcntl(fd, F_GETFL, 0); | |
| 167 | 1495x | if (flags == -1) | |
| 168 | ✗ | return make_err(errno); | |
| 169 | 1495x | if (::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) | |
| 170 | ✗ | return make_err(errno); | |
| 171 | 1495x | if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) | |
| 172 | ✗ | return make_err(errno); | |
| 173 | |||
| 174 | 1495x | if (fd >= FD_SETSIZE) | |
| 175 | ✗ | return make_err(EMFILE); | |
| 176 | |||
| 177 | #ifdef SO_NOSIGPIPE | ||
| 178 | { | ||
| 179 | int one = 1; | ||
| 180 | ::setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); | ||
| 181 | } | ||
| 182 | #endif | ||
| 183 | |||
| 184 | 1495x | return {}; | |
| 185 | } | ||
| 186 | |||
| 187 | /// Apply protocol-specific options after socket creation. | ||
| 188 | /// For IP sockets, sets IPV6_V6ONLY on AF_INET6. | ||
| 189 | static std::error_code | ||
| 190 | 1398x | configure_ip_socket(int fd, int family) noexcept | |
| 191 | { | ||
| 192 | 1398x | if (family == AF_INET6) | |
| 193 | { | ||
| 194 | 13x | int one = 1; | |
| 195 | 13x | ::setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); | |
| 196 | } | ||
| 197 | |||
| 198 | 1398x | return set_fd_options(fd); | |
| 199 | } | ||
| 200 | |||
| 201 | /// Apply protocol-specific options for acceptor sockets. | ||
| 202 | /// For IP acceptors, sets IPV6_V6ONLY=0 (dual-stack). | ||
| 203 | static std::error_code | ||
| 204 | 63x | configure_ip_acceptor(int fd, int family) noexcept | |
| 205 | { | ||
| 206 | 63x | if (family == AF_INET6) | |
| 207 | { | ||
| 208 | 8x | int val = 0; | |
| 209 | 8x | ::setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val)); | |
| 210 | } | ||
| 211 | |||
| 212 | 63x | return set_fd_options(fd); | |
| 213 | } | ||
| 214 | |||
| 215 | /// Apply options for local (unix) sockets. | ||
| 216 | static std::error_code | ||
| 217 | 34x | configure_local_socket(int fd) noexcept | |
| 218 | { | ||
| 219 | 34x | return set_fd_options(fd); | |
| 220 | } | ||
| 221 | }; | ||
| 222 | |||
| 223 | } // namespace boost::corosio::detail | ||
| 224 | |||
| 225 | #endif // BOOST_COROSIO_HAS_SELECT | ||
| 226 | |||
| 227 | #endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP | ||
| 228 |