Home | History | Annotate | Line # | Download | only in isc
log.c revision 1.10
      1   1.9  christos /*	$NetBSD: log.c,v 1.10 2024/02/21 22:52:28 christos Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*
      4   1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5   1.1  christos  *
      6   1.7  christos  * SPDX-License-Identifier: MPL-2.0
      7   1.7  christos  *
      8   1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      9   1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10   1.6  christos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11   1.1  christos  *
     12   1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     13   1.1  christos  * information regarding copyright ownership.
     14   1.1  christos  */
     15   1.1  christos 
     16   1.1  christos /*! \file */
     17   1.1  christos 
     18   1.1  christos #include <errno.h>
     19   1.3  christos #include <inttypes.h>
     20   1.5  christos #include <limits.h>
     21   1.3  christos #include <stdbool.h>
     22   1.1  christos #include <stdlib.h>
     23   1.5  christos #include <sys/types.h> /* dev_t FreeBSD 2.1 */
     24   1.1  christos #include <time.h>
     25  1.10  christos #include <unistd.h>
     26   1.1  christos 
     27   1.5  christos #include <isc/atomic.h>
     28   1.1  christos #include <isc/dir.h>
     29  1.10  christos #include <isc/errno.h>
     30   1.1  christos #include <isc/file.h>
     31   1.1  christos #include <isc/log.h>
     32   1.1  christos #include <isc/magic.h>
     33   1.1  christos #include <isc/mem.h>
     34   1.1  christos #include <isc/print.h>
     35   1.5  christos #include <isc/rwlock.h>
     36   1.1  christos #include <isc/stat.h>
     37   1.1  christos #include <isc/stdio.h>
     38   1.1  christos #include <isc/string.h>
     39  1.10  christos #include <isc/thread.h>
     40   1.1  christos #include <isc/time.h>
     41   1.1  christos #include <isc/util.h>
     42   1.1  christos 
     43   1.5  christos #define LCTX_MAGIC	    ISC_MAGIC('L', 'c', 't', 'x')
     44   1.5  christos #define VALID_CONTEXT(lctx) ISC_MAGIC_VALID(lctx, LCTX_MAGIC)
     45   1.1  christos 
     46   1.5  christos #define LCFG_MAGIC	   ISC_MAGIC('L', 'c', 'f', 'g')
     47   1.5  christos #define VALID_CONFIG(lcfg) ISC_MAGIC_VALID(lcfg, LCFG_MAGIC)
     48   1.5  christos 
     49   1.5  christos #define RDLOCK(lp)   RWLOCK(lp, isc_rwlocktype_read);
     50   1.5  christos #define WRLOCK(lp)   RWLOCK(lp, isc_rwlocktype_write);
     51   1.5  christos #define RDUNLOCK(lp) RWUNLOCK(lp, isc_rwlocktype_read);
     52   1.5  christos #define WRUNLOCK(lp) RWUNLOCK(lp, isc_rwlocktype_write);
     53   1.1  christos 
     54  1.10  christos static thread_local bool forcelog = false;
     55  1.10  christos 
     56   1.1  christos /*
     57   1.1  christos  * XXXDCL make dynamic?
     58   1.1  christos  */
     59   1.5  christos #define LOG_BUFFER_SIZE (8 * 1024)
     60   1.1  christos 
     61   1.1  christos /*!
     62   1.1  christos  * This is the structure that holds each named channel.  A simple linked
     63   1.1  christos  * list chains all of the channels together, so an individual channel is
     64   1.1  christos  * found by doing strcmp()s with the names down the list.  Their should
     65   1.1  christos  * be no performance penalty from this as it is expected that the number
     66   1.1  christos  * of named channels will be no more than a dozen or so, and name lookups
     67   1.1  christos  * from the head of the list are only done when isc_log_usechannel() is
     68   1.1  christos  * called, which should also be very infrequent.
     69   1.1  christos  */
     70   1.1  christos typedef struct isc_logchannel isc_logchannel_t;
     71   1.1  christos 
     72   1.1  christos struct isc_logchannel {
     73   1.5  christos 	char *name;
     74   1.5  christos 	unsigned int type;
     75   1.5  christos 	int level;
     76   1.5  christos 	unsigned int flags;
     77   1.5  christos 	isc_logdestination_t destination;
     78   1.5  christos 	ISC_LINK(isc_logchannel_t) link;
     79   1.1  christos };
     80   1.1  christos 
     81   1.1  christos /*!
     82   1.1  christos  * The logchannellist structure associates categories and modules with
     83   1.1  christos  * channels.  First the appropriate channellist is found based on the
     84   1.1  christos  * category, and then each structure in the linked list is checked for
     85   1.1  christos  * a matching module.  It is expected that the number of channels
     86   1.1  christos  * associated with any given category will be very short, no more than
     87   1.1  christos  * three or four in the more unusual cases.
     88   1.1  christos  */
     89   1.1  christos typedef struct isc_logchannellist isc_logchannellist_t;
     90   1.1  christos 
     91   1.1  christos struct isc_logchannellist {
     92   1.5  christos 	const isc_logmodule_t *module;
     93   1.5  christos 	isc_logchannel_t *channel;
     94   1.5  christos 	ISC_LINK(isc_logchannellist_t) link;
     95   1.1  christos };
     96   1.1  christos 
     97   1.1  christos /*!
     98   1.1  christos  * This structure is used to remember messages for pruning via
     99   1.1  christos  * isc_log_[v]write1().
    100   1.1  christos  */
    101   1.1  christos typedef struct isc_logmessage isc_logmessage_t;
    102   1.1  christos 
    103   1.1  christos struct isc_logmessage {
    104   1.5  christos 	char *text;
    105   1.5  christos 	isc_time_t time;
    106   1.5  christos 	ISC_LINK(isc_logmessage_t) link;
    107   1.1  christos };
    108   1.1  christos 
    109   1.1  christos /*!
    110   1.1  christos  * The isc_logconfig structure is used to store the configurable information
    111   1.1  christos  * about where messages are actually supposed to be sent -- the information
    112   1.1  christos  * that could changed based on some configuration file, as opposed to the
    113   1.1  christos  * the category/module specification of isc_log_[v]write[1] that is compiled
    114   1.1  christos  * into a program, or the debug_level which is dynamic state information.
    115   1.1  christos  */
    116   1.1  christos struct isc_logconfig {
    117   1.5  christos 	unsigned int magic;
    118   1.5  christos 	isc_log_t *lctx;
    119   1.5  christos 	ISC_LIST(isc_logchannel_t) channels;
    120   1.5  christos 	ISC_LIST(isc_logchannellist_t) * channellists;
    121   1.5  christos 	unsigned int channellist_count;
    122   1.5  christos 	unsigned int duplicate_interval;
    123   1.5  christos 	int_fast32_t highest_level;
    124   1.5  christos 	char *tag;
    125   1.5  christos 	bool dynamic;
    126   1.1  christos };
    127   1.1  christos 
    128   1.1  christos /*!
    129   1.1  christos  * This isc_log structure provides the context for the isc_log functions.
    130   1.1  christos  * The log context locks itself in isc_log_doit, the internal backend to
    131   1.1  christos  * isc_log_write.  The locking is necessary both to provide exclusive access
    132   1.1  christos  * to the buffer into which the message is formatted and to guard against
    133   1.1  christos  * competing threads trying to write to the same syslog resource.  (On
    134   1.1  christos  * some systems, such as BSD/OS, stdio is thread safe but syslog is not.)
    135   1.1  christos  * Unfortunately, the lock cannot guard against a _different_ logging
    136   1.1  christos  * context in the same program competing for syslog's attention.  Thus
    137   1.1  christos  * There Can Be Only One, but this is not enforced.
    138   1.1  christos  * XXXDCL enforce it?
    139   1.1  christos  *
    140   1.1  christos  * Note that the category and module information is not locked.
    141   1.1  christos  * This is because in the usual case, only one isc_log_t is ever created
    142   1.1  christos  * in a program, and the category/module registration happens only once.
    143   1.1  christos  * XXXDCL it might be wise to add more locking overall.
    144   1.1  christos  */
    145   1.1  christos struct isc_log {
    146   1.1  christos 	/* Not locked. */
    147   1.5  christos 	unsigned int magic;
    148   1.5  christos 	isc_mem_t *mctx;
    149   1.5  christos 	isc_logcategory_t *categories;
    150   1.5  christos 	unsigned int category_count;
    151   1.5  christos 	isc_logmodule_t *modules;
    152   1.5  christos 	unsigned int module_count;
    153   1.5  christos 	atomic_int_fast32_t debug_level;
    154   1.5  christos 	isc_rwlock_t lcfg_rwl;
    155   1.5  christos 	/* Locked by isc_log lcfg_rwl */
    156   1.5  christos 	isc_logconfig_t *logconfig;
    157   1.5  christos 	isc_mutex_t lock;
    158   1.1  christos 	/* Locked by isc_log lock. */
    159   1.5  christos 	char buffer[LOG_BUFFER_SIZE];
    160   1.5  christos 	ISC_LIST(isc_logmessage_t) messages;
    161   1.5  christos 	atomic_bool dynamic;
    162   1.5  christos 	atomic_int_fast32_t highest_level;
    163   1.1  christos };
    164   1.1  christos 
    165   1.1  christos /*!
    166   1.1  christos  * Used when ISC_LOG_PRINTLEVEL is enabled for a channel.
    167   1.1  christos  */
    168   1.5  christos static const char *log_level_strings[] = { "debug",   "info",  "notice",
    169   1.5  christos 					   "warning", "error", "critical" };
    170   1.1  christos 
    171   1.1  christos /*!
    172   1.1  christos  * Used to convert ISC_LOG_* priorities into syslog priorities.
    173   1.1  christos  * XXXDCL This will need modification for NT.
    174   1.1  christos  */
    175   1.5  christos static const int syslog_map[] = { LOG_DEBUG,   LOG_INFO, LOG_NOTICE,
    176   1.5  christos 				  LOG_WARNING, LOG_ERR,	 LOG_CRIT };
    177   1.1  christos 
    178   1.1  christos /*!
    179   1.1  christos  * When adding new categories, a corresponding ISC_LOGCATEGORY_foo
    180   1.1  christos  * definition needs to be added to <isc/log.h>.
    181   1.1  christos  *
    182   1.1  christos  * The default category is provided so that the internal default can
    183   1.1  christos  * be overridden.  Since the default is always looked up as the first
    184   1.1  christos  * channellist in the log context, it must come first in isc_categories[].
    185   1.1  christos  */
    186  1.10  christos isc_logcategory_t isc_categories[] = { { "default", 0 }, /* "default
    187  1.10  christos 							    must come
    188  1.10  christos 							    first. */
    189  1.10  christos 				       { "general", 0 },
    190  1.10  christos 				       { "sslkeylog", 0 },
    191  1.10  christos 				       { NULL, 0 } };
    192   1.1  christos 
    193   1.1  christos /*!
    194  1.10  christos  * See above comment for categories, and apply it to modules.
    195   1.1  christos  */
    196  1.10  christos isc_logmodule_t isc_modules[] = { { "socket", 0 },    { "time", 0 },
    197  1.10  christos 				  { "interface", 0 }, { "timer", 0 },
    198  1.10  christos 				  { "file", 0 },      { "netmgr", 0 },
    199  1.10  christos 				  { "other", 0 },     { NULL, 0 } };
    200   1.1  christos 
    201   1.1  christos /*!
    202   1.1  christos  * This essentially constant structure must be filled in at run time,
    203   1.1  christos  * because its channel member is pointed to a channel that is created
    204   1.1  christos  * dynamically with isc_log_createchannel.
    205   1.1  christos  */
    206   1.1  christos static isc_logchannellist_t default_channel;
    207   1.1  christos 
    208   1.1  christos /*!
    209   1.1  christos  * libisc logs to this context.
    210   1.1  christos  */
    211  1.10  christos isc_log_t *isc_lctx = NULL;
    212   1.1  christos 
    213   1.1  christos /*!
    214   1.1  christos  * Forward declarations.
    215   1.1  christos  */
    216   1.5  christos static void
    217   1.1  christos assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
    218   1.1  christos 	      const isc_logmodule_t *module, isc_logchannel_t *channel);
    219   1.1  christos 
    220   1.5  christos static void
    221   1.1  christos sync_channellist(isc_logconfig_t *lcfg);
    222   1.1  christos 
    223   1.5  christos static void
    224   1.5  christos sync_highest_level(isc_log_t *lctx, isc_logconfig_t *lcfg);
    225   1.5  christos 
    226   1.1  christos static isc_result_t
    227   1.1  christos greatest_version(isc_logfile_t *file, int versions, int *greatest);
    228   1.1  christos 
    229   1.1  christos static void
    230   1.1  christos isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
    231   1.3  christos 	     isc_logmodule_t *module, int level, bool write_once,
    232   1.5  christos 	     const char *format, va_list args) ISC_FORMAT_PRINTF(6, 0);
    233   1.1  christos 
    234   1.1  christos /*@{*/
    235   1.1  christos /*!
    236   1.1  christos  * Convenience macros.
    237   1.1  christos  */
    238   1.1  christos 
    239   1.1  christos #define FACILITY(channel)	 (channel->destination.facility)
    240   1.1  christos #define FILE_NAME(channel)	 (channel->destination.file.name)
    241   1.1  christos #define FILE_STREAM(channel)	 (channel->destination.file.stream)
    242   1.1  christos #define FILE_VERSIONS(channel)	 (channel->destination.file.versions)
    243   1.1  christos #define FILE_SUFFIX(channel)	 (channel->destination.file.suffix)
    244   1.1  christos #define FILE_MAXSIZE(channel)	 (channel->destination.file.maximum_size)
    245   1.1  christos #define FILE_MAXREACHED(channel) (channel->destination.file.maximum_reached)
    246   1.1  christos 
    247   1.1  christos /*@}*/
    248   1.1  christos /****
    249   1.5  christos **** Public interfaces.
    250   1.5  christos ****/
    251   1.1  christos 
    252   1.1  christos /*
    253   1.1  christos  * Establish a new logging context, with default channels.
    254   1.1  christos  */
    255   1.5  christos void
    256   1.1  christos isc_log_create(isc_mem_t *mctx, isc_log_t **lctxp, isc_logconfig_t **lcfgp) {
    257   1.1  christos 	isc_log_t *lctx;
    258   1.1  christos 	isc_logconfig_t *lcfg = NULL;
    259   1.1  christos 
    260   1.1  christos 	REQUIRE(mctx != NULL);
    261   1.1  christos 	REQUIRE(lctxp != NULL && *lctxp == NULL);
    262   1.1  christos 	REQUIRE(lcfgp == NULL || *lcfgp == NULL);
    263   1.1  christos 
    264   1.1  christos 	lctx = isc_mem_get(mctx, sizeof(*lctx));
    265   1.5  christos 	lctx->mctx = NULL;
    266   1.5  christos 	isc_mem_attach(mctx, &lctx->mctx);
    267   1.5  christos 	lctx->categories = NULL;
    268   1.5  christos 	lctx->category_count = 0;
    269   1.5  christos 	lctx->modules = NULL;
    270   1.5  christos 	lctx->module_count = 0;
    271   1.5  christos 	atomic_init(&lctx->debug_level, 0);
    272   1.1  christos 
    273   1.5  christos 	ISC_LIST_INIT(lctx->messages);
    274   1.1  christos 
    275   1.5  christos 	isc_mutex_init(&lctx->lock);
    276   1.5  christos 	isc_rwlock_init(&lctx->lcfg_rwl, 0, 0);
    277   1.1  christos 
    278   1.5  christos 	/*
    279   1.5  christos 	 * Normally setting the magic number is the last step done
    280   1.5  christos 	 * in a creation function, but a valid log context is needed
    281   1.5  christos 	 * by isc_log_registercategories and isc_logconfig_create.
    282   1.5  christos 	 * If either fails, the lctx is destroyed and not returned
    283   1.5  christos 	 * to the caller.
    284   1.5  christos 	 */
    285   1.5  christos 	lctx->magic = LCTX_MAGIC;
    286   1.1  christos 
    287   1.5  christos 	isc_log_registercategories(lctx, isc_categories);
    288   1.5  christos 	isc_log_registermodules(lctx, isc_modules);
    289   1.5  christos 	isc_logconfig_create(lctx, &lcfg);
    290   1.1  christos 
    291   1.5  christos 	sync_channellist(lcfg);
    292   1.1  christos 
    293   1.5  christos 	lctx->logconfig = lcfg;
    294   1.1  christos 
    295   1.5  christos 	atomic_init(&lctx->highest_level, lcfg->highest_level);
    296   1.5  christos 	atomic_init(&lctx->dynamic, lcfg->dynamic);
    297   1.1  christos 
    298   1.5  christos 	*lctxp = lctx;
    299   1.5  christos 	if (lcfgp != NULL) {
    300   1.5  christos 		*lcfgp = lcfg;
    301   1.1  christos 	}
    302   1.1  christos }
    303   1.1  christos 
    304   1.5  christos void
    305   1.1  christos isc_logconfig_create(isc_log_t *lctx, isc_logconfig_t **lcfgp) {
    306   1.1  christos 	isc_logconfig_t *lcfg;
    307   1.1  christos 	isc_logdestination_t destination;
    308   1.1  christos 	int level = ISC_LOG_INFO;
    309   1.1  christos 
    310   1.1  christos 	REQUIRE(lcfgp != NULL && *lcfgp == NULL);
    311   1.1  christos 	REQUIRE(VALID_CONTEXT(lctx));
    312   1.1  christos 
    313   1.1  christos 	lcfg = isc_mem_get(lctx->mctx, sizeof(*lcfg));
    314   1.1  christos 
    315   1.5  christos 	lcfg->lctx = lctx;
    316   1.5  christos 	lcfg->channellists = NULL;
    317   1.5  christos 	lcfg->channellist_count = 0;
    318   1.5  christos 	lcfg->duplicate_interval = 0;
    319   1.5  christos 	lcfg->highest_level = level;
    320   1.5  christos 	lcfg->tag = NULL;
    321   1.5  christos 	lcfg->dynamic = false;
    322   1.5  christos 	ISC_LIST_INIT(lcfg->channels);
    323   1.5  christos 	lcfg->magic = LCFG_MAGIC;
    324   1.1  christos 
    325   1.1  christos 	/*
    326   1.1  christos 	 * Create the default channels:
    327   1.5  christos 	 *      default_syslog, default_stderr, default_debug and null.
    328   1.1  christos 	 */
    329   1.5  christos 	destination.facility = LOG_DAEMON;
    330   1.5  christos 	isc_log_createchannel(lcfg, "default_syslog", ISC_LOG_TOSYSLOG, level,
    331   1.5  christos 			      &destination, 0);
    332   1.5  christos 
    333   1.5  christos 	destination.file.stream = stderr;
    334   1.5  christos 	destination.file.name = NULL;
    335   1.5  christos 	destination.file.versions = ISC_LOG_ROLLNEVER;
    336   1.5  christos 	destination.file.suffix = isc_log_rollsuffix_increment;
    337   1.5  christos 	destination.file.maximum_size = 0;
    338   1.5  christos 	isc_log_createchannel(lcfg, "default_stderr", ISC_LOG_TOFILEDESC, level,
    339   1.5  christos 			      &destination, ISC_LOG_PRINTTIME);
    340   1.5  christos 
    341   1.5  christos 	/*
    342   1.5  christos 	 * Set the default category's channel to default_stderr,
    343   1.5  christos 	 * which is at the head of the channels list because it was
    344   1.5  christos 	 * just created.
    345   1.5  christos 	 */
    346   1.5  christos 	default_channel.channel = ISC_LIST_HEAD(lcfg->channels);
    347   1.5  christos 
    348   1.5  christos 	destination.file.stream = stderr;
    349   1.5  christos 	destination.file.name = NULL;
    350   1.5  christos 	destination.file.versions = ISC_LOG_ROLLNEVER;
    351   1.5  christos 	destination.file.suffix = isc_log_rollsuffix_increment;
    352   1.5  christos 	destination.file.maximum_size = 0;
    353   1.5  christos 	isc_log_createchannel(lcfg, "default_debug", ISC_LOG_TOFILEDESC,
    354   1.5  christos 			      ISC_LOG_DYNAMIC, &destination, ISC_LOG_PRINTTIME);
    355   1.1  christos 
    356   1.5  christos 	isc_log_createchannel(lcfg, "null", ISC_LOG_TONULL, ISC_LOG_DYNAMIC,
    357   1.5  christos 			      NULL, 0);
    358   1.1  christos 
    359   1.5  christos 	*lcfgp = lcfg;
    360   1.1  christos }
    361   1.1  christos 
    362   1.5  christos void
    363   1.1  christos isc_logconfig_use(isc_log_t *lctx, isc_logconfig_t *lcfg) {
    364   1.1  christos 	isc_logconfig_t *old_cfg;
    365   1.1  christos 
    366   1.1  christos 	REQUIRE(VALID_CONTEXT(lctx));
    367   1.1  christos 	REQUIRE(VALID_CONFIG(lcfg));
    368   1.1  christos 	REQUIRE(lcfg->lctx == lctx);
    369   1.1  christos 
    370   1.1  christos 	/*
    371   1.1  christos 	 * Ensure that lcfg->channellist_count == lctx->category_count.
    372   1.1  christos 	 * They won't be equal if isc_log_usechannel has not been called
    373   1.1  christos 	 * since any call to isc_log_registercategories.
    374   1.1  christos 	 */
    375   1.5  christos 	sync_channellist(lcfg);
    376   1.1  christos 
    377   1.5  christos 	WRLOCK(&lctx->lcfg_rwl);
    378   1.1  christos 	old_cfg = lctx->logconfig;
    379   1.1  christos 	lctx->logconfig = lcfg;
    380   1.5  christos 	sync_highest_level(lctx, lcfg);
    381   1.5  christos 	WRUNLOCK(&lctx->lcfg_rwl);
    382   1.1  christos 
    383   1.1  christos 	isc_logconfig_destroy(&old_cfg);
    384   1.1  christos }
    385   1.1  christos 
    386   1.1  christos void
    387   1.1  christos isc_log_destroy(isc_log_t **lctxp) {
    388   1.1  christos 	isc_log_t *lctx;
    389   1.1  christos 	isc_logconfig_t *lcfg;
    390   1.1  christos 	isc_mem_t *mctx;
    391   1.1  christos 	isc_logmessage_t *message;
    392   1.1  christos 
    393   1.1  christos 	REQUIRE(lctxp != NULL && VALID_CONTEXT(*lctxp));
    394   1.1  christos 
    395   1.1  christos 	lctx = *lctxp;
    396   1.5  christos 	*lctxp = NULL;
    397   1.1  christos 	mctx = lctx->mctx;
    398   1.1  christos 
    399   1.5  christos 	/* Stop the logging as a first thing */
    400   1.5  christos 	atomic_store_release(&lctx->debug_level, 0);
    401   1.5  christos 	atomic_store_release(&lctx->highest_level, 0);
    402   1.5  christos 	atomic_store_release(&lctx->dynamic, false);
    403   1.5  christos 
    404   1.5  christos 	WRLOCK(&lctx->lcfg_rwl);
    405   1.5  christos 	lcfg = lctx->logconfig;
    406   1.5  christos 	lctx->logconfig = NULL;
    407   1.5  christos 	WRUNLOCK(&lctx->lcfg_rwl);
    408   1.5  christos 
    409   1.5  christos 	if (lcfg != NULL) {
    410   1.1  christos 		isc_logconfig_destroy(&lcfg);
    411   1.1  christos 	}
    412   1.1  christos 
    413   1.5  christos 	isc_rwlock_destroy(&lctx->lcfg_rwl);
    414   1.3  christos 	isc_mutex_destroy(&lctx->lock);
    415   1.1  christos 
    416   1.1  christos 	while ((message = ISC_LIST_HEAD(lctx->messages)) != NULL) {
    417   1.1  christos 		ISC_LIST_UNLINK(lctx->messages, message, link);
    418   1.1  christos 
    419   1.1  christos 		isc_mem_put(mctx, message,
    420   1.1  christos 			    sizeof(*message) + strlen(message->text) + 1);
    421   1.1  christos 	}
    422   1.1  christos 
    423   1.1  christos 	lctx->buffer[0] = '\0';
    424   1.1  christos 	lctx->categories = NULL;
    425   1.1  christos 	lctx->category_count = 0;
    426   1.1  christos 	lctx->modules = NULL;
    427   1.1  christos 	lctx->module_count = 0;
    428   1.1  christos 	lctx->mctx = NULL;
    429   1.1  christos 	lctx->magic = 0;
    430   1.1  christos 
    431   1.1  christos 	isc_mem_putanddetach(&mctx, lctx, sizeof(*lctx));
    432   1.1  christos }
    433   1.1  christos 
    434   1.1  christos void
    435   1.1  christos isc_logconfig_destroy(isc_logconfig_t **lcfgp) {
    436   1.1  christos 	isc_logconfig_t *lcfg;
    437   1.1  christos 	isc_mem_t *mctx;
    438   1.1  christos 	isc_logchannel_t *channel;
    439   1.1  christos 	char *filename;
    440   1.1  christos 	unsigned int i;
    441   1.1  christos 
    442   1.1  christos 	REQUIRE(lcfgp != NULL && VALID_CONFIG(*lcfgp));
    443   1.1  christos 
    444   1.1  christos 	lcfg = *lcfgp;
    445   1.5  christos 	*lcfgp = NULL;
    446   1.1  christos 
    447   1.1  christos 	/*
    448   1.1  christos 	 * This function cannot be called with a logconfig that is in
    449   1.1  christos 	 * use by a log context.
    450   1.1  christos 	 */
    451   1.5  christos 	REQUIRE(lcfg->lctx != NULL);
    452   1.5  christos 
    453   1.5  christos 	RDLOCK(&lcfg->lctx->lcfg_rwl);
    454   1.5  christos 	REQUIRE(lcfg->lctx->logconfig != lcfg);
    455   1.5  christos 	RDUNLOCK(&lcfg->lctx->lcfg_rwl);
    456   1.1  christos 
    457   1.1  christos 	mctx = lcfg->lctx->mctx;
    458   1.1  christos 
    459   1.1  christos 	while ((channel = ISC_LIST_HEAD(lcfg->channels)) != NULL) {
    460   1.1  christos 		ISC_LIST_UNLINK(lcfg->channels, channel, link);
    461   1.1  christos 
    462   1.1  christos 		if (channel->type == ISC_LOG_TOFILE) {
    463   1.1  christos 			/*
    464   1.1  christos 			 * The filename for the channel may have ultimately
    465   1.1  christos 			 * started its life in user-land as a const string,
    466   1.1  christos 			 * but in isc_log_createchannel it gets copied
    467   1.1  christos 			 * into writable memory and is not longer truly const.
    468   1.1  christos 			 */
    469   1.1  christos 			DE_CONST(FILE_NAME(channel), filename);
    470   1.1  christos 			isc_mem_free(mctx, filename);
    471   1.1  christos 
    472   1.5  christos 			if (FILE_STREAM(channel) != NULL) {
    473   1.1  christos 				(void)fclose(FILE_STREAM(channel));
    474   1.5  christos 			}
    475   1.1  christos 		}
    476   1.1  christos 
    477   1.1  christos 		isc_mem_free(mctx, channel->name);
    478   1.1  christos 		isc_mem_put(mctx, channel, sizeof(*channel));
    479   1.1  christos 	}
    480   1.1  christos 
    481   1.5  christos 	for (i = 0; i < lcfg->channellist_count; i++) {
    482   1.5  christos 		isc_logchannellist_t *item;
    483   1.1  christos 		while ((item = ISC_LIST_HEAD(lcfg->channellists[i])) != NULL) {
    484   1.1  christos 			ISC_LIST_UNLINK(lcfg->channellists[i], item, link);
    485   1.1  christos 			isc_mem_put(mctx, item, sizeof(*item));
    486   1.1  christos 		}
    487   1.5  christos 	}
    488   1.1  christos 
    489   1.5  christos 	if (lcfg->channellist_count > 0) {
    490   1.1  christos 		isc_mem_put(mctx, lcfg->channellists,
    491   1.1  christos 			    lcfg->channellist_count *
    492   1.5  christos 				    sizeof(ISC_LIST(isc_logchannellist_t)));
    493   1.5  christos 	}
    494   1.1  christos 
    495   1.3  christos 	lcfg->dynamic = false;
    496   1.5  christos 	if (lcfg->tag != NULL) {
    497   1.1  christos 		isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
    498   1.5  christos 	}
    499   1.1  christos 	lcfg->tag = NULL;
    500   1.1  christos 	lcfg->highest_level = 0;
    501   1.1  christos 	lcfg->duplicate_interval = 0;
    502   1.1  christos 	lcfg->magic = 0;
    503   1.1  christos 
    504   1.1  christos 	isc_mem_put(mctx, lcfg, sizeof(*lcfg));
    505   1.1  christos }
    506   1.1  christos 
    507   1.1  christos void
    508   1.1  christos isc_log_registercategories(isc_log_t *lctx, isc_logcategory_t categories[]) {
    509   1.1  christos 	isc_logcategory_t *catp;
    510   1.1  christos 
    511   1.1  christos 	REQUIRE(VALID_CONTEXT(lctx));
    512   1.1  christos 	REQUIRE(categories != NULL && categories[0].name != NULL);
    513   1.1  christos 
    514   1.1  christos 	/*
    515   1.1  christos 	 * XXXDCL This somewhat sleazy situation of using the last pointer
    516   1.1  christos 	 * in one category array to point to the next array exists because
    517   1.1  christos 	 * this registration function returns void and I didn't want to have
    518   1.1  christos 	 * change everything that used it by making it return an isc_result_t.
    519   1.1  christos 	 * It would need to do that if it had to allocate memory to store
    520   1.1  christos 	 * pointers to each array passed in.
    521   1.1  christos 	 */
    522   1.5  christos 	if (lctx->categories == NULL) {
    523   1.1  christos 		lctx->categories = categories;
    524   1.5  christos 	} else {
    525   1.1  christos 		/*
    526   1.1  christos 		 * Adjust the last (NULL) pointer of the already registered
    527   1.1  christos 		 * categories to point to the incoming array.
    528   1.1  christos 		 */
    529   1.5  christos 		for (catp = lctx->categories; catp->name != NULL;) {
    530   1.5  christos 			if (catp->id == UINT_MAX) {
    531   1.1  christos 				/*
    532   1.1  christos 				 * The name pointer points to the next array.
    533   1.1  christos 				 * Ick.
    534   1.1  christos 				 */
    535   1.1  christos 				DE_CONST(catp->name, catp);
    536   1.5  christos 			} else {
    537   1.1  christos 				catp++;
    538   1.5  christos 			}
    539   1.5  christos 		}
    540   1.1  christos 
    541   1.1  christos 		catp->name = (void *)categories;
    542   1.1  christos 		catp->id = UINT_MAX;
    543   1.1  christos 	}
    544   1.1  christos 
    545   1.1  christos 	/*
    546   1.1  christos 	 * Update the id number of the category with its new global id.
    547   1.1  christos 	 */
    548   1.5  christos 	for (catp = categories; catp->name != NULL; catp++) {
    549   1.1  christos 		catp->id = lctx->category_count++;
    550   1.5  christos 	}
    551   1.1  christos }
    552   1.1  christos 
    553   1.1  christos isc_logcategory_t *
    554   1.1  christos isc_log_categorybyname(isc_log_t *lctx, const char *name) {
    555   1.1  christos 	isc_logcategory_t *catp;
    556   1.1  christos 
    557   1.1  christos 	REQUIRE(VALID_CONTEXT(lctx));
    558   1.1  christos 	REQUIRE(name != NULL);
    559   1.1  christos 
    560   1.5  christos 	for (catp = lctx->categories; catp->name != NULL;) {
    561   1.5  christos 		if (catp->id == UINT_MAX) {
    562   1.1  christos 			/*
    563   1.1  christos 			 * catp is neither modified nor returned to the
    564   1.1  christos 			 * caller, so removing its const qualifier is ok.
    565   1.1  christos 			 */
    566   1.1  christos 			DE_CONST(catp->name, catp);
    567   1.5  christos 		} else {
    568   1.5  christos 			if (strcmp(catp->name, name) == 0) {
    569   1.1  christos 				return (catp);
    570   1.5  christos 			}
    571   1.1  christos 			catp++;
    572   1.1  christos 		}
    573   1.5  christos 	}
    574   1.1  christos 
    575   1.1  christos 	return (NULL);
    576   1.1  christos }
    577   1.1  christos 
    578   1.1  christos void
    579   1.1  christos isc_log_registermodules(isc_log_t *lctx, isc_logmodule_t modules[]) {
    580   1.1  christos 	isc_logmodule_t *modp;
    581   1.1  christos 
    582   1.1  christos 	REQUIRE(VALID_CONTEXT(lctx));
    583   1.1  christos 	REQUIRE(modules != NULL && modules[0].name != NULL);
    584   1.1  christos 
    585   1.1  christos 	/*
    586   1.1  christos 	 * XXXDCL This somewhat sleazy situation of using the last pointer
    587   1.1  christos 	 * in one category array to point to the next array exists because
    588   1.1  christos 	 * this registration function returns void and I didn't want to have
    589   1.1  christos 	 * change everything that used it by making it return an isc_result_t.
    590   1.1  christos 	 * It would need to do that if it had to allocate memory to store
    591   1.1  christos 	 * pointers to each array passed in.
    592   1.1  christos 	 */
    593   1.5  christos 	if (lctx->modules == NULL) {
    594   1.1  christos 		lctx->modules = modules;
    595   1.5  christos 	} else {
    596   1.1  christos 		/*
    597   1.1  christos 		 * Adjust the last (NULL) pointer of the already registered
    598   1.1  christos 		 * modules to point to the incoming array.
    599   1.1  christos 		 */
    600   1.5  christos 		for (modp = lctx->modules; modp->name != NULL;) {
    601   1.5  christos 			if (modp->id == UINT_MAX) {
    602   1.1  christos 				/*
    603   1.1  christos 				 * The name pointer points to the next array.
    604   1.1  christos 				 * Ick.
    605   1.1  christos 				 */
    606   1.1  christos 				DE_CONST(modp->name, modp);
    607   1.5  christos 			} else {
    608   1.1  christos 				modp++;
    609   1.5  christos 			}
    610   1.5  christos 		}
    611   1.1  christos 
    612   1.1  christos 		modp->name = (void *)modules;
    613   1.1  christos 		modp->id = UINT_MAX;
    614   1.1  christos 	}
    615   1.1  christos 
    616   1.1  christos 	/*
    617   1.1  christos 	 * Update the id number of the module with its new global id.
    618   1.1  christos 	 */
    619   1.5  christos 	for (modp = modules; modp->name != NULL; modp++) {
    620   1.1  christos 		modp->id = lctx->module_count++;
    621   1.5  christos 	}
    622   1.1  christos }
    623   1.1  christos 
    624   1.1  christos isc_logmodule_t *
    625   1.1  christos isc_log_modulebyname(isc_log_t *lctx, const char *name) {
    626   1.1  christos 	isc_logmodule_t *modp;
    627   1.1  christos 
    628   1.1  christos 	REQUIRE(VALID_CONTEXT(lctx));
    629   1.1  christos 	REQUIRE(name != NULL);
    630   1.1  christos 
    631   1.5  christos 	for (modp = lctx->modules; modp->name != NULL;) {
    632   1.5  christos 		if (modp->id == UINT_MAX) {
    633   1.1  christos 			/*
    634   1.1  christos 			 * modp is neither modified nor returned to the
    635   1.1  christos 			 * caller, so removing its const qualifier is ok.
    636   1.1  christos 			 */
    637   1.1  christos 			DE_CONST(modp->name, modp);
    638   1.5  christos 		} else {
    639   1.5  christos 			if (strcmp(modp->name, name) == 0) {
    640   1.1  christos 				return (modp);
    641   1.5  christos 			}
    642   1.1  christos 			modp++;
    643   1.1  christos 		}
    644   1.5  christos 	}
    645   1.1  christos 
    646   1.1  christos 	return (NULL);
    647   1.1  christos }
    648   1.1  christos 
    649   1.5  christos void
    650   1.1  christos isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
    651   1.1  christos 		      unsigned int type, int level,
    652   1.1  christos 		      const isc_logdestination_t *destination,
    653   1.5  christos 		      unsigned int flags) {
    654   1.1  christos 	isc_logchannel_t *channel;
    655   1.1  christos 	isc_mem_t *mctx;
    656   1.1  christos 	unsigned int permitted = ISC_LOG_PRINTALL | ISC_LOG_DEBUGONLY |
    657   1.1  christos 				 ISC_LOG_BUFFERED | ISC_LOG_ISO8601 |
    658   1.1  christos 				 ISC_LOG_UTC;
    659   1.1  christos 
    660   1.1  christos 	REQUIRE(VALID_CONFIG(lcfg));
    661   1.1  christos 	REQUIRE(name != NULL);
    662   1.5  christos 	REQUIRE(type == ISC_LOG_TOSYSLOG || type == ISC_LOG_TOFILE ||
    663   1.1  christos 		type == ISC_LOG_TOFILEDESC || type == ISC_LOG_TONULL);
    664   1.1  christos 	REQUIRE(destination != NULL || type == ISC_LOG_TONULL);
    665   1.1  christos 	REQUIRE(level >= ISC_LOG_CRITICAL);
    666   1.1  christos 	REQUIRE((flags & ~permitted) == 0);
    667   1.1  christos 
    668   1.1  christos 	/* XXXDCL find duplicate names? */
    669   1.1  christos 
    670   1.1  christos 	mctx = lcfg->lctx->mctx;
    671   1.1  christos 
    672   1.1  christos 	channel = isc_mem_get(mctx, sizeof(*channel));
    673   1.1  christos 
    674   1.1  christos 	channel->name = isc_mem_strdup(mctx, name);
    675   1.1  christos 
    676   1.1  christos 	channel->type = type;
    677   1.1  christos 	channel->level = level;
    678   1.1  christos 	channel->flags = flags;
    679   1.1  christos 	ISC_LINK_INIT(channel, link);
    680   1.1  christos 
    681   1.1  christos 	switch (type) {
    682   1.1  christos 	case ISC_LOG_TOSYSLOG:
    683   1.1  christos 		FACILITY(channel) = destination->facility;
    684   1.1  christos 		break;
    685   1.1  christos 
    686   1.1  christos 	case ISC_LOG_TOFILE:
    687   1.1  christos 		/*
    688   1.1  christos 		 * The file name is copied because greatest_version wants
    689   1.1  christos 		 * to scribble on it, so it needs to be definitely in
    690   1.1  christos 		 * writable memory.
    691   1.1  christos 		 */
    692   1.5  christos 		FILE_NAME(channel) = isc_mem_strdup(mctx,
    693   1.5  christos 						    destination->file.name);
    694   1.1  christos 		FILE_STREAM(channel) = NULL;
    695   1.1  christos 		FILE_VERSIONS(channel) = destination->file.versions;
    696   1.1  christos 		FILE_SUFFIX(channel) = destination->file.suffix;
    697   1.1  christos 		FILE_MAXSIZE(channel) = destination->file.maximum_size;
    698   1.3  christos 		FILE_MAXREACHED(channel) = false;
    699   1.1  christos 		break;
    700   1.1  christos 
    701   1.1  christos 	case ISC_LOG_TOFILEDESC:
    702   1.1  christos 		FILE_NAME(channel) = NULL;
    703   1.1  christos 		FILE_STREAM(channel) = destination->file.stream;
    704   1.1  christos 		FILE_MAXSIZE(channel) = 0;
    705   1.1  christos 		FILE_VERSIONS(channel) = ISC_LOG_ROLLNEVER;
    706   1.1  christos 		FILE_SUFFIX(channel) = isc_log_rollsuffix_increment;
    707   1.1  christos 		break;
    708   1.1  christos 
    709   1.1  christos 	case ISC_LOG_TONULL:
    710   1.1  christos 		/* Nothing. */
    711   1.1  christos 		break;
    712   1.1  christos 
    713   1.1  christos 	default:
    714   1.7  christos 		UNREACHABLE();
    715   1.1  christos 	}
    716   1.1  christos 
    717   1.1  christos 	ISC_LIST_PREPEND(lcfg->channels, channel, link);
    718   1.1  christos 
    719   1.1  christos 	/*
    720   1.1  christos 	 * If default_stderr was redefined, make the default category
    721   1.1  christos 	 * point to the new default_stderr.
    722   1.1  christos 	 */
    723   1.5  christos 	if (strcmp(name, "default_stderr") == 0) {
    724   1.1  christos 		default_channel.channel = channel;
    725   1.5  christos 	}
    726   1.1  christos }
    727   1.1  christos 
    728   1.1  christos isc_result_t
    729   1.1  christos isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
    730   1.1  christos 		   const isc_logcategory_t *category,
    731   1.5  christos 		   const isc_logmodule_t *module) {
    732   1.1  christos 	isc_log_t *lctx;
    733   1.1  christos 	isc_logchannel_t *channel;
    734   1.1  christos 
    735   1.1  christos 	REQUIRE(VALID_CONFIG(lcfg));
    736   1.1  christos 	REQUIRE(name != NULL);
    737   1.1  christos 
    738   1.1  christos 	lctx = lcfg->lctx;
    739   1.1  christos 
    740   1.1  christos 	REQUIRE(category == NULL || category->id < lctx->category_count);
    741   1.1  christos 	REQUIRE(module == NULL || module->id < lctx->module_count);
    742   1.1  christos 
    743   1.1  christos 	for (channel = ISC_LIST_HEAD(lcfg->channels); channel != NULL;
    744   1.1  christos 	     channel = ISC_LIST_NEXT(channel, link))
    745   1.5  christos 	{
    746   1.5  christos 		if (strcmp(name, channel->name) == 0) {
    747   1.1  christos 			break;
    748   1.5  christos 		}
    749   1.5  christos 	}
    750   1.1  christos 
    751   1.5  christos 	if (channel == NULL) {
    752   1.1  christos 		return (ISC_R_NOTFOUND);
    753   1.5  christos 	}
    754   1.1  christos 
    755   1.5  christos 	if (category != NULL) {
    756   1.5  christos 		assignchannel(lcfg, category->id, module, channel);
    757   1.5  christos 	} else {
    758   1.1  christos 		/*
    759   1.1  christos 		 * Assign to all categories.  Note that this includes
    760   1.1  christos 		 * the default channel.
    761   1.1  christos 		 */
    762   1.5  christos 		for (size_t i = 0; i < lctx->category_count; i++) {
    763   1.5  christos 			assignchannel(lcfg, i, module, channel);
    764   1.1  christos 		}
    765   1.5  christos 	}
    766   1.1  christos 
    767   1.5  christos 	/*
    768   1.5  christos 	 * Update the highest logging level, if the current lcfg is in use.
    769   1.5  christos 	 */
    770   1.5  christos 	if (lcfg->lctx->logconfig == lcfg) {
    771   1.5  christos 		sync_highest_level(lctx, lcfg);
    772   1.5  christos 	}
    773   1.5  christos 
    774   1.5  christos 	return (ISC_R_SUCCESS);
    775   1.1  christos }
    776   1.1  christos 
    777   1.1  christos void
    778   1.1  christos isc_log_write(isc_log_t *lctx, isc_logcategory_t *category,
    779   1.5  christos 	      isc_logmodule_t *module, int level, const char *format, ...) {
    780   1.1  christos 	va_list args;
    781   1.1  christos 
    782   1.1  christos 	/*
    783   1.1  christos 	 * Contract checking is done in isc_log_doit().
    784   1.1  christos 	 */
    785   1.1  christos 
    786   1.1  christos 	va_start(args, format);
    787   1.5  christos 	isc_log_doit(lctx, category, module, level, false, format, args);
    788   1.1  christos 	va_end(args);
    789   1.1  christos }
    790   1.1  christos 
    791   1.1  christos void
    792   1.1  christos isc_log_vwrite(isc_log_t *lctx, isc_logcategory_t *category,
    793   1.5  christos 	       isc_logmodule_t *module, int level, const char *format,
    794   1.5  christos 	       va_list args) {
    795   1.1  christos 	/*
    796   1.1  christos 	 * Contract checking is done in isc_log_doit().
    797   1.1  christos 	 */
    798   1.5  christos 	isc_log_doit(lctx, category, module, level, false, format, args);
    799   1.1  christos }
    800   1.1  christos 
    801   1.1  christos void
    802   1.1  christos isc_log_write1(isc_log_t *lctx, isc_logcategory_t *category,
    803   1.5  christos 	       isc_logmodule_t *module, int level, const char *format, ...) {
    804   1.1  christos 	va_list args;
    805   1.1  christos 
    806   1.1  christos 	/*
    807   1.1  christos 	 * Contract checking is done in isc_log_doit().
    808   1.1  christos 	 */
    809   1.1  christos 
    810   1.1  christos 	va_start(args, format);
    811   1.5  christos 	isc_log_doit(lctx, category, module, level, true, format, args);
    812   1.1  christos 	va_end(args);
    813   1.1  christos }
    814   1.1  christos 
    815   1.1  christos void
    816   1.1  christos isc_log_vwrite1(isc_log_t *lctx, isc_logcategory_t *category,
    817   1.5  christos 		isc_logmodule_t *module, int level, const char *format,
    818   1.5  christos 		va_list args) {
    819   1.1  christos 	/*
    820   1.1  christos 	 * Contract checking is done in isc_log_doit().
    821   1.1  christos 	 */
    822   1.5  christos 	isc_log_doit(lctx, category, module, level, true, format, args);
    823   1.1  christos }
    824   1.1  christos 
    825   1.1  christos void
    826   1.1  christos isc_log_setcontext(isc_log_t *lctx) {
    827   1.1  christos 	isc_lctx = lctx;
    828   1.1  christos }
    829   1.1  christos 
    830   1.1  christos void
    831   1.1  christos isc_log_setdebuglevel(isc_log_t *lctx, unsigned int level) {
    832   1.1  christos 	REQUIRE(VALID_CONTEXT(lctx));
    833   1.1  christos 
    834   1.5  christos 	atomic_store_release(&lctx->debug_level, level);
    835   1.1  christos 	/*
    836   1.1  christos 	 * Close ISC_LOG_DEBUGONLY channels if level is zero.
    837   1.1  christos 	 */
    838   1.5  christos 	if (level == 0) {
    839   1.5  christos 		RDLOCK(&lctx->lcfg_rwl);
    840   1.5  christos 		isc_logconfig_t *lcfg = lctx->logconfig;
    841   1.5  christos 		if (lcfg != NULL) {
    842   1.5  christos 			LOCK(&lctx->lock);
    843   1.5  christos 			for (isc_logchannel_t *channel =
    844   1.5  christos 				     ISC_LIST_HEAD(lcfg->channels);
    845   1.5  christos 			     channel != NULL;
    846   1.5  christos 			     channel = ISC_LIST_NEXT(channel, link))
    847   1.5  christos 			{
    848   1.5  christos 				if (channel->type == ISC_LOG_TOFILE &&
    849   1.5  christos 				    (channel->flags & ISC_LOG_DEBUGONLY) != 0 &&
    850   1.5  christos 				    FILE_STREAM(channel) != NULL)
    851   1.5  christos 				{
    852   1.5  christos 					(void)fclose(FILE_STREAM(channel));
    853   1.5  christos 					FILE_STREAM(channel) = NULL;
    854   1.5  christos 				}
    855   1.1  christos 			}
    856   1.5  christos 			UNLOCK(&lctx->lock);
    857   1.5  christos 		}
    858   1.5  christos 		RDUNLOCK(&lctx->lcfg_rwl);
    859   1.5  christos 	}
    860   1.1  christos }
    861   1.1  christos 
    862   1.1  christos unsigned int
    863   1.1  christos isc_log_getdebuglevel(isc_log_t *lctx) {
    864   1.1  christos 	REQUIRE(VALID_CONTEXT(lctx));
    865   1.1  christos 
    866   1.5  christos 	return (atomic_load_acquire(&lctx->debug_level));
    867   1.1  christos }
    868   1.1  christos 
    869   1.1  christos void
    870   1.1  christos isc_log_setduplicateinterval(isc_logconfig_t *lcfg, unsigned int interval) {
    871   1.1  christos 	REQUIRE(VALID_CONFIG(lcfg));
    872   1.1  christos 
    873   1.1  christos 	lcfg->duplicate_interval = interval;
    874   1.1  christos }
    875   1.1  christos 
    876   1.1  christos unsigned int
    877   1.1  christos isc_log_getduplicateinterval(isc_logconfig_t *lcfg) {
    878   1.1  christos 	REQUIRE(VALID_CONTEXT(lcfg));
    879   1.1  christos 
    880   1.1  christos 	return (lcfg->duplicate_interval);
    881   1.1  christos }
    882   1.1  christos 
    883   1.5  christos void
    884   1.1  christos isc_log_settag(isc_logconfig_t *lcfg, const char *tag) {
    885   1.1  christos 	REQUIRE(VALID_CONFIG(lcfg));
    886   1.1  christos 
    887   1.1  christos 	if (tag != NULL && *tag != '\0') {
    888   1.5  christos 		if (lcfg->tag != NULL) {
    889   1.1  christos 			isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
    890   1.5  christos 		}
    891   1.1  christos 		lcfg->tag = isc_mem_strdup(lcfg->lctx->mctx, tag);
    892   1.1  christos 	} else {
    893   1.5  christos 		if (lcfg->tag != NULL) {
    894   1.1  christos 			isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
    895   1.5  christos 		}
    896   1.1  christos 		lcfg->tag = NULL;
    897   1.1  christos 	}
    898   1.1  christos }
    899   1.1  christos 
    900   1.1  christos char *
    901   1.1  christos isc_log_gettag(isc_logconfig_t *lcfg) {
    902   1.1  christos 	REQUIRE(VALID_CONFIG(lcfg));
    903   1.1  christos 
    904   1.1  christos 	return (lcfg->tag);
    905   1.1  christos }
    906   1.1  christos 
    907   1.1  christos /* XXXDCL NT  -- This interface will assuredly be changing. */
    908   1.1  christos void
    909   1.1  christos isc_log_opensyslog(const char *tag, int options, int facility) {
    910   1.1  christos 	(void)openlog(tag, options, facility);
    911   1.1  christos }
    912   1.1  christos 
    913   1.1  christos void
    914   1.1  christos isc_log_closefilelogs(isc_log_t *lctx) {
    915   1.1  christos 	REQUIRE(VALID_CONTEXT(lctx));
    916   1.1  christos 
    917   1.5  christos 	RDLOCK(&lctx->lcfg_rwl);
    918   1.5  christos 	isc_logconfig_t *lcfg = lctx->logconfig;
    919   1.5  christos 	if (lcfg != NULL) {
    920   1.5  christos 		LOCK(&lctx->lock);
    921   1.5  christos 		for (isc_logchannel_t *channel = ISC_LIST_HEAD(lcfg->channels);
    922   1.5  christos 		     channel != NULL; channel = ISC_LIST_NEXT(channel, link))
    923   1.5  christos 		{
    924   1.5  christos 			if (channel->type == ISC_LOG_TOFILE &&
    925   1.8  christos 			    FILE_STREAM(channel) != NULL)
    926   1.8  christos 			{
    927   1.5  christos 				(void)fclose(FILE_STREAM(channel));
    928   1.5  christos 				FILE_STREAM(channel) = NULL;
    929   1.5  christos 			}
    930   1.1  christos 		}
    931   1.5  christos 		UNLOCK(&lctx->lock);
    932   1.5  christos 	}
    933   1.5  christos 	RDUNLOCK(&lctx->lcfg_rwl);
    934   1.1  christos }
    935   1.1  christos 
    936   1.1  christos /****
    937   1.5  christos **** Internal functions
    938   1.5  christos ****/
    939   1.1  christos 
    940   1.5  christos static void
    941   1.1  christos assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
    942   1.5  christos 	      const isc_logmodule_t *module, isc_logchannel_t *channel) {
    943   1.1  christos 	isc_logchannellist_t *new_item;
    944   1.1  christos 	isc_log_t *lctx;
    945   1.1  christos 
    946   1.1  christos 	REQUIRE(VALID_CONFIG(lcfg));
    947   1.1  christos 
    948   1.1  christos 	lctx = lcfg->lctx;
    949   1.1  christos 
    950   1.1  christos 	REQUIRE(category_id < lctx->category_count);
    951   1.1  christos 	REQUIRE(module == NULL || module->id < lctx->module_count);
    952   1.1  christos 	REQUIRE(channel != NULL);
    953   1.1  christos 
    954   1.1  christos 	/*
    955   1.1  christos 	 * Ensure lcfg->channellist_count == lctx->category_count.
    956   1.1  christos 	 */
    957   1.5  christos 	sync_channellist(lcfg);
    958   1.1  christos 
    959   1.1  christos 	new_item = isc_mem_get(lctx->mctx, sizeof(*new_item));
    960   1.1  christos 
    961   1.1  christos 	new_item->channel = channel;
    962   1.1  christos 	new_item->module = module;
    963   1.5  christos 	ISC_LIST_INITANDPREPEND(lcfg->channellists[category_id], new_item,
    964   1.5  christos 				link);
    965   1.1  christos 
    966   1.1  christos 	/*
    967   1.1  christos 	 * Remember the highest logging level set by any channel in the
    968   1.1  christos 	 * logging config, so isc_log_doit() can quickly return if the
    969   1.1  christos 	 * message is too high to be logged by any channel.
    970   1.1  christos 	 */
    971   1.1  christos 	if (channel->type != ISC_LOG_TONULL) {
    972   1.5  christos 		if (lcfg->highest_level < channel->level) {
    973   1.1  christos 			lcfg->highest_level = channel->level;
    974   1.5  christos 		}
    975   1.5  christos 		if (channel->level == ISC_LOG_DYNAMIC) {
    976   1.3  christos 			lcfg->dynamic = true;
    977   1.5  christos 		}
    978   1.1  christos 	}
    979   1.1  christos }
    980   1.1  christos 
    981   1.1  christos /*
    982   1.1  christos  * This would ideally be part of isc_log_registercategories(), except then
    983   1.1  christos  * that function would have to return isc_result_t instead of void.
    984   1.1  christos  */
    985   1.5  christos static void
    986   1.1  christos sync_channellist(isc_logconfig_t *lcfg) {
    987   1.1  christos 	unsigned int bytes;
    988   1.1  christos 	isc_log_t *lctx;
    989   1.1  christos 	void *lists;
    990   1.1  christos 
    991   1.1  christos 	REQUIRE(VALID_CONFIG(lcfg));
    992   1.1  christos 
    993   1.1  christos 	lctx = lcfg->lctx;
    994   1.1  christos 
    995   1.1  christos 	REQUIRE(lctx->category_count != 0);
    996   1.1  christos 
    997   1.5  christos 	if (lctx->category_count == lcfg->channellist_count) {
    998   1.5  christos 		return;
    999   1.5  christos 	}
   1000   1.1  christos 
   1001   1.1  christos 	bytes = lctx->category_count * sizeof(ISC_LIST(isc_logchannellist_t));
   1002   1.1  christos 
   1003   1.1  christos 	lists = isc_mem_get(lctx->mctx, bytes);
   1004   1.1  christos 
   1005   1.1  christos 	memset(lists, 0, bytes);
   1006   1.1  christos 
   1007   1.1  christos 	if (lcfg->channellist_count != 0) {
   1008   1.1  christos 		bytes = lcfg->channellist_count *
   1009   1.1  christos 			sizeof(ISC_LIST(isc_logchannellist_t));
   1010   1.1  christos 		memmove(lists, lcfg->channellists, bytes);
   1011   1.1  christos 		isc_mem_put(lctx->mctx, lcfg->channellists, bytes);
   1012   1.1  christos 	}
   1013   1.1  christos 
   1014   1.1  christos 	lcfg->channellists = lists;
   1015   1.1  christos 	lcfg->channellist_count = lctx->category_count;
   1016   1.5  christos }
   1017   1.1  christos 
   1018   1.5  christos static void
   1019   1.5  christos sync_highest_level(isc_log_t *lctx, isc_logconfig_t *lcfg) {
   1020   1.5  christos 	atomic_store(&lctx->highest_level, lcfg->highest_level);
   1021   1.5  christos 	atomic_store(&lctx->dynamic, lcfg->dynamic);
   1022   1.1  christos }
   1023   1.1  christos 
   1024   1.1  christos static isc_result_t
   1025   1.1  christos greatest_version(isc_logfile_t *file, int versions, int *greatestp) {
   1026  1.10  christos 	char *digit_end;
   1027  1.10  christos 	char dirbuf[PATH_MAX + 1];
   1028  1.10  christos 	const char *bname;
   1029  1.10  christos 	const char *dirname = ".";
   1030   1.1  christos 	int version, greatest = -1;
   1031   1.1  christos 	isc_dir_t dir;
   1032   1.1  christos 	isc_result_t result;
   1033  1.10  christos 	size_t bnamelen;
   1034   1.1  christos 
   1035  1.10  christos 	bname = strrchr(file->name, '/');
   1036   1.1  christos 	if (bname != NULL) {
   1037  1.10  christos 		/*
   1038  1.10  christos 		 * Copy the complete file name to dirbuf.
   1039  1.10  christos 		 */
   1040  1.10  christos 		size_t len = strlcpy(dirbuf, file->name, sizeof(dirbuf));
   1041  1.10  christos 		if (len >= sizeof(dirbuf)) {
   1042  1.10  christos 			result = ISC_R_NOSPACE;
   1043  1.10  christos 			syslog(LOG_ERR, "unable to remove log files: %s",
   1044  1.10  christos 			       isc_result_totext(result));
   1045  1.10  christos 			return (result);
   1046  1.10  christos 		}
   1047  1.10  christos 
   1048  1.10  christos 		/*
   1049  1.10  christos 		 * Truncate after trailing '/' so the code works for
   1050  1.10  christos 		 * files in the root directory.
   1051  1.10  christos 		 */
   1052  1.10  christos 		bname++;
   1053  1.10  christos 		dirbuf[bname - file->name] = '\0';
   1054  1.10  christos 		dirname = dirbuf;
   1055   1.1  christos 	} else {
   1056  1.10  christos 		bname = file->name;
   1057   1.1  christos 	}
   1058   1.1  christos 	bnamelen = strlen(bname);
   1059   1.1  christos 
   1060   1.1  christos 	isc_dir_init(&dir);
   1061   1.1  christos 	result = isc_dir_open(&dir, dirname);
   1062   1.1  christos 
   1063   1.1  christos 	/*
   1064   1.1  christos 	 * Return if the directory open failed.
   1065   1.1  christos 	 */
   1066   1.5  christos 	if (result != ISC_R_SUCCESS) {
   1067   1.1  christos 		return (result);
   1068   1.5  christos 	}
   1069   1.1  christos 
   1070   1.1  christos 	while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
   1071   1.1  christos 		if (dir.entry.length > bnamelen &&
   1072   1.1  christos 		    strncmp(dir.entry.name, bname, bnamelen) == 0 &&
   1073   1.1  christos 		    dir.entry.name[bnamelen] == '.')
   1074   1.1  christos 		{
   1075   1.1  christos 			version = strtol(&dir.entry.name[bnamelen + 1],
   1076   1.1  christos 					 &digit_end, 10);
   1077   1.1  christos 			/*
   1078   1.1  christos 			 * Remove any backup files that exceed versions.
   1079   1.1  christos 			 */
   1080   1.1  christos 			if (*digit_end == '\0' && version >= versions) {
   1081  1.10  christos 				int n = unlinkat(dirfd(dir.handle),
   1082  1.10  christos 						 dir.entry.name, 0);
   1083  1.10  christos 				if (n < 0) {
   1084  1.10  christos 					result = isc_errno_toresult(errno);
   1085  1.10  christos 					if (result != ISC_R_SUCCESS &&
   1086  1.10  christos 					    result != ISC_R_FILENOTFOUND)
   1087  1.10  christos 					{
   1088  1.10  christos 						syslog(LOG_ERR,
   1089  1.10  christos 						       "unable to remove log "
   1090  1.10  christos 						       "file '%s%s': %s",
   1091  1.10  christos 						       bname == file->name
   1092  1.10  christos 							       ? ""
   1093  1.10  christos 							       : dirname,
   1094  1.10  christos 						       dir.entry.name,
   1095  1.10  christos 						       isc_result_totext(
   1096  1.10  christos 							       result));
   1097  1.10  christos 					}
   1098   1.5  christos 				}
   1099   1.5  christos 			} else if (*digit_end == '\0' && version > greatest) {
   1100   1.1  christos 				greatest = version;
   1101   1.5  christos 			}
   1102   1.1  christos 		}
   1103   1.1  christos 	}
   1104   1.1  christos 	isc_dir_close(&dir);
   1105   1.1  christos 
   1106   1.1  christos 	*greatestp = greatest;
   1107   1.1  christos 	return (ISC_R_SUCCESS);
   1108   1.1  christos }
   1109   1.1  christos 
   1110   1.6  christos static void
   1111   1.9  christos insert_sort(int64_t to_keep[], int64_t versions, int64_t version) {
   1112   1.6  christos 	int i = 0;
   1113   1.6  christos 	while (i < versions && version < to_keep[i]) {
   1114   1.6  christos 		i++;
   1115   1.6  christos 	}
   1116   1.6  christos 	if (i == versions) {
   1117   1.6  christos 		return;
   1118   1.6  christos 	}
   1119   1.6  christos 	if (i < versions - 1) {
   1120   1.6  christos 		memmove(&to_keep[i + 1], &to_keep[i],
   1121   1.6  christos 			sizeof(to_keep[0]) * (versions - i - 1));
   1122   1.6  christos 	}
   1123   1.6  christos 	to_keep[i] = version;
   1124   1.6  christos }
   1125   1.6  christos 
   1126   1.6  christos static int64_t
   1127  1.10  christos last_to_keep(int64_t versions, isc_dir_t *dirp, const char *bname,
   1128  1.10  christos 	     size_t bnamelen) {
   1129   1.9  christos 	int64_t to_keep[ISC_LOG_MAX_VERSIONS] = { 0 };
   1130   1.9  christos 	int64_t version = 0;
   1131   1.9  christos 
   1132   1.6  christos 	if (versions <= 0) {
   1133   1.9  christos 		return (INT64_MAX);
   1134   1.6  christos 	}
   1135   1.6  christos 
   1136   1.6  christos 	if (versions > ISC_LOG_MAX_VERSIONS) {
   1137   1.6  christos 		versions = ISC_LOG_MAX_VERSIONS;
   1138   1.6  christos 	}
   1139   1.6  christos 	/*
   1140   1.6  christos 	 * First we fill 'to_keep' structure using insertion sort
   1141   1.6  christos 	 */
   1142   1.6  christos 	memset(to_keep, 0, sizeof(to_keep));
   1143   1.6  christos 	while (isc_dir_read(dirp) == ISC_R_SUCCESS) {
   1144   1.9  christos 		char *digit_end = NULL;
   1145   1.9  christos 		char *ename = NULL;
   1146   1.9  christos 
   1147   1.6  christos 		if (dirp->entry.length <= bnamelen ||
   1148   1.6  christos 		    strncmp(dirp->entry.name, bname, bnamelen) != 0 ||
   1149   1.6  christos 		    dirp->entry.name[bnamelen] != '.')
   1150   1.6  christos 		{
   1151   1.6  christos 			continue;
   1152   1.6  christos 		}
   1153   1.6  christos 
   1154   1.9  christos 		ename = &dirp->entry.name[bnamelen + 1];
   1155   1.6  christos 		version = strtoull(ename, &digit_end, 10);
   1156   1.6  christos 		if (*digit_end == '\0') {
   1157   1.6  christos 			insert_sort(to_keep, versions, version);
   1158   1.6  christos 		}
   1159   1.6  christos 	}
   1160   1.6  christos 
   1161   1.6  christos 	isc_dir_reset(dirp);
   1162   1.6  christos 
   1163   1.6  christos 	/*
   1164   1.6  christos 	 * to_keep[versions - 1] is the last one we want to keep
   1165   1.6  christos 	 */
   1166   1.6  christos 	return (to_keep[versions - 1]);
   1167   1.6  christos }
   1168   1.6  christos 
   1169   1.1  christos static isc_result_t
   1170   1.1  christos remove_old_tsversions(isc_logfile_t *file, int versions) {
   1171  1.10  christos 	char *digit_end;
   1172  1.10  christos 	char dirbuf[PATH_MAX + 1];
   1173  1.10  christos 	const char *bname;
   1174  1.10  christos 	const char *dirname = ".";
   1175  1.10  christos 	int64_t version, last = INT64_MAX;
   1176  1.10  christos 	isc_dir_t dir;
   1177   1.1  christos 	isc_result_t result;
   1178   1.1  christos 	size_t bnamelen;
   1179  1.10  christos 
   1180  1.10  christos 	bname = strrchr(file->name, '/');
   1181   1.1  christos 	if (bname != NULL) {
   1182  1.10  christos 		/*
   1183  1.10  christos 		 * Copy the complete file name to dirbuf.
   1184  1.10  christos 		 */
   1185  1.10  christos 		size_t len = strlcpy(dirbuf, file->name, sizeof(dirbuf));
   1186  1.10  christos 		if (len >= sizeof(dirbuf)) {
   1187  1.10  christos 			result = ISC_R_NOSPACE;
   1188  1.10  christos 			syslog(LOG_ERR, "unable to remove log files: %s",
   1189  1.10  christos 			       isc_result_totext(result));
   1190  1.10  christos 			return (result);
   1191  1.10  christos 		}
   1192  1.10  christos 
   1193  1.10  christos 		/*
   1194  1.10  christos 		 * Truncate after trailing '/' so the code works for
   1195  1.10  christos 		 * files in the root directory.
   1196  1.10  christos 		 */
   1197  1.10  christos 		bname++;
   1198  1.10  christos 		dirbuf[bname - file->name] = '\0';
   1199  1.10  christos 		dirname = dirbuf;
   1200   1.1  christos 	} else {
   1201  1.10  christos 		bname = file->name;
   1202   1.1  christos 	}
   1203   1.1  christos 	bnamelen = strlen(bname);
   1204   1.1  christos 
   1205   1.1  christos 	isc_dir_init(&dir);
   1206   1.1  christos 	result = isc_dir_open(&dir, dirname);
   1207   1.1  christos 
   1208   1.1  christos 	/*
   1209   1.1  christos 	 * Return if the directory open failed.
   1210   1.1  christos 	 */
   1211   1.5  christos 	if (result != ISC_R_SUCCESS) {
   1212   1.1  christos 		return (result);
   1213   1.5  christos 	}
   1214   1.1  christos 
   1215   1.6  christos 	last = last_to_keep(versions, &dir, bname, bnamelen);
   1216   1.1  christos 
   1217   1.1  christos 	while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
   1218   1.1  christos 		if (dir.entry.length > bnamelen &&
   1219   1.1  christos 		    strncmp(dir.entry.name, bname, bnamelen) == 0 &&
   1220   1.1  christos 		    dir.entry.name[bnamelen] == '.')
   1221   1.1  christos 		{
   1222  1.10  christos 			version = strtoull(&dir.entry.name[bnamelen + 1],
   1223  1.10  christos 					   &digit_end, 10);
   1224   1.1  christos 			/*
   1225   1.1  christos 			 * Remove any backup files that exceed versions.
   1226   1.1  christos 			 */
   1227   1.1  christos 			if (*digit_end == '\0' && version < last) {
   1228  1.10  christos 				int n = unlinkat(dirfd(dir.handle),
   1229  1.10  christos 						 dir.entry.name, 0);
   1230  1.10  christos 				if (n < 0) {
   1231  1.10  christos 					result = isc_errno_toresult(errno);
   1232  1.10  christos 					if (result != ISC_R_SUCCESS &&
   1233  1.10  christos 					    result != ISC_R_FILENOTFOUND)
   1234  1.10  christos 					{
   1235  1.10  christos 						syslog(LOG_ERR,
   1236  1.10  christos 						       "unable to remove log "
   1237  1.10  christos 						       "file '%s%s': %s",
   1238  1.10  christos 						       bname == file->name
   1239  1.10  christos 							       ? ""
   1240  1.10  christos 							       : dirname,
   1241  1.10  christos 						       dir.entry.name,
   1242  1.10  christos 						       isc_result_totext(
   1243  1.10  christos 							       result));
   1244  1.10  christos 					}
   1245   1.5  christos 				}
   1246   1.1  christos 			}
   1247   1.1  christos 		}
   1248   1.1  christos 	}
   1249   1.1  christos 	isc_dir_close(&dir);
   1250   1.1  christos 	return (ISC_R_SUCCESS);
   1251   1.1  christos }
   1252   1.1  christos 
   1253   1.1  christos static isc_result_t
   1254   1.1  christos roll_increment(isc_logfile_t *file) {
   1255   1.1  christos 	int i, n, greatest;
   1256   1.1  christos 	char current[PATH_MAX + 1];
   1257   1.1  christos 	char newpath[PATH_MAX + 1];
   1258   1.1  christos 	const char *path;
   1259   1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   1260   1.1  christos 
   1261   1.1  christos 	REQUIRE(file != NULL);
   1262   1.1  christos 	REQUIRE(file->versions != 0);
   1263   1.1  christos 
   1264   1.1  christos 	path = file->name;
   1265   1.1  christos 
   1266   1.1  christos 	if (file->versions == ISC_LOG_ROLLINFINITE) {
   1267   1.1  christos 		/*
   1268   1.1  christos 		 * Find the first missing entry in the log file sequence.
   1269   1.1  christos 		 */
   1270   1.1  christos 		for (greatest = 0; greatest < INT_MAX; greatest++) {
   1271   1.5  christos 			n = snprintf(current, sizeof(current), "%s.%u", path,
   1272   1.5  christos 				     (unsigned)greatest);
   1273   1.1  christos 			if (n >= (int)sizeof(current) || n < 0 ||
   1274   1.8  christos 			    !isc_file_exists(current))
   1275   1.8  christos 			{
   1276   1.1  christos 				break;
   1277   1.1  christos 			}
   1278   1.1  christos 		}
   1279   1.1  christos 	} else {
   1280   1.1  christos 		/*
   1281   1.1  christos 		 * Get the largest existing version and remove any
   1282   1.1  christos 		 * version greater than the permitted version.
   1283   1.1  christos 		 */
   1284   1.1  christos 		result = greatest_version(file, file->versions, &greatest);
   1285   1.1  christos 		if (result != ISC_R_SUCCESS) {
   1286   1.1  christos 			return (result);
   1287   1.1  christos 		}
   1288   1.1  christos 
   1289   1.1  christos 		/*
   1290   1.1  christos 		 * Increment if greatest is not the actual maximum value.
   1291   1.1  christos 		 */
   1292   1.1  christos 		if (greatest < file->versions - 1) {
   1293   1.1  christos 			greatest++;
   1294   1.1  christos 		}
   1295   1.1  christos 	}
   1296   1.1  christos 
   1297   1.1  christos 	for (i = greatest; i > 0; i--) {
   1298   1.1  christos 		result = ISC_R_SUCCESS;
   1299   1.1  christos 		n = snprintf(current, sizeof(current), "%s.%u", path,
   1300   1.1  christos 			     (unsigned)(i - 1));
   1301   1.1  christos 		if (n >= (int)sizeof(current) || n < 0) {
   1302   1.1  christos 			result = ISC_R_NOSPACE;
   1303   1.1  christos 		}
   1304   1.1  christos 		if (result == ISC_R_SUCCESS) {
   1305   1.5  christos 			n = snprintf(newpath, sizeof(newpath), "%s.%u", path,
   1306   1.5  christos 				     (unsigned)i);
   1307   1.1  christos 			if (n >= (int)sizeof(newpath) || n < 0) {
   1308   1.1  christos 				result = ISC_R_NOSPACE;
   1309   1.1  christos 			}
   1310   1.1  christos 		}
   1311   1.1  christos 		if (result == ISC_R_SUCCESS) {
   1312   1.1  christos 			result = isc_file_rename(current, newpath);
   1313   1.1  christos 		}
   1314   1.1  christos 		if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
   1315   1.1  christos 			syslog(LOG_ERR,
   1316   1.1  christos 			       "unable to rename log file '%s.%u' to "
   1317   1.5  christos 			       "'%s.%u': %s",
   1318   1.5  christos 			       path, i - 1, path, i, isc_result_totext(result));
   1319   1.1  christos 		}
   1320   1.1  christos 	}
   1321   1.1  christos 
   1322   1.1  christos 	n = snprintf(newpath, sizeof(newpath), "%s.0", path);
   1323   1.1  christos 	if (n >= (int)sizeof(newpath) || n < 0) {
   1324   1.1  christos 		result = ISC_R_NOSPACE;
   1325   1.1  christos 	} else {
   1326   1.1  christos 		result = isc_file_rename(path, newpath);
   1327   1.1  christos 	}
   1328   1.1  christos 	if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
   1329   1.5  christos 		syslog(LOG_ERR, "unable to rename log file '%s' to '%s.0': %s",
   1330   1.1  christos 		       path, path, isc_result_totext(result));
   1331   1.1  christos 	}
   1332   1.1  christos 
   1333   1.1  christos 	return (ISC_R_SUCCESS);
   1334   1.1  christos }
   1335   1.1  christos 
   1336   1.1  christos static isc_result_t
   1337   1.1  christos roll_timestamp(isc_logfile_t *file) {
   1338   1.1  christos 	int n;
   1339   1.1  christos 	char newts[PATH_MAX + 1];
   1340   1.1  christos 	char newpath[PATH_MAX + 1];
   1341   1.1  christos 	const char *path;
   1342   1.1  christos 	isc_time_t now;
   1343   1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   1344   1.1  christos 
   1345   1.1  christos 	REQUIRE(file != NULL);
   1346   1.1  christos 	REQUIRE(file->versions != 0);
   1347   1.1  christos 
   1348   1.1  christos 	path = file->name;
   1349   1.1  christos 
   1350   1.1  christos 	/*
   1351   1.1  christos 	 * First find all the logfiles and remove the oldest ones
   1352   1.1  christos 	 * Save one fewer than file->versions because we'll be renaming
   1353   1.1  christos 	 * the existing file to a timestamped version after this.
   1354   1.1  christos 	 */
   1355   1.1  christos 	if (file->versions != ISC_LOG_ROLLINFINITE) {
   1356   1.1  christos 		remove_old_tsversions(file, file->versions - 1);
   1357   1.1  christos 	}
   1358   1.1  christos 
   1359   1.1  christos 	/* Then just rename the current logfile */
   1360   1.1  christos 	isc_time_now(&now);
   1361   1.1  christos 	isc_time_formatshorttimestamp(&now, newts, PATH_MAX + 1);
   1362   1.1  christos 	n = snprintf(newpath, sizeof(newpath), "%s.%s", path, newts);
   1363   1.1  christos 	if (n >= (int)sizeof(newpath) || n < 0) {
   1364   1.1  christos 		result = ISC_R_NOSPACE;
   1365   1.1  christos 	} else {
   1366   1.1  christos 		result = isc_file_rename(path, newpath);
   1367   1.1  christos 	}
   1368   1.1  christos 	if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
   1369   1.5  christos 		syslog(LOG_ERR, "unable to rename log file '%s' to '%s.0': %s",
   1370   1.1  christos 		       path, path, isc_result_totext(result));
   1371   1.1  christos 	}
   1372   1.1  christos 
   1373   1.1  christos 	return (ISC_R_SUCCESS);
   1374   1.1  christos }
   1375   1.1  christos 
   1376   1.1  christos isc_result_t
   1377   1.1  christos isc_logfile_roll(isc_logfile_t *file) {
   1378   1.1  christos 	isc_result_t result;
   1379   1.1  christos 
   1380   1.1  christos 	REQUIRE(file != NULL);
   1381   1.1  christos 
   1382   1.1  christos 	/*
   1383   1.1  christos 	 * Do nothing (not even excess version trimming) if ISC_LOG_ROLLNEVER
   1384   1.1  christos 	 * is specified.  Apparently complete external control over the log
   1385   1.1  christos 	 * files is desired.
   1386   1.1  christos 	 */
   1387   1.1  christos 	if (file->versions == ISC_LOG_ROLLNEVER) {
   1388   1.1  christos 		return (ISC_R_SUCCESS);
   1389   1.1  christos 	} else if (file->versions == 0) {
   1390   1.1  christos 		result = isc_file_remove(file->name);
   1391   1.5  christos 		if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
   1392   1.1  christos 			syslog(LOG_ERR, "unable to remove log file '%s': %s",
   1393   1.1  christos 			       file->name, isc_result_totext(result));
   1394   1.5  christos 		}
   1395   1.1  christos 		return (ISC_R_SUCCESS);
   1396   1.1  christos 	}
   1397   1.1  christos 
   1398   1.1  christos 	switch (file->suffix) {
   1399   1.1  christos 	case isc_log_rollsuffix_increment:
   1400   1.1  christos 		return (roll_increment(file));
   1401   1.1  christos 	case isc_log_rollsuffix_timestamp:
   1402   1.1  christos 		return (roll_timestamp(file));
   1403   1.1  christos 	default:
   1404   1.1  christos 		return (ISC_R_UNEXPECTED);
   1405   1.1  christos 	}
   1406   1.1  christos }
   1407   1.1  christos 
   1408   1.1  christos static isc_result_t
   1409   1.1  christos isc_log_open(isc_logchannel_t *channel) {
   1410   1.1  christos 	struct stat statbuf;
   1411   1.3  christos 	bool regular_file;
   1412   1.3  christos 	bool roll = false;
   1413   1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   1414   1.1  christos 	const char *path;
   1415   1.1  christos 
   1416   1.1  christos 	REQUIRE(channel->type == ISC_LOG_TOFILE);
   1417   1.1  christos 	REQUIRE(FILE_STREAM(channel) == NULL);
   1418   1.1  christos 
   1419   1.1  christos 	path = FILE_NAME(channel);
   1420   1.1  christos 
   1421   1.1  christos 	REQUIRE(path != NULL && *path != '\0');
   1422   1.1  christos 
   1423   1.1  christos 	/*
   1424   1.1  christos 	 * Determine type of file; only regular files will be
   1425   1.1  christos 	 * version renamed, and only if the base file exists
   1426   1.1  christos 	 * and either has no size limit or has reached its size limit.
   1427   1.1  christos 	 */
   1428   1.1  christos 	if (stat(path, &statbuf) == 0) {
   1429   1.3  christos 		regular_file = S_ISREG(statbuf.st_mode) ? true : false;
   1430   1.1  christos 		/* XXXDCL if not regular_file complain? */
   1431   1.1  christos 		if ((FILE_MAXSIZE(channel) == 0 &&
   1432   1.1  christos 		     FILE_VERSIONS(channel) != ISC_LOG_ROLLNEVER) ||
   1433   1.1  christos 		    (FILE_MAXSIZE(channel) > 0 &&
   1434   1.1  christos 		     statbuf.st_size >= FILE_MAXSIZE(channel)))
   1435   1.5  christos 		{
   1436   1.1  christos 			roll = regular_file;
   1437   1.5  christos 		}
   1438   1.1  christos 	} else if (errno == ENOENT) {
   1439   1.3  christos 		regular_file = true;
   1440   1.1  christos 		POST(regular_file);
   1441   1.5  christos 	} else {
   1442   1.1  christos 		result = ISC_R_INVALIDFILE;
   1443   1.5  christos 	}
   1444   1.1  christos 
   1445   1.1  christos 	/*
   1446   1.1  christos 	 * Version control.
   1447   1.1  christos 	 */
   1448   1.1  christos 	if (result == ISC_R_SUCCESS && roll) {
   1449   1.5  christos 		if (FILE_VERSIONS(channel) == ISC_LOG_ROLLNEVER) {
   1450   1.1  christos 			return (ISC_R_MAXSIZE);
   1451   1.5  christos 		}
   1452   1.1  christos 		result = isc_logfile_roll(&channel->destination.file);
   1453   1.1  christos 		if (result != ISC_R_SUCCESS) {
   1454   1.1  christos 			if ((channel->flags & ISC_LOG_OPENERR) == 0) {
   1455   1.1  christos 				syslog(LOG_ERR,
   1456   1.1  christos 				       "isc_log_open: isc_logfile_roll '%s' "
   1457   1.1  christos 				       "failed: %s",
   1458   1.1  christos 				       FILE_NAME(channel),
   1459   1.1  christos 				       isc_result_totext(result));
   1460   1.1  christos 				channel->flags |= ISC_LOG_OPENERR;
   1461   1.1  christos 			}
   1462   1.1  christos 			return (result);
   1463   1.1  christos 		}
   1464   1.1  christos 	}
   1465   1.1  christos 
   1466   1.1  christos 	result = isc_stdio_open(path, "a", &FILE_STREAM(channel));
   1467   1.1  christos 
   1468   1.1  christos 	return (result);
   1469   1.1  christos }
   1470   1.1  christos 
   1471   1.6  christos ISC_NO_SANITIZE_THREAD bool
   1472   1.1  christos isc_log_wouldlog(isc_log_t *lctx, int level) {
   1473   1.1  christos 	/*
   1474   1.1  christos 	 * Try to avoid locking the mutex for messages which can't
   1475   1.1  christos 	 * possibly be logged to any channels -- primarily debugging
   1476   1.1  christos 	 * messages that the debug level is not high enough to print.
   1477   1.1  christos 	 *
   1478   1.1  christos 	 * If the level is (mathematically) less than or equal to the
   1479   1.1  christos 	 * highest_level, or if there is a dynamic channel and the level is
   1480   1.1  christos 	 * less than or equal to the debug level, the main loop must be
   1481   1.1  christos 	 * entered to see if the message should really be output.
   1482   1.1  christos 	 */
   1483   1.5  christos 	if (lctx == NULL) {
   1484   1.5  christos 		return (false);
   1485   1.5  christos 	}
   1486  1.10  christos 	if (forcelog) {
   1487  1.10  christos 		return (true);
   1488  1.10  christos 	}
   1489   1.1  christos 
   1490   1.5  christos 	int highest_level = atomic_load_acquire(&lctx->highest_level);
   1491   1.5  christos 	if (level <= highest_level) {
   1492   1.5  christos 		return (true);
   1493   1.5  christos 	}
   1494   1.5  christos 	if (atomic_load_acquire(&lctx->dynamic)) {
   1495   1.5  christos 		int debug_level = atomic_load_acquire(&lctx->debug_level);
   1496   1.5  christos 		if (level <= debug_level) {
   1497   1.5  christos 			return (true);
   1498   1.5  christos 		}
   1499   1.5  christos 	}
   1500   1.1  christos 
   1501   1.5  christos 	return (false);
   1502   1.1  christos }
   1503   1.1  christos 
   1504   1.1  christos static void
   1505   1.1  christos isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
   1506   1.3  christos 	     isc_logmodule_t *module, int level, bool write_once,
   1507   1.5  christos 	     const char *format, va_list args) {
   1508   1.1  christos 	int syslog_level;
   1509   1.1  christos 	const char *time_string;
   1510   1.1  christos 	char local_time[64];
   1511   1.1  christos 	char iso8601z_string[64];
   1512   1.1  christos 	char iso8601l_string[64];
   1513   1.4  christos 	char level_string[24] = { 0 };
   1514   1.1  christos 	struct stat statbuf;
   1515   1.3  christos 	bool matched = false;
   1516   1.3  christos 	bool printtime, iso8601, utc, printtag, printcolon;
   1517   1.3  christos 	bool printcategory, printmodule, printlevel, buffered;
   1518   1.1  christos 	isc_logchannel_t *channel;
   1519   1.1  christos 	isc_logchannellist_t *category_channels;
   1520  1.10  christos 	int_fast32_t dlevel;
   1521   1.1  christos 	isc_result_t result;
   1522   1.1  christos 
   1523   1.1  christos 	REQUIRE(lctx == NULL || VALID_CONTEXT(lctx));
   1524   1.1  christos 	REQUIRE(category != NULL);
   1525   1.1  christos 	REQUIRE(module != NULL);
   1526   1.1  christos 	REQUIRE(level != ISC_LOG_DYNAMIC);
   1527   1.1  christos 	REQUIRE(format != NULL);
   1528   1.1  christos 
   1529   1.1  christos 	/*
   1530   1.1  christos 	 * Programs can use libraries that use this logging code without
   1531   1.1  christos 	 * wanting to do any logging, thus the log context is allowed to
   1532   1.1  christos 	 * be non-existent.
   1533   1.1  christos 	 */
   1534   1.5  christos 	if (lctx == NULL) {
   1535   1.1  christos 		return;
   1536   1.5  christos 	}
   1537   1.1  christos 
   1538   1.1  christos 	REQUIRE(category->id < lctx->category_count);
   1539   1.1  christos 	REQUIRE(module->id < lctx->module_count);
   1540   1.1  christos 
   1541   1.5  christos 	if (!isc_log_wouldlog(lctx, level)) {
   1542   1.1  christos 		return;
   1543   1.5  christos 	}
   1544   1.1  christos 
   1545   1.1  christos 	local_time[0] = '\0';
   1546   1.1  christos 	iso8601l_string[0] = '\0';
   1547   1.1  christos 	iso8601z_string[0] = '\0';
   1548   1.1  christos 
   1549   1.5  christos 	RDLOCK(&lctx->lcfg_rwl);
   1550   1.1  christos 	LOCK(&lctx->lock);
   1551   1.1  christos 
   1552   1.1  christos 	lctx->buffer[0] = '\0';
   1553   1.1  christos 
   1554   1.5  christos 	isc_logconfig_t *lcfg = lctx->logconfig;
   1555   1.1  christos 
   1556   1.1  christos 	category_channels = ISC_LIST_HEAD(lcfg->channellists[category->id]);
   1557   1.1  christos 
   1558   1.1  christos 	/*
   1559   1.5  christos 	 * XXXDCL add duplicate filtering? (To not write multiple times
   1560   1.5  christos 	 * to the same source via various channels).
   1561   1.1  christos 	 */
   1562   1.1  christos 	do {
   1563   1.1  christos 		/*
   1564   1.5  christos 		 * If the channel list end was reached and a match was
   1565   1.5  christos 		 * made, everything is finished.
   1566   1.1  christos 		 */
   1567   1.5  christos 		if (category_channels == NULL && matched) {
   1568   1.1  christos 			break;
   1569   1.5  christos 		}
   1570   1.1  christos 
   1571   1.5  christos 		if (category_channels == NULL && !matched &&
   1572   1.1  christos 		    category_channels != ISC_LIST_HEAD(lcfg->channellists[0]))
   1573   1.5  christos 		{
   1574   1.1  christos 			/*
   1575   1.5  christos 			 * No category/module pair was explicitly
   1576   1.5  christos 			 * configured. Try the category named "default".
   1577   1.1  christos 			 */
   1578   1.1  christos 			category_channels =
   1579   1.1  christos 				ISC_LIST_HEAD(lcfg->channellists[0]);
   1580   1.5  christos 		}
   1581   1.1  christos 
   1582   1.5  christos 		if (category_channels == NULL && !matched) {
   1583   1.1  christos 			/*
   1584   1.1  christos 			 * No matching module was explicitly configured
   1585   1.5  christos 			 * for the category named "default".  Use the
   1586   1.5  christos 			 * internal default channel.
   1587   1.1  christos 			 */
   1588   1.1  christos 			category_channels = &default_channel;
   1589   1.5  christos 		}
   1590   1.1  christos 
   1591   1.1  christos 		if (category_channels->module != NULL &&
   1592   1.8  christos 		    category_channels->module != module)
   1593   1.8  christos 		{
   1594   1.1  christos 			category_channels = ISC_LIST_NEXT(category_channels,
   1595   1.1  christos 							  link);
   1596   1.1  christos 			continue;
   1597   1.1  christos 		}
   1598   1.1  christos 
   1599   1.3  christos 		matched = true;
   1600   1.1  christos 
   1601   1.1  christos 		channel = category_channels->channel;
   1602   1.1  christos 		category_channels = ISC_LIST_NEXT(category_channels, link);
   1603   1.1  christos 
   1604  1.10  christos 		if (!forcelog) {
   1605  1.10  christos 			dlevel = atomic_load_acquire(&lctx->debug_level);
   1606  1.10  christos 			if (((channel->flags & ISC_LOG_DEBUGONLY) != 0) &&
   1607  1.10  christos 			    dlevel == 0)
   1608  1.10  christos 			{
   1609  1.10  christos 				continue;
   1610  1.10  christos 			}
   1611   1.1  christos 
   1612  1.10  christos 			if (channel->level == ISC_LOG_DYNAMIC) {
   1613  1.10  christos 				if (dlevel < level) {
   1614  1.10  christos 					continue;
   1615  1.10  christos 				}
   1616  1.10  christos 			} else if (channel->level < level) {
   1617   1.1  christos 				continue;
   1618   1.5  christos 			}
   1619   1.5  christos 		}
   1620   1.1  christos 
   1621   1.1  christos 		if ((channel->flags & ISC_LOG_PRINTTIME) != 0 &&
   1622   1.8  christos 		    local_time[0] == '\0')
   1623   1.8  christos 		{
   1624   1.1  christos 			isc_time_t isctime;
   1625   1.1  christos 
   1626   1.1  christos 			TIME_NOW(&isctime);
   1627   1.1  christos 
   1628   1.5  christos 			isc_time_formattimestamp(&isctime, local_time,
   1629   1.1  christos 						 sizeof(local_time));
   1630   1.5  christos 			isc_time_formatISO8601ms(&isctime, iso8601z_string,
   1631   1.1  christos 						 sizeof(iso8601z_string));
   1632   1.5  christos 			isc_time_formatISO8601Lms(&isctime, iso8601l_string,
   1633   1.1  christos 						  sizeof(iso8601l_string));
   1634   1.1  christos 		}
   1635   1.1  christos 
   1636   1.1  christos 		if ((channel->flags & ISC_LOG_PRINTLEVEL) != 0 &&
   1637   1.8  christos 		    level_string[0] == '\0')
   1638   1.8  christos 		{
   1639   1.4  christos 			if (level < ISC_LOG_CRITICAL) {
   1640   1.1  christos 				snprintf(level_string, sizeof(level_string),
   1641   1.4  christos 					 "level %d: ", level);
   1642   1.4  christos 			} else if (level > ISC_LOG_DYNAMIC) {
   1643   1.1  christos 				snprintf(level_string, sizeof(level_string),
   1644   1.1  christos 					 "%s %d: ", log_level_strings[0],
   1645   1.1  christos 					 level);
   1646   1.4  christos 			} else {
   1647   1.1  christos 				snprintf(level_string, sizeof(level_string),
   1648   1.1  christos 					 "%s: ", log_level_strings[-level]);
   1649   1.4  christos 			}
   1650   1.1  christos 		}
   1651   1.1  christos 
   1652   1.1  christos 		/*
   1653   1.1  christos 		 * Only format the message once.
   1654   1.1  christos 		 */
   1655   1.1  christos 		if (lctx->buffer[0] == '\0') {
   1656   1.1  christos 			(void)vsnprintf(lctx->buffer, sizeof(lctx->buffer),
   1657   1.4  christos 					format, args);
   1658   1.1  christos 
   1659   1.1  christos 			/*
   1660   1.1  christos 			 * Check for duplicates.
   1661   1.1  christos 			 */
   1662   1.1  christos 			if (write_once) {
   1663   1.1  christos 				isc_logmessage_t *message, *next;
   1664   1.1  christos 				isc_time_t oldest;
   1665   1.1  christos 				isc_interval_t interval;
   1666   1.1  christos 				size_t size;
   1667   1.1  christos 
   1668   1.1  christos 				isc_interval_set(&interval,
   1669   1.1  christos 						 lcfg->duplicate_interval, 0);
   1670   1.1  christos 
   1671   1.1  christos 				/*
   1672   1.5  christos 				 * 'oldest' is the age of the oldest
   1673   1.5  christos 				 * messages which fall within the
   1674   1.5  christos 				 * duplicate_interval range.
   1675   1.1  christos 				 */
   1676   1.1  christos 				TIME_NOW(&oldest);
   1677   1.1  christos 				if (isc_time_subtract(&oldest, &interval,
   1678   1.5  christos 						      &oldest) != ISC_R_SUCCESS)
   1679   1.5  christos 				{
   1680   1.1  christos 					/*
   1681   1.5  christos 					 * Can't effectively do the
   1682   1.5  christos 					 * checking without having a
   1683   1.5  christos 					 * valid time.
   1684   1.1  christos 					 */
   1685   1.1  christos 					message = NULL;
   1686   1.5  christos 				} else {
   1687   1.1  christos 					message = ISC_LIST_HEAD(lctx->messages);
   1688   1.5  christos 				}
   1689   1.1  christos 
   1690   1.1  christos 				while (message != NULL) {
   1691   1.1  christos 					if (isc_time_compare(&message->time,
   1692   1.8  christos 							     &oldest) < 0)
   1693   1.8  christos 					{
   1694   1.1  christos 						/*
   1695   1.1  christos 						 * This message is older
   1696   1.5  christos 						 * than the
   1697   1.5  christos 						 * duplicate_interval,
   1698   1.5  christos 						 * so it should be
   1699   1.5  christos 						 * dropped from the
   1700   1.5  christos 						 * history.
   1701   1.1  christos 						 *
   1702   1.5  christos 						 * Setting the interval
   1703   1.5  christos 						 * to be to be longer
   1704   1.5  christos 						 * will obviously not
   1705   1.5  christos 						 * cause the expired
   1706   1.5  christos 						 * message to spring
   1707   1.5  christos 						 * back into existence.
   1708   1.1  christos 						 */
   1709   1.1  christos 						next = ISC_LIST_NEXT(message,
   1710   1.1  christos 								     link);
   1711   1.1  christos 
   1712   1.1  christos 						ISC_LIST_UNLINK(lctx->messages,
   1713   1.1  christos 								message, link);
   1714   1.1  christos 
   1715   1.5  christos 						isc_mem_put(
   1716   1.5  christos 							lctx->mctx, message,
   1717   1.1  christos 							sizeof(*message) + 1 +
   1718   1.5  christos 								strlen(message->text));
   1719   1.1  christos 
   1720   1.1  christos 						message = next;
   1721   1.1  christos 						continue;
   1722   1.1  christos 					}
   1723   1.1  christos 
   1724   1.1  christos 					/*
   1725   1.5  christos 					 * This message is in the
   1726   1.5  christos 					 * duplicate filtering interval
   1727   1.5  christos 					 * ...
   1728   1.1  christos 					 */
   1729   1.5  christos 					if (strcmp(lctx->buffer,
   1730   1.8  christos 						   message->text) == 0)
   1731   1.8  christos 					{
   1732   1.1  christos 						/*
   1733   1.5  christos 						 * ... and it is a
   1734   1.5  christos 						 * duplicate. Unlock the
   1735   1.5  christos 						 * mutex and get the
   1736   1.5  christos 						 * hell out of Dodge.
   1737   1.1  christos 						 */
   1738   1.5  christos 						goto unlock;
   1739   1.1  christos 					}
   1740   1.1  christos 
   1741   1.1  christos 					message = ISC_LIST_NEXT(message, link);
   1742   1.1  christos 				}
   1743   1.1  christos 
   1744   1.1  christos 				/*
   1745   1.1  christos 				 * It wasn't in the duplicate interval,
   1746   1.1  christos 				 * so add it to the message list.
   1747   1.1  christos 				 */
   1748   1.1  christos 				size = sizeof(isc_logmessage_t) +
   1749   1.1  christos 				       strlen(lctx->buffer) + 1;
   1750   1.1  christos 				message = isc_mem_get(lctx->mctx, size);
   1751   1.5  christos 				message->text = (char *)(message + 1);
   1752   1.5  christos 				size -= sizeof(isc_logmessage_t);
   1753   1.5  christos 				strlcpy(message->text, lctx->buffer, size);
   1754   1.5  christos 				TIME_NOW(&message->time);
   1755   1.5  christos 				ISC_LINK_INIT(message, link);
   1756   1.5  christos 				ISC_LIST_APPEND(lctx->messages, message, link);
   1757   1.1  christos 			}
   1758   1.1  christos 		}
   1759   1.1  christos 
   1760   1.5  christos 		utc = ((channel->flags & ISC_LOG_UTC) != 0);
   1761   1.5  christos 		iso8601 = ((channel->flags & ISC_LOG_ISO8601) != 0);
   1762   1.5  christos 		printtime = ((channel->flags & ISC_LOG_PRINTTIME) != 0);
   1763   1.5  christos 		printtag = ((channel->flags &
   1764   1.5  christos 			     (ISC_LOG_PRINTTAG | ISC_LOG_PRINTPREFIX)) != 0 &&
   1765   1.5  christos 			    lcfg->tag != NULL);
   1766   1.5  christos 		printcolon = ((channel->flags & ISC_LOG_PRINTTAG) != 0 &&
   1767   1.5  christos 			      lcfg->tag != NULL);
   1768   1.3  christos 		printcategory = ((channel->flags & ISC_LOG_PRINTCATEGORY) != 0);
   1769   1.5  christos 		printmodule = ((channel->flags & ISC_LOG_PRINTMODULE) != 0);
   1770   1.5  christos 		printlevel = ((channel->flags & ISC_LOG_PRINTLEVEL) != 0);
   1771   1.5  christos 		buffered = ((channel->flags & ISC_LOG_BUFFERED) != 0);
   1772   1.1  christos 
   1773   1.1  christos 		if (printtime) {
   1774   1.1  christos 			if (iso8601) {
   1775   1.1  christos 				if (utc) {
   1776   1.1  christos 					time_string = iso8601z_string;
   1777   1.1  christos 				} else {
   1778   1.1  christos 					time_string = iso8601l_string;
   1779   1.1  christos 				}
   1780   1.1  christos 			} else {
   1781   1.1  christos 				time_string = local_time;
   1782   1.1  christos 			}
   1783   1.5  christos 		} else {
   1784   1.1  christos 			time_string = "";
   1785   1.5  christos 		}
   1786   1.1  christos 
   1787   1.1  christos 		switch (channel->type) {
   1788   1.1  christos 		case ISC_LOG_TOFILE:
   1789   1.1  christos 			if (FILE_MAXREACHED(channel)) {
   1790   1.1  christos 				/*
   1791   1.1  christos 				 * If the file can be rolled, OR
   1792   1.1  christos 				 * If the file no longer exists, OR
   1793   1.5  christos 				 * If the file is less than the maximum
   1794   1.5  christos 				 * size, (such as if it had been renamed
   1795   1.5  christos 				 * and a new one touched, or it was
   1796   1.5  christos 				 * truncated in place)
   1797   1.5  christos 				 * ... then close it to trigger
   1798   1.5  christos 				 * reopening.
   1799   1.1  christos 				 */
   1800   1.1  christos 				if (FILE_VERSIONS(channel) !=
   1801   1.5  christos 					    ISC_LOG_ROLLNEVER ||
   1802   1.1  christos 				    (stat(FILE_NAME(channel), &statbuf) != 0 &&
   1803   1.1  christos 				     errno == ENOENT) ||
   1804   1.5  christos 				    statbuf.st_size < FILE_MAXSIZE(channel))
   1805   1.5  christos 				{
   1806   1.1  christos 					(void)fclose(FILE_STREAM(channel));
   1807   1.1  christos 					FILE_STREAM(channel) = NULL;
   1808   1.3  christos 					FILE_MAXREACHED(channel) = false;
   1809   1.5  christos 				} else {
   1810   1.1  christos 					/*
   1811   1.1  christos 					 * Eh, skip it.
   1812   1.1  christos 					 */
   1813   1.1  christos 					break;
   1814   1.5  christos 				}
   1815   1.1  christos 			}
   1816   1.1  christos 
   1817   1.1  christos 			if (FILE_STREAM(channel) == NULL) {
   1818   1.1  christos 				result = isc_log_open(channel);
   1819   1.1  christos 				if (result != ISC_R_SUCCESS &&
   1820   1.1  christos 				    result != ISC_R_MAXSIZE &&
   1821   1.5  christos 				    (channel->flags & ISC_LOG_OPENERR) == 0)
   1822   1.5  christos 				{
   1823   1.1  christos 					syslog(LOG_ERR,
   1824   1.5  christos 					       "isc_log_open '%s' "
   1825   1.5  christos 					       "failed: %s",
   1826   1.1  christos 					       FILE_NAME(channel),
   1827   1.1  christos 					       isc_result_totext(result));
   1828   1.1  christos 					channel->flags |= ISC_LOG_OPENERR;
   1829   1.1  christos 				}
   1830   1.5  christos 				if (result != ISC_R_SUCCESS) {
   1831   1.1  christos 					break;
   1832   1.5  christos 				}
   1833   1.1  christos 				channel->flags &= ~ISC_LOG_OPENERR;
   1834   1.1  christos 			}
   1835   1.7  christos 			FALLTHROUGH;
   1836   1.1  christos 
   1837   1.1  christos 		case ISC_LOG_TOFILEDESC:
   1838   1.5  christos 			fprintf(FILE_STREAM(channel), "%s%s%s%s%s%s%s%s%s%s\n",
   1839   1.5  christos 				printtime ? time_string : "",
   1840   1.5  christos 				printtime ? " " : "", printtag ? lcfg->tag : "",
   1841   1.5  christos 				printcolon ? ": " : "",
   1842   1.5  christos 				printcategory ? category->name : "",
   1843   1.5  christos 				printcategory ? ": " : "",
   1844   1.5  christos 				printmodule ? (module != NULL ? module->name
   1845   1.5  christos 							      : "no_module")
   1846   1.5  christos 					    : "",
   1847   1.5  christos 				printmodule ? ": " : "",
   1848   1.5  christos 				printlevel ? level_string : "", lctx->buffer);
   1849   1.1  christos 
   1850   1.5  christos 			if (!buffered) {
   1851   1.1  christos 				fflush(FILE_STREAM(channel));
   1852   1.5  christos 			}
   1853   1.1  christos 
   1854   1.1  christos 			/*
   1855   1.1  christos 			 * If the file now exceeds its maximum size
   1856   1.5  christos 			 * threshold, note it so that it will not be
   1857   1.5  christos 			 * logged to any more.
   1858   1.1  christos 			 */
   1859   1.1  christos 			if (FILE_MAXSIZE(channel) > 0) {
   1860   1.1  christos 				INSIST(channel->type == ISC_LOG_TOFILE);
   1861   1.1  christos 
   1862   1.1  christos 				/* XXXDCL NT fstat/fileno */
   1863   1.1  christos 				/* XXXDCL complain if fstat fails? */
   1864   1.1  christos 				if (fstat(fileno(FILE_STREAM(channel)),
   1865   1.1  christos 					  &statbuf) >= 0 &&
   1866   1.1  christos 				    statbuf.st_size > FILE_MAXSIZE(channel))
   1867   1.5  christos 				{
   1868   1.3  christos 					FILE_MAXREACHED(channel) = true;
   1869   1.5  christos 				}
   1870   1.1  christos 			}
   1871   1.1  christos 
   1872   1.1  christos 			break;
   1873   1.1  christos 
   1874   1.1  christos 		case ISC_LOG_TOSYSLOG:
   1875   1.5  christos 			if (level > 0) {
   1876   1.1  christos 				syslog_level = LOG_DEBUG;
   1877   1.5  christos 			} else if (level < ISC_LOG_CRITICAL) {
   1878   1.1  christos 				syslog_level = LOG_CRIT;
   1879   1.5  christos 			} else {
   1880   1.1  christos 				syslog_level = syslog_map[-level];
   1881   1.5  christos 			}
   1882   1.1  christos 
   1883   1.5  christos 			(void)syslog(
   1884   1.5  christos 				FACILITY(channel) | syslog_level,
   1885   1.5  christos 				"%s%s%s%s%s%s%s%s%s%s",
   1886   1.5  christos 				printtime ? time_string : "",
   1887   1.5  christos 				printtime ? " " : "", printtag ? lcfg->tag : "",
   1888   1.5  christos 				printcolon ? ": " : "",
   1889   1.5  christos 				printcategory ? category->name : "",
   1890   1.5  christos 				printcategory ? ": " : "",
   1891   1.5  christos 				printmodule ? (module != NULL ? module->name
   1892   1.5  christos 							      : "no_module")
   1893   1.5  christos 					    : "",
   1894   1.5  christos 				printmodule ? ": " : "",
   1895   1.5  christos 				printlevel ? level_string : "", lctx->buffer);
   1896   1.1  christos 			break;
   1897   1.1  christos 
   1898   1.1  christos 		case ISC_LOG_TONULL:
   1899   1.1  christos 			break;
   1900   1.1  christos 		}
   1901   1.1  christos 	} while (1);
   1902   1.1  christos 
   1903   1.5  christos unlock:
   1904   1.1  christos 	UNLOCK(&lctx->lock);
   1905   1.5  christos 	RDUNLOCK(&lctx->lcfg_rwl);
   1906   1.1  christos }
   1907  1.10  christos 
   1908  1.10  christos void
   1909  1.10  christos isc_log_setforcelog(bool v) {
   1910  1.10  christos 	forcelog = v;
   1911  1.10  christos }
   1912