Coverage Report

Created: 2026-02-26 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httpd/server/log.c
Line
Count
Source
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
 * contributor license agreements.  See the NOTICE file distributed with
3
 * this work for additional information regarding copyright ownership.
4
 * The ASF licenses this file to You under the Apache License, Version 2.0
5
 * (the "License"); you may not use this file except in compliance with
6
 * the License.  You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
/*
18
 * http_log.c: Dealing with the logs and errors
19
 *
20
 * Rob McCool
21
 *
22
 */
23
24
#include "apr.h"
25
#include "apr_general.h"        /* for signal stuff */
26
#include "apr_strings.h"
27
#include "apr_errno.h"
28
#include "apu_version.h"
29
#if (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7)
30
#include "apu_errno.h"
31
#endif
32
#include "apr_thread_proc.h"
33
#include "apr_lib.h"
34
#include "apr_signal.h"
35
#include "apr_portable.h"
36
#include "apr_base64.h"
37
38
#define APR_WANT_STDIO
39
#define APR_WANT_STRFUNC
40
#include "apr_want.h"
41
42
#if APR_HAVE_STDARG_H
43
#include <stdarg.h>
44
#endif
45
#if APR_HAVE_UNISTD_H
46
#include <unistd.h>
47
#endif
48
#if APR_HAVE_PROCESS_H
49
#include <process.h>            /* for getpid() on Win32 */
50
#endif
51
52
#include "ap_config.h"
53
#include "httpd.h"
54
#include "http_config.h"
55
#include "http_core.h"
56
#include "http_log.h"
57
#include "http_main.h"
58
#include "util_time.h"
59
#include "ap_mpm.h"
60
#include "ap_provider.h"
61
#include "ap_listen.h"
62
63
#ifdef HAVE_SYS_GETTID
64
#include <sys/syscall.h>
65
#include <sys/types.h>
66
#endif
67
68
#ifdef HAVE_PTHREAD_NP_H
69
#include <pthread_np.h>
70
#endif
71
72
/* we know core's module_index is 0 */
73
#undef APLOG_MODULE_INDEX
74
0
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
75
76
#ifndef DEFAULT_LOG_TID
77
#define DEFAULT_LOG_TID NULL
78
#endif
79
80
typedef struct {
81
    const char *t_name;
82
    int t_val;
83
} TRANS;
84
85
APR_HOOK_STRUCT(
86
    APR_HOOK_LINK(error_log)
87
    APR_HOOK_LINK(generate_log_id)
88
)
89
90
int AP_DECLARE_DATA ap_default_loglevel = DEFAULT_LOGLEVEL;
91
92
static const TRANS priorities[] = {
93
    {"emerg",   APLOG_EMERG},
94
    {"alert",   APLOG_ALERT},
95
    {"crit",    APLOG_CRIT},
96
    {"error",   APLOG_ERR},
97
    {"warn",    APLOG_WARNING},
98
    {"notice",  APLOG_NOTICE},
99
    {"info",    APLOG_INFO},
100
    {"debug",   APLOG_DEBUG},
101
    {"trace1",  APLOG_TRACE1},
102
    {"trace2",  APLOG_TRACE2},
103
    {"trace3",  APLOG_TRACE3},
104
    {"trace4",  APLOG_TRACE4},
105
    {"trace5",  APLOG_TRACE5},
106
    {"trace6",  APLOG_TRACE6},
107
    {"trace7",  APLOG_TRACE7},
108
    {"trace8",  APLOG_TRACE8},
109
    {NULL,      -1},
110
};
111
112
static apr_pool_t *stderr_pool = NULL;
113
114
static apr_file_t *stderr_log = NULL;
115
116
/* track pipe handles to close in child process */
117
typedef struct read_handle_t {
118
    struct read_handle_t *next;
119
    apr_file_t *handle;
120
} read_handle_t;
121
122
static read_handle_t *read_handles;
123
124
/**
125
 * @brief The piped logging structure.
126
 *
127
 * Piped logs are used to move functionality out of the main server.
128
 * For example, log rotation is done with piped logs.
129
 */
130
struct piped_log {
131
    /** The pool to use for the piped log */
132
    apr_pool_t *p;
133
    /** The pipe between the server and the logging process */
134
    apr_file_t *read_fd, *write_fd;
135
#ifdef AP_HAVE_RELIABLE_PIPED_LOGS
136
    /** The name of the program the logging process is running */
137
    char *program;
138
    /** The pid of the logging process */
139
    apr_proc_t *pid;
140
    /** How to reinvoke program when it must be replaced */
141
    apr_cmdtype_e cmdtype;
142
#endif
143
};
144
145
AP_DECLARE(apr_file_t *) ap_piped_log_read_fd(piped_log *pl)
146
0
{
147
0
    return pl->read_fd;
148
0
}
149
150
AP_DECLARE(apr_file_t *) ap_piped_log_write_fd(piped_log *pl)
151
0
{
152
0
    return pl->write_fd;
153
0
}
154
155
/* remember to close this handle in the child process
156
 *
157
 * On Win32 this makes zero sense, because we don't
158
 * take the parent process's child procs.
159
 * If the win32 parent instead passed each and every
160
 * logger write handle from itself down to the child,
161
 * and the parent manages all aspects of keeping the
162
 * reliable pipe log children alive, this would still
163
 * make no sense :)  Cripple it on Win32.
164
 */
165
static void close_handle_in_child(apr_pool_t *p, apr_file_t *f)
166
0
{
167
0
#ifndef WIN32
168
0
    read_handle_t *new_handle;
169
170
0
    new_handle = apr_pcalloc(p, sizeof(read_handle_t));
171
0
    new_handle->next = read_handles;
172
0
    new_handle->handle = f;
173
0
    read_handles = new_handle;
174
0
#endif
175
0
}
176
177
void ap_logs_child_init(apr_pool_t *p, server_rec *s)
178
0
{
179
0
    read_handle_t *cur = read_handles;
180
181
0
    while (cur) {
182
0
        apr_file_close(cur->handle);
183
0
        cur = cur->next;
184
0
    }
185
0
}
186
187
AP_DECLARE(void) ap_open_stderr_log(apr_pool_t *p)
188
0
{
189
0
    apr_file_open_stderr(&stderr_log, p);
190
0
}
191
192
AP_DECLARE(apr_status_t) ap_replace_stderr_log(apr_pool_t *p,
193
                                               const char *fname)
194
0
{
195
0
    apr_file_t *stderr_file;
196
0
    apr_status_t rc;
197
0
    char *filename = ap_server_root_relative(p, fname);
198
0
    if (!filename) {
199
0
        ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
200
0
                     APR_EBADPATH, ap_server_conf, APLOGNO(00085) "Invalid -E error log file %s",
201
0
                     fname);
202
0
        return APR_EBADPATH;
203
0
    }
204
0
    if ((rc = apr_file_open(&stderr_file, filename,
205
0
                            APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
206
0
                            APR_OS_DEFAULT, p)) != APR_SUCCESS) {
207
0
        ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, ap_server_conf, APLOGNO(00086)
208
0
                     "%s: could not open error log file %s.",
209
0
                     ap_server_argv0, fname);
210
0
        return rc;
211
0
    }
212
0
    if (!stderr_pool) {
213
        /* This is safe provided we revert it when we are finished.
214
         * We don't manager the callers pool!
215
         */
216
0
        stderr_pool = p;
217
0
    }
218
0
    if ((rc = apr_file_open_stderr(&stderr_log, stderr_pool))
219
0
            == APR_SUCCESS) {
220
0
        apr_file_flush(stderr_log);
221
0
        if ((rc = apr_file_dup2(stderr_log, stderr_file, stderr_pool))
222
0
                == APR_SUCCESS) {
223
0
            apr_file_close(stderr_file);
224
            /*
225
             * You might ponder why stderr_pool should survive?
226
             * The trouble is, stderr_pool may have s_main->error_log,
227
             * so we aren't in a position to destroy stderr_pool until
228
             * the next recycle.  There's also an apparent bug which
229
             * is not; if some folk decided to call this function before
230
             * the core open error logs hook, this pool won't survive.
231
             * Neither does the stderr logger, so this isn't a problem.
232
             */
233
0
        }
234
0
    }
235
    /* Revert, see above */
236
0
    if (stderr_pool == p)
237
0
        stderr_pool = NULL;
238
239
0
    if (rc != APR_SUCCESS) {
240
0
        ap_log_error(APLOG_MARK, APLOG_CRIT, rc, NULL, APLOGNO(00087)
241
0
                     "unable to replace stderr with error log file");
242
0
    }
243
0
    return rc;
244
0
}
245
246
static void log_child_errfn(apr_pool_t *pool, apr_status_t err,
247
                            const char *description)
248
0
{
249
0
    ap_log_error(APLOG_MARK, APLOG_ERR, err, NULL, APLOGNO(00088)
250
0
                 "%s", description);
251
0
}
252
253
/* Create a child process running PROGNAME with a pipe connected to
254
 * the child's stdin.  The write-end of the pipe will be placed in
255
 * *FPIN on successful return.  If dummy_stderr is non-zero, the
256
 * stderr for the child will be the same as the stdout of the parent.
257
 * Otherwise the child will inherit the stderr from the parent. */
258
static int log_child(apr_pool_t *p, const char *progname,
259
                     apr_file_t **fpin, apr_cmdtype_e cmdtype,
260
                     int dummy_stderr)
261
0
{
262
    /* Child process code for 'ErrorLog "|..."';
263
     * may want a common framework for this, since I expect it will
264
     * be common for other foo-loggers to want this sort of thing...
265
     */
266
0
    apr_status_t rc;
267
0
    apr_procattr_t *procattr;
268
0
    apr_proc_t *procnew;
269
0
    apr_file_t *errfile;
270
271
0
    if (((rc = apr_procattr_create(&procattr, p)) == APR_SUCCESS)
272
0
        && ((rc = apr_procattr_dir_set(procattr,
273
0
                                       ap_server_root)) == APR_SUCCESS)
274
0
        && ((rc = apr_procattr_cmdtype_set(procattr, cmdtype)) == APR_SUCCESS)
275
0
        && ((rc = apr_procattr_io_set(procattr,
276
0
                                      APR_FULL_BLOCK,
277
0
                                      APR_NO_PIPE,
278
0
                                      APR_NO_PIPE)) == APR_SUCCESS)
279
0
        && ((rc = apr_procattr_error_check_set(procattr, 1)) == APR_SUCCESS)
280
0
        && ((rc = apr_procattr_child_errfn_set(procattr, log_child_errfn))
281
0
                == APR_SUCCESS)) {
282
0
        char **args;
283
284
0
        apr_tokenize_to_argv(progname, &args, p);
285
0
        procnew = (apr_proc_t *)apr_pcalloc(p, sizeof(*procnew));
286
287
0
        if (dummy_stderr) {
288
0
            if ((rc = apr_file_open_stdout(&errfile, p)) == APR_SUCCESS)
289
0
                rc = apr_procattr_child_err_set(procattr, errfile, NULL);
290
0
        }
291
292
0
        if (rc == APR_SUCCESS) {
293
0
            rc = apr_proc_create(procnew, args[0], (const char * const *)args,
294
0
                                 NULL, procattr, p);
295
0
        }
296
297
0
        if (rc == APR_SUCCESS) {
298
0
            apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
299
0
            (*fpin) = procnew->in;
300
            /* read handle to pipe not kept open, so no need to call
301
             * close_handle_in_child()
302
             */
303
0
        }
304
0
    }
305
306
0
    return rc;
307
0
}
308
309
/* Open the error log for the given server_rec.  If IS_MAIN is
310
 * non-zero, s is the main server. */
311
static int open_error_log(server_rec *s, int is_main, apr_pool_t *p)
312
0
{
313
0
    const char *fname;
314
0
    int rc;
315
316
0
    if (*s->error_fname == '|') {
317
0
        apr_file_t *dummy = NULL;
318
0
        apr_cmdtype_e cmdtype = APR_PROGRAM_ENV;
319
0
        fname = s->error_fname + 1;
320
321
        /* In 2.4 favor PROGRAM_ENV, accept "||prog" syntax for compatibility
322
         * and "|$cmd" to override the default.
323
         * Any 2.2 backport would continue to favor SHELLCMD_ENV so there
324
         * accept "||prog" to override, and "|$cmd" to ease conversion.
325
         */
326
0
        if (*fname == '|')
327
0
            ++fname;
328
0
        if (*fname == '$') {
329
0
            cmdtype = APR_SHELLCMD_ENV;
330
0
            ++fname;
331
0
        }
332
333
        /* Spawn a new child logger.  If this is the main server_rec,
334
         * the new child must use a dummy stderr since the current
335
         * stderr might be a pipe to the old logger.  Otherwise, the
336
         * child inherits the parents stderr. */
337
0
        rc = log_child(p, fname, &dummy, cmdtype, is_main);
338
0
        if (rc != APR_SUCCESS) {
339
0
            ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, ap_server_conf, APLOGNO(00089)
340
0
                         "Couldn't start ErrorLog process '%s'.",
341
0
                         s->error_fname + 1);
342
0
            return DONE;
343
0
        }
344
345
0
        s->error_log = dummy;
346
0
    }
347
0
    else if (s->errorlog_provider) {
348
0
        s->errorlog_provider_handle = s->errorlog_provider->init(p, s);
349
0
        s->error_log = NULL;
350
0
        if (!s->errorlog_provider_handle) {
351
            /* provider must log something to the console */
352
0
            return DONE;
353
0
        }
354
0
    }
355
0
    else {
356
0
        fname = ap_server_root_relative(p, s->error_fname);
357
0
        if (!fname) {
358
0
            ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EBADPATH, ap_server_conf, APLOGNO(00090)
359
0
                         "%s: Invalid error log path %s.",
360
0
                         ap_server_argv0, s->error_fname);
361
0
            return DONE;
362
0
        }
363
0
        if ((rc = apr_file_open(&s->error_log, fname,
364
0
                               APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
365
0
                               APR_OS_DEFAULT, p)) != APR_SUCCESS) {
366
0
            ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, ap_server_conf, APLOGNO(00091)
367
0
                         "%s: could not open error log file %s.",
368
0
                         ap_server_argv0, fname);
369
0
            return DONE;
370
0
        }
371
0
    }
372
373
0
    return OK;
374
0
}
375
376
int ap_open_logs(apr_pool_t *pconf, apr_pool_t *p /* plog */,
377
                 apr_pool_t *ptemp, server_rec *s_main)
378
0
{
379
0
    apr_pool_t *stderr_p;
380
0
    server_rec *virt, *q;
381
0
    int replace_stderr;
382
383
384
    /* Register to throw away the read_handles list when we
385
     * cleanup plog.  Upon fork() for the apache children,
386
     * this read_handles list is closed so only the parent
387
     * can relaunch a lost log child.  These read handles
388
     * are always closed on exec.
389
     * We won't care what happens to our stderr log child
390
     * between log phases, so we don't mind losing stderr's
391
     * read_handle a little bit early.
392
     */
393
0
    apr_pool_cleanup_register(p, &read_handles, ap_pool_cleanup_set_null,
394
0
                              apr_pool_cleanup_null);
395
396
    /* HERE we need a stdout log that outlives plog.
397
     * We *presume* the parent of plog is a process
398
     * or global pool which spans server restarts.
399
     * Create our stderr_pool as a child of the plog's
400
     * parent pool.
401
     */
402
0
    apr_pool_create(&stderr_p, apr_pool_parent_get(p));
403
0
    apr_pool_tag(stderr_p, "stderr_pool");
404
405
0
    if (open_error_log(s_main, 1, stderr_p) != OK) {
406
0
        return DONE;
407
0
    }
408
409
0
    replace_stderr = 1;
410
0
    if (s_main->error_log) {
411
0
        apr_status_t rv;
412
413
        /* Replace existing stderr with new log. */
414
0
        apr_file_flush(s_main->error_log);
415
0
        rv = apr_file_dup2(stderr_log, s_main->error_log, stderr_p);
416
0
        if (rv != APR_SUCCESS) {
417
0
            ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s_main, APLOGNO(00092)
418
0
                         "unable to replace stderr with error_log");
419
0
        }
420
0
        else {
421
            /* We are done with stderr_pool, close it, killing
422
             * the previous generation's stderr logger
423
             */
424
0
            if (stderr_pool)
425
0
                apr_pool_destroy(stderr_pool);
426
0
            stderr_pool = stderr_p;
427
0
            replace_stderr = 0;
428
            /*
429
             * Now that we have dup'ed s_main->error_log to stderr_log
430
             * close it and set s_main->error_log to stderr_log. This avoids
431
             * this fd being inherited by the next piped logger who would
432
             * keep open the writing end of the pipe that this one uses
433
             * as stdin. This in turn would prevent the piped logger from
434
             * exiting.
435
             */
436
0
            apr_file_close(s_main->error_log);
437
0
            s_main->error_log = stderr_log;
438
0
        }
439
0
    }
440
    /* note that stderr may still need to be replaced with something
441
     * because it points to the old error log, or back to the tty
442
     * of the submitter.
443
     * XXX: This is BS - /dev/null is non-portable
444
     *      errno-as-apr_status_t is also non-portable
445
     */
446
447
#ifdef WIN32
448
#define NULL_DEVICE "nul"
449
#else
450
0
#define NULL_DEVICE "/dev/null"
451
0
#endif
452
453
0
    if (replace_stderr) {
454
0
        if (freopen(NULL_DEVICE, "w", stderr) == NULL) {
455
0
            ap_log_error(APLOG_MARK, APLOG_CRIT, errno, s_main, APLOGNO(00093)
456
0
                        "unable to replace stderr with %s", NULL_DEVICE);
457
0
        }
458
0
        stderr_log = NULL;
459
0
    }
460
461
0
    for (virt = s_main->next; virt; virt = virt->next) {
462
0
        if (virt->error_fname) {
463
0
            for (q=s_main; q != virt; q = q->next) {
464
0
                if (q->error_fname != NULL
465
0
                    && strcmp(q->error_fname, virt->error_fname) == 0) {
466
0
                    break;
467
0
                }
468
0
            }
469
470
0
            if (q == virt) {
471
0
                if (open_error_log(virt, 0, p) != OK) {
472
0
                    return DONE;
473
0
                }
474
0
            }
475
0
            else {
476
0
                virt->error_log = q->error_log;
477
0
            }
478
0
        }
479
0
        else if (virt->errorlog_provider) {
480
            /* separately-configured vhost-specific provider */
481
0
            if (open_error_log(virt, 0, p) != OK) {
482
0
                return DONE;
483
0
            }
484
0
        }
485
0
        else if (s_main->errorlog_provider) {
486
            /* inherit provider from s_main */
487
0
            virt->errorlog_provider = s_main->errorlog_provider;
488
0
            virt->errorlog_provider_handle = s_main->errorlog_provider_handle;
489
0
            virt->error_log = NULL;
490
0
        }
491
0
        else {
492
0
            virt->error_log = s_main->error_log;
493
0
        }
494
0
    }
495
0
    return OK;
496
0
}
497
498
0
AP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
499
0
    apr_file_t *errfile = NULL;
500
501
0
    apr_file_open_stderr(&errfile, s->process->pool);
502
0
    if (s->error_log != NULL) {
503
0
        apr_file_dup2(s->error_log, errfile, s->process->pool);
504
0
    }
505
0
}
506
507
static int cpystrn(char *buf, const char *arg, int buflen)
508
0
{
509
0
    char *end;
510
0
    if (!arg)
511
0
        return 0;
512
0
    end = apr_cpystrn(buf, arg, buflen);
513
0
    return end - buf;
514
0
}
515
516
517
static int log_remote_address(const ap_errorlog_info *info, const char *arg,
518
                              char *buf, int buflen)
519
0
{
520
0
    if (info->r && !(arg && *arg == 'c'))
521
0
        return apr_snprintf(buf, buflen, "%s:%d", info->r->useragent_ip,
522
0
                            info->r->useragent_addr ? info->r->useragent_addr->port : 0);
523
0
    else if (info->c)
524
0
        return apr_snprintf(buf, buflen, "%s:%d", info->c->client_ip,
525
0
                            info->c->client_addr ? info->c->client_addr->port : 0);
526
0
    else
527
0
        return 0;
528
0
}
529
530
static int log_local_address(const ap_errorlog_info *info, const char *arg,
531
                             char *buf, int buflen)
532
0
{
533
0
    if (info->c)
534
0
        return apr_snprintf(buf, buflen, "%s:%d", info->c->local_ip,
535
0
                            info->c->local_addr->port);
536
0
    else
537
0
        return 0;
538
0
}
539
540
static int log_pid(const ap_errorlog_info *info, const char *arg,
541
                   char *buf, int buflen)
542
0
{
543
0
    pid_t pid = getpid();
544
0
    return apr_snprintf(buf, buflen, "%" APR_PID_T_FMT, pid);
545
0
}
546
547
static int log_tid(const ap_errorlog_info *info, const char *arg,
548
                   char *buf, int buflen)
549
0
{
550
0
#if APR_HAS_THREADS
551
0
    int result;
552
0
#endif
553
0
#if defined(HAVE_GETTID) || defined(HAVE_SYS_GETTID) || defined(HAVE_PTHREAD_GETTHREADID_NP)
554
0
    if (arg && *arg == 'g') {
555
0
#if defined(HAVE_GETTID)
556
0
        pid_t tid = gettid();
557
#elif defined(HAVE_PTHREAD_GETTHREADID_NP)
558
        pid_t tid = pthread_getthreadid_np();
559
#else
560
        pid_t tid = syscall(SYS_gettid);
561
#endif
562
0
        if (tid == -1)
563
0
            return 0;
564
0
        return apr_snprintf(buf, buflen, "%"APR_PID_T_FMT, tid);
565
0
    }
566
0
#endif /* HAVE_GETTID || HAVE_SYS_GETTID */
567
0
#if APR_HAS_THREADS
568
0
    if (ap_mpm_query(AP_MPMQ_IS_THREADED, &result) == APR_SUCCESS
569
0
        && result != AP_MPMQ_NOT_SUPPORTED)
570
0
    {
571
0
        apr_os_thread_t tid = apr_os_thread_current();
572
0
        return apr_snprintf(buf, buflen, "%pT", &tid);
573
0
    }
574
0
#endif
575
0
    return 0;
576
0
}
577
578
static int log_ctime(const ap_errorlog_info *info, const char *arg,
579
                     char *buf, int buflen)
580
0
{
581
0
    int time_len = buflen;
582
0
    int option = AP_CTIME_OPTION_NONE;
583
584
0
    if (arg) {
585
0
        if (arg[0] == 'u' && !arg[1]) { /* no ErrorLogFormat (fast path) */
586
0
            option |= AP_CTIME_OPTION_USEC;
587
0
        }
588
0
        else if (arg[0] == 'm' && !arg[1]) { /* no ErrorLogFormat (fast path) - msec */
589
0
            option |= AP_CTIME_OPTION_MSEC;
590
0
        }
591
0
        else if (!ap_strchr_c(arg, '%')) { /* special "%{mcuz}t" formats */
592
0
            while (*arg) {
593
0
                switch (*arg++) {
594
0
                case 'm':
595
0
                    option |= AP_CTIME_OPTION_MSEC;
596
0
                    break;
597
0
                case 'u':
598
0
                    option |= AP_CTIME_OPTION_USEC;
599
0
                    break;
600
0
                case 'c':
601
0
                    option |= AP_CTIME_OPTION_COMPACT;
602
0
                    break;
603
0
                case 'z':
604
0
                    option |= AP_CTIME_OPTION_GMTOFF;
605
0
                    break;
606
0
                }
607
0
            }
608
0
        }
609
0
        else { /* "%{strftime %-format}t" */
610
0
            apr_size_t len = 0;
611
0
            apr_time_exp_t expt;
612
0
            ap_explode_recent_localtime(&expt, apr_time_now());
613
0
            apr_strftime(buf, &len, buflen, arg, &expt);
614
0
            return (int)len;
615
0
        }
616
0
    }
617
618
0
    ap_recent_ctime_ex(buf, apr_time_now(), option, &time_len);
619
620
    /* ap_recent_ctime_ex includes the trailing \0 in time_len */
621
0
    return time_len - 1;
622
0
}
623
624
static int log_loglevel(const ap_errorlog_info *info, const char *arg,
625
                        char *buf, int buflen)
626
0
{
627
0
    if (info->level < 0)
628
0
        return 0;
629
0
    else
630
0
        return cpystrn(buf, priorities[info->level].t_name, buflen);
631
0
}
632
633
static int log_log_id(const ap_errorlog_info *info, const char *arg,
634
                      char *buf, int buflen)
635
0
{
636
    /*
637
     * C: log conn log_id if available,
638
     * c: log conn log id if available and not a once-per-request log line
639
     * else: log request log id if available
640
     */
641
0
    if (arg && (*arg == 'c' || *arg == 'C')) {
642
0
        if (info->c && (*arg != 'C' || !info->r)) {
643
0
            return cpystrn(buf, info->c->log_id, buflen);
644
0
        }
645
0
    }
646
0
    else if (info->rmain) {
647
0
        return cpystrn(buf, info->rmain->log_id, buflen);
648
0
    }
649
0
    return 0;
650
0
}
651
652
static int log_keepalives(const ap_errorlog_info *info, const char *arg,
653
                          char *buf, int buflen)
654
0
{
655
0
    if (!info->c)
656
0
        return 0;
657
658
0
    return apr_snprintf(buf, buflen, "%d", info->c->keepalives);
659
0
}
660
661
static int log_module_name(const ap_errorlog_info *info, const char *arg,
662
                           char *buf, int buflen)
663
0
{
664
0
    return cpystrn(buf, ap_find_module_short_name(info->module_index), buflen);
665
0
}
666
667
static int log_file_line(const ap_errorlog_info *info, const char *arg,
668
                         char *buf, int buflen)
669
0
{
670
0
    if (info->file == NULL) {
671
0
        return 0;
672
0
    }
673
0
    else {
674
0
        const char *file = info->file;
675
#if defined(_OSD_POSIX) || defined(WIN32) || defined(__MVS__)
676
        char tmp[256];
677
        char *e = strrchr(file, '/');
678
#ifdef WIN32
679
        if (!e) {
680
            e = strrchr(file, '\\');
681
        }
682
#endif
683
684
        /* In OSD/POSIX, the compiler returns for __FILE__
685
         * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
686
         * (it even returns an absolute path for sources in
687
         * the current directory). Here we try to strip this
688
         * down to the basename.
689
         */
690
        if (e != NULL && e[1] != '\0') {
691
            apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
692
            e = &tmp[strlen(tmp)-1];
693
            if (*e == ')') {
694
                *e = '\0';
695
            }
696
            file = tmp;
697
        }
698
#else /* _OSD_POSIX || WIN32 */
699
0
        const char *p;
700
        /* On Unix, __FILE__ may be an absolute path in a
701
         * VPATH build. */
702
0
        if (file[0] == '/' && (p = ap_strrchr_c(file, '/')) != NULL) {
703
0
            file = p + 1;
704
0
        }
705
0
#endif /*_OSD_POSIX || WIN32 */
706
0
        return apr_snprintf(buf, buflen, "%s(%d)", file, info->line);
707
0
    }
708
0
}
709
710
static int log_apr_status(const ap_errorlog_info *info, const char *arg,
711
                          char *buf, int buflen)
712
0
{
713
0
    apr_status_t status = info->status;
714
0
    int len;
715
0
    if (!status)
716
0
        return 0;
717
718
0
    if (status < APR_OS_START_EAIERR) {
719
0
        len = apr_snprintf(buf, buflen, "(%d)", status);
720
0
    }
721
0
    else if (status < APR_OS_START_SYSERR) {
722
0
        len = apr_snprintf(buf, buflen, "(EAI %d)",
723
0
                           status - APR_OS_START_EAIERR);
724
0
    }
725
0
    else if (status < 100000 + APR_OS_START_SYSERR) {
726
0
        len = apr_snprintf(buf, buflen, "(OS %d)",
727
0
                           status - APR_OS_START_SYSERR);
728
0
    }
729
0
    else {
730
0
        len = apr_snprintf(buf, buflen, "(os 0x%08x)",
731
0
                           status - APR_OS_START_SYSERR);
732
0
    }
733
#if (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7)
734
    if (status < APR_UTIL_START_STATUS) {
735
        apr_strerror(status, buf + len, buflen - len);
736
    }
737
    else if (status < (APR_UTIL_START_STATUS + APR_UTIL_ERRSPACE_SIZE)) {
738
        apu_strerror(status, buf + len, buflen - len);
739
    }
740
    else {
741
        apr_strerror(status, buf + len, buflen - len);
742
    }
743
#else
744
0
    apr_strerror(status, buf + len, buflen - len);
745
0
#endif
746
0
    len += strlen(buf + len);
747
0
    return len;
748
0
}
749
750
static int log_server_name(const ap_errorlog_info *info, const char *arg,
751
                           char *buf, int buflen)
752
0
{
753
0
    if (info->r)
754
0
        return cpystrn(buf, ap_get_server_name((request_rec *)info->r), buflen);
755
756
0
    return 0;
757
0
}
758
759
static int log_virtual_host(const ap_errorlog_info *info, const char *arg,
760
                            char *buf, int buflen)
761
0
{
762
0
    if (info->s)
763
0
        return cpystrn(buf, info->s->server_hostname, buflen);
764
765
0
    return 0;
766
0
}
767
768
769
static int log_table_entry(const apr_table_t *table, const char *name,
770
                           char *buf, int buflen)
771
0
{
772
0
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
773
0
    const char *value;
774
0
    char scratch[MAX_STRING_LEN];
775
776
0
    if ((value = apr_table_get(table, name)) != NULL) {
777
0
        ap_escape_errorlog_item(scratch, value, MAX_STRING_LEN);
778
0
        return cpystrn(buf, scratch, buflen);
779
0
    }
780
781
0
    return 0;
782
#else
783
    return cpystrn(buf, apr_table_get(table, name), buflen);
784
#endif
785
0
}
786
787
static int log_header(const ap_errorlog_info *info, const char *arg,
788
                      char *buf, int buflen)
789
0
{
790
0
    if (info->r)
791
0
        return log_table_entry(info->r->headers_in, arg, buf, buflen);
792
793
0
    return 0;
794
0
}
795
796
static int log_note(const ap_errorlog_info *info, const char *arg,
797
                      char *buf, int buflen)
798
0
{
799
    /* XXX: maybe escaping the entry is not necessary for notes? */
800
0
    if (info->r)
801
0
        return log_table_entry(info->r->notes, arg, buf, buflen);
802
803
0
    return 0;
804
0
}
805
806
static int log_env_var(const ap_errorlog_info *info, const char *arg,
807
                      char *buf, int buflen)
808
0
{
809
0
    if (info->r)
810
0
        return log_table_entry(info->r->subprocess_env, arg, buf, buflen);
811
812
0
    return 0;
813
0
}
814
815
static int core_generate_log_id(const conn_rec *c, const request_rec *r,
816
                                 const char **idstring)
817
0
{
818
0
    apr_uint64_t id, tmp;
819
0
    pid_t pid;
820
0
    int len;
821
0
    char *encoded;
822
823
0
    if (r && r->request_time) {
824
0
        id = (apr_uint64_t)r->request_time;
825
0
    }
826
0
    else {
827
0
        id = (apr_uint64_t)apr_time_now();
828
0
    }
829
830
0
    pid = getpid();
831
0
    if (sizeof(pid_t) > 2) {
832
0
        tmp = pid;
833
0
        tmp = tmp << 40;
834
0
        id ^= tmp;
835
0
        pid = pid >> 24;
836
0
        tmp = pid;
837
0
        tmp = tmp << 56;
838
0
        id ^= tmp;
839
0
    }
840
0
    else {
841
0
        tmp = pid;
842
0
        tmp = tmp << 40;
843
0
        id ^= tmp;
844
0
    }
845
0
#if APR_HAS_THREADS
846
0
    {
847
0
        apr_uintptr_t tmp2 = (apr_uintptr_t)c->current_thread;
848
0
        tmp = tmp2;
849
0
        tmp = tmp << 32;
850
0
        id ^= tmp;
851
0
    }
852
0
#endif
853
854
0
    len = apr_base64_encode_len(sizeof(id)); /* includes trailing \0 */
855
0
    encoded = apr_palloc(r ? r->pool : c->pool, len);
856
0
    apr_base64_encode(encoded, (char *)&id, sizeof(id));
857
858
    /* Skip the last char, it is always '=' */
859
0
    encoded[len - 2] = '\0';
860
861
0
    *idstring = encoded;
862
863
0
    return OK;
864
0
}
865
866
static void add_log_id(const conn_rec *c, const request_rec *r)
867
0
{
868
0
    const char **id;
869
    /* need to cast const away */
870
0
    if (r) {
871
0
        id = &((request_rec *)r)->log_id;
872
0
    }
873
0
    else {
874
0
        id = &((conn_rec *)c)->log_id;
875
0
    }
876
877
0
    ap_run_generate_log_id(c, r, id);
878
0
}
879
880
AP_DECLARE(void) ap_register_log_hooks(apr_pool_t *p)
881
0
{
882
0
    ap_hook_generate_log_id(core_generate_log_id, NULL, NULL,
883
0
                            APR_HOOK_REALLY_LAST);
884
885
0
    ap_register_errorlog_handler(p, "a", log_remote_address, 0);
886
0
    ap_register_errorlog_handler(p, "A", log_local_address, 0);
887
0
    ap_register_errorlog_handler(p, "e", log_env_var, 0);
888
0
    ap_register_errorlog_handler(p, "E", log_apr_status, 0);
889
0
    ap_register_errorlog_handler(p, "F", log_file_line, 0);
890
0
    ap_register_errorlog_handler(p, "i", log_header, 0);
891
0
    ap_register_errorlog_handler(p, "k", log_keepalives, 0);
892
0
    ap_register_errorlog_handler(p, "l", log_loglevel, 0);
893
0
    ap_register_errorlog_handler(p, "L", log_log_id, 0);
894
0
    ap_register_errorlog_handler(p, "m", log_module_name, 0);
895
0
    ap_register_errorlog_handler(p, "n", log_note, 0);
896
0
    ap_register_errorlog_handler(p, "P", log_pid, 0);
897
0
    ap_register_errorlog_handler(p, "t", log_ctime, 0);
898
0
    ap_register_errorlog_handler(p, "T", log_tid, 0);
899
0
    ap_register_errorlog_handler(p, "v", log_virtual_host, 0);
900
0
    ap_register_errorlog_handler(p, "V", log_server_name, 0);
901
0
}
902
903
/*
904
 * This is used if no error log format is defined and during startup.
905
 * It automatically omits the timestamp if logging using provider.
906
 */
907
static int do_errorlog_default(const ap_errorlog_info *info, char *buf,
908
                               int buflen, int *errstr_start, int *errstr_end,
909
                               const char *errstr_fmt, va_list args)
910
0
{
911
0
    int len = 0;
912
0
    int field_start = 0;
913
0
    int item_len;
914
0
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
915
0
    char scratch[MAX_STRING_LEN];
916
0
#endif
917
918
0
    if (!info->using_provider && !info->startup) {
919
0
        buf[len++] = '[';
920
0
        len += log_ctime(info, "u", buf + len, buflen - len);
921
0
        buf[len++] = ']';
922
0
        buf[len++] = ' ';
923
0
    }
924
925
0
    if (!info->startup) {
926
0
        buf[len++] = '[';
927
0
        len += log_module_name(info, NULL, buf + len, buflen - len);
928
0
        buf[len++] = ':';
929
0
        len += log_loglevel(info, NULL, buf + len, buflen - len);
930
0
        len += cpystrn(buf + len, "] [pid ", buflen - len);
931
932
0
        len += log_pid(info, NULL, buf + len, buflen - len);
933
0
#if APR_HAS_THREADS
934
0
        field_start = len;
935
0
        len += cpystrn(buf + len, ":tid ", buflen - len);
936
0
        item_len = log_tid(info, DEFAULT_LOG_TID, buf + len, buflen - len);
937
0
        if (!item_len)
938
0
            len = field_start;
939
0
        else
940
0
            len += item_len;
941
0
#endif
942
0
        buf[len++] = ']';
943
0
        buf[len++] = ' ';
944
0
    }
945
946
0
    if (info->level >= APLOG_DEBUG) {
947
0
        item_len = log_file_line(info, NULL, buf + len, buflen - len);
948
0
        if (item_len) {
949
0
            len += item_len;
950
0
            len += cpystrn(buf + len, ": ", buflen - len);
951
0
        }
952
0
    }
953
954
0
    if (info->status) {
955
0
        item_len = log_apr_status(info, NULL, buf + len, buflen - len);
956
0
        if (item_len) {
957
0
            len += item_len;
958
0
            len += cpystrn(buf + len, ": ", buflen - len);
959
0
        }
960
0
    }
961
962
    /*
963
     * useragent_ip/client_ip can be client or backend server. If we have
964
     * a scoreboard handle, it is likely a client.
965
     */
966
0
    if (info->r) {
967
0
        len += apr_snprintf(buf + len, buflen - len, "[%s %s:%d] ",
968
0
                            info->r->connection->outgoing ? "remote" : "client",
969
0
                            info->r->useragent_ip,
970
0
                            info->r->useragent_addr ? info->r->useragent_addr->port : 0);
971
0
    }
972
0
    else if (info->c) {
973
0
        len += apr_snprintf(buf + len, buflen - len, "[%s %s:%d] ",
974
0
                            info->c->outgoing ? "remote" : "client",
975
0
                            info->c->client_ip,
976
0
                            info->c->client_addr ? info->c->client_addr->port : 0);
977
0
    }
978
979
    /* the actual error message */
980
0
    *errstr_start = len;
981
0
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
982
0
    if (apr_vsnprintf(scratch, MAX_STRING_LEN, errstr_fmt, args)) {
983
0
        len += ap_escape_errorlog_item(buf + len, scratch,
984
0
                                       buflen - len);
985
986
0
    }
987
#else
988
    len += apr_vsnprintf(buf + len, buflen - len, errstr_fmt, args);
989
#endif
990
0
    *errstr_end = len;
991
992
0
    field_start = len;
993
0
    len += cpystrn(buf + len, ", referer: ", buflen - len);
994
0
    item_len = log_header(info, "Referer", buf + len, buflen - len);
995
0
    if (item_len)
996
0
        len += item_len;
997
0
    else
998
0
        len = field_start;
999
1000
0
    return len;
1001
0
}
1002
1003
static int do_errorlog_format(apr_array_header_t *fmt, ap_errorlog_info *info,
1004
                              char *buf, int buflen, int *errstr_start,
1005
                              int *errstr_end, const char *err_fmt, va_list args)
1006
0
{
1007
0
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
1008
0
    char scratch[MAX_STRING_LEN];
1009
0
#endif
1010
0
    int i;
1011
0
    int len = 0;
1012
0
    int field_start = 0;
1013
0
    int skipping = 0;
1014
0
    ap_errorlog_format_item *items = (ap_errorlog_format_item *)fmt->elts;
1015
1016
0
    AP_DEBUG_ASSERT(fmt->nelts > 0);
1017
0
    for (i = 0; i < fmt->nelts; ++i) {
1018
0
        ap_errorlog_format_item *item = &items[i];
1019
0
        if (item->flags & AP_ERRORLOG_FLAG_FIELD_SEP) {
1020
0
            if (skipping) {
1021
0
                skipping = 0;
1022
0
            }
1023
0
            else {
1024
0
                field_start = len;
1025
0
            }
1026
0
        }
1027
1028
0
        if (item->flags & AP_ERRORLOG_FLAG_MESSAGE) {
1029
            /* the actual error message */
1030
0
            *errstr_start = len;
1031
0
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
1032
0
            if (apr_vsnprintf(scratch, MAX_STRING_LEN, err_fmt, args)) {
1033
0
                len += ap_escape_errorlog_item(buf + len, scratch,
1034
0
                                               buflen - len);
1035
1036
0
            }
1037
#else
1038
            len += apr_vsnprintf(buf + len, buflen - len, err_fmt, args);
1039
#endif
1040
0
            *errstr_end = len;
1041
0
        }
1042
0
        else if (skipping) {
1043
0
            continue;
1044
0
        }
1045
0
        else if (info->level != -1 && (int)item->min_loglevel > info->level) {
1046
0
            len = field_start;
1047
0
            skipping = 1;
1048
0
        }
1049
0
        else {
1050
0
            int item_len = (*item->func)(info, item->arg, buf + len,
1051
0
                                         buflen - len);
1052
0
            if (!item_len) {
1053
0
                if (item->flags & AP_ERRORLOG_FLAG_REQUIRED) {
1054
                    /* required item is empty. skip whole line */
1055
0
                    buf[0] = '\0';
1056
0
                    return 0;
1057
0
                }
1058
0
                else if (item->flags & AP_ERRORLOG_FLAG_NULL_AS_HYPHEN) {
1059
0
                    buf[len++] = '-';
1060
0
                }
1061
0
                else {
1062
0
                    len = field_start;
1063
0
                    skipping = 1;
1064
0
                }
1065
0
            }
1066
0
            else {
1067
0
                len += item_len;
1068
0
            }
1069
0
        }
1070
0
    }
1071
0
    return len;
1072
0
}
1073
1074
static void write_logline(char *errstr, apr_size_t len, apr_file_t *logf,
1075
                          int level)
1076
0
{
1077
1078
0
    apr_file_puts(errstr, logf);
1079
0
    apr_file_flush(logf);
1080
0
}
1081
1082
static void log_error_core(const char *file, int line, int module_index,
1083
                           int level,
1084
                           apr_status_t status, const server_rec *s,
1085
                           const conn_rec *c,
1086
                           const request_rec *r, apr_pool_t *pool,
1087
                           const char *fmt, va_list args)
1088
0
{
1089
0
    char errstr[MAX_STRING_LEN];
1090
0
    apr_file_t *logf = NULL;
1091
0
    int level_and_mask = level & APLOG_LEVELMASK;
1092
0
    const request_rec *rmain = NULL;
1093
0
    core_server_config *sconf = NULL;
1094
0
    ap_errorlog_info info;
1095
0
    ap_errorlog_provider *errorlog_provider = NULL;
1096
0
    void *errorlog_provider_handle = NULL;
1097
1098
    /* do we need to log once-per-req or once-per-conn info? */
1099
0
    int log_conn_info = 0, log_req_info = 0;
1100
0
    apr_array_header_t **lines = NULL;
1101
0
    int done = 0;
1102
0
    int line_number = 0;
1103
1104
0
    if (r) {
1105
0
        AP_DEBUG_ASSERT(r->connection != NULL);
1106
0
        c = r->connection;
1107
0
    }
1108
1109
0
    if (s == NULL) {
1110
        /*
1111
         * If we are doing stderr logging (startup), don't log messages that are
1112
         * above the default server log level unless it is a startup/shutdown
1113
         * notice
1114
         */
1115
0
#ifndef DEBUG
1116
0
        if ((level_and_mask != APLOG_NOTICE)
1117
0
            && (level_and_mask > ap_default_loglevel)) {
1118
0
            return;
1119
0
        }
1120
0
#endif
1121
1122
0
        logf = stderr_log;
1123
0
        if (!logf && ap_server_conf && ap_server_conf->errorlog_provider) {
1124
0
            errorlog_provider = ap_server_conf->errorlog_provider;
1125
0
            errorlog_provider_handle = ap_server_conf->errorlog_provider_handle;
1126
0
        }
1127
1128
        /* Use the main ErrorLogFormat if any */
1129
0
        if (ap_server_conf) {
1130
0
            sconf = ap_get_core_module_config(ap_server_conf->module_config);
1131
0
        }
1132
0
    }
1133
0
    else {
1134
0
        int configured_level = r ? ap_get_request_module_loglevel(r, module_index)        :
1135
0
                               c ? ap_get_conn_server_module_loglevel(c, s, module_index) :
1136
0
                                   ap_get_server_module_loglevel(s, module_index);
1137
        /*
1138
         * If we are doing normal logging, don't log messages that are
1139
         * above the module's log level unless it is a startup/shutdown notice
1140
         */
1141
0
        if ((level_and_mask != APLOG_NOTICE)
1142
0
            && (level_and_mask > configured_level)) {
1143
0
            return;
1144
0
        }
1145
1146
0
        if (s->error_log) {
1147
0
            logf = s->error_log;
1148
0
        }
1149
1150
0
        errorlog_provider = s->errorlog_provider;
1151
0
        errorlog_provider_handle = s->errorlog_provider_handle;
1152
1153
        /* the faked server_rec from mod_cgid does not have s->module_config */
1154
0
        if (s->module_config) {
1155
0
            sconf = ap_get_core_module_config(s->module_config);
1156
0
            if (c && !c->log_id) {
1157
0
                add_log_id(c, NULL);
1158
0
                if (sconf->error_log_conn && sconf->error_log_conn->nelts > 0)
1159
0
                    log_conn_info = 1;
1160
0
            }
1161
0
            if (r) {
1162
0
                if (r->main)
1163
0
                    rmain = r->main;
1164
0
                else
1165
0
                    rmain = r;
1166
1167
0
                if (!rmain->log_id) {
1168
                    /* XXX: do we need separate log ids for subrequests? */
1169
0
                    if (sconf->error_log_req && sconf->error_log_req->nelts > 0)
1170
0
                        log_req_info = 1;
1171
                    /*
1172
                     * XXX: potential optimization: only create log id if %L is
1173
                     * XXX: actually used
1174
                     */
1175
0
                    add_log_id(c, rmain);
1176
0
                }
1177
0
            }
1178
0
        }
1179
0
        else if (ap_server_conf) {
1180
            /* Use the main ErrorLogFormat if any */
1181
0
            sconf = ap_get_core_module_config(ap_server_conf->module_config);
1182
0
        }
1183
0
    }
1184
1185
0
    if (!logf && !(errorlog_provider && errorlog_provider_handle)) {
1186
        /* There is no file to send the log message to (or it is
1187
         * redirected to /dev/null and therefore any formatting done below
1188
         * would be lost anyway) and there is no initialized log provider
1189
         * available, so we just return here.
1190
         */
1191
0
        return;
1192
0
    }
1193
1194
0
    info.s             = s;
1195
0
    info.c             = c;
1196
0
    info.pool          = pool;
1197
0
    info.file          = NULL;
1198
0
    info.line          = 0;
1199
0
    info.status        = 0;
1200
0
    info.using_provider= (logf == NULL);
1201
0
    info.startup       = ((level & APLOG_STARTUP) == APLOG_STARTUP);
1202
0
    info.format        = fmt;
1203
1204
0
    while (!done) {
1205
0
        apr_array_header_t *log_format;
1206
0
        int len = 0, errstr_start = 0, errstr_end = 0;
1207
        /* XXX: potential optimization: format common prefixes only once */
1208
0
        if (log_conn_info) {
1209
            /* once-per-connection info */
1210
0
            if (line_number == 0) {
1211
0
                lines = (apr_array_header_t **)sconf->error_log_conn->elts;
1212
0
                info.r = NULL;
1213
0
                info.rmain = NULL;
1214
0
                info.level = -1;
1215
0
                info.module_index = APLOG_NO_MODULE;
1216
0
            }
1217
1218
0
            log_format = lines[line_number++];
1219
1220
0
            if (line_number == sconf->error_log_conn->nelts) {
1221
                /* this is the last line of once-per-connection info */
1222
0
                line_number = 0;
1223
0
                log_conn_info = 0;
1224
0
            }
1225
0
        }
1226
0
        else if (log_req_info) {
1227
            /* once-per-request info */
1228
0
            if (line_number == 0) {
1229
0
                lines = (apr_array_header_t **)sconf->error_log_req->elts;
1230
0
                info.r = rmain;
1231
0
                info.rmain = rmain;
1232
0
                info.level = -1;
1233
0
                info.module_index = APLOG_NO_MODULE;
1234
0
            }
1235
1236
0
            log_format = lines[line_number++];
1237
1238
0
            if (line_number == sconf->error_log_req->nelts) {
1239
                /* this is the last line of once-per-request info */
1240
0
                line_number = 0;
1241
0
                log_req_info = 0;
1242
0
            }
1243
0
        }
1244
0
        else {
1245
            /* the actual error message */
1246
0
            info.r            = r;
1247
0
            info.rmain        = rmain;
1248
0
            info.level        = level_and_mask;
1249
0
            info.module_index = module_index;
1250
0
            info.file         = file;
1251
0
            info.line         = line;
1252
0
            info.status       = status;
1253
0
            log_format = sconf ? sconf->error_log_format : NULL;
1254
0
            done = 1;
1255
0
        }
1256
1257
        /*
1258
         * prepare and log one line
1259
         */
1260
1261
0
        if (log_format && !info.startup) {
1262
0
            len += do_errorlog_format(log_format, &info, errstr + len,
1263
0
                                      MAX_STRING_LEN - len,
1264
0
                                      &errstr_start, &errstr_end, fmt, args);
1265
0
        }
1266
0
        else {
1267
0
            len += do_errorlog_default(&info, errstr + len, MAX_STRING_LEN - len,
1268
0
                                       &errstr_start, &errstr_end, fmt, args);
1269
0
        }
1270
1271
0
        if (!*errstr) {
1272
            /*
1273
             * Don't log empty lines. This can happen with once-per-conn/req
1274
             * info if an item with AP_ERRORLOG_FLAG_REQUIRED is NULL.
1275
             */
1276
0
            continue;
1277
0
        }
1278
1279
0
        if (logf || (errorlog_provider->flags &
1280
0
            AP_ERRORLOG_PROVIDER_ADD_EOL_STR)) {
1281
            /* Truncate for the terminator (as apr_snprintf does) */
1282
0
            if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR)) {
1283
0
                len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
1284
0
            }
1285
0
            strcpy(errstr + len, APR_EOL_STR);
1286
0
            len += strlen(APR_EOL_STR);
1287
0
        }
1288
1289
0
        if (logf) {
1290
0
            write_logline(errstr, len, logf, level_and_mask);
1291
0
        }
1292
0
        else {
1293
0
            errorlog_provider->writer(&info, errorlog_provider_handle,
1294
0
                                      errstr, len);
1295
0
        }
1296
1297
0
        if (done) {
1298
            /*
1299
             * We don't call the error_log hook for per-request/per-conn
1300
             * lines, and we only pass the actual log message, not the
1301
             * prefix and suffix.
1302
             */
1303
0
            errstr[errstr_end] = '\0';
1304
0
            ap_run_error_log(&info, errstr + errstr_start);
1305
0
        }
1306
1307
0
        *errstr = '\0';
1308
0
    }
1309
0
}
1310
1311
/* For internal calls to log_error_core with self-composed arg lists */
1312
static void log_error_va_glue(const char *file, int line, int module_index,
1313
                              int level, apr_status_t status,
1314
                              const server_rec *s, const conn_rec *c,
1315
                              const request_rec *r, apr_pool_t *pool,
1316
                              const char *fmt, ...)
1317
0
{
1318
0
    va_list args;
1319
1320
0
    va_start(args, fmt);
1321
0
    log_error_core(file, line, module_index, level, status, s, c, r, pool,
1322
0
                   fmt, args);
1323
0
    va_end(args);
1324
0
}
1325
1326
AP_DECLARE(void) ap_log_error_(const char *file, int line, int module_index,
1327
                               int level, apr_status_t status,
1328
                               const server_rec *s, const char *fmt, ...)
1329
0
{
1330
0
    va_list args;
1331
1332
0
    va_start(args, fmt);
1333
0
    log_error_core(file, line, module_index, level, status, s, NULL, NULL,
1334
0
                   NULL, fmt, args);
1335
0
    va_end(args);
1336
0
}
1337
1338
AP_DECLARE(void) ap_log_perror_(const char *file, int line, int module_index,
1339
                                int level, apr_status_t status, apr_pool_t *p,
1340
                                const char *fmt, ...)
1341
0
{
1342
0
    va_list args;
1343
1344
0
    va_start(args, fmt);
1345
0
    log_error_core(file, line, module_index, level, status, NULL, NULL, NULL,
1346
0
                   p, fmt, args);
1347
0
    va_end(args);
1348
0
}
1349
1350
AP_DECLARE(void) ap_log_rerror_(const char *file, int line, int module_index,
1351
                                int level, apr_status_t status,
1352
                                const request_rec *r, const char *fmt, ...)
1353
0
{
1354
0
    va_list args;
1355
1356
0
    va_start(args, fmt);
1357
0
    log_error_core(file, line, module_index, level, status, r->server, NULL, r,
1358
0
                   NULL, fmt, args);
1359
1360
    /*
1361
     * IF APLOG_TOCLIENT is set,
1362
     * AND the error level is 'warning' or more severe,
1363
     * AND there isn't already error text associated with this request,
1364
     * THEN make the message text available to ErrorDocument and
1365
     * other error processors.
1366
     */
1367
0
    va_end(args);
1368
0
    va_start(args,fmt);
1369
0
    if ((level & APLOG_TOCLIENT)
1370
0
        && ((level & APLOG_LEVELMASK) <= APLOG_WARNING)
1371
0
        && (apr_table_get(r->notes, "error-notes") == NULL)) {
1372
0
        apr_table_setn(r->notes, "error-notes",
1373
0
                       ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
1374
0
                                                             args)));
1375
0
    }
1376
0
    va_end(args);
1377
0
}
1378
1379
AP_DECLARE(void) ap_log_cserror_(const char *file, int line, int module_index,
1380
                                 int level, apr_status_t status,
1381
                                 const conn_rec *c, const server_rec *s,
1382
                                 const char *fmt, ...)
1383
0
{
1384
0
    va_list args;
1385
1386
0
    va_start(args, fmt);
1387
0
    log_error_core(file, line, module_index, level, status, s, c,
1388
0
                   NULL, NULL, fmt, args);
1389
0
    va_end(args);
1390
0
}
1391
1392
AP_DECLARE(void) ap_log_cerror_(const char *file, int line, int module_index,
1393
                                int level, apr_status_t status,
1394
                                const conn_rec *c, const char *fmt, ...)
1395
0
{
1396
0
    va_list args;
1397
1398
0
    va_start(args, fmt);
1399
0
    log_error_core(file, line, module_index, level, status, c->base_server, c,
1400
0
                   NULL, NULL, fmt, args);
1401
0
    va_end(args);
1402
0
}
1403
1404
0
#define BYTES_LOGGED_PER_LINE 16
1405
0
#define LOG_BYTES_BUFFER_SIZE (BYTES_LOGGED_PER_LINE * 3 + 2)
1406
1407
static void fmt_data(unsigned char *buf, const void *vdata, apr_size_t len, apr_size_t *off)
1408
0
{
1409
0
    const unsigned char *data = (const unsigned char *)vdata;
1410
0
    unsigned char *chars;
1411
0
    unsigned char *hex;
1412
0
    apr_size_t this_time = 0;
1413
1414
0
    memset(buf, ' ', LOG_BYTES_BUFFER_SIZE - 1);
1415
0
    buf[LOG_BYTES_BUFFER_SIZE - 1] = '\0';
1416
    
1417
0
    chars = buf; /* start character dump here */
1418
0
    hex   = buf + BYTES_LOGGED_PER_LINE + 1; /* start hex dump here */
1419
0
    while (*off < len && this_time < BYTES_LOGGED_PER_LINE) {
1420
0
        unsigned char c = data[*off];
1421
1422
0
        if (apr_isprint(c)
1423
0
            && c != '\\') {  /* backslash will be escaped later, which throws
1424
                              * off the formatting
1425
                              */
1426
0
            *chars = c;
1427
0
        }
1428
0
        else {
1429
0
            *chars = '.';
1430
0
        }
1431
1432
0
        if ((c >> 4) >= 10) {
1433
0
            *hex = 'a' + ((c >> 4) - 10);
1434
0
        }
1435
0
        else {
1436
0
            *hex = '0' + (c >> 4);
1437
0
        }
1438
1439
0
        if ((c & 0x0F) >= 10) {
1440
0
            *(hex + 1) = 'a' + ((c & 0x0F) - 10);
1441
0
        }
1442
0
        else {
1443
0
            *(hex + 1) = '0' + (c & 0x0F);
1444
0
        }
1445
1446
0
        chars += 1;
1447
0
        hex += 2;
1448
0
        *off += 1;
1449
0
        ++this_time;
1450
0
    }
1451
0
}
1452
1453
static void log_data_core(const char *file, int line, int module_index,
1454
                          int level, const server_rec *s,
1455
                          const conn_rec *c, const request_rec *r,
1456
                          const char *label, const void *data, apr_size_t len,
1457
                          unsigned int flags)
1458
0
{
1459
0
    unsigned char buf[LOG_BYTES_BUFFER_SIZE];
1460
0
    apr_size_t off;
1461
0
    char prefix[20];
1462
1463
0
    if (!(flags & AP_LOG_DATA_SHOW_OFFSET)) {
1464
0
        prefix[0] = '\0';
1465
0
    }
1466
1467
0
    if (len > 0xffff) { /* bug in caller? */
1468
0
        len = 0xffff;
1469
0
    }
1470
1471
0
    if (label) {
1472
0
        log_error_va_glue(file, line, module_index, level, APR_SUCCESS, s,
1473
0
                          c, r, NULL, "%s (%" APR_SIZE_T_FMT " bytes)",
1474
0
                          label, len);
1475
0
    }
1476
1477
0
    off = 0;
1478
0
    while (off < len) {
1479
0
        if (flags & AP_LOG_DATA_SHOW_OFFSET) {
1480
0
            apr_snprintf(prefix, sizeof prefix, "%04x: ", (unsigned int)off);
1481
0
        }
1482
0
        fmt_data(buf, data, len, &off);
1483
0
        log_error_va_glue(file, line, module_index, level, APR_SUCCESS, s,
1484
0
                          c, r, NULL, "%s%s", prefix, buf);
1485
0
    }
1486
0
}
1487
1488
AP_DECLARE(void) ap_log_data_(const char *file, int line, 
1489
                              int module_index, int level,
1490
                              const server_rec *s, const char *label,
1491
                              const void *data, apr_size_t len,
1492
                              unsigned int flags)
1493
0
{
1494
0
    log_data_core(file, line, module_index, level, s, NULL, NULL, label,
1495
0
                  data, len, flags);
1496
0
}
1497
1498
AP_DECLARE(void) ap_log_rdata_(const char *file, int line,
1499
                               int module_index, int level,
1500
                               const request_rec *r, const char *label,
1501
                               const void *data, apr_size_t len,
1502
                               unsigned int flags)
1503
0
{
1504
0
    log_data_core(file, line, module_index, level, r->server, NULL, r, label,
1505
0
                  data, len, flags);
1506
0
}
1507
1508
AP_DECLARE(void) ap_log_cdata_(const char *file, int line,
1509
                               int module_index, int level,
1510
                               const conn_rec *c, const char *label,
1511
                               const void *data, apr_size_t len,
1512
                               unsigned int flags)
1513
0
{
1514
0
    log_data_core(file, line, module_index, level, c->base_server, c, NULL,
1515
0
                  label, data, len, flags);
1516
0
}
1517
1518
AP_DECLARE(void) ap_log_csdata_(const char *file, int line, int module_index,
1519
                                int level, const conn_rec *c, const server_rec *s,
1520
                                const char *label, const void *data,
1521
                                apr_size_t len, unsigned int flags)
1522
0
{
1523
0
    log_data_core(file, line, module_index, level, s, c, NULL, label, data,
1524
0
                  len, flags);
1525
0
}
1526
1527
AP_DECLARE(void) ap_log_command_line(apr_pool_t *plog, server_rec *s)
1528
0
{
1529
0
    int i;
1530
0
    process_rec *process = s->process;
1531
0
    char *result;
1532
0
    int len_needed = 0;
1533
1534
    /* Piece together the command line from the pieces
1535
     * in process->argv, with spaces in between.
1536
     */
1537
0
    for (i = 0; i < process->argc; i++) {
1538
0
        len_needed += strlen(process->argv[i]) + 1;
1539
0
    }
1540
1541
0
    result = (char *) apr_palloc(plog, len_needed);
1542
0
    *result = '\0';
1543
1544
0
    for (i = 0; i < process->argc; i++) {
1545
0
        strcat(result, process->argv[i]);
1546
0
        if ((i+1)< process->argc) {
1547
0
            strcat(result, " ");
1548
0
        }
1549
0
    }
1550
0
    ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, APLOGNO(00094)
1551
0
                 "Command line: '%s'", result);
1552
0
}
1553
1554
/* grab bag function to log commonly logged and shared info */
1555
AP_DECLARE(void) ap_log_mpm_common(server_rec *s)
1556
0
{
1557
0
    ap_log_error(APLOG_MARK, APLOG_DEBUG , 0, s, APLOGNO(02639)
1558
0
                 "Using SO_REUSEPORT: %s (%d)",
1559
0
                 ap_have_so_reuseport ? "yes" : "no",
1560
0
                 ap_num_listen_buckets);
1561
0
}
1562
1563
AP_DECLARE(void) ap_remove_pid(apr_pool_t *p, const char *rel_fname)
1564
0
{
1565
0
    apr_status_t rv;
1566
0
    const char *fname = ap_runtime_dir_relative(p, rel_fname);
1567
1568
0
    if (fname != NULL) {
1569
0
        rv = apr_file_remove(fname, p);
1570
0
        if (rv != APR_SUCCESS) {
1571
0
            ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf, APLOGNO(00095)
1572
0
                         "failed to remove PID file %s", fname);
1573
0
        }
1574
0
        else {
1575
0
            ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf, APLOGNO(00096)
1576
0
                         "removed PID file %s (pid=%" APR_PID_T_FMT ")",
1577
0
                         fname, getpid());
1578
0
        }
1579
0
    }
1580
0
}
1581
1582
AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
1583
0
{
1584
0
    apr_file_t *pid_file = NULL;
1585
0
    apr_finfo_t finfo;
1586
0
    static pid_t saved_pid = -1;
1587
0
    pid_t mypid;
1588
0
    apr_status_t rv;
1589
0
    const char *fname;
1590
0
    char *temp_fname;
1591
0
    apr_fileperms_t perms;
1592
0
    char pidstr[64];
1593
1594
0
    if (!filename) {
1595
0
        return;
1596
0
    }
1597
1598
0
    fname = ap_runtime_dir_relative(p, filename);
1599
0
    if (!fname) {
1600
0
        ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
1601
0
                     ap_server_conf, APLOGNO(00097) "Invalid PID file path %s, ignoring.", filename);
1602
0
        return;
1603
0
    }
1604
1605
0
    mypid = getpid();
1606
0
    if (mypid != saved_pid
1607
0
        && apr_stat(&finfo, fname, APR_FINFO_MTIME, p) == APR_SUCCESS) {
1608
        /* AP_SIG_GRACEFUL and HUP call this on each restart.
1609
         * Only warn on first time through for this pid.
1610
         *
1611
         * XXX: Could just write first time through too, although
1612
         *      that may screw up scripts written to do something
1613
         *      based on the last modification time of the pid file.
1614
         */
1615
0
        ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, APLOGNO(00098)
1616
0
                      "pid file %s overwritten -- Unclean "
1617
0
                      "shutdown of previous Apache run?",
1618
0
                      fname);
1619
0
    }
1620
1621
0
    temp_fname = apr_pstrcat(p, fname, ".XXXXXX", NULL);
1622
0
    rv = apr_file_mktemp(&pid_file, temp_fname,
1623
0
                         APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE, p);
1624
0
    if (rv != APR_SUCCESS) {
1625
0
        ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, APLOGNO(00099)
1626
0
                     "could not create %s", temp_fname);
1627
0
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, APLOGNO(00100)
1628
0
                     "%s: could not log pid to file %s",
1629
0
                     ap_server_argv0, fname);
1630
0
        exit(1);
1631
0
    }
1632
1633
0
    apr_snprintf(pidstr, sizeof pidstr, "%" APR_PID_T_FMT APR_EOL_STR, mypid);
1634
1635
0
    perms = APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD;
1636
0
    if (((rv = apr_file_perms_set(temp_fname, perms)) != APR_SUCCESS && rv != APR_ENOTIMPL)
1637
0
        || (rv = apr_file_write_full(pid_file, pidstr, strlen(pidstr), NULL)) != APR_SUCCESS
1638
0
        || (rv = apr_file_close(pid_file)) != APR_SUCCESS
1639
0
        || (rv = apr_file_rename(temp_fname, fname, p)) != APR_SUCCESS) {
1640
0
        ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, APLOGNO(10231)
1641
0
                     "%s: Failed creating pid file %s",
1642
0
                     ap_server_argv0, temp_fname);
1643
0
        exit(1);
1644
0
    }
1645
1646
0
    saved_pid = mypid;
1647
0
}
1648
1649
AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
1650
                                     pid_t *mypid)
1651
0
{
1652
0
    const apr_size_t BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */
1653
0
    apr_file_t *pid_file = NULL;
1654
0
    apr_status_t rv;
1655
0
    const char *fname;
1656
0
    char *buf, *endptr;
1657
0
    apr_size_t bytes_read;
1658
1659
0
    if (!filename) {
1660
0
        return APR_EGENERAL;
1661
0
    }
1662
1663
0
    fname = ap_runtime_dir_relative(p, filename);
1664
0
    if (!fname) {
1665
0
        ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
1666
0
                     ap_server_conf, APLOGNO(00101) "Invalid PID file path %s, ignoring.", filename);
1667
0
        return APR_EGENERAL;
1668
0
    }
1669
1670
0
    rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p);
1671
0
    if (rv != APR_SUCCESS) {
1672
0
        return rv;
1673
0
    }
1674
1675
0
    buf = apr_palloc(p, BUFFER_SIZE);
1676
1677
0
    rv = apr_file_read_full(pid_file, buf, BUFFER_SIZE - 1, &bytes_read);
1678
0
    if (rv != APR_SUCCESS && rv != APR_EOF) {
1679
0
        return rv;
1680
0
    }
1681
1682
    /* If we fill the buffer, we're probably reading a corrupt pid file.
1683
     * To be nice, let's also ensure the first char is a digit. */
1684
0
    if (bytes_read == 0 || bytes_read == BUFFER_SIZE - 1 || !apr_isdigit(*buf)) {
1685
0
        return APR_EGENERAL;
1686
0
    }
1687
1688
0
    buf[bytes_read] = '\0';
1689
0
    *mypid = strtol(buf, &endptr, 10);
1690
1691
0
    apr_file_close(pid_file);
1692
0
    return APR_SUCCESS;
1693
0
}
1694
1695
AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile,
1696
                               int nLine)
1697
0
{
1698
0
    char time_str[APR_CTIME_LEN];
1699
1700
0
    apr_ctime(time_str, apr_time_now());
1701
0
    ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL, APLOGNO(00102)
1702
0
                 "[%s] file %s, line %d, assertion \"%s\" failed",
1703
0
                 time_str, szFile, nLine, szExp);
1704
#if defined(WIN32)
1705
    DebugBreak();
1706
#else
1707
    /* unix assert does an abort leading to a core dump */
1708
0
    abort();
1709
0
#endif
1710
0
}
1711
1712
/* piped log support */
1713
1714
#ifdef AP_HAVE_RELIABLE_PIPED_LOGS
1715
/* forward declaration */
1716
static void piped_log_maintenance(int reason, void *data, apr_wait_t status);
1717
1718
/* Spawn the piped logger process pl->program. */
1719
static apr_status_t piped_log_spawn(piped_log *pl)
1720
0
{
1721
0
    apr_procattr_t *procattr;
1722
0
    apr_proc_t *procnew = NULL;
1723
0
    apr_status_t status;
1724
1725
0
    if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
1726
0
        ((status = apr_procattr_dir_set(procattr, ap_server_root))
1727
0
         != APR_SUCCESS) ||
1728
0
        ((status = apr_procattr_cmdtype_set(procattr, pl->cmdtype))
1729
0
         != APR_SUCCESS) ||
1730
0
        ((status = apr_procattr_child_in_set(procattr,
1731
0
                                             pl->read_fd,
1732
0
                                             pl->write_fd))
1733
0
         != APR_SUCCESS) ||
1734
0
        ((status = apr_procattr_child_errfn_set(procattr, log_child_errfn))
1735
0
         != APR_SUCCESS) ||
1736
0
        ((status = apr_procattr_error_check_set(procattr, 1)) != APR_SUCCESS)) {
1737
        /* Something bad happened, give up and go away. */
1738
0
        ap_log_error(APLOG_MARK, APLOG_STARTUP, status, ap_server_conf, APLOGNO(00103)
1739
0
                     "piped_log_spawn: unable to setup child process '%s'",
1740
0
                     pl->program);
1741
0
    }
1742
0
    else {
1743
0
        char **args;
1744
1745
0
        apr_tokenize_to_argv(pl->program, &args, pl->p);
1746
0
        procnew = apr_pcalloc(pl->p, sizeof(apr_proc_t));
1747
0
        status = apr_proc_create(procnew, args[0], (const char * const *) args,
1748
0
                                 NULL, procattr, pl->p);
1749
1750
0
        if (status == APR_SUCCESS) {
1751
0
            pl->pid = procnew;
1752
            /* procnew->in was dup2'd from pl->write_fd;
1753
             * since the original fd is still valid, close the copy to
1754
             * avoid a leak. */
1755
0
            apr_file_close(procnew->in);
1756
0
            procnew->in = NULL;
1757
0
            apr_proc_other_child_register(procnew, piped_log_maintenance, pl,
1758
0
                                          pl->write_fd, pl->p);
1759
0
            close_handle_in_child(pl->p, pl->read_fd);
1760
0
        }
1761
0
        else {
1762
            /* Something bad happened, give up and go away. */
1763
0
            ap_log_error(APLOG_MARK, APLOG_STARTUP, status, ap_server_conf, APLOGNO(00104)
1764
0
                         "unable to start piped log program '%s'",
1765
0
                         pl->program);
1766
0
        }
1767
0
    }
1768
1769
0
    return status;
1770
0
}
1771
1772
1773
static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
1774
0
{
1775
0
    piped_log *pl = data;
1776
0
    apr_status_t rv;
1777
0
    int mpm_state;
1778
1779
0
    switch (reason) {
1780
0
    case APR_OC_REASON_DEATH:
1781
0
    case APR_OC_REASON_LOST:
1782
0
        pl->pid = NULL; /* in case we don't get it going again, this
1783
                         * tells other logic not to try to kill it
1784
                         */
1785
0
        apr_proc_other_child_unregister(pl);
1786
0
        rv = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
1787
0
        if (rv != APR_SUCCESS) {
1788
0
            ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, APLOGNO(00105)
1789
0
                         "can't query MPM state; not restarting "
1790
0
                         "piped log program '%s'",
1791
0
                         pl->program);
1792
0
        }
1793
0
        else if (mpm_state != AP_MPMQ_STOPPING) {
1794
0
            ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, APLOGNO(00106)
1795
0
                         "piped log program '%s' failed unexpectedly",
1796
0
                         pl->program);
1797
0
            if ((rv = piped_log_spawn(pl)) != APR_SUCCESS) {
1798
                /* what can we do?  This could be the error log we're having
1799
                 * problems opening up... */
1800
0
                ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL, APLOGNO(00107)
1801
0
                             "piped_log_maintenance: unable to respawn '%s'",
1802
0
                             pl->program);
1803
0
            }
1804
0
        }
1805
0
        break;
1806
1807
0
    case APR_OC_REASON_UNWRITABLE:
1808
        /* We should not kill off the pipe here, since it may only be full.
1809
         * If it really is locked, we should kill it off manually. */
1810
0
    break;
1811
1812
0
    case APR_OC_REASON_RESTART:
1813
0
        if (pl->pid != NULL) {
1814
0
            apr_proc_kill(pl->pid, SIGTERM);
1815
0
            pl->pid = NULL;
1816
0
        }
1817
0
        break;
1818
1819
0
    case APR_OC_REASON_UNREGISTER:
1820
0
        break;
1821
0
    }
1822
0
}
1823
1824
1825
static apr_status_t piped_log_cleanup_for_exec(void *data)
1826
0
{
1827
0
    piped_log *pl = data;
1828
1829
0
    apr_file_close(pl->read_fd);
1830
0
    apr_file_close(pl->write_fd);
1831
0
    return APR_SUCCESS;
1832
0
}
1833
1834
1835
static apr_status_t piped_log_cleanup(void *data)
1836
0
{
1837
0
    piped_log *pl = data;
1838
1839
0
    if (pl->pid != NULL) {
1840
0
        apr_proc_kill(pl->pid, SIGTERM);
1841
0
    }
1842
0
    return piped_log_cleanup_for_exec(data);
1843
0
}
1844
1845
1846
AP_DECLARE(piped_log *) ap_open_piped_log_ex(apr_pool_t *p,
1847
                                             const char *program,
1848
                                             apr_cmdtype_e cmdtype)
1849
0
{
1850
0
    piped_log *pl;
1851
1852
0
    pl = apr_palloc(p, sizeof (*pl));
1853
0
    pl->p = p;
1854
0
    pl->program = apr_pstrdup(p, program);
1855
0
    pl->pid = NULL;
1856
0
    pl->cmdtype = cmdtype;
1857
0
    if (apr_file_pipe_create_ex(&pl->read_fd,
1858
0
                                &pl->write_fd,
1859
0
                                APR_FULL_BLOCK, p) != APR_SUCCESS) {
1860
0
        return NULL;
1861
0
    }
1862
0
    apr_pool_cleanup_register(p, pl, piped_log_cleanup,
1863
0
                              piped_log_cleanup_for_exec);
1864
0
    if (piped_log_spawn(pl) != APR_SUCCESS) {
1865
0
        apr_pool_cleanup_kill(p, pl, piped_log_cleanup);
1866
0
        apr_file_close(pl->read_fd);
1867
0
        apr_file_close(pl->write_fd);
1868
0
        return NULL;
1869
0
    }
1870
0
    return pl;
1871
0
}
1872
1873
#else /* !AP_HAVE_RELIABLE_PIPED_LOGS */
1874
1875
static apr_status_t piped_log_cleanup(void *data)
1876
{
1877
    piped_log *pl = data;
1878
1879
    apr_file_close(pl->write_fd);
1880
    return APR_SUCCESS;
1881
}
1882
1883
AP_DECLARE(piped_log *) ap_open_piped_log_ex(apr_pool_t *p,
1884
                                             const char *program,
1885
                                             apr_cmdtype_e cmdtype)
1886
{
1887
    piped_log *pl;
1888
    apr_file_t *dummy = NULL;
1889
    int rc;
1890
1891
    rc = log_child(p, program, &dummy, cmdtype, 0);
1892
    if (rc != APR_SUCCESS) {
1893
        ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, ap_server_conf, APLOGNO(00108)
1894
                     "Couldn't start piped log process '%s'.",
1895
                     (program == NULL) ? "NULL" : program);
1896
        return NULL;
1897
    }
1898
1899
    pl = apr_palloc(p, sizeof (*pl));
1900
    pl->p = p;
1901
    pl->read_fd = NULL;
1902
    pl->write_fd = dummy;
1903
    apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
1904
1905
    return pl;
1906
}
1907
1908
#endif
1909
1910
AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p,
1911
                                          const char *program)
1912
0
{
1913
0
    apr_cmdtype_e cmdtype = APR_PROGRAM_ENV;
1914
1915
    /* In 2.4 favor PROGRAM_ENV, accept "||prog" syntax for compatibility
1916
     * and "|$cmd" to override the default.
1917
     * Any 2.2 backport would continue to favor SHELLCMD_ENV so there
1918
     * accept "||prog" to override, and "|$cmd" to ease conversion.
1919
     */
1920
0
    if (*program == '|')
1921
0
        ++program;
1922
0
    if (*program == '$') {
1923
0
        cmdtype = APR_SHELLCMD_ENV;
1924
0
        ++program;
1925
0
    }
1926
1927
0
    return ap_open_piped_log_ex(p, program, cmdtype);
1928
0
}
1929
1930
AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
1931
0
{
1932
0
    apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
1933
0
}
1934
1935
AP_DECLARE(const char *) ap_parse_log_level(const char *str, int *val)
1936
0
{
1937
0
    const char *err = "Log level keyword must be one of emerg/alert/crit/error/"
1938
0
                      "warn/notice/info/debug/trace1/.../trace8";
1939
0
    int i = 0;
1940
1941
0
    if (str == NULL)
1942
0
        return err;
1943
1944
0
    while (priorities[i].t_name != NULL) {
1945
0
        if (!strcasecmp(str, priorities[i].t_name)) {
1946
0
            *val = priorities[i].t_val;
1947
0
            return NULL;
1948
0
        }
1949
0
        i++;
1950
0
    }
1951
0
    return err;
1952
0
}
1953
1954
AP_IMPLEMENT_HOOK_VOID(error_log,
1955
                       (const ap_errorlog_info *info, const char *errstr),
1956
                       (info, errstr))
1957
1958
AP_IMPLEMENT_HOOK_RUN_FIRST(int, generate_log_id,
1959
                            (const conn_rec *c, const request_rec *r,
1960
                             const char **id),
1961
                            (c, r, id), DECLINED)