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