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;
17 static void vMessage(uint32_t class, const char *message, va_list args)
20 #define MESSAGE_BUFLEN 1023
21 static char buff[MESSAGE_BUFLEN+1]={0,};
25 if (secnet_is_daemon) {
26 /* Messages go to the system log interface */
28 vsnprintf(buff+bp,MESSAGE_BUFLEN-bp,message,args);
29 /* Each line is sent separately */
30 while ((nlp=strchr(buff,'\n'))) {
32 slilog(system_log,class,buff);
33 memmove(buff,nlp+1,strlen(nlp+1)+1);
36 /* Messages go to stdout/stderr */
37 if (class & message_level) {
38 if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
41 vfprintf(dest,message,args);
46 void Message(uint32_t class, const char *message, ...)
51 vMessage(class,message,ap);
55 static NORETURN(vfatal(int status, bool_t perror, const char *message,
58 static void vfatal(int status, bool_t perror, const char *message,
65 enter_phase(PHASE_SHUTDOWN);
66 Message(M_FATAL, "secnet fatal error: ");
67 vMessage(M_FATAL, message, args);
69 Message(M_FATAL, ": %s\n",strerror(err));
71 Message(M_FATAL, "\n");
75 void fatal(const char *message, ...)
78 va_start(args,message);
79 vfatal(current_phase,False,message,args);
83 void fatal_status(int status, const char *message, ...)
86 va_start(args,message);
87 vfatal(status,False,message,args);
91 void fatal_perror(const char *message, ...)
94 va_start(args,message);
95 vfatal(current_phase,True,message,args);
99 void fatal_perror_status(int status, const char *message, ...)
102 va_start(args,message);
103 vfatal(status,True,message,args);
107 void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
108 cstring_t facility, const char *message, va_list args)
110 enter_phase(PHASE_SHUTDOWN);
112 if (maybe_f && ferror(maybe_f)) {
114 Message(M_FATAL, "error reading config file (%s, %s): %s",
115 facility, loc.file, strerror(errno));
116 } else if (maybe_f && feof(maybe_f)) {
118 Message(M_FATAL, "unexpected end of config file (%s, %s)",
120 } else if (loc.file && loc.line) {
121 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
123 } else if (!loc.file && loc.line) {
124 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
126 Message(M_FATAL, "config error (%s): ",facility);
129 vMessage(M_FATAL,message,args);
133 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
134 const char *message, ...)
138 va_start(args,message);
139 vcfgfatal_maybefile(maybe_f,loc,facility,message,args);
143 void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
147 va_start(args,message);
148 vcfgfatal_maybefile(0,loc,facility,message,args);
152 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
156 Message(M_FATAL, "error reading config file (%s): %s",
157 loc.file, strerror(errno));
159 } else if (feof(f)) {
160 Message(M_FATAL, "unexpected end of config file (%s)", loc.file);
165 /* Take a list of log closures and merge them */
168 struct loglist *next;
171 static void log_vmulti(void *sst, int class, const char *message, va_list args)
173 struct loglist *st=sst, *i;
175 if (secnet_is_daemon) {
176 for (i=st; i; i=i->next) {
177 i->l->vlog(i->l->st,class,message,args);
180 vMessage(class,message,args);
185 static void log_multi(void *st, int priority, const char *message, ...)
189 va_start(ap,message);
190 log_vmulti(st,priority,message,ap);
194 struct log_if *init_log(list_t *ll)
199 struct loglist *l=NULL, *n;
202 if (list_length(ll)==1) {
203 item=list_elem(ll,0);
204 cl=item->data.closure;
205 if (cl->type!=CL_LOG) {
206 cfgfatal(item->loc,"init_log","closure is not a logger");
208 return cl->interface;
210 while ((item=list_elem(ll,i++))) {
211 if (item->type!=t_closure) {
212 cfgfatal(item->loc,"init_log","item is not a closure");
214 cl=item->data.closure;
215 if (cl->type!=CL_LOG) {
216 cfgfatal(item->loc,"init_log","closure is not a logger");
218 n=safe_malloc(sizeof(*n),"init_log");
224 fatal("init_log: no log");
226 r=safe_malloc(sizeof(*r), "init_log");
242 static cstring_t months[]={
243 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
245 static void logfile_vlog(void *sst, int class, const char *message,
248 struct logfile *st=sst;
252 if (secnet_is_daemon && st->f) {
253 if (class&st->level) {
256 fprintf(st->f,"%s %2d %02d:%02d:%02d ",
257 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
259 vfprintf(st->f,message,args);
264 vMessage(class,message,args);
269 static void logfile_log(void *state, int class, const char *message, ...)
273 va_start(ap,message);
274 logfile_vlog(state,class,message,ap);
278 static void logfile_hup_notify(void *sst, int signum)
280 struct logfile *st=sst;
282 f=fopen(st->logfile,"a");
284 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
285 "logfile: %s",strerror(errno));
289 logfile_log(st,M_INFO,"received SIGHUP");
293 static void logfile_phase_hook(void *sst, uint32_t new_phase)
295 struct logfile *st=sst;
299 f=fopen(st->logfile,"a");
300 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
301 st->loc.file,st->loc.line,st->logfile);
303 request_signal_notification(SIGHUP, logfile_hup_notify,st);
307 static struct flagstr message_class_table[]={
308 { "debug-config", M_DEBUG_CONFIG },
309 { "debug-phase", M_DEBUG_PHASE },
310 { "debug", M_DEBUG },
311 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
313 { "notice", M_NOTICE },
314 { "warning", M_WARNING },
316 { "security", M_SECURITY },
317 { "fatal", M_FATAL },
318 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
319 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
320 { "quiet", M_FATAL },
324 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
331 /* We should defer opening the logfile until the getresources
332 phase. We should defer writing into the logfile until after we
335 st=safe_malloc(sizeof(*st),"logfile_apply");
336 st->cl.description="logfile";
339 st->cl.interface=&st->ops;
341 st->ops.log=logfile_log;
342 st->ops.vlog=logfile_vlog;
346 item=list_elem(args,0);
347 if (!item || item->type!=t_dict) {
348 cfgfatal(loc,"logfile","argument must be a dictionary\n");
350 dict=item->data.dict;
352 st->logfile=dict_read_string(dict,"filename",True,"logfile",loc);
353 st->level=string_list_to_word(dict_lookup(dict,"class"),
354 message_class_table,"logfile");
356 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
358 return new_closure(&st->cl);
369 static int msgclass_to_syslogpriority(uint32_t m)
372 case M_DEBUG_CONFIG: return LOG_DEBUG;
373 case M_DEBUG_PHASE: return LOG_DEBUG;
374 case M_DEBUG: return LOG_DEBUG;
375 case M_INFO: return LOG_INFO;
376 case M_NOTICE: return LOG_NOTICE;
377 case M_WARNING: return LOG_WARNING;
378 case M_ERR: return LOG_ERR;
379 case M_SECURITY: return LOG_CRIT;
380 case M_FATAL: return LOG_EMERG;
381 default: return LOG_NOTICE;
385 static void syslog_vlog(void *sst, int class, const char *message,
388 struct syslog *st=sst;
391 vsyslog(msgclass_to_syslogpriority(class),message,args);
393 vMessage(class,message,args);
398 static void syslog_log(void *sst, int priority, const char *message, ...)
402 va_start(ap,message);
403 syslog_vlog(sst,priority,message,ap);
407 static struct flagstr syslog_facility_table[]={
409 { "auth", LOG_AUTH },
412 { "authpriv", LOG_AUTHPRIV },
414 { "cron", LOG_CRON },
415 { "daemon", LOG_DAEMON },
416 { "kern", LOG_KERN },
417 { "local0", LOG_LOCAL0 },
418 { "local1", LOG_LOCAL1 },
419 { "local2", LOG_LOCAL2 },
420 { "local3", LOG_LOCAL3 },
421 { "local4", LOG_LOCAL4 },
422 { "local5", LOG_LOCAL5 },
423 { "local6", LOG_LOCAL6 },
424 { "local7", LOG_LOCAL7 },
426 { "mail", LOG_MAIL },
427 { "news", LOG_NEWS },
428 { "syslog", LOG_SYSLOG },
429 { "user", LOG_USER },
430 { "uucp", LOG_UUCP },
434 static void syslog_phase_hook(void *sst, uint32_t newphase)
436 struct syslog *st=sst;
439 openlog(st->ident,0,st->facility);
444 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
452 st=safe_malloc(sizeof(*st),"syslog_apply");
453 st->cl.description="syslog";
456 st->cl.interface=&st->ops;
458 st->ops.log=syslog_log;
459 st->ops.vlog=syslog_vlog;
461 item=list_elem(args,0);
462 if (!item || item->type!=t_dict)
463 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
466 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
467 facstr=dict_read_string(d, "facility", True, "syslog", loc);
468 st->facility=string_to_word(facstr,loc,
469 syslog_facility_table,"syslog");
471 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
473 return new_closure(&st->cl);
476 /* Read from a fd and output to a log. This is a quick hack to
477 support logging stderr, and needs code adding to tidy up before it
478 can be used for anything else. */
479 #define FDLOG_BUFSIZE 1024
489 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
491 const struct timeval *tv_now, uint64_t *now)
493 struct fdlog *st=sst;
497 fds[0].events=POLLIN;
502 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds,
503 const struct timeval *tv_now, uint64_t *now)
505 struct fdlog *st=sst;
509 if (fds[0].revents&POLLERR) {
512 if (fds[0].revents&POLLIN) {
513 remain=FDLOG_BUFSIZE-st->i-1;
515 st->buffer[FDLOG_BUFSIZE-1]=0;
516 st->log->log(st->log,M_WARNING,"%s: overlong line: %s",
517 st->prefix,st->buffer);
519 remain=FDLOG_BUFSIZE-1;
521 r=read(st->fd,st->buffer+st->i,remain);
524 for (i=0; i<st->i; i++) {
525 if (st->buffer[i]=='\n') {
527 st->log->log(st->log->st,M_INFO,"%s: %s",
528 st->prefix,st->buffer);
530 memmove(st->buffer,st->buffer+i,st->i-i);
536 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
542 void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
546 st=safe_malloc(sizeof(*st),"log_from_fd");
550 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
554 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,1,
558 init_module log_module;
559 void log_module(dict_t *dict)
561 add_closure(dict,"logfile",logfile_apply);
562 add_closure(dict,"syslog",syslog_apply);