Skip to content

Commit 84c8444

Browse files
committed
Refactor fpm_stdio_child_said
1 parent 503380e commit 84c8444

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) {
@@ -153,59 +151,37 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
153151
log_stream = child->log_stream;
154152
}
155153

156-
while (fifo_in || fifo_out) {
157-
if (fifo_in) {
158-
res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
159-
if (res <= 0) { /* no data */
160-
fifo_in = 0;
161-
if (res == 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
162-
/* pipe is closed or error */
163-
read_fail = (res < 0) ? res : 1;
164-
}
165-
} else {
166-
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-
}
154+
while (1) {
155+
in_buf = read(fd, buf, max_buf_size - 1);
156+
if (in_buf <= 0) { /* no data */
157+
if (in_buf == 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
158+
/* pipe is closed or error */
159+
read_fail = (in_buf < 0) ? in_buf : 1;
182160
}
161+
break;
183162
}
184163

185-
if (fifo_out) {
186-
if (in_buf == 0) {
187-
fifo_out = 0;
188-
} else {
189-
char *nl;
190-
191-
nl = memchr(buf, '\n', in_buf);
192-
if (nl) {
193-
/* we should print each new line int the new message */
194-
int out_len = nl - buf;
195-
zlog_stream_str(log_stream, buf, out_len);
164+
for (start = 0, pos = 0; pos < in_buf; pos++) {
165+
switch (buf[pos]) {
166+
case '\n':
167+
zlog_stream_str(log_stream, buf + start, pos - start);
196168
zlog_stream_finish(log_stream);
197-
/* skip new line */
198-
out_len++;
199-
/* move data in the buffer */
200-
memmove(buf, buf + out_len, in_buf - out_len);
201-
in_buf -= out_len;
202-
} else if (in_buf == max_buf_size - 1 || !fifo_in) {
203-
/* we should print if no more space in the buffer or no more data to come */
204-
zlog_stream_str(log_stream, buf, in_buf);
205-
in_buf = 0;
206-
}
169+
start = pos + 1;
170+
break;
171+
case '\0':
172+
if (pos + sizeof(FPM_STDIO_CMD_FLUSH) <= in_buf &&
173+
!memcmp(buf + pos, FPM_STDIO_CMD_FLUSH, sizeof(FPM_STDIO_CMD_FLUSH))) {
174+
zlog_stream_str(log_stream, buf + start, pos - start);
175+
zlog_stream_finish(log_stream);
176+
start = pos + sizeof(FPM_STDIO_CMD_FLUSH);
177+
pos = start - 1;
178+
}
179+
break;
207180
}
208181
}
182+
if (start < pos) {
183+
zlog_stream_str(log_stream, buf + start, pos - start);
184+
}
209185
}
210186

211187
if (read_fail) {
@@ -226,8 +202,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
226202
close(child->fd_stderr);
227203
child->fd_stderr = -1;
228204
}
229-
} else if (finish_log_stream) {
230-
zlog_stream_finish(log_stream);
231205
}
232206
}
233207
/* }}} */

sapi/fpm/fpm/zlog.c

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

721721
ssize_t zlog_stream_str(struct zlog_stream *stream, const char *str, size_t str_len) /* {{{ */
722722
{
723+
/* do not write anything if the stream is full or str is empty */
724+
if (str_len == 0 || stream->full) {
725+
return 0;
726+
}
727+
723728
/* reset stream if it is finished */
724729
if (stream->finished) {
725730
stream->finished = 0;
726731
stream->len = 0;
727732
stream->full = 0;
728-
} else if (stream->full) {
729-
/* do not write anything if the stream is full */
730-
return 0;
731733
}
732734

733735
if (stream->use_buffer) {

0 commit comments

Comments
 (0)