Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / isc / log.c
blob03d96241109257e313ec976b8fc3a8934d762744
1 /*
2 * Copyright (C) 2004-2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: log.c,v 1.94.128.5 2009/02/16 02:10:58 marka Exp $ */
20 /*! \file
21 * \author Principal Authors: DCL */
23 #include <config.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <limits.h>
28 #include <time.h>
30 #include <sys/types.h> /* dev_t FreeBSD 2.1 */
32 #include <isc/dir.h>
33 #include <isc/file.h>
34 #include <isc/log.h>
35 #include <isc/magic.h>
36 #include <isc/mem.h>
37 #include <isc/msgs.h>
38 #include <isc/print.h>
39 #include <isc/stat.h>
40 #include <isc/stdio.h>
41 #include <isc/string.h>
42 #include <isc/time.h>
43 #include <isc/util.h>
45 #define LCTX_MAGIC ISC_MAGIC('L', 'c', 't', 'x')
46 #define VALID_CONTEXT(lctx) ISC_MAGIC_VALID(lctx, LCTX_MAGIC)
48 #define LCFG_MAGIC ISC_MAGIC('L', 'c', 'f', 'g')
49 #define VALID_CONFIG(lcfg) ISC_MAGIC_VALID(lcfg, LCFG_MAGIC)
52 * XXXDCL make dynamic?
54 #define LOG_BUFFER_SIZE (8 * 1024)
56 #ifndef PATH_MAX
57 #define PATH_MAX 1024 /* AIX and others don't define this. */
58 #endif
60 /*!
61 * This is the structure that holds each named channel. A simple linked
62 * list chains all of the channels together, so an individual channel is
63 * found by doing strcmp()s with the names down the list. Their should
64 * be no performance penalty from this as it is expected that the number
65 * of named channels will be no more than a dozen or so, and name lookups
66 * from the head of the list are only done when isc_log_usechannel() is
67 * called, which should also be very infrequent.
69 typedef struct isc_logchannel isc_logchannel_t;
71 struct isc_logchannel {
72 char * name;
73 unsigned int type;
74 int level;
75 unsigned int flags;
76 isc_logdestination_t destination;
77 ISC_LINK(isc_logchannel_t) link;
80 /*!
81 * The logchannellist structure associates categories and modules with
82 * channels. First the appropriate channellist is found based on the
83 * category, and then each structure in the linked list is checked for
84 * a matching module. It is expected that the number of channels
85 * associated with any given category will be very short, no more than
86 * three or four in the more unusual cases.
88 typedef struct isc_logchannellist isc_logchannellist_t;
90 struct isc_logchannellist {
91 const isc_logmodule_t * module;
92 isc_logchannel_t * channel;
93 ISC_LINK(isc_logchannellist_t) link;
96 /*!
97 * This structure is used to remember messages for pruning via
98 * isc_log_[v]write1().
100 typedef struct isc_logmessage isc_logmessage_t;
102 struct isc_logmessage {
103 char * text;
104 isc_time_t time;
105 ISC_LINK(isc_logmessage_t) link;
109 * The isc_logconfig structure is used to store the configurable information
110 * about where messages are actually supposed to be sent -- the information
111 * that could changed based on some configuration file, as opposed to the
112 * the category/module specification of isc_log_[v]write[1] that is compiled
113 * into a program, or the debug_level which is dynamic state information.
115 struct isc_logconfig {
116 unsigned int magic;
117 isc_log_t * lctx;
118 ISC_LIST(isc_logchannel_t) channels;
119 ISC_LIST(isc_logchannellist_t) *channellists;
120 unsigned int channellist_count;
121 unsigned int duplicate_interval;
122 int highest_level;
123 char * tag;
124 isc_boolean_t dynamic;
128 * This isc_log structure provides the context for the isc_log functions.
129 * The log context locks itself in isc_log_doit, the internal backend to
130 * isc_log_write. The locking is necessary both to provide exclusive access
131 * to the buffer into which the message is formatted and to guard against
132 * competing threads trying to write to the same syslog resource. (On
133 * some systems, such as BSD/OS, stdio is thread safe but syslog is not.)
134 * Unfortunately, the lock cannot guard against a _different_ logging
135 * context in the same program competing for syslog's attention. Thus
136 * There Can Be Only One, but this is not enforced.
137 * XXXDCL enforce it?
139 * Note that the category and module information is not locked.
140 * This is because in the usual case, only one isc_log_t is ever created
141 * in a program, and the category/module registration happens only once.
142 * XXXDCL it might be wise to add more locking overall.
144 struct isc_log {
145 /* Not locked. */
146 unsigned int magic;
147 isc_mem_t * mctx;
148 isc_logcategory_t * categories;
149 unsigned int category_count;
150 isc_logmodule_t * modules;
151 unsigned int module_count;
152 int debug_level;
153 isc_mutex_t lock;
154 /* Locked by isc_log lock. */
155 isc_logconfig_t * logconfig;
156 char buffer[LOG_BUFFER_SIZE];
157 ISC_LIST(isc_logmessage_t) messages;
161 * Used when ISC_LOG_PRINTLEVEL is enabled for a channel.
163 static const char *log_level_strings[] = {
164 "debug",
165 "info",
166 "notice",
167 "warning",
168 "error",
169 "critical"
173 * Used to convert ISC_LOG_* priorities into syslog priorities.
174 * XXXDCL This will need modification for NT.
176 static const int syslog_map[] = {
177 LOG_DEBUG,
178 LOG_INFO,
179 LOG_NOTICE,
180 LOG_WARNING,
181 LOG_ERR,
182 LOG_CRIT
186 * When adding new categories, a corresponding ISC_LOGCATEGORY_foo
187 * definition needs to be added to <isc/log.h>.
189 * The default category is provided so that the internal default can
190 * be overridden. Since the default is always looked up as the first
191 * channellist in the log context, it must come first in isc_categories[].
193 LIBISC_EXTERNAL_DATA isc_logcategory_t isc_categories[] = {
194 { "default", 0 }, /* "default" must come first. */
195 { "general", 0 },
196 { NULL, 0 }
200 * See above comment for categories on LIBISC_EXTERNAL_DATA, and apply it to modules.
202 LIBISC_EXTERNAL_DATA isc_logmodule_t isc_modules[] = {
203 { "socket", 0 },
204 { "time", 0 },
205 { "interface", 0 },
206 { "timer", 0 },
207 { "file", 0 },
208 { NULL, 0 }
212 * This essentially constant structure must be filled in at run time,
213 * because its channel member is pointed to a channel that is created
214 * dynamically with isc_log_createchannel.
216 static isc_logchannellist_t default_channel;
219 * libisc logs to this context.
221 LIBISC_EXTERNAL_DATA isc_log_t *isc_lctx = NULL;
224 * Forward declarations.
226 static isc_result_t
227 assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
228 const isc_logmodule_t *module, isc_logchannel_t *channel);
230 static isc_result_t
231 sync_channellist(isc_logconfig_t *lcfg);
233 static isc_result_t
234 greatest_version(isc_logchannel_t *channel, int *greatest);
236 static isc_result_t
237 roll_log(isc_logchannel_t *channel);
239 static void
240 isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
241 isc_logmodule_t *module, int level, isc_boolean_t write_once,
242 isc_msgcat_t *msgcat, int msgset, int msg,
243 const char *format, va_list args)
244 ISC_FORMAT_PRINTF(9, 0);
246 /*@{*/
248 * Convenience macros.
251 #define FACILITY(channel) (channel->destination.facility)
252 #define FILE_NAME(channel) (channel->destination.file.name)
253 #define FILE_STREAM(channel) (channel->destination.file.stream)
254 #define FILE_VERSIONS(channel) (channel->destination.file.versions)
255 #define FILE_MAXSIZE(channel) (channel->destination.file.maximum_size)
256 #define FILE_MAXREACHED(channel) (channel->destination.file.maximum_reached)
258 /*@}*/
259 /****
260 **** Public interfaces.
261 ****/
264 * Establish a new logging context, with default channels.
266 isc_result_t
267 isc_log_create(isc_mem_t *mctx, isc_log_t **lctxp, isc_logconfig_t **lcfgp) {
268 isc_log_t *lctx;
269 isc_logconfig_t *lcfg = NULL;
270 isc_result_t result;
272 REQUIRE(mctx != NULL);
273 REQUIRE(lctxp != NULL && *lctxp == NULL);
274 REQUIRE(lcfgp == NULL || *lcfgp == NULL);
276 lctx = isc_mem_get(mctx, sizeof(*lctx));
277 if (lctx != NULL) {
278 lctx->mctx = mctx;
279 lctx->categories = NULL;
280 lctx->category_count = 0;
281 lctx->modules = NULL;
282 lctx->module_count = 0;
283 lctx->debug_level = 0;
285 ISC_LIST_INIT(lctx->messages);
287 result = isc_mutex_init(&lctx->lock);
288 if (result != ISC_R_SUCCESS) {
289 isc_mem_put(mctx, lctx, sizeof(*lctx));
290 return (result);
294 * Normally setting the magic number is the last step done
295 * in a creation function, but a valid log context is needed
296 * by isc_log_registercategories and isc_logconfig_create.
297 * If either fails, the lctx is destroyed and not returned
298 * to the caller.
300 lctx->magic = LCTX_MAGIC;
302 isc_log_registercategories(lctx, isc_categories);
303 isc_log_registermodules(lctx, isc_modules);
304 result = isc_logconfig_create(lctx, &lcfg);
306 } else
307 result = ISC_R_NOMEMORY;
309 if (result == ISC_R_SUCCESS)
310 result = sync_channellist(lcfg);
312 if (result == ISC_R_SUCCESS) {
313 lctx->logconfig = lcfg;
315 *lctxp = lctx;
316 if (lcfgp != NULL)
317 *lcfgp = lcfg;
319 } else {
320 if (lcfg != NULL)
321 isc_logconfig_destroy(&lcfg);
322 if (lctx != NULL)
323 isc_log_destroy(&lctx);
326 return (result);
329 isc_result_t
330 isc_logconfig_create(isc_log_t *lctx, isc_logconfig_t **lcfgp) {
331 isc_logconfig_t *lcfg;
332 isc_logdestination_t destination;
333 isc_result_t result = ISC_R_SUCCESS;
334 int level = ISC_LOG_INFO;
336 REQUIRE(lcfgp != NULL && *lcfgp == NULL);
337 REQUIRE(VALID_CONTEXT(lctx));
339 lcfg = isc_mem_get(lctx->mctx, sizeof(*lcfg));
341 if (lcfg != NULL) {
342 lcfg->lctx = lctx;
343 lcfg->channellists = NULL;
344 lcfg->channellist_count = 0;
345 lcfg->duplicate_interval = 0;
346 lcfg->highest_level = level;
347 lcfg->tag = NULL;
348 lcfg->dynamic = ISC_FALSE;
350 ISC_LIST_INIT(lcfg->channels);
353 * Normally the magic number is the last thing set in the
354 * structure, but isc_log_createchannel() needs a valid
355 * config. If the channel creation fails, the lcfg is not
356 * returned to the caller.
358 lcfg->magic = LCFG_MAGIC;
360 } else
361 result = ISC_R_NOMEMORY;
364 * Create the default channels:
365 * default_syslog, default_stderr, default_debug and null.
367 if (result == ISC_R_SUCCESS) {
368 destination.facility = LOG_DAEMON;
369 result = isc_log_createchannel(lcfg, "default_syslog",
370 ISC_LOG_TOSYSLOG, level,
371 &destination, 0);
374 if (result == ISC_R_SUCCESS) {
375 destination.file.stream = stderr;
376 destination.file.name = NULL;
377 destination.file.versions = ISC_LOG_ROLLNEVER;
378 destination.file.maximum_size = 0;
379 result = isc_log_createchannel(lcfg, "default_stderr",
380 ISC_LOG_TOFILEDESC,
381 level,
382 &destination,
383 ISC_LOG_PRINTTIME);
386 if (result == ISC_R_SUCCESS) {
388 * Set the default category's channel to default_stderr,
389 * which is at the head of the channels list because it was
390 * just created.
392 default_channel.channel = ISC_LIST_HEAD(lcfg->channels);
394 destination.file.stream = stderr;
395 destination.file.name = NULL;
396 destination.file.versions = ISC_LOG_ROLLNEVER;
397 destination.file.maximum_size = 0;
398 result = isc_log_createchannel(lcfg, "default_debug",
399 ISC_LOG_TOFILEDESC,
400 ISC_LOG_DYNAMIC,
401 &destination,
402 ISC_LOG_PRINTTIME);
405 if (result == ISC_R_SUCCESS)
406 result = isc_log_createchannel(lcfg, "null",
407 ISC_LOG_TONULL,
408 ISC_LOG_DYNAMIC,
409 NULL, 0);
411 if (result == ISC_R_SUCCESS)
412 *lcfgp = lcfg;
414 else
415 if (lcfg != NULL)
416 isc_logconfig_destroy(&lcfg);
418 return (result);
421 isc_logconfig_t *
422 isc_logconfig_get(isc_log_t *lctx) {
423 REQUIRE(VALID_CONTEXT(lctx));
425 ENSURE(lctx->logconfig != NULL);
427 return (lctx->logconfig);
430 isc_result_t
431 isc_logconfig_use(isc_log_t *lctx, isc_logconfig_t *lcfg) {
432 isc_logconfig_t *old_cfg;
433 isc_result_t result;
435 REQUIRE(VALID_CONTEXT(lctx));
436 REQUIRE(VALID_CONFIG(lcfg));
437 REQUIRE(lcfg->lctx == lctx);
440 * Ensure that lcfg->channellist_count == lctx->category_count.
441 * They won't be equal if isc_log_usechannel has not been called
442 * since any call to isc_log_registercategories.
444 result = sync_channellist(lcfg);
445 if (result != ISC_R_SUCCESS)
446 return (result);
448 LOCK(&lctx->lock);
450 old_cfg = lctx->logconfig;
451 lctx->logconfig = lcfg;
453 UNLOCK(&lctx->lock);
455 isc_logconfig_destroy(&old_cfg);
457 return (ISC_R_SUCCESS);
460 void
461 isc_log_destroy(isc_log_t **lctxp) {
462 isc_log_t *lctx;
463 isc_logconfig_t *lcfg;
464 isc_mem_t *mctx;
465 isc_logmessage_t *message;
467 REQUIRE(lctxp != NULL && VALID_CONTEXT(*lctxp));
469 lctx = *lctxp;
470 mctx = lctx->mctx;
472 if (lctx->logconfig != NULL) {
473 lcfg = lctx->logconfig;
474 lctx->logconfig = NULL;
475 isc_logconfig_destroy(&lcfg);
478 DESTROYLOCK(&lctx->lock);
480 while ((message = ISC_LIST_HEAD(lctx->messages)) != NULL) {
481 ISC_LIST_UNLINK(lctx->messages, message, link);
483 isc_mem_put(mctx, message,
484 sizeof(*message) + strlen(message->text) + 1);
487 lctx->buffer[0] = '\0';
488 lctx->debug_level = 0;
489 lctx->categories = NULL;
490 lctx->category_count = 0;
491 lctx->modules = NULL;
492 lctx->module_count = 0;
493 lctx->mctx = NULL;
494 lctx->magic = 0;
496 isc_mem_put(mctx, lctx, sizeof(*lctx));
498 *lctxp = NULL;
501 void
502 isc_logconfig_destroy(isc_logconfig_t **lcfgp) {
503 isc_logconfig_t *lcfg;
504 isc_mem_t *mctx;
505 isc_logchannel_t *channel;
506 isc_logchannellist_t *item;
507 char *filename;
508 unsigned int i;
510 REQUIRE(lcfgp != NULL && VALID_CONFIG(*lcfgp));
512 lcfg = *lcfgp;
515 * This function cannot be called with a logconfig that is in
516 * use by a log context.
518 REQUIRE(lcfg->lctx != NULL && lcfg->lctx->logconfig != lcfg);
520 mctx = lcfg->lctx->mctx;
522 while ((channel = ISC_LIST_HEAD(lcfg->channels)) != NULL) {
523 ISC_LIST_UNLINK(lcfg->channels, channel, link);
525 if (channel->type == ISC_LOG_TOFILE) {
527 * The filename for the channel may have ultimately
528 * started its life in user-land as a const string,
529 * but in isc_log_createchannel it gets copied
530 * into writable memory and is not longer truly const.
532 DE_CONST(FILE_NAME(channel), filename);
533 isc_mem_free(mctx, filename);
535 if (FILE_STREAM(channel) != NULL)
536 (void)fclose(FILE_STREAM(channel));
539 isc_mem_free(mctx, channel->name);
540 isc_mem_put(mctx, channel, sizeof(*channel));
543 for (i = 0; i < lcfg->channellist_count; i++)
544 while ((item = ISC_LIST_HEAD(lcfg->channellists[i])) != NULL) {
545 ISC_LIST_UNLINK(lcfg->channellists[i], item, link);
546 isc_mem_put(mctx, item, sizeof(*item));
549 if (lcfg->channellist_count > 0)
550 isc_mem_put(mctx, lcfg->channellists,
551 lcfg->channellist_count *
552 sizeof(ISC_LIST(isc_logchannellist_t)));
554 lcfg->dynamic = ISC_FALSE;
555 if (lcfg->tag != NULL)
556 isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
557 lcfg->tag = NULL;
558 lcfg->highest_level = 0;
559 lcfg->duplicate_interval = 0;
560 lcfg->magic = 0;
562 isc_mem_put(mctx, lcfg, sizeof(*lcfg));
564 *lcfgp = NULL;
567 void
568 isc_log_registercategories(isc_log_t *lctx, isc_logcategory_t categories[]) {
569 isc_logcategory_t *catp;
571 REQUIRE(VALID_CONTEXT(lctx));
572 REQUIRE(categories != NULL && categories[0].name != NULL);
575 * XXXDCL This somewhat sleazy situation of using the last pointer
576 * in one category array to point to the next array exists because
577 * this registration function returns void and I didn't want to have
578 * change everything that used it by making it return an isc_result_t.
579 * It would need to do that if it had to allocate memory to store
580 * pointers to each array passed in.
582 if (lctx->categories == NULL)
583 lctx->categories = categories;
585 else {
587 * Adjust the last (NULL) pointer of the already registered
588 * categories to point to the incoming array.
590 for (catp = lctx->categories; catp->name != NULL; )
591 if (catp->id == UINT_MAX)
593 * The name pointer points to the next array.
594 * Ick.
596 DE_CONST(catp->name, catp);
597 else
598 catp++;
600 catp->name = (void *)categories;
601 catp->id = UINT_MAX;
605 * Update the id number of the category with its new global id.
607 for (catp = categories; catp->name != NULL; catp++)
608 catp->id = lctx->category_count++;
611 isc_logcategory_t *
612 isc_log_categorybyname(isc_log_t *lctx, const char *name) {
613 isc_logcategory_t *catp;
615 REQUIRE(VALID_CONTEXT(lctx));
616 REQUIRE(name != NULL);
618 for (catp = lctx->categories; catp->name != NULL; )
619 if (catp->id == UINT_MAX)
621 * catp is neither modified nor returned to the
622 * caller, so removing its const qualifier is ok.
624 DE_CONST(catp->name, catp);
625 else {
626 if (strcmp(catp->name, name) == 0)
627 return (catp);
628 catp++;
631 return (NULL);
634 void
635 isc_log_registermodules(isc_log_t *lctx, isc_logmodule_t modules[]) {
636 isc_logmodule_t *modp;
638 REQUIRE(VALID_CONTEXT(lctx));
639 REQUIRE(modules != NULL && modules[0].name != NULL);
642 * XXXDCL This somewhat sleazy situation of using the last pointer
643 * in one category array to point to the next array exists because
644 * this registration function returns void and I didn't want to have
645 * change everything that used it by making it return an isc_result_t.
646 * It would need to do that if it had to allocate memory to store
647 * pointers to each array passed in.
649 if (lctx->modules == NULL)
650 lctx->modules = modules;
652 else {
654 * Adjust the last (NULL) pointer of the already registered
655 * modules to point to the incoming array.
657 for (modp = lctx->modules; modp->name != NULL; )
658 if (modp->id == UINT_MAX)
660 * The name pointer points to the next array.
661 * Ick.
663 DE_CONST(modp->name, modp);
664 else
665 modp++;
667 modp->name = (void *)modules;
668 modp->id = UINT_MAX;
672 * Update the id number of the module with its new global id.
674 for (modp = modules; modp->name != NULL; modp++)
675 modp->id = lctx->module_count++;
678 isc_logmodule_t *
679 isc_log_modulebyname(isc_log_t *lctx, const char *name) {
680 isc_logmodule_t *modp;
682 REQUIRE(VALID_CONTEXT(lctx));
683 REQUIRE(name != NULL);
685 for (modp = lctx->modules; modp->name != NULL; )
686 if (modp->id == UINT_MAX)
688 * modp is neither modified nor returned to the
689 * caller, so removing its const qualifier is ok.
691 DE_CONST(modp->name, modp);
692 else {
693 if (strcmp(modp->name, name) == 0)
694 return (modp);
695 modp++;
698 return (NULL);
701 isc_result_t
702 isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
703 unsigned int type, int level,
704 const isc_logdestination_t *destination,
705 unsigned int flags)
707 isc_logchannel_t *channel;
708 isc_mem_t *mctx;
710 REQUIRE(VALID_CONFIG(lcfg));
711 REQUIRE(name != NULL);
712 REQUIRE(type == ISC_LOG_TOSYSLOG || type == ISC_LOG_TOFILE ||
713 type == ISC_LOG_TOFILEDESC || type == ISC_LOG_TONULL);
714 REQUIRE(destination != NULL || type == ISC_LOG_TONULL);
715 REQUIRE(level >= ISC_LOG_CRITICAL);
716 REQUIRE((flags &
717 (unsigned int)~(ISC_LOG_PRINTALL | ISC_LOG_DEBUGONLY)) == 0);
719 /* XXXDCL find duplicate names? */
721 mctx = lcfg->lctx->mctx;
723 channel = isc_mem_get(mctx, sizeof(*channel));
724 if (channel == NULL)
725 return (ISC_R_NOMEMORY);
727 channel->name = isc_mem_strdup(mctx, name);
728 if (channel->name == NULL) {
729 isc_mem_put(mctx, channel, sizeof(*channel));
730 return (ISC_R_NOMEMORY);
733 channel->type = type;
734 channel->level = level;
735 channel->flags = flags;
736 ISC_LINK_INIT(channel, link);
738 switch (type) {
739 case ISC_LOG_TOSYSLOG:
740 FACILITY(channel) = destination->facility;
741 break;
743 case ISC_LOG_TOFILE:
745 * The file name is copied because greatest_version wants
746 * to scribble on it, so it needs to be definitely in
747 * writable memory.
749 FILE_NAME(channel) =
750 isc_mem_strdup(mctx, destination->file.name);
751 FILE_STREAM(channel) = NULL;
752 FILE_VERSIONS(channel) = destination->file.versions;
753 FILE_MAXSIZE(channel) = destination->file.maximum_size;
754 FILE_MAXREACHED(channel) = ISC_FALSE;
755 break;
757 case ISC_LOG_TOFILEDESC:
758 FILE_NAME(channel) = NULL;
759 FILE_STREAM(channel) = destination->file.stream;
760 FILE_MAXSIZE(channel) = 0;
761 FILE_VERSIONS(channel) = ISC_LOG_ROLLNEVER;
762 break;
764 case ISC_LOG_TONULL:
765 /* Nothing. */
766 break;
768 default:
769 isc_mem_put(mctx, channel->name, strlen(channel->name) + 1);
770 isc_mem_put(mctx, channel, sizeof(*channel));
771 return (ISC_R_UNEXPECTED);
774 ISC_LIST_PREPEND(lcfg->channels, channel, link);
777 * If default_stderr was redefined, make the default category
778 * point to the new default_stderr.
780 if (strcmp(name, "default_stderr") == 0)
781 default_channel.channel = channel;
783 return (ISC_R_SUCCESS);
786 isc_result_t
787 isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
788 const isc_logcategory_t *category,
789 const isc_logmodule_t *module)
791 isc_log_t *lctx;
792 isc_logchannel_t *channel;
793 isc_result_t result = ISC_R_SUCCESS;
794 unsigned int i;
796 REQUIRE(VALID_CONFIG(lcfg));
797 REQUIRE(name != NULL);
799 lctx = lcfg->lctx;
801 REQUIRE(category == NULL || category->id < lctx->category_count);
802 REQUIRE(module == NULL || module->id < lctx->module_count);
804 for (channel = ISC_LIST_HEAD(lcfg->channels); channel != NULL;
805 channel = ISC_LIST_NEXT(channel, link))
806 if (strcmp(name, channel->name) == 0)
807 break;
809 if (channel == NULL)
810 return (ISC_R_NOTFOUND);
812 if (category != NULL)
813 result = assignchannel(lcfg, category->id, module, channel);
815 else
817 * Assign to all categories. Note that this includes
818 * the default channel.
820 for (i = 0; i < lctx->category_count; i++) {
821 result = assignchannel(lcfg, i, module, channel);
822 if (result != ISC_R_SUCCESS)
823 break;
826 return (result);
829 void
830 isc_log_write(isc_log_t *lctx, isc_logcategory_t *category,
831 isc_logmodule_t *module, int level, const char *format, ...)
833 va_list args;
836 * Contract checking is done in isc_log_doit().
839 va_start(args, format);
840 isc_log_doit(lctx, category, module, level, ISC_FALSE,
841 NULL, 0, 0, format, args);
842 va_end(args);
845 void
846 isc_log_vwrite(isc_log_t *lctx, isc_logcategory_t *category,
847 isc_logmodule_t *module, int level,
848 const char *format, va_list args)
851 * Contract checking is done in isc_log_doit().
853 isc_log_doit(lctx, category, module, level, ISC_FALSE,
854 NULL, 0, 0, format, args);
857 void
858 isc_log_write1(isc_log_t *lctx, isc_logcategory_t *category,
859 isc_logmodule_t *module, int level, const char *format, ...)
861 va_list args;
864 * Contract checking is done in isc_log_doit().
867 va_start(args, format);
868 isc_log_doit(lctx, category, module, level, ISC_TRUE,
869 NULL, 0, 0, format, args);
870 va_end(args);
873 void
874 isc_log_vwrite1(isc_log_t *lctx, isc_logcategory_t *category,
875 isc_logmodule_t *module, int level,
876 const char *format, va_list args)
879 * Contract checking is done in isc_log_doit().
881 isc_log_doit(lctx, category, module, level, ISC_TRUE,
882 NULL, 0, 0, format, args);
885 void
886 isc_log_iwrite(isc_log_t *lctx, isc_logcategory_t *category,
887 isc_logmodule_t *module, int level,
888 isc_msgcat_t *msgcat, int msgset, int msg,
889 const char *format, ...)
891 va_list args;
894 * Contract checking is done in isc_log_doit().
897 va_start(args, format);
898 isc_log_doit(lctx, category, module, level, ISC_FALSE,
899 msgcat, msgset, msg, format, args);
900 va_end(args);
903 void
904 isc_log_ivwrite(isc_log_t *lctx, isc_logcategory_t *category,
905 isc_logmodule_t *module, int level,
906 isc_msgcat_t *msgcat, int msgset, int msg,
907 const char *format, va_list args)
910 * Contract checking is done in isc_log_doit().
912 isc_log_doit(lctx, category, module, level, ISC_FALSE,
913 msgcat, msgset, msg, format, args);
916 void
917 isc_log_iwrite1(isc_log_t *lctx, isc_logcategory_t *category,
918 isc_logmodule_t *module, int level,
919 isc_msgcat_t *msgcat, int msgset, int msg,
920 const char *format, ...)
922 va_list args;
925 * Contract checking is done in isc_log_doit().
928 va_start(args, format);
929 isc_log_doit(lctx, category, module, level, ISC_TRUE,
930 msgcat, msgset, msg, format, args);
931 va_end(args);
934 void
935 isc_log_ivwrite1(isc_log_t *lctx, isc_logcategory_t *category,
936 isc_logmodule_t *module, int level,
937 isc_msgcat_t *msgcat, int msgset, int msg,
938 const char *format, va_list args)
941 * Contract checking is done in isc_log_doit().
943 isc_log_doit(lctx, category, module, level, ISC_TRUE,
944 msgcat, msgset, msg, format, args);
947 void
948 isc_log_setcontext(isc_log_t *lctx) {
949 isc_lctx = lctx;
952 void
953 isc_log_setdebuglevel(isc_log_t *lctx, unsigned int level) {
954 isc_logchannel_t *channel;
956 REQUIRE(VALID_CONTEXT(lctx));
958 LOCK(&lctx->lock);
960 lctx->debug_level = level;
962 * Close ISC_LOG_DEBUGONLY channels if level is zero.
964 if (lctx->debug_level == 0)
965 for (channel = ISC_LIST_HEAD(lctx->logconfig->channels);
966 channel != NULL;
967 channel = ISC_LIST_NEXT(channel, link))
968 if (channel->type == ISC_LOG_TOFILE &&
969 (channel->flags & ISC_LOG_DEBUGONLY) != 0 &&
970 FILE_STREAM(channel) != NULL) {
971 (void)fclose(FILE_STREAM(channel));
972 FILE_STREAM(channel) = NULL;
974 UNLOCK(&lctx->lock);
977 unsigned int
978 isc_log_getdebuglevel(isc_log_t *lctx) {
979 REQUIRE(VALID_CONTEXT(lctx));
981 return (lctx->debug_level);
984 void
985 isc_log_setduplicateinterval(isc_logconfig_t *lcfg, unsigned int interval) {
986 REQUIRE(VALID_CONFIG(lcfg));
988 lcfg->duplicate_interval = interval;
991 unsigned int
992 isc_log_getduplicateinterval(isc_logconfig_t *lcfg) {
993 REQUIRE(VALID_CONTEXT(lcfg));
995 return (lcfg->duplicate_interval);
998 isc_result_t
999 isc_log_settag(isc_logconfig_t *lcfg, const char *tag) {
1000 REQUIRE(VALID_CONFIG(lcfg));
1002 if (tag != NULL && *tag != '\0') {
1003 if (lcfg->tag != NULL)
1004 isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
1005 lcfg->tag = isc_mem_strdup(lcfg->lctx->mctx, tag);
1006 if (lcfg->tag == NULL)
1007 return (ISC_R_NOMEMORY);
1009 } else {
1010 if (lcfg->tag != NULL)
1011 isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
1012 lcfg->tag = NULL;
1015 return (ISC_R_SUCCESS);
1018 char *
1019 isc_log_gettag(isc_logconfig_t *lcfg) {
1020 REQUIRE(VALID_CONFIG(lcfg));
1022 return (lcfg->tag);
1025 /* XXXDCL NT -- This interface will assuredly be changing. */
1026 void
1027 isc_log_opensyslog(const char *tag, int options, int facility) {
1028 (void)openlog(tag, options, facility);
1031 void
1032 isc_log_closefilelogs(isc_log_t *lctx) {
1033 isc_logchannel_t *channel;
1035 REQUIRE(VALID_CONTEXT(lctx));
1037 LOCK(&lctx->lock);
1038 for (channel = ISC_LIST_HEAD(lctx->logconfig->channels);
1039 channel != NULL;
1040 channel = ISC_LIST_NEXT(channel, link))
1042 if (channel->type == ISC_LOG_TOFILE &&
1043 FILE_STREAM(channel) != NULL) {
1044 (void)fclose(FILE_STREAM(channel));
1045 FILE_STREAM(channel) = NULL;
1047 UNLOCK(&lctx->lock);
1050 /****
1051 **** Internal functions
1052 ****/
1054 static isc_result_t
1055 assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
1056 const isc_logmodule_t *module, isc_logchannel_t *channel)
1058 isc_logchannellist_t *new_item;
1059 isc_log_t *lctx;
1060 isc_result_t result;
1062 REQUIRE(VALID_CONFIG(lcfg));
1064 lctx = lcfg->lctx;
1066 REQUIRE(category_id < lctx->category_count);
1067 REQUIRE(module == NULL || module->id < lctx->module_count);
1068 REQUIRE(channel != NULL);
1071 * Ensure lcfg->channellist_count == lctx->category_count.
1073 result = sync_channellist(lcfg);
1074 if (result != ISC_R_SUCCESS)
1075 return (result);
1077 new_item = isc_mem_get(lctx->mctx, sizeof(*new_item));
1078 if (new_item == NULL)
1079 return (ISC_R_NOMEMORY);
1081 new_item->channel = channel;
1082 new_item->module = module;
1083 ISC_LIST_INITANDPREPEND(lcfg->channellists[category_id],
1084 new_item, link);
1087 * Remember the highest logging level set by any channel in the
1088 * logging config, so isc_log_doit() can quickly return if the
1089 * message is too high to be logged by any channel.
1091 if (channel->type != ISC_LOG_TONULL) {
1092 if (lcfg->highest_level < channel->level)
1093 lcfg->highest_level = channel->level;
1094 if (channel->level == ISC_LOG_DYNAMIC)
1095 lcfg->dynamic = ISC_TRUE;
1098 return (ISC_R_SUCCESS);
1102 * This would ideally be part of isc_log_registercategories(), except then
1103 * that function would have to return isc_result_t instead of void.
1105 static isc_result_t
1106 sync_channellist(isc_logconfig_t *lcfg) {
1107 unsigned int bytes;
1108 isc_log_t *lctx;
1109 void *lists;
1111 REQUIRE(VALID_CONFIG(lcfg));
1113 lctx = lcfg->lctx;
1115 REQUIRE(lctx->category_count != 0);
1117 if (lctx->category_count == lcfg->channellist_count)
1118 return (ISC_R_SUCCESS);
1120 bytes = lctx->category_count * sizeof(ISC_LIST(isc_logchannellist_t));
1122 lists = isc_mem_get(lctx->mctx, bytes);
1124 if (lists == NULL)
1125 return (ISC_R_NOMEMORY);
1127 memset(lists, 0, bytes);
1129 if (lcfg->channellist_count != 0) {
1130 bytes = lcfg->channellist_count *
1131 sizeof(ISC_LIST(isc_logchannellist_t));
1132 memcpy(lists, lcfg->channellists, bytes);
1133 isc_mem_put(lctx->mctx, lcfg->channellists, bytes);
1136 lcfg->channellists = lists;
1137 lcfg->channellist_count = lctx->category_count;
1139 return (ISC_R_SUCCESS);
1142 static isc_result_t
1143 greatest_version(isc_logchannel_t *channel, int *greatestp) {
1144 /* XXXDCL HIGHLY NT */
1145 char *basename, *digit_end;
1146 const char *dirname;
1147 int version, greatest = -1;
1148 unsigned int basenamelen;
1149 isc_dir_t dir;
1150 isc_result_t result;
1151 char sep = '/';
1152 #ifdef _WIN32
1153 char *basename2;
1154 #endif
1156 REQUIRE(channel->type == ISC_LOG_TOFILE);
1159 * It is safe to DE_CONST the file.name because it was copied
1160 * with isc_mem_strdup in isc_log_createchannel.
1162 basename = strrchr(FILE_NAME(channel), sep);
1163 #ifdef _WIN32
1164 basename2 = strrchr(FILE_NAME(channel), '\\');
1165 if ((basename != NULL && basename2 != NULL && basename2 > basename) ||
1166 (basename == NULL && basename2 != NULL)) {
1167 basename = basename2;
1168 sep = '\\';
1170 #endif
1171 if (basename != NULL) {
1172 *basename++ = '\0';
1173 dirname = FILE_NAME(channel);
1174 } else {
1175 DE_CONST(FILE_NAME(channel), basename);
1176 dirname = ".";
1178 basenamelen = strlen(basename);
1180 isc_dir_init(&dir);
1181 result = isc_dir_open(&dir, dirname);
1184 * Replace the file separator if it was taken out.
1186 if (basename != FILE_NAME(channel))
1187 *(basename - 1) = sep;
1190 * Return if the directory open failed.
1192 if (result != ISC_R_SUCCESS)
1193 return (result);
1195 while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
1196 if (dir.entry.length > basenamelen &&
1197 strncmp(dir.entry.name, basename, basenamelen) == 0 &&
1198 dir.entry.name[basenamelen] == '.') {
1200 version = strtol(&dir.entry.name[basenamelen + 1],
1201 &digit_end, 10);
1202 if (*digit_end == '\0' && version > greatest)
1203 greatest = version;
1206 isc_dir_close(&dir);
1208 *greatestp = ++greatest;
1210 return (ISC_R_SUCCESS);
1213 static isc_result_t
1214 roll_log(isc_logchannel_t *channel) {
1215 int i, n, greatest;
1216 char current[PATH_MAX + 1];
1217 char new[PATH_MAX + 1];
1218 const char *path;
1219 isc_result_t result;
1222 * Do nothing (not even excess version trimming) if ISC_LOG_ROLLNEVER
1223 * is specified. Apparently complete external control over the log
1224 * files is desired.
1226 if (FILE_VERSIONS(channel) == ISC_LOG_ROLLNEVER)
1227 return (ISC_R_SUCCESS);
1229 path = FILE_NAME(channel);
1232 * Set greatest_version to the greatest existing version
1233 * (not the maximum requested version). This is 1 based even
1234 * though the file names are 0 based, so an oldest log of log.1
1235 * is a greatest_version of 2.
1237 result = greatest_version(channel, &greatest);
1238 if (result != ISC_R_SUCCESS)
1239 return (result);
1242 * Now greatest should be set to the highest version number desired.
1243 * Since the highest number is one less than FILE_VERSIONS(channel)
1244 * when not doing infinite log rolling, greatest will need to be
1245 * decremented when it is equal to -- or greater than --
1246 * FILE_VERSIONS(channel). When greatest is less than
1247 * FILE_VERSIONS(channel), it is already suitable for use as
1248 * the maximum version number.
1251 if (FILE_VERSIONS(channel) == ISC_LOG_ROLLINFINITE ||
1252 FILE_VERSIONS(channel) > greatest)
1253 ; /* Do nothing. */
1254 else
1256 * When greatest is >= FILE_VERSIONS(channel), it needs to
1257 * be reduced until it is FILE_VERSIONS(channel) - 1.
1258 * Remove any excess logs on the way to that value.
1260 while (--greatest >= FILE_VERSIONS(channel)) {
1261 n = snprintf(current, sizeof(current), "%s.%d",
1262 path, greatest);
1263 if (n >= (int)sizeof(current) || n < 0)
1264 result = ISC_R_NOSPACE;
1265 else
1266 result = isc_file_remove(current);
1267 if (result != ISC_R_SUCCESS &&
1268 result != ISC_R_FILENOTFOUND)
1269 syslog(LOG_ERR,
1270 "unable to remove log file '%s.%d': %s",
1271 path, greatest,
1272 isc_result_totext(result));
1275 for (i = greatest; i > 0; i--) {
1276 result = ISC_R_SUCCESS;
1277 n = snprintf(current, sizeof(current), "%s.%d", path, i - 1);
1278 if (n >= (int)sizeof(current) || n < 0)
1279 result = ISC_R_NOSPACE;
1280 if (result == ISC_R_SUCCESS) {
1281 n = snprintf(new, sizeof(new), "%s.%d", path, i);
1282 if (n >= (int)sizeof(new) || n < 0)
1283 result = ISC_R_NOSPACE;
1285 if (result == ISC_R_SUCCESS)
1286 result = isc_file_rename(current, new);
1287 if (result != ISC_R_SUCCESS &&
1288 result != ISC_R_FILENOTFOUND)
1289 syslog(LOG_ERR,
1290 "unable to rename log file '%s.%d' to "
1291 "'%s.%d': %s", path, i - 1, path, i,
1292 isc_result_totext(result));
1295 if (FILE_VERSIONS(channel) != 0) {
1296 n = snprintf(new, sizeof(new), "%s.0", path);
1297 if (n >= (int)sizeof(new) || n < 0)
1298 result = ISC_R_NOSPACE;
1299 else
1300 result = isc_file_rename(path, new);
1301 if (result != ISC_R_SUCCESS &&
1302 result != ISC_R_FILENOTFOUND)
1303 syslog(LOG_ERR,
1304 "unable to rename log file '%s' to '%s.0': %s",
1305 path, path, isc_result_totext(result));
1306 } else {
1307 result = isc_file_remove(path);
1308 if (result != ISC_R_SUCCESS &&
1309 result != ISC_R_FILENOTFOUND)
1310 syslog(LOG_ERR, "unable to remove log file '%s': %s",
1311 path, isc_result_totext(result));
1314 return (ISC_R_SUCCESS);
1317 static isc_result_t
1318 isc_log_open(isc_logchannel_t *channel) {
1319 struct stat statbuf;
1320 isc_boolean_t regular_file;
1321 isc_boolean_t roll = ISC_FALSE;
1322 isc_result_t result = ISC_R_SUCCESS;
1323 const char *path;
1325 REQUIRE(channel->type == ISC_LOG_TOFILE);
1326 REQUIRE(FILE_STREAM(channel) == NULL);
1328 path = FILE_NAME(channel);
1330 REQUIRE(path != NULL && *path != '\0');
1333 * Determine type of file; only regular files will be
1334 * version renamed, and only if the base file exists
1335 * and either has no size limit or has reached its size limit.
1337 if (stat(path, &statbuf) == 0) {
1338 regular_file = S_ISREG(statbuf.st_mode) ? ISC_TRUE : ISC_FALSE;
1339 /* XXXDCL if not regular_file complain? */
1340 if ((FILE_MAXSIZE(channel) == 0 &&
1341 FILE_VERSIONS(channel) != ISC_LOG_ROLLNEVER) ||
1342 (FILE_MAXSIZE(channel) > 0 &&
1343 statbuf.st_size >= FILE_MAXSIZE(channel)))
1344 roll = regular_file;
1345 } else if (errno == ENOENT)
1346 regular_file = ISC_TRUE;
1347 else
1348 result = ISC_R_INVALIDFILE;
1351 * Version control.
1353 if (result == ISC_R_SUCCESS && roll) {
1354 if (FILE_VERSIONS(channel) == ISC_LOG_ROLLNEVER)
1355 return (ISC_R_MAXSIZE);
1356 result = roll_log(channel);
1357 if (result != ISC_R_SUCCESS) {
1358 if ((channel->flags & ISC_LOG_OPENERR) == 0) {
1359 syslog(LOG_ERR,
1360 "isc_log_open: roll_log '%s' "
1361 "failed: %s",
1362 FILE_NAME(channel),
1363 isc_result_totext(result));
1364 channel->flags |= ISC_LOG_OPENERR;
1366 return (result);
1370 result = isc_stdio_open(path, "a", &FILE_STREAM(channel));
1372 return (result);
1375 isc_boolean_t
1376 isc_log_wouldlog(isc_log_t *lctx, int level) {
1378 * Try to avoid locking the mutex for messages which can't
1379 * possibly be logged to any channels -- primarily debugging
1380 * messages that the debug level is not high enough to print.
1382 * If the level is (mathematically) less than or equal to the
1383 * highest_level, or if there is a dynamic channel and the level is
1384 * less than or equal to the debug level, the main loop must be
1385 * entered to see if the message should really be output.
1387 * NOTE: this is UNLOCKED access to the logconfig. However,
1388 * the worst thing that can happen is that a bad decision is made
1389 * about returning without logging, and that's not a big concern,
1390 * because that's a risk anyway if the logconfig is being
1391 * dynamically changed.
1394 if (lctx == NULL || lctx->logconfig == NULL)
1395 return (ISC_FALSE);
1397 return (ISC_TF(level <= lctx->logconfig->highest_level ||
1398 (lctx->logconfig->dynamic &&
1399 level <= lctx->debug_level)));
1402 static void
1403 isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
1404 isc_logmodule_t *module, int level, isc_boolean_t write_once,
1405 isc_msgcat_t *msgcat, int msgset, int msg,
1406 const char *format, va_list args)
1408 int syslog_level;
1409 char time_string[64];
1410 char level_string[24];
1411 const char *iformat;
1412 struct stat statbuf;
1413 isc_boolean_t matched = ISC_FALSE;
1414 isc_boolean_t printtime, printtag;
1415 isc_boolean_t printcategory, printmodule, printlevel;
1416 isc_logconfig_t *lcfg;
1417 isc_logchannel_t *channel;
1418 isc_logchannellist_t *category_channels;
1419 isc_result_t result;
1421 REQUIRE(lctx == NULL || VALID_CONTEXT(lctx));
1422 REQUIRE(category != NULL);
1423 REQUIRE(module != NULL);
1424 REQUIRE(level != ISC_LOG_DYNAMIC);
1425 REQUIRE(format != NULL);
1428 * Programs can use libraries that use this logging code without
1429 * wanting to do any logging, thus the log context is allowed to
1430 * be non-existent.
1432 if (lctx == NULL)
1433 return;
1435 REQUIRE(category->id < lctx->category_count);
1436 REQUIRE(module->id < lctx->module_count);
1438 if (! isc_log_wouldlog(lctx, level))
1439 return;
1441 if (msgcat != NULL)
1442 iformat = isc_msgcat_get(msgcat, msgset, msg, format);
1443 else
1444 iformat = format;
1446 time_string[0] = '\0';
1447 level_string[0] = '\0';
1449 LOCK(&lctx->lock);
1451 lctx->buffer[0] = '\0';
1453 lcfg = lctx->logconfig;
1455 category_channels = ISC_LIST_HEAD(lcfg->channellists[category->id]);
1458 * XXXDCL add duplicate filtering? (To not write multiple times to
1459 * the same source via various channels).
1461 do {
1463 * If the channel list end was reached and a match was made,
1464 * everything is finished.
1466 if (category_channels == NULL && matched)
1467 break;
1469 if (category_channels == NULL && ! matched &&
1470 category_channels != ISC_LIST_HEAD(lcfg->channellists[0]))
1472 * No category/module pair was explicitly configured.
1473 * Try the category named "default".
1475 category_channels =
1476 ISC_LIST_HEAD(lcfg->channellists[0]);
1478 if (category_channels == NULL && ! matched)
1480 * No matching module was explicitly configured
1481 * for the category named "default". Use the internal
1482 * default channel.
1484 category_channels = &default_channel;
1486 if (category_channels->module != NULL &&
1487 category_channels->module != module) {
1488 category_channels = ISC_LIST_NEXT(category_channels,
1489 link);
1490 continue;
1493 matched = ISC_TRUE;
1495 channel = category_channels->channel;
1496 category_channels = ISC_LIST_NEXT(category_channels, link);
1498 if (((channel->flags & ISC_LOG_DEBUGONLY) != 0) &&
1499 lctx->debug_level == 0)
1500 continue;
1502 if (channel->level == ISC_LOG_DYNAMIC) {
1503 if (lctx->debug_level < level)
1504 continue;
1505 } else if (channel->level < level)
1506 continue;
1508 if ((channel->flags & ISC_LOG_PRINTTIME) != 0 &&
1509 time_string[0] == '\0') {
1510 isc_time_t isctime;
1512 TIME_NOW(&isctime);
1513 isc_time_formattimestamp(&isctime, time_string,
1514 sizeof(time_string));
1517 if ((channel->flags & ISC_LOG_PRINTLEVEL) != 0 &&
1518 level_string[0] == '\0') {
1519 if (level < ISC_LOG_CRITICAL)
1520 snprintf(level_string, sizeof(level_string),
1521 isc_msgcat_get(isc_msgcat,
1522 ISC_MSGSET_LOG,
1523 ISC_MSG_LEVEL,
1524 "level %d: "),
1525 level);
1526 else if (level > ISC_LOG_DYNAMIC)
1527 snprintf(level_string, sizeof(level_string),
1528 "%s %d: ", log_level_strings[0],
1529 level);
1530 else
1531 snprintf(level_string, sizeof(level_string),
1532 "%s: ", log_level_strings[-level]);
1536 * Only format the message once.
1538 if (lctx->buffer[0] == '\0') {
1539 (void)vsnprintf(lctx->buffer, sizeof(lctx->buffer),
1540 iformat, args);
1543 * Check for duplicates.
1545 if (write_once) {
1546 isc_logmessage_t *message, *new;
1547 isc_time_t oldest;
1548 isc_interval_t interval;
1550 isc_interval_set(&interval,
1551 lcfg->duplicate_interval, 0);
1554 * 'oldest' is the age of the oldest messages
1555 * which fall within the duplicate_interval
1556 * range.
1558 TIME_NOW(&oldest);
1559 if (isc_time_subtract(&oldest, &interval, &oldest)
1560 != ISC_R_SUCCESS)
1562 * Can't effectively do the checking
1563 * without having a valid time.
1565 message = NULL;
1566 else
1567 message =ISC_LIST_HEAD(lctx->messages);
1569 while (message != NULL) {
1570 if (isc_time_compare(&message->time,
1571 &oldest) < 0) {
1573 * This message is older
1574 * than the duplicate_interval,
1575 * so it should be dropped from
1576 * the history.
1578 * Setting the interval to be
1579 * to be longer will obviously
1580 * not cause the expired
1581 * message to spring back into
1582 * existence.
1584 new = ISC_LIST_NEXT(message,
1585 link);
1587 ISC_LIST_UNLINK(lctx->messages,
1588 message, link);
1590 isc_mem_put(lctx->mctx,
1591 message,
1592 sizeof(*message) + 1 +
1593 strlen(message->text));
1595 message = new;
1596 continue;
1600 * This message is in the duplicate
1601 * filtering interval ...
1603 if (strcmp(lctx->buffer, message->text)
1604 == 0) {
1606 * ... and it is a duplicate.
1607 * Unlock the mutex and
1608 * get the hell out of Dodge.
1610 UNLOCK(&lctx->lock);
1611 return;
1614 message = ISC_LIST_NEXT(message, link);
1618 * It wasn't in the duplicate interval,
1619 * so add it to the message list.
1621 new = isc_mem_get(lctx->mctx,
1622 sizeof(isc_logmessage_t) +
1623 strlen(lctx->buffer) + 1);
1624 if (new != NULL) {
1626 * Put the text immediately after
1627 * the struct. The strcpy is safe.
1629 new->text = (char *)(new + 1);
1630 strcpy(new->text, lctx->buffer);
1632 TIME_NOW(&new->time);
1634 ISC_LIST_APPEND(lctx->messages,
1635 new, link);
1640 printtime = ISC_TF((channel->flags & ISC_LOG_PRINTTIME)
1641 != 0);
1642 printtag = ISC_TF((channel->flags & ISC_LOG_PRINTTAG)
1643 != 0 && lcfg->tag != NULL);
1644 printcategory = ISC_TF((channel->flags & ISC_LOG_PRINTCATEGORY)
1645 != 0);
1646 printmodule = ISC_TF((channel->flags & ISC_LOG_PRINTMODULE)
1647 != 0);
1648 printlevel = ISC_TF((channel->flags & ISC_LOG_PRINTLEVEL)
1649 != 0);
1651 switch (channel->type) {
1652 case ISC_LOG_TOFILE:
1653 if (FILE_MAXREACHED(channel)) {
1655 * If the file can be rolled, OR
1656 * If the file no longer exists, OR
1657 * If the file is less than the maximum size,
1658 * (such as if it had been renamed and
1659 * a new one touched, or it was truncated
1660 * in place)
1661 * ... then close it to trigger reopening.
1663 if (FILE_VERSIONS(channel) !=
1664 ISC_LOG_ROLLNEVER ||
1665 (stat(FILE_NAME(channel), &statbuf) != 0 &&
1666 errno == ENOENT) ||
1667 statbuf.st_size < FILE_MAXSIZE(channel)) {
1668 (void)fclose(FILE_STREAM(channel));
1669 FILE_STREAM(channel) = NULL;
1670 FILE_MAXREACHED(channel) = ISC_FALSE;
1671 } else
1673 * Eh, skip it.
1675 break;
1678 if (FILE_STREAM(channel) == NULL) {
1679 result = isc_log_open(channel);
1680 if (result != ISC_R_SUCCESS &&
1681 result != ISC_R_MAXSIZE &&
1682 (channel->flags & ISC_LOG_OPENERR) == 0) {
1683 syslog(LOG_ERR,
1684 "isc_log_open '%s' failed: %s",
1685 FILE_NAME(channel),
1686 isc_result_totext(result));
1687 channel->flags |= ISC_LOG_OPENERR;
1689 if (result != ISC_R_SUCCESS)
1690 break;
1691 channel->flags &= ~ISC_LOG_OPENERR;
1693 /* FALLTHROUGH */
1695 case ISC_LOG_TOFILEDESC:
1696 fprintf(FILE_STREAM(channel), "%s%s%s%s%s%s%s%s%s%s\n",
1697 printtime ? time_string : "",
1698 printtime ? " " : "",
1699 printtag ? lcfg->tag : "",
1700 printtag ? ": " : "",
1701 printcategory ? category->name : "",
1702 printcategory ? ": " : "",
1703 printmodule ? (module != NULL ? module->name
1704 : "no_module")
1705 : "",
1706 printmodule ? ": " : "",
1707 printlevel ? level_string : "",
1708 lctx->buffer);
1710 fflush(FILE_STREAM(channel));
1713 * If the file now exceeds its maximum size
1714 * threshold, note it so that it will not be logged
1715 * to any more.
1717 if (FILE_MAXSIZE(channel) > 0) {
1718 INSIST(channel->type == ISC_LOG_TOFILE);
1720 /* XXXDCL NT fstat/fileno */
1721 /* XXXDCL complain if fstat fails? */
1722 if (fstat(fileno(FILE_STREAM(channel)),
1723 &statbuf) >= 0 &&
1724 statbuf.st_size > FILE_MAXSIZE(channel))
1725 FILE_MAXREACHED(channel) = ISC_TRUE;
1728 break;
1730 case ISC_LOG_TOSYSLOG:
1731 if (level > 0)
1732 syslog_level = LOG_DEBUG;
1733 else if (level < ISC_LOG_CRITICAL)
1734 syslog_level = LOG_CRIT;
1735 else
1736 syslog_level = syslog_map[-level];
1738 (void)syslog(FACILITY(channel) | syslog_level,
1739 "%s%s%s%s%s%s%s%s%s%s",
1740 printtime ? time_string : "",
1741 printtime ? " " : "",
1742 printtag ? lcfg->tag : "",
1743 printtag ? ": " : "",
1744 printcategory ? category->name : "",
1745 printcategory ? ": " : "",
1746 printmodule ? (module != NULL ? module->name
1747 : "no_module")
1748 : "",
1749 printmodule ? ": " : "",
1750 printlevel ? level_string : "",
1751 lctx->buffer);
1752 break;
1754 case ISC_LOG_TONULL:
1755 break;
1759 } while (1);
1761 UNLOCK(&lctx->lock);