Skip to content

Commit 263577e

Browse files
committed
Refactor fpm_stdio_child_said
1 parent afcd21f commit 263577e

File tree

2 files changed

+32
-56
lines changed

2 files changed

+32
-56
lines changed

sapi/fpm/fpm/fpm_stdio.c

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
122122
struct fpm_child_s *child;
123123
int is_stdout;
124124
struct fpm_event_s *event;
125-
int fifo_in = 1, fifo_out = 1;
126-
int in_buf = 0;
127-
int read_fail = 0, finish_log_stream = 0, create_log_stream;
128-
int res;
125+
int in_buf = 0, pos, start;
126+
int read_fail = 0, create_log_stream;
129127
struct zlog_stream *log_stream;
130128

131129
if (!arg) {
@@ -164,59 +162,37 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
164162
}
165163
}
166164

167-
while (fifo_in || fifo_out) {
168-
if (fifo_in) {
169-
res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
170-
if (res <= 0) { /* no data */
171-
fifo_in = 0;
172-
if (res == 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
173-
/* pipe is closed or error */
174-
read_fail = (res < 0) ? res : 1;
175-
}
176-
} else {
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-
}
165+
while (1) {
166+
in_buf = read(fd, buf, max_buf_size - 1);
167+
if (in_buf <= 0) { /* no data */
168+
if (in_buf == 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
169+
/* pipe is closed or error */
170+
read_fail = (in_buf < 0) ? in_buf : 1;
193171
}
172+
break;
194173
}
195174

196-
if (fifo_out) {
197-
if (in_buf == 0) {
198-
fifo_out = 0;
199-
} else {
200-
char *nl;
201-
202-
nl = memchr(buf, '\n', in_buf);
203-
if (nl) {
204-
/* we should print each new line int the new message */
205-
int out_len = nl - buf;
206-
zlog_stream_str(log_stream, buf, out_len);
175+
for (start = 0, pos = 0; pos < in_buf; pos++) {
176+
switch (buf[pos]) {
177+
case '\n':
178+
zlog_stream_str(log_stream, buf + start, pos - start);
207179
zlog_stream_finish(log_stream);
208-
/* skip new line */
209-
out_len++;
210-
/* move data in the buffer */
211-
memmove(buf, buf + out_len, in_buf - out_len);
212-
in_buf -= out_len;
213-
} else if (in_buf == max_buf_size - 1 || !fifo_in) {
214-
/* we should print if no more space in the buffer or no more data to come */
215-
zlog_stream_str(log_stream, buf, in_buf);
216-
in_buf = 0;
217-
}
180+
start = pos + 1;
181+
break;
182+
case '\0':
183+
if (pos + sizeof(FPM_STDIO_CMD_FLUSH) <= in_buf &&
184+
!memcmp(buf + pos, FPM_STDIO_CMD_FLUSH, sizeof(FPM_STDIO_CMD_FLUSH))) {
185+
zlog_stream_str(log_stream, buf + start, pos - start);
186+
zlog_stream_finish(log_stream);
187+
start = pos + sizeof(FPM_STDIO_CMD_FLUSH);
188+
pos = start - 1;
189+
}
190+
break;
218191
}
219192
}
193+
if (start < pos) {
194+
zlog_stream_str(log_stream, buf + start, pos - start);
195+
}
220196
}
221197

222198
if (read_fail) {
@@ -237,8 +213,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
237213
close(child->fd_stderr);
238214
child->fd_stderr = -1;
239215
}
240-
} else if (finish_log_stream) {
241-
zlog_stream_finish(log_stream);
242216
}
243217
}
244218
/* }}} */

sapi/fpm/fpm/zlog.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,14 +734,16 @@ ssize_t zlog_stream_format(struct zlog_stream *stream, const char *fmt, ...) /*
734734

735735
ssize_t zlog_stream_str(struct zlog_stream *stream, const char *str, size_t str_len) /* {{{ */
736736
{
737+
/* do not write anything if the stream is full or str is empty */
738+
if (str_len == 0 || stream->full) {
739+
return 0;
740+
}
741+
737742
/* reset stream if it is finished */
738743
if (stream->finished) {
739744
stream->finished = 0;
740745
stream->len = 0;
741746
stream->full = 0;
742-
} else if (stream->full) {
743-
/* do not write anything if the stream is full */
744-
return 0;
745747
}
746748

747749
if (stream->use_buffer) {

0 commit comments

Comments
 (0)