1
0
mirror of synced 2025-04-19 00:24:02 +03:00

Merge branch 'thread-safe-cloexec' of github.com:kdombroski/cpp-httplib into kdombroski-thread-safe-cloexec

This commit is contained in:
yhirose 2024-08-06 17:22:43 -04:00
commit ff038f98b7

View File

@ -3251,7 +3251,12 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
const auto addrlen = host.length(); const auto addrlen = host.length();
if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; }
#ifdef SOCK_CLOEXEC
auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, hints.ai_protocol);
#else
auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
#endif
if (sock != INVALID_SOCKET) { if (sock != INVALID_SOCKET) {
sockaddr_un addr{}; sockaddr_un addr{};
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
@ -3261,7 +3266,10 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
hints.ai_addrlen = static_cast<socklen_t>( hints.ai_addrlen = static_cast<socklen_t>(
sizeof(addr) - sizeof(addr.sun_path) + addrlen); sizeof(addr) - sizeof(addr.sun_path) + addrlen);
#ifndef SOCK_CLOEXEC
fcntl(sock, F_SETFD, FD_CLOEXEC); fcntl(sock, F_SETFD, FD_CLOEXEC);
#endif
if (socket_options) { socket_options(sock); } if (socket_options) { socket_options(sock); }
if (!bind_or_connect(sock, hints)) { if (!bind_or_connect(sock, hints)) {
@ -3306,11 +3314,17 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
} }
#else #else
#ifdef SOCK_CLOEXEC
auto sock = socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol);
#else
auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
#endif
#endif #endif
if (sock == INVALID_SOCKET) { continue; } if (sock == INVALID_SOCKET) { continue; }
#ifndef _WIN32 #if !defined _WIN32 && !defined SOCK_CLOEXEC
if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) {
close_socket(sock); close_socket(sock);
continue; continue;
@ -6505,7 +6519,15 @@ inline bool Server::listen_internal() {
#ifndef _WIN32 #ifndef _WIN32
} }
#endif #endif
#if defined _WIN32
// sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, OVERLAPPED
socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0);
#elif defined __linux__
socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC);
#else
socket_t sock = accept(svr_sock_, nullptr, nullptr); socket_t sock = accept(svr_sock_, nullptr, nullptr);
#endif
if (sock == INVALID_SOCKET) { if (sock == INVALID_SOCKET) {
if (errno == EMFILE) { if (errno == EMFILE) {