]> granicus.if.org Git - apache/blob - server/log.c
80b28443fd7f434e95c3f632dfe6bd847be7965e
[apache] / server / log.c
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 "apr_thread_proc.h"
29 #include "apr_lib.h"
30 #include "apr_signal.h"
31 #include "apr_portable.h"
32 #include "apr_base64.h"
33
34 #define APR_WANT_STDIO
35 #define APR_WANT_STRFUNC
36 #include "apr_want.h"
37
38 #if APR_HAVE_STDARG_H
39 #include <stdarg.h>
40 #endif
41 #if APR_HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #include "ap_config.h"
46 #include "httpd.h"
47 #include "http_config.h"
48 #include "http_core.h"
49 #include "http_log.h"
50 #include "http_main.h"
51 #include "util_time.h"
52 #include "ap_mpm.h"
53
54 APLOG_USE_MODULE(core);
55
56 typedef struct {
57     const char *t_name;
58     int t_val;
59 } TRANS;
60
61 APR_HOOK_STRUCT(
62     APR_HOOK_LINK(error_log)
63 )
64
65 int AP_DECLARE_DATA ap_default_loglevel = DEFAULT_LOGLEVEL;
66
67 #ifdef HAVE_SYSLOG
68
69 static const TRANS facilities[] = {
70     {"auth",    LOG_AUTH},
71 #ifdef LOG_AUTHPRIV
72     {"authpriv",LOG_AUTHPRIV},
73 #endif
74 #ifdef LOG_CRON
75     {"cron",    LOG_CRON},
76 #endif
77 #ifdef LOG_DAEMON
78     {"daemon",  LOG_DAEMON},
79 #endif
80 #ifdef LOG_FTP
81     {"ftp", LOG_FTP},
82 #endif
83 #ifdef LOG_KERN
84     {"kern",    LOG_KERN},
85 #endif
86 #ifdef LOG_LPR
87     {"lpr", LOG_LPR},
88 #endif
89 #ifdef LOG_MAIL
90     {"mail",    LOG_MAIL},
91 #endif
92 #ifdef LOG_NEWS
93     {"news",    LOG_NEWS},
94 #endif
95 #ifdef LOG_SYSLOG
96     {"syslog",  LOG_SYSLOG},
97 #endif
98 #ifdef LOG_USER
99     {"user",    LOG_USER},
100 #endif
101 #ifdef LOG_UUCP
102     {"uucp",    LOG_UUCP},
103 #endif
104 #ifdef LOG_LOCAL0
105     {"local0",  LOG_LOCAL0},
106 #endif
107 #ifdef LOG_LOCAL1
108     {"local1",  LOG_LOCAL1},
109 #endif
110 #ifdef LOG_LOCAL2
111     {"local2",  LOG_LOCAL2},
112 #endif
113 #ifdef LOG_LOCAL3
114     {"local3",  LOG_LOCAL3},
115 #endif
116 #ifdef LOG_LOCAL4
117     {"local4",  LOG_LOCAL4},
118 #endif
119 #ifdef LOG_LOCAL5
120     {"local5",  LOG_LOCAL5},
121 #endif
122 #ifdef LOG_LOCAL6
123     {"local6",  LOG_LOCAL6},
124 #endif
125 #ifdef LOG_LOCAL7
126     {"local7",  LOG_LOCAL7},
127 #endif
128     {NULL,      -1},
129 };
130 #endif
131
132 static const TRANS priorities[] = {
133     {"emerg",   APLOG_EMERG},
134     {"alert",   APLOG_ALERT},
135     {"crit",    APLOG_CRIT},
136     {"error",   APLOG_ERR},
137     {"warn",    APLOG_WARNING},
138     {"notice",  APLOG_NOTICE},
139     {"info",    APLOG_INFO},
140     {"debug",   APLOG_DEBUG},
141     {"trace1",  APLOG_TRACE1},
142     {"trace2",  APLOG_TRACE2},
143     {"trace3",  APLOG_TRACE3},
144     {"trace4",  APLOG_TRACE4},
145     {"trace5",  APLOG_TRACE5},
146     {"trace6",  APLOG_TRACE6},
147     {"trace7",  APLOG_TRACE7},
148     {"trace8",  APLOG_TRACE8},
149     {NULL,      -1},
150 };
151
152 static apr_pool_t *stderr_pool = NULL;
153
154 static apr_file_t *stderr_log = NULL;
155
156 /* track pipe handles to close in child process */
157 typedef struct read_handle_t {
158     struct read_handle_t *next;
159     apr_file_t *handle;
160 } read_handle_t;
161
162 static read_handle_t *read_handles;
163
164 /**
165  * @brief The piped logging structure.  
166  *
167  * Piped logs are used to move functionality out of the main server.  
168  * For example, log rotation is done with piped logs.
169  */
170 struct piped_log {
171     /** The pool to use for the piped log */
172     apr_pool_t *p;
173     /** The pipe between the server and the logging process */
174     apr_file_t *read_fd, *write_fd;
175 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
176     /** The name of the program the logging process is running */
177     char *program;
178     /** The pid of the logging process */
179     apr_proc_t *pid;
180     /** How to reinvoke program when it must be replaced */
181     apr_cmdtype_e cmdtype;
182 #endif
183 };
184
185 AP_DECLARE(apr_file_t *) ap_piped_log_read_fd(piped_log *pl)
186 {
187     return pl->read_fd;
188 }
189
190 AP_DECLARE(apr_file_t *) ap_piped_log_write_fd(piped_log *pl)
191 {
192     return pl->write_fd;
193 }
194
195 /* clear_handle_list() is called when plog is cleared; at that
196  * point we need to forget about our old list of pipe read
197  * handles.  We let the plog cleanups close the actual pipes.
198  */
199 static apr_status_t clear_handle_list(void *v)
200 {
201     read_handles = NULL;
202     return APR_SUCCESS;
203 }
204
205 /* remember to close this handle in the child process
206  *
207  * On Win32 this makes zero sense, because we don't
208  * take the parent process's child procs.
209  * If the win32 parent instead passed each and every
210  * logger write handle from itself down to the child,
211  * and the parent manages all aspects of keeping the 
212  * reliable pipe log children alive, this would still
213  * make no sense :)  Cripple it on Win32.
214  */
215 static void close_handle_in_child(apr_pool_t *p, apr_file_t *f)
216 {
217 #ifndef WIN32
218     read_handle_t *new_handle;
219
220     new_handle = apr_pcalloc(p, sizeof(read_handle_t));
221     new_handle->next = read_handles;
222     new_handle->handle = f;
223     read_handles = new_handle;
224 #endif
225 }
226
227 void ap_logs_child_init(apr_pool_t *p, server_rec *s)
228 {
229     read_handle_t *cur = read_handles;
230
231     while (cur) {
232         apr_file_close(cur->handle);
233         cur = cur->next;
234     }
235 }
236
237 AP_DECLARE(void) ap_open_stderr_log(apr_pool_t *p)
238 {
239     apr_file_open_stderr(&stderr_log, p);
240 }
241
242 AP_DECLARE(apr_status_t) ap_replace_stderr_log(apr_pool_t *p,
243                                                const char *fname)
244 {
245     apr_file_t *stderr_file;
246     apr_status_t rc;
247     char *filename = ap_server_root_relative(p, fname);
248     if (!filename) {
249         ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
250                      APR_EBADPATH, NULL, "Invalid -E error log file %s",
251                      fname);
252         return APR_EBADPATH;
253     }
254     if ((rc = apr_file_open(&stderr_file, filename,
255                             APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
256                             APR_OS_DEFAULT, p)) != APR_SUCCESS) {
257         ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
258                      "%s: could not open error log file %s.",
259                      ap_server_argv0, fname);
260         return rc;
261     }
262     if (!stderr_pool) {
263         /* This is safe provided we revert it when we are finished.
264          * We don't manager the callers pool!
265          */
266         stderr_pool = p;
267     }
268     if ((rc = apr_file_open_stderr(&stderr_log, stderr_pool)) 
269             == APR_SUCCESS) {
270         apr_file_flush(stderr_log);
271         if ((rc = apr_file_dup2(stderr_log, stderr_file, stderr_pool)) 
272                 == APR_SUCCESS) {
273             apr_file_close(stderr_file);
274             /*
275              * You might ponder why stderr_pool should survive?
276              * The trouble is, stderr_pool may have s_main->error_log,
277              * so we aren't in a position to destory stderr_pool until
278              * the next recycle.  There's also an apparent bug which 
279              * is not; if some folk decided to call this function before 
280              * the core open error logs hook, this pool won't survive.
281              * Neither does the stderr logger, so this isn't a problem.
282              */
283         }
284     }
285     /* Revert, see above */
286     if (stderr_pool == p)
287         stderr_pool = NULL;
288
289     if (rc != APR_SUCCESS) {
290         ap_log_error(APLOG_MARK, APLOG_CRIT, rc, NULL,
291                      "unable to replace stderr with error log file");
292     }
293     return rc;
294 }
295
296 static void log_child_errfn(apr_pool_t *pool, apr_status_t err,
297                             const char *description)
298 {
299     ap_log_error(APLOG_MARK, APLOG_ERR, err, NULL,
300                  "%s", description);
301 }
302
303 /* Create a child process running PROGNAME with a pipe connected to
304  * the childs stdin.  The write-end of the pipe will be placed in
305  * *FPIN on successful return.  If dummy_stderr is non-zero, the
306  * stderr for the child will be the same as the stdout of the parent.
307  * Otherwise the child will inherit the stderr from the parent. */
308 static int log_child(apr_pool_t *p, const char *progname,
309                      apr_file_t **fpin, apr_cmdtype_e cmdtype,
310                      int dummy_stderr)
311 {
312     /* Child process code for 'ErrorLog "|..."';
313      * may want a common framework for this, since I expect it will
314      * be common for other foo-loggers to want this sort of thing...
315      */
316     apr_status_t rc;
317     apr_procattr_t *procattr;
318     apr_proc_t *procnew;
319     apr_file_t *errfile;
320
321     if (((rc = apr_procattr_create(&procattr, p)) == APR_SUCCESS)
322         && ((rc = apr_procattr_dir_set(procattr,
323                                        ap_server_root)) == APR_SUCCESS)
324         && ((rc = apr_procattr_cmdtype_set(procattr, cmdtype)) == APR_SUCCESS)
325         && ((rc = apr_procattr_io_set(procattr,
326                                       APR_FULL_BLOCK,
327                                       APR_NO_PIPE,
328                                       APR_NO_PIPE)) == APR_SUCCESS)
329         && ((rc = apr_procattr_error_check_set(procattr, 1)) == APR_SUCCESS)
330         && ((rc = apr_procattr_child_errfn_set(procattr, log_child_errfn)) 
331                 == APR_SUCCESS)) {
332         char **args;
333         const char *pname;
334
335         apr_tokenize_to_argv(progname, &args, p);
336         pname = apr_pstrdup(p, args[0]);
337         procnew = (apr_proc_t *)apr_pcalloc(p, sizeof(*procnew));
338
339         if (dummy_stderr) {
340             if ((rc = apr_file_open_stdout(&errfile, p)) == APR_SUCCESS)
341                 rc = apr_procattr_child_err_set(procattr, errfile, NULL);
342         }
343
344         rc = apr_proc_create(procnew, pname, (const char * const *)args,
345                              NULL, procattr, p);
346
347         if (rc == APR_SUCCESS) {
348             apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
349             (*fpin) = procnew->in;
350             /* read handle to pipe not kept open, so no need to call
351              * close_handle_in_child()
352              */
353         }
354     }
355
356     return rc;
357 }
358
359 /* Open the error log for the given server_rec.  If IS_MAIN is
360  * non-zero, s is the main server. */
361 static int open_error_log(server_rec *s, int is_main, apr_pool_t *p)
362 {
363     const char *fname;
364     int rc;
365
366     if (*s->error_fname == '|') {
367         apr_file_t *dummy = NULL;
368         apr_cmdtype_e cmdtype = APR_PROGRAM_ENV;
369         fname = s->error_fname + 1;
370
371         /* In 2.4 favor PROGRAM_ENV, accept "||prog" syntax for compatibility
372          * and "|$cmd" to override the default.
373          * Any 2.2 backport would continue to favor SHELLCMD_ENV so there 
374          * accept "||prog" to override, and "|$cmd" to ease conversion.
375          */
376         if (*fname == '|')
377             ++fname;
378         if (*fname == '$') {
379             cmdtype = APR_SHELLCMD_ENV;
380             ++fname;
381         }
382
383         /* Spawn a new child logger.  If this is the main server_rec,
384          * the new child must use a dummy stderr since the current
385          * stderr might be a pipe to the old logger.  Otherwise, the
386          * child inherits the parents stderr. */
387         rc = log_child(p, fname, &dummy, cmdtype, is_main);
388         if (rc != APR_SUCCESS) {
389             ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
390                          "Couldn't start ErrorLog process '%s'.",
391                          s->error_fname + 1);
392             return DONE;
393         }
394
395         s->error_log = dummy;
396     }
397
398 #ifdef HAVE_SYSLOG
399     else if (!strncasecmp(s->error_fname, "syslog", 6)) {
400         if ((fname = strchr(s->error_fname, ':'))) {
401             const TRANS *fac;
402
403             fname++;
404             for (fac = facilities; fac->t_name; fac++) {
405                 if (!strcasecmp(fname, fac->t_name)) {
406                     openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID,
407                             fac->t_val);
408                     s->error_log = NULL;
409                     return OK;
410                 }
411             }
412         }
413         else {
414             openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
415         }
416
417         s->error_log = NULL;
418     }
419 #endif
420     else {
421         fname = ap_server_root_relative(p, s->error_fname);
422         if (!fname) {
423             ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EBADPATH, NULL,
424                          "%s: Invalid error log path %s.",
425                          ap_server_argv0, s->error_fname);
426             return DONE;
427         }
428         if ((rc = apr_file_open(&s->error_log, fname,
429                                APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
430                                APR_OS_DEFAULT, p)) != APR_SUCCESS) {
431             ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
432                          "%s: could not open error log file %s.",
433                          ap_server_argv0, fname);
434             return DONE;
435         }
436     }
437
438     return OK;
439 }
440
441 int ap_open_logs(apr_pool_t *pconf, apr_pool_t *p /* plog */,
442                  apr_pool_t *ptemp, server_rec *s_main)
443 {
444     apr_pool_t *stderr_p;
445     server_rec *virt, *q;
446     int replace_stderr;
447
448
449     /* Register to throw away the read_handles list when we
450      * cleanup plog.  Upon fork() for the apache children,
451      * this read_handles list is closed so only the parent
452      * can relaunch a lost log child.  These read handles 
453      * are always closed on exec.
454      * We won't care what happens to our stderr log child 
455      * between log phases, so we don't mind losing stderr's 
456      * read_handle a little bit early.
457      */
458     apr_pool_cleanup_register(p, NULL, clear_handle_list,
459                               apr_pool_cleanup_null);
460
461     /* HERE we need a stdout log that outlives plog.
462      * We *presume* the parent of plog is a process 
463      * or global pool which spans server restarts.
464      * Create our stderr_pool as a child of the plog's
465      * parent pool.
466      */
467     apr_pool_create(&stderr_p, apr_pool_parent_get(p));
468     apr_pool_tag(stderr_p, "stderr_pool");
469
470     if (open_error_log(s_main, 1, stderr_p) != OK) {
471         return DONE;
472     }
473
474     replace_stderr = 1;
475     if (s_main->error_log) {
476         apr_status_t rv;
477
478         /* Replace existing stderr with new log. */
479         apr_file_flush(s_main->error_log);
480         rv = apr_file_dup2(stderr_log, s_main->error_log, stderr_p);
481         if (rv != APR_SUCCESS) {
482             ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s_main,
483                          "unable to replace stderr with error_log");
484         }
485         else {
486             /* We are done with stderr_pool, close it, killing
487              * the previous generation's stderr logger
488              */
489             if (stderr_pool)
490                 apr_pool_destroy(stderr_pool);
491             stderr_pool = stderr_p;
492             replace_stderr = 0;
493             /*
494              * Now that we have dup'ed s_main->error_log to stderr_log
495              * close it and set s_main->error_log to stderr_log. This avoids
496              * this fd being inherited by the next piped logger who would
497              * keep open the writing end of the pipe that this one uses
498              * as stdin. This in turn would prevent the piped logger from
499              * exiting.
500              */
501              apr_file_close(s_main->error_log);
502              s_main->error_log = stderr_log;
503         }
504     }
505     /* note that stderr may still need to be replaced with something
506      * because it points to the old error log, or back to the tty
507      * of the submitter.
508      * XXX: This is BS - /dev/null is non-portable
509      *      errno-as-apr_status_t is also non-portable
510      */
511     if (replace_stderr && freopen("/dev/null", "w", stderr) == NULL) {
512         ap_log_error(APLOG_MARK, APLOG_CRIT, errno, s_main,
513                      "unable to replace stderr with /dev/null");
514     }
515
516     for (virt = s_main->next; virt; virt = virt->next) {
517         if (virt->error_fname) {
518             for (q=s_main; q != virt; q = q->next) {
519                 if (q->error_fname != NULL
520                     && strcmp(q->error_fname, virt->error_fname) == 0) {
521                     break;
522                 }
523             }
524
525             if (q == virt) {
526                 if (open_error_log(virt, 0, p) != OK) {
527                     return DONE;
528                 }
529             }
530             else {
531                 virt->error_log = q->error_log;
532             }
533         }
534         else {
535             virt->error_log = s_main->error_log;
536         }
537     }
538     return OK;
539 }
540
541 AP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
542     apr_file_t *errfile = NULL;
543
544     apr_file_open_stderr(&errfile, s->process->pool);
545     if (s->error_log != NULL) {
546         apr_file_dup2(s->error_log, errfile, s->process->pool);
547     }
548 }
549
550 static int cpystrn(char *buf, const char *arg, int buflen)
551 {
552     char *end;
553     if (!arg)
554         return 0;
555     end = apr_cpystrn(buf, arg, buflen);
556     return end - buf;
557 }
558
559
560 static int log_remote_address(const ap_errorlog_info *info, const char *arg,
561                               char *buf, int buflen)
562 {
563     if (info->c)
564         return apr_snprintf(buf, buflen, "%s:%d", info->c->remote_ip,
565                             info->c->remote_addr->port);
566     else
567         return 0;
568 }
569
570 static int log_local_address(const ap_errorlog_info *info, const char *arg,
571                              char *buf, int buflen)
572 {
573     if (info->c)
574         return apr_snprintf(buf, buflen, "%s:%d", info->c->local_ip,
575                             info->c->local_addr->port);
576     else
577         return 0;
578 }
579
580 static int log_pid(const ap_errorlog_info *info, const char *arg,
581                    char *buf, int buflen)
582 {
583     pid_t pid = getpid();
584     return apr_snprintf(buf, buflen, "%" APR_PID_T_FMT, pid);
585 }
586
587 static int log_tid(const ap_errorlog_info *info, const char *arg,
588                    char *buf, int buflen)
589 {
590 #if APR_HAS_THREADS
591     int result;
592
593     if (ap_mpm_query(AP_MPMQ_IS_THREADED, &result) == APR_SUCCESS
594         && result != AP_MPMQ_NOT_SUPPORTED)
595     {
596         apr_os_thread_t tid = apr_os_thread_current();
597         return apr_snprintf(buf, buflen, "%pT", &tid);
598     }
599 #endif
600     return 0;
601 }
602
603 static int log_ctime(const ap_errorlog_info *info, const char *arg,
604                      char *buf, int buflen)
605 {
606     int time_len = buflen;
607     int option = AP_CTIME_OPTION_NONE;
608
609     while(arg && *arg) {
610         switch (*arg) {
611             case 'u':   option |= AP_CTIME_OPTION_USEC;
612                         break;
613             case 'c':   option |= AP_CTIME_OPTION_COMPACT;
614                         break;
615         }
616         arg++;
617     }
618
619     ap_recent_ctime_ex(buf, apr_time_now(), option, &time_len);
620
621     /* ap_recent_ctime_ex includes the trailing \0 in time_len */
622     return time_len - 1;
623 }
624
625 static int log_loglevel(const ap_errorlog_info *info, const char *arg,
626                         char *buf, int buflen)
627 {
628     if (info->level < 0)
629         return 0;
630     else
631         return cpystrn(buf, priorities[info->level].t_name, buflen);
632 }
633
634 static int log_log_id(const ap_errorlog_info *info, const char *arg,
635                       char *buf, int buflen)
636 {
637     /*
638      * C: log conn log_id if available,
639      * c: log conn log id if available and not a once-per-request log line
640      * else: log request log id if available
641      */
642     if (arg && !strcasecmp(arg, "c")) {
643         if (info->c && (*arg != 'C' || !info->r)) {
644             return cpystrn(buf, info->c->log_id, buflen);
645         }
646     }
647     else if (info->r) {
648         return cpystrn(buf, info->r->log_id, buflen);
649     }
650     return 0;
651 }
652
653 static int log_keepalives(const ap_errorlog_info *info, const char *arg,
654                           char *buf, int buflen)
655 {
656     if (!info->c)
657         return 0;
658
659     return apr_snprintf(buf, buflen, "%d", info->c->keepalives);
660 }
661
662 static int log_module_name(const ap_errorlog_info *info, const char *arg,
663                            char *buf, int buflen)
664 {
665     return cpystrn(buf, ap_find_module_short_name(info->module_index), buflen);
666 }
667
668 static int log_file_line(const ap_errorlog_info *info, const char *arg,
669                          char *buf, int buflen)
670 {
671     if (info->file == NULL) {
672         return 0;
673     }
674     else {
675         const char *file = info->file;
676 #if defined(_OSD_POSIX) || defined(WIN32) || defined(__MVS__)
677         char tmp[256];
678         char *e = strrchr(file, '/');
679 #ifdef WIN32
680         if (!e) {
681             e = strrchr(file, '\\');
682         }
683 #endif
684
685         /* In OSD/POSIX, the compiler returns for __FILE__
686          * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
687          * (it even returns an absolute path for sources in
688          * the current directory). Here we try to strip this
689          * down to the basename.
690          */
691         if (e != NULL && e[1] != '\0') {
692             apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
693             e = &tmp[strlen(tmp)-1];
694             if (*e == ')') {
695                 *e = '\0';
696             }
697             file = tmp;
698         }
699 #else /* _OSD_POSIX || WIN32 */
700         const char *p;
701         /* On Unix, __FILE__ may be an absolute path in a
702          * VPATH build. */
703         if (file[0] == '/' && (p = ap_strrchr_c(file, '/')) != NULL) {
704             file = p + 1;
705         }
706 #endif /*_OSD_POSIX || WIN32 */
707         return apr_snprintf(buf, buflen, "%s(%d)", file, info->line);
708     }
709 }
710
711 static int log_apr_status(const ap_errorlog_info *info, const char *arg,
712                           char *buf, int buflen)
713 {
714     apr_status_t status = info->status;
715     int len;
716     if (!status)
717         return 0;
718
719     if (status < APR_OS_START_EAIERR) {
720         len = apr_snprintf(buf, buflen, "(%d)", status);
721     }
722     else if (status < APR_OS_START_SYSERR) {
723         len = apr_snprintf(buf, buflen, "(EAI %d)",
724                            status - APR_OS_START_EAIERR);
725     }
726     else if (status < 100000 + APR_OS_START_SYSERR) {
727         len = apr_snprintf(buf, buflen, "(OS %d)",
728                            status - APR_OS_START_SYSERR);
729     }
730     else {
731         len = apr_snprintf(buf, buflen, "(os 0x%08x)",
732                            status - APR_OS_START_SYSERR);
733     }
734     apr_strerror(status, buf + len, buflen - len);
735     len += strlen(buf + len);
736     return len;
737 }
738
739 static int log_header(const ap_errorlog_info *info, const char *arg,
740                       char *buf, int buflen)
741 {
742     const char *header;
743     int len = 0;
744 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
745     char scratch[MAX_STRING_LEN];
746 #endif
747
748     if ( info->r && (header = apr_table_get(info->r->headers_in, arg))
749 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
750          && ap_escape_errorlog_item(scratch, header, MAX_STRING_LEN)
751 #endif
752        ) {
753         len = cpystrn(buf,
754 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
755                            scratch,
756 #else
757                            header,
758 #endif
759                            buflen);
760     }
761     return len;
762 }
763
764
765
766
767 AP_DECLARE(void) ap_register_builtin_errorlog_handlers(apr_pool_t *p)
768 {
769     ap_register_errorlog_handler(p, "a", log_remote_address, 0);
770     ap_register_errorlog_handler(p, "A", log_local_address, 0);
771     ap_register_errorlog_handler(p, "E", log_apr_status, 0);
772     ap_register_errorlog_handler(p, "F", log_file_line, 0);
773     ap_register_errorlog_handler(p, "i", log_header, 0);
774     ap_register_errorlog_handler(p, "k", log_keepalives, 0);
775     ap_register_errorlog_handler(p, "l", log_loglevel, 0);
776     ap_register_errorlog_handler(p, "L", log_log_id, 0);
777     ap_register_errorlog_handler(p, "m", log_module_name, 0);
778     ap_register_errorlog_handler(p, "P", log_pid, 0);
779     ap_register_errorlog_handler(p, "t", log_ctime, 0);
780     ap_register_errorlog_handler(p, "T", log_tid, 0);
781
782     /* XXX: TODO: envvars, notes */
783 }
784
785 static void add_log_id(const conn_rec *c, const request_rec *r)
786 {
787     apr_uint64_t id, tmp;
788     pid_t pid;
789     int len;
790     char *encoded;
791
792     if (r && r->request_time) {
793         id = (apr_uint64_t)r->request_time;
794     }
795     else {
796         id = (apr_uint64_t)apr_time_now();
797     }
798
799     pid = getpid();
800     if (sizeof(pid_t) > 2) {
801         tmp = pid;
802         tmp = tmp << 40;
803         id ^= tmp;
804         pid = pid >> 24;
805         tmp = pid;
806         tmp = tmp << 56;
807         id ^= tmp;
808     }
809     else {
810         tmp = pid;
811         tmp = tmp << 40;
812         id ^= tmp;
813     }
814 #if APR_HAS_THREADS
815     {
816         apr_uintptr_t tmp2 = (apr_uintptr_t)c->current_thread;
817         tmp = tmp2;
818         tmp = tmp << 32;
819         id ^= tmp;
820     }
821 #endif
822
823     len = apr_base64_encode_len(sizeof(id)); /* includes trailing \0 */
824     encoded = apr_palloc(r ? r->pool : c->pool, len);
825     apr_base64_encode(encoded, (char *)&id, sizeof(id));
826
827     /* Skip the last char, it is always '=' */
828     encoded[len - 2] = '\0'; 
829
830     /* need to cast const away */
831     if (r) {
832         ((request_rec *)r)->log_id = encoded;
833     }
834     else {
835         ((conn_rec *)c)->log_id = encoded;
836     }
837 }
838
839 /*
840  * This is used if no error log format is defined and during startup.
841  * It automatically omits the timestamp if logging to syslog.
842  */
843 static int do_errorlog_default(const ap_errorlog_info *info, char *buf,
844                                int buflen, int *errstr_start, int *errstr_end,
845                                const char *errstr_fmt, va_list args)
846 {
847     int len = 0;
848     int field_start = 0;
849     int item_len;
850 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
851     char scratch[MAX_STRING_LEN];
852 #endif
853
854     if (!info->using_syslog && !info->startup) {
855         buf[len++] = '[';
856         len += log_ctime(info, "u", buf + len, buflen - len);
857         buf[len++] = ']';
858         buf[len++] = ' ';
859     }
860
861     if (!info->startup) {
862         buf[len++] = '[';
863         len += log_module_name(info, NULL, buf + len, buflen - len);
864         buf[len++] = ':';
865         len += log_loglevel(info, NULL, buf + len, buflen - len);
866         len += cpystrn(buf + len, "] [pid ", buflen - len);
867
868         len += log_pid(info, NULL, buf + len, buflen - len);
869 #if APR_HAS_THREADS
870         field_start = len;
871         len += cpystrn(buf + len, ":tid ", buflen - len);
872         item_len = log_tid(info, NULL, buf + len, buflen - len);
873         if (!item_len)
874             len = field_start;
875         else
876             len += item_len;
877 #endif
878         buf[len++] = ']';
879         buf[len++] = ' ';
880     }
881
882     if (info->level >= APLOG_DEBUG) {
883         item_len = log_file_line(info, NULL, buf + len, buflen - len);
884         if (item_len) {
885             len += item_len;
886             len += cpystrn(buf + len, ": ", buflen - len);
887         }
888     }
889
890     if (info->status) {
891         item_len = log_apr_status(info, NULL, buf + len, buflen - len);
892         if (item_len) {
893             len += item_len;
894             len += cpystrn(buf + len, ": ", buflen - len);
895         }
896     }
897
898     if (info->c) {
899         /*
900          * remote_ip can be client or backend server. If we have a scoreboard
901          * handle, it is likely a client.
902          */
903         len += apr_snprintf(buf + len, buflen - len,
904                             info->c->sbh ? "[client %s:%d] " : "[remote %s:%d] ",
905                             info->c->remote_ip, info->c->remote_addr->port);
906     }
907
908     /* the actual error message */
909     *errstr_start = len;
910 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
911     if (apr_vsnprintf(scratch, MAX_STRING_LEN, errstr_fmt, args)) {
912         len += ap_escape_errorlog_item(buf + len, scratch,
913                                        buflen - len);
914
915     }
916 #else
917     len += apr_vsnprintf(buf + len, buflen - len, errstr_fmt, args);
918 #endif
919     *errstr_end = len;
920
921     field_start = len;
922     len += cpystrn(buf + len, ", referer: ", buflen - len);
923     item_len = log_header(info, "Referer", buf + len, buflen - len);
924     if (item_len)
925         len += item_len;
926     else
927         len = field_start;
928
929     return len;
930 }
931
932 static int do_errorlog_format(apr_array_header_t *fmt, ap_errorlog_info *info,
933                               char *buf, int buflen, int *errstr_start,
934                               int *errstr_end, const char *err_fmt, va_list args)
935 {
936 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
937     char scratch[MAX_STRING_LEN];
938 #endif
939     int i;
940     int len = 0;
941     int field_start = 0;
942     int skipping = 0;
943     ap_errorlog_format_item *items = (ap_errorlog_format_item *)fmt->elts;
944
945     for (i = 0; i < fmt->nelts; ++i) {
946         ap_errorlog_format_item *item = &items[i];
947         if (item->flags & AP_ERRORLOG_FLAG_FIELD_SEP) {
948             if (skipping) {
949                 skipping = 0;
950             }
951             else {
952                 field_start = len;
953             }
954         }
955
956         if (item->flags & AP_ERRORLOG_FLAG_MESSAGE) {
957             /* the actual error message */
958             *errstr_start = len;
959 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
960             if (apr_vsnprintf(scratch, MAX_STRING_LEN, err_fmt, args)) {
961                 len += ap_escape_errorlog_item(buf + len, scratch,
962                                                buflen - len);
963
964             }
965 #else
966             len += apr_vsnprintf(buf + len, buflen - len, err_fmt, args);
967 #endif
968             *errstr_end = len;
969         }
970         else if (skipping) {
971             continue;
972         }
973         else {
974             int item_len = (*item->func)(info, item->arg, buf + len,
975                                          buflen - len);
976             if (!item_len) {
977                 if (item->flags & AP_ERRORLOG_FLAG_REQUIRED) {
978                     /* required item is empty. skip whole line */
979                     buf[0] = '\0';
980                     return 0;
981                 }
982                 else if (item->flags & AP_ERRORLOG_FLAG_NULL_AS_HYPHEN) {
983                     buf[len++] = '-';
984                 }
985                 else {
986                     len = field_start;
987                     skipping = 1;
988                 }
989             }
990             else {
991                 len += item_len;
992             }
993         }
994     }
995     return len;
996 }
997
998 static void write_logline(char *errstr, apr_size_t len, apr_file_t *logf,
999                           int level)
1000 {
1001     /* NULL if we are logging to syslog */
1002     if (logf) {
1003         /* Truncate for the terminator (as apr_snprintf does) */
1004         if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR)) {
1005             len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
1006         }
1007         strcpy(errstr + len, APR_EOL_STR);
1008         apr_file_puts(errstr, logf);
1009         apr_file_flush(logf);
1010     }
1011 #ifdef HAVE_SYSLOG
1012     else {
1013         syslog(level < LOG_PRIMASK ? level : APLOG_DEBUG, "%s", errstr);
1014     }
1015 #endif
1016 }
1017
1018 static void log_error_core(const char *file, int line, int module_index,
1019                            int level,
1020                            apr_status_t status, const server_rec *s,
1021                            const conn_rec *c,
1022                            const request_rec *r, apr_pool_t *pool,
1023                            const char *fmt, va_list args)
1024 {
1025     char errstr[MAX_STRING_LEN];
1026     apr_file_t *logf = NULL;
1027     int level_and_mask = level & APLOG_LEVELMASK;
1028     const request_rec *rmain = NULL;
1029     core_server_config *sconf = NULL;
1030     ap_errorlog_info info;
1031
1032     /* do we need to log once-per-req or once-per-conn info? */
1033     int log_conn_info = 0, log_req_info = 0;
1034     apr_array_header_t **lines = NULL;
1035     int done = 0;
1036     int line_number = 0;
1037
1038     if (r && r->connection) {
1039         c = r->connection;
1040     }
1041
1042     if (s == NULL) {
1043         /*
1044          * If we are doing stderr logging (startup), don't log messages that are
1045          * above the default server log level unless it is a startup/shutdown
1046          * notice
1047          */
1048 #ifndef DEBUG
1049         if ((level_and_mask != APLOG_NOTICE)
1050             && (level_and_mask > ap_default_loglevel)) {
1051             return;
1052         }
1053 #endif
1054
1055         logf = stderr_log;
1056     }
1057     else {
1058         int configured_level = r ? ap_get_request_module_loglevel(r, module_index)        :
1059                                c ? ap_get_conn_server_module_loglevel(c, s, module_index) :
1060                                    ap_get_server_module_loglevel(s, module_index);
1061         if (s->error_log) {
1062             /*
1063              * If we are doing normal logging, don't log messages that are
1064              * above the module's log level unless it is a startup/shutdown notice
1065              */
1066             if ((level_and_mask != APLOG_NOTICE)
1067                 && (level_and_mask > configured_level)) {
1068                 return;
1069             }
1070
1071             logf = s->error_log;
1072         }
1073         else {
1074             /*
1075              * If we are doing syslog logging, don't log messages that are
1076              * above the module's log level (including a startup/shutdown notice)
1077              */
1078             if (level_and_mask > configured_level) {
1079                 return;
1080             }
1081         }
1082
1083         sconf = ap_get_module_config(s->module_config, &core_module);
1084         if (c && !c->log_id) {
1085             add_log_id(c, NULL);
1086             if (sconf->error_log_conn && sconf->error_log_conn->nelts > 0)
1087                 log_conn_info = 1;
1088         }
1089         if (r) {
1090             if (r->main)
1091                 rmain = r->main;
1092             else
1093                 rmain = r;
1094
1095             if (!rmain->log_id) {
1096                 /* XXX: do we need separate log ids for subrequests? */
1097                 if (sconf->error_log_req && sconf->error_log_req->nelts > 0)
1098                     log_req_info = 1;
1099                 /*
1100                  * XXX: potential optimization: only create log id if %L is
1101                  * XXX: actually used
1102                  */
1103                 add_log_id(c, rmain);
1104             }
1105         }
1106     }
1107
1108     info.s             = s;
1109     info.c             = c;
1110     info.file          = file;
1111     info.line          = line;
1112     info.status        = status;
1113     info.using_syslog  = (logf == NULL);
1114     info.startup       = ((level & APLOG_STARTUP) == APLOG_STARTUP);
1115
1116
1117     while (!done) {
1118         apr_array_header_t *log_format;
1119         int len = 0, errstr_start = 0, errstr_end = 0;
1120         /* XXX: potential optimization: format common prefixes only once */
1121         if (log_conn_info) {
1122             /* once-per-connection info */
1123             if (line_number == 0) {
1124                 lines = (apr_array_header_t **)sconf->error_log_conn->elts;
1125                 info.r = NULL;
1126                 info.rmain = NULL;
1127                 info.level = -1;
1128                 info.module_index = APLOG_NO_MODULE;
1129             }
1130
1131             log_format = lines[line_number++];
1132
1133             if (line_number == sconf->error_log_conn->nelts) {
1134                 /* this is the last line of once-per-connection info */
1135                 line_number = 0;
1136                 log_conn_info = 0;
1137             }
1138         }
1139         else if (log_req_info) {
1140             /* once-per-request info */
1141             if (line_number == 0) {
1142                 lines = (apr_array_header_t **)sconf->error_log_req->elts;
1143                 info.r = rmain;
1144                 info.rmain = rmain;
1145                 info.level = -1;
1146                 info.module_index = APLOG_NO_MODULE;
1147             }
1148
1149             log_format = lines[line_number++];
1150
1151             if (line_number == sconf->error_log_req->nelts) {
1152                 /* this is the last line of once-per-request info */
1153                 line_number = 0;
1154                 log_req_info = 0;
1155             }
1156         }
1157         else {
1158             /* the actual error message */
1159             info.r = r;
1160             info.rmain = rmain;
1161             info.level = level_and_mask;
1162             info.module_index = module_index;
1163             log_format = sconf ? sconf->error_log_format : NULL;
1164             done = 1;
1165         }
1166
1167         /*
1168          * prepare and log one line
1169          */
1170
1171         if (log_format) {
1172             len += do_errorlog_format(log_format, &info, errstr + len,
1173                                       MAX_STRING_LEN - len,
1174                                       &errstr_start, &errstr_end, fmt, args);
1175         }
1176         else {
1177             len += do_errorlog_default(&info, errstr + len, MAX_STRING_LEN - len,
1178                                        &errstr_start, &errstr_end, fmt, args);
1179         }
1180
1181         if (!*errstr)
1182         {
1183             /*
1184              * Don't log empty lines. This can happen with once-per-conn/req
1185              * info if an item with AP_ERRORLOG_FLAG_REQUIRED is NULL.
1186              */
1187             continue;
1188         }
1189         write_logline(errstr, len, logf, level_and_mask);
1190
1191         if (!log_format) {
1192             /* only pass the real error string to the hook */
1193             errstr[errstr_end] = '\0';
1194             ap_run_error_log(file, line, module_index, level, status, s, r, pool,
1195                              errstr + errstr_start);
1196         }
1197
1198         *errstr = '\0';
1199     }
1200 }
1201
1202 AP_DECLARE(void) ap_log_error_(const char *file, int line, int module_index,
1203                                int level, apr_status_t status,
1204                                const server_rec *s, const char *fmt, ...)
1205 {
1206     va_list args;
1207
1208     va_start(args, fmt);
1209     log_error_core(file, line, module_index, level, status, s, NULL, NULL,
1210                    NULL, fmt, args);
1211     va_end(args);
1212 }
1213
1214 AP_DECLARE(void) ap_log_perror_(const char *file, int line, int module_index,
1215                                 int level, apr_status_t status, apr_pool_t *p,
1216                                 const char *fmt, ...)
1217 {
1218     va_list args;
1219
1220     va_start(args, fmt);
1221     log_error_core(file, line, module_index, level, status, NULL, NULL, NULL,
1222                    p, fmt, args);
1223     va_end(args);
1224 }
1225
1226 AP_DECLARE(void) ap_log_rerror_(const char *file, int line, int module_index,
1227                                 int level, apr_status_t status,
1228                                 const request_rec *r, const char *fmt, ...)
1229 {
1230     va_list args;
1231
1232     va_start(args, fmt);
1233     log_error_core(file, line, module_index, level, status, r->server, NULL, r,
1234                    NULL, fmt, args);
1235
1236     /*
1237      * IF APLOG_TOCLIENT is set,
1238      * AND the error level is 'warning' or more severe,
1239      * AND there isn't already error text associated with this request,
1240      * THEN make the message text available to ErrorDocument and
1241      * other error processors.
1242      */
1243     va_end(args);
1244     va_start(args,fmt);
1245     if ((level & APLOG_TOCLIENT)
1246         && ((level & APLOG_LEVELMASK) <= APLOG_WARNING)
1247         && (apr_table_get(r->notes, "error-notes") == NULL)) {
1248         apr_table_setn(r->notes, "error-notes",
1249                        ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
1250                                                              args)));
1251     }
1252     va_end(args);
1253 }
1254
1255 AP_DECLARE(void) ap_log_cserror_(const char *file, int line, int module_index,
1256                                  int level, apr_status_t status,
1257                                  const conn_rec *c, const server_rec *s,
1258                                  const char *fmt, ...)
1259 {
1260     va_list args;
1261
1262     va_start(args, fmt);
1263     log_error_core(file, line, module_index, level, status, s, c,
1264                    NULL, NULL, fmt, args);
1265     va_end(args);
1266 }
1267
1268 AP_DECLARE(void) ap_log_cerror_(const char *file, int line, int module_index,
1269                                 int level, apr_status_t status,
1270                                 const conn_rec *c, const char *fmt, ...)
1271 {
1272     va_list args;
1273
1274     va_start(args, fmt);
1275     log_error_core(file, line, module_index, level, status, c->base_server, c,
1276                    NULL, NULL, fmt, args);
1277     va_end(args);
1278 }
1279
1280 AP_DECLARE(void) ap_log_command_line(apr_pool_t *plog, server_rec *s)
1281 {
1282     int i;
1283     process_rec *process = s->process;
1284     char *result;
1285     int len_needed = 0;
1286     
1287     /* Piece together the command line from the pieces
1288      * in process->argv, with spaces in between.
1289      */
1290     for (i = 0; i < process->argc; i++) {
1291         len_needed += strlen(process->argv[i]) + 1;
1292     }
1293
1294     result = (char *) apr_palloc(plog, len_needed);
1295     *result = '\0';
1296
1297     for (i = 0; i < process->argc; i++) {
1298         strcat(result, process->argv[i]);
1299         if ((i+1)< process->argc) {
1300             strcat(result, " ");
1301         }
1302     }
1303     ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
1304                  "Command line: '%s'", result);
1305 }
1306
1307 AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
1308 {
1309     apr_file_t *pid_file = NULL;
1310     apr_finfo_t finfo;
1311     static pid_t saved_pid = -1;
1312     pid_t mypid;
1313     apr_status_t rv;
1314     const char *fname;
1315
1316     if (!filename) {
1317         return;
1318     }
1319
1320     fname = ap_server_root_relative(p, filename);
1321     if (!fname) {
1322         ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
1323                      NULL, "Invalid PID file path %s, ignoring.", filename);
1324         return;
1325     }
1326
1327     mypid = getpid();
1328     if (mypid != saved_pid
1329         && apr_stat(&finfo, fname, APR_FINFO_MTIME, p) == APR_SUCCESS) {
1330         /* AP_SIG_GRACEFUL and HUP call this on each restart.
1331          * Only warn on first time through for this pid.
1332          *
1333          * XXX: Could just write first time through too, although
1334          *      that may screw up scripts written to do something
1335          *      based on the last modification time of the pid file.
1336          */
1337         ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p,
1338                       "pid file %s overwritten -- Unclean "
1339                       "shutdown of previous Apache run?",
1340                       fname);
1341     }
1342
1343     if ((rv = apr_file_open(&pid_file, fname,
1344                             APR_WRITE | APR_CREATE | APR_TRUNCATE,
1345                             APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p))
1346         != APR_SUCCESS) {
1347         ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
1348                      "could not create %s", fname);
1349         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
1350                      "%s: could not log pid to file %s",
1351                      ap_server_argv0, fname);
1352         exit(1);
1353     }
1354     apr_file_printf(pid_file, "%" APR_PID_T_FMT APR_EOL_STR, mypid);
1355     apr_file_close(pid_file);
1356     saved_pid = mypid;
1357 }
1358
1359 AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
1360                                      pid_t *mypid)
1361 {
1362     const apr_size_t BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */
1363     apr_file_t *pid_file = NULL;
1364     apr_status_t rv;
1365     const char *fname;
1366     char *buf, *endptr;
1367     apr_size_t bytes_read;
1368
1369     if (!filename) {
1370         return APR_EGENERAL;
1371     }
1372
1373     fname = ap_server_root_relative(p, filename);
1374     if (!fname) {
1375         ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
1376                      NULL, "Invalid PID file path %s, ignoring.", filename);
1377         return APR_EGENERAL;
1378     }
1379
1380     rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p);
1381     if (rv != APR_SUCCESS) {
1382         return rv;
1383     }
1384
1385     buf = apr_palloc(p, BUFFER_SIZE);
1386
1387     rv = apr_file_read_full(pid_file, buf, BUFFER_SIZE - 1, &bytes_read);
1388     if (rv != APR_SUCCESS && rv != APR_EOF) {
1389         return rv;
1390     }
1391
1392     /* If we fill the buffer, we're probably reading a corrupt pid file.
1393      * To be nice, let's also ensure the first char is a digit. */
1394     if (bytes_read == 0 || bytes_read == BUFFER_SIZE - 1 || !apr_isdigit(*buf)) {
1395         return APR_EGENERAL;
1396     }
1397
1398     buf[bytes_read] = '\0';
1399     *mypid = strtol(buf, &endptr, 10);
1400
1401     apr_file_close(pid_file);
1402     return APR_SUCCESS;
1403 }
1404
1405 AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile,
1406                                int nLine)
1407 {
1408     char time_str[APR_CTIME_LEN];
1409
1410     apr_ctime(time_str, apr_time_now());
1411     ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
1412                  "[%s] file %s, line %d, assertion \"%s\" failed",
1413                  time_str, szFile, nLine, szExp);
1414 #if defined(WIN32)
1415     DebugBreak();
1416 #else
1417     /* unix assert does an abort leading to a core dump */
1418     abort();
1419 #endif
1420 }
1421
1422 /* piped log support */
1423
1424 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
1425 /* forward declaration */
1426 static void piped_log_maintenance(int reason, void *data, apr_wait_t status);
1427
1428 /* Spawn the piped logger process pl->program. */
1429 static apr_status_t piped_log_spawn(piped_log *pl)
1430 {
1431     apr_procattr_t *procattr;
1432     apr_proc_t *procnew = NULL;
1433     apr_status_t status;
1434
1435     if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
1436         ((status = apr_procattr_dir_set(procattr, ap_server_root))
1437          != APR_SUCCESS) ||
1438         ((status = apr_procattr_cmdtype_set(procattr, pl->cmdtype))
1439          != APR_SUCCESS) ||
1440         ((status = apr_procattr_child_in_set(procattr,
1441                                              pl->read_fd,
1442                                              pl->write_fd))
1443          != APR_SUCCESS) ||
1444         ((status = apr_procattr_child_errfn_set(procattr, log_child_errfn))
1445          != APR_SUCCESS) ||
1446         ((status = apr_procattr_error_check_set(procattr, 1)) != APR_SUCCESS)) {
1447         char buf[120];
1448         /* Something bad happened, give up and go away. */
1449         ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1450                      "piped_log_spawn: unable to setup child process '%s': %s",
1451                      pl->program, apr_strerror(status, buf, sizeof(buf)));
1452     }
1453     else {
1454         char **args;
1455         const char *pname;
1456
1457         apr_tokenize_to_argv(pl->program, &args, pl->p);
1458         pname = apr_pstrdup(pl->p, args[0]);
1459         procnew = apr_pcalloc(pl->p, sizeof(apr_proc_t));
1460         status = apr_proc_create(procnew, pname, (const char * const *) args,
1461                                  NULL, procattr, pl->p);
1462
1463         if (status == APR_SUCCESS) {
1464             pl->pid = procnew;
1465             /* procnew->in was dup2'd from pl->write_fd;
1466              * since the original fd is still valid, close the copy to
1467              * avoid a leak. */
1468             apr_file_close(procnew->in);
1469             procnew->in = NULL;
1470             apr_proc_other_child_register(procnew, piped_log_maintenance, pl,
1471                                           pl->write_fd, pl->p);
1472             close_handle_in_child(pl->p, pl->read_fd);
1473         }
1474         else {
1475             char buf[120];
1476             /* Something bad happened, give up and go away. */
1477             ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1478                          "unable to start piped log program '%s': %s",
1479                          pl->program, apr_strerror(status, buf, sizeof(buf)));
1480         }
1481     }
1482
1483     return status;
1484 }
1485
1486
1487 static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
1488 {
1489     piped_log *pl = data;
1490     apr_status_t stats;
1491     int mpm_state;
1492
1493     switch (reason) {
1494     case APR_OC_REASON_DEATH:
1495     case APR_OC_REASON_LOST:
1496         pl->pid = NULL; /* in case we don't get it going again, this
1497                          * tells other logic not to try to kill it
1498                          */
1499         apr_proc_other_child_unregister(pl);
1500         stats = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
1501         if (stats != APR_SUCCESS) {
1502             ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1503                          "can't query MPM state; not restarting "
1504                          "piped log program '%s'",
1505                          pl->program);
1506         }
1507         else if (mpm_state != AP_MPMQ_STOPPING) {
1508             ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1509                          "piped log program '%s' failed unexpectedly",
1510                          pl->program);
1511             if ((stats = piped_log_spawn(pl)) != APR_SUCCESS) {
1512                 /* what can we do?  This could be the error log we're having
1513                  * problems opening up... */
1514                 char buf[120];
1515                 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1516                              "piped_log_maintenance: unable to respawn '%s': %s",
1517                              pl->program, apr_strerror(stats, buf, sizeof(buf)));
1518             }
1519         }
1520         break;
1521
1522     case APR_OC_REASON_UNWRITABLE:
1523         /* We should not kill off the pipe here, since it may only be full.
1524          * If it really is locked, we should kill it off manually. */
1525     break;
1526
1527     case APR_OC_REASON_RESTART:
1528         if (pl->pid != NULL) {
1529             apr_proc_kill(pl->pid, SIGTERM);
1530             pl->pid = NULL;
1531         }
1532         break;
1533
1534     case APR_OC_REASON_UNREGISTER:
1535         break;
1536     }
1537 }
1538
1539
1540 static apr_status_t piped_log_cleanup_for_exec(void *data)
1541 {
1542     piped_log *pl = data;
1543
1544     apr_file_close(pl->read_fd);
1545     apr_file_close(pl->write_fd);
1546     return APR_SUCCESS;
1547 }
1548
1549
1550 static apr_status_t piped_log_cleanup(void *data)
1551 {
1552     piped_log *pl = data;
1553
1554     if (pl->pid != NULL) {
1555         apr_proc_kill(pl->pid, SIGTERM);
1556     }
1557     return piped_log_cleanup_for_exec(data);
1558 }
1559
1560
1561 AP_DECLARE(piped_log *) ap_open_piped_log_ex(apr_pool_t *p,
1562                                              const char *program,
1563                                              apr_cmdtype_e cmdtype)
1564 {
1565     piped_log *pl;
1566
1567     pl = apr_palloc(p, sizeof (*pl));
1568     pl->p = p;
1569     pl->program = apr_pstrdup(p, program);
1570     pl->pid = NULL;
1571     pl->cmdtype = cmdtype;
1572     if (apr_file_pipe_create_ex(&pl->read_fd,
1573                                 &pl->write_fd,
1574                                 APR_FULL_BLOCK, p) != APR_SUCCESS) {
1575         return NULL;
1576     }
1577     apr_pool_cleanup_register(p, pl, piped_log_cleanup,
1578                               piped_log_cleanup_for_exec);
1579     if (piped_log_spawn(pl) != APR_SUCCESS) {
1580         apr_pool_cleanup_kill(p, pl, piped_log_cleanup);
1581         apr_file_close(pl->read_fd);
1582         apr_file_close(pl->write_fd);
1583         return NULL;
1584     }
1585     return pl;
1586 }
1587
1588 #else /* !AP_HAVE_RELIABLE_PIPED_LOGS */
1589
1590 static apr_status_t piped_log_cleanup(void *data)
1591 {
1592     piped_log *pl = data;
1593
1594     apr_file_close(pl->write_fd);
1595     return APR_SUCCESS;
1596 }
1597
1598 AP_DECLARE(piped_log *) ap_open_piped_log_ex(apr_pool_t *p,
1599                                              const char *program,
1600                                              apr_cmdtype_e cmdtype)
1601 {
1602     piped_log *pl;
1603     apr_file_t *dummy = NULL;
1604     int rc;
1605
1606     rc = log_child(p, program, &dummy, cmdtype, 0);
1607     if (rc != APR_SUCCESS) {
1608         ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
1609                      "Couldn't start piped log process '%s'.",
1610                      (program == NULL) ? "NULL" : program);
1611         return NULL;
1612     }
1613
1614     pl = apr_palloc(p, sizeof (*pl));
1615     pl->p = p;
1616     pl->read_fd = NULL;
1617     pl->write_fd = dummy;
1618     apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
1619
1620     return pl;
1621 }
1622
1623 #endif
1624
1625 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p,
1626                                           const char *program)
1627 {
1628     apr_cmdtype_e cmdtype = APR_PROGRAM_ENV;
1629
1630     /* In 2.4 favor PROGRAM_ENV, accept "||prog" syntax for compatibility
1631      * and "|$cmd" to override the default.
1632      * Any 2.2 backport would continue to favor SHELLCMD_ENV so there 
1633      * accept "||prog" to override, and "|$cmd" to ease conversion.
1634      */
1635     if (*program == '|')
1636         ++program;
1637     if (*program == '$') {
1638         cmdtype = APR_SHELLCMD_ENV;
1639         ++program;
1640     }
1641
1642     return ap_open_piped_log_ex(p, program, cmdtype);
1643 }
1644
1645 AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
1646 {
1647     apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
1648 }
1649
1650 AP_DECLARE(const char *) ap_parse_log_level(const char *str, int *val)
1651 {
1652     char *err = "Log level keyword must be one of emerg/alert/crit/error/warn/"
1653                 "notice/info/debug/trace1/.../trace8";
1654     int i = 0;
1655
1656     if (str == NULL)
1657         return err;
1658
1659     while (priorities[i].t_name != NULL) {
1660         if (!strcasecmp(str, priorities[i].t_name)) {
1661             *val = priorities[i].t_val;
1662             return NULL;
1663         }
1664         i++;
1665     }
1666     return err;
1667 }
1668
1669 AP_IMPLEMENT_HOOK_VOID(error_log,
1670                        (const char *file, int line, int module_index, int level,
1671                         apr_status_t status, const server_rec *s,
1672                         const request_rec *r, apr_pool_t *pool,
1673                         const char *errstr), (file, line, module_index, level,
1674                         status, s, r, pool, errstr))