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

100.0% Lines (100/100) 100.0% List of functions (12/12) 66.4% Branches (73/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_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 113x 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 113 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 110 times.
113x if (::setsockopt(
66 113x fd, level, optname, data, static_cast<socklen_t>(size)) !=
67 0)
68
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3x return make_err(errno);
69 110x return {};
70 113x }
71 15476x static void pre_shutdown(int) noexcept {}
72 5024x static void pre_destroy(int) noexcept {}
73 };
74
75 struct write_policy
76 {
77 501x static ssize_t write(int fd, iovec* iovecs, int count) noexcept
78 {
79 501x msghdr msg{};
80 501x msg.msg_iov = iovecs;
81 501x msg.msg_iovlen = static_cast<std::size_t>(count);
82
83 #ifdef MSG_NOSIGNAL
84 501x constexpr int send_flags = MSG_NOSIGNAL;
85 #else
86 constexpr int send_flags = 0;
87 #endif
88
89 ssize_t n;
90 501x do
91 {
92
1/2
✓ Branch 0 taken 502 times.
✗ Branch 1 not taken.
502x n = ::sendmsg(fd, &msg, send_flags);
93 1004x }
94
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 499 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
502x while (n < 0 && errno == EINTR);
95 501x 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 452769x write_one(int fd, void const* data, std::size_t size) noexcept
104 {
105 ssize_t n;
106 452769x do
107 {
108 #ifdef MSG_NOSIGNAL
109
1/2
✓ Branch 0 taken 452770 times.
✗ Branch 1 not taken.
452770x n = ::send(fd, data, size, MSG_NOSIGNAL);
110 #else
111 n = ::write(fd, data, size);
112 #endif
113 905540x }
114
3/4
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 452266 times.
✓ Branch 2 taken 504 times.
✗ Branch 3 not taken.
452770x while (n < 0 && errno == EINTR);
115 452769x return n;
116 }
117 };
118
119 using descriptor_write_policy = detail::descriptor_write_policy;
120
121 struct accept_policy
122 {
123 static int
124 3293x do_accept(int fd, sockaddr_storage& peer, socklen_t& addrlen) noexcept
125 {
126 3293x addrlen = sizeof(peer);
127 int new_fd;
128 3293x do
129 {
130 3294x new_fd =
131
1/2
✓ Branch 0 taken 3294 times.
✗ Branch 1 not taken.
3294x ::accept(fd, reinterpret_cast<sockaddr*>(&peer), &addrlen);
132 6588x }
133
3/4
✓ Branch 0 taken 1653 times.
✓ Branch 1 taken 1641 times.
✓ Branch 2 taken 1653 times.
✗ Branch 3 not taken.
3294x while (new_fd < 0 && errno == EINTR);
134
135
2/2
✓ Branch 0 taken 1652 times.
✓ Branch 1 taken 1641 times.
3293x if (new_fd < 0)
136 1652x return new_fd;
137
138
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1640 times.
1641x 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 1640 times.
✗ Branch 1 not taken.
1640x int flags = ::fcntl(new_fd, F_GETFL, 0);
146
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1639 times.
1640x 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 1639 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1638 times.
1639x 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 1638 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1637 times.
1638x 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 1637x int one = 1;
177
3/4
✓ Branch 0 taken 1637 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1636 times.
1637x if (::setsockopt(
178 1637x new_fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)) != 0)
179 {
180
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x int err = errno;
181
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x ::close(new_fd);
182
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x errno = err;
183 1x return -1;
184 }
185 #endif
186
187 1636x return new_fd;
188 3293x }
189 };
190
191 // Create a plain socket (no atomic flags -- select is POSIX-portable).
192 2183x static int create_socket(int family, int type, int protocol) noexcept
193 {
194
1/2
✓ Branch 0 taken 2183 times.
✗ Branch 1 not taken.
2183x 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 2179x static std::error_code set_fd_options(int fd) noexcept
200 {
201
1/2
✓ Branch 0 taken 2179 times.
✗ Branch 1 not taken.
2179x int flags = ::fcntl(fd, F_GETFL, 0);
202
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2177 times.
2179x 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 2177 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2175 times.
2177x 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 2175 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2173 times.
2175x 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 2171 times.
✓ Branch 1 taken 2 times.
2173x 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 2171x int one = 1;
220
3/4
✓ Branch 0 taken 2171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2169 times.
2171x if (::setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)) !=
221 0)
222
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2x return make_err(errno);
223 }
224 #endif
225
226 2169x return {};
227 2179x }
228
229 // Apply protocol-specific options after socket creation.
230 // For IP sockets, sets IPV6_V6ONLY on AF_INET6 (best-effort).
231 1823x static std::error_code configure_ip_socket(int fd, int family) noexcept
232 {
233
2/2
✓ Branch 0 taken 1800 times.
✓ Branch 1 taken 23 times.
1823x 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 1823x 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 250x static std::error_code configure_ip_acceptor(int fd, int family) noexcept
246 {
247
2/2
✓ Branch 0 taken 239 times.
✓ Branch 1 taken 11 times.
250x 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 250x return set_fd_options(fd);
255 }
256
257 // Apply options for local (unix) sockets.
258 106x static std::error_code configure_local_socket(int fd) noexcept
259 {
260 106x 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 144x static std::error_code validate_assigned_fd(int fd) noexcept
267 {
268
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 142 times.
144x if (fd >= FD_SETSIZE)
269 2x return make_err(EMFILE);
270 142x return {};
271 144x }
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