1 /* Copyright 2000-2004 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,
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_io_set(procattr,
203 APR_NO_PIPE)) == APR_SUCCESS)
204 && ((rc = apr_procattr_error_check_set(procattr, 1)) == APR_SUCCESS)
205 && ((rc = apr_procattr_child_errfn_set(procattr, log_child_errfn)) == APR_SUCCESS)) {
209 apr_tokenize_to_argv(progname, &args, p);
210 pname = apr_pstrdup(p, args[0]);
211 procnew = (apr_proc_t *)apr_pcalloc(p, sizeof(*procnew));
212 rc = apr_proc_create(procnew, pname, (const char * const *)args,
215 if (rc == APR_SUCCESS) {
216 apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
217 (*fpin) = procnew->in;
224 static int open_error_log(server_rec *s, apr_pool_t *p)
229 if (*s->error_fname == '|') {
230 apr_file_t *dummy = NULL;
232 /* This starts a new process... */
233 rc = log_child (p, s->error_fname + 1, &dummy);
234 if (rc != APR_SUCCESS) {
235 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
236 "Couldn't start ErrorLog process");
240 s->error_log = dummy;
244 else if (!strncasecmp(s->error_fname, "syslog", 6)) {
245 if ((fname = strchr(s->error_fname, ':'))) {
249 for (fac = facilities; fac->t_name; fac++) {
250 if (!strcasecmp(fname, fac->t_name)) {
251 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID,
259 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
266 fname = ap_server_root_relative(p, s->error_fname);
268 ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EBADPATH, NULL,
269 "%s: Invalid error log path %s.",
270 ap_server_argv0, s->error_fname);
273 if ((rc = apr_file_open(&s->error_log, fname,
274 APR_APPEND | APR_WRITE | APR_CREATE,
275 APR_OS_DEFAULT, p)) != APR_SUCCESS) {
276 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
277 "%s: could not open error log file %s.",
278 ap_server_argv0, fname);
286 int ap_open_logs(apr_pool_t *pconf, apr_pool_t *p /* plog */,
287 apr_pool_t *ptemp, server_rec *s_main)
289 apr_status_t rc = APR_SUCCESS;
290 server_rec *virt, *q;
292 apr_file_t *errfile = NULL;
294 if (open_error_log(s_main, p) != OK) {
299 if (s_main->error_log) {
300 /* replace stderr with this new log */
301 apr_file_flush(s_main->error_log);
302 if ((rc = apr_file_open_stderr(&errfile, p)) == APR_SUCCESS) {
303 rc = apr_file_dup2(errfile, s_main->error_log, p);
305 if (rc != APR_SUCCESS) {
306 ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s_main,
307 "unable to replace stderr with error_log");
313 /* note that stderr may still need to be replaced with something
314 * because it points to the old error log, or back to the tty
316 * XXX: This is BS - /dev/null is non-portable
318 if (replace_stderr && freopen("/dev/null", "w", stderr) == NULL) {
319 ap_log_error(APLOG_MARK, APLOG_CRIT, errno, s_main,
320 "unable to replace stderr with /dev/null");
323 for (virt = s_main->next; virt; virt = virt->next) {
324 if (virt->error_fname) {
325 for (q=s_main; q != virt; q = q->next) {
326 if (q->error_fname != NULL
327 && strcmp(q->error_fname, virt->error_fname) == 0) {
333 if (open_error_log(virt, p) != OK) {
338 virt->error_log = q->error_log;
342 virt->error_log = s_main->error_log;
348 AP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
349 apr_file_t *errfile = NULL;
351 apr_file_open_stderr(&errfile, s->process->pool);
352 if (s->error_log != NULL) {
353 apr_file_dup2(s->error_log, errfile, s->process->pool);
357 static void log_error_core(const char *file, int line, int level,
358 apr_status_t status, const server_rec *s,
359 const request_rec *r, apr_pool_t *pool,
360 const char *fmt, va_list args)
362 char errstr[MAX_STRING_LEN];
363 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
364 char scratch[MAX_STRING_LEN];
366 apr_size_t len, errstrlen;
367 apr_file_t *logf = NULL;
369 int level_and_mask = level & APLOG_LEVELMASK;
373 * If we are doing stderr logging (startup), don't log messages that are
374 * above the default server log level unless it is a startup/shutdown
377 if ((level_and_mask != APLOG_NOTICE)
378 && (level_and_mask > ap_default_loglevel)) {
384 else if (s->error_log) {
386 * If we are doing normal logging, don't log messages that are
387 * above the server log level unless it is a startup/shutdown notice
389 if ((level_and_mask != APLOG_NOTICE)
390 && (level_and_mask > s->loglevel)) {
397 else if (tpf_child) {
399 * If we are doing normal logging, don't log messages that are
400 * above the server log level unless it is a startup/shutdown notice
402 if ((level_and_mask != APLOG_NOTICE)
403 && (level_and_mask > s->loglevel)) {
412 * If we are doing syslog logging, don't log messages that are
413 * above the server log level (including a startup/shutdown notice)
415 if (level_and_mask > s->loglevel) {
420 if (logf && ((level & APLOG_STARTUP) != APLOG_STARTUP)) {
422 ap_recent_ctime(errstr + 1, apr_time_now());
423 errstr[1 + APR_CTIME_LEN - 1] = ']';
424 errstr[1 + APR_CTIME_LEN ] = ' ';
425 len = 1 + APR_CTIME_LEN + 1;
430 if ((level & APLOG_STARTUP) != APLOG_STARTUP) {
431 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
432 "[%s] ", priorities[level_and_mask].t_name);
436 if (file && level_and_mask == APLOG_DEBUG) {
437 #if defined(_OSD_POSIX) || defined(WIN32)
439 char *e = strrchr(file, '/');
442 e = strrchr(file, '\\');
446 /* In OSD/POSIX, the compiler returns for __FILE__
447 * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
448 * (it even returns an absolute path for sources in
449 * the current directory). Here we try to strip this
450 * down to the basename.
452 if (e != NULL && e[1] != '\0') {
453 apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
454 e = &tmp[strlen(tmp)-1];
460 #endif /*_OSD_POSIX*/
461 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
462 "%s(%d): ", file, line);
466 if (r && r->connection) {
467 /* XXX: TODO: add a method of selecting whether logged client
468 * addresses are in dotted quad or resolved form... dotted
469 * quad is the most secure, which is why I'm implementing it
472 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
473 "[client %s] ", r->connection->remote_ip);
476 if (status < APR_OS_START_EAIERR) {
477 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
480 else if (status < APR_OS_START_SYSERR) {
481 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
482 "(EAI %d)", status - APR_OS_START_EAIERR);
484 else if (status < 100000 + APR_OS_START_SYSERR) {
485 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
486 "(OS %d)", status - APR_OS_START_SYSERR);
489 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
490 "(os 0x%08x)", status - APR_OS_START_SYSERR);
492 apr_strerror(status, errstr + len, MAX_STRING_LEN - len);
493 len += strlen(errstr + len);
494 if (MAX_STRING_LEN - len > 2) {
502 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
503 if (apr_vsnprintf(scratch, MAX_STRING_LEN - len, fmt, args)) {
504 len += ap_escape_errorlog_item(errstr + len, scratch,
505 MAX_STRING_LEN - len);
508 len += apr_vsnprintf(errstr + len, MAX_STRING_LEN - len, fmt, args);
511 if ( r && (referer = apr_table_get(r->headers_in, "Referer"))
512 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
513 && ap_escape_errorlog_item(scratch, referer, MAX_STRING_LEN - len)
516 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
518 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
526 /* NULL if we are logging to syslog */
528 /* Truncate for the terminator (as apr_snprintf does) */
529 if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR)) {
530 len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
532 strcpy(errstr + len, APR_EOL_STR);
533 apr_file_puts(errstr, logf);
534 apr_file_flush(logf);
538 syslog(level_and_mask, "%s", errstr);
542 ap_run_error_log(file, line, level, status, s, r, pool, errstr + errstrlen);
545 AP_DECLARE(void) ap_log_error(const char *file, int line, int level,
546 apr_status_t status, const server_rec *s,
547 const char *fmt, ...)
552 log_error_core(file, line, level, status, s, NULL, NULL, fmt, args);
556 AP_DECLARE(void) ap_log_perror(const char *file, int line, int level,
557 apr_status_t status, apr_pool_t *p,
558 const char *fmt, ...)
563 log_error_core(file, line, level, status, NULL, NULL, p, fmt, args);
567 AP_DECLARE(void) ap_log_rerror(const char *file, int line, int level,
568 apr_status_t status, const request_rec *r,
569 const char *fmt, ...)
574 log_error_core(file, line, level, status, r->server, r, NULL, fmt, args);
577 * IF APLOG_TOCLIENT is set,
578 * AND the error level is 'warning' or more severe,
579 * AND there isn't already error text associated with this request,
580 * THEN make the message text available to ErrorDocument and
581 * other error processors.
585 if ((level & APLOG_TOCLIENT)
586 && ((level & APLOG_LEVELMASK) <= APLOG_WARNING)
587 && (apr_table_get(r->notes, "error-notes") == NULL)) {
588 apr_table_setn(r->notes, "error-notes",
589 ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
595 AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
597 apr_file_t *pid_file = NULL;
599 static pid_t saved_pid = -1;
608 fname = ap_server_root_relative(p, filename);
610 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
611 NULL, "Invalid PID file path %s, ignoring.", filename);
616 if (mypid != saved_pid
617 && apr_stat(&finfo, fname, APR_FINFO_MTIME, p) == APR_SUCCESS) {
618 /* AP_SIG_GRACEFUL and HUP call this on each restart.
619 * Only warn on first time through for this pid.
621 * XXX: Could just write first time through too, although
622 * that may screw up scripts written to do something
623 * based on the last modification time of the pid file.
625 ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p,
626 apr_psprintf(p, "pid file %s overwritten -- Unclean "
627 "shutdown of previous Apache run?",
631 if ((rv = apr_file_open(&pid_file, fname,
632 APR_WRITE | APR_CREATE | APR_TRUNCATE,
633 APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p))
635 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
636 "could not create %s", fname);
637 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
638 "%s: could not log pid to file %s",
639 ap_server_argv0, fname);
642 apr_file_printf(pid_file, "%ld" APR_EOL_STR, (long)mypid);
643 apr_file_close(pid_file);
647 AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
650 const apr_size_t BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */
651 apr_file_t *pid_file = NULL;
655 apr_size_t bytes_read;
661 fname = ap_server_root_relative(p, filename);
663 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
664 NULL, "Invalid PID file path %s, ignoring.", filename);
668 rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p);
669 if (rv != APR_SUCCESS) {
673 /* Ensure null-termination, so that strtol doesn't go crazy. */
674 buf = apr_palloc(p, BUFFER_SIZE);
675 buf[BUFFER_SIZE - 1] = '\0';
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 == BUFFER_SIZE - 1 || !apr_isdigit(*buf)) {
688 *mypid = strtol(buf, &endptr, 10);
690 apr_file_close(pid_file);
694 AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile,
697 char time_str[APR_CTIME_LEN];
699 apr_ctime(time_str, apr_time_now());
700 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
701 "[%s] file %s, line %d, assertion \"%s\" failed",
702 time_str, szFile, nLine, szExp);
706 /* unix assert does an abort leading to a core dump */
711 /* piped log support */
713 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
714 /* forward declaration */
715 static void piped_log_maintenance(int reason, void *data, apr_wait_t status);
717 static int piped_log_spawn(piped_log *pl)
720 apr_procattr_t *procattr;
721 apr_proc_t *procnew = NULL;
724 if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
725 ((status = apr_procattr_child_in_set(procattr,
726 ap_piped_log_read_fd(pl),
727 ap_piped_log_write_fd(pl)))
729 ((status = apr_procattr_child_errfn_set(procattr, log_child_errfn))
731 ((status = apr_procattr_error_check_set(procattr, 1)) != APR_SUCCESS)) {
733 /* Something bad happened, give up and go away. */
734 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
735 "piped_log_spawn: unable to setup child process '%s': %s",
736 pl->program, apr_strerror(status, buf, sizeof(buf)));
743 apr_tokenize_to_argv(pl->program, &args, pl->p);
744 pname = apr_pstrdup(pl->p, args[0]);
745 procnew = apr_pcalloc(pl->p, sizeof(apr_proc_t));
746 status = apr_proc_create(procnew, pname, (const char * const *) args,
747 NULL, procattr, pl->p);
749 if (status == APR_SUCCESS) {
751 ap_piped_log_write_fd(pl) = procnew->in;
752 apr_proc_other_child_register(procnew, piped_log_maintenance, pl,
753 ap_piped_log_write_fd(pl), pl->p);
757 /* Something bad happened, give up and go away. */
758 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
759 "unable to start piped log program '%s': %s",
760 pl->program, apr_strerror(status, buf, sizeof(buf)));
769 static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
771 piped_log *pl = data;
776 case APR_OC_REASON_DEATH:
777 case APR_OC_REASON_LOST:
778 pl->pid = NULL; /* in case we don't get it going again, this
779 * tells other logic not to try to kill it
781 apr_proc_other_child_unregister(pl);
782 stats = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
783 if (stats != APR_SUCCESS) {
784 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
785 "can't query MPM state; not restarting "
786 "piped log program '%s'",
789 else if (mpm_state != AP_MPMQ_STOPPING) {
790 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
791 "piped log program '%s' failed unexpectedly",
793 if ((stats = piped_log_spawn(pl)) != APR_SUCCESS) {
794 /* what can we do? This could be the error log we're having
795 * problems opening up... */
797 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
798 "piped_log_maintenance: unable to respawn '%s': %s",
799 pl->program, apr_strerror(stats, buf, sizeof(buf)));
804 case APR_OC_REASON_UNWRITABLE:
805 /* We should not kill off the pipe here, since it may only be full.
806 * If it really is locked, we should kill it off manually. */
809 case APR_OC_REASON_RESTART:
810 if (pl->pid != NULL) {
811 apr_proc_kill(pl->pid, SIGTERM);
816 case APR_OC_REASON_UNREGISTER:
822 static apr_status_t piped_log_cleanup_for_exec(void *data)
824 piped_log *pl = data;
826 apr_file_close(ap_piped_log_read_fd(pl));
827 apr_file_close(ap_piped_log_write_fd(pl));
832 static apr_status_t piped_log_cleanup(void *data)
834 piped_log *pl = data;
836 if (pl->pid != NULL) {
837 apr_proc_kill(pl->pid, SIGTERM);
839 return piped_log_cleanup_for_exec(data);
843 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
847 pl = apr_palloc(p, sizeof (*pl));
849 pl->program = apr_pstrdup(p, program);
851 if (apr_file_pipe_create(&ap_piped_log_read_fd(pl),
852 &ap_piped_log_write_fd(pl), p) != APR_SUCCESS) {
855 apr_pool_cleanup_register(p, pl, piped_log_cleanup,
856 piped_log_cleanup_for_exec);
857 if (piped_log_spawn(pl) == -1) {
858 int save_errno = errno;
859 apr_pool_cleanup_kill(p, pl, piped_log_cleanup);
860 apr_file_close(ap_piped_log_read_fd(pl));
861 apr_file_close(ap_piped_log_write_fd(pl));
868 #else /* !AP_HAVE_RELIABLE_PIPED_LOGS */
870 static apr_status_t piped_log_cleanup(void *data)
872 piped_log *pl = data;
874 apr_file_close(ap_piped_log_write_fd(pl));
878 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
881 apr_file_t *dummy = NULL;
884 rc = log_child(p, program, &dummy);
885 if (rc != APR_SUCCESS) {
886 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
887 "Couldn't start piped log process");
891 pl = apr_palloc(p, sizeof (*pl));
893 ap_piped_log_read_fd(pl) = NULL;
894 ap_piped_log_write_fd(pl) = dummy;
895 apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
902 AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
904 apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
907 AP_IMPLEMENT_HOOK_VOID(error_log,
908 (const char *file, int line, int level,
909 apr_status_t status, const server_rec *s,
910 const request_rec *r, apr_pool_t *pool,
911 const char *errstr), (file, line, level,
912 status, s, r, pool, errstr))