log.c revision 4bb7ba7c1f9972b443562faf99151ccec854446f
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* Copyright 1999-2004 The Apache Software Foundation
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding *
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * Licensed under the Apache License, Version 2.0 (the "License");
33bdcae1f7a1a65e351dda2a766a0cf28b1e695dnd * you may not use this file except in compliance with the License.
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * You may obtain a copy of the License at
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * http://www.apache.org/licenses/LICENSE-2.0
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Unless required by applicable law or agreed to in writing, software
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * distributed under the License is distributed on an "AS IS" BASIS,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * See the License for the specific language governing permissions and
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * limitations under the License.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/*
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * http_log.c: Dealing with the logs and errors
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * Rob McCool
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding *
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding */
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "apr.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "apr_general.h" /* for signal stuff */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "apr_strings.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "apr_errno.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "apr_thread_proc.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "apr_lib.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "apr_signal.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#define APR_WANT_STDIO
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#define APR_WANT_STRFUNC
64185f9824e42f21ca7b9ae6c004484215c031a7rbb#include "apr_want.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#if APR_HAVE_STDARG_H
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include <stdarg.h>
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#endif
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#if APR_HAVE_UNISTD_H
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include <unistd.h>
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#endif
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#define CORE_PRIVATE
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "ap_config.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "httpd.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "http_config.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "http_core.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "http_log.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "http_main.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "util_time.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "ap_mpm.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding char *t_name;
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding int t_val;
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding} TRANS;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingAPR_HOOK_STRUCT(
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding APR_HOOK_LINK(error_log)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingint AP_DECLARE_DATA ap_default_loglevel = DEFAULT_LOGLEVEL;
ba4c566c200c2436dae841b7c811807c80cd712afielding
ba4c566c200c2436dae841b7c811807c80cd712afielding#ifdef HAVE_SYSLOG
ba4c566c200c2436dae841b7c811807c80cd712afielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const TRANS facilities[] = {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"auth", LOG_AUTH},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef LOG_AUTHPRIV
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"authpriv",LOG_AUTHPRIV},
ba4c566c200c2436dae841b7c811807c80cd712afielding#endif
ba4c566c200c2436dae841b7c811807c80cd712afielding#ifdef LOG_CRON
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"cron", LOG_CRON},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef LOG_DAEMON
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"daemon", LOG_DAEMON},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef LOG_FTP
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"ftp", LOG_FTP},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef LOG_KERN
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"kern", LOG_KERN},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
ba4c566c200c2436dae841b7c811807c80cd712afielding#ifdef LOG_LPR
ba4c566c200c2436dae841b7c811807c80cd712afielding {"lpr", LOG_LPR},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef LOG_MAIL
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"mail", LOG_MAIL},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef LOG_NEWS
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"news", LOG_NEWS},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef LOG_SYSLOG
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"syslog", LOG_SYSLOG},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
ba4c566c200c2436dae841b7c811807c80cd712afielding#ifdef LOG_USER
ba4c566c200c2436dae841b7c811807c80cd712afielding {"user", LOG_USER},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef LOG_UUCP
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"uucp", LOG_UUCP},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
ba4c566c200c2436dae841b7c811807c80cd712afielding#ifdef LOG_LOCAL0
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"local0", LOG_LOCAL0},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
ba4c566c200c2436dae841b7c811807c80cd712afielding#ifdef LOG_LOCAL1
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"local1", LOG_LOCAL1},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef LOG_LOCAL2
024cd9589e52cf11ce765dfddb5b5f0c6e421a48gstein {"local2", LOG_LOCAL2},
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#endif
0d496deee49b66bb43883640fa9c1a7e884a1b8ctrawick#ifdef LOG_LOCAL3
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard {"local3", LOG_LOCAL3},
024cd9589e52cf11ce765dfddb5b5f0c6e421a48gstein#endif
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#ifdef LOG_LOCAL4
024cd9589e52cf11ce765dfddb5b5f0c6e421a48gstein {"local4", LOG_LOCAL4},
024cd9589e52cf11ce765dfddb5b5f0c6e421a48gstein#endif
024cd9589e52cf11ce765dfddb5b5f0c6e421a48gstein#ifdef LOG_LOCAL5
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"local5", LOG_LOCAL5},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
4cff088e460b3832142e59c63b357f8cf4d77fa8ake#ifdef LOG_LOCAL6
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard {"local6", LOG_LOCAL6},
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard#endif
7c4aca408b6678f99033503e588bbc2fdb836b09stoddard#ifdef LOG_LOCAL7
5fac45c1ef49924141fe28497deb350cf031b377trawick {"local7", LOG_LOCAL7},
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#endif
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard {NULL, -1},
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard};
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const TRANS priorities[] = {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"emerg", APLOG_EMERG},
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard {"alert", APLOG_ALERT},
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard {"crit", APLOG_CRIT},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"error", APLOG_ERR},
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {"warn", APLOG_WARNING},
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard {"notice", APLOG_NOTICE},
ba4c566c200c2436dae841b7c811807c80cd712afielding {"info", APLOG_INFO},
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar {"debug", APLOG_DEBUG},
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar {NULL, -1},
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard};
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddardstatic apr_file_t *stderr_log = NULL;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddardAP_DECLARE(void) ap_open_stderr_log(apr_pool_t *p)
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard{
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_file_open_stderr(&stderr_log, p);
0d73c7af09ed0dc265900f5db0c896dcd561524bben}
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddardAP_DECLARE(apr_status_t) ap_replace_stderr_log(apr_pool_t *p,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard const char *fname)
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard{
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_file_t *stderr_file;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_status_t rc;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *filename = ap_server_root_relative(p, fname);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!filename) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard APR_EBADPATH, NULL, "Invalid -E error log file %s",
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard fname);
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard return APR_EBADPATH;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((rc = apr_file_open(&stderr_file, filename,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard APR_OS_DEFAULT, p)) != APR_SUCCESS) {
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard "%s: could not open error log file %s.",
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard ap_server_argv0, fname);
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard return rc;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((rc = apr_file_open_stderr(&stderr_log, p)) == APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_file_flush(stderr_log);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((rc = apr_file_dup2(stderr_log, stderr_file, p)) == APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_file_close(stderr_file);
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard }
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (rc != APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_CRIT, rc, NULL,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "unable to replace stderr with error_log");
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return rc;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard}
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddardstatic void log_child_errfn(apr_pool_t *pool, apr_status_t err,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard const char *description)
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard{
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard ap_log_error(APLOG_MARK, APLOG_ERR, err, NULL,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard "%s", description);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard}
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
0329db7881b698897213e816552b0ab31f8d4d56trawickstatic int log_child(apr_pool_t *p, const char *progname,
0329db7881b698897213e816552b0ab31f8d4d56trawick apr_file_t **fpin)
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard{
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard /* Child process code for 'ErrorLog "|..."';
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard * may want a common framework for this, since I expect it will
0329db7881b698897213e816552b0ab31f8d4d56trawick * be common for other foo-loggers to want this sort of thing...
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard */
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh apr_status_t rc;
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh apr_procattr_t *procattr;
54449e92dcdd62884ea83c3e0fb0ced57fb65801jwoolley apr_proc_t *procnew;
a47a28784ca46876076471d2a0b45c11f800bffestoddard
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh if (((rc = apr_procattr_create(&procattr, p)) == APR_SUCCESS)
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh && ((rc = apr_procattr_cmdtype_set(procattr,
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh APR_SHELLCMD_ENV)) == APR_SUCCESS)
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh && ((rc = apr_procattr_io_set(procattr,
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh APR_FULL_BLOCK,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard APR_NO_PIPE,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard APR_NO_PIPE)) == APR_SUCCESS)
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard && ((rc = apr_procattr_error_check_set(procattr, 1)) == APR_SUCCESS)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm && ((rc = apr_procattr_child_errfn_set(procattr, log_child_errfn)) == APR_SUCCESS)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char **args;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein const char *pname;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard apr_tokenize_to_argv(progname, &args, p);
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard pname = apr_pstrdup(p, args[0]);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar procnew = (apr_proc_t *)apr_pcalloc(p, sizeof(*procnew));
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein rc = apr_proc_create(procnew, pname, (const char * const *)args,
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein NULL, procattr, p);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (rc == APR_SUCCESS) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding (*fpin) = procnew->in;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm return rc;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgsteinstatic int open_error_log(server_rec *s, apr_pool_t *p)
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *fname;
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard int rc;
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (*s->error_fname == '|') {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_file_t *dummy = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein /* This starts a new process... */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding rc = log_child (p, s->error_fname + 1, &dummy);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (rc != APR_SUCCESS) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard "Couldn't start ErrorLog process");
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard return DONE;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard s->error_log = dummy;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#ifdef HAVE_SYSLOG
a47a28784ca46876076471d2a0b45c11f800bffestoddard else if (!strncasecmp(s->error_fname, "syslog", 6)) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if ((fname = strchr(s->error_fname, ':'))) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard const TRANS *fac;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard fname++;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard for (fac = facilities; fac->t_name; fac++) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (!strcasecmp(fname, fac->t_name)) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard fac->t_val);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard s->error_log = NULL;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard return OK;
a47a28784ca46876076471d2a0b45c11f800bffestoddard }
a47a28784ca46876076471d2a0b45c11f800bffestoddard }
a47a28784ca46876076471d2a0b45c11f800bffestoddard }
a47a28784ca46876076471d2a0b45c11f800bffestoddard else {
a47a28784ca46876076471d2a0b45c11f800bffestoddard openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
a47a28784ca46876076471d2a0b45c11f800bffestoddard }
a47a28784ca46876076471d2a0b45c11f800bffestoddard
a47a28784ca46876076471d2a0b45c11f800bffestoddard s->error_log = NULL;
a47a28784ca46876076471d2a0b45c11f800bffestoddard }
a47a28784ca46876076471d2a0b45c11f800bffestoddard#endif
a47a28784ca46876076471d2a0b45c11f800bffestoddard else {
a47a28784ca46876076471d2a0b45c11f800bffestoddard fname = ap_server_root_relative(p, s->error_fname);
a47a28784ca46876076471d2a0b45c11f800bffestoddard if (!fname) {
a47a28784ca46876076471d2a0b45c11f800bffestoddard ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EBADPATH, NULL,
a47a28784ca46876076471d2a0b45c11f800bffestoddard "%s: Invalid error log path %s.",
a47a28784ca46876076471d2a0b45c11f800bffestoddard ap_server_argv0, s->error_fname);
a47a28784ca46876076471d2a0b45c11f800bffestoddard return DONE;
a47a28784ca46876076471d2a0b45c11f800bffestoddard }
a47a28784ca46876076471d2a0b45c11f800bffestoddard if ((rc = apr_file_open(&s->error_log, fname,
a47a28784ca46876076471d2a0b45c11f800bffestoddard APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
a47a28784ca46876076471d2a0b45c11f800bffestoddard APR_OS_DEFAULT, p)) != APR_SUCCESS) {
a47a28784ca46876076471d2a0b45c11f800bffestoddard ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
a47a28784ca46876076471d2a0b45c11f800bffestoddard "%s: could not open error log file %s.",
a47a28784ca46876076471d2a0b45c11f800bffestoddard ap_server_argv0, fname);
a47a28784ca46876076471d2a0b45c11f800bffestoddard return DONE;
a47a28784ca46876076471d2a0b45c11f800bffestoddard }
a47a28784ca46876076471d2a0b45c11f800bffestoddard }
a47a28784ca46876076471d2a0b45c11f800bffestoddard
a47a28784ca46876076471d2a0b45c11f800bffestoddard return OK;
a47a28784ca46876076471d2a0b45c11f800bffestoddard}
a47a28784ca46876076471d2a0b45c11f800bffestoddard
a47a28784ca46876076471d2a0b45c11f800bffestoddardint ap_open_logs(apr_pool_t *pconf, apr_pool_t *p /* plog */,
a47a28784ca46876076471d2a0b45c11f800bffestoddard apr_pool_t *ptemp, server_rec *s_main)
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard{
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_status_t rc = APR_SUCCESS;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard server_rec *virt, *q;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard int replace_stderr;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_file_t *errfile = NULL;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (open_error_log(s_main, p) != OK) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard return DONE;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
58ecd4abb0bc207016069618e7d2b9cb640046c4trawick replace_stderr = 1;
a47a28784ca46876076471d2a0b45c11f800bffestoddard if (s_main->error_log) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard /* replace stderr with this new log */
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_file_flush(s_main->error_log);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if ((rc = apr_file_open_stderr(&errfile, p)) == APR_SUCCESS) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard rc = apr_file_dup2(errfile, s_main->error_log, p);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh if (rc != APR_SUCCESS) {
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s_main,
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh "unable to replace stderr with error_log");
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh }
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh else {
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh replace_stderr = 0;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
58ecd4abb0bc207016069618e7d2b9cb640046c4trawick }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard /* note that stderr may still need to be replaced with something
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard * because it points to the old error log, or back to the tty
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard * of the submitter.
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard * XXX: This is BS - /dev/null is non-portable
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard */
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (replace_stderr && freopen("/dev/null", "w", stderr) == NULL) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard ap_log_error(APLOG_MARK, APLOG_CRIT, errno, s_main,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard "unable to replace stderr with /dev/null");
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard for (virt = s_main->next; virt; virt = virt->next) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (virt->error_fname) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard for (q=s_main; q != virt; q = q->next) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (q->error_fname != NULL
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard && strcmp(q->error_fname, virt->error_fname) == 0) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard break;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (q == virt) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (open_error_log(virt, p) != OK) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard return DONE;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard else {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard virt->error_log = q->error_log;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard else {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard virt->error_log = s_main->error_log;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard return OK;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard}
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddardAP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_file_t *errfile = NULL;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_file_open_stderr(&errfile, s->process->pool);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (s->error_log != NULL) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_file_dup2(s->error_log, errfile, s->process->pool);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ba4c566c200c2436dae841b7c811807c80cd712afieldingstatic void log_error_core(const char *file, int line, int level,
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_status_t status, const server_rec *s,
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar const request_rec *r, apr_pool_t *pool,
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar const char *fmt, va_list args)
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char errstr[MAX_STRING_LEN];
0d496deee49b66bb43883640fa9c1a7e884a1b8ctrawick#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
2dc8dcdf14f304577a6210479a4008f7cb6bc827trawick char scratch[MAX_STRING_LEN];
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard#endif
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_size_t len, errstrlen;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_file_t *logf = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *referer;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein int level_and_mask = level & APLOG_LEVELMASK;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (s == NULL) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar /*
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar * If we are doing stderr logging (startup), don't log messages that are
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar * above the default server log level unless it is a startup/shutdown
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar * notice
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar */
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if ((level_and_mask != APLOG_NOTICE)
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar && (level_and_mask > ap_default_loglevel)) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar return;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar logf = stderr_log;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar else if (s->error_log) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar /*
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar * If we are doing normal logging, don't log messages that are
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar * above the server log level unless it is a startup/shutdown notice
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar */
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if ((level_and_mask != APLOG_NOTICE)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding && (level_and_mask > s->loglevel)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding logf = s->error_log;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef TPF
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else if (tpf_child) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /*
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * If we are doing normal logging, don't log messages that are
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * above the server log level unless it is a startup/shutdown notice
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((level_and_mask != APLOG_NOTICE)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding && (level_and_mask > s->loglevel)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard logf = stderr;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard#endif /* TPF */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /*
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * If we are doing syslog logging, don't log messages that are
ba4c566c200c2436dae841b7c811807c80cd712afielding * above the server log level (including a startup/shutdown notice)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard if (level_and_mask > s->loglevel) {
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard return;
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard }
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard }
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard if (logf && ((level & APLOG_STARTUP) != APLOG_STARTUP)) {
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard errstr[0] = '[';
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard ap_recent_ctime(errstr + 1, apr_time_now());
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard errstr[1 + APR_CTIME_LEN - 1] = ']';
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard errstr[1 + APR_CTIME_LEN ] = ' ';
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard len = 1 + APR_CTIME_LEN + 1;
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard } else {
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard len = 0;
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ba4c566c200c2436dae841b7c811807c80cd712afielding if ((level & APLOG_STARTUP) != APLOG_STARTUP) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard "[%s] ", priorities[level_and_mask].t_name);
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar#ifndef TPF
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (file && level_and_mask == APLOG_DEBUG) {
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard#if defined(_OSD_POSIX) || defined(WIN32)
a47a28784ca46876076471d2a0b45c11f800bffestoddard char tmp[256];
a47a28784ca46876076471d2a0b45c11f800bffestoddard char *e = strrchr(file, '/');
a47a28784ca46876076471d2a0b45c11f800bffestoddard#ifdef WIN32
a47a28784ca46876076471d2a0b45c11f800bffestoddard if (!e) {
a47a28784ca46876076471d2a0b45c11f800bffestoddard e = strrchr(file, '\\');
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar#endif
a47a28784ca46876076471d2a0b45c11f800bffestoddard
a47a28784ca46876076471d2a0b45c11f800bffestoddard /* In OSD/POSIX, the compiler returns for __FILE__
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard * (it even returns an absolute path for sources in
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the current directory). Here we try to strip this
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * down to the basename.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (e != NULL && e[1] != '\0') {
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding e = &tmp[strlen(tmp)-1];
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (*e == ')') {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *e = '\0';
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard file = tmp;
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard }
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard#endif /*_OSD_POSIX*/
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
671d7810d2bfbbb809b12d13eb6533c459464671trawick "%s(%d): ", file, line);
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard }
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard#endif /* TPF */
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard if (r && r->connection) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar /* XXX: TODO: add a method of selecting whether logged client
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard * addresses are in dotted quad or resolved form... dotted
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard * quad is the most secure, which is why I'm implementing it
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard * first. -djg
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard */
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard "[client %s] ", r->connection->remote_ip);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard if (status != 0) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (status < APR_OS_START_EAIERR) {
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard "(%d)", status);
ba4c566c200c2436dae841b7c811807c80cd712afielding }
ec3c7f73162e58dc6aa163bdb4b1703ea3987afeminfrin else if (status < APR_OS_START_SYSERR) {
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard "(EAI %d)", status - APR_OS_START_EAIERR);
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard }
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard else if (status < 100000 + APR_OS_START_SYSERR) {
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard "(OS %d)", status - APR_OS_START_SYSERR);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard else {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard "(os 0x%08x)", status - APR_OS_START_SYSERR);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_strerror(status, errstr + len, MAX_STRING_LEN - len);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard len += strlen(errstr + len);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (MAX_STRING_LEN - len > 2) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard errstr[len++] = ':';
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard errstr[len++] = ' ';
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard errstr[len] = '\0';
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard errstrlen = len;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if (apr_vsnprintf(scratch, MAX_STRING_LEN - len, fmt, args)) {
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard len += ap_escape_errorlog_item(errstr + len, scratch,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard MAX_STRING_LEN - len);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard }
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#else
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard len += apr_vsnprintf(errstr + len, MAX_STRING_LEN - len, fmt, args);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#endif
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard if ( r && (referer = apr_table_get(r->headers_in, "Referer"))
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard && ap_escape_errorlog_item(scratch, referer, MAX_STRING_LEN - len)
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard#endif
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard ) {
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard ", referer: %s",
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard scratch
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#else
f2127630a87ab5952a3d173b164e600c99cff25astoddard referer
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard#endif
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard );
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard }
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* NULL if we are logging to syslog */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (logf) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar /* Truncate for the terminator (as apr_snprintf does) */
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR)) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar strcpy(errstr + len, APR_EOL_STR);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_file_puts(errstr, logf);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_file_flush(logf);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar#ifdef HAVE_SYSLOG
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar else {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar syslog(level_and_mask, "%s", errstr);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard ap_run_error_log(file, line, level, status, s, r, pool, errstr + errstrlen);
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard}
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddardAP_DECLARE(void) ap_log_error(const char *file, int line, int level,
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard apr_status_t status, const server_rec *s,
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard const char *fmt, ...)
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard{
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard va_list args;
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard va_start(args, fmt);
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard log_error_core(file, line, level, status, s, NULL, NULL, fmt, args);
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard va_end(args);
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard}
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddardAP_DECLARE(void) ap_log_perror(const char *file, int line, int level,
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard apr_status_t status, apr_pool_t *p,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *fmt, ...)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard va_list args;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding va_start(args, fmt);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard log_error_core(file, line, level, status, NULL, NULL, p, fmt, args);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding va_end(args);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingAP_DECLARE(void) ap_log_rerror(const char *file, int line, int level,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_status_t status, const request_rec *r,
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard const char *fmt, ...)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard va_list args;
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard va_start(args, fmt);
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard log_error_core(file, line, level, status, r->server, r, NULL, fmt, args);
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard /*
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard * IF APLOG_TOCLIENT is set,
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard * AND the error level is 'warning' or more severe,
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard * AND there isn't already error text associated with this request,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * THEN make the message text available to ErrorDocument and
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * other error processors.
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard */
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard va_end(args);
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard va_start(args,fmt);
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard if ((level & APLOG_TOCLIENT)
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard && ((level & APLOG_LEVELMASK) <= APLOG_WARNING)
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard && (apr_table_get(r->notes, "error-notes") == NULL)) {
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard apr_table_setn(r->notes, "error-notes",
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard args)));
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar va_end(args);
a47a28784ca46876076471d2a0b45c11f800bffestoddard}
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddardAP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard{
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_file_t *pid_file = NULL;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_finfo_t finfo;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar static pid_t saved_pid = -1;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar pid_t mypid;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_status_t rv;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar const char *fname;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (!filename) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar return;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar fname = ap_server_root_relative(p, filename);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (!fname) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar NULL, "Invalid PID file path %s, ignoring.", filename);
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard return;
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard }
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard mypid = getpid();
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard if (mypid != saved_pid
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard && apr_stat(&finfo, fname, APR_FINFO_MTIME, p) == APR_SUCCESS) {
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard /* AP_SIG_GRACEFUL and HUP call this on each restart.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Only warn on first time through for this pid.
12901074f5d6b36d08be84d8637b6f2c21e0da26trawick *
a47a28784ca46876076471d2a0b45c11f800bffestoddard * XXX: Could just write first time through too, although
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard * that may screw up scripts written to do something
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard * based on the last modification time of the pid file.
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar */
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p,
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar "pid file %s overwritten -- Unclean "
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard "shutdown of previous Apache run?",
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard fname);
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard }
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard if ((rv = apr_file_open(&pid_file, fname,
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard APR_WRITE | APR_CREATE | APR_TRUNCATE,
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p))
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding != APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar "could not create %s", fname);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar "%s: could not log pid to file %s",
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar ap_server_argv0, fname);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar exit(1);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_file_printf(pid_file, "%ld" APR_EOL_STR, (long)mypid);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_file_close(pid_file);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar saved_pid = mypid;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar}
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coarAP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar pid_t *mypid)
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar{
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar const apr_size_t BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_file_t *pid_file = NULL;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_status_t rv;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar const char *fname;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar char *buf, *endptr;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_size_t bytes_read;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (!filename) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar return APR_EGENERAL;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar fname = ap_server_root_relative(p, filename);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (!fname) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar NULL, "Invalid PID file path %s, ignoring.", filename);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar return APR_EGENERAL;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p);
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (rv != APR_SUCCESS) {
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar return rv;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar }
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Ensure null-termination, so that strtol doesn't go crazy. */
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein buf = apr_palloc(p, BUFFER_SIZE);
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein buf[BUFFER_SIZE - 1] = '\0';
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein rv = apr_file_read_full(pid_file, buf, BUFFER_SIZE - 1, &bytes_read);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (rv != APR_SUCCESS && rv != APR_EOF) {
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard return rv;
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard }
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard
3bc407b46a67691f3e5fd4f5f5ead4e1eff09998stoddard /* If we fill the buffer, we're probably reading a corrupt pid file.
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard * To be nice, let's also ensure the first char is a digit. */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (bytes_read == BUFFER_SIZE - 1 || !apr_isdigit(*buf)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return APR_EGENERAL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard *mypid = strtol(buf, &endptr, 10);
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard apr_file_close(pid_file);
fd709745f1226d683c57ad9bbcdd05971d214d0cstoddard return APR_SUCCESS;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar}
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coarAP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile,
ec3c7f73162e58dc6aa163bdb4b1703ea3987afeminfrin int nLine)
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard{
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard char time_str[APR_CTIME_LEN];
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
73e8b26287de5c06fa470d36162e103dbac9c7e5wrowe apr_ctime(time_str, apr_time_now());
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard "[%s] file %s, line %d, assertion \"%s\" failed",
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard time_str, szFile, nLine, szExp);
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#if defined(WIN32)
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard DebugBreak();
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#else
417f504d4d11631c0d062be85347f82a26c88677aaron /* unix assert does an abort leading to a core dump */
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard abort();
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard#endif
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard}
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard
ed4b039edaff2a734bd8da9c33e43e2fb8dd0db9ianh/* piped log support */
417f504d4d11631c0d062be85347f82a26c88677aaron
417f504d4d11631c0d062be85347f82a26c88677aaron#ifdef AP_HAVE_RELIABLE_PIPED_LOGS
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard/* forward declaration */
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddardstatic void piped_log_maintenance(int reason, void *data, apr_wait_t status);
73e8b26287de5c06fa470d36162e103dbac9c7e5wrowe
577a76180006add04a166b12f1ad130aeedeaa5estoddardstatic int piped_log_spawn(piped_log *pl)
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar{
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar int rc = 0;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_procattr_t *procattr;
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar apr_proc_t *procnew = NULL;
fd0075570654d8f3473f12c47f507c8b3c59a8e4stoddard apr_status_t status;
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
71f3601de4983bc2a6aaffcf37dc1d35c8674a34coar ((status = apr_procattr_cmdtype_set(procattr,
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard APR_SHELLCMD_ENV)) != APR_SUCCESS) ||
ea92d0ffcb30b186010a2c8ca2c80d2ac09e34dastoddard ((status = apr_procattr_child_in_set(procattr,
73e8b26287de5c06fa470d36162e103dbac9c7e5wrowe ap_piped_log_read_fd(pl),
3d96ee83babeec32482c9082c9426340cee8c44dwrowe ap_piped_log_write_fd(pl)))
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding != APR_SUCCESS) ||
577a76180006add04a166b12f1ad130aeedeaa5estoddard ((status = apr_procattr_child_errfn_set(procattr, log_child_errfn))
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding != APR_SUCCESS) ||
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ((status = apr_procattr_error_check_set(procattr, 1)) != APR_SUCCESS)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char buf[120];
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Something bad happened, give up and go away. */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
577a76180006add04a166b12f1ad130aeedeaa5estoddard "piped_log_spawn: unable to setup child process '%s': %s",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding pl->program, apr_strerror(status, buf, sizeof(buf)));
rc = -1;
}
else {
char **args;
const char *pname;
apr_tokenize_to_argv(pl->program, &args, pl->p);
pname = apr_pstrdup(pl->p, args[0]);
procnew = apr_pcalloc(pl->p, sizeof(apr_proc_t));
status = apr_proc_create(procnew, pname, (const char * const *) args,
NULL, procattr, pl->p);
if (status == APR_SUCCESS) {
pl->pid = procnew;
ap_piped_log_write_fd(pl) = procnew->in;
apr_proc_other_child_register(procnew, piped_log_maintenance, pl,
ap_piped_log_write_fd(pl), pl->p);
}
else {
char buf[120];
/* Something bad happened, give up and go away. */
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
"unable to start piped log program '%s': %s",
pl->program, apr_strerror(status, buf, sizeof(buf)));
rc = -1;
}
}
return rc;
}
static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
{
piped_log *pl = data;
apr_status_t stats;
int mpm_state;
switch (reason) {
case APR_OC_REASON_DEATH:
case APR_OC_REASON_LOST:
pl->pid = NULL; /* in case we don't get it going again, this
* tells other logic not to try to kill it
*/
apr_proc_other_child_unregister(pl);
stats = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
if (stats != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
"can't query MPM state; not restarting "
"piped log program '%s'",
pl->program);
}
else if (mpm_state != AP_MPMQ_STOPPING) {
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
"piped log program '%s' failed unexpectedly",
pl->program);
if ((stats = piped_log_spawn(pl)) != APR_SUCCESS) {
/* what can we do? This could be the error log we're having
* problems opening up... */
char buf[120];
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
"piped_log_maintenance: unable to respawn '%s': %s",
pl->program, apr_strerror(stats, buf, sizeof(buf)));
}
}
break;
case APR_OC_REASON_UNWRITABLE:
/* We should not kill off the pipe here, since it may only be full.
* If it really is locked, we should kill it off manually. */
break;
case APR_OC_REASON_RESTART:
if (pl->pid != NULL) {
apr_proc_kill(pl->pid, SIGTERM);
pl->pid = NULL;
}
break;
case APR_OC_REASON_UNREGISTER:
break;
}
}
static apr_status_t piped_log_cleanup_for_exec(void *data)
{
piped_log *pl = data;
apr_file_close(ap_piped_log_read_fd(pl));
apr_file_close(ap_piped_log_write_fd(pl));
return APR_SUCCESS;
}
static apr_status_t piped_log_cleanup(void *data)
{
piped_log *pl = data;
if (pl->pid != NULL) {
apr_proc_kill(pl->pid, SIGTERM);
}
return piped_log_cleanup_for_exec(data);
}
AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
{
piped_log *pl;
pl = apr_palloc(p, sizeof (*pl));
pl->p = p;
pl->program = apr_pstrdup(p, program);
pl->pid = NULL;
if (apr_file_pipe_create(&ap_piped_log_read_fd(pl),
&ap_piped_log_write_fd(pl), p) != APR_SUCCESS) {
return NULL;
}
apr_pool_cleanup_register(p, pl, piped_log_cleanup,
piped_log_cleanup_for_exec);
if (piped_log_spawn(pl) == -1) {
int save_errno = errno;
apr_pool_cleanup_kill(p, pl, piped_log_cleanup);
apr_file_close(ap_piped_log_read_fd(pl));
apr_file_close(ap_piped_log_write_fd(pl));
errno = save_errno;
return NULL;
}
return pl;
}
#else /* !AP_HAVE_RELIABLE_PIPED_LOGS */
static apr_status_t piped_log_cleanup(void *data)
{
piped_log *pl = data;
apr_file_close(ap_piped_log_write_fd(pl));
return APR_SUCCESS;
}
AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
{
piped_log *pl;
apr_file_t *dummy = NULL;
int rc;
rc = log_child(p, program, &dummy);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
"Couldn't start piped log process");
return NULL;
}
pl = apr_palloc(p, sizeof (*pl));
pl->p = p;
ap_piped_log_read_fd(pl) = NULL;
ap_piped_log_write_fd(pl) = dummy;
apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
return pl;
}
#endif
AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
{
apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
}
AP_IMPLEMENT_HOOK_VOID(error_log,
(const char *file, int line, int level,
apr_status_t status, const server_rec *s,
const request_rec *r, apr_pool_t *pool,
const char *errstr), (file, line, level,
status, s, r, pool, errstr))