1
- /* (c) 2007,2008 Andrei Nigmatulin */
1
+ /* (c) 2007,2008 Andrei Nigmatulin */
2
2
3
3
#include "fpm_config.h"
4
4
27
27
#include "fpm_env.h"
28
28
#include "fpm_cleanup.h"
29
29
#include "fpm_scoreboard.h"
30
- #include "zend_smart_string.h"
31
30
32
31
struct listening_socket_s {
33
32
int refcount ;
@@ -59,24 +58,40 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
59
58
unsigned i ;
60
59
unsigned socket_set_count = 0 ;
61
60
unsigned socket_set [FPM_ENV_SOCKET_SET_MAX ];
61
+ unsigned socket_set_buf = 0 ;
62
62
char envname [32 ];
63
- smart_string env_str = {0 };
63
+ char * env_value = 0 ;
64
+ int p = 0 ;
64
65
struct listening_socket_s * ls = sockets_list .data ;
65
66
66
67
for (i = 0 ; i < sockets_list .used ; i ++ , ls ++ ) {
67
68
if (which != FPM_CLEANUP_PARENT_EXEC ) {
68
69
close (ls -> sock );
69
70
} else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
71
+ char fd [32 ];
72
+ char * tmpenv_value ;
73
+ snprintf (fd , sizeof (fd ), "%d" , ls -> sock );
74
+
75
+ socket_set_buf = (i % FPM_ENV_SOCKET_SET_SIZE == 0 && i ) ? 1 : 0 ;
76
+ tmpenv_value = realloc (env_value , p + (p ? 1 : 0 ) + strlen (ls -> key ) + 1 + strlen (fd ) + socket_set_buf + 1 );
77
+ if (!tmpenv_value ) {
78
+ zlog (ZLOG_SYSERROR , "failure to inherit data on parent exec for socket `%s` due to memory allocation failure" , ls -> key );
79
+ free (ls -> key );
80
+ break ;
81
+ }
82
+
83
+ env_value = tmpenv_value ;
84
+
70
85
if (i % FPM_ENV_SOCKET_SET_SIZE == 0 ) {
71
- smart_string_appendc (& env_str , '\0' );
72
- socket_set [socket_set_count ] = env_str .len ;
86
+ socket_set [socket_set_count ] = p + socket_set_buf ;
73
87
socket_set_count ++ ;
74
- } else {
75
- smart_string_appendc (& env_str , ',' );
88
+ if (i ) {
89
+ * (env_value + p + 1 ) = 0 ;
90
+ }
76
91
}
77
- smart_string_appends ( & env_str , ls -> key );
78
- smart_string_appendc ( & env_str , '=' );
79
- smart_string_append_long ( & env_str , ls -> sock ) ;
92
+
93
+ p += sprintf ( env_value + p + socket_set_buf , "%s%s=%s" , ( p && ! socket_set_buf ) ? "," : "" , ls -> key , fd );
94
+ p += socket_set_buf ;
80
95
}
81
96
82
97
if (which == FPM_CLEANUP_PARENT_EXIT_MAIN ) {
@@ -87,15 +102,14 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
87
102
free (ls -> key );
88
103
}
89
104
90
- if (env_str .c ) {
91
- smart_string_appendc (& env_str , '\0' );
105
+ if (env_value ) {
92
106
for (i = 0 ; i < socket_set_count ; i ++ ) {
93
107
fpm_sockets_get_env_name (envname , sizeof (envname ), i );
94
- setenv (envname , env_str . c + socket_set [i ], 1 );
108
+ setenv (envname , env_value + socket_set [i ], 1 );
95
109
}
96
110
fpm_sockets_get_env_name (envname , sizeof (envname ), socket_set_count );
97
111
unsetenv (envname );
98
- smart_string_free ( & env_str );
112
+ free ( env_value );
99
113
}
100
114
101
115
fpm_array_free (& sockets_list );
@@ -383,24 +397,24 @@ static int fpm_socket_af_inet_listening_socket(struct fpm_worker_pool_s *wp) /*
383
397
static int fpm_socket_af_unix_listening_socket (struct fpm_worker_pool_s * wp ) /* {{{ */
384
398
{
385
399
struct sockaddr_un sa_un ;
386
- size_t socket_length = sizeof (sa_un .sun_path );
387
- size_t address_length = strlen (wp -> config -> listen_address );
400
+ size_t socket_length = sizeof (sa_un .sun_path );
401
+ size_t address_length = strlen (wp -> config -> listen_address );
388
402
389
403
memset (& sa_un , 0 , sizeof (sa_un ));
390
404
strlcpy (sa_un .sun_path , wp -> config -> listen_address , socket_length );
391
405
392
- if (address_length >= socket_length ) {
393
- zlog (
394
- ZLOG_WARNING ,
395
- "[pool %s] cannot bind to UNIX socket '%s' as path is too long (found length: %zu, "
406
+ if (address_length >= socket_length ) {
407
+ zlog (
408
+ ZLOG_WARNING ,
409
+ "[pool %s] cannot bind to UNIX socket '%s' as path is too long (found length: %zu, "
396
410
"maximal length: %zu), trying cut socket path instead '%s'" ,
397
- wp -> config -> name ,
398
- wp -> config -> listen_address ,
399
- address_length ,
400
- socket_length ,
401
- sa_un .sun_path
402
- );
403
- }
411
+ wp -> config -> name ,
412
+ wp -> config -> listen_address ,
413
+ address_length ,
414
+ socket_length ,
415
+ sa_un .sun_path
416
+ );
417
+ }
404
418
405
419
sa_un .sun_family = AF_UNIX ;
406
420
return fpm_sockets_get_listening_socket (wp , (struct sockaddr * ) & sa_un , sizeof (struct sockaddr_un ));
@@ -499,7 +513,7 @@ int fpm_sockets_init_main(void)
499
513
return -1 ;
500
514
}
501
515
502
- if (wp -> listen_address_domain == FPM_AF_INET && fpm_socket_get_listening_queue (wp -> listening_socket , NULL , & lq_len ) >= 0 ) {
516
+ if (wp -> listen_address_domain == FPM_AF_INET && fpm_socket_get_listening_queue (wp -> listening_socket , NULL , & lq_len ) >= 0 ) {
503
517
fpm_scoreboard_update (-1 , -1 , -1 , (int )lq_len , -1 , -1 , 0 , 0 , FPM_SCOREBOARD_ACTION_SET , wp -> scoreboard );
504
518
}
505
519
}
0 commit comments