2 Copyright (c) 2003-2006 by Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 static int logLevel
= LOGGING_DEFAULT
;
30 static int logSyslog
= 0;
31 static AtomPtr logFile
= NULL
;
33 static int logFilePermissions
= 0640;
37 static AtomPtr logFacility
= NULL
;
41 #define STR(x) XSTR(x)
44 static void initSyslog(void);
47 static char *syslogBuf
;
48 static int syslogBufSize
;
49 static int syslogBufLength
;
51 static int translateFacility(AtomPtr facility
);
52 static int translatePriority(int type
);
53 static void accumulateSyslogV(int type
, const char *f
, va_list args
);
54 static void accumulateSyslogN(int type
, const char *s
, int len
);
60 CONFIG_VARIABLE_SETTABLE(logLevel
, CONFIG_HEX
, configIntSetter
,
61 "Logging level (max = " STR(LOGGING_MAX
) ").");
62 CONFIG_VARIABLE(logFile
, CONFIG_ATOM
, "Log file (stderr if empty and logSyslog is unset, /var/log/polipo if empty and daemonise is true).");
63 CONFIG_VARIABLE(logFilePermissions
, CONFIG_OCTAL
,
64 "Access rights of the logFile.");
65 CONFIG_VARIABLE_SETTABLE(scrubLogs
, CONFIG_BOOLEAN
, configIntSetter
,
66 "If true, don't include URLs in logs.");
69 CONFIG_VARIABLE(logSyslog
, CONFIG_BOOLEAN
, "Log to syslog.");
70 CONFIG_VARIABLE(logFacility
, CONFIG_ATOM
, "Syslog facility to use.");
71 logFacility
= internAtom("user");
78 loggingToStderr(void) {
79 return(logF
== stderr
);
88 fd
= open(logFile
->string
, O_WRONLY
| O_CREAT
| O_APPEND
,
95 int saved_errno
= errno
;
101 setvbuf(f
, NULL
, _IOLBF
, 0);
108 if(daemonise
&& logFile
== NULL
&& !logSyslog
)
109 logFile
= internAtom("/var/log/polipo");
111 if(logFile
!= NULL
&& logFile
->length
> 0) {
115 do_log_error(L_ERROR
, errno
, "Couldn't open log file %s",
125 if(logFile
== NULL
) {
136 facility
= translateFacility(logFacility
);
138 openlog("polipo", LOG_PID
, facility
);
141 syslogBuf
= strdup("");
147 /* Map a user-provided name to a syslog facility.
149 This is rendered quite ugly because POSIX hardly defines any, but we
150 should allow any the local system knows about. */
153 translateFacility(AtomPtr facility
)
161 /* List of all known valid syslog facilities.
163 This list is terminated by a NULL facility name. */
165 FacilitiesRec facilities
[] = {
166 /* These are all the facilities found in glibc 2.5. */
168 { "auth", LOG_AUTH
},
171 { "authpriv", LOG_AUTHPRIV
},
174 { "cron", LOG_CRON
},
177 { "daemon", LOG_DAEMON
},
183 { "kern", LOG_KERN
},
189 { "mail", LOG_MAIL
},
192 { "news", LOG_NEWS
},
195 { "syslog", LOG_SYSLOG
},
198 { "uucp", LOG_UUCP
},
200 /* These are required by POSIX. */
201 { "user", LOG_USER
},
202 { "local0", LOG_LOCAL0
},
203 { "local1", LOG_LOCAL1
},
204 { "local2", LOG_LOCAL2
},
205 { "local3", LOG_LOCAL3
},
206 { "local4", LOG_LOCAL4
},
207 { "local5", LOG_LOCAL5
},
208 { "local6", LOG_LOCAL6
},
209 { "local7", LOG_LOCAL7
},
212 FacilitiesRec
*current
;
214 /* It would be more fitting to return LOG_DAEMON, but POSIX does not
215 guarantee the existence of that facility. */
221 current
= facilities
;
222 while(current
->name
) {
223 if(!strcmp(current
->name
, atomString(facility
))) {
224 return current
->facility
;
229 /* This will go to stderr because syslog is not yet initialized. */
230 do_log(L_ERROR
, "Specified logFacility %s nonexistent on this system.",
231 atomString(facility
));
236 /* Translate a Polipo error type into a syslog priority. */
239 translatePriority(int type
)
247 /* The list is terminated with a type of zero. */
249 PrioritiesRec priorities
[] = {{ L_ERROR
, LOG_ERR
},
250 { L_WARN
, LOG_WARNING
},
251 { L_INFO
, LOG_NOTICE
},
252 { L_FORBIDDEN
, LOG_DEBUG
},
253 { L_UNCACHEABLE
, LOG_DEBUG
},
254 { L_SUPERSEDED
, LOG_DEBUG
},
255 { L_VARY
, LOG_DEBUG
},
257 PrioritiesRec
*current
;
259 current
= priorities
;
260 while(current
->type
) {
261 if(current
->type
== type
) {
262 return current
->priority
;
271 expandSyslog(int len
)
277 newsize
= syslogBufSize
* 2;
279 newsize
= syslogBufLength
+ len
+ 1;
281 newbuf
= realloc(syslogBuf
, newsize
);
286 syslogBufSize
= newsize
;
291 maybeFlushSyslog(int type
)
295 linefeed
= memchr(syslogBuf
, '\n', syslogBufLength
);
299 syslog(translatePriority(type
), "%s", syslogBuf
);
301 syslogBufLength
-= (linefeed
- syslogBuf
);
302 if(syslogBufLength
> 0)
303 memmove(syslogBuf
, linefeed
, syslogBufLength
);
308 accumulateSyslogV(int type
, const char *f
, va_list args
)
313 rc
= vsnprintf(syslogBuf
+ syslogBufLength
,
314 syslogBufSize
- syslogBufLength
,
317 if(rc
< 0 || rc
>= syslogBufSize
- syslogBufLength
) {
318 rc
= expandSyslog(rc
);
324 syslogBufLength
+= rc
;
326 maybeFlushSyslog(type
);
330 accumulateSyslogN(int type
, const char *s
, int len
)
332 while(syslogBufSize
- syslogBufLength
<= len
)
335 memcpy(syslogBuf
+ syslogBufLength
, s
, len
);
336 syslogBufLength
+= len
;
337 syslogBuf
[syslogBufLength
] = '\0';
339 maybeFlushSyslog(type
);
350 /* Flush any messages waiting to be logged. */
357 /* There shouldn't really be anything here, but let's be paranoid.
358 We can't pick a good value for `type', so just invent one. */
359 if(logSyslog
&& syslogBuf
[0] != '\0') {
360 accumulateSyslogN(L_INFO
, "\n", 1);
363 assert(syslogBufLength
== 0);
374 do_log_error(L_ERROR
, errno
, "Couldn't reopen log file %s",
387 really_do_log(int type
, const char *f
, ...)
392 if(type
& LOGGING_MAX
& logLevel
)
393 really_do_log_v(type
, f
, args
);
398 really_do_log_v(int type
, const char *f
, va_list args
)
400 if(type
& LOGGING_MAX
& logLevel
) {
402 vfprintf(logF
, f
, args
);
405 accumulateSyslogV(type
, f
, args
);
411 really_do_log_error(int type
, int e
, const char *f
, ...)
415 if(type
& LOGGING_MAX
& logLevel
)
416 really_do_log_error_v(type
, e
, f
, args
);
421 really_do_log_error_v(int type
, int e
, const char *f
, va_list args
)
423 if((type
& LOGGING_MAX
& logLevel
) != 0) {
424 char *es
= pstrerror(e
);
426 es
= "Unknown error";
429 vfprintf(logF
, f
, args
);
430 fprintf(logF
, ": %s\n", es
);
437 n
= snnvprintf(msg
, n
, 256, f
, args
);
438 n
= snnprintf(msg
, n
, 256, ": ");
439 n
= snnprint_n(msg
, n
, 256, es
, strlen (es
));
440 n
= snnprintf(msg
, n
, 256, "\n");
441 /* Overflow? Vanishingly unlikely; truncate at 255. */
442 if(n
< 0 || n
> 256) {
449 accumulateSyslogN(type
, msg
, n
);
456 really_do_log_n(int type
, const char *s
, int n
)
458 if((type
& LOGGING_MAX
& logLevel
) != 0) {
460 fwrite(s
, n
, 1, logF
);
464 accumulateSyslogN(type
, s
, n
);
470 scrub(const char *message
)