xref: /freebsd/lib/libc/gen/syslog.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1983, 1988, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
32d201fe46SDaniel Eischen #include "namespace.h"
338129693eSEd Schouten #include <sys/param.h>
3458f0484fSRodney W. Grimes #include <sys/socket.h>
3558f0484fSRodney W. Grimes #include <sys/syslog.h>
368129693eSEd Schouten #include <sys/time.h>
3758f0484fSRodney W. Grimes #include <sys/uio.h>
38d584948eSBrian Somers #include <sys/un.h>
3958f0484fSRodney W. Grimes #include <netdb.h>
4058f0484fSRodney W. Grimes 
4158f0484fSRodney W. Grimes #include <errno.h>
4258f0484fSRodney W. Grimes #include <fcntl.h>
4358f0484fSRodney W. Grimes #include <paths.h>
4496575206SGleb Smirnoff #include <pthread.h>
45ddc68905SGleb Smirnoff #include <stdbool.h>
4658f0484fSRodney W. Grimes #include <stdio.h>
474cd01193SMark Murray #include <stdlib.h>
4858f0484fSRodney W. Grimes #include <string.h>
4958f0484fSRodney W. Grimes #include <time.h>
5058f0484fSRodney W. Grimes #include <unistd.h>
5158f0484fSRodney W. Grimes 
5258f0484fSRodney W. Grimes #include <stdarg.h>
53d201fe46SDaniel Eischen #include "un-namespace.h"
5458f0484fSRodney W. Grimes 
554cd01193SMark Murray #include "libc_private.h"
564cd01193SMark Murray 
5788105995SDmitry Wagin /* Maximum number of characters of syslog message */
5888105995SDmitry Wagin #define	MAXLINE		8192
5988105995SDmitry Wagin 
6058f0484fSRodney W. Grimes static int	LogFile = -1;		/* fd for log */
61ddc68905SGleb Smirnoff static bool	connected;		/* have done connect */
627e6ace85SPeter Wemm static int	opened;			/* have done openlog() */
6358f0484fSRodney W. Grimes static int	LogStat = 0;		/* status bits, set by openlog() */
64e9ae9fa9SEugene Grosbein static pid_t	LogPid = -1;		/* process id to tag the entry with */
6558f0484fSRodney W. Grimes static const char *LogTag = NULL;	/* string to tag the entry with */
66e9ae9fa9SEugene Grosbein static int	LogTagLength = -1;	/* usable part of LogTag */
6758f0484fSRodney W. Grimes static int	LogFacility = LOG_USER;	/* default facility code */
6858f0484fSRodney W. Grimes static int	LogMask = 0xff;		/* mask of priorities to be logged */
6996575206SGleb Smirnoff static pthread_mutex_t	syslog_mutex = PTHREAD_MUTEX_INITIALIZER;
7096575206SGleb Smirnoff 
7196575206SGleb Smirnoff #define	THREAD_LOCK()							\
7296575206SGleb Smirnoff 	do { 								\
7396575206SGleb Smirnoff 		if (__isthreaded) _pthread_mutex_lock(&syslog_mutex);	\
7496575206SGleb Smirnoff 	} while(0)
7596575206SGleb Smirnoff #define	THREAD_UNLOCK()							\
7696575206SGleb Smirnoff 	do {								\
7796575206SGleb Smirnoff 		if (__isthreaded) _pthread_mutex_unlock(&syslog_mutex);	\
7896575206SGleb Smirnoff 	} while(0)
7958f0484fSRodney W. Grimes 
802933cd31SBryan Drewery /* RFC5424 defined value. */
812933cd31SBryan Drewery #define NILVALUE "-"
822933cd31SBryan Drewery 
83b231cb39SDavid E. O'Brien static void	disconnectlog(void); /* disconnect from syslogd */
84b231cb39SDavid E. O'Brien static void	connectlog(void);	/* (re)connect to syslogd */
8596575206SGleb Smirnoff static void	openlog_unlocked(const char *, int, int);
86e9ae9fa9SEugene Grosbein static void	parse_tag(void);	/* parse ident[NNN] if needed */
877e6ace85SPeter Wemm 
8858f0484fSRodney W. Grimes /*
897c8e2aa4SPeter Wemm  * Format of the magic cookie passed through the stdio hook
907c8e2aa4SPeter Wemm  */
917c8e2aa4SPeter Wemm struct bufcookie {
927c8e2aa4SPeter Wemm 	char	*base;	/* start of buffer */
937c8e2aa4SPeter Wemm 	int	left;
947c8e2aa4SPeter Wemm };
957c8e2aa4SPeter Wemm 
967c8e2aa4SPeter Wemm /*
977c8e2aa4SPeter Wemm  * stdio write hook for writing to a static string buffer
987c8e2aa4SPeter Wemm  * XXX: Maybe one day, dynamically allocate it so that the line length
997c8e2aa4SPeter Wemm  *      is `unlimited'.
1007c8e2aa4SPeter Wemm  */
101fd42c4d8SStefan Farfeleder static int
writehook(void * cookie,const char * buf,int len)102fd42c4d8SStefan Farfeleder writehook(void *cookie, const char *buf, int len)
1037c8e2aa4SPeter Wemm {
1047c8e2aa4SPeter Wemm 	struct bufcookie *h;	/* private `handle' */
1057c8e2aa4SPeter Wemm 
1067c8e2aa4SPeter Wemm 	h = (struct bufcookie *)cookie;
1077c8e2aa4SPeter Wemm 	if (len > h->left) {
1087c8e2aa4SPeter Wemm 		/* clip in case of wraparound */
1097c8e2aa4SPeter Wemm 		len = h->left;
1107c8e2aa4SPeter Wemm 	}
1117c8e2aa4SPeter Wemm 	if (len > 0) {
1127c8e2aa4SPeter Wemm 		(void)memcpy(h->base, buf, len); /* `write' it. */
1137c8e2aa4SPeter Wemm 		h->base += len;
1147c8e2aa4SPeter Wemm 		h->left -= len;
1157c8e2aa4SPeter Wemm 	}
1165b1deb3cSPoul-Henning Kamp 	return len;
1177c8e2aa4SPeter Wemm }
1187c8e2aa4SPeter Wemm 
1197c8e2aa4SPeter Wemm /*
12058f0484fSRodney W. Grimes  * syslog, vsyslog --
12158f0484fSRodney W. Grimes  *	print message on log file; output is intended for syslogd(8).
12258f0484fSRodney W. Grimes  */
12358f0484fSRodney W. Grimes void
syslog(int pri,const char * fmt,...)12458f0484fSRodney W. Grimes syslog(int pri, const char *fmt, ...)
12558f0484fSRodney W. Grimes {
12658f0484fSRodney W. Grimes 	va_list ap;
12758f0484fSRodney W. Grimes 
12858f0484fSRodney W. Grimes 	va_start(ap, fmt);
12958f0484fSRodney W. Grimes 	vsyslog(pri, fmt, ap);
13058f0484fSRodney W. Grimes 	va_end(ap);
13158f0484fSRodney W. Grimes }
13258f0484fSRodney W. Grimes 
133f3990417SKonstantin Belousov static void
vsyslog1(int pri,const char * fmt,va_list ap)134f3990417SKonstantin Belousov vsyslog1(int pri, const char *fmt, va_list ap)
13558f0484fSRodney W. Grimes {
1368129693eSEd Schouten 	struct timeval now;
1378129693eSEd Schouten 	struct tm tm;
138b231cb39SDavid E. O'Brien 	char ch, *p;
1398129693eSEd Schouten 	long tz_offset;
1408129693eSEd Schouten 	int cnt, fd, saved_errno;
14188105995SDmitry Wagin 	char hostname[MAXHOSTNAMELEN], *stdp, tbuf[MAXLINE], fmt_cpy[MAXLINE],
1428129693eSEd Schouten 	    errstr[64], tz_sign;
1437c8e2aa4SPeter Wemm 	FILE *fp, *fmt_fp;
1447c8e2aa4SPeter Wemm 	struct bufcookie tbuf_cookie;
1457c8e2aa4SPeter Wemm 	struct bufcookie fmt_cookie;
14658f0484fSRodney W. Grimes 
14758f0484fSRodney W. Grimes #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
14858f0484fSRodney W. Grimes 	/* Check for invalid bits. */
14958f0484fSRodney W. Grimes 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
15058f0484fSRodney W. Grimes 		syslog(INTERNALLOG,
15158f0484fSRodney W. Grimes 		    "syslog: unknown facility/priority: %x", pri);
15258f0484fSRodney W. Grimes 		pri &= LOG_PRIMASK|LOG_FACMASK;
15358f0484fSRodney W. Grimes 	}
15458f0484fSRodney W. Grimes 
1553a31b448SDavid Xu 	saved_errno = errno;
1563a31b448SDavid Xu 
15758f0484fSRodney W. Grimes 	/* Check priority against setlogmask values. */
158f3990417SKonstantin Belousov 	if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
15958f0484fSRodney W. Grimes 		return;
16058f0484fSRodney W. Grimes 
16158f0484fSRodney W. Grimes 	/* Set default facility if none specified. */
16258f0484fSRodney W. Grimes 	if ((pri & LOG_FACMASK) == 0)
16358f0484fSRodney W. Grimes 		pri |= LogFacility;
16458f0484fSRodney W. Grimes 
1657c8e2aa4SPeter Wemm 	/* Create the primary stdio hook */
1667c8e2aa4SPeter Wemm 	tbuf_cookie.base = tbuf;
1677c8e2aa4SPeter Wemm 	tbuf_cookie.left = sizeof(tbuf);
1687c8e2aa4SPeter Wemm 	fp = fwopen(&tbuf_cookie, writehook);
169f3990417SKonstantin Belousov 	if (fp == NULL)
1707c8e2aa4SPeter Wemm 		return;
1717c8e2aa4SPeter Wemm 
1728129693eSEd Schouten 	/* Build the message according to RFC 5424. Tag and version. */
1738129693eSEd Schouten 	(void)fprintf(fp, "<%d>1 ", pri);
1748129693eSEd Schouten 	/* Timestamp similar to RFC 3339. */
1758129693eSEd Schouten 	if (gettimeofday(&now, NULL) == 0 &&
1768129693eSEd Schouten 	    localtime_r(&now.tv_sec, &tm) != NULL) {
1778129693eSEd Schouten 		if (tm.tm_gmtoff < 0) {
1788129693eSEd Schouten 			tz_sign = '-';
1798129693eSEd Schouten 			tz_offset = -tm.tm_gmtoff;
1808129693eSEd Schouten 		} else {
1818129693eSEd Schouten 			tz_sign = '+';
1828129693eSEd Schouten 			tz_offset = tm.tm_gmtoff;
1838129693eSEd Schouten 		}
1848129693eSEd Schouten 
1858129693eSEd Schouten 		(void)fprintf(fp,
1868129693eSEd Schouten 		    "%04d-%02d-%02d"		/* Date. */
1878129693eSEd Schouten 		    "T%02d:%02d:%02d.%06ld"	/* Time. */
1888129693eSEd Schouten 		    "%c%02ld:%02ld ",		/* Time zone offset. */
1898129693eSEd Schouten 		    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
1908129693eSEd Schouten 		    tm.tm_hour, tm.tm_min, tm.tm_sec, now.tv_usec,
1918129693eSEd Schouten 		    tz_sign, tz_offset / 3600, (tz_offset % 3600) / 60);
1928129693eSEd Schouten 	} else
1932933cd31SBryan Drewery 		(void)fputs(NILVALUE " ", fp);
1948129693eSEd Schouten 	/* Hostname. */
1958129693eSEd Schouten 	(void)gethostname(hostname, sizeof(hostname));
1962933cd31SBryan Drewery 	(void)fprintf(fp, "%s ",
1972933cd31SBryan Drewery 	    hostname[0] == '\0' ? NILVALUE : hostname);
1987c8e2aa4SPeter Wemm 	if (LogStat & LOG_PERROR) {
1997c8e2aa4SPeter Wemm 		/* Transfer to string buffer */
2007c8e2aa4SPeter Wemm 		(void)fflush(fp);
2017c8e2aa4SPeter Wemm 		stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left);
2027c8e2aa4SPeter Wemm 	}
2032933cd31SBryan Drewery 	/* Application name. */
2042933cd31SBryan Drewery 	if (LogTag == NULL)
2052933cd31SBryan Drewery 		LogTag = _getprogname();
206e9ae9fa9SEugene Grosbein 	else if (LogTagLength == -1)
207e9ae9fa9SEugene Grosbein 		parse_tag();
208e9ae9fa9SEugene Grosbein 	if (LogTagLength > 0)
209e9ae9fa9SEugene Grosbein 		(void)fprintf(fp, "%.*s ", LogTagLength, LogTag);
210e9ae9fa9SEugene Grosbein 	else
2112933cd31SBryan Drewery 		(void)fprintf(fp, "%s ", LogTag == NULL ? NILVALUE : LogTag);
2128129693eSEd Schouten 	/*
2138129693eSEd Schouten 	 * Provide the process ID regardless of whether LOG_PID has been
2148129693eSEd Schouten 	 * specified, as it provides valuable information. Many
2158129693eSEd Schouten 	 * applications tend not to use this, even though they should.
2168129693eSEd Schouten 	 */
217*2ce3ef55SEugene Grosbein 	if (LogTagLength <= 0)
218e9ae9fa9SEugene Grosbein 		LogPid = getpid();
219e9ae9fa9SEugene Grosbein 	(void)fprintf(fp, "%d ", (int)LogPid);
2202933cd31SBryan Drewery 	/* Message ID. */
2212933cd31SBryan Drewery 	(void)fputs(NILVALUE " ", fp);
2222933cd31SBryan Drewery 	/* Structured data. */
2232933cd31SBryan Drewery 	(void)fputs(NILVALUE " ", fp);
2247c8e2aa4SPeter Wemm 
2257c8e2aa4SPeter Wemm 	/* Check to see if we can skip expanding the %m */
2267c8e2aa4SPeter Wemm 	if (strstr(fmt, "%m")) {
2277c8e2aa4SPeter Wemm 
2287c8e2aa4SPeter Wemm 		/* Create the second stdio hook */
2297c8e2aa4SPeter Wemm 		fmt_cookie.base = fmt_cpy;
2307c8e2aa4SPeter Wemm 		fmt_cookie.left = sizeof(fmt_cpy) - 1;
2317c8e2aa4SPeter Wemm 		fmt_fp = fwopen(&fmt_cookie, writehook);
2327c8e2aa4SPeter Wemm 		if (fmt_fp == NULL) {
2337c8e2aa4SPeter Wemm 			fclose(fp);
2347c8e2aa4SPeter Wemm 			return;
23558f0484fSRodney W. Grimes 		}
23658f0484fSRodney W. Grimes 
237e6cfb1ccSAlfred Perlstein 		/*
238e6cfb1ccSAlfred Perlstein 		 * Substitute error message for %m.  Be careful not to
239e6cfb1ccSAlfred Perlstein 		 * molest an escaped percent "%%m".  We want to pass it
240e6cfb1ccSAlfred Perlstein 		 * on untouched as the format is later parsed by vfprintf.
241e6cfb1ccSAlfred Perlstein 		 */
242e6cfb1ccSAlfred Perlstein 		for ( ; (ch = *fmt); ++fmt) {
24358f0484fSRodney W. Grimes 			if (ch == '%' && fmt[1] == 'm') {
24458f0484fSRodney W. Grimes 				++fmt;
24596575206SGleb Smirnoff 				strerror_r(saved_errno, errstr, sizeof(errstr));
24696575206SGleb Smirnoff 				fputs(errstr, fmt_fp);
247e6cfb1ccSAlfred Perlstein 			} else if (ch == '%' && fmt[1] == '%') {
248e6cfb1ccSAlfred Perlstein 				++fmt;
2497c8e2aa4SPeter Wemm 				fputc(ch, fmt_fp);
250e6cfb1ccSAlfred Perlstein 				fputc(ch, fmt_fp);
251e6cfb1ccSAlfred Perlstein 			} else {
252e6cfb1ccSAlfred Perlstein 				fputc(ch, fmt_fp);
253e6cfb1ccSAlfred Perlstein 			}
254e6cfb1ccSAlfred Perlstein 		}
25558f0484fSRodney W. Grimes 
2567c8e2aa4SPeter Wemm 		/* Null terminate if room */
2577c8e2aa4SPeter Wemm 		fputc(0, fmt_fp);
2587c8e2aa4SPeter Wemm 		fclose(fmt_fp);
2597c8e2aa4SPeter Wemm 
2607c8e2aa4SPeter Wemm 		/* Guarantee null termination */
2617c8e2aa4SPeter Wemm 		fmt_cpy[sizeof(fmt_cpy) - 1] = '\0';
2627c8e2aa4SPeter Wemm 
2637c8e2aa4SPeter Wemm 		fmt = fmt_cpy;
2647c8e2aa4SPeter Wemm 	}
2657c8e2aa4SPeter Wemm 
2662933cd31SBryan Drewery 	/* Message. */
2677c8e2aa4SPeter Wemm 	(void)vfprintf(fp, fmt, ap);
2687c8e2aa4SPeter Wemm 	(void)fclose(fp);
2697c8e2aa4SPeter Wemm 
2707c8e2aa4SPeter Wemm 	cnt = sizeof(tbuf) - tbuf_cookie.left;
27158f0484fSRodney W. Grimes 
272857b57eaSDiomidis Spinellis 	/* Remove a trailing newline */
273857b57eaSDiomidis Spinellis 	if (tbuf[cnt - 1] == '\n')
274857b57eaSDiomidis Spinellis 		cnt--;
275857b57eaSDiomidis Spinellis 
27658f0484fSRodney W. Grimes 	/* Output to stderr if requested. */
27758f0484fSRodney W. Grimes 	if (LogStat & LOG_PERROR) {
27858f0484fSRodney W. Grimes 		struct iovec iov[2];
279b231cb39SDavid E. O'Brien 		struct iovec *v = iov;
28058f0484fSRodney W. Grimes 
28158f0484fSRodney W. Grimes 		v->iov_base = stdp;
28258f0484fSRodney W. Grimes 		v->iov_len = cnt - (stdp - tbuf);
28358f0484fSRodney W. Grimes 		++v;
28458f0484fSRodney W. Grimes 		v->iov_base = "\n";
28558f0484fSRodney W. Grimes 		v->iov_len = 1;
286d201fe46SDaniel Eischen 		(void)_writev(STDERR_FILENO, iov, 2);
28758f0484fSRodney W. Grimes 	}
28858f0484fSRodney W. Grimes 
28958f0484fSRodney W. Grimes 	/* Get connected, output the message to the local logger. */
2907e6ace85SPeter Wemm 	if (!opened)
29196575206SGleb Smirnoff 		openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0);
2927e6ace85SPeter Wemm 	connectlog();
2937e6ace85SPeter Wemm 
2947e6ace85SPeter Wemm 	/*
295ddc68905SGleb Smirnoff 	 * If the send() failed, there are two likely scenarios:
296ddc68905SGleb Smirnoff 	 * 1) syslogd was restarted.  In this case make one (only) attempt
297ddc68905SGleb Smirnoff 	 *    to reconnect.
298ddc68905SGleb Smirnoff 	 * 2) We filled our buffer due to syslogd not being able to read
299ddc68905SGleb Smirnoff 	 *    as fast as we write.  In this case prefer to lose the current
300ddc68905SGleb Smirnoff 	 *    message rather than whole buffer of previously logged data.
3017e6ace85SPeter Wemm 	 */
3022e89951bSGleb Smirnoff 	if (send(LogFile, tbuf, cnt, 0) < 0) {
3032e89951bSGleb Smirnoff 		if (errno != ENOBUFS) {
3047e6ace85SPeter Wemm 			disconnectlog();
3057e6ace85SPeter Wemm 			connectlog();
306f3990417SKonstantin Belousov 			if (send(LogFile, tbuf, cnt, 0) >= 0)
30752e05d9aSXin LI 				return;
30896575206SGleb Smirnoff 		}
309f3990417SKonstantin Belousov 	} else
3108ba85e77SGleb Smirnoff 		return;
31158f0484fSRodney W. Grimes 
31258f0484fSRodney W. Grimes 	/*
31394998878SDavid Malone 	 * Output the message to the console; try not to block
31494998878SDavid Malone 	 * as a blocking console should not stop other processes.
31594998878SDavid Malone 	 * Make sure the error reported is the one from the syslogd failure.
31658f0484fSRodney W. Grimes 	 */
31758f0484fSRodney W. Grimes 	if (LogStat & LOG_CONS &&
31805eb11cbSJilles Tjoelker 	    (fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK|O_CLOEXEC, 0)) >=
31905eb11cbSJilles Tjoelker 	    0) {
32011e67a9fSPeter Wemm 		struct iovec iov[2];
321b231cb39SDavid E. O'Brien 		struct iovec *v = iov;
32211e67a9fSPeter Wemm 
3238129693eSEd Schouten 		p = strchr(tbuf, '>') + 3;
32411e67a9fSPeter Wemm 		v->iov_base = p;
32511e67a9fSPeter Wemm 		v->iov_len = cnt - (p - tbuf);
32611e67a9fSPeter Wemm 		++v;
32711e67a9fSPeter Wemm 		v->iov_base = "\r\n";
32811e67a9fSPeter Wemm 		v->iov_len = 2;
329d201fe46SDaniel Eischen 		(void)_writev(fd, iov, 2);
3309233c4d9SJason Evans 		(void)_close(fd);
33158f0484fSRodney W. Grimes 	}
332f3990417SKonstantin Belousov }
333f3990417SKonstantin Belousov 
334f3990417SKonstantin Belousov static void
syslog_cancel_cleanup(void * arg __unused)335f3990417SKonstantin Belousov syslog_cancel_cleanup(void *arg __unused)
336f3990417SKonstantin Belousov {
33796575206SGleb Smirnoff 
33896575206SGleb Smirnoff 	THREAD_UNLOCK();
33958f0484fSRodney W. Grimes }
34096575206SGleb Smirnoff 
341f3990417SKonstantin Belousov void
vsyslog(int pri,const char * fmt,va_list ap)342f3990417SKonstantin Belousov vsyslog(int pri, const char *fmt, va_list ap)
343f3990417SKonstantin Belousov {
344f3990417SKonstantin Belousov 
345f3990417SKonstantin Belousov 	THREAD_LOCK();
346f3990417SKonstantin Belousov 	pthread_cleanup_push(syslog_cancel_cleanup, NULL);
347f3990417SKonstantin Belousov 	vsyslog1(pri, fmt, ap);
348f3990417SKonstantin Belousov 	pthread_cleanup_pop(1);
349f3990417SKonstantin Belousov }
350f3990417SKonstantin Belousov 
35196575206SGleb Smirnoff /* Should be called with mutex acquired */
3527e6ace85SPeter Wemm static void
disconnectlog(void)353fd42c4d8SStefan Farfeleder disconnectlog(void)
3547e6ace85SPeter Wemm {
3557e6ace85SPeter Wemm 	/*
3567e6ace85SPeter Wemm 	 * If the user closed the FD and opened another in the same slot,
3577e6ace85SPeter Wemm 	 * that's their problem.  They should close it before calling on
3587e6ace85SPeter Wemm 	 * system services.
3597e6ace85SPeter Wemm 	 */
3607e6ace85SPeter Wemm 	if (LogFile != -1) {
3619233c4d9SJason Evans 		_close(LogFile);
3627e6ace85SPeter Wemm 		LogFile = -1;
3637e6ace85SPeter Wemm 	}
364ddc68905SGleb Smirnoff 	connected = false;			/* retry connect */
3657e6ace85SPeter Wemm }
3667e6ace85SPeter Wemm 
36796575206SGleb Smirnoff /* Should be called with mutex acquired */
3687e6ace85SPeter Wemm static void
connectlog(void)369fd42c4d8SStefan Farfeleder connectlog(void)
3707e6ace85SPeter Wemm {
371d584948eSBrian Somers 	struct sockaddr_un SyslogAddr;	/* AF_UNIX address of local logger */
372cf49f439SJohn Polstra 
3737e6ace85SPeter Wemm 	if (LogFile == -1) {
37488105995SDmitry Wagin 		socklen_t len;
37588105995SDmitry Wagin 
3760b89df4aSJilles Tjoelker 		if ((LogFile = _socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC,
3770b89df4aSJilles Tjoelker 		    0)) == -1)
3787e6ace85SPeter Wemm 			return;
37988105995SDmitry Wagin 		if (_getsockopt(LogFile, SOL_SOCKET, SO_SNDBUF, &len,
38088105995SDmitry Wagin 		    &(socklen_t){sizeof(len)}) == 0) {
38188105995SDmitry Wagin 			if (len < MAXLINE) {
38288105995SDmitry Wagin 				len = MAXLINE;
38388105995SDmitry Wagin 				(void)_setsockopt(LogFile, SOL_SOCKET, SO_SNDBUF,
38488105995SDmitry Wagin 				    &len, sizeof(len));
38588105995SDmitry Wagin 			}
38688105995SDmitry Wagin 		}
3877e6ace85SPeter Wemm 	}
388ddc68905SGleb Smirnoff 	if (!connected) {
389d584948eSBrian Somers 		SyslogAddr.sun_len = sizeof(SyslogAddr);
390d584948eSBrian Somers 		SyslogAddr.sun_family = AF_UNIX;
391240d5a9bSGleb Smirnoff 
392d584948eSBrian Somers 		(void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
3930b3b961eSBrian Somers 		    sizeof SyslogAddr.sun_path);
394240d5a9bSGleb Smirnoff 		if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
395240d5a9bSGleb Smirnoff 		    sizeof(SyslogAddr)) != -1)
396ddc68905SGleb Smirnoff 			connected = true;
397ddc68905SGleb Smirnoff 		else {
3989233c4d9SJason Evans 			(void)_close(LogFile);
3997e6ace85SPeter Wemm 			LogFile = -1;
400cf49f439SJohn Polstra 		}
4017e6ace85SPeter Wemm 	}
4027e6ace85SPeter Wemm }
4037e6ace85SPeter Wemm 
40496575206SGleb Smirnoff static void
openlog_unlocked(const char * ident,int logstat,int logfac)405fd42c4d8SStefan Farfeleder openlog_unlocked(const char *ident, int logstat, int logfac)
40658f0484fSRodney W. Grimes {
407e9ae9fa9SEugene Grosbein 	if (ident != NULL) {
40858f0484fSRodney W. Grimes 		LogTag = ident;
409e9ae9fa9SEugene Grosbein 		LogTagLength = -1;
410e9ae9fa9SEugene Grosbein 	}
41158f0484fSRodney W. Grimes 	LogStat = logstat;
412e9ae9fa9SEugene Grosbein 	parse_tag();
41358f0484fSRodney W. Grimes 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
41458f0484fSRodney W. Grimes 		LogFacility = logfac;
41558f0484fSRodney W. Grimes 
4167e6ace85SPeter Wemm 	if (LogStat & LOG_NDELAY)	/* open immediately */
4177e6ace85SPeter Wemm 		connectlog();
4187e6ace85SPeter Wemm 
4197e6ace85SPeter Wemm 	opened = 1;	/* ident and facility has been set */
42058f0484fSRodney W. Grimes }
42158f0484fSRodney W. Grimes 
42258f0484fSRodney W. Grimes void
openlog(const char * ident,int logstat,int logfac)423fd42c4d8SStefan Farfeleder openlog(const char *ident, int logstat, int logfac)
42496575206SGleb Smirnoff {
425f3990417SKonstantin Belousov 
42696575206SGleb Smirnoff 	THREAD_LOCK();
427f3990417SKonstantin Belousov 	pthread_cleanup_push(syslog_cancel_cleanup, NULL);
42896575206SGleb Smirnoff 	openlog_unlocked(ident, logstat, logfac);
429f3990417SKonstantin Belousov 	pthread_cleanup_pop(1);
43096575206SGleb Smirnoff }
43196575206SGleb Smirnoff 
43296575206SGleb Smirnoff 
43396575206SGleb Smirnoff void
closelog(void)434fd42c4d8SStefan Farfeleder closelog(void)
43558f0484fSRodney W. Grimes {
43696575206SGleb Smirnoff 	THREAD_LOCK();
437bf36cf8eSEitan Adler 	if (LogFile != -1) {
4389233c4d9SJason Evans 		(void)_close(LogFile);
43958f0484fSRodney W. Grimes 		LogFile = -1;
440bf36cf8eSEitan Adler 	}
441bedff4e8SRuslan Ermilov 	LogTag = NULL;
442e9ae9fa9SEugene Grosbein 	LogTagLength = -1;
443ddc68905SGleb Smirnoff 	connected = false;
44496575206SGleb Smirnoff 	THREAD_UNLOCK();
44558f0484fSRodney W. Grimes }
44658f0484fSRodney W. Grimes 
44758f0484fSRodney W. Grimes /* setlogmask -- set the log mask level */
44858f0484fSRodney W. Grimes int
setlogmask(int pmask)449fd42c4d8SStefan Farfeleder setlogmask(int pmask)
45058f0484fSRodney W. Grimes {
45158f0484fSRodney W. Grimes 	int omask;
45258f0484fSRodney W. Grimes 
45396575206SGleb Smirnoff 	THREAD_LOCK();
45458f0484fSRodney W. Grimes 	omask = LogMask;
45558f0484fSRodney W. Grimes 	if (pmask != 0)
45658f0484fSRodney W. Grimes 		LogMask = pmask;
45796575206SGleb Smirnoff 	THREAD_UNLOCK();
45858f0484fSRodney W. Grimes 	return (omask);
45958f0484fSRodney W. Grimes }
460e9ae9fa9SEugene Grosbein 
461e9ae9fa9SEugene Grosbein /*
4626ab555cfSEugene Grosbein  * Obtain LogPid from LogTag formatted as per RFC 3164,
4636ab555cfSEugene Grosbein  * Section 5.3 Originating Process Information:
4646ab555cfSEugene Grosbein  *
4656ab555cfSEugene Grosbein  * ident[NNN]
466e9ae9fa9SEugene Grosbein  */
467e9ae9fa9SEugene Grosbein static void
parse_tag(void)468e9ae9fa9SEugene Grosbein parse_tag(void)
469e9ae9fa9SEugene Grosbein {
470e9ae9fa9SEugene Grosbein 	char *begin, *end, *p;
471e9ae9fa9SEugene Grosbein 	pid_t pid;
472e9ae9fa9SEugene Grosbein 
473e9ae9fa9SEugene Grosbein 	if (LogTag == NULL || (LogStat & LOG_PID) != 0)
474e9ae9fa9SEugene Grosbein 		return;
475e9ae9fa9SEugene Grosbein 	/*
476e9ae9fa9SEugene Grosbein 	 * LogTagLength is -1 if LogTag was not parsed yet.
477e9ae9fa9SEugene Grosbein 	 * Avoid multiple passes over same LogTag.
478e9ae9fa9SEugene Grosbein 	 */
479e9ae9fa9SEugene Grosbein 	LogTagLength = 0;
480e9ae9fa9SEugene Grosbein 
481e9ae9fa9SEugene Grosbein 	/* Check for presence of opening [ and non-empty ident. */
482e9ae9fa9SEugene Grosbein 	if ((begin = strchr(LogTag, '[')) == NULL || begin == LogTag)
483e9ae9fa9SEugene Grosbein 		return;
484e9ae9fa9SEugene Grosbein 	/* Check for presence of closing ] at the very end and non-empty pid. */
485e9ae9fa9SEugene Grosbein 	if ((end = strchr(begin + 1, ']')) == NULL || end[1] != 0 ||
486e9ae9fa9SEugene Grosbein 	    (end - begin) < 2)
487e9ae9fa9SEugene Grosbein 		return;
488e9ae9fa9SEugene Grosbein 
489e9ae9fa9SEugene Grosbein 	/* Check for pid to contain digits only. */
490e9ae9fa9SEugene Grosbein 	pid = (pid_t)strtol(begin + 1, &p, 10);
491e9ae9fa9SEugene Grosbein 	if (p != end)
492e9ae9fa9SEugene Grosbein 		return;
493e9ae9fa9SEugene Grosbein 
494e9ae9fa9SEugene Grosbein 	LogPid = pid;
495e9ae9fa9SEugene Grosbein 	LogTagLength = begin - LogTag;
496e9ae9fa9SEugene Grosbein }
497