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 static void log_multi(void *st, int priority, const char *message, ...)
194 static void log_multi(void *st, int priority, const char *message, ...)
198 va_start(ap,message);
199 log_vmulti(st,priority,message,ap);
203 struct log_if *init_log(list_t *ll)
208 struct loglist *l=NULL, *n;
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");
217 return cl->interface;
219 while ((item=list_elem(ll,i++))) {
220 if (item->type!=t_closure) {
221 cfgfatal(item->loc,"init_log","item is not a closure");
223 cl=item->data.closure;
224 if (cl->type!=CL_LOG) {
225 cfgfatal(item->loc,"init_log","closure is not a logger");
227 n=safe_malloc(sizeof(*n),"init_log");
233 fatal("init_log: no log");
235 r=safe_malloc(sizeof(*r), "init_log");
237 r->vlogfn=log_vmulti;
251 static cstring_t months[]={
252 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
254 static void logfile_vlog(void *sst, int class, const char *message,
257 struct logfile *st=sst;
261 if (secnet_is_daemon && st->f) {
262 if (class&st->level) {
265 fprintf(st->f,"%s %2d %02d:%02d:%02d ",
266 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
268 vfprintf(st->f,message,args);
273 vMessageFallback(class,message,args);
274 MessageFallback(class,"\n");
278 static void logfile_log(void *state, int class, const char *message, ...)
280 static void logfile_log(void *state, int class, const char *message, ...)
284 va_start(ap,message);
285 logfile_vlog(state,class,message,ap);
289 static void logfile_hup_notify(void *sst, int signum)
291 struct logfile *st=sst;
293 f=fopen(st->logfile,"a");
295 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
296 "logfile: %s",strerror(errno));
300 logfile_log(st,M_INFO,"received SIGHUP");
304 static void logfile_phase_hook(void *sst, uint32_t new_phase)
306 struct logfile *st=sst;
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);
314 request_signal_notification(SIGHUP, logfile_hup_notify,st);
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 },
324 { "notice", M_NOTICE },
325 { "warning", M_WARNING },
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 },
335 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
342 /* We should defer opening the logfile until the getresources
343 phase. We should defer writing into the logfile until after we
346 st=safe_malloc(sizeof(*st),"logfile_apply");
347 st->cl.description="logfile";
350 st->cl.interface=&st->ops;
352 st->ops.vlogfn=logfile_vlog;
357 item=list_elem(args,0);
358 if (!item || item->type!=t_dict) {
359 cfgfatal(loc,"logfile","argument must be a dictionary\n");
361 dict=item->data.dict;
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");
367 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
369 return new_closure(&st->cl);
380 static int msgclass_to_syslogpriority(uint32_t 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;
396 static void syslog_vlog(void *sst, int class, const char *message,
399 static void syslog_vlog(void *sst, int class, const char *message,
402 struct syslog *st=sst;
405 vsyslog(msgclass_to_syslogpriority(class),message,args);
407 vMessageFallback(class,message,args);
408 MessageFallback(class,"\n");
412 static void syslog_log(void *sst, int priority, const char *message, ...)
414 static void syslog_log(void *sst, int priority, const char *message, ...)
418 va_start(ap,message);
419 syslog_vlog(sst,priority,message,ap);
423 static struct flagstr syslog_facility_table[]={
425 { "auth", LOG_AUTH },
428 { "authpriv", LOG_AUTHPRIV },
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 },
442 { "mail", LOG_MAIL },
443 { "news", LOG_NEWS },
444 { "syslog", LOG_SYSLOG },
445 { "user", LOG_USER },
446 { "uucp", LOG_UUCP },
450 static void syslog_phase_hook(void *sst, uint32_t newphase)
452 struct syslog *st=sst;
455 openlog(st->ident,0,st->facility);
460 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
468 st=safe_malloc(sizeof(*st),"syslog_apply");
469 st->cl.description="syslog";
472 st->cl.interface=&st->ops;
474 st->ops.vlogfn=syslog_vlog;
477 item=list_elem(args,0);
478 if (!item || item->type!=t_dict)
479 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
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");
487 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
489 return new_closure(&st->cl);
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
505 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
508 struct fdlog *st=sst;
512 fds[0].events=POLLIN;
517 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
519 struct fdlog *st=sst;
523 if (fds[0].revents&POLLERR) {
526 if (fds[0].revents&POLLIN) {
527 remain=FDLOG_BUFSIZE-st->i-1;
529 st->buffer[FDLOG_BUFSIZE-1]=0;
530 slilog(st->log,M_WARNING,"%s: overlong line: %s",
531 st->prefix,st->buffer);
533 remain=FDLOG_BUFSIZE-1;
535 r=read(st->fd,st->buffer+st->i,remain);
538 for (i=0; i<st->i; i++) {
539 if (st->buffer[i]=='\n') {
541 slilog(st->log,M_INFO,"%s: %s",
542 st->prefix,st->buffer);
544 memmove(st->buffer,st->buffer+i,st->i-i);
550 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
556 void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
560 st=safe_malloc(sizeof(*st),"log_from_fd");
564 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
568 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,1,
572 void log_module(dict_t *dict)
574 add_closure(dict,"logfile",logfile_apply);
575 add_closure(dict,"syslog",syslog_apply);