10 bool_t secnet_is_daemon
=False
;
11 uint32_t message_level
=M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
;
12 struct log_if
*system_log
=NULL
;
14 static void vMessage(uint32_t class, char *message
, va_list args
)
17 #define MESSAGE_BUFLEN 1023
18 static char buff
[MESSAGE_BUFLEN
+1]={0,};
22 if (secnet_is_daemon
) {
23 /* Messages go to the system log interface */
25 vsnprintf(buff
+bp
,MESSAGE_BUFLEN
-bp
,message
,args
);
26 /* Each line is sent separately */
27 while ((nlp
=strchr(buff
,'\n'))) {
29 log(system_log
,class,buff
);
30 memmove(buff
,nlp
+1,strlen(nlp
+1)+1);
33 /* Messages go to stdout/stderr */
34 if (class & message_level
) {
35 if (class&M_FATAL
|| class&M_ERR
|| class&M_WARNING
) {
38 vfprintf(dest
,message
,args
);
43 void Message(uint32_t class, char *message
, ...)
48 vMessage(class,message
,ap
);
52 static void vfatal(int status
, bool_t perror
, char *message
, va_list args
)
58 enter_phase(PHASE_SHUTDOWN
);
60 Message(M_FATAL
, "secnet fatal error: ");
61 vMessage(M_FATAL
, message
, args
);
62 Message(M_FATAL
, ": %s\n",strerror(err
));
65 Message(M_FATAL
, "secnet fatal error: ");
66 vMessage(M_FATAL
,message
,args
);
71 void fatal(char *message
, ...)
74 va_start(args
,message
);
75 vfatal(current_phase
,False
,message
,args
);
79 void fatal_status(int status
, char *message
, ...)
82 va_start(args
,message
);
83 vfatal(status
,False
,message
,args
);
87 void fatal_perror(char *message
, ...)
90 va_start(args
,message
);
91 vfatal(current_phase
,True
,message
,args
);
95 void fatal_perror_status(int status
, char *message
, ...)
98 va_start(args
,message
);
99 vfatal(status
,True
,message
,args
);
103 void cfgfatal(struct cloc loc
, string_t facility
, char *message
, ...)
107 va_start(args
,message
);
109 enter_phase(PHASE_SHUTDOWN
);
111 if (loc
.file
&& loc
.line
) {
112 Message(M_FATAL
, "config error (%s, %s:%d): ",facility
,loc
.file
,
114 } else if (!loc
.file
&& loc
.line
) {
115 Message(M_FATAL
, "config error (%s, line %d): ",facility
,loc
.line
);
117 Message(M_FATAL
, "config error (%s): ",facility
);
120 vMessage(M_FATAL
,message
,args
);
125 /* Take a list of log closures and merge them */
128 struct loglist
*next
;
131 static void log_vmulti(void *sst
, int class, char *message
, va_list args
)
133 struct loglist
*st
=sst
, *i
;
135 if (secnet_is_daemon
) {
136 for (i
=st
; i
; i
=i
->next
) {
137 i
->l
->vlog(i
->l
->st
,class,message
,args
);
140 vMessage(class,message
,args
);
145 static void log_multi(void *st
, int priority
, char *message
, ...)
149 va_start(ap
,message
);
150 log_vmulti(st
,priority
,message
,ap
);
154 struct log_if
*init_log(list_t
*ll
)
159 struct loglist
*l
=NULL
, *n
;
162 if (list_length(ll
)==1) {
163 item
=list_elem(ll
,0);
164 cl
=item
->data
.closure
;
165 if (cl
->type
!=CL_LOG
) {
166 cfgfatal(item
->loc
,"init_log","closure is not a logger");
168 return cl
->interface
;
170 while ((item
=list_elem(ll
,i
++))) {
171 if (item
->type
!=t_closure
) {
172 cfgfatal(item
->loc
,"init_log","item is not a closure");
174 cl
=item
->data
.closure
;
175 if (cl
->type
!=CL_LOG
) {
176 cfgfatal(item
->loc
,"init_log","closure is not a logger");
178 n
=safe_malloc(sizeof(*n
),"init_log");
184 fatal("init_log: no log");
186 r
=safe_malloc(sizeof(*r
), "init_log");
202 static string_t months
[]={
203 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
205 static void logfile_vlog(void *sst
, int class, char *message
, va_list args
)
207 struct logfile
*st
=sst
;
211 if (secnet_is_daemon
) {
212 if (class&st
->level
) {
215 fprintf(st
->f
,"%s %2d %02d:%02d:%02d ",
216 months
[tm
->tm_mon
],tm
->tm_mday
,tm
->tm_hour
,tm
->tm_min
,
218 vfprintf(st
->f
,message
,args
);
223 vMessage(class,message
,args
);
228 static void logfile_log(void *state
, int priority
, char *message
, ...)
232 va_start(ap
,message
);
233 logfile_vlog(state
,priority
,message
,ap
);
237 static void logfile_hup_notify(void *sst
, int signum
)
239 struct logfile
*st
=sst
;
241 f
=fopen(st
->logfile
,"a");
243 logfile_log(st
,M_FATAL
,"received SIGHUP, but could not reopen "
244 "logfile: %s",strerror(errno
));
248 logfile_log(st
,M_INFO
,"received SIGHUP");
252 static void logfile_phase_hook(void *sst
, uint32_t new_phase
)
254 struct logfile
*st
=sst
;
258 f
=fopen(st
->logfile
,"a");
259 if (!f
) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
260 st
->loc
.file
,st
->loc
.line
,st
->logfile
);
262 request_signal_notification(SIGHUP
, logfile_hup_notify
,st
);
266 static struct flagstr message_class_table
[]={
267 { "debug-config", M_DEBUG_CONFIG
},
268 { "debug-phase", M_DEBUG_PHASE
},
269 { "debug", M_DEBUG
},
270 { "all-debug", M_DEBUG
|M_DEBUG_PHASE
|M_DEBUG_CONFIG
},
272 { "notice", M_NOTICE
},
273 { "warning", M_WARNING
},
275 { "security", M_SECURITY
},
276 { "fatal", M_FATAL
},
277 { "default", M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
},
278 { "verbose", M_INFO
|M_NOTICE
|M_WARNING
|M_ERR
|M_SECURITY
|M_FATAL
},
279 { "quiet", M_FATAL
},
283 static list_t
*logfile_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
290 /* We should defer opening the logfile until the getresources
291 phase. We should defer writing into the logfile until after we
294 st
=safe_malloc(sizeof(*st
),"logfile_apply");
295 st
->cl
.description
="logfile";
298 st
->cl
.interface
=&st
->ops
;
300 st
->ops
.log
=logfile_log
;
301 st
->ops
.vlog
=logfile_vlog
;
305 item
=list_elem(args
,0);
306 if (!item
|| item
->type
!=t_dict
) {
307 cfgfatal(loc
,"logfile","argument must be a dictionary\n");
309 dict
=item
->data
.dict
;
311 st
->logfile
=dict_read_string(dict
,"filename",True
,"logfile",loc
);
312 st
->level
=string_list_to_word(dict_lookup(dict
,"class"),
313 message_class_table
,"logfile");
315 add_hook(PHASE_GETRESOURCES
,logfile_phase_hook
,st
);
317 return new_closure(&st
->cl
);
328 static int msgclass_to_syslogpriority(uint32_t m
)
331 case M_DEBUG_CONFIG
: return LOG_DEBUG
;
332 case M_DEBUG_PHASE
: return LOG_DEBUG
;
333 case M_DEBUG
: return LOG_DEBUG
;
334 case M_INFO
: return LOG_INFO
;
335 case M_NOTICE
: return LOG_NOTICE
;
336 case M_WARNING
: return LOG_WARNING
;
337 case M_ERR
: return LOG_ERR
;
338 case M_SECURITY
: return LOG_CRIT
;
339 case M_FATAL
: return LOG_EMERG
;
340 default: return LOG_NOTICE
;
344 static void syslog_vlog(void *sst
, int class, char *message
,
347 struct syslog
*st
=sst
;
350 vsyslog(msgclass_to_syslogpriority(class),message
,args
);
352 vMessage(class,message
,args
);
357 static void syslog_log(void *sst
, int priority
, char *message
, ...)
361 va_start(ap
,message
);
362 syslog_vlog(sst
,priority
,message
,ap
);
366 static struct flagstr syslog_facility_table
[]={
368 { "auth", LOG_AUTH
},
371 { "authpriv", LOG_AUTHPRIV
},
373 { "cron", LOG_CRON
},
374 { "daemon", LOG_DAEMON
},
375 { "kern", LOG_KERN
},
376 { "local0", LOG_LOCAL0
},
377 { "local1", LOG_LOCAL1
},
378 { "local2", LOG_LOCAL2
},
379 { "local3", LOG_LOCAL3
},
380 { "local4", LOG_LOCAL4
},
381 { "local5", LOG_LOCAL5
},
382 { "local6", LOG_LOCAL6
},
383 { "local7", LOG_LOCAL7
},
385 { "mail", LOG_MAIL
},
386 { "news", LOG_NEWS
},
387 { "syslog", LOG_SYSLOG
},
388 { "user", LOG_USER
},
389 { "uucp", LOG_UUCP
},
393 static void syslog_phase_hook(void *sst
, uint32_t newphase
)
395 struct syslog
*st
=sst
;
398 openlog(st
->ident
,0,st
->facility
);
403 static list_t
*syslog_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
411 st
=safe_malloc(sizeof(*st
),"syslog_apply");
412 st
->cl
.description
="syslog";
415 st
->cl
.interface
=&st
->ops
;
417 st
->ops
.log
=syslog_log
;
418 st
->ops
.vlog
=syslog_vlog
;
420 item
=list_elem(args
,0);
421 if (!item
|| item
->type
!=t_dict
)
422 cfgfatal(loc
,"syslog","parameter must be a dictionary\n");
425 st
->ident
=dict_read_string(d
, "ident", False
, "syslog", loc
);
426 facstr
=dict_read_string(d
, "facility", True
, "syslog", loc
);
427 st
->facility
=string_to_word(facstr
,loc
,
428 syslog_facility_table
,"syslog");
430 add_hook(PHASE_GETRESOURCES
,syslog_phase_hook
,st
);
432 return new_closure(&st
->cl
);
435 init_module log_module
;
436 void log_module(dict_t
*dict
)
438 add_closure(dict
,"logfile",logfile_apply
);
439 add_closure(dict
,"syslog",syslog_apply
);