61.33% Lines (46/75)
91.67% Functions (11/12)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2026 Michael Vandeberg | 2 | // Copyright (c) 2026 Michael Vandeberg | |||||
| 3 | // | 3 | // | |||||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 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) | 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 6 | // | 6 | // | |||||
| 7 | // Official repository: https://github.com/cppalliance/corosio | 7 | // Official repository: https://github.com/cppalliance/corosio | |||||
| 8 | // | 8 | // | |||||
| 9 | 9 | |||||||
| 10 | #ifndef BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP | 10 | #ifndef BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP | |||||
| 11 | #define BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP | 11 | #define BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP | |||||
| 12 | 12 | |||||||
| 13 | #include <boost/corosio/detail/platform.hpp> | 13 | #include <boost/corosio/detail/platform.hpp> | |||||
| 14 | 14 | |||||||
| 15 | #if BOOST_COROSIO_HAS_SELECT | 15 | #if BOOST_COROSIO_HAS_SELECT | |||||
| 16 | 16 | |||||||
| 17 | #include <boost/corosio/native/detail/make_err.hpp> | 17 | #include <boost/corosio/native/detail/make_err.hpp> | |||||
| 18 | #include <boost/corosio/native/detail/reactor/reactor_descriptor_state.hpp> | 18 | #include <boost/corosio/native/detail/reactor/reactor_descriptor_state.hpp> | |||||
| 19 | 19 | |||||||
| 20 | #include <system_error> | 20 | #include <system_error> | |||||
| 21 | 21 | |||||||
| 22 | #include <errno.h> | 22 | #include <errno.h> | |||||
| 23 | #include <fcntl.h> | 23 | #include <fcntl.h> | |||||
| 24 | #include <netinet/in.h> | 24 | #include <netinet/in.h> | |||||
| 25 | #include <sys/select.h> | 25 | #include <sys/select.h> | |||||
| 26 | #include <sys/socket.h> | 26 | #include <sys/socket.h> | |||||
| 27 | #include <unistd.h> | 27 | #include <unistd.h> | |||||
| 28 | 28 | |||||||
| 29 | /* select backend traits. | 29 | /* select backend traits. | |||||
| 30 | 30 | |||||||
| 31 | Captures the platform-specific behavior of the portable select() backend: | 31 | Captures the platform-specific behavior of the portable select() backend: | |||||
| 32 | manual fcntl for O_NONBLOCK/FD_CLOEXEC, FD_SETSIZE validation, | 32 | manual fcntl for O_NONBLOCK/FD_CLOEXEC, FD_SETSIZE validation, | |||||
| 33 | conditional SO_NOSIGPIPE, sendmsg(MSG_NOSIGNAL) where available, | 33 | conditional SO_NOSIGPIPE, sendmsg(MSG_NOSIGNAL) where available, | |||||
| 34 | and accept()+fcntl for accepted connections. | 34 | and accept()+fcntl for accepted connections. | |||||
| 35 | */ | 35 | */ | |||||
| 36 | 36 | |||||||
| 37 | namespace boost::corosio::detail { | 37 | namespace boost::corosio::detail { | |||||
| 38 | 38 | |||||||
| 39 | class select_scheduler; | 39 | class select_scheduler; | |||||
| 40 | 40 | |||||||
| 41 | struct select_traits | 41 | struct select_traits | |||||
| 42 | { | 42 | { | |||||
| 43 | using scheduler_type = select_scheduler; | 43 | using scheduler_type = select_scheduler; | |||||
| 44 | using desc_state_type = reactor_descriptor_state; | 44 | using desc_state_type = reactor_descriptor_state; | |||||
| 45 | 45 | |||||||
| 46 | static constexpr bool needs_write_notification = true; | 46 | static constexpr bool needs_write_notification = true; | |||||
| 47 | 47 | |||||||
| 48 | // No extra per-socket state or lifecycle hooks needed for select. | 48 | // No extra per-socket state or lifecycle hooks needed for select. | |||||
| 49 | struct stream_socket_hook | 49 | struct stream_socket_hook | |||||
| 50 | { | 50 | { | |||||
| HITCBC | 51 | 28 | std::error_code on_set_option( | 51 | 28 | std::error_code on_set_option( | ||
| 52 | int fd, int level, int optname, | 52 | int fd, int level, int optname, | |||||
| 53 | void const* data, std::size_t size) noexcept | 53 | void const* data, std::size_t size) noexcept | |||||
| 54 | { | 54 | { | |||||
| HITCBC | 55 | 28 | if (::setsockopt( | 55 | 28 | if (::setsockopt( | ||
| 56 | fd, level, optname, data, | 56 | fd, level, optname, data, | |||||
| HITCBC | 57 | 28 | static_cast<socklen_t>(size)) != 0) | 57 | 28 | static_cast<socklen_t>(size)) != 0) | ||
| MISUBC | 58 | ✗ | return make_err(errno); | 58 | ✗ | return make_err(errno); | ||
| HITCBC | 59 | 28 | return {}; | 59 | 28 | return {}; | ||
| 60 | } | 60 | } | |||||
| HITCBC | 61 | 23625 | static void pre_shutdown(int) noexcept {} | 61 | 14562 | static void pre_shutdown(int) noexcept {} | ||
| HITCBC | 62 | 7849 | static void pre_destroy(int) noexcept {} | 62 | 4828 | static void pre_destroy(int) noexcept {} | ||
| 63 | }; | 63 | }; | |||||
| 64 | 64 | |||||||
| 65 | struct write_policy | 65 | struct write_policy | |||||
| 66 | { | 66 | { | |||||
| MISLBC | 67 | 105542 | static ssize_t write(int fd, iovec* iovecs, int count) noexcept | 67 | ✗ | static ssize_t write(int fd, iovec* iovecs, int count) noexcept | ||
| 68 | { | 68 | { | |||||
| MISLBC | 69 | 105542 | msghdr msg{}; | 69 | ✗ | msghdr msg{}; | ||
| MISLBC | 70 | 105542 | msg.msg_iov = iovecs; | 70 | ✗ | msg.msg_iov = iovecs; | ||
| MISLBC | 71 | 105542 | msg.msg_iovlen = static_cast<std::size_t>(count); | 71 | ✗ | msg.msg_iovlen = static_cast<std::size_t>(count); | ||
| 72 | 72 | |||||||
| 73 | #ifdef MSG_NOSIGNAL | 73 | #ifdef MSG_NOSIGNAL | |||||
| MISLBC | 74 | 105542 | constexpr int send_flags = MSG_NOSIGNAL; | 74 | ✗ | constexpr int send_flags = MSG_NOSIGNAL; | ||
| 75 | #else | 75 | #else | |||||
| 76 | constexpr int send_flags = 0; | 76 | constexpr int send_flags = 0; | |||||
| 77 | #endif | 77 | #endif | |||||
| 78 | 78 | |||||||
| 79 | ssize_t n; | 79 | ssize_t n; | |||||
| 80 | do | 80 | do | |||||
| 81 | { | 81 | { | |||||
| MISLBC | 82 | 105542 | n = ::sendmsg(fd, &msg, send_flags); | 82 | ✗ | n = ::sendmsg(fd, &msg, send_flags); | ||
| 83 | } | 83 | } | |||||
| MISLBC | 84 | 105542 | while (n < 0 && errno == EINTR); | 84 | ✗ | while (n < 0 && errno == EINTR); | ||
| MISLBC | 85 | 105542 | return n; | 85 | ✗ | return n; | ||
| 86 | } | 86 | } | |||||
| 87 | + | |||||||
| 88 | + | // Single-buffer fast path. Where MSG_NOSIGNAL exists we use | ||||||
| 89 | + | // send() to suppress SIGPIPE inline; otherwise fall back to | ||||||
| 90 | + | // write() and rely on the SO_NOSIGPIPE set in accept_policy | ||||||
| 91 | + | // and set_fd_options. | ||||||
| HITGNC | 92 | + | 127774 | static ssize_t write_one( | ||||
| 93 | + | int fd, void const* data, std::size_t size) noexcept | ||||||
| 94 | + | { | ||||||
| 95 | + | ssize_t n; | ||||||
| 96 | + | do | ||||||
| 97 | + | { | ||||||
| 98 | + | #ifdef MSG_NOSIGNAL | ||||||
| HITGNC | 99 | + | 127774 | n = ::send(fd, data, size, MSG_NOSIGNAL); | ||||
| 100 | + | #else | ||||||
| 101 | + | n = ::write(fd, data, size); | ||||||
| 102 | + | #endif | ||||||
| 103 | + | } | ||||||
| HITGNC | 104 | + | 127774 | while (n < 0 && errno == EINTR); | ||||
| HITGNC | 105 | + | 127774 | return n; | ||||
| 106 | + | } | ||||||
| 87 | }; | 107 | }; | |||||
| 88 | 108 | |||||||
| 89 | struct accept_policy | 109 | struct accept_policy | |||||
| 90 | { | 110 | { | |||||
| HITCBC | 91 | 5212 | static int do_accept( | 111 | 3198 | static int do_accept( | ||
| 92 | int fd, sockaddr_storage& peer, socklen_t& addrlen) noexcept | 112 | int fd, sockaddr_storage& peer, socklen_t& addrlen) noexcept | |||||
| 93 | { | 113 | { | |||||
| HITCBC | 94 | 5212 | addrlen = sizeof(peer); | 114 | 3198 | addrlen = sizeof(peer); | ||
| 95 | int new_fd; | 115 | int new_fd; | |||||
| 96 | do | 116 | do | |||||
| 97 | { | 117 | { | |||||
| HITCBC | 98 | 5212 | new_fd = ::accept( | 118 | 3198 | new_fd = ::accept( | ||
| 99 | fd, reinterpret_cast<sockaddr*>(&peer), &addrlen); | 119 | fd, reinterpret_cast<sockaddr*>(&peer), &addrlen); | |||||
| 100 | } | 120 | } | |||||
| HITCBC | 101 | 5212 | while (new_fd < 0 && errno == EINTR); | 121 | 3198 | while (new_fd < 0 && errno == EINTR); | ||
| 102 | 122 | |||||||
| HITCBC | 103 | 5212 | if (new_fd < 0) | 123 | 3198 | if (new_fd < 0) | ||
| HITCBC | 104 | 2606 | return new_fd; | 124 | 1599 | return new_fd; | ||
| 105 | 125 | |||||||
| HITCBC | 106 | 2606 | if (new_fd >= FD_SETSIZE) | 126 | 1599 | if (new_fd >= FD_SETSIZE) | ||
| 107 | { | 127 | { | |||||
| MISUBC | 108 | ✗ | ::close(new_fd); | 128 | ✗ | ::close(new_fd); | ||
| MISUBC | 109 | ✗ | errno = EINVAL; | 129 | ✗ | errno = EINVAL; | ||
| MISUBC | 110 | ✗ | return -1; | 130 | ✗ | return -1; | ||
| 111 | } | 131 | } | |||||
| 112 | 132 | |||||||
| HITCBC | 113 | 2606 | int flags = ::fcntl(new_fd, F_GETFL, 0); | 133 | 1599 | int flags = ::fcntl(new_fd, F_GETFL, 0); | ||
| HITCBC | 114 | 2606 | if (flags == -1) | 134 | 1599 | if (flags == -1) | ||
| 115 | { | 135 | { | |||||
| MISUBC | 116 | ✗ | int err = errno; | 136 | ✗ | int err = errno; | ||
| MISUBC | 117 | ✗ | ::close(new_fd); | 137 | ✗ | ::close(new_fd); | ||
| MISUBC | 118 | ✗ | errno = err; | 138 | ✗ | errno = err; | ||
| MISUBC | 119 | ✗ | return -1; | 139 | ✗ | return -1; | ||
| 120 | } | 140 | } | |||||
| 121 | 141 | |||||||
| HITCBC | 122 | 2606 | if (::fcntl(new_fd, F_SETFL, flags | O_NONBLOCK) == -1) | 142 | 1599 | if (::fcntl(new_fd, F_SETFL, flags | O_NONBLOCK) == -1) | ||
| 123 | { | 143 | { | |||||
| MISUBC | 124 | ✗ | int err = errno; | 144 | ✗ | int err = errno; | ||
| MISUBC | 125 | ✗ | ::close(new_fd); | 145 | ✗ | ::close(new_fd); | ||
| MISUBC | 126 | ✗ | errno = err; | 146 | ✗ | errno = err; | ||
| MISUBC | 127 | ✗ | return -1; | 147 | ✗ | return -1; | ||
| 128 | } | 148 | } | |||||
| 129 | 149 | |||||||
| HITCBC | 130 | 2606 | if (::fcntl(new_fd, F_SETFD, FD_CLOEXEC) == -1) | 150 | 1599 | if (::fcntl(new_fd, F_SETFD, FD_CLOEXEC) == -1) | ||
| 131 | { | 151 | { | |||||
| MISUBC | 132 | ✗ | int err = errno; | 152 | ✗ | int err = errno; | ||
| MISUBC | 133 | ✗ | ::close(new_fd); | 153 | ✗ | ::close(new_fd); | ||
| MISUBC | 134 | ✗ | errno = err; | 154 | ✗ | errno = err; | ||
| MISUBC | 135 | ✗ | return -1; | 155 | ✗ | return -1; | ||
| 136 | } | 156 | } | |||||
| 137 | 157 | |||||||
| 138 | #ifdef SO_NOSIGPIPE | 158 | #ifdef SO_NOSIGPIPE | |||||
| 139 | // Best-effort: SO_NOSIGPIPE is a safety net; write paths | 159 | // Best-effort: SO_NOSIGPIPE is a safety net; write paths | |||||
| 140 | // also use MSG_NOSIGNAL where available. Failure here | 160 | // also use MSG_NOSIGNAL where available. Failure here | |||||
| 141 | // should not prevent the accepted connection from being used. | 161 | // should not prevent the accepted connection from being used. | |||||
| 142 | int one = 1; | 162 | int one = 1; | |||||
| 143 | (void)::setsockopt( | 163 | (void)::setsockopt( | |||||
| 144 | new_fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); | 164 | new_fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); | |||||
| 145 | #endif | 165 | #endif | |||||
| 146 | 166 | |||||||
| HITCBC | 147 | 2606 | return new_fd; | 167 | 1599 | return new_fd; | ||
| 148 | } | 168 | } | |||||
| 149 | }; | 169 | }; | |||||
| 150 | 170 | |||||||
| 151 | // Create a plain socket (no atomic flags -- select is POSIX-portable). | 171 | // Create a plain socket (no atomic flags -- select is POSIX-portable). | |||||
| HITCBC | 152 | 2766 | static int create_socket(int family, int type, int protocol) noexcept | 172 | 1759 | static int create_socket(int family, int type, int protocol) noexcept | ||
| 153 | { | 173 | { | |||||
| HITCBC | 154 | 2766 | return ::socket(family, type, protocol); | 174 | 1759 | return ::socket(family, type, protocol); | ||
| 155 | } | 175 | } | |||||
| 156 | 176 | |||||||
| 157 | // Set O_NONBLOCK, FD_CLOEXEC; check FD_SETSIZE; optionally SO_NOSIGPIPE. | 177 | // Set O_NONBLOCK, FD_CLOEXEC; check FD_SETSIZE; optionally SO_NOSIGPIPE. | |||||
| 158 | // Caller is responsible for closing fd on error. | 178 | // Caller is responsible for closing fd on error. | |||||
| HITCBC | 159 | 2766 | static std::error_code set_fd_options(int fd) noexcept | 179 | 1759 | static std::error_code set_fd_options(int fd) noexcept | ||
| 160 | { | 180 | { | |||||
| HITCBC | 161 | 2766 | int flags = ::fcntl(fd, F_GETFL, 0); | 181 | 1759 | int flags = ::fcntl(fd, F_GETFL, 0); | ||
| HITCBC | 162 | 2766 | if (flags == -1) | 182 | 1759 | if (flags == -1) | ||
| MISUBC | 163 | ✗ | return make_err(errno); | 183 | ✗ | return make_err(errno); | ||
| HITCBC | 164 | 2766 | if (::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) | 184 | 1759 | if (::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) | ||
| MISUBC | 165 | ✗ | return make_err(errno); | 185 | ✗ | return make_err(errno); | ||
| HITCBC | 166 | 2766 | if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) | 186 | 1759 | if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) | ||
| MISUBC | 167 | ✗ | return make_err(errno); | 187 | ✗ | return make_err(errno); | ||
| 168 | 188 | |||||||
| HITCBC | 169 | 2766 | if (fd >= FD_SETSIZE) | 189 | 1759 | if (fd >= FD_SETSIZE) | ||
| MISUBC | 170 | ✗ | return make_err(EMFILE); | 190 | ✗ | return make_err(EMFILE); | ||
| 171 | 191 | |||||||
| 172 | #ifdef SO_NOSIGPIPE | 192 | #ifdef SO_NOSIGPIPE | |||||
| 173 | // Best-effort: SO_NOSIGPIPE is a safety net; write paths | 193 | // Best-effort: SO_NOSIGPIPE is a safety net; write paths | |||||
| 174 | // also use MSG_NOSIGNAL where available. Match develop's | 194 | // also use MSG_NOSIGNAL where available. Match develop's | |||||
| 175 | // predominant behavior of ignoring failures here rather | 195 | // predominant behavior of ignoring failures here rather | |||||
| 176 | // than failing socket creation. | 196 | // than failing socket creation. | |||||
| 177 | { | 197 | { | |||||
| 178 | int one = 1; | 198 | int one = 1; | |||||
| 179 | (void)::setsockopt( | 199 | (void)::setsockopt( | |||||
| 180 | fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); | 200 | fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); | |||||
| 181 | } | 201 | } | |||||
| 182 | #endif | 202 | #endif | |||||
| 183 | 203 | |||||||
| HITCBC | 184 | 2766 | return {}; | 204 | 1759 | return {}; | ||
| 185 | } | 205 | } | |||||
| 186 | 206 | |||||||
| 187 | // Apply protocol-specific options after socket creation. | 207 | // Apply protocol-specific options after socket creation. | |||||
| 188 | // For IP sockets, sets IPV6_V6ONLY on AF_INET6 (best-effort). | 208 | // For IP sockets, sets IPV6_V6ONLY on AF_INET6 (best-effort). | |||||
| 189 | static std::error_code | 209 | static std::error_code | |||||
| HITCBC | 190 | 2672 | configure_ip_socket(int fd, int family) noexcept | 210 | 1665 | configure_ip_socket(int fd, int family) noexcept | ||
| 191 | { | 211 | { | |||||
| HITCBC | 192 | 2672 | if (family == AF_INET6) | 212 | 1665 | if (family == AF_INET6) | ||
| 193 | { | 213 | { | |||||
| HITCBC | 194 | 14 | int one = 1; | 214 | 14 | int one = 1; | ||
| HITCBC | 195 | 14 | (void)::setsockopt( | 215 | 14 | (void)::setsockopt( | ||
| 196 | fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); | 216 | fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); | |||||
| 197 | } | 217 | } | |||||
| 198 | 218 | |||||||
| HITCBC | 199 | 2672 | return set_fd_options(fd); | 219 | 1665 | return set_fd_options(fd); | ||
| 200 | } | 220 | } | |||||
| 201 | 221 | |||||||
| 202 | // Apply protocol-specific options for acceptor sockets. | 222 | // Apply protocol-specific options for acceptor sockets. | |||||
| 203 | // For IP acceptors, sets IPV6_V6ONLY=0 (dual-stack, best-effort). | 223 | // For IP acceptors, sets IPV6_V6ONLY=0 (dual-stack, best-effort). | |||||
| 204 | static std::error_code | 224 | static std::error_code | |||||
| HITCBC | 205 | 74 | configure_ip_acceptor(int fd, int family) noexcept | 225 | 74 | configure_ip_acceptor(int fd, int family) noexcept | ||
| 206 | { | 226 | { | |||||
| HITCBC | 207 | 74 | if (family == AF_INET6) | 227 | 74 | if (family == AF_INET6) | ||
| 208 | { | 228 | { | |||||
| HITCBC | 209 | 9 | int val = 0; | 229 | 9 | int val = 0; | ||
| HITCBC | 210 | 9 | (void)::setsockopt( | 230 | 9 | (void)::setsockopt( | ||
| 211 | fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val)); | 231 | fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val)); | |||||
| 212 | } | 232 | } | |||||
| 213 | 233 | |||||||
| HITCBC | 214 | 74 | return set_fd_options(fd); | 234 | 74 | return set_fd_options(fd); | ||
| 215 | } | 235 | } | |||||
| 216 | 236 | |||||||
| 217 | // Apply options for local (unix) sockets. | 237 | // Apply options for local (unix) sockets. | |||||
| 218 | static std::error_code | 238 | static std::error_code | |||||
| HITCBC | 219 | 20 | configure_local_socket(int fd) noexcept | 239 | 20 | configure_local_socket(int fd) noexcept | ||
| 220 | { | 240 | { | |||||
| HITCBC | 221 | 20 | return set_fd_options(fd); | 241 | 20 | return set_fd_options(fd); | ||
| 222 | } | 242 | } | |||||
| 223 | 243 | |||||||
| 224 | // Non-mutating validation for fds adopted via assign(). Select's | 244 | // Non-mutating validation for fds adopted via assign(). Select's | |||||
| 225 | // reactor cannot handle fds above FD_SETSIZE, so reject them up | 245 | // reactor cannot handle fds above FD_SETSIZE, so reject them up | |||||
| 226 | // front instead of letting FD_SET clobber unrelated memory. | 246 | // front instead of letting FD_SET clobber unrelated memory. | |||||
| 227 | static std::error_code | 247 | static std::error_code | |||||
| HITCBC | 228 | 14 | validate_assigned_fd(int fd) noexcept | 248 | 14 | validate_assigned_fd(int fd) noexcept | ||
| 229 | { | 249 | { | |||||
| HITCBC | 230 | 14 | if (fd >= FD_SETSIZE) | 250 | 14 | if (fd >= FD_SETSIZE) | ||
| MISUBC | 231 | ✗ | return make_err(EMFILE); | 251 | ✗ | return make_err(EMFILE); | ||
| HITCBC | 232 | 14 | return {}; | 252 | 14 | return {}; | ||
| 233 | } | 253 | } | |||||
| 234 | }; | 254 | }; | |||||
| 235 | 255 | |||||||
| 236 | } // namespace boost::corosio::detail | 256 | } // namespace boost::corosio::detail | |||||
| 237 | 257 | |||||||
| 238 | #endif // BOOST_COROSIO_HAS_SELECT | 258 | #endif // BOOST_COROSIO_HAS_SELECT | |||||
| 239 | 259 | |||||||
| 240 | #endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP | 260 | #endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP | |||||