Skip to content

Commit 47f205d

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 47f205d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

ext/sockets/sockets.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,28 @@ 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+
int mode;
278+
279+
if ((mode = fcntl(out_sock->bsd_socket, F_GETFD)) < 0) {
280+
PHP_SOCKET_ERROR(out_sock, "unable to get fcntl mode on the socket", errno);
281+
return 0;
282+
}
283+
284+
int cloexec = (mode | FD_CLOEXEC);
285+
286+
if (mode != cloexec) {
287+
if (fcntl(out_sock->bsd_socket, F_SETFD, cloexec) < 0) {
288+
PHP_SOCKET_ERROR(out_sock, "unable to set cloexec mode on the socket", errno);
289+
return 0;
290+
}
291+
}
292+
#endif
293+
272294
out_sock->error = 0;
273295
out_sock->blocking = 1;
274296
out_sock->type = la->sa_family;

0 commit comments

Comments
 (0)