etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / named / log.c
blob5cf75919bc73f8ada060dca10a0ca3a5aec1bfc8
1 /* $NetBSD: log.c,v 1.5 2014/12/10 04:37:51 christos Exp $ */
3 /*
4 * Copyright (C) 2004-2007, 2009, 2013 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2002 Internet Software Consortium.
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.
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.
20 /* Id: log.c,v 1.49 2009/01/07 01:46:40 jinmei Exp */
22 /*! \file */
24 #include <config.h>
26 #include <isc/result.h>
28 #include <isccfg/log.h>
30 #include <named/log.h>
32 #ifndef ISC_FACILITY
33 #define ISC_FACILITY LOG_DAEMON
34 #endif
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.
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 }
53 /*%
54 * When adding a new module, be sure to add the appropriate
55 * \#define to <dns/log.h>.
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 }
72 isc_result_t
73 ns_log_init(isc_boolean_t safe) {
74 isc_result_t result;
75 isc_logconfig_t *lcfg = NULL;
77 ns_g_categories = categories;
78 ns_g_modules = modules;
81 * Setup a logging context.
83 result = isc_log_create(ns_g_mctx, &ns_g_lctx, &lcfg);
84 if (result != ISC_R_SUCCESS)
85 return (result);
88 * named-checktool.c:setup_logging() needs to be kept in sync.
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);
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;
104 result = ns_log_setdefaultcategory(lcfg);
105 if (result != ISC_R_SUCCESS)
106 goto cleanup;
108 return (ISC_R_SUCCESS);
110 cleanup:
111 isc_log_destroy(&ns_g_lctx);
112 isc_log_setcontext(NULL);
113 dns_log_setcontext(NULL);
115 return (result);
118 isc_result_t
119 ns_log_setdefaultchannels(isc_logconfig_t *lcfg) {
120 isc_result_t result;
121 isc_logdestination_t destination;
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.
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;
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
153 * Set the initial debug level.
155 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
157 result = ISC_R_SUCCESS;
159 cleanup:
160 return (result);
163 isc_result_t
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
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;
179 * Setting the debug level to zero should get the output
180 * discarded a bit faster.
182 isc_log_setdebuglevel(ns_g_lctx, 0);
183 } else {
184 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
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
196 result = ISC_R_SUCCESS;
198 cleanup:
199 return (result);
202 isc_result_t
203 ns_log_setdefaultcategory(isc_logconfig_t *lcfg) {
204 isc_result_t result;
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;
213 result = isc_log_usechannel(lcfg, "default_debug",
214 ISC_LOGCATEGORY_DEFAULT, NULL);
215 if (result != ISC_R_SUCCESS)
216 goto cleanup;
218 result = ISC_R_SUCCESS;
220 cleanup:
221 return (result);
224 isc_result_t
225 ns_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
226 isc_result_t result;
228 result = isc_log_usechannel(lcfg, "null",
229 NS_LOGCATEGORY_UNMATCHED, NULL);
230 return (result);
233 void
234 ns_log_shutdown(void) {
235 isc_log_destroy(&ns_g_lctx);
236 isc_log_setcontext(NULL);
237 dns_log_setcontext(NULL);