Skip to content

Commit 77bf924

Browse files
yellowcrescentbukka
authored andcommitted
Fix bug #68440: [sapi/fpm] use multiple FPM_SOCKETS env vars to prevent hitting MAX_ARG_STRLEN with a large number of pools
1 parent 8b104d7 commit 77bf924

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

sapi/fpm/fpm/fpm_sockets.c

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ enum { FPM_GET_USE_SOCKET = 1, FPM_STORE_SOCKET = 2, FPM_STORE_USE_SOCKET = 3 };
4444
static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
4545
{
4646
unsigned i;
47+
unsigned socket_set_count = 0;
48+
unsigned socket_set[FPM_ENV_SOCKET_SET_MAX];
49+
unsigned socket_set_buf = 0;
50+
char envname[16];
4751
char *env_value = 0;
4852
int p = 0;
4953
struct listening_socket_s *ls = sockets_list.data;
@@ -54,8 +58,20 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
5458
} else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
5559
char fd[32];
5660
sprintf(fd, "%d", ls->sock);
57-
env_value = realloc(env_value, p + (p ? 1 : 0) + strlen(ls->key) + 1 + strlen(fd) + 1);
58-
p += sprintf(env_value + p, "%s%s=%s", p ? "," : "", ls->key, fd);
61+
62+
socket_set_buf = (i % FPM_ENV_SOCKET_SET_SIZE == 0 && i) ? 1 : 0;
63+
env_value = realloc(env_value, p + (p ? 1 : 0) + strlen(ls->key) + 1 + strlen(fd) + socket_set_buf + 1);
64+
65+
if (i % FPM_ENV_SOCKET_SET_SIZE == 0) {
66+
socket_set[socket_set_count] = p + socket_set_buf;
67+
socket_set_count++;
68+
if (i) {
69+
*(env_value + p + 1) = 0;
70+
}
71+
}
72+
73+
p += sprintf(env_value + p + socket_set_buf, "%s%s=%s", (p && !socket_set_buf) ? "," : "", ls->key, fd);
74+
p += socket_set_buf;
5975
}
6076

6177
if (which == FPM_CLEANUP_PARENT_EXIT_MAIN) {
@@ -67,7 +83,10 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
6783
}
6884

6985
if (env_value) {
70-
setenv("FPM_SOCKETS", env_value, 1);
86+
for(i = 0; i < socket_set_count; i++) {
87+
sprintf(envname, "FPM_SOCKETS_%d", i);
88+
setenv(envname, env_value + socket_set[i], 1);
89+
}
7190
free(env_value);
7291
}
7392

@@ -324,36 +343,43 @@ int fpm_sockets_init_main() /* {{{ */
324343
{
325344
unsigned i, lq_len;
326345
struct fpm_worker_pool_s *wp;
327-
char *inherited = getenv("FPM_SOCKETS");
346+
char sockname[16];
347+
char *inherited;
328348
struct listening_socket_s *ls;
329349

330350
if (0 == fpm_array_init(&sockets_list, sizeof(struct listening_socket_s), 10)) {
331351
return -1;
332352
}
333353

334354
/* import inherited sockets */
335-
while (inherited && *inherited) {
336-
char *comma = strchr(inherited, ',');
337-
int type, fd_no;
338-
char *eq;
339-
340-
if (comma) {
341-
*comma = '\0';
342-
}
355+
for (i = 0; i < FPM_ENV_SOCKET_SET_MAX; i++) {
356+
sprintf(sockname, "FPM_SOCKETS_%d", i);
357+
inherited = getenv(sockname);
358+
if (!inherited) break;
359+
360+
while (inherited && *inherited) {
361+
char *comma = strchr(inherited, ',');
362+
int type, fd_no;
363+
char *eq;
364+
365+
if (comma) {
366+
*comma = '\0';
367+
}
343368

344-
eq = strchr(inherited, '=');
345-
if (eq) {
346-
*eq = '\0';
347-
fd_no = atoi(eq + 1);
348-
type = fpm_sockets_domain_from_address(inherited);
349-
zlog(ZLOG_NOTICE, "using inherited socket fd=%d, \"%s\"", fd_no, inherited);
350-
fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
351-
}
369+
eq = strchr(inherited, '=');
370+
if (eq) {
371+
*eq = '\0';
372+
fd_no = atoi(eq + 1);
373+
type = fpm_sockets_domain_from_address(inherited);
374+
zlog(ZLOG_NOTICE, "using inherited socket fd=%d, \"%s\"", fd_no, inherited);
375+
fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
376+
}
352377

353-
if (comma) {
354-
inherited = comma + 1;
355-
} else {
356-
inherited = 0;
378+
if (comma) {
379+
inherited = comma + 1;
380+
} else {
381+
inherited = 0;
382+
}
357383
}
358384
}
359385

sapi/fpm/fpm/fpm_sockets.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
#if (__FreeBSD__) || (__OpenBSD__)
2020
#define FPM_BACKLOG_DEFAULT -1
2121
#else
22-
#define FPM_BACKLOG_DEFAULT 511
22+
#define FPM_BACKLOG_DEFAULT 511
2323
#endif
2424

25+
#define FPM_ENV_SOCKET_SET_MAX 256
26+
#define FPM_ENV_SOCKET_SET_SIZE 128
27+
2528
enum fpm_address_domain fpm_sockets_domain_from_address(char *addr);
2629
int fpm_sockets_init_main();
2730
int fpm_socket_get_listening_queue(int sock, unsigned *cur_lq, unsigned *max_lq);

0 commit comments

Comments
 (0)