log.c revision ebc18d48bea83ee5ed7a1b4e30007e5192539829
842ae4bd224140319ae7feec1872b93dfd491143fielding/* ====================================================================
842ae4bd224140319ae7feec1872b93dfd491143fielding * The Apache Software License, Version 1.1
842ae4bd224140319ae7feec1872b93dfd491143fielding * Copyright (c) 2000 The Apache Software Foundation. All rights
842ae4bd224140319ae7feec1872b93dfd491143fielding * reserved.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Redistribution and use in source and binary forms, with or without
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * modification, are permitted provided that the following conditions
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 1. Redistributions of source code must retain the above copyright
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * notice, this list of conditions and the following disclaimer.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 2. Redistributions in binary form must reproduce the above copyright
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * notice, this list of conditions and the following disclaimer in
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the documentation and/or other materials provided with the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * distribution.
e8f95a682820a599fe41b22977010636be5c2717jim * 3. The end-user documentation included with the redistribution,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * if any, must include the following acknowledgment:
e8f95a682820a599fe41b22977010636be5c2717jim * "This product includes software developed by the
1747d30b98aa1bdbc43994c02cd46ab4cb9319e4fielding * Apache Software Foundation (http://www.apache.org/)."
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Alternately, this acknowledgment may appear in the software itself,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * if and wherever such third-party acknowledgments normally appear.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 4. The names "Apache" and "Apache Software Foundation" must
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * not be used to endorse or promote products derived from this
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * software without prior written permission. For written
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * permission, please contact apache@apache.org.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 5. Products derived from this software may not be called "Apache",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * nor may "Apache" appear in their name, without prior written
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * permission of the Apache Software Foundation.
5c0419d51818eb02045cf923a9fe456127a44c60wrowe * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * SUCH DAMAGE.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * ====================================================================
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes * This software consists of voluntary contributions made by many
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes * individuals on behalf of the Apache Software Foundation. For more
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes * information on the Apache Software Foundation, please see
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes * Portions of this software are based upon public domain software
cd3bbd6d2df78d6c75e5d159a81ef8bdd5f70df9trawick * originally written at the National Center for Supercomputing Applications,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * University of Illinois, Urbana-Champaign.
0568280364eb026393be492ebc732795c4934643jorton * http_log.c: Dealing with the logs and errors
0568280364eb026393be492ebc732795c4934643jorton * Rob McCool
8113dac419143273351446c3ad653f3fe5ba5cfdwrowetypedef struct {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int log_child(apr_pool_t *p, const char *progname,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Child process code for 'ErrorLog "|..."';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * may want a common framework for this, since I expect it will
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * be common for other foo-loggers to want this sort of thing...
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* No concept of a child process on Win32 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif /* ndef SIGHUP */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (((rc = apr_createprocattr_init(&procattr, p)) == APR_SUCCESS) &&
3e6d7277b90d3011db832139afc20efb5f17e203rederpj const char *pname;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener procnew = (apr_proc_t *) apr_pcalloc(p, sizeof(*procnew));
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener rc = apr_create_process(procnew, pname, (const char * const *)args,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_note_subprocess(p, procnew, kill_after_timeout);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerstatic void open_error_log(server_rec *s, apr_pool_t *p)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener const char *fname;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* This starts a new process... */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener "Couldn't start ErrorLog process");
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener else if (!strncasecmp(s->error_fname, "syslog", 6)) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "%s: could not open error log file %s.",
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholesvoid ap_open_logs(server_rec *s_main, apr_pool_t *p)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* replace stderr with this new log */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if ((rc = apr_dupfile(&errfile, s_main->error_log, p)) != APR_SUCCESS) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "unable to replace stderr with error_log");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* note that stderr may still need to be replaced with something
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * because it points to the old error log, or back to the tty
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * of the submitter.
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (replace_stderr && freopen("/dev/null", "w", stderr) == NULL) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_log_error(APLOG_MARK, APLOG_CRIT, errno, s_main,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "unable to replace stderr with /dev/null");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (virt = s_main->next; virt; virt = virt->next) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_dupfile(&(s->error_log), errfile, s->process->pool);
54d22ed1c429b903b029bbd62621f11a9e286137minfrinstatic void log_error_core(const char *file, int line, int level,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes const char *referer;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * If we are doing stderr logging (startup), don't log messages that are
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * above the default server log level unless it is a startup/shutdown
128a5d93141a86e3afa151e921035a07297c9833rederpj else if (s->error_log) {
9ad7b260be233be7d7b5576979825cac72e15498rederpj * If we are doing normal logging, don't log messages that are
128a5d93141a86e3afa151e921035a07297c9833rederpj * above the server log level unless it is a startup/shutdown notice
9ad7b260be233be7d7b5576979825cac72e15498rederpj * If we are doing normal logging, don't log messages that are
9ad7b260be233be7d7b5576979825cac72e15498rederpj * above the server log level unless it is a startup/shutdown notice
9ad7b260be233be7d7b5576979825cac72e15498rederpj#endif /* TPF */
9ad7b260be233be7d7b5576979825cac72e15498rederpj * If we are doing syslog logging, don't log messages that are
9ad7b260be233be7d7b5576979825cac72e15498rederpj * above the server log level (including a startup/shutdown notice)
9ad7b260be233be7d7b5576979825cac72e15498rederpj if (logf && ((level & APLOG_STARTUP) != APLOG_STARTUP)) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "[%s] ", priorities[level & APLOG_LEVELMASK].t_name);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (file && (level & APLOG_LEVELMASK) == APLOG_DEBUG) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* In OSD/POSIX, the compiler returns for __FILE__
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * (it even returns an absolute path for sources in
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * the current directory). Here we try to strip this
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * down to the basename.
141e1368614dc7564e1627671361b01b4869b491bnicholes if (*e == ')')
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes#endif /*_OSD_POSIX*/
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes#endif /* TPF */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (r && r->connection) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* XXX: TODO: add a method of selecting whether logged client
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * addresses are in dotted quad or resolved form... dotted
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes * quad is the most secure, which is why I'm implementing it
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * first. -djg
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes && (status != 0)) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "(%d)%s: ", status, apr_strerror(status, buf, sizeof(buf)));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes len += apr_vsnprintf(errstr + len, MAX_STRING_LEN - len, fmt, args);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (r && (referer = apr_table_get(r->headers_in, "Referer"))) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* NULL if we are logging to syslog */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* Truncate for the terminator (as apr_snprintf does) */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesAP_DECLARE(void) ap_log_error(const char *file, int line, int level,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes const char *fmt, ...)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes log_error_core(file, line, level, status, s, NULL, NULL, fmt, args);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAP_DECLARE(void) ap_log_perror(const char *file, int line, int level,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *fmt, ...)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes log_error_core(file, line, level, status, NULL, NULL, p, fmt, args);
6733d943c9e8d0f27dd077a04037e8c49eb090ffcovenerAP_DECLARE(void) ap_log_rerror(const char *file, int line, int level,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes const char *fmt, ...)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes log_error_core(file, line, level, status, r->server, r, NULL, fmt, args);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * IF the error level is 'warning' or more severe,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * AND there isn't already error text associated with this request,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * THEN make the message text available to ErrorDocument and
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * other error processors. This can be disabled by stuffing
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * something, even an empty string, into the "error-notes" cell
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * before calling this routine.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes && (apr_table_get(r->notes, "error-notes") == NULL)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (mypid != saved_pid && apr_stat(&finfo, fname, p) == APR_SUCCESS) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* WINCH and HUP call this on each restart.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Only warn on first time through for this pid.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * XXX: Could just write first time through too, although
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * that may screw up scripts written to do something
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * based on the last modification time of the pid file.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_perror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, p,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "pid file %s overwritten -- Unclean shutdown of previous Apache run?",
edc346c3223efd41e6a2057c37cea69744b73dccwrowe if ((rv = apr_open(&pid_file, fname, APR_WRITE | APR_CREATE | APR_TRUNCATE,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p)) != APR_SUCCESS) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, NULL,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "%s: could not log pid to file %s",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_fprintf(pid_file, "%ld" APR_EOL_STR, (long)mypid);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile, int nLine)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
e8f95a682820a599fe41b22977010636be5c2717jim "[%s] file %s, line %d, assertion \"%s\" failed",
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe /* unix assert does an abort leading to a core dump */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* piped log support */
9ad7b260be233be7d7b5576979825cac72e15498rederpj/* forward declaration */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic void piped_log_maintenance(int reason, void *data, apr_wait_t status);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (((status = apr_createprocattr_init(&procattr, pl->p)) != APR_SUCCESS) ||
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ((status = apr_setprocattr_childin(procattr, ap_piped_log_read_fd(pl),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Something bad happened, give up and go away. */
e8f95a682820a599fe41b22977010636be5c2717jim ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
e8f95a682820a599fe41b22977010636be5c2717jim "piped_log_spawn: unable to setup child process '%s': %s",
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe const char *pname;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rc = apr_create_process(procnew, pname, (const char * const *) args,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* pjr - This no longer happens inside the child, */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* I am assuming that if apr_create_process was */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* successful that the child is running. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_register_other_child(procnew, piped_log_maintenance, pl,
e8f95a682820a599fe41b22977010636be5c2717jimstatic void piped_log_maintenance(int reason, void *data, apr_wait_t status)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* during a restart */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* during a restart */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((stats = piped_log_spawn(pl)) != APR_SUCCESS) {
0e05808dc59a321566303084c84b9826a4353cefrederpj /* what can we do? This could be the error log we're having
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * problems opening up... */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener "piped_log_maintenance: unable to respawn '%s': %s",
0e05808dc59a321566303084c84b9826a4353cefrederpj pl->program, apr_strerror(stats, buf, sizeof(buf)));
ebe5305f8b22507374358f32b74d12fb50c05a25covener /* We should not kill off the pipe here, since it may only be full.
ebe5305f8b22507374358f32b74d12fb50c05a25covener * If it really is locked, we should kill it off manually. */
54d22ed1c429b903b029bbd62621f11a9e286137minfrinstatic apr_status_t piped_log_cleanup_for_exec(void *data)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (apr_create_pipe(&ap_piped_log_read_fd(pl), &ap_piped_log_write_fd(pl), p) != APR_SUCCESS) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_register_cleanup(p, pl, piped_log_cleanup, piped_log_cleanup_for_exec);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Couldn't start piped log process");