Skip to content

Commit 75bf3dd

Browse files
committed
ext/sockets: socket_accept setting fcntl's FD_CLOEXEC on unixes.
mainly for scenarios when pcntl_fork/pcntl_exec are involved so when the latter is executed, we avoid unwarranted effects with the file descriptors, instead the socket will be closed on success.
1 parent a1ea464 commit 75bf3dd

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

ext/sockets/sockets.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,18 @@ static bool php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct
269269
return 0;
270270
}
271271

272+
#if !defined(PHP_WIN32)
273+
/**
274+
* accept4 could had been used but not all platforms support it (e.g. Haiku, solaris < 11.4, ...)
275+
* win32, not having any concept of child process, has no need to address it.
276+
*/
277+
278+
if (fcntl(out_sock->bsd_socket, F_GETFD, FD_CLOEXEC) < 0) {
279+
PHP_SOCKET_ERROR(out_sock, "unable to set cloexec mode on the socket", errno);
280+
return 0;
281+
}
282+
#endif
283+
272284
out_sock->error = 0;
273285
out_sock->blocking = 1;
274286
out_sock->type = la->sa_family;

0 commit comments

Comments
 (0)