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