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)
53 /* Messages go to the system log interface */
54 vslilog_part(system_log, class, message, args);
56 vMessageFallback(class,message,args);
60 void Message(uint32_t class, const char *message, ...)
65 vMessage(class,message,ap);
70 static void MessageFallback(uint32_t class, const char *message, ...)
75 vMessageFallback(class,message,ap);
79 static NORETURN(vfatal(int status, bool_t perror, const char *message,
83 static void vfatal(int status, bool_t perror, const char *message,
90 enter_phase(PHASE_SHUTDOWN);
91 Message(M_FATAL, "secnet fatal error: ");
92 vMessage(M_FATAL, message, args);
94 Message(M_FATAL, ": %s\n",strerror(err));
96 Message(M_FATAL, "\n");
100 void fatal(const char *message, ...)
103 va_start(args,message);
104 vfatal(current_phase,False,message,args);
108 void fatal_status(int status, const char *message, ...)
111 va_start(args,message);
112 vfatal(status,False,message,args);
116 void fatal_perror(const char *message, ...)
119 va_start(args,message);
120 vfatal(current_phase,True,message,args);
124 void fatal_perror_status(int status, const char *message, ...)
127 va_start(args,message);
128 vfatal(status,True,message,args);
132 void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
133 cstring_t facility, const char *message, va_list args,
136 enter_phase(PHASE_SHUTDOWN);
138 if (maybe_f && ferror(maybe_f)) {
140 Message(M_FATAL, "error reading config file (%s, %s): %s",
141 facility, loc.file, strerror(errno));
142 } else if (maybe_f && feof(maybe_f)) {
144 Message(M_FATAL, "unexpected end of config file (%s, %s)",
146 } else if (loc.file && loc.line) {
147 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
149 } else if (!loc.file && loc.line) {
150 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
152 Message(M_FATAL, "config error (%s): ",facility);
155 vMessage(M_FATAL,message,args);
156 Message(M_FATAL,"%s",suffix);
160 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
161 const char *message, ...)
165 va_start(args,message);
166 vcfgfatal_maybefile(maybe_f,loc,facility,message,args,0);
170 void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
174 va_start(args,message);
175 vcfgfatal_maybefile(0,loc,facility,message,args,"");
179 void cfgfile_log__vmsg(void *sst, int class, const char *message, va_list args)
181 struct cfgfile_log *st=sst;
182 vcfgfatal_maybefile(0,st->loc,st->facility,message,args,"\n");
185 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
189 Message(M_FATAL, "error reading config file (%s): %s\n",
190 loc.file, strerror(errno));
192 } else if (feof(f)) {
193 Message(M_FATAL, "unexpected end of config file (%s)\n", loc.file);
198 /* Take a list of log closures and merge them */
201 struct loglist *next;
205 static void log_vmulti(void *sst, int class, const char *message, va_list args)
207 struct loglist *st=sst, *i;
209 if (secnet_is_daemon) {
210 for (i=st; i; i=i->next) {
211 vslilog(i->l,class,message,args);
214 vMessage(class,message,args);
220 void lg_vperror(struct log_if *lg, const char *desc, struct cloc *loc,
221 int class, int errnoval, const char *fmt, va_list al)
223 int status=current_phase;
230 enter_phase(PHASE_SHUTDOWN);
232 slilog_part(lg,class,"%s",desc);
234 slilog_part(lg,class," (%s:%d)",loc->file,loc->line);
235 slilog_part(lg,class,": ");
236 vslilog_part(lg,class,fmt,al);
238 slilog_part(lg,class,": %s",strerror(errnoval));
239 slilog_part(lg,class,"\n");
247 void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc,
248 int class, int errnoval, const char *fmt, ...)
252 lg_vperror(lg,desc,loc,class,errnoval,fmt,al);
256 void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc,
257 int class, int status, const char *progname)
260 lg_perror(lg,desc,loc,class,0,"%s exited",progname);
261 else if (WIFEXITED(status))
262 lg_perror(lg,desc,loc,class,0,"%s exited with error exit status %d",
263 progname,WEXITSTATUS(status));
264 else if (WIFSIGNALED(status))
265 lg_perror(lg,desc,loc,class,0,"%s died due to fatal signal %s (%d)%s",
266 progname,strsignal(WTERMSIG(status)),WTERMSIG(status),
267 WCOREDUMP(status)?" (core dumped)":"");
269 lg_perror(lg,desc,loc,class,0,"%s died with unknown wait status %d",
273 struct log_if *init_log(list_t *ll)
278 struct loglist *l=NULL, *n;
281 if (list_length(ll)==1) {
282 item=list_elem(ll,0);
283 cl=item->data.closure;
284 if (cl->type!=CL_LOG) {
285 cfgfatal(item->loc,"init_log","closure is not a logger");
287 return cl->interface;
289 while ((item=list_elem(ll,i++))) {
290 if (item->type!=t_closure) {
291 cfgfatal(item->loc,"init_log","item is not a closure");
293 cl=item->data.closure;
294 if (cl->type!=CL_LOG) {
295 cfgfatal(item->loc,"init_log","closure is not a logger");
303 fatal("init_log: no log");
307 r->vlogfn=log_vmulti;
323 static cstring_t months[]={
324 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
327 static void logfile_vlog(void *sst, int class, const char *message,
330 struct logfile *st=sst;
337 snprintf(pidbuf,sizeof(pidbuf),"[%ld] ",(long)us);
342 if (class&st->level) {
345 fprintf(st->f,"%s %2d %02d:%02d:%02d %s%s%s",
346 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
348 st->prefix, st->prefix[0] ? " " : "",
350 vfprintf(st->f,message,args);
357 static void logfile_log(void *state, int class, const char *message, ...)
361 va_start(ap,message);
362 logfile_vlog(state,class,message,ap);
366 static void logfile_hup_notify(void *sst, int signum)
368 struct logfile *st=sst;
370 if (!st->logfile) return;
371 f=fopen(st->logfile,"a");
373 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
374 "logfile: %s",strerror(errno));
378 logfile_log(st,M_INFO,"received SIGHUP");
382 static void logfile_phase_hook(void *sst, uint32_t new_phase)
384 struct logfile *st=sst;
387 if (background && st->logfile) {
388 f=fopen(st->logfile,"a");
389 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
390 st->loc.file,st->loc.line,st->logfile);
392 request_signal_notification(SIGHUP, logfile_hup_notify,st);
396 static void logfile_childpersist_hook(void *sst, uint32_t new_phase)
398 struct logfile *st=sst;
402 static struct flagstr message_class_table[]={
403 { "debug-config", M_DEBUG_CONFIG },
404 { "debug-phase", M_DEBUG_PHASE },
405 { "debug", M_DEBUG },
406 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
408 { "notice", M_NOTICE },
409 { "warning", M_WARNING },
411 { "security", M_SECURITY },
412 { "fatal", M_FATAL },
413 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
414 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
415 { "quiet", M_FATAL },
419 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
426 /* We should defer opening the logfile until the getresources
427 phase. We should defer writing into the logfile until after we
431 st->cl.description="logfile";
434 st->cl.interface=&st->ops;
436 st->ops.vlogfn=logfile_vlog;
443 item=list_elem(args,0);
444 if (!item || item->type!=t_dict) {
445 cfgfatal(loc,"logfile","argument must be a dictionary\n");
447 dict=item->data.dict;
449 st->logfile=dict_read_string(dict,"filename",False,"logfile",loc);
450 st->prefix=dict_read_string(dict,"prefix",False,"logfile",loc);
451 if (!st->prefix) st->prefix="";
452 st->level=string_list_to_word(dict_lookup(dict,"class"),
453 message_class_table,"logfile");
455 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
456 add_hook(PHASE_CHILDPERSIST,logfile_childpersist_hook,st);
458 return new_closure(&st->cl);
469 static int msgclass_to_syslogpriority(uint32_t m)
472 case M_DEBUG_CONFIG: return LOG_DEBUG;
473 case M_DEBUG_PHASE: return LOG_DEBUG;
474 case M_DEBUG: return LOG_DEBUG;
475 case M_INFO: return LOG_INFO;
476 case M_NOTICE: return LOG_NOTICE;
477 case M_WARNING: return LOG_WARNING;
478 case M_ERR: return LOG_ERR;
479 case M_SECURITY: return LOG_CRIT;
480 case M_FATAL: return LOG_EMERG;
481 default: return LOG_NOTICE;
485 static void syslog_vlog(void *sst, int class, const char *message,
488 static void syslog_vlog(void *sst, int class, const char *message,
491 struct syslog *st=sst;
494 vsyslog(msgclass_to_syslogpriority(class),message,args);
496 vMessageFallback(class,message,args);
497 MessageFallback(class,"\n");
501 static struct flagstr syslog_facility_table[]={
503 { "auth", LOG_AUTH },
506 { "authpriv", LOG_AUTHPRIV },
508 { "cron", LOG_CRON },
509 { "daemon", LOG_DAEMON },
510 { "kern", LOG_KERN },
511 { "local0", LOG_LOCAL0 },
512 { "local1", LOG_LOCAL1 },
513 { "local2", LOG_LOCAL2 },
514 { "local3", LOG_LOCAL3 },
515 { "local4", LOG_LOCAL4 },
516 { "local5", LOG_LOCAL5 },
517 { "local6", LOG_LOCAL6 },
518 { "local7", LOG_LOCAL7 },
520 { "mail", LOG_MAIL },
521 { "news", LOG_NEWS },
522 { "syslog", LOG_SYSLOG },
523 { "user", LOG_USER },
524 { "uucp", LOG_UUCP },
528 static void syslog_phase_hook(void *sst, uint32_t newphase)
530 struct syslog *st=sst;
534 newphase==PHASE_CHILDPERSIST ? LOG_PID : 0,
540 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
549 st->cl.description="syslog";
552 st->cl.interface=&st->ops;
554 st->ops.vlogfn=syslog_vlog;
557 item=list_elem(args,0);
558 if (!item || item->type!=t_dict)
559 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
562 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
563 facstr=dict_read_string(d, "facility", True, "syslog", loc);
564 st->facility=string_to_word(facstr,loc,
565 syslog_facility_table,"syslog");
567 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
568 add_hook(PHASE_CHILDPERSIST,syslog_phase_hook,st);
570 return new_closure(&st->cl);
573 /* Read from a fd and output to a log. This is a quick hack to
574 support logging stderr, and needs code adding to tidy up before it
575 can be used for anything else. */
576 #define FDLOG_BUFSIZE 1024
586 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
589 struct fdlog *st=sst;
591 BEFOREPOLL_WANT_FDS(1);
593 fds[0].events=POLLIN;
595 BEFOREPOLL_WANT_FDS(0);
600 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
602 struct fdlog *st=sst;
606 if (fds[0].revents&POLLERR) {
609 if (fds[0].revents&POLLIN) {
610 remain=FDLOG_BUFSIZE-st->i-1;
612 st->buffer[FDLOG_BUFSIZE-1]=0;
613 slilog(st->log,M_WARNING,"%s: overlong line: %s",
614 st->prefix,st->buffer);
616 remain=FDLOG_BUFSIZE-1;
618 r=read(st->fd,st->buffer+st->i,remain);
621 for (i=0; i<st->i; i++) {
622 if (st->buffer[i]=='\n') {
624 slilog(st->log,M_INFO,"%s: %s",
625 st->prefix,st->buffer);
627 memmove(st->buffer,st->buffer+i,st->i-i);
632 } else if (errno==EINTR || iswouldblock(errno)) {
634 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
640 void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
648 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
654 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,
658 void log_module(dict_t *dict)
662 add_closure(dict,"logfile",logfile_apply);
663 add_closure(dict,"syslog",syslog_apply);