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 buff[sizeof(buff)-2] = '\n';
42 buff[sizeof(buff)-1] = '\0';
43 /* Each line is sent separately */
44 while ((nlp=strchr(buff,'\n'))) {
46 slilog(system_log,class,"%s",buff);
47 memmove(buff,nlp+1,strlen(nlp+1)+1);
50 vMessageFallback(class,message,args);
54 void Message(uint32_t class, const char *message, ...)
59 vMessage(class,message,ap);
63 static void MessageFallback(uint32_t class, const char *message, ...)
68 vMessageFallback(class,message,ap);
72 static NORETURN(vfatal(int status, bool_t perror, const char *message,
75 static void vfatal(int status, bool_t perror, const char *message,
82 enter_phase(PHASE_SHUTDOWN);
83 Message(M_FATAL, "secnet fatal error: ");
84 vMessage(M_FATAL, message, args);
86 Message(M_FATAL, ": %s\n",strerror(err));
88 Message(M_FATAL, "\n");
92 void fatal(const char *message, ...)
95 va_start(args,message);
96 vfatal(current_phase,False,message,args);
100 void fatal_status(int status, const char *message, ...)
103 va_start(args,message);
104 vfatal(status,False,message,args);
108 void fatal_perror(const char *message, ...)
111 va_start(args,message);
112 vfatal(current_phase,True,message,args);
116 void fatal_perror_status(int status, const char *message, ...)
119 va_start(args,message);
120 vfatal(status,True,message,args);
124 void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
125 cstring_t facility, const char *message, va_list args)
127 enter_phase(PHASE_SHUTDOWN);
129 if (maybe_f && ferror(maybe_f)) {
131 Message(M_FATAL, "error reading config file (%s, %s): %s",
132 facility, loc.file, strerror(errno));
133 } else if (maybe_f && feof(maybe_f)) {
135 Message(M_FATAL, "unexpected end of config file (%s, %s)",
137 } else if (loc.file && loc.line) {
138 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
140 } else if (!loc.file && loc.line) {
141 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
143 Message(M_FATAL, "config error (%s): ",facility);
146 vMessage(M_FATAL,message,args);
150 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
151 const char *message, ...)
155 va_start(args,message);
156 vcfgfatal_maybefile(maybe_f,loc,facility,message,args);
160 void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
164 va_start(args,message);
165 vcfgfatal_maybefile(0,loc,facility,message,args);
169 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
173 Message(M_FATAL, "error reading config file (%s): %s\n",
174 loc.file, strerror(errno));
176 } else if (feof(f)) {
177 Message(M_FATAL, "unexpected end of config file (%s)\n", loc.file);
182 /* Take a list of log closures and merge them */
185 struct loglist *next;
188 static void log_vmulti(void *sst, int class, const char *message, va_list args)
190 struct loglist *st=sst, *i;
192 if (secnet_is_daemon) {
193 for (i=st; i; i=i->next) {
194 i->l->vlog(i->l->st,class,message,args);
197 vMessage(class,message,args);
202 static void log_multi(void *st, int priority, const char *message, ...)
206 va_start(ap,message);
207 log_vmulti(st,priority,message,ap);
211 struct log_if *init_log(list_t *ll)
216 struct loglist *l=NULL, *n;
219 if (list_length(ll)==1) {
220 item=list_elem(ll,0);
221 cl=item->data.closure;
222 if (cl->type!=CL_LOG) {
223 cfgfatal(item->loc,"init_log","closure is not a logger");
225 return cl->interface;
227 while ((item=list_elem(ll,i++))) {
228 if (item->type!=t_closure) {
229 cfgfatal(item->loc,"init_log","item is not a closure");
231 cl=item->data.closure;
232 if (cl->type!=CL_LOG) {
233 cfgfatal(item->loc,"init_log","closure is not a logger");
235 n=safe_malloc(sizeof(*n),"init_log");
241 fatal("init_log: no log");
243 r=safe_malloc(sizeof(*r), "init_log");
259 static cstring_t months[]={
260 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
262 static void logfile_vlog(void *sst, int class, const char *message,
265 struct logfile *st=sst;
269 if (secnet_is_daemon && st->f) {
270 if (class&st->level) {
273 fprintf(st->f,"%s %2d %02d:%02d:%02d ",
274 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
276 vfprintf(st->f,message,args);
281 vMessageFallback(class,message,args);
282 MessageFallback(class,"\n");
286 static void logfile_log(void *state, int class, const char *message, ...)
290 va_start(ap,message);
291 logfile_vlog(state,class,message,ap);
295 static void logfile_hup_notify(void *sst, int signum)
297 struct logfile *st=sst;
299 f=fopen(st->logfile,"a");
301 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
302 "logfile: %s",strerror(errno));
306 logfile_log(st,M_INFO,"received SIGHUP");
310 static void logfile_phase_hook(void *sst, uint32_t new_phase)
312 struct logfile *st=sst;
316 f=fopen(st->logfile,"a");
317 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
318 st->loc.file,st->loc.line,st->logfile);
320 request_signal_notification(SIGHUP, logfile_hup_notify,st);
324 static struct flagstr message_class_table[]={
325 { "debug-config", M_DEBUG_CONFIG },
326 { "debug-phase", M_DEBUG_PHASE },
327 { "debug", M_DEBUG },
328 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
330 { "notice", M_NOTICE },
331 { "warning", M_WARNING },
333 { "security", M_SECURITY },
334 { "fatal", M_FATAL },
335 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
336 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
337 { "quiet", M_FATAL },
341 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
348 /* We should defer opening the logfile until the getresources
349 phase. We should defer writing into the logfile until after we
352 st=safe_malloc(sizeof(*st),"logfile_apply");
353 st->cl.description="logfile";
356 st->cl.interface=&st->ops;
358 st->ops.log=logfile_log;
359 st->ops.vlog=logfile_vlog;
363 item=list_elem(args,0);
364 if (!item || item->type!=t_dict) {
365 cfgfatal(loc,"logfile","argument must be a dictionary\n");
367 dict=item->data.dict;
369 st->logfile=dict_read_string(dict,"filename",True,"logfile",loc);
370 st->level=string_list_to_word(dict_lookup(dict,"class"),
371 message_class_table,"logfile");
373 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
375 return new_closure(&st->cl);
386 static int msgclass_to_syslogpriority(uint32_t m)
389 case M_DEBUG_CONFIG: return LOG_DEBUG;
390 case M_DEBUG_PHASE: return LOG_DEBUG;
391 case M_DEBUG: return LOG_DEBUG;
392 case M_INFO: return LOG_INFO;
393 case M_NOTICE: return LOG_NOTICE;
394 case M_WARNING: return LOG_WARNING;
395 case M_ERR: return LOG_ERR;
396 case M_SECURITY: return LOG_CRIT;
397 case M_FATAL: return LOG_EMERG;
398 default: return LOG_NOTICE;
402 static void syslog_vlog(void *sst, int class, const char *message,
405 struct syslog *st=sst;
408 vsyslog(msgclass_to_syslogpriority(class),message,args);
410 vMessageFallback(class,message,args);
411 MessageFallback(class,"\n");
415 static void syslog_log(void *sst, int priority, const char *message, ...)
419 va_start(ap,message);
420 syslog_vlog(sst,priority,message,ap);
424 static struct flagstr syslog_facility_table[]={
426 { "auth", LOG_AUTH },
429 { "authpriv", LOG_AUTHPRIV },
431 { "cron", LOG_CRON },
432 { "daemon", LOG_DAEMON },
433 { "kern", LOG_KERN },
434 { "local0", LOG_LOCAL0 },
435 { "local1", LOG_LOCAL1 },
436 { "local2", LOG_LOCAL2 },
437 { "local3", LOG_LOCAL3 },
438 { "local4", LOG_LOCAL4 },
439 { "local5", LOG_LOCAL5 },
440 { "local6", LOG_LOCAL6 },
441 { "local7", LOG_LOCAL7 },
443 { "mail", LOG_MAIL },
444 { "news", LOG_NEWS },
445 { "syslog", LOG_SYSLOG },
446 { "user", LOG_USER },
447 { "uucp", LOG_UUCP },
451 static void syslog_phase_hook(void *sst, uint32_t newphase)
453 struct syslog *st=sst;
456 openlog(st->ident,0,st->facility);
461 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
469 st=safe_malloc(sizeof(*st),"syslog_apply");
470 st->cl.description="syslog";
473 st->cl.interface=&st->ops;
475 st->ops.log=syslog_log;
476 st->ops.vlog=syslog_vlog;
478 item=list_elem(args,0);
479 if (!item || item->type!=t_dict)
480 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
483 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
484 facstr=dict_read_string(d, "facility", True, "syslog", loc);
485 st->facility=string_to_word(facstr,loc,
486 syslog_facility_table,"syslog");
488 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
490 return new_closure(&st->cl);
493 /* Read from a fd and output to a log. This is a quick hack to
494 support logging stderr, and needs code adding to tidy up before it
495 can be used for anything else. */
496 #define FDLOG_BUFSIZE 1024
506 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
509 struct fdlog *st=sst;
513 fds[0].events=POLLIN;
518 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
520 struct fdlog *st=sst;
524 if (fds[0].revents&POLLERR) {
527 if (fds[0].revents&POLLIN) {
528 remain=FDLOG_BUFSIZE-st->i-1;
530 st->buffer[FDLOG_BUFSIZE-1]=0;
531 st->log->log(st->log,M_WARNING,"%s: overlong line: %s",
532 st->prefix,st->buffer);
534 remain=FDLOG_BUFSIZE-1;
536 r=read(st->fd,st->buffer+st->i,remain);
539 for (i=0; i<st->i; i++) {
540 if (st->buffer[i]=='\n') {
542 st->log->log(st->log->st,M_INFO,"%s: %s",
543 st->prefix,st->buffer);
545 memmove(st->buffer,st->buffer+i,st->i-i);
551 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
557 void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
561 st=safe_malloc(sizeof(*st),"log_from_fd");
565 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
569 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,1,
573 void log_module(dict_t *dict)
575 add_closure(dict,"logfile",logfile_apply);
576 add_closure(dict,"syslog",syslog_apply);