Home | History | Annotate | Line # | Download | only in syslogd
syslogd.c revision 1.5
      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 #ifndef lint
     35 static char copyright[] =
     36 "@(#) 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 /*static char sccsid[] = "@(#)syslogd.c	8.3 (Berkeley) 4/4/94";*/
     42 static char rcsid[] = "$NetBSD: syslogd.c,v 1.5 1996/01/02 17:48:41 perry Exp $";
     43 #endif /* not lint */
     44 
     45 /*
     46  *  syslogd -- log system messages
     47  *
     48  * This program implements a system log. It takes a series of lines.
     49  * Each line may have a priority, signified as "<n>" as
     50  * the first characters of the line.  If this is
     51  * not present, a default priority is used.
     52  *
     53  * To kill syslogd, send a signal 15 (terminate).  A signal 1 (hup) will
     54  * cause it to reread its configuration file.
     55  *
     56  * Defined Constants:
     57  *
     58  * MAXLINE -- the maximimum line length that can be handled.
     59  * DEFUPRI -- the default priority for user messages
     60  * DEFSPRI -- the default priority for kernel messages
     61  *
     62  * Author: Eric Allman
     63  * extensive changes by Ralph Campbell
     64  * more extensive changes by Eric Allman (again)
     65  */
     66 
     67 #define	MAXLINE		1024		/* maximum line length */
     68 #define	MAXSVLINE	120		/* maximum saved line length */
     69 #define DEFUPRI		(LOG_USER|LOG_NOTICE)
     70 #define DEFSPRI		(LOG_KERN|LOG_CRIT)
     71 #define TIMERINTVL	30		/* interval for checking flush, mark */
     72 #define TTYMSGTIME	1		/* timeout passed to ttymsg */
     73 
     74 #include <sys/param.h>
     75 #include <sys/ioctl.h>
     76 #include <sys/stat.h>
     77 #include <sys/wait.h>
     78 #include <sys/socket.h>
     79 #include <sys/msgbuf.h>
     80 #include <sys/uio.h>
     81 #include <sys/un.h>
     82 #include <sys/time.h>
     83 #include <sys/resource.h>
     84 
     85 #include <netinet/in.h>
     86 #include <netdb.h>
     87 #include <arpa/inet.h>
     88 
     89 #include <ctype.h>
     90 #include <errno.h>
     91 #include <fcntl.h>
     92 #include <setjmp.h>
     93 #include <signal.h>
     94 #include <stdio.h>
     95 #include <stdlib.h>
     96 #include <string.h>
     97 #include <unistd.h>
     98 #include <utmp.h>
     99 #include "pathnames.h"
    100 
    101 #define SYSLOG_NAMES
    102 #include <sys/syslog.h>
    103 
    104 char	*LogName = _PATH_LOG;
    105 char	*ConfFile = _PATH_LOGCONF;
    106 char	*PidFile = _PATH_LOGPID;
    107 char	ctty[] = _PATH_CONSOLE;
    108 
    109 #define FDMASK(fd)	(1 << (fd))
    110 
    111 #define	dprintf		if (Debug) printf
    112 
    113 #define MAXUNAMES	20	/* maximum number of user names */
    114 
    115 /*
    116  * Flags to logmsg().
    117  */
    118 
    119 #define IGN_CONS	0x001	/* don't print on console */
    120 #define SYNC_FILE	0x002	/* do fsync on file after printing */
    121 #define ADDDATE		0x004	/* add a date to the message */
    122 #define MARK		0x008	/* this message is a mark */
    123 
    124 /*
    125  * This structure represents the files that will have log
    126  * copies printed.
    127  */
    128 
    129 struct filed {
    130 	struct	filed *f_next;		/* next in linked list */
    131 	short	f_type;			/* entry type, see below */
    132 	short	f_file;			/* file descriptor */
    133 	time_t	f_time;			/* time this was last written */
    134 	u_char	f_pmask[LOG_NFACILITIES+1];	/* priority mask */
    135 	union {
    136 		char	f_uname[MAXUNAMES][UT_NAMESIZE+1];
    137 		struct {
    138 			char	f_hname[MAXHOSTNAMELEN+1];
    139 			struct sockaddr_in	f_addr;
    140 		} f_forw;		/* forwarding address */
    141 		char	f_fname[MAXPATHLEN];
    142 	} f_un;
    143 	char	f_prevline[MAXSVLINE];		/* last message logged */
    144 	char	f_lasttime[16];			/* time of last occurrence */
    145 	char	f_prevhost[MAXHOSTNAMELEN+1];	/* host from which recd. */
    146 	int	f_prevpri;			/* pri of f_prevline */
    147 	int	f_prevlen;			/* length of f_prevline */
    148 	int	f_prevcount;			/* repetition cnt of prevline */
    149 	int	f_repeatcount;			/* number of "repeated" msgs */
    150 };
    151 
    152 /*
    153  * Intervals at which we flush out "message repeated" messages,
    154  * in seconds after previous message is logged.  After each flush,
    155  * we move to the next interval until we reach the largest.
    156  */
    157 int	repeatinterval[] = { 30, 120, 600 };	/* # of secs before flush */
    158 #define	MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
    159 #define	REPEATTIME(f)	((f)->f_time + repeatinterval[(f)->f_repeatcount])
    160 #define	BACKOFF(f)	{ if (++(f)->f_repeatcount > MAXREPEAT) \
    161 				 (f)->f_repeatcount = MAXREPEAT; \
    162 			}
    163 
    164 /* values for f_type */
    165 #define F_UNUSED	0		/* unused entry */
    166 #define F_FILE		1		/* regular file */
    167 #define F_TTY		2		/* terminal */
    168 #define F_CONSOLE	3		/* console terminal */
    169 #define F_FORW		4		/* remote machine */
    170 #define F_USERS		5		/* list of users */
    171 #define F_WALL		6		/* everyone logged on */
    172 
    173 char	*TypeNames[7] = {
    174 	"UNUSED",	"FILE",		"TTY",		"CONSOLE",
    175 	"FORW",		"USERS",	"WALL"
    176 };
    177 
    178 struct	filed *Files;
    179 struct	filed consfile;
    180 
    181 int	Debug;			/* debug flag */
    182 char	LocalHostName[MAXHOSTNAMELEN+1];	/* our hostname */
    183 char	*LocalDomain;		/* our local domain name */
    184 int	InetInuse = 0;		/* non-zero if INET sockets are being used */
    185 int	finet;			/* Internet datagram socket */
    186 int	LogPort;		/* port number for INET connections */
    187 int	Initialized = 0;	/* set when we have initialized ourselves */
    188 int	MarkInterval = 20 * 60;	/* interval between marks in seconds */
    189 int	MarkSeq = 0;		/* mark sequence number */
    190 
    191 void	cfline __P((char *, struct filed *));
    192 char   *cvthname __P((struct sockaddr_in *));
    193 int	decode __P((const char *, CODE *));
    194 void	die __P((int));
    195 void	domark __P((int));
    196 void	fprintlog __P((struct filed *, int, char *));
    197 void	init __P((int));
    198 void	logerror __P((char *));
    199 void	logmsg __P((int, char *, char *, int));
    200 void	printline __P((char *, char *));
    201 void	printsys __P((char *));
    202 void	reapchild __P((int));
    203 char   *ttymsg __P((struct iovec *, int, char *, int));
    204 void	usage __P((void));
    205 void	wallmsg __P((struct filed *, struct iovec *));
    206 
    207 int
    208 main(argc, argv)
    209 	int argc;
    210 	char *argv[];
    211 {
    212 	int ch, funix, i, inetm, fklog, klogm, len;
    213 	struct sockaddr_un sunx, fromunix;
    214 	struct sockaddr_in sin, frominet;
    215 	FILE *fp;
    216 	char *p, line[MSG_BSIZE + 1];
    217 
    218 	while ((ch = getopt(argc, argv, "df:m:p:")) != EOF)
    219 		switch(ch) {
    220 		case 'd':		/* debug */
    221 			Debug++;
    222 			break;
    223 		case 'f':		/* configuration file */
    224 			ConfFile = optarg;
    225 			break;
    226 		case 'm':		/* mark interval */
    227 			MarkInterval = atoi(optarg) * 60;
    228 			break;
    229 		case 'p':		/* path */
    230 			LogName = optarg;
    231 			break;
    232 		case '?':
    233 		default:
    234 			usage();
    235 		}
    236 	if ((argc -= optind) != 0)
    237 		usage();
    238 
    239 	if (!Debug)
    240 		(void)daemon(0, 0);
    241 	else
    242 		setlinebuf(stdout);
    243 
    244 	consfile.f_type = F_CONSOLE;
    245 	(void)strcpy(consfile.f_un.f_fname, ctty);
    246 	(void)gethostname(LocalHostName, sizeof(LocalHostName));
    247 	if ((p = strchr(LocalHostName, '.')) != NULL) {
    248 		*p++ = '\0';
    249 		LocalDomain = p;
    250 	} else
    251 		LocalDomain = "";
    252 	(void)signal(SIGTERM, die);
    253 	(void)signal(SIGINT, Debug ? die : SIG_IGN);
    254 	(void)signal(SIGQUIT, Debug ? die : SIG_IGN);
    255 	(void)signal(SIGCHLD, reapchild);
    256 	(void)signal(SIGALRM, domark);
    257 	(void)alarm(TIMERINTVL);
    258 	(void)unlink(LogName);
    259 
    260 #ifndef SUN_LEN
    261 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
    262 #endif
    263 	memset(&sunx, 0, sizeof(sunx));
    264 	sunx.sun_family = AF_UNIX;
    265 	(void)strncpy(sunx.sun_path, LogName, sizeof(sunx.sun_path));
    266 	funix = socket(AF_UNIX, SOCK_DGRAM, 0);
    267 	if (funix < 0 ||
    268 	    bind(funix, (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 ||
    269 	    chmod(LogName, 0666) < 0) {
    270 		(void) sprintf(line, "cannot create %s", LogName);
    271 		logerror(line);
    272 		dprintf("cannot create %s (%d)\n", LogName, errno);
    273 		die(0);
    274 	}
    275 	finet = socket(AF_INET, SOCK_DGRAM, 0);
    276 	inetm = 0;
    277 	if (finet >= 0) {
    278 		struct servent *sp;
    279 
    280 		sp = getservbyname("syslog", "udp");
    281 		if (sp == NULL) {
    282 			errno = 0;
    283 			logerror("syslog/udp: unknown service");
    284 			die(0);
    285 		}
    286 		memset(&sin, 0, sizeof(sin));
    287 		sin.sin_family = AF_INET;
    288 		sin.sin_port = LogPort = sp->s_port;
    289 		if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    290 			logerror("bind");
    291 			if (!Debug)
    292 				die(0);
    293 		} else {
    294 			inetm = FDMASK(finet);
    295 			InetInuse = 1;
    296 		}
    297 	}
    298 	if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0)
    299 		klogm = FDMASK(fklog);
    300 	else {
    301 		dprintf("can't open %s (%d)\n", _PATH_KLOG, errno);
    302 		klogm = 0;
    303 	}
    304 
    305 	/* tuck my process id away */
    306 	fp = fopen(PidFile, "w");
    307 	if (fp != NULL) {
    308 		fprintf(fp, "%d\n", getpid());
    309 		(void) fclose(fp);
    310 	}
    311 
    312 	dprintf("off & running....\n");
    313 
    314 	init(0);
    315 	(void)signal(SIGHUP, init);
    316 
    317 	for (;;) {
    318 		int nfds, readfds = FDMASK(funix) | inetm | klogm;
    319 
    320 		dprintf("readfds = %#x\n", readfds);
    321 		nfds = select(20, (fd_set *)&readfds, (fd_set *)NULL,
    322 		    (fd_set *)NULL, (struct timeval *)NULL);
    323 		if (nfds == 0)
    324 			continue;
    325 		if (nfds < 0) {
    326 			if (errno != EINTR)
    327 				logerror("select");
    328 			continue;
    329 		}
    330 		dprintf("got a message (%d, %#x)\n", nfds, readfds);
    331 		if (readfds & klogm) {
    332 			i = read(fklog, line, sizeof(line) - 1);
    333 			if (i > 0) {
    334 				line[i] = '\0';
    335 				printsys(line);
    336 			} else if (i < 0 && errno != EINTR) {
    337 				logerror("klog");
    338 				fklog = -1;
    339 				klogm = 0;
    340 			}
    341 		}
    342 		if (readfds & FDMASK(funix)) {
    343 			len = sizeof(fromunix);
    344 			i = recvfrom(funix, line, MAXLINE, 0,
    345 			    (struct sockaddr *)&fromunix, &len);
    346 			if (i > 0) {
    347 				line[i] = '\0';
    348 				printline(LocalHostName, line);
    349 			} else if (i < 0 && errno != EINTR)
    350 				logerror("recvfrom unix");
    351 		}
    352 		if (readfds & inetm) {
    353 			len = sizeof(frominet);
    354 			i = recvfrom(finet, line, MAXLINE, 0,
    355 			    (struct sockaddr *)&frominet, &len);
    356 			if (i > 0) {
    357 				line[i] = '\0';
    358 				printline(cvthname(&frominet), line);
    359 			} else if (i < 0 && errno != EINTR)
    360 				logerror("recvfrom inet");
    361 		}
    362 	}
    363 }
    364 
    365 void
    366 usage()
    367 {
    368 
    369 	(void)fprintf(stderr,
    370 	    "usage: syslogd [-f conffile] [-m markinterval] [-p logpath]\n");
    371 	exit(1);
    372 }
    373 
    374 /*
    375  * Take a raw input line, decode the message, and print the message
    376  * on the appropriate log files.
    377  */
    378 void
    379 printline(hname, msg)
    380 	char *hname;
    381 	char *msg;
    382 {
    383 	int c, pri;
    384 	char *p, *q, line[MAXLINE + 1];
    385 
    386 	/* test for special codes */
    387 	pri = DEFUPRI;
    388 	p = msg;
    389 	if (*p == '<') {
    390 		pri = 0;
    391 		while (isdigit(*++p))
    392 			pri = 10 * pri + (*p - '0');
    393 		if (*p == '>')
    394 			++p;
    395 	}
    396 	if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
    397 		pri = DEFUPRI;
    398 
    399 	/* don't allow users to log kernel messages */
    400 	if (LOG_FAC(pri) == LOG_KERN)
    401 		pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
    402 
    403 	q = line;
    404 
    405 	while ((c = *p++ & 0177) != '\0' &&
    406 	    q < &line[sizeof(line) - 1])
    407 		if (iscntrl(c))
    408 			if (c == '\n')
    409 				*q++ = ' ';
    410 			else if (c == '\t')
    411 				*q++ = '\t';
    412 			else {
    413 				*q++ = '^';
    414 				*q++ = c ^ 0100;
    415 			}
    416 		else
    417 			*q++ = c;
    418 	*q = '\0';
    419 
    420 	logmsg(pri, line, hname, 0);
    421 }
    422 
    423 /*
    424  * Take a raw input line from /dev/klog, split and format similar to syslog().
    425  */
    426 void
    427 printsys(msg)
    428 	char *msg;
    429 {
    430 	int c, pri, flags;
    431 	char *lp, *p, *q, line[MAXLINE + 1];
    432 
    433 	(void)strcpy(line, _PATH_UNIX);
    434 	(void)strcat(line, ": ");
    435 	lp = line + strlen(line);
    436 	for (p = msg; *p != '\0'; ) {
    437 		flags = SYNC_FILE | ADDDATE;	/* fsync file after write */
    438 		pri = DEFSPRI;
    439 		if (*p == '<') {
    440 			pri = 0;
    441 			while (isdigit(*++p))
    442 				pri = 10 * pri + (*p - '0');
    443 			if (*p == '>')
    444 				++p;
    445 		} else {
    446 			/* kernel printf's come out on console */
    447 			flags |= IGN_CONS;
    448 		}
    449 		if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
    450 			pri = DEFSPRI;
    451 		q = lp;
    452 		while (*p != '\0' && (c = *p++) != '\n' &&
    453 		    q < &line[MAXLINE])
    454 			*q++ = c;
    455 		*q = '\0';
    456 		logmsg(pri, line, LocalHostName, flags);
    457 	}
    458 }
    459 
    460 time_t	now;
    461 
    462 /*
    463  * Log a message to the appropriate log files, users, etc. based on
    464  * the priority.
    465  */
    466 void
    467 logmsg(pri, msg, from, flags)
    468 	int pri;
    469 	char *msg, *from;
    470 	int flags;
    471 {
    472 	struct filed *f;
    473 	int fac, msglen, omask, prilev;
    474 	char *timestamp;
    475 
    476 	dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n",
    477 	    pri, flags, from, msg);
    478 
    479 	omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
    480 
    481 	/*
    482 	 * Check to see if msg looks non-standard.
    483 	 */
    484 	msglen = strlen(msg);
    485 	if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
    486 	    msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
    487 		flags |= ADDDATE;
    488 
    489 	(void)time(&now);
    490 	if (flags & ADDDATE)
    491 		timestamp = ctime(&now) + 4;
    492 	else {
    493 		timestamp = msg;
    494 		msg += 16;
    495 		msglen -= 16;
    496 	}
    497 
    498 	/* extract facility and priority level */
    499 	if (flags & MARK)
    500 		fac = LOG_NFACILITIES;
    501 	else
    502 		fac = LOG_FAC(pri);
    503 	prilev = LOG_PRI(pri);
    504 
    505 	/* log the message to the particular outputs */
    506 	if (!Initialized) {
    507 		f = &consfile;
    508 		f->f_file = open(ctty, O_WRONLY, 0);
    509 
    510 		if (f->f_file >= 0) {
    511 			fprintlog(f, flags, msg);
    512 			(void)close(f->f_file);
    513 		}
    514 		(void)sigsetmask(omask);
    515 		return;
    516 	}
    517 	for (f = Files; f; f = f->f_next) {
    518 		/* skip messages that are incorrect priority */
    519 		if (f->f_pmask[fac] < prilev ||
    520 		    f->f_pmask[fac] == INTERNAL_NOPRI)
    521 			continue;
    522 
    523 		if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
    524 			continue;
    525 
    526 		/* don't output marks to recently written files */
    527 		if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
    528 			continue;
    529 
    530 		/*
    531 		 * suppress duplicate lines to this file
    532 		 */
    533 		if ((flags & MARK) == 0 && msglen == f->f_prevlen &&
    534 		    !strcmp(msg, f->f_prevline) &&
    535 		    !strcmp(from, f->f_prevhost)) {
    536 			(void)strncpy(f->f_lasttime, timestamp, 15);
    537 			f->f_prevcount++;
    538 			dprintf("msg repeated %d times, %ld sec of %d\n",
    539 			    f->f_prevcount, now - f->f_time,
    540 			    repeatinterval[f->f_repeatcount]);
    541 			/*
    542 			 * If domark would have logged this by now,
    543 			 * flush it now (so we don't hold isolated messages),
    544 			 * but back off so we'll flush less often
    545 			 * in the future.
    546 			 */
    547 			if (now > REPEATTIME(f)) {
    548 				fprintlog(f, flags, (char *)NULL);
    549 				BACKOFF(f);
    550 			}
    551 		} else {
    552 			/* new line, save it */
    553 			if (f->f_prevcount)
    554 				fprintlog(f, 0, (char *)NULL);
    555 			f->f_repeatcount = 0;
    556 			f->f_prevpri = pri;
    557 			(void)strncpy(f->f_lasttime, timestamp, 15);
    558 			(void)strncpy(f->f_prevhost, from,
    559 					sizeof(f->f_prevhost));
    560 			if (msglen < MAXSVLINE) {
    561 				f->f_prevlen = msglen;
    562 				(void)strcpy(f->f_prevline, msg);
    563 				fprintlog(f, flags, (char *)NULL);
    564 			} else {
    565 				f->f_prevline[0] = 0;
    566 				f->f_prevlen = 0;
    567 				fprintlog(f, flags, msg);
    568 			}
    569 		}
    570 	}
    571 	(void)sigsetmask(omask);
    572 }
    573 
    574 void
    575 fprintlog(f, flags, msg)
    576 	struct filed *f;
    577 	int flags;
    578 	char *msg;
    579 {
    580 	struct iovec iov[6];
    581 	struct iovec *v;
    582 	int l;
    583 	char line[MAXLINE + 1], repbuf[80], greetings[200];
    584 
    585 	v = iov;
    586 	if (f->f_type == F_WALL) {
    587 		v->iov_base = greetings;
    588 		v->iov_len = sprintf(greetings,
    589 		    "\r\n\7Message from syslogd@%s at %.24s ...\r\n",
    590 		    f->f_prevhost, ctime(&now));
    591 		v++;
    592 		v->iov_base = "";
    593 		v->iov_len = 0;
    594 		v++;
    595 	} else {
    596 		v->iov_base = f->f_lasttime;
    597 		v->iov_len = 15;
    598 		v++;
    599 		v->iov_base = " ";
    600 		v->iov_len = 1;
    601 		v++;
    602 	}
    603 	v->iov_base = f->f_prevhost;
    604 	v->iov_len = strlen(v->iov_base);
    605 	v++;
    606 	v->iov_base = " ";
    607 	v->iov_len = 1;
    608 	v++;
    609 
    610 	if (msg) {
    611 		v->iov_base = msg;
    612 		v->iov_len = strlen(msg);
    613 	} else if (f->f_prevcount > 1) {
    614 		v->iov_base = repbuf;
    615 		v->iov_len = sprintf(repbuf, "last message repeated %d times",
    616 		    f->f_prevcount);
    617 	} else {
    618 		v->iov_base = f->f_prevline;
    619 		v->iov_len = f->f_prevlen;
    620 	}
    621 	v++;
    622 
    623 	dprintf("Logging to %s", TypeNames[f->f_type]);
    624 	f->f_time = now;
    625 
    626 	switch (f->f_type) {
    627 	case F_UNUSED:
    628 		dprintf("\n");
    629 		break;
    630 
    631 	case F_FORW:
    632 		dprintf(" %s\n", f->f_un.f_forw.f_hname);
    633 		l = sprintf(line, "<%d>%.15s %s", f->f_prevpri,
    634 		    iov[0].iov_base, iov[4].iov_base);
    635 		if (l > MAXLINE)
    636 			l = MAXLINE;
    637 		if (sendto(finet, line, l, 0,
    638 		    (struct sockaddr *)&f->f_un.f_forw.f_addr,
    639 		    sizeof(f->f_un.f_forw.f_addr)) != l) {
    640 			int e = errno;
    641 			(void)close(f->f_file);
    642 			f->f_type = F_UNUSED;
    643 			errno = e;
    644 			logerror("sendto");
    645 		}
    646 		break;
    647 
    648 	case F_CONSOLE:
    649 		if (flags & IGN_CONS) {
    650 			dprintf(" (ignored)\n");
    651 			break;
    652 		}
    653 		/* FALLTHROUGH */
    654 
    655 	case F_TTY:
    656 	case F_FILE:
    657 		dprintf(" %s\n", f->f_un.f_fname);
    658 		if (f->f_type != F_FILE) {
    659 			v->iov_base = "\r\n";
    660 			v->iov_len = 2;
    661 		} else {
    662 			v->iov_base = "\n";
    663 			v->iov_len = 1;
    664 		}
    665 	again:
    666 		if (writev(f->f_file, iov, 6) < 0) {
    667 			int e = errno;
    668 			(void)close(f->f_file);
    669 			/*
    670 			 * Check for errors on TTY's due to loss of tty
    671 			 */
    672 			if ((e == EIO || e == EBADF) && f->f_type != F_FILE) {
    673 				f->f_file = open(f->f_un.f_fname,
    674 				    O_WRONLY|O_APPEND, 0);
    675 				if (f->f_file < 0) {
    676 					f->f_type = F_UNUSED;
    677 					logerror(f->f_un.f_fname);
    678 				} else
    679 					goto again;
    680 			} else {
    681 				f->f_type = F_UNUSED;
    682 				errno = e;
    683 				logerror(f->f_un.f_fname);
    684 			}
    685 		} else if (flags & SYNC_FILE)
    686 			(void)fsync(f->f_file);
    687 		break;
    688 
    689 	case F_USERS:
    690 	case F_WALL:
    691 		dprintf("\n");
    692 		v->iov_base = "\r\n";
    693 		v->iov_len = 2;
    694 		wallmsg(f, iov);
    695 		break;
    696 	}
    697 	f->f_prevcount = 0;
    698 }
    699 
    700 /*
    701  *  WALLMSG -- Write a message to the world at large
    702  *
    703  *	Write the specified message to either the entire
    704  *	world, or a list of approved users.
    705  */
    706 void
    707 wallmsg(f, iov)
    708 	struct filed *f;
    709 	struct iovec *iov;
    710 {
    711 	static int reenter;			/* avoid calling ourselves */
    712 	FILE *uf;
    713 	struct utmp ut;
    714 	int i;
    715 	char *p;
    716 	char line[sizeof(ut.ut_line) + 1];
    717 
    718 	if (reenter++)
    719 		return;
    720 	if ((uf = fopen(_PATH_UTMP, "r")) == NULL) {
    721 		logerror(_PATH_UTMP);
    722 		reenter = 0;
    723 		return;
    724 	}
    725 	/* NOSTRICT */
    726 	while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) {
    727 		if (ut.ut_name[0] == '\0')
    728 			continue;
    729 		strncpy(line, ut.ut_line, sizeof(ut.ut_line));
    730 		line[sizeof(ut.ut_line)] = '\0';
    731 		if (f->f_type == F_WALL) {
    732 			if ((p = ttymsg(iov, 6, line, TTYMSGTIME)) != NULL) {
    733 				errno = 0;	/* already in msg */
    734 				logerror(p);
    735 			}
    736 			continue;
    737 		}
    738 		/* should we send the message to this user? */
    739 		for (i = 0; i < MAXUNAMES; i++) {
    740 			if (!f->f_un.f_uname[i][0])
    741 				break;
    742 			if (!strncmp(f->f_un.f_uname[i], ut.ut_name,
    743 			    UT_NAMESIZE)) {
    744 				if ((p = ttymsg(iov, 6, line, TTYMSGTIME))
    745 								!= NULL) {
    746 					errno = 0;	/* already in msg */
    747 					logerror(p);
    748 				}
    749 				break;
    750 			}
    751 		}
    752 	}
    753 	(void)fclose(uf);
    754 	reenter = 0;
    755 }
    756 
    757 void
    758 reapchild(signo)
    759 	int signo;
    760 {
    761 	union wait status;
    762 
    763 	while (wait3((int *)&status, WNOHANG, (struct rusage *)NULL) > 0)
    764 		;
    765 }
    766 
    767 /*
    768  * Return a printable representation of a host address.
    769  */
    770 char *
    771 cvthname(f)
    772 	struct sockaddr_in *f;
    773 {
    774 	struct hostent *hp;
    775 	char *p;
    776 
    777 	dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr));
    778 
    779 	if (f->sin_family != AF_INET) {
    780 		dprintf("Malformed from address\n");
    781 		return ("???");
    782 	}
    783 	hp = gethostbyaddr((char *)&f->sin_addr,
    784 	    sizeof(struct in_addr), f->sin_family);
    785 	if (hp == 0) {
    786 		dprintf("Host name for your address (%s) unknown\n",
    787 			inet_ntoa(f->sin_addr));
    788 		return (inet_ntoa(f->sin_addr));
    789 	}
    790 	if ((p = strchr(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0)
    791 		*p = '\0';
    792 	return (hp->h_name);
    793 }
    794 
    795 void
    796 domark(signo)
    797 	int signo;
    798 {
    799 	struct filed *f;
    800 
    801 	now = time((time_t *)NULL);
    802 	MarkSeq += TIMERINTVL;
    803 	if (MarkSeq >= MarkInterval) {
    804 		logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
    805 		MarkSeq = 0;
    806 	}
    807 
    808 	for (f = Files; f; f = f->f_next) {
    809 		if (f->f_prevcount && now >= REPEATTIME(f)) {
    810 			dprintf("flush %s: repeated %d times, %d sec.\n",
    811 			    TypeNames[f->f_type], f->f_prevcount,
    812 			    repeatinterval[f->f_repeatcount]);
    813 			fprintlog(f, 0, (char *)NULL);
    814 			BACKOFF(f);
    815 		}
    816 	}
    817 	(void)alarm(TIMERINTVL);
    818 }
    819 
    820 /*
    821  * Print syslogd errors some place.
    822  */
    823 void
    824 logerror(type)
    825 	char *type;
    826 {
    827 	char buf[100];
    828 
    829 	if (errno)
    830 		(void)snprintf(buf,
    831 		    sizeof(buf), "syslogd: %s: %s", type, strerror(errno));
    832 	else
    833 		(void)snprintf(buf, sizeof(buf), "syslogd: %s", type);
    834 	errno = 0;
    835 	dprintf("%s\n", buf);
    836 	logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
    837 }
    838 
    839 void
    840 die(signo)
    841 	int signo;
    842 {
    843 	struct filed *f;
    844 	char buf[100];
    845 
    846 	for (f = Files; f != NULL; f = f->f_next) {
    847 		/* flush any pending output */
    848 		if (f->f_prevcount)
    849 			fprintlog(f, 0, (char *)NULL);
    850 	}
    851 	if (signo) {
    852 		dprintf("syslogd: exiting on signal %d\n", signo);
    853 		(void)sprintf(buf, "exiting on signal %d", signo);
    854 		errno = 0;
    855 		logerror(buf);
    856 	}
    857 	(void)unlink(LogName);
    858 	exit(0);
    859 }
    860 
    861 /*
    862  *  INIT -- Initialize syslogd from configuration table
    863  */
    864 void
    865 init(signo)
    866 	int signo;
    867 {
    868 	int i;
    869 	FILE *cf;
    870 	struct filed *f, *next, **nextp;
    871 	char *p;
    872 	char cline[LINE_MAX];
    873 
    874 	dprintf("init\n");
    875 
    876 	/*
    877 	 *  Close all open log files.
    878 	 */
    879 	Initialized = 0;
    880 	for (f = Files; f != NULL; f = next) {
    881 		/* flush any pending output */
    882 		if (f->f_prevcount)
    883 			fprintlog(f, 0, (char *)NULL);
    884 
    885 		switch (f->f_type) {
    886 		case F_FILE:
    887 		case F_TTY:
    888 		case F_CONSOLE:
    889 		case F_FORW:
    890 			(void)close(f->f_file);
    891 			break;
    892 		}
    893 		next = f->f_next;
    894 		free((char *)f);
    895 	}
    896 	Files = NULL;
    897 	nextp = &Files;
    898 
    899 	/* open the configuration file */
    900 	if ((cf = fopen(ConfFile, "r")) == NULL) {
    901 		dprintf("cannot open %s\n", ConfFile);
    902 		*nextp = (struct filed *)calloc(1, sizeof(*f));
    903 		cfline("*.ERR\t/dev/console", *nextp);
    904 		(*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
    905 		cfline("*.PANIC\t*", (*nextp)->f_next);
    906 		Initialized = 1;
    907 		return;
    908 	}
    909 
    910 	/*
    911 	 *  Foreach line in the conf table, open that file.
    912 	 */
    913 	f = NULL;
    914 	while (fgets(cline, sizeof(cline), cf) != NULL) {
    915 		/*
    916 		 * check for end-of-section, comments, strip off trailing
    917 		 * spaces and newline character.
    918 		 */
    919 		for (p = cline; isspace(*p); ++p)
    920 			continue;
    921 		if (*p == NULL || *p == '#')
    922 			continue;
    923 		for (p = strchr(cline, '\0'); isspace(*--p);)
    924 			continue;
    925 		*++p = '\0';
    926 		f = (struct filed *)calloc(1, sizeof(*f));
    927 		*nextp = f;
    928 		nextp = &f->f_next;
    929 		cfline(cline, f);
    930 	}
    931 
    932 	/* close the configuration file */
    933 	(void)fclose(cf);
    934 
    935 	Initialized = 1;
    936 
    937 	if (Debug) {
    938 		for (f = Files; f; f = f->f_next) {
    939 			for (i = 0; i <= LOG_NFACILITIES; i++)
    940 				if (f->f_pmask[i] == INTERNAL_NOPRI)
    941 					printf("X ");
    942 				else
    943 					printf("%d ", f->f_pmask[i]);
    944 			printf("%s: ", TypeNames[f->f_type]);
    945 			switch (f->f_type) {
    946 			case F_FILE:
    947 			case F_TTY:
    948 			case F_CONSOLE:
    949 				printf("%s", f->f_un.f_fname);
    950 				break;
    951 
    952 			case F_FORW:
    953 				printf("%s", f->f_un.f_forw.f_hname);
    954 				break;
    955 
    956 			case F_USERS:
    957 				for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
    958 					printf("%s, ", f->f_un.f_uname[i]);
    959 				break;
    960 			}
    961 			printf("\n");
    962 		}
    963 	}
    964 
    965 	logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
    966 	dprintf("syslogd: restarted\n");
    967 }
    968 
    969 /*
    970  * Crack a configuration file line
    971  */
    972 void
    973 cfline(line, f)
    974 	char *line;
    975 	struct filed *f;
    976 {
    977 	struct hostent *hp;
    978 	int i, pri;
    979 	char *bp, *p, *q;
    980 	char buf[MAXLINE], ebuf[100];
    981 
    982 	dprintf("cfline(%s)\n", line);
    983 
    984 	errno = 0;	/* keep strerror() stuff out of logerror messages */
    985 
    986 	/* clear out file entry */
    987 	memset(f, 0, sizeof(*f));
    988 	for (i = 0; i <= LOG_NFACILITIES; i++)
    989 		f->f_pmask[i] = INTERNAL_NOPRI;
    990 
    991 	/* scan through the list of selectors */
    992 	for (p = line; *p && *p != '\t';) {
    993 
    994 		/* find the end of this facility name list */
    995 		for (q = p; *q && *q != '\t' && *q++ != '.'; )
    996 			continue;
    997 
    998 		/* collect priority name */
    999 		for (bp = buf; *q && !strchr("\t,;", *q); )
   1000 			*bp++ = *q++;
   1001 		*bp = '\0';
   1002 
   1003 		/* skip cruft */
   1004 		while (strchr(", ;", *q))
   1005 			q++;
   1006 
   1007 		/* decode priority name */
   1008 		if (*buf == '*')
   1009 			pri = LOG_PRIMASK + 1;
   1010 		else {
   1011 			pri = decode(buf, prioritynames);
   1012 			if (pri < 0) {
   1013 				(void)sprintf(ebuf,
   1014 				    "unknown priority name \"%s\"", buf);
   1015 				logerror(ebuf);
   1016 				return;
   1017 			}
   1018 		}
   1019 
   1020 		/* scan facilities */
   1021 		while (*p && !strchr("\t.;", *p)) {
   1022 			for (bp = buf; *p && !strchr("\t,;.", *p); )
   1023 				*bp++ = *p++;
   1024 			*bp = '\0';
   1025 			if (*buf == '*')
   1026 				for (i = 0; i < LOG_NFACILITIES; i++)
   1027 					f->f_pmask[i] = pri;
   1028 			else {
   1029 				i = decode(buf, facilitynames);
   1030 				if (i < 0) {
   1031 					(void)sprintf(ebuf,
   1032 					    "unknown facility name \"%s\"",
   1033 					    buf);
   1034 					logerror(ebuf);
   1035 					return;
   1036 				}
   1037 				f->f_pmask[i >> 3] = pri;
   1038 			}
   1039 			while (*p == ',' || *p == ' ')
   1040 				p++;
   1041 		}
   1042 
   1043 		p = q;
   1044 	}
   1045 
   1046 	/* skip to action part */
   1047 	while (*p == '\t')
   1048 		p++;
   1049 
   1050 	switch (*p)
   1051 	{
   1052 	case '@':
   1053 		if (!InetInuse)
   1054 			break;
   1055 		(void)strcpy(f->f_un.f_forw.f_hname, ++p);
   1056 		hp = gethostbyname(p);
   1057 		if (hp == NULL) {
   1058 			extern int h_errno;
   1059 
   1060 			logerror(hstrerror(h_errno));
   1061 			break;
   1062 		}
   1063 		memset(&f->f_un.f_forw.f_addr, 0,
   1064 			 sizeof(f->f_un.f_forw.f_addr));
   1065 		f->f_un.f_forw.f_addr.sin_family = AF_INET;
   1066 		f->f_un.f_forw.f_addr.sin_port = LogPort;
   1067 		memmove(&f->f_un.f_forw.f_addr.sin_addr, hp->h_addr, hp->h_length);
   1068 		f->f_type = F_FORW;
   1069 		break;
   1070 
   1071 	case '/':
   1072 		(void)strcpy(f->f_un.f_fname, p);
   1073 		if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
   1074 			f->f_file = F_UNUSED;
   1075 			logerror(p);
   1076 			break;
   1077 		}
   1078 		if (isatty(f->f_file))
   1079 			f->f_type = F_TTY;
   1080 		else
   1081 			f->f_type = F_FILE;
   1082 		if (strcmp(p, ctty) == 0)
   1083 			f->f_type = F_CONSOLE;
   1084 		break;
   1085 
   1086 	case '*':
   1087 		f->f_type = F_WALL;
   1088 		break;
   1089 
   1090 	default:
   1091 		for (i = 0; i < MAXUNAMES && *p; i++) {
   1092 			for (q = p; *q && *q != ','; )
   1093 				q++;
   1094 			(void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE);
   1095 			if ((q - p) > UT_NAMESIZE)
   1096 				f->f_un.f_uname[i][UT_NAMESIZE] = '\0';
   1097 			else
   1098 				f->f_un.f_uname[i][q - p] = '\0';
   1099 			while (*q == ',' || *q == ' ')
   1100 				q++;
   1101 			p = q;
   1102 		}
   1103 		f->f_type = F_USERS;
   1104 		break;
   1105 	}
   1106 }
   1107 
   1108 
   1109 /*
   1110  *  Decode a symbolic name to a numeric value
   1111  */
   1112 int
   1113 decode(name, codetab)
   1114 	const char *name;
   1115 	CODE *codetab;
   1116 {
   1117 	CODE *c;
   1118 	char *p, buf[40];
   1119 
   1120 	if (isdigit(*name))
   1121 		return (atoi(name));
   1122 
   1123 	for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) {
   1124 		if (isupper(*name))
   1125 			*p = tolower(*name);
   1126 		else
   1127 			*p = *name;
   1128 	}
   1129 	*p = '\0';
   1130 	for (c = codetab; c->c_name; c++)
   1131 		if (!strcmp(buf, c->c_name))
   1132 			return (c->c_val);
   1133 
   1134 	return (-1);
   1135 }
   1136