2 * This file is part of secnet.
3 * See README for full list of copyright holders.
5 * secnet is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * secnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * version 3 along with secnet; if not, see
17 * https://www.gnu.org/licenses/gpl.html.
31 bool_t secnet_is_daemon=False;
32 uint32_t message_level=M_WARNING|M_ERR|M_SECURITY|M_FATAL;
33 struct log_if *system_log=NULL;
36 static void vMessageFallback(uint32_t class, const char *message, va_list args)
39 /* Messages go to stdout/stderr */
40 if (class & message_level) {
41 if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
44 vfprintf(dest,message,args);
49 static void vMessage(uint32_t class, const char *message, va_list args)
52 vslilog_part(system_log, class, message, args);
55 void Message(uint32_t class, const char *message, ...)
60 vMessage(class,message,ap);
65 static void MessageFallback(uint32_t class, const char *message, ...)
70 vMessageFallback(class,message,ap);
74 static NORETURN(vfatal(int status, bool_t perror, const char *message,
78 static void vfatal(int status, bool_t perror, const char *message,
85 enter_phase(PHASE_SHUTDOWN);
86 Message(M_FATAL, "secnet fatal error: ");
87 vMessage(M_FATAL, message, args);
89 Message(M_FATAL, ": %s\n",strerror(err));
91 Message(M_FATAL, "\n");
95 void fatal(const char *message, ...)
98 va_start(args,message);
99 vfatal(current_phase,False,message,args);
103 void fatal_status(int status, const char *message, ...)
106 va_start(args,message);
107 vfatal(status,False,message,args);
111 void fatal_perror(const char *message, ...)
114 va_start(args,message);
115 vfatal(current_phase,True,message,args);
119 void fatal_perror_status(int status, const char *message, ...)
122 va_start(args,message);
123 vfatal(status,True,message,args);
127 void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
128 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);
151 Message(M_FATAL,"%s",suffix);
155 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
156 const char *message, ...)
160 va_start(args,message);
161 vcfgfatal_maybefile(maybe_f,loc,facility,message,args,0);
165 void cfgfatal_cl_type(struct cloc loc, const char *facility,
166 closure_t *cl, uint32_t exp_type, const char *name)
168 assert(cl->type != exp_type);
169 cfgfatal(loc,facility,"\"%s\" is the wrong type of closure\n",name);
172 void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
176 va_start(args,message);
177 vcfgfatal_maybefile(0,loc,facility,message,args,"");
181 void cfgfile_log__vmsg(void *sst, int class, const char *message, va_list args)
183 struct cfgfile_log *st=sst;
184 vcfgfatal_maybefile(0,st->loc,st->facility,message,args,"\n");
187 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
191 Message(M_FATAL, "error reading config file (%s): %s\n",
192 loc.file, strerror(errno));
194 } else if (feof(f)) {
195 Message(M_FATAL, "unexpected end of config file (%s)\n", loc.file);
200 /* Take a list of log closures and merge them */
203 struct loglist *next;
207 static void log_vmulti(void *sst, int class, const char *message, va_list args)
209 struct loglist *st=sst, *i;
211 if (secnet_is_daemon) {
212 for (i=st; i; i=i->next) {
213 vslilog(i->l,class,message,args);
216 vMessage(class,message,args);
222 void lg_vperror(struct log_if *lg, const char *desc, struct cloc *loc,
223 int class, int errnoval, const char *fmt, va_list al)
225 int status=current_phase;
232 enter_phase(PHASE_SHUTDOWN);
234 slilog_part(lg,class,"%s",desc);
236 slilog_part(lg,class," (%s:%d)",loc->file,loc->line);
237 slilog_part(lg,class,": ");
238 vslilog_part(lg,class,fmt,al);
240 slilog_part(lg,class,": %s",strerror(errnoval));
241 slilog_part(lg,class,"\n");
249 void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc,
250 int class, int errnoval, const char *fmt, ...)
254 lg_vperror(lg,desc,loc,class,errnoval,fmt,al);
258 void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc,
259 int class, int status, const char *progname)
262 lg_perror(lg,desc,loc,class,0,"%s exited",progname);
263 else if (WIFEXITED(status))
264 lg_perror(lg,desc,loc,class,0,"%s exited with error exit status %d",
265 progname,WEXITSTATUS(status));
266 else if (WIFSIGNALED(status))
267 lg_perror(lg,desc,loc,class,0,"%s died due to fatal signal %s (%d)%s",
268 progname,strsignal(WTERMSIG(status)),WTERMSIG(status),
269 WCOREDUMP(status)?" (core dumped)":"");
271 lg_perror(lg,desc,loc,class,0,"%s died with unknown wait status %d",
275 struct log_if *init_log(list_t *ll)
280 struct loglist *l=NULL, *n;
283 if (list_length(ll)==1) {
284 item=list_elem(ll,0);
285 cl=item->data.closure;
286 if (cl->type!=CL_LOG) {
287 cfgfatal(item->loc,"init_log","closure is not a logger");
289 return cl->interface;
291 while ((item=list_elem(ll,i++))) {
292 if (item->type!=t_closure) {
293 cfgfatal(item->loc,"init_log","item is not a closure");
295 cl=item->data.closure;
296 if (cl->type!=CL_LOG) {
297 cfgfatal(item->loc,"init_log","closure is not a logger");
305 fatal("init_log: no log");
309 r->vlogfn=log_vmulti;
325 static cstring_t months[]={
326 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
329 static void logfile_vlog(void *sst, int class, const char *message,
332 struct logfile *st=sst;
339 snprintf(pidbuf,sizeof(pidbuf),"[%ld] ",(long)us);
344 if (class&st->level) {
347 fprintf(st->f,"%s %2d %02d:%02d:%02d %s%s%s",
348 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
350 st->prefix, st->prefix[0] ? " " : "",
352 vfprintf(st->f,message,args);
359 static void logfile_log(void *state, int class, const char *message, ...)
363 va_start(ap,message);
364 logfile_vlog(state,class,message,ap);
368 static void logfile_hup_notify(void *sst, int signum)
370 struct logfile *st=sst;
372 if (!st->logfile) return;
373 f=fopen(st->logfile,"a");
375 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
376 "logfile: %s",strerror(errno));
380 logfile_log(st,M_INFO,"received SIGHUP");
384 static void logfile_phase_hook(void *sst, uint32_t new_phase)
386 struct logfile *st=sst;
389 if (background && st->logfile) {
390 f=fopen(st->logfile,"a");
391 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
392 st->loc.file,st->loc.line,st->logfile);
394 request_signal_notification(SIGHUP, logfile_hup_notify,st);
398 static void logfile_childpersist_hook(void *sst, uint32_t new_phase)
400 struct logfile *st=sst;
404 static struct flagstr message_class_table[]={
405 { "debug-config", M_DEBUG_CONFIG },
406 { "debug-phase", M_DEBUG_PHASE },
407 { "debug", M_DEBUG },
408 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
410 { "notice", M_NOTICE },
411 { "warning", M_WARNING },
413 { "security", M_SECURITY },
414 { "fatal", M_FATAL },
415 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
416 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
417 { "quiet", M_FATAL },
421 static void logfile_file_init(struct logfile *st, FILE *f, const char *desc)
423 st->cl.description=desc;
426 st->cl.interface=&st->ops;
428 st->ops.vlogfn=logfile_vlog;
438 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
445 /* We should defer opening the logfile until the getresources
446 phase. We should defer writing into the logfile until after we
451 logfile_file_init(st,stderr,"logfile");
453 item=list_elem(args,0);
454 if (!item || item->type!=t_dict) {
455 cfgfatal(loc,"logfile","argument must be a dictionary\n");
457 dict=item->data.dict;
459 st->logfile=dict_read_string(dict,"filename",False,"logfile",loc);
460 st->prefix=dict_read_string(dict,"prefix",False,"logfile",loc);
461 if (!st->prefix) st->prefix="";
462 st->level=string_list_to_word(dict_lookup(dict,"class"),
463 message_class_table,"logfile");
465 add_hook(PHASE_DAEMONIZE,logfile_phase_hook,st);
466 add_hook(PHASE_CHILDPERSIST,logfile_childpersist_hook,st);
468 return new_closure(&st->cl);
479 static int msgclass_to_syslogpriority(uint32_t m)
482 case M_DEBUG_CONFIG: return LOG_DEBUG;
483 case M_DEBUG_PHASE: return LOG_DEBUG;
484 case M_DEBUG: return LOG_DEBUG;
485 case M_INFO: return LOG_INFO;
486 case M_NOTICE: return LOG_NOTICE;
487 case M_WARNING: return LOG_WARNING;
488 case M_ERR: return LOG_ERR;
489 case M_SECURITY: return LOG_CRIT;
490 case M_FATAL: return LOG_EMERG;
491 default: return LOG_NOTICE;
495 static void syslog_vlog(void *sst, int class, const char *message,
498 static void syslog_vlog(void *sst, int class, const char *message,
501 struct syslog *st=sst;
504 vsyslog(msgclass_to_syslogpriority(class),message,args);
506 vMessageFallback(class,message,args);
507 MessageFallback(class,"\n");
511 static struct flagstr syslog_facility_table[]={
513 { "auth", LOG_AUTH },
516 { "authpriv", LOG_AUTHPRIV },
518 { "cron", LOG_CRON },
519 { "daemon", LOG_DAEMON },
520 { "kern", LOG_KERN },
521 { "local0", LOG_LOCAL0 },
522 { "local1", LOG_LOCAL1 },
523 { "local2", LOG_LOCAL2 },
524 { "local3", LOG_LOCAL3 },
525 { "local4", LOG_LOCAL4 },
526 { "local5", LOG_LOCAL5 },
527 { "local6", LOG_LOCAL6 },
528 { "local7", LOG_LOCAL7 },
530 { "mail", LOG_MAIL },
531 { "news", LOG_NEWS },
532 { "syslog", LOG_SYSLOG },
533 { "user", LOG_USER },
534 { "uucp", LOG_UUCP },
538 static void syslog_phase_hook(void *sst, uint32_t newphase)
540 struct syslog *st=sst;
544 newphase==PHASE_CHILDPERSIST ? LOG_PID : 0,
550 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
559 st->cl.description="syslog";
562 st->cl.interface=&st->ops;
564 st->ops.vlogfn=syslog_vlog;
567 item=list_elem(args,0);
568 if (!item || item->type!=t_dict)
569 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
572 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
573 facstr=dict_read_string(d, "facility", True, "syslog", loc);
574 st->facility=string_to_word(facstr,loc,
575 syslog_facility_table,"syslog");
577 add_hook(PHASE_DAEMONIZE,syslog_phase_hook,st);
578 add_hook(PHASE_CHILDPERSIST,syslog_phase_hook,st);
580 return new_closure(&st->cl);
583 /* Read from a fd and output to a log. This is a quick hack to
584 support logging stderr, and needs code adding to tidy up before it
585 can be used for anything else. */
586 #define FDLOG_BUFSIZE 1024
596 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
599 struct fdlog *st=sst;
601 BEFOREPOLL_WANT_FDS(1);
603 fds[0].events=POLLIN;
605 BEFOREPOLL_WANT_FDS(0);
610 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
612 struct fdlog *st=sst;
616 if (fds[0].revents&POLLERR) {
619 if (fds[0].revents&POLLIN) {
620 remain=FDLOG_BUFSIZE-st->i-1;
622 st->buffer[FDLOG_BUFSIZE-1]=0;
623 slilog(st->log,M_WARNING,"%s: overlong line: %s",
624 st->prefix,st->buffer);
626 remain=FDLOG_BUFSIZE-1;
628 r=read(st->fd,st->buffer+st->i,remain);
631 for (i=0; i<st->i; i++) {
632 if (st->buffer[i]=='\n') {
634 slilog(st->log,M_INFO,"%s: %s",
635 st->prefix,st->buffer);
637 memmove(st->buffer,st->buffer+i,st->i-i);
642 } else if (errno==EINTR || iswouldblock(errno)) {
644 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
650 void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
658 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
664 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,
668 static struct logfile startup_log;
669 void log_early_setlevel(void)
671 startup_log.level=message_level;
673 void log_early_init(void)
675 logfile_file_init(&startup_log,stderr,"startup");
676 log_early_setlevel();
677 system_log=&startup_log.ops;;
680 /* for the benefit of main, really */
681 void logfile_init_file(struct logfile *st, FILE *f);
683 void log_module(dict_t *dict)
687 add_closure(dict,"logfile",logfile_apply);
688 add_closure(dict,"syslog",syslog_apply);