23
23
static int fd_stdout [2 ];
24
24
static int fd_stderr [2 ];
25
25
26
- static void fpm_stdio_cleanup (int which , void * arg ) /* {{{ */
27
- {
28
- zlog_cleanup ();
29
- }
30
- /* }}} */
31
-
32
26
int fpm_stdio_init_main () /* {{{ */
33
27
{
34
28
int fd = open ("/dev/null" , O_RDWR );
@@ -37,9 +31,6 @@ int fpm_stdio_init_main() /* {{{ */
37
31
zlog (ZLOG_SYSERROR , "failed to init stdio: open(\"/dev/null\")" );
38
32
return -1 ;
39
33
}
40
- if (0 > fpm_cleanup_add (FPM_CLEANUP_PARENT , fpm_stdio_cleanup , 0 )) {
41
- return -1 ;
42
- }
43
34
44
35
if (0 > dup2 (fd , STDIN_FILENO ) || 0 > dup2 (fd , STDOUT_FILENO )) {
45
36
zlog (ZLOG_SYSERROR , "failed to init stdio: dup2()" );
@@ -116,6 +107,12 @@ int fpm_stdio_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
116
107
}
117
108
/* }}} */
118
109
110
+ int fpm_stdio_flush_child () /* {{{ */
111
+ {
112
+ return write (STDERR_FILENO , "\0" , 1 );
113
+ }
114
+ /* }}} */
115
+
119
116
static void fpm_stdio_child_said (struct fpm_event_s * ev , short which , void * arg ) /* {{{ */
120
117
{
121
118
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)
126
123
struct fpm_event_s * event ;
127
124
int fifo_in = 1 , fifo_out = 1 ;
128
125
int in_buf = 0 ;
129
- int read_fail = 0 ;
126
+ int read_fail = 0 , finish_log_stream = 0 ;
130
127
int res ;
131
- struct zlog_stream stream ;
128
+ struct zlog_stream * log_stream ;
132
129
133
130
if (!arg ) {
134
131
return ;
@@ -142,12 +139,17 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
142
139
event = & child -> ev_stderr ;
143
140
}
144
141
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
+ }
151
153
152
154
while (fifo_in || fifo_out ) {
153
155
if (fifo_in ) {
@@ -160,6 +162,11 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
160
162
}
161
163
} else {
162
164
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
+ }
163
170
}
164
171
}
165
172
@@ -173,25 +180,25 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
173
180
if (nl ) {
174
181
/* we should print each new line int the new message */
175
182
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 );
178
185
/* skip new line */
179
186
out_len ++ ;
180
187
/* move data in the buffer */
181
188
memmove (buf , buf + out_len , in_buf - out_len );
182
189
in_buf -= out_len ;
183
190
} else if (in_buf == max_buf_size - 1 || !fifo_in ) {
184
191
/* 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 );
186
193
in_buf = 0 ;
187
194
}
188
195
}
189
196
}
190
197
}
191
198
192
199
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 );
195
202
if (read_fail < 0 ) {
196
203
zlog (ZLOG_SYSERROR , "unable to read what child say" );
197
204
}
@@ -205,8 +212,8 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
205
212
close (child -> fd_stderr );
206
213
child -> fd_stderr = -1 ;
207
214
}
208
- } else {
209
- zlog_stream_close ( & stream );
215
+ } else if ( finish_log_stream ) {
216
+ zlog_stream_finish ( log_stream );
210
217
}
211
218
}
212
219
/* }}} */
0 commit comments