Blame server/log.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * http_log.c: Dealing with the logs and errors
Packit 90a5c9
 *
Packit 90a5c9
 * Rob McCool
Packit 90a5c9
 *
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr.h"
Packit 90a5c9
#include "apr_general.h"        /* for signal stuff */
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_errno.h"
Packit 90a5c9
#include "apr_thread_proc.h"
Packit 90a5c9
#include "apr_lib.h"
Packit 90a5c9
#include "apr_signal.h"
Packit 90a5c9
#include "apr_portable.h"
Packit 90a5c9
#include "apr_base64.h"
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STDIO
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#if APR_HAVE_STDARG_H
Packit 90a5c9
#include <stdarg.h>
Packit 90a5c9
#endif
Packit 90a5c9
#if APR_HAVE_UNISTD_H
Packit 90a5c9
#include <unistd.h>
Packit 90a5c9
#endif
Packit 90a5c9
#if APR_HAVE_PROCESS_H
Packit 90a5c9
#include <process.h>            /* for getpid() on Win32 */
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "http_main.h"
Packit 90a5c9
#include "util_time.h"
Packit 90a5c9
#include "ap_mpm.h"
Packit 90a5c9
#include "ap_listen.h"
Packit 90a5c9
Packit 90a5c9
#if HAVE_GETTID
Packit 90a5c9
#include <sys/syscall.h>
Packit 90a5c9
#include <sys/types.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
/* we know core's module_index is 0 */
Packit 90a5c9
#undef APLOG_MODULE_INDEX
Packit 90a5c9
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    const char *t_name;
Packit 90a5c9
    int t_val;
Packit 90a5c9
} TRANS;
Packit 90a5c9
Packit 90a5c9
APR_HOOK_STRUCT(
Packit 90a5c9
    APR_HOOK_LINK(error_log)
Packit 90a5c9
    APR_HOOK_LINK(generate_log_id)
Packit 90a5c9
)
Packit 90a5c9
Packit 90a5c9
int AP_DECLARE_DATA ap_default_loglevel = DEFAULT_LOGLEVEL;
Packit 90a5c9
Packit 90a5c9
#ifdef HAVE_SYSLOG
Packit 90a5c9
Packit 90a5c9
static const TRANS facilities[] = {
Packit 90a5c9
    {"auth",    LOG_AUTH},
Packit 90a5c9
#ifdef LOG_AUTHPRIV
Packit 90a5c9
    {"authpriv",LOG_AUTHPRIV},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_CRON
Packit 90a5c9
    {"cron",    LOG_CRON},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_DAEMON
Packit 90a5c9
    {"daemon",  LOG_DAEMON},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_FTP
Packit 90a5c9
    {"ftp", LOG_FTP},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_KERN
Packit 90a5c9
    {"kern",    LOG_KERN},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_LPR
Packit 90a5c9
    {"lpr", LOG_LPR},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_MAIL
Packit 90a5c9
    {"mail",    LOG_MAIL},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_NEWS
Packit 90a5c9
    {"news",    LOG_NEWS},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_SYSLOG
Packit 90a5c9
    {"syslog",  LOG_SYSLOG},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_USER
Packit 90a5c9
    {"user",    LOG_USER},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_UUCP
Packit 90a5c9
    {"uucp",    LOG_UUCP},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_LOCAL0
Packit 90a5c9
    {"local0",  LOG_LOCAL0},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_LOCAL1
Packit 90a5c9
    {"local1",  LOG_LOCAL1},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_LOCAL2
Packit 90a5c9
    {"local2",  LOG_LOCAL2},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_LOCAL3
Packit 90a5c9
    {"local3",  LOG_LOCAL3},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_LOCAL4
Packit 90a5c9
    {"local4",  LOG_LOCAL4},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_LOCAL5
Packit 90a5c9
    {"local5",  LOG_LOCAL5},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_LOCAL6
Packit 90a5c9
    {"local6",  LOG_LOCAL6},
Packit 90a5c9
#endif
Packit 90a5c9
#ifdef LOG_LOCAL7
Packit 90a5c9
    {"local7",  LOG_LOCAL7},
Packit 90a5c9
#endif
Packit 90a5c9
    {NULL,      -1},
Packit 90a5c9
};
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
static const TRANS priorities[] = {
Packit 90a5c9
    {"emerg",   APLOG_EMERG},
Packit 90a5c9
    {"alert",   APLOG_ALERT},
Packit 90a5c9
    {"crit",    APLOG_CRIT},
Packit 90a5c9
    {"error",   APLOG_ERR},
Packit 90a5c9
    {"warn",    APLOG_WARNING},
Packit 90a5c9
    {"notice",  APLOG_NOTICE},
Packit 90a5c9
    {"info",    APLOG_INFO},
Packit 90a5c9
    {"debug",   APLOG_DEBUG},
Packit 90a5c9
    {"trace1",  APLOG_TRACE1},
Packit 90a5c9
    {"trace2",  APLOG_TRACE2},
Packit 90a5c9
    {"trace3",  APLOG_TRACE3},
Packit 90a5c9
    {"trace4",  APLOG_TRACE4},
Packit 90a5c9
    {"trace5",  APLOG_TRACE5},
Packit 90a5c9
    {"trace6",  APLOG_TRACE6},
Packit 90a5c9
    {"trace7",  APLOG_TRACE7},
Packit 90a5c9
    {"trace8",  APLOG_TRACE8},
Packit 90a5c9
    {NULL,      -1},
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static apr_pool_t *stderr_pool = NULL;
Packit 90a5c9
Packit 90a5c9
static apr_file_t *stderr_log = NULL;
Packit 90a5c9
Packit 90a5c9
/* track pipe handles to close in child process */
Packit 90a5c9
typedef struct read_handle_t {
Packit 90a5c9
    struct read_handle_t *next;
Packit 90a5c9
    apr_file_t *handle;
Packit 90a5c9
} read_handle_t;
Packit 90a5c9
Packit 90a5c9
static read_handle_t *read_handles;
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * @brief The piped logging structure.
Packit 90a5c9
 *
Packit 90a5c9
 * Piped logs are used to move functionality out of the main server.
Packit 90a5c9
 * For example, log rotation is done with piped logs.
Packit 90a5c9
 */
Packit 90a5c9
struct piped_log {
Packit 90a5c9
    /** The pool to use for the piped log */
Packit 90a5c9
    apr_pool_t *p;
Packit 90a5c9
    /** The pipe between the server and the logging process */
Packit 90a5c9
    apr_file_t *read_fd, *write_fd;
Packit 90a5c9
#ifdef AP_HAVE_RELIABLE_PIPED_LOGS
Packit 90a5c9
    /** The name of the program the logging process is running */
Packit 90a5c9
    char *program;
Packit 90a5c9
    /** The pid of the logging process */
Packit 90a5c9
    apr_proc_t *pid;
Packit 90a5c9
    /** How to reinvoke program when it must be replaced */
Packit 90a5c9
    apr_cmdtype_e cmdtype;
Packit 90a5c9
#endif
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_file_t *) ap_piped_log_read_fd(piped_log *pl)
Packit 90a5c9
{
Packit 90a5c9
    return pl->read_fd;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_file_t *) ap_piped_log_write_fd(piped_log *pl)
Packit 90a5c9
{
Packit 90a5c9
    return pl->write_fd;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* remember to close this handle in the child process
Packit 90a5c9
 *
Packit 90a5c9
 * On Win32 this makes zero sense, because we don't
Packit 90a5c9
 * take the parent process's child procs.
Packit 90a5c9
 * If the win32 parent instead passed each and every
Packit 90a5c9
 * logger write handle from itself down to the child,
Packit 90a5c9
 * and the parent manages all aspects of keeping the
Packit 90a5c9
 * reliable pipe log children alive, this would still
Packit 90a5c9
 * make no sense :)  Cripple it on Win32.
Packit 90a5c9
 */
Packit 90a5c9
static void close_handle_in_child(apr_pool_t *p, apr_file_t *f)
Packit 90a5c9
{
Packit 90a5c9
#ifndef WIN32
Packit 90a5c9
    read_handle_t *new_handle;
Packit 90a5c9
Packit 90a5c9
    new_handle = apr_pcalloc(p, sizeof(read_handle_t));
Packit 90a5c9
    new_handle->next = read_handles;
Packit 90a5c9
    new_handle->handle = f;
Packit 90a5c9
    read_handles = new_handle;
Packit 90a5c9
#endif
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
void ap_logs_child_init(apr_pool_t *p, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    read_handle_t *cur = read_handles;
Packit 90a5c9
Packit 90a5c9
    while (cur) {
Packit 90a5c9
        apr_file_close(cur->handle);
Packit 90a5c9
        cur = cur->next;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_open_stderr_log(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    apr_file_open_stderr(&stderr_log, p);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_replace_stderr_log(apr_pool_t *p,
Packit 90a5c9
                                               const char *fname)
Packit 90a5c9
{
Packit 90a5c9
    apr_file_t *stderr_file;
Packit 90a5c9
    apr_status_t rc;
Packit 90a5c9
    char *filename = ap_server_root_relative(p, fname);
Packit 90a5c9
    if (!filename) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
Packit 90a5c9
                     APR_EBADPATH, ap_server_conf, APLOGNO(00085) "Invalid -E error log file %s",
Packit 90a5c9
                     fname);
Packit 90a5c9
        return APR_EBADPATH;
Packit 90a5c9
    }
Packit 90a5c9
    if ((rc = apr_file_open(&stderr_file, filename,
Packit 90a5c9
                            APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
Packit 90a5c9
                            APR_OS_DEFAULT, p)) != APR_SUCCESS) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, ap_server_conf, APLOGNO(00086)
Packit 90a5c9
                     "%s: could not open error log file %s.",
Packit 90a5c9
                     ap_server_argv0, fname);
Packit 90a5c9
        return rc;
Packit 90a5c9
    }
Packit 90a5c9
    if (!stderr_pool) {
Packit 90a5c9
        /* This is safe provided we revert it when we are finished.
Packit 90a5c9
         * We don't manager the callers pool!
Packit 90a5c9
         */
Packit 90a5c9
        stderr_pool = p;
Packit 90a5c9
    }
Packit 90a5c9
    if ((rc = apr_file_open_stderr(&stderr_log, stderr_pool))
Packit 90a5c9
            == APR_SUCCESS) {
Packit 90a5c9
        apr_file_flush(stderr_log);
Packit 90a5c9
        if ((rc = apr_file_dup2(stderr_log, stderr_file, stderr_pool))
Packit 90a5c9
                == APR_SUCCESS) {
Packit 90a5c9
            apr_file_close(stderr_file);
Packit 90a5c9
            /*
Packit 90a5c9
             * You might ponder why stderr_pool should survive?
Packit 90a5c9
             * The trouble is, stderr_pool may have s_main->error_log,
Packit 90a5c9
             * so we aren't in a position to destory stderr_pool until
Packit 90a5c9
             * the next recycle.  There's also an apparent bug which
Packit 90a5c9
             * is not; if some folk decided to call this function before
Packit 90a5c9
             * the core open error logs hook, this pool won't survive.
Packit 90a5c9
             * Neither does the stderr logger, so this isn't a problem.
Packit 90a5c9
             */
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    /* Revert, see above */
Packit 90a5c9
    if (stderr_pool == p)
Packit 90a5c9
        stderr_pool = NULL;
Packit 90a5c9
Packit 90a5c9
    if (rc != APR_SUCCESS) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_CRIT, rc, NULL, APLOGNO(00087)
Packit 90a5c9
                     "unable to replace stderr with error log file");
Packit 90a5c9
    }
Packit 90a5c9
    return rc;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void log_child_errfn(apr_pool_t *pool, apr_status_t err,
Packit 90a5c9
                            const char *description)
Packit 90a5c9
{
Packit 90a5c9
    ap_log_error(APLOG_MARK, APLOG_ERR, err, NULL, APLOGNO(00088)
Packit 90a5c9
                 "%s", description);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* Create a child process running PROGNAME with a pipe connected to
Packit 90a5c9
 * the childs stdin.  The write-end of the pipe will be placed in
Packit 90a5c9
 * *FPIN on successful return.  If dummy_stderr is non-zero, the
Packit 90a5c9
 * stderr for the child will be the same as the stdout of the parent.
Packit 90a5c9
 * Otherwise the child will inherit the stderr from the parent. */
Packit 90a5c9
static int log_child(apr_pool_t *p, const char *progname,
Packit 90a5c9
                     apr_file_t **fpin, apr_cmdtype_e cmdtype,
Packit 90a5c9
                     int dummy_stderr)
Packit 90a5c9
{
Packit 90a5c9
    /* Child process code for 'ErrorLog "|..."';
Packit 90a5c9
     * may want a common framework for this, since I expect it will
Packit 90a5c9
     * be common for other foo-loggers to want this sort of thing...
Packit 90a5c9
     */
Packit 90a5c9
    apr_status_t rc;
Packit 90a5c9
    apr_procattr_t *procattr;
Packit 90a5c9
    apr_proc_t *procnew;
Packit 90a5c9
    apr_file_t *errfile;
Packit 90a5c9
Packit 90a5c9
    if (((rc = apr_procattr_create(&procattr, p)) == APR_SUCCESS)
Packit 90a5c9
        && ((rc = apr_procattr_dir_set(procattr,
Packit 90a5c9
                                       ap_server_root)) == APR_SUCCESS)
Packit 90a5c9
        && ((rc = apr_procattr_cmdtype_set(procattr, cmdtype)) == APR_SUCCESS)
Packit 90a5c9
        && ((rc = apr_procattr_io_set(procattr,
Packit 90a5c9
                                      APR_FULL_BLOCK,
Packit 90a5c9
                                      APR_NO_PIPE,
Packit 90a5c9
                                      APR_NO_PIPE)) == APR_SUCCESS)
Packit 90a5c9
        && ((rc = apr_procattr_error_check_set(procattr, 1)) == APR_SUCCESS)
Packit 90a5c9
        && ((rc = apr_procattr_child_errfn_set(procattr, log_child_errfn))
Packit 90a5c9
                == APR_SUCCESS)) {
Packit 90a5c9
        char **args;
Packit 90a5c9
Packit 90a5c9
        apr_tokenize_to_argv(progname, &args, p);
Packit 90a5c9
        procnew = (apr_proc_t *)apr_pcalloc(p, sizeof(*procnew));
Packit 90a5c9
Packit 90a5c9
        if (dummy_stderr) {
Packit 90a5c9
            if ((rc = apr_file_open_stdout(&errfile, p)) == APR_SUCCESS)
Packit 90a5c9
                rc = apr_procattr_child_err_set(procattr, errfile, NULL);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        rc = apr_proc_create(procnew, args[0], (const char * const *)args,
Packit 90a5c9
                             NULL, procattr, p);
Packit 90a5c9
Packit 90a5c9
        if (rc == APR_SUCCESS) {
Packit 90a5c9
            apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
Packit 90a5c9
            (*fpin) = procnew->in;
Packit 90a5c9
            /* read handle to pipe not kept open, so no need to call
Packit 90a5c9
             * close_handle_in_child()
Packit 90a5c9
             */
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return rc;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* Open the error log for the given server_rec.  If IS_MAIN is
Packit 90a5c9
 * non-zero, s is the main server. */
Packit 90a5c9
static int open_error_log(server_rec *s, int is_main, apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    const char *fname;
Packit 90a5c9
    int rc;
Packit 90a5c9
Packit 90a5c9
    if (*s->error_fname == '|') {
Packit 90a5c9
        apr_file_t *dummy = NULL;
Packit 90a5c9
        apr_cmdtype_e cmdtype = APR_PROGRAM_ENV;
Packit 90a5c9
        fname = s->error_fname + 1;
Packit 90a5c9
Packit 90a5c9
        /* In 2.4 favor PROGRAM_ENV, accept "||prog" syntax for compatibility
Packit 90a5c9
         * and "|$cmd" to override the default.
Packit 90a5c9
         * Any 2.2 backport would continue to favor SHELLCMD_ENV so there
Packit 90a5c9
         * accept "||prog" to override, and "|$cmd" to ease conversion.
Packit 90a5c9
         */
Packit 90a5c9
        if (*fname == '|')
Packit 90a5c9
            ++fname;
Packit 90a5c9
        if (*fname == '$') {
Packit 90a5c9
            cmdtype = APR_SHELLCMD_ENV;
Packit 90a5c9
            ++fname;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Spawn a new child logger.  If this is the main server_rec,
Packit 90a5c9
         * the new child must use a dummy stderr since the current
Packit 90a5c9
         * stderr might be a pipe to the old logger.  Otherwise, the
Packit 90a5c9
         * child inherits the parents stderr. */
Packit 90a5c9
        rc = log_child(p, fname, &dummy, cmdtype, is_main);
Packit 90a5c9
        if (rc != APR_SUCCESS) {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, ap_server_conf, APLOGNO(00089)
Packit 90a5c9
                         "Couldn't start ErrorLog process '%s'.",
Packit 90a5c9
                         s->error_fname + 1);
Packit 90a5c9
            return DONE;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        s->error_log = dummy;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
#ifdef HAVE_SYSLOG
Packit 90a5c9
    else if (strcmp(s->error_fname, "syslog") == 0
Packit 90a5c9
             || strncmp(s->error_fname, "syslog:", 7) == 0) {
Packit 90a5c9
        if ((fname = strchr(s->error_fname, ':'))) {
Packit 90a5c9
            /* s->error_fname could be [level]:[tag] (see #60525) */
Packit 90a5c9
            const char *tag;
Packit 90a5c9
            apr_size_t flen;
Packit 90a5c9
            const TRANS *fac;
Packit 90a5c9
Packit 90a5c9
            fname++;
Packit 90a5c9
            tag = ap_strchr_c(fname, ':');
Packit 90a5c9
            if (tag) {
Packit 90a5c9
                flen = tag - fname;
Packit 90a5c9
                tag++;
Packit 90a5c9
                if (*tag == '\0') {
Packit 90a5c9
                    tag = ap_server_argv0;
Packit 90a5c9
                }
Packit 90a5c9
            } else {
Packit 90a5c9
                flen = strlen(fname);
Packit 90a5c9
                tag = ap_server_argv0;
Packit 90a5c9
            }
Packit 90a5c9
            if (flen == 0) {
Packit 90a5c9
                /* Was something like syslog::foobar */
Packit 90a5c9
                openlog(tag, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
Packit 90a5c9
            } else {
Packit 90a5c9
                for (fac = facilities; fac->t_name; fac++) {
Packit 90a5c9
                    if (!strncasecmp(fname, fac->t_name, flen)) {
Packit 90a5c9
                        openlog(tag, LOG_NDELAY|LOG_CONS|LOG_PID,
Packit 90a5c9
                                fac->t_val);
Packit 90a5c9
                        s->error_log = NULL;
Packit 90a5c9
                        return OK;
Packit 90a5c9
                    }
Packit 90a5c9
                }
Packit 90a5c9
                /* Huh? Invalid level name? */
Packit 90a5c9
                ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EBADPATH, NULL, APLOGNO(10036)
Packit 90a5c9
                             "%s: could not open syslog error log %s.",
Packit 90a5c9
                              ap_server_argv0, fname);
Packit 90a5c9
                return DONE;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        s->error_log = NULL;
Packit 90a5c9
    }
Packit 90a5c9
#endif
Packit 90a5c9
    else {
Packit 90a5c9
        fname = ap_server_root_relative(p, s->error_fname);
Packit 90a5c9
        if (!fname) {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EBADPATH, ap_server_conf, APLOGNO(00090)
Packit 90a5c9
                         "%s: Invalid error log path %s.",
Packit 90a5c9
                         ap_server_argv0, s->error_fname);
Packit 90a5c9
            return DONE;
Packit 90a5c9
        }
Packit 90a5c9
        if ((rc = apr_file_open(&s->error_log, fname,
Packit 90a5c9
                               APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
Packit 90a5c9
                               APR_OS_DEFAULT, p)) != APR_SUCCESS) {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, ap_server_conf, APLOGNO(00091)
Packit 90a5c9
                         "%s: could not open error log file %s.",
Packit 90a5c9
                         ap_server_argv0, fname);
Packit 90a5c9
            return DONE;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
int ap_open_logs(apr_pool_t *pconf, apr_pool_t *p /* plog */,
Packit 90a5c9
                 apr_pool_t *ptemp, server_rec *s_main)
Packit 90a5c9
{
Packit 90a5c9
    apr_pool_t *stderr_p;
Packit 90a5c9
    server_rec *virt, *q;
Packit 90a5c9
    int replace_stderr;
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
    /* Register to throw away the read_handles list when we
Packit 90a5c9
     * cleanup plog.  Upon fork() for the apache children,
Packit 90a5c9
     * this read_handles list is closed so only the parent
Packit 90a5c9
     * can relaunch a lost log child.  These read handles
Packit 90a5c9
     * are always closed on exec.
Packit 90a5c9
     * We won't care what happens to our stderr log child
Packit 90a5c9
     * between log phases, so we don't mind losing stderr's
Packit 90a5c9
     * read_handle a little bit early.
Packit 90a5c9
     */
Packit 90a5c9
    apr_pool_cleanup_register(p, &read_handles, ap_pool_cleanup_set_null,
Packit 90a5c9
                              apr_pool_cleanup_null);
Packit 90a5c9
Packit 90a5c9
    /* HERE we need a stdout log that outlives plog.
Packit 90a5c9
     * We *presume* the parent of plog is a process
Packit 90a5c9
     * or global pool which spans server restarts.
Packit 90a5c9
     * Create our stderr_pool as a child of the plog's
Packit 90a5c9
     * parent pool.
Packit 90a5c9
     */
Packit 90a5c9
    apr_pool_create(&stderr_p, apr_pool_parent_get(p));
Packit 90a5c9
    apr_pool_tag(stderr_p, "stderr_pool");
Packit 90a5c9
Packit 90a5c9
    if (open_error_log(s_main, 1, stderr_p) != OK) {
Packit 90a5c9
        return DONE;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    replace_stderr = 1;
Packit 90a5c9
    if (s_main->error_log) {
Packit 90a5c9
        apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
        /* Replace existing stderr with new log. */
Packit 90a5c9
        apr_file_flush(s_main->error_log);
Packit 90a5c9
        rv = apr_file_dup2(stderr_log, s_main->error_log, stderr_p);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s_main, APLOGNO(00092)
Packit 90a5c9
                         "unable to replace stderr with error_log");
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            /* We are done with stderr_pool, close it, killing
Packit 90a5c9
             * the previous generation's stderr logger
Packit 90a5c9
             */
Packit 90a5c9
            if (stderr_pool)
Packit 90a5c9
                apr_pool_destroy(stderr_pool);
Packit 90a5c9
            stderr_pool = stderr_p;
Packit 90a5c9
            replace_stderr = 0;
Packit 90a5c9
            /*
Packit 90a5c9
             * Now that we have dup'ed s_main->error_log to stderr_log
Packit 90a5c9
             * close it and set s_main->error_log to stderr_log. This avoids
Packit 90a5c9
             * this fd being inherited by the next piped logger who would
Packit 90a5c9
             * keep open the writing end of the pipe that this one uses
Packit 90a5c9
             * as stdin. This in turn would prevent the piped logger from
Packit 90a5c9
             * exiting.
Packit 90a5c9
             */
Packit 90a5c9
            apr_file_close(s_main->error_log);
Packit 90a5c9
            s_main->error_log = stderr_log;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    /* note that stderr may still need to be replaced with something
Packit 90a5c9
     * because it points to the old error log, or back to the tty
Packit 90a5c9
     * of the submitter.
Packit 90a5c9
     * XXX: This is BS - /dev/null is non-portable
Packit 90a5c9
     *      errno-as-apr_status_t is also non-portable
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
#ifdef WIN32
Packit 90a5c9
#define NULL_DEVICE "nul"
Packit 90a5c9
#else
Packit 90a5c9
#define NULL_DEVICE "/dev/null"
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
    if (replace_stderr && freopen(NULL_DEVICE, "w", stderr) == NULL) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_CRIT, errno, s_main, APLOGNO(00093)
Packit 90a5c9
                     "unable to replace stderr with %s", NULL_DEVICE);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    for (virt = s_main->next; virt; virt = virt->next) {
Packit 90a5c9
        if (virt->error_fname) {
Packit 90a5c9
            for (q=s_main; q != virt; q = q->next) {
Packit 90a5c9
                if (q->error_fname != NULL
Packit 90a5c9
                    && strcmp(q->error_fname, virt->error_fname) == 0) {
Packit 90a5c9
                    break;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (q == virt) {
Packit 90a5c9
                if (open_error_log(virt, 0, p) != OK) {
Packit 90a5c9
                    return DONE;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                virt->error_log = q->error_log;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            virt->error_log = s_main->error_log;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
Packit 90a5c9
    apr_file_t *errfile = NULL;
Packit 90a5c9
Packit 90a5c9
    apr_file_open_stderr(&errfile, s->process->pool);
Packit 90a5c9
    if (s->error_log != NULL) {
Packit 90a5c9
        apr_file_dup2(s->error_log, errfile, s->process->pool);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int cpystrn(char *buf, const char *arg, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    char *end;
Packit 90a5c9
    if (!arg)
Packit 90a5c9
        return 0;
Packit 90a5c9
    end = apr_cpystrn(buf, arg, buflen);
Packit 90a5c9
    return end - buf;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static int log_remote_address(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                              char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    if (info->r && !(arg && *arg == 'c'))
Packit 90a5c9
        return apr_snprintf(buf, buflen, "%s:%d", info->r->useragent_ip,
Packit 90a5c9
                            info->r->useragent_addr ? info->r->useragent_addr->port : 0);
Packit 90a5c9
    else if (info->c)
Packit 90a5c9
        return apr_snprintf(buf, buflen, "%s:%d", info->c->client_ip,
Packit 90a5c9
                            info->c->client_addr ? info->c->client_addr->port : 0);
Packit 90a5c9
    else
Packit 90a5c9
        return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_local_address(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                             char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    if (info->c)
Packit 90a5c9
        return apr_snprintf(buf, buflen, "%s:%d", info->c->local_ip,
Packit 90a5c9
                            info->c->local_addr->port);
Packit 90a5c9
    else
Packit 90a5c9
        return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_pid(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                   char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    pid_t pid = getpid();
Packit 90a5c9
    return apr_snprintf(buf, buflen, "%" APR_PID_T_FMT, pid);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_tid(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                   char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
    int result;
Packit 90a5c9
#endif
Packit 90a5c9
#if HAVE_GETTID
Packit 90a5c9
    if (arg && *arg == 'g') {
Packit 90a5c9
        pid_t tid = syscall(SYS_gettid);
Packit 90a5c9
        if (tid == -1)
Packit 90a5c9
            return 0;
Packit 90a5c9
        return apr_snprintf(buf, buflen, "%"APR_PID_T_FMT, tid);
Packit 90a5c9
    }
Packit 90a5c9
#endif
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
    if (ap_mpm_query(AP_MPMQ_IS_THREADED, &result) == APR_SUCCESS
Packit 90a5c9
        && result != AP_MPMQ_NOT_SUPPORTED)
Packit 90a5c9
    {
Packit 90a5c9
        apr_os_thread_t tid = apr_os_thread_current();
Packit 90a5c9
        return apr_snprintf(buf, buflen, "%pT", &tid;;
Packit 90a5c9
    }
Packit 90a5c9
#endif
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_ctime(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                     char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    int time_len = buflen;
Packit 90a5c9
    int option = AP_CTIME_OPTION_NONE;
Packit 90a5c9
Packit 90a5c9
    while (arg && *arg) {
Packit 90a5c9
        switch (*arg) {
Packit 90a5c9
            case 'u':   option |= AP_CTIME_OPTION_USEC;
Packit 90a5c9
                        break;
Packit 90a5c9
            case 'c':   option |= AP_CTIME_OPTION_COMPACT;
Packit 90a5c9
                        break;
Packit 90a5c9
        }
Packit 90a5c9
        arg++;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_recent_ctime_ex(buf, apr_time_now(), option, &time_len);
Packit 90a5c9
Packit 90a5c9
    /* ap_recent_ctime_ex includes the trailing \0 in time_len */
Packit 90a5c9
    return time_len - 1;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_loglevel(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                        char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    if (info->level < 0)
Packit 90a5c9
        return 0;
Packit 90a5c9
    else
Packit 90a5c9
        return cpystrn(buf, priorities[info->level].t_name, buflen);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_log_id(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                      char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    /*
Packit 90a5c9
     * C: log conn log_id if available,
Packit 90a5c9
     * c: log conn log id if available and not a once-per-request log line
Packit 90a5c9
     * else: log request log id if available
Packit 90a5c9
     */
Packit 90a5c9
    if (arg && !strcasecmp(arg, "c")) {
Packit 90a5c9
        if (info->c && (*arg != 'C' || !info->r)) {
Packit 90a5c9
            return cpystrn(buf, info->c->log_id, buflen);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else if (info->rmain) {
Packit 90a5c9
        return cpystrn(buf, info->rmain->log_id, buflen);
Packit 90a5c9
    }
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_keepalives(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                          char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    if (!info->c)
Packit 90a5c9
        return 0;
Packit 90a5c9
Packit 90a5c9
    return apr_snprintf(buf, buflen, "%d", info->c->keepalives);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_module_name(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                           char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    return cpystrn(buf, ap_find_module_short_name(info->module_index), buflen);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_file_line(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                         char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    if (info->file == NULL) {
Packit 90a5c9
        return 0;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        const char *file = info->file;
Packit 90a5c9
#if defined(_OSD_POSIX) || defined(WIN32) || defined(__MVS__)
Packit 90a5c9
        char tmp[256];
Packit 90a5c9
        char *e = strrchr(file, '/');
Packit 90a5c9
#ifdef WIN32
Packit 90a5c9
        if (!e) {
Packit 90a5c9
            e = strrchr(file, '\\');
Packit 90a5c9
        }
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
        /* In OSD/POSIX, the compiler returns for __FILE__
Packit 90a5c9
         * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
Packit 90a5c9
         * (it even returns an absolute path for sources in
Packit 90a5c9
         * the current directory). Here we try to strip this
Packit 90a5c9
         * down to the basename.
Packit 90a5c9
         */
Packit 90a5c9
        if (e != NULL && e[1] != '\0') {
Packit 90a5c9
            apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
Packit 90a5c9
            e = &tmp[strlen(tmp)-1];
Packit 90a5c9
            if (*e == ')') {
Packit 90a5c9
                *e = '\0';
Packit 90a5c9
            }
Packit 90a5c9
            file = tmp;
Packit 90a5c9
        }
Packit 90a5c9
#else /* _OSD_POSIX || WIN32 */
Packit 90a5c9
        const char *p;
Packit 90a5c9
        /* On Unix, __FILE__ may be an absolute path in a
Packit 90a5c9
         * VPATH build. */
Packit 90a5c9
        if (file[0] == '/' && (p = ap_strrchr_c(file, '/')) != NULL) {
Packit 90a5c9
            file = p + 1;
Packit 90a5c9
        }
Packit 90a5c9
#endif /*_OSD_POSIX || WIN32 */
Packit 90a5c9
        return apr_snprintf(buf, buflen, "%s(%d)", file, info->line);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_apr_status(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                          char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t status = info->status;
Packit 90a5c9
    int len;
Packit 90a5c9
    if (!status)
Packit 90a5c9
        return 0;
Packit 90a5c9
Packit 90a5c9
    if (status < APR_OS_START_EAIERR) {
Packit 90a5c9
        len = apr_snprintf(buf, buflen, "(%d)", status);
Packit 90a5c9
    }
Packit 90a5c9
    else if (status < APR_OS_START_SYSERR) {
Packit 90a5c9
        len = apr_snprintf(buf, buflen, "(EAI %d)",
Packit 90a5c9
                           status - APR_OS_START_EAIERR);
Packit 90a5c9
    }
Packit 90a5c9
    else if (status < 100000 + APR_OS_START_SYSERR) {
Packit 90a5c9
        len = apr_snprintf(buf, buflen, "(OS %d)",
Packit 90a5c9
                           status - APR_OS_START_SYSERR);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        len = apr_snprintf(buf, buflen, "(os 0x%08x)",
Packit 90a5c9
                           status - APR_OS_START_SYSERR);
Packit 90a5c9
    }
Packit 90a5c9
    apr_strerror(status, buf + len, buflen - len);
Packit 90a5c9
    len += strlen(buf + len);
Packit 90a5c9
    return len;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_server_name(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                           char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    if (info->r)
Packit 90a5c9
        return cpystrn(buf, ap_get_server_name((request_rec *)info->r), buflen);
Packit 90a5c9
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_virtual_host(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                            char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    if (info->s)
Packit 90a5c9
        return cpystrn(buf, info->s->server_hostname, buflen);
Packit 90a5c9
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static int log_table_entry(const apr_table_t *table, const char *name,
Packit 90a5c9
                           char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
Packit 90a5c9
    const char *value;
Packit 90a5c9
    char scratch[MAX_STRING_LEN];
Packit 90a5c9
Packit 90a5c9
    if ((value = apr_table_get(table, name)) != NULL) {
Packit 90a5c9
        ap_escape_errorlog_item(scratch, value, MAX_STRING_LEN);
Packit 90a5c9
        return cpystrn(buf, scratch, buflen);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return 0;
Packit 90a5c9
#else
Packit 90a5c9
    return cpystrn(buf, apr_table_get(table, name), buflen);
Packit 90a5c9
#endif
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_header(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                      char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    if (info->r)
Packit 90a5c9
        return log_table_entry(info->r->headers_in, arg, buf, buflen);
Packit 90a5c9
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_note(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                      char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    /* XXX: maybe escaping the entry is not necessary for notes? */
Packit 90a5c9
    if (info->r)
Packit 90a5c9
        return log_table_entry(info->r->notes, arg, buf, buflen);
Packit 90a5c9
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int log_env_var(const ap_errorlog_info *info, const char *arg,
Packit 90a5c9
                      char *buf, int buflen)
Packit 90a5c9
{
Packit 90a5c9
    if (info->r)
Packit 90a5c9
        return log_table_entry(info->r->subprocess_env, arg, buf, buflen);
Packit 90a5c9
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int core_generate_log_id(const conn_rec *c, const request_rec *r,
Packit 90a5c9
                                 const char **idstring)
Packit 90a5c9
{
Packit 90a5c9
    apr_uint64_t id, tmp;
Packit 90a5c9
    pid_t pid;
Packit 90a5c9
    int len;
Packit 90a5c9
    char *encoded;
Packit 90a5c9
Packit 90a5c9
    if (r && r->request_time) {
Packit 90a5c9
        id = (apr_uint64_t)r->request_time;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        id = (apr_uint64_t)apr_time_now();
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    pid = getpid();
Packit 90a5c9
    if (sizeof(pid_t) > 2) {
Packit 90a5c9
        tmp = pid;
Packit 90a5c9
        tmp = tmp << 40;
Packit 90a5c9
        id ^= tmp;
Packit 90a5c9
        pid = pid >> 24;
Packit 90a5c9
        tmp = pid;
Packit 90a5c9
        tmp = tmp << 56;
Packit 90a5c9
        id ^= tmp;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        tmp = pid;
Packit 90a5c9
        tmp = tmp << 40;
Packit 90a5c9
        id ^= tmp;
Packit 90a5c9
    }
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
    {
Packit 90a5c9
        apr_uintptr_t tmp2 = (apr_uintptr_t)c->current_thread;
Packit 90a5c9
        tmp = tmp2;
Packit 90a5c9
        tmp = tmp << 32;
Packit 90a5c9
        id ^= tmp;
Packit 90a5c9
    }
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
    len = apr_base64_encode_len(sizeof(id)); /* includes trailing \0 */
Packit 90a5c9
    encoded = apr_palloc(r ? r->pool : c->pool, len);
Packit 90a5c9
    apr_base64_encode(encoded, (char *)&id, sizeof(id));
Packit 90a5c9
Packit 90a5c9
    /* Skip the last char, it is always '=' */
Packit 90a5c9
    encoded[len - 2] = '\0';
Packit 90a5c9
Packit 90a5c9
    *idstring = encoded;
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void add_log_id(const conn_rec *c, const request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    const char **id;
Packit 90a5c9
    /* need to cast const away */
Packit 90a5c9
    if (r) {
Packit 90a5c9
        id = &((request_rec *)r)->log_id;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        id = &((conn_rec *)c)->log_id;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_run_generate_log_id(c, r, id);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_register_log_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    ap_hook_generate_log_id(core_generate_log_id, NULL, NULL,
Packit 90a5c9
                            APR_HOOK_REALLY_LAST);
Packit 90a5c9
Packit 90a5c9
    ap_register_errorlog_handler(p, "a", log_remote_address, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "A", log_local_address, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "e", log_env_var, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "E", log_apr_status, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "F", log_file_line, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "i", log_header, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "k", log_keepalives, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "l", log_loglevel, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "L", log_log_id, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "m", log_module_name, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "n", log_note, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "P", log_pid, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "t", log_ctime, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "T", log_tid, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "v", log_virtual_host, 0);
Packit 90a5c9
    ap_register_errorlog_handler(p, "V", log_server_name, 0);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * This is used if no error log format is defined and during startup.
Packit 90a5c9
 * It automatically omits the timestamp if logging to syslog.
Packit 90a5c9
 */
Packit 90a5c9
static int do_errorlog_default(const ap_errorlog_info *info, char *buf,
Packit 90a5c9
                               int buflen, int *errstr_start, int *errstr_end,
Packit 90a5c9
                               const char *errstr_fmt, va_list args)
Packit 90a5c9
{
Packit 90a5c9
    int len = 0;
Packit 90a5c9
    int field_start = 0;
Packit 90a5c9
    int item_len;
Packit 90a5c9
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
Packit 90a5c9
    char scratch[MAX_STRING_LEN];
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
    if (!info->using_syslog && !info->startup) {
Packit 90a5c9
        buf[len++] = '[';
Packit 90a5c9
        len += log_ctime(info, "u", buf + len, buflen - len);
Packit 90a5c9
        buf[len++] = ']';
Packit 90a5c9
        buf[len++] = ' ';
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!info->startup) {
Packit 90a5c9
        buf[len++] = '[';
Packit 90a5c9
        len += log_module_name(info, NULL, buf + len, buflen - len);
Packit 90a5c9
        buf[len++] = ':';
Packit 90a5c9
        len += log_loglevel(info, NULL, buf + len, buflen - len);
Packit 90a5c9
        len += cpystrn(buf + len, "] [pid ", buflen - len);
Packit 90a5c9
Packit 90a5c9
        len += log_pid(info, NULL, buf + len, buflen - len);
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
        field_start = len;
Packit 90a5c9
        len += cpystrn(buf + len, ":tid ", buflen - len);
Packit 90a5c9
        item_len = log_tid(info, NULL, buf + len, buflen - len);
Packit 90a5c9
        if (!item_len)
Packit 90a5c9
            len = field_start;
Packit 90a5c9
        else
Packit 90a5c9
            len += item_len;
Packit 90a5c9
#endif
Packit 90a5c9
        buf[len++] = ']';
Packit 90a5c9
        buf[len++] = ' ';
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (info->level >= APLOG_DEBUG) {
Packit 90a5c9
        item_len = log_file_line(info, NULL, buf + len, buflen - len);
Packit 90a5c9
        if (item_len) {
Packit 90a5c9
            len += item_len;
Packit 90a5c9
            len += cpystrn(buf + len, ": ", buflen - len);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (info->status) {
Packit 90a5c9
        item_len = log_apr_status(info, NULL, buf + len, buflen - len);
Packit 90a5c9
        if (item_len) {
Packit 90a5c9
            len += item_len;
Packit 90a5c9
            len += cpystrn(buf + len, ": ", buflen - len);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * useragent_ip/client_ip can be client or backend server. If we have
Packit 90a5c9
     * a scoreboard handle, it is likely a client.
Packit 90a5c9
     */
Packit 90a5c9
    if (info->r) {
Packit 90a5c9
        len += apr_snprintf(buf + len, buflen - len,
Packit 90a5c9
                            info->r->connection->sbh ? "[client %s:%d] " : "[remote %s:%d] ",
Packit 90a5c9
                            info->r->useragent_ip,
Packit 90a5c9
                            info->r->useragent_addr ? info->r->useragent_addr->port : 0);
Packit 90a5c9
    }
Packit 90a5c9
    else if (info->c) {
Packit 90a5c9
        len += apr_snprintf(buf + len, buflen - len,
Packit 90a5c9
                            info->c->sbh ? "[client %s:%d] " : "[remote %s:%d] ",
Packit 90a5c9
                            info->c->client_ip,
Packit 90a5c9
                            info->c->client_addr ? info->c->client_addr->port : 0);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* the actual error message */
Packit 90a5c9
    *errstr_start = len;
Packit 90a5c9
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
Packit 90a5c9
    if (apr_vsnprintf(scratch, MAX_STRING_LEN, errstr_fmt, args)) {
Packit 90a5c9
        len += ap_escape_errorlog_item(buf + len, scratch,
Packit 90a5c9
                                       buflen - len);
Packit 90a5c9
Packit 90a5c9
    }
Packit 90a5c9
#else
Packit 90a5c9
    len += apr_vsnprintf(buf + len, buflen - len, errstr_fmt, args);
Packit 90a5c9
#endif
Packit 90a5c9
    *errstr_end = len;
Packit 90a5c9
Packit 90a5c9
    field_start = len;
Packit 90a5c9
    len += cpystrn(buf + len, ", referer: ", buflen - len);
Packit 90a5c9
    item_len = log_header(info, "Referer", buf + len, buflen - len);
Packit 90a5c9
    if (item_len)
Packit 90a5c9
        len += item_len;
Packit 90a5c9
    else
Packit 90a5c9
        len = field_start;
Packit 90a5c9
Packit 90a5c9
    return len;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int do_errorlog_format(apr_array_header_t *fmt, ap_errorlog_info *info,
Packit 90a5c9
                              char *buf, int buflen, int *errstr_start,
Packit 90a5c9
                              int *errstr_end, const char *err_fmt, va_list args)
Packit 90a5c9
{
Packit 90a5c9
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
Packit 90a5c9
    char scratch[MAX_STRING_LEN];
Packit 90a5c9
#endif
Packit 90a5c9
    int i;
Packit 90a5c9
    int len = 0;
Packit 90a5c9
    int field_start = 0;
Packit 90a5c9
    int skipping = 0;
Packit 90a5c9
    ap_errorlog_format_item *items = (ap_errorlog_format_item *)fmt->elts;
Packit 90a5c9
Packit 90a5c9
    AP_DEBUG_ASSERT(fmt->nelts > 0);
Packit 90a5c9
    for (i = 0; i < fmt->nelts; ++i) {
Packit 90a5c9
        ap_errorlog_format_item *item = &items[i];
Packit 90a5c9
        if (item->flags & AP_ERRORLOG_FLAG_FIELD_SEP) {
Packit 90a5c9
            if (skipping) {
Packit 90a5c9
                skipping = 0;
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                field_start = len;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (item->flags & AP_ERRORLOG_FLAG_MESSAGE) {
Packit 90a5c9
            /* the actual error message */
Packit 90a5c9
            *errstr_start = len;
Packit 90a5c9
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
Packit 90a5c9
            if (apr_vsnprintf(scratch, MAX_STRING_LEN, err_fmt, args)) {
Packit 90a5c9
                len += ap_escape_errorlog_item(buf + len, scratch,
Packit 90a5c9
                                               buflen - len);
Packit 90a5c9
Packit 90a5c9
            }
Packit 90a5c9
#else
Packit 90a5c9
            len += apr_vsnprintf(buf + len, buflen - len, err_fmt, args);
Packit 90a5c9
#endif
Packit 90a5c9
            *errstr_end = len;
Packit 90a5c9
        }
Packit 90a5c9
        else if (skipping) {
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
        else if (info->level != -1 && (int)item->min_loglevel > info->level) {
Packit 90a5c9
            len = field_start;
Packit 90a5c9
            skipping = 1;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            int item_len = (*item->func)(info, item->arg, buf + len,
Packit 90a5c9
                                         buflen - len);
Packit 90a5c9
            if (!item_len) {
Packit 90a5c9
                if (item->flags & AP_ERRORLOG_FLAG_REQUIRED) {
Packit 90a5c9
                    /* required item is empty. skip whole line */
Packit 90a5c9
                    buf[0] = '\0';
Packit 90a5c9
                    return 0;
Packit 90a5c9
                }
Packit 90a5c9
                else if (item->flags & AP_ERRORLOG_FLAG_NULL_AS_HYPHEN) {
Packit 90a5c9
                    buf[len++] = '-';
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    len = field_start;
Packit 90a5c9
                    skipping = 1;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                len += item_len;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return len;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void write_logline(char *errstr, apr_size_t len, apr_file_t *logf,
Packit 90a5c9
                          int level)
Packit 90a5c9
{
Packit 90a5c9
    /* NULL if we are logging to syslog */
Packit 90a5c9
    if (logf) {
Packit 90a5c9
        /* Truncate for the terminator (as apr_snprintf does) */
Packit 90a5c9
        if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR)) {
Packit 90a5c9
            len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
Packit 90a5c9
        }
Packit 90a5c9
        strcpy(errstr + len, APR_EOL_STR);
Packit 90a5c9
        apr_file_puts(errstr, logf);
Packit 90a5c9
        apr_file_flush(logf);
Packit 90a5c9
    }
Packit 90a5c9
#ifdef HAVE_SYSLOG
Packit 90a5c9
    else {
Packit 90a5c9
        syslog(level < LOG_PRIMASK ? level : APLOG_DEBUG, "%.*s",
Packit 90a5c9
               (int)len, errstr);
Packit 90a5c9
    }
Packit 90a5c9
#endif
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void log_error_core(const char *file, int line, int module_index,
Packit 90a5c9
                           int level,
Packit 90a5c9
                           apr_status_t status, const server_rec *s,
Packit 90a5c9
                           const conn_rec *c,
Packit 90a5c9
                           const request_rec *r, apr_pool_t *pool,
Packit 90a5c9
                           const char *fmt, va_list args)
Packit 90a5c9
{
Packit 90a5c9
    char errstr[MAX_STRING_LEN];
Packit 90a5c9
    apr_file_t *logf = NULL;
Packit 90a5c9
    int level_and_mask = level & APLOG_LEVELMASK;
Packit 90a5c9
    const request_rec *rmain = NULL;
Packit 90a5c9
    core_server_config *sconf = NULL;
Packit 90a5c9
    ap_errorlog_info info;
Packit 90a5c9
Packit 90a5c9
    /* do we need to log once-per-req or once-per-conn info? */
Packit 90a5c9
    int log_conn_info = 0, log_req_info = 0;
Packit 90a5c9
    apr_array_header_t **lines = NULL;
Packit 90a5c9
    int done = 0;
Packit 90a5c9
    int line_number = 0;
Packit 90a5c9
Packit 90a5c9
    if (r) {
Packit 90a5c9
        AP_DEBUG_ASSERT(r->connection != NULL);
Packit 90a5c9
        c = r->connection;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (s == NULL) {
Packit 90a5c9
        /*
Packit 90a5c9
         * If we are doing stderr logging (startup), don't log messages that are
Packit 90a5c9
         * above the default server log level unless it is a startup/shutdown
Packit 90a5c9
         * notice
Packit 90a5c9
         */
Packit 90a5c9
#ifndef DEBUG
Packit 90a5c9
        if ((level_and_mask != APLOG_NOTICE)
Packit 90a5c9
            && (level_and_mask > ap_default_loglevel)) {
Packit 90a5c9
            return;
Packit 90a5c9
        }
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
        logf = stderr_log;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        int configured_level = r ? ap_get_request_module_loglevel(r, module_index)        :
Packit 90a5c9
                               c ? ap_get_conn_server_module_loglevel(c, s, module_index) :
Packit 90a5c9
                                   ap_get_server_module_loglevel(s, module_index);
Packit 90a5c9
        if (s->error_log) {
Packit 90a5c9
            /*
Packit 90a5c9
             * If we are doing normal logging, don't log messages that are
Packit 90a5c9
             * above the module's log level unless it is a startup/shutdown notice
Packit 90a5c9
             */
Packit 90a5c9
            if ((level_and_mask != APLOG_NOTICE)
Packit 90a5c9
                && (level_and_mask > configured_level)) {
Packit 90a5c9
                return;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            logf = s->error_log;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            /*
Packit 90a5c9
             * If we are doing syslog logging, don't log messages that are
Packit 90a5c9
             * above the module's log level (including a startup/shutdown notice)
Packit 90a5c9
             */
Packit 90a5c9
            if (level_and_mask > configured_level) {
Packit 90a5c9
                return;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* the faked server_rec from mod_cgid does not have s->module_config */
Packit 90a5c9
        if (s->module_config) {
Packit 90a5c9
            sconf = ap_get_core_module_config(s->module_config);
Packit 90a5c9
            if (c && !c->log_id) {
Packit 90a5c9
                add_log_id(c, NULL);
Packit 90a5c9
                if (sconf->error_log_conn && sconf->error_log_conn->nelts > 0)
Packit 90a5c9
                    log_conn_info = 1;
Packit 90a5c9
            }
Packit 90a5c9
            if (r) {
Packit 90a5c9
                if (r->main)
Packit 90a5c9
                    rmain = r->main;
Packit 90a5c9
                else
Packit 90a5c9
                    rmain = r;
Packit 90a5c9
Packit 90a5c9
                if (!rmain->log_id) {
Packit 90a5c9
                    /* XXX: do we need separate log ids for subrequests? */
Packit 90a5c9
                    if (sconf->error_log_req && sconf->error_log_req->nelts > 0)
Packit 90a5c9
                        log_req_info = 1;
Packit 90a5c9
                    /*
Packit 90a5c9
                     * XXX: potential optimization: only create log id if %L is
Packit 90a5c9
                     * XXX: actually used
Packit 90a5c9
                     */
Packit 90a5c9
                    add_log_id(c, rmain);
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    info.s             = s;
Packit 90a5c9
    info.c             = c;
Packit 90a5c9
    info.pool          = pool;
Packit 90a5c9
    info.file          = NULL;
Packit 90a5c9
    info.line          = 0;
Packit 90a5c9
    info.status        = 0;
Packit 90a5c9
    info.using_syslog  = (logf == NULL);
Packit 90a5c9
    info.startup       = ((level & APLOG_STARTUP) == APLOG_STARTUP);
Packit 90a5c9
    info.format        = fmt;
Packit 90a5c9
Packit 90a5c9
    while (!done) {
Packit 90a5c9
        apr_array_header_t *log_format;
Packit 90a5c9
        int len = 0, errstr_start = 0, errstr_end = 0;
Packit 90a5c9
        /* XXX: potential optimization: format common prefixes only once */
Packit 90a5c9
        if (log_conn_info) {
Packit 90a5c9
            /* once-per-connection info */
Packit 90a5c9
            if (line_number == 0) {
Packit 90a5c9
                lines = (apr_array_header_t **)sconf->error_log_conn->elts;
Packit 90a5c9
                info.r = NULL;
Packit 90a5c9
                info.rmain = NULL;
Packit 90a5c9
                info.level = -1;
Packit 90a5c9
                info.module_index = APLOG_NO_MODULE;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            log_format = lines[line_number++];
Packit 90a5c9
Packit 90a5c9
            if (line_number == sconf->error_log_conn->nelts) {
Packit 90a5c9
                /* this is the last line of once-per-connection info */
Packit 90a5c9
                line_number = 0;
Packit 90a5c9
                log_conn_info = 0;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else if (log_req_info) {
Packit 90a5c9
            /* once-per-request info */
Packit 90a5c9
            if (line_number == 0) {
Packit 90a5c9
                lines = (apr_array_header_t **)sconf->error_log_req->elts;
Packit 90a5c9
                info.r = rmain;
Packit 90a5c9
                info.rmain = rmain;
Packit 90a5c9
                info.level = -1;
Packit 90a5c9
                info.module_index = APLOG_NO_MODULE;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            log_format = lines[line_number++];
Packit 90a5c9
Packit 90a5c9
            if (line_number == sconf->error_log_req->nelts) {
Packit 90a5c9
                /* this is the last line of once-per-request info */
Packit 90a5c9
                line_number = 0;
Packit 90a5c9
                log_req_info = 0;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            /* the actual error message */
Packit 90a5c9
            info.r            = r;
Packit 90a5c9
            info.rmain        = rmain;
Packit 90a5c9
            info.level        = level_and_mask;
Packit 90a5c9
            info.module_index = module_index;
Packit 90a5c9
            info.file         = file;
Packit 90a5c9
            info.line         = line;
Packit 90a5c9
            info.status       = status;
Packit 90a5c9
            log_format = sconf ? sconf->error_log_format : NULL;
Packit 90a5c9
            done = 1;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * prepare and log one line
Packit 90a5c9
         */
Packit 90a5c9
Packit 90a5c9
        if (log_format && !info.startup) {
Packit 90a5c9
            len += do_errorlog_format(log_format, &info, errstr + len,
Packit 90a5c9
                                      MAX_STRING_LEN - len,
Packit 90a5c9
                                      &errstr_start, &errstr_end, fmt, args);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            len += do_errorlog_default(&info, errstr + len, MAX_STRING_LEN - len,
Packit 90a5c9
                                       &errstr_start, &errstr_end, fmt, args);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (!*errstr) {
Packit 90a5c9
            /*
Packit 90a5c9
             * Don't log empty lines. This can happen with once-per-conn/req
Packit 90a5c9
             * info if an item with AP_ERRORLOG_FLAG_REQUIRED is NULL.
Packit 90a5c9
             */
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
        write_logline(errstr, len, logf, level_and_mask);
Packit 90a5c9
Packit 90a5c9
        if (done) {
Packit 90a5c9
            /*
Packit 90a5c9
             * We don't call the error_log hook for per-request/per-conn
Packit 90a5c9
             * lines, and we only pass the actual log message, not the
Packit 90a5c9
             * prefix and suffix.
Packit 90a5c9
             */
Packit 90a5c9
            errstr[errstr_end] = '\0';
Packit 90a5c9
            ap_run_error_log(&info, errstr + errstr_start);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        *errstr = '\0';
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* For internal calls to log_error_core with self-composed arg lists */
Packit 90a5c9
static void log_error_va_glue(const char *file, int line, int module_index,
Packit 90a5c9
                              int level, apr_status_t status,
Packit 90a5c9
                              const server_rec *s, const conn_rec *c,
Packit 90a5c9
                              const request_rec *r, apr_pool_t *pool,
Packit 90a5c9
                              const char *fmt, ...)
Packit 90a5c9
{
Packit 90a5c9
    va_list args;
Packit 90a5c9
Packit 90a5c9
    va_start(args, fmt);
Packit 90a5c9
    log_error_core(file, line, module_index, level, status, s, c, r, pool,
Packit 90a5c9
                   fmt, args);
Packit 90a5c9
    va_end(args);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_error_(const char *file, int line, int module_index,
Packit 90a5c9
                               int level, apr_status_t status,
Packit 90a5c9
                               const server_rec *s, const char *fmt, ...)
Packit 90a5c9
{
Packit 90a5c9
    va_list args;
Packit 90a5c9
Packit 90a5c9
    va_start(args, fmt);
Packit 90a5c9
    log_error_core(file, line, module_index, level, status, s, NULL, NULL,
Packit 90a5c9
                   NULL, fmt, args);
Packit 90a5c9
    va_end(args);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_perror_(const char *file, int line, int module_index,
Packit 90a5c9
                                int level, apr_status_t status, apr_pool_t *p,
Packit 90a5c9
                                const char *fmt, ...)
Packit 90a5c9
{
Packit 90a5c9
    va_list args;
Packit 90a5c9
Packit 90a5c9
    va_start(args, fmt);
Packit 90a5c9
    log_error_core(file, line, module_index, level, status, NULL, NULL, NULL,
Packit 90a5c9
                   p, fmt, args);
Packit 90a5c9
    va_end(args);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_rerror_(const char *file, int line, int module_index,
Packit 90a5c9
                                int level, apr_status_t status,
Packit 90a5c9
                                const request_rec *r, const char *fmt, ...)
Packit 90a5c9
{
Packit 90a5c9
    va_list args;
Packit 90a5c9
Packit 90a5c9
    va_start(args, fmt);
Packit 90a5c9
    log_error_core(file, line, module_index, level, status, r->server, NULL, r,
Packit 90a5c9
                   NULL, fmt, args);
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * IF APLOG_TOCLIENT is set,
Packit 90a5c9
     * AND the error level is 'warning' or more severe,
Packit 90a5c9
     * AND there isn't already error text associated with this request,
Packit 90a5c9
     * THEN make the message text available to ErrorDocument and
Packit 90a5c9
     * other error processors.
Packit 90a5c9
     */
Packit 90a5c9
    va_end(args);
Packit 90a5c9
    va_start(args,fmt);
Packit 90a5c9
    if ((level & APLOG_TOCLIENT)
Packit 90a5c9
        && ((level & APLOG_LEVELMASK) <= APLOG_WARNING)
Packit 90a5c9
        && (apr_table_get(r->notes, "error-notes") == NULL)) {
Packit 90a5c9
        apr_table_setn(r->notes, "error-notes",
Packit 90a5c9
                       ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
Packit 90a5c9
                                                             args)));
Packit 90a5c9
    }
Packit 90a5c9
    va_end(args);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_cserror_(const char *file, int line, int module_index,
Packit 90a5c9
                                 int level, apr_status_t status,
Packit 90a5c9
                                 const conn_rec *c, const server_rec *s,
Packit 90a5c9
                                 const char *fmt, ...)
Packit 90a5c9
{
Packit 90a5c9
    va_list args;
Packit 90a5c9
Packit 90a5c9
    va_start(args, fmt);
Packit 90a5c9
    log_error_core(file, line, module_index, level, status, s, c,
Packit 90a5c9
                   NULL, NULL, fmt, args);
Packit 90a5c9
    va_end(args);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_cerror_(const char *file, int line, int module_index,
Packit 90a5c9
                                int level, apr_status_t status,
Packit 90a5c9
                                const conn_rec *c, const char *fmt, ...)
Packit 90a5c9
{
Packit 90a5c9
    va_list args;
Packit 90a5c9
Packit 90a5c9
    va_start(args, fmt);
Packit 90a5c9
    log_error_core(file, line, module_index, level, status, c->base_server, c,
Packit 90a5c9
                   NULL, NULL, fmt, args);
Packit 90a5c9
    va_end(args);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
#define BYTES_LOGGED_PER_LINE 16
Packit 90a5c9
#define LOG_BYTES_BUFFER_SIZE (BYTES_LOGGED_PER_LINE * 3 + 2)
Packit 90a5c9
Packit 90a5c9
static void fmt_data(unsigned char *buf, const void *vdata, apr_size_t len, apr_size_t *off)
Packit 90a5c9
{
Packit 90a5c9
    const unsigned char *data = (const unsigned char *)vdata;
Packit 90a5c9
    unsigned char *chars;
Packit 90a5c9
    unsigned char *hex;
Packit 90a5c9
    apr_size_t this_time = 0;
Packit 90a5c9
Packit 90a5c9
    memset(buf, ' ', LOG_BYTES_BUFFER_SIZE - 1);
Packit 90a5c9
    buf[LOG_BYTES_BUFFER_SIZE - 1] = '\0';
Packit 90a5c9
    
Packit 90a5c9
    chars = buf; /* start character dump here */
Packit 90a5c9
    hex   = buf + BYTES_LOGGED_PER_LINE + 1; /* start hex dump here */
Packit 90a5c9
    while (*off < len && this_time < BYTES_LOGGED_PER_LINE) {
Packit 90a5c9
        unsigned char c = data[*off];
Packit 90a5c9
Packit 90a5c9
        if (apr_isprint(c)
Packit 90a5c9
            && c != '\\') {  /* backslash will be escaped later, which throws
Packit 90a5c9
                              * off the formatting
Packit 90a5c9
                              */
Packit 90a5c9
            *chars = c;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            *chars = '.';
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if ((c >> 4) >= 10) {
Packit 90a5c9
            *hex = 'a' + ((c >> 4) - 10);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            *hex = '0' + (c >> 4);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if ((c & 0x0F) >= 10) {
Packit 90a5c9
            *(hex + 1) = 'a' + ((c & 0x0F) - 10);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            *(hex + 1) = '0' + (c & 0x0F);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        chars += 1;
Packit 90a5c9
        hex += 2;
Packit 90a5c9
        *off += 1;
Packit 90a5c9
        ++this_time;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void log_data_core(const char *file, int line, int module_index,
Packit 90a5c9
                          int level, const server_rec *s,
Packit 90a5c9
                          const conn_rec *c, const request_rec *r,
Packit 90a5c9
                          const char *label, const void *data, apr_size_t len,
Packit 90a5c9
                          unsigned int flags)
Packit 90a5c9
{
Packit 90a5c9
    unsigned char buf[LOG_BYTES_BUFFER_SIZE];
Packit 90a5c9
    apr_size_t off;
Packit 90a5c9
    char prefix[20];
Packit 90a5c9
Packit 90a5c9
    if (!(flags & AP_LOG_DATA_SHOW_OFFSET)) {
Packit 90a5c9
        prefix[0] = '\0';
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (len > 0xffff) { /* bug in caller? */
Packit 90a5c9
        len = 0xffff;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (label) {
Packit 90a5c9
        log_error_va_glue(file, line, module_index, level, APR_SUCCESS, s,
Packit 90a5c9
                          c, r, NULL, "%s (%" APR_SIZE_T_FMT " bytes)",
Packit 90a5c9
                          label, len);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    off = 0;
Packit 90a5c9
    while (off < len) {
Packit 90a5c9
        if (flags & AP_LOG_DATA_SHOW_OFFSET) {
Packit 90a5c9
            apr_snprintf(prefix, sizeof prefix, "%04x: ", (unsigned int)off);
Packit 90a5c9
        }
Packit 90a5c9
        fmt_data(buf, data, len, &off;;
Packit 90a5c9
        log_error_va_glue(file, line, module_index, level, APR_SUCCESS, s,
Packit 90a5c9
                          c, r, NULL, "%s%s", prefix, buf);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_data_(const char *file, int line, 
Packit 90a5c9
                              int module_index, int level,
Packit 90a5c9
                              const server_rec *s, const char *label,
Packit 90a5c9
                              const void *data, apr_size_t len,
Packit 90a5c9
                              unsigned int flags)
Packit 90a5c9
{
Packit 90a5c9
    log_data_core(file, line, module_index, level, s, NULL, NULL, label,
Packit 90a5c9
                  data, len, flags);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_rdata_(const char *file, int line,
Packit 90a5c9
                               int module_index, int level,
Packit 90a5c9
                               const request_rec *r, const char *label,
Packit 90a5c9
                               const void *data, apr_size_t len,
Packit 90a5c9
                               unsigned int flags)
Packit 90a5c9
{
Packit 90a5c9
    log_data_core(file, line, module_index, level, r->server, NULL, r, label,
Packit 90a5c9
                  data, len, flags);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_cdata_(const char *file, int line,
Packit 90a5c9
                               int module_index, int level,
Packit 90a5c9
                               const conn_rec *c, const char *label,
Packit 90a5c9
                               const void *data, apr_size_t len,
Packit 90a5c9
                               unsigned int flags)
Packit 90a5c9
{
Packit 90a5c9
    log_data_core(file, line, module_index, level, c->base_server, c, NULL,
Packit 90a5c9
                  label, data, len, flags);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_csdata_(const char *file, int line, int module_index,
Packit 90a5c9
                                int level, const conn_rec *c, const server_rec *s,
Packit 90a5c9
                                const char *label, const void *data,
Packit 90a5c9
                                apr_size_t len, unsigned int flags)
Packit 90a5c9
{
Packit 90a5c9
    log_data_core(file, line, module_index, level, s, c, NULL, label, data,
Packit 90a5c9
                  len, flags);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_command_line(apr_pool_t *plog, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    int i;
Packit 90a5c9
    process_rec *process = s->process;
Packit 90a5c9
    char *result;
Packit 90a5c9
    int len_needed = 0;
Packit 90a5c9
Packit 90a5c9
    /* Piece together the command line from the pieces
Packit 90a5c9
     * in process->argv, with spaces in between.
Packit 90a5c9
     */
Packit 90a5c9
    for (i = 0; i < process->argc; i++) {
Packit 90a5c9
        len_needed += strlen(process->argv[i]) + 1;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    result = (char *) apr_palloc(plog, len_needed);
Packit 90a5c9
    *result = '\0';
Packit 90a5c9
Packit 90a5c9
    for (i = 0; i < process->argc; i++) {
Packit 90a5c9
        strcat(result, process->argv[i]);
Packit 90a5c9
        if ((i+1)< process->argc) {
Packit 90a5c9
            strcat(result, " ");
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, APLOGNO(00094)
Packit 90a5c9
                 "Command line: '%s'", result);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* grab bag function to log commonly logged and shared info */
Packit 90a5c9
AP_DECLARE(void) ap_log_mpm_common(server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    ap_log_error(APLOG_MARK, APLOG_DEBUG , 0, s, APLOGNO(02639)
Packit 90a5c9
                 "Using SO_REUSEPORT: %s (%d)",
Packit 90a5c9
                 ap_have_so_reuseport ? "yes" : "no",
Packit 90a5c9
                 ap_num_listen_buckets);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_remove_pid(apr_pool_t *p, const char *rel_fname)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    const char *fname = ap_server_root_relative(p, rel_fname);
Packit 90a5c9
Packit 90a5c9
    if (fname != NULL) {
Packit 90a5c9
        rv = apr_file_remove(fname, p);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf, APLOGNO(00095)
Packit 90a5c9
                         "failed to remove PID file %s", fname);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf, APLOGNO(00096)
Packit 90a5c9
                         "removed PID file %s (pid=%" APR_PID_T_FMT ")",
Packit 90a5c9
                         fname, getpid());
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
Packit 90a5c9
{
Packit 90a5c9
    apr_file_t *pid_file = NULL;
Packit 90a5c9
    apr_finfo_t finfo;
Packit 90a5c9
    static pid_t saved_pid = -1;
Packit 90a5c9
    pid_t mypid;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    const char *fname;
Packit 90a5c9
Packit 90a5c9
    if (!filename) {
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    fname = ap_server_root_relative(p, filename);
Packit 90a5c9
    if (!fname) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
Packit 90a5c9
                     ap_server_conf, APLOGNO(00097) "Invalid PID file path %s, ignoring.", filename);
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    mypid = getpid();
Packit 90a5c9
    if (mypid != saved_pid
Packit 90a5c9
        && apr_stat(&finfo, fname, APR_FINFO_MTIME, p) == APR_SUCCESS) {
Packit 90a5c9
        /* AP_SIG_GRACEFUL and HUP call this on each restart.
Packit 90a5c9
         * Only warn on first time through for this pid.
Packit 90a5c9
         *
Packit 90a5c9
         * XXX: Could just write first time through too, although
Packit 90a5c9
         *      that may screw up scripts written to do something
Packit 90a5c9
         *      based on the last modification time of the pid file.
Packit 90a5c9
         */
Packit 90a5c9
        ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, APLOGNO(00098)
Packit 90a5c9
                      "pid file %s overwritten -- Unclean "
Packit 90a5c9
                      "shutdown of previous Apache run?",
Packit 90a5c9
                      fname);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((rv = apr_file_open(&pid_file, fname,
Packit 90a5c9
                            APR_WRITE | APR_CREATE | APR_TRUNCATE,
Packit 90a5c9
                            APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p))
Packit 90a5c9
        != APR_SUCCESS) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, APLOGNO(00099)
Packit 90a5c9
                     "could not create %s", fname);
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, APLOGNO(00100)
Packit 90a5c9
                     "%s: could not log pid to file %s",
Packit 90a5c9
                     ap_server_argv0, fname);
Packit 90a5c9
        exit(1);
Packit 90a5c9
    }
Packit 90a5c9
    apr_file_printf(pid_file, "%" APR_PID_T_FMT APR_EOL_STR, mypid);
Packit 90a5c9
    apr_file_close(pid_file);
Packit 90a5c9
    saved_pid = mypid;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
Packit 90a5c9
                                     pid_t *mypid)
Packit 90a5c9
{
Packit 90a5c9
    const apr_size_t BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */
Packit 90a5c9
    apr_file_t *pid_file = NULL;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    const char *fname;
Packit 90a5c9
    char *buf, *endptr;
Packit 90a5c9
    apr_size_t bytes_read;
Packit 90a5c9
Packit 90a5c9
    if (!filename) {
Packit 90a5c9
        return APR_EGENERAL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    fname = ap_server_root_relative(p, filename);
Packit 90a5c9
    if (!fname) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
Packit 90a5c9
                     ap_server_conf, APLOGNO(00101) "Invalid PID file path %s, ignoring.", filename);
Packit 90a5c9
        return APR_EGENERAL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p);
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    buf = apr_palloc(p, BUFFER_SIZE);
Packit 90a5c9
Packit 90a5c9
    rv = apr_file_read_full(pid_file, buf, BUFFER_SIZE - 1, &bytes_read);
Packit 90a5c9
    if (rv != APR_SUCCESS && rv != APR_EOF) {
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* If we fill the buffer, we're probably reading a corrupt pid file.
Packit 90a5c9
     * To be nice, let's also ensure the first char is a digit. */
Packit 90a5c9
    if (bytes_read == 0 || bytes_read == BUFFER_SIZE - 1 || !apr_isdigit(*buf)) {
Packit 90a5c9
        return APR_EGENERAL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    buf[bytes_read] = '\0';
Packit 90a5c9
    *mypid = strtol(buf, &endptr, 10);
Packit 90a5c9
Packit 90a5c9
    apr_file_close(pid_file);
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile,
Packit 90a5c9
                               int nLine)
Packit 90a5c9
{
Packit 90a5c9
    char time_str[APR_CTIME_LEN];
Packit 90a5c9
Packit 90a5c9
    apr_ctime(time_str, apr_time_now());
Packit 90a5c9
    ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL, APLOGNO(00102)
Packit 90a5c9
                 "[%s] file %s, line %d, assertion \"%s\" failed",
Packit 90a5c9
                 time_str, szFile, nLine, szExp);
Packit 90a5c9
#if defined(WIN32)
Packit 90a5c9
    DebugBreak();
Packit 90a5c9
#else
Packit 90a5c9
    /* unix assert does an abort leading to a core dump */
Packit 90a5c9
    abort();
Packit 90a5c9
#endif
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* piped log support */
Packit 90a5c9
Packit 90a5c9
#ifdef AP_HAVE_RELIABLE_PIPED_LOGS
Packit 90a5c9
/* forward declaration */
Packit 90a5c9
static void piped_log_maintenance(int reason, void *data, apr_wait_t status);
Packit 90a5c9
Packit 90a5c9
/* Spawn the piped logger process pl->program. */
Packit 90a5c9
static apr_status_t piped_log_spawn(piped_log *pl)
Packit 90a5c9
{
Packit 90a5c9
    apr_procattr_t *procattr;
Packit 90a5c9
    apr_proc_t *procnew = NULL;
Packit 90a5c9
    apr_status_t status;
Packit 90a5c9
Packit 90a5c9
    if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
Packit 90a5c9
        ((status = apr_procattr_dir_set(procattr, ap_server_root))
Packit 90a5c9
         != APR_SUCCESS) ||
Packit 90a5c9
        ((status = apr_procattr_cmdtype_set(procattr, pl->cmdtype))
Packit 90a5c9
         != APR_SUCCESS) ||
Packit 90a5c9
        ((status = apr_procattr_child_in_set(procattr,
Packit 90a5c9
                                             pl->read_fd,
Packit 90a5c9
                                             pl->write_fd))
Packit 90a5c9
         != APR_SUCCESS) ||
Packit 90a5c9
        ((status = apr_procattr_child_errfn_set(procattr, log_child_errfn))
Packit 90a5c9
         != APR_SUCCESS) ||
Packit 90a5c9
        ((status = apr_procattr_error_check_set(procattr, 1)) != APR_SUCCESS)) {
Packit 90a5c9
        /* Something bad happened, give up and go away. */
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_STARTUP, status, ap_server_conf, APLOGNO(00103)
Packit 90a5c9
                     "piped_log_spawn: unable to setup child process '%s'",
Packit 90a5c9
                     pl->program);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        char **args;
Packit 90a5c9
Packit 90a5c9
        apr_tokenize_to_argv(pl->program, &args, pl->p);
Packit 90a5c9
        procnew = apr_pcalloc(pl->p, sizeof(apr_proc_t));
Packit 90a5c9
        status = apr_proc_create(procnew, args[0], (const char * const *) args,
Packit 90a5c9
                                 NULL, procattr, pl->p);
Packit 90a5c9
Packit 90a5c9
        if (status == APR_SUCCESS) {
Packit 90a5c9
            pl->pid = procnew;
Packit 90a5c9
            /* procnew->in was dup2'd from pl->write_fd;
Packit 90a5c9
             * since the original fd is still valid, close the copy to
Packit 90a5c9
             * avoid a leak. */
Packit 90a5c9
            apr_file_close(procnew->in);
Packit 90a5c9
            procnew->in = NULL;
Packit 90a5c9
            apr_proc_other_child_register(procnew, piped_log_maintenance, pl,
Packit 90a5c9
                                          pl->write_fd, pl->p);
Packit 90a5c9
            close_handle_in_child(pl->p, pl->read_fd);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            /* Something bad happened, give up and go away. */
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_STARTUP, status, ap_server_conf, APLOGNO(00104)
Packit 90a5c9
                         "unable to start piped log program '%s'",
Packit 90a5c9
                         pl->program);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return status;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
Packit 90a5c9
{
Packit 90a5c9
    piped_log *pl = data;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    int mpm_state;
Packit 90a5c9
Packit 90a5c9
    switch (reason) {
Packit 90a5c9
    case APR_OC_REASON_DEATH:
Packit 90a5c9
    case APR_OC_REASON_LOST:
Packit 90a5c9
        pl->pid = NULL; /* in case we don't get it going again, this
Packit 90a5c9
                         * tells other logic not to try to kill it
Packit 90a5c9
                         */
Packit 90a5c9
        apr_proc_other_child_unregister(pl);
Packit 90a5c9
        rv = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, APLOGNO(00105)
Packit 90a5c9
                         "can't query MPM state; not restarting "
Packit 90a5c9
                         "piped log program '%s'",
Packit 90a5c9
                         pl->program);
Packit 90a5c9
        }
Packit 90a5c9
        else if (mpm_state != AP_MPMQ_STOPPING) {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, APLOGNO(00106)
Packit 90a5c9
                         "piped log program '%s' failed unexpectedly",
Packit 90a5c9
                         pl->program);
Packit 90a5c9
            if ((rv = piped_log_spawn(pl)) != APR_SUCCESS) {
Packit 90a5c9
                /* what can we do?  This could be the error log we're having
Packit 90a5c9
                 * problems opening up... */
Packit 90a5c9
                ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL, APLOGNO(00107)
Packit 90a5c9
                             "piped_log_maintenance: unable to respawn '%s'",
Packit 90a5c9
                             pl->program);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        break;
Packit 90a5c9
Packit 90a5c9
    case APR_OC_REASON_UNWRITABLE:
Packit 90a5c9
        /* We should not kill off the pipe here, since it may only be full.
Packit 90a5c9
         * If it really is locked, we should kill it off manually. */
Packit 90a5c9
    break;
Packit 90a5c9
Packit 90a5c9
    case APR_OC_REASON_RESTART:
Packit 90a5c9
        if (pl->pid != NULL) {
Packit 90a5c9
            apr_proc_kill(pl->pid, SIGTERM);
Packit 90a5c9
            pl->pid = NULL;
Packit 90a5c9
        }
Packit 90a5c9
        break;
Packit 90a5c9
Packit 90a5c9
    case APR_OC_REASON_UNREGISTER:
Packit 90a5c9
        break;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static apr_status_t piped_log_cleanup_for_exec(void *data)
Packit 90a5c9
{
Packit 90a5c9
    piped_log *pl = data;
Packit 90a5c9
Packit 90a5c9
    apr_file_close(pl->read_fd);
Packit 90a5c9
    apr_file_close(pl->write_fd);
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static apr_status_t piped_log_cleanup(void *data)
Packit 90a5c9
{
Packit 90a5c9
    piped_log *pl = data;
Packit 90a5c9
Packit 90a5c9
    if (pl->pid != NULL) {
Packit 90a5c9
        apr_proc_kill(pl->pid, SIGTERM);
Packit 90a5c9
    }
Packit 90a5c9
    return piped_log_cleanup_for_exec(data);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(piped_log *) ap_open_piped_log_ex(apr_pool_t *p,
Packit 90a5c9
                                             const char *program,
Packit 90a5c9
                                             apr_cmdtype_e cmdtype)
Packit 90a5c9
{
Packit 90a5c9
    piped_log *pl;
Packit 90a5c9
Packit 90a5c9
    pl = apr_palloc(p, sizeof (*pl));
Packit 90a5c9
    pl->p = p;
Packit 90a5c9
    pl->program = apr_pstrdup(p, program);
Packit 90a5c9
    pl->pid = NULL;
Packit 90a5c9
    pl->cmdtype = cmdtype;
Packit 90a5c9
    if (apr_file_pipe_create_ex(&pl->read_fd,
Packit 90a5c9
                                &pl->write_fd,
Packit 90a5c9
                                APR_FULL_BLOCK, p) != APR_SUCCESS) {
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
    apr_pool_cleanup_register(p, pl, piped_log_cleanup,
Packit 90a5c9
                              piped_log_cleanup_for_exec);
Packit 90a5c9
    if (piped_log_spawn(pl) != APR_SUCCESS) {
Packit 90a5c9
        apr_pool_cleanup_kill(p, pl, piped_log_cleanup);
Packit 90a5c9
        apr_file_close(pl->read_fd);
Packit 90a5c9
        apr_file_close(pl->write_fd);
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
    return pl;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
#else /* !AP_HAVE_RELIABLE_PIPED_LOGS */
Packit 90a5c9
Packit 90a5c9
static apr_status_t piped_log_cleanup(void *data)
Packit 90a5c9
{
Packit 90a5c9
    piped_log *pl = data;
Packit 90a5c9
Packit 90a5c9
    apr_file_close(pl->write_fd);
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(piped_log *) ap_open_piped_log_ex(apr_pool_t *p,
Packit 90a5c9
                                             const char *program,
Packit 90a5c9
                                             apr_cmdtype_e cmdtype)
Packit 90a5c9
{
Packit 90a5c9
    piped_log *pl;
Packit 90a5c9
    apr_file_t *dummy = NULL;
Packit 90a5c9
    int rc;
Packit 90a5c9
Packit 90a5c9
    rc = log_child(p, program, &dummy, cmdtype, 0);
Packit 90a5c9
    if (rc != APR_SUCCESS) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, ap_server_conf, APLOGNO(00108)
Packit 90a5c9
                     "Couldn't start piped log process '%s'.",
Packit 90a5c9
                     (program == NULL) ? "NULL" : program);
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    pl = apr_palloc(p, sizeof (*pl));
Packit 90a5c9
    pl->p = p;
Packit 90a5c9
    pl->read_fd = NULL;
Packit 90a5c9
    pl->write_fd = dummy;
Packit 90a5c9
    apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
Packit 90a5c9
Packit 90a5c9
    return pl;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p,
Packit 90a5c9
                                          const char *program)
Packit 90a5c9
{
Packit 90a5c9
    apr_cmdtype_e cmdtype = APR_PROGRAM_ENV;
Packit 90a5c9
Packit 90a5c9
    /* In 2.4 favor PROGRAM_ENV, accept "||prog" syntax for compatibility
Packit 90a5c9
     * and "|$cmd" to override the default.
Packit 90a5c9
     * Any 2.2 backport would continue to favor SHELLCMD_ENV so there
Packit 90a5c9
     * accept "||prog" to override, and "|$cmd" to ease conversion.
Packit 90a5c9
     */
Packit 90a5c9
    if (*program == '|')
Packit 90a5c9
        ++program;
Packit 90a5c9
    if (*program == '$') {
Packit 90a5c9
        cmdtype = APR_SHELLCMD_ENV;
Packit 90a5c9
        ++program;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return ap_open_piped_log_ex(p, program, cmdtype);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
Packit 90a5c9
{
Packit 90a5c9
    apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(const char *) ap_parse_log_level(const char *str, int *val)
Packit 90a5c9
{
Packit 90a5c9
    char *err = "Log level keyword must be one of emerg/alert/crit/error/warn/"
Packit 90a5c9
                "notice/info/debug/trace1/.../trace8";
Packit 90a5c9
    int i = 0;
Packit 90a5c9
Packit 90a5c9
    if (str == NULL)
Packit 90a5c9
        return err;
Packit 90a5c9
Packit 90a5c9
    while (priorities[i].t_name != NULL) {
Packit 90a5c9
        if (!strcasecmp(str, priorities[i].t_name)) {
Packit 90a5c9
            *val = priorities[i].t_val;
Packit 90a5c9
            return NULL;
Packit 90a5c9
        }
Packit 90a5c9
        i++;
Packit 90a5c9
    }
Packit 90a5c9
    return err;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_IMPLEMENT_HOOK_VOID(error_log,
Packit 90a5c9
                       (const ap_errorlog_info *info, const char *errstr),
Packit 90a5c9
                       (info, errstr))
Packit 90a5c9
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(int, generate_log_id,
Packit 90a5c9
                            (const conn_rec *c, const request_rec *r,
Packit 90a5c9
                             const char **id),
Packit 90a5c9
                            (c, r, id), DECLINED)