Import of bind-9.3.2-P1
[dragonfly.git] / contrib / bind-9.3 / bin / named / log.c
1 /*
2  * Copyright (C) 2004, 2005  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.33.2.1.10.6 2005/05/24 23:58:17 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/result.h>
23
24 #include <isccfg/log.h>
25
26 #include <named/log.h>
27
28 #ifndef ISC_FACILITY
29 #define ISC_FACILITY LOG_DAEMON
30 #endif
31
32 /*
33  * When adding a new category, be sure to add the appropriate
34  * #define to <named/log.h>.
35  */
36 static isc_logcategory_t categories[] = {
37         { "",                           0 },
38         { "client",                     0 },
39         { "network",                    0 },
40         { "update",                     0 },
41         { "queries",                    0 },
42         { "unmatched",                  0 },
43         { "update-security",            0 },
44         { NULL,                         0 }
45 };
46
47 /*
48  * When adding a new module, be sure to add the appropriate
49  * #define to <dns/log.h>.
50  */
51 static isc_logmodule_t modules[] = {
52         { "main",                       0 },
53         { "client",                     0 },
54         { "server",                     0 },
55         { "query",                      0 },
56         { "interfacemgr",               0 },
57         { "update",                     0 },
58         { "xfer-in",                    0 },
59         { "xfer-out",                   0 },
60         { "notify",                     0 },
61         { "control",                    0 },
62         { "lwresd",                     0 },
63         { NULL,                         0 }
64 };
65
66 isc_result_t
67 ns_log_init(isc_boolean_t safe) {
68         isc_result_t result;
69         isc_logconfig_t *lcfg = NULL;
70
71         ns_g_categories = categories;
72         ns_g_modules = modules;
73
74         /*
75          * Setup a logging context.
76          */
77         result = isc_log_create(ns_g_mctx, &ns_g_lctx, &lcfg);
78         if (result != ISC_R_SUCCESS)
79                 return (result);
80
81         isc_log_registercategories(ns_g_lctx, ns_g_categories);
82         isc_log_registermodules(ns_g_lctx, ns_g_modules);
83         isc_log_setcontext(ns_g_lctx);
84         dns_log_init(ns_g_lctx);
85         dns_log_setcontext(ns_g_lctx);
86         cfg_log_init(ns_g_lctx);
87
88         if (safe)
89                 result = ns_log_setsafechannels(lcfg);
90         else
91                 result = ns_log_setdefaultchannels(lcfg);
92         if (result != ISC_R_SUCCESS)
93                 goto cleanup;
94
95         result = ns_log_setdefaultcategory(lcfg);
96         if (result != ISC_R_SUCCESS)
97                 goto cleanup;
98
99         return (ISC_R_SUCCESS);
100
101  cleanup:
102         isc_log_destroy(&ns_g_lctx);
103         isc_log_setcontext(NULL);
104         dns_log_setcontext(NULL);
105
106         return (result);
107 }
108
109 isc_result_t
110 ns_log_setdefaultchannels(isc_logconfig_t *lcfg) {
111         isc_result_t result;
112         isc_logdestination_t destination;
113
114         /*
115          * By default, the logging library makes "default_debug" log to
116          * stderr.  In BIND, we want to override this and log to named.run
117          * instead, unless the the -g option was given.
118          */
119         if (! ns_g_logstderr) {
120                 destination.file.stream = NULL;
121                 destination.file.name = "named.run";
122                 destination.file.versions = ISC_LOG_ROLLNEVER;
123                 destination.file.maximum_size = 0;
124                 result = isc_log_createchannel(lcfg, "default_debug",
125                                                ISC_LOG_TOFILE,
126                                                ISC_LOG_DYNAMIC,
127                                                &destination,
128                                                ISC_LOG_PRINTTIME|
129                                                ISC_LOG_DEBUGONLY);
130                 if (result != ISC_R_SUCCESS)
131                         goto cleanup;
132         }
133
134 #if ISC_FACILITY != LOG_DAEMON
135         destination.facility = ISC_FACILITY;
136         result = isc_log_createchannel(lcfg, "default_syslog",
137                                        ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
138                                        &destination, 0);
139         if (result != ISC_R_SUCCESS)
140                 goto cleanup;
141 #endif
142
143         /*
144          * Set the initial debug level.
145          */
146         isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
147
148         result = ISC_R_SUCCESS;
149
150  cleanup:
151         return (result);
152 }
153
154 isc_result_t
155 ns_log_setsafechannels(isc_logconfig_t *lcfg) {
156         isc_result_t result;
157 #if ISC_FACILITY != LOG_DAEMON
158         isc_logdestination_t destination;
159 #endif
160
161         if (! ns_g_logstderr) {
162                 result = isc_log_createchannel(lcfg, "default_debug",
163                                                ISC_LOG_TONULL,
164                                                ISC_LOG_DYNAMIC,
165                                                NULL, 0);
166                 if (result != ISC_R_SUCCESS)
167                         goto cleanup;
168
169                 /*
170                  * Setting the debug level to zero should get the output
171                  * discarded a bit faster.
172                  */
173                 isc_log_setdebuglevel(ns_g_lctx, 0);
174         } else {
175                 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
176         }
177
178 #if ISC_FACILITY != LOG_DAEMON
179         destination.facility = ISC_FACILITY;
180         result = isc_log_createchannel(lcfg, "default_syslog",
181                                        ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
182                                        &destination, 0);
183         if (result != ISC_R_SUCCESS)
184                 goto cleanup;
185 #endif
186
187         result = ISC_R_SUCCESS;
188
189  cleanup:
190         return (result);
191 }
192
193 isc_result_t
194 ns_log_setdefaultcategory(isc_logconfig_t *lcfg) {
195         isc_result_t result;
196
197         if (! ns_g_logstderr) {
198                 result = isc_log_usechannel(lcfg, "default_syslog",
199                                             ISC_LOGCATEGORY_DEFAULT, NULL);
200                 if (result != ISC_R_SUCCESS)
201                         goto cleanup;
202         }
203
204         result = isc_log_usechannel(lcfg, "default_debug",
205                                     ISC_LOGCATEGORY_DEFAULT, NULL);
206         if (result != ISC_R_SUCCESS)
207                 goto cleanup;
208
209         result = ISC_R_SUCCESS;
210
211  cleanup:
212         return (result);
213 }
214
215 isc_result_t
216 ns_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
217         isc_result_t result;
218
219         result = isc_log_usechannel(lcfg, "null",
220                                     NS_LOGCATEGORY_UNMATCHED, NULL);
221         return (result);
222 }
223
224 void
225 ns_log_shutdown(void) {
226         isc_log_destroy(&ns_g_lctx);
227         isc_log_setcontext(NULL);
228         dns_log_setcontext(NULL);
229 }