php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #67383
Patch SOCK_CLOEXEC-and-FD_CLOEXEC revision 2014-06-05 11:26 UTC by casper at langemeijer dot eu

Patch SOCK_CLOEXEC-and-FD_CLOEXEC for *General Issues Bug #67383

Patch version 2014-06-05 11:26 UTC

Return to Bug #67383 | Download this patch
Patch Revisions:

Developer: casper@langemeijer.eu

diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c
index bd0a8f2..c1e215b 100644
--- a/ext/sockets/sockets.c
+++ b/ext/sockets/sockets.c
@@ -434,7 +434,7 @@ static int php_open_listen_sock(php_socket **php_sock, int port, int backlog TSR
 	la.sin_family = hp->h_addrtype;
 	la.sin_port = htons((unsigned short) port);
 
-	sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
+	sock->bsd_socket = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
 	sock->blocking = 1;
 
 	if (IS_INVALID_SOCKET(sock)) {
@@ -1354,7 +1354,7 @@ PHP_FUNCTION(socket_create)
 		arg2 = SOCK_STREAM;
 	}
 
-	php_sock->bsd_socket = socket(arg1, arg2, arg3);
+	php_sock->bsd_socket = socket(arg1, arg2 | SOCK_CLOEXEC, arg3);
 	php_sock->type = arg1;
 
 	if (IS_INVALID_SOCKET(php_sock)) {
diff --git a/main/network.c b/main/network.c
index fc2a94b..189514f 100644
--- a/main/network.c
+++ b/main/network.c
@@ -435,7 +435,7 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po
 		sa = *sal;
 
 		/* create a socket for this address */
-		sock = socket(sa->sa_family, socktype, 0);
+		sock = socket(sa->sa_family, socktype | SOCK_CLOEXEC, 0);
 
 		if (sock == SOCK_ERR) {
 			continue;
@@ -798,7 +798,7 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
 		sa = *sal;
 
 		/* create a socket for this address */
-		sock = socket(sa->sa_family, socktype, 0);
+		sock = socket(sa->sa_family, socktype | SOCK_CLOEXEC, 0);
 
 		if (sock == SOCK_ERR) {
 			continue;
diff --git a/main/streams/xp_socket.c b/main/streams/xp_socket.c
index a6dc115..07aae33 100644
--- a/main/streams/xp_socket.c
+++ b/main/streams/xp_socket.c
@@ -575,7 +575,7 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t *
 	if (stream->ops == &php_stream_unix_socket_ops || stream->ops == &php_stream_unixdg_socket_ops) {
 		struct sockaddr_un unix_addr;
 
-		sock->socket = socket(PF_UNIX, stream->ops == &php_stream_unix_socket_ops ? SOCK_STREAM : SOCK_DGRAM, 0);
+		sock->socket = socket(PF_UNIX, (stream->ops == &php_stream_unix_socket_ops ? SOCK_STREAM : SOCK_DGRAM) | SOCK_CLOEXEC, 0);
 
 		if (sock->socket == SOCK_ERR) {
 			if (xparam->want_errortext) {
@@ -625,7 +625,7 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
 	if (stream->ops == &php_stream_unix_socket_ops || stream->ops == &php_stream_unixdg_socket_ops) {
 		struct sockaddr_un unix_addr;
 
-		sock->socket = socket(PF_UNIX, stream->ops == &php_stream_unix_socket_ops ? SOCK_STREAM : SOCK_DGRAM, 0);
+		sock->socket = socket(PF_UNIX, (stream->ops == &php_stream_unix_socket_ops ? SOCK_STREAM : SOCK_DGRAM) | SOCK_CLOEXEC, 0);
 
 		if (sock->socket == SOCK_ERR) {
 			if (xparam->want_errortext) {
diff --git a/sapi/cgi/fastcgi.c b/sapi/cgi/fastcgi.c
index 8ddc2e4..2e640c0 100644
--- a/sapi/cgi/fastcgi.c
+++ b/sapi/cgi/fastcgi.c
@@ -669,7 +669,7 @@ int fcgi_listen(const char *path, int backlog)
 	}
 
 	/* Create, bind socket and start listen on it */
-	if ((listen_socket = socket(sa.sa.sa_family, SOCK_STREAM, 0)) < 0 ||
+	if ((listen_socket = socket(sa.sa.sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0 ||
 #ifdef SO_REUSEADDR
 	    setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse)) < 0 ||
 #endif
diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c
index e9bc9dc..48651c1 100644
--- a/sapi/cli/php_cli_server.c
+++ b/sapi/cli/php_cli_server.c
@@ -1295,7 +1295,7 @@ static int php_network_listen_socket(const char *host, int *port, int socktype,
 			sa = NULL;
 		}
 
-		retval = socket((*p)->sa_family, socktype, 0);
+		retval = socket((*p)->sa_family, socktype | SOCK_CLOEXEC, 0);
 		if (retval == SOCK_ERR) {
 			continue;
 		}
diff --git a/sapi/fpm/fpm/fpm_signals.c b/sapi/fpm/fpm/fpm_signals.c
index c5d0692..deb0468 100644
--- a/sapi/fpm/fpm/fpm_signals.c
+++ b/sapi/fpm/fpm/fpm_signals.c
@@ -145,7 +145,7 @@ static void sig_soft_quit(int signo) /* {{{ */
 
 	/* closing fastcgi listening socket will force fcgi_accept() exit immediately */
 	close(0);
-	if (0 > socket(AF_UNIX, SOCK_STREAM, 0)) {
+	if (0 > socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) {
 		zlog(ZLOG_WARNING, "failed to create a new socket");
 	}
 	fpm_php_soft_quit();
diff --git a/sapi/fpm/fpm/fpm_sockets.c b/sapi/fpm/fpm/fpm_sockets.c
index e056565..1fbf587 100644
--- a/sapi/fpm/fpm/fpm_sockets.c
+++ b/sapi/fpm/fpm/fpm_sockets.c
@@ -169,7 +169,7 @@ static int fpm_sockets_new_listening_socket(struct fpm_worker_pool_s *wp, struct
 	int sock;
 	mode_t saved_umask = 0;
 
-	sock = socket(sa->sa_family, SOCK_STREAM, 0);
+	sock = socket(sa->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
 
 	if (0 > sock) {
 		zlog(ZLOG_SYSERROR, "failed to create new listening socket: socket()");
@@ -482,7 +482,7 @@ int fpm_socket_unix_test_connect(struct sockaddr_un *sock, size_t socklen) /* {{
 		return -1;
 	}
 
-	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+	if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0) {
 		return -1;
 	}
 
diff --git a/sapi/litespeed/lsapilib.c b/sapi/litespeed/lsapilib.c
index cdd6076..c5c1476 100644
--- a/sapi/litespeed/lsapilib.c
+++ b/sapi/litespeed/lsapilib.c
@@ -2227,7 +2227,7 @@ int LSAPI_CreateListenSock2( const struct sockaddr * pServerAddr, int backlog )
         return -1;
     }
 
-    fd = socket( pServerAddr->sa_family, SOCK_STREAM, 0 );
+    fd = socket( pServerAddr->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0 );
     if ( fd == -1 )
         return -1;
 
diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c
index 93fdbd7..02b6028 100644
--- a/sapi/phpdbg/phpdbg.c
+++ b/sapi/phpdbg/phpdbg.c
@@ -714,7 +714,7 @@ static inline void phpdbg_sigint_handler(int signo) /* {{{ */
 #ifndef _WIN32
 int phpdbg_open_socket(const char *interface, short port) /* {{{ */
 {
-	int fd = socket(AF_INET, SOCK_STREAM, 0);
+	int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
 
 	switch (fd) {
 		case -1:
diff --git a/ext/dba/dba_cdb.c b/ext/dba/dba_cdb.c
index 075aedb..353f110 100644
--- a/ext/dba/dba_cdb.c
+++ b/ext/dba/dba_cdb.c
@@ -82,6 +82,12 @@ DBA_OPEN_FUNC(cdb)
 				*error = "Unable to open file";
 				return FAILURE;
 			}
+#ifdef F_SETFD
+# ifndef FD_CLOEXEC
+#  define FD_CLOEXEC 1
+# endif
+			fcntl(file, F_SETFD, FD_CLOEXEC);
+#endif
 #endif
 			break;
 #if DBA_CDB_BUILTIN
diff --git a/ext/readline/readline_cli.c b/ext/readline/readline_cli.c
index c2bf876..b6dd9ca 100644
--- a/ext/readline/readline_cli.c
+++ b/ext/readline/readline_cli.c
@@ -90,6 +90,12 @@ static size_t readline_shell_write(const char *str, uint str_length TSRMLS_DC) /
 		pager_pipe = VCWD_POPEN(CLIR_G(pager), "w");
 	}
 	if (pager_pipe) {
+#ifdef F_SETFD
+# ifndef FD_CLOEXEC
+#  define FD_CLOEXEC 1
+# endif
+		fcntl(pager_pipe, F_SETFD, FD_CLOEXEC);
+#endif
 		return fwrite(str, 1, MIN(str_length, 16384), pager_pipe);
 	}
 
diff --git a/ext/standard/file.c b/ext/standard/file.c
index e1f24ab..ffb509c 100644
--- a/ext/standard/file.c
+++ b/ext/standard/file.c
@@ -943,6 +943,13 @@ PHP_FUNCTION(popen)
 		RETURN_FALSE;
 	}
 
+#ifdef F_SETFD
+# ifndef FD_CLOEXEC
+#  define FD_CLOEXEC 1
+# endif
+	fcntl(fp, F_SETFD, FD_CLOEXEC);
+#endif
+
 	stream = php_stream_fopen_from_pipe(fp, mode);
 
 	if (stream == NULL)	{
diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c
index 3be7b5f..40833ca 100644
--- a/main/fopen_wrappers.c
+++ b/main/fopen_wrappers.c
@@ -348,6 +348,12 @@ static FILE *php_fopen_and_set_opened_path(const char *path, const char *mode, c
 	}
 	fp = VCWD_FOPEN(path, mode);
 	if (fp && opened_path) {
+#ifdef F_SETFD
+# ifndef FD_CLOEXEC
+#  define FD_CLOEXEC 1
+# endif
+		fcntl(fp, F_SETFD, FD_CLOEXEC);
+#endif
 		*opened_path = expand_filepath_with_mode(path, NULL, NULL, 0, CWD_EXPAND TSRMLS_CC);
 	}
 	return fp;
diff --git a/main/php_open_temporary_file.c b/main/php_open_temporary_file.c
index ebe5350..f456fd6 100644
--- a/main/php_open_temporary_file.c
+++ b/main/php_open_temporary_file.c
@@ -168,6 +168,12 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char **
 	if (fd == -1 || !opened_path_p) {
 		efree(opened_path);
 	} else {
+#ifdef F_SETFD
+# ifndef FD_CLOEXEC
+#  define FD_CLOEXEC 1
+# endif
+		fcntl(fd, F_SETFD, FD_CLOEXEC);
+#endif
 		*opened_path_p = opened_path;
 	}
 	efree(new_state.cwd);
diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c
index 5e9e5c7..d364125 100644
--- a/main/streams/plain_wrapper.c
+++ b/main/streams/plain_wrapper.c
@@ -938,6 +938,12 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
 	fd = open(realpath, open_flags, 0666);
 
 	if (fd != -1)	{
+#ifdef F_SETFD
+# ifndef FD_CLOEXEC
+#  define FD_CLOEXEC 1
+# endif
+		fcntl(fd, F_SETFD, FD_CLOEXEC);
+#endif
 
 		if (options & STREAM_OPEN_FOR_INCLUDE) {
 			ret = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 04 08:01:29 2024 UTC