Coverage Report

Created: 2025-07-11 06:40

/src/httpd/server/log.c
Line
Count
Source (jump to first uncovered line)
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 (!ap_strchr_c(arg, '%')) { /* special "%{cuz}t" formats */
589
0
            while (*arg) {
590
0
                switch (*arg++) {
591
0
                case 'u':
592
0
                    option |= AP_CTIME_OPTION_USEC;
593
0
                    break;
594
0
                case 'c':
595
0
                    option |= AP_CTIME_OPTION_COMPACT;
596
0
                    break;
597
0
                case 'z':
598
0
                    option |= AP_CTIME_OPTION_GMTOFF;
599
0
                    break;
600
0
                }
601
0
            }
602
0
        }
603
0
        else { /* "%{strftime %-format}t" */
604
0
            apr_size_t len = 0;
605
0
            apr_time_exp_t expt;
606
0
            ap_explode_recent_localtime(&expt, apr_time_now());
607
0
            apr_strftime(buf, &len, buflen, arg, &expt);
608
0
            return (int)len;
609
0
        }
610
0
    }
611
612
0
    ap_recent_ctime_ex(buf, apr_time_now(), option, &time_len);
613
614
    /* ap_recent_ctime_ex includes the trailing \0 in time_len */
615
0
    return time_len - 1;
616
0
}
617
618
static int log_loglevel(const ap_errorlog_info *info, const char *arg,
619
                        char *buf, int buflen)
620
0
{
621
0
    if (info->level < 0)
622
0
        return 0;
623
0
    else
624
0
        return cpystrn(buf, priorities[info->level].t_name, buflen);
625
0
}
626
627
static int log_log_id(const ap_errorlog_info *info, const char *arg,
628
                      char *buf, int buflen)
629
0
{
630
    /*
631
     * C: log conn log_id if available,
632
     * c: log conn log id if available and not a once-per-request log line
633
     * else: log request log id if available
634
     */
635
0
    if (arg && (*arg == 'c' || *arg == 'C')) {
636
0
        if (info->c && (*arg != 'C' || !info->r)) {
637
0
            return cpystrn(buf, info->c->log_id, buflen);
638
0
        }
639
0
    }
640
0
    else if (info->rmain) {
641
0
        return cpystrn(buf, info->rmain->log_id, buflen);
642
0
    }
643
0
    return 0;
644
0
}
645
646
static int log_keepalives(const ap_errorlog_info *info, const char *arg,
647
                          char *buf, int buflen)
648
0
{
649
0
    if (!info->c)
650
0
        return 0;
651
652
0
    return apr_snprintf(buf, buflen, "%d", info->c->keepalives);
653
0
}
654
655
static int log_module_name(const ap_errorlog_info *info, const char *arg,
656
                           char *buf, int buflen)
657
0
{
658
0
    return cpystrn(buf, ap_find_module_short_name(info->module_index), buflen);
659
0
}
660
661
static int log_file_line(const ap_errorlog_info *info, const char *arg,
662
                         char *buf, int buflen)
663
0
{
664
0
    if (info->file == NULL) {
665
0
        return 0;
666
0
    }
667
0
    else {
668
0
        const char *file = info->file;
669
#if defined(_OSD_POSIX) || defined(WIN32) || defined(__MVS__)
670
        char tmp[256];
671
        char *e = strrchr(file, '/');
672
#ifdef WIN32
673
        if (!e) {
674
            e = strrchr(file, '\\');
675
        }
676
#endif
677
678
        /* In OSD/POSIX, the compiler returns for __FILE__
679
         * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
680
         * (it even returns an absolute path for sources in
681
         * the current directory). Here we try to strip this
682
         * down to the basename.
683
         */
684
        if (e != NULL && e[1] != '\0') {
685
            apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
686
            e = &tmp[strlen(tmp)-1];
687
            if (*e == ')') {
688
                *e = '\0';
689
            }
690
            file = tmp;
691
        }
692
#else /* _OSD_POSIX || WIN32 */
693
0
        const char *p;
694
        /* On Unix, __FILE__ may be an absolute path in a
695
         * VPATH build. */
696
0
        if (file[0] == '/' && (p = ap_strrchr_c(file, '/')) != NULL) {
697
0
            file = p + 1;
698
0
        }
699
0
#endif /*_OSD_POSIX || WIN32 */
700
0
        return apr_snprintf(buf, buflen, "%s(%d)", file, info->line);
701
0
    }
702
0
}
703
704
static int log_apr_status(const ap_errorlog_info *info, const char *arg,
705
                          char *buf, int buflen)
706
0
{
707
0
    apr_status_t status = info->status;
708
0
    int len;
709
0
    if (!status)
710
0
        return 0;
711
712
0
    if (status < APR_OS_START_EAIERR) {
713
0
        len = apr_snprintf(buf, buflen, "(%d)", status);
714
0
    }
715
0
    else if (status < APR_OS_START_SYSERR) {
716
0
        len = apr_snprintf(buf, buflen, "(EAI %d)",
717
0
                           status - APR_OS_START_EAIERR);
718
0
    }
719
0
    else if (status < 100000 + APR_OS_START_SYSERR) {
720
0
        len = apr_snprintf(buf, buflen, "(OS %d)",
721
0
                           status - APR_OS_START_SYSERR);
722
0
    }
723
0
    else {
724
0
        len = apr_snprintf(buf, buflen, "(os 0x%08x)",
725
0
                           status - APR_OS_START_SYSERR);
726
0
    }
727
#if (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7)
728
    if (status < APR_UTIL_START_STATUS) {
729
        apr_strerror(status, buf + len, buflen - len);
730
    }
731
    else if (status < (APR_UTIL_START_STATUS + APR_UTIL_ERRSPACE_SIZE)) {
732
        apu_strerror(status, buf + len, buflen - len);
733
    }
734
    else {
735
        apr_strerror(status, buf + len, buflen - len);
736
    }
737
#else
738
0
    apr_strerror(status, buf + len, buflen - len);
739
0
#endif
740
0
    len += strlen(buf + len);
741
0
    return len;
742
0
}
743
744
static int log_server_name(const ap_errorlog_info *info, const char *arg,
745
                           char *buf, int buflen)
746
0
{
747
0
    if (info->r)
748
0
        return cpystrn(buf, ap_get_server_name((request_rec *)info->r), buflen);
749
750
0
    return 0;
751
0
}
752
753
static int log_virtual_host(const ap_errorlog_info *info, const char *arg,
754
                            char *buf, int buflen)
755
0
{
756
0
    if (info->s)
757
0
        return cpystrn(buf, info->s->server_hostname, buflen);
758
759
0
    return 0;
760
0
}
761
762
763
static int log_table_entry(const apr_table_t *table, const char *name,
764
                           char *buf, int buflen)
765
0
{
766
0
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
767
0
    const char *value;
768
0
    char scratch[MAX_STRING_LEN];
769
770
0
    if ((value = apr_table_get(table, name)) != NULL) {
771
0
        ap_escape_errorlog_item(scratch, value, MAX_STRING_LEN);
772
0
        return cpystrn(buf, scratch, buflen);
773
0
    }
774
775
0
    return 0;
776
#else
777
    return cpystrn(buf, apr_table_get(table, name), buflen);
778
#endif
779
0
}
780
781
static int log_header(const ap_errorlog_info *info, const char *arg,
782
                      char *buf, int buflen)
783
0
{
784
0
    if (info->r)
785
0
        return log_table_entry(info->r->headers_in, arg, buf, buflen);
786
787
0
    return 0;
788
0
}
789
790
static int log_note(const ap_errorlog_info *info, const char *arg,
791
                      char *buf, int buflen)
792
0
{
793
    /* XXX: maybe escaping the entry is not necessary for notes? */
794
0
    if (info->r)
795
0
        return log_table_entry(info->r->notes, arg, buf, buflen);
796
797
0
    return 0;
798
0
}
799
800
static int log_env_var(const ap_errorlog_info *info, const char *arg,
801
                      char *buf, int buflen)
802
0
{
803
0
    if (info->r)
804
0
        return log_table_entry(info->r->subprocess_env, arg, buf, buflen);
805
806
0
    return 0;
807
0
}
808
809
static int core_generate_log_id(const conn_rec *c, const request_rec *r,
810
                                 const char **idstring)
811
0
{
812
0
    apr_uint64_t id, tmp;
813
0
    pid_t pid;
814
0
    int len;
815
0
    char *encoded;
816
817
0
    if (r && r->request_time) {
818
0
        id = (apr_uint64_t)r->request_time;
819
0
    }
820
0
    else {
821
0
        id = (apr_uint64_t)apr_time_now();
822
0
    }
823
824
0
    pid = getpid();
825
0
    if (sizeof(pid_t) > 2) {
826
0
        tmp = pid;
827
0
        tmp = tmp << 40;
828
0
        id ^= tmp;
829
0
        pid = pid >> 24;
830
0
        tmp = pid;
831
0
        tmp = tmp << 56;
832
0
        id ^= tmp;
833
0
    }
834
0
    else {
835
0
        tmp = pid;
836
0
        tmp = tmp << 40;
837
0
        id ^= tmp;
838
0
    }
839
0
#if APR_HAS_THREADS
840
0
    {
841
0
        apr_uintptr_t tmp2 = (apr_uintptr_t)c->current_thread;
842
0
        tmp = tmp2;
843
0
        tmp = tmp << 32;
844
0
        id ^= tmp;
845
0
    }
846
0
#endif
847
848
0
    len = apr_base64_encode_len(sizeof(id)); /* includes trailing \0 */
849
0
    encoded = apr_palloc(r ? r->pool : c->pool, len);
850
0
    apr_base64_encode(encoded, (char *)&id, sizeof(id));
851
852
    /* Skip the last char, it is always '=' */
853
0
    encoded[len - 2] = '\0';
854
855
0
    *idstring = encoded;
856
857
0
    return OK;
858
0
}
859
860
static void add_log_id(const conn_rec *c, const request_rec *r)
861
0
{
862
0
    const char **id;
863
    /* need to cast const away */
864
0
    if (r) {
865
0
        id = &((request_rec *)r)->log_id;
866
0
    }
867
0
    else {
868
0
        id = &((conn_rec *)c)->log_id;
869
0
    }
870
871
0
    ap_run_generate_log_id(c, r, id);
872
0
}
873
874
AP_DECLARE(void) ap_register_log_hooks(apr_pool_t *p)
875
0
{
876
0
    ap_hook_generate_log_id(core_generate_log_id, NULL, NULL,
877
0
                            APR_HOOK_REALLY_LAST);
878
879
0
    ap_register_errorlog_handler(p, "a", log_remote_address, 0);
880
0
    ap_register_errorlog_handler(p, "A", log_local_address, 0);
881
0
    ap_register_errorlog_handler(p, "e", log_env_var, 0);
882
0
    ap_register_errorlog_handler(p, "E", log_apr_status, 0);
883
0
    ap_register_errorlog_handler(p, "F", log_file_line, 0);
884
0
    ap_register_errorlog_handler(p, "i", log_header, 0);
885
0
    ap_register_errorlog_handler(p, "k", log_keepalives, 0);
886
0
    ap_register_errorlog_handler(p, "l", log_loglevel, 0);
887
0
    ap_register_errorlog_handler(p, "L", log_log_id, 0);
888
0
    ap_register_errorlog_handler(p, "m", log_module_name, 0);
889
0
    ap_register_errorlog_handler(p, "n", log_note, 0);
890
0
    ap_register_errorlog_handler(p, "P", log_pid, 0);
891
0
    ap_register_errorlog_handler(p, "t", log_ctime, 0);
892
0
    ap_register_errorlog_handler(p, "T", log_tid, 0);
893
0
    ap_register_errorlog_handler(p, "v", log_virtual_host, 0);
894
0
    ap_register_errorlog_handler(p, "V", log_server_name, 0);
895
0
}
896
897
/*
898
 * This is used if no error log format is defined and during startup.
899
 * It automatically omits the timestamp if logging using provider.
900
 */
901
static int do_errorlog_default(const ap_errorlog_info *info, char *buf,
902
                               int buflen, int *errstr_start, int *errstr_end,
903
                               const char *errstr_fmt, va_list args)
904
0
{
905
0
    int len = 0;
906
0
    int field_start = 0;
907
0
    int item_len;
908
0
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
909
0
    char scratch[MAX_STRING_LEN];
910
0
#endif
911
912
0
    if (!info->using_provider && !info->startup) {
913
0
        buf[len++] = '[';
914
0
        len += log_ctime(info, "u", buf + len, buflen - len);
915
0
        buf[len++] = ']';
916
0
        buf[len++] = ' ';
917
0
    }
918
919
0
    if (!info->startup) {
920
0
        buf[len++] = '[';
921
0
        len += log_module_name(info, NULL, buf + len, buflen - len);
922
0
        buf[len++] = ':';
923
0
        len += log_loglevel(info, NULL, buf + len, buflen - len);
924
0
        len += cpystrn(buf + len, "] [pid ", buflen - len);
925
926
0
        len += log_pid(info, NULL, buf + len, buflen - len);
927
0
#if APR_HAS_THREADS
928
0
        field_start = len;
929
0
        len += cpystrn(buf + len, ":tid ", buflen - len);
930
0
        item_len = log_tid(info, DEFAULT_LOG_TID, buf + len, buflen - len);
931
0
        if (!item_len)
932
0
            len = field_start;
933
0
        else
934
0
            len += item_len;
935
0
#endif
936
0
        buf[len++] = ']';
937
0
        buf[len++] = ' ';
938
0
    }
939
940
0
    if (info->level >= APLOG_DEBUG) {
941
0
        item_len = log_file_line(info, NULL, buf + len, buflen - len);
942
0
        if (item_len) {
943
0
            len += item_len;
944
0
            len += cpystrn(buf + len, ": ", buflen - len);
945
0
        }
946
0
    }
947
948
0
    if (info->status) {
949
0
        item_len = log_apr_status(info, NULL, buf + len, buflen - len);
950
0
        if (item_len) {
951
0
            len += item_len;
952
0
            len += cpystrn(buf + len, ": ", buflen - len);
953
0
        }
954
0
    }
955
956
    /*
957
     * useragent_ip/client_ip can be client or backend server. If we have
958
     * a scoreboard handle, it is likely a client.
959
     */
960
0
    if (info->r) {
961
0
        len += apr_snprintf(buf + len, buflen - len, "[%s %s:%d] ",
962
0
                            info->r->connection->outgoing ? "remote" : "client",
963
0
                            info->r->useragent_ip,
964
0
                            info->r->useragent_addr ? info->r->useragent_addr->port : 0);
965
0
    }
966
0
    else if (info->c) {
967
0
        len += apr_snprintf(buf + len, buflen - len, "[%s %s:%d] ",
968
0
                            info->c->outgoing ? "remote" : "client",
969
0
                            info->c->client_ip,
970
0
                            info->c->client_addr ? info->c->client_addr->port : 0);
971
0
    }
972
973
    /* the actual error message */
974
0
    *errstr_start = len;
975
0
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
976
0
    if (apr_vsnprintf(scratch, MAX_STRING_LEN, errstr_fmt, args)) {
977
0
        len += ap_escape_errorlog_item(buf + len, scratch,
978
0
                                       buflen - len);
979
980
0
    }
981
#else
982
    len += apr_vsnprintf(buf + len, buflen - len, errstr_fmt, args);
983
#endif
984
0
    *errstr_end = len;
985
986
0
    field_start = len;
987
0
    len += cpystrn(buf + len, ", referer: ", buflen - len);
988
0
    item_len = log_header(info, "Referer", buf + len, buflen - len);
989
0
    if (item_len)
990
0
        len += item_len;
991
0
    else
992
0
        len = field_start;
993
994
0
    return len;
995
0
}
996
997
static int do_errorlog_format(apr_array_header_t *fmt, ap_errorlog_info *info,
998
                              char *buf, int buflen, int *errstr_start,
999
                              int *errstr_end, const char *err_fmt, va_list args)
1000
0
{
1001
0
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
1002
0
    char scratch[MAX_STRING_LEN];
1003
0
#endif
1004
0
    int i;
1005
0
    int len = 0;
1006
0
    int field_start = 0;
1007
0
    int skipping = 0;
1008
0
    ap_errorlog_format_item *items = (ap_errorlog_format_item *)fmt->elts;
1009
1010
0
    AP_DEBUG_ASSERT(fmt->nelts > 0);
1011
0
    for (i = 0; i < fmt->nelts; ++i) {
1012
0
        ap_errorlog_format_item *item = &items[i];
1013
0
        if (item->flags & AP_ERRORLOG_FLAG_FIELD_SEP) {
1014
0
            if (skipping) {
1015
0
                skipping = 0;
1016
0
            }
1017
0
            else {
1018
0
                field_start = len;
1019
0
            }
1020
0
        }
1021
1022
0
        if (item->flags & AP_ERRORLOG_FLAG_MESSAGE) {
1023
            /* the actual error message */
1024
0
            *errstr_start = len;
1025
0
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
1026
0
            if (apr_vsnprintf(scratch, MAX_STRING_LEN, err_fmt, args)) {
1027
0
                len += ap_escape_errorlog_item(buf + len, scratch,
1028
0
                                               buflen - len);
1029
1030
0
            }
1031
#else
1032
            len += apr_vsnprintf(buf + len, buflen - len, err_fmt, args);
1033
#endif
1034
0
            *errstr_end = len;
1035
0
        }
1036
0
        else if (skipping) {
1037
0
            continue;
1038
0
        }
1039
0
        else if (info->level != -1 && (int)item->min_loglevel > info->level) {
1040
0
            len = field_start;
1041
0
            skipping = 1;
1042
0
        }
1043
0
        else {
1044
0
            int item_len = (*item->func)(info, item->arg, buf + len,
1045
0
                                         buflen - len);
1046
0
            if (!item_len) {
1047
0
                if (item->flags & AP_ERRORLOG_FLAG_REQUIRED) {
1048
                    /* required item is empty. skip whole line */
1049
0
                    buf[0] = '\0';
1050
0
                    return 0;
1051
0
                }
1052
0
                else if (item->flags & AP_ERRORLOG_FLAG_NULL_AS_HYPHEN) {
1053
0
                    buf[len++] = '-';
1054
0
                }
1055
0
                else {
1056
0
                    len = field_start;
1057
0
                    skipping = 1;
1058
0
                }
1059
0
            }
1060
0
            else {
1061
0
                len += item_len;
1062
0
            }
1063
0
        }
1064
0
    }
1065
0
    return len;
1066
0
}
1067
1068
static void write_logline(char *errstr, apr_size_t len, apr_file_t *logf,
1069
                          int level)
1070
0
{
1071
1072
0
    apr_file_puts(errstr, logf);
1073
0
    apr_file_flush(logf);
1074
0
}
1075
1076
static void log_error_core(const char *file, int line, int module_index,
1077
                           int level,
1078
                           apr_status_t status, const server_rec *s,
1079
                           const conn_rec *c,
1080
                           const request_rec *r, apr_pool_t *pool,
1081
                           const char *fmt, va_list args)
1082
0
{
1083
0
    char errstr[MAX_STRING_LEN];
1084
0
    apr_file_t *logf = NULL;
1085
0
    int level_and_mask = level & APLOG_LEVELMASK;
1086
0
    const request_rec *rmain = NULL;
1087
0
    core_server_config *sconf = NULL;
1088
0
    ap_errorlog_info info;
1089
0
    ap_errorlog_provider *errorlog_provider = NULL;
1090
0
    void *errorlog_provider_handle = NULL;
1091
1092
    /* do we need to log once-per-req or once-per-conn info? */
1093
0
    int log_conn_info = 0, log_req_info = 0;
1094
0
    apr_array_header_t **lines = NULL;
1095
0
    int done = 0;
1096
0
    int line_number = 0;
1097
1098
0
    if (r) {
1099
0
        AP_DEBUG_ASSERT(r->connection != NULL);
1100
0
        c = r->connection;
1101
0
    }
1102
1103
0
    if (s == NULL) {
1104
        /*
1105
         * If we are doing stderr logging (startup), don't log messages that are
1106
         * above the default server log level unless it is a startup/shutdown
1107
         * notice
1108
         */
1109
0
#ifndef DEBUG
1110
0
        if ((level_and_mask != APLOG_NOTICE)
1111
0
            && (level_and_mask > ap_default_loglevel)) {
1112
0
            return;
1113
0
        }
1114
0
#endif
1115
1116
0
        logf = stderr_log;
1117
0
        if (!logf && ap_server_conf && ap_server_conf->errorlog_provider) {
1118
0
            errorlog_provider = ap_server_conf->errorlog_provider;
1119
0
            errorlog_provider_handle = ap_server_conf->errorlog_provider_handle;
1120
0
        }
1121
1122
        /* Use the main ErrorLogFormat if any */
1123
0
        if (ap_server_conf) {
1124
0
            sconf = ap_get_core_module_config(ap_server_conf->module_config);
1125
0
        }
1126
0
    }
1127
0
    else {
1128
0
        int configured_level = r ? ap_get_request_module_loglevel(r, module_index)        :
1129
0
                               c ? ap_get_conn_server_module_loglevel(c, s, module_index) :
1130
0
                                   ap_get_server_module_loglevel(s, module_index);
1131
        /*
1132
         * If we are doing normal logging, don't log messages that are
1133
         * above the module's log level unless it is a startup/shutdown notice
1134
         */
1135
0
        if ((level_and_mask != APLOG_NOTICE)
1136
0
            && (level_and_mask > configured_level)) {
1137
0
            return;
1138
0
        }
1139
1140
0
        if (s->error_log) {
1141
0
            logf = s->error_log;
1142
0
        }
1143
1144
0
        errorlog_provider = s->errorlog_provider;
1145
0
        errorlog_provider_handle = s->errorlog_provider_handle;
1146
1147
        /* the faked server_rec from mod_cgid does not have s->module_config */
1148
0
        if (s->module_config) {
1149
0
            sconf = ap_get_core_module_config(s->module_config);
1150
0
            if (c && !c->log_id) {
1151
0
                add_log_id(c, NULL);
1152
0
                if (sconf->error_log_conn && sconf->error_log_conn->nelts > 0)
1153
0
                    log_conn_info = 1;
1154
0
            }
1155
0
            if (r) {
1156
0
                if (r->main)
1157
0
                    rmain = r->main;
1158
0
                else
1159
0
                    rmain = r;
1160
1161
0
                if (!rmain->log_id) {
1162
                    /* XXX: do we need separate log ids for subrequests? */
1163
0
                    if (sconf->error_log_req && sconf->error_log_req->nelts > 0)
1164
0
                        log_req_info = 1;
1165
                    /*
1166
                     * XXX: potential optimization: only create log id if %L is
1167
                     * XXX: actually used
1168
                     */
1169
0
                    add_log_id(c, rmain);
1170
0
                }
1171
0
            }
1172
0
        }
1173
0
        else if (ap_server_conf) {
1174
            /* Use the main ErrorLogFormat if any */
1175
0
            sconf = ap_get_core_module_config(ap_server_conf->module_config);
1176
0
        }
1177
0
    }
1178
1179
0
    if (!logf && !(errorlog_provider && errorlog_provider_handle)) {
1180
        /* There is no file to send the log message to (or it is
1181
         * redirected to /dev/null and therefore any formatting done below
1182
         * would be lost anyway) and there is no initialized log provider
1183
         * available, so we just return here.
1184
         */
1185
0
        return;
1186
0
    }
1187
1188
0
    info.s             = s;
1189
0
    info.c             = c;
1190
0
    info.pool          = pool;
1191
0
    info.file          = NULL;
1192
0
    info.line          = 0;
1193
0
    info.status        = 0;
1194
0
    info.using_provider= (logf == NULL);
1195
0
    info.startup       = ((level & APLOG_STARTUP) == APLOG_STARTUP);
1196
0
    info.format        = fmt;
1197
1198
0
    while (!done) {
1199
0
        apr_array_header_t *log_format;
1200
0
        int len = 0, errstr_start = 0, errstr_end = 0;
1201
        /* XXX: potential optimization: format common prefixes only once */
1202
0
        if (log_conn_info) {
1203
            /* once-per-connection info */
1204
0
            if (line_number == 0) {
1205
0
                lines = (apr_array_header_t **)sconf->error_log_conn->elts;
1206
0
                info.r = NULL;
1207
0
                info.rmain = NULL;
1208
0
                info.level = -1;
1209
0
                info.module_index = APLOG_NO_MODULE;
1210
0
            }
1211
1212
0
            log_format = lines[line_number++];
1213
1214
0
            if (line_number == sconf->error_log_conn->nelts) {
1215
                /* this is the last line of once-per-connection info */
1216
0
                line_number = 0;
1217
0
                log_conn_info = 0;
1218
0
            }
1219
0
        }
1220
0
        else if (log_req_info) {
1221
            /* once-per-request info */
1222
0
            if (line_number == 0) {
1223
0
                lines = (apr_array_header_t **)sconf->error_log_req->elts;
1224
0
                info.r = rmain;
1225
0
                info.rmain = rmain;
1226
0
                info.level = -1;
1227
0
                info.module_index = APLOG_NO_MODULE;
1228
0
            }
1229
1230
0
            log_format = lines[line_number++];
1231
1232
0
            if (line_number == sconf->error_log_req->nelts) {
1233
                /* this is the last line of once-per-request info */
1234
0
                line_number = 0;
1235
0
                log_req_info = 0;
1236
0
            }
1237
0
        }
1238
0
        else {
1239
            /* the actual error message */
1240
0
            info.r            = r;
1241
0
            info.rmain        = rmain;
1242
0
            info.level        = level_and_mask;
1243
0
            info.module_index = module_index;
1244
0
            info.file         = file;
1245
0
            info.line         = line;
1246
0
            info.status       = status;
1247
0
            log_format = sconf ? sconf->error_log_format : NULL;
1248
0
            done = 1;
1249
0
        }
1250
1251
        /*
1252
         * prepare and log one line
1253
         */
1254
1255
0
        if (log_format && !info.startup) {
1256
0
            len += do_errorlog_format(log_format, &info, errstr + len,
1257
0
                                      MAX_STRING_LEN - len,
1258
0
                                      &errstr_start, &errstr_end, fmt, args);
1259
0
        }
1260
0
        else {
1261
0
            len += do_errorlog_default(&info, errstr + len, MAX_STRING_LEN - len,
1262
0
                                       &errstr_start, &errstr_end, fmt, args);
1263
0
        }
1264
1265
0
        if (!*errstr) {
1266
            /*
1267
             * Don't log empty lines. This can happen with once-per-conn/req
1268
             * info if an item with AP_ERRORLOG_FLAG_REQUIRED is NULL.
1269
             */
1270
0
            continue;
1271
0
        }
1272
1273
0
        if (logf || (errorlog_provider->flags &
1274
0
            AP_ERRORLOG_PROVIDER_ADD_EOL_STR)) {
1275
            /* Truncate for the terminator (as apr_snprintf does) */
1276
0
            if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR)) {
1277
0
                len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
1278
0
            }
1279
0
            strcpy(errstr + len, APR_EOL_STR);
1280
0
            len += strlen(APR_EOL_STR);
1281
0
        }
1282
1283
0
        if (logf) {
1284
0
            write_logline(errstr, len, logf, level_and_mask);
1285
0
        }
1286
0
        else {
1287
0
            errorlog_provider->writer(&info, errorlog_provider_handle,
1288
0
                                      errstr, len);
1289
0
        }
1290
1291
0
        if (done) {
1292
            /*
1293
             * We don't call the error_log hook for per-request/per-conn
1294
             * lines, and we only pass the actual log message, not the
1295
             * prefix and suffix.
1296
             */
1297
0
            errstr[errstr_end] = '\0';
1298
0
            ap_run_error_log(&info, errstr + errstr_start);
1299
0
        }
1300
1301
0
        *errstr = '\0';
1302
0
    }
1303
0
}
1304
1305
/* For internal calls to log_error_core with self-composed arg lists */
1306
static void log_error_va_glue(const char *file, int line, int module_index,
1307
                              int level, apr_status_t status,
1308
                              const server_rec *s, const conn_rec *c,
1309
                              const request_rec *r, apr_pool_t *pool,
1310
                              const char *fmt, ...)
1311
0
{
1312
0
    va_list args;
1313
1314
0
    va_start(args, fmt);
1315
0
    log_error_core(file, line, module_index, level, status, s, c, r, pool,
1316
0
                   fmt, args);
1317
0
    va_end(args);
1318
0
}
1319
1320
AP_DECLARE(void) ap_log_error_(const char *file, int line, int module_index,
1321
                               int level, apr_status_t status,
1322
                               const server_rec *s, const char *fmt, ...)
1323
0
{
1324
0
    va_list args;
1325
1326
0
    va_start(args, fmt);
1327
0
    log_error_core(file, line, module_index, level, status, s, NULL, NULL,
1328
0
                   NULL, fmt, args);
1329
0
    va_end(args);
1330
0
}
1331
1332
AP_DECLARE(void) ap_log_perror_(const char *file, int line, int module_index,
1333
                                int level, apr_status_t status, apr_pool_t *p,
1334
                                const char *fmt, ...)
1335
0
{
1336
0
    va_list args;
1337
1338
0
    va_start(args, fmt);
1339
0
    log_error_core(file, line, module_index, level, status, NULL, NULL, NULL,
1340
0
                   p, fmt, args);
1341
0
    va_end(args);
1342
0
}
1343
1344
AP_DECLARE(void) ap_log_rerror_(const char *file, int line, int module_index,
1345
                                int level, apr_status_t status,
1346
                                const request_rec *r, const char *fmt, ...)
1347
0
{
1348
0
    va_list args;
1349
1350
0
    va_start(args, fmt);
1351
0
    log_error_core(file, line, module_index, level, status, r->server, NULL, r,
1352
0
                   NULL, fmt, args);
1353
1354
    /*
1355
     * IF APLOG_TOCLIENT is set,
1356
     * AND the error level is 'warning' or more severe,
1357
     * AND there isn't already error text associated with this request,
1358
     * THEN make the message text available to ErrorDocument and
1359
     * other error processors.
1360
     */
1361
0
    va_end(args);
1362
0
    va_start(args,fmt);
1363
0
    if ((level & APLOG_TOCLIENT)
1364
0
        && ((level & APLOG_LEVELMASK) <= APLOG_WARNING)
1365
0
        && (apr_table_get(r->notes, "error-notes") == NULL)) {
1366
0
        apr_table_setn(r->notes, "error-notes",
1367
0
                       ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
1368
0
                                                             args)));
1369
0
    }
1370
0
    va_end(args);
1371
0
}
1372
1373
AP_DECLARE(void) ap_log_cserror_(const char *file, int line, int module_index,
1374
                                 int level, apr_status_t status,
1375
                                 const conn_rec *c, const server_rec *s,
1376
                                 const char *fmt, ...)
1377
0
{
1378
0
    va_list args;
1379
1380
0
    va_start(args, fmt);
1381
0
    log_error_core(file, line, module_index, level, status, s, c,
1382
0
                   NULL, NULL, fmt, args);
1383
0
    va_end(args);
1384
0
}
1385
1386
AP_DECLARE(void) ap_log_cerror_(const char *file, int line, int module_index,
1387
                                int level, apr_status_t status,
1388
                                const conn_rec *c, const char *fmt, ...)
1389
0
{
1390
0
    va_list args;
1391
1392
0
    va_start(args, fmt);
1393
0
    log_error_core(file, line, module_index, level, status, c->base_server, c,
1394
0
                   NULL, NULL, fmt, args);
1395
0
    va_end(args);
1396
0
}
1397
1398
0
#define BYTES_LOGGED_PER_LINE 16
1399
0
#define LOG_BYTES_BUFFER_SIZE (BYTES_LOGGED_PER_LINE * 3 + 2)
1400
1401
static void fmt_data(unsigned char *buf, const void *vdata, apr_size_t len, apr_size_t *off)
1402
0
{
1403
0
    const unsigned char *data = (const unsigned char *)vdata;
1404
0
    unsigned char *chars;
1405
0
    unsigned char *hex;
1406
0
    apr_size_t this_time = 0;
1407
1408
0
    memset(buf, ' ', LOG_BYTES_BUFFER_SIZE - 1);
1409
0
    buf[LOG_BYTES_BUFFER_SIZE - 1] = '\0';
1410
    
1411
0
    chars = buf; /* start character dump here */
1412
0
    hex   = buf + BYTES_LOGGED_PER_LINE + 1; /* start hex dump here */
1413
0
    while (*off < len && this_time < BYTES_LOGGED_PER_LINE) {
1414
0
        unsigned char c = data[*off];
1415
1416
0
        if (apr_isprint(c)
1417
0
            && c != '\\') {  /* backslash will be escaped later, which throws
1418
                              * off the formatting
1419
                              */
1420
0
            *chars = c;
1421
0
        }
1422
0
        else {
1423
0
            *chars = '.';
1424
0
        }
1425
1426
0
        if ((c >> 4) >= 10) {
1427
0
            *hex = 'a' + ((c >> 4) - 10);
1428
0
        }
1429
0
        else {
1430
0
            *hex = '0' + (c >> 4);
1431
0
        }
1432
1433
0
        if ((c & 0x0F) >= 10) {
1434
0
            *(hex + 1) = 'a' + ((c & 0x0F) - 10);
1435
0
        }
1436
0
        else {
1437
0
            *(hex + 1) = '0' + (c & 0x0F);
1438
0
        }
1439
1440
0
        chars += 1;
1441
0
        hex += 2;
1442
0
        *off += 1;
1443
0
        ++this_time;
1444
0
    }
1445
0
}
1446
1447
static void log_data_core(const char *file, int line, int module_index,
1448
                          int level, const server_rec *s,
1449
                          const conn_rec *c, const request_rec *r,
1450
                          const char *label, const void *data, apr_size_t len,
1451
                          unsigned int flags)
1452
0
{
1453
0
    unsigned char buf[LOG_BYTES_BUFFER_SIZE];
1454
0
    apr_size_t off;
1455
0
    char prefix[20];
1456
1457
0
    if (!(flags & AP_LOG_DATA_SHOW_OFFSET)) {
1458
0
        prefix[0] = '\0';
1459
0
    }
1460
1461
0
    if (len > 0xffff) { /* bug in caller? */
1462
0
        len = 0xffff;
1463
0
    }
1464
1465
0
    if (label) {
1466
0
        log_error_va_glue(file, line, module_index, level, APR_SUCCESS, s,
1467
0
                          c, r, NULL, "%s (%" APR_SIZE_T_FMT " bytes)",
1468
0
                          label, len);
1469
0
    }
1470
1471
0
    off = 0;
1472
0
    while (off < len) {
1473
0
        if (flags & AP_LOG_DATA_SHOW_OFFSET) {
1474
0
            apr_snprintf(prefix, sizeof prefix, "%04x: ", (unsigned int)off);
1475
0
        }
1476
0
        fmt_data(buf, data, len, &off);
1477
0
        log_error_va_glue(file, line, module_index, level, APR_SUCCESS, s,
1478
0
                          c, r, NULL, "%s%s", prefix, buf);
1479
0
    }
1480
0
}
1481
1482
AP_DECLARE(void) ap_log_data_(const char *file, int line, 
1483
                              int module_index, int level,
1484
                              const server_rec *s, const char *label,
1485
                              const void *data, apr_size_t len,
1486
                              unsigned int flags)
1487
0
{
1488
0
    log_data_core(file, line, module_index, level, s, NULL, NULL, label,
1489
0
                  data, len, flags);
1490
0
}
1491
1492
AP_DECLARE(void) ap_log_rdata_(const char *file, int line,
1493
                               int module_index, int level,
1494
                               const request_rec *r, const char *label,
1495
                               const void *data, apr_size_t len,
1496
                               unsigned int flags)
1497
0
{
1498
0
    log_data_core(file, line, module_index, level, r->server, NULL, r, label,
1499
0
                  data, len, flags);
1500
0
}
1501
1502
AP_DECLARE(void) ap_log_cdata_(const char *file, int line,
1503
                               int module_index, int level,
1504
                               const conn_rec *c, const char *label,
1505
                               const void *data, apr_size_t len,
1506
                               unsigned int flags)
1507
0
{
1508
0
    log_data_core(file, line, module_index, level, c->base_server, c, NULL,
1509
0
                  label, data, len, flags);
1510
0
}
1511
1512
AP_DECLARE(void) ap_log_csdata_(const char *file, int line, int module_index,
1513
                                int level, const conn_rec *c, const server_rec *s,
1514
                                const char *label, const void *data,
1515
                                apr_size_t len, unsigned int flags)
1516
0
{
1517
0
    log_data_core(file, line, module_index, level, s, c, NULL, label, data,
1518
0
                  len, flags);
1519
0
}
1520
1521
AP_DECLARE(void) ap_log_command_line(apr_pool_t *plog, server_rec *s)
1522
0
{
1523
0
    int i;
1524
0
    process_rec *process = s->process;
1525
0
    char *result;
1526
0
    int len_needed = 0;
1527
1528
    /* Piece together the command line from the pieces
1529
     * in process->argv, with spaces in between.
1530
     */
1531
0
    for (i = 0; i < process->argc; i++) {
1532
0
        len_needed += strlen(process->argv[i]) + 1;
1533
0
    }
1534
1535
0
    result = (char *) apr_palloc(plog, len_needed);
1536
0
    *result = '\0';
1537
1538
0
    for (i = 0; i < process->argc; i++) {
1539
0
        strcat(result, process->argv[i]);
1540
0
        if ((i+1)< process->argc) {
1541
0
            strcat(result, " ");
1542
0
        }
1543
0
    }
1544
0
    ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, APLOGNO(00094)
1545
0
                 "Command line: '%s'", result);
1546
0
}
1547
1548
/* grab bag function to log commonly logged and shared info */
1549
AP_DECLARE(void) ap_log_mpm_common(server_rec *s)
1550
0
{
1551
0
    ap_log_error(APLOG_MARK, APLOG_DEBUG , 0, s, APLOGNO(02639)
1552
0
                 "Using SO_REUSEPORT: %s (%d)",
1553
0
                 ap_have_so_reuseport ? "yes" : "no",
1554
0
                 ap_num_listen_buckets);
1555
0
}
1556
1557
AP_DECLARE(void) ap_remove_pid(apr_pool_t *p, const char *rel_fname)
1558
0
{
1559
0
    apr_status_t rv;
1560
0
    const char *fname = ap_runtime_dir_relative(p, rel_fname);
1561
1562
0
    if (fname != NULL) {
1563
0
        rv = apr_file_remove(fname, p);
1564
0
        if (rv != APR_SUCCESS) {
1565
0
            ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf, APLOGNO(00095)
1566
0
                         "failed to remove PID file %s", fname);
1567
0
        }
1568
0
        else {
1569
0
            ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf, APLOGNO(00096)
1570
0
                         "removed PID file %s (pid=%" APR_PID_T_FMT ")",
1571
0
                         fname, getpid());
1572
0
        }
1573
0
    }
1574
0
}
1575
1576
AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
1577
0
{
1578
0
    apr_file_t *pid_file = NULL;
1579
0
    apr_finfo_t finfo;
1580
0
    static pid_t saved_pid = -1;
1581
0
    pid_t mypid;
1582
0
    apr_status_t rv;
1583
0
    const char *fname;
1584
0
    char *temp_fname;
1585
0
    apr_fileperms_t perms;
1586
0
    char pidstr[64];
1587
1588
0
    if (!filename) {
1589
0
        return;
1590
0
    }
1591
1592
0
    fname = ap_runtime_dir_relative(p, filename);
1593
0
    if (!fname) {
1594
0
        ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
1595
0
                     ap_server_conf, APLOGNO(00097) "Invalid PID file path %s, ignoring.", filename);
1596
0
        return;
1597
0
    }
1598
1599
0
    mypid = getpid();
1600
0
    if (mypid != saved_pid
1601
0
        && apr_stat(&finfo, fname, APR_FINFO_MTIME, p) == APR_SUCCESS) {
1602
        /* AP_SIG_GRACEFUL and HUP call this on each restart.
1603
         * Only warn on first time through for this pid.
1604
         *
1605
         * XXX: Could just write first time through too, although
1606
         *      that may screw up scripts written to do something
1607
         *      based on the last modification time of the pid file.
1608
         */
1609
0
        ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, APLOGNO(00098)
1610
0
                      "pid file %s overwritten -- Unclean "
1611
0
                      "shutdown of previous Apache run?",
1612
0
                      fname);
1613
0
    }
1614
1615
0
    temp_fname = apr_pstrcat(p, fname, ".XXXXXX", NULL);
1616
0
    rv = apr_file_mktemp(&pid_file, temp_fname,
1617
0
                         APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE, p);
1618
0
    if (rv != APR_SUCCESS) {
1619
0
        ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, APLOGNO(00099)
1620
0
                     "could not create %s", temp_fname);
1621
0
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, APLOGNO(00100)
1622
0
                     "%s: could not log pid to file %s",
1623
0
                     ap_server_argv0, fname);
1624
0
        exit(1);
1625
0
    }
1626
1627
0
    apr_snprintf(pidstr, sizeof pidstr, "%" APR_PID_T_FMT APR_EOL_STR, mypid);
1628
1629
0
    perms = APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD;
1630
0
    if (((rv = apr_file_perms_set(temp_fname, perms)) != APR_SUCCESS && rv != APR_ENOTIMPL)
1631
0
        || (rv = apr_file_write_full(pid_file, pidstr, strlen(pidstr), NULL)) != APR_SUCCESS
1632
0
        || (rv = apr_file_close(pid_file)) != APR_SUCCESS
1633
0
        || (rv = apr_file_rename(temp_fname, fname, p)) != APR_SUCCESS) {
1634
0
        ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, APLOGNO(10231)
1635
0
                     "%s: Failed creating pid file %s",
1636
0
                     ap_server_argv0, temp_fname);
1637
0
        exit(1);
1638
0
    }
1639
1640
0
    saved_pid = mypid;
1641
0
}
1642
1643
AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
1644
                                     pid_t *mypid)
1645
0
{
1646
0
    const apr_size_t BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */
1647
0
    apr_file_t *pid_file = NULL;
1648
0
    apr_status_t rv;
1649
0
    const char *fname;
1650
0
    char *buf, *endptr;
1651
0
    apr_size_t bytes_read;
1652
1653
0
    if (!filename) {
1654
0
        return APR_EGENERAL;
1655
0
    }
1656
1657
0
    fname = ap_runtime_dir_relative(p, filename);
1658
0
    if (!fname) {
1659
0
        ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
1660
0
                     ap_server_conf, APLOGNO(00101) "Invalid PID file path %s, ignoring.", filename);
1661
0
        return APR_EGENERAL;
1662
0
    }
1663
1664
0
    rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p);
1665
0
    if (rv != APR_SUCCESS) {
1666
0
        return rv;
1667
0
    }
1668
1669
0
    buf = apr_palloc(p, BUFFER_SIZE);
1670
1671
0
    rv = apr_file_read_full(pid_file, buf, BUFFER_SIZE - 1, &bytes_read);
1672
0
    if (rv != APR_SUCCESS && rv != APR_EOF) {
1673
0
        return rv;
1674
0
    }
1675
1676
    /* If we fill the buffer, we're probably reading a corrupt pid file.
1677
     * To be nice, let's also ensure the first char is a digit. */
1678
0
    if (bytes_read == 0 || bytes_read == BUFFER_SIZE - 1 || !apr_isdigit(*buf)) {
1679
0
        return APR_EGENERAL;
1680
0
    }
1681
1682
0
    buf[bytes_read] = '\0';
1683
0
    *mypid = strtol(buf, &endptr, 10);
1684
1685
0
    apr_file_close(pid_file);
1686
0
    return APR_SUCCESS;
1687
0
}
1688
1689
AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile,
1690
                               int nLine)
1691
0
{
1692
0
    char time_str[APR_CTIME_LEN];
1693
1694
0
    apr_ctime(time_str, apr_time_now());
1695
0
    ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL, APLOGNO(00102)
1696
0
                 "[%s] file %s, line %d, assertion \"%s\" failed",
1697
0
                 time_str, szFile, nLine, szExp);
1698
#if defined(WIN32)
1699
    DebugBreak();
1700
#else
1701
    /* unix assert does an abort leading to a core dump */
1702
0
    abort();
1703
0
#endif
1704
0
}
1705
1706
/* piped log support */
1707
1708
#ifdef AP_HAVE_RELIABLE_PIPED_LOGS
1709
/* forward declaration */
1710
static void piped_log_maintenance(int reason, void *data, apr_wait_t status);
1711
1712
/* Spawn the piped logger process pl->program. */
1713
static apr_status_t piped_log_spawn(piped_log *pl)
1714
0
{
1715
0
    apr_procattr_t *procattr;
1716
0
    apr_proc_t *procnew = NULL;
1717
0
    apr_status_t status;
1718
1719
0
    if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
1720
0
        ((status = apr_procattr_dir_set(procattr, ap_server_root))
1721
0
         != APR_SUCCESS) ||
1722
0
        ((status = apr_procattr_cmdtype_set(procattr, pl->cmdtype))
1723
0
         != APR_SUCCESS) ||
1724
0
        ((status = apr_procattr_child_in_set(procattr,
1725
0
                                             pl->read_fd,
1726
0
                                             pl->write_fd))
1727
0
         != APR_SUCCESS) ||
1728
0
        ((status = apr_procattr_child_errfn_set(procattr, log_child_errfn))
1729
0
         != APR_SUCCESS) ||
1730
0
        ((status = apr_procattr_error_check_set(procattr, 1)) != APR_SUCCESS)) {
1731
        /* Something bad happened, give up and go away. */
1732
0
        ap_log_error(APLOG_MARK, APLOG_STARTUP, status, ap_server_conf, APLOGNO(00103)
1733
0
                     "piped_log_spawn: unable to setup child process '%s'",
1734
0
                     pl->program);
1735
0
    }
1736
0
    else {
1737
0
        char **args;
1738
1739
0
        apr_tokenize_to_argv(pl->program, &args, pl->p);
1740
0
        procnew = apr_pcalloc(pl->p, sizeof(apr_proc_t));
1741
0
        status = apr_proc_create(procnew, args[0], (const char * const *) args,
1742
0
                                 NULL, procattr, pl->p);
1743
1744
0
        if (status == APR_SUCCESS) {
1745
0
            pl->pid = procnew;
1746
            /* procnew->in was dup2'd from pl->write_fd;
1747
             * since the original fd is still valid, close the copy to
1748
             * avoid a leak. */
1749
0
            apr_file_close(procnew->in);
1750
0
            procnew->in = NULL;
1751
0
            apr_proc_other_child_register(procnew, piped_log_maintenance, pl,
1752
0
                                          pl->write_fd, pl->p);
1753
0
            close_handle_in_child(pl->p, pl->read_fd);
1754
0
        }
1755
0
        else {
1756
            /* Something bad happened, give up and go away. */
1757
0
            ap_log_error(APLOG_MARK, APLOG_STARTUP, status, ap_server_conf, APLOGNO(00104)
1758
0
                         "unable to start piped log program '%s'",
1759
0
                         pl->program);
1760
0
        }
1761
0
    }
1762
1763
0
    return status;
1764
0
}
1765
1766
1767
static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
1768
0
{
1769
0
    piped_log *pl = data;
1770
0
    apr_status_t rv;
1771
0
    int mpm_state;
1772
1773
0
    switch (reason) {
1774
0
    case APR_OC_REASON_DEATH:
1775
0
    case APR_OC_REASON_LOST:
1776
0
        pl->pid = NULL; /* in case we don't get it going again, this
1777
                         * tells other logic not to try to kill it
1778
                         */
1779
0
        apr_proc_other_child_unregister(pl);
1780
0
        rv = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
1781
0
        if (rv != APR_SUCCESS) {
1782
0
            ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, APLOGNO(00105)
1783
0
                         "can't query MPM state; not restarting "
1784
0
                         "piped log program '%s'",
1785
0
                         pl->program);
1786
0
        }
1787
0
        else if (mpm_state != AP_MPMQ_STOPPING) {
1788
0
            ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, APLOGNO(00106)
1789
0
                         "piped log program '%s' failed unexpectedly",
1790
0
                         pl->program);
1791
0
            if ((rv = piped_log_spawn(pl)) != APR_SUCCESS) {
1792
                /* what can we do?  This could be the error log we're having
1793
                 * problems opening up... */
1794
0
                ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL, APLOGNO(00107)
1795
0
                             "piped_log_maintenance: unable to respawn '%s'",
1796
0
                             pl->program);
1797
0
            }
1798
0
        }
1799
0
        break;
1800
1801
0
    case APR_OC_REASON_UNWRITABLE:
1802
        /* We should not kill off the pipe here, since it may only be full.
1803
         * If it really is locked, we should kill it off manually. */
1804
0
    break;
1805
1806
0
    case APR_OC_REASON_RESTART:
1807
0
        if (pl->pid != NULL) {
1808
0
            apr_proc_kill(pl->pid, SIGTERM);
1809
0
            pl->pid = NULL;
1810
0
        }
1811
0
        break;
1812
1813
0
    case APR_OC_REASON_UNREGISTER:
1814
0
        break;
1815
0
    }
1816
0
}
1817
1818
1819
static apr_status_t piped_log_cleanup_for_exec(void *data)
1820
0
{
1821
0
    piped_log *pl = data;
1822
1823
0
    apr_file_close(pl->read_fd);
1824
0
    apr_file_close(pl->write_fd);
1825
0
    return APR_SUCCESS;
1826
0
}
1827
1828
1829
static apr_status_t piped_log_cleanup(void *data)
1830
0
{
1831
0
    piped_log *pl = data;
1832
1833
0
    if (pl->pid != NULL) {
1834
0
        apr_proc_kill(pl->pid, SIGTERM);
1835
0
    }
1836
0
    return piped_log_cleanup_for_exec(data);
1837
0
}
1838
1839
1840
AP_DECLARE(piped_log *) ap_open_piped_log_ex(apr_pool_t *p,
1841
                                             const char *program,
1842
                                             apr_cmdtype_e cmdtype)
1843
0
{
1844
0
    piped_log *pl;
1845
1846
0
    pl = apr_palloc(p, sizeof (*pl));
1847
0
    pl->p = p;
1848
0
    pl->program = apr_pstrdup(p, program);
1849
0
    pl->pid = NULL;
1850
0
    pl->cmdtype = cmdtype;
1851
0
    if (apr_file_pipe_create_ex(&pl->read_fd,
1852
0
                                &pl->write_fd,
1853
0
                                APR_FULL_BLOCK, p) != APR_SUCCESS) {
1854
0
        return NULL;
1855
0
    }
1856
0
    apr_pool_cleanup_register(p, pl, piped_log_cleanup,
1857
0
                              piped_log_cleanup_for_exec);
1858
0
    if (piped_log_spawn(pl) != APR_SUCCESS) {
1859
0
        apr_pool_cleanup_kill(p, pl, piped_log_cleanup);
1860
0
        apr_file_close(pl->read_fd);
1861
0
        apr_file_close(pl->write_fd);
1862
0
        return NULL;
1863
0
    }
1864
0
    return pl;
1865
0
}
1866
1867
#else /* !AP_HAVE_RELIABLE_PIPED_LOGS */
1868
1869
static apr_status_t piped_log_cleanup(void *data)
1870
{
1871
    piped_log *pl = data;
1872
1873
    apr_file_close(pl->write_fd);
1874
    return APR_SUCCESS;
1875
}
1876
1877
AP_DECLARE(piped_log *) ap_open_piped_log_ex(apr_pool_t *p,
1878
                                             const char *program,
1879
                                             apr_cmdtype_e cmdtype)
1880
{
1881
    piped_log *pl;
1882
    apr_file_t *dummy = NULL;
1883
    int rc;
1884
1885
    rc = log_child(p, program, &dummy, cmdtype, 0);
1886
    if (rc != APR_SUCCESS) {
1887
        ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, ap_server_conf, APLOGNO(00108)
1888
                     "Couldn't start piped log process '%s'.",
1889
                     (program == NULL) ? "NULL" : program);
1890
        return NULL;
1891
    }
1892
1893
    pl = apr_palloc(p, sizeof (*pl));
1894
    pl->p = p;
1895
    pl->read_fd = NULL;
1896
    pl->write_fd = dummy;
1897
    apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
1898
1899
    return pl;
1900
}
1901
1902
#endif
1903
1904
AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p,
1905
                                          const char *program)
1906
0
{
1907
0
    apr_cmdtype_e cmdtype = APR_PROGRAM_ENV;
1908
1909
    /* In 2.4 favor PROGRAM_ENV, accept "||prog" syntax for compatibility
1910
     * and "|$cmd" to override the default.
1911
     * Any 2.2 backport would continue to favor SHELLCMD_ENV so there
1912
     * accept "||prog" to override, and "|$cmd" to ease conversion.
1913
     */
1914
0
    if (*program == '|')
1915
0
        ++program;
1916
0
    if (*program == '$') {
1917
0
        cmdtype = APR_SHELLCMD_ENV;
1918
0
        ++program;
1919
0
    }
1920
1921
0
    return ap_open_piped_log_ex(p, program, cmdtype);
1922
0
}
1923
1924
AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
1925
0
{
1926
0
    apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
1927
0
}
1928
1929
AP_DECLARE(const char *) ap_parse_log_level(const char *str, int *val)
1930
0
{
1931
0
    const char *err = "Log level keyword must be one of emerg/alert/crit/error/"
1932
0
                      "warn/notice/info/debug/trace1/.../trace8";
1933
0
    int i = 0;
1934
1935
0
    if (str == NULL)
1936
0
        return err;
1937
1938
0
    while (priorities[i].t_name != NULL) {
1939
0
        if (!strcasecmp(str, priorities[i].t_name)) {
1940
0
            *val = priorities[i].t_val;
1941
0
            return NULL;
1942
0
        }
1943
0
        i++;
1944
0
    }
1945
0
    return err;
1946
0
}
1947
1948
AP_IMPLEMENT_HOOK_VOID(error_log,
1949
                       (const ap_errorlog_info *info, const char *errstr),
1950
                       (info, errstr))
1951
1952
AP_IMPLEMENT_HOOK_RUN_FIRST(int, generate_log_id,
1953
                            (const conn_rec *c, const request_rec *r,
1954
                             const char **id),
1955
                            (c, r, id), DECLINED)