Skip to content

Commit f6f37a7

Browse files
committed
Create a new IO ctrl pipe for flushing log stream
1 parent 72c0103 commit f6f37a7

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

@@ -164,21 +190,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
164190
}
165191
} else {
166192
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-
}
182193
}
183194
}
184195

@@ -226,8 +237,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
226237
close(child->fd_stderr);
227238
child->fd_stderr = -1;
228239
}
229-
} else if (finish_log_stream) {
230-
zlog_stream_finish(log_stream);
231240
}
232241
}
233242
/* }}} */
@@ -250,12 +259,25 @@ int fpm_stdio_prepare_pipes(struct fpm_child_s *child) /* {{{ */
250259
return -1;
251260
}
252261

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)) {
254274
zlog(ZLOG_SYSERROR, "failed to unblock pipes");
255275
close(fd_stdout[0]);
256276
close(fd_stdout[1]);
257277
close(fd_stderr[0]);
258278
close(fd_stderr[1]);
279+
close(fd_ioctrl[0]);
280+
close(fd_ioctrl[1]);
259281
return -1;
260282
}
261283
return 0;
@@ -273,12 +295,17 @@ int fpm_stdio_parent_use_pipes(struct fpm_child_s *child) /* {{{ */
273295

274296
child->fd_stdout = fd_stdout[0];
275297
child->fd_stderr = fd_stderr[0];
298+
child->fd_ioctrl = fd_ioctrl[0];
276299

277300
fpm_event_set(&child->ev_stdout, child->fd_stdout, FPM_EV_READ, fpm_stdio_child_said, child);
278301
fpm_event_add(&child->ev_stdout, 0);
279302

280303
fpm_event_set(&child->ev_stderr, child->fd_stderr, FPM_EV_READ, fpm_stdio_child_said, child);
281304
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+
282309
return 0;
283310
}
284311
/* }}} */
@@ -291,9 +318,12 @@ int fpm_stdio_discard_pipes(struct fpm_child_s *child) /* {{{ */
291318

292319
close(fd_stdout[1]);
293320
close(fd_stderr[1]);
321+
close(fd_ioctrl[1]);
294322

295323
close(fd_stdout[0]);
296324
close(fd_stderr[0]);
325+
close(fd_ioctrl[0]);
326+
297327
return 0;
298328
}
299329
/* }}} */
@@ -303,8 +333,11 @@ void fpm_stdio_child_use_pipes(struct fpm_child_s *child) /* {{{ */
303333
if (child->wp->config->catch_workers_output) {
304334
dup2(fd_stdout[1], STDOUT_FILENO);
305335
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]);
308341
} else {
309342
/* stdout of parent is always /dev/null */
310343
dup2(STDOUT_FILENO, STDERR_FILENO);

0 commit comments

Comments
 (0)