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)
20 /* Messages go to stdout/stderr */
21 if (class & message_level) {
22 if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
25 vfprintf(dest,message,args);
29 static void vMessage(uint32_t class, const char *message, va_list args)
31 #define MESSAGE_BUFLEN 1023
32 static char buff[MESSAGE_BUFLEN+1]={0,};
37 /* Messages go to the system log interface */
39 assert(bp < MESSAGE_BUFLEN);
40 vsnprintf(buff+bp,MESSAGE_BUFLEN-bp,message,args);
41 /* Each line is sent separately */
42 while ((nlp=strchr(buff,'\n'))) {
44 slilog(system_log,class,"%s",buff);
45 memmove(buff,nlp+1,strlen(nlp+1)+1);
48 vMessageFallback(class,message,args);
52 void Message(uint32_t class, const char *message, ...)
57 vMessage(class,message,ap);
61 static void MessageFallback(uint32_t class, const char *message, ...)
66 vMessageFallback(class,message,ap);
70 static NORETURN(vfatal(int status, bool_t perror, const char *message,
73 static void vfatal(int status, bool_t perror, const char *message,
80 enter_phase(PHASE_SHUTDOWN);
81 Message(M_FATAL, "secnet fatal error: ");
82 vMessage(M_FATAL, message, args);
84 Message(M_FATAL, ": %s\n",strerror(err));
86 Message(M_FATAL, "\n");
90 void fatal(const char *message, ...)
93 va_start(args,message);
94 vfatal(current_phase,False,message,args);
98 void fatal_status(int status, const char *message, ...)
101 va_start(args,message);
102 vfatal(status,False,message,args);
106 void fatal_perror(const char *message, ...)
109 va_start(args,message);
110 vfatal(current_phase,True,message,args);
114 void fatal_perror_status(int status, const char *message, ...)
117 va_start(args,message);
118 vfatal(status,True,message,args);
122 void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
123 cstring_t facility, const char *message, va_list args)
125 enter_phase(PHASE_SHUTDOWN);
127 if (maybe_f && ferror(maybe_f)) {
129 Message(M_FATAL, "error reading config file (%s, %s): %s",
130 facility, loc.file, strerror(errno));
131 } else if (maybe_f && feof(maybe_f)) {
133 Message(M_FATAL, "unexpected end of config file (%s, %s)",
135 } else if (loc.file && loc.line) {
136 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
138 } else if (!loc.file && loc.line) {
139 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
141 Message(M_FATAL, "config error (%s): ",facility);
144 vMessage(M_FATAL,message,args);
148 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
149 const char *message, ...)
153 va_start(args,message);
154 vcfgfatal_maybefile(maybe_f,loc,facility,message,args);
158 void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
162 va_start(args,message);
163 vcfgfatal_maybefile(0,loc,facility,message,args);
167 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
171 Message(M_FATAL, "error reading config file (%s): %s",
172 loc.file, strerror(errno));
174 } else if (feof(f)) {
175 Message(M_FATAL, "unexpected end of config file (%s)", loc.file);
180 /* Take a list of log closures and merge them */
183 struct loglist *next;
186 static void log_vmulti(void *sst, int class, const char *message, va_list args)
188 struct loglist *st=sst, *i;
190 if (secnet_is_daemon) {
191 for (i=st; i; i=i->next) {
192 i->l->vlog(i->l->st,class,message,args);
195 vMessage(class,message,args);
200 static void log_multi(void *st, int priority, const char *message, ...)
204 va_start(ap,message);
205 log_vmulti(st,priority,message,ap);
209 struct log_if *init_log(list_t *ll)
214 struct loglist *l=NULL, *n;
217 if (list_length(ll)==1) {
218 item=list_elem(ll,0);
219 cl=item->data.closure;
220 if (cl->type!=CL_LOG) {
221 cfgfatal(item->loc,"init_log","closure is not a logger");
223 return cl->interface;
225 while ((item=list_elem(ll,i++))) {
226 if (item->type!=t_closure) {
227 cfgfatal(item->loc,"init_log","item is not a closure");
229 cl=item->data.closure;
230 if (cl->type!=CL_LOG) {
231 cfgfatal(item->loc,"init_log","closure is not a logger");
233 n=safe_malloc(sizeof(*n),"init_log");
239 fatal("init_log: no log");
241 r=safe_malloc(sizeof(*r), "init_log");
257 static cstring_t months[]={
258 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
260 static void logfile_vlog(void *sst, int class, const char *message,
263 struct logfile *st=sst;
267 if (secnet_is_daemon && st->f) {
268 if (class&st->level) {
271 fprintf(st->f,"%s %2d %02d:%02d:%02d ",
272 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
274 vfprintf(st->f,message,args);
279 vMessageFallback(class,message,args);
280 MessageFallback(class,"\n");
284 static void logfile_log(void *state, int class, const char *message, ...)
288 va_start(ap,message);
289 logfile_vlog(state,class,message,ap);
293 static void logfile_hup_notify(void *sst, int signum)
295 struct logfile *st=sst;
297 f=fopen(st->logfile,"a");
299 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
300 "logfile: %s",strerror(errno));
304 logfile_log(st,M_INFO,"received SIGHUP");
308 static void logfile_phase_hook(void *sst, uint32_t new_phase)
310 struct logfile *st=sst;
314 f=fopen(st->logfile,"a");
315 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
316 st->loc.file,st->loc.line,st->logfile);
318 request_signal_notification(SIGHUP, logfile_hup_notify,st);
322 static struct flagstr message_class_table[]={
323 { "debug-config", M_DEBUG_CONFIG },
324 { "debug-phase", M_DEBUG_PHASE },
325 { "debug", M_DEBUG },
326 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
328 { "notice", M_NOTICE },
329 { "warning", M_WARNING },
331 { "security", M_SECURITY },
332 { "fatal", M_FATAL },
333 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
334 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
335 { "quiet", M_FATAL },
339 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
346 /* We should defer opening the logfile until the getresources
347 phase. We should defer writing into the logfile until after we
350 st=safe_malloc(sizeof(*st),"logfile_apply");
351 st->cl.description="logfile";
354 st->cl.interface=&st->ops;
356 st->ops.log=logfile_log;
357 st->ops.vlog=logfile_vlog;
361 item=list_elem(args,0);
362 if (!item || item->type!=t_dict) {
363 cfgfatal(loc,"logfile","argument must be a dictionary\n");
365 dict=item->data.dict;
367 st->logfile=dict_read_string(dict,"filename",True,"logfile",loc);
368 st->level=string_list_to_word(dict_lookup(dict,"class"),
369 message_class_table,"logfile");
371 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
373 return new_closure(&st->cl);
384 static int msgclass_to_syslogpriority(uint32_t m)
387 case M_DEBUG_CONFIG: return LOG_DEBUG;
388 case M_DEBUG_PHASE: return LOG_DEBUG;
389 case M_DEBUG: return LOG_DEBUG;
390 case M_INFO: return LOG_INFO;
391 case M_NOTICE: return LOG_NOTICE;
392 case M_WARNING: return LOG_WARNING;
393 case M_ERR: return LOG_ERR;
394 case M_SECURITY: return LOG_CRIT;
395 case M_FATAL: return LOG_EMERG;
396 default: return LOG_NOTICE;
400 static void syslog_vlog(void *sst, int class, const char *message,
403 struct syslog *st=sst;
406 vsyslog(msgclass_to_syslogpriority(class),message,args);
408 vMessageFallback(class,message,args);
409 MessageFallback(class,"\n");
413 static void syslog_log(void *sst, int priority, const char *message, ...)
417 va_start(ap,message);
418 syslog_vlog(sst,priority,message,ap);
422 static struct flagstr syslog_facility_table[]={
424 { "auth", LOG_AUTH },
427 { "authpriv", LOG_AUTHPRIV },
429 { "cron", LOG_CRON },
430 { "daemon", LOG_DAEMON },
431 { "kern", LOG_KERN },
432 { "local0", LOG_LOCAL0 },
433 { "local1", LOG_LOCAL1 },
434 { "local2", LOG_LOCAL2 },
435 { "local3", LOG_LOCAL3 },
436 { "local4", LOG_LOCAL4 },
437 { "local5", LOG_LOCAL5 },
438 { "local6", LOG_LOCAL6 },
439 { "local7", LOG_LOCAL7 },
441 { "mail", LOG_MAIL },
442 { "news", LOG_NEWS },
443 { "syslog", LOG_SYSLOG },
444 { "user", LOG_USER },
445 { "uucp", LOG_UUCP },
449 static void syslog_phase_hook(void *sst, uint32_t newphase)
451 struct syslog *st=sst;
454 openlog(st->ident,0,st->facility);
459 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
467 st=safe_malloc(sizeof(*st),"syslog_apply");
468 st->cl.description="syslog";
471 st->cl.interface=&st->ops;
473 st->ops.log=syslog_log;
474 st->ops.vlog=syslog_vlog;
476 item=list_elem(args,0);
477 if (!item || item->type!=t_dict)
478 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
481 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
482 facstr=dict_read_string(d, "facility", True, "syslog", loc);
483 st->facility=string_to_word(facstr,loc,
484 syslog_facility_table,"syslog");
486 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
488 return new_closure(&st->cl);
491 /* Read from a fd and output to a log. This is a quick hack to
492 support logging stderr, and needs code adding to tidy up before it
493 can be used for anything else. */
494 #define FDLOG_BUFSIZE 1024
504 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
507 struct fdlog *st=sst;
511 fds[0].events=POLLIN;
516 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
518 struct fdlog *st=sst;
522 if (fds[0].revents&POLLERR) {
525 if (fds[0].revents&POLLIN) {
526 remain=FDLOG_BUFSIZE-st->i-1;
528 st->buffer[FDLOG_BUFSIZE-1]=0;
529 st->log->log(st->log,M_WARNING,"%s: overlong line: %s",
530 st->prefix,st->buffer);
532 remain=FDLOG_BUFSIZE-1;
534 r=read(st->fd,st->buffer+st->i,remain);
537 for (i=0; i<st->i; i++) {
538 if (st->buffer[i]=='\n') {
540 st->log->log(st->log->st,M_INFO,"%s: %s",
541 st->prefix,st->buffer);
543 memmove(st->buffer,st->buffer+i,st->i-i);
549 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
555 void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
559 st=safe_malloc(sizeof(*st),"log_from_fd");
563 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
567 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,1,
571 void log_module(dict_t *dict)
573 add_closure(dict,"logfile",logfile_apply);
574 add_closure(dict,"syslog",syslog_apply);