21
21
22
22
static int fd_stdout [2 ];
23
23
static int fd_stderr [2 ];
24
- static int fd_ioctrl [2 ];
25
24
26
25
int fpm_stdio_init_main () /* {{{ */
27
26
{
@@ -107,36 +106,11 @@ int fpm_stdio_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
107
106
}
108
107
/* }}} */
109
108
110
- #define FPM_STDIO_CTRL_FLUSH "f "
109
+ #define FPM_STDIO_CMD_FLUSH "\0fscf "
111
110
112
111
int fpm_stdio_flush_child () /* {{{ */
113
112
{
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
- }
113
+ return write (STDERR_FILENO , FPM_STDIO_CMD_FLUSH , sizeof (FPM_STDIO_CMD_FLUSH ));
140
114
}
141
115
/* }}} */
142
116
@@ -150,7 +124,7 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
150
124
struct fpm_event_s * event ;
151
125
int fifo_in = 1 , fifo_out = 1 ;
152
126
int in_buf = 0 ;
153
- int read_fail = 0 , create_log_stream ;
127
+ int read_fail = 0 , finish_log_stream = 0 , create_log_stream ;
154
128
int res ;
155
129
struct zlog_stream * log_stream ;
156
130
@@ -201,6 +175,21 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
201
175
}
202
176
} else {
203
177
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
+ }
204
193
}
205
194
}
206
195
@@ -248,6 +237,8 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
248
237
close (child -> fd_stderr );
249
238
child -> fd_stderr = -1 ;
250
239
}
240
+ } else if (finish_log_stream ) {
241
+ zlog_stream_finish (log_stream );
251
242
}
252
243
}
253
244
/* }}} */
@@ -270,25 +261,12 @@ int fpm_stdio_prepare_pipes(struct fpm_child_s *child) /* {{{ */
270
261
return -1 ;
271
262
}
272
263
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 )) {
264
+ if (0 > fd_set_blocked (fd_stdout [0 ], 0 ) || 0 > fd_set_blocked (fd_stderr [0 ], 0 )) {
285
265
zlog (ZLOG_SYSERROR , "failed to unblock pipes" );
286
266
close (fd_stdout [0 ]);
287
267
close (fd_stdout [1 ]);
288
268
close (fd_stderr [0 ]);
289
269
close (fd_stderr [1 ]);
290
- close (fd_ioctrl [0 ]);
291
- close (fd_ioctrl [1 ]);
292
270
return -1 ;
293
271
}
294
272
return 0 ;
@@ -306,17 +284,12 @@ int fpm_stdio_parent_use_pipes(struct fpm_child_s *child) /* {{{ */
306
284
307
285
child -> fd_stdout = fd_stdout [0 ];
308
286
child -> fd_stderr = fd_stderr [0 ];
309
- child -> fd_ioctrl = fd_ioctrl [0 ];
310
287
311
288
fpm_event_set (& child -> ev_stdout , child -> fd_stdout , FPM_EV_READ , fpm_stdio_child_said , child );
312
289
fpm_event_add (& child -> ev_stdout , 0 );
313
290
314
291
fpm_event_set (& child -> ev_stderr , child -> fd_stderr , FPM_EV_READ , fpm_stdio_child_said , child );
315
292
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
-
320
293
return 0 ;
321
294
}
322
295
/* }}} */
@@ -329,12 +302,9 @@ int fpm_stdio_discard_pipes(struct fpm_child_s *child) /* {{{ */
329
302
330
303
close (fd_stdout [1 ]);
331
304
close (fd_stderr [1 ]);
332
- close (fd_ioctrl [1 ]);
333
305
334
306
close (fd_stdout [0 ]);
335
307
close (fd_stderr [0 ]);
336
- close (fd_ioctrl [0 ]);
337
-
338
308
return 0 ;
339
309
}
340
310
/* }}} */
@@ -344,11 +314,8 @@ void fpm_stdio_child_use_pipes(struct fpm_child_s *child) /* {{{ */
344
314
if (child -> wp -> config -> catch_workers_output ) {
345
315
dup2 (fd_stdout [1 ], STDOUT_FILENO );
346
316
dup2 (fd_stderr [1 ], STDERR_FILENO );
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 ]);
317
+ close (fd_stdout [0 ]); close (fd_stdout [1 ]);
318
+ close (fd_stderr [0 ]); close (fd_stderr [1 ]);
352
319
} else {
353
320
/* stdout of parent is always /dev/null */
354
321
dup2 (STDOUT_FILENO , STDERR_FILENO );
0 commit comments