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.332.5 2009-02-16 02:04:05 marka Exp $ */
21 * \author Principal Authors: DCL */
30 #include <sys/types.h> /* dev_t FreeBSD 2.1 */
35 #include <isc/magic.h>
38 #include <isc/print.h>
40 #include <isc/stdio.h>
41 #include <isc/string.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)
57 #define PATH_MAX 1024 /* AIX and others don't define this. */
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 {
76 isc_logdestination_t destination;
77 ISC_LINK(isc_logchannel_t) link;
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;
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 {
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 {
118 ISC_LIST(isc_logchannel_t) channels;
119 ISC_LIST(isc_logchannellist_t) *channellists;
120 unsigned int channellist_count;
121 unsigned int duplicate_interval;
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.
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.
148 isc_logcategory_t * categories;
149 unsigned int category_count;
150 isc_logmodule_t * modules;
151 unsigned int module_count;
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[] = {
173 * Used to convert ISC_LOG_* priorities into syslog priorities.
174 * XXXDCL This will need modification for NT.
176 static const int syslog_map[] = {
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. */
200 * See above comment for categories on LIBISC_EXTERNAL_DATA, and apply it to modules.
202 LIBISC_EXTERNAL_DATA isc_logmodule_t isc_modules[] = {
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.
227 assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
228 const isc_logmodule_t *module, isc_logchannel_t *channel);
231 sync_channellist(isc_logconfig_t *lcfg);
234 greatest_version(isc_logchannel_t *channel, int *greatest);
237 roll_log(isc_logchannel_t *channel);
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);
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)
260 **** Public interfaces.
264 * Establish a new logging context, with default channels.
267 isc_log_create(isc_mem_t *mctx, isc_log_t **lctxp, isc_logconfig_t **lcfgp) {
269 isc_logconfig_t *lcfg = NULL;
272 REQUIRE(mctx != NULL);
273 REQUIRE(lctxp != NULL && *lctxp == NULL);
274 REQUIRE(lcfgp == NULL || *lcfgp == NULL);
276 lctx = isc_mem_get(mctx, sizeof(*lctx));
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));
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
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);
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;
321 isc_logconfig_destroy(&lcfg);
323 isc_log_destroy(&lctx);
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));
343 lcfg->channellists = NULL;
344 lcfg->channellist_count = 0;
345 lcfg->duplicate_interval = 0;
346 lcfg->highest_level = level;
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;
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,
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",
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
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",
405 if (result == ISC_R_SUCCESS)
406 result = isc_log_createchannel(lcfg, "null",
411 if (result == ISC_R_SUCCESS)
416 isc_logconfig_destroy(&lcfg);
422 isc_logconfig_get(isc_log_t *lctx) {
423 REQUIRE(VALID_CONTEXT(lctx));
425 ENSURE(lctx->logconfig != NULL);
427 return (lctx->logconfig);
431 isc_logconfig_use(isc_log_t *lctx, isc_logconfig_t *lcfg) {
432 isc_logconfig_t *old_cfg;
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)
450 old_cfg = lctx->logconfig;
451 lctx->logconfig = lcfg;
455 isc_logconfig_destroy(&old_cfg);
457 return (ISC_R_SUCCESS);
461 isc_log_destroy(isc_log_t **lctxp) {
463 isc_logconfig_t *lcfg;
465 isc_logmessage_t *message;
467 REQUIRE(lctxp != NULL && VALID_CONTEXT(*lctxp));
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;
496 isc_mem_put(mctx, lctx, sizeof(*lctx));
502 isc_logconfig_destroy(isc_logconfig_t **lcfgp) {
503 isc_logconfig_t *lcfg;
505 isc_logchannel_t *channel;
506 isc_logchannellist_t *item;
510 REQUIRE(lcfgp != NULL && VALID_CONFIG(*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);
558 lcfg->highest_level = 0;
559 lcfg->duplicate_interval = 0;
562 isc_mem_put(mctx, lcfg, sizeof(*lcfg));
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;
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.
596 DE_CONST(catp->name, catp);
600 catp->name = (void *)categories;
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++;
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);
626 if (strcmp(catp->name, name) == 0)
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;
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.
663 DE_CONST(modp->name, modp);
667 modp->name = (void *)modules;
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++;
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);
693 if (strcmp(modp->name, name) == 0)
702 isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
703 unsigned int type, int level,
704 const isc_logdestination_t *destination,
707 isc_logchannel_t *channel;
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);
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));
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);
739 case ISC_LOG_TOSYSLOG:
740 FACILITY(channel) = destination->facility;
745 * The file name is copied because greatest_version wants
746 * to scribble on it, so it needs to be definitely in
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;
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;
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);
787 isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
788 const isc_logcategory_t *category,
789 const isc_logmodule_t *module)
792 isc_logchannel_t *channel;
793 isc_result_t result = ISC_R_SUCCESS;
796 REQUIRE(VALID_CONFIG(lcfg));
797 REQUIRE(name != NULL);
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)
810 return (ISC_R_NOTFOUND);
812 if (category != NULL)
813 result = assignchannel(lcfg, category->id, module, channel);
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)
830 isc_log_write(isc_log_t *lctx, isc_logcategory_t *category,
831 isc_logmodule_t *module, int level, const char *format, ...)
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);
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);
858 isc_log_write1(isc_log_t *lctx, isc_logcategory_t *category,
859 isc_logmodule_t *module, int level, const char *format, ...)
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);
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);
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, ...)
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);
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);
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, ...)
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);
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);
948 isc_log_setcontext(isc_log_t *lctx) {
953 isc_log_setdebuglevel(isc_log_t *lctx, unsigned int level) {
954 isc_logchannel_t *channel;
956 REQUIRE(VALID_CONTEXT(lctx));
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);
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;
978 isc_log_getdebuglevel(isc_log_t *lctx) {
979 REQUIRE(VALID_CONTEXT(lctx));
981 return (lctx->debug_level);
985 isc_log_setduplicateinterval(isc_logconfig_t *lcfg, unsigned int interval) {
986 REQUIRE(VALID_CONFIG(lcfg));
988 lcfg->duplicate_interval = interval;
992 isc_log_getduplicateinterval(isc_logconfig_t *lcfg) {
993 REQUIRE(VALID_CONTEXT(lcfg));
995 return (lcfg->duplicate_interval);
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);
1010 if (lcfg->tag != NULL)
1011 isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
1015 return (ISC_R_SUCCESS);
1019 isc_log_gettag(isc_logconfig_t *lcfg) {
1020 REQUIRE(VALID_CONFIG(lcfg));
1025 /* XXXDCL NT -- This interface will assuredly be changing. */
1027 isc_log_opensyslog(const char *tag, int options, int facility) {
1028 (void)openlog(tag, options, facility);
1032 isc_log_closefilelogs(isc_log_t *lctx) {
1033 isc_logchannel_t *channel;
1035 REQUIRE(VALID_CONTEXT(lctx));
1038 for (channel = ISC_LIST_HEAD(lctx->logconfig->channels);
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);
1051 **** Internal functions
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;
1060 isc_result_t result;
1062 REQUIRE(VALID_CONFIG(lcfg));
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)
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],
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.
1106 sync_channellist(isc_logconfig_t *lcfg) {
1111 REQUIRE(VALID_CONFIG(lcfg));
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);
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);
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;
1150 isc_result_t result;
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);
1164 basename2 = strrchr(FILE_NAME(channel), '\\');
1165 if ((basename != NULL && basename2 != NULL && basename2 > basename) ||
1166 (basename == NULL && basename2 != NULL)) {
1167 basename = basename2;
1171 if (basename != NULL) {
1173 dirname = FILE_NAME(channel);
1175 DE_CONST(FILE_NAME(channel), basename);
1178 basenamelen = strlen(basename);
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)
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],
1202 if (*digit_end == '\0' && version > greatest)
1206 isc_dir_close(&dir);
1208 *greatestp = ++greatest;
1210 return (ISC_R_SUCCESS);
1214 roll_log(isc_logchannel_t *channel) {
1216 char current[PATH_MAX + 1];
1217 char new[PATH_MAX + 1];
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
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)
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)
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",
1263 if (n >= (int)sizeof(current) || n < 0)
1264 result = ISC_R_NOSPACE;
1266 result = isc_file_remove(current);
1267 if (result != ISC_R_SUCCESS &&
1268 result != ISC_R_FILENOTFOUND)
1270 "unable to remove log file '%s.%d': %s",
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)
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;
1300 result = isc_file_rename(path, new);
1301 if (result != ISC_R_SUCCESS &&
1302 result != ISC_R_FILENOTFOUND)
1304 "unable to rename log file '%s' to '%s.0': %s",
1305 path, path, isc_result_totext(result));
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);
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;
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;
1348 result = ISC_R_INVALIDFILE;
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) {
1360 "isc_log_open: roll_log '%s' "
1363 isc_result_totext(result));
1364 channel->flags |= ISC_LOG_OPENERR;
1370 result = isc_stdio_open(path, "a", &FILE_STREAM(channel));
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)
1397 return (ISC_TF(level <= lctx->logconfig->highest_level ||
1398 (lctx->logconfig->dynamic &&
1399 level <= lctx->debug_level)));
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)
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
1435 REQUIRE(category->id < lctx->category_count);
1436 REQUIRE(module->id < lctx->module_count);
1438 if (! isc_log_wouldlog(lctx, level))
1442 iformat = isc_msgcat_get(msgcat, msgset, msg, format);
1446 time_string[0] = '\0';
1447 level_string[0] = '\0';
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).
1463 * If the channel list end was reached and a match was made,
1464 * everything is finished.
1466 if (category_channels == NULL && matched)
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".
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
1484 category_channels = &default_channel;
1486 if (category_channels->module != NULL &&
1487 category_channels->module != module) {
1488 category_channels = ISC_LIST_NEXT(category_channels,
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)
1502 if (channel->level == ISC_LOG_DYNAMIC) {
1503 if (lctx->debug_level < level)
1505 } else if (channel->level < level)
1508 if ((channel->flags & ISC_LOG_PRINTTIME) != 0 &&
1509 time_string[0] == '\0') {
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,
1526 else if (level > ISC_LOG_DYNAMIC)
1527 snprintf(level_string, sizeof(level_string),
1528 "%s %d: ", log_level_strings[0],
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),
1543 * Check for duplicates.
1546 isc_logmessage_t *message, *new;
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
1559 if (isc_time_subtract(&oldest, &interval, &oldest)
1562 * Can't effectively do the checking
1563 * without having a valid time.
1567 message =ISC_LIST_HEAD(lctx->messages);
1569 while (message != NULL) {
1570 if (isc_time_compare(&message->time,
1573 * This message is older
1574 * than the duplicate_interval,
1575 * so it should be dropped from
1578 * Setting the interval to be
1579 * to be longer will obviously
1580 * not cause the expired
1581 * message to spring back into
1584 new = ISC_LIST_NEXT(message,
1587 ISC_LIST_UNLINK(lctx->messages,
1590 isc_mem_put(lctx->mctx,
1592 sizeof(*message) + 1 +
1593 strlen(message->text));
1600 * This message is in the duplicate
1601 * filtering interval ...
1603 if (strcmp(lctx->buffer, message->text)
1606 * ... and it is a duplicate.
1607 * Unlock the mutex and
1608 * get the hell out of Dodge.
1610 UNLOCK(&lctx->lock);
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);
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,
1640 printtime = ISC_TF((channel->flags & ISC_LOG_PRINTTIME)
1642 printtag = ISC_TF((channel->flags & ISC_LOG_PRINTTAG)
1643 != 0 && lcfg->tag != NULL);
1644 printcategory = ISC_TF((channel->flags & ISC_LOG_PRINTCATEGORY)
1646 printmodule = ISC_TF((channel->flags & ISC_LOG_PRINTMODULE)
1648 printlevel = ISC_TF((channel->flags & ISC_LOG_PRINTLEVEL)
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
1661 * ... then close it to trigger reopening.
1663 if (FILE_VERSIONS(channel) !=
1664 ISC_LOG_ROLLNEVER ||
1665 (stat(FILE_NAME(channel), &statbuf) != 0 &&
1667 statbuf.st_size < FILE_MAXSIZE(channel)) {
1668 (void)fclose(FILE_STREAM(channel));
1669 FILE_STREAM(channel) = NULL;
1670 FILE_MAXREACHED(channel) = ISC_FALSE;
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) {
1684 "isc_log_open '%s' failed: %s",
1686 isc_result_totext(result));
1687 channel->flags |= ISC_LOG_OPENERR;
1689 if (result != ISC_R_SUCCESS)
1691 channel->flags &= ~ISC_LOG_OPENERR;
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
1706 printmodule ? ": " : "",
1707 printlevel ? level_string : "",
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
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)),
1724 statbuf.st_size > FILE_MAXSIZE(channel))
1725 FILE_MAXREACHED(channel) = ISC_TRUE;
1730 case ISC_LOG_TOSYSLOG:
1732 syslog_level = LOG_DEBUG;
1733 else if (level < ISC_LOG_CRITICAL)
1734 syslog_level = LOG_CRIT;
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
1749 printmodule ? ": " : "",
1750 printlevel ? level_string : "",
1754 case ISC_LOG_TONULL:
1761 UNLOCK(&lctx->lock);