15185a700Sflorian /*
25185a700Sflorian * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
35185a700Sflorian *
45185a700Sflorian * Permission to use, copy, modify, and/or distribute this software for any
55185a700Sflorian * purpose with or without fee is hereby granted, provided that the above
65185a700Sflorian * copyright notice and this permission notice appear in all copies.
75185a700Sflorian *
85185a700Sflorian * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
95185a700Sflorian * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
105185a700Sflorian * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
115185a700Sflorian * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
125185a700Sflorian * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
135185a700Sflorian * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
145185a700Sflorian * PERFORMANCE OF THIS SOFTWARE.
155185a700Sflorian */
165185a700Sflorian
17*1fb015a8Sflorian /* $Id: log.c,v 1.18 2020/09/14 08:40:44 florian Exp $ */
185185a700Sflorian
195185a700Sflorian /*! \file
205185a700Sflorian * \author Principal Authors: DCL */
215185a700Sflorian
224465bcfbSjsg #include <sys/time.h>
235185a700Sflorian #include <limits.h>
248b553854Sflorian #include <stdlib.h>
258b553854Sflorian #include <string.h>
26c6d1a7a6Sjsg #include <syslog.h>
274465bcfbSjsg #include <time.h>
285185a700Sflorian
295185a700Sflorian #include <isc/log.h>
305185a700Sflorian #include <isc/util.h>
315185a700Sflorian
325185a700Sflorian /*
335185a700Sflorian * XXXDCL make dynamic?
345185a700Sflorian */
355185a700Sflorian #define LOG_BUFFER_SIZE (8 * 1024)
365185a700Sflorian
375185a700Sflorian /*!
385185a700Sflorian * This is the structure that holds each named channel. A simple linked
395185a700Sflorian * list chains all of the channels together, so an individual channel is
405185a700Sflorian * found by doing strcmp()s with the names down the list. Their should
415185a700Sflorian * be no performance penalty from this as it is expected that the number
425185a700Sflorian * of named channels will be no more than a dozen or so, and name lookups
435185a700Sflorian * from the head of the list are only done when isc_log_usechannel() is
445185a700Sflorian * called, which should also be very infrequent.
455185a700Sflorian */
465185a700Sflorian typedef struct isc_logchannel isc_logchannel_t;
475185a700Sflorian
485185a700Sflorian struct isc_logchannel {
495185a700Sflorian char * name;
505185a700Sflorian unsigned int type;
515185a700Sflorian int level;
525185a700Sflorian unsigned int flags;
535185a700Sflorian isc_logdestination_t destination;
545185a700Sflorian ISC_LINK(isc_logchannel_t) link;
555185a700Sflorian };
565185a700Sflorian
575185a700Sflorian /*!
585185a700Sflorian * The logchannellist structure associates categories and modules with
595185a700Sflorian * channels. First the appropriate channellist is found based on the
605185a700Sflorian * category, and then each structure in the linked list is checked for
615185a700Sflorian * a matching module. It is expected that the number of channels
625185a700Sflorian * associated with any given category will be very short, no more than
635185a700Sflorian * three or four in the more unusual cases.
645185a700Sflorian */
655185a700Sflorian typedef struct isc_logchannellist isc_logchannellist_t;
665185a700Sflorian
675185a700Sflorian struct isc_logchannellist {
685185a700Sflorian const isc_logmodule_t * module;
695185a700Sflorian isc_logchannel_t * channel;
705185a700Sflorian ISC_LINK(isc_logchannellist_t) link;
715185a700Sflorian };
725185a700Sflorian
735185a700Sflorian /*!
745185a700Sflorian * This structure is used to remember messages for pruning via
755185a700Sflorian * isc_log_[v]write1().
765185a700Sflorian */
775185a700Sflorian typedef struct isc_logmessage isc_logmessage_t;
785185a700Sflorian
795185a700Sflorian struct isc_logmessage {
805185a700Sflorian char * text;
817238a213Sflorian struct timespec time;
825185a700Sflorian ISC_LINK(isc_logmessage_t) link;
835185a700Sflorian };
845185a700Sflorian
855185a700Sflorian /*!
865185a700Sflorian * The isc_logconfig structure is used to store the configurable information
875185a700Sflorian * about where messages are actually supposed to be sent -- the information
885185a700Sflorian * that could changed based on some configuration file, as opposed to the
895185a700Sflorian * the category/module specification of isc_log_[v]write[1] that is compiled
905185a700Sflorian * into a program, or the debug_level which is dynamic state information.
915185a700Sflorian */
925185a700Sflorian struct isc_logconfig {
935185a700Sflorian isc_log_t * lctx;
945185a700Sflorian ISC_LIST(isc_logchannel_t) channels;
955185a700Sflorian ISC_LIST(isc_logchannellist_t) *channellists;
965185a700Sflorian unsigned int channellist_count;
975185a700Sflorian unsigned int duplicate_interval;
985185a700Sflorian int highest_level;
995185a700Sflorian char * tag;
100*1fb015a8Sflorian int dynamic;
1015185a700Sflorian };
1025185a700Sflorian
1035185a700Sflorian /*!
1045185a700Sflorian * This isc_log structure provides the context for the isc_log functions.
1055185a700Sflorian * The log context locks itself in isc_log_doit, the internal backend to
1065185a700Sflorian * isc_log_write. The locking is necessary both to provide exclusive access
1075185a700Sflorian * to the buffer into which the message is formatted and to guard against
1085185a700Sflorian * competing threads trying to write to the same syslog resource. (On
1095185a700Sflorian * some systems, such as BSD/OS, stdio is thread safe but syslog is not.)
1105185a700Sflorian * Unfortunately, the lock cannot guard against a _different_ logging
1115185a700Sflorian * context in the same program competing for syslog's attention. Thus
1125185a700Sflorian * There Can Be Only One, but this is not enforced.
1135185a700Sflorian * XXXDCL enforce it?
1145185a700Sflorian *
1155185a700Sflorian * Note that the category and module information is not locked.
1165185a700Sflorian * This is because in the usual case, only one isc_log_t is ever created
1175185a700Sflorian * in a program, and the category/module registration happens only once.
1185185a700Sflorian * XXXDCL it might be wise to add more locking overall.
1195185a700Sflorian */
1205185a700Sflorian struct isc_log {
1215185a700Sflorian /* Not locked. */
1225185a700Sflorian isc_logcategory_t * categories;
1235185a700Sflorian unsigned int category_count;
1245185a700Sflorian isc_logmodule_t * modules;
1255185a700Sflorian unsigned int module_count;
1265185a700Sflorian int debug_level;
1275185a700Sflorian /* Locked by isc_log lock. */
1285185a700Sflorian isc_logconfig_t * logconfig;
1295185a700Sflorian char buffer[LOG_BUFFER_SIZE];
1305185a700Sflorian ISC_LIST(isc_logmessage_t) messages;
1315185a700Sflorian };
1325185a700Sflorian
1335185a700Sflorian /*!
1345185a700Sflorian * Used when ISC_LOG_PRINTLEVEL is enabled for a channel.
1355185a700Sflorian */
1365185a700Sflorian static const char *log_level_strings[] = {
1375185a700Sflorian "debug",
1385185a700Sflorian "info",
1395185a700Sflorian "notice",
1405185a700Sflorian "warning",
1415185a700Sflorian "error",
1425185a700Sflorian "critical"
1435185a700Sflorian };
1445185a700Sflorian
1455185a700Sflorian /*!
1465185a700Sflorian * Used to convert ISC_LOG_* priorities into syslog priorities.
1475185a700Sflorian * XXXDCL This will need modification for NT.
1485185a700Sflorian */
1495185a700Sflorian static const int syslog_map[] = {
1505185a700Sflorian LOG_DEBUG,
1515185a700Sflorian LOG_INFO,
1525185a700Sflorian LOG_NOTICE,
1535185a700Sflorian LOG_WARNING,
1545185a700Sflorian LOG_ERR,
1555185a700Sflorian LOG_CRIT
1565185a700Sflorian };
1575185a700Sflorian
1585185a700Sflorian /*!
1595185a700Sflorian * When adding new categories, a corresponding ISC_LOGCATEGORY_foo
1605185a700Sflorian * definition needs to be added to <isc/log.h>.
1615185a700Sflorian *
1625185a700Sflorian * The default category is provided so that the internal default can
1635185a700Sflorian * be overridden. Since the default is always looked up as the first
1645185a700Sflorian * channellist in the log context, it must come first in isc_categories[].
1655185a700Sflorian */
1665185a700Sflorian isc_logcategory_t isc_categories[] = {
1675185a700Sflorian { "default", 0 }, /* "default" must come first. */
1685185a700Sflorian { "general", 0 },
1695185a700Sflorian { NULL, 0 }
1705185a700Sflorian };
1715185a700Sflorian
1725185a700Sflorian /*!
1735185a700Sflorian * See above comment for categories on LIBISC_EXTERNAL_DATA, and apply it to modules.
1745185a700Sflorian */
1755185a700Sflorian isc_logmodule_t isc_modules[] = {
1765185a700Sflorian { "socket", 0 },
1775185a700Sflorian { "time", 0 },
1785185a700Sflorian { "interface", 0 },
1795185a700Sflorian { "timer", 0 },
1805185a700Sflorian { "file", 0 },
1815185a700Sflorian { "other", 0 },
1825185a700Sflorian { NULL, 0 }
1835185a700Sflorian };
1845185a700Sflorian
1855185a700Sflorian /*!
1865185a700Sflorian * This essentially constant structure must be filled in at run time,
1875185a700Sflorian * because its channel member is pointed to a channel that is created
1885185a700Sflorian * dynamically with isc_log_createchannel.
1895185a700Sflorian */
1905185a700Sflorian static isc_logchannellist_t default_channel;
1915185a700Sflorian
1925185a700Sflorian /*!
1935185a700Sflorian * libisc logs to this context.
1945185a700Sflorian */
1955185a700Sflorian isc_log_t *isc_lctx = NULL;
1965185a700Sflorian
1975185a700Sflorian /*!
1985185a700Sflorian * Forward declarations.
1995185a700Sflorian */
2005185a700Sflorian static isc_result_t
2015185a700Sflorian assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
2025185a700Sflorian const isc_logmodule_t *module, isc_logchannel_t *channel);
2035185a700Sflorian
2045185a700Sflorian static isc_result_t
2055185a700Sflorian sync_channellist(isc_logconfig_t *lcfg);
2065185a700Sflorian
2075185a700Sflorian static void
2085185a700Sflorian isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
209*1fb015a8Sflorian isc_logmodule_t *module, int level, int write_once,
2105185a700Sflorian const char *format, va_list args)
21187f06ebfSflorian __attribute__((__format__(__printf__, 6, 0)));
2125185a700Sflorian
2135185a700Sflorian /*@{*/
2145185a700Sflorian /*!
2155185a700Sflorian * Convenience macros.
2165185a700Sflorian */
2175185a700Sflorian
2185185a700Sflorian #define FACILITY(channel) (channel->destination.facility)
2195185a700Sflorian #define FILE_NAME(channel) (channel->destination.file.name)
2205185a700Sflorian #define FILE_STREAM(channel) (channel->destination.file.stream)
2215185a700Sflorian #define FILE_VERSIONS(channel) (channel->destination.file.versions)
2225185a700Sflorian #define FILE_MAXSIZE(channel) (channel->destination.file.maximum_size)
2235185a700Sflorian
2245185a700Sflorian /*@}*/
2255185a700Sflorian /****
2265185a700Sflorian **** Public interfaces.
2275185a700Sflorian ****/
2285185a700Sflorian
2295185a700Sflorian /*
2305185a700Sflorian * Establish a new logging context, with default channels.
2315185a700Sflorian */
2325185a700Sflorian isc_result_t
isc_log_create(isc_log_t ** lctxp,isc_logconfig_t ** lcfgp)2335185a700Sflorian isc_log_create(isc_log_t **lctxp, isc_logconfig_t **lcfgp) {
2345185a700Sflorian isc_log_t *lctx;
2355185a700Sflorian isc_logconfig_t *lcfg = NULL;
2365185a700Sflorian isc_result_t result;
2375185a700Sflorian
2385185a700Sflorian REQUIRE(lctxp != NULL && *lctxp == NULL);
2395185a700Sflorian REQUIRE(lcfgp == NULL || *lcfgp == NULL);
2405185a700Sflorian
2415185a700Sflorian lctx = malloc(sizeof(*lctx));
2425185a700Sflorian if (lctx != NULL) {
2435185a700Sflorian lctx->categories = NULL;
2445185a700Sflorian lctx->category_count = 0;
2455185a700Sflorian lctx->modules = NULL;
2465185a700Sflorian lctx->module_count = 0;
2475185a700Sflorian lctx->debug_level = 0;
2485185a700Sflorian
2495185a700Sflorian ISC_LIST_INIT(lctx->messages);
2505185a700Sflorian
2515185a700Sflorian isc_log_registercategories(lctx, isc_categories);
2525185a700Sflorian isc_log_registermodules(lctx, isc_modules);
2535185a700Sflorian result = isc_logconfig_create(lctx, &lcfg);
2545185a700Sflorian
2555185a700Sflorian } else
2565185a700Sflorian result = ISC_R_NOMEMORY;
2575185a700Sflorian
2585185a700Sflorian if (result == ISC_R_SUCCESS)
2595185a700Sflorian result = sync_channellist(lcfg);
2605185a700Sflorian
2615185a700Sflorian if (result == ISC_R_SUCCESS) {
2625185a700Sflorian lctx->logconfig = lcfg;
2635185a700Sflorian
2645185a700Sflorian *lctxp = lctx;
2655185a700Sflorian if (lcfgp != NULL)
2665185a700Sflorian *lcfgp = lcfg;
2675185a700Sflorian
2685185a700Sflorian } else {
2695185a700Sflorian if (lcfg != NULL)
2705185a700Sflorian isc_logconfig_destroy(&lcfg);
2715185a700Sflorian if (lctx != NULL)
2725185a700Sflorian isc_log_destroy(&lctx);
2735185a700Sflorian }
2745185a700Sflorian
2755185a700Sflorian return (result);
2765185a700Sflorian }
2775185a700Sflorian
2785185a700Sflorian isc_result_t
isc_logconfig_create(isc_log_t * lctx,isc_logconfig_t ** lcfgp)2795185a700Sflorian isc_logconfig_create(isc_log_t *lctx, isc_logconfig_t **lcfgp) {
2805185a700Sflorian isc_logconfig_t *lcfg;
2815185a700Sflorian isc_logdestination_t destination;
2825185a700Sflorian isc_result_t result = ISC_R_SUCCESS;
2835185a700Sflorian int level = ISC_LOG_INFO;
2845185a700Sflorian
2855185a700Sflorian REQUIRE(lcfgp != NULL && *lcfgp == NULL);
2865185a700Sflorian
2875185a700Sflorian lcfg = malloc(sizeof(*lcfg));
2885185a700Sflorian
2895185a700Sflorian if (lcfg != NULL) {
2905185a700Sflorian lcfg->lctx = lctx;
2915185a700Sflorian lcfg->channellists = NULL;
2925185a700Sflorian lcfg->channellist_count = 0;
2935185a700Sflorian lcfg->duplicate_interval = 0;
2945185a700Sflorian lcfg->highest_level = level;
2955185a700Sflorian lcfg->tag = NULL;
296*1fb015a8Sflorian lcfg->dynamic = 0;
2975185a700Sflorian
2985185a700Sflorian ISC_LIST_INIT(lcfg->channels);
2995185a700Sflorian
3005185a700Sflorian } else
3015185a700Sflorian result = ISC_R_NOMEMORY;
3025185a700Sflorian
3035185a700Sflorian /*
3045185a700Sflorian * Create the default channels:
3055185a700Sflorian * default_syslog, default_stderr, default_debug and null.
3065185a700Sflorian */
3075185a700Sflorian if (result == ISC_R_SUCCESS) {
3085185a700Sflorian destination.facility = LOG_DAEMON;
3095185a700Sflorian result = isc_log_createchannel(lcfg, "default_syslog",
3105185a700Sflorian ISC_LOG_TOSYSLOG, level,
3115185a700Sflorian &destination, 0);
3125185a700Sflorian }
3135185a700Sflorian
3145185a700Sflorian if (result == ISC_R_SUCCESS) {
3155185a700Sflorian destination.file.stream = stderr;
3165185a700Sflorian destination.file.name = NULL;
3175185a700Sflorian destination.file.versions = ISC_LOG_ROLLNEVER;
3185185a700Sflorian destination.file.maximum_size = 0;
3195185a700Sflorian result = isc_log_createchannel(lcfg, "default_stderr",
3205185a700Sflorian ISC_LOG_TOFILEDESC,
3215185a700Sflorian level,
3225185a700Sflorian &destination,
3235185a700Sflorian ISC_LOG_PRINTTIME);
3245185a700Sflorian }
3255185a700Sflorian
3265185a700Sflorian if (result == ISC_R_SUCCESS) {
3275185a700Sflorian /*
3285185a700Sflorian * Set the default category's channel to default_stderr,
3295185a700Sflorian * which is at the head of the channels list because it was
3305185a700Sflorian * just created.
3315185a700Sflorian */
3325185a700Sflorian default_channel.channel = ISC_LIST_HEAD(lcfg->channels);
3335185a700Sflorian
3345185a700Sflorian destination.file.stream = stderr;
3355185a700Sflorian destination.file.name = NULL;
3365185a700Sflorian destination.file.versions = ISC_LOG_ROLLNEVER;
3375185a700Sflorian destination.file.maximum_size = 0;
3385185a700Sflorian result = isc_log_createchannel(lcfg, "default_debug",
3395185a700Sflorian ISC_LOG_TOFILEDESC,
3405185a700Sflorian ISC_LOG_DYNAMIC,
3415185a700Sflorian &destination,
3425185a700Sflorian ISC_LOG_PRINTTIME);
3435185a700Sflorian }
3445185a700Sflorian
3455185a700Sflorian if (result == ISC_R_SUCCESS)
3465185a700Sflorian result = isc_log_createchannel(lcfg, "null",
3475185a700Sflorian ISC_LOG_TONULL,
3485185a700Sflorian ISC_LOG_DYNAMIC,
3495185a700Sflorian NULL, 0);
3505185a700Sflorian
3515185a700Sflorian if (result == ISC_R_SUCCESS)
3525185a700Sflorian *lcfgp = lcfg;
3535185a700Sflorian
3545185a700Sflorian else
3555185a700Sflorian if (lcfg != NULL)
3565185a700Sflorian isc_logconfig_destroy(&lcfg);
3575185a700Sflorian
3585185a700Sflorian return (result);
3595185a700Sflorian }
3605185a700Sflorian
3615185a700Sflorian void
isc_log_destroy(isc_log_t ** lctxp)3625185a700Sflorian isc_log_destroy(isc_log_t **lctxp) {
3635185a700Sflorian isc_log_t *lctx;
3645185a700Sflorian isc_logconfig_t *lcfg;
3655185a700Sflorian isc_logmessage_t *message;
3665185a700Sflorian
3678b553854Sflorian REQUIRE(lctxp != NULL);
3685185a700Sflorian
3695185a700Sflorian lctx = *lctxp;
3705185a700Sflorian if (lctx->logconfig != NULL) {
3715185a700Sflorian lcfg = lctx->logconfig;
3725185a700Sflorian lctx->logconfig = NULL;
3735185a700Sflorian isc_logconfig_destroy(&lcfg);
3745185a700Sflorian }
3755185a700Sflorian
3765185a700Sflorian while ((message = ISC_LIST_HEAD(lctx->messages)) != NULL) {
3775185a700Sflorian ISC_LIST_UNLINK(lctx->messages, message, link);
3785185a700Sflorian
3795185a700Sflorian free(message);
3805185a700Sflorian }
3815185a700Sflorian
3825185a700Sflorian lctx->buffer[0] = '\0';
3835185a700Sflorian lctx->debug_level = 0;
3845185a700Sflorian lctx->categories = NULL;
3855185a700Sflorian lctx->category_count = 0;
3865185a700Sflorian lctx->modules = NULL;
3875185a700Sflorian lctx->module_count = 0;
3885185a700Sflorian free(lctx);
3895185a700Sflorian *lctxp = NULL;
3905185a700Sflorian }
3915185a700Sflorian
3925185a700Sflorian void
isc_logconfig_destroy(isc_logconfig_t ** lcfgp)3935185a700Sflorian isc_logconfig_destroy(isc_logconfig_t **lcfgp) {
3945185a700Sflorian isc_logconfig_t *lcfg;
3955185a700Sflorian isc_logchannel_t *channel;
3965185a700Sflorian isc_logchannellist_t *item;
3975185a700Sflorian unsigned int i;
3985185a700Sflorian
3998b553854Sflorian REQUIRE(lcfgp != NULL);
4005185a700Sflorian
4015185a700Sflorian lcfg = *lcfgp;
4025185a700Sflorian
4035185a700Sflorian /*
4045185a700Sflorian * This function cannot be called with a logconfig that is in
4055185a700Sflorian * use by a log context.
4065185a700Sflorian */
4075185a700Sflorian REQUIRE(lcfg->lctx != NULL && lcfg->lctx->logconfig != lcfg);
4085185a700Sflorian
4095185a700Sflorian while ((channel = ISC_LIST_HEAD(lcfg->channels)) != NULL) {
4105185a700Sflorian ISC_LIST_UNLINK(lcfg->channels, channel, link);
4115185a700Sflorian
4125185a700Sflorian free(channel->name);
4135185a700Sflorian free(channel);
4145185a700Sflorian }
4155185a700Sflorian
4165185a700Sflorian for (i = 0; i < lcfg->channellist_count; i++)
4175185a700Sflorian while ((item = ISC_LIST_HEAD(lcfg->channellists[i])) != NULL) {
4185185a700Sflorian ISC_LIST_UNLINK(lcfg->channellists[i], item, link);
4195185a700Sflorian free(item);
4205185a700Sflorian }
4215185a700Sflorian
4225185a700Sflorian if (lcfg->channellist_count > 0)
4235185a700Sflorian free(lcfg->channellists);
4245185a700Sflorian
425*1fb015a8Sflorian lcfg->dynamic = 0;
4265185a700Sflorian if (lcfg->tag != NULL)
4275185a700Sflorian free(lcfg->tag);
4285185a700Sflorian lcfg->tag = NULL;
4295185a700Sflorian lcfg->highest_level = 0;
4305185a700Sflorian lcfg->duplicate_interval = 0;
4315185a700Sflorian free(lcfg);
4325185a700Sflorian *lcfgp = NULL;
4335185a700Sflorian }
4345185a700Sflorian
4355185a700Sflorian void
isc_log_registercategories(isc_log_t * lctx,isc_logcategory_t categories[])4365185a700Sflorian isc_log_registercategories(isc_log_t *lctx, isc_logcategory_t categories[]) {
4375185a700Sflorian isc_logcategory_t *catp;
4385185a700Sflorian
4395185a700Sflorian REQUIRE(categories != NULL && categories[0].name != NULL);
4405185a700Sflorian
4415185a700Sflorian /*
4425185a700Sflorian * XXXDCL This somewhat sleazy situation of using the last pointer
4435185a700Sflorian * in one category array to point to the next array exists because
4445185a700Sflorian * this registration function returns void and I didn't want to have
4455185a700Sflorian * change everything that used it by making it return an isc_result_t.
4465185a700Sflorian * It would need to do that if it had to allocate memory to store
4475185a700Sflorian * pointers to each array passed in.
4485185a700Sflorian */
4495185a700Sflorian if (lctx->categories == NULL)
4505185a700Sflorian lctx->categories = categories;
4515185a700Sflorian
4525185a700Sflorian else {
4535185a700Sflorian /*
4545185a700Sflorian * Adjust the last (NULL) pointer of the already registered
4555185a700Sflorian * categories to point to the incoming array.
4565185a700Sflorian */
4575185a700Sflorian for (catp = lctx->categories; catp->name != NULL; )
4585185a700Sflorian if (catp->id == UINT_MAX)
4595185a700Sflorian /*
4605185a700Sflorian * The name pointer points to the next array.
4615185a700Sflorian * Ick.
4625185a700Sflorian */
4635185a700Sflorian DE_CONST(catp->name, catp);
4645185a700Sflorian else
4655185a700Sflorian catp++;
4665185a700Sflorian
4675185a700Sflorian catp->name = (void *)categories;
4685185a700Sflorian catp->id = UINT_MAX;
4695185a700Sflorian }
4705185a700Sflorian
4715185a700Sflorian /*
4725185a700Sflorian * Update the id number of the category with its new global id.
4735185a700Sflorian */
4745185a700Sflorian for (catp = categories; catp->name != NULL; catp++)
4755185a700Sflorian catp->id = lctx->category_count++;
4765185a700Sflorian }
4775185a700Sflorian
4785185a700Sflorian void
isc_log_registermodules(isc_log_t * lctx,isc_logmodule_t modules[])4795185a700Sflorian isc_log_registermodules(isc_log_t *lctx, isc_logmodule_t modules[]) {
4805185a700Sflorian isc_logmodule_t *modp;
4815185a700Sflorian
4825185a700Sflorian REQUIRE(modules != NULL && modules[0].name != NULL);
4835185a700Sflorian
4845185a700Sflorian /*
4855185a700Sflorian * XXXDCL This somewhat sleazy situation of using the last pointer
4865185a700Sflorian * in one category array to point to the next array exists because
4875185a700Sflorian * this registration function returns void and I didn't want to have
4885185a700Sflorian * change everything that used it by making it return an isc_result_t.
4895185a700Sflorian * It would need to do that if it had to allocate memory to store
4905185a700Sflorian * pointers to each array passed in.
4915185a700Sflorian */
4925185a700Sflorian if (lctx->modules == NULL)
4935185a700Sflorian lctx->modules = modules;
4945185a700Sflorian
4955185a700Sflorian else {
4965185a700Sflorian /*
4975185a700Sflorian * Adjust the last (NULL) pointer of the already registered
4985185a700Sflorian * modules to point to the incoming array.
4995185a700Sflorian */
5005185a700Sflorian for (modp = lctx->modules; modp->name != NULL; )
5015185a700Sflorian if (modp->id == UINT_MAX)
5025185a700Sflorian /*
5035185a700Sflorian * The name pointer points to the next array.
5045185a700Sflorian * Ick.
5055185a700Sflorian */
5065185a700Sflorian DE_CONST(modp->name, modp);
5075185a700Sflorian else
5085185a700Sflorian modp++;
5095185a700Sflorian
5105185a700Sflorian modp->name = (void *)modules;
5115185a700Sflorian modp->id = UINT_MAX;
5125185a700Sflorian }
5135185a700Sflorian
5145185a700Sflorian /*
5155185a700Sflorian * Update the id number of the module with its new global id.
5165185a700Sflorian */
5175185a700Sflorian for (modp = modules; modp->name != NULL; modp++)
5185185a700Sflorian modp->id = lctx->module_count++;
5195185a700Sflorian }
5205185a700Sflorian
5215185a700Sflorian isc_result_t
isc_log_createchannel(isc_logconfig_t * lcfg,const char * name,unsigned int type,int level,const isc_logdestination_t * destination,unsigned int flags)5225185a700Sflorian isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
5235185a700Sflorian unsigned int type, int level,
5245185a700Sflorian const isc_logdestination_t *destination,
5255185a700Sflorian unsigned int flags)
5265185a700Sflorian {
5275185a700Sflorian isc_logchannel_t *channel;
5285185a700Sflorian
5295185a700Sflorian REQUIRE(name != NULL);
5305185a700Sflorian REQUIRE(type == ISC_LOG_TOSYSLOG ||
5315185a700Sflorian type == ISC_LOG_TOFILEDESC || type == ISC_LOG_TONULL);
5325185a700Sflorian REQUIRE(destination != NULL || type == ISC_LOG_TONULL);
5335185a700Sflorian REQUIRE(level >= ISC_LOG_CRITICAL);
5345185a700Sflorian REQUIRE((flags &
5355185a700Sflorian (unsigned int)~(ISC_LOG_PRINTALL | ISC_LOG_DEBUGONLY)) == 0);
5365185a700Sflorian
5375185a700Sflorian /* XXXDCL find duplicate names? */
5385185a700Sflorian
5395185a700Sflorian channel = malloc(sizeof(*channel));
5405185a700Sflorian if (channel == NULL)
5415185a700Sflorian return (ISC_R_NOMEMORY);
5425185a700Sflorian
5435185a700Sflorian channel->name = strdup(name);
5445185a700Sflorian if (channel->name == NULL) {
5455185a700Sflorian free(channel);
5465185a700Sflorian return (ISC_R_NOMEMORY);
5475185a700Sflorian }
5485185a700Sflorian
5495185a700Sflorian channel->type = type;
5505185a700Sflorian channel->level = level;
5515185a700Sflorian channel->flags = flags;
5525185a700Sflorian ISC_LINK_INIT(channel, link);
5535185a700Sflorian
5545185a700Sflorian switch (type) {
5555185a700Sflorian case ISC_LOG_TOSYSLOG:
5565185a700Sflorian FACILITY(channel) = destination->facility;
5575185a700Sflorian break;
5585185a700Sflorian
5595185a700Sflorian case ISC_LOG_TOFILEDESC:
5605185a700Sflorian FILE_NAME(channel) = NULL;
5615185a700Sflorian FILE_STREAM(channel) = destination->file.stream;
5625185a700Sflorian FILE_MAXSIZE(channel) = 0;
5635185a700Sflorian FILE_VERSIONS(channel) = ISC_LOG_ROLLNEVER;
5645185a700Sflorian break;
5655185a700Sflorian
5665185a700Sflorian case ISC_LOG_TONULL:
5675185a700Sflorian /* Nothing. */
5685185a700Sflorian break;
5695185a700Sflorian
5705185a700Sflorian default:
5715185a700Sflorian free(channel->name);
5725185a700Sflorian free(channel);
5735185a700Sflorian return (ISC_R_UNEXPECTED);
5745185a700Sflorian }
5755185a700Sflorian
5765185a700Sflorian ISC_LIST_PREPEND(lcfg->channels, channel, link);
5775185a700Sflorian
5785185a700Sflorian /*
5795185a700Sflorian * If default_stderr was redefined, make the default category
5805185a700Sflorian * point to the new default_stderr.
5815185a700Sflorian */
5825185a700Sflorian if (strcmp(name, "default_stderr") == 0)
5835185a700Sflorian default_channel.channel = channel;
5845185a700Sflorian
5855185a700Sflorian return (ISC_R_SUCCESS);
5865185a700Sflorian }
5875185a700Sflorian
5885185a700Sflorian isc_result_t
isc_log_usechannel(isc_logconfig_t * lcfg,const char * name,const isc_logcategory_t * category,const isc_logmodule_t * module)5895185a700Sflorian isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
5905185a700Sflorian const isc_logcategory_t *category,
5915185a700Sflorian const isc_logmodule_t *module)
5925185a700Sflorian {
5935185a700Sflorian isc_log_t *lctx;
5945185a700Sflorian isc_logchannel_t *channel;
5955185a700Sflorian isc_result_t result = ISC_R_SUCCESS;
5965185a700Sflorian unsigned int i;
5975185a700Sflorian
5985185a700Sflorian REQUIRE(name != NULL);
5995185a700Sflorian
6005185a700Sflorian lctx = lcfg->lctx;
6015185a700Sflorian
6025185a700Sflorian REQUIRE(category == NULL || category->id < lctx->category_count);
6035185a700Sflorian REQUIRE(module == NULL || module->id < lctx->module_count);
6045185a700Sflorian
6055185a700Sflorian for (channel = ISC_LIST_HEAD(lcfg->channels); channel != NULL;
6065185a700Sflorian channel = ISC_LIST_NEXT(channel, link))
6075185a700Sflorian if (strcmp(name, channel->name) == 0)
6085185a700Sflorian break;
6095185a700Sflorian
6105185a700Sflorian if (channel == NULL)
6115185a700Sflorian return (ISC_R_NOTFOUND);
6125185a700Sflorian
6135185a700Sflorian if (category != NULL)
6145185a700Sflorian result = assignchannel(lcfg, category->id, module, channel);
6155185a700Sflorian
6165185a700Sflorian else
6175185a700Sflorian /*
6185185a700Sflorian * Assign to all categories. Note that this includes
6195185a700Sflorian * the default channel.
6205185a700Sflorian */
6215185a700Sflorian for (i = 0; i < lctx->category_count; i++) {
6225185a700Sflorian result = assignchannel(lcfg, i, module, channel);
6235185a700Sflorian if (result != ISC_R_SUCCESS)
6245185a700Sflorian break;
6255185a700Sflorian }
6265185a700Sflorian
6275185a700Sflorian return (result);
6285185a700Sflorian }
6295185a700Sflorian
6305185a700Sflorian void
isc_log_write(isc_log_t * lctx,isc_logcategory_t * category,isc_logmodule_t * module,int level,const char * format,...)6315185a700Sflorian isc_log_write(isc_log_t *lctx, isc_logcategory_t *category,
6325185a700Sflorian isc_logmodule_t *module, int level, const char *format, ...)
6335185a700Sflorian {
6345185a700Sflorian va_list args;
6355185a700Sflorian
6365185a700Sflorian /*
6375185a700Sflorian * Contract checking is done in isc_log_doit().
6385185a700Sflorian */
6395185a700Sflorian
6405185a700Sflorian va_start(args, format);
641*1fb015a8Sflorian isc_log_doit(lctx, category, module, level, 0, format, args);
6425185a700Sflorian va_end(args);
6435185a700Sflorian }
6445185a700Sflorian
6455185a700Sflorian void
isc_log_setcontext(isc_log_t * lctx)6465185a700Sflorian isc_log_setcontext(isc_log_t *lctx) {
6475185a700Sflorian isc_lctx = lctx;
6485185a700Sflorian }
6495185a700Sflorian
6505185a700Sflorian void
isc_log_setdebuglevel(isc_log_t * lctx,unsigned int level)6515185a700Sflorian isc_log_setdebuglevel(isc_log_t *lctx, unsigned int level) {
6525185a700Sflorian
6535185a700Sflorian lctx->debug_level = level;
6545185a700Sflorian }
6555185a700Sflorian
6565185a700Sflorian /****
6575185a700Sflorian **** Internal functions
6585185a700Sflorian ****/
6595185a700Sflorian
6605185a700Sflorian static isc_result_t
assignchannel(isc_logconfig_t * lcfg,unsigned int category_id,const isc_logmodule_t * module,isc_logchannel_t * channel)6615185a700Sflorian assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
6625185a700Sflorian const isc_logmodule_t *module, isc_logchannel_t *channel)
6635185a700Sflorian {
6645185a700Sflorian isc_logchannellist_t *new_item;
6655185a700Sflorian isc_log_t *lctx;
6665185a700Sflorian isc_result_t result;
6675185a700Sflorian
6685185a700Sflorian lctx = lcfg->lctx;
6695185a700Sflorian
6705185a700Sflorian REQUIRE(category_id < lctx->category_count);
6715185a700Sflorian REQUIRE(module == NULL || module->id < lctx->module_count);
6725185a700Sflorian REQUIRE(channel != NULL);
6735185a700Sflorian
6745185a700Sflorian /*
6755185a700Sflorian * Ensure lcfg->channellist_count == lctx->category_count.
6765185a700Sflorian */
6775185a700Sflorian result = sync_channellist(lcfg);
6785185a700Sflorian if (result != ISC_R_SUCCESS)
6795185a700Sflorian return (result);
6805185a700Sflorian
6815185a700Sflorian new_item = malloc(sizeof(*new_item));
6825185a700Sflorian if (new_item == NULL)
6835185a700Sflorian return (ISC_R_NOMEMORY);
6845185a700Sflorian
6855185a700Sflorian new_item->channel = channel;
6865185a700Sflorian new_item->module = module;
6875185a700Sflorian ISC_LIST_INITANDPREPEND(lcfg->channellists[category_id],
6885185a700Sflorian new_item, link);
6895185a700Sflorian
6905185a700Sflorian /*
6915185a700Sflorian * Remember the highest logging level set by any channel in the
6925185a700Sflorian * logging config, so isc_log_doit() can quickly return if the
6935185a700Sflorian * message is too high to be logged by any channel.
6945185a700Sflorian */
6955185a700Sflorian if (channel->type != ISC_LOG_TONULL) {
6965185a700Sflorian if (lcfg->highest_level < channel->level)
6975185a700Sflorian lcfg->highest_level = channel->level;
6985185a700Sflorian if (channel->level == ISC_LOG_DYNAMIC)
699*1fb015a8Sflorian lcfg->dynamic = 1;
7005185a700Sflorian }
7015185a700Sflorian
7025185a700Sflorian return (ISC_R_SUCCESS);
7035185a700Sflorian }
7045185a700Sflorian
7055185a700Sflorian /*
7065185a700Sflorian * This would ideally be part of isc_log_registercategories(), except then
7075185a700Sflorian * that function would have to return isc_result_t instead of void.
7085185a700Sflorian */
7095185a700Sflorian static isc_result_t
sync_channellist(isc_logconfig_t * lcfg)7105185a700Sflorian sync_channellist(isc_logconfig_t *lcfg) {
7115185a700Sflorian unsigned int bytes;
7125185a700Sflorian isc_log_t *lctx;
7135185a700Sflorian void *lists;
7145185a700Sflorian
7155185a700Sflorian lctx = lcfg->lctx;
7165185a700Sflorian
7175185a700Sflorian REQUIRE(lctx->category_count != 0);
7185185a700Sflorian
7195185a700Sflorian if (lctx->category_count == lcfg->channellist_count)
7205185a700Sflorian return (ISC_R_SUCCESS);
7215185a700Sflorian
7225185a700Sflorian bytes = lctx->category_count * sizeof(ISC_LIST(isc_logchannellist_t));
7235185a700Sflorian
7245185a700Sflorian lists = malloc(bytes);
7255185a700Sflorian
7265185a700Sflorian if (lists == NULL)
7275185a700Sflorian return (ISC_R_NOMEMORY);
7285185a700Sflorian
7295185a700Sflorian memset(lists, 0, bytes);
7305185a700Sflorian
7315185a700Sflorian if (lcfg->channellist_count != 0) {
7325185a700Sflorian bytes = lcfg->channellist_count *
7335185a700Sflorian sizeof(ISC_LIST(isc_logchannellist_t));
7345185a700Sflorian memmove(lists, lcfg->channellists, bytes);
7355185a700Sflorian free(lcfg->channellists);
7365185a700Sflorian }
7375185a700Sflorian
7385185a700Sflorian lcfg->channellists = lists;
7395185a700Sflorian lcfg->channellist_count = lctx->category_count;
7405185a700Sflorian
7415185a700Sflorian return (ISC_R_SUCCESS);
7425185a700Sflorian }
7435185a700Sflorian
744*1fb015a8Sflorian int
isc_log_wouldlog(isc_log_t * lctx,int level)7455185a700Sflorian isc_log_wouldlog(isc_log_t *lctx, int level) {
7465185a700Sflorian /*
7475185a700Sflorian * If the level is (mathematically) less than or equal to the
7485185a700Sflorian * highest_level, or if there is a dynamic channel and the level is
7495185a700Sflorian * less than or equal to the debug level, the main loop must be
7505185a700Sflorian * entered to see if the message should really be output.
7515185a700Sflorian *
7525185a700Sflorian * NOTE: this is UNLOCKED access to the logconfig. However,
7535185a700Sflorian * the worst thing that can happen is that a bad decision is made
7545185a700Sflorian * about returning without logging, and that's not a big concern,
7555185a700Sflorian * because that's a risk anyway if the logconfig is being
7565185a700Sflorian * dynamically changed.
7575185a700Sflorian */
7585185a700Sflorian
7595185a700Sflorian if (lctx == NULL || lctx->logconfig == NULL)
760*1fb015a8Sflorian return (0);
7615185a700Sflorian
762*1fb015a8Sflorian return (level <= lctx->logconfig->highest_level ||
7635185a700Sflorian (lctx->logconfig->dynamic &&
764*1fb015a8Sflorian level <= lctx->debug_level));
7655185a700Sflorian }
7665185a700Sflorian
7675185a700Sflorian static void
isc_log_doit(isc_log_t * lctx,isc_logcategory_t * category,isc_logmodule_t * module,int level,int write_once,const char * format,va_list args)7685185a700Sflorian isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
769*1fb015a8Sflorian isc_logmodule_t *module, int level, int write_once,
7705185a700Sflorian const char *format, va_list args)
7715185a700Sflorian {
7725185a700Sflorian int syslog_level;
7735185a700Sflorian char time_string[64];
7745185a700Sflorian char level_string[24];
7755185a700Sflorian const char *iformat;
776*1fb015a8Sflorian int matched = 0;
777*1fb015a8Sflorian int printtime, printtag, printcolon;
778*1fb015a8Sflorian int printcategory, printmodule, printlevel;
7795185a700Sflorian isc_logconfig_t *lcfg;
7805185a700Sflorian isc_logchannel_t *channel;
7815185a700Sflorian isc_logchannellist_t *category_channels;
7825185a700Sflorian
7835185a700Sflorian REQUIRE(category != NULL);
7845185a700Sflorian REQUIRE(module != NULL);
7855185a700Sflorian REQUIRE(level != ISC_LOG_DYNAMIC);
7865185a700Sflorian REQUIRE(format != NULL);
7875185a700Sflorian
7885185a700Sflorian /*
7895185a700Sflorian * Programs can use libraries that use this logging code without
7905185a700Sflorian * wanting to do any logging, thus the log context is allowed to
7915185a700Sflorian * be non-existent.
7925185a700Sflorian */
7935185a700Sflorian if (lctx == NULL)
7945185a700Sflorian return;
7955185a700Sflorian
7965185a700Sflorian REQUIRE(category->id < lctx->category_count);
7975185a700Sflorian REQUIRE(module->id < lctx->module_count);
7985185a700Sflorian
7995185a700Sflorian if (! isc_log_wouldlog(lctx, level))
8005185a700Sflorian return;
8015185a700Sflorian
8025185a700Sflorian iformat = format;
8035185a700Sflorian
8045185a700Sflorian time_string[0] = '\0';
8055185a700Sflorian level_string[0] = '\0';
8065185a700Sflorian
8075185a700Sflorian lctx->buffer[0] = '\0';
8085185a700Sflorian
8095185a700Sflorian lcfg = lctx->logconfig;
8105185a700Sflorian
8115185a700Sflorian category_channels = ISC_LIST_HEAD(lcfg->channellists[category->id]);
8125185a700Sflorian
8135185a700Sflorian /*
8145185a700Sflorian * XXXDCL add duplicate filtering? (To not write multiple times to
8155185a700Sflorian * the same source via various channels).
8165185a700Sflorian */
8175185a700Sflorian do {
8185185a700Sflorian /*
8195185a700Sflorian * If the channel list end was reached and a match was made,
8205185a700Sflorian * everything is finished.
8215185a700Sflorian */
8225185a700Sflorian if (category_channels == NULL && matched)
8235185a700Sflorian break;
8245185a700Sflorian
8255185a700Sflorian if (category_channels == NULL && ! matched &&
8265185a700Sflorian category_channels != ISC_LIST_HEAD(lcfg->channellists[0]))
8275185a700Sflorian /*
8285185a700Sflorian * No category/module pair was explicitly configured.
8295185a700Sflorian * Try the category named "default".
8305185a700Sflorian */
8315185a700Sflorian category_channels =
8325185a700Sflorian ISC_LIST_HEAD(lcfg->channellists[0]);
8335185a700Sflorian
8345185a700Sflorian if (category_channels == NULL && ! matched)
8355185a700Sflorian /*
8365185a700Sflorian * No matching module was explicitly configured
8375185a700Sflorian * for the category named "default". Use the internal
8385185a700Sflorian * default channel.
8395185a700Sflorian */
8405185a700Sflorian category_channels = &default_channel;
8415185a700Sflorian
8425185a700Sflorian if (category_channels->module != NULL &&
8435185a700Sflorian category_channels->module != module) {
8445185a700Sflorian category_channels = ISC_LIST_NEXT(category_channels,
8455185a700Sflorian link);
8465185a700Sflorian continue;
8475185a700Sflorian }
8485185a700Sflorian
849*1fb015a8Sflorian matched = 1;
8505185a700Sflorian
8515185a700Sflorian channel = category_channels->channel;
8525185a700Sflorian category_channels = ISC_LIST_NEXT(category_channels, link);
8535185a700Sflorian
8545185a700Sflorian if (((channel->flags & ISC_LOG_DEBUGONLY) != 0) &&
8555185a700Sflorian lctx->debug_level == 0)
8565185a700Sflorian continue;
8575185a700Sflorian
8585185a700Sflorian if (channel->level == ISC_LOG_DYNAMIC) {
8595185a700Sflorian if (lctx->debug_level < level)
8605185a700Sflorian continue;
8615185a700Sflorian } else if (channel->level < level)
8625185a700Sflorian continue;
8635185a700Sflorian
8645185a700Sflorian if ((channel->flags & ISC_LOG_PRINTTIME) != 0 &&
8655185a700Sflorian time_string[0] == '\0') {
866b53d8310Sflorian time_t now;
867b53d8310Sflorian now = time(NULL);
868f9959b0fSflorian strftime(time_string, sizeof(time_string),
869b53d8310Sflorian "%d-%b-%Y %X", localtime(&now));
8705185a700Sflorian }
8715185a700Sflorian
8725185a700Sflorian if ((channel->flags & ISC_LOG_PRINTLEVEL) != 0 &&
8735185a700Sflorian level_string[0] == '\0') {
8745185a700Sflorian if (level < ISC_LOG_CRITICAL)
8755185a700Sflorian snprintf(level_string, sizeof(level_string),
8765185a700Sflorian "level %d: ", level);
8775185a700Sflorian else if (level > ISC_LOG_DYNAMIC)
8785185a700Sflorian snprintf(level_string, sizeof(level_string),
8795185a700Sflorian "%s %d: ", log_level_strings[0],
8805185a700Sflorian level);
8815185a700Sflorian else
8825185a700Sflorian snprintf(level_string, sizeof(level_string),
8835185a700Sflorian "%s: ", log_level_strings[-level]);
8845185a700Sflorian }
8855185a700Sflorian
8865185a700Sflorian /*
8875185a700Sflorian * Only format the message once.
8885185a700Sflorian */
8895185a700Sflorian if (lctx->buffer[0] == '\0') {
8905185a700Sflorian (void)vsnprintf(lctx->buffer, sizeof(lctx->buffer),
8915185a700Sflorian iformat, args);
8925185a700Sflorian
8935185a700Sflorian /*
8945185a700Sflorian * Check for duplicates.
8955185a700Sflorian */
8965185a700Sflorian if (write_once) {
8975185a700Sflorian isc_logmessage_t *message, *next;
8987238a213Sflorian struct timespec oldest;
8997238a213Sflorian struct timespec interval;
9005185a700Sflorian size_t size;
901396be909Sflorian interval.tv_sec = lcfg->duplicate_interval;
902396be909Sflorian interval.tv_nsec = 0;
9035185a700Sflorian
9045185a700Sflorian /*
9055185a700Sflorian * 'oldest' is the age of the oldest messages
9065185a700Sflorian * which fall within the duplicate_interval
9075185a700Sflorian * range.
9085185a700Sflorian */
909b53d8310Sflorian clock_gettime(CLOCK_MONOTONIC, &oldest);
910ffbbf1a1Sflorian timespecsub(&oldest, &interval, &oldest);
9115185a700Sflorian message = ISC_LIST_HEAD(lctx->messages);
9125185a700Sflorian
9135185a700Sflorian while (message != NULL) {
914ba6f4614Sflorian if (timespeccmp(&message->time,
915ba6f4614Sflorian &oldest, <)) {
9165185a700Sflorian /*
9175185a700Sflorian * This message is older
9185185a700Sflorian * than the duplicate_interval,
9195185a700Sflorian * so it should be dropped from
9205185a700Sflorian * the history.
9215185a700Sflorian *
9225185a700Sflorian * Setting the interval to be
9235185a700Sflorian * to be longer will obviously
9245185a700Sflorian * not cause the expired
9255185a700Sflorian * message to spring back into
9265185a700Sflorian * existence.
9275185a700Sflorian */
9285185a700Sflorian next = ISC_LIST_NEXT(message,
9295185a700Sflorian link);
9305185a700Sflorian
9315185a700Sflorian ISC_LIST_UNLINK(lctx->messages,
9325185a700Sflorian message, link);
9335185a700Sflorian
9345185a700Sflorian free(message);
9355185a700Sflorian
9365185a700Sflorian message = next;
9375185a700Sflorian continue;
9385185a700Sflorian }
9395185a700Sflorian
9405185a700Sflorian /*
9415185a700Sflorian * This message is in the duplicate
9425185a700Sflorian * filtering interval ...
9435185a700Sflorian */
9445185a700Sflorian if (strcmp(lctx->buffer, message->text)
9455185a700Sflorian == 0) {
9465185a700Sflorian /*
9475185a700Sflorian * ... and it is a duplicate.
9485185a700Sflorian */
9495185a700Sflorian return;
9505185a700Sflorian }
9515185a700Sflorian
9525185a700Sflorian message = ISC_LIST_NEXT(message, link);
9535185a700Sflorian }
9545185a700Sflorian
9555185a700Sflorian /*
9565185a700Sflorian * It wasn't in the duplicate interval,
9575185a700Sflorian * so add it to the message list.
9585185a700Sflorian */
9595185a700Sflorian size = sizeof(isc_logmessage_t) +
9605185a700Sflorian strlen(lctx->buffer) + 1;
9615185a700Sflorian message = malloc(size);
9625185a700Sflorian if (message != NULL) {
9635185a700Sflorian /*
9645185a700Sflorian * Put the text immediately after
9655185a700Sflorian * the struct. The strcpy is safe.
9665185a700Sflorian */
9675185a700Sflorian message->text = (char *)(message + 1);
9685185a700Sflorian size -= sizeof(isc_logmessage_t);
9695185a700Sflorian strlcpy(message->text, lctx->buffer,
9705185a700Sflorian size);
9715185a700Sflorian
972b53d8310Sflorian clock_gettime(CLOCK_MONOTONIC,
973b53d8310Sflorian &message->time);
9745185a700Sflorian
9755185a700Sflorian ISC_LINK_INIT(message, link);
9765185a700Sflorian ISC_LIST_APPEND(lctx->messages,
9775185a700Sflorian message, link);
9785185a700Sflorian }
9795185a700Sflorian }
9805185a700Sflorian }
9815185a700Sflorian
982*1fb015a8Sflorian printtime = (channel->flags & ISC_LOG_PRINTTIME) != 0;
983*1fb015a8Sflorian printtag = (channel->flags &
9845185a700Sflorian (ISC_LOG_PRINTTAG|ISC_LOG_PRINTPREFIX))
985*1fb015a8Sflorian != 0 && lcfg->tag != NULL;
986*1fb015a8Sflorian printcolon = (channel->flags & ISC_LOG_PRINTTAG)
987*1fb015a8Sflorian != 0 && lcfg->tag != NULL;
988*1fb015a8Sflorian printcategory = (channel->flags & ISC_LOG_PRINTCATEGORY) != 0;
989*1fb015a8Sflorian printmodule = (channel->flags & ISC_LOG_PRINTMODULE) != 0;
990*1fb015a8Sflorian printlevel = (channel->flags & ISC_LOG_PRINTLEVEL) != 0;
9915185a700Sflorian
9925185a700Sflorian switch (channel->type) {
9935185a700Sflorian case ISC_LOG_TOFILEDESC:
9945185a700Sflorian fprintf(FILE_STREAM(channel),
9955185a700Sflorian "%s%s%s%s%s%s%s%s%s%s\n",
9965185a700Sflorian printtime ? time_string : "",
9975185a700Sflorian printtime ? " " : "",
9985185a700Sflorian printtag ? lcfg->tag : "",
9995185a700Sflorian printcolon ? ": " : "",
10005185a700Sflorian printcategory ? category->name : "",
10015185a700Sflorian printcategory ? ": " : "",
10025185a700Sflorian printmodule ? (module != NULL ? module->name
10035185a700Sflorian : "no_module")
10045185a700Sflorian : "",
10055185a700Sflorian printmodule ? ": " : "",
10065185a700Sflorian printlevel ? level_string : "",
10075185a700Sflorian lctx->buffer);
10085185a700Sflorian
10095185a700Sflorian fflush(FILE_STREAM(channel));
10105185a700Sflorian break;
10115185a700Sflorian
10125185a700Sflorian case ISC_LOG_TOSYSLOG:
10135185a700Sflorian if (level > 0)
10145185a700Sflorian syslog_level = LOG_DEBUG;
10155185a700Sflorian else if (level < ISC_LOG_CRITICAL)
10165185a700Sflorian syslog_level = LOG_CRIT;
10175185a700Sflorian else
10185185a700Sflorian syslog_level = syslog_map[-level];
10195185a700Sflorian
10205185a700Sflorian (void)syslog(FACILITY(channel) | syslog_level,
10215185a700Sflorian "%s%s%s%s%s%s%s%s%s%s",
10225185a700Sflorian printtime ? time_string : "",
10235185a700Sflorian printtime ? " " : "",
10245185a700Sflorian printtag ? lcfg->tag : "",
10255185a700Sflorian printcolon ? ": " : "",
10265185a700Sflorian printcategory ? category->name : "",
10275185a700Sflorian printcategory ? ": " : "",
10285185a700Sflorian printmodule ? (module != NULL
10295185a700Sflorian ? module->name
10305185a700Sflorian : "no_module")
10315185a700Sflorian : "",
10325185a700Sflorian printmodule ? ": " : "",
10335185a700Sflorian printlevel ? level_string : "",
10345185a700Sflorian lctx->buffer);
10355185a700Sflorian break;
10365185a700Sflorian
10375185a700Sflorian case ISC_LOG_TONULL:
10385185a700Sflorian break;
10395185a700Sflorian
10405185a700Sflorian }
10415185a700Sflorian
10425185a700Sflorian } while (1);
10435185a700Sflorian }
1044