#include <sys/time.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <isc/log.h>
#include <isc/util.h>
#define LOG_BUFFER_SIZE (8 * 1024)
typedef struct isc_logchannel isc_logchannel_t;
struct isc_logchannel {
char * name;
unsigned int type;
int level;
unsigned int flags;
isc_logdestination_t destination;
ISC_LINK(isc_logchannel_t) link;
};
typedef struct isc_logchannellist isc_logchannellist_t;
struct isc_logchannellist {
const isc_logmodule_t * module;
isc_logchannel_t * channel;
ISC_LINK(isc_logchannellist_t) link;
};
typedef struct isc_logmessage isc_logmessage_t;
struct isc_logmessage {
char * text;
struct timespec time;
ISC_LINK(isc_logmessage_t) link;
};
struct isc_logconfig {
isc_log_t * lctx;
ISC_LIST(isc_logchannel_t) channels;
ISC_LIST(isc_logchannellist_t) *channellists;
unsigned int channellist_count;
unsigned int duplicate_interval;
int highest_level;
char * tag;
int dynamic;
};
struct isc_log {
isc_logcategory_t * categories;
unsigned int category_count;
isc_logmodule_t * modules;
unsigned int module_count;
int debug_level;
isc_logconfig_t * logconfig;
char buffer[LOG_BUFFER_SIZE];
ISC_LIST(isc_logmessage_t) messages;
};
static const char *log_level_strings[] = {
"debug",
"info",
"notice",
"warning",
"error",
"critical"
};
static const int syslog_map[] = {
LOG_DEBUG,
LOG_INFO,
LOG_NOTICE,
LOG_WARNING,
LOG_ERR,
LOG_CRIT
};
isc_logcategory_t isc_categories[] = {
{ "default", 0 },
{ "general", 0 },
{ NULL, 0 }
};
isc_logmodule_t isc_modules[] = {
{ "socket", 0 },
{ "time", 0 },
{ "interface", 0 },
{ "timer", 0 },
{ "file", 0 },
{ "other", 0 },
{ NULL, 0 }
};
static isc_logchannellist_t default_channel;
isc_log_t *isc_lctx = NULL;
static isc_result_t
assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
const isc_logmodule_t *module, isc_logchannel_t *channel);
static isc_result_t
sync_channellist(isc_logconfig_t *lcfg);
static void
isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
isc_logmodule_t *module, int level, int write_once,
const char *format, va_list args)
__attribute__((__format__(__printf__, 6, 0)));
#define FACILITY(channel) (channel->destination.facility)
#define FILE_NAME(channel) (channel->destination.file.name)
#define FILE_STREAM(channel) (channel->destination.file.stream)
#define FILE_VERSIONS(channel) (channel->destination.file.versions)
#define FILE_MAXSIZE(channel) (channel->destination.file.maximum_size)
isc_result_t
isc_log_create(isc_log_t **lctxp, isc_logconfig_t **lcfgp) {
isc_log_t *lctx;
isc_logconfig_t *lcfg = NULL;
isc_result_t result;
REQUIRE(lctxp != NULL && *lctxp == NULL);
REQUIRE(lcfgp == NULL || *lcfgp == NULL);
lctx = malloc(sizeof(*lctx));
if (lctx != NULL) {
lctx->categories = NULL;
lctx->category_count = 0;
lctx->modules = NULL;
lctx->module_count = 0;
lctx->debug_level = 0;
ISC_LIST_INIT(lctx->messages);
isc_log_registercategories(lctx, isc_categories);
isc_log_registermodules(lctx, isc_modules);
result = isc_logconfig_create(lctx, &lcfg);
} else
result = ISC_R_NOMEMORY;
if (result == ISC_R_SUCCESS)
result = sync_channellist(lcfg);
if (result == ISC_R_SUCCESS) {
lctx->logconfig = lcfg;
*lctxp = lctx;
if (lcfgp != NULL)
*lcfgp = lcfg;
} else {
if (lcfg != NULL)
isc_logconfig_destroy(&lcfg);
if (lctx != NULL)
isc_log_destroy(&lctx);
}
return (result);
}
isc_result_t
isc_logconfig_create(isc_log_t *lctx, isc_logconfig_t **lcfgp) {
isc_logconfig_t *lcfg;
isc_logdestination_t destination;
isc_result_t result = ISC_R_SUCCESS;
int level = ISC_LOG_INFO;
REQUIRE(lcfgp != NULL && *lcfgp == NULL);
lcfg = malloc(sizeof(*lcfg));
if (lcfg != NULL) {
lcfg->lctx = lctx;
lcfg->channellists = NULL;
lcfg->channellist_count = 0;
lcfg->duplicate_interval = 0;
lcfg->highest_level = level;
lcfg->tag = NULL;
lcfg->dynamic = 0;
ISC_LIST_INIT(lcfg->channels);
} else
result = ISC_R_NOMEMORY;
if (result == ISC_R_SUCCESS) {
destination.facility = LOG_DAEMON;
result = isc_log_createchannel(lcfg, "default_syslog",
ISC_LOG_TOSYSLOG, level,
&destination, 0);
}
if (result == ISC_R_SUCCESS) {
destination.file.stream = stderr;
destination.file.name = NULL;
destination.file.versions = ISC_LOG_ROLLNEVER;
destination.file.maximum_size = 0;
result = isc_log_createchannel(lcfg, "default_stderr",
ISC_LOG_TOFILEDESC,
level,
&destination,
ISC_LOG_PRINTTIME);
}
if (result == ISC_R_SUCCESS) {
default_channel.channel = ISC_LIST_HEAD(lcfg->channels);
destination.file.stream = stderr;
destination.file.name = NULL;
destination.file.versions = ISC_LOG_ROLLNEVER;
destination.file.maximum_size = 0;
result = isc_log_createchannel(lcfg, "default_debug",
ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC,
&destination,
ISC_LOG_PRINTTIME);
}
if (result == ISC_R_SUCCESS)
result = isc_log_createchannel(lcfg, "null",
ISC_LOG_TONULL,
ISC_LOG_DYNAMIC,
NULL, 0);
if (result == ISC_R_SUCCESS)
*lcfgp = lcfg;
else
if (lcfg != NULL)
isc_logconfig_destroy(&lcfg);
return (result);
}
void
isc_log_destroy(isc_log_t **lctxp) {
isc_log_t *lctx;
isc_logconfig_t *lcfg;
isc_logmessage_t *message;
REQUIRE(lctxp != NULL);
lctx = *lctxp;
if (lctx->logconfig != NULL) {
lcfg = lctx->logconfig;
lctx->logconfig = NULL;
isc_logconfig_destroy(&lcfg);
}
while ((message = ISC_LIST_HEAD(lctx->messages)) != NULL) {
ISC_LIST_UNLINK(lctx->messages, message, link);
free(message);
}
lctx->buffer[0] = '\0';
lctx->debug_level = 0;
lctx->categories = NULL;
lctx->category_count = 0;
lctx->modules = NULL;
lctx->module_count = 0;
free(lctx);
*lctxp = NULL;
}
void
isc_logconfig_destroy(isc_logconfig_t **lcfgp) {
isc_logconfig_t *lcfg;
isc_logchannel_t *channel;
isc_logchannellist_t *item;
unsigned int i;
REQUIRE(lcfgp != NULL);
lcfg = *lcfgp;
REQUIRE(lcfg->lctx != NULL && lcfg->lctx->logconfig != lcfg);
while ((channel = ISC_LIST_HEAD(lcfg->channels)) != NULL) {
ISC_LIST_UNLINK(lcfg->channels, channel, link);
free(channel->name);
free(channel);
}
for (i = 0; i < lcfg->channellist_count; i++)
while ((item = ISC_LIST_HEAD(lcfg->channellists[i])) != NULL) {
ISC_LIST_UNLINK(lcfg->channellists[i], item, link);
free(item);
}
if (lcfg->channellist_count > 0)
free(lcfg->channellists);
lcfg->dynamic = 0;
if (lcfg->tag != NULL)
free(lcfg->tag);
lcfg->tag = NULL;
lcfg->highest_level = 0;
lcfg->duplicate_interval = 0;
free(lcfg);
*lcfgp = NULL;
}
void
isc_log_registercategories(isc_log_t *lctx, isc_logcategory_t categories[]) {
isc_logcategory_t *catp;
REQUIRE(categories != NULL && categories[0].name != NULL);
if (lctx->categories == NULL)
lctx->categories = categories;
else {
for (catp = lctx->categories; catp->name != NULL; )
if (catp->id == UINT_MAX)
DE_CONST(catp->name, catp);
else
catp++;
catp->name = (void *)categories;
catp->id = UINT_MAX;
}
for (catp = categories; catp->name != NULL; catp++)
catp->id = lctx->category_count++;
}
void
isc_log_registermodules(isc_log_t *lctx, isc_logmodule_t modules[]) {
isc_logmodule_t *modp;
REQUIRE(modules != NULL && modules[0].name != NULL);
if (lctx->modules == NULL)
lctx->modules = modules;
else {
for (modp = lctx->modules; modp->name != NULL; )
if (modp->id == UINT_MAX)
DE_CONST(modp->name, modp);
else
modp++;
modp->name = (void *)modules;
modp->id = UINT_MAX;
}
for (modp = modules; modp->name != NULL; modp++)
modp->id = lctx->module_count++;
}
isc_result_t
isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
unsigned int type, int level,
const isc_logdestination_t *destination,
unsigned int flags)
{
isc_logchannel_t *channel;
REQUIRE(name != NULL);
REQUIRE(type == ISC_LOG_TOSYSLOG ||
type == ISC_LOG_TOFILEDESC || type == ISC_LOG_TONULL);
REQUIRE(destination != NULL || type == ISC_LOG_TONULL);
REQUIRE(level >= ISC_LOG_CRITICAL);
REQUIRE((flags &
(unsigned int)~(ISC_LOG_PRINTALL | ISC_LOG_DEBUGONLY)) == 0);
channel = malloc(sizeof(*channel));
if (channel == NULL)
return (ISC_R_NOMEMORY);
channel->name = strdup(name);
if (channel->name == NULL) {
free(channel);
return (ISC_R_NOMEMORY);
}
channel->type = type;
channel->level = level;
channel->flags = flags;
ISC_LINK_INIT(channel, link);
switch (type) {
case ISC_LOG_TOSYSLOG:
FACILITY(channel) = destination->facility;
break;
case ISC_LOG_TOFILEDESC:
FILE_NAME(channel) = NULL;
FILE_STREAM(channel) = destination->file.stream;
FILE_MAXSIZE(channel) = 0;
FILE_VERSIONS(channel) = ISC_LOG_ROLLNEVER;
break;
case ISC_LOG_TONULL:
break;
default:
free(channel->name);
free(channel);
return (ISC_R_UNEXPECTED);
}
ISC_LIST_PREPEND(lcfg->channels, channel, link);
if (strcmp(name, "default_stderr") == 0)
default_channel.channel = channel;
return (ISC_R_SUCCESS);
}
isc_result_t
isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
const isc_logcategory_t *category,
const isc_logmodule_t *module)
{
isc_log_t *lctx;
isc_logchannel_t *channel;
isc_result_t result = ISC_R_SUCCESS;
unsigned int i;
REQUIRE(name != NULL);
lctx = lcfg->lctx;
REQUIRE(category == NULL || category->id < lctx->category_count);
REQUIRE(module == NULL || module->id < lctx->module_count);
for (channel = ISC_LIST_HEAD(lcfg->channels); channel != NULL;
channel = ISC_LIST_NEXT(channel, link))
if (strcmp(name, channel->name) == 0)
break;
if (channel == NULL)
return (ISC_R_NOTFOUND);
if (category != NULL)
result = assignchannel(lcfg, category->id, module, channel);
else
for (i = 0; i < lctx->category_count; i++) {
result = assignchannel(lcfg, i, module, channel);
if (result != ISC_R_SUCCESS)
break;
}
return (result);
}
void
isc_log_write(isc_log_t *lctx, isc_logcategory_t *category,
isc_logmodule_t *module, int level, const char *format, ...)
{
va_list args;
va_start(args, format);
isc_log_doit(lctx, category, module, level, 0, format, args);
va_end(args);
}
void
isc_log_setcontext(isc_log_t *lctx) {
isc_lctx = lctx;
}
void
isc_log_setdebuglevel(isc_log_t *lctx, unsigned int level) {
lctx->debug_level = level;
}
static isc_result_t
assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
const isc_logmodule_t *module, isc_logchannel_t *channel)
{
isc_logchannellist_t *new_item;
isc_log_t *lctx;
isc_result_t result;
lctx = lcfg->lctx;
REQUIRE(category_id < lctx->category_count);
REQUIRE(module == NULL || module->id < lctx->module_count);
REQUIRE(channel != NULL);
result = sync_channellist(lcfg);
if (result != ISC_R_SUCCESS)
return (result);
new_item = malloc(sizeof(*new_item));
if (new_item == NULL)
return (ISC_R_NOMEMORY);
new_item->channel = channel;
new_item->module = module;
ISC_LIST_INITANDPREPEND(lcfg->channellists[category_id],
new_item, link);
if (channel->type != ISC_LOG_TONULL) {
if (lcfg->highest_level < channel->level)
lcfg->highest_level = channel->level;
if (channel->level == ISC_LOG_DYNAMIC)
lcfg->dynamic = 1;
}
return (ISC_R_SUCCESS);
}
static isc_result_t
sync_channellist(isc_logconfig_t *lcfg) {
unsigned int bytes;
isc_log_t *lctx;
void *lists;
lctx = lcfg->lctx;
REQUIRE(lctx->category_count != 0);
if (lctx->category_count == lcfg->channellist_count)
return (ISC_R_SUCCESS);
bytes = lctx->category_count * sizeof(ISC_LIST(isc_logchannellist_t));
lists = malloc(bytes);
if (lists == NULL)
return (ISC_R_NOMEMORY);
memset(lists, 0, bytes);
if (lcfg->channellist_count != 0) {
bytes = lcfg->channellist_count *
sizeof(ISC_LIST(isc_logchannellist_t));
memmove(lists, lcfg->channellists, bytes);
free(lcfg->channellists);
}
lcfg->channellists = lists;
lcfg->channellist_count = lctx->category_count;
return (ISC_R_SUCCESS);
}
int
isc_log_wouldlog(isc_log_t *lctx, int level) {
if (lctx == NULL || lctx->logconfig == NULL)
return (0);
return (level <= lctx->logconfig->highest_level ||
(lctx->logconfig->dynamic &&
level <= lctx->debug_level));
}
static void
isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
isc_logmodule_t *module, int level, int write_once,
const char *format, va_list args)
{
int syslog_level;
char time_string[64];
char level_string[24];
const char *iformat;
int matched = 0;
int printtime, printtag, printcolon;
int printcategory, printmodule, printlevel;
isc_logconfig_t *lcfg;
isc_logchannel_t *channel;
isc_logchannellist_t *category_channels;
REQUIRE(category != NULL);
REQUIRE(module != NULL);
REQUIRE(level != ISC_LOG_DYNAMIC);
REQUIRE(format != NULL);
if (lctx == NULL)
return;
REQUIRE(category->id < lctx->category_count);
REQUIRE(module->id < lctx->module_count);
if (! isc_log_wouldlog(lctx, level))
return;
iformat = format;
time_string[0] = '\0';
level_string[0] = '\0';
lctx->buffer[0] = '\0';
lcfg = lctx->logconfig;
category_channels = ISC_LIST_HEAD(lcfg->channellists[category->id]);
do {
if (category_channels == NULL && matched)
break;
if (category_channels == NULL && ! matched &&
category_channels != ISC_LIST_HEAD(lcfg->channellists[0]))
category_channels =
ISC_LIST_HEAD(lcfg->channellists[0]);
if (category_channels == NULL && ! matched)
category_channels = &default_channel;
if (category_channels->module != NULL &&
category_channels->module != module) {
category_channels = ISC_LIST_NEXT(category_channels,
link);
continue;
}
matched = 1;
channel = category_channels->channel;
category_channels = ISC_LIST_NEXT(category_channels, link);
if (((channel->flags & ISC_LOG_DEBUGONLY) != 0) &&
lctx->debug_level == 0)
continue;
if (channel->level == ISC_LOG_DYNAMIC) {
if (lctx->debug_level < level)
continue;
} else if (channel->level < level)
continue;
if ((channel->flags & ISC_LOG_PRINTTIME) != 0 &&
time_string[0] == '\0') {
time_t now;
now = time(NULL);
strftime(time_string, sizeof(time_string),
"%d-%b-%Y %X", localtime(&now));
}
if ((channel->flags & ISC_LOG_PRINTLEVEL) != 0 &&
level_string[0] == '\0') {
if (level < ISC_LOG_CRITICAL)
snprintf(level_string, sizeof(level_string),
"level %d: ", level);
else if (level > ISC_LOG_DYNAMIC)
snprintf(level_string, sizeof(level_string),
"%s %d: ", log_level_strings[0],
level);
else
snprintf(level_string, sizeof(level_string),
"%s: ", log_level_strings[-level]);
}
if (lctx->buffer[0] == '\0') {
(void)vsnprintf(lctx->buffer, sizeof(lctx->buffer),
iformat, args);
if (write_once) {
isc_logmessage_t *message, *next;
struct timespec oldest;
struct timespec interval;
size_t size;
interval.tv_sec = lcfg->duplicate_interval;
interval.tv_nsec = 0;
clock_gettime(CLOCK_MONOTONIC, &oldest);
timespecsub(&oldest, &interval, &oldest);
message = ISC_LIST_HEAD(lctx->messages);
while (message != NULL) {
if (timespeccmp(&message->time,
&oldest, <)) {
next = ISC_LIST_NEXT(message,
link);
ISC_LIST_UNLINK(lctx->messages,
message, link);
free(message);
message = next;
continue;
}
if (strcmp(lctx->buffer, message->text)
== 0) {
return;
}
message = ISC_LIST_NEXT(message, link);
}
size = sizeof(isc_logmessage_t) +
strlen(lctx->buffer) + 1;
message = malloc(size);
if (message != NULL) {
message->text = (char *)(message + 1);
size -= sizeof(isc_logmessage_t);
strlcpy(message->text, lctx->buffer,
size);
clock_gettime(CLOCK_MONOTONIC,
&message->time);
ISC_LINK_INIT(message, link);
ISC_LIST_APPEND(lctx->messages,
message, link);
}
}
}
printtime = (channel->flags & ISC_LOG_PRINTTIME) != 0;
printtag = (channel->flags &
(ISC_LOG_PRINTTAG|ISC_LOG_PRINTPREFIX))
!= 0 && lcfg->tag != NULL;
printcolon = (channel->flags & ISC_LOG_PRINTTAG)
!= 0 && lcfg->tag != NULL;
printcategory = (channel->flags & ISC_LOG_PRINTCATEGORY) != 0;
printmodule = (channel->flags & ISC_LOG_PRINTMODULE) != 0;
printlevel = (channel->flags & ISC_LOG_PRINTLEVEL) != 0;
switch (channel->type) {
case ISC_LOG_TOFILEDESC:
fprintf(FILE_STREAM(channel),
"%s%s%s%s%s%s%s%s%s%s\n",
printtime ? time_string : "",
printtime ? " " : "",
printtag ? lcfg->tag : "",
printcolon ? ": " : "",
printcategory ? category->name : "",
printcategory ? ": " : "",
printmodule ? (module != NULL ? module->name
: "no_module")
: "",
printmodule ? ": " : "",
printlevel ? level_string : "",
lctx->buffer);
fflush(FILE_STREAM(channel));
break;
case ISC_LOG_TOSYSLOG:
if (level > 0)
syslog_level = LOG_DEBUG;
else if (level < ISC_LOG_CRITICAL)
syslog_level = LOG_CRIT;
else
syslog_level = syslog_map[-level];
(void)syslog(FACILITY(channel) | syslog_level,
"%s%s%s%s%s%s%s%s%s%s",
printtime ? time_string : "",
printtime ? " " : "",
printtag ? lcfg->tag : "",
printcolon ? ": " : "",
printcategory ? category->name : "",
printcategory ? ": " : "",
printmodule ? (module != NULL
? module->name
: "no_module")
: "",
printmodule ? ": " : "",
printlevel ? level_string : "",
lctx->buffer);
break;
case ISC_LOG_TONULL:
break;
}
} while (1);
}