@@ -3251,7 +3251,12 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
3251
3251
const auto addrlen = host.length ();
3252
3252
if (addrlen > sizeof (sockaddr_un::sun_path)) { return INVALID_SOCKET; }
3253
3253
3254
+ #ifdef SOCK_CLOEXEC
3255
+ auto sock = socket (hints.ai_family , hints.ai_socktype | SOCK_CLOEXEC, hints.ai_protocol );
3256
+ #else
3254
3257
auto sock = socket (hints.ai_family , hints.ai_socktype , hints.ai_protocol );
3258
+ #endif
3259
+
3255
3260
if (sock != INVALID_SOCKET) {
3256
3261
sockaddr_un addr{};
3257
3262
addr.sun_family = AF_UNIX;
@@ -3261,7 +3266,10 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
3261
3266
hints.ai_addrlen = static_cast <socklen_t >(
3262
3267
sizeof (addr) - sizeof (addr.sun_path ) + addrlen);
3263
3268
3269
+ #ifndef SOCK_CLOEXEC
3264
3270
fcntl (sock, F_SETFD, FD_CLOEXEC);
3271
+ #endif
3272
+
3265
3273
if (socket_options) { socket_options (sock); }
3266
3274
3267
3275
if (!bind_or_connect (sock, hints)) {
@@ -3306,11 +3314,17 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
3306
3314
sock = socket (rp->ai_family , rp->ai_socktype , rp->ai_protocol );
3307
3315
}
3308
3316
#else
3317
+
3318
+ #ifdef SOCK_CLOEXEC
3319
+ auto sock = socket (rp->ai_family , rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol );
3320
+ #else
3309
3321
auto sock = socket (rp->ai_family , rp->ai_socktype , rp->ai_protocol );
3322
+ #endif
3323
+
3310
3324
#endif
3311
3325
if (sock == INVALID_SOCKET) { continue ; }
3312
3326
3313
- #ifndef _WIN32
3327
+ #if !defined _WIN32 && !defined SOCK_CLOEXEC
3314
3328
if (fcntl (sock, F_SETFD, FD_CLOEXEC) == -1 ) {
3315
3329
close_socket (sock);
3316
3330
continue ;
@@ -6505,7 +6519,15 @@ inline bool Server::listen_internal() {
6505
6519
#ifndef _WIN32
6506
6520
}
6507
6521
#endif
6522
+
6523
+ #if defined _WIN32
6524
+ // sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, OVERLAPPED
6525
+ socket_t sock = WSAAccept (svr_sock_, nullptr , nullptr , nullptr , 0 );
6526
+ #elif defined __linux__
6527
+ socket_t sock = accept4 (svr_sock_, nullptr , nullptr , SOCK_CLOEXEC);
6528
+ #else
6508
6529
socket_t sock = accept (svr_sock_, nullptr , nullptr );
6530
+ #endif
6509
6531
6510
6532
if (sock == INVALID_SOCKET) {
6511
6533
if (errno == EMFILE) {
0 commit comments