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"
60 APR_HOOK_LINK(error_log)
63 int AP_DECLARE_DATA ap_default_loglevel = DEFAULT_LOGLEVEL;
67 static const TRANS facilities[] = {
70 {"authpriv",LOG_AUTHPRIV},
76 {"daemon", LOG_DAEMON},
94 {"syslog", LOG_SYSLOG},
103 {"local0", LOG_LOCAL0},
106 {"local1", LOG_LOCAL1},
109 {"local2", LOG_LOCAL2},
112 {"local3", LOG_LOCAL3},
115 {"local4", LOG_LOCAL4},
118 {"local5", LOG_LOCAL5},
121 {"local6", LOG_LOCAL6},
124 {"local7", LOG_LOCAL7},
130 static const TRANS priorities[] = {
131 {"emerg", APLOG_EMERG},
132 {"alert", APLOG_ALERT},
133 {"crit", APLOG_CRIT},
134 {"error", APLOG_ERR},
135 {"warn", APLOG_WARNING},
136 {"notice", APLOG_NOTICE},
137 {"info", APLOG_INFO},
138 {"debug", APLOG_DEBUG},
142 static apr_file_t *stderr_log = NULL;
144 /* track pipe handles to close in child process */
145 typedef struct read_handle_t {
146 struct read_handle_t *next;
150 static read_handle_t *read_handles;
152 /* clear_handle_list() is called when plog is cleared; at that
153 * point we need to forget about our old list of pipe read
156 static apr_status_t clear_handle_list(void *v)
162 /* remember to close this handle in the child process */
163 static void close_handle_in_child(apr_pool_t *p, apr_file_t *f)
165 read_handle_t *new_handle;
167 new_handle = apr_pcalloc(p, sizeof(read_handle_t));
168 new_handle->next = read_handles;
169 new_handle->handle = f;
170 read_handles = new_handle;
173 void ap_logs_child_init(apr_pool_t *p, server_rec *s)
175 read_handle_t *cur = read_handles;
178 apr_file_close(cur->handle);
183 AP_DECLARE(void) ap_open_stderr_log(apr_pool_t *p)
185 apr_file_open_stderr(&stderr_log, p);
188 AP_DECLARE(apr_status_t) ap_replace_stderr_log(apr_pool_t *p,
191 apr_file_t *stderr_file;
193 char *filename = ap_server_root_relative(p, fname);
195 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
196 APR_EBADPATH, NULL, "Invalid -E error log file %s",
200 if ((rc = apr_file_open(&stderr_file, filename,
201 APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
202 APR_OS_DEFAULT, p)) != APR_SUCCESS) {
203 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
204 "%s: could not open error log file %s.",
205 ap_server_argv0, fname);
208 if ((rc = apr_file_open_stderr(&stderr_log, p)) == APR_SUCCESS) {
209 apr_file_flush(stderr_log);
210 if ((rc = apr_file_dup2(stderr_log, stderr_file, p)) == APR_SUCCESS) {
211 apr_file_close(stderr_file);
214 if (rc != APR_SUCCESS) {
215 ap_log_error(APLOG_MARK, APLOG_CRIT, rc, NULL,
216 "unable to replace stderr with error log file");
221 static void log_child_errfn(apr_pool_t *pool, apr_status_t err,
222 const char *description)
224 ap_log_error(APLOG_MARK, APLOG_ERR, err, NULL,
228 /* Create a child process running PROGNAME with a pipe connected to
229 * the childs stdin. The write-end of the pipe will be placed in
230 * *FPIN on successful return. If dummy_stderr is non-zero, the
231 * stderr for the child will be the same as the stdout of the parent.
232 * Otherwise the child will inherit the stderr from the parent. */
233 static int log_child(apr_pool_t *p, const char *progname,
234 apr_file_t **fpin, int dummy_stderr)
236 /* Child process code for 'ErrorLog "|..."';
237 * may want a common framework for this, since I expect it will
238 * be common for other foo-loggers to want this sort of thing...
241 apr_procattr_t *procattr;
245 if (((rc = apr_procattr_create(&procattr, p)) == APR_SUCCESS)
246 && ((rc = apr_procattr_cmdtype_set(procattr,
247 APR_SHELLCMD_ENV)) == APR_SUCCESS)
248 && ((rc = apr_procattr_io_set(procattr,
251 APR_NO_PIPE)) == APR_SUCCESS)
252 && ((rc = apr_procattr_error_check_set(procattr, 1)) == APR_SUCCESS)
253 && ((rc = apr_procattr_child_errfn_set(procattr, log_child_errfn)) == APR_SUCCESS)
255 || ((rc = apr_file_open_stdout(&errfile, p)) == APR_SUCCESS
256 && (rc = apr_procattr_child_err_set(procattr,
258 errfile)) == APR_SUCCESS))) {
262 apr_tokenize_to_argv(progname, &args, p);
263 pname = apr_pstrdup(p, args[0]);
264 procnew = (apr_proc_t *)apr_pcalloc(p, sizeof(*procnew));
265 rc = apr_proc_create(procnew, pname, (const char * const *)args,
268 if (rc == APR_SUCCESS) {
269 apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
270 (*fpin) = procnew->in;
271 /* read handle to pipe not kept open, so no need to call
272 * close_handle_in_child()
276 /* apr_procattr_child_err_set dups errfile twice: close the
277 * remaining "parent-side" copy (apr_proc_create closes the
280 apr_file_close(procnew->err);
287 /* Open the error log for the given server_rec. If IS_MAIN is
288 * non-zero, s is the main server. */
289 static int open_error_log(server_rec *s, int is_main, apr_pool_t *p)
294 if (*s->error_fname == '|') {
295 apr_file_t *dummy = NULL;
297 /* Spawn a new child logger. If this is the main server_rec,
298 * the new child must use a dummy stderr since the current
299 * stderr might be a pipe to the old logger. Otherwise, the
300 * child inherits the parents stderr. */
301 rc = log_child(p, s->error_fname + 1, &dummy, is_main);
302 if (rc != APR_SUCCESS) {
303 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
304 "Couldn't start ErrorLog process");
308 s->error_log = dummy;
312 else if (!strncasecmp(s->error_fname, "syslog", 6)) {
313 if ((fname = strchr(s->error_fname, ':'))) {
317 for (fac = facilities; fac->t_name; fac++) {
318 if (!strcasecmp(fname, fac->t_name)) {
319 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID,
327 openlog(ap_server_argv0, LOG_NDELAY|LOG_CONS|LOG_PID, LOG_LOCAL7);
334 fname = ap_server_root_relative(p, s->error_fname);
336 ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EBADPATH, NULL,
337 "%s: Invalid error log path %s.",
338 ap_server_argv0, s->error_fname);
341 if ((rc = apr_file_open(&s->error_log, fname,
342 APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
343 APR_OS_DEFAULT, p)) != APR_SUCCESS) {
344 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
345 "%s: could not open error log file %s.",
346 ap_server_argv0, fname);
354 int ap_open_logs(apr_pool_t *pconf, apr_pool_t *p /* plog */,
355 apr_pool_t *ptemp, server_rec *s_main)
357 server_rec *virt, *q;
360 apr_pool_cleanup_register(p, NULL, clear_handle_list,
361 apr_pool_cleanup_null);
362 if (open_error_log(s_main, 1, p) != OK) {
367 if (s_main->error_log) {
370 /* Replace existing stderr with new log. */
371 apr_file_flush(s_main->error_log);
372 rv = apr_file_dup2(stderr_log, s_main->error_log, p);
373 if (rv != APR_SUCCESS) {
374 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s_main,
375 "unable to replace stderr with error_log");
381 /* note that stderr may still need to be replaced with something
382 * because it points to the old error log, or back to the tty
384 * XXX: This is BS - /dev/null is non-portable
386 if (replace_stderr && freopen("/dev/null", "w", stderr) == NULL) {
387 ap_log_error(APLOG_MARK, APLOG_CRIT, errno, s_main,
388 "unable to replace stderr with /dev/null");
391 for (virt = s_main->next; virt; virt = virt->next) {
392 if (virt->error_fname) {
393 for (q=s_main; q != virt; q = q->next) {
394 if (q->error_fname != NULL
395 && strcmp(q->error_fname, virt->error_fname) == 0) {
401 if (open_error_log(virt, 0, p) != OK) {
406 virt->error_log = q->error_log;
410 virt->error_log = s_main->error_log;
416 AP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
417 apr_file_t *errfile = NULL;
419 apr_file_open_stderr(&errfile, s->process->pool);
420 if (s->error_log != NULL) {
421 apr_file_dup2(s->error_log, errfile, s->process->pool);
425 static void log_error_core(const char *file, int line, int level,
426 apr_status_t status, const server_rec *s,
428 const request_rec *r, apr_pool_t *pool,
429 const char *fmt, va_list args)
431 char errstr[MAX_STRING_LEN];
432 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
433 char scratch[MAX_STRING_LEN];
435 apr_size_t len, errstrlen;
436 apr_file_t *logf = NULL;
438 int level_and_mask = level & APLOG_LEVELMASK;
440 if (r && r->connection) {
446 * If we are doing stderr logging (startup), don't log messages that are
447 * above the default server log level unless it is a startup/shutdown
450 if ((level_and_mask != APLOG_NOTICE)
451 && (level_and_mask > ap_default_loglevel)) {
457 else if (s->error_log) {
459 * If we are doing normal logging, don't log messages that are
460 * above the server log level unless it is a startup/shutdown notice
462 if ((level_and_mask != APLOG_NOTICE)
463 && (level_and_mask > s->loglevel)) {
470 else if (tpf_child) {
472 * If we are doing normal logging, don't log messages that are
473 * above the server log level unless it is a startup/shutdown notice
475 if ((level_and_mask != APLOG_NOTICE)
476 && (level_and_mask > s->loglevel)) {
485 * If we are doing syslog logging, don't log messages that are
486 * above the server log level (including a startup/shutdown notice)
488 if (level_and_mask > s->loglevel) {
493 if (logf && ((level & APLOG_STARTUP) != APLOG_STARTUP)) {
495 ap_recent_ctime(errstr + 1, apr_time_now());
496 errstr[1 + APR_CTIME_LEN - 1] = ']';
497 errstr[1 + APR_CTIME_LEN ] = ' ';
498 len = 1 + APR_CTIME_LEN + 1;
503 if ((level & APLOG_STARTUP) != APLOG_STARTUP) {
504 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
505 "[%s] ", priorities[level_and_mask].t_name);
509 if (file && level_and_mask == APLOG_DEBUG) {
510 #if defined(_OSD_POSIX) || defined(WIN32) || defined(__MVS__)
512 char *e = strrchr(file, '/');
515 e = strrchr(file, '\\');
519 /* In OSD/POSIX, the compiler returns for __FILE__
520 * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
521 * (it even returns an absolute path for sources in
522 * the current directory). Here we try to strip this
523 * down to the basename.
525 if (e != NULL && e[1] != '\0') {
526 apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
527 e = &tmp[strlen(tmp)-1];
533 #else /* _OSD_POSIX || WIN32 */
535 /* On Unix, __FILE__ may be an absolute path in a
537 if (file[0] == '/' && (p = ap_strrchr_c(file, '/')) != NULL) {
540 #endif /*_OSD_POSIX || WIN32 */
541 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
542 "%s(%d): ", file, line);
547 /* XXX: TODO: add a method of selecting whether logged client
548 * addresses are in dotted quad or resolved form... dotted
549 * quad is the most secure, which is why I'm implementing it
552 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
553 "[client %s] ", c->remote_ip);
556 if (status < APR_OS_START_EAIERR) {
557 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
560 else if (status < APR_OS_START_SYSERR) {
561 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
562 "(EAI %d)", status - APR_OS_START_EAIERR);
564 else if (status < 100000 + APR_OS_START_SYSERR) {
565 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
566 "(OS %d)", status - APR_OS_START_SYSERR);
569 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
570 "(os 0x%08x)", status - APR_OS_START_SYSERR);
572 apr_strerror(status, errstr + len, MAX_STRING_LEN - len);
573 len += strlen(errstr + len);
574 if (MAX_STRING_LEN - len > 2) {
582 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
583 if (apr_vsnprintf(scratch, MAX_STRING_LEN - len, fmt, args)) {
584 len += ap_escape_errorlog_item(errstr + len, scratch,
585 MAX_STRING_LEN - len);
588 len += apr_vsnprintf(errstr + len, MAX_STRING_LEN - len, fmt, args);
591 if ( r && (referer = apr_table_get(r->headers_in, "Referer"))
592 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
593 && ap_escape_errorlog_item(scratch, referer, MAX_STRING_LEN - len)
596 len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
598 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
606 /* NULL if we are logging to syslog */
608 /* Truncate for the terminator (as apr_snprintf does) */
609 if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR)) {
610 len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
612 strcpy(errstr + len, APR_EOL_STR);
613 apr_file_puts(errstr, logf);
614 apr_file_flush(logf);
618 syslog(level_and_mask, "%s", errstr);
622 ap_run_error_log(file, line, level, status, s, r, pool, errstr + errstrlen);
625 AP_DECLARE(void) ap_log_error(const char *file, int line, int level,
626 apr_status_t status, const server_rec *s,
627 const char *fmt, ...)
632 log_error_core(file, line, level, status, s, NULL, NULL, NULL, fmt, args);
636 AP_DECLARE(void) ap_log_perror(const char *file, int line, int level,
637 apr_status_t status, apr_pool_t *p,
638 const char *fmt, ...)
643 log_error_core(file, line, level, status, NULL, NULL, NULL, p, fmt, args);
647 AP_DECLARE(void) ap_log_rerror(const char *file, int line, int level,
648 apr_status_t status, const request_rec *r,
649 const char *fmt, ...)
654 log_error_core(file, line, level, status, r->server, NULL, r, NULL, fmt,
658 * IF APLOG_TOCLIENT is set,
659 * AND the error level is 'warning' or more severe,
660 * AND there isn't already error text associated with this request,
661 * THEN make the message text available to ErrorDocument and
662 * other error processors.
666 if ((level & APLOG_TOCLIENT)
667 && ((level & APLOG_LEVELMASK) <= APLOG_WARNING)
668 && (apr_table_get(r->notes, "error-notes") == NULL)) {
669 apr_table_setn(r->notes, "error-notes",
670 ap_escape_html(r->pool, apr_pvsprintf(r->pool, fmt,
676 AP_DECLARE(void) ap_log_cerror(const char *file, int line, int level,
677 apr_status_t status, const conn_rec *c,
678 const char *fmt, ...)
683 log_error_core(file, line, level, status, c->base_server, c, NULL, NULL,
688 AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
690 apr_file_t *pid_file = NULL;
692 static pid_t saved_pid = -1;
701 fname = ap_server_root_relative(p, filename);
703 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
704 NULL, "Invalid PID file path %s, ignoring.", filename);
709 if (mypid != saved_pid
710 && apr_stat(&finfo, fname, APR_FINFO_MTIME, p) == APR_SUCCESS) {
711 /* AP_SIG_GRACEFUL and HUP call this on each restart.
712 * Only warn on first time through for this pid.
714 * XXX: Could just write first time through too, although
715 * that may screw up scripts written to do something
716 * based on the last modification time of the pid file.
718 ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p,
719 "pid file %s overwritten -- Unclean "
720 "shutdown of previous Apache run?",
724 if ((rv = apr_file_open(&pid_file, fname,
725 APR_WRITE | APR_CREATE | APR_TRUNCATE,
726 APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p))
728 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
729 "could not create %s", fname);
730 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
731 "%s: could not log pid to file %s",
732 ap_server_argv0, fname);
735 apr_file_printf(pid_file, "%ld" APR_EOL_STR, (long)mypid);
736 apr_file_close(pid_file);
740 AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
743 const apr_size_t BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */
744 apr_file_t *pid_file = NULL;
748 apr_size_t bytes_read;
754 fname = ap_server_root_relative(p, filename);
756 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH,
757 NULL, "Invalid PID file path %s, ignoring.", filename);
761 rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p);
762 if (rv != APR_SUCCESS) {
766 buf = apr_palloc(p, BUFFER_SIZE);
768 rv = apr_file_read_full(pid_file, buf, BUFFER_SIZE - 1, &bytes_read);
769 if (rv != APR_SUCCESS && rv != APR_EOF) {
773 /* If we fill the buffer, we're probably reading a corrupt pid file.
774 * To be nice, let's also ensure the first char is a digit. */
775 if (bytes_read == 0 || bytes_read == BUFFER_SIZE - 1 || !apr_isdigit(*buf)) {
779 buf[bytes_read] = '\0';
780 *mypid = strtol(buf, &endptr, 10);
782 apr_file_close(pid_file);
786 AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile,
789 char time_str[APR_CTIME_LEN];
791 apr_ctime(time_str, apr_time_now());
792 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
793 "[%s] file %s, line %d, assertion \"%s\" failed",
794 time_str, szFile, nLine, szExp);
798 /* unix assert does an abort leading to a core dump */
803 /* piped log support */
805 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
806 /* forward declaration */
807 static void piped_log_maintenance(int reason, void *data, apr_wait_t status);
809 /* Spawn the piped logger process pl->program. */
810 static apr_status_t piped_log_spawn(piped_log *pl)
812 apr_procattr_t *procattr;
813 apr_proc_t *procnew = NULL;
816 if (((status = apr_procattr_create(&procattr, pl->p)) != APR_SUCCESS) ||
817 ((status = apr_procattr_cmdtype_set(procattr,
818 APR_SHELLCMD_ENV)) != APR_SUCCESS) ||
819 ((status = apr_procattr_child_in_set(procattr,
820 ap_piped_log_read_fd(pl),
821 ap_piped_log_write_fd(pl)))
823 ((status = apr_procattr_child_errfn_set(procattr, log_child_errfn))
825 ((status = apr_procattr_error_check_set(procattr, 1)) != APR_SUCCESS)) {
827 /* Something bad happened, give up and go away. */
828 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
829 "piped_log_spawn: unable to setup child process '%s': %s",
830 pl->program, apr_strerror(status, buf, sizeof(buf)));
836 apr_tokenize_to_argv(pl->program, &args, pl->p);
837 pname = apr_pstrdup(pl->p, args[0]);
838 procnew = apr_pcalloc(pl->p, sizeof(apr_proc_t));
839 status = apr_proc_create(procnew, pname, (const char * const *) args,
840 NULL, procattr, pl->p);
842 if (status == APR_SUCCESS) {
844 /* procnew->in was dup2'd from ap_piped_log_write_fd(pl);
845 * since the original fd is still valid, close the copy to
847 apr_file_close(procnew->in);
849 apr_proc_other_child_register(procnew, piped_log_maintenance, pl,
850 ap_piped_log_write_fd(pl), pl->p);
851 close_handle_in_child(pl->p, ap_piped_log_read_fd(pl));
855 /* Something bad happened, give up and go away. */
856 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
857 "unable to start piped log program '%s': %s",
858 pl->program, apr_strerror(status, buf, sizeof(buf)));
866 static void piped_log_maintenance(int reason, void *data, apr_wait_t status)
868 piped_log *pl = data;
873 case APR_OC_REASON_DEATH:
874 case APR_OC_REASON_LOST:
875 pl->pid = NULL; /* in case we don't get it going again, this
876 * tells other logic not to try to kill it
878 apr_proc_other_child_unregister(pl);
879 stats = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
880 if (stats != APR_SUCCESS) {
881 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
882 "can't query MPM state; not restarting "
883 "piped log program '%s'",
886 else if (mpm_state != AP_MPMQ_STOPPING) {
887 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
888 "piped log program '%s' failed unexpectedly",
890 if ((stats = piped_log_spawn(pl)) != APR_SUCCESS) {
891 /* what can we do? This could be the error log we're having
892 * problems opening up... */
894 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
895 "piped_log_maintenance: unable to respawn '%s': %s",
896 pl->program, apr_strerror(stats, buf, sizeof(buf)));
901 case APR_OC_REASON_UNWRITABLE:
902 /* We should not kill off the pipe here, since it may only be full.
903 * If it really is locked, we should kill it off manually. */
906 case APR_OC_REASON_RESTART:
907 if (pl->pid != NULL) {
908 apr_proc_kill(pl->pid, SIGTERM);
913 case APR_OC_REASON_UNREGISTER:
919 static apr_status_t piped_log_cleanup_for_exec(void *data)
921 piped_log *pl = data;
923 apr_file_close(ap_piped_log_read_fd(pl));
924 apr_file_close(ap_piped_log_write_fd(pl));
929 static apr_status_t piped_log_cleanup(void *data)
931 piped_log *pl = data;
933 if (pl->pid != NULL) {
934 apr_proc_kill(pl->pid, SIGTERM);
936 return piped_log_cleanup_for_exec(data);
940 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
944 pl = apr_palloc(p, sizeof (*pl));
946 pl->program = apr_pstrdup(p, program);
948 if (apr_file_pipe_create(&ap_piped_log_read_fd(pl),
949 &ap_piped_log_write_fd(pl), p) != APR_SUCCESS) {
952 apr_pool_cleanup_register(p, pl, piped_log_cleanup,
953 piped_log_cleanup_for_exec);
954 if (piped_log_spawn(pl) != APR_SUCCESS) {
955 apr_pool_cleanup_kill(p, pl, piped_log_cleanup);
956 apr_file_close(ap_piped_log_read_fd(pl));
957 apr_file_close(ap_piped_log_write_fd(pl));
963 #else /* !AP_HAVE_RELIABLE_PIPED_LOGS */
965 static apr_status_t piped_log_cleanup(void *data)
967 piped_log *pl = data;
969 apr_file_close(ap_piped_log_write_fd(pl));
973 AP_DECLARE(piped_log *) ap_open_piped_log(apr_pool_t *p, const char *program)
976 apr_file_t *dummy = NULL;
979 rc = log_child(p, program, &dummy, 0);
980 if (rc != APR_SUCCESS) {
981 ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, NULL,
982 "Couldn't start piped log process");
986 pl = apr_palloc(p, sizeof (*pl));
988 ap_piped_log_read_fd(pl) = NULL;
989 ap_piped_log_write_fd(pl) = dummy;
990 apr_pool_cleanup_register(p, pl, piped_log_cleanup, piped_log_cleanup);
997 AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
999 apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
1002 AP_IMPLEMENT_HOOK_VOID(error_log,
1003 (const char *file, int line, int level,
1004 apr_status_t status, const server_rec *s,
1005 const request_rec *r, apr_pool_t *pool,
1006 const char *errstr), (file, line, level,
1007 status, s, r, pool, errstr))