Skip to content

Commit 3e5afbf

Browse files
committed
Refactore FPM logging
1 parent 2a78006 commit 3e5afbf

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
@@ -80,6 +80,8 @@ struct fpm_global_config_s fpm_global_config = {
8080
.systemd_watchdog = 0,
8181
.systemd_interval = -1, /* -1 means not set */
8282
#endif
83+
.log_buffering = ZLOG_DEFAULT_BUFFERING,
84+
.log_limit = ZLOG_DEFAULT_LIMIT
8385
};
8486
static struct fpm_worker_pool_s *current_wp = NULL;
8587
static int ini_recursion = 0;
@@ -97,7 +99,9 @@ static struct ini_value_parser_s ini_fpm_global_options[] = {
9799
{ "syslog.ident", &fpm_conf_set_string, GO(syslog_ident) },
98100
{ "syslog.facility", &fpm_conf_set_syslog_facility, GO(syslog_facility) },
99101
#endif
102+
{ "log_buffering", &fpm_conf_set_boolean, GO(log_buffering) },
100103
{ "log_level", &fpm_conf_set_log_level, GO(log_level) },
104+
{ "log_limit", &fpm_conf_set_integer, GO(log_limit) },
101105
{ "emergency_restart_threshold", &fpm_conf_set_integer, GO(emergency_restart_threshold) },
102106
{ "emergency_restart_interval", &fpm_conf_set_time, GO(emergency_restart_interval) },
103107
{ "process_control_timeout", &fpm_conf_set_time, GO(process_control_timeout) },
@@ -153,6 +157,7 @@ static struct ini_value_parser_s ini_fpm_pool_options[] = {
153157
{ "chroot", &fpm_conf_set_string, WPO(chroot) },
154158
{ "chdir", &fpm_conf_set_string, WPO(chdir) },
155159
{ "catch_workers_output", &fpm_conf_set_boolean, WPO(catch_workers_output) },
160+
{ "decorate_workers_output", &fpm_conf_set_boolean, WPO(decorate_workers_output) },
156161
{ "clear_env", &fpm_conf_set_boolean, WPO(clear_env) },
157162
{ "security.limit_extensions", &fpm_conf_set_string, WPO(security_limit_extensions) },
158163
#ifdef HAVE_APPARMOR
@@ -616,6 +621,7 @@ static void *fpm_worker_pool_config_alloc() /* {{{ */
616621
wp->config->process_priority = 64; /* 64 means unset */
617622
wp->config->process_dumpable = 0;
618623
wp->config->clear_env = 1;
624+
wp->config->decorate_workers_output = 1;
619625

620626
if (!fpm_worker_all_pools) {
621627
fpm_worker_all_pools = wp;
@@ -1192,6 +1198,12 @@ static int fpm_conf_post_process(int force_daemon) /* {{{ */
11921198

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

11961208
if (fpm_global_config.process_max < 0) {
11971209
zlog(ZLOG_ERROR, "process_max can't be negative");
@@ -1265,6 +1277,7 @@ static void fpm_conf_cleanup(int which, void *arg) /* {{{ */
12651277
free(fpm_global_config.events_mechanism);
12661278
fpm_global_config.pid_file = 0;
12671279
fpm_global_config.error_log = 0;
1280+
fpm_global_config.log_limit = ZLOG_DEFAULT_LIMIT;
12681281
#ifdef HAVE_SYSLOG_H
12691282
free(fpm_global_config.syslog_ident);
12701283
fpm_global_config.syslog_ident = 0;
@@ -1606,7 +1619,9 @@ static void fpm_conf_dump() /* {{{ */
16061619
zlog(ZLOG_NOTICE, "\tsyslog.ident = %s", STR2STR(fpm_global_config.syslog_ident));
16071620
zlog(ZLOG_NOTICE, "\tsyslog.facility = %d", fpm_global_config.syslog_facility); /* FIXME: convert to string */
16081621
#endif
1622+
zlog(ZLOG_NOTICE, "\tlog_buffering = %s", BOOL2STR(fpm_global_config.log_buffering));
16091623
zlog(ZLOG_NOTICE, "\tlog_level = %s", zlog_get_level_name(fpm_globals.log_level));
1624+
zlog(ZLOG_NOTICE, "\tlog_limit = %d", fpm_global_config.log_limit);
16101625
zlog(ZLOG_NOTICE, "\temergency_restart_interval = %ds", fpm_global_config.emergency_restart_interval);
16111626
zlog(ZLOG_NOTICE, "\temergency_restart_threshold = %d", fpm_global_config.emergency_restart_threshold);
16121627
zlog(ZLOG_NOTICE, "\tprocess_control_timeout = %ds", fpm_global_config.process_control_timeout);
@@ -1669,6 +1684,7 @@ static void fpm_conf_dump() /* {{{ */
16691684
zlog(ZLOG_NOTICE, "\tchroot = %s", STR2STR(wp->config->chroot));
16701685
zlog(ZLOG_NOTICE, "\tchdir = %s", STR2STR(wp->config->chdir));
16711686
zlog(ZLOG_NOTICE, "\tcatch_workers_output = %s", BOOL2STR(wp->config->catch_workers_output));
1687+
zlog(ZLOG_NOTICE, "\tdecorate_workers_output = %s", BOOL2STR(wp->config->decorate_workers_output));
16721688
zlog(ZLOG_NOTICE, "\tclear_env = %s", BOOL2STR(wp->config->clear_env));
16731689
zlog(ZLOG_NOTICE, "\tsecurity.limit_extensions = %s", wp->config->security_limit_extensions);
16741690

sapi/fpm/fpm/fpm_conf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct fpm_global_config_s {
3030
int syslog_facility;
3131
#endif
3232
int log_level;
33+
int log_limit;
34+
int log_buffering;
3335
int emergency_restart_threshold;
3436
int emergency_restart_interval;
3537
int process_control_timeout;
@@ -85,6 +87,7 @@ struct fpm_worker_pool_config_s {
8587
char *chroot;
8688
char *chdir;
8789
int catch_workers_output;
90+
int decorate_workers_output;
8891
int clear_env;
8992
char *security_limit_extensions;
9093
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
@@ -637,21 +637,15 @@ void sapi_cgi_log_fastcgi(int level, char *message, size_t len)
637637

638638
fcgi_request *request = (fcgi_request*) SG(server_context);
639639

640-
/* ensure we want:
641-
* - to log (fastcgi.logging in php.ini)
640+
/* message is written to FCGI_STDERR if following conditions are met:
641+
* - logging is enabled (fastcgi.logging in php.ini)
642642
* - we are currently dealing with a request
643643
* - the message is not empty
644+
* - the fcgi_write did not fail
644645
*/
645-
if (CGIG(fcgi_logging) && request && message && len > 0) {
646-
ssize_t ret;
647-
char *buf = malloc(len + 2);
648-
memcpy(buf, message, len);
649-
memcpy(buf + len, "\n", sizeof("\n"));
650-
ret = fcgi_write(request, FCGI_STDERR, buf, len + 1);
651-
free(buf);
652-
if (ret < 0) {
653-
php_handle_aborted_connection();
654-
}
646+
if (CGIG(fcgi_logging) && request && message && len > 0
647+
&& fcgi_write(request, FCGI_STDERR, message, len) < 0) {
648+
php_handle_aborted_connection();
655649
}
656650
}
657651
/* }}} */
@@ -660,7 +654,7 @@ void sapi_cgi_log_fastcgi(int level, char *message, size_t len)
660654
*/
661655
static void sapi_cgi_log_message(char *message, int syslog_type_int)
662656
{
663-
zlog(ZLOG_NOTICE, "PHP message: %s", message);
657+
zlog_msg(ZLOG_NOTICE, "PHP message: ", message);
664658
}
665659
/* }}} */
666660

sapi/fpm/fpm/fpm_stdio.c

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

1515
#include "fpm.h"
1616
#include "fpm_children.h"
17+
#include "fpm_cleanup.h"
1718
#include "fpm_events.h"
1819
#include "fpm_sockets.h"
1920
#include "fpm_stdio.h"
@@ -22,6 +23,12 @@
2223
static int fd_stdout[2];
2324
static int fd_stderr[2];
2425

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

3444
if (0 > dup2(fd, STDIN_FILENO) || 0 > dup2(fd, STDOUT_FILENO)) {
3545
zlog(ZLOG_SYSERROR, "failed to init stdio: dup2()");
@@ -115,21 +125,30 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
115125
int is_stdout;
116126
struct fpm_event_s *event;
117127
int fifo_in = 1, fifo_out = 1;
118-
int is_last_message = 0;
119128
int in_buf = 0;
120129
int res;
130+
struct zlog_stream stream;
121131

122132
if (!arg) {
123133
return;
124134
}
125135
child = (struct fpm_child_s *)arg;
136+
126137
is_stdout = (fd == child->fd_stdout);
127138
if (is_stdout) {
128139
event = &child->ev_stdout;
129140
} else {
130141
event = &child->ev_stderr;
131142
}
132143

144+
zlog_stream_init_ex(&stream, ZLOG_WARNING, STDERR_FILENO);
145+
zlog_stream_set_decorating(&stream, child->wp->config->decorate_workers_output);
146+
zlog_stream_set_wrapping(&stream, ZLOG_TRUE);
147+
zlog_stream_set_msg_prefix(&stream, "[pool %s] child %d said into %s: ",
148+
child->wp->config->name, (int) child->pid, is_stdout ? "stdout" : "stderr");
149+
zlog_stream_set_msg_suffix(&stream, NULL, ", pipe is closed");
150+
zlog_stream_set_msg_quoting(&stream, ZLOG_TRUE);
151+
133152
while (fifo_in || fifo_out) {
134153
if (fifo_in) {
135154
res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
@@ -144,7 +163,6 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
144163
}
145164

146165
fpm_event_del(event);
147-
is_last_message = 1;
148166

149167
if (is_stdout) {
150168
close(child->fd_stdout);
@@ -164,42 +182,27 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
164182
fifo_out = 0;
165183
} else {
166184
char *nl;
167-
int should_print = 0;
168-
buf[in_buf] = '\0';
169185

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

0 commit comments

Comments
 (0)