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

Annotation of /hydra/src/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Sat Sep 28 16:32:37 2002 UTC (21 years, 6 months ago) by nmav
Branch: MAIN
CVS Tags: hydra_0_0_3, hydra_0_0_6, hydra_0_0_4, hydra_0_0_5
Changes since 1.3: +2 -2 lines
File MIME type: text/plain
In sighup and sigterm, the HIC thread is terminated as well.

1 nmav 1.1 /*
2 nmav 1.4 * Hydra, an http server
3 nmav 1.1 * 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 nmav 1.4 /* $Id: log.c,v 1.3 2002/09/24 17:12:47 nmav Exp $*/
24 nmav 1.1
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 nmav 1.2 char buf[30];
144    
145     get_commonlog_time( buf);
146 nmav 1.1 fprintf(access_log, "%s - - %s\"%s\" %d %ld \"%s\" \"%s\"\n",
147     req->remote_ip_addr,
148 nmav 1.2 buf,
149 nmav 1.1 req->logline,
150     req->response_status,
151     req->filepos,
152     (req->header_referer ? req->header_referer : "-"),
153     (req->header_user_agent ? req->header_user_agent : "-"));
154    
155     }
156     }
157    
158     /*
159     * Name: log_error_doc
160     *
161     * Description: Logs the current time and transaction identification
162     * to the stderr (the error log):
163     * should always be followed by an fprintf to stderr
164     *
165     * This function used to be implemented with a big fprintf, but not
166     * all fprintf's are reliable in the face of null string pointers
167     * (SunOS, in particular). As long as I had to add the checks for
168     * null pointers, I changed from fprintf to fputs.
169     *
170     * Example output:
171     [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
172     */
173    
174     void log_error_doc(request * req)
175     {
176     int errno_save = errno;
177 nmav 1.2 char buf[30];
178    
179     get_commonlog_time(buf);
180 nmav 1.1 fprintf(stderr, "%srequest from %s \"%s\" (\"%s\"): ",
181 nmav 1.2 buf,
182 nmav 1.1 req->remote_ip_addr,
183     (req->logline != NULL ?
184     req->logline : "(null)"),
185     (req->pathname != NULL ? req->pathname : "(null)"));
186    
187     errno = errno_save;
188     }
189    
190     /*
191     * Name: boa_perror
192     *
193     * Description: logs an error to user and error file both
194     *
195     */
196     void boa_perror(request * req, char *message)
197     {
198     log_error_doc(req);
199     perror(message); /* don't need to save errno because log_error_doc does */
200     send_r_error(req);
201     }
202    
203     /*
204     * Name: log_error_time
205     *
206     * Description: Logs the current time to the stderr (the error log):
207     * should always be followed by an fprintf to stderr
208     */
209    
210     void log_error_time()
211     {
212 nmav 1.2 char buf[30];
213     int errno_save = errno;
214    
215     get_commonlog_time( buf);
216     fputs( buf, stderr);
217 nmav 1.1 errno = errno_save;
218     }
219    
220     /*
221     * Name: log_error_mesg
222     *
223     * Description: performs a log_error_time, writes the file and lineno
224     * to stderr (saving errno), and then a perror with message
225     *
226     */
227    
228     void log_error_mesg(char *file, int line, char *mesg)
229     {
230     int errno_save = errno;
231 nmav 1.2 char buf[30];
232    
233     get_commonlog_time( buf);
234     fprintf(stderr, "%s%s:%d - ", buf, file, line);
235 nmav 1.1 errno = errno_save;
236     perror(mesg);
237     errno = errno_save;
238     }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26