21
21
22
22
static int fd_stdout [2 ];
23
23
static int fd_stderr [2 ];
24
+ static int fd_ioctrl [2 ];
24
25
25
26
int fpm_stdio_init_main () /* {{{ */
26
27
{
@@ -106,11 +107,36 @@ int fpm_stdio_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
106
107
}
107
108
/* }}} */
108
109
109
- #define FPM_STDIO_CMD_FLUSH "\0fscf "
110
+ #define FPM_STDIO_CTRL_FLUSH "f "
110
111
111
112
int fpm_stdio_flush_child () /* {{{ */
112
113
{
113
- return write (STDERR_FILENO , FPM_STDIO_CMD_FLUSH , sizeof (FPM_STDIO_CMD_FLUSH ));
114
+ return write (fd_ioctrl [1 ], FPM_STDIO_CTRL_FLUSH , sizeof (FPM_STDIO_CTRL_FLUSH ));
115
+ }
116
+ /* }}} */
117
+
118
+ static void fpm_stdio_child_ctrl (struct fpm_event_s * ev , short which , void * arg ) /* {{{ */
119
+ {
120
+ static const int max_buf_size = 16 ;
121
+ int fd = ev -> fd ;
122
+ char buf [max_buf_size ];
123
+ struct fpm_child_s * child ;
124
+ int res ;
125
+
126
+ if (!arg ) {
127
+ return ;
128
+ }
129
+ child = (struct fpm_child_s * )arg ;
130
+
131
+ res = read (fd , buf , max_buf_size );
132
+
133
+ if (res <= 0 ) {
134
+ return ;
135
+ }
136
+
137
+ if (!memcmp (buf , FPM_STDIO_CTRL_FLUSH , sizeof (FPM_STDIO_CTRL_FLUSH )) && child -> log_stream ) {
138
+ zlog_stream_finish (child -> log_stream );
139
+ }
114
140
}
115
141
/* }}} */
116
142
@@ -124,7 +150,7 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
124
150
struct fpm_event_s * event ;
125
151
int fifo_in = 1 , fifo_out = 1 ;
126
152
int in_buf = 0 ;
127
- int read_fail = 0 , finish_log_stream = 0 , create_log_stream ;
153
+ int read_fail = 0 , create_log_stream ;
128
154
int res ;
129
155
struct zlog_stream * log_stream ;
130
156
@@ -175,21 +201,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
175
201
}
176
202
} else {
177
203
in_buf += res ;
178
- /* check if buffer should be flushed */
179
- if (!buf [in_buf - 1 ] && in_buf >= sizeof (FPM_STDIO_CMD_FLUSH ) &&
180
- !memcmp (buf + in_buf - sizeof (FPM_STDIO_CMD_FLUSH ),
181
- FPM_STDIO_CMD_FLUSH , sizeof (FPM_STDIO_CMD_FLUSH ))) {
182
- /* if buffer ends with flush cmd, then the stream will be finished */
183
- finish_log_stream = 1 ;
184
- in_buf -= sizeof (FPM_STDIO_CMD_FLUSH );
185
- } else if (!buf [0 ] && in_buf > sizeof (FPM_STDIO_CMD_FLUSH ) &&
186
- !memcmp (buf , FPM_STDIO_CMD_FLUSH , sizeof (FPM_STDIO_CMD_FLUSH ))) {
187
- /* if buffer starts with flush cmd, then the stream will be finished */
188
- finish_log_stream = 1 ;
189
- in_buf -= sizeof (FPM_STDIO_CMD_FLUSH );
190
- /* move data behind the flush cmd */
191
- memmove (buf , buf + sizeof (FPM_STDIO_CMD_FLUSH ), in_buf );
192
- }
193
204
}
194
205
}
195
206
@@ -237,8 +248,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
237
248
close (child -> fd_stderr );
238
249
child -> fd_stderr = -1 ;
239
250
}
240
- } else if (finish_log_stream ) {
241
- zlog_stream_finish (log_stream );
242
251
}
243
252
}
244
253
/* }}} */
@@ -261,12 +270,25 @@ int fpm_stdio_prepare_pipes(struct fpm_child_s *child) /* {{{ */
261
270
return -1 ;
262
271
}
263
272
264
- if (0 > fd_set_blocked (fd_stdout [0 ], 0 ) || 0 > fd_set_blocked (fd_stderr [0 ], 0 )) {
273
+ if (0 > pipe (fd_ioctrl )) {
274
+ zlog (ZLOG_SYSERROR , "failed to prepare the IO control pipe" );
275
+ close (fd_stdout [0 ]);
276
+ close (fd_stdout [1 ]);
277
+ close (fd_stderr [0 ]);
278
+ close (fd_stderr [1 ]);
279
+ return -1 ;
280
+ }
281
+
282
+ if (0 > fd_set_blocked (fd_stdout [0 ], 0 ) ||
283
+ 0 > fd_set_blocked (fd_stderr [0 ], 0 ) ||
284
+ 0 > fd_set_blocked (fd_ioctrl [0 ], 0 )) {
265
285
zlog (ZLOG_SYSERROR , "failed to unblock pipes" );
266
286
close (fd_stdout [0 ]);
267
287
close (fd_stdout [1 ]);
268
288
close (fd_stderr [0 ]);
269
289
close (fd_stderr [1 ]);
290
+ close (fd_ioctrl [0 ]);
291
+ close (fd_ioctrl [1 ]);
270
292
return -1 ;
271
293
}
272
294
return 0 ;
@@ -284,12 +306,17 @@ int fpm_stdio_parent_use_pipes(struct fpm_child_s *child) /* {{{ */
284
306
285
307
child -> fd_stdout = fd_stdout [0 ];
286
308
child -> fd_stderr = fd_stderr [0 ];
309
+ child -> fd_ioctrl = fd_ioctrl [0 ];
287
310
288
311
fpm_event_set (& child -> ev_stdout , child -> fd_stdout , FPM_EV_READ , fpm_stdio_child_said , child );
289
312
fpm_event_add (& child -> ev_stdout , 0 );
290
313
291
314
fpm_event_set (& child -> ev_stderr , child -> fd_stderr , FPM_EV_READ , fpm_stdio_child_said , child );
292
315
fpm_event_add (& child -> ev_stderr , 0 );
316
+
317
+ fpm_event_set (& child -> ev_ioctrl , child -> fd_ioctrl , FPM_EV_READ , fpm_stdio_child_ctrl , child );
318
+ fpm_event_add (& child -> ev_ioctrl , 0 );
319
+
293
320
return 0 ;
294
321
}
295
322
/* }}} */
@@ -302,9 +329,12 @@ int fpm_stdio_discard_pipes(struct fpm_child_s *child) /* {{{ */
302
329
303
330
close (fd_stdout [1 ]);
304
331
close (fd_stderr [1 ]);
332
+ close (fd_ioctrl [1 ]);
305
333
306
334
close (fd_stdout [0 ]);
307
335
close (fd_stderr [0 ]);
336
+ close (fd_ioctrl [0 ]);
337
+
308
338
return 0 ;
309
339
}
310
340
/* }}} */
@@ -314,8 +344,11 @@ void fpm_stdio_child_use_pipes(struct fpm_child_s *child) /* {{{ */
314
344
if (child -> wp -> config -> catch_workers_output ) {
315
345
dup2 (fd_stdout [1 ], STDOUT_FILENO );
316
346
dup2 (fd_stderr [1 ], STDERR_FILENO );
317
- close (fd_stdout [0 ]); close (fd_stdout [1 ]);
318
- close (fd_stderr [0 ]); close (fd_stderr [1 ]);
347
+ close (fd_stdout [0 ]);
348
+ close (fd_stdout [1 ]);
349
+ close (fd_stderr [0 ]);
350
+ close (fd_stderr [1 ]);
351
+ close (fd_ioctrl [0 ]);
319
352
} else {
320
353
/* stdout of parent is always /dev/null */
321
354
dup2 (STDOUT_FILENO , STDERR_FILENO );
0 commit comments