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