From f9d9c5ebd9de319fcea925ea64e5fa1ee910aa24 Mon Sep 17 00:00:00 2001 From: Jakub Zelenka Date: Sun, 5 Aug 2018 18:19:00 +0100 Subject: [PATCH 1/3] Fix FPM logging when log pipe is closed --- sapi/fpm/fpm/fpm_stdio.c | 45 +++++++++++++++++++++----------------- sapi/fpm/fpm/zlog.c | 9 ++++++++ sapi/fpm/tests/logtool.inc | 17 ++++++++++++-- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/sapi/fpm/fpm/fpm_stdio.c b/sapi/fpm/fpm/fpm_stdio.c index 380f9644bd3..88774133b8d 100644 --- a/sapi/fpm/fpm/fpm_stdio.c +++ b/sapi/fpm/fpm/fpm_stdio.c @@ -126,6 +126,7 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg) struct fpm_event_s *event; int fifo_in = 1, fifo_out = 1; int in_buf = 0; + int read_fail = 0; int res; struct zlog_stream stream; @@ -146,7 +147,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg) zlog_stream_set_wrapping(&stream, ZLOG_TRUE); zlog_stream_set_msg_prefix(&stream, "[pool %s] child %d said into %s: ", child->wp->config->name, (int) child->pid, is_stdout ? "stdout" : "stderr"); - zlog_stream_set_msg_suffix(&stream, NULL, ", pipe is closed"); zlog_stream_set_msg_quoting(&stream, ZLOG_TRUE); while (fifo_in || fifo_out) { @@ -154,23 +154,9 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg) res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf); if (res <= 0) { /* no data */ fifo_in = 0; - if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { - /* just no more data ready */ - } else { /* error or pipe is closed */ - - if (res < 0) { /* error */ - zlog(ZLOG_SYSERROR, "unable to read what child say"); - } - - fpm_event_del(event); - - if (is_stdout) { - close(child->fd_stdout); - child->fd_stdout = -1; - } else { - close(child->fd_stderr); - child->fd_stderr = -1; - } + if (res == 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) { + /* pipe is closed or error */ + read_fail = (res < 0) ? res : 1; } } else { in_buf += res; @@ -202,7 +188,26 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg) } } } - zlog_stream_close(&stream); + + if (read_fail) { + zlog_stream_set_msg_suffix(&stream, NULL, ", pipe is closed"); + zlog_stream_close(&stream); + if (read_fail < 0) { + zlog(ZLOG_SYSERROR, "unable to read what child say"); + } + + fpm_event_del(event); + + if (is_stdout) { + close(child->fd_stdout); + child->fd_stdout = -1; + } else { + close(child->fd_stderr); + child->fd_stderr = -1; + } + } else { + zlog_stream_close(&stream); + } } /* }}} */ @@ -292,7 +297,7 @@ int fpm_stdio_open_error_log(int reopen) /* {{{ */ #ifdef HAVE_SYSLOG_H if (!strcasecmp(fpm_global_config.error_log, "syslog")) { - php_openlog(fpm_global_config.syslog_ident, LOG_PID | LOG_CONS, fpm_global_config.syslog_facility); + openlog(fpm_global_config.syslog_ident, LOG_PID | LOG_CONS, fpm_global_config.syslog_facility); fpm_globals.error_log_fd = ZLOG_SYSLOG; if (fpm_use_error_log()) { zlog_set_fd(fpm_globals.error_log_fd); diff --git a/sapi/fpm/fpm/zlog.c b/sapi/fpm/fpm/zlog.c index 0fb6b955cb8..e7e07cbc9c8 100644 --- a/sapi/fpm/fpm/zlog.c +++ b/sapi/fpm/fpm/zlog.c @@ -633,6 +633,9 @@ zlog_bool zlog_stream_set_msg_suffix( stream->msg_suffix_len = strlen(suffix); stream->msg_final_suffix_len = strlen(final_suffix); len = stream->msg_suffix_len + stream->msg_final_suffix_len + 2; + if (stream->msg_suffix != NULL) { + free(stream->msg_suffix); + } stream->msg_suffix = malloc(len); if (stream->msg_suffix == NULL) { return ZLOG_FALSE; @@ -646,6 +649,9 @@ zlog_bool zlog_stream_set_msg_suffix( stream->msg_suffix_len = strlen(suffix); len = stream->msg_suffix_len + 1; stream->msg_suffix = malloc(len); + if (stream->msg_suffix != NULL) { + free(stream->msg_suffix); + } if (stream->msg_suffix == NULL) { return ZLOG_FALSE; } @@ -656,6 +662,9 @@ zlog_bool zlog_stream_set_msg_suffix( stream->msg_final_suffix_len = strlen(final_suffix); len = stream->msg_final_suffix_len + 1; stream->msg_final_suffix = malloc(len); + if (stream->msg_final_suffix != NULL) { + free(stream->msg_suffix); + } if (stream->msg_final_suffix == NULL) { return ZLOG_FALSE; } diff --git a/sapi/fpm/tests/logtool.inc b/sapi/fpm/tests/logtool.inc index 1cbef50a1ab..678f11ca46d 100644 --- a/sapi/fpm/tests/logtool.inc +++ b/sapi/fpm/tests/logtool.inc @@ -43,6 +43,11 @@ class LogTool */ private $error; + /** + * @var bool + */ + private $pipeClosed = false; + /** * @param string $message * @param int $limit @@ -72,6 +77,14 @@ class LogTool return $this->level ?: 'WARNING'; } + /** + * @param bool $pipeClosed + */ + public function setPipeClosed(bool $pipeClosed) + { + $this->pipeClosed = $pipeClosed; + } + /** * @param string $line * @return bool @@ -205,13 +218,13 @@ class LogTool if ($rem !== $outLen) { return $this->error("Printed more than the message len"); } - if ($finalSuffix === null) { + if (!$this->pipeClosed || $finalSuffix === null) { return false; } if ($finalSuffix === false) { return $this->error("No final suffix"); } - if (strpos(self::FINAL_SUFFIX, $finalSuffix) === false) { + if (empty($finalSuffix) || strpos(self::FINAL_SUFFIX, $finalSuffix) === false) { return $this->error("The final suffix has to be equal to ', pipe is closed'"); } if (self::FINAL_SUFFIX !== $finalSuffix) { From 86d472e24917c2588a06af378f90f10c59ba0a88 Mon Sep 17 00:00:00 2001 From: Jakub Zelenka Date: Sun, 5 Aug 2018 18:21:52 +0100 Subject: [PATCH 2/3] Use php_openlog instead of openlog in FPM Fix incorrect port in the previous commit --- sapi/fpm/fpm/fpm_stdio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sapi/fpm/fpm/fpm_stdio.c b/sapi/fpm/fpm/fpm_stdio.c index 88774133b8d..599333e3c4e 100644 --- a/sapi/fpm/fpm/fpm_stdio.c +++ b/sapi/fpm/fpm/fpm_stdio.c @@ -297,7 +297,7 @@ int fpm_stdio_open_error_log(int reopen) /* {{{ */ #ifdef HAVE_SYSLOG_H if (!strcasecmp(fpm_global_config.error_log, "syslog")) { - openlog(fpm_global_config.syslog_ident, LOG_PID | LOG_CONS, fpm_global_config.syslog_facility); + php_openlog(fpm_global_config.syslog_ident, LOG_PID | LOG_CONS, fpm_global_config.syslog_facility); fpm_globals.error_log_fd = ZLOG_SYSLOG; if (fpm_use_error_log()) { zlog_set_fd(fpm_globals.error_log_fd); From 9b462f332eaa0588ced82851efe88c04ca0fe2a8 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 30 Jul 2018 09:43:00 +0100 Subject: [PATCH 3/3] fpm process name, FreeBSD 12.x using new setproctitle_fast --- sapi/fpm/config.m4 | 2 +- sapi/fpm/fpm/fpm_env.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sapi/fpm/config.m4 b/sapi/fpm/config.m4 index 647b64607f0..44d842b285e 100644 --- a/sapi/fpm/config.m4 +++ b/sapi/fpm/config.m4 @@ -6,7 +6,7 @@ PHP_ARG_ENABLE(fpm,, dnl configure checks {{{ AC_DEFUN([AC_FPM_STDLIBS], [ - AC_CHECK_FUNCS(setenv clearenv setproctitle) + AC_CHECK_FUNCS(setenv clearenv setproctitle setproctitle_fast) AC_SEARCH_LIBS(socket, socket) AC_SEARCH_LIBS(inet_addr, nsl) diff --git a/sapi/fpm/fpm/fpm_env.c b/sapi/fpm/fpm/fpm_env.c index b805e97edb7..99c7cc1ba10 100644 --- a/sapi/fpm/fpm/fpm_env.c +++ b/sapi/fpm/fpm/fpm_env.c @@ -119,7 +119,9 @@ static char * nvmatch(char *s1, char *s2) /* {{{ */ void fpm_env_setproctitle(char *title) /* {{{ */ { -#ifdef HAVE_SETPROCTITLE +#if defined(HAVE_SETPROCTITLE_FAST) + setproctitle_fast("%s", title); +#elif defined(HAVE_SETPROCTITLE) setproctitle("%s", title); #else #ifdef __linux__