Home | History | Annotate | Line # | Download | only in syslogd
syslogd.c revision 1.31
      1 /*
      2  * Copyright (c) 1983, 1988, 1993, 1994
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993, 1994\n\
     37 	The Regents of the University of California.  All rights reserved.\n");
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)syslogd.c	8.3 (Berkeley) 4/4/94";
     43 #else
     44 __RCSID("$NetBSD: syslogd.c,v 1.31 1999/12/06 01:26:26 itojun Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 /*
     49  *  syslogd -- log system messages
     50  *
     51  * This program implements a system log. It takes a series of lines.
     52  * Each line may have a priority, signified as "<n>" as
     53  * the first characters of the line.  If this is
     54  * not present, a default priority is used.
     55  *
     56  * To kill syslogd, send a signal 15 (terminate).  A signal 1 (hup) will
     57  * cause it to reread its configuration file.
     58  *
     59  * Defined Constants:
     60  *
     61  * MAXLINE -- the maximimum line length that can be handled.
     62  * DEFUPRI -- the default priority for user messages
     63  * DEFSPRI -- the default priority for kernel messages
     64  *
     65  * Author: Eric Allman
     66  * extensive changes by Ralph Campbell
     67  * more extensive changes by Eric Allman (again)
     68  */
     69 
     70 #define	MAXLINE		1024		/* maximum line length */
     71 #define	MAXSVLINE	120		/* maximum saved line length */
     72 #define DEFUPRI		(LOG_USER|LOG_NOTICE)
     73 #define DEFSPRI		(LOG_KERN|LOG_CRIT)
     74 #define TIMERINTVL	30		/* interval for checking flush, mark */
     75 #define TTYMSGTIME	1		/* timeout passed to ttymsg */
     76 
     77 #include <sys/param.h>
     78 #include <sys/ioctl.h>
     79 #include <sys/stat.h>
     80 #include <sys/wait.h>
     81 #include <sys/socket.h>
     82 #include <sys/msgbuf.h>
     83 #include <sys/uio.h>
     84 #include <sys/poll.h>
     85 #include <sys/un.h>
     86 #include <sys/time.h>
     87 #include <sys/resource.h>
     88 #include <sys/sysctl.h>
     89 
     90 #include <netinet/in.h>
     91 #include <netdb.h>
     92 #include <arpa/inet.h>
     93 
     94 #include <ctype.h>
     95 #include <errno.h>
     96 #include <fcntl.h>
     97 #include <setjmp.h>
     98 #include <signal.h>
     99 #include <stdio.h>
    100 #include <stdlib.h>
    101 #include <string.h>
    102 #include <unistd.h>
    103 #include <utmp.h>
    104 #include <util.h>
    105 #include "pathnames.h"
    106 
    107 #define SYSLOG_NAMES
    108 #include <sys/syslog.h>
    109 
    110 char	*ConfFile = _PATH_LOGCONF;
    111 char	ctty[] = _PATH_CONSOLE;
    112 
    113 #define FDMASK(fd)	(1 << (fd))
    114 
    115 #define	dprintf		if (Debug) printf
    116 
    117 #define MAXUNAMES	20	/* maximum number of user names */
    118 
    119 /*
    120  * Flags to logmsg().
    121  */
    122 
    123 #define IGN_CONS	0x001	/* don't print on console */
    124 #define SYNC_FILE	0x002	/* do fsync on file after printing */
    125 #define ADDDATE		0x004	/* add a date to the message */
    126 #define MARK		0x008	/* this message is a mark */
    127 
    128 /*
    129  * This structure represents the files that will have log
    130  * copies printed.
    131  */
    132 
    133 struct filed {
    134 	struct	filed *f_next;		/* next in linked list */
    135 	short	f_type;			/* entry type, see below */
    136 	short	f_file;			/* file descriptor */
    137 	time_t	f_time;			/* time this was last written */
    138 	u_char	f_pmask[LOG_NFACILITIES+1];	/* priority mask */
    139 	union {
    140 		char	f_uname[MAXUNAMES][UT_NAMESIZE+1];
    141 		struct {
    142 			char	f_hname[MAXHOSTNAMELEN+1];
    143 			struct	addrinfo *f_addr;
    144 		} f_forw;		/* forwarding address */
    145 		char	f_fname[MAXPATHLEN];
    146 	} f_un;
    147 	char	f_prevline[MAXSVLINE];		/* last message logged */
    148 	char	f_lasttime[16];			/* time of last occurrence */
    149 	char	f_prevhost[MAXHOSTNAMELEN+1];	/* host from which recd. */
    150 	int	f_prevpri;			/* pri of f_prevline */
    151 	int	f_prevlen;			/* length of f_prevline */
    152 	int	f_prevcount;			/* repetition cnt of prevline */
    153 	int	f_repeatcount;			/* number of "repeated" msgs */
    154 };
    155 
    156 /*
    157  * Intervals at which we flush out "message repeated" messages,
    158  * in seconds after previous message is logged.  After each flush,
    159  * we move to the next interval until we reach the largest.
    160  */
    161 int	repeatinterval[] = { 30, 120, 600 };	/* # of secs before flush */
    162 #define	MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
    163 #define	REPEATTIME(f)	((f)->f_time + repeatinterval[(f)->f_repeatcount])
    164 #define	BACKOFF(f)	{ if (++(f)->f_repeatcount > MAXREPEAT) \
    165 				 (f)->f_repeatcount = MAXREPEAT; \
    166 			}
    167 
    168 /* values for f_type */
    169 #define F_UNUSED	0		/* unused entry */
    170 #define F_FILE		1		/* regular file */
    171 #define F_TTY		2		/* terminal */
    172 #define F_CONSOLE	3		/* console terminal */
    173 #define F_FORW		4		/* remote machine */
    174 #define F_USERS		5		/* list of users */
    175 #define F_WALL		6		/* everyone logged on */
    176 
    177 char	*TypeNames[7] = {
    178 	"UNUSED",	"FILE",		"TTY",		"CONSOLE",
    179 	"FORW",		"USERS",	"WALL"
    180 };
    181 
    182 struct	filed *Files;
    183 struct	filed consfile;
    184 
    185 int	Debug;			/* debug flag */
    186 char	LocalHostName[MAXHOSTNAMELEN+1];	/* our hostname */
    187 char	*LocalDomain;		/* our local domain name */
    188 int	InetInuse = 0;		/* non-zero if INET sockets are being used */
    189 int	*finet;			/* Internet datagram socket */
    190 int	Initialized = 0;	/* set when we have initialized ourselves */
    191 int	MarkInterval = 20 * 60;	/* interval between marks in seconds */
    192 int	MarkSeq = 0;		/* mark sequence number */
    193 int	SecureMode = 0;		/* when true, speak only unix domain socks */
    194 char	**LogPaths;		/* array of pathnames to read messages from */
    195 
    196 void	cfline __P((char *, struct filed *));
    197 char   *cvthname __P((struct sockaddr_storage *));
    198 int	decode __P((const char *, CODE *));
    199 void	die __P((int));
    200 void	domark __P((int));
    201 void	fprintlog __P((struct filed *, int, char *));
    202 int	getmsgbufsize __P((void));
    203 int*	socksetup __P((int));
    204 void	init __P((int));
    205 void	logerror __P((char *));
    206 void	logmsg __P((int, char *, char *, int));
    207 void	printline __P((char *, char *));
    208 void	printsys __P((char *));
    209 void	reapchild __P((int));
    210 void	usage __P((void));
    211 void	wallmsg __P((struct filed *, struct iovec *));
    212 int	main __P((int, char *[]));
    213 void	logpath_add __P((char ***, int *, int *, char *));
    214 void	logpath_fileadd __P((char ***, int *, int *, char *));
    215 
    216 int
    217 main(argc, argv)
    218 	int argc;
    219 	char *argv[];
    220 {
    221 	int ch, *funix, i, j, fklog, len, linesize;
    222 	int *nfinetix, nfklogix, nfunixbaseix, nfds;
    223 	int funixsize = 0, funixmaxsize = 0;
    224 	struct sockaddr_un sunx, fromunix;
    225 	struct sockaddr_storage frominet;
    226 	char *p, *line, **pp;
    227 	struct pollfd *readfds;
    228 
    229 	while ((ch = getopt(argc, argv, "dsf:m:p:P:")) != -1)
    230 		switch(ch) {
    231 		case 'd':		/* debug */
    232 			Debug++;
    233 			break;
    234 		case 'f':		/* configuration file */
    235 			ConfFile = optarg;
    236 			break;
    237 		case 'm':		/* mark interval */
    238 			MarkInterval = atoi(optarg) * 60;
    239 			break;
    240 		case 'p':		/* path */
    241 			logpath_add(&LogPaths, &funixsize,
    242 			    &funixmaxsize, optarg);
    243 			break;
    244 		case 'P':		/* file of paths */
    245 			logpath_fileadd(&LogPaths, &funixsize,
    246 			    &funixmaxsize, optarg);
    247 			break;
    248 		case 's':		/* no network mode */
    249 			SecureMode++;
    250 			break;
    251 		case '?':
    252 		default:
    253 			usage();
    254 		}
    255 	if ((argc -= optind) != 0)
    256 		usage();
    257 
    258 	if (!Debug)
    259 		(void)daemon(0, 0);
    260 	else
    261 		setlinebuf(stdout);
    262 
    263 	consfile.f_type = F_CONSOLE;
    264 	(void)strcpy(consfile.f_un.f_fname, ctty);
    265 	(void)gethostname(LocalHostName, sizeof(LocalHostName));
    266 	LocalHostName[sizeof(LocalHostName) - 1] = '\0';
    267 	if ((p = strchr(LocalHostName, '.')) != NULL) {
    268 		*p++ = '\0';
    269 		LocalDomain = p;
    270 	} else
    271 		LocalDomain = "";
    272 	linesize = getmsgbufsize();
    273 	if (linesize < MAXLINE)
    274 		linesize = MAXLINE;
    275 	linesize++;
    276 	line = malloc(linesize);
    277 	if (line == NULL) {
    278 		logerror("couldn't allocate line buffer");
    279 		die(0);
    280 	}
    281 	(void)signal(SIGTERM, die);
    282 	(void)signal(SIGINT, Debug ? die : SIG_IGN);
    283 	(void)signal(SIGQUIT, Debug ? die : SIG_IGN);
    284 	(void)signal(SIGCHLD, reapchild);
    285 	(void)signal(SIGALRM, domark);
    286 	(void)alarm(TIMERINTVL);
    287 
    288 #ifndef SUN_LEN
    289 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
    290 #endif
    291 	if (funixsize == 0)
    292 		logpath_add(&LogPaths, &funixsize,
    293 		    &funixmaxsize, _PATH_LOG);
    294 	funix = (int *)malloc(sizeof(int) * funixsize);
    295 	if (funix == NULL) {
    296 		logerror("couldn't allocate funix descriptors");
    297 		die(0);
    298 	}
    299 	for (j = 0, pp = LogPaths; *pp; pp++, j++) {
    300 		dprintf("making unix dgram socket %s\n", *pp);
    301 		unlink(*pp);
    302 		memset(&sunx, 0, sizeof(sunx));
    303 		sunx.sun_family = AF_LOCAL;
    304 		(void)strncpy(sunx.sun_path, *pp, sizeof(sunx.sun_path));
    305 		funix[j] = socket(AF_LOCAL, SOCK_DGRAM, 0);
    306 		if (funix[j] < 0 || bind(funix[j],
    307 		    (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 ||
    308 		    chmod(*pp, 0666) < 0) {
    309 			int serrno = errno;
    310 			(void)snprintf(line, sizeof line,
    311 			    "cannot create %s", *pp);
    312 			errno = serrno;
    313 			logerror(line);
    314 			errno = serrno;
    315 			dprintf("cannot create %s (%d)\n", *pp, errno);
    316 			die(0);
    317 		}
    318 		dprintf("listening on unix dgram socket %s\n", *pp);
    319 	}
    320 
    321 	if (!SecureMode)
    322 		finet = socksetup(PF_UNSPEC);
    323 	else
    324 		finet = NULL;
    325 
    326 	if (finet && *finet) {
    327 		dprintf("listening on inet and/or inet6 socket\n");
    328 		InetInuse = 1;
    329 	}
    330 
    331 	if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) < 0) {
    332 		dprintf("can't open %s (%d)\n", _PATH_KLOG, errno);
    333 	} else {
    334 		dprintf("listening on kernel log %s\n", _PATH_KLOG);
    335 	}
    336 
    337 	/* tuck my process id away, if i'm not in debug mode */
    338 	if (Debug == 0)
    339 		pidfile(NULL);
    340 
    341 	dprintf("off & running....\n");
    342 
    343 	init(0);
    344 	(void)signal(SIGHUP, init);
    345 
    346 	/* setup pollfd set. */
    347 	readfds = (struct pollfd *)malloc(sizeof(struct pollfd) *
    348 			(funixsize + (finet ? *finet : 0) + 1));
    349 	if (readfds == NULL) {
    350 		logerror("couldn't allocate pollfds");
    351 		die(0);
    352 	}
    353 	nfds = 0;
    354 	if (fklog >= 0) {
    355 		nfklogix = nfds++;
    356 		readfds[nfklogix].fd = fklog;
    357 		readfds[nfklogix].events = POLLIN | POLLPRI;
    358 	}
    359 	if (finet) {
    360 		nfinetix = malloc(*finet * sizeof(*nfinetix));
    361 		for (j = 0; j < *finet; j++) {
    362 			nfinetix[j] = nfds++;
    363 			readfds[nfinetix[j]].fd = finet[j+1];
    364 			readfds[nfinetix[j]].events = POLLIN | POLLPRI;
    365 		}
    366 	}
    367 	nfunixbaseix = nfds;
    368 	for (j = 0, pp = LogPaths; *pp; pp++) {
    369 		readfds[nfds].fd = funix[j++];
    370 		readfds[nfds++].events = POLLIN | POLLPRI;
    371 	}
    372 
    373 	for (;;) {
    374 		int rv;
    375 
    376 		rv = poll(readfds, nfds, INFTIM);
    377 		if (rv == 0)
    378 			continue;
    379 		if (rv < 0) {
    380 			if (errno != EINTR)
    381 				logerror("poll");
    382 			continue;
    383 		}
    384 		dprintf("got a message (%d)\n", rv);
    385 		if (fklog >= 0 &&
    386 		    (readfds[nfklogix].revents & (POLLIN | POLLPRI))) {
    387 			dprintf("kernel log active\n");
    388 			i = read(fklog, line, linesize - 1);
    389 			if (i > 0) {
    390 				line[i] = '\0';
    391 				printsys(line);
    392 			} else if (i < 0 && errno != EINTR) {
    393 				logerror("klog");
    394 				fklog = -1;
    395 			}
    396 		}
    397 		for (j = 0, pp = LogPaths; *pp; pp++, j++) {
    398 			if ((readfds[nfunixbaseix + j].revents &
    399 			    (POLLIN | POLLPRI)) == 0)
    400 				continue;
    401 
    402 			dprintf("unix socket (%s) active\n", *pp);
    403 			len = sizeof(fromunix);
    404 			i = recvfrom(funix[j], line, MAXLINE, 0,
    405 			    (struct sockaddr *)&fromunix, &len);
    406 			if (i > 0) {
    407 				line[i] = '\0';
    408 				printline(LocalHostName, line);
    409 			} else if (i < 0 && errno != EINTR) {
    410 				char buf[MAXPATHLEN];
    411 				int serrno = errno;
    412 
    413 				(void)snprintf(buf, sizeof buf,
    414 				    "recvfrom unix %s", *pp);
    415 				errno = serrno;
    416 				logerror(buf);
    417 			}
    418 		}
    419 		if (finet) {
    420 			for (j = 0; j < *finet; j++) {
    421 		    		if (readfds[nfinetix[j]].revents & (POLLIN | POLLPRI)) {
    422 					dprintf("inet socket active\n");
    423 					len = sizeof(frominet);
    424 					i = recvfrom(finet[j+1], line, MAXLINE, 0,
    425 			    				(struct sockaddr *)&frominet, &len);
    426 					if (i > 0) {
    427 						line[i] = '\0';
    428 						printline(cvthname(&frominet), line);
    429 					} else if (i < 0 && errno != EINTR)
    430 						logerror("recvfrom inet");
    431 				}
    432 			}
    433 		}
    434 	}
    435 }
    436 
    437 void
    438 usage()
    439 {
    440 
    441 	(void)fprintf(stderr,
    442 	    "usage: syslogd [-f conffile] [-m markinterval] [-p logpath1] [-p logpath2 ..]\n");
    443 	exit(1);
    444 }
    445 
    446 /*
    447  * given a pointer to an array of char *'s, a pointer to it's current
    448  * size and current allocated max size, and a new char * to add, add
    449  * it, update everything as necessary, possibly allocating a new array
    450  */
    451 void
    452 logpath_add(lp, szp, maxszp, new)
    453 	char ***lp;
    454 	int *szp;
    455 	int *maxszp;
    456 	char *new;
    457 {
    458 
    459 	dprintf("adding %s to the %p logpath list\n", new, *lp);
    460 	if (*szp == *maxszp) {
    461 		if (*maxszp == 0) {
    462 			*maxszp = 4;	/* start of with enough for now */
    463 			*lp = NULL;
    464 		}
    465 		else
    466 			*maxszp *= 2;
    467 		*lp = realloc(*lp, sizeof(char *) * (*maxszp + 1));
    468 		if (*lp == NULL) {
    469 			logerror("couldn't allocate line buffer");
    470 			die(0);
    471 		}
    472 	}
    473 	(*lp)[(*szp)++] = new;
    474 	(*lp)[(*szp)] = NULL;		/* always keep it NULL terminated */
    475 }
    476 
    477 /* do a file of log sockets */
    478 void
    479 logpath_fileadd(lp, szp, maxszp, file)
    480 	char ***lp;
    481 	int *szp;
    482 	int *maxszp;
    483 	char *file;
    484 {
    485 	FILE *fp;
    486 	char *line;
    487 	size_t len;
    488 
    489 	fp = fopen(file, "r");
    490 	if (fp == NULL) {
    491 		int serrno = errno;
    492 
    493 		dprintf("can't open %s (%d)\n", file, errno);
    494 		errno = serrno;
    495 		logerror("could not open socket file list");
    496 		die(0);
    497 	}
    498 
    499 	while ((line = fgetln(fp, &len))) {
    500 		line[len - 1] = 0;
    501 		logpath_add(lp, szp, maxszp, line);
    502 	}
    503 	fclose(fp);
    504 }
    505 
    506 /*
    507  * Take a raw input line, decode the message, and print the message
    508  * on the appropriate log files.
    509  */
    510 void
    511 printline(hname, msg)
    512 	char *hname;
    513 	char *msg;
    514 {
    515 	int c, pri;
    516 	char *p, *q, line[MAXLINE + 1];
    517 
    518 	/* test for special codes */
    519 	pri = DEFUPRI;
    520 	p = msg;
    521 	if (*p == '<') {
    522 		pri = 0;
    523 		while (isdigit(*++p))
    524 			pri = 10 * pri + (*p - '0');
    525 		if (*p == '>')
    526 			++p;
    527 	}
    528 	if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
    529 		pri = DEFUPRI;
    530 
    531 	/* don't allow users to log kernel messages */
    532 	if (LOG_FAC(pri) == LOG_KERN)
    533 		pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
    534 
    535 	q = line;
    536 
    537 	while ((c = *p++ & 0177) != '\0' &&
    538 	    q < &line[sizeof(line) - 1])
    539 		if (iscntrl(c))
    540 			if (c == '\n')
    541 				*q++ = ' ';
    542 			else if (c == '\t')
    543 				*q++ = '\t';
    544 			else {
    545 				*q++ = '^';
    546 				*q++ = c ^ 0100;
    547 			}
    548 		else
    549 			*q++ = c;
    550 	*q = '\0';
    551 
    552 	logmsg(pri, line, hname, 0);
    553 }
    554 
    555 /*
    556  * Take a raw input line from /dev/klog, split and format similar to syslog().
    557  */
    558 void
    559 printsys(msg)
    560 	char *msg;
    561 {
    562 	int c, pri, flags;
    563 	char *lp, *p, *q, line[MAXLINE + 1];
    564 
    565 	(void)strcpy(line, _PATH_UNIX);
    566 	(void)strcat(line, ": ");
    567 	lp = line + strlen(line);
    568 	for (p = msg; *p != '\0'; ) {
    569 		flags = SYNC_FILE | ADDDATE;	/* fsync file after write */
    570 		pri = DEFSPRI;
    571 		if (*p == '<') {
    572 			pri = 0;
    573 			while (isdigit(*++p))
    574 				pri = 10 * pri + (*p - '0');
    575 			if (*p == '>')
    576 				++p;
    577 		} else {
    578 			/* kernel printf's come out on console */
    579 			flags |= IGN_CONS;
    580 		}
    581 		if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
    582 			pri = DEFSPRI;
    583 		q = lp;
    584 		while (*p != '\0' && (c = *p++) != '\n' &&
    585 		    q < &line[MAXLINE])
    586 			*q++ = c;
    587 		*q = '\0';
    588 		logmsg(pri, line, LocalHostName, flags);
    589 	}
    590 }
    591 
    592 time_t	now;
    593 
    594 /*
    595  * Log a message to the appropriate log files, users, etc. based on
    596  * the priority.
    597  */
    598 void
    599 logmsg(pri, msg, from, flags)
    600 	int pri;
    601 	char *msg, *from;
    602 	int flags;
    603 {
    604 	struct filed *f;
    605 	int fac, msglen, omask, prilev;
    606 	char *timestamp;
    607 
    608 	dprintf("logmsg: pri 0%o, flags 0x%x, from %s, msg %s\n",
    609 	    pri, flags, from, msg);
    610 
    611 	omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
    612 
    613 	/*
    614 	 * Check to see if msg looks non-standard.
    615 	 */
    616 	msglen = strlen(msg);
    617 	if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
    618 	    msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
    619 		flags |= ADDDATE;
    620 
    621 	(void)time(&now);
    622 	if (flags & ADDDATE)
    623 		timestamp = ctime(&now) + 4;
    624 	else {
    625 		timestamp = msg;
    626 		msg += 16;
    627 		msglen -= 16;
    628 	}
    629 
    630 	/* extract facility and priority level */
    631 	if (flags & MARK)
    632 		fac = LOG_NFACILITIES;
    633 	else
    634 		fac = LOG_FAC(pri);
    635 	prilev = LOG_PRI(pri);
    636 
    637 	/* log the message to the particular outputs */
    638 	if (!Initialized) {
    639 		f = &consfile;
    640 		f->f_file = open(ctty, O_WRONLY, 0);
    641 
    642 		if (f->f_file >= 0) {
    643 			fprintlog(f, flags, msg);
    644 			(void)close(f->f_file);
    645 		}
    646 		(void)sigsetmask(omask);
    647 		return;
    648 	}
    649 	for (f = Files; f; f = f->f_next) {
    650 		/* skip messages that are incorrect priority */
    651 		if (f->f_pmask[fac] < prilev ||
    652 		    f->f_pmask[fac] == INTERNAL_NOPRI)
    653 			continue;
    654 
    655 		if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
    656 			continue;
    657 
    658 		/* don't output marks to recently written files */
    659 		if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
    660 			continue;
    661 
    662 		/*
    663 		 * suppress duplicate lines to this file
    664 		 */
    665 		if ((flags & MARK) == 0 && msglen == f->f_prevlen &&
    666 		    !strcmp(msg, f->f_prevline) &&
    667 		    !strcmp(from, f->f_prevhost)) {
    668 			(void)strncpy(f->f_lasttime, timestamp, 15);
    669 			f->f_prevcount++;
    670 			dprintf("msg repeated %d times, %ld sec of %d\n",
    671 			    f->f_prevcount, (long)(now - f->f_time),
    672 			    repeatinterval[f->f_repeatcount]);
    673 			/*
    674 			 * If domark would have logged this by now,
    675 			 * flush it now (so we don't hold isolated messages),
    676 			 * but back off so we'll flush less often
    677 			 * in the future.
    678 			 */
    679 			if (now > REPEATTIME(f)) {
    680 				fprintlog(f, flags, (char *)NULL);
    681 				BACKOFF(f);
    682 			}
    683 		} else {
    684 			/* new line, save it */
    685 			if (f->f_prevcount)
    686 				fprintlog(f, 0, (char *)NULL);
    687 			f->f_repeatcount = 0;
    688 			f->f_prevpri = pri;
    689 			(void)strncpy(f->f_lasttime, timestamp, 15);
    690 			(void)strncpy(f->f_prevhost, from,
    691 					sizeof(f->f_prevhost));
    692 			if (msglen < MAXSVLINE) {
    693 				f->f_prevlen = msglen;
    694 				(void)strcpy(f->f_prevline, msg);
    695 				fprintlog(f, flags, (char *)NULL);
    696 			} else {
    697 				f->f_prevline[0] = 0;
    698 				f->f_prevlen = 0;
    699 				fprintlog(f, flags, msg);
    700 			}
    701 		}
    702 	}
    703 	(void)sigsetmask(omask);
    704 }
    705 
    706 void
    707 fprintlog(f, flags, msg)
    708 	struct filed *f;
    709 	int flags;
    710 	char *msg;
    711 {
    712 	struct iovec iov[6];
    713 	struct iovec *v;
    714 	struct addrinfo *r;
    715 	int j, l, lsent;
    716 	char line[MAXLINE + 1], repbuf[80], greetings[200];
    717 
    718 	v = iov;
    719 	if (f->f_type == F_WALL) {
    720 		v->iov_base = greetings;
    721 		v->iov_len = snprintf(greetings, sizeof greetings,
    722 		    "\r\n\7Message from syslogd@%s at %.24s ...\r\n",
    723 		    f->f_prevhost, ctime(&now));
    724 		v++;
    725 		v->iov_base = "";
    726 		v->iov_len = 0;
    727 		v++;
    728 	} else {
    729 		v->iov_base = f->f_lasttime;
    730 		v->iov_len = 15;
    731 		v++;
    732 		v->iov_base = " ";
    733 		v->iov_len = 1;
    734 		v++;
    735 	}
    736 	v->iov_base = f->f_prevhost;
    737 	v->iov_len = strlen(v->iov_base);
    738 	v++;
    739 	v->iov_base = " ";
    740 	v->iov_len = 1;
    741 	v++;
    742 
    743 	if (msg) {
    744 		v->iov_base = msg;
    745 		v->iov_len = strlen(msg);
    746 	} else if (f->f_prevcount > 1) {
    747 		v->iov_base = repbuf;
    748 		v->iov_len = snprintf(repbuf, sizeof repbuf,
    749 		    "last message repeated %d times", f->f_prevcount);
    750 	} else {
    751 		v->iov_base = f->f_prevline;
    752 		v->iov_len = f->f_prevlen;
    753 	}
    754 	v++;
    755 
    756 	dprintf("Logging to %s", TypeNames[f->f_type]);
    757 	f->f_time = now;
    758 
    759 	switch (f->f_type) {
    760 	case F_UNUSED:
    761 		dprintf("\n");
    762 		break;
    763 
    764 	case F_FORW:
    765 		dprintf(" %s\n", f->f_un.f_forw.f_hname);
    766 		/* check for local vs remote messages (from FreeBSD PR#bin/7055) */
    767 		if (strcmp(f->f_prevhost, LocalHostName)) {
    768 			l = snprintf(line, sizeof(line) - 1,
    769 				     "<%d>%.15s [%s]: %s",
    770 				     f->f_prevpri, (char *) iov[0].iov_base,
    771 				     f->f_prevhost, (char *) iov[4].iov_base);
    772 		} else {
    773 			l = snprintf(line, sizeof(line) - 1, "<%d>%.15s %s",
    774 				     f->f_prevpri, (char *) iov[0].iov_base,
    775 				     (char *) iov[4].iov_base);
    776 		}
    777 		if (l > MAXLINE)
    778 			l = MAXLINE;
    779 		if (finet && *finet) {
    780 			for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) {
    781 				for (j = 0; j < *finet; j++) {
    782 #if 0
    783 					/* should we check AF first, or just trial and error? FWD */
    784 					if (r->ai_family == address_family_of(finet[j+1]))
    785 #endif
    786 					lsent = sendto(finet[j+1], line, l, 0, r->ai_addr, r->ai_addrlen);
    787 					if (lsent == l)
    788 						break;
    789 				}
    790 			}
    791 			if (lsent != l) {
    792 				f->f_type = F_UNUSED;
    793 				logerror("sendto");
    794 			}
    795 		}
    796 		break;
    797 
    798 	case F_CONSOLE:
    799 		if (flags & IGN_CONS) {
    800 			dprintf(" (ignored)\n");
    801 			break;
    802 		}
    803 		/* FALLTHROUGH */
    804 
    805 	case F_TTY:
    806 	case F_FILE:
    807 		dprintf(" %s\n", f->f_un.f_fname);
    808 		if (f->f_type != F_FILE) {
    809 			v->iov_base = "\r\n";
    810 			v->iov_len = 2;
    811 		} else {
    812 			v->iov_base = "\n";
    813 			v->iov_len = 1;
    814 		}
    815 	again:
    816 		if (writev(f->f_file, iov, 6) < 0) {
    817 			int e = errno;
    818 			(void)close(f->f_file);
    819 			/*
    820 			 * Check for errors on TTY's due to loss of tty
    821 			 */
    822 			if ((e == EIO || e == EBADF) && f->f_type != F_FILE) {
    823 				f->f_file = open(f->f_un.f_fname,
    824 				    O_WRONLY|O_APPEND, 0);
    825 				if (f->f_file < 0) {
    826 					f->f_type = F_UNUSED;
    827 					logerror(f->f_un.f_fname);
    828 				} else
    829 					goto again;
    830 			} else {
    831 				f->f_type = F_UNUSED;
    832 				errno = e;
    833 				logerror(f->f_un.f_fname);
    834 			}
    835 		} else if (flags & SYNC_FILE)
    836 			(void)fsync(f->f_file);
    837 		break;
    838 
    839 	case F_USERS:
    840 	case F_WALL:
    841 		dprintf("\n");
    842 		v->iov_base = "\r\n";
    843 		v->iov_len = 2;
    844 		wallmsg(f, iov);
    845 		break;
    846 	}
    847 	f->f_prevcount = 0;
    848 }
    849 
    850 /*
    851  *  WALLMSG -- Write a message to the world at large
    852  *
    853  *	Write the specified message to either the entire
    854  *	world, or a list of approved users.
    855  */
    856 void
    857 wallmsg(f, iov)
    858 	struct filed *f;
    859 	struct iovec *iov;
    860 {
    861 	static int reenter;			/* avoid calling ourselves */
    862 	FILE *uf;
    863 	struct utmp ut;
    864 	int i;
    865 	char *p;
    866 	char line[sizeof(ut.ut_line) + 1];
    867 
    868 	if (reenter++)
    869 		return;
    870 	if ((uf = fopen(_PATH_UTMP, "r")) == NULL) {
    871 		logerror(_PATH_UTMP);
    872 		reenter = 0;
    873 		return;
    874 	}
    875 	/* NOSTRICT */
    876 	while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) {
    877 		if (ut.ut_name[0] == '\0')
    878 			continue;
    879 		strncpy(line, ut.ut_line, sizeof(ut.ut_line));
    880 		line[sizeof(ut.ut_line)] = '\0';
    881 		if (f->f_type == F_WALL) {
    882 			if ((p = ttymsg(iov, 6, line, TTYMSGTIME)) != NULL) {
    883 				errno = 0;	/* already in msg */
    884 				logerror(p);
    885 			}
    886 			continue;
    887 		}
    888 		/* should we send the message to this user? */
    889 		for (i = 0; i < MAXUNAMES; i++) {
    890 			if (!f->f_un.f_uname[i][0])
    891 				break;
    892 			if (!strncmp(f->f_un.f_uname[i], ut.ut_name,
    893 			    UT_NAMESIZE)) {
    894 				if ((p = ttymsg(iov, 6, line, TTYMSGTIME))
    895 								!= NULL) {
    896 					errno = 0;	/* already in msg */
    897 					logerror(p);
    898 				}
    899 				break;
    900 			}
    901 		}
    902 	}
    903 	(void)fclose(uf);
    904 	reenter = 0;
    905 }
    906 
    907 void
    908 reapchild(signo)
    909 	int signo;
    910 {
    911 	union wait status;
    912 
    913 	while (wait3((int *)&status, WNOHANG, (struct rusage *)NULL) > 0)
    914 		;
    915 }
    916 
    917 /*
    918  * Return a printable representation of a host address.
    919  */
    920 char *
    921 cvthname(f)
    922 	struct sockaddr_storage *f;
    923 {
    924 	int error;
    925 	char *p;
    926 #ifdef KAME_SCOPEID
    927 	const int niflag = NI_DGRAM | NI_WITHSCOPEID;
    928 #else
    929 	const int niflag = NI_DGRAM;
    930 #endif
    931 	static char host[NI_MAXHOST], ip[NI_MAXHOST];
    932 
    933 	error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len,
    934 			ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST|niflag);
    935 
    936 	dprintf("cvthname(%s)\n", ip);
    937 
    938 	if (error) {
    939 		dprintf("Malformed from address %s\n", gai_strerror(error));
    940 		return ("???");
    941 	}
    942 
    943 	error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len,
    944 			host, NI_MAXHOST, NULL, 0, niflag);
    945 	if (error) {
    946 		dprintf("Host name for your address (%s) unknown\n", ip);
    947 		return (ip);
    948 	}
    949 	if ((p = strchr(host, '.')) && strcmp(p + 1, LocalDomain) == 0)
    950 		*p = '\0';
    951 	return (host);
    952 }
    953 
    954 void
    955 domark(signo)
    956 	int signo;
    957 {
    958 	struct filed *f;
    959 
    960 	now = time((time_t *)NULL);
    961 	MarkSeq += TIMERINTVL;
    962 	if (MarkSeq >= MarkInterval) {
    963 		logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
    964 		MarkSeq = 0;
    965 	}
    966 
    967 	for (f = Files; f; f = f->f_next) {
    968 		if (f->f_prevcount && now >= REPEATTIME(f)) {
    969 			dprintf("flush %s: repeated %d times, %d sec.\n",
    970 			    TypeNames[f->f_type], f->f_prevcount,
    971 			    repeatinterval[f->f_repeatcount]);
    972 			fprintlog(f, 0, (char *)NULL);
    973 			BACKOFF(f);
    974 		}
    975 	}
    976 	(void)alarm(TIMERINTVL);
    977 }
    978 
    979 /*
    980  * Print syslogd errors some place.
    981  */
    982 void
    983 logerror(type)
    984 	char *type;
    985 {
    986 	char buf[100];
    987 
    988 	if (errno)
    989 		(void)snprintf(buf,
    990 		    sizeof(buf), "syslogd: %s: %s", type, strerror(errno));
    991 	else
    992 		(void)snprintf(buf, sizeof(buf), "syslogd: %s", type);
    993 	errno = 0;
    994 	dprintf("%s\n", buf);
    995 	logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
    996 }
    997 
    998 void
    999 die(signo)
   1000 	int signo;
   1001 {
   1002 	struct filed *f;
   1003 	char buf[100], **p;
   1004 
   1005 	for (f = Files; f != NULL; f = f->f_next) {
   1006 		/* flush any pending output */
   1007 		if (f->f_prevcount)
   1008 			fprintlog(f, 0, (char *)NULL);
   1009 	}
   1010 	if (signo) {
   1011 		dprintf("syslogd: exiting on signal %d\n", signo);
   1012 		(void)snprintf(buf, sizeof buf, "exiting on signal %d", signo);
   1013 		errno = 0;
   1014 		logerror(buf);
   1015 	}
   1016 	for (p = LogPaths; p && *p; p++)
   1017 		unlink(*p);
   1018 	exit(0);
   1019 }
   1020 
   1021 /*
   1022  *  INIT -- Initialize syslogd from configuration table
   1023  */
   1024 void
   1025 init(signo)
   1026 	int signo;
   1027 {
   1028 	int i;
   1029 	FILE *cf;
   1030 	struct filed *f, *next, **nextp;
   1031 	char *p;
   1032 	char cline[LINE_MAX];
   1033 
   1034 	dprintf("init\n");
   1035 
   1036 	/*
   1037 	 *  Close all open log files.
   1038 	 */
   1039 	Initialized = 0;
   1040 	for (f = Files; f != NULL; f = next) {
   1041 		/* flush any pending output */
   1042 		if (f->f_prevcount)
   1043 			fprintlog(f, 0, (char *)NULL);
   1044 
   1045 		switch (f->f_type) {
   1046 		case F_FILE:
   1047 		case F_TTY:
   1048 		case F_CONSOLE:
   1049 			(void)close(f->f_file);
   1050 			break;
   1051 		}
   1052 		next = f->f_next;
   1053 		free((char *)f);
   1054 	}
   1055 	Files = NULL;
   1056 	nextp = &Files;
   1057 
   1058 	/* open the configuration file */
   1059 	if ((cf = fopen(ConfFile, "r")) == NULL) {
   1060 		dprintf("cannot open %s\n", ConfFile);
   1061 		*nextp = (struct filed *)calloc(1, sizeof(*f));
   1062 		cfline("*.ERR\t/dev/console", *nextp);
   1063 		(*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
   1064 		cfline("*.PANIC\t*", (*nextp)->f_next);
   1065 		Initialized = 1;
   1066 		return;
   1067 	}
   1068 
   1069 	/*
   1070 	 *  Foreach line in the conf table, open that file.
   1071 	 */
   1072 	f = NULL;
   1073 	while (fgets(cline, sizeof(cline), cf) != NULL) {
   1074 		/*
   1075 		 * check for end-of-section, comments, strip off trailing
   1076 		 * spaces and newline character.
   1077 		 */
   1078 		for (p = cline; isspace(*p); ++p)
   1079 			continue;
   1080 		if (*p == '\0' || *p == '#')
   1081 			continue;
   1082 		for (p = strchr(cline, '\0'); isspace(*--p);)
   1083 			continue;
   1084 		*++p = '\0';
   1085 		f = (struct filed *)calloc(1, sizeof(*f));
   1086 		*nextp = f;
   1087 		nextp = &f->f_next;
   1088 		cfline(cline, f);
   1089 	}
   1090 
   1091 	/* close the configuration file */
   1092 	(void)fclose(cf);
   1093 
   1094 	Initialized = 1;
   1095 
   1096 	if (Debug) {
   1097 		for (f = Files; f; f = f->f_next) {
   1098 			for (i = 0; i <= LOG_NFACILITIES; i++)
   1099 				if (f->f_pmask[i] == INTERNAL_NOPRI)
   1100 					printf("X ");
   1101 				else
   1102 					printf("%d ", f->f_pmask[i]);
   1103 			printf("%s: ", TypeNames[f->f_type]);
   1104 			switch (f->f_type) {
   1105 			case F_FILE:
   1106 			case F_TTY:
   1107 			case F_CONSOLE:
   1108 				printf("%s", f->f_un.f_fname);
   1109 				break;
   1110 
   1111 			case F_FORW:
   1112 				printf("%s", f->f_un.f_forw.f_hname);
   1113 				break;
   1114 
   1115 			case F_USERS:
   1116 				for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
   1117 					printf("%s, ", f->f_un.f_uname[i]);
   1118 				break;
   1119 			}
   1120 			printf("\n");
   1121 		}
   1122 	}
   1123 
   1124 	logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
   1125 	dprintf("syslogd: restarted\n");
   1126 }
   1127 
   1128 /*
   1129  * Crack a configuration file line
   1130  */
   1131 void
   1132 cfline(line, f)
   1133 	char *line;
   1134 	struct filed *f;
   1135 {
   1136 	struct addrinfo hints, *res;
   1137 	int    error, i, pri;
   1138 	char   *bp, *p, *q;
   1139 	char   buf[MAXLINE], ebuf[100];
   1140 
   1141 	dprintf("cfline(%s)\n", line);
   1142 
   1143 	errno = 0;	/* keep strerror() stuff out of logerror messages */
   1144 
   1145 	/* clear out file entry */
   1146 	memset(f, 0, sizeof(*f));
   1147 	for (i = 0; i <= LOG_NFACILITIES; i++)
   1148 		f->f_pmask[i] = INTERNAL_NOPRI;
   1149 
   1150 	/* scan through the list of selectors */
   1151 	for (p = line; *p && *p != '\t';) {
   1152 
   1153 		/* find the end of this facility name list */
   1154 		for (q = p; *q && *q != '\t' && *q++ != '.'; )
   1155 			continue;
   1156 
   1157 		/* collect priority name */
   1158 		for (bp = buf; *q && !strchr("\t,;", *q); )
   1159 			*bp++ = *q++;
   1160 		*bp = '\0';
   1161 
   1162 		/* skip cruft */
   1163 		while (strchr(", ;", *q))
   1164 			q++;
   1165 
   1166 		/* decode priority name */
   1167 		if (*buf == '*')
   1168 			pri = LOG_PRIMASK + 1;
   1169 		else {
   1170 			pri = decode(buf, prioritynames);
   1171 			if (pri < 0) {
   1172 				(void)snprintf(ebuf, sizeof ebuf,
   1173 				    "unknown priority name \"%s\"", buf);
   1174 				logerror(ebuf);
   1175 				return;
   1176 			}
   1177 		}
   1178 
   1179 		/* scan facilities */
   1180 		while (*p && !strchr("\t.;", *p)) {
   1181 			for (bp = buf; *p && !strchr("\t,;.", *p); )
   1182 				*bp++ = *p++;
   1183 			*bp = '\0';
   1184 			if (*buf == '*')
   1185 				for (i = 0; i < LOG_NFACILITIES; i++)
   1186 					f->f_pmask[i] = pri;
   1187 			else {
   1188 				i = decode(buf, facilitynames);
   1189 				if (i < 0) {
   1190 					(void)snprintf(ebuf, sizeof ebuf,
   1191 					    "unknown facility name \"%s\"",
   1192 					    buf);
   1193 					logerror(ebuf);
   1194 					return;
   1195 				}
   1196 				f->f_pmask[i >> 3] = pri;
   1197 			}
   1198 			while (*p == ',' || *p == ' ')
   1199 				p++;
   1200 		}
   1201 
   1202 		p = q;
   1203 	}
   1204 
   1205 	/* skip to action part */
   1206 	while (*p == '\t')
   1207 		p++;
   1208 
   1209 	switch (*p)
   1210 	{
   1211 	case '@':
   1212 		if (!InetInuse)
   1213 			break;
   1214 		(void)strcpy(f->f_un.f_forw.f_hname, ++p);
   1215 		memset(&hints, 0, sizeof(hints));
   1216 		hints.ai_family = AF_UNSPEC;
   1217 		hints.ai_socktype = SOCK_DGRAM;
   1218 		hints.ai_protocol = 0;
   1219 		error = getaddrinfo(f->f_un.f_forw.f_hname, "syslog", &hints, &res);
   1220 		if (error) {
   1221 			logerror(gai_strerror(error));
   1222 			break;
   1223 		}
   1224 		f->f_un.f_forw.f_addr = res;
   1225 		f->f_type = F_FORW;
   1226 		break;
   1227 
   1228 	case '/':
   1229 		(void)strcpy(f->f_un.f_fname, p);
   1230 		if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
   1231 			f->f_type = F_UNUSED;
   1232 			logerror(p);
   1233 			break;
   1234 		}
   1235 		if (isatty(f->f_file))
   1236 			f->f_type = F_TTY;
   1237 		else
   1238 			f->f_type = F_FILE;
   1239 		if (strcmp(p, ctty) == 0)
   1240 			f->f_type = F_CONSOLE;
   1241 		break;
   1242 
   1243 	case '*':
   1244 		f->f_type = F_WALL;
   1245 		break;
   1246 
   1247 	default:
   1248 		for (i = 0; i < MAXUNAMES && *p; i++) {
   1249 			for (q = p; *q && *q != ','; )
   1250 				q++;
   1251 			(void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE);
   1252 			if ((q - p) > UT_NAMESIZE)
   1253 				f->f_un.f_uname[i][UT_NAMESIZE] = '\0';
   1254 			else
   1255 				f->f_un.f_uname[i][q - p] = '\0';
   1256 			while (*q == ',' || *q == ' ')
   1257 				q++;
   1258 			p = q;
   1259 		}
   1260 		f->f_type = F_USERS;
   1261 		break;
   1262 	}
   1263 }
   1264 
   1265 
   1266 /*
   1267  *  Decode a symbolic name to a numeric value
   1268  */
   1269 int
   1270 decode(name, codetab)
   1271 	const char *name;
   1272 	CODE *codetab;
   1273 {
   1274 	CODE *c;
   1275 	char *p, buf[40];
   1276 
   1277 	if (isdigit(*name))
   1278 		return (atoi(name));
   1279 
   1280 	for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) {
   1281 		if (isupper(*name))
   1282 			*p = tolower(*name);
   1283 		else
   1284 			*p = *name;
   1285 	}
   1286 	*p = '\0';
   1287 	for (c = codetab; c->c_name; c++)
   1288 		if (!strcmp(buf, c->c_name))
   1289 			return (c->c_val);
   1290 
   1291 	return (-1);
   1292 }
   1293 
   1294 /*
   1295  * Retrieve the size of the kernel message buffer, via sysctl.
   1296  */
   1297 int
   1298 getmsgbufsize()
   1299 {
   1300 	int msgbufsize, mib[2];
   1301 	size_t size;
   1302 
   1303 	mib[0] = CTL_KERN;
   1304 	mib[1] = KERN_MSGBUFSIZE;
   1305 	size = sizeof msgbufsize;
   1306 	if (sysctl(mib, 2, &msgbufsize, &size, NULL, 0) == -1) {
   1307 		dprintf("couldn't get kern.msgbufsize\n");
   1308 		return (0);
   1309 	}
   1310 	return (msgbufsize);
   1311 }
   1312 
   1313 int *
   1314 socksetup(af)
   1315 	int af;
   1316 {
   1317 	struct addrinfo hints, *res, *r;
   1318 	int error, maxs, *s, *socks;
   1319 
   1320 	memset(&hints, 0, sizeof(hints));
   1321 	hints.ai_flags = AI_PASSIVE;
   1322 	hints.ai_family = af;
   1323 	hints.ai_socktype = SOCK_DGRAM;
   1324 	error = getaddrinfo(NULL, "syslog", &hints, &res);
   1325 	if (error) {
   1326 		logerror(gai_strerror(error));
   1327 		errno = 0;
   1328 		die(0);
   1329 	}
   1330 
   1331 	/* Count max number of sockets we may open */
   1332 	for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
   1333 	socks = malloc ((maxs+1) * sizeof(int));
   1334 	if (!socks) {
   1335 		logerror("couldn't allocate memory for sockets");
   1336 		die(0);
   1337 	}
   1338 
   1339 	*socks = 0;   /* num of sockets counter at start of array */
   1340 	s = socks+1;
   1341 	for (r = res; r; r = r->ai_next) {
   1342 		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
   1343 		if (*s < 0) {
   1344 			logerror("socket");
   1345 			continue;
   1346 		}
   1347 		if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
   1348 			close (*s);
   1349 			logerror("bind");
   1350 			continue;
   1351 		}
   1352 
   1353 		*socks = *socks + 1;
   1354 		s++;
   1355 	}
   1356 
   1357 	if (*socks == 0) {
   1358 		free (socks);
   1359 		if(Debug)
   1360 			return(NULL);
   1361 		else
   1362 			die(0);
   1363 	}
   1364 	if (res)
   1365 		freeaddrinfo(res);
   1366 
   1367 	return(socks);
   1368 }
   1369