xref: /freebsd/lib/libc/gen/syslog.c (revision 9657520621daea11d06b4c8ecdb4191ca3353b3e)
158f0484fSRodney W. Grimes /*
258f0484fSRodney W. Grimes  * Copyright (c) 1983, 1988, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1358f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1458f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1558f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1658f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
3458f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
35adf6ad9eSPeter Wemm static char sccsid[] = "@(#)syslog.c	8.5 (Berkeley) 4/29/95";
3658f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
37b231cb39SDavid E. O'Brien #include <sys/cdefs.h>
38b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$");
3958f0484fSRodney W. Grimes 
40d201fe46SDaniel Eischen #include "namespace.h"
4158f0484fSRodney W. Grimes #include <sys/types.h>
4258f0484fSRodney W. Grimes #include <sys/socket.h>
4358f0484fSRodney W. Grimes #include <sys/syslog.h>
4458f0484fSRodney W. Grimes #include <sys/uio.h>
45d584948eSBrian Somers #include <sys/un.h>
4658f0484fSRodney W. Grimes #include <netdb.h>
4758f0484fSRodney W. Grimes 
4858f0484fSRodney W. Grimes #include <errno.h>
4958f0484fSRodney W. Grimes #include <fcntl.h>
5058f0484fSRodney W. Grimes #include <paths.h>
5196575206SGleb Smirnoff #include <pthread.h>
5258f0484fSRodney W. Grimes #include <stdio.h>
534cd01193SMark Murray #include <stdlib.h>
5458f0484fSRodney W. Grimes #include <string.h>
5558f0484fSRodney W. Grimes #include <time.h>
5658f0484fSRodney W. Grimes #include <unistd.h>
5758f0484fSRodney W. Grimes 
5858f0484fSRodney W. Grimes #include <stdarg.h>
59d201fe46SDaniel Eischen #include "un-namespace.h"
6058f0484fSRodney W. Grimes 
614cd01193SMark Murray #include "libc_private.h"
624cd01193SMark Murray 
6358f0484fSRodney W. Grimes static int	LogFile = -1;		/* fd for log */
64240d5a9bSGleb Smirnoff static int	status;			/* connection status */
657e6ace85SPeter Wemm static int	opened;			/* have done openlog() */
6658f0484fSRodney W. Grimes static int	LogStat = 0;		/* status bits, set by openlog() */
6758f0484fSRodney W. Grimes static const char *LogTag = NULL;	/* string to tag the entry with */
6858f0484fSRodney W. Grimes static int	LogFacility = LOG_USER;	/* default facility code */
6958f0484fSRodney W. Grimes static int	LogMask = 0xff;		/* mask of priorities to be logged */
7096575206SGleb Smirnoff static pthread_mutex_t	syslog_mutex = PTHREAD_MUTEX_INITIALIZER;
7196575206SGleb Smirnoff 
7296575206SGleb Smirnoff #define	THREAD_LOCK()							\
7396575206SGleb Smirnoff 	do { 								\
7496575206SGleb Smirnoff 		if (__isthreaded) _pthread_mutex_lock(&syslog_mutex);	\
7596575206SGleb Smirnoff 	} while(0)
7696575206SGleb Smirnoff #define	THREAD_UNLOCK()							\
7796575206SGleb Smirnoff 	do {								\
7896575206SGleb Smirnoff 		if (__isthreaded) _pthread_mutex_unlock(&syslog_mutex);	\
7996575206SGleb Smirnoff 	} while(0)
8058f0484fSRodney W. Grimes 
81b231cb39SDavid E. O'Brien static void	disconnectlog(void); /* disconnect from syslogd */
82b231cb39SDavid E. O'Brien static void	connectlog(void);	/* (re)connect to syslogd */
8396575206SGleb Smirnoff static void	openlog_unlocked(const char *, int, int);
847e6ace85SPeter Wemm 
85240d5a9bSGleb Smirnoff enum {
86240d5a9bSGleb Smirnoff 	NOCONN = 0,
87240d5a9bSGleb Smirnoff 	CONNDEF,
88240d5a9bSGleb Smirnoff 	CONNPRIV,
89240d5a9bSGleb Smirnoff };
90240d5a9bSGleb Smirnoff 
9158f0484fSRodney W. Grimes /*
927c8e2aa4SPeter Wemm  * Format of the magic cookie passed through the stdio hook
937c8e2aa4SPeter Wemm  */
947c8e2aa4SPeter Wemm struct bufcookie {
957c8e2aa4SPeter Wemm 	char	*base;	/* start of buffer */
967c8e2aa4SPeter Wemm 	int	left;
977c8e2aa4SPeter Wemm };
987c8e2aa4SPeter Wemm 
997c8e2aa4SPeter Wemm /*
1007c8e2aa4SPeter Wemm  * stdio write hook for writing to a static string buffer
1017c8e2aa4SPeter Wemm  * XXX: Maybe one day, dynamically allocate it so that the line length
1027c8e2aa4SPeter Wemm  *      is `unlimited'.
1037c8e2aa4SPeter Wemm  */
1047e6ace85SPeter Wemm static
1057e6ace85SPeter Wemm int writehook(cookie, buf, len)
1067c8e2aa4SPeter Wemm 	void	*cookie;	/* really [struct bufcookie *] */
1077c8e2aa4SPeter Wemm 	char	*buf;		/* characters to copy */
1087c8e2aa4SPeter Wemm 	int	len;		/* length to copy */
1097c8e2aa4SPeter Wemm {
1107c8e2aa4SPeter Wemm 	struct bufcookie *h;	/* private `handle' */
1117c8e2aa4SPeter Wemm 
1127c8e2aa4SPeter Wemm 	h = (struct bufcookie *)cookie;
1137c8e2aa4SPeter Wemm 	if (len > h->left) {
1147c8e2aa4SPeter Wemm 		/* clip in case of wraparound */
1157c8e2aa4SPeter Wemm 		len = h->left;
1167c8e2aa4SPeter Wemm 	}
1177c8e2aa4SPeter Wemm 	if (len > 0) {
1187c8e2aa4SPeter Wemm 		(void)memcpy(h->base, buf, len); /* `write' it. */
1197c8e2aa4SPeter Wemm 		h->base += len;
1207c8e2aa4SPeter Wemm 		h->left -= len;
1217c8e2aa4SPeter Wemm 	}
1227c8e2aa4SPeter Wemm 	return 0;
1237c8e2aa4SPeter Wemm }
1247c8e2aa4SPeter Wemm 
1257c8e2aa4SPeter Wemm /*
12658f0484fSRodney W. Grimes  * syslog, vsyslog --
12758f0484fSRodney W. Grimes  *	print message on log file; output is intended for syslogd(8).
12858f0484fSRodney W. Grimes  */
12958f0484fSRodney W. Grimes void
13058f0484fSRodney W. Grimes syslog(int pri, const char *fmt, ...)
13158f0484fSRodney W. Grimes {
13258f0484fSRodney W. Grimes 	va_list ap;
13358f0484fSRodney W. Grimes 
13458f0484fSRodney W. Grimes 	va_start(ap, fmt);
13558f0484fSRodney W. Grimes 	vsyslog(pri, fmt, ap);
13658f0484fSRodney W. Grimes 	va_end(ap);
13758f0484fSRodney W. Grimes }
13858f0484fSRodney W. Grimes 
13958f0484fSRodney W. Grimes void
14058f0484fSRodney W. Grimes vsyslog(pri, fmt, ap)
14158f0484fSRodney W. Grimes 	int pri;
142b231cb39SDavid E. O'Brien 	const char *fmt;
14358f0484fSRodney W. Grimes 	va_list ap;
14458f0484fSRodney W. Grimes {
145b231cb39SDavid E. O'Brien 	int cnt;
146b231cb39SDavid E. O'Brien 	char ch, *p;
14758f0484fSRodney W. Grimes 	time_t now;
14858f0484fSRodney W. Grimes 	int fd, saved_errno;
14996575206SGleb Smirnoff 	char *stdp, tbuf[2048], fmt_cpy[1024], timbuf[26], errstr[64];
1507c8e2aa4SPeter Wemm 	FILE *fp, *fmt_fp;
1517c8e2aa4SPeter Wemm 	struct bufcookie tbuf_cookie;
1527c8e2aa4SPeter Wemm 	struct bufcookie fmt_cookie;
15358f0484fSRodney W. Grimes 
15458f0484fSRodney W. Grimes #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
15558f0484fSRodney W. Grimes 	/* Check for invalid bits. */
15658f0484fSRodney W. Grimes 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
15758f0484fSRodney W. Grimes 		syslog(INTERNALLOG,
15858f0484fSRodney W. Grimes 		    "syslog: unknown facility/priority: %x", pri);
15958f0484fSRodney W. Grimes 		pri &= LOG_PRIMASK|LOG_FACMASK;
16058f0484fSRodney W. Grimes 	}
16158f0484fSRodney W. Grimes 
16296575206SGleb Smirnoff 	THREAD_LOCK();
16396575206SGleb Smirnoff 
16458f0484fSRodney W. Grimes 	/* Check priority against setlogmask values. */
16596575206SGleb Smirnoff 	if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) {
16696575206SGleb Smirnoff 		THREAD_UNLOCK();
16758f0484fSRodney W. Grimes 		return;
16896575206SGleb Smirnoff 	}
16958f0484fSRodney W. Grimes 
17058f0484fSRodney W. Grimes 	saved_errno = errno;
17158f0484fSRodney W. Grimes 
17258f0484fSRodney W. Grimes 	/* Set default facility if none specified. */
17358f0484fSRodney W. Grimes 	if ((pri & LOG_FACMASK) == 0)
17458f0484fSRodney W. Grimes 		pri |= LogFacility;
17558f0484fSRodney W. Grimes 
1767c8e2aa4SPeter Wemm 	/* Create the primary stdio hook */
1777c8e2aa4SPeter Wemm 	tbuf_cookie.base = tbuf;
1787c8e2aa4SPeter Wemm 	tbuf_cookie.left = sizeof(tbuf);
1797c8e2aa4SPeter Wemm 	fp = fwopen(&tbuf_cookie, writehook);
18096575206SGleb Smirnoff 	if (fp == NULL) {
18196575206SGleb Smirnoff 		THREAD_UNLOCK();
1827c8e2aa4SPeter Wemm 		return;
18396575206SGleb Smirnoff 	}
1847c8e2aa4SPeter Wemm 
18558f0484fSRodney W. Grimes 	/* Build the message. */
18658f0484fSRodney W. Grimes 	(void)time(&now);
1877c8e2aa4SPeter Wemm 	(void)fprintf(fp, "<%d>", pri);
188ab09ef00SDavid Malone 	(void)fprintf(fp, "%.15s ", ctime_r(&now, timbuf) + 4);
1897c8e2aa4SPeter Wemm 	if (LogStat & LOG_PERROR) {
1907c8e2aa4SPeter Wemm 		/* Transfer to string buffer */
1917c8e2aa4SPeter Wemm 		(void)fflush(fp);
1927c8e2aa4SPeter Wemm 		stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left);
1937c8e2aa4SPeter Wemm 	}
19458f0484fSRodney W. Grimes 	if (LogTag == NULL)
1954cd01193SMark Murray 		LogTag = _getprogname();
19658f0484fSRodney W. Grimes 	if (LogTag != NULL)
1977c8e2aa4SPeter Wemm 		(void)fprintf(fp, "%s", LogTag);
19858f0484fSRodney W. Grimes 	if (LogStat & LOG_PID)
1997c8e2aa4SPeter Wemm 		(void)fprintf(fp, "[%d]", getpid());
20058f0484fSRodney W. Grimes 	if (LogTag != NULL) {
2017c8e2aa4SPeter Wemm 		(void)fprintf(fp, ": ");
2027c8e2aa4SPeter Wemm 	}
2037c8e2aa4SPeter Wemm 
2047c8e2aa4SPeter Wemm 	/* Check to see if we can skip expanding the %m */
2057c8e2aa4SPeter Wemm 	if (strstr(fmt, "%m")) {
2067c8e2aa4SPeter Wemm 
2077c8e2aa4SPeter Wemm 		/* Create the second stdio hook */
2087c8e2aa4SPeter Wemm 		fmt_cookie.base = fmt_cpy;
2097c8e2aa4SPeter Wemm 		fmt_cookie.left = sizeof(fmt_cpy) - 1;
2107c8e2aa4SPeter Wemm 		fmt_fp = fwopen(&fmt_cookie, writehook);
2117c8e2aa4SPeter Wemm 		if (fmt_fp == NULL) {
2127c8e2aa4SPeter Wemm 			fclose(fp);
21396575206SGleb Smirnoff 			THREAD_UNLOCK();
2147c8e2aa4SPeter Wemm 			return;
21558f0484fSRodney W. Grimes 		}
21658f0484fSRodney W. Grimes 
217e6cfb1ccSAlfred Perlstein 		/*
218e6cfb1ccSAlfred Perlstein 		 * Substitute error message for %m.  Be careful not to
219e6cfb1ccSAlfred Perlstein 		 * molest an escaped percent "%%m".  We want to pass it
220e6cfb1ccSAlfred Perlstein 		 * on untouched as the format is later parsed by vfprintf.
221e6cfb1ccSAlfred Perlstein 		 */
222e6cfb1ccSAlfred Perlstein 		for ( ; (ch = *fmt); ++fmt) {
22358f0484fSRodney W. Grimes 			if (ch == '%' && fmt[1] == 'm') {
22458f0484fSRodney W. Grimes 				++fmt;
22596575206SGleb Smirnoff 				strerror_r(saved_errno, errstr, sizeof(errstr));
22696575206SGleb Smirnoff 				fputs(errstr, fmt_fp);
227e6cfb1ccSAlfred Perlstein 			} else if (ch == '%' && fmt[1] == '%') {
228e6cfb1ccSAlfred Perlstein 				++fmt;
2297c8e2aa4SPeter Wemm 				fputc(ch, fmt_fp);
230e6cfb1ccSAlfred Perlstein 				fputc(ch, fmt_fp);
231e6cfb1ccSAlfred Perlstein 			} else {
232e6cfb1ccSAlfred Perlstein 				fputc(ch, fmt_fp);
233e6cfb1ccSAlfred Perlstein 			}
234e6cfb1ccSAlfred Perlstein 		}
23558f0484fSRodney W. Grimes 
2367c8e2aa4SPeter Wemm 		/* Null terminate if room */
2377c8e2aa4SPeter Wemm 		fputc(0, fmt_fp);
2387c8e2aa4SPeter Wemm 		fclose(fmt_fp);
2397c8e2aa4SPeter Wemm 
2407c8e2aa4SPeter Wemm 		/* Guarantee null termination */
2417c8e2aa4SPeter Wemm 		fmt_cpy[sizeof(fmt_cpy) - 1] = '\0';
2427c8e2aa4SPeter Wemm 
2437c8e2aa4SPeter Wemm 		fmt = fmt_cpy;
2447c8e2aa4SPeter Wemm 	}
2457c8e2aa4SPeter Wemm 
2467c8e2aa4SPeter Wemm 	(void)vfprintf(fp, fmt, ap);
2477c8e2aa4SPeter Wemm 	(void)fclose(fp);
2487c8e2aa4SPeter Wemm 
2497c8e2aa4SPeter Wemm 	cnt = sizeof(tbuf) - tbuf_cookie.left;
25058f0484fSRodney W. Grimes 
251857b57eaSDiomidis Spinellis 	/* Remove a trailing newline */
252857b57eaSDiomidis Spinellis 	if (tbuf[cnt - 1] == '\n')
253857b57eaSDiomidis Spinellis 		cnt--;
254857b57eaSDiomidis Spinellis 
25558f0484fSRodney W. Grimes 	/* Output to stderr if requested. */
25658f0484fSRodney W. Grimes 	if (LogStat & LOG_PERROR) {
25758f0484fSRodney W. Grimes 		struct iovec iov[2];
258b231cb39SDavid E. O'Brien 		struct iovec *v = iov;
25958f0484fSRodney W. Grimes 
26058f0484fSRodney W. Grimes 		v->iov_base = stdp;
26158f0484fSRodney W. Grimes 		v->iov_len = cnt - (stdp - tbuf);
26258f0484fSRodney W. Grimes 		++v;
26358f0484fSRodney W. Grimes 		v->iov_base = "\n";
26458f0484fSRodney W. Grimes 		v->iov_len = 1;
265d201fe46SDaniel Eischen 		(void)_writev(STDERR_FILENO, iov, 2);
26658f0484fSRodney W. Grimes 	}
26758f0484fSRodney W. Grimes 
26858f0484fSRodney W. Grimes 	/* Get connected, output the message to the local logger. */
2697e6ace85SPeter Wemm 	if (!opened)
27096575206SGleb Smirnoff 		openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0);
2717e6ace85SPeter Wemm 	connectlog();
2727e6ace85SPeter Wemm 
2737e6ace85SPeter Wemm 	/*
2742e89951bSGleb Smirnoff 	 * If the send() failed, there are two likely scenarios:
2752e89951bSGleb Smirnoff 	 *  1) syslogd was restarted
276240d5a9bSGleb Smirnoff 	 *  2) /var/run/log is out of socket buffer space, which
277240d5a9bSGleb Smirnoff 	 *     in most cases means local DoS.
2782e89951bSGleb Smirnoff 	 * We attempt to reconnect to /var/run/log to take care of
2792e89951bSGleb Smirnoff 	 * case #1 and keep send()ing data to cover case #2
2802e89951bSGleb Smirnoff 	 * to give syslogd a chance to empty its socket buffer.
281240d5a9bSGleb Smirnoff 	 *
282240d5a9bSGleb Smirnoff 	 * If we are working with a priveleged socket, then take
283240d5a9bSGleb Smirnoff 	 * only one attempt, because we don't want to freeze a
284240d5a9bSGleb Smirnoff 	 * critical application like su(1) or sshd(8).
285240d5a9bSGleb Smirnoff 	 *
2867e6ace85SPeter Wemm 	 */
2872e89951bSGleb Smirnoff 
2882e89951bSGleb Smirnoff 	if (send(LogFile, tbuf, cnt, 0) < 0) {
2892e89951bSGleb Smirnoff 		if (errno != ENOBUFS) {
2907e6ace85SPeter Wemm 			disconnectlog();
2917e6ace85SPeter Wemm 			connectlog();
2922e89951bSGleb Smirnoff 		}
2932e89951bSGleb Smirnoff 		do {
2942e89951bSGleb Smirnoff 			usleep(1);
29596575206SGleb Smirnoff 			if (send(LogFile, tbuf, cnt, 0) >= 0) {
29696575206SGleb Smirnoff 				THREAD_UNLOCK();
2978ba85e77SGleb Smirnoff 				return;
29896575206SGleb Smirnoff 			}
299240d5a9bSGleb Smirnoff 			if (status == CONNPRIV)
300240d5a9bSGleb Smirnoff 				break;
3012e89951bSGleb Smirnoff 		} while (errno == ENOBUFS);
30296575206SGleb Smirnoff 	} else {
30396575206SGleb Smirnoff 		THREAD_UNLOCK();
3048ba85e77SGleb Smirnoff 		return;
30596575206SGleb Smirnoff 	}
30658f0484fSRodney W. Grimes 
30758f0484fSRodney W. Grimes 	/*
30894998878SDavid Malone 	 * Output the message to the console; try not to block
30994998878SDavid Malone 	 * as a blocking console should not stop other processes.
31094998878SDavid Malone 	 * Make sure the error reported is the one from the syslogd failure.
31158f0484fSRodney W. Grimes 	 */
31258f0484fSRodney W. Grimes 	if (LogStat & LOG_CONS &&
31394998878SDavid Malone 	    (fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) {
31411e67a9fSPeter Wemm 		struct iovec iov[2];
315b231cb39SDavid E. O'Brien 		struct iovec *v = iov;
31611e67a9fSPeter Wemm 
31711e67a9fSPeter Wemm 		p = strchr(tbuf, '>') + 1;
31811e67a9fSPeter Wemm 		v->iov_base = p;
31911e67a9fSPeter Wemm 		v->iov_len = cnt - (p - tbuf);
32011e67a9fSPeter Wemm 		++v;
32111e67a9fSPeter Wemm 		v->iov_base = "\r\n";
32211e67a9fSPeter Wemm 		v->iov_len = 2;
323d201fe46SDaniel Eischen 		(void)_writev(fd, iov, 2);
3249233c4d9SJason Evans 		(void)_close(fd);
32558f0484fSRodney W. Grimes 	}
32696575206SGleb Smirnoff 
32796575206SGleb Smirnoff 	THREAD_UNLOCK();
32858f0484fSRodney W. Grimes }
32996575206SGleb Smirnoff 
33096575206SGleb Smirnoff /* Should be called with mutex acquired */
3317e6ace85SPeter Wemm static void
3327e6ace85SPeter Wemm disconnectlog()
3337e6ace85SPeter Wemm {
3347e6ace85SPeter Wemm 	/*
3357e6ace85SPeter Wemm 	 * If the user closed the FD and opened another in the same slot,
3367e6ace85SPeter Wemm 	 * that's their problem.  They should close it before calling on
3377e6ace85SPeter Wemm 	 * system services.
3387e6ace85SPeter Wemm 	 */
3397e6ace85SPeter Wemm 	if (LogFile != -1) {
3409233c4d9SJason Evans 		_close(LogFile);
3417e6ace85SPeter Wemm 		LogFile = -1;
3427e6ace85SPeter Wemm 	}
343240d5a9bSGleb Smirnoff 	status = NOCONN;			/* retry connect */
3447e6ace85SPeter Wemm }
3457e6ace85SPeter Wemm 
34696575206SGleb Smirnoff /* Should be called with mutex acquired */
3477e6ace85SPeter Wemm static void
3487e6ace85SPeter Wemm connectlog()
3497e6ace85SPeter Wemm {
350d584948eSBrian Somers 	struct sockaddr_un SyslogAddr;	/* AF_UNIX address of local logger */
351cf49f439SJohn Polstra 
3527e6ace85SPeter Wemm 	if (LogFile == -1) {
353d201fe46SDaniel Eischen 		if ((LogFile = _socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
3547e6ace85SPeter Wemm 			return;
3559233c4d9SJason Evans 		(void)_fcntl(LogFile, F_SETFD, 1);
3567e6ace85SPeter Wemm 	}
357240d5a9bSGleb Smirnoff 	if (LogFile != -1 && status == NOCONN) {
358d584948eSBrian Somers 		SyslogAddr.sun_len = sizeof(SyslogAddr);
359d584948eSBrian Somers 		SyslogAddr.sun_family = AF_UNIX;
360240d5a9bSGleb Smirnoff 
361240d5a9bSGleb Smirnoff 		/*
362240d5a9bSGleb Smirnoff 		 * First try priveleged socket. If no success,
363240d5a9bSGleb Smirnoff 		 * then try default socket.
364240d5a9bSGleb Smirnoff 		 */
365240d5a9bSGleb Smirnoff 		(void)strncpy(SyslogAddr.sun_path, _PATH_LOG_PRIV,
366240d5a9bSGleb Smirnoff 		    sizeof SyslogAddr.sun_path);
367240d5a9bSGleb Smirnoff 		if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
368240d5a9bSGleb Smirnoff 		    sizeof(SyslogAddr)) != -1)
369240d5a9bSGleb Smirnoff 			status = CONNPRIV;
370240d5a9bSGleb Smirnoff 
371240d5a9bSGleb Smirnoff 		if (status == NOCONN) {
372d584948eSBrian Somers 			(void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
3730b3b961eSBrian Somers 			    sizeof SyslogAddr.sun_path);
374240d5a9bSGleb Smirnoff 			if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
375240d5a9bSGleb Smirnoff 			    sizeof(SyslogAddr)) != -1)
376240d5a9bSGleb Smirnoff 				status = CONNDEF;
377240d5a9bSGleb Smirnoff 		}
378cf49f439SJohn Polstra 
379240d5a9bSGleb Smirnoff 		if (status == NOCONN) {
380cf49f439SJohn Polstra 			/*
381cf49f439SJohn Polstra 			 * Try the old "/dev/log" path, for backward
382cf49f439SJohn Polstra 			 * compatibility.
383cf49f439SJohn Polstra 			 */
384d584948eSBrian Somers 			(void)strncpy(SyslogAddr.sun_path, _PATH_OLDLOG,
3850b3b961eSBrian Somers 			    sizeof SyslogAddr.sun_path);
386240d5a9bSGleb Smirnoff 			if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
387240d5a9bSGleb Smirnoff 			    sizeof(SyslogAddr)) != -1)
388240d5a9bSGleb Smirnoff 				status = CONNDEF;
389cf49f439SJohn Polstra 		}
390cf49f439SJohn Polstra 
391240d5a9bSGleb Smirnoff 		if (status == NOCONN) {
3929233c4d9SJason Evans 			(void)_close(LogFile);
3937e6ace85SPeter Wemm 			LogFile = -1;
394cf49f439SJohn Polstra 		}
3957e6ace85SPeter Wemm 	}
3967e6ace85SPeter Wemm }
3977e6ace85SPeter Wemm 
39896575206SGleb Smirnoff static void
39996575206SGleb Smirnoff openlog_unlocked(ident, logstat, logfac)
40058f0484fSRodney W. Grimes 	const char *ident;
40158f0484fSRodney W. Grimes 	int logstat, logfac;
40258f0484fSRodney W. Grimes {
40358f0484fSRodney W. Grimes 	if (ident != NULL)
40458f0484fSRodney W. Grimes 		LogTag = ident;
40558f0484fSRodney W. Grimes 	LogStat = logstat;
40658f0484fSRodney W. Grimes 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
40758f0484fSRodney W. Grimes 		LogFacility = logfac;
40858f0484fSRodney W. Grimes 
4097e6ace85SPeter Wemm 	if (LogStat & LOG_NDELAY)	/* open immediately */
4107e6ace85SPeter Wemm 		connectlog();
4117e6ace85SPeter Wemm 
4127e6ace85SPeter Wemm 	opened = 1;	/* ident and facility has been set */
41358f0484fSRodney W. Grimes }
41458f0484fSRodney W. Grimes 
41558f0484fSRodney W. Grimes void
41696575206SGleb Smirnoff openlog(ident, logstat, logfac)
41796575206SGleb Smirnoff 	const char *ident;
41896575206SGleb Smirnoff 	int logstat, logfac;
41996575206SGleb Smirnoff {
42096575206SGleb Smirnoff 	THREAD_LOCK();
42196575206SGleb Smirnoff 	openlog_unlocked(ident, logstat, logfac);
42296575206SGleb Smirnoff 	THREAD_UNLOCK();
42396575206SGleb Smirnoff }
42496575206SGleb Smirnoff 
42596575206SGleb Smirnoff 
42696575206SGleb Smirnoff void
42758f0484fSRodney W. Grimes closelog()
42858f0484fSRodney W. Grimes {
42996575206SGleb Smirnoff 	THREAD_LOCK();
4309233c4d9SJason Evans 	(void)_close(LogFile);
43158f0484fSRodney W. Grimes 	LogFile = -1;
432bedff4e8SRuslan Ermilov 	LogTag = NULL;
433240d5a9bSGleb Smirnoff 	status = NOCONN;
43496575206SGleb Smirnoff 	THREAD_UNLOCK();
43558f0484fSRodney W. Grimes }
43658f0484fSRodney W. Grimes 
43758f0484fSRodney W. Grimes /* setlogmask -- set the log mask level */
43858f0484fSRodney W. Grimes int
43958f0484fSRodney W. Grimes setlogmask(pmask)
44058f0484fSRodney W. Grimes 	int pmask;
44158f0484fSRodney W. Grimes {
44258f0484fSRodney W. Grimes 	int omask;
44358f0484fSRodney W. Grimes 
44496575206SGleb Smirnoff 	THREAD_LOCK();
44558f0484fSRodney W. Grimes 	omask = LogMask;
44658f0484fSRodney W. Grimes 	if (pmask != 0)
44758f0484fSRodney W. Grimes 		LogMask = pmask;
44896575206SGleb Smirnoff 	THREAD_UNLOCK();
44958f0484fSRodney W. Grimes 	return (omask);
45058f0484fSRodney W. Grimes }
451