Skip to content

Commit afcd21f

Browse files
committed
Revert "Create a new IO ctrl pipe for flushing log stream"
This reverts commit f6f37a7.
1 parent ae38f5e commit afcd21f

File tree

2 files changed

+25
-58
lines changed

2 files changed

+25
-58
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, ev_ioctrl;
25+
struct fpm_event_s ev_stdout, ev_stderr;
2626
int shm_slot_i;
27-
int fd_stdout, fd_stderr, fd_ioctrl;
27+
int fd_stdout, fd_stderr;
2828
void (*tracer)(struct fpm_child_s *);
2929
struct timeval slow_logged;
3030
int idle_kill;

sapi/fpm/fpm/fpm_stdio.c

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

2222
static int fd_stdout[2];
2323
static int fd_stderr[2];
24-
static int fd_ioctrl[2];
2524

2625
int fpm_stdio_init_main() /* {{{ */
2726
{
@@ -107,36 +106,11 @@ int fpm_stdio_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
107106
}
108107
/* }}} */
109108

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

112111
int fpm_stdio_flush_child() /* {{{ */
113112
{
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));
140114
}
141115
/* }}} */
142116

@@ -150,7 +124,7 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
150124
struct fpm_event_s *event;
151125
int fifo_in = 1, fifo_out = 1;
152126
int in_buf = 0;
153-
int read_fail = 0, create_log_stream;
127+
int read_fail = 0, finish_log_stream = 0, create_log_stream;
154128
int res;
155129
struct zlog_stream *log_stream;
156130

@@ -201,6 +175,21 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
201175
}
202176
} else {
203177
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+
}
204193
}
205194
}
206195

@@ -248,6 +237,8 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
248237
close(child->fd_stderr);
249238
child->fd_stderr = -1;
250239
}
240+
} else if (finish_log_stream) {
241+
zlog_stream_finish(log_stream);
251242
}
252243
}
253244
/* }}} */
@@ -270,25 +261,12 @@ int fpm_stdio_prepare_pipes(struct fpm_child_s *child) /* {{{ */
270261
return -1;
271262
}
272263

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)) {
285265
zlog(ZLOG_SYSERROR, "failed to unblock pipes");
286266
close(fd_stdout[0]);
287267
close(fd_stdout[1]);
288268
close(fd_stderr[0]);
289269
close(fd_stderr[1]);
290-
close(fd_ioctrl[0]);
291-
close(fd_ioctrl[1]);
292270
return -1;
293271
}
294272
return 0;
@@ -306,17 +284,12 @@ int fpm_stdio_parent_use_pipes(struct fpm_child_s *child) /* {{{ */
306284

307285
child->fd_stdout = fd_stdout[0];
308286
child->fd_stderr = fd_stderr[0];
309-
child->fd_ioctrl = fd_ioctrl[0];
310287

311288
fpm_event_set(&child->ev_stdout, child->fd_stdout, FPM_EV_READ, fpm_stdio_child_said, child);
312289
fpm_event_add(&child->ev_stdout, 0);
313290

314291
fpm_event_set(&child->ev_stderr, child->fd_stderr, FPM_EV_READ, fpm_stdio_child_said, child);
315292
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-
320293
return 0;
321294
}
322295
/* }}} */
@@ -329,12 +302,9 @@ int fpm_stdio_discard_pipes(struct fpm_child_s *child) /* {{{ */
329302

330303
close(fd_stdout[1]);
331304
close(fd_stderr[1]);
332-
close(fd_ioctrl[1]);
333305

334306
close(fd_stdout[0]);
335307
close(fd_stderr[0]);
336-
close(fd_ioctrl[0]);
337-
338308
return 0;
339309
}
340310
/* }}} */
@@ -344,11 +314,8 @@ void fpm_stdio_child_use_pipes(struct fpm_child_s *child) /* {{{ */
344314
if (child->wp->config->catch_workers_output) {
345315
dup2(fd_stdout[1], STDOUT_FILENO);
346316
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]);
352319
} else {
353320
/* stdout of parent is always /dev/null */
354321
dup2(STDOUT_FILENO, STDERR_FILENO);

0 commit comments

Comments
 (0)