xref: /netbsd/external/mpl/bind/dist/bin/named/log.c (revision c0b5d9fb)
1*c0b5d9fbSchristos /*	$NetBSD: log.c,v 1.6 2022/09/23 12:15:21 christos Exp $	*/
2e2b1b9c0Schristos 
3e2b1b9c0Schristos /*
4e2b1b9c0Schristos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5e2b1b9c0Schristos  *
6*c0b5d9fbSchristos  * SPDX-License-Identifier: MPL-2.0
7*c0b5d9fbSchristos  *
8e2b1b9c0Schristos  * This Source Code Form is subject to the terms of the Mozilla Public
9e2b1b9c0Schristos  * License, v. 2.0. If a copy of the MPL was not distributed with this
1073584a28Schristos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11e2b1b9c0Schristos  *
12e2b1b9c0Schristos  * See the COPYRIGHT file distributed with this work for additional
13e2b1b9c0Schristos  * information regarding copyright ownership.
14e2b1b9c0Schristos  */
15e2b1b9c0Schristos 
16e2b1b9c0Schristos /*! \file */
17e2b1b9c0Schristos 
18e2b1b9c0Schristos #include <isc/result.h>
19e2b1b9c0Schristos 
20e2b1b9c0Schristos #include <dns/log.h>
21e2b1b9c0Schristos 
22e2b1b9c0Schristos #include <isccfg/log.h>
23e2b1b9c0Schristos 
249742fdb4Schristos #include <ns/log.h>
259742fdb4Schristos 
26e2b1b9c0Schristos #include <named/log.h>
27e2b1b9c0Schristos 
28e2b1b9c0Schristos #ifndef ISC_FACILITY
29e2b1b9c0Schristos #define ISC_FACILITY LOG_DAEMON
309742fdb4Schristos #endif /* ifndef ISC_FACILITY */
31e2b1b9c0Schristos 
32e2b1b9c0Schristos /*%
33e2b1b9c0Schristos  * When adding a new category, be sure to add the appropriate
34e2b1b9c0Schristos  * \#define to <named/log.h> and to update the list in
35e2b1b9c0Schristos  * bin/check/check-tool.c.
36e2b1b9c0Schristos  */
379742fdb4Schristos static isc_logcategory_t categories[] = { { "", 0 },
38e2b1b9c0Schristos 					  { "unmatched", 0 },
399742fdb4Schristos 					  { NULL, 0 } };
40e2b1b9c0Schristos 
41e2b1b9c0Schristos /*%
42e2b1b9c0Schristos  * When adding a new module, be sure to add the appropriate
43e2b1b9c0Schristos  * \#define to <dns/log.h>.
44e2b1b9c0Schristos  */
45e2b1b9c0Schristos static isc_logmodule_t modules[] = {
469742fdb4Schristos 	{ "main", 0 }, { "server", 0 }, { "control", 0 }, { NULL, 0 }
47e2b1b9c0Schristos };
48e2b1b9c0Schristos 
49e2b1b9c0Schristos isc_result_t
named_log_init(bool safe)50f2e20987Schristos named_log_init(bool safe) {
51e2b1b9c0Schristos 	isc_result_t result;
52e2b1b9c0Schristos 	isc_logconfig_t *lcfg = NULL;
53e2b1b9c0Schristos 
54e2b1b9c0Schristos 	named_g_categories = categories;
55e2b1b9c0Schristos 	named_g_modules = modules;
56e2b1b9c0Schristos 
57e2b1b9c0Schristos 	/*
58e2b1b9c0Schristos 	 * Setup a logging context.
59e2b1b9c0Schristos 	 */
609742fdb4Schristos 	isc_log_create(named_g_mctx, &named_g_lctx, &lcfg);
61e2b1b9c0Schristos 
62e2b1b9c0Schristos 	/*
63e2b1b9c0Schristos 	 * named-checktool.c:setup_logging() needs to be kept in sync.
64e2b1b9c0Schristos 	 */
65e2b1b9c0Schristos 	isc_log_registercategories(named_g_lctx, named_g_categories);
66e2b1b9c0Schristos 	isc_log_registermodules(named_g_lctx, named_g_modules);
67e2b1b9c0Schristos 	isc_log_setcontext(named_g_lctx);
68e2b1b9c0Schristos 	dns_log_init(named_g_lctx);
69e2b1b9c0Schristos 	dns_log_setcontext(named_g_lctx);
70e2b1b9c0Schristos 	cfg_log_init(named_g_lctx);
71e2b1b9c0Schristos 	ns_log_init(named_g_lctx);
72e2b1b9c0Schristos 	ns_log_setcontext(named_g_lctx);
73e2b1b9c0Schristos 
749742fdb4Schristos 	if (safe) {
759742fdb4Schristos 		named_log_setsafechannels(lcfg);
769742fdb4Schristos 	} else {
779742fdb4Schristos 		named_log_setdefaultchannels(lcfg);
789742fdb4Schristos 	}
79e2b1b9c0Schristos 
80e2b1b9c0Schristos 	result = named_log_setdefaultcategory(lcfg);
819742fdb4Schristos 	if (result != ISC_R_SUCCESS) {
82e2b1b9c0Schristos 		goto cleanup;
839742fdb4Schristos 	}
84e2b1b9c0Schristos 
85e2b1b9c0Schristos 	return (ISC_R_SUCCESS);
86e2b1b9c0Schristos 
87e2b1b9c0Schristos cleanup:
88e2b1b9c0Schristos 	isc_log_destroy(&named_g_lctx);
89e2b1b9c0Schristos 	isc_log_setcontext(NULL);
90e2b1b9c0Schristos 	dns_log_setcontext(NULL);
91e2b1b9c0Schristos 
92e2b1b9c0Schristos 	return (result);
93e2b1b9c0Schristos }
94e2b1b9c0Schristos 
959742fdb4Schristos void
named_log_setdefaultchannels(isc_logconfig_t * lcfg)96e2b1b9c0Schristos named_log_setdefaultchannels(isc_logconfig_t *lcfg) {
97e2b1b9c0Schristos 	isc_logdestination_t destination;
98e2b1b9c0Schristos 
99e2b1b9c0Schristos 	/*
100e2b1b9c0Schristos 	 * By default, the logging library makes "default_debug" log to
101e2b1b9c0Schristos 	 * stderr.  In BIND, we want to override this and log to named.run
102e2b1b9c0Schristos 	 * instead, unless the -g option was given.
103e2b1b9c0Schristos 	 */
104e2b1b9c0Schristos 	if (!named_g_logstderr) {
105e2b1b9c0Schristos 		destination.file.stream = NULL;
106e2b1b9c0Schristos 		destination.file.name = "named.run";
107e2b1b9c0Schristos 		destination.file.versions = ISC_LOG_ROLLNEVER;
108e2b1b9c0Schristos 		destination.file.maximum_size = 0;
1099742fdb4Schristos 		isc_log_createchannel(lcfg, "default_debug", ISC_LOG_TOFILE,
1109742fdb4Schristos 				      ISC_LOG_DYNAMIC, &destination,
1119742fdb4Schristos 				      ISC_LOG_PRINTTIME | ISC_LOG_DEBUGONLY);
112e2b1b9c0Schristos 	}
113e2b1b9c0Schristos 
114e2b1b9c0Schristos 	if (named_g_logfile != NULL) {
115e2b1b9c0Schristos 		destination.file.stream = NULL;
116e2b1b9c0Schristos 		destination.file.name = named_g_logfile;
117e2b1b9c0Schristos 		destination.file.versions = ISC_LOG_ROLLNEVER;
118e2b1b9c0Schristos 		destination.file.maximum_size = 0;
1199742fdb4Schristos 		isc_log_createchannel(lcfg, "default_logfile", ISC_LOG_TOFILE,
1209742fdb4Schristos 				      ISC_LOG_DYNAMIC, &destination,
121e2b1b9c0Schristos 				      ISC_LOG_PRINTTIME |
122e2b1b9c0Schristos 					      ISC_LOG_PRINTCATEGORY |
123e2b1b9c0Schristos 					      ISC_LOG_PRINTLEVEL);
124e2b1b9c0Schristos 	}
125e2b1b9c0Schristos 
126e2b1b9c0Schristos #if ISC_FACILITY != LOG_DAEMON
127e2b1b9c0Schristos 	destination.facility = ISC_FACILITY;
1289742fdb4Schristos 	isc_log_createchannel(lcfg, "default_syslog", ISC_LOG_TOSYSLOG,
1299742fdb4Schristos 			      ISC_LOG_INFO, &destination, 0);
1309742fdb4Schristos #endif /* if ISC_FACILITY != LOG_DAEMON */
131e2b1b9c0Schristos 
132e2b1b9c0Schristos 	/*
133e2b1b9c0Schristos 	 * Set the initial debug level.
134e2b1b9c0Schristos 	 */
135e2b1b9c0Schristos 	isc_log_setdebuglevel(named_g_lctx, named_g_debuglevel);
136e2b1b9c0Schristos }
137e2b1b9c0Schristos 
1389742fdb4Schristos void
named_log_setsafechannels(isc_logconfig_t * lcfg)139e2b1b9c0Schristos named_log_setsafechannels(isc_logconfig_t *lcfg) {
140e2b1b9c0Schristos 	isc_logdestination_t destination;
141e2b1b9c0Schristos 
142e2b1b9c0Schristos 	if (!named_g_logstderr) {
1439742fdb4Schristos 		isc_log_createchannel(lcfg, "default_debug", ISC_LOG_TONULL,
1449742fdb4Schristos 				      ISC_LOG_DYNAMIC, NULL, 0);
145e2b1b9c0Schristos 
146e2b1b9c0Schristos 		/*
147e2b1b9c0Schristos 		 * Setting the debug level to zero should get the output
148e2b1b9c0Schristos 		 * discarded a bit faster.
149e2b1b9c0Schristos 		 */
150e2b1b9c0Schristos 		isc_log_setdebuglevel(named_g_lctx, 0);
151e2b1b9c0Schristos 	} else {
152e2b1b9c0Schristos 		isc_log_setdebuglevel(named_g_lctx, named_g_debuglevel);
153e2b1b9c0Schristos 	}
154e2b1b9c0Schristos 
155e2b1b9c0Schristos 	if (named_g_logfile != NULL) {
156e2b1b9c0Schristos 		destination.file.stream = NULL;
157e2b1b9c0Schristos 		destination.file.name = named_g_logfile;
158e2b1b9c0Schristos 		destination.file.versions = ISC_LOG_ROLLNEVER;
159e2b1b9c0Schristos 		destination.file.maximum_size = 0;
1609742fdb4Schristos 		isc_log_createchannel(lcfg, "default_logfile", ISC_LOG_TOFILE,
1619742fdb4Schristos 				      ISC_LOG_DYNAMIC, &destination,
162e2b1b9c0Schristos 				      ISC_LOG_PRINTTIME |
163e2b1b9c0Schristos 					      ISC_LOG_PRINTCATEGORY |
164e2b1b9c0Schristos 					      ISC_LOG_PRINTLEVEL);
165e2b1b9c0Schristos 	}
166e2b1b9c0Schristos 
167e2b1b9c0Schristos #if ISC_FACILITY != LOG_DAEMON
168e2b1b9c0Schristos 	destination.facility = ISC_FACILITY;
1699742fdb4Schristos 	isc_log_createchannel(lcfg, "default_syslog", ISC_LOG_TOSYSLOG,
1709742fdb4Schristos 			      ISC_LOG_INFO, &destination, 0);
1719742fdb4Schristos #endif /* if ISC_FACILITY != LOG_DAEMON */
172e2b1b9c0Schristos }
173e2b1b9c0Schristos 
174e2b1b9c0Schristos isc_result_t
named_log_setdefaultcategory(isc_logconfig_t * lcfg)175e2b1b9c0Schristos named_log_setdefaultcategory(isc_logconfig_t *lcfg) {
176e2b1b9c0Schristos 	isc_result_t result = ISC_R_SUCCESS;
177e2b1b9c0Schristos 
178e2b1b9c0Schristos 	result = isc_log_usechannel(lcfg, "default_debug",
179e2b1b9c0Schristos 				    ISC_LOGCATEGORY_DEFAULT, NULL);
1809742fdb4Schristos 	if (result != ISC_R_SUCCESS) {
181e2b1b9c0Schristos 		goto cleanup;
1829742fdb4Schristos 	}
183e2b1b9c0Schristos 
184e2b1b9c0Schristos 	if (!named_g_logstderr) {
1859742fdb4Schristos 		if (named_g_logfile != NULL) {
186e2b1b9c0Schristos 			result = isc_log_usechannel(lcfg, "default_logfile",
187e2b1b9c0Schristos 						    ISC_LOGCATEGORY_DEFAULT,
188e2b1b9c0Schristos 						    NULL);
1899742fdb4Schristos 		} else if (!named_g_nosyslog) {
190e2b1b9c0Schristos 			result = isc_log_usechannel(lcfg, "default_syslog",
191e2b1b9c0Schristos 						    ISC_LOGCATEGORY_DEFAULT,
192e2b1b9c0Schristos 						    NULL);
193e2b1b9c0Schristos 		}
1949742fdb4Schristos 	}
195e2b1b9c0Schristos 
196e2b1b9c0Schristos cleanup:
197e2b1b9c0Schristos 	return (result);
198e2b1b9c0Schristos }
199e2b1b9c0Schristos 
200e2b1b9c0Schristos isc_result_t
named_log_setunmatchedcategory(isc_logconfig_t * lcfg)201e2b1b9c0Schristos named_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
202e2b1b9c0Schristos 	isc_result_t result;
203e2b1b9c0Schristos 
2049742fdb4Schristos 	result = isc_log_usechannel(lcfg, "null", NAMED_LOGCATEGORY_UNMATCHED,
2059742fdb4Schristos 				    NULL);
206e2b1b9c0Schristos 	return (result);
207e2b1b9c0Schristos }
208e2b1b9c0Schristos 
209e2b1b9c0Schristos void
named_log_shutdown(void)210e2b1b9c0Schristos named_log_shutdown(void) {
211e2b1b9c0Schristos 	isc_log_destroy(&named_g_lctx);
212e2b1b9c0Schristos 	isc_log_setcontext(NULL);
213e2b1b9c0Schristos 	dns_log_setcontext(NULL);
214e2b1b9c0Schristos }
215