log.c revision 1.2 1 1.2 christos /* $NetBSD: log.c,v 1.2 2018/08/12 13:02:37 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos *
6 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
7 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
8 1.1 christos * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 1.1 christos *
10 1.1 christos * See the COPYRIGHT file distributed with this work for additional
11 1.1 christos * information regarding copyright ownership.
12 1.1 christos */
13 1.1 christos
14 1.1 christos /*! \file */
15 1.1 christos
16 1.1 christos #include <config.h>
17 1.1 christos
18 1.1 christos #include <errno.h>
19 1.1 christos #include <stdlib.h>
20 1.1 christos #include <limits.h>
21 1.1 christos #include <time.h>
22 1.1 christos
23 1.1 christos #include <sys/types.h> /* dev_t FreeBSD 2.1 */
24 1.1 christos
25 1.1 christos #include <isc/dir.h>
26 1.1 christos #include <isc/file.h>
27 1.1 christos #include <isc/log.h>
28 1.1 christos #include <isc/magic.h>
29 1.1 christos #include <isc/mem.h>
30 1.1 christos #include <isc/msgs.h>
31 1.1 christos #include <isc/print.h>
32 1.1 christos #include <isc/stat.h>
33 1.1 christos #include <isc/stdio.h>
34 1.1 christos #include <isc/string.h>
35 1.1 christos #include <isc/time.h>
36 1.1 christos #include <isc/util.h>
37 1.1 christos
38 1.1 christos #define LCTX_MAGIC ISC_MAGIC('L', 'c', 't', 'x')
39 1.1 christos #define VALID_CONTEXT(lctx) ISC_MAGIC_VALID(lctx, LCTX_MAGIC)
40 1.1 christos
41 1.1 christos #define LCFG_MAGIC ISC_MAGIC('L', 'c', 'f', 'g')
42 1.1 christos #define VALID_CONFIG(lcfg) ISC_MAGIC_VALID(lcfg, LCFG_MAGIC)
43 1.1 christos
44 1.1 christos /*
45 1.1 christos * XXXDCL make dynamic?
46 1.1 christos */
47 1.1 christos #define LOG_BUFFER_SIZE (8 * 1024)
48 1.1 christos
49 1.1 christos #ifndef PATH_MAX
50 1.1 christos #define PATH_MAX 1024 /* AIX and others don't define this. */
51 1.1 christos #endif
52 1.1 christos
53 1.1 christos /*!
54 1.1 christos * This is the structure that holds each named channel. A simple linked
55 1.1 christos * list chains all of the channels together, so an individual channel is
56 1.1 christos * found by doing strcmp()s with the names down the list. Their should
57 1.1 christos * be no performance penalty from this as it is expected that the number
58 1.1 christos * of named channels will be no more than a dozen or so, and name lookups
59 1.1 christos * from the head of the list are only done when isc_log_usechannel() is
60 1.1 christos * called, which should also be very infrequent.
61 1.1 christos */
62 1.1 christos typedef struct isc_logchannel isc_logchannel_t;
63 1.1 christos
64 1.1 christos struct isc_logchannel {
65 1.1 christos char * name;
66 1.1 christos unsigned int type;
67 1.1 christos int level;
68 1.1 christos unsigned int flags;
69 1.1 christos isc_logdestination_t destination;
70 1.1 christos ISC_LINK(isc_logchannel_t) link;
71 1.1 christos };
72 1.1 christos
73 1.1 christos /*!
74 1.1 christos * The logchannellist structure associates categories and modules with
75 1.1 christos * channels. First the appropriate channellist is found based on the
76 1.1 christos * category, and then each structure in the linked list is checked for
77 1.1 christos * a matching module. It is expected that the number of channels
78 1.1 christos * associated with any given category will be very short, no more than
79 1.1 christos * three or four in the more unusual cases.
80 1.1 christos */
81 1.1 christos typedef struct isc_logchannellist isc_logchannellist_t;
82 1.1 christos
83 1.1 christos struct isc_logchannellist {
84 1.1 christos const isc_logmodule_t * module;
85 1.1 christos isc_logchannel_t * channel;
86 1.1 christos ISC_LINK(isc_logchannellist_t) link;
87 1.1 christos };
88 1.1 christos
89 1.1 christos /*!
90 1.1 christos * This structure is used to remember messages for pruning via
91 1.1 christos * isc_log_[v]write1().
92 1.1 christos */
93 1.1 christos typedef struct isc_logmessage isc_logmessage_t;
94 1.1 christos
95 1.1 christos struct isc_logmessage {
96 1.1 christos char * text;
97 1.1 christos isc_time_t time;
98 1.1 christos ISC_LINK(isc_logmessage_t) link;
99 1.1 christos };
100 1.1 christos
101 1.1 christos /*!
102 1.1 christos * The isc_logconfig structure is used to store the configurable information
103 1.1 christos * about where messages are actually supposed to be sent -- the information
104 1.1 christos * that could changed based on some configuration file, as opposed to the
105 1.1 christos * the category/module specification of isc_log_[v]write[1] that is compiled
106 1.1 christos * into a program, or the debug_level which is dynamic state information.
107 1.1 christos */
108 1.1 christos struct isc_logconfig {
109 1.1 christos unsigned int magic;
110 1.1 christos isc_log_t * lctx;
111 1.1 christos ISC_LIST(isc_logchannel_t) channels;
112 1.1 christos ISC_LIST(isc_logchannellist_t) *channellists;
113 1.1 christos unsigned int channellist_count;
114 1.1 christos unsigned int duplicate_interval;
115 1.1 christos int highest_level;
116 1.1 christos char * tag;
117 1.1 christos isc_boolean_t dynamic;
118 1.1 christos };
119 1.1 christos
120 1.1 christos /*!
121 1.1 christos * This isc_log structure provides the context for the isc_log functions.
122 1.1 christos * The log context locks itself in isc_log_doit, the internal backend to
123 1.1 christos * isc_log_write. The locking is necessary both to provide exclusive access
124 1.1 christos * to the buffer into which the message is formatted and to guard against
125 1.1 christos * competing threads trying to write to the same syslog resource. (On
126 1.1 christos * some systems, such as BSD/OS, stdio is thread safe but syslog is not.)
127 1.1 christos * Unfortunately, the lock cannot guard against a _different_ logging
128 1.1 christos * context in the same program competing for syslog's attention. Thus
129 1.1 christos * There Can Be Only One, but this is not enforced.
130 1.1 christos * XXXDCL enforce it?
131 1.1 christos *
132 1.1 christos * Note that the category and module information is not locked.
133 1.1 christos * This is because in the usual case, only one isc_log_t is ever created
134 1.1 christos * in a program, and the category/module registration happens only once.
135 1.1 christos * XXXDCL it might be wise to add more locking overall.
136 1.1 christos */
137 1.1 christos struct isc_log {
138 1.1 christos /* Not locked. */
139 1.1 christos unsigned int magic;
140 1.1 christos isc_mem_t * mctx;
141 1.1 christos isc_logcategory_t * categories;
142 1.1 christos unsigned int category_count;
143 1.1 christos isc_logmodule_t * modules;
144 1.1 christos unsigned int module_count;
145 1.1 christos int debug_level;
146 1.1 christos isc_mutex_t lock;
147 1.1 christos /* Locked by isc_log lock. */
148 1.1 christos isc_logconfig_t * logconfig;
149 1.1 christos char buffer[LOG_BUFFER_SIZE];
150 1.1 christos ISC_LIST(isc_logmessage_t) messages;
151 1.1 christos };
152 1.1 christos
153 1.1 christos /*!
154 1.1 christos * Used when ISC_LOG_PRINTLEVEL is enabled for a channel.
155 1.1 christos */
156 1.1 christos static const char *log_level_strings[] = {
157 1.1 christos "debug",
158 1.1 christos "info",
159 1.1 christos "notice",
160 1.1 christos "warning",
161 1.1 christos "error",
162 1.1 christos "critical"
163 1.1 christos };
164 1.1 christos
165 1.1 christos /*!
166 1.1 christos * Used to convert ISC_LOG_* priorities into syslog priorities.
167 1.1 christos * XXXDCL This will need modification for NT.
168 1.1 christos */
169 1.1 christos static const int syslog_map[] = {
170 1.1 christos LOG_DEBUG,
171 1.1 christos LOG_INFO,
172 1.1 christos LOG_NOTICE,
173 1.1 christos LOG_WARNING,
174 1.1 christos LOG_ERR,
175 1.1 christos LOG_CRIT
176 1.1 christos };
177 1.1 christos
178 1.1 christos /*!
179 1.1 christos * When adding new categories, a corresponding ISC_LOGCATEGORY_foo
180 1.1 christos * definition needs to be added to <isc/log.h>.
181 1.1 christos *
182 1.1 christos * The default category is provided so that the internal default can
183 1.1 christos * be overridden. Since the default is always looked up as the first
184 1.1 christos * channellist in the log context, it must come first in isc_categories[].
185 1.1 christos */
186 1.1 christos LIBISC_EXTERNAL_DATA isc_logcategory_t isc_categories[] = {
187 1.1 christos { "default", 0 }, /* "default" must come first. */
188 1.1 christos { "general", 0 },
189 1.1 christos { NULL, 0 }
190 1.1 christos };
191 1.1 christos
192 1.1 christos /*!
193 1.1 christos * See above comment for categories on LIBISC_EXTERNAL_DATA, and apply it to modules.
194 1.1 christos */
195 1.1 christos LIBISC_EXTERNAL_DATA isc_logmodule_t isc_modules[] = {
196 1.1 christos { "socket", 0 },
197 1.1 christos { "time", 0 },
198 1.1 christos { "interface", 0 },
199 1.1 christos { "timer", 0 },
200 1.1 christos { "file", 0 },
201 1.1 christos { "other", 0 },
202 1.1 christos { NULL, 0 }
203 1.1 christos };
204 1.1 christos
205 1.1 christos /*!
206 1.1 christos * This essentially constant structure must be filled in at run time,
207 1.1 christos * because its channel member is pointed to a channel that is created
208 1.1 christos * dynamically with isc_log_createchannel.
209 1.1 christos */
210 1.1 christos static isc_logchannellist_t default_channel;
211 1.1 christos
212 1.1 christos /*!
213 1.1 christos * libisc logs to this context.
214 1.1 christos */
215 1.1 christos LIBISC_EXTERNAL_DATA isc_log_t *isc_lctx = NULL;
216 1.1 christos
217 1.1 christos /*!
218 1.1 christos * Forward declarations.
219 1.1 christos */
220 1.1 christos static isc_result_t
221 1.1 christos assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
222 1.1 christos const isc_logmodule_t *module, isc_logchannel_t *channel);
223 1.1 christos
224 1.1 christos static isc_result_t
225 1.1 christos sync_channellist(isc_logconfig_t *lcfg);
226 1.1 christos
227 1.1 christos static isc_result_t
228 1.1 christos greatest_version(isc_logfile_t *file, int versions, int *greatest);
229 1.1 christos
230 1.1 christos static void
231 1.1 christos isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
232 1.1 christos isc_logmodule_t *module, int level, isc_boolean_t write_once,
233 1.1 christos isc_msgcat_t *msgcat, int msgset, int msg,
234 1.1 christos const char *format, va_list args)
235 1.1 christos ISC_FORMAT_PRINTF(9, 0);
236 1.1 christos
237 1.1 christos /*@{*/
238 1.1 christos /*!
239 1.1 christos * Convenience macros.
240 1.1 christos */
241 1.1 christos
242 1.1 christos #define FACILITY(channel) (channel->destination.facility)
243 1.1 christos #define FILE_NAME(channel) (channel->destination.file.name)
244 1.1 christos #define FILE_STREAM(channel) (channel->destination.file.stream)
245 1.1 christos #define FILE_VERSIONS(channel) (channel->destination.file.versions)
246 1.1 christos #define FILE_SUFFIX(channel) (channel->destination.file.suffix)
247 1.1 christos #define FILE_MAXSIZE(channel) (channel->destination.file.maximum_size)
248 1.1 christos #define FILE_MAXREACHED(channel) (channel->destination.file.maximum_reached)
249 1.1 christos
250 1.1 christos /*@}*/
251 1.1 christos /****
252 1.1 christos **** Public interfaces.
253 1.1 christos ****/
254 1.1 christos
255 1.1 christos /*
256 1.1 christos * Establish a new logging context, with default channels.
257 1.1 christos */
258 1.1 christos isc_result_t
259 1.1 christos isc_log_create(isc_mem_t *mctx, isc_log_t **lctxp, isc_logconfig_t **lcfgp) {
260 1.1 christos isc_log_t *lctx;
261 1.1 christos isc_logconfig_t *lcfg = NULL;
262 1.1 christos isc_result_t result;
263 1.1 christos
264 1.1 christos REQUIRE(mctx != NULL);
265 1.1 christos REQUIRE(lctxp != NULL && *lctxp == NULL);
266 1.1 christos REQUIRE(lcfgp == NULL || *lcfgp == NULL);
267 1.1 christos
268 1.1 christos lctx = isc_mem_get(mctx, sizeof(*lctx));
269 1.1 christos if (lctx != NULL) {
270 1.1 christos lctx->mctx = NULL;
271 1.1 christos isc_mem_attach(mctx, &lctx->mctx);
272 1.1 christos lctx->categories = NULL;
273 1.1 christos lctx->category_count = 0;
274 1.1 christos lctx->modules = NULL;
275 1.1 christos lctx->module_count = 0;
276 1.1 christos lctx->debug_level = 0;
277 1.1 christos
278 1.1 christos ISC_LIST_INIT(lctx->messages);
279 1.1 christos
280 1.1 christos result = isc_mutex_init(&lctx->lock);
281 1.1 christos if (result != ISC_R_SUCCESS) {
282 1.1 christos isc_mem_putanddetach(&mctx, lctx, sizeof(*lctx));
283 1.1 christos return (result);
284 1.1 christos }
285 1.1 christos
286 1.1 christos /*
287 1.1 christos * Normally setting the magic number is the last step done
288 1.1 christos * in a creation function, but a valid log context is needed
289 1.1 christos * by isc_log_registercategories and isc_logconfig_create.
290 1.1 christos * If either fails, the lctx is destroyed and not returned
291 1.1 christos * to the caller.
292 1.1 christos */
293 1.1 christos lctx->magic = LCTX_MAGIC;
294 1.1 christos
295 1.1 christos isc_log_registercategories(lctx, isc_categories);
296 1.1 christos isc_log_registermodules(lctx, isc_modules);
297 1.1 christos result = isc_logconfig_create(lctx, &lcfg);
298 1.1 christos
299 1.1 christos } else
300 1.1 christos result = ISC_R_NOMEMORY;
301 1.1 christos
302 1.1 christos if (result == ISC_R_SUCCESS)
303 1.1 christos result = sync_channellist(lcfg);
304 1.1 christos
305 1.1 christos if (result == ISC_R_SUCCESS) {
306 1.1 christos lctx->logconfig = lcfg;
307 1.1 christos
308 1.1 christos *lctxp = lctx;
309 1.1 christos if (lcfgp != NULL)
310 1.1 christos *lcfgp = lcfg;
311 1.1 christos
312 1.1 christos } else {
313 1.1 christos if (lcfg != NULL)
314 1.1 christos isc_logconfig_destroy(&lcfg);
315 1.1 christos if (lctx != NULL)
316 1.1 christos isc_log_destroy(&lctx);
317 1.1 christos }
318 1.1 christos
319 1.1 christos return (result);
320 1.1 christos }
321 1.1 christos
322 1.1 christos isc_result_t
323 1.1 christos isc_logconfig_create(isc_log_t *lctx, isc_logconfig_t **lcfgp) {
324 1.1 christos isc_logconfig_t *lcfg;
325 1.1 christos isc_logdestination_t destination;
326 1.1 christos isc_result_t result = ISC_R_SUCCESS;
327 1.1 christos int level = ISC_LOG_INFO;
328 1.1 christos
329 1.1 christos REQUIRE(lcfgp != NULL && *lcfgp == NULL);
330 1.1 christos REQUIRE(VALID_CONTEXT(lctx));
331 1.1 christos
332 1.1 christos lcfg = isc_mem_get(lctx->mctx, sizeof(*lcfg));
333 1.1 christos
334 1.1 christos if (lcfg != NULL) {
335 1.1 christos lcfg->lctx = lctx;
336 1.1 christos lcfg->channellists = NULL;
337 1.1 christos lcfg->channellist_count = 0;
338 1.1 christos lcfg->duplicate_interval = 0;
339 1.1 christos lcfg->highest_level = level;
340 1.1 christos lcfg->tag = NULL;
341 1.1 christos lcfg->dynamic = ISC_FALSE;
342 1.1 christos
343 1.1 christos ISC_LIST_INIT(lcfg->channels);
344 1.1 christos
345 1.1 christos /*
346 1.1 christos * Normally the magic number is the last thing set in the
347 1.1 christos * structure, but isc_log_createchannel() needs a valid
348 1.1 christos * config. If the channel creation fails, the lcfg is not
349 1.1 christos * returned to the caller.
350 1.1 christos */
351 1.1 christos lcfg->magic = LCFG_MAGIC;
352 1.1 christos
353 1.1 christos } else
354 1.1 christos result = ISC_R_NOMEMORY;
355 1.1 christos
356 1.1 christos /*
357 1.1 christos * Create the default channels:
358 1.1 christos * default_syslog, default_stderr, default_debug and null.
359 1.1 christos */
360 1.1 christos if (result == ISC_R_SUCCESS) {
361 1.1 christos destination.facility = LOG_DAEMON;
362 1.1 christos result = isc_log_createchannel(lcfg, "default_syslog",
363 1.1 christos ISC_LOG_TOSYSLOG, level,
364 1.1 christos &destination, 0);
365 1.1 christos }
366 1.1 christos
367 1.1 christos if (result == ISC_R_SUCCESS) {
368 1.1 christos destination.file.stream = stderr;
369 1.1 christos destination.file.name = NULL;
370 1.1 christos destination.file.versions = ISC_LOG_ROLLNEVER;
371 1.1 christos destination.file.suffix = isc_log_rollsuffix_increment;
372 1.1 christos destination.file.maximum_size = 0;
373 1.1 christos result = isc_log_createchannel(lcfg, "default_stderr",
374 1.1 christos ISC_LOG_TOFILEDESC,
375 1.1 christos level,
376 1.1 christos &destination,
377 1.1 christos ISC_LOG_PRINTTIME);
378 1.1 christos }
379 1.1 christos
380 1.1 christos if (result == ISC_R_SUCCESS) {
381 1.1 christos /*
382 1.1 christos * Set the default category's channel to default_stderr,
383 1.1 christos * which is at the head of the channels list because it was
384 1.1 christos * just created.
385 1.1 christos */
386 1.1 christos default_channel.channel = ISC_LIST_HEAD(lcfg->channels);
387 1.1 christos
388 1.1 christos destination.file.stream = stderr;
389 1.1 christos destination.file.name = NULL;
390 1.1 christos destination.file.versions = ISC_LOG_ROLLNEVER;
391 1.1 christos destination.file.suffix = isc_log_rollsuffix_increment;
392 1.1 christos destination.file.maximum_size = 0;
393 1.1 christos result = isc_log_createchannel(lcfg, "default_debug",
394 1.1 christos ISC_LOG_TOFILEDESC,
395 1.1 christos ISC_LOG_DYNAMIC,
396 1.1 christos &destination,
397 1.1 christos ISC_LOG_PRINTTIME);
398 1.1 christos }
399 1.1 christos
400 1.1 christos if (result == ISC_R_SUCCESS)
401 1.1 christos result = isc_log_createchannel(lcfg, "null",
402 1.1 christos ISC_LOG_TONULL,
403 1.1 christos ISC_LOG_DYNAMIC,
404 1.1 christos NULL, 0);
405 1.1 christos
406 1.1 christos if (result == ISC_R_SUCCESS)
407 1.1 christos *lcfgp = lcfg;
408 1.1 christos
409 1.1 christos else
410 1.1 christos if (lcfg != NULL)
411 1.1 christos isc_logconfig_destroy(&lcfg);
412 1.1 christos
413 1.1 christos return (result);
414 1.1 christos }
415 1.1 christos
416 1.1 christos isc_logconfig_t *
417 1.1 christos isc_logconfig_get(isc_log_t *lctx) {
418 1.1 christos REQUIRE(VALID_CONTEXT(lctx));
419 1.1 christos
420 1.1 christos ENSURE(lctx->logconfig != NULL);
421 1.1 christos
422 1.1 christos return (lctx->logconfig);
423 1.1 christos }
424 1.1 christos
425 1.1 christos isc_result_t
426 1.1 christos isc_logconfig_use(isc_log_t *lctx, isc_logconfig_t *lcfg) {
427 1.1 christos isc_logconfig_t *old_cfg;
428 1.1 christos isc_result_t result;
429 1.1 christos
430 1.1 christos REQUIRE(VALID_CONTEXT(lctx));
431 1.1 christos REQUIRE(VALID_CONFIG(lcfg));
432 1.1 christos REQUIRE(lcfg->lctx == lctx);
433 1.1 christos
434 1.1 christos /*
435 1.1 christos * Ensure that lcfg->channellist_count == lctx->category_count.
436 1.1 christos * They won't be equal if isc_log_usechannel has not been called
437 1.1 christos * since any call to isc_log_registercategories.
438 1.1 christos */
439 1.1 christos result = sync_channellist(lcfg);
440 1.1 christos if (result != ISC_R_SUCCESS)
441 1.1 christos return (result);
442 1.1 christos
443 1.1 christos LOCK(&lctx->lock);
444 1.1 christos
445 1.1 christos old_cfg = lctx->logconfig;
446 1.1 christos lctx->logconfig = lcfg;
447 1.1 christos
448 1.1 christos UNLOCK(&lctx->lock);
449 1.1 christos
450 1.1 christos isc_logconfig_destroy(&old_cfg);
451 1.1 christos
452 1.1 christos return (ISC_R_SUCCESS);
453 1.1 christos }
454 1.1 christos
455 1.1 christos void
456 1.1 christos isc_log_destroy(isc_log_t **lctxp) {
457 1.1 christos isc_log_t *lctx;
458 1.1 christos isc_logconfig_t *lcfg;
459 1.1 christos isc_mem_t *mctx;
460 1.1 christos isc_logmessage_t *message;
461 1.1 christos
462 1.1 christos REQUIRE(lctxp != NULL && VALID_CONTEXT(*lctxp));
463 1.1 christos
464 1.1 christos lctx = *lctxp;
465 1.1 christos mctx = lctx->mctx;
466 1.1 christos
467 1.1 christos if (lctx->logconfig != NULL) {
468 1.1 christos lcfg = lctx->logconfig;
469 1.1 christos lctx->logconfig = NULL;
470 1.1 christos isc_logconfig_destroy(&lcfg);
471 1.1 christos }
472 1.1 christos
473 1.1 christos DESTROYLOCK(&lctx->lock);
474 1.1 christos
475 1.1 christos while ((message = ISC_LIST_HEAD(lctx->messages)) != NULL) {
476 1.1 christos ISC_LIST_UNLINK(lctx->messages, message, link);
477 1.1 christos
478 1.1 christos isc_mem_put(mctx, message,
479 1.1 christos sizeof(*message) + strlen(message->text) + 1);
480 1.1 christos }
481 1.1 christos
482 1.1 christos lctx->buffer[0] = '\0';
483 1.1 christos lctx->debug_level = 0;
484 1.1 christos lctx->categories = NULL;
485 1.1 christos lctx->category_count = 0;
486 1.1 christos lctx->modules = NULL;
487 1.1 christos lctx->module_count = 0;
488 1.1 christos lctx->mctx = NULL;
489 1.1 christos lctx->magic = 0;
490 1.1 christos
491 1.1 christos isc_mem_putanddetach(&mctx, lctx, sizeof(*lctx));
492 1.1 christos
493 1.1 christos *lctxp = NULL;
494 1.1 christos }
495 1.1 christos
496 1.1 christos void
497 1.1 christos isc_logconfig_destroy(isc_logconfig_t **lcfgp) {
498 1.1 christos isc_logconfig_t *lcfg;
499 1.1 christos isc_mem_t *mctx;
500 1.1 christos isc_logchannel_t *channel;
501 1.1 christos isc_logchannellist_t *item;
502 1.1 christos char *filename;
503 1.1 christos unsigned int i;
504 1.1 christos
505 1.1 christos REQUIRE(lcfgp != NULL && VALID_CONFIG(*lcfgp));
506 1.1 christos
507 1.1 christos lcfg = *lcfgp;
508 1.1 christos
509 1.1 christos /*
510 1.1 christos * This function cannot be called with a logconfig that is in
511 1.1 christos * use by a log context.
512 1.1 christos */
513 1.1 christos REQUIRE(lcfg->lctx != NULL && lcfg->lctx->logconfig != lcfg);
514 1.1 christos
515 1.1 christos mctx = lcfg->lctx->mctx;
516 1.1 christos
517 1.1 christos while ((channel = ISC_LIST_HEAD(lcfg->channels)) != NULL) {
518 1.1 christos ISC_LIST_UNLINK(lcfg->channels, channel, link);
519 1.1 christos
520 1.1 christos if (channel->type == ISC_LOG_TOFILE) {
521 1.1 christos /*
522 1.1 christos * The filename for the channel may have ultimately
523 1.1 christos * started its life in user-land as a const string,
524 1.1 christos * but in isc_log_createchannel it gets copied
525 1.1 christos * into writable memory and is not longer truly const.
526 1.1 christos */
527 1.1 christos DE_CONST(FILE_NAME(channel), filename);
528 1.1 christos isc_mem_free(mctx, filename);
529 1.1 christos
530 1.1 christos if (FILE_STREAM(channel) != NULL)
531 1.1 christos (void)fclose(FILE_STREAM(channel));
532 1.1 christos }
533 1.1 christos
534 1.1 christos isc_mem_free(mctx, channel->name);
535 1.1 christos isc_mem_put(mctx, channel, sizeof(*channel));
536 1.1 christos }
537 1.1 christos
538 1.1 christos for (i = 0; i < lcfg->channellist_count; i++)
539 1.1 christos while ((item = ISC_LIST_HEAD(lcfg->channellists[i])) != NULL) {
540 1.1 christos ISC_LIST_UNLINK(lcfg->channellists[i], item, link);
541 1.1 christos isc_mem_put(mctx, item, sizeof(*item));
542 1.1 christos }
543 1.1 christos
544 1.1 christos if (lcfg->channellist_count > 0)
545 1.1 christos isc_mem_put(mctx, lcfg->channellists,
546 1.1 christos lcfg->channellist_count *
547 1.1 christos sizeof(ISC_LIST(isc_logchannellist_t)));
548 1.1 christos
549 1.1 christos lcfg->dynamic = ISC_FALSE;
550 1.1 christos if (lcfg->tag != NULL)
551 1.1 christos isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
552 1.1 christos lcfg->tag = NULL;
553 1.1 christos lcfg->highest_level = 0;
554 1.1 christos lcfg->duplicate_interval = 0;
555 1.1 christos lcfg->magic = 0;
556 1.1 christos
557 1.1 christos isc_mem_put(mctx, lcfg, sizeof(*lcfg));
558 1.1 christos
559 1.1 christos *lcfgp = NULL;
560 1.1 christos }
561 1.1 christos
562 1.1 christos void
563 1.1 christos isc_log_registercategories(isc_log_t *lctx, isc_logcategory_t categories[]) {
564 1.1 christos isc_logcategory_t *catp;
565 1.1 christos
566 1.1 christos REQUIRE(VALID_CONTEXT(lctx));
567 1.1 christos REQUIRE(categories != NULL && categories[0].name != NULL);
568 1.1 christos
569 1.1 christos /*
570 1.1 christos * XXXDCL This somewhat sleazy situation of using the last pointer
571 1.1 christos * in one category array to point to the next array exists because
572 1.1 christos * this registration function returns void and I didn't want to have
573 1.1 christos * change everything that used it by making it return an isc_result_t.
574 1.1 christos * It would need to do that if it had to allocate memory to store
575 1.1 christos * pointers to each array passed in.
576 1.1 christos */
577 1.1 christos if (lctx->categories == NULL)
578 1.1 christos lctx->categories = categories;
579 1.1 christos
580 1.1 christos else {
581 1.1 christos /*
582 1.1 christos * Adjust the last (NULL) pointer of the already registered
583 1.1 christos * categories to point to the incoming array.
584 1.1 christos */
585 1.1 christos for (catp = lctx->categories; catp->name != NULL; )
586 1.1 christos if (catp->id == UINT_MAX)
587 1.1 christos /*
588 1.1 christos * The name pointer points to the next array.
589 1.1 christos * Ick.
590 1.1 christos */
591 1.1 christos DE_CONST(catp->name, catp);
592 1.1 christos else
593 1.1 christos catp++;
594 1.1 christos
595 1.1 christos catp->name = (void *)categories;
596 1.1 christos catp->id = UINT_MAX;
597 1.1 christos }
598 1.1 christos
599 1.1 christos /*
600 1.1 christos * Update the id number of the category with its new global id.
601 1.1 christos */
602 1.1 christos for (catp = categories; catp->name != NULL; catp++)
603 1.1 christos catp->id = lctx->category_count++;
604 1.1 christos }
605 1.1 christos
606 1.1 christos isc_logcategory_t *
607 1.1 christos isc_log_categorybyname(isc_log_t *lctx, const char *name) {
608 1.1 christos isc_logcategory_t *catp;
609 1.1 christos
610 1.1 christos REQUIRE(VALID_CONTEXT(lctx));
611 1.1 christos REQUIRE(name != NULL);
612 1.1 christos
613 1.1 christos for (catp = lctx->categories; catp->name != NULL; )
614 1.1 christos if (catp->id == UINT_MAX)
615 1.1 christos /*
616 1.1 christos * catp is neither modified nor returned to the
617 1.1 christos * caller, so removing its const qualifier is ok.
618 1.1 christos */
619 1.1 christos DE_CONST(catp->name, catp);
620 1.1 christos else {
621 1.1 christos if (strcmp(catp->name, name) == 0)
622 1.1 christos return (catp);
623 1.1 christos catp++;
624 1.1 christos }
625 1.1 christos
626 1.1 christos return (NULL);
627 1.1 christos }
628 1.1 christos
629 1.1 christos void
630 1.1 christos isc_log_registermodules(isc_log_t *lctx, isc_logmodule_t modules[]) {
631 1.1 christos isc_logmodule_t *modp;
632 1.1 christos
633 1.1 christos REQUIRE(VALID_CONTEXT(lctx));
634 1.1 christos REQUIRE(modules != NULL && modules[0].name != NULL);
635 1.1 christos
636 1.1 christos /*
637 1.1 christos * XXXDCL This somewhat sleazy situation of using the last pointer
638 1.1 christos * in one category array to point to the next array exists because
639 1.1 christos * this registration function returns void and I didn't want to have
640 1.1 christos * change everything that used it by making it return an isc_result_t.
641 1.1 christos * It would need to do that if it had to allocate memory to store
642 1.1 christos * pointers to each array passed in.
643 1.1 christos */
644 1.1 christos if (lctx->modules == NULL)
645 1.1 christos lctx->modules = modules;
646 1.1 christos
647 1.1 christos else {
648 1.1 christos /*
649 1.1 christos * Adjust the last (NULL) pointer of the already registered
650 1.1 christos * modules to point to the incoming array.
651 1.1 christos */
652 1.1 christos for (modp = lctx->modules; modp->name != NULL; )
653 1.1 christos if (modp->id == UINT_MAX)
654 1.1 christos /*
655 1.1 christos * The name pointer points to the next array.
656 1.1 christos * Ick.
657 1.1 christos */
658 1.1 christos DE_CONST(modp->name, modp);
659 1.1 christos else
660 1.1 christos modp++;
661 1.1 christos
662 1.1 christos modp->name = (void *)modules;
663 1.1 christos modp->id = UINT_MAX;
664 1.1 christos }
665 1.1 christos
666 1.1 christos /*
667 1.1 christos * Update the id number of the module with its new global id.
668 1.1 christos */
669 1.1 christos for (modp = modules; modp->name != NULL; modp++)
670 1.1 christos modp->id = lctx->module_count++;
671 1.1 christos }
672 1.1 christos
673 1.1 christos isc_logmodule_t *
674 1.1 christos isc_log_modulebyname(isc_log_t *lctx, const char *name) {
675 1.1 christos isc_logmodule_t *modp;
676 1.1 christos
677 1.1 christos REQUIRE(VALID_CONTEXT(lctx));
678 1.1 christos REQUIRE(name != NULL);
679 1.1 christos
680 1.1 christos for (modp = lctx->modules; modp->name != NULL; )
681 1.1 christos if (modp->id == UINT_MAX)
682 1.1 christos /*
683 1.1 christos * modp is neither modified nor returned to the
684 1.1 christos * caller, so removing its const qualifier is ok.
685 1.1 christos */
686 1.1 christos DE_CONST(modp->name, modp);
687 1.1 christos else {
688 1.1 christos if (strcmp(modp->name, name) == 0)
689 1.1 christos return (modp);
690 1.1 christos modp++;
691 1.1 christos }
692 1.1 christos
693 1.1 christos return (NULL);
694 1.1 christos }
695 1.1 christos
696 1.1 christos isc_result_t
697 1.1 christos isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
698 1.1 christos unsigned int type, int level,
699 1.1 christos const isc_logdestination_t *destination,
700 1.1 christos unsigned int flags)
701 1.1 christos {
702 1.1 christos isc_logchannel_t *channel;
703 1.1 christos isc_mem_t *mctx;
704 1.1 christos unsigned int permitted = ISC_LOG_PRINTALL | ISC_LOG_DEBUGONLY |
705 1.1 christos ISC_LOG_BUFFERED | ISC_LOG_ISO8601 |
706 1.1 christos ISC_LOG_UTC;
707 1.1 christos
708 1.1 christos REQUIRE(VALID_CONFIG(lcfg));
709 1.1 christos REQUIRE(name != NULL);
710 1.1 christos REQUIRE(type == ISC_LOG_TOSYSLOG || type == ISC_LOG_TOFILE ||
711 1.1 christos type == ISC_LOG_TOFILEDESC || type == ISC_LOG_TONULL);
712 1.1 christos REQUIRE(destination != NULL || type == ISC_LOG_TONULL);
713 1.1 christos REQUIRE(level >= ISC_LOG_CRITICAL);
714 1.1 christos REQUIRE((flags & ~permitted) == 0);
715 1.1 christos
716 1.1 christos /* XXXDCL find duplicate names? */
717 1.1 christos
718 1.1 christos mctx = lcfg->lctx->mctx;
719 1.1 christos
720 1.1 christos channel = isc_mem_get(mctx, sizeof(*channel));
721 1.1 christos if (channel == NULL)
722 1.1 christos return (ISC_R_NOMEMORY);
723 1.1 christos
724 1.1 christos channel->name = isc_mem_strdup(mctx, name);
725 1.1 christos if (channel->name == NULL) {
726 1.1 christos isc_mem_put(mctx, channel, sizeof(*channel));
727 1.1 christos return (ISC_R_NOMEMORY);
728 1.1 christos }
729 1.1 christos
730 1.1 christos channel->type = type;
731 1.1 christos channel->level = level;
732 1.1 christos channel->flags = flags;
733 1.1 christos ISC_LINK_INIT(channel, link);
734 1.1 christos
735 1.1 christos switch (type) {
736 1.1 christos case ISC_LOG_TOSYSLOG:
737 1.1 christos FACILITY(channel) = destination->facility;
738 1.1 christos break;
739 1.1 christos
740 1.1 christos case ISC_LOG_TOFILE:
741 1.1 christos /*
742 1.1 christos * The file name is copied because greatest_version wants
743 1.1 christos * to scribble on it, so it needs to be definitely in
744 1.1 christos * writable memory.
745 1.1 christos */
746 1.1 christos FILE_NAME(channel) =
747 1.1 christos isc_mem_strdup(mctx, destination->file.name);
748 1.1 christos FILE_STREAM(channel) = NULL;
749 1.1 christos FILE_VERSIONS(channel) = destination->file.versions;
750 1.1 christos FILE_SUFFIX(channel) = destination->file.suffix;
751 1.1 christos FILE_MAXSIZE(channel) = destination->file.maximum_size;
752 1.1 christos FILE_MAXREACHED(channel) = ISC_FALSE;
753 1.1 christos break;
754 1.1 christos
755 1.1 christos case ISC_LOG_TOFILEDESC:
756 1.1 christos FILE_NAME(channel) = NULL;
757 1.1 christos FILE_STREAM(channel) = destination->file.stream;
758 1.1 christos FILE_MAXSIZE(channel) = 0;
759 1.1 christos FILE_VERSIONS(channel) = ISC_LOG_ROLLNEVER;
760 1.1 christos FILE_SUFFIX(channel) = isc_log_rollsuffix_increment;
761 1.1 christos break;
762 1.1 christos
763 1.1 christos case ISC_LOG_TONULL:
764 1.1 christos /* Nothing. */
765 1.1 christos break;
766 1.1 christos
767 1.1 christos default:
768 1.1 christos isc_mem_free(mctx, channel->name);
769 1.1 christos isc_mem_put(mctx, channel, sizeof(*channel));
770 1.1 christos return (ISC_R_UNEXPECTED);
771 1.1 christos }
772 1.1 christos
773 1.1 christos ISC_LIST_PREPEND(lcfg->channels, channel, link);
774 1.1 christos
775 1.1 christos /*
776 1.1 christos * If default_stderr was redefined, make the default category
777 1.1 christos * point to the new default_stderr.
778 1.1 christos */
779 1.1 christos if (strcmp(name, "default_stderr") == 0)
780 1.1 christos default_channel.channel = channel;
781 1.1 christos
782 1.1 christos return (ISC_R_SUCCESS);
783 1.1 christos }
784 1.1 christos
785 1.1 christos isc_result_t
786 1.1 christos isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
787 1.1 christos const isc_logcategory_t *category,
788 1.1 christos const isc_logmodule_t *module)
789 1.1 christos {
790 1.1 christos isc_log_t *lctx;
791 1.1 christos isc_logchannel_t *channel;
792 1.1 christos isc_result_t result = ISC_R_SUCCESS;
793 1.1 christos unsigned int i;
794 1.1 christos
795 1.1 christos REQUIRE(VALID_CONFIG(lcfg));
796 1.1 christos REQUIRE(name != NULL);
797 1.1 christos
798 1.1 christos lctx = lcfg->lctx;
799 1.1 christos
800 1.1 christos REQUIRE(category == NULL || category->id < lctx->category_count);
801 1.1 christos REQUIRE(module == NULL || module->id < lctx->module_count);
802 1.1 christos
803 1.1 christos for (channel = ISC_LIST_HEAD(lcfg->channels); channel != NULL;
804 1.1 christos channel = ISC_LIST_NEXT(channel, link))
805 1.1 christos if (strcmp(name, channel->name) == 0)
806 1.1 christos break;
807 1.1 christos
808 1.1 christos if (channel == NULL)
809 1.1 christos return (ISC_R_NOTFOUND);
810 1.1 christos
811 1.1 christos if (category != NULL)
812 1.1 christos result = assignchannel(lcfg, category->id, module, channel);
813 1.1 christos
814 1.1 christos else
815 1.1 christos /*
816 1.1 christos * Assign to all categories. Note that this includes
817 1.1 christos * the default channel.
818 1.1 christos */
819 1.1 christos for (i = 0; i < lctx->category_count; i++) {
820 1.1 christos result = assignchannel(lcfg, i, module, channel);
821 1.1 christos if (result != ISC_R_SUCCESS)
822 1.1 christos break;
823 1.1 christos }
824 1.1 christos
825 1.1 christos return (result);
826 1.1 christos }
827 1.1 christos
828 1.1 christos void
829 1.1 christos isc_log_write(isc_log_t *lctx, isc_logcategory_t *category,
830 1.1 christos isc_logmodule_t *module, int level, const char *format, ...)
831 1.1 christos {
832 1.1 christos va_list args;
833 1.1 christos
834 1.1 christos /*
835 1.1 christos * Contract checking is done in isc_log_doit().
836 1.1 christos */
837 1.1 christos
838 1.1 christos va_start(args, format);
839 1.1 christos isc_log_doit(lctx, category, module, level, ISC_FALSE,
840 1.1 christos NULL, 0, 0, format, args);
841 1.1 christos va_end(args);
842 1.1 christos }
843 1.1 christos
844 1.1 christos void
845 1.1 christos isc_log_vwrite(isc_log_t *lctx, isc_logcategory_t *category,
846 1.1 christos isc_logmodule_t *module, int level,
847 1.1 christos const char *format, va_list args)
848 1.1 christos {
849 1.1 christos /*
850 1.1 christos * Contract checking is done in isc_log_doit().
851 1.1 christos */
852 1.1 christos isc_log_doit(lctx, category, module, level, ISC_FALSE,
853 1.1 christos NULL, 0, 0, format, args);
854 1.1 christos }
855 1.1 christos
856 1.1 christos void
857 1.1 christos isc_log_write1(isc_log_t *lctx, isc_logcategory_t *category,
858 1.1 christos isc_logmodule_t *module, int level, const char *format, ...)
859 1.1 christos {
860 1.1 christos va_list args;
861 1.1 christos
862 1.1 christos /*
863 1.1 christos * Contract checking is done in isc_log_doit().
864 1.1 christos */
865 1.1 christos
866 1.1 christos va_start(args, format);
867 1.1 christos isc_log_doit(lctx, category, module, level, ISC_TRUE,
868 1.1 christos NULL, 0, 0, format, args);
869 1.1 christos va_end(args);
870 1.1 christos }
871 1.1 christos
872 1.1 christos void
873 1.1 christos isc_log_vwrite1(isc_log_t *lctx, isc_logcategory_t *category,
874 1.1 christos isc_logmodule_t *module, int level,
875 1.1 christos const char *format, va_list args)
876 1.1 christos {
877 1.1 christos /*
878 1.1 christos * Contract checking is done in isc_log_doit().
879 1.1 christos */
880 1.1 christos isc_log_doit(lctx, category, module, level, ISC_TRUE,
881 1.1 christos NULL, 0, 0, format, args);
882 1.1 christos }
883 1.1 christos
884 1.1 christos void
885 1.1 christos isc_log_iwrite(isc_log_t *lctx, isc_logcategory_t *category,
886 1.1 christos isc_logmodule_t *module, int level,
887 1.1 christos isc_msgcat_t *msgcat, int msgset, int msg,
888 1.1 christos const char *format, ...)
889 1.1 christos {
890 1.1 christos va_list args;
891 1.1 christos
892 1.1 christos /*
893 1.1 christos * Contract checking is done in isc_log_doit().
894 1.1 christos */
895 1.1 christos
896 1.1 christos va_start(args, format);
897 1.1 christos isc_log_doit(lctx, category, module, level, ISC_FALSE,
898 1.1 christos msgcat, msgset, msg, format, args);
899 1.1 christos va_end(args);
900 1.1 christos }
901 1.1 christos
902 1.1 christos void
903 1.1 christos isc_log_ivwrite(isc_log_t *lctx, isc_logcategory_t *category,
904 1.1 christos isc_logmodule_t *module, int level,
905 1.1 christos isc_msgcat_t *msgcat, int msgset, int msg,
906 1.1 christos const char *format, va_list args)
907 1.1 christos {
908 1.1 christos /*
909 1.1 christos * Contract checking is done in isc_log_doit().
910 1.1 christos */
911 1.1 christos isc_log_doit(lctx, category, module, level, ISC_FALSE,
912 1.1 christos msgcat, msgset, msg, format, args);
913 1.1 christos }
914 1.1 christos
915 1.1 christos void
916 1.1 christos isc_log_iwrite1(isc_log_t *lctx, isc_logcategory_t *category,
917 1.1 christos isc_logmodule_t *module, int level,
918 1.1 christos isc_msgcat_t *msgcat, int msgset, int msg,
919 1.1 christos const char *format, ...)
920 1.1 christos {
921 1.1 christos va_list args;
922 1.1 christos
923 1.1 christos /*
924 1.1 christos * Contract checking is done in isc_log_doit().
925 1.1 christos */
926 1.1 christos
927 1.1 christos va_start(args, format);
928 1.1 christos isc_log_doit(lctx, category, module, level, ISC_TRUE,
929 1.1 christos msgcat, msgset, msg, format, args);
930 1.1 christos va_end(args);
931 1.1 christos }
932 1.1 christos
933 1.1 christos void
934 1.1 christos isc_log_ivwrite1(isc_log_t *lctx, isc_logcategory_t *category,
935 1.1 christos isc_logmodule_t *module, int level,
936 1.1 christos isc_msgcat_t *msgcat, int msgset, int msg,
937 1.1 christos const char *format, va_list args)
938 1.1 christos {
939 1.1 christos /*
940 1.1 christos * Contract checking is done in isc_log_doit().
941 1.1 christos */
942 1.1 christos isc_log_doit(lctx, category, module, level, ISC_TRUE,
943 1.1 christos msgcat, msgset, msg, format, args);
944 1.1 christos }
945 1.1 christos
946 1.1 christos void
947 1.1 christos isc_log_setcontext(isc_log_t *lctx) {
948 1.1 christos isc_lctx = lctx;
949 1.1 christos }
950 1.1 christos
951 1.1 christos void
952 1.1 christos isc_log_setdebuglevel(isc_log_t *lctx, unsigned int level) {
953 1.1 christos isc_logchannel_t *channel;
954 1.1 christos
955 1.1 christos REQUIRE(VALID_CONTEXT(lctx));
956 1.1 christos
957 1.1 christos LOCK(&lctx->lock);
958 1.1 christos
959 1.1 christos lctx->debug_level = level;
960 1.1 christos /*
961 1.1 christos * Close ISC_LOG_DEBUGONLY channels if level is zero.
962 1.1 christos */
963 1.1 christos if (lctx->debug_level == 0)
964 1.1 christos for (channel = ISC_LIST_HEAD(lctx->logconfig->channels);
965 1.1 christos channel != NULL;
966 1.1 christos channel = ISC_LIST_NEXT(channel, link))
967 1.1 christos if (channel->type == ISC_LOG_TOFILE &&
968 1.1 christos (channel->flags & ISC_LOG_DEBUGONLY) != 0 &&
969 1.1 christos FILE_STREAM(channel) != NULL) {
970 1.1 christos (void)fclose(FILE_STREAM(channel));
971 1.1 christos FILE_STREAM(channel) = NULL;
972 1.1 christos }
973 1.1 christos UNLOCK(&lctx->lock);
974 1.1 christos }
975 1.1 christos
976 1.1 christos unsigned int
977 1.1 christos isc_log_getdebuglevel(isc_log_t *lctx) {
978 1.1 christos REQUIRE(VALID_CONTEXT(lctx));
979 1.1 christos
980 1.1 christos return (lctx->debug_level);
981 1.1 christos }
982 1.1 christos
983 1.1 christos void
984 1.1 christos isc_log_setduplicateinterval(isc_logconfig_t *lcfg, unsigned int interval) {
985 1.1 christos REQUIRE(VALID_CONFIG(lcfg));
986 1.1 christos
987 1.1 christos lcfg->duplicate_interval = interval;
988 1.1 christos }
989 1.1 christos
990 1.1 christos unsigned int
991 1.1 christos isc_log_getduplicateinterval(isc_logconfig_t *lcfg) {
992 1.1 christos REQUIRE(VALID_CONTEXT(lcfg));
993 1.1 christos
994 1.1 christos return (lcfg->duplicate_interval);
995 1.1 christos }
996 1.1 christos
997 1.1 christos isc_result_t
998 1.1 christos isc_log_settag(isc_logconfig_t *lcfg, const char *tag) {
999 1.1 christos REQUIRE(VALID_CONFIG(lcfg));
1000 1.1 christos
1001 1.1 christos if (tag != NULL && *tag != '\0') {
1002 1.1 christos if (lcfg->tag != NULL)
1003 1.1 christos isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
1004 1.1 christos lcfg->tag = isc_mem_strdup(lcfg->lctx->mctx, tag);
1005 1.1 christos if (lcfg->tag == NULL)
1006 1.1 christos return (ISC_R_NOMEMORY);
1007 1.1 christos
1008 1.1 christos } else {
1009 1.1 christos if (lcfg->tag != NULL)
1010 1.1 christos isc_mem_free(lcfg->lctx->mctx, lcfg->tag);
1011 1.1 christos lcfg->tag = NULL;
1012 1.1 christos }
1013 1.1 christos
1014 1.1 christos return (ISC_R_SUCCESS);
1015 1.1 christos }
1016 1.1 christos
1017 1.1 christos char *
1018 1.1 christos isc_log_gettag(isc_logconfig_t *lcfg) {
1019 1.1 christos REQUIRE(VALID_CONFIG(lcfg));
1020 1.1 christos
1021 1.1 christos return (lcfg->tag);
1022 1.1 christos }
1023 1.1 christos
1024 1.1 christos /* XXXDCL NT -- This interface will assuredly be changing. */
1025 1.1 christos void
1026 1.1 christos isc_log_opensyslog(const char *tag, int options, int facility) {
1027 1.1 christos (void)openlog(tag, options, facility);
1028 1.1 christos }
1029 1.1 christos
1030 1.1 christos void
1031 1.1 christos isc_log_closefilelogs(isc_log_t *lctx) {
1032 1.1 christos isc_logchannel_t *channel;
1033 1.1 christos
1034 1.1 christos REQUIRE(VALID_CONTEXT(lctx));
1035 1.1 christos
1036 1.1 christos LOCK(&lctx->lock);
1037 1.1 christos for (channel = ISC_LIST_HEAD(lctx->logconfig->channels);
1038 1.1 christos channel != NULL;
1039 1.1 christos channel = ISC_LIST_NEXT(channel, link))
1040 1.1 christos
1041 1.1 christos if (channel->type == ISC_LOG_TOFILE &&
1042 1.1 christos FILE_STREAM(channel) != NULL) {
1043 1.1 christos (void)fclose(FILE_STREAM(channel));
1044 1.1 christos FILE_STREAM(channel) = NULL;
1045 1.1 christos }
1046 1.1 christos UNLOCK(&lctx->lock);
1047 1.1 christos }
1048 1.1 christos
1049 1.1 christos /****
1050 1.1 christos **** Internal functions
1051 1.1 christos ****/
1052 1.1 christos
1053 1.1 christos static isc_result_t
1054 1.1 christos assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
1055 1.1 christos const isc_logmodule_t *module, isc_logchannel_t *channel)
1056 1.1 christos {
1057 1.1 christos isc_logchannellist_t *new_item;
1058 1.1 christos isc_log_t *lctx;
1059 1.1 christos isc_result_t result;
1060 1.1 christos
1061 1.1 christos REQUIRE(VALID_CONFIG(lcfg));
1062 1.1 christos
1063 1.1 christos lctx = lcfg->lctx;
1064 1.1 christos
1065 1.1 christos REQUIRE(category_id < lctx->category_count);
1066 1.1 christos REQUIRE(module == NULL || module->id < lctx->module_count);
1067 1.1 christos REQUIRE(channel != NULL);
1068 1.1 christos
1069 1.1 christos /*
1070 1.1 christos * Ensure lcfg->channellist_count == lctx->category_count.
1071 1.1 christos */
1072 1.1 christos result = sync_channellist(lcfg);
1073 1.1 christos if (result != ISC_R_SUCCESS)
1074 1.1 christos return (result);
1075 1.1 christos
1076 1.1 christos new_item = isc_mem_get(lctx->mctx, sizeof(*new_item));
1077 1.1 christos if (new_item == NULL)
1078 1.1 christos return (ISC_R_NOMEMORY);
1079 1.1 christos
1080 1.1 christos new_item->channel = channel;
1081 1.1 christos new_item->module = module;
1082 1.1 christos ISC_LIST_INITANDPREPEND(lcfg->channellists[category_id],
1083 1.1 christos new_item, link);
1084 1.1 christos
1085 1.1 christos /*
1086 1.1 christos * Remember the highest logging level set by any channel in the
1087 1.1 christos * logging config, so isc_log_doit() can quickly return if the
1088 1.1 christos * message is too high to be logged by any channel.
1089 1.1 christos */
1090 1.1 christos if (channel->type != ISC_LOG_TONULL) {
1091 1.1 christos if (lcfg->highest_level < channel->level)
1092 1.1 christos lcfg->highest_level = channel->level;
1093 1.1 christos if (channel->level == ISC_LOG_DYNAMIC)
1094 1.1 christos lcfg->dynamic = ISC_TRUE;
1095 1.1 christos }
1096 1.1 christos
1097 1.1 christos return (ISC_R_SUCCESS);
1098 1.1 christos }
1099 1.1 christos
1100 1.1 christos /*
1101 1.1 christos * This would ideally be part of isc_log_registercategories(), except then
1102 1.1 christos * that function would have to return isc_result_t instead of void.
1103 1.1 christos */
1104 1.1 christos static isc_result_t
1105 1.1 christos sync_channellist(isc_logconfig_t *lcfg) {
1106 1.1 christos unsigned int bytes;
1107 1.1 christos isc_log_t *lctx;
1108 1.1 christos void *lists;
1109 1.1 christos
1110 1.1 christos REQUIRE(VALID_CONFIG(lcfg));
1111 1.1 christos
1112 1.1 christos lctx = lcfg->lctx;
1113 1.1 christos
1114 1.1 christos REQUIRE(lctx->category_count != 0);
1115 1.1 christos
1116 1.1 christos if (lctx->category_count == lcfg->channellist_count)
1117 1.1 christos return (ISC_R_SUCCESS);
1118 1.1 christos
1119 1.1 christos bytes = lctx->category_count * sizeof(ISC_LIST(isc_logchannellist_t));
1120 1.1 christos
1121 1.1 christos lists = isc_mem_get(lctx->mctx, bytes);
1122 1.1 christos
1123 1.1 christos if (lists == NULL)
1124 1.1 christos return (ISC_R_NOMEMORY);
1125 1.1 christos
1126 1.1 christos memset(lists, 0, bytes);
1127 1.1 christos
1128 1.1 christos if (lcfg->channellist_count != 0) {
1129 1.1 christos bytes = lcfg->channellist_count *
1130 1.1 christos sizeof(ISC_LIST(isc_logchannellist_t));
1131 1.1 christos memmove(lists, lcfg->channellists, bytes);
1132 1.1 christos isc_mem_put(lctx->mctx, lcfg->channellists, bytes);
1133 1.1 christos }
1134 1.1 christos
1135 1.1 christos lcfg->channellists = lists;
1136 1.1 christos lcfg->channellist_count = lctx->category_count;
1137 1.1 christos
1138 1.1 christos return (ISC_R_SUCCESS);
1139 1.1 christos }
1140 1.1 christos
1141 1.1 christos static isc_result_t
1142 1.1 christos greatest_version(isc_logfile_t *file, int versions, int *greatestp) {
1143 1.1 christos char *bname, *digit_end;
1144 1.1 christos const char *dirname;
1145 1.1 christos int version, greatest = -1;
1146 1.1 christos size_t bnamelen;
1147 1.1 christos isc_dir_t dir;
1148 1.1 christos isc_result_t result;
1149 1.1 christos char sep = '/';
1150 1.1 christos #ifdef _WIN32
1151 1.1 christos char *bname2;
1152 1.1 christos #endif
1153 1.1 christos
1154 1.1 christos /*
1155 1.1 christos * It is safe to DE_CONST the file.name because it was copied
1156 1.1 christos * with isc_mem_strdup().
1157 1.1 christos */
1158 1.1 christos bname = strrchr(file->name, sep);
1159 1.1 christos #ifdef _WIN32
1160 1.1 christos bname2 = strrchr(file->name, '\\');
1161 1.1 christos if ((bname != NULL && bname2 != NULL && bname2 > bname) ||
1162 1.1 christos (bname == NULL && bname2 != NULL)) {
1163 1.1 christos bname = bname2;
1164 1.1 christos sep = '\\';
1165 1.1 christos }
1166 1.1 christos #endif
1167 1.1 christos if (bname != NULL) {
1168 1.1 christos *bname++ = '\0';
1169 1.1 christos dirname = file->name;
1170 1.1 christos } else {
1171 1.1 christos DE_CONST(file->name, bname);
1172 1.1 christos dirname = ".";
1173 1.1 christos }
1174 1.1 christos bnamelen = strlen(bname);
1175 1.1 christos
1176 1.1 christos isc_dir_init(&dir);
1177 1.1 christos result = isc_dir_open(&dir, dirname);
1178 1.1 christos
1179 1.1 christos /*
1180 1.1 christos * Replace the file separator if it was taken out.
1181 1.1 christos */
1182 1.1 christos if (bname != file->name)
1183 1.1 christos *(bname - 1) = sep;
1184 1.1 christos
1185 1.1 christos /*
1186 1.1 christos * Return if the directory open failed.
1187 1.1 christos */
1188 1.1 christos if (result != ISC_R_SUCCESS)
1189 1.1 christos return (result);
1190 1.1 christos
1191 1.1 christos while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
1192 1.1 christos if (dir.entry.length > bnamelen &&
1193 1.1 christos strncmp(dir.entry.name, bname, bnamelen) == 0 &&
1194 1.1 christos dir.entry.name[bnamelen] == '.')
1195 1.1 christos {
1196 1.1 christos version = strtol(&dir.entry.name[bnamelen + 1],
1197 1.1 christos &digit_end, 10);
1198 1.1 christos /*
1199 1.1 christos * Remove any backup files that exceed versions.
1200 1.1 christos */
1201 1.1 christos if (*digit_end == '\0' && version >= versions) {
1202 1.1 christos result = isc_file_remove(dir.entry.name);
1203 1.1 christos if (result != ISC_R_SUCCESS &&
1204 1.1 christos result != ISC_R_FILENOTFOUND)
1205 1.1 christos syslog(LOG_ERR, "unable to remove "
1206 1.1 christos "log file '%s': %s",
1207 1.1 christos dir.entry.name,
1208 1.1 christos isc_result_totext(result));
1209 1.1 christos } else if (*digit_end == '\0' && version > greatest)
1210 1.1 christos greatest = version;
1211 1.1 christos }
1212 1.1 christos }
1213 1.1 christos isc_dir_close(&dir);
1214 1.1 christos
1215 1.1 christos *greatestp = greatest;
1216 1.1 christos
1217 1.1 christos return (ISC_R_SUCCESS);
1218 1.1 christos }
1219 1.1 christos
1220 1.1 christos static isc_result_t
1221 1.1 christos remove_old_tsversions(isc_logfile_t *file, int versions) {
1222 1.1 christos isc_result_t result;
1223 1.1 christos char *bname, *digit_end;
1224 1.1 christos const char *dirname;
1225 1.1 christos isc_int64_t version, last = ISC_INT64_MAX;
1226 1.1 christos isc_int64_t to_keep[ISC_LOG_MAX_VERSIONS];
1227 1.1 christos size_t bnamelen;
1228 1.1 christos isc_dir_t dir;
1229 1.1 christos char sep = '/';
1230 1.1 christos #ifdef _WIN32
1231 1.1 christos char *bname2;
1232 1.1 christos #endif
1233 1.1 christos /*
1234 1.1 christos * It is safe to DE_CONST the file.name because it was copied
1235 1.1 christos * with isc_mem_strdup().
1236 1.1 christos */
1237 1.1 christos bname = strrchr(file->name, sep);
1238 1.1 christos #ifdef _WIN32
1239 1.1 christos bname2 = strrchr(file->name, '\\');
1240 1.1 christos if ((bname != NULL && bname2 != NULL && bname2 > bname) ||
1241 1.1 christos (bname == NULL && bname2 != NULL)) {
1242 1.1 christos bname = bname2;
1243 1.1 christos sep = '\\';
1244 1.1 christos }
1245 1.1 christos #endif
1246 1.1 christos if (bname != NULL) {
1247 1.1 christos *bname++ = '\0';
1248 1.1 christos dirname = file->name;
1249 1.1 christos } else {
1250 1.1 christos DE_CONST(file->name, bname);
1251 1.1 christos dirname = ".";
1252 1.1 christos }
1253 1.1 christos bnamelen = strlen(bname);
1254 1.1 christos
1255 1.1 christos isc_dir_init(&dir);
1256 1.1 christos result = isc_dir_open(&dir, dirname);
1257 1.1 christos
1258 1.1 christos /*
1259 1.1 christos * Replace the file separator if it was taken out.
1260 1.1 christos */
1261 1.1 christos if (bname != file->name) {
1262 1.1 christos *(bname - 1) = sep;
1263 1.1 christos }
1264 1.1 christos
1265 1.1 christos /*
1266 1.1 christos * Return if the directory open failed.
1267 1.1 christos */
1268 1.1 christos if (result != ISC_R_SUCCESS)
1269 1.1 christos return (result);
1270 1.1 christos
1271 1.1 christos if (versions > 0) {
1272 1.1 christos /*
1273 1.1 christos * First we fill 'to_keep' structure using insertion sort
1274 1.1 christos */
1275 1.1 christos memset(to_keep, 0, versions * sizeof(long long));
1276 1.1 christos while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
1277 1.1 christos if (dir.entry.length > bnamelen &&
1278 1.1 christos strncmp(dir.entry.name, bname, bnamelen) == 0 &&
1279 1.1 christos dir.entry.name[bnamelen] == '.')
1280 1.1 christos {
1281 1.1 christos char *ename = &dir.entry.name[bnamelen + 1];
1282 1.1 christos version = isc_string_touint64(ename,
1283 1.1 christos &digit_end, 10);
1284 1.1 christos if (*digit_end == '\0') {
1285 1.1 christos int i = 0;
1286 1.1 christos while (i < versions &&
1287 1.1 christos version < to_keep[i])
1288 1.1 christos {
1289 1.1 christos i++;
1290 1.1 christos }
1291 1.1 christos if (i < versions) {
1292 1.1 christos memmove(&to_keep[i + 1],
1293 1.1 christos &to_keep[i],
1294 1.1 christos sizeof(long long) *
1295 1.1 christos versions - i - 1);
1296 1.1 christos to_keep[i] = version;
1297 1.1 christos }
1298 1.1 christos }
1299 1.1 christos }
1300 1.1 christos }
1301 1.1 christos
1302 1.1 christos /*
1303 1.1 christos * to_keep[versions - 1] is the last one we want to keep
1304 1.1 christos */
1305 1.1 christos last = to_keep[versions - 1];
1306 1.1 christos isc_dir_reset(&dir);
1307 1.1 christos }
1308 1.1 christos
1309 1.1 christos /*
1310 1.1 christos * Then we remove all files that we don't want to_keep
1311 1.1 christos */
1312 1.1 christos while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
1313 1.1 christos if (dir.entry.length > bnamelen &&
1314 1.1 christos strncmp(dir.entry.name, bname, bnamelen) == 0 &&
1315 1.1 christos dir.entry.name[bnamelen] == '.')
1316 1.1 christos {
1317 1.1 christos char *ename = &dir.entry.name[bnamelen + 1];
1318 1.1 christos version = isc_string_touint64(ename, &digit_end, 10);
1319 1.1 christos /*
1320 1.1 christos * Remove any backup files that exceed versions.
1321 1.1 christos */
1322 1.1 christos if (*digit_end == '\0' && version < last) {
1323 1.1 christos result = isc_file_remove(dir.entry.name);
1324 1.1 christos if (result != ISC_R_SUCCESS &&
1325 1.1 christos result != ISC_R_FILENOTFOUND)
1326 1.1 christos syslog(LOG_ERR, "unable to remove "
1327 1.1 christos "log file '%s': %s",
1328 1.1 christos dir.entry.name,
1329 1.1 christos isc_result_totext(result));
1330 1.1 christos }
1331 1.1 christos }
1332 1.1 christos }
1333 1.1 christos
1334 1.1 christos isc_dir_close(&dir);
1335 1.1 christos
1336 1.1 christos return (ISC_R_SUCCESS);
1337 1.1 christos }
1338 1.1 christos
1339 1.1 christos static isc_result_t
1340 1.1 christos roll_increment(isc_logfile_t *file) {
1341 1.1 christos int i, n, greatest;
1342 1.1 christos char current[PATH_MAX + 1];
1343 1.1 christos char newpath[PATH_MAX + 1];
1344 1.1 christos const char *path;
1345 1.1 christos isc_result_t result = ISC_R_SUCCESS;
1346 1.1 christos
1347 1.1 christos REQUIRE(file != NULL);
1348 1.1 christos REQUIRE(file->versions != 0);
1349 1.1 christos
1350 1.1 christos path = file->name;
1351 1.1 christos
1352 1.1 christos if (file->versions == ISC_LOG_ROLLINFINITE) {
1353 1.1 christos /*
1354 1.1 christos * Find the first missing entry in the log file sequence.
1355 1.1 christos */
1356 1.1 christos for (greatest = 0; greatest < INT_MAX; greatest++) {
1357 1.1 christos n = snprintf(current, sizeof(current),
1358 1.1 christos "%s.%u", path, (unsigned)greatest) ;
1359 1.1 christos if (n >= (int)sizeof(current) || n < 0 ||
1360 1.1 christos !isc_file_exists(current))
1361 1.1 christos {
1362 1.1 christos break;
1363 1.1 christos }
1364 1.1 christos }
1365 1.1 christos } else {
1366 1.1 christos /*
1367 1.1 christos * Get the largest existing version and remove any
1368 1.1 christos * version greater than the permitted version.
1369 1.1 christos */
1370 1.1 christos result = greatest_version(file, file->versions, &greatest);
1371 1.1 christos if (result != ISC_R_SUCCESS) {
1372 1.1 christos return (result);
1373 1.1 christos }
1374 1.1 christos
1375 1.1 christos /*
1376 1.1 christos * Increment if greatest is not the actual maximum value.
1377 1.1 christos */
1378 1.1 christos if (greatest < file->versions - 1) {
1379 1.1 christos greatest++;
1380 1.1 christos }
1381 1.1 christos }
1382 1.1 christos
1383 1.1 christos for (i = greatest; i > 0; i--) {
1384 1.1 christos result = ISC_R_SUCCESS;
1385 1.1 christos n = snprintf(current, sizeof(current), "%s.%u", path,
1386 1.1 christos (unsigned)(i - 1));
1387 1.1 christos if (n >= (int)sizeof(current) || n < 0) {
1388 1.1 christos result = ISC_R_NOSPACE;
1389 1.1 christos }
1390 1.1 christos if (result == ISC_R_SUCCESS) {
1391 1.1 christos n = snprintf(newpath, sizeof(newpath), "%s.%u",
1392 1.1 christos path, (unsigned)i);
1393 1.1 christos if (n >= (int)sizeof(newpath) || n < 0) {
1394 1.1 christos result = ISC_R_NOSPACE;
1395 1.1 christos }
1396 1.1 christos }
1397 1.1 christos if (result == ISC_R_SUCCESS) {
1398 1.1 christos result = isc_file_rename(current, newpath);
1399 1.1 christos }
1400 1.1 christos if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
1401 1.1 christos syslog(LOG_ERR,
1402 1.1 christos "unable to rename log file '%s.%u' to "
1403 1.1 christos "'%s.%u': %s", path, i - 1, path, i,
1404 1.1 christos isc_result_totext(result));
1405 1.1 christos }
1406 1.1 christos }
1407 1.1 christos
1408 1.1 christos n = snprintf(newpath, sizeof(newpath), "%s.0", path);
1409 1.1 christos if (n >= (int)sizeof(newpath) || n < 0) {
1410 1.1 christos result = ISC_R_NOSPACE;
1411 1.1 christos } else {
1412 1.1 christos result = isc_file_rename(path, newpath);
1413 1.1 christos }
1414 1.1 christos if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
1415 1.1 christos syslog(LOG_ERR,
1416 1.1 christos "unable to rename log file '%s' to '%s.0': %s",
1417 1.1 christos path, path, isc_result_totext(result));
1418 1.1 christos }
1419 1.1 christos
1420 1.1 christos return (ISC_R_SUCCESS);
1421 1.1 christos }
1422 1.1 christos
1423 1.1 christos static isc_result_t
1424 1.1 christos roll_timestamp(isc_logfile_t *file) {
1425 1.1 christos int n;
1426 1.1 christos char newts[PATH_MAX + 1];
1427 1.1 christos char newpath[PATH_MAX + 1];
1428 1.1 christos const char *path;
1429 1.1 christos isc_time_t now;
1430 1.1 christos isc_result_t result = ISC_R_SUCCESS;
1431 1.1 christos
1432 1.1 christos REQUIRE(file != NULL);
1433 1.1 christos REQUIRE(file->versions != 0);
1434 1.1 christos
1435 1.1 christos path = file->name;
1436 1.1 christos
1437 1.1 christos /*
1438 1.1 christos * First find all the logfiles and remove the oldest ones
1439 1.1 christos * Save one fewer than file->versions because we'll be renaming
1440 1.1 christos * the existing file to a timestamped version after this.
1441 1.1 christos */
1442 1.1 christos if (file->versions != ISC_LOG_ROLLINFINITE) {
1443 1.1 christos remove_old_tsversions(file, file->versions - 1);
1444 1.1 christos }
1445 1.1 christos
1446 1.1 christos /* Then just rename the current logfile */
1447 1.1 christos isc_time_now(&now);
1448 1.1 christos isc_time_formatshorttimestamp(&now, newts, PATH_MAX + 1);
1449 1.1 christos n = snprintf(newpath, sizeof(newpath), "%s.%s", path, newts);
1450 1.1 christos if (n >= (int)sizeof(newpath) || n < 0) {
1451 1.1 christos result = ISC_R_NOSPACE;
1452 1.1 christos } else {
1453 1.1 christos result = isc_file_rename(path, newpath);
1454 1.1 christos }
1455 1.1 christos if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
1456 1.1 christos syslog(LOG_ERR,
1457 1.1 christos "unable to rename log file '%s' to '%s.0': %s",
1458 1.1 christos path, path, isc_result_totext(result));
1459 1.1 christos }
1460 1.1 christos
1461 1.1 christos return (ISC_R_SUCCESS);
1462 1.1 christos }
1463 1.1 christos
1464 1.1 christos
1465 1.1 christos isc_result_t
1466 1.1 christos isc_logfile_roll(isc_logfile_t *file) {
1467 1.1 christos isc_result_t result;
1468 1.1 christos
1469 1.1 christos REQUIRE(file != NULL);
1470 1.1 christos
1471 1.1 christos /*
1472 1.1 christos * Do nothing (not even excess version trimming) if ISC_LOG_ROLLNEVER
1473 1.1 christos * is specified. Apparently complete external control over the log
1474 1.1 christos * files is desired.
1475 1.1 christos */
1476 1.1 christos if (file->versions == ISC_LOG_ROLLNEVER) {
1477 1.1 christos return (ISC_R_SUCCESS);
1478 1.1 christos } else if (file->versions == 0) {
1479 1.1 christos result = isc_file_remove(file->name);
1480 1.1 christos if (result != ISC_R_SUCCESS &&
1481 1.1 christos result != ISC_R_FILENOTFOUND)
1482 1.1 christos syslog(LOG_ERR, "unable to remove log file '%s': %s",
1483 1.1 christos file->name, isc_result_totext(result));
1484 1.1 christos return (ISC_R_SUCCESS);
1485 1.1 christos }
1486 1.1 christos
1487 1.1 christos switch (file->suffix) {
1488 1.1 christos case isc_log_rollsuffix_increment:
1489 1.1 christos return (roll_increment(file));
1490 1.1 christos case isc_log_rollsuffix_timestamp:
1491 1.1 christos return (roll_timestamp(file));
1492 1.1 christos default:
1493 1.1 christos return (ISC_R_UNEXPECTED);
1494 1.1 christos }
1495 1.1 christos }
1496 1.1 christos
1497 1.1 christos static isc_result_t
1498 1.1 christos isc_log_open(isc_logchannel_t *channel) {
1499 1.1 christos struct stat statbuf;
1500 1.1 christos isc_boolean_t regular_file;
1501 1.1 christos isc_boolean_t roll = ISC_FALSE;
1502 1.1 christos isc_result_t result = ISC_R_SUCCESS;
1503 1.1 christos const char *path;
1504 1.1 christos
1505 1.1 christos REQUIRE(channel->type == ISC_LOG_TOFILE);
1506 1.1 christos REQUIRE(FILE_STREAM(channel) == NULL);
1507 1.1 christos
1508 1.1 christos path = FILE_NAME(channel);
1509 1.1 christos
1510 1.1 christos REQUIRE(path != NULL && *path != '\0');
1511 1.1 christos
1512 1.1 christos /*
1513 1.1 christos * Determine type of file; only regular files will be
1514 1.1 christos * version renamed, and only if the base file exists
1515 1.1 christos * and either has no size limit or has reached its size limit.
1516 1.1 christos */
1517 1.1 christos if (stat(path, &statbuf) == 0) {
1518 1.1 christos regular_file = S_ISREG(statbuf.st_mode) ? ISC_TRUE : ISC_FALSE;
1519 1.1 christos /* XXXDCL if not regular_file complain? */
1520 1.1 christos if ((FILE_MAXSIZE(channel) == 0 &&
1521 1.1 christos FILE_VERSIONS(channel) != ISC_LOG_ROLLNEVER) ||
1522 1.1 christos (FILE_MAXSIZE(channel) > 0 &&
1523 1.1 christos statbuf.st_size >= FILE_MAXSIZE(channel)))
1524 1.1 christos roll = regular_file;
1525 1.1 christos } else if (errno == ENOENT) {
1526 1.1 christos regular_file = ISC_TRUE;
1527 1.1 christos POST(regular_file);
1528 1.1 christos } else
1529 1.1 christos result = ISC_R_INVALIDFILE;
1530 1.1 christos
1531 1.1 christos /*
1532 1.1 christos * Version control.
1533 1.1 christos */
1534 1.1 christos if (result == ISC_R_SUCCESS && roll) {
1535 1.1 christos if (FILE_VERSIONS(channel) == ISC_LOG_ROLLNEVER)
1536 1.1 christos return (ISC_R_MAXSIZE);
1537 1.1 christos result = isc_logfile_roll(&channel->destination.file);
1538 1.1 christos if (result != ISC_R_SUCCESS) {
1539 1.1 christos if ((channel->flags & ISC_LOG_OPENERR) == 0) {
1540 1.1 christos syslog(LOG_ERR,
1541 1.1 christos "isc_log_open: isc_logfile_roll '%s' "
1542 1.1 christos "failed: %s",
1543 1.1 christos FILE_NAME(channel),
1544 1.1 christos isc_result_totext(result));
1545 1.1 christos channel->flags |= ISC_LOG_OPENERR;
1546 1.1 christos }
1547 1.1 christos return (result);
1548 1.1 christos }
1549 1.1 christos }
1550 1.1 christos
1551 1.1 christos result = isc_stdio_open(path, "a", &FILE_STREAM(channel));
1552 1.1 christos
1553 1.1 christos return (result);
1554 1.1 christos }
1555 1.1 christos
1556 1.1 christos isc_boolean_t
1557 1.1 christos isc_log_wouldlog(isc_log_t *lctx, int level) {
1558 1.1 christos /*
1559 1.1 christos * Try to avoid locking the mutex for messages which can't
1560 1.1 christos * possibly be logged to any channels -- primarily debugging
1561 1.1 christos * messages that the debug level is not high enough to print.
1562 1.1 christos *
1563 1.1 christos * If the level is (mathematically) less than or equal to the
1564 1.1 christos * highest_level, or if there is a dynamic channel and the level is
1565 1.1 christos * less than or equal to the debug level, the main loop must be
1566 1.1 christos * entered to see if the message should really be output.
1567 1.1 christos *
1568 1.1 christos * NOTE: this is UNLOCKED access to the logconfig. However,
1569 1.1 christos * the worst thing that can happen is that a bad decision is made
1570 1.1 christos * about returning without logging, and that's not a big concern,
1571 1.1 christos * because that's a risk anyway if the logconfig is being
1572 1.1 christos * dynamically changed.
1573 1.1 christos */
1574 1.1 christos
1575 1.1 christos if (lctx == NULL || lctx->logconfig == NULL)
1576 1.1 christos return (ISC_FALSE);
1577 1.1 christos
1578 1.1 christos return (ISC_TF(level <= lctx->logconfig->highest_level ||
1579 1.1 christos (lctx->logconfig->dynamic &&
1580 1.1 christos level <= lctx->debug_level)));
1581 1.1 christos }
1582 1.1 christos
1583 1.1 christos static void
1584 1.1 christos isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
1585 1.1 christos isc_logmodule_t *module, int level, isc_boolean_t write_once,
1586 1.1 christos isc_msgcat_t *msgcat, int msgset, int msg,
1587 1.1 christos const char *format, va_list args)
1588 1.1 christos {
1589 1.1 christos int syslog_level;
1590 1.1 christos const char *time_string;
1591 1.1 christos char local_time[64];
1592 1.1 christos char iso8601z_string[64];
1593 1.1 christos char iso8601l_string[64];
1594 1.1 christos char level_string[24];
1595 1.1 christos const char *iformat;
1596 1.1 christos struct stat statbuf;
1597 1.1 christos isc_boolean_t matched = ISC_FALSE;
1598 1.1 christos isc_boolean_t printtime, iso8601, utc, printtag, printcolon;
1599 1.1 christos isc_boolean_t printcategory, printmodule, printlevel, buffered;
1600 1.1 christos isc_logconfig_t *lcfg;
1601 1.1 christos isc_logchannel_t *channel;
1602 1.1 christos isc_logchannellist_t *category_channels;
1603 1.1 christos isc_result_t result;
1604 1.1 christos
1605 1.1 christos REQUIRE(lctx == NULL || VALID_CONTEXT(lctx));
1606 1.1 christos REQUIRE(category != NULL);
1607 1.1 christos REQUIRE(module != NULL);
1608 1.1 christos REQUIRE(level != ISC_LOG_DYNAMIC);
1609 1.1 christos REQUIRE(format != NULL);
1610 1.1 christos
1611 1.1 christos /*
1612 1.1 christos * Programs can use libraries that use this logging code without
1613 1.1 christos * wanting to do any logging, thus the log context is allowed to
1614 1.1 christos * be non-existent.
1615 1.1 christos */
1616 1.1 christos if (lctx == NULL)
1617 1.1 christos return;
1618 1.1 christos
1619 1.1 christos REQUIRE(category->id < lctx->category_count);
1620 1.1 christos REQUIRE(module->id < lctx->module_count);
1621 1.1 christos
1622 1.1 christos if (! isc_log_wouldlog(lctx, level))
1623 1.1 christos return;
1624 1.1 christos
1625 1.1 christos if (msgcat != NULL)
1626 1.1 christos iformat = isc_msgcat_get(msgcat, msgset, msg, format);
1627 1.1 christos else
1628 1.1 christos iformat = format;
1629 1.1 christos
1630 1.1 christos local_time[0] = '\0';
1631 1.1 christos iso8601l_string[0] = '\0';
1632 1.1 christos iso8601z_string[0] = '\0';
1633 1.1 christos level_string[0] = '\0';
1634 1.1 christos
1635 1.1 christos LOCK(&lctx->lock);
1636 1.1 christos
1637 1.1 christos lctx->buffer[0] = '\0';
1638 1.1 christos
1639 1.1 christos lcfg = lctx->logconfig;
1640 1.1 christos
1641 1.1 christos category_channels = ISC_LIST_HEAD(lcfg->channellists[category->id]);
1642 1.1 christos
1643 1.1 christos /*
1644 1.1 christos * XXXDCL add duplicate filtering? (To not write multiple times to
1645 1.1 christos * the same source via various channels).
1646 1.1 christos */
1647 1.1 christos do {
1648 1.1 christos /*
1649 1.1 christos * If the channel list end was reached and a match was made,
1650 1.1 christos * everything is finished.
1651 1.1 christos */
1652 1.1 christos if (category_channels == NULL && matched)
1653 1.1 christos break;
1654 1.1 christos
1655 1.1 christos if (category_channels == NULL && ! matched &&
1656 1.1 christos category_channels != ISC_LIST_HEAD(lcfg->channellists[0]))
1657 1.1 christos /*
1658 1.1 christos * No category/module pair was explicitly configured.
1659 1.1 christos * Try the category named "default".
1660 1.1 christos */
1661 1.1 christos category_channels =
1662 1.1 christos ISC_LIST_HEAD(lcfg->channellists[0]);
1663 1.1 christos
1664 1.1 christos if (category_channels == NULL && ! matched)
1665 1.1 christos /*
1666 1.1 christos * No matching module was explicitly configured
1667 1.1 christos * for the category named "default". Use the internal
1668 1.1 christos * default channel.
1669 1.1 christos */
1670 1.1 christos category_channels = &default_channel;
1671 1.1 christos
1672 1.1 christos if (category_channels->module != NULL &&
1673 1.1 christos category_channels->module != module) {
1674 1.1 christos category_channels = ISC_LIST_NEXT(category_channels,
1675 1.1 christos link);
1676 1.1 christos continue;
1677 1.1 christos }
1678 1.1 christos
1679 1.1 christos matched = ISC_TRUE;
1680 1.1 christos
1681 1.1 christos channel = category_channels->channel;
1682 1.1 christos category_channels = ISC_LIST_NEXT(category_channels, link);
1683 1.1 christos
1684 1.1 christos if (((channel->flags & ISC_LOG_DEBUGONLY) != 0) &&
1685 1.1 christos lctx->debug_level == 0)
1686 1.1 christos continue;
1687 1.1 christos
1688 1.1 christos if (channel->level == ISC_LOG_DYNAMIC) {
1689 1.1 christos if (lctx->debug_level < level)
1690 1.1 christos continue;
1691 1.1 christos } else if (channel->level < level)
1692 1.1 christos continue;
1693 1.1 christos
1694 1.1 christos if ((channel->flags & ISC_LOG_PRINTTIME) != 0 &&
1695 1.1 christos local_time[0] == '\0')
1696 1.1 christos {
1697 1.1 christos isc_time_t isctime;
1698 1.1 christos
1699 1.1 christos TIME_NOW(&isctime);
1700 1.1 christos
1701 1.1 christos isc_time_formattimestamp(&isctime,
1702 1.1 christos local_time,
1703 1.1 christos sizeof(local_time));
1704 1.1 christos isc_time_formatISO8601ms(&isctime,
1705 1.1 christos iso8601z_string,
1706 1.1 christos sizeof(iso8601z_string));
1707 1.1 christos isc_time_formatISO8601Lms(&isctime,
1708 1.1 christos iso8601l_string,
1709 1.1 christos sizeof(iso8601l_string));
1710 1.1 christos }
1711 1.1 christos
1712 1.1 christos if ((channel->flags & ISC_LOG_PRINTLEVEL) != 0 &&
1713 1.1 christos level_string[0] == '\0') {
1714 1.1 christos if (level < ISC_LOG_CRITICAL)
1715 1.1 christos snprintf(level_string, sizeof(level_string),
1716 1.1 christos isc_msgcat_get(isc_msgcat,
1717 1.1 christos ISC_MSGSET_LOG,
1718 1.1 christos ISC_MSG_LEVEL,
1719 1.1 christos "level %d: "),
1720 1.1 christos level);
1721 1.1 christos else if (level > ISC_LOG_DYNAMIC)
1722 1.1 christos snprintf(level_string, sizeof(level_string),
1723 1.1 christos "%s %d: ", log_level_strings[0],
1724 1.1 christos level);
1725 1.1 christos else
1726 1.1 christos snprintf(level_string, sizeof(level_string),
1727 1.1 christos "%s: ", log_level_strings[-level]);
1728 1.1 christos }
1729 1.1 christos
1730 1.1 christos /*
1731 1.1 christos * Only format the message once.
1732 1.1 christos */
1733 1.1 christos if (lctx->buffer[0] == '\0') {
1734 1.1 christos (void)vsnprintf(lctx->buffer, sizeof(lctx->buffer),
1735 1.1 christos iformat, args);
1736 1.1 christos
1737 1.1 christos /*
1738 1.1 christos * Check for duplicates.
1739 1.1 christos */
1740 1.1 christos if (write_once) {
1741 1.1 christos isc_logmessage_t *message, *next;
1742 1.1 christos isc_time_t oldest;
1743 1.1 christos isc_interval_t interval;
1744 1.1 christos size_t size;
1745 1.1 christos
1746 1.1 christos isc_interval_set(&interval,
1747 1.1 christos lcfg->duplicate_interval, 0);
1748 1.1 christos
1749 1.1 christos /*
1750 1.1 christos * 'oldest' is the age of the oldest messages
1751 1.1 christos * which fall within the duplicate_interval
1752 1.1 christos * range.
1753 1.1 christos */
1754 1.1 christos TIME_NOW(&oldest);
1755 1.1 christos if (isc_time_subtract(&oldest, &interval,
1756 1.1 christos &oldest)
1757 1.1 christos != ISC_R_SUCCESS)
1758 1.1 christos /*
1759 1.1 christos * Can't effectively do the checking
1760 1.1 christos * without having a valid time.
1761 1.1 christos */
1762 1.1 christos message = NULL;
1763 1.1 christos else
1764 1.1 christos message = ISC_LIST_HEAD(lctx->messages);
1765 1.1 christos
1766 1.1 christos while (message != NULL) {
1767 1.1 christos if (isc_time_compare(&message->time,
1768 1.1 christos &oldest) < 0) {
1769 1.1 christos /*
1770 1.1 christos * This message is older
1771 1.1 christos * than the duplicate_interval,
1772 1.1 christos * so it should be dropped from
1773 1.1 christos * the history.
1774 1.1 christos *
1775 1.1 christos * Setting the interval to be
1776 1.1 christos * to be longer will obviously
1777 1.1 christos * not cause the expired
1778 1.1 christos * message to spring back into
1779 1.1 christos * existence.
1780 1.1 christos */
1781 1.1 christos next = ISC_LIST_NEXT(message,
1782 1.1 christos link);
1783 1.1 christos
1784 1.1 christos ISC_LIST_UNLINK(lctx->messages,
1785 1.1 christos message, link);
1786 1.1 christos
1787 1.1 christos isc_mem_put(lctx->mctx,
1788 1.1 christos message,
1789 1.1 christos sizeof(*message) + 1 +
1790 1.1 christos strlen(message->text));
1791 1.1 christos
1792 1.1 christos message = next;
1793 1.1 christos continue;
1794 1.1 christos }
1795 1.1 christos
1796 1.1 christos /*
1797 1.1 christos * This message is in the duplicate
1798 1.1 christos * filtering interval ...
1799 1.1 christos */
1800 1.1 christos if (strcmp(lctx->buffer, message->text)
1801 1.1 christos == 0) {
1802 1.1 christos /*
1803 1.1 christos * ... and it is a duplicate.
1804 1.1 christos * Unlock the mutex and
1805 1.1 christos * get the hell out of Dodge.
1806 1.1 christos */
1807 1.1 christos UNLOCK(&lctx->lock);
1808 1.1 christos return;
1809 1.1 christos }
1810 1.1 christos
1811 1.1 christos message = ISC_LIST_NEXT(message, link);
1812 1.1 christos }
1813 1.1 christos
1814 1.1 christos /*
1815 1.1 christos * It wasn't in the duplicate interval,
1816 1.1 christos * so add it to the message list.
1817 1.1 christos */
1818 1.1 christos size = sizeof(isc_logmessage_t) +
1819 1.1 christos strlen(lctx->buffer) + 1;
1820 1.1 christos message = isc_mem_get(lctx->mctx, size);
1821 1.1 christos if (message != NULL) {
1822 1.1 christos /*
1823 1.1 christos * Put the text immediately after
1824 1.1 christos * the struct. The strcpy is safe.
1825 1.1 christos */
1826 1.1 christos message->text = (char *)(message + 1);
1827 1.1 christos size -= sizeof(isc_logmessage_t);
1828 1.1 christos strlcpy(message->text, lctx->buffer,
1829 1.1 christos size);
1830 1.1 christos
1831 1.1 christos TIME_NOW(&message->time);
1832 1.1 christos
1833 1.1 christos ISC_LINK_INIT(message, link);
1834 1.1 christos ISC_LIST_APPEND(lctx->messages,
1835 1.1 christos message, link);
1836 1.1 christos }
1837 1.1 christos }
1838 1.1 christos }
1839 1.1 christos
1840 1.1 christos utc = ISC_TF((channel->flags & ISC_LOG_UTC) != 0);
1841 1.1 christos iso8601 = ISC_TF((channel->flags & ISC_LOG_ISO8601) != 0);
1842 1.1 christos printtime = ISC_TF((channel->flags & ISC_LOG_PRINTTIME)
1843 1.1 christos != 0);
1844 1.1 christos printtag = ISC_TF((channel->flags &
1845 1.1 christos (ISC_LOG_PRINTTAG|ISC_LOG_PRINTPREFIX))
1846 1.1 christos != 0 && lcfg->tag != NULL);
1847 1.1 christos printcolon = ISC_TF((channel->flags & ISC_LOG_PRINTTAG)
1848 1.1 christos != 0 && lcfg->tag != NULL);
1849 1.1 christos printcategory = ISC_TF((channel->flags & ISC_LOG_PRINTCATEGORY)
1850 1.1 christos != 0);
1851 1.1 christos printmodule = ISC_TF((channel->flags & ISC_LOG_PRINTMODULE)
1852 1.1 christos != 0);
1853 1.1 christos printlevel = ISC_TF((channel->flags & ISC_LOG_PRINTLEVEL)
1854 1.1 christos != 0);
1855 1.1 christos buffered = ISC_TF((channel->flags & ISC_LOG_BUFFERED)
1856 1.1 christos != 0);
1857 1.1 christos
1858 1.1 christos if (printtime) {
1859 1.1 christos if (iso8601) {
1860 1.1 christos if (utc) {
1861 1.1 christos time_string = iso8601z_string;
1862 1.1 christos } else {
1863 1.1 christos time_string = iso8601l_string;
1864 1.1 christos }
1865 1.1 christos } else {
1866 1.1 christos time_string = local_time;
1867 1.1 christos }
1868 1.1 christos } else
1869 1.1 christos time_string = "";
1870 1.1 christos
1871 1.1 christos switch (channel->type) {
1872 1.1 christos case ISC_LOG_TOFILE:
1873 1.1 christos if (FILE_MAXREACHED(channel)) {
1874 1.1 christos /*
1875 1.1 christos * If the file can be rolled, OR
1876 1.1 christos * If the file no longer exists, OR
1877 1.1 christos * If the file is less than the maximum size,
1878 1.1 christos * (such as if it had been renamed and
1879 1.1 christos * a new one touched, or it was truncated
1880 1.1 christos * in place)
1881 1.1 christos * ... then close it to trigger reopening.
1882 1.1 christos */
1883 1.1 christos if (FILE_VERSIONS(channel) !=
1884 1.1 christos ISC_LOG_ROLLNEVER ||
1885 1.1 christos (stat(FILE_NAME(channel), &statbuf) != 0 &&
1886 1.1 christos errno == ENOENT) ||
1887 1.1 christos statbuf.st_size < FILE_MAXSIZE(channel)) {
1888 1.1 christos (void)fclose(FILE_STREAM(channel));
1889 1.1 christos FILE_STREAM(channel) = NULL;
1890 1.1 christos FILE_MAXREACHED(channel) = ISC_FALSE;
1891 1.1 christos } else
1892 1.1 christos /*
1893 1.1 christos * Eh, skip it.
1894 1.1 christos */
1895 1.1 christos break;
1896 1.1 christos }
1897 1.1 christos
1898 1.1 christos if (FILE_STREAM(channel) == NULL) {
1899 1.1 christos result = isc_log_open(channel);
1900 1.1 christos if (result != ISC_R_SUCCESS &&
1901 1.1 christos result != ISC_R_MAXSIZE &&
1902 1.1 christos (channel->flags & ISC_LOG_OPENERR) == 0) {
1903 1.1 christos syslog(LOG_ERR,
1904 1.1 christos "isc_log_open '%s' failed: %s",
1905 1.1 christos FILE_NAME(channel),
1906 1.1 christos isc_result_totext(result));
1907 1.1 christos channel->flags |= ISC_LOG_OPENERR;
1908 1.1 christos }
1909 1.1 christos if (result != ISC_R_SUCCESS)
1910 1.1 christos break;
1911 1.1 christos channel->flags &= ~ISC_LOG_OPENERR;
1912 1.1 christos }
1913 1.1 christos /* FALLTHROUGH */
1914 1.1 christos
1915 1.1 christos case ISC_LOG_TOFILEDESC:
1916 1.1 christos fprintf(FILE_STREAM(channel),
1917 1.1 christos "%s%s%s%s%s%s%s%s%s%s\n",
1918 1.1 christos printtime ? time_string : "",
1919 1.1 christos printtime ? " " : "",
1920 1.1 christos printtag ? lcfg->tag : "",
1921 1.1 christos printcolon ? ": " : "",
1922 1.1 christos printcategory ? category->name : "",
1923 1.1 christos printcategory ? ": " : "",
1924 1.1 christos printmodule ? (module != NULL ? module->name
1925 1.1 christos : "no_module")
1926 1.1 christos : "",
1927 1.1 christos printmodule ? ": " : "",
1928 1.1 christos printlevel ? level_string : "",
1929 1.1 christos lctx->buffer);
1930 1.1 christos
1931 1.1 christos if (!buffered)
1932 1.1 christos fflush(FILE_STREAM(channel));
1933 1.1 christos
1934 1.1 christos /*
1935 1.1 christos * If the file now exceeds its maximum size
1936 1.1 christos * threshold, note it so that it will not be logged
1937 1.1 christos * to any more.
1938 1.1 christos */
1939 1.1 christos if (FILE_MAXSIZE(channel) > 0) {
1940 1.1 christos INSIST(channel->type == ISC_LOG_TOFILE);
1941 1.1 christos
1942 1.1 christos /* XXXDCL NT fstat/fileno */
1943 1.1 christos /* XXXDCL complain if fstat fails? */
1944 1.1 christos if (fstat(fileno(FILE_STREAM(channel)),
1945 1.1 christos &statbuf) >= 0 &&
1946 1.1 christos statbuf.st_size > FILE_MAXSIZE(channel))
1947 1.1 christos FILE_MAXREACHED(channel) = ISC_TRUE;
1948 1.1 christos }
1949 1.1 christos
1950 1.1 christos break;
1951 1.1 christos
1952 1.1 christos case ISC_LOG_TOSYSLOG:
1953 1.1 christos if (level > 0)
1954 1.1 christos syslog_level = LOG_DEBUG;
1955 1.1 christos else if (level < ISC_LOG_CRITICAL)
1956 1.1 christos syslog_level = LOG_CRIT;
1957 1.1 christos else
1958 1.1 christos syslog_level = syslog_map[-level];
1959 1.1 christos
1960 1.1 christos (void)syslog(FACILITY(channel) | syslog_level,
1961 1.1 christos "%s%s%s%s%s%s%s%s%s%s",
1962 1.1 christos printtime ? time_string : "",
1963 1.1 christos printtime ? " " : "",
1964 1.1 christos printtag ? lcfg->tag : "",
1965 1.1 christos printcolon ? ": " : "",
1966 1.1 christos printcategory ? category->name : "",
1967 1.1 christos printcategory ? ": " : "",
1968 1.1 christos printmodule ? (module != NULL
1969 1.1 christos ? module->name
1970 1.1 christos : "no_module")
1971 1.1 christos : "",
1972 1.1 christos printmodule ? ": " : "",
1973 1.1 christos printlevel ? level_string : "",
1974 1.1 christos lctx->buffer);
1975 1.1 christos break;
1976 1.1 christos
1977 1.1 christos case ISC_LOG_TONULL:
1978 1.1 christos break;
1979 1.1 christos
1980 1.1 christos }
1981 1.1 christos
1982 1.1 christos } while (1);
1983 1.1 christos
1984 1.1 christos UNLOCK(&lctx->lock);
1985 1.1 christos }
1986