Skip to content

Commit bbffed6

Browse files
committed
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).
1 parent 17826d3 commit bbffed6

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ dnl Checks for library functions.
525525
dnl ----------------------------------------------------------------------------
526526

527527
AC_CHECK_FUNCS(m4_normalize([
528+
accept4
528529
alphasort
529530
asctime_r
530531
asprintf

ext/sockets/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PHP_ARG_ENABLE([sockets],
44
[Enable sockets support])])
55

66
if test "$PHP_SOCKETS" != "no"; then
7-
AC_CHECK_FUNCS([hstrerror if_nametoindex if_indextoname sockatmark accept4])
7+
AC_CHECK_FUNCS([hstrerror if_nametoindex if_indextoname sockatmark])
88
AC_CHECK_HEADERS([sys/sockio.h linux/filter.h linux/if_packet.h linux/if_ether.h linux/udp.h])
99
AC_DEFINE([HAVE_SOCKETS], [1],
1010
[Define to 1 if the PHP extension 'sockets' is available.])

main/fastcgi.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,11 @@ int fcgi_accept_request(fcgi_request *req)
13941394
socklen_t len = sizeof(sa);
13951395

13961396
FCGI_LOCK(req->listen_socket);
1397+
#if defined(HAVE_ACCEPT4)
1398+
req->fd = accept4(listen_socket, (struct sockaddr *)&sa, &len, SOCK_CLOEXEC);
1399+
#else
13971400
req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
1401+
#endif
13981402
FCGI_UNLOCK(req->listen_socket);
13991403

14001404
client_sa = sa;
@@ -1414,7 +1418,8 @@ int fcgi_accept_request(fcgi_request *req)
14141418
return -1;
14151419
}
14161420

1417-
#if defined(F_SETFD) && defined(FD_CLOEXEC)
1421+
// TODO once we drop support for older solaris (i.e < 11.4) we could drop this block
1422+
#if defined(F_SETFD) && defined(FD_CLOEXEC) && !defined(HAVE_ACCEPT4)
14181423
int fd_attrs = fcntl(req->fd, F_GETFD);
14191424
if (0 > fd_attrs) {
14201425
fcgi_log(FCGI_WARNING, "failed to get attributes of the connection socket");

0 commit comments

Comments
 (0)