11 bool_t secnet_is_daemon=False;
12 uint32_t message_level=M_WARNING|M_ERR|M_SECURITY|M_FATAL;
13 struct log_if *system_log=NULL;
15 static void vMessage(uint32_t class, char *message, va_list args)
18 #define MESSAGE_BUFLEN 1023
19 static char buff[MESSAGE_BUFLEN+1]={0,};
23 if (secnet_is_daemon) {
24 /* Messages go to the system log interface */
26 vsnprintf(buff+bp,MESSAGE_BUFLEN-bp,message,args);
27 /* Each line is sent separately */
28 while ((nlp=strchr(buff,'\n'))) {
30 log(system_log,class,buff);
31 memmove(buff,nlp+1,strlen(nlp+1)+1);
34 /* Messages go to stdout/stderr */
35 if (class & message_level) {
36 if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
39 vfprintf(dest,message,args);
44 void Message(uint32_t class, char *message, ...)
49 vMessage(class,message,ap);
53 static NORETURN(vfatal(int status, bool_t perror, char *message,
56 static void vfatal(int status, bool_t perror, char *message, va_list args)
62 enter_phase(PHASE_SHUTDOWN);
63 Message(M_FATAL, "secnet fatal error: ");
64 vMessage(M_FATAL, message, args);
66 Message(M_FATAL, ": %s\n",strerror(err));
68 Message(M_FATAL, "\n");
72 void fatal(char *message, ...)
75 va_start(args,message);
76 vfatal(current_phase,False,message,args);
80 void fatal_status(int status, char *message, ...)
83 va_start(args,message);
84 vfatal(status,False,message,args);
88 void fatal_perror(char *message, ...)
91 va_start(args,message);
92 vfatal(current_phase,True,message,args);
96 void fatal_perror_status(int status, char *message, ...)
99 va_start(args,message);
100 vfatal(status,True,message,args);
104 void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
105 string_t facility, char *message, va_list args)
107 enter_phase(PHASE_SHUTDOWN);
109 if (maybe_f && ferror(maybe_f)) {
111 Message(M_FATAL, "error reading config file (%s, %s): %s",
112 facility, loc.file, strerror(errno));
113 } else if (maybe_f && feof(maybe_f)) {
115 Message(M_FATAL, "unexpected end of config file (%s, %s)",
117 } else if (loc.file && loc.line) {
118 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
120 } else if (!loc.file && loc.line) {
121 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
123 Message(M_FATAL, "config error (%s): ",facility);
126 vMessage(M_FATAL,message,args);
130 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, string_t facility,
135 va_start(args,message);
136 vcfgfatal_maybefile(maybe_f,loc,facility,message,args);
140 void cfgfatal(struct cloc loc, string_t facility, char *message, ...)
144 va_start(args,message);
145 vcfgfatal_maybefile(0,loc,facility,message,args);
149 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
153 Message(M_FATAL, "error reading config file (%s): %s",
154 loc.file, strerror(errno));
156 } else if (feof(f)) {
157 Message(M_FATAL, "unexpected end of config file (%s)", loc.file);
162 /* Take a list of log closures and merge them */
165 struct loglist *next;
168 static void log_vmulti(void *sst, int class, char *message, va_list args)
170 struct loglist *st=sst, *i;
172 if (secnet_is_daemon) {
173 for (i=st; i; i=i->next) {
174 i->l->vlog(i->l->st,class,message,args);
177 vMessage(class,message,args);
182 static void log_multi(void *st, int priority, char *message, ...)
186 va_start(ap,message);
187 log_vmulti(st,priority,message,ap);
191 struct log_if *init_log(list_t *ll)
196 struct loglist *l=NULL, *n;
199 if (list_length(ll)==1) {
200 item=list_elem(ll,0);
201 cl=item->data.closure;
202 if (cl->type!=CL_LOG) {
203 cfgfatal(item->loc,"init_log","closure is not a logger");
205 return cl->interface;
207 while ((item=list_elem(ll,i++))) {
208 if (item->type!=t_closure) {
209 cfgfatal(item->loc,"init_log","item is not a closure");
211 cl=item->data.closure;
212 if (cl->type!=CL_LOG) {
213 cfgfatal(item->loc,"init_log","closure is not a logger");
215 n=safe_malloc(sizeof(*n),"init_log");
221 fatal("init_log: no log");
223 r=safe_malloc(sizeof(*r), "init_log");
239 static string_t months[]={
240 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
242 static void logfile_vlog(void *sst, int class, char *message, va_list args)
244 struct logfile *st=sst;
248 if (secnet_is_daemon) {
249 if (class&st->level) {
252 fprintf(st->f,"%s %2d %02d:%02d:%02d ",
253 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
255 vfprintf(st->f,message,args);
260 vMessage(class,message,args);
265 static void logfile_log(void *state, int priority, char *message, ...)
269 va_start(ap,message);
270 logfile_vlog(state,priority,message,ap);
274 static void logfile_hup_notify(void *sst, int signum)
276 struct logfile *st=sst;
278 f=fopen(st->logfile,"a");
280 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
281 "logfile: %s",strerror(errno));
285 logfile_log(st,M_INFO,"received SIGHUP");
289 static void logfile_phase_hook(void *sst, uint32_t new_phase)
291 struct logfile *st=sst;
295 f=fopen(st->logfile,"a");
296 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
297 st->loc.file,st->loc.line,st->logfile);
299 request_signal_notification(SIGHUP, logfile_hup_notify,st);
303 static struct flagstr message_class_table[]={
304 { "debug-config", M_DEBUG_CONFIG },
305 { "debug-phase", M_DEBUG_PHASE },
306 { "debug", M_DEBUG },
307 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
309 { "notice", M_NOTICE },
310 { "warning", M_WARNING },
312 { "security", M_SECURITY },
313 { "fatal", M_FATAL },
314 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
315 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
316 { "quiet", M_FATAL },
320 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
327 /* We should defer opening the logfile until the getresources
328 phase. We should defer writing into the logfile until after we
331 st=safe_malloc(sizeof(*st),"logfile_apply");
332 st->cl.description="logfile";
335 st->cl.interface=&st->ops;
337 st->ops.log=logfile_log;
338 st->ops.vlog=logfile_vlog;
342 item=list_elem(args,0);
343 if (!item || item->type!=t_dict) {
344 cfgfatal(loc,"logfile","argument must be a dictionary\n");
346 dict=item->data.dict;
348 st->logfile=dict_read_string(dict,"filename",True,"logfile",loc);
349 st->level=string_list_to_word(dict_lookup(dict,"class"),
350 message_class_table,"logfile");
352 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
354 return new_closure(&st->cl);
365 static int msgclass_to_syslogpriority(uint32_t m)
368 case M_DEBUG_CONFIG: return LOG_DEBUG;
369 case M_DEBUG_PHASE: return LOG_DEBUG;
370 case M_DEBUG: return LOG_DEBUG;
371 case M_INFO: return LOG_INFO;
372 case M_NOTICE: return LOG_NOTICE;
373 case M_WARNING: return LOG_WARNING;
374 case M_ERR: return LOG_ERR;
375 case M_SECURITY: return LOG_CRIT;
376 case M_FATAL: return LOG_EMERG;
377 default: return LOG_NOTICE;
381 static void syslog_vlog(void *sst, int class, char *message,
384 struct syslog *st=sst;
387 vsyslog(msgclass_to_syslogpriority(class),message,args);
389 vMessage(class,message,args);
394 static void syslog_log(void *sst, int priority, char *message, ...)
398 va_start(ap,message);
399 syslog_vlog(sst,priority,message,ap);
403 static struct flagstr syslog_facility_table[]={
405 { "auth", LOG_AUTH },
408 { "authpriv", LOG_AUTHPRIV },
410 { "cron", LOG_CRON },
411 { "daemon", LOG_DAEMON },
412 { "kern", LOG_KERN },
413 { "local0", LOG_LOCAL0 },
414 { "local1", LOG_LOCAL1 },
415 { "local2", LOG_LOCAL2 },
416 { "local3", LOG_LOCAL3 },
417 { "local4", LOG_LOCAL4 },
418 { "local5", LOG_LOCAL5 },
419 { "local6", LOG_LOCAL6 },
420 { "local7", LOG_LOCAL7 },
422 { "mail", LOG_MAIL },
423 { "news", LOG_NEWS },
424 { "syslog", LOG_SYSLOG },
425 { "user", LOG_USER },
426 { "uucp", LOG_UUCP },
430 static void syslog_phase_hook(void *sst, uint32_t newphase)
432 struct syslog *st=sst;
435 openlog(st->ident,0,st->facility);
440 static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
448 st=safe_malloc(sizeof(*st),"syslog_apply");
449 st->cl.description="syslog";
452 st->cl.interface=&st->ops;
454 st->ops.log=syslog_log;
455 st->ops.vlog=syslog_vlog;
457 item=list_elem(args,0);
458 if (!item || item->type!=t_dict)
459 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
462 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
463 facstr=dict_read_string(d, "facility", True, "syslog", loc);
464 st->facility=string_to_word(facstr,loc,
465 syslog_facility_table,"syslog");
467 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
469 return new_closure(&st->cl);
472 init_module log_module;
473 void log_module(dict_t *dict)
475 add_closure(dict,"logfile",logfile_apply);
476 add_closure(dict,"syslog",syslog_apply);