xref: /freebsd/usr.sbin/syslogd/syslogd.c (revision d1ba25f456132eabc6f1244e4bbbf3d19e8f3a31)
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 const 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 #if 0
42 static char sccsid[] = "@(#)syslogd.c	8.3 (Berkeley) 4/4/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 /*
49  *  syslogd -- log system messages
50  *
51  * This program implements a system log. It takes a series of lines.
52  * Each line may have a priority, signified as "<n>" as
53  * the first characters of the line.  If this is
54  * not present, a default priority is used.
55  *
56  * To kill syslogd, send a signal 15 (terminate).  A signal 1 (hup) will
57  * cause it to reread its configuration file.
58  *
59  * Defined Constants:
60  *
61  * MAXLINE -- the maximimum line length that can be handled.
62  * DEFUPRI -- the default priority for user messages
63  * DEFSPRI -- the default priority for kernel messages
64  *
65  * Author: Eric Allman
66  * extensive changes by Ralph Campbell
67  * more extensive changes by Eric Allman (again)
68  * Extension to log by program name as well as facility and priority
69  *   by Peter da Silva.
70  * -u and -v by Harlan Stenn.
71  * Priority comparison code by Harlan Stenn.
72  */
73 
74 #define	MAXLINE		1024		/* maximum line length */
75 #define	MAXSVLINE	120		/* maximum saved line length */
76 #define DEFUPRI		(LOG_USER|LOG_NOTICE)
77 #define DEFSPRI		(LOG_KERN|LOG_CRIT)
78 #define TIMERINTVL	30		/* interval for checking flush, mark */
79 #define TTYMSGTIME	1		/* timed out passed to ttymsg */
80 
81 #include <sys/param.h>
82 #include <sys/ioctl.h>
83 #include <sys/stat.h>
84 #include <sys/wait.h>
85 #include <sys/socket.h>
86 #include <sys/queue.h>
87 #include <sys/uio.h>
88 #include <sys/un.h>
89 #include <sys/time.h>
90 #include <sys/resource.h>
91 #include <sys/syslimits.h>
92 #include <paths.h>
93 
94 #include <netinet/in.h>
95 #include <netdb.h>
96 #include <arpa/inet.h>
97 
98 #include <ctype.h>
99 #include <err.h>
100 #include <errno.h>
101 #include <fcntl.h>
102 #include <regex.h>
103 #include <setjmp.h>
104 #include <signal.h>
105 #include <stdio.h>
106 #include <stdlib.h>
107 #include <string.h>
108 #include <sysexits.h>
109 #include <unistd.h>
110 #include <utmp.h>
111 #include "pathnames.h"
112 
113 #define SYSLOG_NAMES
114 #include <sys/syslog.h>
115 
116 const char	*ConfFile = _PATH_LOGCONF;
117 const char	*PidFile = _PATH_LOGPID;
118 const char	ctty[] = _PATH_CONSOLE;
119 
120 #define	dprintf		if (Debug) printf
121 
122 #define MAXUNAMES	20	/* maximum number of user names */
123 
124 #define MAXFUNIX       20
125 
126 int nfunix = 1;
127 char *funixn[MAXFUNIX] = { _PATH_LOG };
128 int funix[MAXFUNIX];
129 
130 /*
131  * Flags to logmsg().
132  */
133 
134 #define IGN_CONS	0x001	/* don't print on console */
135 #define SYNC_FILE	0x002	/* do fsync on file after printing */
136 #define ADDDATE		0x004	/* add a date to the message */
137 #define MARK		0x008	/* this message is a mark */
138 #define ISKERNEL	0x010	/* kernel generated message */
139 
140 /*
141  * This structure represents the files that will have log
142  * copies printed.
143  */
144 
145 struct filed {
146 	struct	filed *f_next;		/* next in linked list */
147 	short	f_type;			/* entry type, see below */
148 	short	f_file;			/* file descriptor */
149 	time_t	f_time;			/* time this was last written */
150 	char	*f_host;		/* host from which to recd. */
151 	u_char	f_pmask[LOG_NFACILITIES+1];	/* priority mask */
152 	u_char	f_pcmp[LOG_NFACILITIES+1];	/* compare priority */
153 #define PRI_LT	0x1
154 #define PRI_EQ	0x2
155 #define PRI_GT	0x4
156 	char	*f_program;		/* program this applies to */
157 	union {
158 		char	f_uname[MAXUNAMES][UT_NAMESIZE+1];
159 		struct {
160 			char	f_hname[MAXHOSTNAMELEN+1];
161 			struct sockaddr_in	f_addr;
162 		} f_forw;		/* forwarding address */
163 		char	f_fname[MAXPATHLEN];
164 		struct {
165 			char	f_pname[MAXPATHLEN];
166 			pid_t	f_pid;
167 		} f_pipe;
168 	} f_un;
169 	char	f_prevline[MAXSVLINE];		/* last message logged */
170 	char	f_lasttime[16];			/* time of last occurrence */
171 	char	f_prevhost[MAXHOSTNAMELEN+1];	/* host from which recd. */
172 	int	f_prevpri;			/* pri of f_prevline */
173 	int	f_prevlen;			/* length of f_prevline */
174 	int	f_prevcount;			/* repetition cnt of prevline */
175 	int	f_repeatcount;			/* number of "repeated" msgs */
176 };
177 
178 /*
179  * Queue of about-to-be dead processes we should watch out for.
180  */
181 
182 TAILQ_HEAD(stailhead, deadq_entry) deadq_head;
183 struct stailhead *deadq_headp;
184 
185 struct deadq_entry {
186 	pid_t				dq_pid;
187 	int				dq_timeout;
188 	TAILQ_ENTRY(deadq_entry)	dq_entries;
189 };
190 
191 /*
192  * The timeout to apply to processes waiting on the dead queue.  Unit
193  * of measure is `mark intervals', i.e. 20 minutes by default.
194  * Processes on the dead queue will be terminated after that time.
195  */
196 
197 #define DQ_TIMO_INIT	2
198 
199 typedef struct deadq_entry *dq_t;
200 
201 
202 /*
203  * Struct to hold records of network addresses that are allowed to log
204  * to us.
205  */
206 struct allowedpeer {
207 	int isnumeric;
208 	u_short port;
209 	union {
210 		struct {
211 			struct in_addr addr;
212 			struct in_addr mask;
213 		} numeric;
214 		char *name;
215 	} u;
216 #define a_addr u.numeric.addr
217 #define a_mask u.numeric.mask
218 #define a_name u.name
219 };
220 
221 
222 /*
223  * Intervals at which we flush out "message repeated" messages,
224  * in seconds after previous message is logged.  After each flush,
225  * we move to the next interval until we reach the largest.
226  */
227 int	repeatinterval[] = { 30, 120, 600 };	/* # of secs before flush */
228 #define	MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
229 #define	REPEATTIME(f)	((f)->f_time + repeatinterval[(f)->f_repeatcount])
230 #define	BACKOFF(f)	{ if (++(f)->f_repeatcount > MAXREPEAT) \
231 				 (f)->f_repeatcount = MAXREPEAT; \
232 			}
233 
234 /* values for f_type */
235 #define F_UNUSED	0		/* unused entry */
236 #define F_FILE		1		/* regular file */
237 #define F_TTY		2		/* terminal */
238 #define F_CONSOLE	3		/* console terminal */
239 #define F_FORW		4		/* remote machine */
240 #define F_USERS		5		/* list of users */
241 #define F_WALL		6		/* everyone logged on */
242 #define F_PIPE		7		/* pipe to program */
243 
244 char	*TypeNames[8] = {
245 	"UNUSED",	"FILE",		"TTY",		"CONSOLE",
246 	"FORW",		"USERS",	"WALL",		"PIPE"
247 };
248 
249 struct	filed *Files;
250 struct	filed consfile;
251 
252 int	Debug;			/* debug flag */
253 int	resolve = 1;		/* resolve hostname */
254 char	LocalHostName[MAXHOSTNAMELEN+1];	/* our hostname */
255 char	*LocalDomain;		/* our local domain name */
256 int	finet = -1;		/* Internet datagram socket */
257 int	fklog = -1;		/* /dev/klog */
258 int	LogPort;		/* port number for INET connections */
259 int	Initialized = 0;	/* set when we have initialized ourselves */
260 int	MarkInterval = 20 * 60;	/* interval between marks in seconds */
261 int	MarkSeq = 0;		/* mark sequence number */
262 int	SecureMode = 0;		/* when true, receive only unix domain socks */
263 
264 char	bootfile[MAXLINE+1];	/* booted kernel file */
265 
266 struct allowedpeer *AllowedPeers;
267 int	NumAllowed = 0;		/* # of AllowedPeer entries */
268 
269 int	UniquePriority = 0;	/* Only log specified priority? */
270 int	LogFacPri = 0;		/* Put facility and priority in log message: */
271 				/* 0=no, 1=numeric, 2=names */
272 int	KeepKernFac = 0;	/* Keep remotely logged kernel facility */
273 
274 int	allowaddr __P((char *));
275 void	cfline __P((char *, struct filed *, char *, char *));
276 char   *cvthname __P((struct sockaddr_in *));
277 void	deadq_enter __P((pid_t, const char *));
278 int	deadq_remove __P((pid_t));
279 int	decode __P((const char *, CODE *));
280 void	die __P((int));
281 void	domark __P((int));
282 void	fprintlog __P((struct filed *, int, char *));
283 void	init __P((int));
284 void	logerror __P((const char *));
285 void	logmsg __P((int, char *, char *, int));
286 void	log_deadchild __P((pid_t, int, const char *));
287 void	printline __P((char *, char *));
288 void	printsys __P((char *));
289 int	p_open __P((char *, pid_t *));
290 void	readklog __P((void));
291 void	reapchild __P((int));
292 char   *ttymsg __P((struct iovec *, int, char *, int));
293 static void	usage __P((void));
294 int	validate __P((struct sockaddr_in *, const char *));
295 void	wallmsg __P((struct filed *, struct iovec *));
296 int	waitdaemon __P((int, int, int));
297 void	timedout __P((int));
298 
299 int
300 main(argc, argv)
301 	int argc;
302 	char *argv[];
303 {
304 	int ch, i, l;
305 	struct sockaddr_un sunx, fromunix;
306 	struct sockaddr_in sin, frominet;
307 	FILE *fp;
308 	char *p, *hname, line[MAXLINE + 1];
309 	struct timeval tv, *tvp;
310 	struct sigaction sact;
311 	sigset_t mask;
312 	pid_t ppid = 1;
313 	socklen_t len;
314 
315 	while ((ch = getopt(argc, argv, "a:df:kl:m:np:suv")) != -1)
316 		switch (ch) {
317 		case 'a':		/* allow specific network addresses only */
318 			if (allowaddr(optarg) == -1)
319 				usage();
320 			break;
321 		case 'd':		/* debug */
322 			Debug++;
323 			break;
324 		case 'f':		/* configuration file */
325 			ConfFile = optarg;
326 			break;
327 		case 'k':		/* keep remote kern fac */
328 			KeepKernFac = 1;
329 			break;
330 		case 'l':
331 			if (nfunix < MAXFUNIX)
332 				funixn[nfunix++] = optarg;
333 			else
334 				warnx("out of descriptors, ignoring %s",
335 					optarg);
336 			break;
337 		case 'm':		/* mark interval */
338 			MarkInterval = atoi(optarg) * 60;
339 			break;
340 		case 'n':
341 			resolve = 0;
342 			break;
343 		case 'p':		/* path */
344 			funixn[0] = optarg;
345 			break;
346 		case 's':		/* no network mode */
347 			SecureMode++;
348 			break;
349 		case 'u':		/* only log specified priority */
350 		        UniquePriority++;
351 			break;
352 		case 'v':		/* log facility and priority */
353 		  	LogFacPri++;
354 			break;
355 		case '?':
356 		default:
357 			usage();
358 		}
359 	if ((argc -= optind) != 0)
360 		usage();
361 
362 	if (!Debug) {
363 		ppid = waitdaemon(0, 0, 30);
364 		if (ppid < 0)
365 			err(1, "could not become daemon");
366 	} else
367 		setlinebuf(stdout);
368 
369 	if (NumAllowed)
370 		endservent();
371 
372 	consfile.f_type = F_CONSOLE;
373 	(void)strcpy(consfile.f_un.f_fname, ctty + sizeof _PATH_DEV - 1);
374 	(void)gethostname(LocalHostName, sizeof(LocalHostName));
375 	if ((p = strchr(LocalHostName, '.')) != NULL) {
376 		*p++ = '\0';
377 		LocalDomain = p;
378 	} else
379 		LocalDomain = "";
380 	(void)strcpy(bootfile, getbootfile());
381 	(void)signal(SIGTERM, die);
382 	(void)signal(SIGINT, Debug ? die : SIG_IGN);
383 	(void)signal(SIGQUIT, Debug ? die : SIG_IGN);
384 	/*
385 	 * We don't want the SIGCHLD and SIGHUP handlers to interfere
386 	 * with each other; they are likely candidates for being called
387 	 * simultaneously (SIGHUP closes pipe descriptor, process dies,
388 	 * SIGCHLD happens).
389 	 */
390 	sigemptyset(&mask);
391 	sigaddset(&mask, SIGHUP);
392 	sact.sa_handler = reapchild;
393 	sact.sa_mask = mask;
394 	sact.sa_flags = SA_RESTART;
395 	(void)sigaction(SIGCHLD, &sact, NULL);
396 	(void)signal(SIGALRM, domark);
397 	(void)signal(SIGPIPE, SIG_IGN);	/* We'll catch EPIPE instead. */
398 	(void)alarm(TIMERINTVL);
399 
400 	TAILQ_INIT(&deadq_head);
401 
402 #ifndef SUN_LEN
403 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
404 #endif
405 	for (i = 0; i < nfunix; i++) {
406 		memset(&sunx, 0, sizeof(sunx));
407 		sunx.sun_family = AF_UNIX;
408 		(void)strncpy(sunx.sun_path, funixn[i], sizeof(sunx.sun_path));
409 		funix[i] = socket(AF_UNIX, SOCK_DGRAM, 0);
410 		if (funix[i] < 0 ||
411 		    bind(funix[i], (struct sockaddr *)&sunx,
412 			 SUN_LEN(&sunx)) < 0 ||
413 		    chmod(funixn[i], 0666) < 0) {
414 			(void) snprintf(line, sizeof line,
415 					"cannot create %s", funixn[i]);
416 			logerror(line);
417 			dprintf("cannot create %s (%d)\n", funixn[i], errno);
418 			if (i == 0)
419 				die(0);
420 		}
421 	}
422 	if (SecureMode <= 1)
423 		finet = socket(AF_INET, SOCK_DGRAM, 0);
424 	if (finet >= 0) {
425 		struct servent *sp;
426 
427 		sp = getservbyname("syslog", "udp");
428 		if (sp == NULL) {
429 			errno = 0;
430 			logerror("syslog/udp: unknown service");
431 			die(0);
432 		}
433 		memset(&sin, 0, sizeof(sin));
434 		sin.sin_family = AF_INET;
435 		sin.sin_port = LogPort = sp->s_port;
436 
437 		if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
438 			logerror("bind");
439 			if (!Debug)
440 				die(0);
441 		}
442 	}
443 	if (finet >= 0 && SecureMode) {
444 		if (shutdown(finet, SHUT_RD) < 0) {
445 			logerror("shutdown");
446 			if (!Debug)
447 				die(0);
448 		}
449 	}
450 
451 	if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0)
452 		if (fcntl(fklog, F_SETFL, O_NONBLOCK) < 0)
453 			fklog = -1;
454 	if (fklog < 0)
455 		dprintf("can't open %s (%d)\n", _PATH_KLOG, errno);
456 
457 	/* tuck my process id away */
458 	fp = fopen(PidFile, "w");
459 	if (fp != NULL) {
460 		fprintf(fp, "%d\n", getpid());
461 		(void) fclose(fp);
462 	}
463 
464 	dprintf("off & running....\n");
465 
466 	init(0);
467 	/* prevent SIGHUP and SIGCHLD handlers from running in parallel */
468 	sigemptyset(&mask);
469 	sigaddset(&mask, SIGCHLD);
470 	sact.sa_handler = init;
471 	sact.sa_mask = mask;
472 	sact.sa_flags = SA_RESTART;
473 	(void)sigaction(SIGHUP, &sact, NULL);
474 
475 	tvp = &tv;
476 	tv.tv_sec = tv.tv_usec = 0;
477 
478 	for (;;) {
479 		fd_set readfds;
480 		int nfds = 0;
481 
482 		FD_ZERO(&readfds);
483 		if (fklog != -1) {
484 			FD_SET(fklog, &readfds);
485 			if (fklog > nfds)
486 				nfds = fklog;
487 		}
488 		if (finet != -1 && !SecureMode) {
489 			FD_SET(finet, &readfds);
490 			if (finet > nfds)
491 				nfds = finet;
492 		}
493 		for (i = 0; i < nfunix; i++) {
494 			if (funix[i] != -1) {
495 				FD_SET(funix[i], &readfds);
496 				if (funix[i] > nfds)
497 					nfds = funix[i];
498 			}
499 		}
500 
501 		/*dprintf("readfds = %#x\n", readfds);*/
502 		nfds = select(nfds+1, &readfds, (fd_set *)NULL,
503 			      (fd_set *)NULL, tvp);
504 		if (nfds == 0) {
505 			if (tvp) {
506 				tvp = NULL;
507 				if (ppid != 1)
508 					kill(ppid, SIGALRM);
509 			}
510 			continue;
511 		}
512 		if (nfds < 0) {
513 			if (errno != EINTR)
514 				logerror("select");
515 			continue;
516 		}
517 		/*dprintf("got a message (%d, %#x)\n", nfds, readfds);*/
518 		if (fklog != -1 && FD_ISSET(fklog, &readfds))
519 			readklog();
520 		if (finet != -1 && FD_ISSET(finet, &readfds)) {
521 			len = sizeof(frominet);
522 			l = recvfrom(finet, line, MAXLINE, 0,
523 			    (struct sockaddr *)&frominet, &len);
524 			if (l > 0) {
525 				line[l] = '\0';
526 				if (resolve)
527 					hname = cvthname(&frominet);
528 				else
529 					hname = inet_ntoa(frominet.sin_addr);
530 				if (validate(&frominet, hname))
531 					printline(hname, line);
532 			} else if (l < 0 && errno != EINTR)
533 				logerror("recvfrom inet");
534 		}
535 		for (i = 0; i < nfunix; i++) {
536 			if (funix[i] != -1 && FD_ISSET(funix[i], &readfds)) {
537 				len = sizeof(fromunix);
538 				l = recvfrom(funix[i], line, MAXLINE, 0,
539 				    (struct sockaddr *)&fromunix, &len);
540 				if (l > 0) {
541 					line[l] = '\0';
542 					printline(LocalHostName, line);
543 				} else if (l < 0 && errno != EINTR)
544 					logerror("recvfrom unix");
545 			}
546 		}
547 	}
548 }
549 
550 static void
551 usage()
552 {
553 
554 	fprintf(stderr, "%s\n%s\n%s\n",
555 		"usage: syslogd [-dnsuv] [-a allowed_peer] [-f config_file]",
556 		"               [-m mark_interval] [-p log_socket]",
557 		"               [-l log_socket]");
558 	exit(1);
559 }
560 
561 /*
562  * Take a raw input line, decode the message, and print the message
563  * on the appropriate log files.
564  */
565 void
566 printline(hname, msg)
567 	char *hname;
568 	char *msg;
569 {
570 	int c, pri;
571 	char *p, *q, line[MAXLINE + 1];
572 
573 	/* test for special codes */
574 	pri = DEFUPRI;
575 	p = msg;
576 	if (*p == '<') {
577 		pri = 0;
578 		while (isdigit(*++p))
579 			pri = 10 * pri + (*p - '0');
580 		if (*p == '>')
581 			++p;
582 	}
583 	if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
584 		pri = DEFUPRI;
585 
586 	/* don't allow users to log kernel messages */
587 	if (LOG_FAC(pri) == LOG_KERN && !KeepKernFac)
588 		pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
589 
590 	q = line;
591 
592 	while ((c = (unsigned char)*p++) != '\0' &&
593 	    q < &line[sizeof(line) - 4]) {
594 		if ((c & 0x80) && c < 0xA0) {
595 			c &= 0x7F;
596 			*q++ = 'M';
597 			*q++ = '-';
598 		}
599 		if (isascii(c) && iscntrl(c)) {
600 			if (c == '\n')
601 				*q++ = ' ';
602 			else if (c == '\t')
603 				*q++ = '\t';
604 			else {
605 				*q++ = '^';
606 				*q++ = c ^ 0100;
607 			}
608 		} else
609 			*q++ = c;
610 	}
611 	*q = '\0';
612 
613 	logmsg(pri, line, hname, 0);
614 }
615 
616 /*
617  * Read /dev/klog while data are available, split into lines.
618  */
619 void
620 readklog()
621 {
622 	char *p, *q, line[MAXLINE + 1];
623 	int len, i;
624 
625 	len = 0;
626 	for (;;) {
627 		i = read(fklog, line + len, MAXLINE - 1 - len);
628 		if (i > 0)
629 			line[i + len] = '\0';
630 		else if (i < 0 && errno != EINTR && errno != EAGAIN) {
631 			logerror("klog");
632 			fklog = -1;
633 			break;
634 		} else
635 			break;
636 
637 		for (p = line; (q = strchr(p, '\n')) != NULL; p = q + 1) {
638 			*q = '\0';
639 			printsys(p);
640 		}
641 		len = strlen(p);
642 		if (len >= MAXLINE - 1) {
643 			printsys(p);
644 			len = 0;
645 		}
646 		if (len > 0)
647 			memmove(line, p, len + 1);
648 	}
649 	if (len > 0)
650 		printsys(line);
651 }
652 
653 /*
654  * Take a raw input line from /dev/klog, format similar to syslog().
655  */
656 void
657 printsys(p)
658 	char *p;
659 {
660 	int pri, flags;
661 
662 	flags = ISKERNEL | SYNC_FILE | ADDDATE;	/* fsync after write */
663 	pri = DEFSPRI;
664 	if (*p == '<') {
665 		pri = 0;
666 		while (isdigit(*++p))
667 			pri = 10 * pri + (*p - '0');
668 		if (*p == '>')
669 			++p;
670 	} else {
671 		/* kernel printf's come out on console */
672 		flags |= IGN_CONS;
673 	}
674 	if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
675 		pri = DEFSPRI;
676 	logmsg(pri, p, LocalHostName, flags);
677 }
678 
679 time_t	now;
680 
681 /*
682  * Log a message to the appropriate log files, users, etc. based on
683  * the priority.
684  */
685 void
686 logmsg(pri, msg, from, flags)
687 	int pri;
688 	char *msg, *from;
689 	int flags;
690 {
691 	struct filed *f;
692 	int i, fac, msglen, omask, prilev;
693 	char *timestamp;
694  	char prog[NAME_MAX+1];
695 	char buf[MAXLINE+1];
696 
697 	dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n",
698 	    pri, flags, from, msg);
699 
700 	omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
701 
702 	/*
703 	 * Check to see if msg looks non-standard.
704 	 */
705 	msglen = strlen(msg);
706 	if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
707 	    msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
708 		flags |= ADDDATE;
709 
710 	(void)time(&now);
711 	if (flags & ADDDATE)
712 		timestamp = ctime(&now) + 4;
713 	else {
714 		timestamp = msg;
715 		msg += 16;
716 		msglen -= 16;
717 	}
718 
719 	/* skip leading blanks */
720 	while (isspace(*msg)) {
721 		msg++;
722 		msglen--;
723 	}
724 
725 	/* extract facility and priority level */
726 	if (flags & MARK)
727 		fac = LOG_NFACILITIES;
728 	else
729 		fac = LOG_FAC(pri);
730 	prilev = LOG_PRI(pri);
731 
732 	/* extract program name */
733 	for (i = 0; i < NAME_MAX; i++) {
734 		if (!isalnum(msg[i]))
735 			break;
736 		prog[i] = msg[i];
737 	}
738 	prog[i] = 0;
739 
740 	/* add kernel prefix for kernel messages */
741 	if (flags & ISKERNEL) {
742 		snprintf(buf, sizeof(buf), "%s: %s", bootfile, msg);
743 		msg = buf;
744 		msglen = strlen(buf);
745 	}
746 
747 	/* log the message to the particular outputs */
748 	if (!Initialized) {
749 		f = &consfile;
750 		f->f_file = open(ctty, O_WRONLY, 0);
751 
752 		if (f->f_file >= 0) {
753 			fprintlog(f, flags, msg);
754 			(void)close(f->f_file);
755 		}
756 		(void)sigsetmask(omask);
757 		return;
758 	}
759 	for (f = Files; f; f = f->f_next) {
760 		/* skip messages that are incorrect priority */
761 		if (!(((f->f_pcmp[fac] & PRI_EQ) && (f->f_pmask[fac] == prilev))
762 		     ||((f->f_pcmp[fac] & PRI_LT) && (f->f_pmask[fac] < prilev))
763 		     ||((f->f_pcmp[fac] & PRI_GT) && (f->f_pmask[fac] > prilev))
764 		     )
765 		    || f->f_pmask[fac] == INTERNAL_NOPRI)
766 			continue;
767 		/* skip messages with the incorrect hostname */
768 		if (f->f_host)
769 			switch (f->f_host[0]) {
770 			case '+':
771 				if (strcmp(from, f->f_host + 1) != 0)
772 					continue;
773 				break;
774 			case '-':
775 				if (strcmp(from, f->f_host + 1) == 0)
776 					continue;
777 				break;
778 			}
779 
780 		/* skip messages with the incorrect program name */
781 		if (f->f_program)
782 			if (strcmp(prog, f->f_program) != 0)
783 				continue;
784 
785 		if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
786 			continue;
787 
788 		/* don't output marks to recently written files */
789 		if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
790 			continue;
791 
792 		/*
793 		 * suppress duplicate lines to this file
794 		 */
795 		if ((flags & MARK) == 0 && msglen == f->f_prevlen &&
796 		    !strcmp(msg, f->f_prevline) &&
797 		    !strcasecmp(from, f->f_prevhost)) {
798 			(void)strncpy(f->f_lasttime, timestamp, 15);
799 			f->f_prevcount++;
800 			dprintf("msg repeated %d times, %ld sec of %d\n",
801 			    f->f_prevcount, (long)(now - f->f_time),
802 			    repeatinterval[f->f_repeatcount]);
803 			/*
804 			 * If domark would have logged this by now,
805 			 * flush it now (so we don't hold isolated messages),
806 			 * but back off so we'll flush less often
807 			 * in the future.
808 			 */
809 			if (now > REPEATTIME(f)) {
810 				fprintlog(f, flags, (char *)NULL);
811 				BACKOFF(f);
812 			}
813 		} else {
814 			/* new line, save it */
815 			if (f->f_prevcount)
816 				fprintlog(f, 0, (char *)NULL);
817 			f->f_repeatcount = 0;
818 			f->f_prevpri = pri;
819 			(void)strncpy(f->f_lasttime, timestamp, 15);
820 			(void)strncpy(f->f_prevhost, from,
821 					sizeof(f->f_prevhost)-1);
822 			f->f_prevhost[sizeof(f->f_prevhost)-1] = '\0';
823 			if (msglen < MAXSVLINE) {
824 				f->f_prevlen = msglen;
825 				(void)strcpy(f->f_prevline, msg);
826 				fprintlog(f, flags, (char *)NULL);
827 			} else {
828 				f->f_prevline[0] = 0;
829 				f->f_prevlen = 0;
830 				fprintlog(f, flags, msg);
831 			}
832 		}
833 	}
834 	(void)sigsetmask(omask);
835 }
836 
837 void
838 fprintlog(f, flags, msg)
839 	struct filed *f;
840 	int flags;
841 	char *msg;
842 {
843 	struct iovec iov[7];
844 	struct iovec *v;
845 	int l;
846 	char line[MAXLINE + 1], repbuf[80], greetings[200];
847 	char *msgret;
848 
849 	v = iov;
850 	if (f->f_type == F_WALL) {
851 		v->iov_base = greetings;
852 		v->iov_len = snprintf(greetings, sizeof greetings,
853 		    "\r\n\7Message from syslogd@%s at %.24s ...\r\n",
854 		    f->f_prevhost, ctime(&now));
855 		v++;
856 		v->iov_base = "";
857 		v->iov_len = 0;
858 		v++;
859 	} else {
860 		v->iov_base = f->f_lasttime;
861 		v->iov_len = 15;
862 		v++;
863 		v->iov_base = " ";
864 		v->iov_len = 1;
865 		v++;
866 	}
867 
868 	if (LogFacPri) {
869 	  	static char fp_buf[30];	/* Hollow laugh */
870 		int fac = f->f_prevpri & LOG_FACMASK;
871 		int pri = LOG_PRI(f->f_prevpri);
872 		char *f_s = 0;
873 		char f_n[5];	/* Hollow laugh */
874 		char *p_s = 0;
875 		char p_n[5];	/* Hollow laugh */
876 
877 		if (LogFacPri > 1) {
878 		  CODE *c;
879 
880 		  for (c = facilitynames; c->c_name; c++) {
881 		    if (c->c_val == fac) {
882 		      f_s = c->c_name;
883 		      break;
884 		    }
885 		  }
886 		  for (c = prioritynames; c->c_name; c++) {
887 		    if (c->c_val == pri) {
888 		      p_s = c->c_name;
889 		      break;
890 		    }
891 		  }
892 		}
893 		if (!f_s) {
894 		  snprintf(f_n, sizeof f_n, "%d", LOG_FAC(fac));
895 		  f_s = f_n;
896 		}
897 		if (!p_s) {
898 		  snprintf(p_n, sizeof p_n, "%d", pri);
899 		  p_s = p_n;
900 		}
901 		snprintf(fp_buf, sizeof fp_buf, "<%s.%s> ", f_s, p_s);
902 		v->iov_base = fp_buf;
903 		v->iov_len = strlen(fp_buf);
904 	} else {
905 	        v->iov_base="";
906 		v->iov_len = 0;
907 	}
908 	v++;
909 
910 	v->iov_base = f->f_prevhost;
911 	v->iov_len = strlen(v->iov_base);
912 	v++;
913 	v->iov_base = " ";
914 	v->iov_len = 1;
915 	v++;
916 
917 	if (msg) {
918 		v->iov_base = msg;
919 		v->iov_len = strlen(msg);
920 	} else if (f->f_prevcount > 1) {
921 		v->iov_base = repbuf;
922 		v->iov_len = sprintf(repbuf, "last message repeated %d times",
923 		    f->f_prevcount);
924 	} else {
925 		v->iov_base = f->f_prevline;
926 		v->iov_len = f->f_prevlen;
927 	}
928 	v++;
929 
930 	dprintf("Logging to %s", TypeNames[f->f_type]);
931 	f->f_time = now;
932 
933 	switch (f->f_type) {
934 	case F_UNUSED:
935 		dprintf("\n");
936 		break;
937 
938 	case F_FORW:
939 		dprintf(" %s\n", f->f_un.f_forw.f_hname);
940 		/* check for local vs remote messages */
941 		if (strcasecmp(f->f_prevhost, LocalHostName))
942 			l = snprintf(line, sizeof line - 1,
943 			    "<%d>%.15s Forwarded from %s: %s",
944 			    f->f_prevpri, iov[0].iov_base, f->f_prevhost,
945 			    iov[5].iov_base);
946 		else
947 			l = snprintf(line, sizeof line - 1, "<%d>%.15s %s",
948 			     f->f_prevpri, iov[0].iov_base, iov[5].iov_base);
949 		if (l > MAXLINE)
950 			l = MAXLINE;
951 		if ((finet >= 0) &&
952 		     (sendto(finet, line, l, 0,
953 			     (struct sockaddr *)&f->f_un.f_forw.f_addr,
954 			     sizeof(f->f_un.f_forw.f_addr)) != l)) {
955 			int e = errno;
956 			(void)close(f->f_file);
957 			f->f_type = F_UNUSED;
958 			errno = e;
959 			logerror("sendto");
960 		}
961 		break;
962 
963 	case F_FILE:
964 		dprintf(" %s\n", f->f_un.f_fname);
965 		v->iov_base = "\n";
966 		v->iov_len = 1;
967 		if (writev(f->f_file, iov, 7) < 0) {
968 			int e = errno;
969 			(void)close(f->f_file);
970 			f->f_type = F_UNUSED;
971 			errno = e;
972 			logerror(f->f_un.f_fname);
973 		} else if (flags & SYNC_FILE)
974 			(void)fsync(f->f_file);
975 		break;
976 
977 	case F_PIPE:
978 		dprintf(" %s\n", f->f_un.f_pipe.f_pname);
979 		v->iov_base = "\n";
980 		v->iov_len = 1;
981 		if (f->f_un.f_pipe.f_pid == 0) {
982 			if ((f->f_file = p_open(f->f_un.f_pipe.f_pname,
983 						&f->f_un.f_pipe.f_pid)) < 0) {
984 				f->f_type = F_UNUSED;
985 				logerror(f->f_un.f_pipe.f_pname);
986 				break;
987 			}
988 		}
989 		if (writev(f->f_file, iov, 7) < 0) {
990 			int e = errno;
991 			(void)close(f->f_file);
992 			if (f->f_un.f_pipe.f_pid > 0)
993 				deadq_enter(f->f_un.f_pipe.f_pid,
994 					    f->f_un.f_pipe.f_pname);
995 			f->f_un.f_pipe.f_pid = 0;
996 			errno = e;
997 			logerror(f->f_un.f_pipe.f_pname);
998 		}
999 		break;
1000 
1001 	case F_CONSOLE:
1002 		if (flags & IGN_CONS) {
1003 			dprintf(" (ignored)\n");
1004 			break;
1005 		}
1006 		/* FALLTHROUGH */
1007 
1008 	case F_TTY:
1009 		dprintf(" %s%s\n", _PATH_DEV, f->f_un.f_fname);
1010 		v->iov_base = "\r\n";
1011 		v->iov_len = 2;
1012 
1013 		errno = 0;	/* ttymsg() only sometimes returns an errno */
1014 		if ((msgret = ttymsg(iov, 7, f->f_un.f_fname, 10))) {
1015 			f->f_type = F_UNUSED;
1016 			logerror(msgret);
1017 		}
1018 		break;
1019 
1020 	case F_USERS:
1021 	case F_WALL:
1022 		dprintf("\n");
1023 		v->iov_base = "\r\n";
1024 		v->iov_len = 2;
1025 		wallmsg(f, iov);
1026 		break;
1027 	}
1028 	f->f_prevcount = 0;
1029 }
1030 
1031 /*
1032  *  WALLMSG -- Write a message to the world at large
1033  *
1034  *	Write the specified message to either the entire
1035  *	world, or a list of approved users.
1036  */
1037 void
1038 wallmsg(f, iov)
1039 	struct filed *f;
1040 	struct iovec *iov;
1041 {
1042 	static int reenter;			/* avoid calling ourselves */
1043 	FILE *uf;
1044 	struct utmp ut;
1045 	int i;
1046 	char *p;
1047 	char line[sizeof(ut.ut_line) + 1];
1048 
1049 	if (reenter++)
1050 		return;
1051 	if ((uf = fopen(_PATH_UTMP, "r")) == NULL) {
1052 		logerror(_PATH_UTMP);
1053 		reenter = 0;
1054 		return;
1055 	}
1056 	/* NOSTRICT */
1057 	while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) {
1058 		if (ut.ut_name[0] == '\0')
1059 			continue;
1060 		strncpy(line, ut.ut_line, sizeof(ut.ut_line));
1061 		line[sizeof(ut.ut_line)] = '\0';
1062 		if (f->f_type == F_WALL) {
1063 			if ((p = ttymsg(iov, 7, line, TTYMSGTIME)) != NULL) {
1064 				errno = 0;	/* already in msg */
1065 				logerror(p);
1066 			}
1067 			continue;
1068 		}
1069 		/* should we send the message to this user? */
1070 		for (i = 0; i < MAXUNAMES; i++) {
1071 			if (!f->f_un.f_uname[i][0])
1072 				break;
1073 			if (!strncmp(f->f_un.f_uname[i], ut.ut_name,
1074 			    UT_NAMESIZE)) {
1075 				if ((p = ttymsg(iov, 7, line, TTYMSGTIME))
1076 								!= NULL) {
1077 					errno = 0;	/* already in msg */
1078 					logerror(p);
1079 				}
1080 				break;
1081 			}
1082 		}
1083 	}
1084 	(void)fclose(uf);
1085 	reenter = 0;
1086 }
1087 
1088 void
1089 reapchild(signo)
1090 	int signo;
1091 {
1092 	int status;
1093 	pid_t pid;
1094 	struct filed *f;
1095 
1096 	while ((pid = wait3(&status, WNOHANG, (struct rusage *)NULL)) > 0) {
1097 		if (!Initialized)
1098 			/* Don't tell while we are initting. */
1099 			continue;
1100 
1101 		/* First, look if it's a process from the dead queue. */
1102 		if (deadq_remove(pid))
1103 			goto oncemore;
1104 
1105 		/* Now, look in list of active processes. */
1106 		for (f = Files; f; f = f->f_next)
1107 			if (f->f_type == F_PIPE &&
1108 			    f->f_un.f_pipe.f_pid == pid) {
1109 				(void)close(f->f_file);
1110 				f->f_un.f_pipe.f_pid = 0;
1111 				log_deadchild(pid, status,
1112 					      f->f_un.f_pipe.f_pname);
1113 				break;
1114 			}
1115 	  oncemore:
1116 		continue;
1117 	}
1118 }
1119 
1120 /*
1121  * Return a printable representation of a host address.
1122  */
1123 char *
1124 cvthname(f)
1125 	struct sockaddr_in *f;
1126 {
1127 	struct hostent *hp;
1128 	sigset_t omask, nmask;
1129 	char *p;
1130 
1131 	dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr));
1132 
1133 	if (f->sin_family != AF_INET) {
1134 		dprintf("Malformed from address\n");
1135 		return ("???");
1136 	}
1137 	sigemptyset(&nmask);
1138 	sigaddset(&nmask, SIGHUP);
1139 	sigprocmask(SIG_BLOCK, &nmask, &omask);
1140 	hp = gethostbyaddr((char *)&f->sin_addr,
1141 	    sizeof(struct in_addr), f->sin_family);
1142 	sigprocmask(SIG_SETMASK, &omask, NULL);
1143 	if (hp == 0) {
1144 		dprintf("Host name for your address (%s) unknown\n",
1145 			inet_ntoa(f->sin_addr));
1146 		return (inet_ntoa(f->sin_addr));
1147 	}
1148 	if ((p = strchr(hp->h_name, '.')) &&
1149 	    strcasecmp(p + 1, LocalDomain) == 0)
1150 		*p = '\0';
1151 	return (hp->h_name);
1152 }
1153 
1154 void
1155 domark(signo)
1156 	int signo;
1157 {
1158 	struct filed *f;
1159 	dq_t q;
1160 
1161 	now = time((time_t *)NULL);
1162 	MarkSeq += TIMERINTVL;
1163 	if (MarkSeq >= MarkInterval) {
1164 		logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
1165 		MarkSeq = 0;
1166 	}
1167 
1168 	for (f = Files; f; f = f->f_next) {
1169 		if (f->f_prevcount && now >= REPEATTIME(f)) {
1170 			dprintf("flush %s: repeated %d times, %d sec.\n",
1171 			    TypeNames[f->f_type], f->f_prevcount,
1172 			    repeatinterval[f->f_repeatcount]);
1173 			fprintlog(f, 0, (char *)NULL);
1174 			BACKOFF(f);
1175 		}
1176 	}
1177 
1178 	/* Walk the dead queue, and see if we should signal somebody. */
1179 	for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = TAILQ_NEXT(q, dq_entries))
1180 		switch (q->dq_timeout) {
1181 		case 0:
1182 			/* Already signalled once, try harder now. */
1183 			if (kill(q->dq_pid, SIGKILL) != 0)
1184 				(void)deadq_remove(q->dq_pid);
1185 			break;
1186 
1187 		case 1:
1188 			/*
1189 			 * Timed out on dead queue, send terminate
1190 			 * signal.  Note that we leave the removal
1191 			 * from the dead queue to reapchild(), which
1192 			 * will also log the event (unless the process
1193 			 * didn't even really exist, in case we simply
1194 			 * drop it from the dead queue).
1195 			 */
1196 			if (kill(q->dq_pid, SIGTERM) != 0)
1197 				(void)deadq_remove(q->dq_pid);
1198 			/* FALLTHROUGH */
1199 
1200 		default:
1201 			q->dq_timeout--;
1202 		}
1203 
1204 	(void)alarm(TIMERINTVL);
1205 }
1206 
1207 /*
1208  * Print syslogd errors some place.
1209  */
1210 void
1211 logerror(type)
1212 	const char *type;
1213 {
1214 	char buf[512];
1215 
1216 	if (errno)
1217 		(void)snprintf(buf,
1218 		    sizeof buf, "syslogd: %s: %s", type, strerror(errno));
1219 	else
1220 		(void)snprintf(buf, sizeof buf, "syslogd: %s", type);
1221 	errno = 0;
1222 	dprintf("%s\n", buf);
1223 	logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
1224 }
1225 
1226 void
1227 die(signo)
1228 	int signo;
1229 {
1230 	struct filed *f;
1231 	int was_initialized;
1232 	char buf[100];
1233 	int i;
1234 
1235 	was_initialized = Initialized;
1236 	Initialized = 0;	/* Don't log SIGCHLDs. */
1237 	for (f = Files; f != NULL; f = f->f_next) {
1238 		/* flush any pending output */
1239 		if (f->f_prevcount)
1240 			fprintlog(f, 0, (char *)NULL);
1241 		if (f->f_type == F_PIPE)
1242 			(void)close(f->f_file);
1243 	}
1244 	Initialized = was_initialized;
1245 	if (signo) {
1246 		dprintf("syslogd: exiting on signal %d\n", signo);
1247 		(void)sprintf(buf, "exiting on signal %d", signo);
1248 		errno = 0;
1249 		logerror(buf);
1250 	}
1251 	for (i = 0; i < nfunix; i++)
1252 		if (funixn[i] && funix[i] != -1)
1253 			(void)unlink(funixn[i]);
1254 	exit(1);
1255 }
1256 
1257 /*
1258  *  INIT -- Initialize syslogd from configuration table
1259  */
1260 void
1261 init(signo)
1262 	int signo;
1263 {
1264 	int i;
1265 	FILE *cf;
1266 	struct filed *f, *next, **nextp;
1267 	char *p;
1268 	char cline[LINE_MAX];
1269  	char prog[NAME_MAX+1];
1270 	char host[MAXHOSTNAMELEN+1];
1271 
1272 	dprintf("init\n");
1273 
1274 	/*
1275 	 *  Close all open log files.
1276 	 */
1277 	Initialized = 0;
1278 	for (f = Files; f != NULL; f = next) {
1279 		/* flush any pending output */
1280 		if (f->f_prevcount)
1281 			fprintlog(f, 0, (char *)NULL);
1282 
1283 		switch (f->f_type) {
1284 		case F_FILE:
1285 		case F_FORW:
1286 		case F_CONSOLE:
1287 		case F_TTY:
1288 			(void)close(f->f_file);
1289 			break;
1290 		case F_PIPE:
1291 			(void)close(f->f_file);
1292 			if (f->f_un.f_pipe.f_pid > 0)
1293 				deadq_enter(f->f_un.f_pipe.f_pid,
1294 					    f->f_un.f_pipe.f_pname);
1295 			f->f_un.f_pipe.f_pid = 0;
1296 			break;
1297 		}
1298 		next = f->f_next;
1299 		if (f->f_program) free(f->f_program);
1300 		if (f->f_host) free(f->f_host);
1301 		free((char *)f);
1302 	}
1303 	Files = NULL;
1304 	nextp = &Files;
1305 
1306 	/* open the configuration file */
1307 	if ((cf = fopen(ConfFile, "r")) == NULL) {
1308 		dprintf("cannot open %s\n", ConfFile);
1309 		*nextp = (struct filed *)calloc(1, sizeof(*f));
1310 		cfline("*.ERR\t/dev/console", *nextp, "*", "*");
1311 		(*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
1312 		cfline("*.PANIC\t*", (*nextp)->f_next, "*", "*");
1313 		Initialized = 1;
1314 		return;
1315 	}
1316 
1317 	/*
1318 	 *  Foreach line in the conf table, open that file.
1319 	 */
1320 	f = NULL;
1321 	strcpy(host, "*");
1322 	strcpy(prog, "*");
1323 	while (fgets(cline, sizeof(cline), cf) != NULL) {
1324 		/*
1325 		 * check for end-of-section, comments, strip off trailing
1326 		 * spaces and newline character. #!prog is treated specially:
1327 		 * following lines apply only to that program.
1328 		 */
1329 		for (p = cline; isspace(*p); ++p)
1330 			continue;
1331 		if (*p == 0)
1332 			continue;
1333 		if (*p == '#') {
1334 			p++;
1335 			if (*p != '!' && *p != '+' && *p != '-')
1336 				continue;
1337 		}
1338 		if (*p == '+' || *p == '-') {
1339 			host[0] = *p++;
1340 			while (isspace(*p)) p++;
1341 			if ((!*p) || (*p == '*')) {
1342 				strcpy(host, "*");
1343 				continue;
1344 			}
1345 			if (*p == '@')
1346 				p = LocalHostName;
1347 			for (i = 1; i < MAXHOSTNAMELEN; i++) {
1348 				if (!isalnum(*p) && *p != '.' && *p != '-')
1349 					break;
1350 				host[i] = *p++;
1351 			}
1352 			host[i] = '\0';
1353 			continue;
1354 		}
1355 		if (*p == '!') {
1356 			p++;
1357 			while (isspace(*p)) p++;
1358 			if ((!*p) || (*p == '*')) {
1359 				strcpy(prog, "*");
1360 				continue;
1361 			}
1362 			for (i = 0; i < NAME_MAX; i++) {
1363 				if (!isalnum(p[i]))
1364 					break;
1365 				prog[i] = p[i];
1366 			}
1367 			prog[i] = 0;
1368 			continue;
1369 		}
1370 		for (p = strchr(cline, '\0'); isspace(*--p);)
1371 			continue;
1372 		*++p = '\0';
1373 		f = (struct filed *)calloc(1, sizeof(*f));
1374 		*nextp = f;
1375 		nextp = &f->f_next;
1376 		cfline(cline, f, prog, host);
1377 	}
1378 
1379 	/* close the configuration file */
1380 	(void)fclose(cf);
1381 
1382 	Initialized = 1;
1383 
1384 	if (Debug) {
1385 		for (f = Files; f; f = f->f_next) {
1386 			for (i = 0; i <= LOG_NFACILITIES; i++)
1387 				if (f->f_pmask[i] == INTERNAL_NOPRI)
1388 					printf("X ");
1389 				else
1390 					printf("%d ", f->f_pmask[i]);
1391 			printf("%s: ", TypeNames[f->f_type]);
1392 			switch (f->f_type) {
1393 			case F_FILE:
1394 				printf("%s", f->f_un.f_fname);
1395 				break;
1396 
1397 			case F_CONSOLE:
1398 			case F_TTY:
1399 				printf("%s%s", _PATH_DEV, f->f_un.f_fname);
1400 				break;
1401 
1402 			case F_FORW:
1403 				printf("%s", f->f_un.f_forw.f_hname);
1404 				break;
1405 
1406 			case F_PIPE:
1407 				printf("%s", f->f_un.f_pipe.f_pname);
1408 				break;
1409 
1410 			case F_USERS:
1411 				for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
1412 					printf("%s, ", f->f_un.f_uname[i]);
1413 				break;
1414 			}
1415 			if (f->f_program)
1416 				printf(" (%s)", f->f_program);
1417 			printf("\n");
1418 		}
1419 	}
1420 
1421 	logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
1422 	dprintf("syslogd: restarted\n");
1423 }
1424 
1425 /*
1426  * Crack a configuration file line
1427  */
1428 void
1429 cfline(line, f, prog, host)
1430 	char *line;
1431 	struct filed *f;
1432 	char *prog;
1433 	char *host;
1434 {
1435 	struct hostent *hp;
1436 	int i, pri;
1437 	char *bp, *p, *q;
1438 	char buf[MAXLINE], ebuf[100];
1439 
1440 	dprintf("cfline(\"%s\", f, \"%s\", \"%s\")\n", line, prog, host);
1441 
1442 	errno = 0;	/* keep strerror() stuff out of logerror messages */
1443 
1444 	/* clear out file entry */
1445 	memset(f, 0, sizeof(*f));
1446 	for (i = 0; i <= LOG_NFACILITIES; i++)
1447 		f->f_pmask[i] = INTERNAL_NOPRI;
1448 
1449 	/* save hostname if any */
1450 	if (host && *host == '*')
1451 		host = NULL;
1452 	if (host)
1453 		f->f_host = strdup(host);
1454 
1455 	/* save program name if any */
1456 	if (prog && *prog == '*')
1457 		prog = NULL;
1458 	if (prog)
1459 		f->f_program = strdup(prog);
1460 
1461 	/* scan through the list of selectors */
1462 	for (p = line; *p && *p != '\t' && *p != ' ';) {
1463 		int pri_done;
1464 		int pri_cmp;
1465 
1466 		/* find the end of this facility name list */
1467 		for (q = p; *q && *q != '\t' && *q != ' ' && *q++ != '.'; )
1468 			continue;
1469 
1470 		/* get the priority comparison */
1471 		pri_cmp = 0;
1472 		pri_done = 0;
1473 		while (!pri_done) {
1474 			switch (*q) {
1475 			case '<':
1476 				pri_cmp |= PRI_LT;
1477 				q++;
1478 				break;
1479 			case '=':
1480 				pri_cmp |= PRI_EQ;
1481 				q++;
1482 				break;
1483 			case '>':
1484 				pri_cmp |= PRI_GT;
1485 				q++;
1486 				break;
1487 			default:
1488 				pri_done++;
1489 				break;
1490 			}
1491 		}
1492 		if (!pri_cmp)
1493 			pri_cmp = (UniquePriority)
1494 				  ? (PRI_EQ)
1495 				  : (PRI_EQ | PRI_GT)
1496 				  ;
1497 
1498 		/* collect priority name */
1499 		for (bp = buf; *q && !strchr("\t,; ", *q); )
1500 			*bp++ = *q++;
1501 		*bp = '\0';
1502 
1503 		/* skip cruft */
1504 		while (strchr(",;", *q))
1505 			q++;
1506 
1507 		/* decode priority name */
1508 		if (*buf == '*')
1509 			pri = LOG_PRIMASK + 1;
1510 		else {
1511 			pri = decode(buf, prioritynames);
1512 			if (pri < 0) {
1513 				(void)snprintf(ebuf, sizeof ebuf,
1514 				    "unknown priority name \"%s\"", buf);
1515 				logerror(ebuf);
1516 				return;
1517 			}
1518 		}
1519 
1520 		/* scan facilities */
1521 		while (*p && !strchr("\t.; ", *p)) {
1522 			for (bp = buf; *p && !strchr("\t,;. ", *p); )
1523 				*bp++ = *p++;
1524 			*bp = '\0';
1525 
1526 			if (*buf == '*')
1527 				for (i = 0; i < LOG_NFACILITIES; i++) {
1528 					f->f_pmask[i] = pri;
1529 					f->f_pcmp[i] = pri_cmp;
1530 				}
1531 			else {
1532 				i = decode(buf, facilitynames);
1533 				if (i < 0) {
1534 					(void)snprintf(ebuf, sizeof ebuf,
1535 					    "unknown facility name \"%s\"",
1536 					    buf);
1537 					logerror(ebuf);
1538 					return;
1539 				}
1540 				f->f_pmask[i >> 3] = pri;
1541 				f->f_pcmp[i >> 3] = pri_cmp;
1542 			}
1543 			while (*p == ',' || *p == ' ')
1544 				p++;
1545 		}
1546 
1547 		p = q;
1548 	}
1549 
1550 	/* skip to action part */
1551 	while (*p == '\t' || *p == ' ')
1552 		p++;
1553 
1554 	switch (*p)
1555 	{
1556 	case '@':
1557 		(void)strncpy(f->f_un.f_forw.f_hname, ++p,
1558 			sizeof(f->f_un.f_forw.f_hname)-1);
1559 		f->f_un.f_forw.f_hname[sizeof(f->f_un.f_forw.f_hname)-1] = '\0';
1560 		hp = gethostbyname(f->f_un.f_forw.f_hname);
1561 		if (hp == NULL) {
1562 
1563 			logerror(hstrerror(h_errno));
1564 			break;
1565 		}
1566 		memset(&f->f_un.f_forw.f_addr, 0,
1567 			 sizeof(f->f_un.f_forw.f_addr));
1568 		f->f_un.f_forw.f_addr.sin_family = AF_INET;
1569 		f->f_un.f_forw.f_addr.sin_port = LogPort;
1570 		memmove(&f->f_un.f_forw.f_addr.sin_addr, hp->h_addr, hp->h_length);
1571 		f->f_type = F_FORW;
1572 		break;
1573 
1574 	case '/':
1575 		if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
1576 			f->f_type = F_UNUSED;
1577 			logerror(p);
1578 			break;
1579 		}
1580 		if (isatty(f->f_file)) {
1581 			if (strcmp(p, ctty) == 0)
1582 				f->f_type = F_CONSOLE;
1583 			else
1584 				f->f_type = F_TTY;
1585 			(void)strcpy(f->f_un.f_fname, p + sizeof _PATH_DEV - 1);
1586 		} else {
1587 			(void)strcpy(f->f_un.f_fname, p);
1588 			f->f_type = F_FILE;
1589 		}
1590 		break;
1591 
1592 	case '|':
1593 		f->f_un.f_pipe.f_pid = 0;
1594 		(void)strcpy(f->f_un.f_pipe.f_pname, p + 1);
1595 		f->f_type = F_PIPE;
1596 		break;
1597 
1598 	case '*':
1599 		f->f_type = F_WALL;
1600 		break;
1601 
1602 	default:
1603 		for (i = 0; i < MAXUNAMES && *p; i++) {
1604 			for (q = p; *q && *q != ','; )
1605 				q++;
1606 			(void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE);
1607 			if ((q - p) > UT_NAMESIZE)
1608 				f->f_un.f_uname[i][UT_NAMESIZE] = '\0';
1609 			else
1610 				f->f_un.f_uname[i][q - p] = '\0';
1611 			while (*q == ',' || *q == ' ')
1612 				q++;
1613 			p = q;
1614 		}
1615 		f->f_type = F_USERS;
1616 		break;
1617 	}
1618 }
1619 
1620 
1621 /*
1622  *  Decode a symbolic name to a numeric value
1623  */
1624 int
1625 decode(name, codetab)
1626 	const char *name;
1627 	CODE *codetab;
1628 {
1629 	CODE *c;
1630 	char *p, buf[40];
1631 
1632 	if (isdigit(*name))
1633 		return (atoi(name));
1634 
1635 	for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) {
1636 		if (isupper(*name))
1637 			*p = tolower(*name);
1638 		else
1639 			*p = *name;
1640 	}
1641 	*p = '\0';
1642 	for (c = codetab; c->c_name; c++)
1643 		if (!strcmp(buf, c->c_name))
1644 			return (c->c_val);
1645 
1646 	return (-1);
1647 }
1648 
1649 /*
1650  * fork off and become a daemon, but wait for the child to come online
1651  * before returing to the parent, or we get disk thrashing at boot etc.
1652  * Set a timer so we don't hang forever if it wedges.
1653  */
1654 int
1655 waitdaemon(nochdir, noclose, maxwait)
1656 	int nochdir, noclose, maxwait;
1657 {
1658 	int fd;
1659 	int status;
1660 	pid_t pid, childpid;
1661 
1662 	switch (childpid = fork()) {
1663 	case -1:
1664 		return (-1);
1665 	case 0:
1666 		break;
1667 	default:
1668 		signal(SIGALRM, timedout);
1669 		alarm(maxwait);
1670 		while ((pid = wait3(&status, 0, NULL)) != -1) {
1671 			if (WIFEXITED(status))
1672 				errx(1, "child pid %d exited with return code %d",
1673 					pid, WEXITSTATUS(status));
1674 			if (WIFSIGNALED(status))
1675 				errx(1, "child pid %d exited on signal %d%s",
1676 					pid, WTERMSIG(status),
1677 					WCOREDUMP(status) ? " (core dumped)" :
1678 					"");
1679 			if (pid == childpid)	/* it's gone... */
1680 				break;
1681 		}
1682 		exit(0);
1683 	}
1684 
1685 	if (setsid() == -1)
1686 		return (-1);
1687 
1688 	if (!nochdir)
1689 		(void)chdir("/");
1690 
1691 	if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1692 		(void)dup2(fd, STDIN_FILENO);
1693 		(void)dup2(fd, STDOUT_FILENO);
1694 		(void)dup2(fd, STDERR_FILENO);
1695 		if (fd > 2)
1696 			(void)close (fd);
1697 	}
1698 	return (getppid());
1699 }
1700 
1701 /*
1702  * We get a SIGALRM from the child when it's running and finished doing it's
1703  * fsync()'s or O_SYNC writes for all the boot messages.
1704  *
1705  * We also get a signal from the kernel if the timer expires, so check to
1706  * see what happened.
1707  */
1708 void
1709 timedout(sig)
1710 	int sig __unused;
1711 {
1712 	int left;
1713 	left = alarm(0);
1714 	signal(SIGALRM, SIG_DFL);
1715 	if (left == 0)
1716 		errx(1, "timed out waiting for child");
1717 	else
1718 		exit(0);
1719 }
1720 
1721 /*
1722  * Add `s' to the list of allowable peer addresses to accept messages
1723  * from.
1724  *
1725  * `s' is a string in the form:
1726  *
1727  *    [*]domainname[:{servicename|portnumber|*}]
1728  *
1729  * or
1730  *
1731  *    netaddr/maskbits[:{servicename|portnumber|*}]
1732  *
1733  * Returns -1 on error, 0 if the argument was valid.
1734  */
1735 int
1736 allowaddr(s)
1737 	char *s;
1738 {
1739 	char *cp1, *cp2;
1740 	struct allowedpeer ap;
1741 	struct servent *se;
1742 	regex_t re;
1743 	int i;
1744 
1745 	if ((cp1 = strrchr(s, ':'))) {
1746 		/* service/port provided */
1747 		*cp1++ = '\0';
1748 		if (strlen(cp1) == 1 && *cp1 == '*')
1749 			/* any port allowed */
1750 			ap.port = htons(0);
1751 		else if ((se = getservbyname(cp1, "udp")))
1752 			ap.port = se->s_port;
1753 		else {
1754 			ap.port = htons((int)strtol(cp1, &cp2, 0));
1755 			if (*cp2 != '\0')
1756 				return -1; /* port not numeric */
1757 		}
1758 	} else {
1759 		if ((se = getservbyname("syslog", "udp")))
1760 			ap.port = se->s_port;
1761 		else
1762 			/* sanity, should not happen */
1763 			ap.port = htons(514);
1764 	}
1765 
1766 	/* the regexp's are ugly, but the cleanest way */
1767 
1768 	if (regcomp(&re, "^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+(/[0-9]+)?$",
1769 		    REG_EXTENDED))
1770 		/* if RE compilation fails, that's an internal error */
1771 		abort();
1772 	if (regexec(&re, s, 0, 0, 0) == 0) {
1773 		/* arg `s' is numeric */
1774 		ap.isnumeric = 1;
1775 		if ((cp1 = strchr(s, '/')) != NULL) {
1776 			*cp1++ = '\0';
1777 			i = atoi(cp1);
1778 			if (i < 0 || i > 32)
1779 				return -1;
1780 			/* convert masklen to netmask */
1781 			ap.a_mask.s_addr = htonl(~((1 << (32 - i)) - 1));
1782 		}
1783 		if (ascii2addr(AF_INET, s, &ap.a_addr) == -1)
1784 			return -1;
1785 		if (cp1 == NULL) {
1786 			/* use default netmask */
1787 			if (IN_CLASSA(ntohl(ap.a_addr.s_addr)))
1788 				ap.a_mask.s_addr = htonl(IN_CLASSA_NET);
1789 			else if (IN_CLASSB(ntohl(ap.a_addr.s_addr)))
1790 				ap.a_mask.s_addr = htonl(IN_CLASSB_NET);
1791 			else
1792 				ap.a_mask.s_addr = htonl(IN_CLASSC_NET);
1793 		}
1794 	} else {
1795 		/* arg `s' is domain name */
1796 		ap.isnumeric = 0;
1797 		ap.a_name = s;
1798 	}
1799 	regfree(&re);
1800 
1801 	if (Debug) {
1802 		printf("allowaddr: rule %d: ", NumAllowed);
1803 		if (ap.isnumeric) {
1804 			printf("numeric, ");
1805 			printf("addr = %s, ",
1806 			       addr2ascii(AF_INET, &ap.a_addr, sizeof(struct in_addr), 0));
1807 			printf("mask = %s; ",
1808 			       addr2ascii(AF_INET, &ap.a_mask, sizeof(struct in_addr), 0));
1809 		} else
1810 			printf("domainname = %s; ", ap.a_name);
1811 		printf("port = %d\n", ntohs(ap.port));
1812 	}
1813 
1814 	if ((AllowedPeers = realloc(AllowedPeers,
1815 				    ++NumAllowed * sizeof(struct allowedpeer)))
1816 	    == NULL) {
1817 		fprintf(stderr, "Out of memory!\n");
1818 		exit(EX_OSERR);
1819 	}
1820 	memcpy(&AllowedPeers[NumAllowed - 1], &ap, sizeof(struct allowedpeer));
1821 	return 0;
1822 }
1823 
1824 /*
1825  * Validate that the remote peer has permission to log to us.
1826  */
1827 int
1828 validate(sin, hname)
1829 	struct sockaddr_in *sin;
1830 	const char *hname;
1831 {
1832 	int i;
1833 	size_t l1, l2;
1834 	char *cp, name[MAXHOSTNAMELEN];
1835 	struct allowedpeer *ap;
1836 
1837 	if (NumAllowed == 0)
1838 		/* traditional behaviour, allow everything */
1839 		return 1;
1840 
1841 	strlcpy(name, hname, sizeof name);
1842 	if (strchr(name, '.') == NULL) {
1843 		strlcat(name, ".", sizeof name);
1844 		strlcat(name, LocalDomain, sizeof name);
1845 	}
1846 	dprintf("validate: dgram from IP %s, port %d, name %s;\n",
1847 		addr2ascii(AF_INET, &sin->sin_addr, sizeof(struct in_addr), 0),
1848 		ntohs(sin->sin_port), name);
1849 
1850 	/* now, walk down the list */
1851 	for (i = 0, ap = AllowedPeers; i < NumAllowed; i++, ap++) {
1852 		if (ntohs(ap->port) != 0 && ap->port != sin->sin_port) {
1853 			dprintf("rejected in rule %d due to port mismatch.\n", i);
1854 			continue;
1855 		}
1856 
1857 		if (ap->isnumeric) {
1858 			if ((sin->sin_addr.s_addr & ap->a_mask.s_addr)
1859 			    != ap->a_addr.s_addr) {
1860 				dprintf("rejected in rule %d due to IP mismatch.\n", i);
1861 				continue;
1862 			}
1863 		} else {
1864 			cp = ap->a_name;
1865 			l1 = strlen(name);
1866 			if (*cp == '*') {
1867 				/* allow wildmatch */
1868 				cp++;
1869 				l2 = strlen(cp);
1870 				if (l2 > l1 || memcmp(cp, &name[l1 - l2], l2) != 0) {
1871 					dprintf("rejected in rule %d due to name mismatch.\n", i);
1872 					continue;
1873 				}
1874 			} else {
1875 				/* exact match */
1876 				l2 = strlen(cp);
1877 				if (l2 != l1 || memcmp(cp, name, l1) != 0) {
1878 					dprintf("rejected in rule %d due to name mismatch.\n", i);
1879 					continue;
1880 				}
1881 			}
1882 		}
1883 		dprintf("accepted in rule %d.\n", i);
1884 		return 1;	/* hooray! */
1885 	}
1886 	return 0;
1887 }
1888 
1889 /*
1890  * Fairly similar to popen(3), but returns an open descriptor, as
1891  * opposed to a FILE *.
1892  */
1893 int
1894 p_open(prog, pid)
1895 	char *prog;
1896 	pid_t *pid;
1897 {
1898 	int pfd[2], nulldesc, i;
1899 	sigset_t omask, mask;
1900 	char *argv[4]; /* sh -c cmd NULL */
1901 	char errmsg[200];
1902 
1903 	if (pipe(pfd) == -1)
1904 		return -1;
1905 	if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1)
1906 		/* we are royally screwed anyway */
1907 		return -1;
1908 
1909 	sigemptyset(&mask);
1910 	sigaddset(&mask, SIGALRM);
1911 	sigaddset(&mask, SIGHUP);
1912 	sigprocmask(SIG_BLOCK, &mask, &omask);
1913 	switch ((*pid = fork())) {
1914 	case -1:
1915 		sigprocmask(SIG_SETMASK, &omask, 0);
1916 		close(nulldesc);
1917 		return -1;
1918 
1919 	case 0:
1920 		argv[0] = "sh";
1921 		argv[1] = "-c";
1922 		argv[2] = prog;
1923 		argv[3] = NULL;
1924 
1925 		alarm(0);
1926 		(void)setsid();	/* Avoid catching SIGHUPs. */
1927 
1928 		/*
1929 		 * Throw away pending signals, and reset signal
1930 		 * behaviour to standard values.
1931 		 */
1932 		signal(SIGALRM, SIG_IGN);
1933 		signal(SIGHUP, SIG_IGN);
1934 		sigprocmask(SIG_SETMASK, &omask, 0);
1935 		signal(SIGPIPE, SIG_DFL);
1936 		signal(SIGQUIT, SIG_DFL);
1937 		signal(SIGALRM, SIG_DFL);
1938 		signal(SIGHUP, SIG_DFL);
1939 
1940 		dup2(pfd[0], STDIN_FILENO);
1941 		dup2(nulldesc, STDOUT_FILENO);
1942 		dup2(nulldesc, STDERR_FILENO);
1943 		for (i = getdtablesize(); i > 2; i--)
1944 			(void) close(i);
1945 
1946 		(void) execvp(_PATH_BSHELL, argv);
1947 		_exit(255);
1948 	}
1949 
1950 	sigprocmask(SIG_SETMASK, &omask, 0);
1951 	close(nulldesc);
1952 	close(pfd[0]);
1953 	/*
1954 	 * Avoid blocking on a hung pipe.  With O_NONBLOCK, we are
1955 	 * supposed to get an EWOULDBLOCK on writev(2), which is
1956 	 * caught by the logic above anyway, which will in turn close
1957 	 * the pipe, and fork a new logging subprocess if necessary.
1958 	 * The stale subprocess will be killed some time later unless
1959 	 * it terminated itself due to closing its input pipe (so we
1960 	 * get rid of really dead puppies).
1961 	 */
1962 	if (fcntl(pfd[1], F_SETFL, O_NONBLOCK) == -1) {
1963 		/* This is bad. */
1964 		(void)snprintf(errmsg, sizeof errmsg,
1965 			       "Warning: cannot change pipe to PID %d to "
1966 			       "non-blocking behaviour.",
1967 			       (int)*pid);
1968 		logerror(errmsg);
1969 	}
1970 	return pfd[1];
1971 }
1972 
1973 void
1974 deadq_enter(pid, name)
1975 	pid_t pid;
1976 	const char *name;
1977 {
1978 	dq_t p;
1979 	int status;
1980 
1981 	/*
1982 	 * Be paranoid, if we can't signal the process, don't enter it
1983 	 * into the dead queue (perhaps it's already dead).  If possible,
1984 	 * we try to fetch and log the child's status.
1985 	 */
1986 	if (kill(pid, 0) != 0) {
1987 		if (waitpid(pid, &status, WNOHANG) > 0)
1988 			log_deadchild(pid, status, name);
1989 		return;
1990 	}
1991 
1992 	p = malloc(sizeof(struct deadq_entry));
1993 	if (p == 0) {
1994 		errno = 0;
1995 		logerror("panic: out of virtual memory!");
1996 		exit(1);
1997 	}
1998 
1999 	p->dq_pid = pid;
2000 	p->dq_timeout = DQ_TIMO_INIT;
2001 	TAILQ_INSERT_TAIL(&deadq_head, p, dq_entries);
2002 }
2003 
2004 int
2005 deadq_remove(pid)
2006 	pid_t pid;
2007 {
2008 	dq_t q;
2009 
2010 	for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = TAILQ_NEXT(q, dq_entries))
2011 		if (q->dq_pid == pid) {
2012 			TAILQ_REMOVE(&deadq_head, q, dq_entries);
2013 				free(q);
2014 				return 1;
2015 		}
2016 
2017 	return 0;
2018 }
2019 
2020 void
2021 log_deadchild(pid, status, name)
2022 	pid_t pid;
2023 	int status;
2024 	const char *name;
2025 {
2026 	int code;
2027 	char buf[256];
2028 	const char *reason;
2029 
2030 	errno = 0; /* Keep strerror() stuff out of logerror messages. */
2031 	if (WIFSIGNALED(status)) {
2032 		reason = "due to signal";
2033 		code = WTERMSIG(status);
2034 	} else {
2035 		reason = "with status";
2036 		code = WEXITSTATUS(status);
2037 		if (code == 0)
2038 			return;
2039 	}
2040 	(void)snprintf(buf, sizeof buf,
2041 		       "Logging subprocess %d (%s) exited %s %d.",
2042 		       pid, name, reason, code);
2043 	logerror(buf);
2044 }
2045