log.c revision edcd1247ad7e81bb8b430e610d9718f64c70f05d
/*
* Copyright (C) 1999, 2000 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/* $Id: log.c,v 1.16 2000/02/26 19:57:01 tale Exp $ */
/* Principal Authors: DCL */
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <isc/assertions.h>
typedef struct isc_logchannel isc_logchannel_t;
/*
* This is the structure that holds each named channel. A simple linked
* list chains all of the channels together, so an individual channel is
* found by doing strcmp()s with the names down the list. Their should
* be no peformance penalty from this as it is expected that the number
* of named channels will be no more than a dozen or so, and name lookups
* from the head of the list are only done when isc_log_usechannel() is
* called, which should also be very infrequent.
*/
struct isc_logchannel {
char * name;
unsigned int type;
int level;
unsigned int flags;
/* XXXDCL should be an ISC_LINK, so this can be an ISC_LIST */
};
/*
* The logchannelist structure associates categories and modules with
* channels. First the appropriate channellist is found based on the
* category, and then each structure in the linked list is checked for
* a matching module. It is expected that the number of channels
* associated with any given category will be very short, no more than
* three or four in the more unusual cases.
*/
typedef struct isc_logchannellist isc_logchannellist_t;
struct isc_logchannellist {
/* XXXDCL should be an ISC_LINK, so this can be an ISC_LIST */
};
/*
* This structure is used to remember messages for pruning via
* isc_log_[v]write1().
*/
typedef struct isc_logmessage isc_logmessage_t;
struct isc_logmessage {
char * text;
};
/*
* XXX describe
*/
struct isc_logconfig {
unsigned int magic;
unsigned int channellist_count;
unsigned int duplicate_interval;
};
/*
* This isc_log structure provides the context for the isc_log functions.
* The log context locks itself in isc_log_doit, the internal backend to
* isc_log_write. The locking is necessary both to provide exclusive access
* to the the buffer into which the message is formatted and to guard against
* competing threads trying to write to the same syslog resource. (On
* Unfortunately, the lock cannot guard against a _different_ logging
* context in the same program competing for syslog's attention. Thus
* There Can Be Only One, but this is not enforced.
* XXX enforce it?
*
* Note that the category and module information is not locked.
* This is because in the usual case, only one isc_log_t is ever created
* XXX it might be wise to add more locking overall.
*/
struct isc_log {
/* Not locked. */
unsigned int magic;
unsigned int category_count;
unsigned int module_count;
int debug_level;
char buffer[LOG_BUFFER_SIZE];
/* Locked by isc_log lock. */
};
/*
* Used when ISC_LOG_PRINTLEVEL is enabled for a channel.
*/
static char *log_level_strings[] = {
"debug", "info", "notice", "warning", "error", "critical"
};
/*
* Used to convert ISC_LOG_* priorities into syslog priorities.
* XXXDCL NT
*/
static const int syslog_map[] = {
};
/*
* When adding new categories, a corresponding ISC_LOGCATEGORY_foo
*
* The default category is provided so that the internal default can
* be overridden. Since the default is always looked up as the first
* channellist in the log context, it must come first in isc_categories[].
*/
{ "default", 0 }, /* "default" must come first. */
{ NULL, 0 }
};
/*
* This essentially static structure must be filled in at run time,
* so that the default_debug channel's structure can be addressed.
*/
/*
* Forward declarations.
*/
static isc_result_t
static isc_result_t
static unsigned int
static isc_result_t
static void
/*
* Convenience macros.
*/
/****
**** Public interfaces.
****/
/*
* Establish a new logging context, with default channels.
*/
lctx->category_count = 0;
lctx->module_count = 0;
lctx->debug_level = 0;
/*
* Normally setting the magic number is the last step done
* in a creation function, but a valid log context is needed
* by isc_log_registercategories and isc_logconfig_create.
* If either fails, the lctx is destroyed and not returned
* to the caller.
*/
} else
if (result == ISC_R_SUCCESS) {
} else
return (result);
}
lcfg->channellist_count = 0;
lcfg->duplicate_interval = 0;
/*
* Normally the magic number is the last thing set in the
* structure, but isc_log_createchannel() needs a valid
* config. If the channel creation fails, the lcfg is not
* returned to the caller.
*/
} else
/*
* Create the default channels:
* default_syslog, default_stderr, default_debug and null.
*/
if (result == ISC_R_SUCCESS) {
&destination, 0);
}
if (result == ISC_R_SUCCESS) {
}
/*
* Set the default category's channel to default_stderr.
* XXX I find this odd.
*/
if (result == ISC_R_SUCCESS) {
}
if (result == ISC_R_SUCCESS)
NULL, 0);
if (result == ISC_R_SUCCESS)
else
return (result);
}
}
/*
* Ensure that lcfg->channellist_count == lctx->category_count.
* They won't be equal if isc_log_usechannel has not been called
* since any call to isc_log_registercategories.
*/
if (result != ISC_R_SUCCESS)
return (result);
return (ISC_R_SUCCESS);
}
void
}
}
lctx->debug_level = 0;
lctx->category_count = 0;
lctx->module_count = 0;
}
void
unsigned int i;
/*
* This function cannot be called with a logconfig that is in
* use by a log context.
*/
channel = next_channel) {
}
}
for (i = 0; i < lcfg->channellist_count; i++)
}
lcfg->duplicate_interval = 0;
}
void
/*
* Total the number of categories that will exist when these are added.
* Update the id number of the category with its new global id.
*/
}
void
/*
* Total the number of modules that will exist when these are added.
* Update the id number of the module with its new global id.
*/
}
{
/* XXXDCL find duplicate names? */
return (ISC_R_NOMEMORY);
return (ISC_R_NOMEMORY);
}
switch (type) {
case ISC_LOG_TOSYSLOG:
break;
case ISC_LOG_TOFILE:
/*
* The file name is copied because greatest_version wants
* to scribble on it, so it needs to be definitely in
* writable memory.
*/
break;
case ISC_LOG_TOFILEDESC:
FILE_MAXSIZE(channel) = 0;
break;
case ISC_LOG_TONULL:
/* Nothing. */
break;
default:
return (ISC_R_UNEXPECTED);
}
/*
* If default_stderr was redefined, make the default category
* point to the new default_stderr.
*/
return (ISC_R_SUCCESS);
}
{
unsigned int i;
break;
return (ISC_R_NOTFOUND);
else
/*
* Assign to all categories. Note that this includes
* the default channel.
*/
for (i = 0; i < lctx->category_count; i++) {
if (result != ISC_R_SUCCESS)
break;
}
return (result);
}
void
{
/*
* Contract checking is done in isc_log_doit().
*/
}
void
{
/*
* Contract checking is done in isc_log_doit().
*/
}
void
{
/*
* Contract checking is done in isc_log_doit().
*/
}
void
{
/*
* Contract checking is done in isc_log_doit().
*/
}
void
}
unsigned int
return (lctx->debug_level);
}
void
}
unsigned int
return (lcfg->duplicate_interval);
}
/* XXXDCL NT -- This interface will assuredly be changing. */
void
}
void
}
}
/****
**** Internal functions
****/
static isc_result_t
{
/*
* Ensure lcfg->channellist_count == lctx->category_count.
*/
if (result != ISC_R_SUCCESS)
return (result);
sizeof(*new_item));
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
}
static isc_result_t
int bytes;
return (ISC_R_SUCCESS);
return (ISC_R_NOMEMORY);
if (lcfg->channellist_count != 0) {
sizeof(isc_logchannellist_t *);
}
return (ISC_R_SUCCESS);
}
static unsigned int
/* XXXDCL HIGHLY NT */
unsigned int basenamelen;
*basename++ = '\0';
} else {
dirname = ".";
}
isc_dir_init(&dir);
if (result != ISC_R_SUCCESS)
return (0); /* ... and roll_log will likely report an error. */
&digit_end, 10);
}
}
*--basename = '/';
return (++greatest);
}
static isc_result_t
char *path;
/*
* XXXDCL versions = 0 & versions == ISC_LOG_ROLLINFINITE do not work.
*/
/*
* Do nothing (not even excess version trimming) if ISC_LOG_ROLLNEVER
* is specified. Apparently complete external control over the log
* files is desired.
*/
return (ISC_R_SUCCESS);
/*
* Set greatest_version to the greatest existing version
* (not the maximum requested version). This is 1 based even
* though the file names are 0 based, so an oldest log of log.1
* is a greatest_version of 2.
*/
/*
* Now greatest should be set to the highest version number desired.
* Since the highest number is one less than FILE_VERSIONS(channel)
* when not doing infinite log rolling, greatest will need to be
* decremented when it is equal to -- or greater than --
* FILE_VERSIONS(channel). When greatest is less than
* FILE_VERSIONS(channel), it is already suitable for use as
* the maximum version number.
*/
; /* Do nothing. */
else
/*
* When greatest is >= FILE_VERSIONS(channel), it needs to
* be reduced until it is FILE_VERSIONS(channel) - 1.
* Remove any excess logs on the way to that value.
*/
}
for (i = greatest; i > 0; i /= 10)
digits++;
/*
* Ensure the name fits in the filesystem. Note that in this will not
* trigger failure until there is going to be a log rolled into a name
* that is too long, not when the maximum possible version name would
* be too long. Imagine a case where the name for logs 0-9 is exactly
* as long as the maximum filename, but FILE_VERSIONS is configured as
* 11. log.10's name will be too long, but no error will be triggered
* until log.9 exists and needs to be rolled.
*/
return (ISC_R_INVALIDFILE);
for (i = greatest; i > 0; i--) {
}
if (FILE_VERSIONS(channel) != 0) {
} else if (FILE_VERSIONS(channel) == 0)
return (ISC_R_SUCCESS);
}
static isc_result_t
char *path;
/*
* Determine type of file; only regular files will be
* version renamed.
*/
else
return (ISC_R_INVALIDFILE);
/*
* Version control.
*/
if (regular_file)
return (ISC_R_INVALIDFILE);
/* XXXDCL if not regular_file complain? */
return (ISC_R_INVALIDFILE);
return (ISC_R_SUCCESS);
}
static void
{
int syslog_level;
char time_string[64];
char level_string[24];
/*
* Programs can use libraries that use this logging code without
* wanting to do any logging, thus the log context is allowed to
* be non-existent.
*/
return;
time_string[0] = '\0';
level_string[0] = '\0';
/*
* XXX duplicate filtering (do not write multiple times to same source
* via various channels)
*/
do {
/*
* If the channel list end was reached and a match was made,
* everything is finished.
*/
break;
/*
* Try the category named "default".
*/
/*
* No matching module was explicitly configured
* for the category named "default". Use the internal
* default channel.
*/
continue;
}
continue;
continue;
time_string[0] == '\0') {
/*
* Emulate syslog's time format.
* XXXDCL it would be nice if format were configurable.
*/
"%b %d %X ", timeptr);
}
level_string[0] == '\0') {
if (level < ISC_LOG_CRITICAL)
else if (level > ISC_LOG_DYNAMIC)
log_level_strings[0], level);
else
}
/*
* Only format the message once.
*/
/*
* Check for duplicates.
*/
if (write_once) {
lcfg->duplicate_interval, 0);
/*
* 'oldest' is the age of the oldest messages
* which fall within the duplicate_interval
* range.
*/
else
&oldest);
&oldest) < 0) {
/*
* This message is older
* than the duplicate_interval,
* so it should be dropped from
* the history.
*
* Setting the interval to be
* to be longer will obviously
* not cause the expired
* message to spring back into
* existence.
*/
link);
sizeof(*message) + 1 +
continue;
}
/*
* This message is in the duplicate
* filtering interval ...
*/
== 0) {
/*
* ... and it is a duplicate.
* Unlock the mutex and
* get the hell out of Dodge.
*/
return;
}
}
/*
* It wasn't in the duplicate interval,
* so add it to the message list.
*/
sizeof(isc_logmessage_t) +
/*
* Put the text immediately after
* the struct. The strcpy is safe.
*/
/*
* This will cause the message
* to immediately expire on
* the next call to [v]write1.
* What's a fella to do if
* getting the time fails?
*/
}
}
}
case ISC_LOG_TOFILE:
if (result != ISC_R_SUCCESS)
break;
/*
* Probably something more meaningful should be
* done with an error.
*/
}
/* FALLTHROUGH */
case ISC_LOG_TOFILEDESC:
time_string : "",
": " : "",
"no_module")
: "",
": " : "",
level_string : "",
/*
* If the file now exceeds its maximum size
* threshold, close it and mark it ready
* for reopening the next time the channel is used.
*/
if (FILE_MAXSIZE(channel) != 0) {
/* XXXDCL complain if fstat fails? */
&statbuf) >= 0 &&
}
}
break;
case ISC_LOG_TOSYSLOG:
if (level > 0)
else if (level < ISC_LOG_CRITICAL)
else
"%s%s%s%s%s%s%s",
time_string : "",
": " : "",
"no_module")
: "",
": " : "",
level_string : "",
break;
case ISC_LOG_TONULL:
break;
}
} while (1);
}