1999-09-23 17:43:51 +00:00
|
|
|
/*
|
2018-02-15 13:20:59 +11:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
1999-09-23 17:43:51 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
1999-09-23 17:43:51 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
1999-09-23 17:43:51 +00:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
2000-03-01 17:31:56 +00:00
|
|
|
* information regarding copyright ownership.
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
1999-09-23 17:43:51 +00:00
|
|
|
#include <limits.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
1999-09-23 17:43:51 +00:00
|
|
|
#include <stdlib.h>
|
2022-12-16 11:59:48 +00:00
|
|
|
#include <sys/stat.h>
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <sys/types.h> /* dev_t FreeBSD 2.1 */
|
2000-08-24 23:22:40 +00:00
|
|
|
#include <time.h>
|
2023-04-14 13:53:41 +10:00
|
|
|
#include <unistd.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
|
2019-12-20 19:29:18 -03:00
|
|
|
#include <isc/atomic.h>
|
1999-09-23 17:43:51 +00:00
|
|
|
#include <isc/dir.h>
|
2023-04-14 13:53:41 +10:00
|
|
|
#include <isc/errno.h>
|
2001-06-27 04:37:11 +00:00
|
|
|
#include <isc/file.h>
|
1999-09-23 17:43:51 +00:00
|
|
|
#include <isc/log.h>
|
2000-05-16 03:37:39 +00:00
|
|
|
#include <isc/magic.h>
|
1999-09-23 17:43:51 +00:00
|
|
|
#include <isc/mem.h>
|
2001-03-28 04:16:32 +00:00
|
|
|
#include <isc/stdio.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2020-05-15 16:37:44 -07:00
|
|
|
#include <isc/thread.h>
|
1999-10-25 19:55:06 +00:00
|
|
|
#include <isc/time.h>
|
2023-12-19 10:41:15 +03:00
|
|
|
#include <isc/urcu.h>
|
2000-02-26 19:57:02 +00:00
|
|
|
#include <isc/util.h>
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2001-06-04 19:33:39 +00:00
|
|
|
#define LCTX_MAGIC ISC_MAGIC('L', 'c', 't', 'x')
|
2000-05-16 03:37:39 +00:00
|
|
|
#define VALID_CONTEXT(lctx) ISC_MAGIC_VALID(lctx, LCTX_MAGIC)
|
2001-06-04 19:33:39 +00:00
|
|
|
|
|
|
|
#define LCFG_MAGIC ISC_MAGIC('L', 'c', 'f', 'g')
|
2000-05-16 03:37:39 +00:00
|
|
|
#define VALID_CONFIG(lcfg) ISC_MAGIC_VALID(lcfg, LCFG_MAGIC)
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2020-05-15 16:37:44 -07:00
|
|
|
static thread_local bool forcelog = false;
|
|
|
|
|
2000-03-01 17:31:56 +00:00
|
|
|
/*
|
|
|
|
* XXXDCL make dynamic?
|
|
|
|
*/
|
1999-09-23 17:43:51 +00:00
|
|
|
#define LOG_BUFFER_SIZE (8 * 1024)
|
|
|
|
|
2024-08-13 18:20:26 +02:00
|
|
|
/*
|
|
|
|
* Private isc_log_t data type.
|
|
|
|
*/
|
|
|
|
typedef struct isc_log isc_log_t;
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1999-09-23 17:43:51 +00:00
|
|
|
* This is the structure that holds each named channel. A simple linked
|
|
|
|
* list chains all of the channels together, so an individual channel is
|
|
|
|
* found by doing strcmp()s with the names down the list. Their should
|
2009-01-18 00:50:21 +00:00
|
|
|
* be no performance penalty from this as it is expected that the number
|
1999-09-23 17:43:51 +00:00
|
|
|
* of named channels will be no more than a dozen or so, and name lookups
|
|
|
|
* from the head of the list are only done when isc_log_usechannel() is
|
|
|
|
* called, which should also be very infrequent.
|
|
|
|
*/
|
2000-03-01 17:31:56 +00:00
|
|
|
typedef struct isc_logchannel isc_logchannel_t;
|
|
|
|
|
1999-09-23 17:43:51 +00:00
|
|
|
struct isc_logchannel {
|
2000-03-01 17:31:56 +00:00
|
|
|
char *name;
|
|
|
|
unsigned int type;
|
|
|
|
int level;
|
|
|
|
unsigned int flags;
|
|
|
|
isc_logdestination_t destination;
|
|
|
|
ISC_LINK(isc_logchannel_t) link;
|
1999-09-23 17:43:51 +00:00
|
|
|
};
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
2000-05-24 02:33:16 +00:00
|
|
|
* The logchannellist structure associates categories and modules with
|
1999-09-23 17:43:51 +00:00
|
|
|
* channels. First the appropriate channellist is found based on the
|
|
|
|
* category, and then each structure in the linked list is checked for
|
|
|
|
* a matching module. It is expected that the number of channels
|
|
|
|
* associated with any given category will be very short, no more than
|
|
|
|
* three or four in the more unusual cases.
|
|
|
|
*/
|
|
|
|
typedef struct isc_logchannellist isc_logchannellist_t;
|
|
|
|
|
|
|
|
struct isc_logchannellist {
|
2024-08-14 13:25:50 +02:00
|
|
|
isc_logmodule_t module;
|
2000-03-01 17:31:56 +00:00
|
|
|
isc_logchannel_t *channel;
|
|
|
|
ISC_LINK(isc_logchannellist_t) link;
|
1999-10-25 19:55:06 +00:00
|
|
|
};
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1999-10-25 19:55:06 +00:00
|
|
|
* This structure is used to remember messages for pruning via
|
|
|
|
* isc_log_[v]write1().
|
|
|
|
*/
|
|
|
|
typedef struct isc_logmessage isc_logmessage_t;
|
|
|
|
|
|
|
|
struct isc_logmessage {
|
2000-02-26 19:57:02 +00:00
|
|
|
char *text;
|
|
|
|
isc_time_t time;
|
|
|
|
ISC_LINK(isc_logmessage_t) link;
|
|
|
|
};
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
2000-03-01 17:31:56 +00:00
|
|
|
* The isc_logconfig structure is used to store the configurable information
|
|
|
|
* about where messages are actually supposed to be sent -- the information
|
|
|
|
* that could changed based on some configuration file, as opposed to the
|
|
|
|
* the category/module specification of isc_log_[v]write[1] that is compiled
|
|
|
|
* into a program, or the debug_level which is dynamic state information.
|
2000-02-26 19:57:02 +00:00
|
|
|
*/
|
|
|
|
struct isc_logconfig {
|
|
|
|
unsigned int magic;
|
|
|
|
isc_log_t *lctx;
|
2000-03-01 17:31:56 +00:00
|
|
|
ISC_LIST(isc_logchannel_t) channels;
|
2024-08-14 13:25:50 +02:00
|
|
|
ISC_LIST(isc_logchannellist_t) channellists[ISC_LOGCATEGORY_MAX];
|
2020-04-01 11:50:32 +02:00
|
|
|
int_fast32_t highest_level;
|
2000-05-16 03:37:39 +00:00
|
|
|
char *tag;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool dynamic;
|
1999-09-23 17:43:51 +00:00
|
|
|
};
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1999-09-23 17:43:51 +00:00
|
|
|
* This isc_log structure provides the context for the isc_log functions.
|
2000-02-26 19:57:02 +00:00
|
|
|
* The log context locks itself in isc_log_doit, the internal backend to
|
1999-09-23 17:43:51 +00:00
|
|
|
* isc_log_write. The locking is necessary both to provide exclusive access
|
2009-01-05 23:20:22 +00:00
|
|
|
* to the buffer into which the message is formatted and to guard against
|
2024-08-13 14:46:42 +02:00
|
|
|
* competing threads trying to write to the same syslog resource.
|
2000-02-26 19:57:02 +00:00
|
|
|
*
|
2024-08-13 17:16:57 +02:00
|
|
|
* FIXME: We can remove the locking by using per-thread .buffer.
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
|
|
|
struct isc_log {
|
2000-02-26 19:57:02 +00:00
|
|
|
/* Not locked. */
|
1999-10-25 19:55:06 +00:00
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t *mctx;
|
2020-04-01 11:50:32 +02:00
|
|
|
atomic_int_fast32_t debug_level;
|
2023-12-19 10:41:15 +03:00
|
|
|
/* RCU-protected pointer */
|
2020-03-13 15:16:14 +01:00
|
|
|
isc_logconfig_t *logconfig;
|
2000-02-26 19:57:02 +00:00
|
|
|
isc_mutex_t lock;
|
|
|
|
/* Locked by isc_log lock. */
|
2000-03-01 17:31:56 +00:00
|
|
|
char buffer[LOG_BUFFER_SIZE];
|
2020-04-01 11:50:32 +02:00
|
|
|
atomic_bool dynamic;
|
|
|
|
atomic_int_fast32_t highest_level;
|
1999-09-23 17:43:51 +00:00
|
|
|
};
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1999-09-23 17:43:51 +00:00
|
|
|
* Used when ISC_LOG_PRINTLEVEL is enabled for a channel.
|
|
|
|
*/
|
2000-03-01 17:31:56 +00:00
|
|
|
static const char *log_level_strings[] = { "debug", "info", "notice",
|
2000-05-03 21:09:34 +00:00
|
|
|
"warning", "error", "critical" };
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1999-09-23 17:43:51 +00:00
|
|
|
* Used to convert ISC_LOG_* priorities into syslog priorities.
|
2000-03-01 17:31:56 +00:00
|
|
|
* XXXDCL This will need modification for NT.
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
|
|
|
static const int syslog_map[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE,
|
2000-05-03 21:09:34 +00:00
|
|
|
LOG_WARNING, LOG_ERR, LOG_CRIT };
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1999-09-23 17:43:51 +00:00
|
|
|
* When adding new categories, a corresponding ISC_LOGCATEGORY_foo
|
2000-02-26 19:57:02 +00:00
|
|
|
* definition needs to be added to <isc/log.h>.
|
1999-09-23 17:43:51 +00:00
|
|
|
*
|
|
|
|
* The default category is provided so that the internal default can
|
|
|
|
* be overridden. Since the default is always looked up as the first
|
|
|
|
* channellist in the log context, it must come first in isc_categories[].
|
|
|
|
*/
|
2024-08-14 13:25:50 +02:00
|
|
|
static const char *categories_description[] = {
|
|
|
|
/* libisc categories */
|
|
|
|
[ISC_LOGCATEGORY_DEFAULT] = "default",
|
|
|
|
[ISC_LOGCATEGORY_GENERAL] = "general",
|
|
|
|
[ISC_LOGCATEGORY_SSLKEYLOG] = "sslkeylog",
|
|
|
|
/* dns categories */
|
2024-06-14 11:23:53 +10:00
|
|
|
[DNS_LOGCATEGORY_CNAME] = "cname",
|
2024-08-14 13:25:50 +02:00
|
|
|
[DNS_LOGCATEGORY_DATABASE] = "database",
|
|
|
|
[DNS_LOGCATEGORY_DISPATCH] = "dispatch",
|
2024-06-14 11:23:53 +10:00
|
|
|
[DNS_LOGCATEGORY_DNSSEC] = "dnssec",
|
|
|
|
[DNS_LOGCATEGORY_DNSTAP] = "dnstap",
|
2024-08-14 13:25:50 +02:00
|
|
|
[DNS_LOGCATEGORY_EDNS_DISABLED] = "edns-disabled",
|
2024-06-14 11:23:53 +10:00
|
|
|
[DNS_LOGCATEGORY_LAME_SERVERS] = "lame-servers",
|
|
|
|
[DNS_LOGCATEGORY_NOTIFY] = "notify",
|
|
|
|
[DNS_LOGCATEGORY_NSID] = "nsid",
|
|
|
|
[DNS_LOGCATEGORY_RESOLVER] = "resolver",
|
2024-08-14 13:25:50 +02:00
|
|
|
[DNS_LOGCATEGORY_RPZ] = "rpz",
|
2024-06-14 11:23:53 +10:00
|
|
|
[DNS_LOGCATEGORY_RPZ_PASSTHRU] = "rpz-passthru",
|
2024-08-14 13:25:50 +02:00
|
|
|
[DNS_LOGCATEGORY_RRL] = "rate-limit",
|
2024-06-14 11:23:53 +10:00
|
|
|
[DNS_LOGCATEGORY_SECURITY] = "security",
|
2024-08-14 13:25:50 +02:00
|
|
|
[DNS_LOGCATEGORY_SPILL] = "spill",
|
2024-08-28 14:12:05 +10:00
|
|
|
[DNS_LOGCATEGORY_UPDATE_POLICY] = "update-policy",
|
2024-06-14 11:23:53 +10:00
|
|
|
[DNS_LOGCATEGORY_XFER_IN] = "xfer-in",
|
|
|
|
[DNS_LOGCATEGORY_XFER_OUT] = "xfer-out",
|
|
|
|
[DNS_LOGCATEGORY_ZONELOAD] = "zoneload",
|
|
|
|
[DNS_LOGCATEGORY_ZONEVERSION] = "zoneversion",
|
2024-08-14 13:25:50 +02:00
|
|
|
/* ns categories */
|
|
|
|
[NS_LOGCATEGORY_CLIENT] = "client",
|
2024-06-14 11:23:53 +10:00
|
|
|
[NS_LOGCATEGORY_DRA] = "dns-reporting-agent",
|
2024-08-14 13:25:50 +02:00
|
|
|
[NS_LOGCATEGORY_NETWORK] = "network",
|
|
|
|
[NS_LOGCATEGORY_QUERIES] = "queries",
|
|
|
|
[NS_LOGCATEGORY_QUERY_ERRORS] = "query-errors",
|
2019-06-19 16:20:24 +10:00
|
|
|
[NS_LOGCATEGORY_RESPONSES] = "responses",
|
2024-06-14 11:23:53 +10:00
|
|
|
[NS_LOGCATEGORY_SERVE_STALE] = "serve-stale",
|
|
|
|
[NS_LOGCATEGORY_TAT] = "trust-anchor-telemetry",
|
|
|
|
[NS_LOGCATEGORY_UPDATE] = "update",
|
|
|
|
[NS_LOGCATEGORY_UPDATE_SECURITY] = "update-security",
|
2024-08-14 13:25:50 +02:00
|
|
|
/* cfg categories */
|
|
|
|
[CFG_LOGCATEGORY_CONFIG] = "config",
|
|
|
|
/* named categories */
|
|
|
|
[NAMED_LOGCATEGORY_UNMATCHED] = "unmatched",
|
|
|
|
/* delv categories */
|
|
|
|
[DELV_LOGCATEGORY_DEFAULT] = "delv",
|
|
|
|
};
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
2021-06-10 08:06:48 +02:00
|
|
|
* See above comment for categories, and apply it to modules.
|
2000-05-03 21:09:34 +00:00
|
|
|
*/
|
2024-08-14 13:25:50 +02:00
|
|
|
static const char *modules_description[] = {
|
|
|
|
/* isc modules */
|
2024-08-14 14:38:07 +02:00
|
|
|
[ISC_LOGMODULE_DEFAULT] = "no_module",
|
2024-08-14 13:25:50 +02:00
|
|
|
[ISC_LOGMODULE_SOCKET] = "socket",
|
|
|
|
[ISC_LOGMODULE_TIME] = "time",
|
|
|
|
[ISC_LOGMODULE_INTERFACE] = "interface",
|
|
|
|
[ISC_LOGMODULE_TIMER] = "timer",
|
|
|
|
[ISC_LOGMODULE_FILE] = "file",
|
|
|
|
[ISC_LOGMODULE_NETMGR] = "netmgr",
|
|
|
|
[ISC_LOGMODULE_OTHER] = "other",
|
2024-08-08 11:26:27 +02:00
|
|
|
[ISC_LOGMODULE_CRYPTO] = "crypto",
|
2024-08-14 13:25:50 +02:00
|
|
|
/* dns modules */
|
|
|
|
[DNS_LOGMODULE_DB] = "dns/db",
|
|
|
|
[DNS_LOGMODULE_RDATA] = "dns/rdata",
|
|
|
|
[DNS_LOGMODULE_MASTER] = "dns/master",
|
|
|
|
[DNS_LOGMODULE_MESSAGE] = "dns/message",
|
|
|
|
[DNS_LOGMODULE_CACHE] = "dns/cache",
|
|
|
|
[DNS_LOGMODULE_CONFIG] = "dns/config",
|
|
|
|
[DNS_LOGMODULE_RESOLVER] = "dns/resolver",
|
|
|
|
[DNS_LOGMODULE_ZONE] = "dns/zone",
|
|
|
|
[DNS_LOGMODULE_JOURNAL] = "dns/journal",
|
|
|
|
[DNS_LOGMODULE_ADB] = "dns/adb",
|
|
|
|
[DNS_LOGMODULE_XFER_IN] = "dns/xfrin",
|
|
|
|
[DNS_LOGMODULE_XFER_OUT] = "dns/xfrout",
|
|
|
|
[DNS_LOGMODULE_ACL] = "dns/acl",
|
|
|
|
[DNS_LOGMODULE_VALIDATOR] = "dns/validator",
|
|
|
|
[DNS_LOGMODULE_DISPATCH] = "dns/dispatch",
|
|
|
|
[DNS_LOGMODULE_REQUEST] = "dns/request",
|
|
|
|
[DNS_LOGMODULE_MASTERDUMP] = "dns/masterdump",
|
|
|
|
[DNS_LOGMODULE_TSIG] = "dns/tsig",
|
|
|
|
[DNS_LOGMODULE_TKEY] = "dns/tkey",
|
|
|
|
[DNS_LOGMODULE_SDB] = "dns/sdb",
|
|
|
|
[DNS_LOGMODULE_DIFF] = "dns/diff",
|
|
|
|
[DNS_LOGMODULE_HINTS] = "dns/hints",
|
|
|
|
[DNS_LOGMODULE_UNUSED1] = "dns/unused1",
|
|
|
|
[DNS_LOGMODULE_DLZ] = "dns/dlz",
|
|
|
|
[DNS_LOGMODULE_DNSSEC] = "dns/dnssec",
|
|
|
|
[DNS_LOGMODULE_CRYPTO] = "dns/crypto",
|
|
|
|
[DNS_LOGMODULE_PACKETS] = "dns/packets",
|
|
|
|
[DNS_LOGMODULE_NTA] = "dns/nta",
|
|
|
|
[DNS_LOGMODULE_DYNDB] = "dns/dyndb",
|
|
|
|
[DNS_LOGMODULE_DNSTAP] = "dns/dnstap",
|
|
|
|
[DNS_LOGMODULE_SSU] = "dns/ssu",
|
|
|
|
[DNS_LOGMODULE_QP] = "dns/qp",
|
2024-12-10 19:11:26 -08:00
|
|
|
[DNS_LOGMODULE_RPZ] = "dns/rpz",
|
|
|
|
[DNS_LOGMODULE_CATZ] = "dns/catz",
|
2024-08-14 13:25:50 +02:00
|
|
|
/* ns modules */
|
|
|
|
[NS_LOGMODULE_CLIENT] = "ns/client",
|
|
|
|
[NS_LOGMODULE_QUERY] = "ns/query",
|
|
|
|
[NS_LOGMODULE_INTERFACEMGR] = "ns/interfacemgr",
|
|
|
|
[NS_LOGMODULE_UPDATE] = "ns/update",
|
|
|
|
[NS_LOGMODULE_XFER_IN] = "ns/xfer-in",
|
|
|
|
[NS_LOGMODULE_XFER_OUT] = "ns/xfer-out",
|
|
|
|
[NS_LOGMODULE_NOTIFY] = "ns/notify",
|
|
|
|
[NS_LOGMODULE_HOOKS] = "ns/hooks",
|
|
|
|
/* cfg modules */
|
|
|
|
[CFG_LOGMODULE_PARSER] = "isccfg/parser",
|
|
|
|
/* named modules */
|
|
|
|
[NAMED_LOGMODULE_MAIN] = "main",
|
|
|
|
[NAMED_LOGMODULE_SERVER] = "server",
|
|
|
|
[NAMED_LOGMODULE_CONTROL] = "control",
|
|
|
|
/* delv modules */
|
|
|
|
[DELV_LOGMODULE_DEFAULT] = "delv",
|
|
|
|
};
|
2000-05-03 21:09:34 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
2000-04-28 17:29:25 +00:00
|
|
|
* This essentially constant structure must be filled in at run time,
|
2000-03-01 17:31:56 +00:00
|
|
|
* because its channel member is pointed to a channel that is created
|
|
|
|
* dynamically with isc_log_createchannel.
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
2000-04-28 17:29:25 +00:00
|
|
|
static isc_logchannellist_t default_channel;
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
2000-05-03 21:09:34 +00:00
|
|
|
* libisc logs to this context.
|
|
|
|
*/
|
2024-08-13 14:46:42 +02:00
|
|
|
static isc_log_t *isc__lctx = NULL;
|
2000-05-03 21:09:34 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1999-09-23 17:43:51 +00:00
|
|
|
* Forward declarations.
|
|
|
|
*/
|
2020-03-18 14:17:55 +11:00
|
|
|
static void
|
2024-08-14 13:25:50 +02:00
|
|
|
assignchannel(isc_logconfig_t *lcfg, const isc_logcategory_t category,
|
|
|
|
const isc_logmodule_t module, isc_logchannel_t *channel);
|
2000-02-26 19:57:02 +00:00
|
|
|
|
2020-04-01 11:50:32 +02:00
|
|
|
static void
|
2024-08-13 18:20:26 +02:00
|
|
|
sync_highest_level(isc_logconfig_t *lcfg);
|
2020-04-01 11:50:32 +02:00
|
|
|
|
2016-11-30 10:55:21 +11:00
|
|
|
static isc_result_t
|
|
|
|
greatest_version(isc_logfile_t *file, int versions, int *greatest);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
1999-10-25 19:55:06 +00:00
|
|
|
static void
|
2024-08-14 13:25:50 +02:00
|
|
|
isc_log_doit(isc_logcategory_t category, isc_logmodule_t module, int level,
|
2024-08-13 18:20:26 +02:00
|
|
|
const char *format, va_list args) ISC_FORMAT_PRINTF(4, 0);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*@{*/
|
|
|
|
/*!
|
1999-09-23 17:43:51 +00:00
|
|
|
* Convenience macros.
|
|
|
|
*/
|
|
|
|
|
2000-12-23 19:23:48 +00:00
|
|
|
#define FACILITY(channel) (channel->destination.facility)
|
|
|
|
#define FILE_NAME(channel) (channel->destination.file.name)
|
|
|
|
#define FILE_STREAM(channel) (channel->destination.file.stream)
|
|
|
|
#define FILE_VERSIONS(channel) (channel->destination.file.versions)
|
2017-03-08 23:20:40 -08:00
|
|
|
#define FILE_SUFFIX(channel) (channel->destination.file.suffix)
|
2000-12-23 19:23:48 +00:00
|
|
|
#define FILE_MAXSIZE(channel) (channel->destination.file.maximum_size)
|
|
|
|
#define FILE_MAXREACHED(channel) (channel->destination.file.maximum_reached)
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*@}*/
|
1999-09-23 17:43:51 +00:00
|
|
|
/****
|
|
|
|
**** Public interfaces.
|
|
|
|
****/
|
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
void
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_logconfig_create(isc_logconfig_t **lcfgp) {
|
2024-08-13 14:46:42 +02:00
|
|
|
REQUIRE(lcfgp != NULL && *lcfgp == NULL);
|
|
|
|
REQUIRE(VALID_CONTEXT(isc__lctx));
|
2000-02-26 19:57:02 +00:00
|
|
|
|
2000-11-24 01:37:26 +00:00
|
|
|
int level = ISC_LOG_INFO;
|
2000-02-26 19:57:02 +00:00
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
isc_logconfig_t *lcfg = isc_mem_get(isc__lctx->mctx, sizeof(*lcfg));
|
2000-02-26 19:57:02 +00:00
|
|
|
|
2023-12-19 10:41:15 +03:00
|
|
|
*lcfg = (isc_logconfig_t){
|
|
|
|
.magic = LCFG_MAGIC,
|
2024-08-13 14:46:42 +02:00
|
|
|
.lctx = isc__lctx,
|
2023-12-19 10:41:15 +03:00
|
|
|
.channels = ISC_LIST_INITIALIZER,
|
|
|
|
.highest_level = level,
|
|
|
|
};
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the default channels:
|
2020-04-01 11:50:32 +02:00
|
|
|
* default_syslog, default_stderr, default_debug and null.
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
2020-03-18 14:17:55 +11:00
|
|
|
isc_log_createchannel(lcfg, "default_syslog", ISC_LOG_TOSYSLOG, level,
|
2024-08-14 13:25:50 +02:00
|
|
|
ISC_LOGDESTINATION_SYSLOG(LOG_DAEMON), 0);
|
2020-03-18 14:17:55 +11:00
|
|
|
|
|
|
|
isc_log_createchannel(lcfg, "default_stderr", ISC_LOG_TOFILEDESC, level,
|
2024-08-14 13:25:50 +02:00
|
|
|
ISC_LOGDESTINATION_STDERR, ISC_LOG_PRINTTIME);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
/*
|
|
|
|
* Set the default category's channel to default_stderr,
|
|
|
|
* which is at the head of the channels list because it was
|
|
|
|
* just created.
|
|
|
|
*/
|
|
|
|
default_channel.channel = ISC_LIST_HEAD(lcfg->channels);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
isc_log_createchannel(lcfg, "default_debug", ISC_LOG_TOFILEDESC,
|
2024-08-14 13:25:50 +02:00
|
|
|
ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR,
|
|
|
|
ISC_LOG_PRINTTIME);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
isc_log_createchannel(lcfg, "null", ISC_LOG_TONULL, ISC_LOG_DYNAMIC,
|
|
|
|
NULL, 0);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
*lcfgp = lcfg;
|
2000-02-26 19:57:02 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 15:52:51 +02:00
|
|
|
isc_logconfig_t *
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_logconfig_get(void) {
|
2024-08-13 15:52:51 +02:00
|
|
|
REQUIRE(VALID_CONTEXT(isc__lctx));
|
|
|
|
|
|
|
|
return rcu_dereference(isc__lctx->logconfig);
|
|
|
|
}
|
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
void
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_logconfig_set(isc_logconfig_t *lcfg) {
|
2024-08-13 14:46:42 +02:00
|
|
|
REQUIRE(VALID_CONTEXT(isc__lctx));
|
2000-02-26 19:57:02 +00:00
|
|
|
REQUIRE(VALID_CONFIG(lcfg));
|
2024-08-13 14:46:42 +02:00
|
|
|
REQUIRE(lcfg->lctx == isc__lctx);
|
2000-02-26 19:57:02 +00:00
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
isc_logconfig_t *old_cfg = rcu_xchg_pointer(&isc__lctx->logconfig,
|
|
|
|
lcfg);
|
2024-08-13 18:20:26 +02:00
|
|
|
sync_highest_level(lcfg);
|
2023-12-19 10:41:15 +03:00
|
|
|
synchronize_rcu();
|
2020-03-13 15:16:14 +01:00
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
if (old_cfg != NULL) {
|
|
|
|
isc_logconfig_destroy(&old_cfg);
|
2000-02-26 19:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_logconfig_destroy(isc_logconfig_t **lcfgp) {
|
2025-03-22 15:26:16 -07:00
|
|
|
isc_logconfig_t *lcfg = NULL;
|
|
|
|
isc_mem_t *mctx = NULL;
|
|
|
|
char *filename = NULL;
|
2000-02-26 19:57:02 +00:00
|
|
|
|
|
|
|
REQUIRE(lcfgp != NULL && VALID_CONFIG(*lcfgp));
|
|
|
|
|
|
|
|
lcfg = *lcfgp;
|
2020-02-08 04:37:54 -08:00
|
|
|
*lcfgp = NULL;
|
2000-02-26 19:57:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function cannot be called with a logconfig that is in
|
|
|
|
* use by a log context.
|
|
|
|
*/
|
2020-03-13 15:16:14 +01:00
|
|
|
REQUIRE(lcfg->lctx != NULL);
|
|
|
|
|
2023-12-19 10:41:15 +03:00
|
|
|
rcu_read_lock();
|
|
|
|
REQUIRE(rcu_dereference(lcfg->lctx->logconfig) != lcfg);
|
|
|
|
rcu_read_unlock();
|
2000-02-26 19:57:02 +00:00
|
|
|
|
|
|
|
mctx = lcfg->lctx->mctx;
|
|
|
|
|
2025-05-23 13:02:22 -07:00
|
|
|
ISC_LIST_FOREACH (lcfg->channels, channel, link) {
|
1999-09-23 17:43:51 +00:00
|
|
|
if (channel->type == ISC_LOG_TOFILE) {
|
2000-06-01 17:20:56 +00:00
|
|
|
/*
|
|
|
|
* The filename for the channel may have ultimately
|
|
|
|
* started its life in user-land as a const string,
|
|
|
|
* but in isc_log_createchannel it gets copied
|
|
|
|
* into writable memory and is not longer truly const.
|
|
|
|
*/
|
2023-03-30 22:34:12 +02:00
|
|
|
filename = UNCONST(FILE_NAME(channel));
|
2000-06-01 17:20:56 +00:00
|
|
|
isc_mem_free(mctx, filename);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2000-06-01 17:20:56 +00:00
|
|
|
if (FILE_STREAM(channel) != NULL) {
|
|
|
|
(void)fclose(FILE_STREAM(channel));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_mem_free(mctx, channel->name);
|
|
|
|
isc_mem_put(mctx, channel, sizeof(*channel));
|
|
|
|
}
|
|
|
|
|
2024-08-14 13:25:50 +02:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(lcfg->channellists); i++) {
|
2025-05-23 13:02:22 -07:00
|
|
|
ISC_LIST_FOREACH (lcfg->channellists[i], item, link) {
|
2000-03-01 17:31:56 +00:00
|
|
|
ISC_LIST_UNLINK(lcfg->channellists[i], item, link);
|
|
|
|
isc_mem_put(mctx, item, sizeof(*item));
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
lcfg->dynamic = false;
|
2000-06-02 18:15:45 +00:00
|
|
|
if (lcfg->tag != NULL) {
|
|
|
|
isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-04-06 20:32:31 +00:00
|
|
|
lcfg->highest_level = 0;
|
2000-02-26 19:57:02 +00:00
|
|
|
lcfg->magic = 0;
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2000-02-26 19:57:02 +00:00
|
|
|
isc_mem_put(mctx, lcfg, sizeof(*lcfg));
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 13:25:50 +02:00
|
|
|
isc_logcategory_t
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_log_categorybyname(const char *name) {
|
2024-08-13 14:46:42 +02:00
|
|
|
REQUIRE(VALID_CONTEXT(isc__lctx));
|
2000-03-04 16:41:14 +00:00
|
|
|
REQUIRE(name != NULL);
|
2000-03-04 00:43:40 +00:00
|
|
|
|
2024-08-14 13:25:50 +02:00
|
|
|
for (isc_logcategory_t category = 0; category < ISC_LOGCATEGORY_MAX;
|
|
|
|
category++)
|
|
|
|
{
|
|
|
|
if (strcmp(categories_description[category], name) == 0) {
|
|
|
|
return category;
|
2000-06-23 17:52:20 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2024-08-14 13:25:50 +02:00
|
|
|
return ISC_LOGCATEGORY_INVALID;
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
void
|
2000-02-26 19:57:02 +00:00
|
|
|
isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
|
|
|
|
unsigned int type, int level,
|
2000-06-01 17:20:56 +00:00
|
|
|
const isc_logdestination_t *destination,
|
|
|
|
unsigned int flags) {
|
1999-09-23 17:43:51 +00:00
|
|
|
isc_logchannel_t *channel;
|
2000-02-26 19:57:02 +00:00
|
|
|
isc_mem_t *mctx;
|
2014-10-30 11:37:05 +11:00
|
|
|
unsigned int permitted = ISC_LOG_PRINTALL | ISC_LOG_DEBUGONLY |
|
2016-11-22 23:34:47 -08:00
|
|
|
ISC_LOG_BUFFERED | ISC_LOG_ISO8601 |
|
2024-09-25 16:46:16 +02:00
|
|
|
ISC_LOG_UTC | ISC_LOG_TZINFO;
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2000-02-26 19:57:02 +00:00
|
|
|
REQUIRE(VALID_CONFIG(lcfg));
|
1999-09-23 17:43:51 +00:00
|
|
|
REQUIRE(name != NULL);
|
|
|
|
REQUIRE(type == ISC_LOG_TOSYSLOG || type == ISC_LOG_TOFILE ||
|
|
|
|
type == ISC_LOG_TOFILEDESC || type == ISC_LOG_TONULL);
|
|
|
|
REQUIRE(destination != NULL || type == ISC_LOG_TONULL);
|
|
|
|
REQUIRE(level >= ISC_LOG_CRITICAL);
|
2014-10-30 11:37:05 +11:00
|
|
|
REQUIRE((flags & ~permitted) == 0);
|
2024-09-25 16:46:16 +02:00
|
|
|
REQUIRE(!(flags & ISC_LOG_UTC) || !(flags & ISC_LOG_TZINFO));
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2024-08-14 14:38:07 +02:00
|
|
|
/* FIXME: find duplicate names? */
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2000-02-26 19:57:02 +00:00
|
|
|
mctx = lcfg->lctx->mctx;
|
|
|
|
|
2000-03-01 17:31:56 +00:00
|
|
|
channel = isc_mem_get(mctx, sizeof(*channel));
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2000-02-26 19:57:02 +00:00
|
|
|
channel->name = isc_mem_strdup(mctx, name);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
channel->type = type;
|
|
|
|
channel->level = level;
|
|
|
|
channel->flags = flags;
|
2000-10-20 02:21:58 +00:00
|
|
|
ISC_LINK_INIT(channel, link);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case ISC_LOG_TOSYSLOG:
|
|
|
|
FACILITY(channel) = destination->facility;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ISC_LOG_TOFILE:
|
|
|
|
/*
|
|
|
|
* The file name is copied because greatest_version wants
|
|
|
|
* to scribble on it, so it needs to be definitely in
|
|
|
|
* writable memory.
|
|
|
|
*/
|
2019-07-23 11:12:11 -04:00
|
|
|
FILE_NAME(channel) = isc_mem_strdup(mctx,
|
|
|
|
destination->file.name);
|
1999-09-23 17:43:51 +00:00
|
|
|
FILE_STREAM(channel) = NULL;
|
|
|
|
FILE_VERSIONS(channel) = destination->file.versions;
|
2017-03-08 23:20:40 -08:00
|
|
|
FILE_SUFFIX(channel) = destination->file.suffix;
|
2000-12-23 19:23:48 +00:00
|
|
|
FILE_MAXSIZE(channel) = destination->file.maximum_size;
|
2018-04-17 08:29:14 -07:00
|
|
|
FILE_MAXREACHED(channel) = false;
|
1999-09-23 17:43:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ISC_LOG_TOFILEDESC:
|
|
|
|
FILE_NAME(channel) = NULL;
|
|
|
|
FILE_STREAM(channel) = destination->file.stream;
|
|
|
|
FILE_MAXSIZE(channel) = 0;
|
|
|
|
FILE_VERSIONS(channel) = ISC_LOG_ROLLNEVER;
|
2017-03-08 23:20:40 -08:00
|
|
|
FILE_SUFFIX(channel) = isc_log_rollsuffix_increment;
|
1999-09-23 17:43:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ISC_LOG_TONULL:
|
|
|
|
/* Nothing. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-10-11 12:50:17 +02:00
|
|
|
UNREACHABLE();
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-03-01 17:31:56 +00:00
|
|
|
ISC_LIST_PREPEND(lcfg->channels, channel, link);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If default_stderr was redefined, make the default category
|
|
|
|
* point to the new default_stderr.
|
|
|
|
*/
|
|
|
|
if (strcmp(name, "default_stderr") == 0) {
|
|
|
|
default_channel.channel = channel;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2000-02-26 19:57:02 +00:00
|
|
|
isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
|
2024-08-14 13:25:50 +02:00
|
|
|
const isc_logcategory_t category,
|
|
|
|
const isc_logmodule_t module) {
|
2025-03-20 22:25:56 -07:00
|
|
|
isc_logchannel_t *channel = NULL;
|
|
|
|
|
2000-02-26 19:57:02 +00:00
|
|
|
REQUIRE(VALID_CONFIG(lcfg));
|
1999-09-23 17:43:51 +00:00
|
|
|
REQUIRE(name != NULL);
|
2024-08-14 14:38:07 +02:00
|
|
|
REQUIRE(category >= ISC_LOGCATEGORY_DEFAULT &&
|
|
|
|
category < ISC_LOGCATEGORY_MAX);
|
|
|
|
REQUIRE(module >= ISC_LOGMODULE_DEFAULT && module < ISC_LOGMODULE_MAX);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2025-03-20 22:25:56 -07:00
|
|
|
ISC_LIST_FOREACH (lcfg->channels, c, link) {
|
|
|
|
if (strcmp(name, c->name) == 0) {
|
|
|
|
channel = c;
|
1999-09-23 17:43:51 +00:00
|
|
|
break;
|
2020-02-13 18:16:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-09-23 17:43:51 +00:00
|
|
|
if (channel == NULL) {
|
|
|
|
return ISC_R_NOTFOUND;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2024-08-14 14:38:07 +02:00
|
|
|
if (category != ISC_LOGCATEGORY_DEFAULT) {
|
2024-08-14 13:25:50 +02:00
|
|
|
assignchannel(lcfg, category, module, channel);
|
1999-09-23 17:43:51 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Assign to all categories. Note that this includes
|
|
|
|
* the default channel.
|
|
|
|
*/
|
2024-08-14 14:38:07 +02:00
|
|
|
for (size_t i = ISC_LOGCATEGORY_DEFAULT;
|
|
|
|
i < ISC_LOGCATEGORY_MAX; i++)
|
|
|
|
{
|
2020-03-18 14:17:55 +11:00
|
|
|
assignchannel(lcfg, i, module, channel);
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2020-04-01 11:50:32 +02:00
|
|
|
/*
|
|
|
|
* Update the highest logging level, if the current lcfg is in use.
|
|
|
|
*/
|
2023-12-19 10:41:15 +03:00
|
|
|
rcu_read_lock();
|
|
|
|
if (rcu_dereference(lcfg->lctx->logconfig) == lcfg) {
|
2024-08-13 18:20:26 +02:00
|
|
|
sync_highest_level(lcfg);
|
2020-04-01 11:50:32 +02:00
|
|
|
}
|
2023-12-19 10:41:15 +03:00
|
|
|
rcu_read_unlock();
|
2020-04-01 11:50:32 +02:00
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
return ISC_R_SUCCESS;
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 14:38:07 +02:00
|
|
|
void
|
|
|
|
isc_log_createandusechannel(isc_logconfig_t *lcfg, const char *name,
|
|
|
|
unsigned int type, int level,
|
|
|
|
const isc_logdestination_t *destination,
|
|
|
|
unsigned int flags,
|
|
|
|
const isc_logcategory_t category,
|
|
|
|
const isc_logmodule_t module) {
|
|
|
|
isc_log_createchannel(lcfg, name, type, level, destination, flags);
|
|
|
|
RUNTIME_CHECK(isc_log_usechannel(lcfg, name, category, module) ==
|
|
|
|
ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-09-23 17:43:51 +00:00
|
|
|
void
|
2024-08-14 13:25:50 +02:00
|
|
|
isc_log_write(isc_logcategory_t category, isc_logmodule_t module, int level,
|
2024-08-13 18:20:26 +02:00
|
|
|
const char *format, ...) {
|
1999-09-23 17:43:51 +00:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
/*
|
1999-10-25 19:55:06 +00:00
|
|
|
* Contract checking is done in isc_log_doit().
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
va_start(args, format);
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_log_doit(category, module, level, format, args);
|
1999-09-23 17:43:51 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
1999-10-25 19:55:06 +00:00
|
|
|
void
|
2024-08-14 13:25:50 +02:00
|
|
|
isc_log_vwrite(isc_logcategory_t category, isc_logmodule_t module, int level,
|
2024-08-13 18:20:26 +02:00
|
|
|
const char *format, va_list args) {
|
1999-10-25 19:55:06 +00:00
|
|
|
/*
|
|
|
|
* Contract checking is done in isc_log_doit().
|
|
|
|
*/
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_log_doit(category, module, level, format, args);
|
1999-10-25 19:55:06 +00:00
|
|
|
}
|
|
|
|
|
1999-09-23 17:43:51 +00:00
|
|
|
void
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_log_setdebuglevel(unsigned int level) {
|
2024-08-13 14:46:42 +02:00
|
|
|
REQUIRE(VALID_CONTEXT(isc__lctx));
|
2002-06-03 03:39:56 +00:00
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
atomic_store_release(&isc__lctx->debug_level, level);
|
2001-06-08 02:57:13 +00:00
|
|
|
/*
|
|
|
|
* Close ISC_LOG_DEBUGONLY channels if level is zero.
|
|
|
|
*/
|
2020-03-13 15:16:14 +01:00
|
|
|
if (level == 0) {
|
2023-12-19 10:41:15 +03:00
|
|
|
rcu_read_lock();
|
2024-08-13 14:46:42 +02:00
|
|
|
isc_logconfig_t *lcfg = rcu_dereference(isc__lctx->logconfig);
|
2019-12-20 19:29:18 -03:00
|
|
|
if (lcfg != NULL) {
|
2024-08-13 14:46:42 +02:00
|
|
|
LOCK(&isc__lctx->lock);
|
2025-03-22 15:26:16 -07:00
|
|
|
ISC_LIST_FOREACH (lcfg->channels, channel, link) {
|
2019-12-20 19:29:18 -03:00
|
|
|
if (channel->type == ISC_LOG_TOFILE &&
|
|
|
|
(channel->flags & ISC_LOG_DEBUGONLY) != 0 &&
|
|
|
|
FILE_STREAM(channel) != NULL)
|
2020-02-13 18:16:57 +01:00
|
|
|
{
|
|
|
|
(void)fclose(FILE_STREAM(channel));
|
|
|
|
FILE_STREAM(channel) = NULL;
|
|
|
|
}
|
2001-06-08 02:57:13 +00:00
|
|
|
}
|
2024-08-13 14:46:42 +02:00
|
|
|
UNLOCK(&isc__lctx->lock);
|
2020-02-13 18:16:57 +01:00
|
|
|
}
|
2023-12-19 10:41:15 +03:00
|
|
|
rcu_read_unlock();
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_log_getdebuglevel(void) {
|
2024-08-13 14:46:42 +02:00
|
|
|
REQUIRE(VALID_CONTEXT(isc__lctx));
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
return atomic_load_acquire(&isc__lctx->debug_level);
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
void
|
2000-06-01 17:20:56 +00:00
|
|
|
isc_log_settag(isc_logconfig_t *lcfg, const char *tag) {
|
2000-05-16 03:37:39 +00:00
|
|
|
REQUIRE(VALID_CONFIG(lcfg));
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-06-01 17:20:56 +00:00
|
|
|
if (tag != NULL && *tag != '\0') {
|
2002-01-09 06:16:10 +00:00
|
|
|
if (lcfg->tag != NULL) {
|
|
|
|
isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-06-01 17:20:56 +00:00
|
|
|
lcfg->tag = isc_mem_strdup(lcfg->lctx->mctx, tag);
|
|
|
|
} else {
|
|
|
|
if (lcfg->tag != NULL) {
|
|
|
|
isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-06-01 17:20:56 +00:00
|
|
|
}
|
2000-05-16 03:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
isc_log_gettag(isc_logconfig_t *lcfg) {
|
|
|
|
REQUIRE(VALID_CONFIG(lcfg));
|
|
|
|
|
|
|
|
return lcfg->tag;
|
|
|
|
}
|
|
|
|
|
1999-09-23 17:43:51 +00:00
|
|
|
/* XXXDCL NT -- This interface will assuredly be changing. */
|
|
|
|
void
|
|
|
|
isc_log_opensyslog(const char *tag, int options, int facility) {
|
2001-11-30 01:59:49 +00:00
|
|
|
(void)openlog(tag, options, facility);
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_log_closefilelogs(void) {
|
2024-08-13 14:46:42 +02:00
|
|
|
REQUIRE(VALID_CONTEXT(isc__lctx));
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2023-12-19 10:41:15 +03:00
|
|
|
rcu_read_lock();
|
2024-08-13 14:46:42 +02:00
|
|
|
isc_logconfig_t *lcfg = rcu_dereference(isc__lctx->logconfig);
|
2019-12-20 19:29:18 -03:00
|
|
|
if (lcfg != NULL) {
|
2024-08-13 14:46:42 +02:00
|
|
|
LOCK(&isc__lctx->lock);
|
2025-03-20 22:25:56 -07:00
|
|
|
ISC_LIST_FOREACH (lcfg->channels, channel, link) {
|
2019-12-20 19:29:18 -03:00
|
|
|
if (channel->type == ISC_LOG_TOFILE &&
|
2022-11-02 19:33:14 +01:00
|
|
|
FILE_STREAM(channel) != NULL)
|
|
|
|
{
|
2020-02-13 18:16:57 +01:00
|
|
|
(void)fclose(FILE_STREAM(channel));
|
|
|
|
FILE_STREAM(channel) = NULL;
|
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
2024-08-13 14:46:42 +02:00
|
|
|
UNLOCK(&isc__lctx->lock);
|
2020-02-13 18:16:57 +01:00
|
|
|
}
|
2023-12-19 10:41:15 +03:00
|
|
|
rcu_read_unlock();
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
**** Internal functions
|
|
|
|
****/
|
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
static void
|
2024-08-14 13:25:50 +02:00
|
|
|
assignchannel(isc_logconfig_t *lcfg, const isc_logcategory_t category,
|
|
|
|
const isc_logmodule_t module, isc_logchannel_t *channel) {
|
2000-02-26 19:57:02 +00:00
|
|
|
REQUIRE(VALID_CONFIG(lcfg));
|
2024-08-13 14:46:42 +02:00
|
|
|
REQUIRE(channel != NULL);
|
2000-02-26 19:57:02 +00:00
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
isc_log_t *lctx = lcfg->lctx;
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2024-08-14 14:38:07 +02:00
|
|
|
REQUIRE(category >= ISC_LOGCATEGORY_DEFAULT &&
|
2024-08-14 13:25:50 +02:00
|
|
|
category < ISC_LOGCATEGORY_MAX);
|
2024-08-14 14:38:07 +02:00
|
|
|
REQUIRE(module >= ISC_LOGMODULE_DEFAULT && module < ISC_LOGMODULE_MAX);
|
2000-02-26 19:57:02 +00:00
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
isc_logchannellist_t *new_item = isc_mem_get(lctx->mctx,
|
|
|
|
sizeof(*new_item));
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
new_item->channel = channel;
|
|
|
|
new_item->module = module;
|
2024-08-14 13:25:50 +02:00
|
|
|
ISC_LIST_INITANDPREPEND(lcfg->channellists[category], new_item, link);
|
2000-02-26 19:57:02 +00:00
|
|
|
|
2000-04-06 20:32:31 +00:00
|
|
|
/*
|
|
|
|
* Remember the highest logging level set by any channel in the
|
|
|
|
* logging config, so isc_log_doit() can quickly return if the
|
|
|
|
* message is too high to be logged by any channel.
|
|
|
|
*/
|
2000-04-11 18:22:34 +00:00
|
|
|
if (channel->type != ISC_LOG_TONULL) {
|
2000-04-06 20:32:31 +00:00
|
|
|
if (lcfg->highest_level < channel->level) {
|
|
|
|
lcfg->highest_level = channel->level;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-04-06 20:32:31 +00:00
|
|
|
if (channel->level == ISC_LOG_DYNAMIC) {
|
2018-04-17 08:29:14 -07:00
|
|
|
lcfg->dynamic = true;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-04-06 20:32:31 +00:00
|
|
|
}
|
2000-02-26 19:57:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 11:50:32 +02:00
|
|
|
static void
|
2024-08-13 18:20:26 +02:00
|
|
|
sync_highest_level(isc_logconfig_t *lcfg) {
|
2024-08-13 14:46:42 +02:00
|
|
|
atomic_store(&isc__lctx->highest_level, lcfg->highest_level);
|
|
|
|
atomic_store(&isc__lctx->dynamic, lcfg->dynamic);
|
2020-04-01 11:50:32 +02:00
|
|
|
}
|
|
|
|
|
2001-04-25 23:59:44 +00:00
|
|
|
static isc_result_t
|
2016-11-30 10:55:21 +11:00
|
|
|
greatest_version(isc_logfile_t *file, int versions, int *greatestp) {
|
2023-04-14 13:53:41 +10:00
|
|
|
char *digit_end;
|
|
|
|
char dirbuf[PATH_MAX + 1];
|
|
|
|
const char *bname;
|
|
|
|
const char *dirname = ".";
|
1999-09-23 17:43:51 +00:00
|
|
|
int version, greatest = -1;
|
|
|
|
isc_dir_t dir;
|
|
|
|
isc_result_t result;
|
2023-04-14 13:53:41 +10:00
|
|
|
size_t bnamelen;
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2023-04-14 13:53:41 +10:00
|
|
|
bname = strrchr(file->name, '/');
|
2016-06-28 21:25:30 -04:00
|
|
|
if (bname != NULL) {
|
2023-04-14 13:53:41 +10:00
|
|
|
/*
|
|
|
|
* Copy the complete file name to dirbuf.
|
|
|
|
*/
|
|
|
|
size_t len = strlcpy(dirbuf, file->name, sizeof(dirbuf));
|
|
|
|
if (len >= sizeof(dirbuf)) {
|
|
|
|
result = ISC_R_NOSPACE;
|
|
|
|
syslog(LOG_ERR, "unable to remove log files: %s",
|
|
|
|
isc_result_totext(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Truncate after trailing '/' so the code works for
|
|
|
|
* files in the root directory.
|
|
|
|
*/
|
|
|
|
bname++;
|
|
|
|
dirbuf[bname - file->name] = '\0';
|
|
|
|
dirname = dirbuf;
|
1999-09-23 17:43:51 +00:00
|
|
|
} else {
|
2023-04-14 13:53:41 +10:00
|
|
|
bname = file->name;
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
2016-06-28 21:25:30 -04:00
|
|
|
bnamelen = strlen(bname);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
isc_dir_init(&dir);
|
1999-10-31 19:09:23 +00:00
|
|
|
result = isc_dir_open(&dir, dirname);
|
2001-04-25 23:59:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return if the directory open failed.
|
|
|
|
*/
|
1999-09-23 17:43:51 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2023-04-14 13:53:41 +10:00
|
|
|
return result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
|
2016-06-28 21:25:30 -04:00
|
|
|
if (dir.entry.length > bnamelen &&
|
|
|
|
strncmp(dir.entry.name, bname, bnamelen) == 0 &&
|
2016-07-13 01:12:47 -07:00
|
|
|
dir.entry.name[bnamelen] == '.')
|
|
|
|
{
|
2016-06-28 21:25:30 -04:00
|
|
|
version = strtol(&dir.entry.name[bnamelen + 1],
|
2001-04-28 01:08:07 +00:00
|
|
|
&digit_end, 10);
|
2016-11-30 10:55:21 +11:00
|
|
|
/*
|
|
|
|
* Remove any backup files that exceed versions.
|
|
|
|
*/
|
|
|
|
if (*digit_end == '\0' && version >= versions) {
|
2024-08-15 09:23:31 +02:00
|
|
|
int n = dirfd(dir.handle);
|
|
|
|
if (n >= 0) {
|
|
|
|
n = unlinkat(n, dir.entry.name, 0);
|
|
|
|
}
|
2023-04-14 13:53:41 +10:00
|
|
|
if (n < 0) {
|
|
|
|
result = isc_errno_toresult(errno);
|
|
|
|
if (result != ISC_R_SUCCESS &&
|
|
|
|
result != ISC_R_FILENOTFOUND)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"unable to remove log "
|
|
|
|
"file '%s%s': %s",
|
|
|
|
bname == file->name
|
|
|
|
? ""
|
|
|
|
: dirname,
|
|
|
|
dir.entry.name,
|
|
|
|
isc_result_totext(
|
|
|
|
result));
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-11-30 10:55:21 +11:00
|
|
|
} else if (*digit_end == '\0' && version > greatest) {
|
1999-09-23 17:43:51 +00:00
|
|
|
greatest = version;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
2000-08-31 20:58:15 +00:00
|
|
|
isc_dir_close(&dir);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2016-11-30 10:55:21 +11:00
|
|
|
*greatestp = greatest;
|
2023-04-14 13:53:41 +10:00
|
|
|
return ISC_R_SUCCESS;
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 11:53:40 +10:00
|
|
|
static void
|
2021-10-12 16:31:47 -07:00
|
|
|
insert_sort(int64_t to_keep[], int64_t versions, int64_t version) {
|
2020-07-20 11:53:40 +10:00
|
|
|
int i = 0;
|
|
|
|
while (i < versions && version < to_keep[i]) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i == versions) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (i < versions - 1) {
|
|
|
|
memmove(&to_keep[i + 1], &to_keep[i],
|
|
|
|
sizeof(to_keep[0]) * (versions - i - 1));
|
|
|
|
}
|
|
|
|
to_keep[i] = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t
|
2023-04-14 13:53:41 +10:00
|
|
|
last_to_keep(int64_t versions, isc_dir_t *dirp, const char *bname,
|
|
|
|
size_t bnamelen) {
|
2021-10-12 16:31:47 -07:00
|
|
|
int64_t to_keep[ISC_LOG_MAX_VERSIONS] = { 0 };
|
|
|
|
int64_t version = 0;
|
|
|
|
|
2020-07-20 11:53:40 +10:00
|
|
|
if (versions <= 0) {
|
2021-10-12 16:31:47 -07:00
|
|
|
return INT64_MAX;
|
2020-07-20 11:53:40 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
if (versions > ISC_LOG_MAX_VERSIONS) {
|
|
|
|
versions = ISC_LOG_MAX_VERSIONS;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* First we fill 'to_keep' structure using insertion sort
|
|
|
|
*/
|
|
|
|
memset(to_keep, 0, sizeof(to_keep));
|
|
|
|
while (isc_dir_read(dirp) == ISC_R_SUCCESS) {
|
2021-10-12 16:31:47 -07:00
|
|
|
char *digit_end = NULL;
|
|
|
|
char *ename = NULL;
|
|
|
|
|
2020-07-20 11:53:40 +10:00
|
|
|
if (dirp->entry.length <= bnamelen ||
|
|
|
|
strncmp(dirp->entry.name, bname, bnamelen) != 0 ||
|
|
|
|
dirp->entry.name[bnamelen] != '.')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-12 16:31:47 -07:00
|
|
|
ename = &dirp->entry.name[bnamelen + 1];
|
2020-07-20 11:53:40 +10:00
|
|
|
version = strtoull(ename, &digit_end, 10);
|
|
|
|
if (*digit_end == '\0') {
|
|
|
|
insert_sort(to_keep, versions, version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_dir_reset(dirp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* to_keep[versions - 1] is the last one we want to keep
|
|
|
|
*/
|
|
|
|
return to_keep[versions - 1];
|
|
|
|
}
|
|
|
|
|
2017-03-08 23:20:40 -08:00
|
|
|
static isc_result_t
|
|
|
|
remove_old_tsversions(isc_logfile_t *file, int versions) {
|
2023-04-14 13:53:41 +10:00
|
|
|
char *digit_end;
|
|
|
|
char dirbuf[PATH_MAX + 1];
|
|
|
|
const char *bname;
|
|
|
|
const char *dirname = ".";
|
2018-03-28 14:19:37 +02:00
|
|
|
int64_t version, last = INT64_MAX;
|
2017-03-08 23:20:40 -08:00
|
|
|
isc_dir_t dir;
|
2023-04-14 13:53:41 +10:00
|
|
|
isc_result_t result;
|
|
|
|
size_t bnamelen;
|
2021-10-12 16:31:47 -07:00
|
|
|
|
2023-04-14 13:53:41 +10:00
|
|
|
bname = strrchr(file->name, '/');
|
2017-03-08 23:20:40 -08:00
|
|
|
if (bname != NULL) {
|
2023-04-14 13:53:41 +10:00
|
|
|
/*
|
|
|
|
* Copy the complete file name to dirbuf.
|
|
|
|
*/
|
|
|
|
size_t len = strlcpy(dirbuf, file->name, sizeof(dirbuf));
|
|
|
|
if (len >= sizeof(dirbuf)) {
|
|
|
|
result = ISC_R_NOSPACE;
|
|
|
|
syslog(LOG_ERR, "unable to remove log files: %s",
|
|
|
|
isc_result_totext(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Truncate after trailing '/' so the code works for
|
|
|
|
* files in the root directory.
|
|
|
|
*/
|
|
|
|
bname++;
|
|
|
|
dirbuf[bname - file->name] = '\0';
|
|
|
|
dirname = dirbuf;
|
2017-03-08 23:20:40 -08:00
|
|
|
} else {
|
2023-04-14 13:53:41 +10:00
|
|
|
bname = file->name;
|
2017-03-08 23:20:40 -08:00
|
|
|
}
|
|
|
|
bnamelen = strlen(bname);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2017-03-08 23:20:40 -08:00
|
|
|
isc_dir_init(&dir);
|
|
|
|
result = isc_dir_open(&dir, dirname);
|
2016-07-13 01:12:47 -07:00
|
|
|
|
2017-03-08 23:20:40 -08:00
|
|
|
/*
|
|
|
|
* Return if the directory open failed.
|
|
|
|
*/
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2023-04-14 13:53:41 +10:00
|
|
|
return result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-03-08 23:20:40 -08:00
|
|
|
|
2020-07-20 11:53:40 +10:00
|
|
|
last = last_to_keep(versions, &dir, bname, bnamelen);
|
2017-03-08 23:20:40 -08:00
|
|
|
|
|
|
|
while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
|
|
|
|
if (dir.entry.length > bnamelen &&
|
|
|
|
strncmp(dir.entry.name, bname, bnamelen) == 0 &&
|
|
|
|
dir.entry.name[bnamelen] == '.')
|
|
|
|
{
|
2023-04-14 13:53:41 +10:00
|
|
|
version = strtoull(&dir.entry.name[bnamelen + 1],
|
|
|
|
&digit_end, 10);
|
2017-03-08 23:20:40 -08:00
|
|
|
/*
|
|
|
|
* Remove any backup files that exceed versions.
|
|
|
|
*/
|
|
|
|
if (*digit_end == '\0' && version < last) {
|
2024-08-15 09:23:31 +02:00
|
|
|
int n = dirfd(dir.handle);
|
|
|
|
if (n >= 0) {
|
|
|
|
n = unlinkat(n, dir.entry.name, 0);
|
|
|
|
}
|
2023-04-14 13:53:41 +10:00
|
|
|
if (n < 0) {
|
|
|
|
result = isc_errno_toresult(errno);
|
|
|
|
if (result != ISC_R_SUCCESS &&
|
|
|
|
result != ISC_R_FILENOTFOUND)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"unable to remove log "
|
|
|
|
"file '%s%s': %s",
|
|
|
|
bname == file->name
|
|
|
|
? ""
|
|
|
|
: dirname,
|
|
|
|
dir.entry.name,
|
|
|
|
isc_result_totext(
|
|
|
|
result));
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-03-08 23:20:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isc_dir_close(&dir);
|
2023-04-14 13:53:41 +10:00
|
|
|
return ISC_R_SUCCESS;
|
2017-03-08 23:20:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
roll_increment(isc_logfile_t *file) {
|
|
|
|
int i, n, greatest;
|
|
|
|
char current[PATH_MAX + 1];
|
|
|
|
char newpath[PATH_MAX + 1];
|
|
|
|
const char *path;
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
REQUIRE(file != NULL);
|
|
|
|
REQUIRE(file->versions != 0);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2016-07-13 01:12:47 -07:00
|
|
|
path = file->name;
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2016-11-30 10:55:21 +11:00
|
|
|
if (file->versions == ISC_LOG_ROLLINFINITE) {
|
1999-09-23 17:43:51 +00:00
|
|
|
/*
|
2016-11-30 10:55:21 +11:00
|
|
|
* Find the first missing entry in the log file sequence.
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
2016-11-30 10:55:21 +11:00
|
|
|
for (greatest = 0; greatest < INT_MAX; greatest++) {
|
|
|
|
n = snprintf(current, sizeof(current), "%s.%u", path,
|
2022-09-07 17:22:47 +02:00
|
|
|
(unsigned int)greatest);
|
2017-03-08 23:20:40 -08:00
|
|
|
if (n >= (int)sizeof(current) || n < 0 ||
|
2022-11-02 19:33:14 +01:00
|
|
|
!isc_file_exists(current))
|
|
|
|
{
|
2016-11-30 10:55:21 +11:00
|
|
|
break;
|
2017-03-08 23:20:40 -08:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
2016-11-30 10:55:21 +11:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Get the largest existing version and remove any
|
|
|
|
* version greater than the permitted version.
|
|
|
|
*/
|
|
|
|
result = greatest_version(file, file->versions, &greatest);
|
2017-03-08 23:20:40 -08:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2016-11-30 10:55:21 +11:00
|
|
|
return result;
|
2017-03-08 23:20:40 -08:00
|
|
|
}
|
2016-11-30 10:55:21 +11:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Increment if greatest is not the actual maximum value.
|
|
|
|
*/
|
2017-03-08 23:20:40 -08:00
|
|
|
if (greatest < file->versions - 1) {
|
2016-11-30 10:55:21 +11:00
|
|
|
greatest++;
|
2017-03-08 23:20:40 -08:00
|
|
|
}
|
2016-11-30 10:55:21 +11:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
for (i = greatest; i > 0; i--) {
|
2002-10-16 13:15:30 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2018-02-15 13:20:59 +11:00
|
|
|
n = snprintf(current, sizeof(current), "%s.%u", path,
|
2022-09-07 17:22:47 +02:00
|
|
|
(unsigned int)(i - 1));
|
2017-03-08 23:20:40 -08:00
|
|
|
if (n >= (int)sizeof(current) || n < 0) {
|
2002-10-16 13:15:30 +00:00
|
|
|
result = ISC_R_NOSPACE;
|
2017-03-08 23:20:40 -08:00
|
|
|
}
|
2002-10-16 13:15:30 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
2017-03-08 23:20:40 -08:00
|
|
|
n = snprintf(newpath, sizeof(newpath), "%s.%u", path,
|
2022-09-07 17:22:47 +02:00
|
|
|
(unsigned int)i);
|
2017-03-08 23:20:40 -08:00
|
|
|
if (n >= (int)sizeof(newpath) || n < 0) {
|
2002-10-16 13:15:30 +00:00
|
|
|
result = ISC_R_NOSPACE;
|
2017-03-08 23:20:40 -08:00
|
|
|
}
|
2002-10-16 13:15:30 +00:00
|
|
|
}
|
2017-03-08 23:20:40 -08:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
result = isc_file_rename(current, newpath);
|
|
|
|
}
|
|
|
|
if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
|
2002-10-16 13:15:30 +00:00
|
|
|
syslog(LOG_ERR,
|
2016-11-30 10:55:21 +11:00
|
|
|
"unable to rename log file '%s.%u' to "
|
|
|
|
"'%s.%u': %s",
|
2002-10-16 13:15:30 +00:00
|
|
|
path, i - 1, path, i, isc_result_totext(result));
|
2017-03-08 23:20:40 -08:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 23:20:40 -08:00
|
|
|
n = snprintf(newpath, sizeof(newpath), "%s.0", path);
|
|
|
|
if (n >= (int)sizeof(newpath) || n < 0) {
|
|
|
|
result = ISC_R_NOSPACE;
|
|
|
|
} else {
|
|
|
|
result = isc_file_rename(path, newpath);
|
|
|
|
}
|
|
|
|
if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
|
|
|
|
syslog(LOG_ERR, "unable to rename log file '%s' to '%s.0': %s",
|
|
|
|
path, path, isc_result_totext(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ISC_R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
roll_timestamp(isc_logfile_t *file) {
|
|
|
|
int n;
|
|
|
|
char newts[PATH_MAX + 1];
|
|
|
|
char newpath[PATH_MAX + 1];
|
|
|
|
const char *path;
|
|
|
|
isc_time_t now;
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
REQUIRE(file != NULL);
|
|
|
|
REQUIRE(file->versions != 0);
|
|
|
|
|
|
|
|
path = file->name;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First find all the logfiles and remove the oldest ones
|
|
|
|
* Save one fewer than file->versions because we'll be renaming
|
|
|
|
* the existing file to a timestamped version after this.
|
|
|
|
*/
|
|
|
|
if (file->versions != ISC_LOG_ROLLINFINITE) {
|
|
|
|
remove_old_tsversions(file, file->versions - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then just rename the current logfile */
|
2023-03-31 00:12:33 +02:00
|
|
|
now = isc_time_now();
|
2017-03-08 23:20:40 -08:00
|
|
|
isc_time_formatshorttimestamp(&now, newts, PATH_MAX + 1);
|
|
|
|
n = snprintf(newpath, sizeof(newpath), "%s.%s", path, newts);
|
|
|
|
if (n >= (int)sizeof(newpath) || n < 0) {
|
|
|
|
result = ISC_R_NOSPACE;
|
2002-10-16 13:15:30 +00:00
|
|
|
} else {
|
2017-03-08 23:20:40 -08:00
|
|
|
result = isc_file_rename(path, newpath);
|
|
|
|
}
|
|
|
|
if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
|
|
|
|
syslog(LOG_ERR, "unable to rename log file '%s' to '%s.0': %s",
|
|
|
|
path, path, isc_result_totext(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ISC_R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_logfile_roll(isc_logfile_t *file) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(file != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do nothing (not even excess version trimming) if ISC_LOG_ROLLNEVER
|
|
|
|
* is specified. Apparently complete external control over the log
|
|
|
|
* files is desired.
|
|
|
|
*/
|
|
|
|
if (file->versions == ISC_LOG_ROLLNEVER) {
|
|
|
|
return ISC_R_SUCCESS;
|
|
|
|
} else if (file->versions == 0) {
|
|
|
|
result = isc_file_remove(file->name);
|
2002-10-16 13:15:30 +00:00
|
|
|
if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
|
|
|
|
syslog(LOG_ERR, "unable to remove log file '%s': %s",
|
2017-03-08 23:20:40 -08:00
|
|
|
file->name, isc_result_totext(result));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-03-08 23:20:40 -08:00
|
|
|
return ISC_R_SUCCESS;
|
2002-10-16 13:15:30 +00:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2017-03-08 23:20:40 -08:00
|
|
|
switch (file->suffix) {
|
|
|
|
case isc_log_rollsuffix_increment:
|
|
|
|
return roll_increment(file);
|
|
|
|
case isc_log_rollsuffix_timestamp:
|
|
|
|
return roll_timestamp(file);
|
|
|
|
default:
|
|
|
|
return ISC_R_UNEXPECTED;
|
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
isc_log_open(isc_logchannel_t *channel) {
|
|
|
|
struct stat statbuf;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool regular_file;
|
|
|
|
bool roll = false;
|
2001-03-28 04:16:32 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2000-06-01 17:20:56 +00:00
|
|
|
const char *path;
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
REQUIRE(channel->type == ISC_LOG_TOFILE);
|
|
|
|
REQUIRE(FILE_STREAM(channel) == NULL);
|
|
|
|
|
|
|
|
path = FILE_NAME(channel);
|
|
|
|
|
|
|
|
REQUIRE(path != NULL && *path != '\0');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine type of file; only regular files will be
|
2001-03-28 04:16:32 +00:00
|
|
|
* version renamed, and only if the base file exists
|
|
|
|
* and either has no size limit or has reached its size limit.
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
2001-03-28 04:16:32 +00:00
|
|
|
if (stat(path, &statbuf) == 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
regular_file = S_ISREG(statbuf.st_mode) ? true : false;
|
2001-03-28 04:16:32 +00:00
|
|
|
/* XXXDCL if not regular_file complain? */
|
2004-06-11 00:36:30 +00:00
|
|
|
if ((FILE_MAXSIZE(channel) == 0 &&
|
|
|
|
FILE_VERSIONS(channel) != ISC_LOG_ROLLNEVER) ||
|
|
|
|
(FILE_MAXSIZE(channel) > 0 &&
|
|
|
|
statbuf.st_size >= FILE_MAXSIZE(channel)))
|
|
|
|
{
|
|
|
|
roll = regular_file;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-03-11 06:11:27 +00:00
|
|
|
} else if (errno == ENOENT) {
|
2018-04-17 08:29:14 -07:00
|
|
|
regular_file = true;
|
2011-03-11 06:11:27 +00:00
|
|
|
POST(regular_file);
|
|
|
|
} else {
|
2001-03-28 04:16:32 +00:00
|
|
|
result = ISC_R_INVALIDFILE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Version control.
|
|
|
|
*/
|
2001-03-28 04:16:32 +00:00
|
|
|
if (result == ISC_R_SUCCESS && roll) {
|
2004-03-03 05:39:05 +00:00
|
|
|
if (FILE_VERSIONS(channel) == ISC_LOG_ROLLNEVER) {
|
|
|
|
return ISC_R_MAXSIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-07-13 01:12:47 -07:00
|
|
|
result = isc_logfile_roll(&channel->destination.file);
|
2002-10-16 13:15:30 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
if ((channel->flags & ISC_LOG_OPENERR) == 0) {
|
|
|
|
syslog(LOG_ERR,
|
2016-07-13 01:12:47 -07:00
|
|
|
"isc_log_open: isc_logfile_roll '%s' "
|
2002-10-16 13:15:30 +00:00
|
|
|
"failed: %s",
|
|
|
|
FILE_NAME(channel),
|
|
|
|
isc_result_totext(result));
|
|
|
|
channel->flags |= ISC_LOG_OPENERR;
|
|
|
|
}
|
2001-03-28 04:16:32 +00:00
|
|
|
return result;
|
2002-10-16 13:15:30 +00:00
|
|
|
}
|
2001-03-28 04:16:32 +00:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2001-03-28 04:16:32 +00:00
|
|
|
result = isc_stdio_open(path, "a", &FILE_STREAM(channel));
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2001-03-28 04:16:32 +00:00
|
|
|
return result;
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 22:41:46 +10:00
|
|
|
ISC_NO_SANITIZE_THREAD bool
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_log_wouldlog(int level) {
|
2000-07-13 00:19:02 +00:00
|
|
|
/*
|
|
|
|
* Try to avoid locking the mutex for messages which can't
|
|
|
|
* possibly be logged to any channels -- primarily debugging
|
|
|
|
* messages that the debug level is not high enough to print.
|
|
|
|
*
|
|
|
|
* If the level is (mathematically) less than or equal to the
|
|
|
|
* highest_level, or if there is a dynamic channel and the level is
|
|
|
|
* less than or equal to the debug level, the main loop must be
|
|
|
|
* entered to see if the message should really be output.
|
|
|
|
*/
|
2024-08-13 14:46:42 +02:00
|
|
|
if (isc__lctx == NULL) {
|
2019-12-20 19:29:18 -03:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-15 16:37:44 -07:00
|
|
|
if (forcelog) {
|
|
|
|
return true;
|
|
|
|
}
|
2000-07-13 00:19:02 +00:00
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
int highest_level = atomic_load_acquire(&isc__lctx->highest_level);
|
2020-04-08 14:27:33 +02:00
|
|
|
if (level <= highest_level) {
|
2020-04-01 11:50:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
2024-08-13 14:46:42 +02:00
|
|
|
if (atomic_load_acquire(&isc__lctx->dynamic)) {
|
|
|
|
int debug_level = atomic_load_acquire(&isc__lctx->debug_level);
|
2020-04-08 14:27:33 +02:00
|
|
|
if (level <= debug_level) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2020-04-01 11:50:32 +02:00
|
|
|
return false;
|
2000-07-13 00:19:02 +00:00
|
|
|
}
|
|
|
|
|
1999-10-25 19:55:06 +00:00
|
|
|
static void
|
2024-08-14 13:25:50 +02:00
|
|
|
isc_log_doit(isc_logcategory_t category, isc_logmodule_t module, int level,
|
2024-08-13 18:20:26 +02:00
|
|
|
const char *format, va_list args) {
|
1999-09-23 17:43:51 +00:00
|
|
|
int syslog_level;
|
2016-11-22 23:34:47 -08:00
|
|
|
const char *time_string;
|
2024-09-25 16:46:16 +02:00
|
|
|
char local_time[64] = { 0 };
|
|
|
|
char iso8601z_string[64] = { 0 };
|
|
|
|
char iso8601l_string[64] = { 0 };
|
|
|
|
char iso8601tz_string[64] = { 0 };
|
2018-11-23 21:35:01 +01:00
|
|
|
char level_string[24] = { 0 };
|
1999-09-23 17:43:51 +00:00
|
|
|
struct stat statbuf;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool matched = false;
|
2024-09-25 16:46:16 +02:00
|
|
|
bool printtime, iso8601, utc, tzinfo, printtag, printcolon;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool printcategory, printmodule, printlevel, buffered;
|
1999-09-23 17:43:51 +00:00
|
|
|
isc_logchannel_t *channel;
|
|
|
|
isc_logchannellist_t *category_channels;
|
2020-05-15 16:37:44 -07:00
|
|
|
int_fast32_t dlevel;
|
1999-09-23 17:43:51 +00:00
|
|
|
isc_result_t result;
|
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
REQUIRE(isc__lctx == NULL || VALID_CONTEXT(isc__lctx));
|
2024-08-14 14:38:07 +02:00
|
|
|
REQUIRE(category > ISC_LOGCATEGORY_DEFAULT &&
|
|
|
|
category < ISC_LOGCATEGORY_MAX);
|
|
|
|
REQUIRE(module > ISC_LOGMODULE_DEFAULT && module < ISC_LOGMODULE_MAX);
|
2000-05-18 17:20:15 +00:00
|
|
|
REQUIRE(level != ISC_LOG_DYNAMIC);
|
|
|
|
REQUIRE(format != NULL);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2024-08-13 18:20:26 +02:00
|
|
|
if (!isc_log_wouldlog(level)) {
|
2000-04-06 20:32:31 +00:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-04-06 20:32:31 +00:00
|
|
|
|
2023-12-19 10:41:15 +03:00
|
|
|
rcu_read_lock();
|
2024-08-13 14:46:42 +02:00
|
|
|
LOCK(&isc__lctx->lock);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
isc__lctx->buffer[0] = '\0';
|
2009-01-06 23:47:57 +00:00
|
|
|
|
2024-08-13 14:46:42 +02:00
|
|
|
isc_logconfig_t *lcfg = rcu_dereference(isc__lctx->logconfig);
|
2024-08-13 09:45:26 +00:00
|
|
|
if (lcfg == NULL) {
|
|
|
|
goto unlock;
|
|
|
|
}
|
2000-02-26 19:57:02 +00:00
|
|
|
|
2024-08-14 13:25:50 +02:00
|
|
|
category_channels = ISC_LIST_HEAD(lcfg->channellists[category]);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
1999-11-03 01:07:02 +00:00
|
|
|
do {
|
1999-10-25 19:55:06 +00:00
|
|
|
/*
|
2020-04-01 11:50:32 +02:00
|
|
|
* If the channel list end was reached and a match was
|
|
|
|
* made, everything is finished.
|
1999-10-25 19:55:06 +00:00
|
|
|
*/
|
1999-09-23 17:43:51 +00:00
|
|
|
if (category_channels == NULL && matched) {
|
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
if (category_channels == NULL && !matched &&
|
2000-03-01 17:31:56 +00:00
|
|
|
category_channels != ISC_LIST_HEAD(lcfg->channellists[0]))
|
|
|
|
{
|
1999-09-23 17:43:51 +00:00
|
|
|
/*
|
2020-04-01 11:50:32 +02:00
|
|
|
* No category/module pair was explicitly
|
|
|
|
* configured. Try the category named "default".
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
2000-03-01 17:31:56 +00:00
|
|
|
category_channels =
|
|
|
|
ISC_LIST_HEAD(lcfg->channellists[0]);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
if (category_channels == NULL && !matched) {
|
|
|
|
/*
|
|
|
|
* No matching module was explicitly configured
|
2020-04-01 11:50:32 +02:00
|
|
|
* for the category named "default". Use the
|
|
|
|
* internal default channel.
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
|
|
|
category_channels = &default_channel;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2024-08-14 14:38:07 +02:00
|
|
|
if (category_channels->module != ISC_LOGMODULE_DEFAULT &&
|
2022-11-02 19:33:14 +01:00
|
|
|
category_channels->module != module)
|
|
|
|
{
|
2000-03-01 17:31:56 +00:00
|
|
|
category_channels = ISC_LIST_NEXT(category_channels,
|
|
|
|
link);
|
1999-09-23 17:43:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
matched = true;
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
channel = category_channels->channel;
|
2000-03-01 17:31:56 +00:00
|
|
|
category_channels = ISC_LIST_NEXT(category_channels, link);
|
|
|
|
|
2020-05-15 16:37:44 -07:00
|
|
|
if (!forcelog) {
|
2024-08-13 14:46:42 +02:00
|
|
|
dlevel = atomic_load_acquire(&isc__lctx->debug_level);
|
2020-05-15 16:37:44 -07:00
|
|
|
if (((channel->flags & ISC_LOG_DEBUGONLY) != 0) &&
|
2022-11-02 19:33:14 +01:00
|
|
|
dlevel == 0)
|
|
|
|
{
|
2020-05-15 16:37:44 -07:00
|
|
|
continue;
|
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2020-05-15 16:37:44 -07:00
|
|
|
if (channel->level == ISC_LOG_DYNAMIC) {
|
|
|
|
if (dlevel < level) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (channel->level < level) {
|
1999-09-23 17:43:51 +00:00
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2000-05-16 03:37:39 +00:00
|
|
|
if ((channel->flags & ISC_LOG_PRINTTIME) != 0 &&
|
2022-11-02 19:33:14 +01:00
|
|
|
local_time[0] == '\0')
|
|
|
|
{
|
2001-11-30 01:59:49 +00:00
|
|
|
isc_time_t isctime;
|
2009-01-06 23:47:57 +00:00
|
|
|
|
2023-03-31 00:12:33 +02:00
|
|
|
isctime = isc_time_now();
|
2016-11-22 23:34:47 -08:00
|
|
|
|
|
|
|
isc_time_formattimestamp(&isctime, local_time,
|
|
|
|
sizeof(local_time));
|
|
|
|
isc_time_formatISO8601ms(&isctime, iso8601z_string,
|
|
|
|
sizeof(iso8601z_string));
|
|
|
|
isc_time_formatISO8601Lms(&isctime, iso8601l_string,
|
|
|
|
sizeof(iso8601l_string));
|
2024-09-25 16:46:16 +02:00
|
|
|
isc_time_formatISO8601TZms(&isctime, iso8601tz_string,
|
|
|
|
sizeof(iso8601tz_string));
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
2000-05-16 03:37:39 +00:00
|
|
|
if ((channel->flags & ISC_LOG_PRINTLEVEL) != 0 &&
|
2022-11-02 19:33:14 +01:00
|
|
|
level_string[0] == '\0')
|
|
|
|
{
|
2018-11-23 21:35:01 +01:00
|
|
|
if (level < ISC_LOG_CRITICAL) {
|
2003-04-11 07:25:31 +00:00
|
|
|
snprintf(level_string, sizeof(level_string),
|
2018-11-23 21:35:01 +01:00
|
|
|
"level %d: ", level);
|
|
|
|
} else if (level > ISC_LOG_DYNAMIC) {
|
2003-04-11 07:25:31 +00:00
|
|
|
snprintf(level_string, sizeof(level_string),
|
|
|
|
"%s %d: ", log_level_strings[0],
|
|
|
|
level);
|
2018-11-23 21:35:01 +01:00
|
|
|
} else {
|
2003-04-11 07:25:31 +00:00
|
|
|
snprintf(level_string, sizeof(level_string),
|
|
|
|
"%s: ", log_level_strings[-level]);
|
2018-11-23 21:35:01 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-10-11 14:50:51 +00:00
|
|
|
* Only format the message once.
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
2024-08-13 14:46:42 +02:00
|
|
|
if (isc__lctx->buffer[0] == '\0') {
|
|
|
|
(void)vsnprintf(isc__lctx->buffer,
|
|
|
|
sizeof(isc__lctx->buffer), format,
|
|
|
|
args);
|
1999-10-25 19:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 11:57:57 +02:00
|
|
|
utc = ((channel->flags & ISC_LOG_UTC) != 0);
|
2024-09-25 16:46:16 +02:00
|
|
|
tzinfo = ((channel->flags & ISC_LOG_TZINFO) != 0);
|
2018-10-11 11:57:57 +02:00
|
|
|
iso8601 = ((channel->flags & ISC_LOG_ISO8601) != 0);
|
|
|
|
printtime = ((channel->flags & ISC_LOG_PRINTTIME) != 0);
|
2018-04-17 08:29:14 -07:00
|
|
|
printtag = ((channel->flags &
|
2018-10-11 11:57:57 +02:00
|
|
|
(ISC_LOG_PRINTTAG | ISC_LOG_PRINTPREFIX)) != 0 &&
|
2018-04-17 08:29:14 -07:00
|
|
|
lcfg->tag != NULL);
|
2018-10-11 11:57:57 +02:00
|
|
|
printcolon = ((channel->flags & ISC_LOG_PRINTTAG) != 0 &&
|
2018-04-17 08:29:14 -07:00
|
|
|
lcfg->tag != NULL);
|
2018-10-11 11:57:57 +02:00
|
|
|
printcategory = ((channel->flags & ISC_LOG_PRINTCATEGORY) != 0);
|
|
|
|
printmodule = ((channel->flags & ISC_LOG_PRINTMODULE) != 0);
|
|
|
|
printlevel = ((channel->flags & ISC_LOG_PRINTLEVEL) != 0);
|
|
|
|
buffered = ((channel->flags & ISC_LOG_BUFFERED) != 0);
|
2000-05-16 03:37:39 +00:00
|
|
|
|
2016-11-22 23:34:47 -08:00
|
|
|
if (printtime) {
|
|
|
|
if (iso8601) {
|
|
|
|
if (utc) {
|
|
|
|
time_string = iso8601z_string;
|
2024-09-25 16:46:16 +02:00
|
|
|
} else if (tzinfo) {
|
|
|
|
time_string = iso8601tz_string;
|
2016-11-22 23:34:47 -08:00
|
|
|
} else {
|
|
|
|
time_string = iso8601l_string;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
time_string = local_time;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
time_string = "";
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-11-22 23:34:47 -08:00
|
|
|
|
1999-09-23 17:43:51 +00:00
|
|
|
switch (channel->type) {
|
|
|
|
case ISC_LOG_TOFILE:
|
2000-12-23 19:23:48 +00:00
|
|
|
if (FILE_MAXREACHED(channel)) {
|
|
|
|
/*
|
|
|
|
* If the file can be rolled, OR
|
|
|
|
* If the file no longer exists, OR
|
2020-04-01 11:50:32 +02:00
|
|
|
* If the file is less than the maximum
|
|
|
|
* size, (such as if it had been renamed
|
|
|
|
* and a new one touched, or it was
|
|
|
|
* truncated in place)
|
|
|
|
* ... then close it to trigger
|
|
|
|
* reopening.
|
2000-12-23 19:23:48 +00:00
|
|
|
*/
|
|
|
|
if (FILE_VERSIONS(channel) !=
|
|
|
|
ISC_LOG_ROLLNEVER ||
|
|
|
|
(stat(FILE_NAME(channel), &statbuf) != 0 &&
|
|
|
|
errno == ENOENT) ||
|
|
|
|
statbuf.st_size < FILE_MAXSIZE(channel))
|
|
|
|
{
|
2024-06-19 17:16:34 +10:00
|
|
|
if (FILE_STREAM(channel) != NULL) {
|
|
|
|
(void)fclose(
|
|
|
|
FILE_STREAM(channel));
|
|
|
|
FILE_STREAM(channel) = NULL;
|
|
|
|
}
|
2018-04-17 08:29:14 -07:00
|
|
|
FILE_MAXREACHED(channel) = false;
|
2000-12-23 19:23:48 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Eh, skip it.
|
|
|
|
*/
|
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-12-23 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
1999-09-23 17:43:51 +00:00
|
|
|
if (FILE_STREAM(channel) == NULL) {
|
|
|
|
result = isc_log_open(channel);
|
2002-10-16 13:15:30 +00:00
|
|
|
if (result != ISC_R_SUCCESS &&
|
2004-03-03 05:39:05 +00:00
|
|
|
result != ISC_R_MAXSIZE &&
|
2002-10-16 13:15:30 +00:00
|
|
|
(channel->flags & ISC_LOG_OPENERR) == 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR,
|
2020-04-01 11:50:32 +02:00
|
|
|
"isc_log_open '%s' "
|
|
|
|
"failed: %s",
|
2002-10-16 13:15:30 +00:00
|
|
|
FILE_NAME(channel),
|
|
|
|
isc_result_totext(result));
|
|
|
|
channel->flags |= ISC_LOG_OPENERR;
|
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2002-10-16 13:15:30 +00:00
|
|
|
channel->flags &= ~ISC_LOG_OPENERR;
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
2021-10-11 12:09:16 +02:00
|
|
|
FALLTHROUGH;
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
case ISC_LOG_TOFILEDESC:
|
2014-02-16 13:03:17 -08:00
|
|
|
fprintf(FILE_STREAM(channel), "%s%s%s%s%s%s%s%s%s%s\n",
|
2016-11-22 23:34:47 -08:00
|
|
|
printtime ? time_string : "",
|
2000-05-16 03:37:39 +00:00
|
|
|
printtime ? " " : "", printtag ? lcfg->tag : "",
|
2014-02-16 13:03:17 -08:00
|
|
|
printcolon ? ": " : "",
|
2024-08-14 13:25:50 +02:00
|
|
|
printcategory ? categories_description[category]
|
|
|
|
: "",
|
2000-05-16 03:37:39 +00:00
|
|
|
printcategory ? ": " : "",
|
2024-08-14 13:25:50 +02:00
|
|
|
printmodule ? modules_description[module] : "",
|
2000-05-16 03:37:39 +00:00
|
|
|
printmodule ? ": " : "",
|
2024-08-13 14:46:42 +02:00
|
|
|
printlevel ? level_string : "",
|
|
|
|
isc__lctx->buffer);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2014-10-30 11:37:05 +11:00
|
|
|
if (!buffered) {
|
|
|
|
fflush(FILE_STREAM(channel));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the file now exceeds its maximum size
|
2020-04-01 11:50:32 +02:00
|
|
|
* threshold, note it so that it will not be
|
|
|
|
* logged to any more.
|
1999-09-23 17:43:51 +00:00
|
|
|
*/
|
2004-04-10 04:33:36 +00:00
|
|
|
if (FILE_MAXSIZE(channel) > 0) {
|
1999-09-23 17:43:51 +00:00
|
|
|
INSIST(channel->type == ISC_LOG_TOFILE);
|
|
|
|
|
|
|
|
/* XXXDCL NT fstat/fileno */
|
|
|
|
/* XXXDCL complain if fstat fails? */
|
|
|
|
if (fstat(fileno(FILE_STREAM(channel)),
|
|
|
|
&statbuf) >= 0 &&
|
2000-12-23 19:23:48 +00:00
|
|
|
statbuf.st_size > FILE_MAXSIZE(channel))
|
|
|
|
{
|
2018-04-17 08:29:14 -07:00
|
|
|
FILE_MAXREACHED(channel) = true;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ISC_LOG_TOSYSLOG:
|
|
|
|
if (level > 0) {
|
|
|
|
syslog_level = LOG_DEBUG;
|
|
|
|
} else if (level < ISC_LOG_CRITICAL) {
|
|
|
|
syslog_level = LOG_CRIT;
|
|
|
|
} else {
|
|
|
|
syslog_level = syslog_map[-level];
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2001-11-30 01:59:49 +00:00
|
|
|
(void)syslog(
|
|
|
|
FACILITY(channel) | syslog_level,
|
2006-03-01 02:20:41 +00:00
|
|
|
"%s%s%s%s%s%s%s%s%s%s",
|
2000-05-16 03:37:39 +00:00
|
|
|
printtime ? time_string : "",
|
|
|
|
printtime ? " " : "", printtag ? lcfg->tag : "",
|
2014-02-16 13:03:17 -08:00
|
|
|
printcolon ? ": " : "",
|
2024-08-14 13:25:50 +02:00
|
|
|
printcategory ? categories_description[category]
|
|
|
|
: "",
|
2000-05-16 03:37:39 +00:00
|
|
|
printcategory ? ": " : "",
|
2024-08-14 13:25:50 +02:00
|
|
|
printmodule ? modules_description[module] : "",
|
2000-05-16 03:37:39 +00:00
|
|
|
printmodule ? ": " : "",
|
2024-08-13 14:46:42 +02:00
|
|
|
printlevel ? level_string : "",
|
|
|
|
isc__lctx->buffer);
|
1999-09-23 17:43:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ISC_LOG_TONULL:
|
|
|
|
break;
|
|
|
|
}
|
1999-11-03 01:07:02 +00:00
|
|
|
} while (1);
|
1999-09-23 17:43:51 +00:00
|
|
|
|
2020-03-13 15:16:14 +01:00
|
|
|
unlock:
|
2024-08-13 14:46:42 +02:00
|
|
|
UNLOCK(&isc__lctx->lock);
|
2023-12-19 10:41:15 +03:00
|
|
|
rcu_read_unlock();
|
1999-09-23 17:43:51 +00:00
|
|
|
}
|
2020-05-15 16:37:44 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
isc_log_setforcelog(bool v) {
|
|
|
|
forcelog = v;
|
|
|
|
}
|
2024-08-13 14:46:42 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
isc__log_initialize(void) {
|
|
|
|
REQUIRE(isc__lctx == NULL);
|
|
|
|
|
|
|
|
isc_mem_t *mctx = NULL;
|
|
|
|
|
|
|
|
isc_mem_create(&mctx);
|
|
|
|
|
|
|
|
isc__lctx = isc_mem_get(mctx, sizeof(*isc__lctx));
|
|
|
|
*isc__lctx = (isc_log_t){
|
2024-08-13 17:16:57 +02:00
|
|
|
.magic = LCTX_MAGIC, .mctx = mctx, /* implicit attach */
|
2024-08-13 14:46:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
isc_mutex_init(&isc__lctx->lock);
|
|
|
|
|
|
|
|
/* Create default logging configuration */
|
|
|
|
isc_logconfig_t *lcfg = NULL;
|
2024-08-13 18:20:26 +02:00
|
|
|
isc_logconfig_create(&lcfg);
|
2024-08-13 14:46:42 +02:00
|
|
|
|
|
|
|
atomic_init(&isc__lctx->highest_level, lcfg->highest_level);
|
|
|
|
atomic_init(&isc__lctx->dynamic, lcfg->dynamic);
|
|
|
|
|
|
|
|
isc__lctx->logconfig = lcfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc__log_shutdown(void) {
|
|
|
|
REQUIRE(VALID_CONTEXT(isc__lctx));
|
|
|
|
|
|
|
|
isc_mem_t *mctx = isc__lctx->mctx;
|
|
|
|
|
|
|
|
/* Stop the logging as a first thing */
|
|
|
|
atomic_store_release(&isc__lctx->debug_level, 0);
|
|
|
|
atomic_store_release(&isc__lctx->highest_level, 0);
|
|
|
|
atomic_store_release(&isc__lctx->dynamic, false);
|
|
|
|
|
|
|
|
if (isc__lctx->logconfig != NULL) {
|
|
|
|
isc_logconfig_destroy(&isc__lctx->logconfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_mutex_destroy(&isc__lctx->lock);
|
|
|
|
|
|
|
|
isc_mem_putanddetach(&mctx, isc__lctx, sizeof(*isc__lctx));
|
|
|
|
}
|