13 bool_t secnet_is_daemon
=False
;
14 uint32_t message_level
=M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
;
15 struct log_if
*system_log
=NULL
;
17 static void vMessage(uint32_t class, const char *message
, va_list args
)
20 #define MESSAGE_BUFLEN 1023
21 static char buff
[MESSAGE_BUFLEN
+1]={0,};
26 /* Messages go to the system log interface */
28 assert(bp
< MESSAGE_BUFLEN
);
29 vsnprintf(buff
+bp
,MESSAGE_BUFLEN
-bp
,message
,args
);
30 /* Each line is sent separately */
31 while ((nlp
=strchr(buff
,'\n'))) {
33 slilog(system_log
,class,"%s",buff
);
34 memmove(buff
,nlp
+1,strlen(nlp
+1)+1);
37 /* Messages go to stdout/stderr */
38 if (class & message_level
) {
39 if (class&M_FATAL
|| class&M_ERR
|| class&M_WARNING
) {
42 vfprintf(dest
,message
,args
);
47 void Message(uint32_t class, const char *message
, ...)
52 vMessage(class,message
,ap
);
56 static NORETURN(vfatal(int status
, bool_t perror
, const char *message
,
59 static void vfatal(int status
, bool_t perror
, const char *message
,
66 enter_phase(PHASE_SHUTDOWN
);
67 Message(M_FATAL
, "secnet fatal error: ");
68 vMessage(M_FATAL
, message
, args
);
70 Message(M_FATAL
, ": %s\n",strerror(err
));
72 Message(M_FATAL
, "\n");
76 void fatal(const char *message
, ...)
79 va_start(args
,message
);
80 vfatal(current_phase
,False
,message
,args
);
84 void fatal_status(int status
, const char *message
, ...)
87 va_start(args
,message
);
88 vfatal(status
,False
,message
,args
);
92 void fatal_perror(const char *message
, ...)
95 va_start(args
,message
);
96 vfatal(current_phase
,True
,message
,args
);
100 void fatal_perror_status(int status
, const char *message
, ...)
103 va_start(args
,message
);
104 vfatal(status
,True
,message
,args
);
108 void vcfgfatal_maybefile(FILE *maybe_f
/* or 0 */, struct cloc loc
,
109 cstring_t facility
, const char *message
, va_list args
)
111 enter_phase(PHASE_SHUTDOWN
);
113 if (maybe_f
&& ferror(maybe_f
)) {
115 Message(M_FATAL
, "error reading config file (%s, %s): %s",
116 facility
, loc
.file
, strerror(errno
));
117 } else if (maybe_f
&& feof(maybe_f
)) {
119 Message(M_FATAL
, "unexpected end of config file (%s, %s)",
121 } else if (loc
.file
&& loc
.line
) {
122 Message(M_FATAL
, "config error (%s, %s:%d): ",facility
,loc
.file
,
124 } else if (!loc
.file
&& loc
.line
) {
125 Message(M_FATAL
, "config error (%s, line %d): ",facility
,loc
.line
);
127 Message(M_FATAL
, "config error (%s): ",facility
);
130 vMessage(M_FATAL
,message
,args
);
134 void cfgfatal_maybefile(FILE *maybe_f
, struct cloc loc
, cstring_t facility
,
135 const char *message
, ...)
139 va_start(args
,message
);
140 vcfgfatal_maybefile(maybe_f
,loc
,facility
,message
,args
);
144 void cfgfatal(struct cloc loc
, cstring_t facility
, const char *message
, ...)
148 va_start(args
,message
);
149 vcfgfatal_maybefile(0,loc
,facility
,message
,args
);
153 void cfgfile_postreadcheck(struct cloc loc
, FILE *f
)
157 Message(M_FATAL
, "error reading config file (%s): %s",
158 loc
.file
, strerror(errno
));
160 } else if (feof(f
)) {
161 Message(M_FATAL
, "unexpected end of config file (%s)", loc
.file
);
166 /* Take a list of log closures and merge them */
169 struct loglist
*next
;
172 static void log_vmulti(void *sst
, int class, const char *message
, va_list args
)
174 struct loglist
*st
=sst
, *i
;
176 if (secnet_is_daemon
) {
177 for (i
=st
; i
; i
=i
->next
) {
178 i
->l
->vlog(i
->l
->st
,class,message
,args
);
181 vMessage(class,message
,args
);
186 static void log_multi(void *st
, int priority
, const char *message
, ...)
190 va_start(ap
,message
);
191 log_vmulti(st
,priority
,message
,ap
);
195 struct log_if
*init_log(list_t
*ll
)
200 struct loglist
*l
=NULL
, *n
;
203 if (list_length(ll
)==1) {
204 item
=list_elem(ll
,0);
205 cl
=item
->data
.closure
;
206 if (cl
->type
!=CL_LOG
) {
207 cfgfatal(item
->loc
,"init_log","closure is not a logger");
209 return cl
->interface
;
211 while ((item
=list_elem(ll
,i
++))) {
212 if (item
->type
!=t_closure
) {
213 cfgfatal(item
->loc
,"init_log","item is not a closure");
215 cl
=item
->data
.closure
;
216 if (cl
->type
!=CL_LOG
) {
217 cfgfatal(item
->loc
,"init_log","closure is not a logger");
219 n
=safe_malloc(sizeof(*n
),"init_log");
225 fatal("init_log: no log");
227 r
=safe_malloc(sizeof(*r
), "init_log");
243 static cstring_t months
[]={
244 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
246 static void logfile_vlog(void *sst
, int class, const char *message
,
249 struct logfile
*st
=sst
;
253 if (secnet_is_daemon
&& st
->f
) {
254 if (class&st
->level
) {
257 fprintf(st
->f
,"%s %2d %02d:%02d:%02d ",
258 months
[tm
->tm_mon
],tm
->tm_mday
,tm
->tm_hour
,tm
->tm_min
,
260 vfprintf(st
->f
,message
,args
);
265 vMessage(class,message
,args
);
270 static void logfile_log(void *state
, int class, const char *message
, ...)
274 va_start(ap
,message
);
275 logfile_vlog(state
,class,message
,ap
);
279 static void logfile_hup_notify(void *sst
, int signum
)
281 struct logfile
*st
=sst
;
283 f
=fopen(st
->logfile
,"a");
285 logfile_log(st
,M_FATAL
,"received SIGHUP, but could not reopen "
286 "logfile: %s",strerror(errno
));
290 logfile_log(st
,M_INFO
,"received SIGHUP");
294 static void logfile_phase_hook(void *sst
, uint32_t new_phase
)
296 struct logfile
*st
=sst
;
300 f
=fopen(st
->logfile
,"a");
301 if (!f
) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
302 st
->loc
.file
,st
->loc
.line
,st
->logfile
);
304 request_signal_notification(SIGHUP
, logfile_hup_notify
,st
);
308 static struct flagstr message_class_table
[]={
309 { "debug-config", M_DEBUG_CONFIG
},
310 { "debug-phase", M_DEBUG_PHASE
},
311 { "debug", M_DEBUG
},
312 { "all-debug", M_DEBUG
|M_DEBUG_PHASE
|M_DEBUG_CONFIG
},
314 { "notice", M_NOTICE
},
315 { "warning", M_WARNING
},
317 { "security", M_SECURITY
},
318 { "fatal", M_FATAL
},
319 { "default", M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
},
320 { "verbose", M_INFO
|M_NOTICE
|M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
},
321 { "quiet", M_FATAL
},
325 static list_t
*logfile_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
332 /* We should defer opening the logfile until the getresources
333 phase. We should defer writing into the logfile until after we
336 st
=safe_malloc(sizeof(*st
),"logfile_apply");
337 st
->cl
.description
="logfile";
340 st
->cl
.interface
=&st
->ops
;
342 st
->ops
.log
=logfile_log
;
343 st
->ops
.vlog
=logfile_vlog
;
347 item
=list_elem(args
,0);
348 if (!item
|| item
->type
!=t_dict
) {
349 cfgfatal(loc
,"logfile","argument must be a dictionary\n");
351 dict
=item
->data
.dict
;
353 st
->logfile
=dict_read_string(dict
,"filename",True
,"logfile",loc
);
354 st
->level
=string_list_to_word(dict_lookup(dict
,"class"),
355 message_class_table
,"logfile");
357 add_hook(PHASE_GETRESOURCES
,logfile_phase_hook
,st
);
359 return new_closure(&st
->cl
);
370 static int msgclass_to_syslogpriority(uint32_t m
)
373 case M_DEBUG_CONFIG
: return LOG_DEBUG
;
374 case M_DEBUG_PHASE
: return LOG_DEBUG
;
375 case M_DEBUG
: return LOG_DEBUG
;
376 case M_INFO
: return LOG_INFO
;
377 case M_NOTICE
: return LOG_NOTICE
;
378 case M_WARNING
: return LOG_WARNING
;
379 case M_ERR
: return LOG_ERR
;
380 case M_SECURITY
: return LOG_CRIT
;
381 case M_FATAL
: return LOG_EMERG
;
382 default: return LOG_NOTICE
;
386 static void syslog_vlog(void *sst
, int class, const char *message
,
389 struct syslog
*st
=sst
;
392 vsyslog(msgclass_to_syslogpriority(class),message
,args
);
394 vMessage(class,message
,args
);
399 static void syslog_log(void *sst
, int priority
, const char *message
, ...)
403 va_start(ap
,message
);
404 syslog_vlog(sst
,priority
,message
,ap
);
408 static struct flagstr syslog_facility_table
[]={
410 { "auth", LOG_AUTH
},
413 { "authpriv", LOG_AUTHPRIV
},
415 { "cron", LOG_CRON
},
416 { "daemon", LOG_DAEMON
},
417 { "kern", LOG_KERN
},
418 { "local0", LOG_LOCAL0
},
419 { "local1", LOG_LOCAL1
},
420 { "local2", LOG_LOCAL2
},
421 { "local3", LOG_LOCAL3
},
422 { "local4", LOG_LOCAL4
},
423 { "local5", LOG_LOCAL5
},
424 { "local6", LOG_LOCAL6
},
425 { "local7", LOG_LOCAL7
},
427 { "mail", LOG_MAIL
},
428 { "news", LOG_NEWS
},
429 { "syslog", LOG_SYSLOG
},
430 { "user", LOG_USER
},
431 { "uucp", LOG_UUCP
},
435 static void syslog_phase_hook(void *sst
, uint32_t newphase
)
437 struct syslog
*st
=sst
;
440 openlog(st
->ident
,0,st
->facility
);
445 static list_t
*syslog_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
453 st
=safe_malloc(sizeof(*st
),"syslog_apply");
454 st
->cl
.description
="syslog";
457 st
->cl
.interface
=&st
->ops
;
459 st
->ops
.log
=syslog_log
;
460 st
->ops
.vlog
=syslog_vlog
;
462 item
=list_elem(args
,0);
463 if (!item
|| item
->type
!=t_dict
)
464 cfgfatal(loc
,"syslog","parameter must be a dictionary\n");
467 st
->ident
=dict_read_string(d
, "ident", False
, "syslog", loc
);
468 facstr
=dict_read_string(d
, "facility", True
, "syslog", loc
);
469 st
->facility
=string_to_word(facstr
,loc
,
470 syslog_facility_table
,"syslog");
472 add_hook(PHASE_GETRESOURCES
,syslog_phase_hook
,st
);
474 return new_closure(&st
->cl
);
477 /* Read from a fd and output to a log. This is a quick hack to
478 support logging stderr, and needs code adding to tidy up before it
479 can be used for anything else. */
480 #define FDLOG_BUFSIZE 1024
490 static int log_from_fd_beforepoll(void *sst
, struct pollfd
*fds
, int *nfds_io
,
493 struct fdlog
*st
=sst
;
497 fds
[0].events
=POLLIN
;
502 static void log_from_fd_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
)
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 void log_module(dict_t
*dict
)
559 add_closure(dict
,"logfile",logfile_apply
);
560 add_closure(dict
,"syslog",syslog_apply
);