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)
33 #define MESSAGE_BUFLEN 1023
34 static char buff[MESSAGE_BUFLEN+1]={0,};
39 /* Messages go to the system log interface */
41 assert(bp < MESSAGE_BUFLEN);
42 vsnprintf(buff+bp,MESSAGE_BUFLEN-bp,message,args);
43 buff[sizeof(buff)-2] = '\n';
44 buff[sizeof(buff)-1] = '\0';
45 /* Each line is sent separately */
46 while ((nlp=strchr(buff,'\n'))) {
48 slilog(system_log,class,"%s",buff);
49 memmove(buff,nlp+1,strlen(nlp+1)+1);
52 vMessageFallback(class,message,args);
56 void Message(uint32_t class, const char *message, ...)
61 vMessage(class,message,ap);
65 static void MessageFallback(uint32_t class, const char *message, ...)
67 static void MessageFallback(uint32_t class, const char *message, ...)
72 vMessageFallback(class,message,ap);
76 static NORETURN(vfatal(int status, bool_t perror, const char *message,
79 static void vfatal(int status, bool_t perror, const char *message,
86 enter_phase(PHASE_SHUTDOWN);
87 Message(M_FATAL, "secnet fatal error: ");
88 vMessage(M_FATAL, message, args);
90 Message(M_FATAL, ": %s\n",strerror(err));
92 Message(M_FATAL, "\n");
96 void fatal(const char *message, ...)
99 va_start(args,message);
100 vfatal(current_phase,False,message,args);
104 void fatal_status(int status, const char *message, ...)
107 va_start(args,message);
108 vfatal(status,False,message,args);
112 void fatal_perror(const char *message, ...)
115 va_start(args,message);
116 vfatal(current_phase,True,message,args);
120 void fatal_perror_status(int status, const char *message, ...)
123 va_start(args,message);
124 vfatal(status,True,message,args);
128 void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
129 cstring_t facility, const char *message, va_list args)
131 enter_phase(PHASE_SHUTDOWN);
133 if (maybe_f && ferror(maybe_f)) {
135 Message(M_FATAL, "error reading config file (%s, %s): %s",
136 facility, loc.file, strerror(errno));
137 } else if (maybe_f && feof(maybe_f)) {
139 Message(M_FATAL, "unexpected end of config file (%s, %s)",
141 } else if (loc.file && loc.line) {
142 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
144 } else if (!loc.file && loc.line) {
145 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
147 Message(M_FATAL, "config error (%s): ",facility);
150 vMessage(M_FATAL,message,args);
154 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
155 const char *message, ...)
159 va_start(args,message);
160 vcfgfatal_maybefile(maybe_f,loc,facility,message,args);
164 void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
168 va_start(args,message);
169 vcfgfatal_maybefile(0,loc,facility,message,args);
173 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
177 Message(M_FATAL, "error reading config file (%s): %s\n",
178 loc.file, strerror(errno));
180 } else if (feof(f)) {
181 Message(M_FATAL, "unexpected end of config file (%s)\n", loc.file);
186 /* Take a list of log closures and merge them */
189 struct loglist *next;
192 static void log_vmulti(void *sst, int class, const char *message, va_list args)
194 struct loglist *st=sst, *i;
196 if (secnet_is_daemon) {
197 for (i=st; i; i=i->next) {
198 vslilog(i->l,class,message,args);
201 vMessage(class,message,args);
206 static void log_multi(void *st, int priority, const char *message, ...)
208 static void log_multi(void *st, int priority, const char *message, ...)
212 va_start(ap,message);
213 log_vmulti(st,priority,message,ap);
217 struct log_if *init_log(list_t *ll)
222 struct loglist *l=NULL, *n;
225 if (list_length(ll)==1) {
226 item=list_elem(ll,0);
227 cl=item->data.closure;
228 if (cl->type!=CL_LOG) {
229 cfgfatal(item->loc,"init_log","closure is not a logger");
231 return cl->interface;
233 while ((item=list_elem(ll,i++))) {
234 if (item->type!=t_closure) {
235 cfgfatal(item->loc,"init_log","item is not a closure");
237 cl=item->data.closure;
238 if (cl->type!=CL_LOG) {
239 cfgfatal(item->loc,"init_log","closure is not a logger");
241 n=safe_malloc(sizeof(*n),"init_log");
247 fatal("init_log: no log");
249 r=safe_malloc(sizeof(*r), "init_log");
252 r->vlogfn=log_vmulti;
265 static cstring_t months[]={
266 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
268 static void logfile_vlog(void *sst, int class, const char *message,
271 struct logfile *st=sst;
275 if (secnet_is_daemon && st->f) {
276 if (class&st->level) {
279 fprintf(st->f,"%s %2d %02d:%02d:%02d ",
280 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
282 vfprintf(st->f,message,args);
287 vMessageFallback(class,message,args);
288 MessageFallback(class,"\n");
292 static void logfile_log(void *state, int class, const char *message, ...)
294 static void logfile_log(void *state, int class, const char *message, ...)
298 va_start(ap,message);
299 logfile_vlog(state,class,message,ap);
303 static void logfile_hup_notify(void *sst, int signum)
305 struct logfile *st=sst;
307 f=fopen(st->logfile,"a");
309 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
310 "logfile: %s",strerror(errno));
314 logfile_log(st,M_INFO,"received SIGHUP");
318 static void logfile_phase_hook(void *sst, uint32_t new_phase)
320 struct logfile *st=sst;
324 f=fopen(st->logfile,"a");
325 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
326 st->loc.file,st->loc.line,st->logfile);
328 request_signal_notification(SIGHUP, logfile_hup_notify,st);
332 static struct flagstr message_class_table[]={
333 { "debug-config", M_DEBUG_CONFIG },
334 { "debug-phase", M_DEBUG_PHASE },
335 { "debug", M_DEBUG },
336 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
338 { "notice", M_NOTICE },
339 { "warning", M_WARNING },
341 { "security", M_SECURITY },
342 { "fatal", M_FATAL },
343 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
344 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
345 { "quiet", M_FATAL },
349 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
356 /* We should defer opening the logfile until the getresources
357 phase. We should defer writing into the logfile until after we
360 st=safe_malloc(sizeof(*st),"logfile_apply");
361 st->cl.description="logfile";
364 st->cl.interface=&st->ops;
366 st->ops.logfn=logfile_log;
367 st->ops.vlogfn=logfile_vlog;
371 item=list_elem(args,0);
372 if (!item || item->type!=t_dict) {
373 cfgfatal(loc,"logfile","argument must be a dictionary\n");
375 dict=item->data.dict;
377 st->logfile=dict_read_string(dict,"filename",True,"logfile",loc);
378 st->level=string_list_to_word(dict_lookup(dict,"class"),
379 message_class_table,"logfile");
381 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
383 return new_closure(&st->cl);
394 static int msgclass_to_syslogpriority(uint32_t m)
397 case M_DEBUG_CONFIG: return LOG_DEBUG;
398 case M_DEBUG_PHASE: return LOG_DEBUG;
399 case M_DEBUG: return LOG_DEBUG;
400 case M_INFO: return LOG_INFO;
401 case M_NOTICE: return LOG_NOTICE;
402 case M_WARNING: return LOG_WARNING;
403 case M_ERR: return LOG_ERR;
404 case M_SECURITY: return LOG_CRIT;
405 case M_FATAL: return LOG_EMERG;
406 default: return LOG_NOTICE;
410 static void syslog_vlog(void *sst, int class, const char *message,
413 static void syslog_vlog(void *sst, int class, const char *message,
416 struct syslog *st=sst;
419 vsyslog(msgclass_to_syslogpriority(class),message,args);
421 vMessageFallback(class,message,args);
422 MessageFallback(class,"\n");
426 static void syslog_log(void *sst, int priority, const char *message, ...)
428 static void syslog_log(void *sst, int priority, const char *message, ...)
432 va_start(ap,message);
433 syslog_vlog(sst,priority,message,ap);
437 static struct flagstr syslog_facility_table[]={
439 { "auth", LOG_AUTH },
442 { "authpriv", LOG_AUTHPRIV },
444 { "cron", LOG_CRON },
445 { "daemon", LOG_DAEMON },
446 { "kern", LOG_KERN },
447 { "local0", LOG_LOCAL0 },
448 { "local1", LOG_LOCAL1 },
449 { "local2", LOG_LOCAL2 },
450 { "local3", LOG_LOCAL3 },
451 { "local4", LOG_LOCAL4 },
452 { "local5", LOG_LOCAL5 },
453 { "local6", LOG_LOCAL6 },
454 { "local7", LOG_LOCAL7 },
456 { "mail", LOG_MAIL },
457 { "news", LOG_NEWS },
458 { "syslog", LOG_SYSLOG },
459 { "user", LOG_USER },
460 { "uucp", LOG_UUCP },
464 static void syslog_phase_hook(void *sst, uint32_t newphase)
466 struct syslog *st=sst;
469 openlog(st->ident,0,st->facility);
474 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
482 st=safe_malloc(sizeof(*st),"syslog_apply");
483 st->cl.description="syslog";
486 st->cl.interface=&st->ops;
488 st->ops.logfn=syslog_log;
489 st->ops.vlogfn=syslog_vlog;
491 item=list_elem(args,0);
492 if (!item || item->type!=t_dict)
493 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
496 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
497 facstr=dict_read_string(d, "facility", True, "syslog", loc);
498 st->facility=string_to_word(facstr,loc,
499 syslog_facility_table,"syslog");
501 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
503 return new_closure(&st->cl);
506 /* Read from a fd and output to a log. This is a quick hack to
507 support logging stderr, and needs code adding to tidy up before it
508 can be used for anything else. */
509 #define FDLOG_BUFSIZE 1024
519 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
522 struct fdlog *st=sst;
526 fds[0].events=POLLIN;
531 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
533 struct fdlog *st=sst;
537 if (fds[0].revents&POLLERR) {
540 if (fds[0].revents&POLLIN) {
541 remain=FDLOG_BUFSIZE-st->i-1;
543 st->buffer[FDLOG_BUFSIZE-1]=0;
544 slilog(st->log,M_WARNING,"%s: overlong line: %s",
545 st->prefix,st->buffer);
547 remain=FDLOG_BUFSIZE-1;
549 r=read(st->fd,st->buffer+st->i,remain);
552 for (i=0; i<st->i; i++) {
553 if (st->buffer[i]=='\n') {
555 slilog(st->log,M_INFO,"%s: %s",
556 st->prefix,st->buffer);
558 memmove(st->buffer,st->buffer+i,st->i-i);
564 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
570 void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
574 st=safe_malloc(sizeof(*st),"log_from_fd");
578 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
582 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,1,
586 void log_module(dict_t *dict)
588 add_closure(dict,"logfile",logfile_apply);
589 add_closure(dict,"syslog",syslog_apply);