1 |
/* |
2 |
* Boa, 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.1.1.1 2002/09/21 13:53:23 nmav Exp $*/ |
24 |
|
25 |
#include "boa.h" |
26 |
|
27 |
FILE *access_log; |
28 |
|
29 |
char *error_log_name; |
30 |
char *access_log_name; |
31 |
char *cgi_log_name; |
32 |
int cgi_log_fd; |
33 |
|
34 |
FILE *fopen_gen_fd(char *spec, const char *mode); |
35 |
|
36 |
FILE *fopen_gen_fd(char *spec, const char *mode) |
37 |
{ |
38 |
int fd; |
39 |
if (!spec || *spec == '\0') |
40 |
return NULL; |
41 |
fd = open_gen_fd(spec); |
42 |
if (fd == -1) |
43 |
return NULL; |
44 |
return fdopen(fd, mode); |
45 |
} |
46 |
|
47 |
/* |
48 |
* Name: open_logs |
49 |
* |
50 |
* Description: Opens access log, error log, and if specified, cgi log |
51 |
* Ties stderr to error log, except during cgi execution, at which |
52 |
* time cgi log is the stderr for cgis. |
53 |
* |
54 |
* Access log is line buffered, error log is not buffered. |
55 |
* |
56 |
*/ |
57 |
|
58 |
void open_logs(void) |
59 |
{ |
60 |
int error_log; |
61 |
|
62 |
/* if error_log_name is set, dup2 stderr to it */ |
63 |
/* otherwise, leave stderr alone */ |
64 |
/* we don't want to tie stderr to /dev/null */ |
65 |
if (error_log_name) { |
66 |
/* open the log file */ |
67 |
if (!(error_log = open_gen_fd(error_log_name))) { |
68 |
DIE("unable to open error log"); |
69 |
} |
70 |
|
71 |
/* redirect stderr to error_log */ |
72 |
if (dup2(error_log, STDERR_FILENO) == -1) { |
73 |
DIE("unable to dup2 the error log"); |
74 |
} |
75 |
close(error_log); |
76 |
} |
77 |
|
78 |
/* set the close-on-exec to true */ |
79 |
if (fcntl(STDERR_FILENO, F_SETFD, 1) == -1) { |
80 |
DIE("unable to fcntl the error log"); |
81 |
} |
82 |
|
83 |
if (access_log_name) { |
84 |
/* Used the "a" flag with fopen, but fopen_gen_fd builds that in |
85 |
* implicitly when used as a file, and "a" is incompatible with |
86 |
* pipes and network sockets. */ |
87 |
if (!(access_log = fopen_gen_fd(access_log_name, "w"))) { |
88 |
int errno_save = errno; |
89 |
fprintf(stderr, "Cannot open %s for logging: ", |
90 |
access_log_name); |
91 |
errno = errno_save; |
92 |
perror("logfile open"); |
93 |
exit(errno); |
94 |
} |
95 |
/* line buffer the access log */ |
96 |
#ifdef SETVBUF_REVERSED |
97 |
setvbuf(access_log, _IOLBF, (char *) NULL, 0); |
98 |
#else |
99 |
setvbuf(access_log, (char *) NULL, _IOLBF, 0); |
100 |
#endif |
101 |
} else |
102 |
access_log = NULL; |
103 |
|
104 |
if (cgi_log_name) { |
105 |
cgi_log_fd = open_gen_fd(cgi_log_name); |
106 |
if (cgi_log_fd == -1) { |
107 |
WARN("open cgi_log"); |
108 |
free(cgi_log_name); |
109 |
cgi_log_name = NULL; |
110 |
cgi_log_fd = 0; |
111 |
} else { |
112 |
if (fcntl(cgi_log_fd, F_SETFD, 1) == -1) { |
113 |
WARN("unable to set close-on-exec flag for cgi_log"); |
114 |
close(cgi_log_fd); |
115 |
cgi_log_fd = 0; |
116 |
free(cgi_log_name); |
117 |
cgi_log_name = NULL; |
118 |
} |
119 |
} |
120 |
} |
121 |
} |
122 |
|
123 |
/* |
124 |
* Name: close_access_log |
125 |
* |
126 |
* Description: closes access_log file |
127 |
*/ |
128 |
void close_access_log(void) |
129 |
{ |
130 |
if (access_log) |
131 |
fclose(access_log); |
132 |
} |
133 |
|
134 |
/* |
135 |
* Name: log_access |
136 |
* |
137 |
* Description: Writes log data to access_log. |
138 |
*/ |
139 |
|
140 |
void log_access(request * req) |
141 |
{ |
142 |
if (access_log) { |
143 |
char buf[30]; |
144 |
|
145 |
get_commonlog_time( buf); |
146 |
if (virtualhost) |
147 |
fprintf(access_log, "%s ", req->local_ip_addr); |
148 |
fprintf(access_log, "%s - - %s\"%s\" %d %ld \"%s\" \"%s\"\n", |
149 |
req->remote_ip_addr, |
150 |
buf, |
151 |
req->logline, |
152 |
req->response_status, |
153 |
req->filepos, |
154 |
(req->header_referer ? req->header_referer : "-"), |
155 |
(req->header_user_agent ? req->header_user_agent : "-")); |
156 |
|
157 |
} |
158 |
} |
159 |
|
160 |
/* |
161 |
* Name: log_error_doc |
162 |
* |
163 |
* Description: Logs the current time and transaction identification |
164 |
* to the stderr (the error log): |
165 |
* should always be followed by an fprintf to stderr |
166 |
* |
167 |
* This function used to be implemented with a big fprintf, but not |
168 |
* all fprintf's are reliable in the face of null string pointers |
169 |
* (SunOS, in particular). As long as I had to add the checks for |
170 |
* null pointers, I changed from fprintf to fputs. |
171 |
* |
172 |
* Example output: |
173 |
[08/Nov/1997:01:05:03 -0600] request from 192.228.331.232 "GET /~joeblow/dir/ HTTP/1.0" ("/usr/user1/joeblow/public_html/dir/"): write: Broken pipe |
174 |
*/ |
175 |
|
176 |
void log_error_doc(request * req) |
177 |
{ |
178 |
int errno_save = errno; |
179 |
char buf[30]; |
180 |
|
181 |
get_commonlog_time(buf); |
182 |
fprintf(stderr, "%srequest from %s \"%s\" (\"%s\"): ", |
183 |
buf, |
184 |
req->remote_ip_addr, |
185 |
(req->logline != NULL ? |
186 |
req->logline : "(null)"), |
187 |
(req->pathname != NULL ? req->pathname : "(null)")); |
188 |
|
189 |
errno = errno_save; |
190 |
} |
191 |
|
192 |
/* |
193 |
* Name: boa_perror |
194 |
* |
195 |
* Description: logs an error to user and error file both |
196 |
* |
197 |
*/ |
198 |
void boa_perror(request * req, char *message) |
199 |
{ |
200 |
log_error_doc(req); |
201 |
perror(message); /* don't need to save errno because log_error_doc does */ |
202 |
send_r_error(req); |
203 |
} |
204 |
|
205 |
/* |
206 |
* Name: log_error_time |
207 |
* |
208 |
* Description: Logs the current time to the stderr (the error log): |
209 |
* should always be followed by an fprintf to stderr |
210 |
*/ |
211 |
|
212 |
void log_error_time() |
213 |
{ |
214 |
char buf[30]; |
215 |
int errno_save = errno; |
216 |
|
217 |
get_commonlog_time( buf); |
218 |
fputs( buf, stderr); |
219 |
errno = errno_save; |
220 |
} |
221 |
|
222 |
/* |
223 |
* Name: log_error_mesg |
224 |
* |
225 |
* Description: performs a log_error_time, writes the file and lineno |
226 |
* to stderr (saving errno), and then a perror with message |
227 |
* |
228 |
*/ |
229 |
|
230 |
void log_error_mesg(char *file, int line, char *mesg) |
231 |
{ |
232 |
int errno_save = errno; |
233 |
char buf[30]; |
234 |
|
235 |
get_commonlog_time( buf); |
236 |
fprintf(stderr, "%s%s:%d - ", buf, file, line); |
237 |
errno = errno_save; |
238 |
perror(mesg); |
239 |
errno = errno_save; |
240 |
} |