/[hydra]/hydra/src/log.c
ViewVC logotype

Contents of /hydra/src/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations)
Tue Oct 29 11:52:53 2002 UTC (21 years, 5 months ago) by nmav
Branch: MAIN
CVS Tags: hydra_0_0_10, hydra_0_1_3, hydra_0_1_2, hydra_0_1_1, hydra_0_1_0
Branch point for: hydra_0_1_0_patches
Changes since 1.11: +5 -5 lines
File MIME type: text/plain
Correct error log file logging.

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.11 2002/10/27 08:45:51 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 *fopen_gen_fd(char *spec, const char *mode);
33
34 FILE *fopen_gen_fd(char *spec, const char *mode)
35 {
36 int fd;
37 if (!spec || *spec == '\0')
38 return NULL;
39 fd = open_gen_fd(spec);
40 if (fd == -1)
41 return NULL;
42 return fdopen(fd, mode);
43 }
44
45 /*
46 * Name: open_logs
47 *
48 * Description: Opens access log, error log, and if specified, cgi log
49 * Ties stderr to error log, except during cgi execution, at which
50 * time cgi log is the stderr for cgis.
51 *
52 * Access log is line buffered, error log is not buffered.
53 *
54 */
55
56 void open_logs(void)
57 {
58 int access_log;
59
60 /* if error_log_name is set, dup2 stderr to it */
61 /* otherwise, leave stderr alone */
62 /* we don't want to tie stderr to /dev/null */
63 if (error_log_name) {
64 int error_log;
65
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 (access_log_name) {
80 access_log = open_gen_fd(access_log_name);
81 } else {
82 access_log = open("/dev/null", 0);
83 }
84
85 if (access_log < 0) {
86 DIE("unable to open access log");
87 }
88 if (dup2(access_log, STDOUT_FILENO) == -1) {
89 DIE("can't dup2 /dev/null to STDOUT_FILENO");
90 }
91 close(access_log);
92
93 if (cgi_log_name) {
94 cgi_log_fd = open_gen_fd(cgi_log_name);
95 if (cgi_log_fd == -1) {
96 WARN("open cgi_log");
97 free(cgi_log_name);
98 cgi_log_name = NULL;
99 cgi_log_fd = -1;
100 } else {
101 if (set_cloexec_fd(cgi_log_fd) == -1) {
102 WARN("unable to set close-on-exec flag for cgi_log");
103 free(cgi_log_name);
104 cgi_log_name = NULL;
105 close(cgi_log_fd);
106 cgi_log_fd = -1;
107 }
108 }
109 }
110
111 #ifdef SETVBUF_REVERSED
112 setvbuf(stderr, _IONBF, (char *) NULL, 0);
113 setvbuf(stdout, _IOLBF, (char *) NULL, 0);
114 #else
115 setvbuf(stderr, (char *) NULL, _IONBF, 0);
116 setvbuf(stdout, (char *) NULL, _IOLBF, 0);
117 #endif
118
119 }
120
121 /*
122 * Name: log_access
123 *
124 * Description: Writes log data to access_log.
125 */
126
127 /* NOTES on the commonlog format:
128 * Taken from notes on the NetBuddy program
129 * http://www.computer-dynamics.com/commonlog.html
130 *
131 * remotehost
132 *
133 * remotehost rfc931 authuser [date] "request" status bytes
134 *
135 * remotehost - IP of the client
136 * rfc931 - remote name of the user (always '-')
137 * authuser - username entered for authentication - almost always '-'
138 * [date] - the date in [08/Nov/1997:01:05:03 -0600] (with brackets) format
139 * "request" - literal request from the client (boa may clean this up,
140 * replacing control-characters with '_' perhaps - NOTE: not done)
141 * status - http status code
142 * bytes - number of bytes transferred
143 *
144 * boa appends:
145 * referer
146 * user-agent
147 *
148 * and may prepend (depending on configuration):
149 * virtualhost - the name or IP (depending on whether name-based
150 * virtualhosting is enabled) of the host the client accessed
151 */
152
153 void log_access(request * req)
154 {
155 char buf[30];
156
157 if (!access_log_name)
158 return;
159
160 if (req->hostname && req->hostname[0]!=0) {
161 printf("%s ", req->hostname);
162 } else {
163 printf( "unknown ");
164 }
165
166 get_commonlog_time( buf);
167 #ifndef USE_LONG_OFFSETS
168 printf( "%s - - %s\"%s\" %d %ld \"%s\" \"%s\"\n",
169 #else
170 printf( "%s - - %s\"%s\" %d %lld \"%s\" \"%s\"\n",
171 #endif
172 req->remote_ip_addr,
173 buf,
174 req->logline,
175 req->response_status,
176 req->filepos,
177 (req->header_referer ? req->header_referer : "-"),
178 (req->header_user_agent ? req->header_user_agent : "-"));
179 }
180
181 /*
182 * Name: log_error_doc
183 *
184 * Description: Logs the current time and transaction identification
185 * to the stderr (the error log):
186 * should always be followed by an fprintf to stderr
187 *
188 * This function used to be implemented with a big fprintf, but not
189 * all fprintf's are reliable in the face of null string pointers
190 * (SunOS, in particular). As long as I had to add the checks for
191 * null pointers, I changed from fprintf to fputs.
192 *
193 * Example output:
194 * 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
195 *
196 * Apache uses:
197 * [Wed Oct 11 14:32:52 2000] [error] [client 127.0.0.1] client denied by server configuration: /export/home/live/ap/htdocs/test
198 */
199
200 void log_error_doc(request * req)
201 {
202 int errno_save = errno;
203 char buf[30];
204
205 if (req->hostname && req->hostname[0]!=0) {
206 fprintf(stderr, "%s ", req->hostname);
207 } else {
208 fprintf(stderr, "unknown ");
209 }
210
211 get_commonlog_time(buf);
212 fprintf(stderr, "%s - - %srequest \"%s\" (\"%s\"): ",
213 req->remote_ip_addr,
214 buf,
215 (req->logline != NULL ? req->logline : ""),
216 (req->pathname != NULL ? req->pathname : ""));
217
218 errno = errno_save;
219 }
220
221 /*
222 * Name: boa_perror
223 *
224 * Description: logs an error to user and error file both
225 *
226 */
227 void boa_perror(request * req, char *message)
228 {
229 log_error_doc(req);
230 perror(message); /* don't need to save errno because log_error_doc does */
231 send_r_error(req);
232 }
233
234 /*
235 * Name: log_error_time
236 *
237 * Description: Logs the current time to the stderr (the error log):
238 * should always be followed by an fprintf to stderr
239 */
240
241 void log_error_time()
242 {
243 char buf[30];
244 int errno_save = errno;
245
246 get_commonlog_time( buf);
247 fputs( buf, stderr);
248 errno = errno_save;
249 }
250
251 /*
252 * Name: log_error_mesg
253 *
254 * Description: performs a log_error_time, writes the file and lineno
255 * to stderr (saving errno), and then a perror with message
256 *
257 */
258
259 void log_error_mesg(char *file, int line, char *mesg)
260 {
261 int errno_save = errno;
262 char buf[30];
263
264 get_commonlog_time( buf);
265 fprintf(stderr, "%s%s:%d - ", buf, file, line);
266 errno = errno_save;
267 perror(mesg);
268 errno = errno_save;
269 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26