12 bool_t secnet_is_daemon
=False
;
13 uint32_t message_level
=M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
;
14 struct log_if
*system_log
=NULL
;
16 static void vMessage(uint32_t class, const char *message
, va_list args
)
19 #define MESSAGE_BUFLEN 1023
20 static char buff
[MESSAGE_BUFLEN
+1]={0,};
24 if (secnet_is_daemon
) {
25 /* Messages go to the system log interface */
27 vsnprintf(buff
+bp
,MESSAGE_BUFLEN
-bp
,message
,args
);
28 /* Each line is sent separately */
29 while ((nlp
=strchr(buff
,'\n'))) {
31 slilog(system_log
,class,buff
);
32 memmove(buff
,nlp
+1,strlen(nlp
+1)+1);
35 /* Messages go to stdout/stderr */
36 if (class & message_level
) {
37 if (class&M_FATAL
|| class&M_ERR
|| class&M_WARNING
) {
40 vfprintf(dest
,message
,args
);
45 void Message(uint32_t class, const char *message
, ...)
50 vMessage(class,message
,ap
);
54 static NORETURN(vfatal(int status
, bool_t perror
, const char *message
,
57 static void vfatal(int status
, bool_t perror
, const char *message
,
64 enter_phase(PHASE_SHUTDOWN
);
65 Message(M_FATAL
, "secnet fatal error: ");
66 vMessage(M_FATAL
, message
, args
);
68 Message(M_FATAL
, ": %s\n",strerror(err
));
70 Message(M_FATAL
, "\n");
74 void fatal(const char *message
, ...)
77 va_start(args
,message
);
78 vfatal(current_phase
,False
,message
,args
);
82 void fatal_status(int status
, const char *message
, ...)
85 va_start(args
,message
);
86 vfatal(status
,False
,message
,args
);
90 void fatal_perror(const char *message
, ...)
93 va_start(args
,message
);
94 vfatal(current_phase
,True
,message
,args
);
98 void fatal_perror_status(int status
, const char *message
, ...)
101 va_start(args
,message
);
102 vfatal(status
,True
,message
,args
);
106 void vcfgfatal_maybefile(FILE *maybe_f
/* or 0 */, struct cloc loc
,
107 cstring_t facility
, const char *message
, va_list args
)
109 enter_phase(PHASE_SHUTDOWN
);
111 if (maybe_f
&& ferror(maybe_f
)) {
113 Message(M_FATAL
, "error reading config file (%s, %s): %s",
114 facility
, loc
.file
, strerror(errno
));
115 } else if (maybe_f
&& feof(maybe_f
)) {
117 Message(M_FATAL
, "unexpected end of config file (%s, %s)",
119 } else if (loc
.file
&& loc
.line
) {
120 Message(M_FATAL
, "config error (%s, %s:%d): ",facility
,loc
.file
,
122 } else if (!loc
.file
&& loc
.line
) {
123 Message(M_FATAL
, "config error (%s, line %d): ",facility
,loc
.line
);
125 Message(M_FATAL
, "config error (%s): ",facility
);
128 vMessage(M_FATAL
,message
,args
);
132 void cfgfatal_maybefile(FILE *maybe_f
, struct cloc loc
, cstring_t facility
,
133 const char *message
, ...)
137 va_start(args
,message
);
138 vcfgfatal_maybefile(maybe_f
,loc
,facility
,message
,args
);
142 void cfgfatal(struct cloc loc
, cstring_t facility
, const char *message
, ...)
146 va_start(args
,message
);
147 vcfgfatal_maybefile(0,loc
,facility
,message
,args
);
151 void cfgfile_postreadcheck(struct cloc loc
, FILE *f
)
155 Message(M_FATAL
, "error reading config file (%s): %s",
156 loc
.file
, strerror(errno
));
158 } else if (feof(f
)) {
159 Message(M_FATAL
, "unexpected end of config file (%s)", loc
.file
);
164 /* Take a list of log closures and merge them */
167 struct loglist
*next
;
170 static void log_vmulti(void *sst
, int class, const char *message
, va_list args
)
172 struct loglist
*st
=sst
, *i
;
174 if (secnet_is_daemon
) {
175 for (i
=st
; i
; i
=i
->next
) {
176 i
->l
->vlog(i
->l
->st
,class,message
,args
);
179 vMessage(class,message
,args
);
184 static void log_multi(void *st
, int priority
, const char *message
, ...)
188 va_start(ap
,message
);
189 log_vmulti(st
,priority
,message
,ap
);
193 struct log_if
*init_log(list_t
*ll
)
198 struct loglist
*l
=NULL
, *n
;
201 if (list_length(ll
)==1) {
202 item
=list_elem(ll
,0);
203 cl
=item
->data
.closure
;
204 if (cl
->type
!=CL_LOG
) {
205 cfgfatal(item
->loc
,"init_log","closure is not a logger");
207 return cl
->interface
;
209 while ((item
=list_elem(ll
,i
++))) {
210 if (item
->type
!=t_closure
) {
211 cfgfatal(item
->loc
,"init_log","item is not a closure");
213 cl
=item
->data
.closure
;
214 if (cl
->type
!=CL_LOG
) {
215 cfgfatal(item
->loc
,"init_log","closure is not a logger");
217 n
=safe_malloc(sizeof(*n
),"init_log");
223 fatal("init_log: no log");
225 r
=safe_malloc(sizeof(*r
), "init_log");
241 static cstring_t months
[]={
242 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
244 static void logfile_vlog(void *sst
, int class, const char *message
,
247 struct logfile
*st
=sst
;
251 if (secnet_is_daemon
&& st
->f
) {
252 if (class&st
->level
) {
255 fprintf(st
->f
,"%s %2d %02d:%02d:%02d ",
256 months
[tm
->tm_mon
],tm
->tm_mday
,tm
->tm_hour
,tm
->tm_min
,
258 vfprintf(st
->f
,message
,args
);
263 vMessage(class,message
,args
);
268 static void logfile_log(void *state
, int class, const char *message
, ...)
272 va_start(ap
,message
);
273 logfile_vlog(state
,class,message
,ap
);
277 static void logfile_hup_notify(void *sst
, int signum
)
279 struct logfile
*st
=sst
;
281 f
=fopen(st
->logfile
,"a");
283 logfile_log(st
,M_FATAL
,"received SIGHUP, but could not reopen "
284 "logfile: %s",strerror(errno
));
288 logfile_log(st
,M_INFO
,"received SIGHUP");
292 static void logfile_phase_hook(void *sst
, uint32_t new_phase
)
294 struct logfile
*st
=sst
;
298 f
=fopen(st
->logfile
,"a");
299 if (!f
) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
300 st
->loc
.file
,st
->loc
.line
,st
->logfile
);
302 request_signal_notification(SIGHUP
, logfile_hup_notify
,st
);
306 static struct flagstr message_class_table
[]={
307 { "debug-config", M_DEBUG_CONFIG
},
308 { "debug-phase", M_DEBUG_PHASE
},
309 { "debug", M_DEBUG
},
310 { "all-debug", M_DEBUG
|M_DEBUG_PHASE
|M_DEBUG_CONFIG
},
312 { "notice", M_NOTICE
},
313 { "warning", M_WARNING
},
315 { "security", M_SECURITY
},
316 { "fatal", M_FATAL
},
317 { "default", M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
},
318 { "verbose", M_INFO
|M_NOTICE
|M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
},
319 { "quiet", M_FATAL
},
323 static list_t
*logfile_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
330 /* We should defer opening the logfile until the getresources
331 phase. We should defer writing into the logfile until after we
334 st
=safe_malloc(sizeof(*st
),"logfile_apply");
335 st
->cl
.description
="logfile";
338 st
->cl
.interface
=&st
->ops
;
340 st
->ops
.log
=logfile_log
;
341 st
->ops
.vlog
=logfile_vlog
;
345 item
=list_elem(args
,0);
346 if (!item
|| item
->type
!=t_dict
) {
347 cfgfatal(loc
,"logfile","argument must be a dictionary\n");
349 dict
=item
->data
.dict
;
351 st
->logfile
=dict_read_string(dict
,"filename",True
,"logfile",loc
);
352 st
->level
=string_list_to_word(dict_lookup(dict
,"class"),
353 message_class_table
,"logfile");
355 add_hook(PHASE_GETRESOURCES
,logfile_phase_hook
,st
);
357 return new_closure(&st
->cl
);
368 static int msgclass_to_syslogpriority(uint32_t m
)
371 case M_DEBUG_CONFIG
: return LOG_DEBUG
;
372 case M_DEBUG_PHASE
: return LOG_DEBUG
;
373 case M_DEBUG
: return LOG_DEBUG
;
374 case M_INFO
: return LOG_INFO
;
375 case M_NOTICE
: return LOG_NOTICE
;
376 case M_WARNING
: return LOG_WARNING
;
377 case M_ERR
: return LOG_ERR
;
378 case M_SECURITY
: return LOG_CRIT
;
379 case M_FATAL
: return LOG_EMERG
;
380 default: return LOG_NOTICE
;
384 static void syslog_vlog(void *sst
, int class, const char *message
,
387 struct syslog
*st
=sst
;
390 vsyslog(msgclass_to_syslogpriority(class),message
,args
);
392 vMessage(class,message
,args
);
397 static void syslog_log(void *sst
, int priority
, const char *message
, ...)
401 va_start(ap
,message
);
402 syslog_vlog(sst
,priority
,message
,ap
);
406 static struct flagstr syslog_facility_table
[]={
408 { "auth", LOG_AUTH
},
411 { "authpriv", LOG_AUTHPRIV
},
413 { "cron", LOG_CRON
},
414 { "daemon", LOG_DAEMON
},
415 { "kern", LOG_KERN
},
416 { "local0", LOG_LOCAL0
},
417 { "local1", LOG_LOCAL1
},
418 { "local2", LOG_LOCAL2
},
419 { "local3", LOG_LOCAL3
},
420 { "local4", LOG_LOCAL4
},
421 { "local5", LOG_LOCAL5
},
422 { "local6", LOG_LOCAL6
},
423 { "local7", LOG_LOCAL7
},
425 { "mail", LOG_MAIL
},
426 { "news", LOG_NEWS
},
427 { "syslog", LOG_SYSLOG
},
428 { "user", LOG_USER
},
429 { "uucp", LOG_UUCP
},
433 static void syslog_phase_hook(void *sst
, uint32_t newphase
)
435 struct syslog
*st
=sst
;
438 openlog(st
->ident
,0,st
->facility
);
443 static list_t
*syslog_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
451 st
=safe_malloc(sizeof(*st
),"syslog_apply");
452 st
->cl
.description
="syslog";
455 st
->cl
.interface
=&st
->ops
;
457 st
->ops
.log
=syslog_log
;
458 st
->ops
.vlog
=syslog_vlog
;
460 item
=list_elem(args
,0);
461 if (!item
|| item
->type
!=t_dict
)
462 cfgfatal(loc
,"syslog","parameter must be a dictionary\n");
465 st
->ident
=dict_read_string(d
, "ident", False
, "syslog", loc
);
466 facstr
=dict_read_string(d
, "facility", True
, "syslog", loc
);
467 st
->facility
=string_to_word(facstr
,loc
,
468 syslog_facility_table
,"syslog");
470 add_hook(PHASE_GETRESOURCES
,syslog_phase_hook
,st
);
472 return new_closure(&st
->cl
);
475 /* Read from a fd and output to a log. This is a quick hack to
476 support logging stderr, and needs code adding to tidy up before it
477 can be used for anything else. */
478 #define FDLOG_BUFSIZE 1024
488 static int log_from_fd_beforepoll(void *sst
, struct pollfd
*fds
, int *nfds_io
,
490 const struct timeval
*tv_now
, uint64_t *now
)
492 struct fdlog
*st
=sst
;
496 fds
[0].events
=POLLIN
;
501 static void log_from_fd_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
,
502 const struct timeval
*tv_now
, uint64_t *now
)
504 struct fdlog
*st
=sst
;
508 if (fds
[0].revents
&POLLERR
) {
511 if (fds
[0].revents
&POLLIN
) {
512 remain
=FDLOG_BUFSIZE
-st
->i
-1;
514 st
->buffer
[FDLOG_BUFSIZE
-1]=0;
515 st
->log
->log(st
->log
,M_WARNING
,"%s: overlong line: %s",
516 st
->prefix
,st
->buffer
);
518 remain
=FDLOG_BUFSIZE
-1;
520 r
=read(st
->fd
,st
->buffer
+st
->i
,remain
);
523 for (i
=0; i
<st
->i
; i
++) {
524 if (st
->buffer
[i
]=='\n') {
526 st
->log
->log(st
->log
->st
,M_INFO
,"%s: %s",
527 st
->prefix
,st
->buffer
);
529 memmove(st
->buffer
,st
->buffer
+i
,st
->i
-i
);
535 Message(M_WARNING
,"log_from_fd: %s\n",strerror(errno
));
541 void log_from_fd(int fd
, cstring_t prefix
, struct log_if
*log
)
545 st
=safe_malloc(sizeof(*st
),"log_from_fd");
549 st
->buffer
=safe_malloc(FDLOG_BUFSIZE
,"log_from_fd");
553 register_for_poll(st
,log_from_fd_beforepoll
,log_from_fd_afterpoll
,1,
557 init_module log_module
;
558 void log_module(dict_t
*dict
)
560 add_closure(dict
,"logfile",logfile_apply
);
561 add_closure(dict
,"syslog",syslog_apply
);