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

Contents of /hydra/src/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Mon Oct 21 18:46:26 2002 UTC (21 years, 5 months ago) by nmav
Branch: MAIN
CVS Tags: hydra_0_0_8
Changes since 1.6: +82 -59 lines
File MIME type: text/plain
Added several stuff from Boa 0.94.14rc1

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.6 2002/10/04 09:16:33 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 (fcntl(cgi_log_fd, F_SETFD, 1) == -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) {
161 printf("%s ", req->hostname);
162 }
163
164 get_commonlog_time( buf);
165 #ifndef HAVE_STAT64
166 printf( "%s - - %s\"%s\" %d %ld \"%s\" \"%s\"\n",
167 #else
168 printf( "%s - - %s\"%s\" %d %lld \"%s\" \"%s\"\n",
169 #endif
170 req->remote_ip_addr,
171 buf,
172 req->logline,
173 req->response_status,
174 req->filepos,
175 (req->header_referer ? req->header_referer : "-"),
176 (req->header_user_agent ? req->header_user_agent : "-"));
177 }
178
179 /*
180 * Name: log_error_doc
181 *
182 * Description: Logs the current time and transaction identification
183 * to the stderr (the error log):
184 * should always be followed by an fprintf to stderr
185 *
186 * This function used to be implemented with a big fprintf, but not
187 * all fprintf's are reliable in the face of null string pointers
188 * (SunOS, in particular). As long as I had to add the checks for
189 * null pointers, I changed from fprintf to fputs.
190 *
191 * Example output:
192 * 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
193 *
194 * Apache uses:
195 * [Wed Oct 11 14:32:52 2000] [error] [client 127.0.0.1] client denied by server configuration: /export/home/live/ap/htdocs/test
196 */
197
198 void log_error_doc(request * req)
199 {
200 int errno_save = errno;
201 char buf[30];
202
203 if (req->hostname) {
204 fprintf(stderr, "%s ", req->hostname);
205 }
206
207 get_commonlog_time(buf);
208 fprintf(stderr, "%s - - %srequest \"%s\" (\"%s\"): ",
209 req->remote_ip_addr,
210 buf,
211 (req->logline != NULL ? req->logline : "(null)"),
212 (req->pathname != NULL ? req->pathname : "(null)"));
213
214 errno = errno_save;
215 }
216
217 /*
218 * Name: boa_perror
219 *
220 * Description: logs an error to user and error file both
221 *
222 */
223 void boa_perror(request * req, char *message)
224 {
225 log_error_doc(req);
226 perror(message); /* don't need to save errno because log_error_doc does */
227 send_r_error(req);
228 }
229
230 /*
231 * Name: log_error_time
232 *
233 * Description: Logs the current time to the stderr (the error log):
234 * should always be followed by an fprintf to stderr
235 */
236
237 void log_error_time()
238 {
239 char buf[30];
240 int errno_save = errno;
241
242 get_commonlog_time( buf);
243 fputs( buf, stderr);
244 errno = errno_save;
245 }
246
247 /*
248 * Name: log_error_mesg
249 *
250 * Description: performs a log_error_time, writes the file and lineno
251 * to stderr (saving errno), and then a perror with message
252 *
253 */
254
255 void log_error_mesg(char *file, int line, char *mesg)
256 {
257 int errno_save = errno;
258 char buf[30];
259
260 get_commonlog_time( buf);
261 fprintf(stderr, "%s%s:%d - ", buf, file, line);
262 errno = errno_save;
263 perror(mesg);
264 errno = errno_save;
265 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26