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