Avoid crash when not logging to syslog.
[polipo.git] / log.c
blob326e2d3badf66adaeee544f6bfa5f64e6239c240
1 /*
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
20 THE SOFTWARE.
23 #include "polipo.h"
25 #ifdef HAVE_SYSLOG
26 #include <syslog.h>
27 #endif
29 static int logLevel = LOGGING_DEFAULT;
30 static int logSyslog = 0;
31 static AtomPtr logFile = NULL;
32 static FILE *logF;
33 #ifdef HAVE_SYSLOG
34 static AtomPtr logFacility = NULL;
35 static int facility;
36 #endif
38 #define STR(x) XSTR(x)
39 #define XSTR(x) #x
41 static void initSyslog(void);
43 #ifdef HAVE_SYSLOG
44 static char *syslogBuf;
45 static ssize_t syslogBufSize;
46 static size_t syslogBufLength;
48 static int translateFacility(AtomPtr facility);
49 static int translatePriority(int type);
50 static void accumulateSyslog(int type, const char *msg, int len);
51 #endif
53 void
54 preinitLog()
56 CONFIG_VARIABLE_SETTABLE(logLevel, CONFIG_HEX, configIntSetter,
57 "Logging level (max = " STR(LOGGING_MAX) ").");
58 CONFIG_VARIABLE(logFile, CONFIG_ATOM, "Log file (stderr if empty and logSyslog is unset).");
60 #ifdef HAVE_SYSLOG
61 CONFIG_VARIABLE(logSyslog, CONFIG_BOOLEAN, "Log to syslog.");
62 CONFIG_VARIABLE(logFacility, CONFIG_ATOM, "Facility to use.");
63 #endif
65 logF = stderr;
68 int
69 loggingToStderr(void) {
70 return(logF == stderr);
73 void
74 initLog(void)
76 if(daemonise && logFile == NULL && !logSyslog)
77 logFile = internAtom("/var/log/polipo");
79 if(logFile != NULL && logFile->length > 0) {
80 FILE *f;
81 f = fopen(logFile->string, "a");
82 if(f == NULL) {
83 do_log_error(L_ERROR, errno, "Couldn't open log file %s",
84 logFile->string);
85 exit(1);
87 setvbuf(f, NULL, _IOLBF, 0);
88 logF = f;
91 if(logSyslog) {
92 initSyslog();
94 if(logFile == NULL) {
95 logF = NULL;
100 #ifdef HAVE_SYSLOG
101 static void
102 initSyslog()
104 if(logSyslog) {
105 facility = translateFacility(logFacility);
106 closelog();
107 openlog("polipo", LOG_PID, facility);
109 if(!syslogBuf) {
110 syslogBuf = strdup("");
111 syslogBufSize = 1;
116 /* Map a user-provided name to a syslog facility.
118 This is rendered quite ugly because POSIX hardly defines any, but we
119 should allow any the local system knows about. */
121 static int
122 translateFacility(AtomPtr facility)
124 typedef struct
126 const char *name;
127 int facility;
128 } FacilitiesRec;
130 /* List of all known valid syslog facilities.
132 This list is terminated by a NULL facility name. */
134 FacilitiesRec facilities[] = {
135 /* These are all the facilities found in glibc 2.5. */
136 #ifdef LOG_AUTH
137 { "auth", LOG_AUTH },
138 #endif
139 #ifdef LOG_AUTHPRIV
140 { "authpriv", LOG_AUTHPRIV },
141 #endif
142 #ifdef LOG_CRON
143 { "cron", LOG_CRON },
144 #endif
145 #ifdef LOG_DAEMON
146 { "daemon", LOG_DAEMON },
147 #endif
148 #ifdef LOG_FTP
149 { "ftp", LOG_FTP },
150 #endif
151 #ifdef LOG_KERN
152 { "kern", LOG_KERN },
153 #endif
154 #ifdef LOG_LPR
155 { "lpr", LOG_LPR },
156 #endif
157 #ifdef LOG_MAIL
158 { "mail", LOG_MAIL },
159 #endif
160 #ifdef LOG_NEWS
161 { "news", LOG_NEWS },
162 #endif
163 #ifdef LOG_SYSLOG
164 { "syslog", LOG_SYSLOG },
165 #endif
166 #ifdef LOG_uucp
167 { "uucp", LOG_UUCP },
168 #endif
169 /* These are required by POSIX. */
170 { "user", LOG_USER },
171 { "local0", LOG_LOCAL0 },
172 { "local1", LOG_LOCAL1 },
173 { "local2", LOG_LOCAL2 },
174 { "local3", LOG_LOCAL3 },
175 { "local4", LOG_LOCAL4 },
176 { "local5", LOG_LOCAL5 },
177 { "local6", LOG_LOCAL6 },
178 { "local7", LOG_LOCAL7 },
179 { NULL, 0 }};
181 FacilitiesRec *current;
183 /* It would be more fitting to return LOG_DAEMON, but POSIX does not
184 guarantee the existence of that facility. */
186 if(!facility) {
187 return LOG_USER;
190 current = facilities;
191 while(current->name) {
192 if(!strcmp(current->name, atomString(facility))) {
193 return current->facility;
195 current++;
198 /* This will go to stderr because syslog is not yet initialized. */
199 do_log(L_ERROR, "Specified logFacility %s nonexistent on this system.",
200 atomString(facility));
202 return LOG_USER;
205 /* Translate a Polipo error type into a syslog priority. */
207 static int
208 translatePriority(int type)
210 typedef struct
212 int type;
213 int priority;
214 } PrioritiesRec;
216 /* The list is terminated with a type of zero. */
218 PrioritiesRec priorities[] = {{ L_ERROR, LOG_ERR },
219 { L_WARN, LOG_WARNING },
220 { L_INFO, LOG_NOTICE },
221 { L_FORBIDDEN, LOG_DEBUG },
222 { L_UNCACHEABLE, LOG_DEBUG },
223 { L_SUPERSEDED, LOG_DEBUG },
224 { L_VARY, LOG_DEBUG },
225 { 0, 0 }};
226 PrioritiesRec *current;
228 current = priorities;
229 while(current->type) {
230 if(current->type == type) {
231 return current->priority;
233 current++;
236 return LOG_DEBUG;
239 /* Accumulate a message for syslogging, emitting pieces ending in linefeeds.
241 Argument formatting is done by the caller. */
243 static void
244 accumulateSyslog(int type, const char *msg, int len)
246 ssize_t new_syslogBufSize;
247 char *linefeed;
249 /* First, expand (possibly) and concatenate. */
251 new_syslogBufSize = syslogBufLength + len;
253 if(new_syslogBufSize >= syslogBufSize) {
254 char *new_syslogBuf;
256 new_syslogBuf = realloc(syslogBuf, new_syslogBufSize);
258 if(!new_syslogBuf)
259 return;
260 syslogBuf = new_syslogBuf;
261 syslogBufSize = new_syslogBufSize;
264 memcpy(syslogBuf + syslogBufLength, msg, len);
265 syslogBufLength += len;
267 /* Now look for a linefeed and flush as long as one is found. */
269 while((linefeed = memchr(syslogBuf, '\n', syslogBufLength)) != NULL) {
270 *linefeed = '\0';
271 syslog(translatePriority(type), "%s", syslogBuf);
272 linefeed++;
274 /* Some is printed: flush it. */
276 syslogBufLength -= linefeed - syslogBuf;
277 memmove(syslogBuf, linefeed, syslogBufLength);
280 #else
281 static void
282 initSyslog()
284 return;
286 #endif
288 /* Flush any messages waiting to be logged. */
289 void flushLog()
291 if(logF)
292 fflush(logF);
294 #ifdef HAVE_SYSLOG
295 /* There shouldn't really be anything here, but let's be paranoid.
296 We can't pick a good value for `type', so just invent one. */
297 if(logSyslog && syslogBuf[0] != '\0') {
298 accumulateSyslog(L_INFO, "\n", 1);
301 assert(syslogBufLength == 0);
302 #endif
305 void
306 reopenLog()
308 if(logFile) {
309 FILE *f;
310 f = fopen(logFile->string, "a");
311 if(f == NULL) {
312 do_log_error(L_ERROR, errno, "Couldn't reopen log file %s",
313 logFile->string);
314 exit(1);
316 setvbuf(f, NULL, _IOLBF, 0);
317 fclose(logF);
318 logF = f;
321 if(logSyslog) {
322 initSyslog();
326 void
327 really_do_log(int type, const char *f, ...)
329 va_list args;
331 va_start(args, f);
332 if(type & LOGGING_MAX & logLevel)
333 really_do_log_v(type, f, args);
334 va_end(args);
337 void
338 really_do_log_v(int type, const char *f, va_list args)
340 if(type & LOGGING_MAX & logLevel) {
341 if(logF)
342 vfprintf(logF, f, args);
343 #ifdef HAVE_SYSLOG
344 if(logSyslog) {
345 int n;
346 char msg[256];
347 n = vsnprintf(msg, 256, f, args);
348 if(n >= 0 && n < 255)
349 accumulateSyslog(type, msg, n);
350 else
351 accumulateSyslog(LOG_ERR, "Syslog message lost.\n", 21);
353 #endif
357 void
358 really_do_log_error(int type, int e, const char *f, ...)
360 va_list args;
361 va_start(args, f);
362 if(type & LOGGING_MAX & logLevel)
363 really_do_log_error_v(type, e, f, args);
364 va_end(args);
367 void
368 really_do_log_error_v(int type, int e, const char *f, va_list args)
370 if((type & LOGGING_MAX & logLevel) != 0) {
371 char *es = pstrerror(e);
372 if(es == NULL)
373 es = "Unknown error";
375 if(logF) {
376 vfprintf(logF, f, args);
377 fprintf(logF, ": %s\n", es);
379 #ifdef HAVE_SYSLOG
380 if(logSyslog) {
381 char msg[256];
382 size_t n = 0;
384 n = snnvprintf(msg, n, 256, f, args);
385 n = snnprintf(msg, n, 256, ": ");
386 n = snnprint_n(msg, n, 256, es, strlen (es));
387 n = snnprintf(msg, n, 256, "\n");
388 /* Overflow? Vanishingly unlikely; truncate at 255. */
389 if(n < 0 || n > 256) {
390 n = 256;
391 msg[255] = '\0';
393 else
394 msg[n] = '\0';
396 accumulateSyslog(type, msg, n);
398 #endif
402 void
403 really_do_log_n(int type, const char *s, int n)
405 if((type & LOGGING_MAX & logLevel) != 0) {
406 if(logF) {
407 fwrite(s, n, 1, logF);
409 #ifdef HAVE_SYSLOG
410 if(logSyslog) {
411 accumulateSyslog(type, s, n);
413 #endif