include/boost/corosio/native/detail/select/select_traits.hpp
100.0% Lines (77/0/77)
100.0% List of functions (12/0/12)
Functions (12)
Function
Calls
Lines
Blocks
boost::corosio::detail::select_traits::stream_socket_hook::on_set_option(int, int, int, void const*, unsigned long)
:53
100x
100.0%
100.0%
boost::corosio::detail::select_traits::stream_socket_hook::pre_shutdown(int)
:63
18529x
100.0%
100.0%
boost::corosio::detail::select_traits::stream_socket_hook::pre_destroy(int)
:64
6013x
100.0%
100.0%
boost::corosio::detail::select_traits::write_policy::write(int, iovec*, int)
:69
72x
100.0%
100.0%
boost::corosio::detail::select_traits::write_policy::write_one(int, void const*, unsigned long)
:94
98344x
100.0%
100.0%
boost::corosio::detail::select_traits::accept_policy::do_accept(int, sockaddr_storage&, unsigned int&)
:113
3950x
100.0%
100.0%
boost::corosio::detail::select_traits::create_socket(int, int, int)
:183
2449x
100.0%
100.0%
boost::corosio::detail::select_traits::set_fd_options(int)
:190
2443x
100.0%
100.0%
boost::corosio::detail::select_traits::configure_ip_socket(int, int)
:223
2133x
100.0%
100.0%
boost::corosio::detail::select_traits::configure_ip_acceptor(int, int)
:238
212x
100.0%
100.0%
boost::corosio::detail::select_traits::configure_local_socket(int)
:252
98x
100.0%
100.0%
boost::corosio::detail::select_traits::validate_assigned_fd(int)
:261
146x
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 | #include <tuple> | ||
| 22 | |||
| 23 | #include <errno.h> | ||
| 24 | #include <fcntl.h> | ||
| 25 | #include <netinet/in.h> | ||
| 26 | #include <sys/select.h> | ||
| 27 | #include <sys/socket.h> | ||
| 28 | #include <unistd.h> | ||
| 29 | |||
| 30 | /* select backend traits. | ||
| 31 | |||
| 32 | Captures the platform-specific behavior of the portable select() backend: | ||
| 33 | manual fcntl for O_NONBLOCK/FD_CLOEXEC, FD_SETSIZE validation, | ||
| 34 | mandatory SO_NOSIGPIPE where the platform defines it, | ||
| 35 | sendmsg(MSG_NOSIGNAL) where available, and accept()+fcntl for | ||
| 36 | accepted connections. | ||
| 37 | */ | ||
| 38 | |||
| 39 | namespace boost::corosio::detail { | ||
| 40 | |||
| 41 | class select_scheduler; | ||
| 42 | |||
| 43 | struct select_traits | ||
| 44 | { | ||
| 45 | using scheduler_type = select_scheduler; | ||
| 46 | using desc_state_type = reactor_descriptor_state; | ||
| 47 | |||
| 48 | static constexpr bool needs_write_notification = true; | ||
| 49 | |||
| 50 | // No extra per-socket state or lifecycle hooks needed for select. | ||
| 51 | struct stream_socket_hook | ||
| 52 | { | ||
| 53 | 100x | std::error_code on_set_option( | |
| 54 | int fd, int level, int optname, | ||
| 55 | void const* data, std::size_t size) noexcept | ||
| 56 | { | ||
| 57 | 100x | if (::setsockopt( | |
| 58 | fd, level, optname, data, | ||
| 59 | 100x | static_cast<socklen_t>(size)) != 0) | |
| 60 | 4x | return make_err(errno); | |
| 61 | 96x | return {}; | |
| 62 | } | ||
| 63 | 18529x | static void pre_shutdown(int) noexcept {} | |
| 64 | 6013x | static void pre_destroy(int) noexcept {} | |
| 65 | }; | ||
| 66 | |||
| 67 | struct write_policy | ||
| 68 | { | ||
| 69 | 72x | static ssize_t write(int fd, iovec* iovecs, int count) noexcept | |
| 70 | { | ||
| 71 | 72x | msghdr msg{}; | |
| 72 | 72x | msg.msg_iov = iovecs; | |
| 73 | 72x | msg.msg_iovlen = static_cast<std::size_t>(count); | |
| 74 | |||
| 75 | #ifdef MSG_NOSIGNAL | ||
| 76 | 72x | constexpr int send_flags = MSG_NOSIGNAL; | |
| 77 | #else | ||
| 78 | constexpr int send_flags = 0; | ||
| 79 | #endif | ||
| 80 | |||
| 81 | ssize_t n; | ||
| 82 | do | ||
| 83 | { | ||
| 84 | 73x | n = ::sendmsg(fd, &msg, send_flags); | |
| 85 | } | ||
| 86 | 73x | while (n < 0 && errno == EINTR); | |
| 87 | 72x | return n; | |
| 88 | } | ||
| 89 | |||
| 90 | // Single-buffer fast path. Where MSG_NOSIGNAL exists we use | ||
| 91 | // send() to suppress SIGPIPE inline; otherwise fall back to | ||
| 92 | // write() and rely on the SO_NOSIGPIPE set in accept_policy | ||
| 93 | // and set_fd_options. | ||
| 94 | 98344x | static ssize_t write_one( | |
| 95 | int fd, void const* data, std::size_t size) noexcept | ||
| 96 | { | ||
| 97 | ssize_t n; | ||
| 98 | do | ||
| 99 | { | ||
| 100 | #ifdef MSG_NOSIGNAL | ||
| 101 | 98345x | n = ::send(fd, data, size, MSG_NOSIGNAL); | |
| 102 | #else | ||
| 103 | n = ::write(fd, data, size); | ||
| 104 | #endif | ||
| 105 | } | ||
| 106 | 98345x | while (n < 0 && errno == EINTR); | |
| 107 | 98344x | return n; | |
| 108 | } | ||
| 109 | }; | ||
| 110 | |||
| 111 | struct accept_policy | ||
| 112 | { | ||
| 113 | 3950x | static int do_accept( | |
| 114 | int fd, sockaddr_storage& peer, socklen_t& addrlen) noexcept | ||
| 115 | { | ||
| 116 | 3950x | addrlen = sizeof(peer); | |
| 117 | int new_fd; | ||
| 118 | do | ||
| 119 | { | ||
| 120 | 3951x | new_fd = ::accept( | |
| 121 | fd, reinterpret_cast<sockaddr*>(&peer), &addrlen); | ||
| 122 | } | ||
| 123 | 3951x | while (new_fd < 0 && errno == EINTR); | |
| 124 | |||
| 125 | 3950x | if (new_fd < 0) | |
| 126 | 1976x | return new_fd; | |
| 127 | |||
| 128 | 1974x | if (new_fd >= FD_SETSIZE) | |
| 129 | { | ||
| 130 | 1x | ::close(new_fd); | |
| 131 | 1x | errno = EMFILE; | |
| 132 | 1x | return -1; | |
| 133 | } | ||
| 134 | |||
| 135 | 1973x | int flags = ::fcntl(new_fd, F_GETFL, 0); | |
| 136 | 1973x | if (flags == -1) | |
| 137 | { | ||
| 138 | 1x | int err = errno; | |
| 139 | 1x | ::close(new_fd); | |
| 140 | 1x | errno = err; | |
| 141 | 1x | return -1; | |
| 142 | } | ||
| 143 | |||
| 144 | 1972x | if (::fcntl(new_fd, F_SETFL, flags | O_NONBLOCK) == -1) | |
| 145 | { | ||
| 146 | 1x | int err = errno; | |
| 147 | 1x | ::close(new_fd); | |
| 148 | 1x | errno = err; | |
| 149 | 1x | return -1; | |
| 150 | } | ||
| 151 | |||
| 152 | 1971x | if (::fcntl(new_fd, F_SETFD, FD_CLOEXEC) == -1) | |
| 153 | { | ||
| 154 | 1x | int err = errno; | |
| 155 | 1x | ::close(new_fd); | |
| 156 | 1x | errno = err; | |
| 157 | 1x | return -1; | |
| 158 | } | ||
| 159 | |||
| 160 | #ifdef SO_NOSIGPIPE | ||
| 161 | // MSG_NOSIGNAL is not universal across the platforms this | ||
| 162 | // portable backend covers, and the write() the fast path | ||
| 163 | // falls back to there takes no flag at all; SO_NOSIGPIPE is | ||
| 164 | // the per-descriptor guard that covers both. Treat failure | ||
| 165 | // as fatal, matching the kqueue backend. | ||
| 166 | int one = 1; | ||
| 167 | if (::setsockopt( | ||
| 168 | new_fd, SOL_SOCKET, SO_NOSIGPIPE, | ||
| 169 | &one, sizeof(one)) != 0) | ||
| 170 | { | ||
| 171 | int err = errno; | ||
| 172 | ::close(new_fd); | ||
| 173 | errno = err; | ||
| 174 | return -1; | ||
| 175 | } | ||
| 176 | #endif | ||
| 177 | |||
| 178 | 1970x | return new_fd; | |
| 179 | } | ||
| 180 | }; | ||
| 181 | |||
| 182 | // Create a plain socket (no atomic flags -- select is POSIX-portable). | ||
| 183 | 2449x | static int create_socket(int family, int type, int protocol) noexcept | |
| 184 | { | ||
| 185 | 2449x | return ::socket(family, type, protocol); | |
| 186 | } | ||
| 187 | |||
| 188 | // Set O_NONBLOCK, FD_CLOEXEC; check FD_SETSIZE; optionally SO_NOSIGPIPE. | ||
| 189 | // Caller is responsible for closing fd on error. | ||
| 190 | 2443x | static std::error_code set_fd_options(int fd) noexcept | |
| 191 | { | ||
| 192 | 2443x | int flags = ::fcntl(fd, F_GETFL, 0); | |
| 193 | 2443x | if (flags == -1) | |
| 194 | 2x | return make_err(errno); | |
| 195 | 2441x | if (::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) | |
| 196 | 2x | return make_err(errno); | |
| 197 | 2439x | if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) | |
| 198 | 2x | return make_err(errno); | |
| 199 | |||
| 200 | 2437x | if (fd >= FD_SETSIZE) | |
| 201 | 2x | return make_err(EMFILE); | |
| 202 | |||
| 203 | #ifdef SO_NOSIGPIPE | ||
| 204 | // MSG_NOSIGNAL is not universal across the platforms this | ||
| 205 | // portable backend covers, and the write() the fast path falls | ||
| 206 | // back to there takes no flag at all; SO_NOSIGPIPE is the | ||
| 207 | // per-descriptor guard that covers both. Treat failure as fatal, | ||
| 208 | // matching the kqueue backend. Caller closes fd on error. | ||
| 209 | { | ||
| 210 | int one = 1; | ||
| 211 | if (::setsockopt( | ||
| 212 | fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)) != 0) | ||
| 213 | return make_err(errno); | ||
| 214 | } | ||
| 215 | #endif | ||
| 216 | |||
| 217 | 2435x | return {}; | |
| 218 | } | ||
| 219 | |||
| 220 | // Apply protocol-specific options after socket creation. | ||
| 221 | // For IP sockets, sets IPV6_V6ONLY on AF_INET6 (best-effort). | ||
| 222 | static std::error_code | ||
| 223 | 2133x | configure_ip_socket(int fd, int family) noexcept | |
| 224 | { | ||
| 225 | 2133x | if (family == AF_INET6) | |
| 226 | { | ||
| 227 | 22x | int one = 1; | |
| 228 | 44x | std::ignore = ::setsockopt( | |
| 229 | 22x | fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); | |
| 230 | } | ||
| 231 | |||
| 232 | 2133x | return set_fd_options(fd); | |
| 233 | } | ||
| 234 | |||
| 235 | // Apply protocol-specific options for acceptor sockets. | ||
| 236 | // For IP acceptors, sets IPV6_V6ONLY=0 (dual-stack, best-effort). | ||
| 237 | static std::error_code | ||
| 238 | 212x | configure_ip_acceptor(int fd, int family) noexcept | |
| 239 | { | ||
| 240 | 212x | if (family == AF_INET6) | |
| 241 | { | ||
| 242 | 11x | int val = 0; | |
| 243 | 22x | std::ignore = ::setsockopt( | |
| 244 | 11x | fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val)); | |
| 245 | } | ||
| 246 | |||
| 247 | 212x | return set_fd_options(fd); | |
| 248 | } | ||
| 249 | |||
| 250 | // Apply options for local (unix) sockets. | ||
| 251 | static std::error_code | ||
| 252 | 98x | configure_local_socket(int fd) noexcept | |
| 253 | { | ||
| 254 | 98x | return set_fd_options(fd); | |
| 255 | } | ||
| 256 | |||
| 257 | // Non-mutating validation for fds adopted via assign(). Select's | ||
| 258 | // reactor cannot handle fds above FD_SETSIZE, so reject them up | ||
| 259 | // front instead of letting FD_SET clobber unrelated memory. | ||
| 260 | static std::error_code | ||
| 261 | 146x | validate_assigned_fd(int fd) noexcept | |
| 262 | { | ||
| 263 | 146x | if (fd >= FD_SETSIZE) | |
| 264 | 2x | return make_err(EMFILE); | |
| 265 | 144x | return {}; | |
| 266 | } | ||
| 267 | }; | ||
| 268 | |||
| 269 | } // namespace boost::corosio::detail | ||
| 270 | |||
| 271 | #endif // BOOST_COROSIO_HAS_SELECT | ||
| 272 | |||
| 273 | #endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP | ||
| 274 |