27
27
#include "fpm_env.h"
28
28
#include "fpm_cleanup.h"
29
29
#include "fpm_scoreboard.h"
30
+ #include "zend_smart_string.h"
30
31
31
32
struct listening_socket_s {
32
33
int refcount ;
@@ -60,38 +61,36 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
60
61
unsigned socket_set [FPM_ENV_SOCKET_SET_MAX ];
61
62
unsigned socket_set_buf = 0 ;
62
63
char envname [32 ];
63
- char * env_value = 0 ;
64
- int p = 0 ;
64
+ smart_string env_str = {0 };
65
65
struct listening_socket_s * ls = sockets_list .data ;
66
66
67
67
for (i = 0 ; i < sockets_list .used ; i ++ , ls ++ ) {
68
68
if (which != FPM_CLEANUP_PARENT_EXEC ) {
69
69
close (ls -> sock );
70
70
} else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
71
71
char fd [32 ];
72
- char * tmpenv_value ;
73
72
snprintf (fd , sizeof (fd ), "%d" , ls -> sock );
74
73
75
74
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
75
85
76
if (i % FPM_ENV_SOCKET_SET_SIZE == 0 ) {
86
- socket_set [socket_set_count ] = p + socket_set_buf ;
77
+ socket_set [socket_set_count ] = env_str . len + socket_set_buf ;
87
78
socket_set_count ++ ;
88
79
if (i ) {
89
- * ( env_value + p + 1 ) = 0 ;
80
+ smart_string_appendc ( & env_str , '\0' ) ;
90
81
}
91
82
}
92
83
93
- p += sprintf (env_value + p + socket_set_buf , "%s%s=%s" , (p && !socket_set_buf ) ? "," : "" , ls -> key , fd );
94
- p += socket_set_buf ;
84
+ if (env_str .len && !socket_set_buf ) {
85
+ smart_string_appendc (& env_str , ',' );
86
+ }
87
+ smart_string_appends (& env_str , ls -> key );
88
+ smart_string_appendc (& env_str , '=' );
89
+ smart_string_appends (& env_str , fd );
90
+
91
+ if (socket_set_buf ) {
92
+ smart_string_appendc (& env_str , '\0' );
93
+ }
95
94
}
96
95
97
96
if (which == FPM_CLEANUP_PARENT_EXIT_MAIN ) {
@@ -102,14 +101,15 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
102
101
free (ls -> key );
103
102
}
104
103
105
- if (env_value ) {
104
+ if (env_str .c ) {
105
+ smart_string_0 (& env_str );
106
106
for (i = 0 ; i < socket_set_count ; i ++ ) {
107
107
fpm_sockets_get_env_name (envname , sizeof (envname ), i );
108
- setenv (envname , env_value + socket_set [i ], 1 );
108
+ setenv (envname , env_str . c + socket_set [i ], 1 );
109
109
}
110
110
fpm_sockets_get_env_name (envname , sizeof (envname ), socket_set_count );
111
111
unsetenv (envname );
112
- free ( env_value );
112
+ smart_string_free ( & env_str );
113
113
}
114
114
115
115
fpm_array_free (& sockets_list );
0 commit comments