2323static int fd_stdout [2 ];
2424static int fd_stderr [2 ];
2525
26- static void fpm_stdio_cleanup (int which , void * arg ) /* {{{ */
27- {
28- zlog_cleanup ();
29- }
30- /* }}} */
31-
3226int fpm_stdio_init_main () /* {{{ */
3327{
3428 int fd = open ("/dev/null" , O_RDWR );
@@ -37,9 +31,6 @@ int fpm_stdio_init_main() /* {{{ */
3731 zlog (ZLOG_SYSERROR , "failed to init stdio: open(\"/dev/null\")" );
3832 return -1 ;
3933 }
40- if (0 > fpm_cleanup_add (FPM_CLEANUP_PARENT , fpm_stdio_cleanup , 0 )) {
41- return -1 ;
42- }
4334
4435 if (0 > dup2 (fd , STDIN_FILENO ) || 0 > dup2 (fd , STDOUT_FILENO )) {
4536 zlog (ZLOG_SYSERROR , "failed to init stdio: dup2()" );
@@ -116,6 +107,12 @@ int fpm_stdio_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
116107}
117108/* }}} */
118109
110+ int fpm_stdio_flush_child () /* {{{ */
111+ {
112+ return write (STDERR_FILENO , "\0" , 1 );
113+ }
114+ /* }}} */
115+
119116static void fpm_stdio_child_said (struct fpm_event_s * ev , short which , void * arg ) /* {{{ */
120117{
121118 static const int max_buf_size = 1024 ;
@@ -126,9 +123,9 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
126123 struct fpm_event_s * event ;
127124 int fifo_in = 1 , fifo_out = 1 ;
128125 int in_buf = 0 ;
129- int read_fail = 0 ;
126+ int read_fail = 0 , finish_log_stream = 0 ;
130127 int res ;
131- struct zlog_stream stream ;
128+ struct zlog_stream * log_stream ;
132129
133130 if (!arg ) {
134131 return ;
@@ -142,12 +139,17 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
142139 event = & child -> ev_stderr ;
143140 }
144141
145- zlog_stream_init_ex (& stream , ZLOG_WARNING , STDERR_FILENO );
146- zlog_stream_set_decorating (& stream , child -> wp -> config -> decorate_workers_output );
147- zlog_stream_set_wrapping (& stream , ZLOG_TRUE );
148- zlog_stream_set_msg_prefix (& stream , "[pool %s] child %d said into %s: " ,
149- child -> wp -> config -> name , (int ) child -> pid , is_stdout ? "stdout" : "stderr" );
150- zlog_stream_set_msg_quoting (& stream , ZLOG_TRUE );
142+ if (!child -> log_stream ) {
143+ log_stream = child -> log_stream = malloc (sizeof (struct zlog_stream ));
144+ zlog_stream_init_ex (log_stream , ZLOG_WARNING , STDERR_FILENO );
145+ zlog_stream_set_decorating (log_stream , child -> wp -> config -> decorate_workers_output );
146+ zlog_stream_set_wrapping (log_stream , ZLOG_TRUE );
147+ zlog_stream_set_msg_prefix (log_stream , "[pool %s] child %d said into %s: " ,
148+ child -> wp -> config -> name , (int ) child -> pid , is_stdout ? "stdout" : "stderr" );
149+ zlog_stream_set_msg_quoting (log_stream , ZLOG_TRUE );
150+ } else {
151+ log_stream = child -> log_stream ;
152+ }
151153
152154 while (fifo_in || fifo_out ) {
153155 if (fifo_in ) {
@@ -160,6 +162,11 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
160162 }
161163 } else {
162164 in_buf += res ;
165+ /* if buffer ends with \0, then the stream will be finished */
166+ if (!buf [in_buf - 1 ]) {
167+ finish_log_stream = 1 ;
168+ in_buf -- ;
169+ }
163170 }
164171 }
165172
@@ -173,25 +180,25 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
173180 if (nl ) {
174181 /* we should print each new line int the new message */
175182 int out_len = nl - buf ;
176- zlog_stream_str (& stream , buf , out_len );
177- zlog_stream_finish (& stream );
183+ zlog_stream_str (log_stream , buf , out_len );
184+ zlog_stream_finish (log_stream );
178185 /* skip new line */
179186 out_len ++ ;
180187 /* move data in the buffer */
181188 memmove (buf , buf + out_len , in_buf - out_len );
182189 in_buf -= out_len ;
183190 } else if (in_buf == max_buf_size - 1 || !fifo_in ) {
184191 /* we should print if no more space in the buffer or no more data to come */
185- zlog_stream_str (& stream , buf , in_buf );
192+ zlog_stream_str (log_stream , buf , in_buf );
186193 in_buf = 0 ;
187194 }
188195 }
189196 }
190197 }
191198
192199 if (read_fail ) {
193- zlog_stream_set_msg_suffix (& stream , NULL , ", pipe is closed" );
194- zlog_stream_close ( & stream );
200+ zlog_stream_set_msg_suffix (log_stream , NULL , ", pipe is closed" );
201+ zlog_stream_finish ( log_stream );
195202 if (read_fail < 0 ) {
196203 zlog (ZLOG_SYSERROR , "unable to read what child say" );
197204 }
@@ -205,8 +212,8 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
205212 close (child -> fd_stderr );
206213 child -> fd_stderr = -1 ;
207214 }
208- } else {
209- zlog_stream_close ( & stream );
215+ } else if ( finish_log_stream ) {
216+ zlog_stream_finish ( log_stream );
210217 }
211218}
212219/* }}} */
0 commit comments