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"
32 #define APR_WANT_STDIO
33 #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"
55 #define APR_LARGEFILE 0
64 APR_HOOK_LINK(error_log)
67 int AP_DECLARE_DATA ap_default_loglevel = DEFAULT_LOGLEVEL;
71 static const TRANS facilities[] = {
74 {"authpriv",LOG_AUTHPRIV},
80 {"daemon", LOG_DAEMON},
98 {"syslog", LOG_SYSLOG},
107 {"local0", LOG_LOCAL0},
110 {"local1", LOG_LOCAL1},
113 {"local2", LOG_LOCAL2},
116 {"local3", LOG_LOCAL3},
119 {"local4", LOG_LOCAL4},
122 {"local5", LOG_LOCAL5},
125 {"local6", LOG_LOCAL6},
128 {"local7", LOG_LOCAL7},
134 static const TRANS priorities[] = {
135 {"emerg", APLOG_EMERG},
136 {"alert", APLOG_ALERT},
137 {"crit", APLOG_CRIT},
138 {"error", APLOG_ERR},
139 {"warn", APLOG_WARNING},
140 {"notice", APLOG_NOTICE},
141 {"info", APLOG_INFO},
142 {"debug", APLOG_DEBUG},
146 static apr_pool_t *stderr_pool = NULL;
148 static apr_file_t *stderr_log = NULL;
150 /* track pipe handles to close in child process */
151 typedef struct read_handle_t {
152 struct read_handle_t *next;
156 static read_handle_t *read_handles;
158 /* clear_handle_list() is called when plog is cleared; at that
159 * point we need to forget about our old list of pipe read
160 * handles. We let the plog cleanups close the actual pipes.
162 static apr_status_t clear_handle_list(void *v)
168 /* remember to close this handle in the child process
170 * On Win32 this makes zero sense, because we don't
171 * take the parent process's child procs.
172 * If the win32 parent instead passed each and every
173 * logger write handle from itself down to the child,
174 * and the parent manages all aspects of keeping the
175 * reliable pipe log children alive, this would still
176 * make no sense :) Cripple it on Win32.
178 static void close_handle_in_child(apr_pool_t *p, apr_file_t *f)
181 read_handle_t *new_handle;
183 new_handle = apr_pcalloc(p, sizeof(read_handle_t));
184 new_handle->next = read_handles;
185 new_handle->handle = f;
186 read_handles = new_handle;
190 void ap_logs_child_init(apr_pool_t *p, server_rec *s)
192 read_handle_t *cur = read_handles;
195 apr_file_close(cur->handle);
200 AP_DECLARE(void) ap_open_stderr_log(apr_pool_t *p)
202 apr_file_open_stderr(&stderr_log, p);
205 AP_DECLARE(apr_status_t) ap_replace_stderr_log(apr_pool_t *p,
208 apr_file_t *stderr_file;
210 char *filename = ap_server_root_relative(p, fname);
212 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
213 APR_EBADPATH, NULL, "Invalid -E error log file %s",
217 if ((rc = apr_file_open(&stderr_file, filename,
218 APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
219 APR_OS_DEFAULT, p)) != APR_SUCCESS) {
220 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
221 "%s: could not open error log file %s.",
222 ap_server_argv0, fname);
226 /* This is safe provided we revert it when we are finished.
227 * We don't manager the callers pool!
231 if ((rc = apr_file_open_stderr(&stderr_log, stderr_pool))
233 apr_file_flush(stderr_log);
234 if ((rc = apr_file_dup2(stderr_log, stderr_file, stderr_pool))
236 apr_file_close(stderr_file);
238 * You might ponder why stderr_pool should survive?
239 * The trouble is, stderr_pool may have s_main->error_log,
240 * so we aren't in a position to destory stderr_pool until
241 * the next recycle. There's also an apparent bug which
242 * is not; if some folk decided to call this function before
243 * the core open error logs hook, this pool won't survive.
244 * Neither does the stderr logger, so this isn't a problem.
248 /* Revert, see above */
249 if (stderr_pool == p)
252 if (rc != APR_SUCCESS) {
253 ap_log_error(APLOG_MARK, APLOG_CRIT, rc, NULL,
254 "unable to replace stderr with error log file");
259 static void log_child_errfn(apr_pool_t *pool, apr_status_t err,
260 const char *description)
262 ap_log_error(APLOG_MARK, APLOG_ERR, err, NULL,
266 /* Create a child process running PROGNAME with a pipe connected to
267 * the childs stdin. The write-end of the pipe will be placed in
268 * *FPIN on successful return. If dummy_stderr is non-zero, the
269 * stderr for the child will be the same as the stdout of the parent.
270 * Otherwise the child will inherit the stderr from the parent. */
271 static int log_child(apr_pool_t *p, const char *progname,
272 apr_file_t **fpin, int dummy_stderr)
274 /* Child process code for 'ErrorLog "|..."';
275 * may want a common framework for this, since I expect it will
276 * be common for other foo-loggers to want this sort of thing...
279 apr_procattr_t *procattr;
281 apr_file_t *outfile, *errfile;
283 if (((rc = apr_procattr_create(&procattr, p)) == APR_SUCCESS)
284 && ((rc = apr_procattr_io_set(procattr,
287 APR_NO_PIPE)) == APR_SUCCESS)
288 && ((rc = apr_procattr_error_check_set(procattr, 1)) == APR_SUCCESS)
289 && ((rc = apr_procattr_child_errfn_set(procattr, log_child_errfn))
294 apr_tokenize_to_argv(progname, &args, p);
295 pname = apr_pstrdup(p, args[0]);
296 procnew = (apr_proc_t *)apr_pcalloc(p, sizeof(*procnew));
298 if ((rc = apr_file_open_stdout(&outfile, p)) == APR_SUCCESS) {
299 rc = apr_procattr_child_out_set(procattr, outfile, NULL);
301 rc = apr_procattr_child_err_set(procattr, outfile, NULL);
302 else if ((rc = apr_file_open_stderr(&errfile, p)) == APR_SUCCESS)
303 rc = apr_procattr_child_err_set(procattr, errfile, NULL);
306 rc = apr_proc_create(procnew, pname, (const char * const *)args,
309 if (rc == APR_SUCCESS) {
310 apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
311 (*fpin) = procnew->in;
312 /* read handle to pipe not kept open, so no need to call
313 * close_handle_in_child()
321 /* Open the error log for the given server_rec. If IS_MAIN is
322 * non-zero, s is the main server. */
323 static int open_error_log(server_rec *s, int is_main, apr_pool_t *p)
328 if (*s->error_fname == '|') {
329 apr_file_t *dummy = NULL;
331 /* Spawn a new child logger. If this is the main server_rec,
332 * the new child must use a dummy stderr since the current
333 * stderr might be a pipe to the old logger. Otherwise, the
334 * child inherits the parents stderr. */
335 rc = log_child(p, s->error_fname + 1, &dummy, is_main);
336 if (rc != APR_SUCCESS) {
337 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
338 "Couldn't start ErrorLog process");
342 s->error_log = dummy;
346 else if (!strncasecmp(s->error_fname, "syslog", 6)) {
347 if ((fname = strchr(s->error_fname, ':'))) {
351 for (fac = facilities; fac->t_name; fac++) {
352 if (!strcasecmp(fname, fac->t_name)) {
353 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID,
361 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
368 fname = ap_server_root_relative(p, s->error_fname);
370 ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EBADPATH, NULL,
371 "%s: Invalid error log path %s.",
372 ap_server_argv0, s->error_fname);
375 if ((rc = apr_file_open(&s->error_log, fname,
376 APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
377 APR_OS_DEFAULT, p)) != APR_SUCCESS) {
378 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
379 "%s: could not open error log file %s.",
380 ap_server_argv0, fname);
388 int ap_open_logs(apr_pool_t *pconf, apr_pool_t *p /* plog */,
389 apr_pool_t *ptemp, server_rec *s_main)
391 apr_pool_t *stderr_p;
392 server_rec *virt, *q;
396 /* Register to throw away the read_handles list when we
397 * cleanup plog. Upon fork() for the apache children,
398 * this read_handles list is closed so only the parent
399 * can relaunch a lost log child. These read handles
400 * are always closed on exec.
401 * We won't care what happens to our stderr log child
402 * between log phases, so we don't mind losing stderr's
403 * read_handle a little bit early.
405 apr_pool_cleanup_register(p, NULL, clear_handle_list,
406 apr_pool_cleanup_null);
408 /* HERE we need a stdout log that outlives plog.
409 * We *presume* the parent of plog is a process
410 * or global pool which spans server restarts.
411 * Create our stderr_pool as a child of the plog's
414 apr_pool_create(&stderr_p, apr_pool_parent_get(p));
415 apr_pool_tag(stderr_p, "stderr_pool");
417 if (open_error_log(s_main, 1, stderr_p) != OK) {
422 if (s_main->error_log) {
425 /* Replace existing stderr with new log. */
426 apr_file_flush(s_main->error_log);
427 rv = apr_file_dup2(stderr_log, s_main->error_log, stderr_p);
428 if (rv != APR_SUCCESS) {
429 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s_main,
430 "unable to replace stderr with error_log");
433 /* We are done with stderr_pool, close it, killing
434 * the previous generation's stderr logger
437 apr_pool_destroy(stderr_pool);
438 stderr_pool = stderr_p;
441 * Now that we have dup'ed s_main->error_log to stderr_log
442 * close it and set s_main->error_log to stderr_log. This avoids
443 * this fd being inherited by the next piped logger who would
444 * keep open the writing end of the pipe that this one uses
445 * as stdin. This in turn would prevent the piped logger from
448 apr_file_close(s_main->error_log);
449 s_main->error_log = stderr_log;
452 /* note that stderr may still need to be replaced with something
453 * because it points to the old error log, or back to the tty
455 * XXX: This is BS - /dev/null is non-portable
457 if (replace_stderr && freopen("/dev/null", "w", stderr) == NULL) {
458 ap_log_error(APLOG_MARK, APLOG_CRIT, errno, s_main,
459 "unable to replace stderr with /dev/null");
462 for (virt = s_main->next; virt; virt = virt->next) {
463 if (virt->error_fname) {
464 for (q=s_main; q != virt; q = q->next) {
465 if (q->error_fname != NULL
466 && strcmp(q->error_fname, virt->error_fname) == 0) {
472 if (open_error_log(virt, 0, p) != OK) {
477 virt->error_log = q->error_log;
481 virt->error_log = s_main->error_log;
487 AP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
488 apr_file_t *errfile = NULL;
490 apr_file_open_stderr(&errfile, s->process->pool);
491 if (s->error_log != NULL) {
492 apr_file_dup2(s->error_log, errfile, s->process->pool);
496 static void log_error_core(const char *file, int line, int level,
497 apr_status_t status, const server_rec *s,
499 const request_rec *r, apr_pool_t *pool,
500 const char *fmt, va_list args)
502 char errstr[MAX_STRING_LEN];
503 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
504 char scratch[MAX_STRING_LEN];
506 apr_size_t len, errstrlen;
507 apr_file_t *logf = NULL;
509 int level_and_mask = level & APLOG_LEVELMASK;
511 if (r && r->connection) {
517 * If we are doing stderr logging (startup), don't log messages that are
518 * above the default server log level unless it is a startup/shutdown
521 if ((level_and_mask != APLOG_NOTICE)
522 && (level_and_mask > ap_default_loglevel)) {
528 else if (s->error_log) {
530 * If we are doing normal logging, don't log messages that are
531 * above the server log level unless it is a startup/shutdown notice
533 if ((level_and_mask != APLOG_NOTICE)
534 && (level_and_mask > s->loglevel)) {
541 else if (tpf_child) {
543 * If we are doing normal logging, don't log messages that are
544 * above the server log level unless it is a startup/shutdown notice
546 if ((level_and_mask != APLOG_NOTICE)
547 && (level_and_mask > s->loglevel)) {
556 * If we are doing syslog logging, don't log messages that are
557 * above the server log level (including a startup/shutdown notice)
559 if (level_and_mask > s->loglevel) {
564 if (logf && ((level & APLOG_STARTUP) != APLOG_STARTUP)) {
566 ap_recent_ctime(errstr + 1, apr_time_now());
567 errstr[1 + APR_CTIME_LEN - 1] = ']';
568 errstr[1 + APR_CTIME_LEN ] = ' ';
569 len = 1 + APR_CTIME_LEN + 1;
574 if ((level & APLOG_STARTUP) != APLOG_STARTUP) {
575 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
576 "[%s] ", priorities[level_and_mask].t_name);
580 if (file && level_and_mask == APLOG_DEBUG) {
581 #if defined(_OSD_POSIX) || defined(WIN32) || defined(__MVS__)
583 char *e = strrchr(file, '/');
586 e = strrchr(file, '\\');
590 /* In OSD/POSIX, the compiler returns for __FILE__
591 * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
592 * (it even returns an absolute path for sources in
593 * the current directory). Here we try to strip this
594 * down to the basename.
596 if (e != NULL && e[1] != '\0') {
597 apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
598 e = &tmp[strlen(tmp)-1];
604 #else /* _OSD_POSIX || WIN32 */
606 /* On Unix, __FILE__ may be an absolute path in a
608 if (file[0] == '/' && (p = ap_strrchr_c(file, '/')) != NULL) {
611 #endif /*_OSD_POSIX || WIN32 */
612 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
613 "%s(%d): ", file, line);
618 /* XXX: TODO: add a method of selecting whether logged client
619 * addresses are in dotted quad or resolved form... dotted
620 * quad is the most secure, which is why I'm implementing it
623 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
624 "[client %s] ", c->remote_ip);
627 if (status < APR_OS_START_EAIERR) {
628 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
631 else if (status < APR_OS_START_SYSERR) {
632 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
633 "(EAI %d)", status - APR_OS_START_EAIERR);
635 else if (status < 100000 + APR_OS_START_SYSERR) {
636 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
637 "(OS %d)", status - APR_OS_START_SYSERR);
640 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
641 "(os 0x%08x)", status - APR_OS_START_SYSERR);
643 apr_strerror(status, errstr + len, MAX_STRING_LEN - len);
644 len += strlen(errstr + len);
645 if (MAX_STRING_LEN - len > 2) {
653 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
654 if (apr_vsnprintf(scratch, MAX_STRING_LEN - len, fmt, args)) {
655 len += ap_escape_errorlog_item(errstr + len, scratch,
656 MAX_STRING_LEN - len);
659 len += apr_vsnprintf(errstr + len, MAX_STRING_LEN - len, fmt, args);
662 if ( r && (referer = apr_table_get(r->headers_in, "Referer"))
663 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
664 && ap_escape_errorlog_item(scratch, referer, MAX_STRING_LEN - len)
667 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
669 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
677 /* NULL if we are logging to syslog */
679 /* Truncate for the terminator (as apr_snprintf does) */
680 if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR)) {
681 len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
683 strcpy(errstr + len, APR_EOL_STR);
684 apr_file_puts(errstr, logf);
685 apr_file_flush(logf);
689 syslog(level_and_mask, "%s", errstr);
693 ap_run_error_log(file, line, level, status, s, r, pool, errstr + errstrlen);
696 AP_DECLARE(void) ap_log_error(const char *file, int line, int level,
697 apr_status_t status, const server_rec *s,
698 const char *fmt, ...)
703 log_error_core(file, line, level, status, s, NULL, NULL, NULL, fmt, args);
707 AP_DECLARE(void) ap_log_perror(const char *file, int line, int level,
708 apr_status_t status, apr_pool_t *p,
709 const char *fmt, ...)
714 log_error_core(file, line, level, status, NULL, NULL, NULL, p, fmt, args);
718 AP_DECLARE(void) ap_log_rerror(const char *file, int line, int level,
719 apr_status_t status, const request_rec *r,
720 const char *fmt, ...)
725 log_error_core(file, line, level, status, r->server, NULL, r, NULL, fmt,
729 * IF APLOG_TOCLIENT is set,
730 * AND the error level is 'warning' or more severe,
731 * AND there isn't already error text associated with this request,
732 * THEN make the message text available to ErrorDocument and
733 * other error processors.
737 if ((level & APLOG_TOCLIENT)
738 && ((level & APLOG_LEVELMASK) <= APLOG_WARNING)
739 && (apr_table_get(r->notes, "error-notes") == NULL)) {
740 apr_table_setn(r->notes, "error-notes",
741 ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
747 AP_DECLARE(void) ap_log_cerror(const char *file, int line, int level,
748 apr_status_t status, const conn_rec *c,
749 const char *fmt, ...)
754 log_error_core(file, line, level, status, c->base_server, c, NULL, NULL,
759 AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
761 apr_file_t *pid_file = NULL;
763 static pid_t saved_pid = -1;
772 fname = ap_server_root_relative(p, filename);
774 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
775 NULL, "Invalid PID file path %s, ignoring.", filename);
780 if (mypid != saved_pid
781 && apr_stat(&finfo, fname, APR_FINFO_MTIME, p) == APR_SUCCESS) {
782 /* AP_SIG_GRACEFUL and HUP call this on each restart.
783 * Only warn on first time through for this pid.
785 * XXX: Could just write first time through too, although
786 * that may screw up scripts written to do something
787 * based on the last modification time of the pid file.
789 ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p,
790 apr_psprintf(p, "pid file %s overwritten -- Unclean "
791 "shutdown of previous Apache run?",
795 if ((rv = apr_file_open(&pid_file, fname,
796 APR_WRITE | APR_CREATE | APR_TRUNCATE,
797 APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p))
799 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
800 "could not create %s", fname);
801 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
802 "%s: could not log pid to file %s",
803 ap_server_argv0, fname);
806 apr_file_printf(pid_file, "%ld" APR_EOL_STR, (long)mypid);
807 apr_file_close(pid_file);
811 AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
814 const apr_size_t BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */
815 apr_file_t *pid_file = NULL;
819 apr_size_t bytes_read;
825 fname = ap_server_root_relative(p, filename);
827 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
828 NULL, "Invalid PID file path %s, ignoring.", filename);
832 rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p);
833 if (rv != APR_SUCCESS) {
837 /* Ensure null-termination, so that strtol doesn't go crazy. */
838 buf = apr_palloc(p, BUFFER_SIZE);
839 buf[BUFFER_SIZE - 1] = '\0';
841 rv = apr_file_read_full(pid_file, buf, BUFFER_SIZE - 1, &bytes_read);
842 if (rv != APR_SUCCESS && rv != APR_EOF) {
846 /* If we fill the buffer, we're probably reading a corrupt pid file.
847 * To be nice, let's also ensure the first char is a digit. */
848 if (bytes_read == BUFFER_SIZE - 1 || !apr_isdigit(*buf)) {
852 *mypid = strtol(buf, &endptr, 10);
854 apr_file_close(pid_file);
858 AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile,
861 char time_str[APR_CTIME_LEN];
863 apr_ctime(time_str, apr_time_now());
864 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
865 "[%s] file %s, line %d, assertion \"%s\" failed",
866 time_str, szFile, nLine, szExp);
870 /* unix assert does an abort leading to a core dump */
875 /* piped log support */
877 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
878 /* forward declaration */
879 static void piped_log_maintenance(int reason, void *data, apr_wait_t status);
881 /* Spawn the piped logger process pl->program. */
882 static apr_status_t piped_log_spawn(piped_log *pl)
884 apr_procattr_t *procattr;
885 apr_proc_t *procnew = NULL;
888 if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
889 ((status = apr_procattr_child_in_set(procattr,
890 ap_piped_log_read_fd(pl),
891 ap_piped_log_write_fd(pl)))
893 ((status = apr_procattr_child_errfn_set(procattr, log_child_errfn))
895 ((status = apr_procattr_error_check_set(procattr, 1)) != APR_SUCCESS)) {
897 /* Something bad happened, give up and go away. */
898 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
899 "piped_log_spawn: unable to setup child process '%s': %s",
900 pl->program, apr_strerror(status, buf, sizeof(buf)));
905 apr_file_t *outfile, *errfile;
907 if ((status = apr_file_open_stdout(&outfile, pl->p)) == APR_SUCCESS)
908 status = apr_procattr_child_out_set(procattr, outfile, NULL);
909 if ((status = apr_file_open_stderr(&errfile, pl->p)) == APR_SUCCESS)
910 status = apr_procattr_child_err_set(procattr, errfile, NULL);
912 apr_tokenize_to_argv(pl->program, &args, pl->p);
913 pname = apr_pstrdup(pl->p, args[0]);
914 procnew = apr_pcalloc(pl->p, sizeof(apr_proc_t));
915 status = apr_proc_create(procnew, pname, (const char * const *) args,
916 NULL, procattr, pl->p);
918 if (status == APR_SUCCESS) {
920 /* procnew->in was dup2'd from ap_piped_log_write_fd(pl);
921 * since the original fd is still valid, close the copy to
923 apr_file_close(procnew->in);
925 apr_proc_other_child_register(procnew, piped_log_maintenance, pl,
926 ap_piped_log_write_fd(pl), pl->p);
927 close_handle_in_child(pl->p, ap_piped_log_read_fd(pl));
931 /* Something bad happened, give up and go away. */
932 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
933 "unable to start piped log program '%s': %s",
934 pl->program, apr_strerror(status, buf, sizeof(buf)));
942 static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
944 piped_log *pl = data;
949 case APR_OC_REASON_DEATH:
950 case APR_OC_REASON_LOST:
951 pl->pid = NULL; /* in case we don't get it going again, this
952 * tells other logic not to try to kill it
954 apr_proc_other_child_unregister(pl);
955 stats = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
956 if (stats != APR_SUCCESS) {
957 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
958 "can't query MPM state; not restarting "
959 "piped log program '%s'",
962 else if (mpm_state != AP_MPMQ_STOPPING) {
963 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
964 "piped log program '%s' failed unexpectedly",
966 if ((stats = piped_log_spawn(pl)) != APR_SUCCESS) {
967 /* what can we do? This could be the error log we're having
968 * problems opening up... */
970 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
971 "piped_log_maintenance: unable to respawn '%s': %s",
972 pl->program, apr_strerror(stats, buf, sizeof(buf)));
977 case APR_OC_REASON_UNWRITABLE:
978 /* We should not kill off the pipe here, since it may only be full.
979 * If it really is locked, we should kill it off manually. */
982 case APR_OC_REASON_RESTART:
983 if (pl->pid != NULL) {
984 apr_proc_kill(pl->pid, SIGTERM);
989 case APR_OC_REASON_UNREGISTER:
995 static apr_status_t piped_log_cleanup_for_exec(void *data)
997 piped_log *pl = data;
999 apr_file_close(ap_piped_log_read_fd(pl));
1000 apr_file_close(ap_piped_log_write_fd(pl));
1005 static apr_status_t piped_log_cleanup(void *data)
1007 piped_log *pl = data;
1009 if (pl->pid != NULL) {
1010 apr_proc_kill(pl->pid, SIGTERM);
1012 return piped_log_cleanup_for_exec(data);
1016 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
1020 pl = apr_palloc(p, sizeof (*pl));
1022 pl->program = apr_pstrdup(p, program);
1024 if (apr_file_pipe_create(&ap_piped_log_read_fd(pl),
1025 &ap_piped_log_write_fd(pl), p) != APR_SUCCESS) {
1028 apr_pool_cleanup_register(p, pl, piped_log_cleanup,
1029 piped_log_cleanup_for_exec);
1030 if (piped_log_spawn(pl) != APR_SUCCESS) {
1031 apr_pool_cleanup_kill(p, pl, piped_log_cleanup);
1032 apr_file_close(ap_piped_log_read_fd(pl));
1033 apr_file_close(ap_piped_log_write_fd(pl));
1039 #else /* !AP_HAVE_RELIABLE_PIPED_LOGS */
1041 static apr_status_t piped_log_cleanup(void *data)
1043 piped_log *pl = data;
1045 apr_file_close(ap_piped_log_write_fd(pl));
1049 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
1052 apr_file_t *dummy = NULL;
1055 rc = log_child(p, program, &dummy, 0);
1056 if (rc != APR_SUCCESS) {
1057 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
1058 "Couldn't start piped log process");
1062 pl = apr_palloc(p, sizeof (*pl));
1064 ap_piped_log_read_fd(pl) = NULL;
1065 ap_piped_log_write_fd(pl) = dummy;
1066 apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
1073 AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
1075 apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
1078 AP_IMPLEMENT_HOOK_VOID(error_log,
1079 (const char *file, int line, int level,
1080 apr_status_t status, const server_rec *s,
1081 const request_rec *r, apr_pool_t *pool,
1082 const char *errstr), (file, line, level,
1083 status, s, r, pool, errstr))