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

100.0% Lines (100 / 100) 66.4% Branches (73 / 110) 100.0% Functions (12 / 12)
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_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 1681x 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
3/4
✓ Branch 0 taken 1681 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 1676 times.
1681x if (::setsockopt(
66 1681x fd, level, optname, data, static_cast<socklen_t>(size)) !=
67 0)
68
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5x return make_err(errno);
69 1676x return {};
70 1681x }
71 35782x static void pre_shutdown(int) noexcept {}
72 11327x static void pre_destroy(int) noexcept {}
73 };
74
75 struct write_policy
76 {
77 517x static ssize_t write(int fd, iovec* iovecs, int count) noexcept
78 {
79 517x msghdr msg{};
80 517x msg.msg_iov = iovecs;
81 517x msg.msg_iovlen = static_cast<std::size_t>(count);
82
83 #ifdef MSG_NOSIGNAL
84 517x constexpr int send_flags = MSG_NOSIGNAL;
85 #else
86 constexpr int send_flags = 0;
87 #endif
88
89 ssize_t n;
90 517x do
91 {
92
1/2
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
518x n = ::sendmsg(fd, &msg, send_flags);
93 1036x }
94
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 515 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
518x while (n < 0 && errno == EINTR);
95 517x 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 285476x write_one(int fd, void const* data, std::size_t size) noexcept
104 {
105 ssize_t n;
106 285476x do
107 {
108 #ifdef MSG_NOSIGNAL
109
1/2
✓ Branch 0 taken 285477 times.
✗ Branch 1 not taken.
285477x n = ::send(fd, data, size, MSG_NOSIGNAL);
110 #else
111 n = ::write(fd, data, size);
112 #endif
113 570954x }
114
3/4
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 284959 times.
✓ Branch 2 taken 518 times.
✗ Branch 3 not taken.
285477x while (n < 0 && errno == EINTR);
115 285476x return n;
116 }
117 };
118
119 using descriptor_write_policy = detail::descriptor_write_policy;
120
121 struct accept_policy
122 {
123 static int
124 10017x do_accept(int fd, sockaddr_storage& peer, socklen_t& addrlen) noexcept
125 {
126 10017x addrlen = sizeof(peer);
127 int new_fd;
128 10017x do
129 {
130 10018x new_fd =
131
1/2
✓ Branch 0 taken 10018 times.
✗ Branch 1 not taken.
10018x ::accept(fd, reinterpret_cast<sockaddr*>(&peer), &addrlen);
132 20036x }
133
3/4
✓ Branch 0 taken 3843 times.
✓ Branch 1 taken 6175 times.
✓ Branch 2 taken 3843 times.
✗ Branch 3 not taken.
10018x while (new_fd < 0 && errno == EINTR);
134
135
2/2
✓ Branch 0 taken 3842 times.
✓ Branch 1 taken 6175 times.
10017x if (new_fd < 0)
136 3842x return new_fd;
137
138
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6174 times.
6175x if (new_fd >= FD_SETSIZE)
139 {
140
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x ::close(new_fd);
141
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x errno = EMFILE;
142 1x return -1;
143 }
144
145
1/2
✓ Branch 0 taken 6174 times.
✗ Branch 1 not taken.
6174x int flags = ::fcntl(new_fd, F_GETFL, 0);
146
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6173 times.
6174x if (flags == -1)
147 {
148
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x int err = errno;
149
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x ::close(new_fd);
150
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x errno = err;
151 1x return -1;
152 }
153
154
3/4
✓ Branch 0 taken 6173 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 6172 times.
6173x if (::fcntl(new_fd, F_SETFL, flags | O_NONBLOCK) == -1)
155 {
156
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x int err = errno;
157
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x ::close(new_fd);
158
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x errno = err;
159 1x return -1;
160 }
161
162
3/4
✓ Branch 0 taken 6172 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 6171 times.
6172x if (::fcntl(new_fd, F_SETFD, FD_CLOEXEC) == -1)
163 {
164
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x int err = errno;
165
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x ::close(new_fd);
166
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
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 6171x int one = 1;
177
3/4
✓ Branch 0 taken 6171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1814 times.
✓ Branch 3 taken 4357 times.
6171x if (::setsockopt(
178 6171x new_fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)) != 0)
179 {
180
1/2
✓ Branch 0 taken 1814 times.
✗ Branch 1 not taken.
1814x int err = errno;
181
1/2
✓ Branch 0 taken 1814 times.
✗ Branch 1 not taken.
1814x ::close(new_fd);
182
1/2
✓ Branch 0 taken 1814 times.
✗ Branch 1 not taken.
1814x errno = err;
183 1814x return -1;
184 }
185 #endif
186
187 4357x return new_fd;
188 10017x }
189 };
190
191 // Create a plain socket (no atomic flags -- select is POSIX-portable).
192 3593x static int create_socket(int family, int type, int protocol) noexcept
193 {
194
1/2
✓ Branch 0 taken 3593 times.
✗ Branch 1 not taken.
3593x 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 3587x static std::error_code set_fd_options(int fd) noexcept
200 {
201
1/2
✓ Branch 0 taken 3587 times.
✗ Branch 1 not taken.
3587x int flags = ::fcntl(fd, F_GETFL, 0);
202
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3585 times.
3587x if (flags == -1)
203
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2x return make_err(errno);
204
3/4
✓ Branch 0 taken 3585 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3583 times.
3585x if (::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
205
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2x return make_err(errno);
206
3/4
✓ Branch 0 taken 3583 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3581 times.
3583x if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
207
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2x return make_err(errno);
208
209
2/2
✓ Branch 0 taken 3579 times.
✓ Branch 1 taken 2 times.
3581x 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 3579x int one = 1;
220
3/4
✓ Branch 0 taken 3579 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 3573 times.
3579x if (::setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)) !=
221 0)
222
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6x return make_err(errno);
223 }
224 #endif
225
226 3573x return {};
227 3587x }
228
229 // Apply protocol-specific options after socket creation.
230 // For IP sockets, sets IPV6_V6ONLY on AF_INET6 (best-effort).
231 2409x static std::error_code configure_ip_socket(int fd, int family) noexcept
232 {
233
2/2
✓ Branch 0 taken 2386 times.
✓ Branch 1 taken 23 times.
2409x if (family == AF_INET6)
234 {
235 23x int one = 1;
236 23x std::ignore =
237
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23x ::setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
238 23x }
239
240 2409x 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 1064x static std::error_code configure_ip_acceptor(int fd, int family) noexcept
246 {
247
2/2
✓ Branch 0 taken 1053 times.
✓ Branch 1 taken 11 times.
1064x if (family == AF_INET6)
248 {
249 11x int val = 0;
250 11x std::ignore =
251
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11x ::setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
252 11x }
253
254 1064x return set_fd_options(fd);
255 }
256
257 // Apply options for local (unix) sockets.
258 114x static std::error_code configure_local_socket(int fd) noexcept
259 {
260 114x 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 2656x static std::error_code validate_assigned_fd(int fd) noexcept
267 {
268
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2654 times.
2656x if (fd >= FD_SETSIZE)
269 2x return make_err(EMFILE);
270 2654x return {};
271 2656x }
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