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