Home | History | Annotate | Line # | Download | only in gen
syslog.c revision 1.32
      1  1.32  christos /*	$NetBSD: syslog.c,v 1.32 2006/10/25 23:49:31 christos Exp $	*/
      2   1.6       cgd 
      3   1.1       cgd /*
      4   1.6       cgd  * Copyright (c) 1983, 1988, 1993
      5   1.6       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.30       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.11  christos #include <sys/cdefs.h>
     33   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     34   1.6       cgd #if 0
     35  1.13     perry static char sccsid[] = "@(#)syslog.c	8.5 (Berkeley) 4/29/95";
     36   1.6       cgd #else
     37  1.32  christos __RCSID("$NetBSD: syslog.c,v 1.32 2006/10/25 23:49:31 christos Exp $");
     38   1.6       cgd #endif
     39   1.1       cgd #endif /* LIBC_SCCS and not lint */
     40   1.1       cgd 
     41  1.12       jtc #include "namespace.h"
     42   1.1       cgd #include <sys/types.h>
     43   1.1       cgd #include <sys/socket.h>
     44   1.1       cgd #include <sys/syslog.h>
     45   1.1       cgd #include <sys/uio.h>
     46  1.21        is #include <sys/un.h>
     47   1.1       cgd #include <netdb.h>
     48   1.2     glass 
     49   1.2     glass #include <errno.h>
     50   1.2     glass #include <fcntl.h>
     51   1.2     glass #include <paths.h>
     52  1.28       wiz #include <stdarg.h>
     53   1.2     glass #include <stdio.h>
     54  1.26       cgd #include <stdlib.h>
     55   1.1       cgd #include <string.h>
     56   1.2     glass #include <time.h>
     57   1.2     glass #include <unistd.h>
     58  1.20    kleink #include "reentrant.h"
     59   1.2     glass 
     60  1.12       jtc #ifdef __weak_alias
     61  1.24   mycroft __weak_alias(closelog,_closelog)
     62  1.24   mycroft __weak_alias(openlog,_openlog)
     63  1.24   mycroft __weak_alias(setlogmask,_setlogmask)
     64  1.24   mycroft __weak_alias(syslog,_syslog)
     65  1.24   mycroft __weak_alias(vsyslog,_vsyslog)
     66  1.32  christos 
     67  1.32  christos __weak_alias(closelog_r,_closelog_r)
     68  1.32  christos __weak_alias(openlog_r,_openlog_r)
     69  1.32  christos __weak_alias(setlogmask_r,_setlogmask_r)
     70  1.32  christos __weak_alias(syslog_r,_syslog_r)
     71  1.32  christos __weak_alias(vsyslog_r,_vsyslog_r)
     72   1.1       cgd #endif
     73   1.1       cgd 
     74  1.32  christos static struct syslog_data sdata = SYSLOG_DATA_INIT;
     75   1.1       cgd 
     76  1.32  christos static void	openlog_unlocked_r(const char *, int, int,
     77  1.32  christos     struct syslog_data *);
     78  1.32  christos static void	disconnectlog_r(struct syslog_data *);
     79  1.32  christos static void	connectlog_r(struct syslog_data *);
     80  1.32  christos 
     81  1.20    kleink 
     82  1.29   thorpej #ifdef _REENTRANT
     83  1.20    kleink static mutex_t	syslog_mutex = MUTEX_INITIALIZER;
     84  1.20    kleink #endif
     85  1.20    kleink 
     86   1.1       cgd /*
     87   1.1       cgd  * syslog, vsyslog --
     88   1.1       cgd  *	print message on log file; output is intended for syslogd(8).
     89   1.1       cgd  */
     90   1.1       cgd void
     91   1.1       cgd syslog(int pri, const char *fmt, ...)
     92   1.1       cgd {
     93   1.1       cgd 	va_list ap;
     94   1.1       cgd 
     95   1.1       cgd 	va_start(ap, fmt);
     96   1.1       cgd 	vsyslog(pri, fmt, ap);
     97   1.1       cgd 	va_end(ap);
     98   1.1       cgd }
     99   1.1       cgd 
    100   1.1       cgd void
    101  1.32  christos vsyslog(int pri, const char *fmt, va_list ap)
    102  1.32  christos {
    103  1.32  christos 	vsyslog_r(pri, &sdata, fmt, ap);
    104  1.32  christos }
    105  1.32  christos 
    106  1.32  christos void
    107  1.32  christos openlog(const char *ident, int logstat, int logfac)
    108   1.1       cgd {
    109  1.32  christos 	openlog_r(ident, logstat, logfac, &sdata);
    110  1.32  christos }
    111  1.32  christos 
    112  1.32  christos void
    113  1.32  christos closelog(void)
    114  1.32  christos {
    115  1.32  christos 	closelog_r(&sdata);
    116  1.32  christos }
    117  1.32  christos 
    118  1.32  christos /* setlogmask -- set the log mask level */
    119  1.32  christos int
    120  1.32  christos setlogmask(int pmask)
    121  1.32  christos {
    122  1.32  christos 	return setlogmask_r(pmask, &sdata);
    123  1.32  christos }
    124  1.32  christos 
    125  1.32  christos /* Reentrant version of syslog, i.e. syslog_r() */
    126  1.32  christos 
    127  1.32  christos void
    128  1.32  christos syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
    129  1.32  christos {
    130  1.32  christos 	va_list ap;
    131  1.32  christos 
    132  1.32  christos 	va_start(ap, fmt);
    133  1.32  christos 	vsyslog_r(pri, data, fmt, ap);
    134  1.32  christos 	va_end(ap);
    135  1.32  christos }
    136  1.32  christos 
    137  1.32  christos void
    138  1.32  christos vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
    139  1.32  christos {
    140  1.32  christos 	size_t cnt, prlen;
    141  1.14     perry 	char ch, *p, *t;
    142   1.2     glass 	time_t now;
    143  1.18    kleink 	struct tm tmnow;
    144  1.32  christos 	int fd, saved_errno;
    145  1.10   mycroft #define	TBUF_LEN	2048
    146  1.10   mycroft #define	FMT_LEN		1024
    147  1.11  christos 	char *stdp = NULL;	/* pacify gcc */
    148  1.11  christos 	char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
    149  1.32  christos 	size_t tbuf_left, fmt_left;
    150   1.2     glass 
    151   1.2     glass #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
    152   1.2     glass 	/* Check for invalid bits. */
    153   1.2     glass 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
    154  1.32  christos 		if (data == &sdata) {
    155  1.32  christos 			syslog(INTERNALLOG,
    156  1.32  christos 			    "syslog: unknown facility/priority: %x", pri);
    157  1.32  christos 		} else {
    158  1.32  christos 			syslog_r(INTERNALLOG, data,
    159  1.32  christos 			    "syslog_r: unknown facility/priority: %x", pri);
    160  1.32  christos 		}
    161   1.2     glass 		pri &= LOG_PRIMASK|LOG_FACMASK;
    162   1.2     glass 	}
    163   1.1       cgd 
    164   1.2     glass 	/* Check priority against setlogmask values. */
    165  1.32  christos 	if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
    166   1.1       cgd 		return;
    167   1.1       cgd 
    168   1.1       cgd 	saved_errno = errno;
    169   1.1       cgd 
    170   1.6       cgd 	/* Set default facility if none specified. */
    171   1.1       cgd 	if ((pri & LOG_FACMASK) == 0)
    172  1.32  christos 		pri |= data->log_fac;
    173   1.1       cgd 
    174   1.6       cgd 	/* Build the message. */
    175   1.9       jtc 
    176  1.10   mycroft 	/*
    177   1.9       jtc  	 * Although it's tempting, we can't ignore the possibility of
    178   1.9       jtc 	 * overflowing the buffer when assembling the "fixed" portion
    179   1.9       jtc 	 * of the message.  Strftime's "%h" directive expands to the
    180   1.9       jtc 	 * locale's abbreviated month name, but if the user has the
    181   1.9       jtc 	 * ability to construct to his own locale files, it may be
    182   1.9       jtc 	 * arbitrarily long.
    183   1.9       jtc 	 */
    184  1.32  christos 	 if (data == &sdata)
    185  1.32  christos 		(void)time(&now);
    186   1.9       jtc 
    187   1.9       jtc 	p = tbuf;
    188  1.10   mycroft 	tbuf_left = TBUF_LEN;
    189   1.9       jtc 
    190  1.19  christos #define	DEC()							\
    191  1.19  christos 	do {							\
    192  1.19  christos 		if (prlen >= tbuf_left)				\
    193  1.19  christos 			prlen = tbuf_left - 1;			\
    194  1.19  christos 		p += prlen;					\
    195  1.19  christos 		tbuf_left -= prlen;				\
    196  1.32  christos 	} while (/*CONSTCOND*/0)
    197  1.10   mycroft 
    198  1.10   mycroft 	prlen = snprintf(p, tbuf_left, "<%d>", pri);
    199   1.9       jtc 	DEC();
    200   1.9       jtc 
    201  1.32  christos 	if (data == &sdata) {
    202  1.32  christos 		/* strftime() implies tzset(), localtime_r() doesn't. */
    203  1.32  christos 		tzset();
    204  1.32  christos 		prlen = strftime(p, tbuf_left, "%h %e %T ",
    205  1.32  christos 		    localtime_r(&now, &tmnow));
    206  1.32  christos 		DEC();
    207  1.32  christos 	}
    208   1.9       jtc 
    209  1.32  christos 	if (data->log_stat & LOG_PERROR)
    210   1.1       cgd 		stdp = p;
    211  1.32  christos 	if (data->log_tag == NULL)
    212  1.32  christos 		data->log_tag = getprogname();
    213  1.32  christos 	if (data->log_tag != NULL) {
    214  1.32  christos 		prlen = snprintf(p, tbuf_left, "%s", data->log_tag);
    215   1.9       jtc 		DEC();
    216   1.9       jtc 	}
    217  1.32  christos 	if (data->log_stat & LOG_PID) {
    218  1.10   mycroft 		prlen = snprintf(p, tbuf_left, "[%d]", getpid());
    219   1.9       jtc 		DEC();
    220   1.9       jtc 	}
    221  1.32  christos 	if (data->log_tag != NULL) {
    222  1.10   mycroft 		if (tbuf_left > 1) {
    223   1.9       jtc 			*p++ = ':';
    224  1.10   mycroft 			tbuf_left--;
    225   1.9       jtc 		}
    226  1.10   mycroft 		if (tbuf_left > 1) {
    227   1.9       jtc 			*p++ = ' ';
    228  1.10   mycroft 			tbuf_left--;
    229   1.9       jtc 		}
    230   1.1       cgd 	}
    231   1.1       cgd 
    232   1.9       jtc 	/*
    233   1.9       jtc 	 * We wouldn't need this mess if printf handled %m, or if
    234   1.9       jtc 	 * strerror() had been invented before syslog().
    235   1.9       jtc 	 */
    236  1.11  christos 	for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) {
    237   1.6       cgd 		if (ch == '%' && fmt[1] == 'm') {
    238  1.32  christos 			char ebuf[128];
    239   1.6       cgd 			++fmt;
    240  1.32  christos 			if (strerror_r(saved_errno, ebuf, sizeof(ebuf)))
    241  1.32  christos 				prlen = snprintf(t, fmt_left, "Error %d",
    242  1.32  christos 				    saved_errno);
    243  1.32  christos 			else
    244  1.32  christos 				prlen = snprintf(t, fmt_left, "%s", ebuf);
    245  1.10   mycroft 			if (prlen >= fmt_left)
    246  1.10   mycroft 				prlen = fmt_left - 1;
    247  1.10   mycroft 			t += prlen;
    248  1.10   mycroft 			fmt_left -= prlen;
    249  1.32  christos 		} else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
    250  1.32  christos 			*t++ = '%';
    251  1.32  christos 			*t++ = '%';
    252  1.32  christos 			fmt++;
    253  1.32  christos 			fmt_left -= 2;
    254   1.9       jtc 		} else {
    255  1.10   mycroft 			if (fmt_left > 1) {
    256   1.9       jtc 				*t++ = ch;
    257  1.10   mycroft 				fmt_left--;
    258   1.9       jtc 			}
    259   1.9       jtc 		}
    260   1.9       jtc 	}
    261   1.6       cgd 	*t = '\0';
    262   1.1       cgd 
    263  1.10   mycroft 	prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
    264   1.9       jtc 	DEC();
    265   1.6       cgd 	cnt = p - tbuf;
    266   1.1       cgd 
    267   1.6       cgd 	/* Output to stderr if requested. */
    268  1.32  christos 	if (data->log_stat & LOG_PERROR) {
    269   1.1       cgd 		struct iovec iov[2];
    270   1.1       cgd 
    271  1.10   mycroft 		iov[0].iov_base = stdp;
    272  1.10   mycroft 		iov[0].iov_len = cnt - (stdp - tbuf);
    273  1.31  christos 		iov[1].iov_base = __UNCONST("\n");
    274  1.10   mycroft 		iov[1].iov_len = 1;
    275   1.1       cgd 		(void)writev(STDERR_FILENO, iov, 2);
    276   1.1       cgd 	}
    277   1.1       cgd 
    278   1.6       cgd 	/* Get connected, output the message to the local logger. */
    279  1.32  christos 	if (data == &sdata)
    280  1.32  christos 		mutex_lock(&syslog_mutex);
    281  1.32  christos 	if (!data->opened)
    282  1.32  christos 		openlog_unlocked_r(data->log_tag, data->log_stat, 0, data);
    283  1.32  christos 	connectlog_r(data);
    284  1.32  christos 
    285  1.27    atatat 	/*
    286  1.32  christos 	 * If the send() failed, there are two likely scenarios:
    287  1.32  christos 	 *  1) syslogd was restarted
    288  1.32  christos 	 *  2) /dev/log is out of socket buffer space
    289  1.32  christos 	 * We attempt to reconnect to /dev/log to take care of
    290  1.32  christos 	 * case #1 and keep send()ing data to cover case #2
    291  1.32  christos 	 * to give syslogd a chance to empty its socket buffer.
    292  1.27    atatat 	 */
    293  1.32  christos 	if (send(data->log_file, tbuf, cnt, 0) == -1) {
    294  1.32  christos 		if (errno != ENOBUFS) {
    295  1.32  christos 			disconnectlog_r(data);
    296  1.32  christos 			connectlog_r(data);
    297  1.32  christos 		}
    298  1.32  christos 		do {
    299  1.32  christos 			usleep(1);
    300  1.32  christos 			if (send(data->log_file, tbuf, cnt, 0) != -1)
    301  1.32  christos 				break;
    302  1.32  christos 		} while (errno == ENOBUFS);
    303  1.27    atatat 	}
    304  1.32  christos 	if (data == &sdata)
    305  1.32  christos 		mutex_unlock(&syslog_mutex);
    306   1.1       cgd 
    307   1.1       cgd 	/*
    308  1.32  christos 	 * Output the message to the console; try not to block
    309  1.32  christos 	 * as a blocking console should not stop other processes.
    310  1.32  christos 	 * Make sure the error reported is the one from the syslogd failure.
    311   1.1       cgd 	 */
    312  1.32  christos 	if ((data->log_stat & LOG_CONS) &&
    313  1.32  christos 	    (fd = open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) {
    314   1.9       jtc 		struct iovec iov[2];
    315   1.9       jtc 
    316   1.5       jtc 		p = strchr(tbuf, '>') + 1;
    317  1.10   mycroft 		iov[0].iov_base = p;
    318  1.10   mycroft 		iov[0].iov_len = cnt - (p - tbuf);
    319  1.31  christos 		iov[1].iov_base = __UNCONST("\r\n");
    320  1.10   mycroft 		iov[1].iov_len = 2;
    321   1.9       jtc 		(void)writev(fd, iov, 2);
    322   1.1       cgd 		(void)close(fd);
    323   1.1       cgd 	}
    324  1.32  christos 	if (data != &sdata)
    325  1.32  christos 		closelog_r(data);
    326   1.1       cgd }
    327   1.1       cgd 
    328  1.32  christos static void
    329  1.32  christos disconnectlog_r(struct syslog_data *data)
    330  1.32  christos {
    331  1.32  christos 	/*
    332  1.32  christos 	 * If the user closed the FD and opened another in the same slot,
    333  1.32  christos 	 * that's their problem.  They should close it before calling on
    334  1.32  christos 	 * system services.
    335  1.32  christos 	 */
    336  1.32  christos 	if (data->log_file != -1) {
    337  1.32  christos 		(void)close(data->log_file);
    338  1.32  christos 		data->log_file = -1;
    339  1.32  christos 	}
    340  1.32  christos 	data->connected = 0;		/* retry connect */
    341  1.32  christos }
    342   1.1       cgd 
    343  1.20    kleink static void
    344  1.32  christos connectlog_r(struct syslog_data *data)
    345   1.1       cgd {
    346  1.32  christos 	/* AF_UNIX address of local logger */
    347  1.32  christos 	static const struct sockaddr_un sun = {
    348  1.32  christos 		.sun_family = AF_LOCAL,
    349  1.32  christos 		.sun_len = sizeof(sun),
    350  1.32  christos 		.sun_path = _PATH_LOG,
    351  1.32  christos 	};
    352  1.20    kleink 
    353  1.32  christos 	if (data->log_file == -1) {
    354  1.32  christos 		if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
    355  1.32  christos 			return;
    356  1.32  christos 		(void)fcntl(data->log_file, F_SETFD, 1);
    357   1.1       cgd 	}
    358  1.32  christos 	if (data->log_file != -1 && !data->connected) {
    359  1.32  christos 		if (connect(data->log_file,
    360  1.32  christos 		    (const struct sockaddr *)(const void *)&sun,
    361  1.32  christos 		    sizeof(sun)) == -1) {
    362  1.32  christos 			(void)close(data->log_file);
    363  1.32  christos 			data->log_file = -1;
    364  1.32  christos  		} else
    365  1.32  christos 			data->connected = 1;
    366  1.17   thorpej 	}
    367   1.1       cgd }
    368   1.1       cgd 
    369  1.32  christos static void
    370  1.32  christos openlog_unlocked_r(const char *ident, int logstat, int logfac,
    371  1.32  christos     struct syslog_data *data)
    372  1.20    kleink {
    373  1.32  christos 	if (ident != NULL)
    374  1.32  christos 		data->log_tag = ident;
    375  1.32  christos 	data->log_stat = logstat;
    376  1.32  christos 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
    377  1.32  christos 		data->log_fac = logfac;
    378  1.20    kleink 
    379  1.32  christos 	if (data->log_stat & LOG_NDELAY)	/* open immediately */
    380  1.32  christos 		connectlog_r(data);
    381  1.20    kleink }
    382  1.20    kleink 
    383  1.32  christos void
    384  1.32  christos openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data)
    385   1.1       cgd {
    386  1.32  christos 	if (data == &sdata)
    387  1.32  christos 		mutex_lock(&syslog_mutex);
    388  1.32  christos 	openlog_unlocked_r(ident, logstat, logfac, data);
    389  1.32  christos 	if (data == &sdata)
    390  1.32  christos 		mutex_unlock(&syslog_mutex);
    391  1.20    kleink }
    392  1.20    kleink 
    393  1.20    kleink void
    394  1.32  christos closelog_r(struct syslog_data *data)
    395  1.20    kleink {
    396  1.32  christos 	if (data == &sdata)
    397  1.32  christos 		mutex_lock(&syslog_mutex);
    398  1.32  christos 	(void)close(data->log_file);
    399  1.32  christos 	data->log_file = -1;
    400  1.32  christos 	data->connected = 0;
    401  1.32  christos 	data->log_tag = NULL;
    402  1.32  christos 	if (data == &sdata)
    403  1.32  christos 		mutex_unlock(&syslog_mutex);
    404   1.1       cgd }
    405   1.1       cgd 
    406   1.2     glass int
    407  1.32  christos setlogmask_r(int pmask, struct syslog_data *data)
    408   1.1       cgd {
    409   1.1       cgd 	int omask;
    410   1.1       cgd 
    411  1.32  christos 	omask = data->log_mask;
    412   1.1       cgd 	if (pmask != 0)
    413  1.32  christos 		data->log_mask = pmask;
    414  1.32  christos 	return omask;
    415   1.1       cgd }
    416