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 vMessageFallback(uint32_t class, const char *message, va_list args)
19 static void vMessageFallback(uint32_t class, const char *message, va_list args)
22 /* Messages go to stdout/stderr */
23 if (class & message_level) {
24 if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
27 vfprintf(dest,message,args);
31 static void vMessage(uint32_t class, const char *message, va_list args)
35 /* Messages go to the system log interface */
36 vslilog_part(system_log, class, message, args);
38 vMessageFallback(class,message,args);
42 void Message(uint32_t class, const char *message, ...)
47 vMessage(class,message,ap);
51 static void MessageFallback(uint32_t class, const char *message, ...)
53 static void MessageFallback(uint32_t class, const char *message, ...)
58 vMessageFallback(class,message,ap);
62 static NORETURN(vfatal(int status, bool_t perror, const char *message,
65 static void vfatal(int status, bool_t perror, const char *message,
72 enter_phase(PHASE_SHUTDOWN);
73 Message(M_FATAL, "secnet fatal error: ");
74 vMessage(M_FATAL, message, args);
76 Message(M_FATAL, ": %s\n",strerror(err));
78 Message(M_FATAL, "\n");
82 void fatal(const char *message, ...)
85 va_start(args,message);
86 vfatal(current_phase,False,message,args);
90 void fatal_status(int status, const char *message, ...)
93 va_start(args,message);
94 vfatal(status,False,message,args);
98 void fatal_perror(const char *message, ...)
101 va_start(args,message);
102 vfatal(current_phase,True,message,args);
106 void fatal_perror_status(int status, const char *message, ...)
109 va_start(args,message);
110 vfatal(status,True,message,args);
114 void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
115 cstring_t facility, const char *message, va_list args)
117 enter_phase(PHASE_SHUTDOWN);
119 if (maybe_f && ferror(maybe_f)) {
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)) {
125 Message(M_FATAL, "unexpected end of config file (%s, %s)",
127 } else if (loc.file && loc.line) {
128 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
130 } else if (!loc.file && loc.line) {
131 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
133 Message(M_FATAL, "config error (%s): ",facility);
136 vMessage(M_FATAL,message,args);
140 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
141 const char *message, ...)
145 va_start(args,message);
146 vcfgfatal_maybefile(maybe_f,loc,facility,message,args);
150 void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
154 va_start(args,message);
155 vcfgfatal_maybefile(0,loc,facility,message,args);
159 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
163 Message(M_FATAL, "error reading config file (%s): %s\n",
164 loc.file, strerror(errno));
166 } else if (feof(f)) {
167 Message(M_FATAL, "unexpected end of config file (%s)\n", loc.file);
172 /* Take a list of log closures and merge them */
175 struct loglist *next;
178 static void log_vmulti(void *sst, int class, const char *message, va_list args)
180 struct loglist *st=sst, *i;
182 if (secnet_is_daemon) {
183 for (i=st; i; i=i->next) {
184 vslilog(i->l,class,message,args);
187 vMessage(class,message,args);
192 void lg_vperror(struct log_if *lg, const char *desc, struct cloc *loc,
193 int class, int errnoval, const char *fmt, va_list al)
195 int status=current_phase;
202 enter_phase(PHASE_SHUTDOWN);
204 slilog_part(lg,class,"%s",desc);
206 slilog_part(lg,class," (%s:%d)",loc->file,loc->line);
207 slilog_part(lg,class,": ");
208 vslilog_part(lg,class,fmt,al);
210 slilog_part(lg,class,": %s",strerror(errnoval));
211 slilog_part(lg,class,"\n");
219 void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc,
220 int class, int errnoval, const char *fmt, ...)
224 lg_vperror(lg,desc,loc,class,errnoval,fmt,al);
228 void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc,
229 int class, int status, const char *progname)
232 lg_perror(lg,desc,loc,class,0,"%s exited",progname);
233 else if (WIFEXITED(status))
234 lg_perror(lg,desc,loc,class,0,"%s exited with error exit status %d",
235 progname,WEXITSTATUS(status));
236 else if (WIFSIGNALED(status))
237 lg_perror(lg,desc,loc,class,0,"%s died due to fatal signal %s (%d)%s",
238 progname,strsignal(WTERMSIG(status)),WTERMSIG(status),
239 WCOREDUMP(status)?" (core dumped)":"");
241 lg_perror(lg,desc,loc,class,0,"%s died with unknown wait status %d",
245 struct log_if *init_log(list_t *ll)
250 struct loglist *l=NULL, *n;
253 if (list_length(ll)==1) {
254 item=list_elem(ll,0);
255 cl=item->data.closure;
256 if (cl->type!=CL_LOG) {
257 cfgfatal(item->loc,"init_log","closure is not a logger");
259 return cl->interface;
261 while ((item=list_elem(ll,i++))) {
262 if (item->type!=t_closure) {
263 cfgfatal(item->loc,"init_log","item is not a closure");
265 cl=item->data.closure;
266 if (cl->type!=CL_LOG) {
267 cfgfatal(item->loc,"init_log","closure is not a logger");
269 n=safe_malloc(sizeof(*n),"init_log");
275 fatal("init_log: no log");
277 r=safe_malloc(sizeof(*r), "init_log");
279 r->vlogfn=log_vmulti;
293 static cstring_t months[]={
294 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
296 static void logfile_vlog(void *sst, int class, const char *message,
299 struct logfile *st=sst;
303 if (secnet_is_daemon && st->f) {
304 if (class&st->level) {
307 fprintf(st->f,"%s %2d %02d:%02d:%02d ",
308 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
310 vfprintf(st->f,message,args);
315 vMessageFallback(class,message,args);
316 MessageFallback(class,"\n");
320 static void logfile_log(void *state, int class, const char *message, ...)
322 static void logfile_log(void *state, int class, const char *message, ...)
326 va_start(ap,message);
327 logfile_vlog(state,class,message,ap);
331 static void logfile_hup_notify(void *sst, int signum)
333 struct logfile *st=sst;
335 f=fopen(st->logfile,"a");
337 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
338 "logfile: %s",strerror(errno));
342 logfile_log(st,M_INFO,"received SIGHUP");
346 static void logfile_phase_hook(void *sst, uint32_t new_phase)
348 struct logfile *st=sst;
352 f=fopen(st->logfile,"a");
353 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
354 st->loc.file,st->loc.line,st->logfile);
356 request_signal_notification(SIGHUP, logfile_hup_notify,st);
360 static struct flagstr message_class_table[]={
361 { "debug-config", M_DEBUG_CONFIG },
362 { "debug-phase", M_DEBUG_PHASE },
363 { "debug", M_DEBUG },
364 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
366 { "notice", M_NOTICE },
367 { "warning", M_WARNING },
369 { "security", M_SECURITY },
370 { "fatal", M_FATAL },
371 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
372 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
373 { "quiet", M_FATAL },
377 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
384 /* We should defer opening the logfile until the getresources
385 phase. We should defer writing into the logfile until after we
388 st=safe_malloc(sizeof(*st),"logfile_apply");
389 st->cl.description="logfile";
392 st->cl.interface=&st->ops;
394 st->ops.vlogfn=logfile_vlog;
399 item=list_elem(args,0);
400 if (!item || item->type!=t_dict) {
401 cfgfatal(loc,"logfile","argument must be a dictionary\n");
403 dict=item->data.dict;
405 st->logfile=dict_read_string(dict,"filename",True,"logfile",loc);
406 st->level=string_list_to_word(dict_lookup(dict,"class"),
407 message_class_table,"logfile");
409 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
411 return new_closure(&st->cl);
422 static int msgclass_to_syslogpriority(uint32_t m)
425 case M_DEBUG_CONFIG: return LOG_DEBUG;
426 case M_DEBUG_PHASE: return LOG_DEBUG;
427 case M_DEBUG: return LOG_DEBUG;
428 case M_INFO: return LOG_INFO;
429 case M_NOTICE: return LOG_NOTICE;
430 case M_WARNING: return LOG_WARNING;
431 case M_ERR: return LOG_ERR;
432 case M_SECURITY: return LOG_CRIT;
433 case M_FATAL: return LOG_EMERG;
434 default: return LOG_NOTICE;
438 static void syslog_vlog(void *sst, int class, const char *message,
441 static void syslog_vlog(void *sst, int class, const char *message,
444 struct syslog *st=sst;
447 vsyslog(msgclass_to_syslogpriority(class),message,args);
449 vMessageFallback(class,message,args);
450 MessageFallback(class,"\n");
454 static struct flagstr syslog_facility_table[]={
456 { "auth", LOG_AUTH },
459 { "authpriv", LOG_AUTHPRIV },
461 { "cron", LOG_CRON },
462 { "daemon", LOG_DAEMON },
463 { "kern", LOG_KERN },
464 { "local0", LOG_LOCAL0 },
465 { "local1", LOG_LOCAL1 },
466 { "local2", LOG_LOCAL2 },
467 { "local3", LOG_LOCAL3 },
468 { "local4", LOG_LOCAL4 },
469 { "local5", LOG_LOCAL5 },
470 { "local6", LOG_LOCAL6 },
471 { "local7", LOG_LOCAL7 },
473 { "mail", LOG_MAIL },
474 { "news", LOG_NEWS },
475 { "syslog", LOG_SYSLOG },
476 { "user", LOG_USER },
477 { "uucp", LOG_UUCP },
481 static void syslog_phase_hook(void *sst, uint32_t newphase)
483 struct syslog *st=sst;
486 openlog(st->ident,0,st->facility);
491 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
499 st=safe_malloc(sizeof(*st),"syslog_apply");
500 st->cl.description="syslog";
503 st->cl.interface=&st->ops;
505 st->ops.vlogfn=syslog_vlog;
508 item=list_elem(args,0);
509 if (!item || item->type!=t_dict)
510 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
513 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
514 facstr=dict_read_string(d, "facility", True, "syslog", loc);
515 st->facility=string_to_word(facstr,loc,
516 syslog_facility_table,"syslog");
518 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
520 return new_closure(&st->cl);
523 /* Read from a fd and output to a log. This is a quick hack to
524 support logging stderr, and needs code adding to tidy up before it
525 can be used for anything else. */
526 #define FDLOG_BUFSIZE 1024
536 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
539 struct fdlog *st=sst;
541 BEFOREPOLL_WANT_FDS(1);
543 fds[0].events=POLLIN;
545 BEFOREPOLL_WANT_FDS(0);
550 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
552 struct fdlog *st=sst;
556 if (fds[0].revents&POLLERR) {
559 if (fds[0].revents&POLLIN) {
560 remain=FDLOG_BUFSIZE-st->i-1;
562 st->buffer[FDLOG_BUFSIZE-1]=0;
563 slilog(st->log,M_WARNING,"%s: overlong line: %s",
564 st->prefix,st->buffer);
566 remain=FDLOG_BUFSIZE-1;
568 r=read(st->fd,st->buffer+st->i,remain);
571 for (i=0; i<st->i; i++) {
572 if (st->buffer[i]=='\n') {
574 slilog(st->log,M_INFO,"%s: %s",
575 st->prefix,st->buffer);
577 memmove(st->buffer,st->buffer+i,st->i-i);
582 } else if (errno==EINTR || iswouldblock(errno)) {
584 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
590 void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
594 st=safe_malloc(sizeof(*st),"log_from_fd");
598 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
604 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,
608 void log_module(dict_t *dict)
610 add_closure(dict,"logfile",logfile_apply);
611 add_closure(dict,"syslog",syslog_apply);