include/boost/corosio/native/detail/select/select_traits.hpp

75.7% Lines (78/103) 100.0% List of functions (12/12) 38.2% Branches (42/110)
select_traits.hpp
f(x) Functions (12)
Line Branch 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 76x std::error_code on_set_option(
54 int fd, int level, int optname,
55 void const* data, std::size_t size) noexcept
56 {
57
3/4
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 74 times.
76x if (::setsockopt(
58 76x fd, level, optname, data,
59 76x static_cast<socklen_t>(size)) != 0)
60
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2x return make_err(errno);
61 74x return {};
62 76x }
63 15822x static void pre_shutdown(int) noexcept {}
64 5154x static void pre_destroy(int) noexcept {}
65 };
66
67 struct write_policy
68 {
69 510x static ssize_t write(int fd, iovec* iovecs, int count) noexcept
70 {
71 510x msghdr msg{};
72 510x msg.msg_iov = iovecs;
73 510x msg.msg_iovlen = static_cast<std::size_t>(count);
74
75 #ifdef MSG_NOSIGNAL
76 510x constexpr int send_flags = MSG_NOSIGNAL;
77 #else
78 constexpr int send_flags = 0;
79 #endif
80
81 ssize_t n;
82 510x do
83 {
84
1/2
✓ Branch 0 taken 510 times.
✗ Branch 1 not taken.
510x n = ::sendmsg(fd, &msg, send_flags);
85 1020x }
86
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 510 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
510x while (n < 0 && errno == EINTR);
87 510x 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 395680x static ssize_t write_one(
95 int fd, void const* data, std::size_t size) noexcept
96 {
97 ssize_t n;
98 395680x do
99 {
100 #ifdef MSG_NOSIGNAL
101
1/2
✓ Branch 0 taken 395680 times.
✗ Branch 1 not taken.
395680x n = ::send(fd, data, size, MSG_NOSIGNAL);
102 #else
103 n = ::write(fd, data, size);
104 #endif
105 791360x }
106
3/4
✓ Branch 0 taken 510 times.
✓ Branch 1 taken 395170 times.
✓ Branch 2 taken 510 times.
✗ Branch 3 not taken.
395680x while (n < 0 && errno == EINTR);
107 395680x return n;
108 }
109 };
110
111 struct accept_policy
112 {
113 3399x static int do_accept(
114 int fd, sockaddr_storage& peer, socklen_t& addrlen) noexcept
115 {
116 3399x addrlen = sizeof(peer);
117 int new_fd;
118 3399x do
119 {
120
1/2
✓ Branch 0 taken 3399 times.
✗ Branch 1 not taken.
3399x new_fd = ::accept(
121 3399x fd, reinterpret_cast<sockaddr*>(&peer), &addrlen);
122 6798x }
123
3/4
✓ Branch 0 taken 1705 times.
✓ Branch 1 taken 1694 times.
✓ Branch 2 taken 1705 times.
✗ Branch 3 not taken.
3399x while (new_fd < 0 && errno == EINTR);
124
125
2/2
✓ Branch 0 taken 1705 times.
✓ Branch 1 taken 1694 times.
3399x if (new_fd < 0)
126 1705x return new_fd;
127
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1694 times.
1694x if (new_fd >= FD_SETSIZE)
129 {
130 ::close(new_fd);
131 errno = EINVAL;
132 return -1;
133 }
134
135
1/2
✓ Branch 0 taken 1694 times.
✗ Branch 1 not taken.
1694x int flags = ::fcntl(new_fd, F_GETFL, 0);
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1694 times.
1694x if (flags == -1)
137 {
138 int err = errno;
139 ::close(new_fd);
140 errno = err;
141 return -1;
142 }
143
144
2/4
✓ Branch 0 taken 1694 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1694 times.
1694x if (::fcntl(new_fd, F_SETFL, flags | O_NONBLOCK) == -1)
145 {
146 int err = errno;
147 ::close(new_fd);
148 errno = err;
149 return -1;
150 }
151
152
2/4
✓ Branch 0 taken 1694 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1694 times.
1694x if (::fcntl(new_fd, F_SETFD, FD_CLOEXEC) == -1)
153 {
154 int err = errno;
155 ::close(new_fd);
156 errno = err;
157 return -1;
158 }
159
160 #ifdef SO_NOSIGPIPE
161 // SO_NOSIGPIPE is the only SIGPIPE guard on platforms that
162 // lack MSG_NOSIGNAL (macOS/BSD). Treat failure as fatal,
163 // matching the kqueue backend and Boost.Asio.
164 1694x int one = 1;
165
2/4
✓ Branch 0 taken 1694 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1694 times.
1694x if (::setsockopt(
166 1694x new_fd, SOL_SOCKET, SO_NOSIGPIPE,
167 1694x &one, sizeof(one)) != 0)
168 {
169 int err = errno;
170 ::close(new_fd);
171 errno = err;
172 return -1;
173 }
174 #endif
175
176 1694x return new_fd;
177 3399x }
178 };
179
180 // Create a plain socket (no atomic flags -- select is POSIX-portable).
181 2089x static int create_socket(int family, int type, int protocol) noexcept
182 {
183
1/2
✓ Branch 0 taken 2089 times.
✗ Branch 1 not taken.
2089x return ::socket(family, type, protocol);
184 }
185
186 // Set O_NONBLOCK, FD_CLOEXEC; check FD_SETSIZE; optionally SO_NOSIGPIPE.
187 // Caller is responsible for closing fd on error.
188 2089x static std::error_code set_fd_options(int fd) noexcept
189 {
190
1/2
✓ Branch 0 taken 2089 times.
✗ Branch 1 not taken.
2089x int flags = ::fcntl(fd, F_GETFL, 0);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2089 times.
2089x if (flags == -1)
192 return make_err(errno);
193
2/4
✓ Branch 0 taken 2089 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2089 times.
2089x if (::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
194 return make_err(errno);
195
2/4
✓ Branch 0 taken 2089 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2089 times.
2089x if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
196 return make_err(errno);
197
198
1/2
✓ Branch 0 taken 2089 times.
✗ Branch 1 not taken.
2089x if (fd >= FD_SETSIZE)
199 return make_err(EMFILE);
200
201 #ifdef SO_NOSIGPIPE
202 // SO_NOSIGPIPE is the only SIGPIPE guard on platforms that lack
203 // MSG_NOSIGNAL (macOS/BSD). Treat failure as fatal, matching the
204 // kqueue backend and Boost.Asio. Caller closes fd on error.
205 {
206 2089x int one = 1;
207
2/4
✓ Branch 0 taken 2089 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2089 times.
2089x if (::setsockopt(
208 2089x fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)) != 0)
209 return make_err(errno);
210 }
211 #endif
212
213 2089x return {};
214 2089x }
215
216 // Apply protocol-specific options after socket creation.
217 // For IP sockets, sets IPV6_V6ONLY on AF_INET6 (best-effort).
218 static std::error_code
219 1827x configure_ip_socket(int fd, int family) noexcept
220 {
221
2/2
✓ Branch 0 taken 1806 times.
✓ Branch 1 taken 21 times.
1827x if (family == AF_INET6)
222 {
223 21x int one = 1;
224
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21x std::ignore = ::setsockopt(
225 21x fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
226 21x }
227
228 1827x return set_fd_options(fd);
229 }
230
231 // Apply protocol-specific options for acceptor sockets.
232 // For IP acceptors, sets IPV6_V6ONLY=0 (dual-stack, best-effort).
233 static std::error_code
234 173x configure_ip_acceptor(int fd, int family) noexcept
235 {
236
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 11 times.
173x if (family == AF_INET6)
237 {
238 11x int val = 0;
239
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11x std::ignore = ::setsockopt(
240 11x fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
241 11x }
242
243 173x return set_fd_options(fd);
244 }
245
246 // Apply options for local (unix) sockets.
247 static std::error_code
248 89x configure_local_socket(int fd) noexcept
249 {
250 89x return set_fd_options(fd);
251 }
252
253 // Non-mutating validation for fds adopted via assign(). Select's
254 // reactor cannot handle fds above FD_SETSIZE, so reject them up
255 // front instead of letting FD_SET clobber unrelated memory.
256 static std::error_code
257 114x validate_assigned_fd(int fd) noexcept
258 {
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
114x if (fd >= FD_SETSIZE)
260 return make_err(EMFILE);
261 114x return {};
262 114x }
263 };
264
265 } // namespace boost::corosio::detail
266
267 #endif // BOOST_COROSIO_HAS_SELECT
268
269 #endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_TRAITS_HPP
270