xref: /minix/external/bsd/bind/dist/bin/named/log.c (revision 00b67f09)
1 /*	$NetBSD: log.c,v 1.5 2014/12/10 04:37:51 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004-2007, 2009, 2013  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2002  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: log.c,v 1.49 2009/01/07 01:46:40 jinmei Exp  */
21 
22 /*! \file */
23 
24 #include <config.h>
25 
26 #include <isc/result.h>
27 
28 #include <isccfg/log.h>
29 
30 #include <named/log.h>
31 
32 #ifndef ISC_FACILITY
33 #define ISC_FACILITY LOG_DAEMON
34 #endif
35 
36 /*%
37  * When adding a new category, be sure to add the appropriate
38  * \#define to <named/log.h> and to update the list in
39  * bin/check/check-tool.c.
40  */
41 static isc_logcategory_t categories[] = {
42 	{ "",		 		0 },
43 	{ "client",	 		0 },
44 	{ "network",	 		0 },
45 	{ "update",	 		0 },
46 	{ "queries",	 		0 },
47 	{ "unmatched",	 		0 },
48 	{ "update-security",		0 },
49 	{ "query-errors",		0 },
50 	{ NULL, 			0 }
51 };
52 
53 /*%
54  * When adding a new module, be sure to add the appropriate
55  * \#define to <dns/log.h>.
56  */
57 static isc_logmodule_t modules[] = {
58 	{ "main",	 		0 },
59 	{ "client",	 		0 },
60 	{ "server",		 	0 },
61 	{ "query",		 	0 },
62 	{ "interfacemgr",	 	0 },
63 	{ "update",	 		0 },
64 	{ "xfer-in",	 		0 },
65 	{ "xfer-out",	 		0 },
66 	{ "notify",	 		0 },
67 	{ "control",	 		0 },
68 	{ "lwresd",	 		0 },
69 	{ NULL, 			0 }
70 };
71 
72 isc_result_t
ns_log_init(isc_boolean_t safe)73 ns_log_init(isc_boolean_t safe) {
74 	isc_result_t result;
75 	isc_logconfig_t *lcfg = NULL;
76 
77 	ns_g_categories = categories;
78 	ns_g_modules = modules;
79 
80 	/*
81 	 * Setup a logging context.
82 	 */
83 	result = isc_log_create(ns_g_mctx, &ns_g_lctx, &lcfg);
84 	if (result != ISC_R_SUCCESS)
85 		return (result);
86 
87 	/*
88 	 * named-checktool.c:setup_logging() needs to be kept in sync.
89 	 */
90 	isc_log_registercategories(ns_g_lctx, ns_g_categories);
91 	isc_log_registermodules(ns_g_lctx, ns_g_modules);
92 	isc_log_setcontext(ns_g_lctx);
93 	dns_log_init(ns_g_lctx);
94 	dns_log_setcontext(ns_g_lctx);
95 	cfg_log_init(ns_g_lctx);
96 
97 	if (safe)
98 		result = ns_log_setsafechannels(lcfg);
99 	else
100 		result = ns_log_setdefaultchannels(lcfg);
101 	if (result != ISC_R_SUCCESS)
102 		goto cleanup;
103 
104 	result = ns_log_setdefaultcategory(lcfg);
105 	if (result != ISC_R_SUCCESS)
106 		goto cleanup;
107 
108 	return (ISC_R_SUCCESS);
109 
110  cleanup:
111 	isc_log_destroy(&ns_g_lctx);
112 	isc_log_setcontext(NULL);
113 	dns_log_setcontext(NULL);
114 
115 	return (result);
116 }
117 
118 isc_result_t
ns_log_setdefaultchannels(isc_logconfig_t * lcfg)119 ns_log_setdefaultchannels(isc_logconfig_t *lcfg) {
120 	isc_result_t result;
121 	isc_logdestination_t destination;
122 
123 	/*
124 	 * By default, the logging library makes "default_debug" log to
125 	 * stderr.  In BIND, we want to override this and log to named.run
126 	 * instead, unless the -g option was given.
127 	 */
128 	if (! ns_g_logstderr) {
129 		destination.file.stream = NULL;
130 		destination.file.name = "named.run";
131 		destination.file.versions = ISC_LOG_ROLLNEVER;
132 		destination.file.maximum_size = 0;
133 		result = isc_log_createchannel(lcfg, "default_debug",
134 					       ISC_LOG_TOFILE,
135 					       ISC_LOG_DYNAMIC,
136 					       &destination,
137 					       ISC_LOG_PRINTTIME|
138 					       ISC_LOG_DEBUGONLY);
139 		if (result != ISC_R_SUCCESS)
140 			goto cleanup;
141 	}
142 
143 #if ISC_FACILITY != LOG_DAEMON
144 	destination.facility = ISC_FACILITY;
145 	result = isc_log_createchannel(lcfg, "default_syslog",
146 				       ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
147 				       &destination, 0);
148 	if (result != ISC_R_SUCCESS)
149 		goto cleanup;
150 #endif
151 
152 	/*
153 	 * Set the initial debug level.
154 	 */
155 	isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
156 
157 	result = ISC_R_SUCCESS;
158 
159  cleanup:
160 	return (result);
161 }
162 
163 isc_result_t
ns_log_setsafechannels(isc_logconfig_t * lcfg)164 ns_log_setsafechannels(isc_logconfig_t *lcfg) {
165 	isc_result_t result;
166 #if ISC_FACILITY != LOG_DAEMON
167 	isc_logdestination_t destination;
168 #endif
169 
170 	if (! ns_g_logstderr) {
171 		result = isc_log_createchannel(lcfg, "default_debug",
172 					       ISC_LOG_TONULL,
173 					       ISC_LOG_DYNAMIC,
174 					       NULL, 0);
175 		if (result != ISC_R_SUCCESS)
176 			goto cleanup;
177 
178 		/*
179 		 * Setting the debug level to zero should get the output
180 		 * discarded a bit faster.
181 		 */
182 		isc_log_setdebuglevel(ns_g_lctx, 0);
183 	} else {
184 		isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
185 	}
186 
187 #if ISC_FACILITY != LOG_DAEMON
188 	destination.facility = ISC_FACILITY;
189 	result = isc_log_createchannel(lcfg, "default_syslog",
190 				       ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
191 				       &destination, 0);
192 	if (result != ISC_R_SUCCESS)
193 		goto cleanup;
194 #endif
195 
196 	result = ISC_R_SUCCESS;
197 
198  cleanup:
199 	return (result);
200 }
201 
202 isc_result_t
ns_log_setdefaultcategory(isc_logconfig_t * lcfg)203 ns_log_setdefaultcategory(isc_logconfig_t *lcfg) {
204 	isc_result_t result;
205 
206 	if (! ns_g_logstderr && ! ns_g_nosyslog) {
207 		result = isc_log_usechannel(lcfg, "default_syslog",
208 					    ISC_LOGCATEGORY_DEFAULT, NULL);
209 		if (result != ISC_R_SUCCESS)
210 			goto cleanup;
211 	}
212 
213 	result = isc_log_usechannel(lcfg, "default_debug",
214 				    ISC_LOGCATEGORY_DEFAULT, NULL);
215 	if (result != ISC_R_SUCCESS)
216 		goto cleanup;
217 
218 	result = ISC_R_SUCCESS;
219 
220  cleanup:
221 	return (result);
222 }
223 
224 isc_result_t
ns_log_setunmatchedcategory(isc_logconfig_t * lcfg)225 ns_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
226 	isc_result_t result;
227 
228 	result = isc_log_usechannel(lcfg, "null",
229 				    NS_LOGCATEGORY_UNMATCHED, NULL);
230 	return (result);
231 }
232 
233 void
ns_log_shutdown(void)234 ns_log_shutdown(void) {
235 	isc_log_destroy(&ns_g_lctx);
236 	isc_log_setcontext(NULL);
237 	dns_log_setcontext(NULL);
238 }
239