Skip to content

Commit 1bcb57d

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. close GH-14606
1 parent 7232636 commit 1bcb57d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ PHP NEWS
269269
macOs and FreeBSD. (David Carlier)
270270
. Added SO_LINGER_SEC for macOs, true equivalent of SO_LINGER in other platforms.
271271
(David Carlier)
272+
. Add close-on-exec on socket created with socket_accept on unixes. (David Carlier)
272273

273274
- SNMP:
274275
. Removed the deprecated inet_ntoa call support. (David Carlier)

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)