From bbffed61da4bfb83f4a32c1f772f490252572ce6 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 7 Aug 2025 12:27:22 +0100 Subject: [PATCH] fastcgi: using accept4 on connections accept when possible. saving 1/2 fcntl calls in the process and possibly safer than doing so in 1 operation (possible race condition in between accept/fcntl parts). --- configure.ac | 1 + ext/sockets/config.m4 | 2 +- main/fastcgi.c | 7 ++++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 2bd6ae26ce625..8b0159de1ef28 100644 --- a/configure.ac +++ b/configure.ac @@ -525,6 +525,7 @@ dnl Checks for library functions. dnl ---------------------------------------------------------------------------- AC_CHECK_FUNCS(m4_normalize([ + accept4 alphasort asctime_r asprintf diff --git a/ext/sockets/config.m4 b/ext/sockets/config.m4 index bf183f5e6e7d8..f637c32b3b66d 100644 --- a/ext/sockets/config.m4 +++ b/ext/sockets/config.m4 @@ -4,7 +4,7 @@ PHP_ARG_ENABLE([sockets], [Enable sockets support])]) if test "$PHP_SOCKETS" != "no"; then - AC_CHECK_FUNCS([hstrerror if_nametoindex if_indextoname sockatmark accept4]) + AC_CHECK_FUNCS([hstrerror if_nametoindex if_indextoname sockatmark]) AC_CHECK_HEADERS([sys/sockio.h linux/filter.h linux/if_packet.h linux/if_ether.h linux/udp.h]) AC_DEFINE([HAVE_SOCKETS], [1], [Define to 1 if the PHP extension 'sockets' is available.]) diff --git a/main/fastcgi.c b/main/fastcgi.c index 448576a978598..296f2bdc544ad 100644 --- a/main/fastcgi.c +++ b/main/fastcgi.c @@ -1394,7 +1394,11 @@ int fcgi_accept_request(fcgi_request *req) socklen_t len = sizeof(sa); FCGI_LOCK(req->listen_socket); +#if defined(HAVE_ACCEPT4) + req->fd = accept4(listen_socket, (struct sockaddr *)&sa, &len, SOCK_CLOEXEC); +#else req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len); +#endif FCGI_UNLOCK(req->listen_socket); client_sa = sa; @@ -1414,7 +1418,8 @@ int fcgi_accept_request(fcgi_request *req) return -1; } -#if defined(F_SETFD) && defined(FD_CLOEXEC) +// TODO once we drop support for older solaris (i.e < 11.4) we could drop this block +#if defined(F_SETFD) && defined(FD_CLOEXEC) && !defined(HAVE_ACCEPT4) int fd_attrs = fcntl(req->fd, F_GETFD); if (0 > fd_attrs) { fcgi_log(FCGI_WARNING, "failed to get attributes of the connection socket");