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)
135 enter_phase(PHASE_SHUTDOWN);
137 if (maybe_f && ferror(maybe_f)) {
139 Message(M_FATAL, "error reading config file (%s, %s): %s",
140 facility, loc.file, strerror(errno));
141 } else if (maybe_f && feof(maybe_f)) {
143 Message(M_FATAL, "unexpected end of config file (%s, %s)",
145 } else if (loc.file && loc.line) {
146 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
148 } else if (!loc.file && loc.line) {
149 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
151 Message(M_FATAL, "config error (%s): ",facility);
154 vMessage(M_FATAL,message,args);
158 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
159 const char *message, ...)
163 va_start(args,message);
164 vcfgfatal_maybefile(maybe_f,loc,facility,message,args);
168 void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
172 va_start(args,message);
173 vcfgfatal_maybefile(0,loc,facility,message,args);
177 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
181 Message(M_FATAL, "error reading config file (%s): %s\n",
182 loc.file, strerror(errno));
184 } else if (feof(f)) {
185 Message(M_FATAL, "unexpected end of config file (%s)\n", loc.file);
190 /* Take a list of log closures and merge them */
193 struct loglist *next;
197 static void log_vmulti(void *sst, int class, const char *message, va_list args)
199 struct loglist *st=sst, *i;
201 if (secnet_is_daemon) {
202 for (i=st; i; i=i->next) {
203 vslilog(i->l,class,message,args);
206 vMessage(class,message,args);
212 void lg_vperror(struct log_if *lg, const char *desc, struct cloc *loc,
213 int class, int errnoval, const char *fmt, va_list al)
215 int status=current_phase;
222 enter_phase(PHASE_SHUTDOWN);
224 slilog_part(lg,class,"%s",desc);
226 slilog_part(lg,class," (%s:%d)",loc->file,loc->line);
227 slilog_part(lg,class,": ");
228 vslilog_part(lg,class,fmt,al);
230 slilog_part(lg,class,": %s",strerror(errnoval));
231 slilog_part(lg,class,"\n");
239 void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc,
240 int class, int errnoval, const char *fmt, ...)
244 lg_vperror(lg,desc,loc,class,errnoval,fmt,al);
248 void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc,
249 int class, int status, const char *progname)
252 lg_perror(lg,desc,loc,class,0,"%s exited",progname);
253 else if (WIFEXITED(status))
254 lg_perror(lg,desc,loc,class,0,"%s exited with error exit status %d",
255 progname,WEXITSTATUS(status));
256 else if (WIFSIGNALED(status))
257 lg_perror(lg,desc,loc,class,0,"%s died due to fatal signal %s (%d)%s",
258 progname,strsignal(WTERMSIG(status)),WTERMSIG(status),
259 WCOREDUMP(status)?" (core dumped)":"");
261 lg_perror(lg,desc,loc,class,0,"%s died with unknown wait status %d",
265 struct log_if *init_log(list_t *ll)
270 struct loglist *l=NULL, *n;
273 if (list_length(ll)==1) {
274 item=list_elem(ll,0);
275 cl=item->data.closure;
276 if (cl->type!=CL_LOG) {
277 cfgfatal(item->loc,"init_log","closure is not a logger");
279 return cl->interface;
281 while ((item=list_elem(ll,i++))) {
282 if (item->type!=t_closure) {
283 cfgfatal(item->loc,"init_log","item is not a closure");
285 cl=item->data.closure;
286 if (cl->type!=CL_LOG) {
287 cfgfatal(item->loc,"init_log","closure is not a logger");
295 fatal("init_log: no log");
299 r->vlogfn=log_vmulti;
314 static cstring_t months[]={
315 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
318 static void logfile_vlog(void *sst, int class, const char *message,
321 struct logfile *st=sst;
328 snprintf(pidbuf,sizeof(pidbuf),"[%ld] ",(long)us);
333 if (secnet_is_daemon && st->f) {
334 if (class&st->level) {
337 fprintf(st->f,"%s %2d %02d:%02d:%02d %s",
338 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
341 vfprintf(st->f,message,args);
346 if (pidbuf[0]) MessageFallback(class,"%s",pidbuf);
347 vMessageFallback(class,message,args);
348 MessageFallback(class,"\n");
353 static void logfile_log(void *state, int class, const char *message, ...)
357 va_start(ap,message);
358 logfile_vlog(state,class,message,ap);
362 static void logfile_hup_notify(void *sst, int signum)
364 struct logfile *st=sst;
366 f=fopen(st->logfile,"a");
368 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
369 "logfile: %s",strerror(errno));
373 logfile_log(st,M_INFO,"received SIGHUP");
377 static void logfile_phase_hook(void *sst, uint32_t new_phase)
379 struct logfile *st=sst;
383 f=fopen(st->logfile,"a");
384 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
385 st->loc.file,st->loc.line,st->logfile);
387 request_signal_notification(SIGHUP, logfile_hup_notify,st);
391 static void logfile_childpersist_hook(void *sst, uint32_t new_phase)
393 struct logfile *st=sst;
397 static struct flagstr message_class_table[]={
398 { "debug-config", M_DEBUG_CONFIG },
399 { "debug-phase", M_DEBUG_PHASE },
400 { "debug", M_DEBUG },
401 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
403 { "notice", M_NOTICE },
404 { "warning", M_WARNING },
406 { "security", M_SECURITY },
407 { "fatal", M_FATAL },
408 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
409 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
410 { "quiet", M_FATAL },
414 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
421 /* We should defer opening the logfile until the getresources
422 phase. We should defer writing into the logfile until after we
426 st->cl.description="logfile";
429 st->cl.interface=&st->ops;
431 st->ops.vlogfn=logfile_vlog;
437 item=list_elem(args,0);
438 if (!item || item->type!=t_dict) {
439 cfgfatal(loc,"logfile","argument must be a dictionary\n");
441 dict=item->data.dict;
443 st->logfile=dict_read_string(dict,"filename",True,"logfile",loc);
444 st->level=string_list_to_word(dict_lookup(dict,"class"),
445 message_class_table,"logfile");
447 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
448 add_hook(PHASE_CHILDPERSIST,logfile_childpersist_hook,st);
450 return new_closure(&st->cl);
461 static int msgclass_to_syslogpriority(uint32_t m)
464 case M_DEBUG_CONFIG: return LOG_DEBUG;
465 case M_DEBUG_PHASE: return LOG_DEBUG;
466 case M_DEBUG: return LOG_DEBUG;
467 case M_INFO: return LOG_INFO;
468 case M_NOTICE: return LOG_NOTICE;
469 case M_WARNING: return LOG_WARNING;
470 case M_ERR: return LOG_ERR;
471 case M_SECURITY: return LOG_CRIT;
472 case M_FATAL: return LOG_EMERG;
473 default: return LOG_NOTICE;
477 static void syslog_vlog(void *sst, int class, const char *message,
480 static void syslog_vlog(void *sst, int class, const char *message,
483 struct syslog *st=sst;
486 vsyslog(msgclass_to_syslogpriority(class),message,args);
488 vMessageFallback(class,message,args);
489 MessageFallback(class,"\n");
493 static struct flagstr syslog_facility_table[]={
495 { "auth", LOG_AUTH },
498 { "authpriv", LOG_AUTHPRIV },
500 { "cron", LOG_CRON },
501 { "daemon", LOG_DAEMON },
502 { "kern", LOG_KERN },
503 { "local0", LOG_LOCAL0 },
504 { "local1", LOG_LOCAL1 },
505 { "local2", LOG_LOCAL2 },
506 { "local3", LOG_LOCAL3 },
507 { "local4", LOG_LOCAL4 },
508 { "local5", LOG_LOCAL5 },
509 { "local6", LOG_LOCAL6 },
510 { "local7", LOG_LOCAL7 },
512 { "mail", LOG_MAIL },
513 { "news", LOG_NEWS },
514 { "syslog", LOG_SYSLOG },
515 { "user", LOG_USER },
516 { "uucp", LOG_UUCP },
520 static void syslog_phase_hook(void *sst, uint32_t newphase)
522 struct syslog *st=sst;
526 newphase==PHASE_CHILDPERSIST ? LOG_PID : 0,
532 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
541 st->cl.description="syslog";
544 st->cl.interface=&st->ops;
546 st->ops.vlogfn=syslog_vlog;
549 item=list_elem(args,0);
550 if (!item || item->type!=t_dict)
551 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
554 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
555 facstr=dict_read_string(d, "facility", True, "syslog", loc);
556 st->facility=string_to_word(facstr,loc,
557 syslog_facility_table,"syslog");
559 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
560 add_hook(PHASE_CHILDPERSIST,syslog_phase_hook,st);
562 return new_closure(&st->cl);
565 /* Read from a fd and output to a log. This is a quick hack to
566 support logging stderr, and needs code adding to tidy up before it
567 can be used for anything else. */
568 #define FDLOG_BUFSIZE 1024
578 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
581 struct fdlog *st=sst;
583 BEFOREPOLL_WANT_FDS(1);
585 fds[0].events=POLLIN;
587 BEFOREPOLL_WANT_FDS(0);
592 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
594 struct fdlog *st=sst;
598 if (fds[0].revents&POLLERR) {
601 if (fds[0].revents&POLLIN) {
602 remain=FDLOG_BUFSIZE-st->i-1;
604 st->buffer[FDLOG_BUFSIZE-1]=0;
605 slilog(st->log,M_WARNING,"%s: overlong line: %s",
606 st->prefix,st->buffer);
608 remain=FDLOG_BUFSIZE-1;
610 r=read(st->fd,st->buffer+st->i,remain);
613 for (i=0; i<st->i; i++) {
614 if (st->buffer[i]=='\n') {
616 slilog(st->log,M_INFO,"%s: %s",
617 st->prefix,st->buffer);
619 memmove(st->buffer,st->buffer+i,st->i-i);
624 } else if (errno==EINTR || iswouldblock(errno)) {
626 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
632 void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
640 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
646 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,
650 void log_module(dict_t *dict)
652 add_closure(dict,"logfile",logfile_apply);
653 add_closure(dict,"syslog",syslog_apply);