Skip to content

Commit 2a67d62

Browse files
committed
Fix FPM logging when log pipe is closed
1 parent 37245e6 commit 2a67d62

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

sapi/fpm/fpm/fpm_stdio.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
126126
struct fpm_event_s *event;
127127
int fifo_in = 1, fifo_out = 1;
128128
int in_buf = 0;
129+
int read_fail = 0;
129130
int res;
130131
struct zlog_stream stream;
131132

@@ -146,31 +147,16 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
146147
zlog_stream_set_wrapping(&stream, ZLOG_TRUE);
147148
zlog_stream_set_msg_prefix(&stream, "[pool %s] child %d said into %s: ",
148149
child->wp->config->name, (int) child->pid, is_stdout ? "stdout" : "stderr");
149-
zlog_stream_set_msg_suffix(&stream, NULL, ", pipe is closed");
150150
zlog_stream_set_msg_quoting(&stream, ZLOG_TRUE);
151151

152152
while (fifo_in || fifo_out) {
153153
if (fifo_in) {
154154
res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
155155
if (res <= 0) { /* no data */
156156
fifo_in = 0;
157-
if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
158-
/* just no more data ready */
159-
} else { /* error or pipe is closed */
160-
161-
if (res < 0) { /* error */
162-
zlog(ZLOG_SYSERROR, "unable to read what child say");
163-
}
164-
165-
fpm_event_del(event);
166-
167-
if (is_stdout) {
168-
close(child->fd_stdout);
169-
child->fd_stdout = -1;
170-
} else {
171-
close(child->fd_stderr);
172-
child->fd_stderr = -1;
173-
}
157+
if (res == 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
158+
/* pipe is closed or error */
159+
read_fail = (res < 0) ? res : 1;
174160
}
175161
} else {
176162
in_buf += res;
@@ -202,7 +188,26 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
202188
}
203189
}
204190
}
205-
zlog_stream_close(&stream);
191+
192+
if (read_fail) {
193+
zlog_stream_set_msg_suffix(&stream, NULL, ", pipe is closed");
194+
zlog_stream_close(&stream);
195+
if (read_fail < 0) {
196+
zlog(ZLOG_SYSERROR, "unable to read what child say");
197+
}
198+
199+
fpm_event_del(event);
200+
201+
if (is_stdout) {
202+
close(child->fd_stdout);
203+
child->fd_stdout = -1;
204+
} else {
205+
close(child->fd_stderr);
206+
child->fd_stderr = -1;
207+
}
208+
} else {
209+
zlog_stream_close(&stream);
210+
}
206211
}
207212
/* }}} */
208213

@@ -292,7 +297,7 @@ int fpm_stdio_open_error_log(int reopen) /* {{{ */
292297

293298
#ifdef HAVE_SYSLOG_H
294299
if (!strcasecmp(fpm_global_config.error_log, "syslog")) {
295-
php_openlog(fpm_global_config.syslog_ident, LOG_PID | LOG_CONS, fpm_global_config.syslog_facility);
300+
openlog(fpm_global_config.syslog_ident, LOG_PID | LOG_CONS, fpm_global_config.syslog_facility);
296301
fpm_globals.error_log_fd = ZLOG_SYSLOG;
297302
if (fpm_use_error_log()) {
298303
zlog_set_fd(fpm_globals.error_log_fd);

sapi/fpm/fpm/zlog.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,9 @@ zlog_bool zlog_stream_set_msg_suffix(
633633
stream->msg_suffix_len = strlen(suffix);
634634
stream->msg_final_suffix_len = strlen(final_suffix);
635635
len = stream->msg_suffix_len + stream->msg_final_suffix_len + 2;
636+
if (stream->msg_suffix != NULL) {
637+
free(stream->msg_suffix);
638+
}
636639
stream->msg_suffix = malloc(len);
637640
if (stream->msg_suffix == NULL) {
638641
return ZLOG_FALSE;
@@ -646,6 +649,9 @@ zlog_bool zlog_stream_set_msg_suffix(
646649
stream->msg_suffix_len = strlen(suffix);
647650
len = stream->msg_suffix_len + 1;
648651
stream->msg_suffix = malloc(len);
652+
if (stream->msg_suffix != NULL) {
653+
free(stream->msg_suffix);
654+
}
649655
if (stream->msg_suffix == NULL) {
650656
return ZLOG_FALSE;
651657
}
@@ -656,6 +662,9 @@ zlog_bool zlog_stream_set_msg_suffix(
656662
stream->msg_final_suffix_len = strlen(final_suffix);
657663
len = stream->msg_final_suffix_len + 1;
658664
stream->msg_final_suffix = malloc(len);
665+
if (stream->msg_final_suffix != NULL) {
666+
free(stream->msg_suffix);
667+
}
659668
if (stream->msg_final_suffix == NULL) {
660669
return ZLOG_FALSE;
661670
}

sapi/fpm/tests/logtool.inc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class LogTool
4343
*/
4444
private $error;
4545

46+
/**
47+
* @var bool
48+
*/
49+
private $pipeClosed = false;
50+
4651
/**
4752
* @param string $message
4853
* @param int $limit
@@ -72,6 +77,14 @@ class LogTool
7277
return $this->level ?: 'WARNING';
7378
}
7479

80+
/**
81+
* @param bool $pipeClosed
82+
*/
83+
public function setPipeClosed(bool $pipeClosed)
84+
{
85+
$this->pipeClosed = $pipeClosed;
86+
}
87+
7588
/**
7689
* @param string $line
7790
* @return bool
@@ -205,13 +218,13 @@ class LogTool
205218
if ($rem !== $outLen) {
206219
return $this->error("Printed more than the message len");
207220
}
208-
if ($finalSuffix === null) {
221+
if (!$this->pipeClosed || $finalSuffix === null) {
209222
return false;
210223
}
211224
if ($finalSuffix === false) {
212225
return $this->error("No final suffix");
213226
}
214-
if (strpos(self::FINAL_SUFFIX, $finalSuffix) === false) {
227+
if (empty($finalSuffix) || strpos(self::FINAL_SUFFIX, $finalSuffix) === false) {
215228
return $this->error("The final suffix has to be equal to ', pipe is closed'");
216229
}
217230
if (self::FINAL_SUFFIX !== $finalSuffix) {

0 commit comments

Comments
 (0)