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