Skip to content

Commit 068aaed

Browse files
authored
ext/sockets: using accept4 wheneever possible for php_accept_connect helper (php#19268)
Haiku now supports it as well now, so we can simplify the workflow a bit for those platforms.
1 parent 191290e commit 068aaed

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

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])
7+
AC_CHECK_FUNCS([hstrerror if_nametoindex if_indextoname sockatmark accept4])
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.])

ext/sockets/sockets.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,19 @@ static bool php_open_listen_sock(php_socket *sock, unsigned short port, int back
283283

284284
static bool php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct sockaddr *la, socklen_t *la_len) /* {{{ */
285285
{
286+
#if defined(HAVE_ACCEPT4)
287+
int flags = SOCK_CLOEXEC;
288+
if (!in_sock->blocking) {
289+
flags |= SOCK_NONBLOCK;
290+
}
291+
292+
out_sock->bsd_socket = accept4(in_sock->bsd_socket, la, la_len, flags);
293+
294+
if (IS_INVALID_SOCKET(out_sock)) {
295+
PHP_SOCKET_ERROR(out_sock, "unable to accept incoming connection", errno);
296+
return 0;
297+
}
298+
#else
286299
out_sock->bsd_socket = accept(in_sock->bsd_socket, la, la_len);
287300

288301
if (IS_INVALID_SOCKET(out_sock)) {
@@ -292,7 +305,7 @@ static bool php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct
292305

293306
#if !defined(PHP_WIN32)
294307
/**
295-
* accept4 could had been used but not all platforms support it (e.g. Haiku, solaris < 11.4, ...)
308+
* for fewer and fewer platforms not supporting accept4 syscall we use fcntl instead,
296309
* win32, not having any concept of child process, has no need to address it.
297310
*/
298311
int mode;
@@ -310,6 +323,7 @@ static bool php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct
310323
return 0;
311324
}
312325
}
326+
#endif
313327
#endif
314328

315329
out_sock->error = 0;

0 commit comments

Comments
 (0)