Skip to content

Commit ae38f5e

Browse files
committed
Create a new IO ctrl pipe for flushing log stream
1 parent 47397a4 commit ae38f5e

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

sapi/fpm/fpm/fpm_children.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ struct fpm_child_s {
2222
struct fpm_child_s *prev, *next;
2323
struct timeval started;
2424
struct fpm_worker_pool_s *wp;
25-
struct fpm_event_s ev_stdout, ev_stderr;
25+
struct fpm_event_s ev_stdout, ev_stderr, ev_ioctrl;
2626
int shm_slot_i;
27-
int fd_stdout, fd_stderr;
27+
int fd_stdout, fd_stderr, fd_ioctrl;
2828
void (*tracer)(struct fpm_child_s *);
2929
struct timeval slow_logged;
3030
int idle_kill;

sapi/fpm/fpm/fpm_stdio.c

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
static int fd_stdout[2];
2323
static int fd_stderr[2];
24+
static int fd_ioctrl[2];
2425

2526
int fpm_stdio_init_main() /* {{{ */
2627
{
@@ -106,11 +107,36 @@ int fpm_stdio_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
106107
}
107108
/* }}} */
108109

109-
#define FPM_STDIO_CMD_FLUSH "\0fscf"
110+
#define FPM_STDIO_CTRL_FLUSH "f"
110111

111112
int fpm_stdio_flush_child() /* {{{ */
112113
{
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+
}
114140
}
115141
/* }}} */
116142

@@ -124,7 +150,7 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
124150
struct fpm_event_s *event;
125151
int fifo_in = 1, fifo_out = 1;
126152
int in_buf = 0;
127-
int read_fail = 0, finish_log_stream = 0, create_log_stream;
153+
int read_fail = 0, create_log_stream;
128154
int res;
129155
struct zlog_stream *log_stream;
130156

@@ -175,21 +201,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
175201
}
176202
} else {
177203
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-
}
193204
}
194205
}
195206

@@ -237,8 +248,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
237248
close(child->fd_stderr);
238249
child->fd_stderr = -1;
239250
}
240-
} else if (finish_log_stream) {
241-
zlog_stream_finish(log_stream);
242251
}
243252
}
244253
/* }}} */
@@ -261,12 +270,25 @@ int fpm_stdio_prepare_pipes(struct fpm_child_s *child) /* {{{ */
261270
return -1;
262271
}
263272

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)) {
265285
zlog(ZLOG_SYSERROR, "failed to unblock pipes");
266286
close(fd_stdout[0]);
267287
close(fd_stdout[1]);
268288
close(fd_stderr[0]);
269289
close(fd_stderr[1]);
290+
close(fd_ioctrl[0]);
291+
close(fd_ioctrl[1]);
270292
return -1;
271293
}
272294
return 0;
@@ -284,12 +306,17 @@ int fpm_stdio_parent_use_pipes(struct fpm_child_s *child) /* {{{ */
284306

285307
child->fd_stdout = fd_stdout[0];
286308
child->fd_stderr = fd_stderr[0];
309+
child->fd_ioctrl = fd_ioctrl[0];
287310

288311
fpm_event_set(&child->ev_stdout, child->fd_stdout, FPM_EV_READ, fpm_stdio_child_said, child);
289312
fpm_event_add(&child->ev_stdout, 0);
290313

291314
fpm_event_set(&child->ev_stderr, child->fd_stderr, FPM_EV_READ, fpm_stdio_child_said, child);
292315
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+
293320
return 0;
294321
}
295322
/* }}} */
@@ -302,9 +329,12 @@ int fpm_stdio_discard_pipes(struct fpm_child_s *child) /* {{{ */
302329

303330
close(fd_stdout[1]);
304331
close(fd_stderr[1]);
332+
close(fd_ioctrl[1]);
305333

306334
close(fd_stdout[0]);
307335
close(fd_stderr[0]);
336+
close(fd_ioctrl[0]);
337+
308338
return 0;
309339
}
310340
/* }}} */
@@ -314,8 +344,11 @@ void fpm_stdio_child_use_pipes(struct fpm_child_s *child) /* {{{ */
314344
if (child->wp->config->catch_workers_output) {
315345
dup2(fd_stdout[1], STDOUT_FILENO);
316346
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]);
319352
} else {
320353
/* stdout of parent is always /dev/null */
321354
dup2(STDOUT_FILENO, STDERR_FILENO);

0 commit comments

Comments
 (0)