1 /* Copyright 1999-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * http_log.c: Dealing with the logs and errors
24 #include "apr_general.h" /* for signal stuff */
25 #include "apr_strings.h"
26 #include "apr_errno.h"
27 #include "apr_thread_proc.h"
29 #include "apr_signal.h"
31 #define APR_WANT_STDIO
32 #define APR_WANT_STRFUNC
44 #include "ap_config.h"
46 #include "http_config.h"
47 #include "http_core.h"
49 #include "http_main.h"
50 #include "util_time.h"
59 APR_HOOK_LINK(error_log)
62 int AP_DECLARE_DATA ap_default_loglevel = DEFAULT_LOGLEVEL;
66 static const TRANS facilities[] = {
69 {"authpriv",LOG_AUTHPRIV},
75 {"daemon", LOG_DAEMON},
93 {"syslog", LOG_SYSLOG},
102 {"local0", LOG_LOCAL0},
105 {"local1", LOG_LOCAL1},
108 {"local2", LOG_LOCAL2},
111 {"local3", LOG_LOCAL3},
114 {"local4", LOG_LOCAL4},
117 {"local5", LOG_LOCAL5},
120 {"local6", LOG_LOCAL6},
123 {"local7", LOG_LOCAL7},
129 static const TRANS priorities[] = {
130 {"emerg", APLOG_EMERG},
131 {"alert", APLOG_ALERT},
132 {"crit", APLOG_CRIT},
133 {"error", APLOG_ERR},
134 {"warn", APLOG_WARNING},
135 {"notice", APLOG_NOTICE},
136 {"info", APLOG_INFO},
137 {"debug", APLOG_DEBUG},
141 static apr_file_t *stderr_log = NULL;
143 AP_DECLARE(void) ap_open_stderr_log(apr_pool_t *p)
145 apr_file_open_stderr(&stderr_log, p);
148 AP_DECLARE(apr_status_t) ap_replace_stderr_log(apr_pool_t *p,
151 apr_file_t *stderr_file;
153 char *filename = ap_server_root_relative(p, fname);
155 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
156 APR_EBADPATH, NULL, "Invalid -E error log file %s",
160 if ((rc = apr_file_open(&stderr_file, filename,
161 APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
162 APR_OS_DEFAULT, p)) != APR_SUCCESS) {
163 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
164 "%s: could not open error log file %s.",
165 ap_server_argv0, fname);
168 if ((rc = apr_file_open_stderr(&stderr_log, p)) == APR_SUCCESS) {
169 apr_file_flush(stderr_log);
170 if ((rc = apr_file_dup2(stderr_log, stderr_file, p)) == APR_SUCCESS) {
171 apr_file_close(stderr_file);
174 if (rc != APR_SUCCESS) {
175 ap_log_error(APLOG_MARK, APLOG_CRIT, rc, NULL,
176 "unable to replace stderr with error_log");
181 static void log_child_errfn(apr_pool_t *pool, apr_status_t err,
182 const char *description)
184 ap_log_error(APLOG_MARK, APLOG_ERR, err, NULL,
188 static int log_child(apr_pool_t *p, const char *progname,
191 /* Child process code for 'ErrorLog "|..."';
192 * may want a common framework for this, since I expect it will
193 * be common for other foo-loggers to want this sort of thing...
196 apr_procattr_t *procattr;
199 if (((rc = apr_procattr_create(&procattr, p)) == APR_SUCCESS)
200 && ((rc = apr_procattr_cmdtype_set(procattr,
201 APR_SHELLCMD_ENV)) == APR_SUCCESS)
202 && ((rc = apr_procattr_io_set(procattr,
205 APR_NO_PIPE)) == APR_SUCCESS)
206 && ((rc = apr_procattr_error_check_set(procattr, 1)) == APR_SUCCESS)
207 && ((rc = apr_procattr_child_errfn_set(procattr, log_child_errfn)) == APR_SUCCESS)) {
211 apr_tokenize_to_argv(progname, &args, p);
212 pname = apr_pstrdup(p, args[0]);
213 procnew = (apr_proc_t *)apr_pcalloc(p, sizeof(*procnew));
214 rc = apr_proc_create(procnew, pname, (const char * const *)args,
217 if (rc == APR_SUCCESS) {
218 apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
219 (*fpin) = procnew->in;
226 static int open_error_log(server_rec *s, apr_pool_t *p)
231 if (*s->error_fname == '|') {
232 apr_file_t *dummy = NULL;
234 /* This starts a new process... */
235 rc = log_child (p, s->error_fname + 1, &dummy);
236 if (rc != APR_SUCCESS) {
237 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
238 "Couldn't start ErrorLog process");
242 s->error_log = dummy;
246 else if (!strncasecmp(s->error_fname, "syslog", 6)) {
247 if ((fname = strchr(s->error_fname, ':'))) {
251 for (fac = facilities; fac->t_name; fac++) {
252 if (!strcasecmp(fname, fac->t_name)) {
253 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID,
261 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
268 fname = ap_server_root_relative(p, s->error_fname);
270 ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EBADPATH, NULL,
271 "%s: Invalid error log path %s.",
272 ap_server_argv0, s->error_fname);
275 if ((rc = apr_file_open(&s->error_log, fname,
276 APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
277 APR_OS_DEFAULT, p)) != APR_SUCCESS) {
278 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
279 "%s: could not open error log file %s.",
280 ap_server_argv0, fname);
288 int ap_open_logs(apr_pool_t *pconf, apr_pool_t *p /* plog */,
289 apr_pool_t *ptemp, server_rec *s_main)
291 apr_status_t rc = APR_SUCCESS;
292 server_rec *virt, *q;
294 apr_file_t *errfile = NULL;
296 if (open_error_log(s_main, p) != OK) {
301 if (s_main->error_log) {
302 /* replace stderr with this new log */
303 apr_file_flush(s_main->error_log);
304 if ((rc = apr_file_open_stderr(&errfile, p)) == APR_SUCCESS) {
305 rc = apr_file_dup2(errfile, s_main->error_log, p);
307 if (rc != APR_SUCCESS) {
308 ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s_main,
309 "unable to replace stderr with error_log");
315 /* note that stderr may still need to be replaced with something
316 * because it points to the old error log, or back to the tty
318 * XXX: This is BS - /dev/null is non-portable
320 if (replace_stderr && freopen("/dev/null", "w", stderr) == NULL) {
321 ap_log_error(APLOG_MARK, APLOG_CRIT, errno, s_main,
322 "unable to replace stderr with /dev/null");
325 for (virt = s_main->next; virt; virt = virt->next) {
326 if (virt->error_fname) {
327 for (q=s_main; q != virt; q = q->next) {
328 if (q->error_fname != NULL
329 && strcmp(q->error_fname, virt->error_fname) == 0) {
335 if (open_error_log(virt, p) != OK) {
340 virt->error_log = q->error_log;
344 virt->error_log = s_main->error_log;
350 AP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
351 apr_file_t *errfile = NULL;
353 apr_file_open_stderr(&errfile, s->process->pool);
354 if (s->error_log != NULL) {
355 apr_file_dup2(s->error_log, errfile, s->process->pool);
359 static void log_error_core(const char *file, int line, int level,
360 apr_status_t status, const server_rec *s,
361 const request_rec *r, apr_pool_t *pool,
362 const char *fmt, va_list args)
364 char errstr[MAX_STRING_LEN];
365 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
366 char scratch[MAX_STRING_LEN];
368 apr_size_t len, errstrlen;
369 apr_file_t *logf = NULL;
371 int level_and_mask = level & APLOG_LEVELMASK;
375 * If we are doing stderr logging (startup), don't log messages that are
376 * above the default server log level unless it is a startup/shutdown
379 if ((level_and_mask != APLOG_NOTICE)
380 && (level_and_mask > ap_default_loglevel)) {
386 else if (s->error_log) {
388 * If we are doing normal logging, don't log messages that are
389 * above the server log level unless it is a startup/shutdown notice
391 if ((level_and_mask != APLOG_NOTICE)
392 && (level_and_mask > s->loglevel)) {
399 else if (tpf_child) {
401 * If we are doing normal logging, don't log messages that are
402 * above the server log level unless it is a startup/shutdown notice
404 if ((level_and_mask != APLOG_NOTICE)
405 && (level_and_mask > s->loglevel)) {
414 * If we are doing syslog logging, don't log messages that are
415 * above the server log level (including a startup/shutdown notice)
417 if (level_and_mask > s->loglevel) {
422 if (logf && ((level & APLOG_STARTUP) != APLOG_STARTUP)) {
424 ap_recent_ctime(errstr + 1, apr_time_now());
425 errstr[1 + APR_CTIME_LEN - 1] = ']';
426 errstr[1 + APR_CTIME_LEN ] = ' ';
427 len = 1 + APR_CTIME_LEN + 1;
432 if ((level & APLOG_STARTUP) != APLOG_STARTUP) {
433 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
434 "[%s] ", priorities[level_and_mask].t_name);
438 if (file && level_and_mask == APLOG_DEBUG) {
439 #if defined(_OSD_POSIX) || defined(WIN32)
441 char *e = strrchr(file, '/');
444 e = strrchr(file, '\\');
448 /* In OSD/POSIX, the compiler returns for __FILE__
449 * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
450 * (it even returns an absolute path for sources in
451 * the current directory). Here we try to strip this
452 * down to the basename.
454 if (e != NULL && e[1] != '\0') {
455 apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
456 e = &tmp[strlen(tmp)-1];
462 #endif /*_OSD_POSIX*/
463 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
464 "%s(%d): ", file, line);
468 if (r && r->connection) {
469 /* XXX: TODO: add a method of selecting whether logged client
470 * addresses are in dotted quad or resolved form... dotted
471 * quad is the most secure, which is why I'm implementing it
474 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
475 "[client %s] ", r->connection->remote_ip);
478 if (status < APR_OS_START_EAIERR) {
479 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
482 else if (status < APR_OS_START_SYSERR) {
483 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
484 "(EAI %d)", status - APR_OS_START_EAIERR);
486 else if (status < 100000 + APR_OS_START_SYSERR) {
487 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
488 "(OS %d)", status - APR_OS_START_SYSERR);
491 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
492 "(os 0x%08x)", status - APR_OS_START_SYSERR);
494 apr_strerror(status, errstr + len, MAX_STRING_LEN - len);
495 len += strlen(errstr + len);
496 if (MAX_STRING_LEN - len > 2) {
504 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
505 if (apr_vsnprintf(scratch, MAX_STRING_LEN - len, fmt, args)) {
506 len += ap_escape_errorlog_item(errstr + len, scratch,
507 MAX_STRING_LEN - len);
510 len += apr_vsnprintf(errstr + len, MAX_STRING_LEN - len, fmt, args);
513 if ( r && (referer = apr_table_get(r->headers_in, "Referer"))
514 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
515 && ap_escape_errorlog_item(scratch, referer, MAX_STRING_LEN - len)
518 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
520 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
528 /* NULL if we are logging to syslog */
530 /* Truncate for the terminator (as apr_snprintf does) */
531 if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR)) {
532 len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
534 strcpy(errstr + len, APR_EOL_STR);
535 apr_file_puts(errstr, logf);
536 apr_file_flush(logf);
540 syslog(level_and_mask, "%s", errstr);
544 ap_run_error_log(file, line, level, status, s, r, pool, errstr + errstrlen);
547 AP_DECLARE(void) ap_log_error(const char *file, int line, int level,
548 apr_status_t status, const server_rec *s,
549 const char *fmt, ...)
554 log_error_core(file, line, level, status, s, NULL, NULL, fmt, args);
558 AP_DECLARE(void) ap_log_perror(const char *file, int line, int level,
559 apr_status_t status, apr_pool_t *p,
560 const char *fmt, ...)
565 log_error_core(file, line, level, status, NULL, NULL, p, fmt, args);
569 AP_DECLARE(void) ap_log_rerror(const char *file, int line, int level,
570 apr_status_t status, const request_rec *r,
571 const char *fmt, ...)
576 log_error_core(file, line, level, status, r->server, r, NULL, fmt, args);
579 * IF APLOG_TOCLIENT is set,
580 * AND the error level is 'warning' or more severe,
581 * AND there isn't already error text associated with this request,
582 * THEN make the message text available to ErrorDocument and
583 * other error processors.
587 if ((level & APLOG_TOCLIENT)
588 && ((level & APLOG_LEVELMASK) <= APLOG_WARNING)
589 && (apr_table_get(r->notes, "error-notes") == NULL)) {
590 apr_table_setn(r->notes, "error-notes",
591 ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
597 AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
599 apr_file_t *pid_file = NULL;
601 static pid_t saved_pid = -1;
610 fname = ap_server_root_relative(p, filename);
612 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
613 NULL, "Invalid PID file path %s, ignoring.", filename);
618 if (mypid != saved_pid
619 && apr_stat(&finfo, fname, APR_FINFO_MTIME, p) == APR_SUCCESS) {
620 /* AP_SIG_GRACEFUL and HUP call this on each restart.
621 * Only warn on first time through for this pid.
623 * XXX: Could just write first time through too, although
624 * that may screw up scripts written to do something
625 * based on the last modification time of the pid file.
627 ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p,
628 "pid file %s overwritten -- Unclean "
629 "shutdown of previous Apache run?",
633 if ((rv = apr_file_open(&pid_file, fname,
634 APR_WRITE | APR_CREATE | APR_TRUNCATE,
635 APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p))
637 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
638 "could not create %s", fname);
639 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
640 "%s: could not log pid to file %s",
641 ap_server_argv0, fname);
644 apr_file_printf(pid_file, "%ld" APR_EOL_STR, (long)mypid);
645 apr_file_close(pid_file);
649 AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
652 const apr_size_t BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */
653 apr_file_t *pid_file = NULL;
657 apr_size_t bytes_read;
663 fname = ap_server_root_relative(p, filename);
665 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
666 NULL, "Invalid PID file path %s, ignoring.", filename);
670 rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p);
671 if (rv != APR_SUCCESS) {
675 buf = apr_palloc(p, BUFFER_SIZE);
677 rv = apr_file_read_full(pid_file, buf, BUFFER_SIZE - 1, &bytes_read);
678 if (rv != APR_SUCCESS && rv != APR_EOF) {
682 /* If we fill the buffer, we're probably reading a corrupt pid file.
683 * To be nice, let's also ensure the first char is a digit. */
684 if (bytes_read == 0 || bytes_read == BUFFER_SIZE - 1 || !apr_isdigit(*buf)) {
688 buf[bytes_read] = '\0';
689 *mypid = strtol(buf, &endptr, 10);
691 apr_file_close(pid_file);
695 AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile,
698 char time_str[APR_CTIME_LEN];
700 apr_ctime(time_str, apr_time_now());
701 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
702 "[%s] file %s, line %d, assertion \"%s\" failed",
703 time_str, szFile, nLine, szExp);
707 /* unix assert does an abort leading to a core dump */
712 /* piped log support */
714 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
715 /* forward declaration */
716 static void piped_log_maintenance(int reason, void *data, apr_wait_t status);
718 static int piped_log_spawn(piped_log *pl)
721 apr_procattr_t *procattr;
722 apr_proc_t *procnew = NULL;
725 if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
726 ((status = apr_procattr_cmdtype_set(procattr,
727 APR_SHELLCMD_ENV)) != APR_SUCCESS) ||
728 ((status = apr_procattr_child_in_set(procattr,
729 ap_piped_log_read_fd(pl),
730 ap_piped_log_write_fd(pl)))
732 ((status = apr_procattr_child_errfn_set(procattr, log_child_errfn))
734 ((status = apr_procattr_error_check_set(procattr, 1)) != APR_SUCCESS)) {
736 /* Something bad happened, give up and go away. */
737 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
738 "piped_log_spawn: unable to setup child process '%s': %s",
739 pl->program, apr_strerror(status, buf, sizeof(buf)));
746 apr_tokenize_to_argv(pl->program, &args, pl->p);
747 pname = apr_pstrdup(pl->p, args[0]);
748 procnew = apr_pcalloc(pl->p, sizeof(apr_proc_t));
749 status = apr_proc_create(procnew, pname, (const char * const *) args,
750 NULL, procattr, pl->p);
752 if (status == APR_SUCCESS) {
754 ap_piped_log_write_fd(pl) = procnew->in;
755 apr_proc_other_child_register(procnew, piped_log_maintenance, pl,
756 ap_piped_log_write_fd(pl), pl->p);
760 /* Something bad happened, give up and go away. */
761 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
762 "unable to start piped log program '%s': %s",
763 pl->program, apr_strerror(status, buf, sizeof(buf)));
772 static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
774 piped_log *pl = data;
779 case APR_OC_REASON_DEATH:
780 case APR_OC_REASON_LOST:
781 pl->pid = NULL; /* in case we don't get it going again, this
782 * tells other logic not to try to kill it
784 apr_proc_other_child_unregister(pl);
785 stats = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
786 if (stats != APR_SUCCESS) {
787 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
788 "can't query MPM state; not restarting "
789 "piped log program '%s'",
792 else if (mpm_state != AP_MPMQ_STOPPING) {
793 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
794 "piped log program '%s' failed unexpectedly",
796 if ((stats = piped_log_spawn(pl)) != APR_SUCCESS) {
797 /* what can we do? This could be the error log we're having
798 * problems opening up... */
800 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
801 "piped_log_maintenance: unable to respawn '%s': %s",
802 pl->program, apr_strerror(stats, buf, sizeof(buf)));
807 case APR_OC_REASON_UNWRITABLE:
808 /* We should not kill off the pipe here, since it may only be full.
809 * If it really is locked, we should kill it off manually. */
812 case APR_OC_REASON_RESTART:
813 if (pl->pid != NULL) {
814 apr_proc_kill(pl->pid, SIGTERM);
819 case APR_OC_REASON_UNREGISTER:
825 static apr_status_t piped_log_cleanup_for_exec(void *data)
827 piped_log *pl = data;
829 apr_file_close(ap_piped_log_read_fd(pl));
830 apr_file_close(ap_piped_log_write_fd(pl));
835 static apr_status_t piped_log_cleanup(void *data)
837 piped_log *pl = data;
839 if (pl->pid != NULL) {
840 apr_proc_kill(pl->pid, SIGTERM);
842 return piped_log_cleanup_for_exec(data);
846 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
850 pl = apr_palloc(p, sizeof (*pl));
852 pl->program = apr_pstrdup(p, program);
854 if (apr_file_pipe_create(&ap_piped_log_read_fd(pl),
855 &ap_piped_log_write_fd(pl), p) != APR_SUCCESS) {
858 apr_pool_cleanup_register(p, pl, piped_log_cleanup,
859 piped_log_cleanup_for_exec);
860 if (piped_log_spawn(pl) == -1) {
861 int save_errno = errno;
862 apr_pool_cleanup_kill(p, pl, piped_log_cleanup);
863 apr_file_close(ap_piped_log_read_fd(pl));
864 apr_file_close(ap_piped_log_write_fd(pl));
871 #else /* !AP_HAVE_RELIABLE_PIPED_LOGS */
873 static apr_status_t piped_log_cleanup(void *data)
875 piped_log *pl = data;
877 apr_file_close(ap_piped_log_write_fd(pl));
881 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
884 apr_file_t *dummy = NULL;
887 rc = log_child(p, program, &dummy);
888 if (rc != APR_SUCCESS) {
889 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
890 "Couldn't start piped log process");
894 pl = apr_palloc(p, sizeof (*pl));
896 ap_piped_log_read_fd(pl) = NULL;
897 ap_piped_log_write_fd(pl) = dummy;
898 apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
905 AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
907 apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
910 AP_IMPLEMENT_HOOK_VOID(error_log,
911 (const char *file, int line, int level,
912 apr_status_t status, const server_rec *s,
913 const request_rec *r, apr_pool_t *pool,
914 const char *errstr), (file, line, level,
915 status, s, r, pool, errstr))