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