xref: /freebsd/lib/libc/gen/syslog.c (revision 3a31b448c000f0f6eb840010b4287565221d94a7)
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  */
104fd42c4d8SStefan Farfeleder static int
105fd42c4d8SStefan Farfeleder writehook(void *cookie, const char *buf, int len)
1067c8e2aa4SPeter Wemm {
1077c8e2aa4SPeter Wemm 	struct bufcookie *h;	/* private `handle' */
1087c8e2aa4SPeter Wemm 
1097c8e2aa4SPeter Wemm 	h = (struct bufcookie *)cookie;
1107c8e2aa4SPeter Wemm 	if (len > h->left) {
1117c8e2aa4SPeter Wemm 		/* clip in case of wraparound */
1127c8e2aa4SPeter Wemm 		len = h->left;
1137c8e2aa4SPeter Wemm 	}
1147c8e2aa4SPeter Wemm 	if (len > 0) {
1157c8e2aa4SPeter Wemm 		(void)memcpy(h->base, buf, len); /* `write' it. */
1167c8e2aa4SPeter Wemm 		h->base += len;
1177c8e2aa4SPeter Wemm 		h->left -= len;
1187c8e2aa4SPeter Wemm 	}
1197c8e2aa4SPeter Wemm 	return 0;
1207c8e2aa4SPeter Wemm }
1217c8e2aa4SPeter Wemm 
1227c8e2aa4SPeter Wemm /*
12358f0484fSRodney W. Grimes  * syslog, vsyslog --
12458f0484fSRodney W. Grimes  *	print message on log file; output is intended for syslogd(8).
12558f0484fSRodney W. Grimes  */
12658f0484fSRodney W. Grimes void
12758f0484fSRodney W. Grimes syslog(int pri, const char *fmt, ...)
12858f0484fSRodney W. Grimes {
12958f0484fSRodney W. Grimes 	va_list ap;
13058f0484fSRodney W. Grimes 
13158f0484fSRodney W. Grimes 	va_start(ap, fmt);
13258f0484fSRodney W. Grimes 	vsyslog(pri, fmt, ap);
13358f0484fSRodney W. Grimes 	va_end(ap);
13458f0484fSRodney W. Grimes }
13558f0484fSRodney W. Grimes 
13658f0484fSRodney W. Grimes void
137fd42c4d8SStefan Farfeleder vsyslog(int pri, const char *fmt, va_list ap)
13858f0484fSRodney W. Grimes {
139b231cb39SDavid E. O'Brien 	int cnt;
140b231cb39SDavid E. O'Brien 	char ch, *p;
14158f0484fSRodney W. Grimes 	time_t now;
14258f0484fSRodney W. Grimes 	int fd, saved_errno;
14396575206SGleb Smirnoff 	char *stdp, tbuf[2048], fmt_cpy[1024], timbuf[26], errstr[64];
1447c8e2aa4SPeter Wemm 	FILE *fp, *fmt_fp;
1457c8e2aa4SPeter Wemm 	struct bufcookie tbuf_cookie;
1467c8e2aa4SPeter Wemm 	struct bufcookie fmt_cookie;
14758f0484fSRodney W. Grimes 
14858f0484fSRodney W. Grimes #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
14958f0484fSRodney W. Grimes 	/* Check for invalid bits. */
15058f0484fSRodney W. Grimes 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
15158f0484fSRodney W. Grimes 		syslog(INTERNALLOG,
15258f0484fSRodney W. Grimes 		    "syslog: unknown facility/priority: %x", pri);
15358f0484fSRodney W. Grimes 		pri &= LOG_PRIMASK|LOG_FACMASK;
15458f0484fSRodney W. Grimes 	}
15558f0484fSRodney W. Grimes 
1563a31b448SDavid Xu 	saved_errno = errno;
1573a31b448SDavid Xu 
15896575206SGleb Smirnoff 	THREAD_LOCK();
15996575206SGleb Smirnoff 
16058f0484fSRodney W. Grimes 	/* Check priority against setlogmask values. */
16196575206SGleb Smirnoff 	if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) {
16296575206SGleb Smirnoff 		THREAD_UNLOCK();
16358f0484fSRodney W. Grimes 		return;
16496575206SGleb Smirnoff 	}
16558f0484fSRodney W. Grimes 
16658f0484fSRodney W. Grimes 	/* Set default facility if none specified. */
16758f0484fSRodney W. Grimes 	if ((pri & LOG_FACMASK) == 0)
16858f0484fSRodney W. Grimes 		pri |= LogFacility;
16958f0484fSRodney W. Grimes 
1707c8e2aa4SPeter Wemm 	/* Create the primary stdio hook */
1717c8e2aa4SPeter Wemm 	tbuf_cookie.base = tbuf;
1727c8e2aa4SPeter Wemm 	tbuf_cookie.left = sizeof(tbuf);
1737c8e2aa4SPeter Wemm 	fp = fwopen(&tbuf_cookie, writehook);
17496575206SGleb Smirnoff 	if (fp == NULL) {
17596575206SGleb Smirnoff 		THREAD_UNLOCK();
1767c8e2aa4SPeter Wemm 		return;
17796575206SGleb Smirnoff 	}
1787c8e2aa4SPeter Wemm 
17958f0484fSRodney W. Grimes 	/* Build the message. */
18058f0484fSRodney W. Grimes 	(void)time(&now);
1817c8e2aa4SPeter Wemm 	(void)fprintf(fp, "<%d>", pri);
182ab09ef00SDavid Malone 	(void)fprintf(fp, "%.15s ", ctime_r(&now, timbuf) + 4);
1837c8e2aa4SPeter Wemm 	if (LogStat & LOG_PERROR) {
1847c8e2aa4SPeter Wemm 		/* Transfer to string buffer */
1857c8e2aa4SPeter Wemm 		(void)fflush(fp);
1867c8e2aa4SPeter Wemm 		stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left);
1877c8e2aa4SPeter Wemm 	}
18858f0484fSRodney W. Grimes 	if (LogTag == NULL)
1894cd01193SMark Murray 		LogTag = _getprogname();
19058f0484fSRodney W. Grimes 	if (LogTag != NULL)
1917c8e2aa4SPeter Wemm 		(void)fprintf(fp, "%s", LogTag);
19258f0484fSRodney W. Grimes 	if (LogStat & LOG_PID)
1937c8e2aa4SPeter Wemm 		(void)fprintf(fp, "[%d]", getpid());
19458f0484fSRodney W. Grimes 	if (LogTag != NULL) {
1957c8e2aa4SPeter Wemm 		(void)fprintf(fp, ": ");
1967c8e2aa4SPeter Wemm 	}
1977c8e2aa4SPeter Wemm 
1987c8e2aa4SPeter Wemm 	/* Check to see if we can skip expanding the %m */
1997c8e2aa4SPeter Wemm 	if (strstr(fmt, "%m")) {
2007c8e2aa4SPeter Wemm 
2017c8e2aa4SPeter Wemm 		/* Create the second stdio hook */
2027c8e2aa4SPeter Wemm 		fmt_cookie.base = fmt_cpy;
2037c8e2aa4SPeter Wemm 		fmt_cookie.left = sizeof(fmt_cpy) - 1;
2047c8e2aa4SPeter Wemm 		fmt_fp = fwopen(&fmt_cookie, writehook);
2057c8e2aa4SPeter Wemm 		if (fmt_fp == NULL) {
2067c8e2aa4SPeter Wemm 			fclose(fp);
20796575206SGleb Smirnoff 			THREAD_UNLOCK();
2087c8e2aa4SPeter Wemm 			return;
20958f0484fSRodney W. Grimes 		}
21058f0484fSRodney W. Grimes 
211e6cfb1ccSAlfred Perlstein 		/*
212e6cfb1ccSAlfred Perlstein 		 * Substitute error message for %m.  Be careful not to
213e6cfb1ccSAlfred Perlstein 		 * molest an escaped percent "%%m".  We want to pass it
214e6cfb1ccSAlfred Perlstein 		 * on untouched as the format is later parsed by vfprintf.
215e6cfb1ccSAlfred Perlstein 		 */
216e6cfb1ccSAlfred Perlstein 		for ( ; (ch = *fmt); ++fmt) {
21758f0484fSRodney W. Grimes 			if (ch == '%' && fmt[1] == 'm') {
21858f0484fSRodney W. Grimes 				++fmt;
21996575206SGleb Smirnoff 				strerror_r(saved_errno, errstr, sizeof(errstr));
22096575206SGleb Smirnoff 				fputs(errstr, fmt_fp);
221e6cfb1ccSAlfred Perlstein 			} else if (ch == '%' && fmt[1] == '%') {
222e6cfb1ccSAlfred Perlstein 				++fmt;
2237c8e2aa4SPeter Wemm 				fputc(ch, fmt_fp);
224e6cfb1ccSAlfred Perlstein 				fputc(ch, fmt_fp);
225e6cfb1ccSAlfred Perlstein 			} else {
226e6cfb1ccSAlfred Perlstein 				fputc(ch, fmt_fp);
227e6cfb1ccSAlfred Perlstein 			}
228e6cfb1ccSAlfred Perlstein 		}
22958f0484fSRodney W. Grimes 
2307c8e2aa4SPeter Wemm 		/* Null terminate if room */
2317c8e2aa4SPeter Wemm 		fputc(0, fmt_fp);
2327c8e2aa4SPeter Wemm 		fclose(fmt_fp);
2337c8e2aa4SPeter Wemm 
2347c8e2aa4SPeter Wemm 		/* Guarantee null termination */
2357c8e2aa4SPeter Wemm 		fmt_cpy[sizeof(fmt_cpy) - 1] = '\0';
2367c8e2aa4SPeter Wemm 
2377c8e2aa4SPeter Wemm 		fmt = fmt_cpy;
2387c8e2aa4SPeter Wemm 	}
2397c8e2aa4SPeter Wemm 
2407c8e2aa4SPeter Wemm 	(void)vfprintf(fp, fmt, ap);
2417c8e2aa4SPeter Wemm 	(void)fclose(fp);
2427c8e2aa4SPeter Wemm 
2437c8e2aa4SPeter Wemm 	cnt = sizeof(tbuf) - tbuf_cookie.left;
24458f0484fSRodney W. Grimes 
245857b57eaSDiomidis Spinellis 	/* Remove a trailing newline */
246857b57eaSDiomidis Spinellis 	if (tbuf[cnt - 1] == '\n')
247857b57eaSDiomidis Spinellis 		cnt--;
248857b57eaSDiomidis Spinellis 
24958f0484fSRodney W. Grimes 	/* Output to stderr if requested. */
25058f0484fSRodney W. Grimes 	if (LogStat & LOG_PERROR) {
25158f0484fSRodney W. Grimes 		struct iovec iov[2];
252b231cb39SDavid E. O'Brien 		struct iovec *v = iov;
25358f0484fSRodney W. Grimes 
25458f0484fSRodney W. Grimes 		v->iov_base = stdp;
25558f0484fSRodney W. Grimes 		v->iov_len = cnt - (stdp - tbuf);
25658f0484fSRodney W. Grimes 		++v;
25758f0484fSRodney W. Grimes 		v->iov_base = "\n";
25858f0484fSRodney W. Grimes 		v->iov_len = 1;
259d201fe46SDaniel Eischen 		(void)_writev(STDERR_FILENO, iov, 2);
26058f0484fSRodney W. Grimes 	}
26158f0484fSRodney W. Grimes 
26258f0484fSRodney W. Grimes 	/* Get connected, output the message to the local logger. */
2637e6ace85SPeter Wemm 	if (!opened)
26496575206SGleb Smirnoff 		openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0);
2657e6ace85SPeter Wemm 	connectlog();
2667e6ace85SPeter Wemm 
2677e6ace85SPeter Wemm 	/*
2682e89951bSGleb Smirnoff 	 * If the send() failed, there are two likely scenarios:
2692e89951bSGleb Smirnoff 	 *  1) syslogd was restarted
270240d5a9bSGleb Smirnoff 	 *  2) /var/run/log is out of socket buffer space, which
271240d5a9bSGleb Smirnoff 	 *     in most cases means local DoS.
2722e89951bSGleb Smirnoff 	 * We attempt to reconnect to /var/run/log to take care of
2732e89951bSGleb Smirnoff 	 * case #1 and keep send()ing data to cover case #2
2742e89951bSGleb Smirnoff 	 * to give syslogd a chance to empty its socket buffer.
275240d5a9bSGleb Smirnoff 	 *
276240d5a9bSGleb Smirnoff 	 * If we are working with a priveleged socket, then take
277240d5a9bSGleb Smirnoff 	 * only one attempt, because we don't want to freeze a
278240d5a9bSGleb Smirnoff 	 * critical application like su(1) or sshd(8).
279240d5a9bSGleb Smirnoff 	 *
2807e6ace85SPeter Wemm 	 */
2812e89951bSGleb Smirnoff 
2822e89951bSGleb Smirnoff 	if (send(LogFile, tbuf, cnt, 0) < 0) {
2832e89951bSGleb Smirnoff 		if (errno != ENOBUFS) {
2847e6ace85SPeter Wemm 			disconnectlog();
2857e6ace85SPeter Wemm 			connectlog();
2862e89951bSGleb Smirnoff 		}
2872e89951bSGleb Smirnoff 		do {
288ed0b0abcSDaniel Eischen 			_usleep(1);
28996575206SGleb Smirnoff 			if (send(LogFile, tbuf, cnt, 0) >= 0) {
29096575206SGleb Smirnoff 				THREAD_UNLOCK();
2918ba85e77SGleb Smirnoff 				return;
29296575206SGleb Smirnoff 			}
293240d5a9bSGleb Smirnoff 			if (status == CONNPRIV)
294240d5a9bSGleb Smirnoff 				break;
2952e89951bSGleb Smirnoff 		} while (errno == ENOBUFS);
29696575206SGleb Smirnoff 	} else {
29796575206SGleb Smirnoff 		THREAD_UNLOCK();
2988ba85e77SGleb Smirnoff 		return;
29996575206SGleb Smirnoff 	}
30058f0484fSRodney W. Grimes 
30158f0484fSRodney W. Grimes 	/*
30294998878SDavid Malone 	 * Output the message to the console; try not to block
30394998878SDavid Malone 	 * as a blocking console should not stop other processes.
30494998878SDavid Malone 	 * Make sure the error reported is the one from the syslogd failure.
30558f0484fSRodney W. Grimes 	 */
30658f0484fSRodney W. Grimes 	if (LogStat & LOG_CONS &&
30794998878SDavid Malone 	    (fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) {
30811e67a9fSPeter Wemm 		struct iovec iov[2];
309b231cb39SDavid E. O'Brien 		struct iovec *v = iov;
31011e67a9fSPeter Wemm 
31111e67a9fSPeter Wemm 		p = strchr(tbuf, '>') + 1;
31211e67a9fSPeter Wemm 		v->iov_base = p;
31311e67a9fSPeter Wemm 		v->iov_len = cnt - (p - tbuf);
31411e67a9fSPeter Wemm 		++v;
31511e67a9fSPeter Wemm 		v->iov_base = "\r\n";
31611e67a9fSPeter Wemm 		v->iov_len = 2;
317d201fe46SDaniel Eischen 		(void)_writev(fd, iov, 2);
3189233c4d9SJason Evans 		(void)_close(fd);
31958f0484fSRodney W. Grimes 	}
32096575206SGleb Smirnoff 
32196575206SGleb Smirnoff 	THREAD_UNLOCK();
32258f0484fSRodney W. Grimes }
32396575206SGleb Smirnoff 
32496575206SGleb Smirnoff /* Should be called with mutex acquired */
3257e6ace85SPeter Wemm static void
326fd42c4d8SStefan Farfeleder disconnectlog(void)
3277e6ace85SPeter Wemm {
3287e6ace85SPeter Wemm 	/*
3297e6ace85SPeter Wemm 	 * If the user closed the FD and opened another in the same slot,
3307e6ace85SPeter Wemm 	 * that's their problem.  They should close it before calling on
3317e6ace85SPeter Wemm 	 * system services.
3327e6ace85SPeter Wemm 	 */
3337e6ace85SPeter Wemm 	if (LogFile != -1) {
3349233c4d9SJason Evans 		_close(LogFile);
3357e6ace85SPeter Wemm 		LogFile = -1;
3367e6ace85SPeter Wemm 	}
337240d5a9bSGleb Smirnoff 	status = NOCONN;			/* retry connect */
3387e6ace85SPeter Wemm }
3397e6ace85SPeter Wemm 
34096575206SGleb Smirnoff /* Should be called with mutex acquired */
3417e6ace85SPeter Wemm static void
342fd42c4d8SStefan Farfeleder connectlog(void)
3437e6ace85SPeter Wemm {
344d584948eSBrian Somers 	struct sockaddr_un SyslogAddr;	/* AF_UNIX address of local logger */
345cf49f439SJohn Polstra 
3467e6ace85SPeter Wemm 	if (LogFile == -1) {
347d201fe46SDaniel Eischen 		if ((LogFile = _socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
3487e6ace85SPeter Wemm 			return;
3499233c4d9SJason Evans 		(void)_fcntl(LogFile, F_SETFD, 1);
3507e6ace85SPeter Wemm 	}
351240d5a9bSGleb Smirnoff 	if (LogFile != -1 && status == NOCONN) {
352d584948eSBrian Somers 		SyslogAddr.sun_len = sizeof(SyslogAddr);
353d584948eSBrian Somers 		SyslogAddr.sun_family = AF_UNIX;
354240d5a9bSGleb Smirnoff 
355240d5a9bSGleb Smirnoff 		/*
356240d5a9bSGleb Smirnoff 		 * First try priveleged socket. If no success,
357240d5a9bSGleb Smirnoff 		 * then try default socket.
358240d5a9bSGleb Smirnoff 		 */
359240d5a9bSGleb Smirnoff 		(void)strncpy(SyslogAddr.sun_path, _PATH_LOG_PRIV,
360240d5a9bSGleb Smirnoff 		    sizeof SyslogAddr.sun_path);
361240d5a9bSGleb Smirnoff 		if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
362240d5a9bSGleb Smirnoff 		    sizeof(SyslogAddr)) != -1)
363240d5a9bSGleb Smirnoff 			status = CONNPRIV;
364240d5a9bSGleb Smirnoff 
365240d5a9bSGleb Smirnoff 		if (status == NOCONN) {
366d584948eSBrian Somers 			(void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
3670b3b961eSBrian Somers 			    sizeof SyslogAddr.sun_path);
368240d5a9bSGleb Smirnoff 			if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
369240d5a9bSGleb Smirnoff 			    sizeof(SyslogAddr)) != -1)
370240d5a9bSGleb Smirnoff 				status = CONNDEF;
371240d5a9bSGleb Smirnoff 		}
372cf49f439SJohn Polstra 
373240d5a9bSGleb Smirnoff 		if (status == NOCONN) {
374cf49f439SJohn Polstra 			/*
375cf49f439SJohn Polstra 			 * Try the old "/dev/log" path, for backward
376cf49f439SJohn Polstra 			 * compatibility.
377cf49f439SJohn Polstra 			 */
378d584948eSBrian Somers 			(void)strncpy(SyslogAddr.sun_path, _PATH_OLDLOG,
3790b3b961eSBrian Somers 			    sizeof SyslogAddr.sun_path);
380240d5a9bSGleb Smirnoff 			if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
381240d5a9bSGleb Smirnoff 			    sizeof(SyslogAddr)) != -1)
382240d5a9bSGleb Smirnoff 				status = CONNDEF;
383cf49f439SJohn Polstra 		}
384cf49f439SJohn Polstra 
385240d5a9bSGleb Smirnoff 		if (status == NOCONN) {
3869233c4d9SJason Evans 			(void)_close(LogFile);
3877e6ace85SPeter Wemm 			LogFile = -1;
388cf49f439SJohn Polstra 		}
3897e6ace85SPeter Wemm 	}
3907e6ace85SPeter Wemm }
3917e6ace85SPeter Wemm 
39296575206SGleb Smirnoff static void
393fd42c4d8SStefan Farfeleder openlog_unlocked(const char *ident, int logstat, int logfac)
39458f0484fSRodney W. Grimes {
39558f0484fSRodney W. Grimes 	if (ident != NULL)
39658f0484fSRodney W. Grimes 		LogTag = ident;
39758f0484fSRodney W. Grimes 	LogStat = logstat;
39858f0484fSRodney W. Grimes 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
39958f0484fSRodney W. Grimes 		LogFacility = logfac;
40058f0484fSRodney W. Grimes 
4017e6ace85SPeter Wemm 	if (LogStat & LOG_NDELAY)	/* open immediately */
4027e6ace85SPeter Wemm 		connectlog();
4037e6ace85SPeter Wemm 
4047e6ace85SPeter Wemm 	opened = 1;	/* ident and facility has been set */
40558f0484fSRodney W. Grimes }
40658f0484fSRodney W. Grimes 
40758f0484fSRodney W. Grimes void
408fd42c4d8SStefan Farfeleder openlog(const char *ident, int logstat, int logfac)
40996575206SGleb Smirnoff {
41096575206SGleb Smirnoff 	THREAD_LOCK();
41196575206SGleb Smirnoff 	openlog_unlocked(ident, logstat, logfac);
41296575206SGleb Smirnoff 	THREAD_UNLOCK();
41396575206SGleb Smirnoff }
41496575206SGleb Smirnoff 
41596575206SGleb Smirnoff 
41696575206SGleb Smirnoff void
417fd42c4d8SStefan Farfeleder closelog(void)
41858f0484fSRodney W. Grimes {
41996575206SGleb Smirnoff 	THREAD_LOCK();
4209233c4d9SJason Evans 	(void)_close(LogFile);
42158f0484fSRodney W. Grimes 	LogFile = -1;
422bedff4e8SRuslan Ermilov 	LogTag = NULL;
423240d5a9bSGleb Smirnoff 	status = NOCONN;
42496575206SGleb Smirnoff 	THREAD_UNLOCK();
42558f0484fSRodney W. Grimes }
42658f0484fSRodney W. Grimes 
42758f0484fSRodney W. Grimes /* setlogmask -- set the log mask level */
42858f0484fSRodney W. Grimes int
429fd42c4d8SStefan Farfeleder setlogmask(int pmask)
43058f0484fSRodney W. Grimes {
43158f0484fSRodney W. Grimes 	int omask;
43258f0484fSRodney W. Grimes 
43396575206SGleb Smirnoff 	THREAD_LOCK();
43458f0484fSRodney W. Grimes 	omask = LogMask;
43558f0484fSRodney W. Grimes 	if (pmask != 0)
43658f0484fSRodney W. Grimes 		LogMask = pmask;
43796575206SGleb Smirnoff 	THREAD_UNLOCK();
43858f0484fSRodney W. Grimes 	return (omask);
43958f0484fSRodney W. Grimes }
440