1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 * http_log.c: Dealing with the logs and errors
67 #include "apr_general.h" /* for signal stuff */
68 #include "apr_strings.h"
69 #include "apr_errno.h"
70 #include "apr_thread_proc.h"
72 #include "apr_signal.h"
74 #define APR_WANT_STDIO
75 #define APR_WANT_STRFUNC
87 #include "ap_config.h"
89 #include "http_config.h"
90 #include "http_core.h"
92 #include "http_main.h"
101 static const TRANS facilities[] = {
104 {"authpriv",LOG_AUTHPRIV},
110 {"daemon", LOG_DAEMON},
128 {"syslog", LOG_SYSLOG},
137 {"local0", LOG_LOCAL0},
140 {"local1", LOG_LOCAL1},
143 {"local2", LOG_LOCAL2},
146 {"local3", LOG_LOCAL3},
149 {"local4", LOG_LOCAL4},
152 {"local5", LOG_LOCAL5},
155 {"local6", LOG_LOCAL6},
158 {"local7", LOG_LOCAL7},
164 static const TRANS priorities[] = {
165 {"emerg", APLOG_EMERG},
166 {"alert", APLOG_ALERT},
167 {"crit", APLOG_CRIT},
168 {"error", APLOG_ERR},
169 {"warn", APLOG_WARNING},
170 {"notice", APLOG_NOTICE},
171 {"info", APLOG_INFO},
172 {"debug", APLOG_DEBUG},
176 static apr_file_t *stderr_log;
178 AP_DECLARE(void) ap_open_stderr_log(apr_pool_t *p)
180 apr_file_open_stderr(&stderr_log, p);
183 static int log_child(apr_pool_t *p, const char *progname,
186 /* Child process code for 'ErrorLog "|..."';
187 * may want a common framework for this, since I expect it will
188 * be common for other foo-loggers to want this sort of thing...
191 apr_procattr_t *procattr;
195 /* No concept of a child process on Win32 */
196 apr_signal(SIGHUP, SIG_IGN);
197 #endif /* ndef SIGHUP */
199 if (((rc = apr_procattr_create(&procattr, p)) == APR_SUCCESS) &&
200 ((rc = apr_procattr_io_set(procattr,
203 APR_NO_PIPE)) == APR_SUCCESS)) {
207 apr_tokenize_to_argv(progname, &args, p);
208 pname = apr_pstrdup(p, args[0]);
209 procnew = (apr_proc_t *) apr_pcalloc(p, sizeof(*procnew));
210 rc = apr_proc_create(procnew, pname, (const char * const *)args,
213 if (rc == APR_SUCCESS) {
214 apr_pool_note_subprocess(p, procnew, kill_after_timeout);
215 (*fpin) = procnew->in;
222 static void open_error_log(server_rec *s, apr_pool_t *p)
227 if (*s->error_fname == '|') {
228 apr_file_t *dummy = NULL;
230 /* This starts a new process... */
231 rc = log_child (p, s->error_fname+1, &dummy);
232 if (rc != APR_SUCCESS) {
233 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
234 "Couldn't start ErrorLog process");
238 s->error_log = dummy;
242 else if (!strncasecmp(s->error_fname, "syslog", 6)) {
243 if ((fname = strchr(s->error_fname, ':'))) {
247 for (fac = facilities; fac->t_name; fac++) {
248 if (!strcasecmp(fname, fac->t_name)) {
249 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID,
257 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
263 fname = ap_server_root_relative(p, s->error_fname);
264 rc = apr_file_open(&s->error_log, fname,
265 APR_APPEND | APR_READ | APR_WRITE | APR_CREATE,
267 if (rc != APR_SUCCESS) {
268 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
269 "%s: could not open error log file %s.",
270 ap_server_argv0, fname);
276 void ap_open_logs(server_rec *s_main, apr_pool_t *p)
278 apr_status_t rc = APR_SUCCESS;
279 server_rec *virt, *q;
281 apr_file_t *errfile = NULL;
283 open_error_log(s_main, p);
286 if (s_main->error_log) {
287 /* replace stderr with this new log */
288 apr_file_flush(s_main->error_log);
289 apr_file_open_stderr(&errfile, p);
290 if ((rc = apr_file_dup(&errfile, s_main->error_log, p)) != APR_SUCCESS) {
291 ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s_main,
292 "unable to replace stderr with error_log");
297 /* note that stderr may still need to be replaced with something
298 * because it points to the old error log, or back to the tty
301 if (replace_stderr && freopen("/dev/null", "w", stderr) == NULL) {
302 ap_log_error(APLOG_MARK, APLOG_CRIT, errno, s_main,
303 "unable to replace stderr with /dev/null");
306 for (virt = s_main->next; virt; virt = virt->next) {
307 if (virt->error_fname) {
308 for (q=s_main; q != virt; q = q->next)
309 if (q->error_fname != NULL &&
310 strcmp(q->error_fname, virt->error_fname) == 0)
313 open_error_log(virt, p);
315 virt->error_log = q->error_log;
318 virt->error_log = s_main->error_log;
322 AP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
323 apr_file_t *errfile = NULL;
325 apr_file_open_stderr(&errfile, s->process->pool);
326 if (s->error_log != NULL) {
327 apr_file_dup(&(s->error_log), errfile, s->process->pool);
331 static void log_error_core(const char *file, int line, int level,
332 apr_status_t status, const server_rec *s,
333 const request_rec *r, apr_pool_t *pool,
334 const char *fmt, va_list args)
336 char errstr[MAX_STRING_LEN];
338 apr_file_t *logf = NULL;
343 * If we are doing stderr logging (startup), don't log messages that are
344 * above the default server log level unless it is a startup/shutdown
347 if (((level & APLOG_LEVELMASK) != APLOG_NOTICE) &&
348 ((level & APLOG_LEVELMASK) > DEFAULT_LOGLEVEL))
352 else if (s->error_log) {
354 * If we are doing normal logging, don't log messages that are
355 * above the server log level unless it is a startup/shutdown notice
357 if (((level & APLOG_LEVELMASK) != APLOG_NOTICE) &&
358 ((level & APLOG_LEVELMASK) > s->loglevel))
363 else if (tpf_child) {
365 * If we are doing normal logging, don't log messages that are
366 * above the server log level unless it is a startup/shutdown notice
368 if (((level & APLOG_LEVELMASK) != APLOG_NOTICE) &&
369 ((level & APLOG_LEVELMASK) > s->loglevel))
376 * If we are doing syslog logging, don't log messages that are
377 * above the server log level (including a startup/shutdown notice)
379 if ((level & APLOG_LEVELMASK) > s->loglevel)
384 if (logf && ((level & APLOG_STARTUP) != APLOG_STARTUP)) {
386 apr_ctime(errstr + 1, apr_time_now());
387 errstr[1 + APR_CTIME_LEN - 1] = ']';
388 errstr[1 + APR_CTIME_LEN ] = ' ';
389 len = 1 + APR_CTIME_LEN + 1;
394 if ((level & APLOG_STARTUP) != APLOG_STARTUP) {
395 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
396 "[%s] ", priorities[level & APLOG_LEVELMASK].t_name);
399 if (file && (level & APLOG_LEVELMASK) == APLOG_DEBUG) {
402 char *e = strrchr(file, '/');
404 /* In OSD/POSIX, the compiler returns for __FILE__
405 * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
406 * (it even returns an absolute path for sources in
407 * the current directory). Here we try to strip this
408 * down to the basename.
410 if (e != NULL && e[1] != '\0') {
411 apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
412 e = &tmp[strlen(tmp)-1];
417 #endif /*_OSD_POSIX*/
418 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
419 "%s(%d): ", file, line);
422 if (r && r->connection) {
423 /* XXX: TODO: add a method of selecting whether logged client
424 * addresses are in dotted quad or resolved form... dotted
425 * quad is the most secure, which is why I'm implementing it
428 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
429 "[client %s] ", r->connection->remote_ip);
431 if (!(level & APLOG_NOERRNO)
434 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
435 "(%d)%s: ", status, apr_strerror(status, buf, sizeof(buf)));
437 len += apr_vsnprintf(errstr + len, MAX_STRING_LEN - len, fmt, args);
439 if (r && (referer = apr_table_get(r->headers_in, "Referer"))) {
440 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
441 ", referer: %s", referer);
444 /* NULL if we are logging to syslog */
446 /* Truncate for the terminator (as apr_snprintf does) */
447 if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR))
448 len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
449 strcpy(errstr + len, APR_EOL_STR);
450 apr_file_puts(errstr, logf);
451 apr_file_flush(logf);
455 syslog(level & APLOG_LEVELMASK, "%s", errstr);
460 AP_DECLARE(void) ap_log_error(const char *file, int line, int level,
461 apr_status_t status, const server_rec *s,
462 const char *fmt, ...)
467 log_error_core(file, line, level, status, s, NULL, NULL, fmt, args);
471 AP_DECLARE(void) ap_log_perror(const char *file, int line, int level,
472 apr_status_t status, apr_pool_t *p,
473 const char *fmt, ...)
478 log_error_core(file, line, level, status, NULL, NULL, p, fmt, args);
482 AP_DECLARE(void) ap_log_rerror(const char *file, int line, int level,
483 apr_status_t status, const request_rec *r,
484 const char *fmt, ...)
489 log_error_core(file, line, level, status, r->server, r, NULL, fmt, args);
491 * IF the error level is 'warning' or more severe,
492 * AND there isn't already error text associated with this request,
493 * THEN make the message text available to ErrorDocument and
494 * other error processors. This can be disabled by stuffing
495 * something, even an empty string, into the "error-notes" cell
496 * before calling this routine.
500 if (((level & APLOG_LEVELMASK) <= APLOG_WARNING)
501 && (apr_table_get(r->notes, "error-notes") == NULL)) {
502 apr_table_setn(r->notes, "error-notes",
503 ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
509 void ap_log_pid(apr_pool_t *p, const char *fname)
511 apr_file_t *pid_file = NULL;
513 static pid_t saved_pid = -1;
520 fname = ap_server_root_relative(p, fname);
522 if (mypid != saved_pid
523 && apr_stat(&finfo, fname, APR_FINFO_MTIME, p) == APR_SUCCESS) {
524 /* WINCH and HUP call this on each restart.
525 * Only warn on first time through for this pid.
527 * XXX: Could just write first time through too, although
528 * that may screw up scripts written to do something
529 * based on the last modification time of the pid file.
531 ap_log_perror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, p,
533 "pid file %s overwritten -- Unclean shutdown of previous Apache run?",
538 if ((rv = apr_file_open(&pid_file, fname, APR_WRITE | APR_CREATE | APR_TRUNCATE,
539 APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p)) != APR_SUCCESS) {
540 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
541 "could not create %s", fname);
542 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, NULL,
543 "%s: could not log pid to file %s",
544 ap_server_argv0, fname);
547 apr_file_printf(pid_file, "%ld" APR_EOL_STR, (long)mypid);
548 apr_file_close(pid_file);
552 AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile, int nLine)
554 char time_str[APR_CTIME_LEN];
556 apr_ctime(time_str, apr_time_now());
557 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
558 "[%s] file %s, line %d, assertion \"%s\" failed",
559 time_str, szFile, nLine, szExp);
561 /* unix assert does an abort leading to a core dump */
568 /* piped log support */
570 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
571 /* forward declaration */
572 static void piped_log_maintenance(int reason, void *data, apr_wait_t status);
574 static int piped_log_spawn(piped_log *pl)
577 apr_procattr_t *procattr;
578 apr_proc_t *procnew = NULL;
582 apr_signal(SIGHUP, SIG_IGN);
584 if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
585 ((status = apr_procattr_child_in_set(procattr, ap_piped_log_read_fd(pl),
586 ap_piped_log_write_fd(pl))) != APR_SUCCESS)) {
588 /* Something bad happened, give up and go away. */
589 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
590 "piped_log_spawn: unable to setup child process '%s': %s",
591 pl->program, apr_strerror(status, buf, sizeof(buf)));
598 apr_tokenize_to_argv(pl->program, &args, pl->p);
599 pname = apr_pstrdup(pl->p, args[0]);
600 procnew = apr_pcalloc(pl->p, sizeof(apr_proc_t));
601 rc = apr_proc_create(procnew, pname, (const char * const *) args,
602 NULL, procattr, pl->p);
604 if (rc == APR_SUCCESS) {
605 /* pjr - This no longer happens inside the child, */
606 /* I am assuming that if apr_proc_create was */
607 /* successful that the child is running. */
608 RAISE_SIGSTOP(PIPED_LOG_SPAWN);
610 ap_piped_log_write_fd(pl) = procnew->in;
611 apr_proc_other_child_register(procnew, piped_log_maintenance, pl,
612 ap_piped_log_write_fd(pl), pl->p);
620 static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
622 piped_log *pl = data;
626 case APR_OC_REASON_DEATH:
628 apr_proc_other_child_unregister(pl);
629 if (pl->program == NULL) {
630 /* during a restart */
634 case APR_OC_REASON_LOST:
636 apr_proc_other_child_unregister(pl);
637 if (pl->program == NULL) {
638 /* during a restart */
641 if ((stats = piped_log_spawn(pl)) != APR_SUCCESS) {
642 /* what can we do? This could be the error log we're having
643 * problems opening up... */
645 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
646 "piped_log_maintenance: unable to respawn '%s': %s",
647 pl->program, apr_strerror(stats, buf, sizeof(buf)));
651 case APR_OC_REASON_UNWRITABLE:
652 /* We should not kill off the pipe here, since it may only be full.
653 * If it really is locked, we should kill it off manually. */
656 case APR_OC_REASON_RESTART:
658 if (pl->pid != NULL) {
659 apr_proc_kill(pl->pid, SIGTERM);
663 case APR_OC_REASON_UNREGISTER:
669 static apr_status_t piped_log_cleanup(void *data)
671 piped_log *pl = data;
673 if (pl->pid != NULL) {
674 apr_proc_kill(pl->pid, SIGTERM);
676 apr_proc_other_child_unregister(pl);
677 apr_file_close(ap_piped_log_read_fd(pl));
678 apr_file_close(ap_piped_log_write_fd(pl));
683 static apr_status_t piped_log_cleanup_for_exec(void *data)
685 piped_log *pl = data;
687 apr_file_close(ap_piped_log_read_fd(pl));
688 apr_file_close(ap_piped_log_write_fd(pl));
692 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
696 pl = apr_palloc(p, sizeof (*pl));
698 pl->program = apr_pstrdup(p, program);
700 if (apr_file_pipe_create(&ap_piped_log_read_fd(pl), &ap_piped_log_write_fd(pl), p) != APR_SUCCESS) {
703 apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup_for_exec);
704 if (piped_log_spawn(pl) == -1) {
705 int save_errno = errno;
706 apr_pool_cleanup_kill(p, pl, piped_log_cleanup);
707 apr_file_close(ap_piped_log_read_fd(pl));
708 apr_file_close(ap_piped_log_write_fd(pl));
717 static apr_status_t piped_log_cleanup(void *data)
719 piped_log *pl = data;
721 apr_file_close(ap_piped_log_write_fd(pl));
725 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
728 apr_file_t *dummy = NULL;
731 rc = log_child(p, program, &dummy);
732 if (rc != APR_SUCCESS) {
733 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
734 "Couldn't start piped log process");
738 pl = apr_palloc(p, sizeof (*pl));
740 ap_piped_log_read_fd(pl) = NULL;
741 ap_piped_log_write_fd(pl) = dummy;
742 apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
749 AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
751 apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);