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