Skip to content

Commit ad4639b

Browse files
author
Mikhail Galanin
committed
[cloexec-listen-socket-fpm]: Set CLOEXEC on listened sockets when forking FPM children
1 parent 72e2e25 commit ad4639b

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

sapi/fpm/fpm/fpm_children.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ struct fpm_child_s *fpm_child_find(pid_t pid) /* {{{ */
167167
}
168168
/* }}} */
169169

170+
static int fpm_child_cloexec(void)
171+
{
172+
/* If PHP code invokes pcntl_fork()/exec(), we don't want the external programm to inherit the descriptor.
173+
If the external process accidentally uses the socket it will likely break the communication */
174+
int attrs = fcntl(fpm_globals.listening_socket, F_GETFD);
175+
if (0 > attrs) {
176+
zlog(ZLOG_WARNING, "failed to get attributes of listening socket, errno: %d", errno);
177+
return -1;
178+
}
179+
180+
/* set CLOEXEC to prevent the descriptor leaking to child processes */
181+
if (0 > fcntl(fpm_globals.listening_socket, F_SETFD, attrs | FD_CLOEXEC)) {
182+
zlog(ZLOG_WARNING, "failed to change attribute of listening socket");
183+
return -1;
184+
}
185+
186+
return 0;
187+
}
188+
170189
static void fpm_child_init(struct fpm_worker_pool_s *wp) /* {{{ */
171190
{
172191
fpm_globals.max_requests = wp->config->pm_max_requests;
@@ -178,7 +197,8 @@ static void fpm_child_init(struct fpm_worker_pool_s *wp) /* {{{ */
178197
0 > fpm_unix_init_child(wp) ||
179198
0 > fpm_signals_init_child() ||
180199
0 > fpm_env_init_child(wp) ||
181-
0 > fpm_php_init_child(wp)) {
200+
0 > fpm_php_init_child(wp) ||
201+
0 > fpm_child_cloexec()) {
182202

183203
zlog(ZLOG_ERROR, "[pool %s] child failed to initialize", wp->config->name);
184204
exit(FPM_EXIT_SOFTWARE);

0 commit comments

Comments
 (0)