1 |
/* |
2 |
* Hydra, an http server |
3 |
* Copyright (C) 1995 Paul Phillips <paulp@go2net.com> |
4 |
* Some changes Copyright (C) 1996 Larry Doolittle <ldoolitt@boa.org> |
5 |
* Some changes Copyright (C) 1999 Jon Nelson <jnelson@boa.org> |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 1, or (at your option) |
10 |
* any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
* |
21 |
*/ |
22 |
|
23 |
/* $Id: log.c,v 1.12 2002/10/29 11:52:53 nmav Exp $ */ |
24 |
|
25 |
#include "boa.h" |
26 |
|
27 |
extern char *error_log_name; |
28 |
extern char *access_log_name; |
29 |
extern char *cgi_log_name; |
30 |
int cgi_log_fd; |
31 |
|
32 |
FILE * |
33 |
fopen_gen_fd(char *spec, const char *mode); |
34 |
|
35 |
FILE * |
36 |
fopen_gen_fd(char *spec, const char *mode) |
37 |
{ |
38 |
int fd; |
39 |
if (!spec || *spec == '\0') |
40 |
return NULL; |
41 |
if ((fd = open_gen_fd(spec)) == -1) |
42 |
return NULL; |
43 |
return fdopen(fd, mode); |
44 |
} /* fopen_gen_fd() */ |
45 |
|
46 |
/* |
47 |
* Name: open_logs |
48 |
* |
49 |
* Description: Opens access log, error log, and if specified, cgi log |
50 |
* Ties stderr to error log, except during cgi execution, at which |
51 |
* time cgi log is the stderr for cgis. |
52 |
* |
53 |
* Access log is line buffered, error log is not buffered. |
54 |
* |
55 |
*/ |
56 |
|
57 |
void |
58 |
open_logs(void) |
59 |
{ |
60 |
int access_log; |
61 |
|
62 |
/* |
63 |
* if error_log_name is set, dup2 stderr to it |
64 |
* otherwise leave stderr alone |
65 |
* we don't want to tie stderr to /dev/null |
66 |
*/ |
67 |
if (error_log_name) { |
68 |
int error_log; |
69 |
|
70 |
/* open the log file */ |
71 |
if (!(error_log = open_gen_fd(error_log_name))) { |
72 |
DIE("unable to open error log"); |
73 |
} |
74 |
|
75 |
/* redirect stderr to error_log */ |
76 |
if (dup2(error_log, STDERR_FILENO) == -1) { |
77 |
DIE("unable to dup2 the error log"); |
78 |
} |
79 |
close(error_log); |
80 |
} |
81 |
|
82 |
/* set the close-on-exec to true */ |
83 |
if (access_log_name) { |
84 |
access_log = open_gen_fd(access_log_name); |
85 |
} else { |
86 |
access_log = open("/dev/null", 0); |
87 |
} |
88 |
|
89 |
if (access_log < 0) { |
90 |
DIE("unable to open access log"); |
91 |
} |
92 |
if (dup2(access_log, STDOUT_FILENO) == -1) { |
93 |
DIE("can't dup2 /dev/null to STDOUT_FILENO"); |
94 |
} |
95 |
close(access_log); |
96 |
|
97 |
if (cgi_log_name) { |
98 |
cgi_log_fd = open_gen_fd(cgi_log_name); |
99 |
if (cgi_log_fd == -1) { |
100 |
WARN("open cgi_log"); |
101 |
free(cgi_log_name); |
102 |
cgi_log_name = NULL; |
103 |
cgi_log_fd = -1; |
104 |
} else { |
105 |
if (set_cloexec_fd(cgi_log_fd) == -1) { |
106 |
WARN("unable to set close-on-exec flag for cgi_log"); |
107 |
free(cgi_log_name); |
108 |
cgi_log_name = NULL; |
109 |
close(cgi_log_fd); |
110 |
cgi_log_fd = -1; |
111 |
} |
112 |
} |
113 |
} |
114 |
|
115 |
#ifdef SETVBUF_REVERSED |
116 |
setvbuf(stderr, _IONBF, (char *) NULL, 0); |
117 |
setvbuf(stdout, _IOLBF, (char *) NULL, 0); |
118 |
#else |
119 |
setvbuf(stderr, (char *) NULL, _IONBF, 0); |
120 |
setvbuf(stdout, (char *) NULL, _IOLBF, 0); |
121 |
#endif |
122 |
|
123 |
} /* open_logs() */ |
124 |
|
125 |
/* |
126 |
* Name: log_access |
127 |
* |
128 |
* Description: Writes log data to access_log. |
129 |
*/ |
130 |
|
131 |
/* NOTES on the commonlog format: |
132 |
* Taken from notes on the NetBuddy program |
133 |
* http://www.computer-dynamics.com/commonlog.html |
134 |
* |
135 |
* remotehost |
136 |
* |
137 |
* remotehost rfc931 authuser [date] "request" status bytes |
138 |
* |
139 |
* remotehost - IP of the client |
140 |
* rfc931 - remote name of the user (always '-') |
141 |
* authuser - username entered for authentication - almost always '-' |
142 |
* [date] - the date in [08/Nov/1997:01:05:03 -0600] (with brackets) format |
143 |
* "request" - literal request from the client (boa may clean this up, |
144 |
* replacing control-characters with '_' perhaps - NOTE: not done) |
145 |
* status - http status code |
146 |
* bytes - number of bytes transferred |
147 |
* |
148 |
* boa appends: |
149 |
* referer |
150 |
* user-agent |
151 |
* |
152 |
* and may prepend (depending on configuration): |
153 |
* virtualhost - the name or IP (depending on whether name-based |
154 |
* virtualhosting is enabled) of the host the client accessed |
155 |
*/ |
156 |
|
157 |
void |
158 |
log_access(request *req) |
159 |
{ |
160 |
char buf[30]; |
161 |
|
162 |
if (!access_log_name) |
163 |
return; |
164 |
|
165 |
if (req->hostname && req->hostname[0]!=0) { |
166 |
printf("%s ", req->hostname); |
167 |
} else { |
168 |
printf( "unknown "); |
169 |
} |
170 |
|
171 |
get_commonlog_time(buf); |
172 |
#ifndef USE_LONG_OFFSETS |
173 |
printf("%s - - %s\"%s\" %d %ld \"%s\" \"%s\"\n", |
174 |
#else |
175 |
printf("%s - - %s\"%s\" %d %lld \"%s\" \"%s\"\n", |
176 |
#endif |
177 |
req->remote_ip_addr, |
178 |
buf, |
179 |
req->logline, |
180 |
req->response_status, |
181 |
req->filepos, |
182 |
(req->header_referer ? req->header_referer : "-"), |
183 |
(req->header_user_agent ? req->header_user_agent : "-")); |
184 |
} /* log_access() */ |
185 |
|
186 |
/* |
187 |
* Name: log_error_doc |
188 |
* |
189 |
* Description: Logs the current time and transaction identification |
190 |
* to the stderr (the error log): |
191 |
* should always be followed by an fprintf to stderr |
192 |
* |
193 |
* This function used to be implemented with a big fprintf, but not |
194 |
* all fprintf's are reliable in the face of null string pointers |
195 |
* (SunOS, in particular). As long as I had to add the checks for |
196 |
* null pointers, I changed from fprintf to fputs. |
197 |
* |
198 |
* Example output: |
199 |
* www.testserver.com [08/Nov/1997:01:05:03 -0600] request 192.228.331.232 "GET /~joeblow/dir/ HTTP/1.0" ("/usr/user1/joeblow/public_html/dir/"): write: Broken pipe |
200 |
* |
201 |
* Apache uses: |
202 |
* [Wed Oct 11 14:32:52 2000] [error] [client 127.0.0.1] client denied by server configuration: /export/home/live/ap/htdocs/test |
203 |
*/ |
204 |
|
205 |
void |
206 |
log_error_doc(request *req) |
207 |
{ |
208 |
int errno_save = errno; /* it's a push-pop thingie; we don't want to alter errno. */ |
209 |
char buf[30]; |
210 |
|
211 |
if (req->hostname && req->hostname[0]!=0) { |
212 |
fprintf(stderr, "%s ", req->hostname); |
213 |
} else { |
214 |
fprintf(stderr, "unknown "); |
215 |
} |
216 |
|
217 |
get_commonlog_time(buf); |
218 |
fprintf(stderr, "%s - - %srequest \"%s\" (\"%s\"): ", |
219 |
req->remote_ip_addr, |
220 |
buf, |
221 |
(req->logline != NULL ? req->logline : ""), |
222 |
(req->pathname != NULL ? req->pathname : "")); |
223 |
errno = errno_save; |
224 |
} /* log_error_doc() */ |
225 |
|
226 |
/* |
227 |
* Name: boa_perror |
228 |
* |
229 |
* Description: logs an error to user and error file both |
230 |
* |
231 |
*/ |
232 |
void boa_perror(request * req, char *message) |
233 |
{ |
234 |
log_error_doc(req); |
235 |
perror(message); /* don't need to save errno because log_error_doc does */ |
236 |
send_r_error(req); |
237 |
} |
238 |
|
239 |
/* |
240 |
* Name: log_error_time |
241 |
* |
242 |
* Description: Logs the current time to the stderr (the error log): |
243 |
* should always be followed by an fprintf to stderr |
244 |
*/ |
245 |
|
246 |
void |
247 |
log_error_time() |
248 |
{ |
249 |
int errno_save = errno; |
250 |
char buf[30]; |
251 |
|
252 |
get_commonlog_time(buf); |
253 |
fputs(buf, stderr); |
254 |
errno = errno_save; |
255 |
} /* log_error_time() */ |
256 |
|
257 |
/* |
258 |
* Name: log_error_mesg |
259 |
* |
260 |
* Description: performs a log_error_time, writes the file and lineno |
261 |
* to stderr (saving errno), and then a perror with message |
262 |
* |
263 |
*/ |
264 |
|
265 |
void |
266 |
log_error_mesg(char *file, int line, char *mesg) |
267 |
{ |
268 |
int errno_save = errno; |
269 |
char buf[30]; |
270 |
|
271 |
get_commonlog_time( buf); |
272 |
fprintf(stderr, "%s%s:%d - ", buf, file, line); |
273 |
errno = errno_save; |
274 |
perror(mesg); |
275 |
errno = errno_save; |
276 |
} /* log_error_mesg() */ |