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