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 d 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
;
35 static void vMessageFallback(uint32_t class, const char *message
, va_list args
)
37 static void vMessageFallback(uint32_t class, const char *message
, va_list args
)
40 /* Messages go to stdout/stderr */
41 if (class & message_level
) {
42 if (class&M_FATAL
|| class&M_ERR
|| class&M_WARNING
) {
45 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
);
69 static void MessageFallback(uint32_t class, const char *message
, ...)
71 static void MessageFallback(uint32_t class, const char *message
, ...)
76 vMessageFallback(class,message
,ap
);
80 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
;
196 static void log_vmulti(void *sst
, int class, const char *message
, va_list args
)
198 struct loglist
*st
=sst
, *i
;
200 if (secnet_is_daemon
) {
201 for (i
=st
; i
; i
=i
->next
) {
202 vslilog(i
->l
,class,message
,args
);
205 vMessage(class,message
,args
);
210 void lg_vperror(struct log_if
*lg
, const char *desc
, struct cloc
*loc
,
211 int class, int errnoval
, const char *fmt
, va_list al
)
213 int status
=current_phase
;
220 enter_phase(PHASE_SHUTDOWN
);
222 slilog_part(lg
,class,"%s",desc
);
224 slilog_part(lg
,class," (%s:%d)",loc
->file
,loc
->line
);
225 slilog_part(lg
,class,": ");
226 vslilog_part(lg
,class,fmt
,al
);
228 slilog_part(lg
,class,": %s",strerror(errnoval
));
229 slilog_part(lg
,class,"\n");
237 void lg_perror(struct log_if
*lg
, const char *desc
, struct cloc
*loc
,
238 int class, int errnoval
, const char *fmt
, ...)
242 lg_vperror(lg
,desc
,loc
,class,errnoval
,fmt
,al
);
246 void lg_exitstatus(struct log_if
*lg
, const char *desc
, struct cloc
*loc
,
247 int class, int status
, const char *progname
)
250 lg_perror(lg
,desc
,loc
,class,0,"%s exited",progname
);
251 else if (WIFEXITED(status
))
252 lg_perror(lg
,desc
,loc
,class,0,"%s exited with error exit status %d",
253 progname
,WEXITSTATUS(status
));
254 else if (WIFSIGNALED(status
))
255 lg_perror(lg
,desc
,loc
,class,0,"%s died due to fatal signal %s (%d)%s",
256 progname
,strsignal(WTERMSIG(status
)),WTERMSIG(status
),
257 WCOREDUMP(status
)?
" (core dumped)":"");
259 lg_perror(lg
,desc
,loc
,class,0,"%s died with unknown wait status %d",
263 struct log_if
*init_log(list_t
*ll
)
268 struct loglist
*l
=NULL
, *n
;
271 if (list_length(ll
)==1) {
272 item
=list_elem(ll
,0);
273 cl
=item
->data
.closure
;
274 if (cl
->type
!=CL_LOG
) {
275 cfgfatal(item
->loc
,"init_log","closure is not a logger");
277 return cl
->interface
;
279 while ((item
=list_elem(ll
,i
++))) {
280 if (item
->type
!=t_closure
) {
281 cfgfatal(item
->loc
,"init_log","item is not a closure");
283 cl
=item
->data
.closure
;
284 if (cl
->type
!=CL_LOG
) {
285 cfgfatal(item
->loc
,"init_log","closure is not a logger");
293 fatal("init_log: no log");
297 r
->vlogfn
=log_vmulti
;
312 static cstring_t months
[]={
313 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
315 static void logfile_vlog(void *sst
, int class, const char *message
,
318 struct logfile
*st
=sst
;
325 snprintf(pidbuf
,sizeof(pidbuf
),"[%ld] ",(long)us
);
330 if (secnet_is_daemon
&& st
->f
) {
331 if (class&st
->level
) {
334 fprintf(st
->f
,"%s %2d %02d:%02d:%02d %s",
335 months
[tm
->tm_mon
],tm
->tm_mday
,tm
->tm_hour
,tm
->tm_min
,
338 vfprintf(st
->f
,message
,args
);
343 if (pidbuf
[0]) MessageFallback(class,"%s",pidbuf
);
344 vMessageFallback(class,message
,args
);
345 MessageFallback(class,"\n");
349 static void logfile_log(void *state
, int class, const char *message
, ...)
351 static void logfile_log(void *state
, int class, const char *message
, ...)
355 va_start(ap
,message
);
356 logfile_vlog(state
,class,message
,ap
);
360 static void logfile_hup_notify(void *sst
, int signum
)
362 struct logfile
*st
=sst
;
364 f
=fopen(st
->logfile
,"a");
366 logfile_log(st
,M_FATAL
,"received SIGHUP, but could not reopen "
367 "logfile: %s",strerror(errno
));
371 logfile_log(st
,M_INFO
,"received SIGHUP");
375 static void logfile_phase_hook(void *sst
, uint32_t new_phase
)
377 struct logfile
*st
=sst
;
381 f
=fopen(st
->logfile
,"a");
382 if (!f
) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
383 st
->loc
.file
,st
->loc
.line
,st
->logfile
);
385 request_signal_notification(SIGHUP
, logfile_hup_notify
,st
);
389 static void logfile_childpersist_hook(void *sst
, uint32_t new_phase
)
391 struct logfile
*st
=sst
;
395 static struct flagstr message_class_table
[]={
396 { "debug-config", M_DEBUG_CONFIG
},
397 { "debug-phase", M_DEBUG_PHASE
},
398 { "debug", M_DEBUG
},
399 { "all-debug", M_DEBUG
|M_DEBUG_PHASE
|M_DEBUG_CONFIG
},
401 { "notice", M_NOTICE
},
402 { "warning", M_WARNING
},
404 { "security", M_SECURITY
},
405 { "fatal", M_FATAL
},
406 { "default", M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
},
407 { "verbose", M_INFO
|M_NOTICE
|M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
},
408 { "quiet", M_FATAL
},
412 static list_t
*logfile_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
419 /* We should defer opening the logfile until the getresources
420 phase. We should defer writing into the logfile until after we
424 st
->cl
.description
="logfile";
427 st
->cl
.interface
=&st
->ops
;
429 st
->ops
.vlogfn
=logfile_vlog
;
435 item
=list_elem(args
,0);
436 if (!item
|| item
->type
!=t_dict
) {
437 cfgfatal(loc
,"logfile","argument must be a dictionary\n");
439 dict
=item
->data
.dict
;
441 st
->logfile
=dict_read_string(dict
,"filename",True
,"logfile",loc
);
442 st
->level
=string_list_to_word(dict_lookup(dict
,"class"),
443 message_class_table
,"logfile");
445 add_hook(PHASE_GETRESOURCES
,logfile_phase_hook
,st
);
446 add_hook(PHASE_CHILDPERSIST
,logfile_childpersist_hook
,st
);
448 return new_closure(&st
->cl
);
459 static int msgclass_to_syslogpriority(uint32_t m
)
462 case M_DEBUG_CONFIG
: return LOG_DEBUG
;
463 case M_DEBUG_PHASE
: return LOG_DEBUG
;
464 case M_DEBUG
: return LOG_DEBUG
;
465 case M_INFO
: return LOG_INFO
;
466 case M_NOTICE
: return LOG_NOTICE
;
467 case M_WARNING
: return LOG_WARNING
;
468 case M_ERR
: return LOG_ERR
;
469 case M_SECURITY
: return LOG_CRIT
;
470 case M_FATAL
: return LOG_EMERG
;
471 default: return LOG_NOTICE
;
475 static void syslog_vlog(void *sst
, int class, const char *message
,
478 static void syslog_vlog(void *sst
, int class, const char *message
,
481 struct syslog
*st
=sst
;
484 vsyslog(msgclass_to_syslogpriority(class),message
,args
);
486 vMessageFallback(class,message
,args
);
487 MessageFallback(class,"\n");
491 static struct flagstr syslog_facility_table
[]={
493 { "auth", LOG_AUTH
},
496 { "authpriv", LOG_AUTHPRIV
},
498 { "cron", LOG_CRON
},
499 { "daemon", LOG_DAEMON
},
500 { "kern", LOG_KERN
},
501 { "local0", LOG_LOCAL0
},
502 { "local1", LOG_LOCAL1
},
503 { "local2", LOG_LOCAL2
},
504 { "local3", LOG_LOCAL3
},
505 { "local4", LOG_LOCAL4
},
506 { "local5", LOG_LOCAL5
},
507 { "local6", LOG_LOCAL6
},
508 { "local7", LOG_LOCAL7
},
510 { "mail", LOG_MAIL
},
511 { "news", LOG_NEWS
},
512 { "syslog", LOG_SYSLOG
},
513 { "user", LOG_USER
},
514 { "uucp", LOG_UUCP
},
518 static void syslog_phase_hook(void *sst
, uint32_t newphase
)
520 struct syslog
*st
=sst
;
524 newphase
==PHASE_CHILDPERSIST ? LOG_PID
: 0,
530 static list_t
*syslog_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
539 st
->cl
.description
="syslog";
542 st
->cl
.interface
=&st
->ops
;
544 st
->ops
.vlogfn
=syslog_vlog
;
547 item
=list_elem(args
,0);
548 if (!item
|| item
->type
!=t_dict
)
549 cfgfatal(loc
,"syslog","parameter must be a dictionary\n");
552 st
->ident
=dict_read_string(d
, "ident", False
, "syslog", loc
);
553 facstr
=dict_read_string(d
, "facility", True
, "syslog", loc
);
554 st
->facility
=string_to_word(facstr
,loc
,
555 syslog_facility_table
,"syslog");
557 add_hook(PHASE_GETRESOURCES
,syslog_phase_hook
,st
);
558 add_hook(PHASE_CHILDPERSIST
,syslog_phase_hook
,st
);
560 return new_closure(&st
->cl
);
563 /* Read from a fd and output to a log. This is a quick hack to
564 support logging stderr, and needs code adding to tidy up before it
565 can be used for anything else. */
566 #define FDLOG_BUFSIZE 1024
576 static int log_from_fd_beforepoll(void *sst
, struct pollfd
*fds
, int *nfds_io
,
579 struct fdlog
*st
=sst
;
581 BEFOREPOLL_WANT_FDS(1);
583 fds
[0].events
=POLLIN
;
585 BEFOREPOLL_WANT_FDS(0);
590 static void log_from_fd_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
)
592 struct fdlog
*st
=sst
;
596 if (fds
[0].revents
&POLLERR
) {
599 if (fds
[0].revents
&POLLIN
) {
600 remain
=FDLOG_BUFSIZE
-st
->i
-1;
602 st
->buffer
[FDLOG_BUFSIZE
-1]=0;
603 slilog(st
->log
,M_WARNING
,"%s: overlong line: %s",
604 st
->prefix
,st
->buffer
);
606 remain
=FDLOG_BUFSIZE
-1;
608 r
=read(st
->fd
,st
->buffer
+st
->i
,remain
);
611 for (i
=0; i
<st
->i
; i
++) {
612 if (st
->buffer
[i
]=='\n') {
614 slilog(st
->log
,M_INFO
,"%s: %s",
615 st
->prefix
,st
->buffer
);
617 memmove(st
->buffer
,st
->buffer
+i
,st
->i
-i
);
622 } else if (errno
==EINTR
|| iswouldblock(errno
)) {
624 Message(M_WARNING
,"log_from_fd: %s\n",strerror(errno
));
630 void log_from_fd(int fd
, cstring_t prefix
, struct log_if
*log
)
638 st
->buffer
=safe_malloc(FDLOG_BUFSIZE
,"log_from_fd");
644 register_for_poll(st
,log_from_fd_beforepoll
,log_from_fd_afterpoll
,
648 void log_module(dict_t
*dict
)
650 add_closure(dict
,"logfile",logfile_apply
);
651 add_closure(dict
,"syslog",syslog_apply
);