Skip to content

Commit 30f5ff0

Browse files
committed
Refactore FPM logging
1 parent 591f4f0 commit 30f5ff0

22 files changed

+1472
-90
lines changed

sapi/fpm/fpm/fpm_conf.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ struct fpm_global_config_s fpm_global_config = {
8181
.systemd_watchdog = 0,
8282
.systemd_interval = -1, /* -1 means not set */
8383
#endif
84+
.log_buffering = ZLOG_DEFAULT_BUFFERING,
85+
.log_limit = ZLOG_DEFAULT_LIMIT
8486
};
8587
static struct fpm_worker_pool_s *current_wp = NULL;
8688
static int ini_recursion = 0;
@@ -98,7 +100,9 @@ static struct ini_value_parser_s ini_fpm_global_options[] = {
98100
{ "syslog.ident", &fpm_conf_set_string, GO(syslog_ident) },
99101
{ "syslog.facility", &fpm_conf_set_syslog_facility, GO(syslog_facility) },
100102
#endif
103+
{ "log_buffering", &fpm_conf_set_boolean, GO(log_buffering) },
101104
{ "log_level", &fpm_conf_set_log_level, GO(log_level) },
105+
{ "log_limit", &fpm_conf_set_integer, GO(log_limit) },
102106
{ "emergency_restart_threshold", &fpm_conf_set_integer, GO(emergency_restart_threshold) },
103107
{ "emergency_restart_interval", &fpm_conf_set_time, GO(emergency_restart_interval) },
104108
{ "process_control_timeout", &fpm_conf_set_time, GO(process_control_timeout) },
@@ -154,6 +158,7 @@ static struct ini_value_parser_s ini_fpm_pool_options[] = {
154158
{ "chroot", &fpm_conf_set_string, WPO(chroot) },
155159
{ "chdir", &fpm_conf_set_string, WPO(chdir) },
156160
{ "catch_workers_output", &fpm_conf_set_boolean, WPO(catch_workers_output) },
161+
{ "decorate_workers_output", &fpm_conf_set_boolean, WPO(decorate_workers_output) },
157162
{ "clear_env", &fpm_conf_set_boolean, WPO(clear_env) },
158163
{ "security.limit_extensions", &fpm_conf_set_string, WPO(security_limit_extensions) },
159164
#ifdef HAVE_APPARMOR
@@ -617,6 +622,7 @@ static void *fpm_worker_pool_config_alloc() /* {{{ */
617622
wp->config->process_priority = 64; /* 64 means unset */
618623
wp->config->process_dumpable = 0;
619624
wp->config->clear_env = 1;
625+
wp->config->decorate_workers_output = 1;
620626

621627
if (!fpm_worker_all_pools) {
622628
fpm_worker_all_pools = wp;
@@ -1193,6 +1199,12 @@ static int fpm_conf_post_process(int force_daemon) /* {{{ */
11931199

11941200
fpm_globals.log_level = fpm_global_config.log_level;
11951201
zlog_set_level(fpm_globals.log_level);
1202+
if (fpm_global_config.log_limit < ZLOG_MIN_LIMIT) {
1203+
zlog(ZLOG_ERROR, "log_limit must be greater than %d", ZLOG_MIN_LIMIT);
1204+
return -1;
1205+
}
1206+
zlog_set_limit(fpm_global_config.log_limit);
1207+
zlog_set_buffering(fpm_global_config.log_buffering);
11961208

11971209
if (fpm_global_config.process_max < 0) {
11981210
zlog(ZLOG_ERROR, "process_max can't be negative");
@@ -1266,6 +1278,7 @@ static void fpm_conf_cleanup(int which, void *arg) /* {{{ */
12661278
free(fpm_global_config.events_mechanism);
12671279
fpm_global_config.pid_file = 0;
12681280
fpm_global_config.error_log = 0;
1281+
fpm_global_config.log_limit = ZLOG_DEFAULT_LIMIT;
12691282
#ifdef HAVE_SYSLOG_H
12701283
free(fpm_global_config.syslog_ident);
12711284
fpm_global_config.syslog_ident = 0;
@@ -1607,7 +1620,9 @@ static void fpm_conf_dump() /* {{{ */
16071620
zlog(ZLOG_NOTICE, "\tsyslog.ident = %s", STR2STR(fpm_global_config.syslog_ident));
16081621
zlog(ZLOG_NOTICE, "\tsyslog.facility = %d", fpm_global_config.syslog_facility); /* FIXME: convert to string */
16091622
#endif
1623+
zlog(ZLOG_NOTICE, "\tlog_buffering = %s", BOOL2STR(fpm_global_config.log_buffering));
16101624
zlog(ZLOG_NOTICE, "\tlog_level = %s", zlog_get_level_name(fpm_globals.log_level));
1625+
zlog(ZLOG_NOTICE, "\tlog_limit = %d", fpm_global_config.log_limit);
16111626
zlog(ZLOG_NOTICE, "\temergency_restart_interval = %ds", fpm_global_config.emergency_restart_interval);
16121627
zlog(ZLOG_NOTICE, "\temergency_restart_threshold = %d", fpm_global_config.emergency_restart_threshold);
16131628
zlog(ZLOG_NOTICE, "\tprocess_control_timeout = %ds", fpm_global_config.process_control_timeout);
@@ -1670,6 +1685,7 @@ static void fpm_conf_dump() /* {{{ */
16701685
zlog(ZLOG_NOTICE, "\tchroot = %s", STR2STR(wp->config->chroot));
16711686
zlog(ZLOG_NOTICE, "\tchdir = %s", STR2STR(wp->config->chdir));
16721687
zlog(ZLOG_NOTICE, "\tcatch_workers_output = %s", BOOL2STR(wp->config->catch_workers_output));
1688+
zlog(ZLOG_NOTICE, "\tdecorate_workers_output = %s", BOOL2STR(wp->config->decorate_workers_output));
16731689
zlog(ZLOG_NOTICE, "\tclear_env = %s", BOOL2STR(wp->config->clear_env));
16741690
zlog(ZLOG_NOTICE, "\tsecurity.limit_extensions = %s", wp->config->security_limit_extensions);
16751691

sapi/fpm/fpm/fpm_conf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct fpm_global_config_s {
3131
int syslog_facility;
3232
#endif
3333
int log_level;
34+
int log_limit;
35+
int log_buffering;
3436
int emergency_restart_threshold;
3537
int emergency_restart_interval;
3638
int process_control_timeout;
@@ -86,6 +88,7 @@ struct fpm_worker_pool_config_s {
8688
char *chroot;
8789
char *chdir;
8890
int catch_workers_output;
91+
int decorate_workers_output;
8992
int clear_env;
9093
char *security_limit_extensions;
9194
struct key_value_s *env;

sapi/fpm/fpm/fpm_main.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -639,21 +639,15 @@ void sapi_cgi_log_fastcgi(int level, char *message, size_t len)
639639

640640
fcgi_request *request = (fcgi_request*) SG(server_context);
641641

642-
/* ensure we want:
643-
* - to log (fastcgi.logging in php.ini)
642+
/* message is written to FCGI_STDERR if following conditions are met:
643+
* - logging is enabled (fastcgi.logging in php.ini)
644644
* - we are currently dealing with a request
645645
* - the message is not empty
646+
* - the fcgi_write did not fail
646647
*/
647-
if (CGIG(fcgi_logging) && request && message && len > 0) {
648-
ssize_t ret;
649-
char *buf = malloc(len + 2);
650-
memcpy(buf, message, len);
651-
memcpy(buf + len, "\n", sizeof("\n"));
652-
ret = fcgi_write(request, FCGI_STDERR, buf, len + 1);
653-
free(buf);
654-
if (ret < 0) {
655-
php_handle_aborted_connection();
656-
}
648+
if (CGIG(fcgi_logging) && request && message && len > 0
649+
&& fcgi_write(request, FCGI_STDERR, message, len) < 0) {
650+
php_handle_aborted_connection();
657651
}
658652
}
659653
/* }}} */
@@ -662,7 +656,7 @@ void sapi_cgi_log_fastcgi(int level, char *message, size_t len)
662656
*/
663657
static void sapi_cgi_log_message(char *message, int syslog_type_int)
664658
{
665-
zlog(ZLOG_NOTICE, "PHP message: %s", message);
659+
zlog_msg(ZLOG_NOTICE, "PHP message: ", message);
666660
}
667661
/* }}} */
668662

sapi/fpm/fpm/fpm_stdio.c

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "fpm.h"
1717
#include "fpm_children.h"
18+
#include "fpm_cleanup.h"
1819
#include "fpm_events.h"
1920
#include "fpm_sockets.h"
2021
#include "fpm_stdio.h"
@@ -23,6 +24,12 @@
2324
static int fd_stdout[2];
2425
static int fd_stderr[2];
2526

27+
static void fpm_stdio_cleanup(int which, void *arg) /* {{{ */
28+
{
29+
zlog_cleanup();
30+
}
31+
/* }}} */
32+
2633
int fpm_stdio_init_main() /* {{{ */
2734
{
2835
int fd = open("/dev/null", O_RDWR);
@@ -31,6 +38,9 @@ int fpm_stdio_init_main() /* {{{ */
3138
zlog(ZLOG_SYSERROR, "failed to init stdio: open(\"/dev/null\")");
3239
return -1;
3340
}
41+
if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT, fpm_stdio_cleanup, 0)) {
42+
return -1;
43+
}
3444

3545
if (0 > dup2(fd, STDIN_FILENO) || 0 > dup2(fd, STDOUT_FILENO)) {
3646
zlog(ZLOG_SYSERROR, "failed to init stdio: dup2()");
@@ -122,21 +132,30 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
122132
int is_stdout;
123133
struct fpm_event_s *event;
124134
int fifo_in = 1, fifo_out = 1;
125-
int is_last_message = 0;
126135
int in_buf = 0;
127136
int res;
137+
struct zlog_stream stream;
128138

129139
if (!arg) {
130140
return;
131141
}
132142
child = (struct fpm_child_s *)arg;
143+
133144
is_stdout = (fd == child->fd_stdout);
134145
if (is_stdout) {
135146
event = &child->ev_stdout;
136147
} else {
137148
event = &child->ev_stderr;
138149
}
139150

151+
zlog_stream_init_ex(&stream, ZLOG_WARNING, STDERR_FILENO);
152+
zlog_stream_set_decorating(&stream, child->wp->config->decorate_workers_output);
153+
zlog_stream_set_wrapping(&stream, ZLOG_TRUE);
154+
zlog_stream_set_msg_prefix(&stream, "[pool %s] child %d said into %s: ",
155+
child->wp->config->name, (int) child->pid, is_stdout ? "stdout" : "stderr");
156+
zlog_stream_set_msg_suffix(&stream, NULL, ", pipe is closed");
157+
zlog_stream_set_msg_quoting(&stream, ZLOG_TRUE);
158+
140159
while (fifo_in || fifo_out) {
141160
if (fifo_in) {
142161
res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
@@ -151,7 +170,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
151170
}
152171

153172
fpm_event_del(event);
154-
is_last_message = 1;
155173

156174
if (is_stdout) {
157175
close(child->fd_stdout);
@@ -171,42 +189,27 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
171189
fifo_out = 0;
172190
} else {
173191
char *nl;
174-
int should_print = 0;
175-
buf[in_buf] = '\0';
176192

177-
/* FIXME: there might be binary data */
178-
179-
/* we should print if no more space in the buffer */
180-
if (in_buf == max_buf_size - 1) {
181-
should_print = 1;
182-
}
183-
184-
/* we should print if no more data to come */
185-
if (!fifo_in) {
186-
should_print = 1;
187-
}
188-
189-
nl = strchr(buf, '\n');
190-
if (nl || should_print) {
191-
192-
if (nl) {
193-
*nl = '\0';
194-
}
195-
196-
zlog(ZLOG_WARNING, "[pool %s] child %d said into %s: \"%s\"%s", child->wp->config->name,
197-
(int) child->pid, is_stdout ? "stdout" : "stderr", buf, is_last_message ? ", pipe is closed" : "");
198-
199-
if (nl) {
200-
int out_buf = 1 + nl - buf;
201-
memmove(buf, buf + out_buf, in_buf - out_buf);
202-
in_buf -= out_buf;
203-
} else {
204-
in_buf = 0;
205-
}
193+
nl = memchr(buf, '\n', in_buf);
194+
if (nl) {
195+
/* we should print each new line int the new message */
196+
int out_len = nl - buf;
197+
zlog_stream_str(&stream, buf, out_len);
198+
zlog_stream_finish(&stream);
199+
/* skip new line */
200+
out_len++;
201+
/* move data in the buffer */
202+
memmove(buf, buf + out_len, in_buf - out_len);
203+
in_buf -= out_len;
204+
} else if (in_buf == max_buf_size - 1 || !fifo_in) {
205+
/* we should print if no more space in the buffer or no more data to come */
206+
zlog_stream_str(&stream, buf, in_buf);
207+
in_buf = 0;
206208
}
207209
}
208210
}
209211
}
212+
zlog_stream_close(&stream);
210213
}
211214
/* }}} */
212215

0 commit comments

Comments
 (0)