]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/bind9/bin/named/log.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / bind9 / bin / named / log.c
1 /*
2  * Copyright (C) 2004-2006  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2002  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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.37.18.6 2006/06/09 00:54:08 marka 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         { NULL,                         0 }
48 };
49
50 /*%
51  * When adding a new module, be sure to add the appropriate
52  * #define to <dns/log.h>.
53  */
54 static isc_logmodule_t modules[] = {
55         { "main",                       0 },
56         { "client",                     0 },
57         { "server",                     0 },
58         { "query",                      0 },
59         { "interfacemgr",               0 },
60         { "update",                     0 },
61         { "xfer-in",                    0 },
62         { "xfer-out",                   0 },
63         { "notify",                     0 },
64         { "control",                    0 },
65         { "lwresd",                     0 },
66         { NULL,                         0 }
67 };
68
69 isc_result_t
70 ns_log_init(isc_boolean_t safe) {
71         isc_result_t result;
72         isc_logconfig_t *lcfg = NULL;
73
74         ns_g_categories = categories;
75         ns_g_modules = modules;
76
77         /*
78          * Setup a logging context.
79          */
80         result = isc_log_create(ns_g_mctx, &ns_g_lctx, &lcfg);
81         if (result != ISC_R_SUCCESS)
82                 return (result);
83
84         /*
85          * named-checktool.c:setup_logging() needs to be kept in sync.
86          */
87         isc_log_registercategories(ns_g_lctx, ns_g_categories);
88         isc_log_registermodules(ns_g_lctx, ns_g_modules);
89         isc_log_setcontext(ns_g_lctx);
90         dns_log_init(ns_g_lctx);
91         dns_log_setcontext(ns_g_lctx);
92         cfg_log_init(ns_g_lctx);
93
94         if (safe)
95                 result = ns_log_setsafechannels(lcfg);
96         else
97                 result = ns_log_setdefaultchannels(lcfg);
98         if (result != ISC_R_SUCCESS)
99                 goto cleanup;
100
101         result = ns_log_setdefaultcategory(lcfg);
102         if (result != ISC_R_SUCCESS)
103                 goto cleanup;
104
105         return (ISC_R_SUCCESS);
106
107  cleanup:
108         isc_log_destroy(&ns_g_lctx);
109         isc_log_setcontext(NULL);
110         dns_log_setcontext(NULL);
111
112         return (result);
113 }
114
115 isc_result_t
116 ns_log_setdefaultchannels(isc_logconfig_t *lcfg) {
117         isc_result_t result;
118         isc_logdestination_t destination;
119
120         /*
121          * By default, the logging library makes "default_debug" log to
122          * stderr.  In BIND, we want to override this and log to named.run
123          * instead, unless the the -g option was given.
124          */
125         if (! ns_g_logstderr) {
126                 destination.file.stream = NULL;
127                 destination.file.name = "named.run";
128                 destination.file.versions = ISC_LOG_ROLLNEVER;
129                 destination.file.maximum_size = 0;
130                 result = isc_log_createchannel(lcfg, "default_debug",
131                                                ISC_LOG_TOFILE,
132                                                ISC_LOG_DYNAMIC,
133                                                &destination,
134                                                ISC_LOG_PRINTTIME|
135                                                ISC_LOG_DEBUGONLY);
136                 if (result != ISC_R_SUCCESS)
137                         goto cleanup;
138         }
139
140 #if ISC_FACILITY != LOG_DAEMON
141         destination.facility = ISC_FACILITY;
142         result = isc_log_createchannel(lcfg, "default_syslog",
143                                        ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
144                                        &destination, 0);
145         if (result != ISC_R_SUCCESS)
146                 goto cleanup;
147 #endif
148
149         /*
150          * Set the initial debug level.
151          */
152         isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
153
154         result = ISC_R_SUCCESS;
155
156  cleanup:
157         return (result);
158 }
159
160 isc_result_t
161 ns_log_setsafechannels(isc_logconfig_t *lcfg) {
162         isc_result_t result;
163 #if ISC_FACILITY != LOG_DAEMON
164         isc_logdestination_t destination;
165 #endif
166
167         if (! ns_g_logstderr) {
168                 result = isc_log_createchannel(lcfg, "default_debug",
169                                                ISC_LOG_TONULL,
170                                                ISC_LOG_DYNAMIC,
171                                                NULL, 0);
172                 if (result != ISC_R_SUCCESS)
173                         goto cleanup;
174
175                 /*
176                  * Setting the debug level to zero should get the output
177                  * discarded a bit faster.
178                  */
179                 isc_log_setdebuglevel(ns_g_lctx, 0);
180         } else {
181                 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
182         }
183
184 #if ISC_FACILITY != LOG_DAEMON
185         destination.facility = ISC_FACILITY;
186         result = isc_log_createchannel(lcfg, "default_syslog",
187                                        ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
188                                        &destination, 0);
189         if (result != ISC_R_SUCCESS)
190                 goto cleanup;
191 #endif
192
193         result = ISC_R_SUCCESS;
194
195  cleanup:
196         return (result);
197 }
198
199 isc_result_t
200 ns_log_setdefaultcategory(isc_logconfig_t *lcfg) {
201         isc_result_t result;
202
203         if (! ns_g_logstderr) {
204                 result = isc_log_usechannel(lcfg, "default_syslog",
205                                             ISC_LOGCATEGORY_DEFAULT, NULL);
206                 if (result != ISC_R_SUCCESS)
207                         goto cleanup;
208         }
209
210         result = isc_log_usechannel(lcfg, "default_debug",
211                                     ISC_LOGCATEGORY_DEFAULT, NULL);
212         if (result != ISC_R_SUCCESS)
213                 goto cleanup;
214
215         result = ISC_R_SUCCESS;
216
217  cleanup:
218         return (result);
219 }
220
221 isc_result_t
222 ns_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
223         isc_result_t result;
224
225         result = isc_log_usechannel(lcfg, "null",
226                                     NS_LOGCATEGORY_UNMATCHED, NULL);
227         return (result);
228 }
229
230 void
231 ns_log_shutdown(void) {
232         isc_log_destroy(&ns_g_lctx);
233         isc_log_setcontext(NULL);
234         dns_log_setcontext(NULL);
235 }