Home | History | Annotate | Line # | Download | only in gen
syslog.c revision 1.51
      1  1.51  christos /*	$NetBSD: syslog.c,v 1.51 2012/10/10 22:50:51 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.51  christos __RCSID("$NetBSD: syslog.c,v 1.51 2012/10/10 22:50:51 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.43  christos #include <sys/param.h>
     44   1.1       cgd #include <sys/socket.h>
     45   1.1       cgd #include <sys/syslog.h>
     46   1.1       cgd #include <sys/uio.h>
     47  1.21        is #include <sys/un.h>
     48   1.1       cgd #include <netdb.h>
     49   1.2     glass 
     50   1.2     glass #include <errno.h>
     51   1.2     glass #include <fcntl.h>
     52   1.2     glass #include <paths.h>
     53  1.28       wiz #include <stdarg.h>
     54   1.2     glass #include <stdio.h>
     55  1.26       cgd #include <stdlib.h>
     56   1.1       cgd #include <string.h>
     57   1.2     glass #include <time.h>
     58   1.2     glass #include <unistd.h>
     59  1.20    kleink #include "reentrant.h"
     60  1.39  christos #include "extern.h"
     61   1.2     glass 
     62  1.12       jtc #ifdef __weak_alias
     63  1.24   mycroft __weak_alias(closelog,_closelog)
     64  1.24   mycroft __weak_alias(openlog,_openlog)
     65  1.24   mycroft __weak_alias(setlogmask,_setlogmask)
     66  1.24   mycroft __weak_alias(syslog,_syslog)
     67  1.24   mycroft __weak_alias(vsyslog,_vsyslog)
     68  1.43  christos __weak_alias(syslogp,_syslogp)
     69  1.43  christos __weak_alias(vsyslogp,_vsyslogp)
     70  1.32  christos 
     71  1.38  christos __weak_alias(syslog_ss,_syslog_ss)
     72  1.38  christos __weak_alias(vsyslog_ss,_vsyslog_ss)
     73  1.43  christos __weak_alias(syslogp_ss,_syslogp_ss)
     74  1.43  christos __weak_alias(vsyslogp_ss,_vsyslogp_ss)
     75   1.1       cgd #endif
     76   1.1       cgd 
     77  1.32  christos static struct syslog_data sdata = SYSLOG_DATA_INIT;
     78   1.1       cgd 
     79  1.32  christos static void	openlog_unlocked_r(const char *, int, int,
     80  1.32  christos     struct syslog_data *);
     81  1.32  christos static void	disconnectlog_r(struct syslog_data *);
     82  1.32  christos static void	connectlog_r(struct syslog_data *);
     83  1.34  christos 
     84  1.34  christos #define LOG_SIGNAL_SAFE	(int)0x80000000
     85  1.43  christos 
     86  1.20    kleink 
     87  1.29   thorpej #ifdef _REENTRANT
     88  1.20    kleink static mutex_t	syslog_mutex = MUTEX_INITIALIZER;
     89  1.20    kleink #endif
     90  1.20    kleink 
     91   1.1       cgd /*
     92   1.1       cgd  * syslog, vsyslog --
     93   1.1       cgd  *	print message on log file; output is intended for syslogd(8).
     94   1.1       cgd  */
     95   1.1       cgd void
     96   1.1       cgd syslog(int pri, const char *fmt, ...)
     97   1.1       cgd {
     98   1.1       cgd 	va_list ap;
     99   1.1       cgd 
    100   1.1       cgd 	va_start(ap, fmt);
    101   1.1       cgd 	vsyslog(pri, fmt, ap);
    102   1.1       cgd 	va_end(ap);
    103   1.1       cgd }
    104   1.1       cgd 
    105   1.1       cgd void
    106  1.32  christos vsyslog(int pri, const char *fmt, va_list ap)
    107  1.32  christos {
    108  1.32  christos 	vsyslog_r(pri, &sdata, fmt, ap);
    109  1.32  christos }
    110  1.32  christos 
    111  1.43  christos /*
    112  1.43  christos  * syslogp, vsyslogp --
    113  1.43  christos  *	like syslog but take additional arguments for MSGID and SD
    114  1.43  christos  */
    115  1.43  christos void
    116  1.43  christos syslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, ...)
    117  1.43  christos {
    118  1.43  christos 	va_list ap;
    119  1.43  christos 
    120  1.43  christos 	va_start(ap, msgfmt);
    121  1.43  christos 	vsyslogp(pri, msgid, sdfmt, msgfmt, ap);
    122  1.43  christos 	va_end(ap);
    123  1.43  christos }
    124  1.43  christos 
    125  1.43  christos void
    126  1.43  christos vsyslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, va_list ap)
    127  1.43  christos {
    128  1.43  christos 	vsyslogp_r(pri, &sdata, msgid, sdfmt, msgfmt, ap);
    129  1.43  christos }
    130  1.43  christos 
    131  1.32  christos void
    132  1.32  christos openlog(const char *ident, int logstat, int logfac)
    133   1.1       cgd {
    134  1.32  christos 	openlog_r(ident, logstat, logfac, &sdata);
    135  1.32  christos }
    136  1.32  christos 
    137  1.32  christos void
    138  1.32  christos closelog(void)
    139  1.32  christos {
    140  1.32  christos 	closelog_r(&sdata);
    141  1.32  christos }
    142  1.32  christos 
    143  1.32  christos /* setlogmask -- set the log mask level */
    144  1.32  christos int
    145  1.32  christos setlogmask(int pmask)
    146  1.32  christos {
    147  1.32  christos 	return setlogmask_r(pmask, &sdata);
    148  1.32  christos }
    149  1.32  christos 
    150  1.32  christos /* Reentrant version of syslog, i.e. syslog_r() */
    151  1.32  christos 
    152  1.32  christos void
    153  1.32  christos syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
    154  1.32  christos {
    155  1.32  christos 	va_list ap;
    156  1.32  christos 
    157  1.32  christos 	va_start(ap, fmt);
    158  1.32  christos 	vsyslog_r(pri, data, fmt, ap);
    159  1.32  christos 	va_end(ap);
    160  1.32  christos }
    161  1.32  christos 
    162  1.32  christos void
    163  1.43  christos syslogp_r(int pri, struct syslog_data *data, const char *msgid,
    164  1.43  christos 	const char *sdfmt, const char *msgfmt, ...)
    165  1.43  christos {
    166  1.43  christos 	va_list ap;
    167  1.43  christos 
    168  1.43  christos 	va_start(ap, msgfmt);
    169  1.43  christos 	vsyslogp_r(pri, data, msgid, sdfmt, msgfmt, ap);
    170  1.43  christos 	va_end(ap);
    171  1.43  christos }
    172  1.43  christos 
    173  1.43  christos void
    174  1.34  christos syslog_ss(int pri, struct syslog_data *data, const char *fmt, ...)
    175  1.34  christos {
    176  1.34  christos 	va_list ap;
    177  1.34  christos 
    178  1.34  christos 	va_start(ap, fmt);
    179  1.34  christos 	vsyslog_r(pri | LOG_SIGNAL_SAFE, data, fmt, ap);
    180  1.34  christos 	va_end(ap);
    181  1.34  christos }
    182  1.34  christos 
    183  1.34  christos void
    184  1.43  christos syslogp_ss(int pri, struct syslog_data *data, const char *msgid,
    185  1.43  christos 	const char *sdfmt, const char *msgfmt, ...)
    186  1.43  christos {
    187  1.43  christos 	va_list ap;
    188  1.43  christos 
    189  1.43  christos 	va_start(ap, msgfmt);
    190  1.43  christos 	vsyslogp_r(pri | LOG_SIGNAL_SAFE, data, msgid, sdfmt, msgfmt, ap);
    191  1.43  christos 	va_end(ap);
    192  1.43  christos }
    193  1.43  christos 
    194  1.43  christos void
    195  1.34  christos vsyslog_ss(int pri, struct syslog_data *data, const char *fmt, va_list ap)
    196  1.34  christos {
    197  1.34  christos 	vsyslog_r(pri | LOG_SIGNAL_SAFE, data, fmt, ap);
    198  1.34  christos }
    199  1.34  christos 
    200  1.34  christos void
    201  1.43  christos vsyslogp_ss(int pri, struct syslog_data *data, const char *msgid,
    202  1.43  christos 	const char *sdfmt, const char *msgfmt, va_list ap)
    203  1.43  christos {
    204  1.43  christos 	vsyslogp_r(pri | LOG_SIGNAL_SAFE, data, msgid, sdfmt, msgfmt, ap);
    205  1.43  christos }
    206  1.43  christos 
    207  1.43  christos 
    208  1.43  christos void
    209  1.32  christos vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
    210  1.32  christos {
    211  1.43  christos 	vsyslogp_r(pri, data, NULL, NULL, fmt, ap);
    212  1.43  christos }
    213  1.43  christos 
    214  1.43  christos void
    215  1.43  christos vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
    216  1.43  christos 	const char *sdfmt, const char *msgfmt, va_list ap)
    217  1.43  christos {
    218  1.48  christos 	static const char BRCOSP[] = "]: ";
    219  1.48  christos 	static const char CRLF[] = "\r\n";
    220  1.40  christos 	size_t cnt, prlen, tries;
    221  1.14     perry 	char ch, *p, *t;
    222  1.43  christos 	struct timeval tv;
    223  1.43  christos 	struct tm tmnow;
    224   1.2     glass 	time_t now;
    225  1.32  christos 	int fd, saved_errno;
    226  1.43  christos #define TBUF_LEN	2048
    227  1.43  christos #define FMT_LEN		1024
    228  1.40  christos #define MAXTRIES	10
    229  1.43  christos 	char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN], fmt_cat[FMT_LEN] = "";
    230  1.48  christos 	size_t tbuf_left, fmt_left, msgsdlen;
    231  1.43  christos 	char *fmt = fmt_cat;
    232  1.34  christos 	int signal_safe = pri & LOG_SIGNAL_SAFE;
    233  1.48  christos 	struct iovec iov[7];	/* prog + [ + pid + ]: + fmt + crlf */
    234  1.48  christos 	int opened, iovcnt;
    235  1.34  christos 
    236  1.35  christos 	pri &= ~LOG_SIGNAL_SAFE;
    237   1.2     glass 
    238  1.43  christos #define INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
    239   1.2     glass 	/* Check for invalid bits. */
    240   1.2     glass 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
    241  1.34  christos 		syslog_r(INTERNALLOG | signal_safe, data,
    242  1.34  christos 		    "syslog_r: unknown facility/priority: %x", pri);
    243   1.2     glass 		pri &= LOG_PRIMASK|LOG_FACMASK;
    244   1.2     glass 	}
    245   1.1       cgd 
    246   1.2     glass 	/* Check priority against setlogmask values. */
    247  1.32  christos 	if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
    248   1.1       cgd 		return;
    249   1.1       cgd 
    250   1.1       cgd 	saved_errno = errno;
    251   1.1       cgd 
    252   1.6       cgd 	/* Set default facility if none specified. */
    253   1.1       cgd 	if ((pri & LOG_FACMASK) == 0)
    254  1.32  christos 		pri |= data->log_fac;
    255   1.1       cgd 
    256   1.6       cgd 	/* Build the message. */
    257  1.43  christos 	p = tbuf;
    258  1.43  christos 	tbuf_left = TBUF_LEN;
    259   1.9       jtc 
    260  1.43  christos #define DEC()							\
    261  1.19  christos 	do {							\
    262  1.19  christos 		if (prlen >= tbuf_left)				\
    263  1.19  christos 			prlen = tbuf_left - 1;			\
    264  1.19  christos 		p += prlen;					\
    265  1.19  christos 		tbuf_left -= prlen;				\
    266  1.32  christos 	} while (/*CONSTCOND*/0)
    267  1.10   mycroft 
    268  1.43  christos 	prlen = snprintf_ss(p, tbuf_left, "<%d>1 ", pri);
    269   1.9       jtc 	DEC();
    270   1.9       jtc 
    271  1.43  christos 	if (!signal_safe && (gettimeofday(&tv, NULL) != -1)) {
    272  1.32  christos 		/* strftime() implies tzset(), localtime_r() doesn't. */
    273  1.32  christos 		tzset();
    274  1.43  christos 		now = (time_t) tv.tv_sec;
    275  1.43  christos 		localtime_r(&now, &tmnow);
    276  1.43  christos 
    277  1.43  christos 		prlen = strftime(p, tbuf_left, "%FT%T", &tmnow);
    278  1.43  christos 		DEC();
    279  1.47  christos 		prlen = snprintf(p, tbuf_left, ".%06ld", (long)tv.tv_usec);
    280  1.32  christos 		DEC();
    281  1.43  christos 		prlen = strftime(p, tbuf_left-1, "%z", &tmnow);
    282  1.43  christos 		/* strftime gives eg. "+0200", but we need "+02:00" */
    283  1.43  christos 		if (prlen == 5) {
    284  1.43  christos 			p[prlen+1] = p[prlen];
    285  1.43  christos 			p[prlen]   = p[prlen-1];
    286  1.43  christos 			p[prlen-1] = p[prlen-2];
    287  1.43  christos 			p[prlen-2] = ':';
    288  1.43  christos 			prlen += 1;
    289  1.43  christos 		}
    290  1.43  christos 	} else {
    291  1.43  christos 		prlen = snprintf_ss(p, tbuf_left, "-");
    292  1.43  christos 
    293  1.43  christos 		/* if gmtime_r() was signal-safe we could output the UTC-time:
    294  1.43  christos 		gmtime_r(&now, &tmnow);
    295  1.43  christos 		prlen = strftime(p, tbuf_left, "%FT%TZ", &tmnow);
    296  1.43  christos 		*/
    297  1.32  christos 	}
    298  1.51  christos 
    299  1.51  christos 	if (data->log_hostname[0] == '\0' && gethostname(data->log_hostname,
    300  1.51  christos 	    sizeof(data->log_hostname)) == -1) {
    301  1.51  christos 		/* can this really happen? */
    302  1.51  christos 		data->log_hostname[0] = '-';
    303  1.51  christos 		data->log_hostname[1] = '\0';
    304  1.51  christos 	}
    305  1.51  christos 
    306  1.43  christos 	DEC();
    307  1.51  christos 	prlen = snprintf_ss(p, tbuf_left, " %s ", data->log_hostname);
    308   1.9       jtc 
    309  1.32  christos 	if (data->log_tag == NULL)
    310  1.32  christos 		data->log_tag = getprogname();
    311  1.43  christos 
    312  1.51  christos 	DEC();
    313  1.43  christos 	prlen = snprintf_ss(p, tbuf_left, "%s ",
    314  1.43  christos 	    data->log_tag ? data->log_tag : "-");
    315  1.48  christos 	if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
    316  1.48  christos 		iovcnt = 0;
    317  1.48  christos 		iov[iovcnt].iov_base = p;
    318  1.48  christos 		iov[iovcnt].iov_len = prlen - 1;
    319  1.48  christos 		iovcnt++;
    320  1.48  christos 	}
    321  1.43  christos 	DEC();
    322  1.43  christos 
    323  1.48  christos 	if (data->log_stat & LOG_PID) {
    324  1.43  christos 		prlen = snprintf_ss(p, tbuf_left, "%d ", getpid());
    325  1.48  christos 		if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
    326  1.48  christos 			iov[iovcnt].iov_base = __UNCONST("[");
    327  1.48  christos 			iov[iovcnt].iov_len = 1;
    328  1.48  christos 			iovcnt++;
    329  1.48  christos 			iov[iovcnt].iov_base = p;
    330  1.48  christos 			iov[iovcnt].iov_len = prlen - 1;
    331  1.48  christos 			iovcnt++;
    332  1.48  christos 			iov[iovcnt].iov_base = __UNCONST(BRCOSP);
    333  1.48  christos 			iov[iovcnt].iov_len = 3;
    334  1.48  christos 			iovcnt++;
    335  1.48  christos 		}
    336  1.48  christos 	} else {
    337  1.43  christos 		prlen = snprintf_ss(p, tbuf_left, "- ");
    338  1.48  christos 		if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
    339  1.48  christos 			iov[iovcnt].iov_base = __UNCONST(BRCOSP + 1);
    340  1.48  christos 			iov[iovcnt].iov_len = 2;
    341  1.48  christos 			iovcnt++;
    342  1.48  christos 		}
    343  1.48  christos 	}
    344  1.43  christos 	DEC();
    345  1.43  christos 
    346  1.43  christos 	/*
    347  1.43  christos 	 * concat the format strings, then use one vsnprintf()
    348  1.43  christos 	 */
    349  1.43  christos 	if (msgid != NULL && *msgid != '\0') {
    350  1.43  christos 		strlcat(fmt_cat, msgid, FMT_LEN);
    351  1.43  christos 		strlcat(fmt_cat, " ", FMT_LEN);
    352  1.43  christos 	} else
    353  1.43  christos 		strlcat(fmt_cat, "- ", FMT_LEN);
    354  1.43  christos 
    355  1.43  christos 	if (sdfmt != NULL && *sdfmt != '\0') {
    356  1.43  christos 		strlcat(fmt_cat, sdfmt, FMT_LEN);
    357  1.43  christos 	} else
    358  1.43  christos 		strlcat(fmt_cat, "-", FMT_LEN);
    359  1.43  christos 
    360  1.48  christos 	if (data->log_stat & (LOG_PERROR|LOG_CONS))
    361  1.48  christos 		msgsdlen = strlen(fmt_cat) + 1;
    362  1.48  christos 	else
    363  1.48  christos 		msgsdlen = 0;	/* XXX: GCC */
    364  1.48  christos 
    365  1.43  christos 	if (msgfmt != NULL && *msgfmt != '\0') {
    366  1.43  christos 		strlcat(fmt_cat, " ", FMT_LEN);
    367  1.43  christos 		strlcat(fmt_cat, msgfmt, FMT_LEN);
    368   1.1       cgd 	}
    369   1.1       cgd 
    370  1.43  christos 	/*
    371  1.43  christos 	 * We wouldn't need this mess if printf handled %m, or if
    372   1.9       jtc 	 * strerror() had been invented before syslog().
    373   1.9       jtc 	 */
    374  1.11  christos 	for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) {
    375   1.6       cgd 		if (ch == '%' && fmt[1] == 'm') {
    376  1.32  christos 			char ebuf[128];
    377   1.6       cgd 			++fmt;
    378  1.34  christos 			if (signal_safe ||
    379  1.33  christos 			    strerror_r(saved_errno, ebuf, sizeof(ebuf)))
    380  1.43  christos 				prlen = snprintf_ss(t, fmt_left, "Error %d",
    381  1.32  christos 				    saved_errno);
    382  1.32  christos 			else
    383  1.34  christos 				prlen = snprintf_ss(t, fmt_left, "%s", ebuf);
    384  1.10   mycroft 			if (prlen >= fmt_left)
    385  1.10   mycroft 				prlen = fmt_left - 1;
    386  1.10   mycroft 			t += prlen;
    387  1.10   mycroft 			fmt_left -= prlen;
    388  1.32  christos 		} else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
    389  1.32  christos 			*t++ = '%';
    390  1.32  christos 			*t++ = '%';
    391  1.32  christos 			fmt++;
    392  1.32  christos 			fmt_left -= 2;
    393   1.9       jtc 		} else {
    394  1.10   mycroft 			if (fmt_left > 1) {
    395   1.9       jtc 				*t++ = ch;
    396  1.10   mycroft 				fmt_left--;
    397   1.9       jtc 			}
    398   1.9       jtc 		}
    399   1.9       jtc 	}
    400   1.6       cgd 	*t = '\0';
    401   1.1       cgd 
    402  1.34  christos 	if (signal_safe)
    403  1.34  christos 		prlen = vsnprintf_ss(p, tbuf_left, fmt_cpy, ap);
    404  1.34  christos 	else
    405  1.34  christos 		prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
    406  1.48  christos 
    407  1.48  christos 	if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
    408  1.48  christos 		iov[iovcnt].iov_base = p + msgsdlen;
    409  1.48  christos 		iov[iovcnt].iov_len = prlen - msgsdlen;
    410  1.48  christos 		iovcnt++;
    411  1.48  christos 	}
    412  1.48  christos 
    413   1.9       jtc 	DEC();
    414   1.6       cgd 	cnt = p - tbuf;
    415   1.1       cgd 
    416   1.6       cgd 	/* Output to stderr if requested. */
    417  1.32  christos 	if (data->log_stat & LOG_PERROR) {
    418  1.48  christos 		iov[iovcnt].iov_base = __UNCONST(CRLF + 1);
    419  1.48  christos 		iov[iovcnt].iov_len = 1;
    420  1.48  christos 		(void)writev(STDERR_FILENO, iov, iovcnt + 1);
    421   1.1       cgd 	}
    422   1.1       cgd 
    423   1.6       cgd 	/* Get connected, output the message to the local logger. */
    424  1.32  christos 	if (data == &sdata)
    425  1.32  christos 		mutex_lock(&syslog_mutex);
    426  1.51  christos 	opened = !data->log_opened;
    427  1.42    dogcow 	if (opened)
    428  1.32  christos 		openlog_unlocked_r(data->log_tag, data->log_stat, 0, data);
    429  1.32  christos 	connectlog_r(data);
    430  1.32  christos 
    431  1.27    atatat 	/*
    432  1.32  christos 	 * If the send() failed, there are two likely scenarios:
    433  1.32  christos 	 *  1) syslogd was restarted
    434  1.32  christos 	 *  2) /dev/log is out of socket buffer space
    435  1.32  christos 	 * We attempt to reconnect to /dev/log to take care of
    436  1.32  christos 	 * case #1 and keep send()ing data to cover case #2
    437  1.32  christos 	 * to give syslogd a chance to empty its socket buffer.
    438  1.27    atatat 	 */
    439  1.40  christos 	for (tries = 0; tries < MAXTRIES; tries++) {
    440  1.40  christos 		if (send(data->log_file, tbuf, cnt, 0) != -1)
    441  1.40  christos 			break;
    442  1.32  christos 		if (errno != ENOBUFS) {
    443  1.32  christos 			disconnectlog_r(data);
    444  1.32  christos 			connectlog_r(data);
    445  1.40  christos 		} else
    446  1.40  christos 			(void)usleep(1);
    447  1.27    atatat 	}
    448   1.1       cgd 
    449   1.1       cgd 	/*
    450  1.32  christos 	 * Output the message to the console; try not to block
    451  1.32  christos 	 * as a blocking console should not stop other processes.
    452  1.32  christos 	 * Make sure the error reported is the one from the syslogd failure.
    453   1.1       cgd 	 */
    454  1.40  christos 	if (tries == MAXTRIES && (data->log_stat & LOG_CONS) &&
    455  1.48  christos 	    (fd = open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) {
    456  1.48  christos 		iov[iovcnt].iov_base = __UNCONST(CRLF);
    457  1.48  christos 		iov[iovcnt].iov_len = 2;
    458  1.48  christos 		(void)writev(fd, iov, iovcnt + 1);
    459   1.1       cgd 		(void)close(fd);
    460   1.1       cgd 	}
    461  1.40  christos 
    462  1.40  christos 	if (data == &sdata)
    463  1.40  christos 		mutex_unlock(&syslog_mutex);
    464  1.40  christos 
    465  1.41  christos 	if (data != &sdata && opened) {
    466  1.41  christos 		/* preserve log tag */
    467  1.41  christos 		const char *ident = data->log_tag;
    468  1.32  christos 		closelog_r(data);
    469  1.41  christos 		data->log_tag = ident;
    470  1.41  christos 	}
    471   1.1       cgd }
    472   1.1       cgd 
    473  1.32  christos static void
    474  1.32  christos disconnectlog_r(struct syslog_data *data)
    475  1.32  christos {
    476  1.32  christos 	/*
    477  1.32  christos 	 * If the user closed the FD and opened another in the same slot,
    478  1.32  christos 	 * that's their problem.  They should close it before calling on
    479  1.32  christos 	 * system services.
    480  1.32  christos 	 */
    481  1.32  christos 	if (data->log_file != -1) {
    482  1.32  christos 		(void)close(data->log_file);
    483  1.32  christos 		data->log_file = -1;
    484  1.32  christos 	}
    485  1.51  christos 	data->log_connected = 0;		/* retry connect */
    486  1.32  christos }
    487   1.1       cgd 
    488  1.20    kleink static void
    489  1.32  christos connectlog_r(struct syslog_data *data)
    490   1.1       cgd {
    491  1.32  christos 	/* AF_UNIX address of local logger */
    492  1.32  christos 	static const struct sockaddr_un sun = {
    493  1.32  christos 		.sun_family = AF_LOCAL,
    494  1.32  christos 		.sun_len = sizeof(sun),
    495  1.32  christos 		.sun_path = _PATH_LOG,
    496  1.32  christos 	};
    497  1.20    kleink 
    498  1.36  christos 	if (data->log_file == -1 || fcntl(data->log_file, F_GETFL, 0) == -1) {
    499  1.49  christos 		if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC,
    500  1.49  christos 		    0)) == -1)
    501  1.32  christos 			return;
    502  1.51  christos 		data->log_connected = 0;
    503   1.1       cgd 	}
    504  1.51  christos 	if (!data->log_connected) {
    505  1.32  christos 		if (connect(data->log_file,
    506  1.32  christos 		    (const struct sockaddr *)(const void *)&sun,
    507  1.50  christos 		    (socklen_t)sizeof(sun)) == -1) {
    508  1.32  christos 			(void)close(data->log_file);
    509  1.32  christos 			data->log_file = -1;
    510  1.43  christos 		} else
    511  1.51  christos 			data->log_connected = 1;
    512  1.17   thorpej 	}
    513   1.1       cgd }
    514   1.1       cgd 
    515  1.32  christos static void
    516  1.32  christos openlog_unlocked_r(const char *ident, int logstat, int logfac,
    517  1.32  christos     struct syslog_data *data)
    518  1.20    kleink {
    519  1.32  christos 	if (ident != NULL)
    520  1.32  christos 		data->log_tag = ident;
    521  1.32  christos 	data->log_stat = logstat;
    522  1.32  christos 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
    523  1.32  christos 		data->log_fac = logfac;
    524  1.20    kleink 
    525  1.32  christos 	if (data->log_stat & LOG_NDELAY)	/* open immediately */
    526  1.32  christos 		connectlog_r(data);
    527  1.41  christos 
    528  1.51  christos 	data->log_opened = 1;
    529  1.20    kleink }
    530  1.20    kleink 
    531  1.32  christos void
    532  1.32  christos openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data)
    533   1.1       cgd {
    534  1.32  christos 	if (data == &sdata)
    535  1.32  christos 		mutex_lock(&syslog_mutex);
    536  1.32  christos 	openlog_unlocked_r(ident, logstat, logfac, data);
    537  1.32  christos 	if (data == &sdata)
    538  1.32  christos 		mutex_unlock(&syslog_mutex);
    539  1.20    kleink }
    540  1.20    kleink 
    541  1.20    kleink void
    542  1.32  christos closelog_r(struct syslog_data *data)
    543  1.20    kleink {
    544  1.32  christos 	if (data == &sdata)
    545  1.32  christos 		mutex_lock(&syslog_mutex);
    546  1.32  christos 	(void)close(data->log_file);
    547  1.32  christos 	data->log_file = -1;
    548  1.51  christos 	data->log_connected = 0;
    549  1.32  christos 	data->log_tag = NULL;
    550  1.32  christos 	if (data == &sdata)
    551  1.32  christos 		mutex_unlock(&syslog_mutex);
    552   1.1       cgd }
    553   1.1       cgd 
    554   1.2     glass int
    555  1.32  christos setlogmask_r(int pmask, struct syslog_data *data)
    556   1.1       cgd {
    557   1.1       cgd 	int omask;
    558   1.1       cgd 
    559  1.32  christos 	omask = data->log_mask;
    560   1.1       cgd 	if (pmask != 0)
    561  1.32  christos 		data->log_mask = pmask;
    562  1.32  christos 	return omask;
    563   1.1       cgd }
    564