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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * http_log.c: Dealing with the logs and errors
25 #include "apr_general.h" /* for signal stuff */
26 #include "apr_strings.h"
27 #include "apr_errno.h"
28 #include "apr_thread_proc.h"
30 #include "apr_signal.h"
31 #include "apr_portable.h"
32 #include "apr_base64.h"
34 #define APR_WANT_STDIO
35 #define APR_WANT_STRFUNC
45 #include "ap_config.h"
47 #include "http_config.h"
48 #include "http_core.h"
50 #include "http_main.h"
51 #include "util_time.h"
54 APLOG_USE_MODULE(core);
62 APR_HOOK_LINK(error_log)
65 int AP_DECLARE_DATA ap_default_loglevel = DEFAULT_LOGLEVEL;
69 static const TRANS facilities[] = {
72 {"authpriv",LOG_AUTHPRIV},
78 {"daemon", LOG_DAEMON},
96 {"syslog", LOG_SYSLOG},
105 {"local0", LOG_LOCAL0},
108 {"local1", LOG_LOCAL1},
111 {"local2", LOG_LOCAL2},
114 {"local3", LOG_LOCAL3},
117 {"local4", LOG_LOCAL4},
120 {"local5", LOG_LOCAL5},
123 {"local6", LOG_LOCAL6},
126 {"local7", LOG_LOCAL7},
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},
152 static apr_pool_t *stderr_pool = NULL;
154 static apr_file_t *stderr_log = NULL;
156 /* track pipe handles to close in child process */
157 typedef struct read_handle_t {
158 struct read_handle_t *next;
162 static read_handle_t *read_handles;
165 * @brief The piped logging structure.
167 * Piped logs are used to move functionality out of the main server.
168 * For example, log rotation is done with piped logs.
171 /** The pool to use for the piped log */
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 */
178 /** The pid of the logging process */
180 /** How to reinvoke program when it must be replaced */
181 apr_cmdtype_e cmdtype;
185 AP_DECLARE(apr_file_t *) ap_piped_log_read_fd(piped_log *pl)
190 AP_DECLARE(apr_file_t *) ap_piped_log_write_fd(piped_log *pl)
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.
199 static apr_status_t clear_handle_list(void *v)
205 /* remember to close this handle in the child process
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.
215 static void close_handle_in_child(apr_pool_t *p, apr_file_t *f)
218 read_handle_t *new_handle;
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;
227 void ap_logs_child_init(apr_pool_t *p, server_rec *s)
229 read_handle_t *cur = read_handles;
232 apr_file_close(cur->handle);
237 AP_DECLARE(void) ap_open_stderr_log(apr_pool_t *p)
239 apr_file_open_stderr(&stderr_log, p);
242 AP_DECLARE(apr_status_t) ap_replace_stderr_log(apr_pool_t *p,
245 apr_file_t *stderr_file;
247 char *filename = ap_server_root_relative(p, fname);
249 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
250 APR_EBADPATH, NULL, "Invalid -E error log file %s",
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);
263 /* This is safe provided we revert it when we are finished.
264 * We don't manager the callers pool!
268 if ((rc = apr_file_open_stderr(&stderr_log, stderr_pool))
270 apr_file_flush(stderr_log);
271 if ((rc = apr_file_dup2(stderr_log, stderr_file, stderr_pool))
273 apr_file_close(stderr_file);
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.
285 /* Revert, see above */
286 if (stderr_pool == p)
289 if (rc != APR_SUCCESS) {
290 ap_log_error(APLOG_MARK, APLOG_CRIT, rc, NULL,
291 "unable to replace stderr with error log file");
296 static void log_child_errfn(apr_pool_t *pool, apr_status_t err,
297 const char *description)
299 ap_log_error(APLOG_MARK, APLOG_ERR, err, NULL,
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,
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...
317 apr_procattr_t *procattr;
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,
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))
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));
340 if ((rc = apr_file_open_stdout(&errfile, p)) == APR_SUCCESS)
341 rc = apr_procattr_child_err_set(procattr, errfile, NULL);
344 rc = apr_proc_create(procnew, pname, (const char * const *)args,
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()
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)
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;
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.
379 cmdtype = APR_SHELLCMD_ENV;
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'.",
395 s->error_log = dummy;
399 else if (!strncasecmp(s->error_fname, "syslog", 6)) {
400 if ((fname = strchr(s->error_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,
414 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
421 fname = ap_server_root_relative(p, s->error_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);
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);
441 int ap_open_logs(apr_pool_t *pconf, apr_pool_t *p /* plog */,
442 apr_pool_t *ptemp, server_rec *s_main)
444 apr_pool_t *stderr_p;
445 server_rec *virt, *q;
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.
458 apr_pool_cleanup_register(p, NULL, clear_handle_list,
459 apr_pool_cleanup_null);
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
467 apr_pool_create(&stderr_p, apr_pool_parent_get(p));
468 apr_pool_tag(stderr_p, "stderr_pool");
470 if (open_error_log(s_main, 1, stderr_p) != OK) {
475 if (s_main->error_log) {
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");
486 /* We are done with stderr_pool, close it, killing
487 * the previous generation's stderr logger
490 apr_pool_destroy(stderr_pool);
491 stderr_pool = stderr_p;
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
501 apr_file_close(s_main->error_log);
502 s_main->error_log = stderr_log;
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
508 * XXX: This is BS - /dev/null is non-portable
509 * errno-as-apr_status_t is also non-portable
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");
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) {
526 if (open_error_log(virt, 0, p) != OK) {
531 virt->error_log = q->error_log;
535 virt->error_log = s_main->error_log;
541 AP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
542 apr_file_t *errfile = NULL;
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);
550 static int cpystrn(char *buf, const char *arg, int buflen)
555 end = apr_cpystrn(buf, arg, buflen);
560 static int log_remote_address(const ap_errorlog_info *info, const char *arg,
561 char *buf, int buflen)
564 return apr_snprintf(buf, buflen, "%s:%d", info->c->remote_ip,
565 info->c->remote_addr->port);
570 static int log_local_address(const ap_errorlog_info *info, const char *arg,
571 char *buf, int buflen)
574 return apr_snprintf(buf, buflen, "%s:%d", info->c->local_ip,
575 info->c->local_addr->port);
580 static int log_pid(const ap_errorlog_info *info, const char *arg,
581 char *buf, int buflen)
583 pid_t pid = getpid();
584 return apr_snprintf(buf, buflen, "%" APR_PID_T_FMT, pid);
587 static int log_tid(const ap_errorlog_info *info, const char *arg,
588 char *buf, int buflen)
593 if (ap_mpm_query(AP_MPMQ_IS_THREADED, &result) == APR_SUCCESS
594 && result != AP_MPMQ_NOT_SUPPORTED)
596 apr_os_thread_t tid = apr_os_thread_current();
597 return apr_snprintf(buf, buflen, "%pT", &tid);
603 static int log_ctime(const ap_errorlog_info *info, const char *arg,
604 char *buf, int buflen)
606 int time_len = buflen;
607 int option = AP_CTIME_OPTION_NONE;
611 case 'u': option |= AP_CTIME_OPTION_USEC;
613 case 'c': option |= AP_CTIME_OPTION_COMPACT;
619 ap_recent_ctime_ex(buf, apr_time_now(), option, &time_len);
621 /* ap_recent_ctime_ex includes the trailing \0 in time_len */
625 static int log_loglevel(const ap_errorlog_info *info, const char *arg,
626 char *buf, int buflen)
631 return cpystrn(buf, priorities[info->level].t_name, buflen);
634 static int log_log_id(const ap_errorlog_info *info, const char *arg,
635 char *buf, int buflen)
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
642 if (arg && !strcasecmp(arg, "c")) {
643 if (info->c && (*arg != 'C' || !info->r)) {
644 return cpystrn(buf, info->c->log_id, buflen);
648 return cpystrn(buf, info->r->log_id, buflen);
653 static int log_keepalives(const ap_errorlog_info *info, const char *arg,
654 char *buf, int buflen)
659 return apr_snprintf(buf, buflen, "%d", info->c->keepalives);
662 static int log_module_name(const ap_errorlog_info *info, const char *arg,
663 char *buf, int buflen)
665 return cpystrn(buf, ap_find_module_short_name(info->module_index), buflen);
668 static int log_file_line(const ap_errorlog_info *info, const char *arg,
669 char *buf, int buflen)
671 if (info->file == NULL) {
675 const char *file = info->file;
676 #if defined(_OSD_POSIX) || defined(WIN32) || defined(__MVS__)
678 char *e = strrchr(file, '/');
681 e = strrchr(file, '\\');
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.
691 if (e != NULL && e[1] != '\0') {
692 apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
693 e = &tmp[strlen(tmp)-1];
699 #else /* _OSD_POSIX || WIN32 */
701 /* On Unix, __FILE__ may be an absolute path in a
703 if (file[0] == '/' && (p = ap_strrchr_c(file, '/')) != NULL) {
706 #endif /*_OSD_POSIX || WIN32 */
707 return apr_snprintf(buf, buflen, "%s(%d)", file, info->line);
711 static int log_apr_status(const ap_errorlog_info *info, const char *arg,
712 char *buf, int buflen)
714 apr_status_t status = info->status;
719 if (status < APR_OS_START_EAIERR) {
720 len = apr_snprintf(buf, buflen, "(%d)", status);
722 else if (status < APR_OS_START_SYSERR) {
723 len = apr_snprintf(buf, buflen, "(EAI %d)",
724 status - APR_OS_START_EAIERR);
726 else if (status < 100000 + APR_OS_START_SYSERR) {
727 len = apr_snprintf(buf, buflen, "(OS %d)",
728 status - APR_OS_START_SYSERR);
731 len = apr_snprintf(buf, buflen, "(os 0x%08x)",
732 status - APR_OS_START_SYSERR);
734 apr_strerror(status, buf + len, buflen - len);
735 len += strlen(buf + len);
739 static int log_header(const ap_errorlog_info *info, const char *arg,
740 char *buf, int buflen)
744 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
745 char scratch[MAX_STRING_LEN];
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)
754 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
767 AP_DECLARE(void) ap_register_builtin_errorlog_handlers(apr_pool_t *p)
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);
782 /* XXX: TODO: envvars, notes */
785 static void add_log_id(const conn_rec *c, const request_rec *r)
787 apr_uint64_t id, tmp;
792 if (r && r->request_time) {
793 id = (apr_uint64_t)r->request_time;
796 id = (apr_uint64_t)apr_time_now();
800 if (sizeof(pid_t) > 2) {
816 apr_uintptr_t tmp2 = (apr_uintptr_t)c->current_thread;
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));
827 /* Skip the last char, it is always '=' */
828 encoded[len - 2] = '\0';
830 /* need to cast const away */
832 ((request_rec *)r)->log_id = encoded;
835 ((conn_rec *)c)->log_id = encoded;
840 * This is used if no error log format is defined and during startup.
841 * It automatically omits the timestamp if logging to syslog.
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)
850 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
851 char scratch[MAX_STRING_LEN];
854 if (!info->using_syslog && !info->startup) {
856 len += log_ctime(info, "u", buf + len, buflen - len);
861 if (!info->startup) {
863 len += log_module_name(info, NULL, buf + len, buflen - len);
865 len += log_loglevel(info, NULL, buf + len, buflen - len);
866 len += cpystrn(buf + len, "] [pid ", buflen - len);
868 len += log_pid(info, NULL, buf + len, buflen - len);
871 len += cpystrn(buf + len, ":tid ", buflen - len);
872 item_len = log_tid(info, NULL, buf + len, buflen - len);
882 if (info->level >= APLOG_DEBUG) {
883 item_len = log_file_line(info, NULL, buf + len, buflen - len);
886 len += cpystrn(buf + len, ": ", buflen - len);
891 item_len = log_apr_status(info, NULL, buf + len, buflen - len);
894 len += cpystrn(buf + len, ": ", buflen - len);
900 * remote_ip can be client or backend server. If we have a scoreboard
901 * handle, it is likely a client.
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);
908 /* the actual error message */
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,
917 len += apr_vsnprintf(buf + len, buflen - len, errstr_fmt, args);
922 len += cpystrn(buf + len, ", referer: ", buflen - len);
923 item_len = log_header(info, "Referer", buf + len, buflen - len);
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)
936 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
937 char scratch[MAX_STRING_LEN];
943 ap_errorlog_format_item *items = (ap_errorlog_format_item *)fmt->elts;
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) {
956 if (item->flags & AP_ERRORLOG_FLAG_MESSAGE) {
957 /* the actual error message */
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,
966 len += apr_vsnprintf(buf + len, buflen - len, err_fmt, args);
974 int item_len = (*item->func)(info, item->arg, buf + len,
977 if (item->flags & AP_ERRORLOG_FLAG_REQUIRED) {
978 /* required item is empty. skip whole line */
982 else if (item->flags & AP_ERRORLOG_FLAG_NULL_AS_HYPHEN) {
998 static void write_logline(char *errstr, apr_size_t len, apr_file_t *logf,
1001 /* NULL if we are logging to syslog */
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);
1007 strcpy(errstr + len, APR_EOL_STR);
1008 apr_file_puts(errstr, logf);
1009 apr_file_flush(logf);
1013 syslog(level < LOG_PRIMASK ? level : APLOG_DEBUG, "%s", errstr);
1018 static void log_error_core(const char *file, int line, int module_index,
1020 apr_status_t status, const server_rec *s,
1022 const request_rec *r, apr_pool_t *pool,
1023 const char *fmt, va_list args)
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;
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;
1036 int line_number = 0;
1038 if (r && r->connection) {
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
1049 if ((level_and_mask != APLOG_NOTICE)
1050 && (level_and_mask > ap_default_loglevel)) {
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);
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
1066 if ((level_and_mask != APLOG_NOTICE)
1067 && (level_and_mask > configured_level)) {
1071 logf = s->error_log;
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)
1078 if (level_and_mask > configured_level) {
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)
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)
1100 * XXX: potential optimization: only create log id if %L is
1101 * XXX: actually used
1103 add_log_id(c, rmain);
1112 info.status = status;
1113 info.using_syslog = (logf == NULL);
1114 info.startup = ((level & APLOG_STARTUP) == APLOG_STARTUP);
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;
1128 info.module_index = APLOG_NO_MODULE;
1131 log_format = lines[line_number++];
1133 if (line_number == sconf->error_log_conn->nelts) {
1134 /* this is the last line of once-per-connection info */
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;
1146 info.module_index = APLOG_NO_MODULE;
1149 log_format = lines[line_number++];
1151 if (line_number == sconf->error_log_req->nelts) {
1152 /* this is the last line of once-per-request info */
1158 /* the actual error message */
1161 info.level = level_and_mask;
1162 info.module_index = module_index;
1163 log_format = sconf ? sconf->error_log_format : NULL;
1168 * prepare and log one line
1172 len += do_errorlog_format(log_format, &info, errstr + len,
1173 MAX_STRING_LEN - len,
1174 &errstr_start, &errstr_end, fmt, args);
1177 len += do_errorlog_default(&info, errstr + len, MAX_STRING_LEN - len,
1178 &errstr_start, &errstr_end, fmt, args);
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.
1189 write_logline(errstr, len, logf, level_and_mask);
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);
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, ...)
1208 va_start(args, fmt);
1209 log_error_core(file, line, module_index, level, status, s, NULL, NULL,
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, ...)
1220 va_start(args, fmt);
1221 log_error_core(file, line, module_index, level, status, NULL, NULL, NULL,
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, ...)
1232 va_start(args, fmt);
1233 log_error_core(file, line, module_index, level, status, r->server, NULL, r,
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.
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,
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, ...)
1262 va_start(args, fmt);
1263 log_error_core(file, line, module_index, level, status, s, c,
1264 NULL, NULL, fmt, args);
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, ...)
1274 va_start(args, fmt);
1275 log_error_core(file, line, module_index, level, status, c->base_server, c,
1276 NULL, NULL, fmt, args);
1280 AP_DECLARE(void) ap_log_command_line(apr_pool_t *plog, server_rec *s)
1283 process_rec *process = s->process;
1287 /* Piece together the command line from the pieces
1288 * in process->argv, with spaces in between.
1290 for (i = 0; i < process->argc; i++) {
1291 len_needed += strlen(process->argv[i]) + 1;
1294 result = (char *) apr_palloc(plog, len_needed);
1297 for (i = 0; i < process->argc; i++) {
1298 strcat(result, process->argv[i]);
1299 if ((i+1)< process->argc) {
1300 strcat(result, " ");
1303 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
1304 "Command line: '%s'", result);
1307 AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
1309 apr_file_t *pid_file = NULL;
1311 static pid_t saved_pid = -1;
1320 fname = ap_server_root_relative(p, filename);
1322 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
1323 NULL, "Invalid PID file path %s, ignoring.", filename);
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.
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.
1337 ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p,
1338 "pid file %s overwritten -- Unclean "
1339 "shutdown of previous Apache run?",
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))
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);
1354 apr_file_printf(pid_file, "%" APR_PID_T_FMT APR_EOL_STR, mypid);
1355 apr_file_close(pid_file);
1359 AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
1362 const apr_size_t BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */
1363 apr_file_t *pid_file = NULL;
1367 apr_size_t bytes_read;
1370 return APR_EGENERAL;
1373 fname = ap_server_root_relative(p, filename);
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;
1380 rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p);
1381 if (rv != APR_SUCCESS) {
1385 buf = apr_palloc(p, BUFFER_SIZE);
1387 rv = apr_file_read_full(pid_file, buf, BUFFER_SIZE - 1, &bytes_read);
1388 if (rv != APR_SUCCESS && rv != APR_EOF) {
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;
1398 buf[bytes_read] = '\0';
1399 *mypid = strtol(buf, &endptr, 10);
1401 apr_file_close(pid_file);
1405 AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile,
1408 char time_str[APR_CTIME_LEN];
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);
1417 /* unix assert does an abort leading to a core dump */
1422 /* piped log support */
1424 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
1425 /* forward declaration */
1426 static void piped_log_maintenance(int reason, void *data, apr_wait_t status);
1428 /* Spawn the piped logger process pl->program. */
1429 static apr_status_t piped_log_spawn(piped_log *pl)
1431 apr_procattr_t *procattr;
1432 apr_proc_t *procnew = NULL;
1433 apr_status_t status;
1435 if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
1436 ((status = apr_procattr_dir_set(procattr, ap_server_root))
1438 ((status = apr_procattr_cmdtype_set(procattr, pl->cmdtype))
1440 ((status = apr_procattr_child_in_set(procattr,
1444 ((status = apr_procattr_child_errfn_set(procattr, log_child_errfn))
1446 ((status = apr_procattr_error_check_set(procattr, 1)) != APR_SUCCESS)) {
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)));
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);
1463 if (status == APR_SUCCESS) {
1465 /* procnew->in was dup2'd from pl->write_fd;
1466 * since the original fd is still valid, close the copy to
1468 apr_file_close(procnew->in);
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);
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)));
1487 static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
1489 piped_log *pl = data;
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
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'",
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",
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... */
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)));
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. */
1527 case APR_OC_REASON_RESTART:
1528 if (pl->pid != NULL) {
1529 apr_proc_kill(pl->pid, SIGTERM);
1534 case APR_OC_REASON_UNREGISTER:
1540 static apr_status_t piped_log_cleanup_for_exec(void *data)
1542 piped_log *pl = data;
1544 apr_file_close(pl->read_fd);
1545 apr_file_close(pl->write_fd);
1550 static apr_status_t piped_log_cleanup(void *data)
1552 piped_log *pl = data;
1554 if (pl->pid != NULL) {
1555 apr_proc_kill(pl->pid, SIGTERM);
1557 return piped_log_cleanup_for_exec(data);
1561 AP_DECLARE(piped_log *) ap_open_piped_log_ex(apr_pool_t *p,
1562 const char *program,
1563 apr_cmdtype_e cmdtype)
1567 pl = apr_palloc(p, sizeof (*pl));
1569 pl->program = apr_pstrdup(p, program);
1571 pl->cmdtype = cmdtype;
1572 if (apr_file_pipe_create_ex(&pl->read_fd,
1574 APR_FULL_BLOCK, p) != APR_SUCCESS) {
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);
1588 #else /* !AP_HAVE_RELIABLE_PIPED_LOGS */
1590 static apr_status_t piped_log_cleanup(void *data)
1592 piped_log *pl = data;
1594 apr_file_close(pl->write_fd);
1598 AP_DECLARE(piped_log *) ap_open_piped_log_ex(apr_pool_t *p,
1599 const char *program,
1600 apr_cmdtype_e cmdtype)
1603 apr_file_t *dummy = NULL;
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);
1614 pl = apr_palloc(p, sizeof (*pl));
1617 pl->write_fd = dummy;
1618 apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
1625 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p,
1626 const char *program)
1628 apr_cmdtype_e cmdtype = APR_PROGRAM_ENV;
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.
1635 if (*program == '|')
1637 if (*program == '$') {
1638 cmdtype = APR_SHELLCMD_ENV;
1642 return ap_open_piped_log_ex(p, program, cmdtype);
1645 AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
1647 apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
1650 AP_DECLARE(const char *) ap_parse_log_level(const char *str, int *val)
1652 char *err = "Log level keyword must be one of emerg/alert/crit/error/warn/"
1653 "notice/info/debug/trace1/.../trace8";
1659 while (priorities[i].t_name != NULL) {
1660 if (!strcasecmp(str, priorities[i].t_name)) {
1661 *val = priorities[i].t_val;
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))