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
@@ -164,21 +190,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
164
190
}
165
191
} else {
166
192
in_buf += res ;
167
- /* check if buffer should be flushed */
168
- if (!buf [in_buf - 1 ] && in_buf >= sizeof (FPM_STDIO_CMD_FLUSH ) &&
169
- !memcmp (buf + in_buf - sizeof (FPM_STDIO_CMD_FLUSH ),
170
- FPM_STDIO_CMD_FLUSH , sizeof (FPM_STDIO_CMD_FLUSH ))) {
171
- /* if buffer ends with flush cmd, then the stream will be finished */
172
- finish_log_stream = 1 ;
173
- in_buf -= sizeof (FPM_STDIO_CMD_FLUSH );
174
- } else if (!buf [0 ] && in_buf > sizeof (FPM_STDIO_CMD_FLUSH ) &&
175
- !memcmp (buf , FPM_STDIO_CMD_FLUSH , sizeof (FPM_STDIO_CMD_FLUSH ))) {
176
- /* if buffer starts with flush cmd, then the stream will be finished */
177
- finish_log_stream = 1 ;
178
- in_buf -= sizeof (FPM_STDIO_CMD_FLUSH );
179
- /* move data behind the flush cmd */
180
- memmove (buf , buf + sizeof (FPM_STDIO_CMD_FLUSH ), in_buf );
181
- }
182
193
}
183
194
}
184
195
@@ -226,8 +237,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
226
237
close (child -> fd_stderr );
227
238
child -> fd_stderr = -1 ;
228
239
}
229
- } else if (finish_log_stream ) {
230
- zlog_stream_finish (log_stream );
231
240
}
232
241
}
233
242
/* }}} */
@@ -250,12 +259,25 @@ int fpm_stdio_prepare_pipes(struct fpm_child_s *child) /* {{{ */
250
259
return -1 ;
251
260
}
252
261
253
- if (0 > fd_set_blocked (fd_stdout [0 ], 0 ) || 0 > fd_set_blocked (fd_stderr [0 ], 0 )) {
262
+ if (0 > pipe (fd_ioctrl )) {
263
+ zlog (ZLOG_SYSERROR , "failed to prepare the IO control pipe" );
264
+ close (fd_stdout [0 ]);
265
+ close (fd_stdout [1 ]);
266
+ close (fd_stderr [0 ]);
267
+ close (fd_stderr [1 ]);
268
+ return -1 ;
269
+ }
270
+
271
+ if (0 > fd_set_blocked (fd_stdout [0 ], 0 ) ||
272
+ 0 > fd_set_blocked (fd_stderr [0 ], 0 ) ||
273
+ 0 > fd_set_blocked (fd_ioctrl [0 ], 0 )) {
254
274
zlog (ZLOG_SYSERROR , "failed to unblock pipes" );
255
275
close (fd_stdout [0 ]);
256
276
close (fd_stdout [1 ]);
257
277
close (fd_stderr [0 ]);
258
278
close (fd_stderr [1 ]);
279
+ close (fd_ioctrl [0 ]);
280
+ close (fd_ioctrl [1 ]);
259
281
return -1 ;
260
282
}
261
283
return 0 ;
@@ -273,12 +295,17 @@ int fpm_stdio_parent_use_pipes(struct fpm_child_s *child) /* {{{ */
273
295
274
296
child -> fd_stdout = fd_stdout [0 ];
275
297
child -> fd_stderr = fd_stderr [0 ];
298
+ child -> fd_ioctrl = fd_ioctrl [0 ];
276
299
277
300
fpm_event_set (& child -> ev_stdout , child -> fd_stdout , FPM_EV_READ , fpm_stdio_child_said , child );
278
301
fpm_event_add (& child -> ev_stdout , 0 );
279
302
280
303
fpm_event_set (& child -> ev_stderr , child -> fd_stderr , FPM_EV_READ , fpm_stdio_child_said , child );
281
304
fpm_event_add (& child -> ev_stderr , 0 );
305
+
306
+ fpm_event_set (& child -> ev_ioctrl , child -> fd_ioctrl , FPM_EV_READ , fpm_stdio_child_ctrl , child );
307
+ fpm_event_add (& child -> ev_ioctrl , 0 );
308
+
282
309
return 0 ;
283
310
}
284
311
/* }}} */
@@ -291,9 +318,12 @@ int fpm_stdio_discard_pipes(struct fpm_child_s *child) /* {{{ */
291
318
292
319
close (fd_stdout [1 ]);
293
320
close (fd_stderr [1 ]);
321
+ close (fd_ioctrl [1 ]);
294
322
295
323
close (fd_stdout [0 ]);
296
324
close (fd_stderr [0 ]);
325
+ close (fd_ioctrl [0 ]);
326
+
297
327
return 0 ;
298
328
}
299
329
/* }}} */
@@ -303,8 +333,11 @@ void fpm_stdio_child_use_pipes(struct fpm_child_s *child) /* {{{ */
303
333
if (child -> wp -> config -> catch_workers_output ) {
304
334
dup2 (fd_stdout [1 ], STDOUT_FILENO );
305
335
dup2 (fd_stderr [1 ], STDERR_FILENO );
306
- close (fd_stdout [0 ]); close (fd_stdout [1 ]);
307
- close (fd_stderr [0 ]); close (fd_stderr [1 ]);
336
+ close (fd_stdout [0 ]);
337
+ close (fd_stdout [1 ]);
338
+ close (fd_stderr [0 ]);
339
+ close (fd_stderr [1 ]);
340
+ close (fd_ioctrl [0 ]);
308
341
} else {
309
342
/* stdout of parent is always /dev/null */
310
343
dup2 (STDOUT_FILENO , STDERR_FILENO );
0 commit comments