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