10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
3*11038SRao.Shoaib@Sun.COM * Copyright (C) 1996-1999, 2001, 2003 Internet Software Consortium.
40Sstevel@tonic-gate *
5*11038SRao.Shoaib@Sun.COM * Permission to use, copy, modify, and/or distribute this software for any
60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate *
9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*11038SRao.Shoaib@Sun.COM * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*11038SRao.Shoaib@Sun.COM * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*11038SRao.Shoaib@Sun.COM * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*11038SRao.Shoaib@Sun.COM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*11038SRao.Shoaib@Sun.COM * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*11038SRao.Shoaib@Sun.COM * PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate */
170Sstevel@tonic-gate
180Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: logging.c,v 1.9 2008/11/14 02:36:51 marka Exp $";
200Sstevel@tonic-gate #endif /* not lint */
210Sstevel@tonic-gate
220Sstevel@tonic-gate #include "port_before.h"
230Sstevel@tonic-gate
240Sstevel@tonic-gate #include <sys/types.h>
250Sstevel@tonic-gate #include <sys/time.h>
260Sstevel@tonic-gate #include <sys/stat.h>
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <fcntl.h>
290Sstevel@tonic-gate #include <limits.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <stdarg.h>
340Sstevel@tonic-gate #include <syslog.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <time.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate #include <isc/assertions.h>
400Sstevel@tonic-gate #include <isc/logging.h>
410Sstevel@tonic-gate #include <isc/memcluster.h>
420Sstevel@tonic-gate #include <isc/misc.h>
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include "port_after.h"
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include "logging_p.h"
470Sstevel@tonic-gate
480Sstevel@tonic-gate static const int syslog_priority[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE,
490Sstevel@tonic-gate LOG_WARNING, LOG_ERR, LOG_CRIT };
500Sstevel@tonic-gate
510Sstevel@tonic-gate static const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
520Sstevel@tonic-gate "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
530Sstevel@tonic-gate
540Sstevel@tonic-gate static const char *level_text[] = {
550Sstevel@tonic-gate "info: ", "notice: ", "warning: ", "error: ", "critical: "
560Sstevel@tonic-gate };
570Sstevel@tonic-gate
580Sstevel@tonic-gate static void
version_rename(log_channel chan)590Sstevel@tonic-gate version_rename(log_channel chan) {
600Sstevel@tonic-gate unsigned int ver;
610Sstevel@tonic-gate char old_name[PATH_MAX+1];
620Sstevel@tonic-gate char new_name[PATH_MAX+1];
63*11038SRao.Shoaib@Sun.COM
640Sstevel@tonic-gate ver = chan->out.file.versions;
650Sstevel@tonic-gate if (ver < 1)
660Sstevel@tonic-gate return;
670Sstevel@tonic-gate if (ver > LOG_MAX_VERSIONS)
680Sstevel@tonic-gate ver = LOG_MAX_VERSIONS;
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate * Need to have room for '.nn' (XXX assumes LOG_MAX_VERSIONS < 100)
710Sstevel@tonic-gate */
72*11038SRao.Shoaib@Sun.COM if (strlen(chan->out.file.name) > (size_t)(PATH_MAX-3))
730Sstevel@tonic-gate return;
740Sstevel@tonic-gate for (ver--; ver > 0; ver--) {
750Sstevel@tonic-gate sprintf(old_name, "%s.%d", chan->out.file.name, ver-1);
760Sstevel@tonic-gate sprintf(new_name, "%s.%d", chan->out.file.name, ver);
77*11038SRao.Shoaib@Sun.COM (void)isc_movefile(old_name, new_name);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate sprintf(new_name, "%s.0", chan->out.file.name);
80*11038SRao.Shoaib@Sun.COM (void)isc_movefile(chan->out.file.name, new_name);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate FILE *
log_open_stream(log_channel chan)840Sstevel@tonic-gate log_open_stream(log_channel chan) {
850Sstevel@tonic-gate FILE *stream;
860Sstevel@tonic-gate int fd, flags;
870Sstevel@tonic-gate struct stat sb;
880Sstevel@tonic-gate int regular;
890Sstevel@tonic-gate
900Sstevel@tonic-gate if (chan == NULL || chan->type != log_file) {
910Sstevel@tonic-gate errno = EINVAL;
920Sstevel@tonic-gate return (NULL);
930Sstevel@tonic-gate }
94*11038SRao.Shoaib@Sun.COM
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * Don't open already open streams
970Sstevel@tonic-gate */
980Sstevel@tonic-gate if (chan->out.file.stream != NULL)
990Sstevel@tonic-gate return (chan->out.file.stream);
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate if (stat(chan->out.file.name, &sb) < 0) {
1020Sstevel@tonic-gate if (errno != ENOENT) {
1030Sstevel@tonic-gate syslog(LOG_ERR,
1040Sstevel@tonic-gate "log_open_stream: stat of %s failed: %s",
1050Sstevel@tonic-gate chan->out.file.name, strerror(errno));
1060Sstevel@tonic-gate chan->flags |= LOG_CHANNEL_BROKEN;
1070Sstevel@tonic-gate return (NULL);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate regular = 1;
1100Sstevel@tonic-gate } else
111*11038SRao.Shoaib@Sun.COM regular = (sb.st_mode & S_IFREG);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if (chan->out.file.versions) {
1140Sstevel@tonic-gate if (!regular) {
1150Sstevel@tonic-gate syslog(LOG_ERR,
1160Sstevel@tonic-gate "log_open_stream: want versions but %s isn't a regular file",
1170Sstevel@tonic-gate chan->out.file.name);
1180Sstevel@tonic-gate chan->flags |= LOG_CHANNEL_BROKEN;
1190Sstevel@tonic-gate errno = EINVAL;
1200Sstevel@tonic-gate return (NULL);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate flags = O_WRONLY|O_CREAT|O_APPEND;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if ((chan->flags & LOG_TRUNCATE) != 0) {
1270Sstevel@tonic-gate if (regular) {
1280Sstevel@tonic-gate (void)unlink(chan->out.file.name);
1290Sstevel@tonic-gate flags |= O_EXCL;
1300Sstevel@tonic-gate } else {
1310Sstevel@tonic-gate syslog(LOG_ERR,
1320Sstevel@tonic-gate "log_open_stream: want truncation but %s isn't a regular file",
1330Sstevel@tonic-gate chan->out.file.name);
1340Sstevel@tonic-gate chan->flags |= LOG_CHANNEL_BROKEN;
1350Sstevel@tonic-gate errno = EINVAL;
1360Sstevel@tonic-gate return (NULL);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate fd = open(chan->out.file.name, flags,
1410Sstevel@tonic-gate S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
1420Sstevel@tonic-gate if (fd < 0) {
1430Sstevel@tonic-gate syslog(LOG_ERR, "log_open_stream: open(%s) failed: %s",
1440Sstevel@tonic-gate chan->out.file.name, strerror(errno));
1450Sstevel@tonic-gate chan->flags |= LOG_CHANNEL_BROKEN;
1460Sstevel@tonic-gate return (NULL);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate stream = fdopen(fd, "a");
1490Sstevel@tonic-gate if (stream == NULL) {
1500Sstevel@tonic-gate syslog(LOG_ERR, "log_open_stream: fdopen() failed");
1510Sstevel@tonic-gate chan->flags |= LOG_CHANNEL_BROKEN;
1520Sstevel@tonic-gate return (NULL);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate (void) fchown(fd, chan->out.file.owner, chan->out.file.group);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate chan->out.file.stream = stream;
1570Sstevel@tonic-gate return (stream);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate int
log_close_stream(log_channel chan)1610Sstevel@tonic-gate log_close_stream(log_channel chan) {
1620Sstevel@tonic-gate FILE *stream;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate if (chan == NULL || chan->type != log_file) {
1650Sstevel@tonic-gate errno = EINVAL;
1660Sstevel@tonic-gate return (0);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate stream = chan->out.file.stream;
1690Sstevel@tonic-gate chan->out.file.stream = NULL;
1700Sstevel@tonic-gate if (stream != NULL && fclose(stream) == EOF)
1710Sstevel@tonic-gate return (-1);
1720Sstevel@tonic-gate return (0);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate void
log_close_debug_channels(log_context lc)1760Sstevel@tonic-gate log_close_debug_channels(log_context lc) {
1770Sstevel@tonic-gate log_channel_list lcl;
1780Sstevel@tonic-gate int i;
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate for (i = 0; i < lc->num_categories; i++)
1810Sstevel@tonic-gate for (lcl = lc->categories[i]; lcl != NULL; lcl = lcl->next)
1820Sstevel@tonic-gate if (lcl->channel->type == log_file &&
1830Sstevel@tonic-gate lcl->channel->out.file.stream != NULL &&
1840Sstevel@tonic-gate lcl->channel->flags & LOG_REQUIRE_DEBUG)
1850Sstevel@tonic-gate (void)log_close_stream(lcl->channel);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate FILE *
log_get_stream(log_channel chan)1890Sstevel@tonic-gate log_get_stream(log_channel chan) {
1900Sstevel@tonic-gate if (chan == NULL || chan->type != log_file) {
1910Sstevel@tonic-gate errno = EINVAL;
1920Sstevel@tonic-gate return (NULL);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate return (chan->out.file.stream);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate char *
log_get_filename(log_channel chan)1980Sstevel@tonic-gate log_get_filename(log_channel chan) {
1990Sstevel@tonic-gate if (chan == NULL || chan->type != log_file) {
2000Sstevel@tonic-gate errno = EINVAL;
2010Sstevel@tonic-gate return (NULL);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate return (chan->out.file.name);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate int
log_check_channel(log_context lc,int level,log_channel chan)2070Sstevel@tonic-gate log_check_channel(log_context lc, int level, log_channel chan) {
2080Sstevel@tonic-gate int debugging, chan_level;
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate REQUIRE(lc != NULL);
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate debugging = ((lc->flags & LOG_OPTION_DEBUG) != 0);
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate * If not debugging, short circuit debugging messages very early.
2160Sstevel@tonic-gate */
2170Sstevel@tonic-gate if (level > 0 && !debugging)
2180Sstevel@tonic-gate return (0);
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate if ((chan->flags & (LOG_CHANNEL_BROKEN|LOG_CHANNEL_OFF)) != 0)
2210Sstevel@tonic-gate return (0);
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate /* Some channels only log when debugging is on. */
2240Sstevel@tonic-gate if ((chan->flags & LOG_REQUIRE_DEBUG) && !debugging)
2250Sstevel@tonic-gate return (0);
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /* Some channels use the global level. */
2280Sstevel@tonic-gate if ((chan->flags & LOG_USE_CONTEXT_LEVEL) != 0) {
2290Sstevel@tonic-gate chan_level = lc->level;
2300Sstevel@tonic-gate } else
2310Sstevel@tonic-gate chan_level = chan->level;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (level > chan_level)
2340Sstevel@tonic-gate return (0);
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate return (1);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
239*11038SRao.Shoaib@Sun.COM int
log_check(log_context lc,int category,int level)2400Sstevel@tonic-gate log_check(log_context lc, int category, int level) {
2410Sstevel@tonic-gate log_channel_list lcl;
2420Sstevel@tonic-gate int debugging;
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate REQUIRE(lc != NULL);
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate debugging = ((lc->flags & LOG_OPTION_DEBUG) != 0);
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * If not debugging, short circuit debugging messages very early.
2500Sstevel@tonic-gate */
2510Sstevel@tonic-gate if (level > 0 && !debugging)
2520Sstevel@tonic-gate return (0);
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if (category < 0 || category > lc->num_categories)
255*11038SRao.Shoaib@Sun.COM category = 0; /*%< use default */
2560Sstevel@tonic-gate lcl = lc->categories[category];
2570Sstevel@tonic-gate if (lcl == NULL) {
2580Sstevel@tonic-gate category = 0;
2590Sstevel@tonic-gate lcl = lc->categories[0];
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate for ( /* nothing */; lcl != NULL; lcl = lcl->next) {
2630Sstevel@tonic-gate if (log_check_channel(lc, level, lcl->channel))
2640Sstevel@tonic-gate return (1);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate return (0);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate void
log_vwrite(log_context lc,int category,int level,const char * format,va_list args)270*11038SRao.Shoaib@Sun.COM log_vwrite(log_context lc, int category, int level, const char *format,
2710Sstevel@tonic-gate va_list args) {
2720Sstevel@tonic-gate log_channel_list lcl;
2730Sstevel@tonic-gate int pri, debugging, did_vsprintf = 0;
2740Sstevel@tonic-gate int original_category;
2750Sstevel@tonic-gate FILE *stream;
2760Sstevel@tonic-gate log_channel chan;
2770Sstevel@tonic-gate struct timeval tv;
2780Sstevel@tonic-gate struct tm *local_tm;
2790Sstevel@tonic-gate #ifdef HAVE_TIME_R
2800Sstevel@tonic-gate struct tm tm_tmp;
2810Sstevel@tonic-gate #endif
2820Sstevel@tonic-gate time_t tt;
2830Sstevel@tonic-gate const char *category_name;
2840Sstevel@tonic-gate const char *level_str;
2850Sstevel@tonic-gate char time_buf[256];
2860Sstevel@tonic-gate char level_buf[256];
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate REQUIRE(lc != NULL);
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate debugging = (lc->flags & LOG_OPTION_DEBUG);
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate * If not debugging, short circuit debugging messages very early.
2940Sstevel@tonic-gate */
2950Sstevel@tonic-gate if (level > 0 && !debugging)
2960Sstevel@tonic-gate return;
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate if (category < 0 || category > lc->num_categories)
299*11038SRao.Shoaib@Sun.COM category = 0; /*%< use default */
3000Sstevel@tonic-gate original_category = category;
3010Sstevel@tonic-gate lcl = lc->categories[category];
3020Sstevel@tonic-gate if (lcl == NULL) {
3030Sstevel@tonic-gate category = 0;
3040Sstevel@tonic-gate lcl = lc->categories[0];
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate * Get the current time and format it.
3090Sstevel@tonic-gate */
3100Sstevel@tonic-gate time_buf[0]='\0';
3110Sstevel@tonic-gate if (gettimeofday(&tv, NULL) < 0) {
3120Sstevel@tonic-gate syslog(LOG_INFO, "gettimeofday failed in log_vwrite()");
3130Sstevel@tonic-gate } else {
3140Sstevel@tonic-gate tt = tv.tv_sec;
3150Sstevel@tonic-gate #ifdef HAVE_TIME_R
3160Sstevel@tonic-gate local_tm = localtime_r(&tt, &tm_tmp);
3170Sstevel@tonic-gate #else
3180Sstevel@tonic-gate local_tm = localtime(&tt);
3190Sstevel@tonic-gate #endif
3200Sstevel@tonic-gate if (local_tm != NULL) {
3210Sstevel@tonic-gate sprintf(time_buf, "%02d-%s-%4d %02d:%02d:%02d.%03ld ",
3220Sstevel@tonic-gate local_tm->tm_mday, months[local_tm->tm_mon],
3230Sstevel@tonic-gate local_tm->tm_year+1900, local_tm->tm_hour,
3240Sstevel@tonic-gate local_tm->tm_min, local_tm->tm_sec,
3250Sstevel@tonic-gate (long)tv.tv_usec/1000);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate * Make a string representation of the current category and level
3310Sstevel@tonic-gate */
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate if (lc->category_names != NULL &&
3340Sstevel@tonic-gate lc->category_names[original_category] != NULL)
3350Sstevel@tonic-gate category_name = lc->category_names[original_category];
3360Sstevel@tonic-gate else
3370Sstevel@tonic-gate category_name = "";
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate if (level >= log_critical) {
3400Sstevel@tonic-gate if (level >= 0) {
3410Sstevel@tonic-gate sprintf(level_buf, "debug %d: ", level);
3420Sstevel@tonic-gate level_str = level_buf;
3430Sstevel@tonic-gate } else
3440Sstevel@tonic-gate level_str = level_text[-level-1];
3450Sstevel@tonic-gate } else {
3460Sstevel@tonic-gate sprintf(level_buf, "level %d: ", level);
3470Sstevel@tonic-gate level_str = level_buf;
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate * Write the message to channels.
3520Sstevel@tonic-gate */
3530Sstevel@tonic-gate for ( /* nothing */; lcl != NULL; lcl = lcl->next) {
3540Sstevel@tonic-gate chan = lcl->channel;
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate if (!log_check_channel(lc, level, chan))
3570Sstevel@tonic-gate continue;
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate if (!did_vsprintf) {
360*11038SRao.Shoaib@Sun.COM (void)vsprintf(lc->buffer, format, args);
361*11038SRao.Shoaib@Sun.COM if (strlen(lc->buffer) > (size_t)LOG_BUFFER_SIZE) {
3620Sstevel@tonic-gate syslog(LOG_CRIT,
3630Sstevel@tonic-gate "memory overrun in log_vwrite()");
3640Sstevel@tonic-gate exit(1);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate did_vsprintf = 1;
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate switch (chan->type) {
3700Sstevel@tonic-gate case log_syslog:
3710Sstevel@tonic-gate if (level >= log_critical)
3720Sstevel@tonic-gate pri = (level >= 0) ? 0 : -level;
3730Sstevel@tonic-gate else
3740Sstevel@tonic-gate pri = -log_critical;
3750Sstevel@tonic-gate syslog(chan->out.facility|syslog_priority[pri],
3760Sstevel@tonic-gate "%s%s%s%s",
3770Sstevel@tonic-gate (chan->flags & LOG_TIMESTAMP) ? time_buf : "",
3780Sstevel@tonic-gate (chan->flags & LOG_PRINT_CATEGORY) ?
3790Sstevel@tonic-gate category_name : "",
3800Sstevel@tonic-gate (chan->flags & LOG_PRINT_LEVEL) ?
3810Sstevel@tonic-gate level_str : "",
3820Sstevel@tonic-gate lc->buffer);
3830Sstevel@tonic-gate break;
3840Sstevel@tonic-gate case log_file:
3850Sstevel@tonic-gate stream = chan->out.file.stream;
3860Sstevel@tonic-gate if (stream == NULL) {
3870Sstevel@tonic-gate stream = log_open_stream(chan);
3880Sstevel@tonic-gate if (stream == NULL)
3890Sstevel@tonic-gate break;
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate if (chan->out.file.max_size != ULONG_MAX) {
3920Sstevel@tonic-gate long pos;
393*11038SRao.Shoaib@Sun.COM
3940Sstevel@tonic-gate pos = ftell(stream);
3950Sstevel@tonic-gate if (pos >= 0 &&
3960Sstevel@tonic-gate (unsigned long)pos >
3970Sstevel@tonic-gate chan->out.file.max_size) {
3980Sstevel@tonic-gate /*
3990Sstevel@tonic-gate * try to roll over the log files,
4000Sstevel@tonic-gate * ignoring all all return codes
4010Sstevel@tonic-gate * except the open (we don't want
4020Sstevel@tonic-gate * to write any more anyway)
4030Sstevel@tonic-gate */
4040Sstevel@tonic-gate log_close_stream(chan);
4050Sstevel@tonic-gate version_rename(chan);
4060Sstevel@tonic-gate stream = log_open_stream(chan);
4070Sstevel@tonic-gate if (stream == NULL)
4080Sstevel@tonic-gate break;
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate }
411*11038SRao.Shoaib@Sun.COM fprintf(stream, "%s%s%s%s\n",
4120Sstevel@tonic-gate (chan->flags & LOG_TIMESTAMP) ? time_buf : "",
4130Sstevel@tonic-gate (chan->flags & LOG_PRINT_CATEGORY) ?
4140Sstevel@tonic-gate category_name : "",
4150Sstevel@tonic-gate (chan->flags & LOG_PRINT_LEVEL) ?
4160Sstevel@tonic-gate level_str : "",
4170Sstevel@tonic-gate lc->buffer);
4180Sstevel@tonic-gate fflush(stream);
4190Sstevel@tonic-gate break;
4200Sstevel@tonic-gate case log_null:
4210Sstevel@tonic-gate break;
4220Sstevel@tonic-gate default:
4230Sstevel@tonic-gate syslog(LOG_ERR,
4240Sstevel@tonic-gate "unknown channel type in log_vwrite()");
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate void
log_write(log_context lc,int category,int level,const char * format,...)4300Sstevel@tonic-gate log_write(log_context lc, int category, int level, const char *format, ...) {
4310Sstevel@tonic-gate va_list args;
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate va_start(args, format);
4340Sstevel@tonic-gate log_vwrite(lc, category, level, format, args);
4350Sstevel@tonic-gate va_end(args);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate
438*11038SRao.Shoaib@Sun.COM /*%
4390Sstevel@tonic-gate * Functions to create, set, or destroy contexts
4400Sstevel@tonic-gate */
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate int
log_new_context(int num_categories,char ** category_names,log_context * lc)4430Sstevel@tonic-gate log_new_context(int num_categories, char **category_names, log_context *lc) {
4440Sstevel@tonic-gate log_context nlc;
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate nlc = memget(sizeof (struct log_context));
4470Sstevel@tonic-gate if (nlc == NULL) {
4480Sstevel@tonic-gate errno = ENOMEM;
4490Sstevel@tonic-gate return (-1);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate nlc->num_categories = num_categories;
4520Sstevel@tonic-gate nlc->category_names = category_names;
4530Sstevel@tonic-gate nlc->categories = memget(num_categories * sizeof (log_channel_list));
4540Sstevel@tonic-gate if (nlc->categories == NULL) {
4550Sstevel@tonic-gate memput(nlc, sizeof (struct log_context));
4560Sstevel@tonic-gate errno = ENOMEM;
4570Sstevel@tonic-gate return (-1);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate memset(nlc->categories, '\0',
4600Sstevel@tonic-gate num_categories * sizeof (log_channel_list));
4610Sstevel@tonic-gate nlc->flags = 0U;
4620Sstevel@tonic-gate nlc->level = 0;
4630Sstevel@tonic-gate *lc = nlc;
4640Sstevel@tonic-gate return (0);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate void
log_free_context(log_context lc)4680Sstevel@tonic-gate log_free_context(log_context lc) {
4690Sstevel@tonic-gate log_channel_list lcl, lcl_next;
4700Sstevel@tonic-gate log_channel chan;
4710Sstevel@tonic-gate int i;
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate REQUIRE(lc != NULL);
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate for (i = 0; i < lc->num_categories; i++)
4760Sstevel@tonic-gate for (lcl = lc->categories[i]; lcl != NULL; lcl = lcl_next) {
4770Sstevel@tonic-gate lcl_next = lcl->next;
4780Sstevel@tonic-gate chan = lcl->channel;
4790Sstevel@tonic-gate (void)log_free_channel(chan);
4800Sstevel@tonic-gate memput(lcl, sizeof (struct log_channel_list));
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate memput(lc->categories,
4830Sstevel@tonic-gate lc->num_categories * sizeof (log_channel_list));
4840Sstevel@tonic-gate memput(lc, sizeof (struct log_context));
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate int
log_add_channel(log_context lc,int category,log_channel chan)4880Sstevel@tonic-gate log_add_channel(log_context lc, int category, log_channel chan) {
4890Sstevel@tonic-gate log_channel_list lcl;
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate if (lc == NULL || category < 0 || category >= lc->num_categories) {
4920Sstevel@tonic-gate errno = EINVAL;
4930Sstevel@tonic-gate return (-1);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate lcl = memget(sizeof (struct log_channel_list));
4970Sstevel@tonic-gate if (lcl == NULL) {
4980Sstevel@tonic-gate errno = ENOMEM;
4990Sstevel@tonic-gate return(-1);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate lcl->channel = chan;
5020Sstevel@tonic-gate lcl->next = lc->categories[category];
5030Sstevel@tonic-gate lc->categories[category] = lcl;
5040Sstevel@tonic-gate chan->references++;
5050Sstevel@tonic-gate return (0);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate int
log_remove_channel(log_context lc,int category,log_channel chan)5090Sstevel@tonic-gate log_remove_channel(log_context lc, int category, log_channel chan) {
5100Sstevel@tonic-gate log_channel_list lcl, prev_lcl, next_lcl;
5110Sstevel@tonic-gate int found = 0;
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate if (lc == NULL || category < 0 || category >= lc->num_categories) {
5140Sstevel@tonic-gate errno = EINVAL;
5150Sstevel@tonic-gate return (-1);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate for (prev_lcl = NULL, lcl = lc->categories[category];
5190Sstevel@tonic-gate lcl != NULL;
5200Sstevel@tonic-gate lcl = next_lcl) {
5210Sstevel@tonic-gate next_lcl = lcl->next;
5220Sstevel@tonic-gate if (lcl->channel == chan) {
5230Sstevel@tonic-gate log_free_channel(chan);
5240Sstevel@tonic-gate if (prev_lcl != NULL)
5250Sstevel@tonic-gate prev_lcl->next = next_lcl;
5260Sstevel@tonic-gate else
5270Sstevel@tonic-gate lc->categories[category] = next_lcl;
5280Sstevel@tonic-gate memput(lcl, sizeof (struct log_channel_list));
5290Sstevel@tonic-gate /*
5300Sstevel@tonic-gate * We just set found instead of returning because
5310Sstevel@tonic-gate * the channel might be on the list more than once.
5320Sstevel@tonic-gate */
5330Sstevel@tonic-gate found = 1;
5340Sstevel@tonic-gate } else
5350Sstevel@tonic-gate prev_lcl = lcl;
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate if (!found) {
5380Sstevel@tonic-gate errno = ENOENT;
5390Sstevel@tonic-gate return (-1);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate return (0);
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate int
log_option(log_context lc,int option,int value)5450Sstevel@tonic-gate log_option(log_context lc, int option, int value) {
5460Sstevel@tonic-gate if (lc == NULL) {
5470Sstevel@tonic-gate errno = EINVAL;
5480Sstevel@tonic-gate return (-1);
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate switch (option) {
5510Sstevel@tonic-gate case LOG_OPTION_DEBUG:
5520Sstevel@tonic-gate if (value)
5530Sstevel@tonic-gate lc->flags |= option;
5540Sstevel@tonic-gate else
5550Sstevel@tonic-gate lc->flags &= ~option;
5560Sstevel@tonic-gate break;
5570Sstevel@tonic-gate case LOG_OPTION_LEVEL:
5580Sstevel@tonic-gate lc->level = value;
5590Sstevel@tonic-gate break;
5600Sstevel@tonic-gate default:
5610Sstevel@tonic-gate errno = EINVAL;
5620Sstevel@tonic-gate return (-1);
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate return (0);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate int
log_category_is_active(log_context lc,int category)5680Sstevel@tonic-gate log_category_is_active(log_context lc, int category) {
5690Sstevel@tonic-gate if (lc == NULL) {
5700Sstevel@tonic-gate errno = EINVAL;
5710Sstevel@tonic-gate return (-1);
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate if (category >= 0 && category < lc->num_categories &&
5740Sstevel@tonic-gate lc->categories[category] != NULL)
5750Sstevel@tonic-gate return (1);
5760Sstevel@tonic-gate return (0);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate
5790Sstevel@tonic-gate log_channel
log_new_syslog_channel(unsigned int flags,int level,int facility)5800Sstevel@tonic-gate log_new_syslog_channel(unsigned int flags, int level, int facility) {
5810Sstevel@tonic-gate log_channel chan;
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate chan = memget(sizeof (struct log_channel));
5840Sstevel@tonic-gate if (chan == NULL) {
5850Sstevel@tonic-gate errno = ENOMEM;
5860Sstevel@tonic-gate return (NULL);
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate chan->type = log_syslog;
5890Sstevel@tonic-gate chan->flags = flags;
5900Sstevel@tonic-gate chan->level = level;
5910Sstevel@tonic-gate chan->out.facility = facility;
5920Sstevel@tonic-gate chan->references = 0;
5930Sstevel@tonic-gate return (chan);
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate log_channel
log_new_file_channel(unsigned int flags,int level,const char * name,FILE * stream,unsigned int versions,unsigned long max_size)5970Sstevel@tonic-gate log_new_file_channel(unsigned int flags, int level,
5980Sstevel@tonic-gate const char *name, FILE *stream, unsigned int versions,
5990Sstevel@tonic-gate unsigned long max_size) {
6000Sstevel@tonic-gate log_channel chan;
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate chan = memget(sizeof (struct log_channel));
6030Sstevel@tonic-gate if (chan == NULL) {
6040Sstevel@tonic-gate errno = ENOMEM;
6050Sstevel@tonic-gate return (NULL);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate chan->type = log_file;
6080Sstevel@tonic-gate chan->flags = flags;
6090Sstevel@tonic-gate chan->level = level;
6100Sstevel@tonic-gate if (name != NULL) {
6110Sstevel@tonic-gate size_t len;
612*11038SRao.Shoaib@Sun.COM
6130Sstevel@tonic-gate len = strlen(name);
614*11038SRao.Shoaib@Sun.COM /*
6150Sstevel@tonic-gate * Quantize length to a multiple of 256. There's space for the
6160Sstevel@tonic-gate * NUL, since if len is a multiple of 256, the size chosen will
6170Sstevel@tonic-gate * be the next multiple.
6180Sstevel@tonic-gate */
6190Sstevel@tonic-gate chan->out.file.name_size = ((len / 256) + 1) * 256;
6200Sstevel@tonic-gate chan->out.file.name = memget(chan->out.file.name_size);
6210Sstevel@tonic-gate if (chan->out.file.name == NULL) {
6220Sstevel@tonic-gate memput(chan, sizeof (struct log_channel));
6230Sstevel@tonic-gate errno = ENOMEM;
6240Sstevel@tonic-gate return (NULL);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate /* This is safe. */
6270Sstevel@tonic-gate strcpy(chan->out.file.name, name);
6280Sstevel@tonic-gate } else {
6290Sstevel@tonic-gate chan->out.file.name_size = 0;
6300Sstevel@tonic-gate chan->out.file.name = NULL;
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate chan->out.file.stream = stream;
6330Sstevel@tonic-gate chan->out.file.versions = versions;
6340Sstevel@tonic-gate chan->out.file.max_size = max_size;
6350Sstevel@tonic-gate chan->out.file.owner = getuid();
6360Sstevel@tonic-gate chan->out.file.group = getgid();
6370Sstevel@tonic-gate chan->references = 0;
6380Sstevel@tonic-gate return (chan);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate int
log_set_file_owner(log_channel chan,uid_t owner,gid_t group)6420Sstevel@tonic-gate log_set_file_owner(log_channel chan, uid_t owner, gid_t group) {
6430Sstevel@tonic-gate if (chan->type != log_file) {
6440Sstevel@tonic-gate errno = EBADF;
6450Sstevel@tonic-gate return (-1);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate chan->out.file.owner = owner;
6480Sstevel@tonic-gate chan->out.file.group = group;
6490Sstevel@tonic-gate return (0);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate log_channel
log_new_null_channel()6530Sstevel@tonic-gate log_new_null_channel() {
6540Sstevel@tonic-gate log_channel chan;
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate chan = memget(sizeof (struct log_channel));
6570Sstevel@tonic-gate if (chan == NULL) {
6580Sstevel@tonic-gate errno = ENOMEM;
6590Sstevel@tonic-gate return (NULL);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate chan->type = log_null;
6620Sstevel@tonic-gate chan->flags = LOG_CHANNEL_OFF;
6630Sstevel@tonic-gate chan->level = log_info;
6640Sstevel@tonic-gate chan->references = 0;
6650Sstevel@tonic-gate return (chan);
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate int
log_inc_references(log_channel chan)6690Sstevel@tonic-gate log_inc_references(log_channel chan) {
6700Sstevel@tonic-gate if (chan == NULL) {
6710Sstevel@tonic-gate errno = EINVAL;
6720Sstevel@tonic-gate return (-1);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate chan->references++;
6750Sstevel@tonic-gate return (0);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate int
log_dec_references(log_channel chan)6790Sstevel@tonic-gate log_dec_references(log_channel chan) {
6800Sstevel@tonic-gate if (chan == NULL || chan->references <= 0) {
6810Sstevel@tonic-gate errno = EINVAL;
6820Sstevel@tonic-gate return (-1);
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate chan->references--;
6850Sstevel@tonic-gate return (0);
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate log_channel_type
log_get_channel_type(log_channel chan)6890Sstevel@tonic-gate log_get_channel_type(log_channel chan) {
6900Sstevel@tonic-gate REQUIRE(chan != NULL);
691*11038SRao.Shoaib@Sun.COM
6920Sstevel@tonic-gate return (chan->type);
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate int
log_free_channel(log_channel chan)6960Sstevel@tonic-gate log_free_channel(log_channel chan) {
6970Sstevel@tonic-gate if (chan == NULL || chan->references <= 0) {
6980Sstevel@tonic-gate errno = EINVAL;
6990Sstevel@tonic-gate return (-1);
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate chan->references--;
7020Sstevel@tonic-gate if (chan->references == 0) {
7030Sstevel@tonic-gate if (chan->type == log_file) {
7040Sstevel@tonic-gate if ((chan->flags & LOG_CLOSE_STREAM) &&
7050Sstevel@tonic-gate chan->out.file.stream != NULL)
7060Sstevel@tonic-gate (void)fclose(chan->out.file.stream);
7070Sstevel@tonic-gate if (chan->out.file.name != NULL)
7080Sstevel@tonic-gate memput(chan->out.file.name,
7090Sstevel@tonic-gate chan->out.file.name_size);
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate memput(chan, sizeof (struct log_channel));
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate return (0);
7140Sstevel@tonic-gate }
715*11038SRao.Shoaib@Sun.COM
716*11038SRao.Shoaib@Sun.COM /*! \file */
717