log: Introduce slilog_part; abolish log_if->logfn
[secnet] / log.c
1 #include "secnet.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <time.h>
6 #include <errno.h>
7 #include <syslog.h>
8 #include <assert.h>
9 #include <unistd.h>
10 #include "process.h"
11 #include "util.h"
12
13 bool_t secnet_is_daemon=False;
14 uint32_t message_level=M_WARNING|M_ERR|M_SECURITY|M_FATAL;
15 struct log_if *system_log=NULL;
16
17 static void vMessageFallback(uint32_t class, const char *message, va_list args)
18 FORMAT(printf,2,0);
19 static void vMessageFallback(uint32_t class, const char *message, va_list args)
20 {
21 FILE *dest=stdout;
22 /* Messages go to stdout/stderr */
23 if (class & message_level) {
24 if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
25 dest=stderr;
26 }
27 vfprintf(dest,message,args);
28 }
29 }
30
31 static void vMessage(uint32_t class, const char *message, va_list args)
32 {
33
34 if (system_log) {
35 /* Messages go to the system log interface */
36 vslilog_part(system_log, class, message, args);
37 } else {
38 vMessageFallback(class,message,args);
39 }
40 }
41
42 void Message(uint32_t class, const char *message, ...)
43 {
44 va_list ap;
45
46 va_start(ap,message);
47 vMessage(class,message,ap);
48 va_end(ap);
49 }
50
51 static void MessageFallback(uint32_t class, const char *message, ...)
52 FORMAT(printf,2,3);
53 static void MessageFallback(uint32_t class, const char *message, ...)
54 {
55 va_list ap;
56
57 va_start(ap,message);
58 vMessageFallback(class,message,ap);
59 va_end(ap);
60 }
61
62 static NORETURN(vfatal(int status, bool_t perror, const char *message,
63 va_list args));
64
65 static void vfatal(int status, bool_t perror, const char *message,
66 va_list args)
67 {
68 int err;
69
70 err=errno;
71
72 enter_phase(PHASE_SHUTDOWN);
73 Message(M_FATAL, "secnet fatal error: ");
74 vMessage(M_FATAL, message, args);
75 if (perror)
76 Message(M_FATAL, ": %s\n",strerror(err));
77 else
78 Message(M_FATAL, "\n");
79 exit(status);
80 }
81
82 void fatal(const char *message, ...)
83 {
84 va_list args;
85 va_start(args,message);
86 vfatal(current_phase,False,message,args);
87 va_end(args);
88 }
89
90 void fatal_status(int status, const char *message, ...)
91 {
92 va_list args;
93 va_start(args,message);
94 vfatal(status,False,message,args);
95 va_end(args);
96 }
97
98 void fatal_perror(const char *message, ...)
99 {
100 va_list args;
101 va_start(args,message);
102 vfatal(current_phase,True,message,args);
103 va_end(args);
104 }
105
106 void fatal_perror_status(int status, const char *message, ...)
107 {
108 va_list args;
109 va_start(args,message);
110 vfatal(status,True,message,args);
111 va_end(args);
112 }
113
114 void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
115 cstring_t facility, const char *message, va_list args)
116 {
117 enter_phase(PHASE_SHUTDOWN);
118
119 if (maybe_f && ferror(maybe_f)) {
120 assert(loc.file);
121 Message(M_FATAL, "error reading config file (%s, %s): %s",
122 facility, loc.file, strerror(errno));
123 } else if (maybe_f && feof(maybe_f)) {
124 assert(loc.file);
125 Message(M_FATAL, "unexpected end of config file (%s, %s)",
126 facility, loc.file);
127 } else if (loc.file && loc.line) {
128 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
129 loc.line);
130 } else if (!loc.file && loc.line) {
131 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
132 } else {
133 Message(M_FATAL, "config error (%s): ",facility);
134 }
135
136 vMessage(M_FATAL,message,args);
137 exit(current_phase);
138 }
139
140 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
141 const char *message, ...)
142 {
143 va_list args;
144
145 va_start(args,message);
146 vcfgfatal_maybefile(maybe_f,loc,facility,message,args);
147 va_end(args);
148 }
149
150 void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
151 {
152 va_list args;
153
154 va_start(args,message);
155 vcfgfatal_maybefile(0,loc,facility,message,args);
156 va_end(args);
157 }
158
159 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
160 {
161 assert(loc.file);
162 if (ferror(f)) {
163 Message(M_FATAL, "error reading config file (%s): %s\n",
164 loc.file, strerror(errno));
165 exit(current_phase);
166 } else if (feof(f)) {
167 Message(M_FATAL, "unexpected end of config file (%s)\n", loc.file);
168 exit(current_phase);
169 }
170 }
171
172 /* Take a list of log closures and merge them */
173 struct loglist {
174 struct log_if *l;
175 struct loglist *next;
176 };
177
178 static void log_vmulti(void *sst, int class, const char *message, va_list args)
179 {
180 struct loglist *st=sst, *i;
181
182 if (secnet_is_daemon) {
183 for (i=st; i; i=i->next) {
184 vslilog(i->l,class,message,args);
185 }
186 } else {
187 vMessage(class,message,args);
188 Message(class,"\n");
189 }
190 }
191
192 static void log_multi(void *st, int priority, const char *message, ...)
193 FORMAT(printf,3,4);
194 static void log_multi(void *st, int priority, const char *message, ...)
195 {
196 va_list ap;
197
198 va_start(ap,message);
199 log_vmulti(st,priority,message,ap);
200 va_end(ap);
201 }
202
203 struct log_if *init_log(list_t *ll)
204 {
205 int i=0;
206 item_t *item;
207 closure_t *cl;
208 struct loglist *l=NULL, *n;
209 struct log_if *r;
210
211 if (list_length(ll)==1) {
212 item=list_elem(ll,0);
213 cl=item->data.closure;
214 if (cl->type!=CL_LOG) {
215 cfgfatal(item->loc,"init_log","closure is not a logger");
216 }
217 return cl->interface;
218 }
219 while ((item=list_elem(ll,i++))) {
220 if (item->type!=t_closure) {
221 cfgfatal(item->loc,"init_log","item is not a closure");
222 }
223 cl=item->data.closure;
224 if (cl->type!=CL_LOG) {
225 cfgfatal(item->loc,"init_log","closure is not a logger");
226 }
227 n=safe_malloc(sizeof(*n),"init_log");
228 n->l=cl->interface;
229 n->next=l;
230 l=n;
231 }
232 if (!l) {
233 fatal("init_log: no log");
234 }
235 r=safe_malloc(sizeof(*r), "init_log");
236 r->st=l;
237 r->vlogfn=log_vmulti;
238 r->buff[0]=0;
239 return r;
240 }
241
242 struct logfile {
243 closure_t cl;
244 struct log_if ops;
245 struct cloc loc;
246 string_t logfile;
247 uint32_t level;
248 FILE *f;
249 };
250
251 static cstring_t months[]={
252 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
253
254 static void logfile_vlog(void *sst, int class, const char *message,
255 va_list args)
256 {
257 struct logfile *st=sst;
258 time_t t;
259 struct tm *tm;
260
261 if (secnet_is_daemon && st->f) {
262 if (class&st->level) {
263 t=time(NULL);
264 tm=localtime(&t);
265 fprintf(st->f,"%s %2d %02d:%02d:%02d ",
266 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
267 tm->tm_sec);
268 vfprintf(st->f,message,args);
269 fprintf(st->f,"\n");
270 fflush(st->f);
271 }
272 } else {
273 vMessageFallback(class,message,args);
274 MessageFallback(class,"\n");
275 }
276 }
277
278 static void logfile_log(void *state, int class, const char *message, ...)
279 FORMAT(printf,3,4);
280 static void logfile_log(void *state, int class, const char *message, ...)
281 {
282 va_list ap;
283
284 va_start(ap,message);
285 logfile_vlog(state,class,message,ap);
286 va_end(ap);
287 }
288
289 static void logfile_hup_notify(void *sst, int signum)
290 {
291 struct logfile *st=sst;
292 FILE *f;
293 f=fopen(st->logfile,"a");
294 if (!f) {
295 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
296 "logfile: %s",strerror(errno));
297 } else {
298 fclose(st->f);
299 st->f=f;
300 logfile_log(st,M_INFO,"received SIGHUP");
301 }
302 }
303
304 static void logfile_phase_hook(void *sst, uint32_t new_phase)
305 {
306 struct logfile *st=sst;
307 FILE *f;
308
309 if (background) {
310 f=fopen(st->logfile,"a");
311 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
312 st->loc.file,st->loc.line,st->logfile);
313 st->f=f;
314 request_signal_notification(SIGHUP, logfile_hup_notify,st);
315 }
316 }
317
318 static struct flagstr message_class_table[]={
319 { "debug-config", M_DEBUG_CONFIG },
320 { "debug-phase", M_DEBUG_PHASE },
321 { "debug", M_DEBUG },
322 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
323 { "info", M_INFO },
324 { "notice", M_NOTICE },
325 { "warning", M_WARNING },
326 { "error", M_ERR },
327 { "security", M_SECURITY },
328 { "fatal", M_FATAL },
329 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
330 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
331 { "quiet", M_FATAL },
332 { NULL, 0 }
333 };
334
335 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
336 list_t *args)
337 {
338 struct logfile *st;
339 item_t *item;
340 dict_t *dict;
341
342 /* We should defer opening the logfile until the getresources
343 phase. We should defer writing into the logfile until after we
344 become a daemon. */
345
346 st=safe_malloc(sizeof(*st),"logfile_apply");
347 st->cl.description="logfile";
348 st->cl.type=CL_LOG;
349 st->cl.apply=NULL;
350 st->cl.interface=&st->ops;
351 st->ops.st=st;
352 st->ops.vlogfn=logfile_vlog;
353 st->ops.buff[0]=0;
354 st->loc=loc;
355 st->f=NULL;
356
357 item=list_elem(args,0);
358 if (!item || item->type!=t_dict) {
359 cfgfatal(loc,"logfile","argument must be a dictionary\n");
360 }
361 dict=item->data.dict;
362
363 st->logfile=dict_read_string(dict,"filename",True,"logfile",loc);
364 st->level=string_list_to_word(dict_lookup(dict,"class"),
365 message_class_table,"logfile");
366
367 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
368
369 return new_closure(&st->cl);
370 }
371
372 struct syslog {
373 closure_t cl;
374 struct log_if ops;
375 string_t ident;
376 int facility;
377 bool_t open;
378 };
379
380 static int msgclass_to_syslogpriority(uint32_t m)
381 {
382 switch (m) {
383 case M_DEBUG_CONFIG: return LOG_DEBUG;
384 case M_DEBUG_PHASE: return LOG_DEBUG;
385 case M_DEBUG: return LOG_DEBUG;
386 case M_INFO: return LOG_INFO;
387 case M_NOTICE: return LOG_NOTICE;
388 case M_WARNING: return LOG_WARNING;
389 case M_ERR: return LOG_ERR;
390 case M_SECURITY: return LOG_CRIT;
391 case M_FATAL: return LOG_EMERG;
392 default: return LOG_NOTICE;
393 }
394 }
395
396 static void syslog_vlog(void *sst, int class, const char *message,
397 va_list args)
398 FORMAT(printf,3,0);
399 static void syslog_vlog(void *sst, int class, const char *message,
400 va_list args)
401 {
402 struct syslog *st=sst;
403
404 if (st->open)
405 vsyslog(msgclass_to_syslogpriority(class),message,args);
406 else {
407 vMessageFallback(class,message,args);
408 MessageFallback(class,"\n");
409 }
410 }
411
412 static void syslog_log(void *sst, int priority, const char *message, ...)
413 FORMAT(printf,3,4);
414 static void syslog_log(void *sst, int priority, const char *message, ...)
415 {
416 va_list ap;
417
418 va_start(ap,message);
419 syslog_vlog(sst,priority,message,ap);
420 va_end(ap);
421 }
422
423 static struct flagstr syslog_facility_table[]={
424 #ifdef LOG_AUTH
425 { "auth", LOG_AUTH },
426 #endif
427 #ifdef LOG_AUTHPRIV
428 { "authpriv", LOG_AUTHPRIV },
429 #endif
430 { "cron", LOG_CRON },
431 { "daemon", LOG_DAEMON },
432 { "kern", LOG_KERN },
433 { "local0", LOG_LOCAL0 },
434 { "local1", LOG_LOCAL1 },
435 { "local2", LOG_LOCAL2 },
436 { "local3", LOG_LOCAL3 },
437 { "local4", LOG_LOCAL4 },
438 { "local5", LOG_LOCAL5 },
439 { "local6", LOG_LOCAL6 },
440 { "local7", LOG_LOCAL7 },
441 { "lpr", LOG_LPR },
442 { "mail", LOG_MAIL },
443 { "news", LOG_NEWS },
444 { "syslog", LOG_SYSLOG },
445 { "user", LOG_USER },
446 { "uucp", LOG_UUCP },
447 { NULL, 0 }
448 };
449
450 static void syslog_phase_hook(void *sst, uint32_t newphase)
451 {
452 struct syslog *st=sst;
453
454 if (background) {
455 openlog(st->ident,0,st->facility);
456 st->open=True;
457 }
458 }
459
460 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
461 list_t *args)
462 {
463 struct syslog *st;
464 dict_t *d;
465 item_t *item;
466 string_t facstr;
467
468 st=safe_malloc(sizeof(*st),"syslog_apply");
469 st->cl.description="syslog";
470 st->cl.type=CL_LOG;
471 st->cl.apply=NULL;
472 st->cl.interface=&st->ops;
473 st->ops.st=st;
474 st->ops.vlogfn=syslog_vlog;
475 st->ops.buff[0]=0;
476
477 item=list_elem(args,0);
478 if (!item || item->type!=t_dict)
479 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
480 d=item->data.dict;
481
482 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
483 facstr=dict_read_string(d, "facility", True, "syslog", loc);
484 st->facility=string_to_word(facstr,loc,
485 syslog_facility_table,"syslog");
486 st->open=False;
487 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
488
489 return new_closure(&st->cl);
490 }
491
492 /* Read from a fd and output to a log. This is a quick hack to
493 support logging stderr, and needs code adding to tidy up before it
494 can be used for anything else. */
495 #define FDLOG_BUFSIZE 1024
496 struct fdlog {
497 struct log_if *log;
498 int fd;
499 cstring_t prefix;
500 string_t buffer;
501 int i;
502 bool_t finished;
503 };
504
505 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
506 int *timeout_io)
507 {
508 struct fdlog *st=sst;
509 if (!st->finished) {
510 *nfds_io=1;
511 fds[0].fd=st->fd;
512 fds[0].events=POLLIN;
513 }
514 return 0;
515 }
516
517 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
518 {
519 struct fdlog *st=sst;
520 int r,remain,i;
521
522 if (nfds==0) return;
523 if (fds[0].revents&POLLERR) {
524 st->finished=True;
525 }
526 if (fds[0].revents&POLLIN) {
527 remain=FDLOG_BUFSIZE-st->i-1;
528 if (remain<=0) {
529 st->buffer[FDLOG_BUFSIZE-1]=0;
530 slilog(st->log,M_WARNING,"%s: overlong line: %s",
531 st->prefix,st->buffer);
532 st->i=0;
533 remain=FDLOG_BUFSIZE-1;
534 }
535 r=read(st->fd,st->buffer+st->i,remain);
536 if (r>0) {
537 st->i+=r;
538 for (i=0; i<st->i; i++) {
539 if (st->buffer[i]=='\n') {
540 st->buffer[i]=0;
541 slilog(st->log,M_INFO,"%s: %s",
542 st->prefix,st->buffer);
543 i++;
544 memmove(st->buffer,st->buffer+i,st->i-i);
545 st->i-=i;
546 i=-1;
547 }
548 }
549 } else {
550 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
551 st->finished=True;
552 }
553 }
554 }
555
556 void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
557 {
558 struct fdlog *st;
559
560 st=safe_malloc(sizeof(*st),"log_from_fd");
561 st->log=log;
562 st->fd=fd;
563 st->prefix=prefix;
564 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
565 st->i=0;
566 st->finished=False;
567
568 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,1,
569 prefix);
570 }
571
572 void log_module(dict_t *dict)
573 {
574 add_closure(dict,"logfile",logfile_apply);
575 add_closure(dict,"syslog",syslog_apply);
576 }