@@ -122,10 +122,8 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
122
122
struct fpm_child_s * child ;
123
123
int is_stdout ;
124
124
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 ;
129
127
struct zlog_stream * log_stream ;
130
128
131
129
if (!arg ) {
@@ -164,59 +162,37 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
164
162
}
165
163
}
166
164
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 ;
193
171
}
172
+ break ;
194
173
}
195
174
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 );
207
179
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 ;
218
191
}
219
192
}
193
+ if (start < pos ) {
194
+ zlog_stream_str (log_stream , buf + start , pos - start );
195
+ }
220
196
}
221
197
222
198
if (read_fail ) {
@@ -237,8 +213,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
237
213
close (child -> fd_stderr );
238
214
child -> fd_stderr = -1 ;
239
215
}
240
- } else if (finish_log_stream ) {
241
- zlog_stream_finish (log_stream );
242
216
}
243
217
}
244
218
/* }}} */
0 commit comments