@@ -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 ) {
@@ -153,59 +151,37 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
153
151
log_stream = child -> log_stream ;
154
152
}
155
153
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 ;
182
160
}
161
+ break ;
183
162
}
184
163
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 );
196
168
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 ;
207
180
}
208
181
}
182
+ if (start < pos ) {
183
+ zlog_stream_str (log_stream , buf + start , pos - start );
184
+ }
209
185
}
210
186
211
187
if (read_fail ) {
@@ -226,8 +202,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
226
202
close (child -> fd_stderr );
227
203
child -> fd_stderr = -1 ;
228
204
}
229
- } else if (finish_log_stream ) {
230
- zlog_stream_finish (log_stream );
231
205
}
232
206
}
233
207
/* }}} */
0 commit comments