Home | History | Annotate | Line # | Download | only in syslogd
syslogd.c revision 1.74
      1  1.74      matt /*	$NetBSD: syslogd.c,v 1.74 2004/12/09 00:56:47 matt Exp $	*/
      2  1.32        ad 
      3   1.1       cgd /*
      4   1.5     perry  * Copyright (c) 1983, 1988, 1993, 1994
      5   1.5     perry  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.60       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.11  christos #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.11  christos __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993, 1994\n\
     35  1.11  christos 	The Regents of the University of California.  All rights reserved.\n");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39  1.11  christos #if 0
     40  1.11  christos static char sccsid[] = "@(#)syslogd.c	8.3 (Berkeley) 4/4/94";
     41  1.11  christos #else
     42  1.74      matt __RCSID("$NetBSD: syslogd.c,v 1.74 2004/12/09 00:56:47 matt Exp $");
     43  1.11  christos #endif
     44   1.1       cgd #endif /* not lint */
     45   1.1       cgd 
     46   1.1       cgd /*
     47   1.1       cgd  *  syslogd -- log system messages
     48   1.1       cgd  *
     49   1.1       cgd  * This program implements a system log. It takes a series of lines.
     50   1.1       cgd  * Each line may have a priority, signified as "<n>" as
     51   1.1       cgd  * the first characters of the line.  If this is
     52   1.1       cgd  * not present, a default priority is used.
     53   1.1       cgd  *
     54   1.1       cgd  * To kill syslogd, send a signal 15 (terminate).  A signal 1 (hup) will
     55   1.1       cgd  * cause it to reread its configuration file.
     56   1.1       cgd  *
     57   1.1       cgd  * Defined Constants:
     58   1.1       cgd  *
     59   1.1       cgd  * MAXLINE -- the maximimum line length that can be handled.
     60   1.1       cgd  * DEFUPRI -- the default priority for user messages
     61   1.1       cgd  * DEFSPRI -- the default priority for kernel messages
     62   1.1       cgd  *
     63   1.1       cgd  * Author: Eric Allman
     64   1.1       cgd  * extensive changes by Ralph Campbell
     65   1.1       cgd  * more extensive changes by Eric Allman (again)
     66  1.70   thorpej  * Extension to log by program name as well as facility and priority
     67  1.70   thorpej  *   by Peter da Silva.
     68  1.72       wiz  * -U and -v by Harlan Stenn.
     69  1.70   thorpej  * Priority comparison code by Harlan Stenn.
     70   1.1       cgd  */
     71   1.1       cgd 
     72   1.1       cgd #define	MAXLINE		1024		/* maximum line length */
     73   1.1       cgd #define	MAXSVLINE	120		/* maximum saved line length */
     74   1.1       cgd #define DEFUPRI		(LOG_USER|LOG_NOTICE)
     75  1.70   thorpej #define DEFSPRI		(LOG_KERN|LOG_NOTICE)
     76   1.1       cgd #define TIMERINTVL	30		/* interval for checking flush, mark */
     77   1.5     perry #define TTYMSGTIME	1		/* timeout passed to ttymsg */
     78   1.1       cgd 
     79   1.1       cgd #include <sys/param.h>
     80   1.1       cgd #include <sys/socket.h>
     81  1.53       wiz #include <sys/sysctl.h>
     82  1.53       wiz #include <sys/types.h>
     83   1.1       cgd #include <sys/un.h>
     84  1.53       wiz #include <sys/wait.h>
     85  1.70   thorpej #include <sys/queue.h>
     86  1.70   thorpej #include <sys/event.h>
     87   1.1       cgd 
     88  1.56    itojun #include <netinet/in.h>
     89  1.56    itojun 
     90   1.5     perry #include <ctype.h>
     91   1.5     perry #include <errno.h>
     92   1.5     perry #include <fcntl.h>
     93  1.53       wiz #include <grp.h>
     94  1.41      tron #include <locale.h>
     95  1.53       wiz #include <netdb.h>
     96  1.53       wiz #include <pwd.h>
     97   1.5     perry #include <signal.h>
     98  1.53       wiz #include <stdarg.h>
     99   1.1       cgd #include <stdio.h>
    100   1.5     perry #include <stdlib.h>
    101   1.1       cgd #include <string.h>
    102   1.1       cgd #include <unistd.h>
    103  1.53       wiz #include <util.h>
    104  1.53       wiz 
    105  1.55  christos #include "utmpentry.h"
    106   1.1       cgd #include "pathnames.h"
    107   1.1       cgd 
    108   1.1       cgd #define SYSLOG_NAMES
    109   1.1       cgd #include <sys/syslog.h>
    110   1.1       cgd 
    111  1.46    itojun #ifdef LIBWRAP
    112  1.46    itojun #include <tcpd.h>
    113  1.46    itojun 
    114  1.46    itojun int allow_severity = LOG_AUTH|LOG_INFO;
    115  1.46    itojun int deny_severity = LOG_AUTH|LOG_WARNING;
    116  1.46    itojun #endif
    117  1.46    itojun 
    118   1.1       cgd char	*ConfFile = _PATH_LOGCONF;
    119   1.1       cgd char	ctty[] = _PATH_CONSOLE;
    120   1.1       cgd 
    121   1.1       cgd #define FDMASK(fd)	(1 << (fd))
    122   1.1       cgd 
    123   1.1       cgd #define	dprintf		if (Debug) printf
    124   1.1       cgd 
    125   1.1       cgd #define MAXUNAMES	20	/* maximum number of user names */
    126   1.1       cgd 
    127   1.1       cgd /*
    128   1.1       cgd  * Flags to logmsg().
    129   1.1       cgd  */
    130   1.1       cgd 
    131   1.1       cgd #define IGN_CONS	0x001	/* don't print on console */
    132   1.1       cgd #define SYNC_FILE	0x002	/* do fsync on file after printing */
    133   1.1       cgd #define ADDDATE		0x004	/* add a date to the message */
    134   1.1       cgd #define MARK		0x008	/* this message is a mark */
    135  1.70   thorpej #define	ISKERNEL	0x010	/* kernel generated message */
    136   1.1       cgd 
    137   1.1       cgd /*
    138   1.1       cgd  * This structure represents the files that will have log
    139   1.1       cgd  * copies printed.
    140  1.70   thorpej  * We require f_file to be valid if f_type is F_FILE, F_CONSOLE, F_TTY,
    141  1.70   thorpej  * or if f_type is F_PIPE and f_pid > 0.
    142   1.1       cgd  */
    143   1.1       cgd 
    144   1.1       cgd struct filed {
    145   1.1       cgd 	struct	filed *f_next;		/* next in linked list */
    146   1.1       cgd 	short	f_type;			/* entry type, see below */
    147   1.1       cgd 	short	f_file;			/* file descriptor */
    148   1.1       cgd 	time_t	f_time;			/* time this was last written */
    149  1.70   thorpej 	char	*f_host;		/* host from which to record */
    150   1.1       cgd 	u_char	f_pmask[LOG_NFACILITIES+1];	/* priority mask */
    151  1.70   thorpej 	u_char	f_pcmp[LOG_NFACILITIES+1];	/* compare priority */
    152  1.70   thorpej #define	PRI_LT	0x1
    153  1.70   thorpej #define	PRI_EQ	0x2
    154  1.70   thorpej #define	PRI_GT	0x4
    155  1.70   thorpej 	char	*f_program;		/* program this applies to */
    156   1.1       cgd 	union {
    157   1.1       cgd 		char	f_uname[MAXUNAMES][UT_NAMESIZE+1];
    158   1.1       cgd 		struct {
    159  1.70   thorpej 			char	f_hname[MAXHOSTNAMELEN];
    160  1.30    itojun 			struct	addrinfo *f_addr;
    161   1.1       cgd 		} f_forw;		/* forwarding address */
    162   1.1       cgd 		char	f_fname[MAXPATHLEN];
    163  1.70   thorpej 		struct {
    164  1.70   thorpej 			char	f_pname[MAXPATHLEN];
    165  1.70   thorpej 			pid_t	f_pid;
    166  1.70   thorpej 		} f_pipe;
    167   1.1       cgd 	} f_un;
    168   1.1       cgd 	char	f_prevline[MAXSVLINE];		/* last message logged */
    169   1.1       cgd 	char	f_lasttime[16];			/* time of last occurrence */
    170  1.70   thorpej 	char	f_prevhost[MAXHOSTNAMELEN];	/* host from which recd. */
    171   1.1       cgd 	int	f_prevpri;			/* pri of f_prevline */
    172   1.1       cgd 	int	f_prevlen;			/* length of f_prevline */
    173   1.1       cgd 	int	f_prevcount;			/* repetition cnt of prevline */
    174   1.1       cgd 	int	f_repeatcount;			/* number of "repeated" msgs */
    175  1.74      matt 	int	f_lasterror;			/* last error on writev() */
    176  1.70   thorpej 	int	f_flags;			/* file-specific flags */
    177  1.70   thorpej #define	FFLAG_SYNC	0x01
    178   1.1       cgd };
    179   1.1       cgd 
    180   1.1       cgd /*
    181  1.70   thorpej  * Queue of about-to-be-dead processes we should watch out for.
    182  1.70   thorpej  */
    183  1.70   thorpej TAILQ_HEAD(, deadq_entry) deadq_head = TAILQ_HEAD_INITIALIZER(deadq_head);
    184  1.70   thorpej 
    185  1.70   thorpej typedef struct deadq_entry {
    186  1.70   thorpej 	pid_t				dq_pid;
    187  1.70   thorpej 	int				dq_timeout;
    188  1.70   thorpej 	TAILQ_ENTRY(deadq_entry)	dq_entries;
    189  1.70   thorpej } *dq_t;
    190  1.70   thorpej 
    191  1.70   thorpej /*
    192  1.70   thorpej  * The timeout to apply to processes waiting on the dead queue.  Unit
    193  1.70   thorpej  * of measure is "mark intervals", i.e. 20 minutes by default.
    194  1.70   thorpej  * Processes on the dead queue will be terminated after that time.
    195  1.70   thorpej  */
    196  1.70   thorpej #define	DQ_TIMO_INIT	2
    197  1.70   thorpej 
    198  1.70   thorpej /*
    199   1.1       cgd  * Intervals at which we flush out "message repeated" messages,
    200   1.1       cgd  * in seconds after previous message is logged.  After each flush,
    201   1.1       cgd  * we move to the next interval until we reach the largest.
    202   1.1       cgd  */
    203   1.1       cgd int	repeatinterval[] = { 30, 120, 600 };	/* # of secs before flush */
    204   1.1       cgd #define	MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
    205   1.1       cgd #define	REPEATTIME(f)	((f)->f_time + repeatinterval[(f)->f_repeatcount])
    206   1.1       cgd #define	BACKOFF(f)	{ if (++(f)->f_repeatcount > MAXREPEAT) \
    207   1.1       cgd 				 (f)->f_repeatcount = MAXREPEAT; \
    208   1.1       cgd 			}
    209   1.1       cgd 
    210   1.1       cgd /* values for f_type */
    211   1.1       cgd #define F_UNUSED	0		/* unused entry */
    212   1.1       cgd #define F_FILE		1		/* regular file */
    213   1.1       cgd #define F_TTY		2		/* terminal */
    214   1.1       cgd #define F_CONSOLE	3		/* console terminal */
    215   1.1       cgd #define F_FORW		4		/* remote machine */
    216   1.1       cgd #define F_USERS		5		/* list of users */
    217   1.1       cgd #define F_WALL		6		/* everyone logged on */
    218  1.70   thorpej #define	F_PIPE		7		/* pipe to program */
    219   1.1       cgd 
    220  1.70   thorpej char	*TypeNames[8] = {
    221   1.1       cgd 	"UNUSED",	"FILE",		"TTY",		"CONSOLE",
    222  1.70   thorpej 	"FORW",		"USERS",	"WALL",		"PIPE"
    223   1.1       cgd };
    224   1.1       cgd 
    225   1.1       cgd struct	filed *Files;
    226   1.1       cgd struct	filed consfile;
    227   1.1       cgd 
    228   1.1       cgd int	Debug;			/* debug flag */
    229  1.47      manu int	daemonized = 0;		/* we are not daemonized yet */
    230  1.70   thorpej char	LocalHostName[MAXHOSTNAMELEN];	/* our hostname */
    231  1.70   thorpej char	oldLocalHostName[MAXHOSTNAMELEN];/* previous hostname */
    232   1.1       cgd char	*LocalDomain;		/* our local domain name */
    233  1.70   thorpej size_t	LocalDomainLen;		/* length of LocalDomain */
    234  1.59    itojun int	*finet = NULL;		/* Internet datagram sockets */
    235  1.70   thorpej int	Initialized;		/* set when we have initialized ourselves */
    236  1.70   thorpej int	ShuttingDown;		/* set when we die() */
    237   1.1       cgd int	MarkInterval = 20 * 60;	/* interval between marks in seconds */
    238   1.1       cgd int	MarkSeq = 0;		/* mark sequence number */
    239  1.35     jwise int	SecureMode = 0;		/* listen only on unix domain socks */
    240  1.45       mrg int	UseNameService = 1;	/* make domain name queries */
    241  1.36     jwise int	NumForwards = 0;	/* number of forwarding actions in conf file */
    242  1.22       mrg char	**LogPaths;		/* array of pathnames to read messages from */
    243  1.63     lukem int	NoRepeat = 0;		/* disable "repeated"; log always */
    244  1.66   mycroft int	SyncKernel = 0;		/* write kernel messages synchronously */
    245  1.70   thorpej int	UniquePriority = 0;	/* only log specified priority */
    246  1.70   thorpej int	LogFacPri = 0;		/* put facility and priority in log messages: */
    247  1.70   thorpej 				/* 0=no, 1=numeric, 2=names */
    248   1.1       cgd 
    249  1.70   thorpej void	cfline(char *, struct filed *, char *, char *);
    250  1.53       wiz char   *cvthname(struct sockaddr_storage *);
    251  1.70   thorpej void	deadq_enter(pid_t, const char *);
    252  1.70   thorpej int	deadq_remove(pid_t);
    253  1.53       wiz int	decode(const char *, CODE *);
    254  1.70   thorpej void	die(struct kevent *);	/* SIGTERM kevent dispatch routine */
    255  1.70   thorpej void	domark(struct kevent *);/* timer kevent dispatch routine */
    256  1.53       wiz void	fprintlog(struct filed *, int, char *);
    257  1.53       wiz int	getmsgbufsize(void);
    258  1.53       wiz int*	socksetup(int);
    259  1.70   thorpej void	init(struct kevent *);	/* SIGHUP kevent dispatch routine */
    260  1.53       wiz void	logerror(const char *, ...);
    261  1.53       wiz void	logmsg(int, char *, char *, int);
    262  1.70   thorpej void	log_deadchild(pid_t, int, const char *);
    263  1.70   thorpej int	matches_spec(const char *, const char *,
    264  1.70   thorpej 		     char *(*)(const char *, const char *));
    265  1.53       wiz void	printline(char *, char *);
    266  1.53       wiz void	printsys(char *);
    267  1.70   thorpej int	p_open(char *, pid_t *);
    268  1.70   thorpej void	trim_localdomain(char *);
    269  1.70   thorpej void	reapchild(struct kevent *); /* SIGCHLD kevent dispatch routine */
    270  1.53       wiz void	usage(void);
    271  1.53       wiz void	wallmsg(struct filed *, struct iovec *);
    272  1.53       wiz int	main(int, char *[]);
    273  1.53       wiz void	logpath_add(char ***, int *, int *, char *);
    274  1.53       wiz void	logpath_fileadd(char ***, int *, int *, char *);
    275   1.1       cgd 
    276  1.70   thorpej static int fkq;
    277  1.70   thorpej 
    278  1.70   thorpej static struct kevent *allocevchange(void);
    279  1.70   thorpej static int wait_for_events(struct kevent *, size_t);
    280  1.70   thorpej 
    281  1.70   thorpej static void dispatch_read_klog(struct kevent *);
    282  1.70   thorpej static void dispatch_read_finet(struct kevent *);
    283  1.70   thorpej static void dispatch_read_funix(struct kevent *);
    284  1.70   thorpej 
    285  1.70   thorpej /*
    286  1.70   thorpej  * Global line buffer.  Since we only process one event at a time,
    287  1.70   thorpej  * a global one will do.
    288  1.70   thorpej  */
    289  1.70   thorpej static char *linebuf;
    290  1.70   thorpej static size_t linebufsize;
    291  1.70   thorpej 
    292  1.70   thorpej #define	A_CNT(x)	(sizeof((x)) / sizeof((x)[0]))
    293  1.70   thorpej 
    294   1.5     perry int
    295  1.53       wiz main(int argc, char *argv[])
    296   1.1       cgd {
    297  1.70   thorpej 	int ch, *funix, j, fklog;
    298  1.22       mrg 	int funixsize = 0, funixmaxsize = 0;
    299  1.70   thorpej 	struct kevent events[16];
    300  1.70   thorpej 	struct sockaddr_un sunx;
    301  1.70   thorpej 	char **pp;
    302  1.70   thorpej 	struct kevent *ev;
    303  1.47      manu 	uid_t uid = 0;
    304  1.47      manu 	gid_t gid = 0;
    305  1.47      manu 	char *user = NULL;
    306  1.47      manu 	char *group = NULL;
    307  1.47      manu 	char *root = "/";
    308  1.47      manu 	char *endp;
    309  1.47      manu 	struct group   *gr;
    310  1.47      manu 	struct passwd  *pw;
    311  1.57    itojun 	unsigned long l;
    312  1.41      tron 
    313  1.41      tron 	(void)setlocale(LC_ALL, "");
    314   1.1       cgd 
    315  1.70   thorpej 	while ((ch = getopt(argc, argv, "dnsSf:m:p:P:ru:g:t:Uv")) != -1)
    316   1.5     perry 		switch(ch) {
    317  1.53       wiz 		case 'd':		/* debug */
    318  1.53       wiz 			Debug++;
    319  1.53       wiz 			break;
    320  1.53       wiz 		case 'f':		/* configuration file */
    321  1.53       wiz 			ConfFile = optarg;
    322  1.47      manu 			break;
    323  1.47      manu 		case 'g':
    324  1.47      manu 			group = optarg;
    325  1.47      manu 			if (*group == '\0')
    326  1.47      manu 				usage();
    327  1.47      manu 			break;
    328   1.1       cgd 		case 'm':		/* mark interval */
    329   1.1       cgd 			MarkInterval = atoi(optarg) * 60;
    330   1.1       cgd 			break;
    331  1.45       mrg 		case 'n':		/* turn off DNS queries */
    332  1.45       mrg 			UseNameService = 0;
    333  1.45       mrg 			break;
    334   1.1       cgd 		case 'p':		/* path */
    335  1.22       mrg 			logpath_add(&LogPaths, &funixsize,
    336  1.22       mrg 			    &funixmaxsize, optarg);
    337  1.22       mrg 			break;
    338  1.22       mrg 		case 'P':		/* file of paths */
    339  1.22       mrg 			logpath_fileadd(&LogPaths, &funixsize,
    340  1.22       mrg 			    &funixmaxsize, optarg);
    341   1.1       cgd 			break;
    342  1.63     lukem 		case 'r':		/* disable "repeated" compression */
    343  1.63     lukem 			NoRepeat++;
    344  1.63     lukem 			break;
    345  1.35     jwise 		case 's':		/* no network listen mode */
    346   1.7     perry 			SecureMode++;
    347   1.7     perry 			break;
    348  1.66   mycroft 		case 'S':
    349  1.66   mycroft 			SyncKernel = 1;
    350  1.66   mycroft 			break;
    351  1.53       wiz 		case 't':
    352  1.53       wiz 			root = optarg;
    353  1.53       wiz 			if (*root == '\0')
    354  1.53       wiz 				usage();
    355  1.53       wiz 			break;
    356  1.53       wiz 		case 'u':
    357  1.53       wiz 			user = optarg;
    358  1.53       wiz 			if (*user == '\0')
    359  1.53       wiz 				usage();
    360  1.53       wiz 			break;
    361  1.70   thorpej 		case 'U':		/* only log specified priority */
    362  1.70   thorpej 			UniquePriority = 1;
    363  1.70   thorpej 			break;
    364  1.70   thorpej 		case 'v':		/* log facility and priority */
    365  1.70   thorpej 			if (LogFacPri < 2)
    366  1.70   thorpej 				LogFacPri++;
    367  1.70   thorpej 			break;
    368   1.1       cgd 		default:
    369   1.1       cgd 			usage();
    370   1.1       cgd 		}
    371   1.5     perry 	if ((argc -= optind) != 0)
    372   1.1       cgd 		usage();
    373   1.1       cgd 
    374  1.47      manu 	setlinebuf(stdout);
    375  1.47      manu 
    376  1.47      manu 	if (user != NULL) {
    377  1.47      manu 		if (isdigit((unsigned char)*user)) {
    378  1.57    itojun 			errno = 0;
    379  1.57    itojun 			endp = NULL;
    380  1.57    itojun 			l = strtoul(user, &endp, 0);
    381  1.57    itojun 			if (errno || *endp != '\0')
    382  1.47      manu 	    			goto getuser;
    383  1.57    itojun 			uid = (uid_t)l;
    384  1.57    itojun 			if (uid != l) {
    385  1.57    itojun 				errno = 0;
    386  1.57    itojun 				logerror("UID out of range");
    387  1.70   thorpej 				die(NULL);
    388  1.57    itojun 			}
    389  1.47      manu 		} else {
    390  1.47      manu getuser:
    391  1.47      manu 			if ((pw = getpwnam(user)) != NULL) {
    392  1.47      manu 				uid = pw->pw_uid;
    393  1.47      manu 			} else {
    394  1.47      manu 				errno = 0;
    395  1.47      manu 				logerror("Cannot find user `%s'", user);
    396  1.70   thorpej 				die(NULL);
    397  1.47      manu 			}
    398  1.47      manu 		}
    399  1.47      manu 	}
    400  1.47      manu 
    401  1.47      manu 	if (group != NULL) {
    402  1.47      manu 		if (isdigit((unsigned char)*group)) {
    403  1.57    itojun 			errno = 0;
    404  1.57    itojun 			endp = NULL;
    405  1.57    itojun 			l = strtoul(group, &endp, 0);
    406  1.57    itojun 			if (errno || *endp != '\0')
    407  1.47      manu 	    			goto getgroup;
    408  1.57    itojun 			gid = (gid_t)l;
    409  1.57    itojun 			if (gid != l) {
    410  1.57    itojun 				errno = 0;
    411  1.57    itojun 				logerror("GID out of range");
    412  1.70   thorpej 				die(NULL);
    413  1.57    itojun 			}
    414  1.47      manu 		} else {
    415  1.47      manu getgroup:
    416  1.47      manu 			if ((gr = getgrnam(group)) != NULL) {
    417  1.47      manu 				gid = gr->gr_gid;
    418  1.47      manu 			} else {
    419  1.47      manu 				errno = 0;
    420  1.47      manu 				logerror("Cannot find group `%s'", group);
    421  1.70   thorpej 				die(NULL);
    422  1.47      manu 			}
    423  1.47      manu 		}
    424  1.47      manu 	}
    425  1.47      manu 
    426  1.70   thorpej 	if (access(root, F_OK | R_OK)) {
    427  1.53       wiz 		logerror("Cannot access `%s'", root);
    428  1.70   thorpej 		die(NULL);
    429  1.47      manu 	}
    430   1.1       cgd 
    431   1.1       cgd 	consfile.f_type = F_CONSOLE;
    432  1.58    itojun 	(void)strlcpy(consfile.f_un.f_fname, ctty,
    433  1.58    itojun 	    sizeof(consfile.f_un.f_fname));
    434  1.70   thorpej 	linebufsize = getmsgbufsize();
    435  1.70   thorpej 	if (linebufsize < MAXLINE)
    436  1.70   thorpej 		linebufsize = MAXLINE;
    437  1.70   thorpej 	linebufsize++;
    438  1.70   thorpej 	linebuf = malloc(linebufsize);
    439  1.70   thorpej 	if (linebuf == NULL) {
    440  1.47      manu 		logerror("Couldn't allocate line buffer");
    441  1.70   thorpej 		die(NULL);
    442  1.15       leo 	}
    443   1.5     perry 
    444   1.5     perry #ifndef SUN_LEN
    445   1.5     perry #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
    446   1.5     perry #endif
    447  1.24      tron 	if (funixsize == 0)
    448  1.24      tron 		logpath_add(&LogPaths, &funixsize,
    449  1.24      tron 		    &funixmaxsize, _PATH_LOG);
    450  1.22       mrg 	funix = (int *)malloc(sizeof(int) * funixsize);
    451  1.22       mrg 	if (funix == NULL) {
    452  1.47      manu 		logerror("Couldn't allocate funix descriptors");
    453  1.70   thorpej 		die(NULL);
    454   1.1       cgd 	}
    455  1.26      tron 	for (j = 0, pp = LogPaths; *pp; pp++, j++) {
    456  1.47      manu 		dprintf("Making unix dgram socket `%s'\n", *pp);
    457  1.22       mrg 		unlink(*pp);
    458  1.22       mrg 		memset(&sunx, 0, sizeof(sunx));
    459  1.22       mrg 		sunx.sun_family = AF_LOCAL;
    460  1.22       mrg 		(void)strncpy(sunx.sun_path, *pp, sizeof(sunx.sun_path));
    461  1.22       mrg 		funix[j] = socket(AF_LOCAL, SOCK_DGRAM, 0);
    462  1.22       mrg 		if (funix[j] < 0 || bind(funix[j],
    463  1.22       mrg 		    (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 ||
    464  1.22       mrg 		    chmod(*pp, 0666) < 0) {
    465  1.47      manu 			logerror("Cannot create `%s'", *pp);
    466  1.70   thorpej 			die(NULL);
    467  1.22       mrg 		}
    468  1.47      manu 		dprintf("Listening on unix dgram socket `%s'\n", *pp);
    469  1.22       mrg 	}
    470   1.7     perry 
    471  1.22       mrg 	if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) < 0) {
    472  1.47      manu 		dprintf("Can't open `%s' (%d)\n", _PATH_KLOG, errno);
    473  1.22       mrg 	} else {
    474  1.47      manu 		dprintf("Listening on kernel log `%s'\n", _PATH_KLOG);
    475   1.1       cgd 	}
    476   1.1       cgd 
    477  1.47      manu 	/*
    478  1.47      manu 	 * All files are open, we can drop privileges and chroot
    479  1.47      manu 	 */
    480  1.53       wiz 	dprintf("Attempt to chroot to `%s'\n", root);
    481  1.53       wiz 	if (chroot(root)) {
    482  1.53       wiz 		logerror("Failed to chroot to `%s'", root);
    483  1.70   thorpej 		die(NULL);
    484  1.47      manu 	}
    485  1.53       wiz 	dprintf("Attempt to set GID/EGID to `%d'\n", gid);
    486  1.53       wiz 	if (setgid(gid) || setegid(gid)) {
    487  1.53       wiz 		logerror("Failed to set gid to `%d'", gid);
    488  1.70   thorpej 		die(NULL);
    489  1.47      manu 	}
    490  1.53       wiz 	dprintf("Attempt to set UID/EUID to `%d'\n", uid);
    491  1.53       wiz 	if (setuid(uid) || seteuid(uid)) {
    492  1.53       wiz 		logerror("Failed to set uid to `%d'", uid);
    493  1.70   thorpej 		die(NULL);
    494  1.47      manu 	}
    495  1.47      manu 
    496  1.47      manu 	/*
    497  1.47      manu 	 * We cannot detach from the terminal before we are sure we won't
    498  1.47      manu 	 * have a fatal error, because error message would not go to the
    499  1.47      manu 	 * terminal and would not be logged because syslogd dies.
    500  1.47      manu 	 * All die() calls are behind us, we can call daemon()
    501  1.47      manu 	 */
    502  1.47      manu 	if (!Debug) {
    503  1.47      manu 		(void)daemon(0, 0);
    504  1.47      manu 		daemonized = 1;
    505  1.48      taca 
    506  1.48      taca 		/* tuck my process id away, if i'm not in debug mode */
    507  1.48      taca 		pidfile(NULL);
    508  1.47      manu 	}
    509  1.47      manu 
    510  1.70   thorpej 	/*
    511  1.70   thorpej 	 * Create the global kernel event descriptor.
    512  1.70   thorpej 	 *
    513  1.70   thorpej 	 * NOTE: We MUST do this after daemon(), bacause the kqueue()
    514  1.70   thorpej 	 * API dictates that kqueue descriptors are not inherited
    515  1.70   thorpej 	 * across forks (lame!).
    516  1.70   thorpej 	 */
    517  1.70   thorpej 	if ((fkq = kqueue()) < 0) {
    518  1.70   thorpej 		logerror("Cannot create event queue");
    519  1.70   thorpej 		die(NULL);	/* XXX This error is lost! */
    520  1.70   thorpej 	}
    521  1.70   thorpej 
    522  1.70   thorpej 	/*
    523  1.71   thorpej 	 * We must read the configuration file for the first time
    524  1.71   thorpej 	 * after the kqueue descriptor is created, because we install
    525  1.71   thorpej 	 * events during this process.
    526  1.71   thorpej 	 */
    527  1.71   thorpej 	init(NULL);
    528  1.71   thorpej 
    529  1.71   thorpej 	/*
    530  1.70   thorpej 	 * Always exit on SIGTERM.  Also exit on SIGINT and SIGQUIT
    531  1.70   thorpej 	 * if we're debugging.
    532  1.70   thorpej 	 */
    533  1.70   thorpej 	(void)signal(SIGTERM, SIG_IGN);
    534  1.70   thorpej 	(void)signal(SIGINT, SIG_IGN);
    535  1.70   thorpej 	(void)signal(SIGQUIT, SIG_IGN);
    536  1.70   thorpej 	ev = allocevchange();
    537  1.70   thorpej 	EV_SET(ev, SIGTERM, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0,
    538  1.70   thorpej 	    (intptr_t) die);
    539  1.70   thorpej 	if (Debug) {
    540  1.70   thorpej 		ev = allocevchange();
    541  1.70   thorpej 		EV_SET(ev, SIGINT, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0,
    542  1.70   thorpej 		    (intptr_t) die);
    543  1.70   thorpej 
    544  1.70   thorpej 		ev = allocevchange();
    545  1.70   thorpej 		EV_SET(ev, SIGQUIT, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0,
    546  1.70   thorpej 		    (intptr_t) die);
    547  1.70   thorpej 	}
    548  1.70   thorpej 
    549  1.70   thorpej 	ev = allocevchange();
    550  1.70   thorpej 	EV_SET(ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0,
    551  1.70   thorpej 	    (intptr_t) reapchild);
    552  1.70   thorpej 
    553  1.70   thorpej 	ev = allocevchange();
    554  1.70   thorpej 	EV_SET(ev, 0, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0,
    555  1.70   thorpej 	    TIMERINTVL * 1000 /* seconds -> ms */, (intptr_t) domark);
    556  1.70   thorpej 
    557  1.70   thorpej 	(void)signal(SIGPIPE, SIG_IGN);	/* We'll catch EPIPE instead. */
    558  1.70   thorpej 
    559  1.70   thorpej 	/* Re-read configuration on SIGHUP. */
    560  1.70   thorpej 	(void) signal(SIGHUP, SIG_IGN);
    561  1.70   thorpej 	ev = allocevchange();
    562  1.70   thorpej 	EV_SET(ev, SIGHUP, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0,
    563  1.70   thorpej 	    (intptr_t) init);
    564  1.70   thorpej 
    565  1.70   thorpej 	if (fklog >= 0) {
    566  1.70   thorpej 		ev = allocevchange();
    567  1.70   thorpej 		EV_SET(ev, fklog, EVFILT_READ, EV_ADD | EV_ENABLE,
    568  1.70   thorpej 		    0, 0, (intptr_t) dispatch_read_klog);
    569  1.70   thorpej 	}
    570  1.70   thorpej 	for (j = 0, pp = LogPaths; *pp; pp++, j++) {
    571  1.70   thorpej 		ev = allocevchange();
    572  1.70   thorpej 		EV_SET(ev, funix[j], EVFILT_READ, EV_ADD | EV_ENABLE,
    573  1.70   thorpej 		    0, 0, (intptr_t) dispatch_read_funix);
    574  1.70   thorpej 	}
    575  1.70   thorpej 
    576  1.70   thorpej 	dprintf("Off & running....\n");
    577  1.68      heas 
    578   1.1       cgd 	for (;;) {
    579  1.70   thorpej 		void (*handler)(struct kevent *);
    580  1.70   thorpej 		int i, rv;
    581   1.1       cgd 
    582  1.70   thorpej 		rv = wait_for_events(events, A_CNT(events));
    583  1.22       mrg 		if (rv == 0)
    584   1.1       cgd 			continue;
    585  1.22       mrg 		if (rv < 0) {
    586   1.1       cgd 			if (errno != EINTR)
    587  1.70   thorpej 				logerror("kevent() failed");
    588   1.1       cgd 			continue;
    589   1.1       cgd 		}
    590  1.70   thorpej 		dprintf("Got an event (%d)\n", rv);
    591  1.70   thorpej 		for (i = 0; i < rv; i++) {
    592  1.70   thorpej 			handler = (void *) events[i].udata;
    593  1.70   thorpej 			(*handler)(&events[i]);
    594  1.30    itojun 		}
    595   1.1       cgd 	}
    596   1.1       cgd }
    597   1.1       cgd 
    598   1.5     perry void
    599  1.53       wiz usage(void)
    600   1.1       cgd {
    601   1.5     perry 
    602   1.5     perry 	(void)fprintf(stderr,
    603  1.70   thorpej 	    "usage: %s [-dnrSsUv] [-f config_file] [-g group] [-m mark_interval]\n"
    604  1.53       wiz 	    "\t[-P file_list] [-p log_socket [-p log_socket2 ...]]\n"
    605  1.53       wiz 	    "\t[-t chroot_dir] [-u user]\n", getprogname());
    606   1.1       cgd 	exit(1);
    607   1.1       cgd }
    608   1.1       cgd 
    609   1.1       cgd /*
    610  1.70   thorpej  * Dispatch routine for reading /dev/klog
    611  1.70   thorpej  */
    612  1.70   thorpej static void
    613  1.70   thorpej dispatch_read_klog(struct kevent *ev)
    614  1.70   thorpej {
    615  1.70   thorpej 	ssize_t rv;
    616  1.70   thorpej 	int fd = ev->ident;
    617  1.70   thorpej 
    618  1.70   thorpej 	dprintf("Kernel log active\n");
    619  1.70   thorpej 
    620  1.70   thorpej 	rv = read(fd, linebuf, linebufsize - 1);
    621  1.70   thorpej 	if (rv > 0) {
    622  1.70   thorpej 		linebuf[rv] = '\0';
    623  1.70   thorpej 		printsys(linebuf);
    624  1.70   thorpej 	} else if (rv < 0 && errno != EINTR) {
    625  1.70   thorpej 		/*
    626  1.70   thorpej 		 * /dev/klog has croaked.  Disable the event
    627  1.70   thorpej 		 * so it won't bother us again.
    628  1.70   thorpej 		 */
    629  1.70   thorpej 		struct kevent *cev = allocevchange();
    630  1.70   thorpej 		logerror("klog failed");
    631  1.70   thorpej 		EV_SET(cev, fd, EVFILT_READ, EV_DISABLE,
    632  1.70   thorpej 		    0, 0, (intptr_t) dispatch_read_klog);
    633  1.70   thorpej 	}
    634  1.70   thorpej }
    635  1.70   thorpej 
    636  1.70   thorpej /*
    637  1.70   thorpej  * Dispatch routine for reading Unix domain sockets.
    638  1.70   thorpej  */
    639  1.70   thorpej static void
    640  1.70   thorpej dispatch_read_funix(struct kevent *ev)
    641  1.70   thorpej {
    642  1.70   thorpej 	struct sockaddr_un myname, fromunix;
    643  1.70   thorpej 	ssize_t rv;
    644  1.70   thorpej 	socklen_t sunlen;
    645  1.70   thorpej 	int fd = ev->ident;
    646  1.70   thorpej 
    647  1.70   thorpej 	sunlen = sizeof(myname);
    648  1.70   thorpej 	if (getsockname(fd, (struct sockaddr *)&myname, &sunlen) != 0) {
    649  1.70   thorpej 		/*
    650  1.70   thorpej 		 * This should never happen, so ensure that it doesn't
    651  1.70   thorpej 		 * happen again.
    652  1.70   thorpej 		 */
    653  1.70   thorpej 		struct kevent *cev = allocevchange();
    654  1.70   thorpej 		logerror("getsockname() unix failed");
    655  1.70   thorpej 		EV_SET(cev, fd, EVFILT_READ, EV_DISABLE,
    656  1.70   thorpej 		    0, 0, (intptr_t) dispatch_read_funix);
    657  1.70   thorpej 		return;
    658  1.70   thorpej 	}
    659  1.70   thorpej 
    660  1.70   thorpej 	dprintf("Unix socket (%s) active\n", myname.sun_path);
    661  1.70   thorpej 
    662  1.70   thorpej 	sunlen = sizeof(fromunix);
    663  1.70   thorpej 	rv = recvfrom(fd, linebuf, MAXLINE, 0,
    664  1.70   thorpej 	    (struct sockaddr *)&fromunix, &sunlen);
    665  1.70   thorpej 	if (rv > 0) {
    666  1.70   thorpej 		linebuf[rv] = '\0';
    667  1.70   thorpej 		printline(LocalHostName, linebuf);
    668  1.70   thorpej 	} else if (rv < 0 && errno != EINTR) {
    669  1.70   thorpej 		logerror("recvfrom() unix `%s'", myname.sun_path);
    670  1.70   thorpej 	}
    671  1.70   thorpej }
    672  1.70   thorpej 
    673  1.70   thorpej /*
    674  1.70   thorpej  * Dispatch routine for reading Internet sockets.
    675  1.70   thorpej  */
    676  1.70   thorpej static void
    677  1.70   thorpej dispatch_read_finet(struct kevent *ev)
    678  1.70   thorpej {
    679  1.70   thorpej #ifdef LIBWRAP
    680  1.70   thorpej 	struct request_info req;
    681  1.70   thorpej #endif
    682  1.70   thorpej 	struct sockaddr_storage frominet;
    683  1.70   thorpej 	ssize_t rv;
    684  1.70   thorpej 	socklen_t len;
    685  1.70   thorpej 	int fd = ev->ident;
    686  1.70   thorpej 	int reject = 0;
    687  1.70   thorpej 
    688  1.70   thorpej 	dprintf("inet socket active\n");
    689  1.70   thorpej 
    690  1.70   thorpej #ifdef LIBWRAP
    691  1.70   thorpej 	request_init(&req, RQ_DAEMON, "syslogd", RQ_FILE, fd, NULL);
    692  1.70   thorpej 	fromhost(&req);
    693  1.70   thorpej 	reject = !hosts_access(&req);
    694  1.70   thorpej 	if (reject)
    695  1.70   thorpej 		dprintf("access denied\n");
    696  1.70   thorpej #endif
    697  1.70   thorpej 
    698  1.70   thorpej 	len = sizeof(frominet);
    699  1.70   thorpej 	rv = recvfrom(fd, linebuf, MAXLINE, 0,
    700  1.70   thorpej 	    (struct sockaddr *)&frominet, &len);
    701  1.70   thorpej 	if (rv == 0 || (rv < 0 && errno == EINTR))
    702  1.70   thorpej 		return;
    703  1.70   thorpej 	else if (rv < 0) {
    704  1.70   thorpej 		logerror("recvfrom inet");
    705  1.70   thorpej 		return;
    706  1.70   thorpej 	}
    707  1.70   thorpej 
    708  1.70   thorpej 	linebuf[rv] = '\0';
    709  1.70   thorpej 	if (!reject)
    710  1.70   thorpej 		printline(cvthname(&frominet), linebuf);
    711  1.70   thorpej }
    712  1.70   thorpej 
    713  1.70   thorpej /*
    714  1.22       mrg  * given a pointer to an array of char *'s, a pointer to it's current
    715  1.22       mrg  * size and current allocated max size, and a new char * to add, add
    716  1.22       mrg  * it, update everything as necessary, possibly allocating a new array
    717  1.22       mrg  */
    718  1.22       mrg void
    719  1.53       wiz logpath_add(char ***lp, int *szp, int *maxszp, char *new)
    720  1.22       mrg {
    721  1.62    itojun 	char **nlp;
    722  1.62    itojun 	int newmaxsz;
    723  1.22       mrg 
    724  1.47      manu 	dprintf("Adding `%s' to the %p logpath list\n", new, *lp);
    725  1.22       mrg 	if (*szp == *maxszp) {
    726  1.22       mrg 		if (*maxszp == 0) {
    727  1.62    itojun 			newmaxsz = 4;	/* start of with enough for now */
    728  1.26      tron 			*lp = NULL;
    729  1.50     lukem 		} else
    730  1.62    itojun 			newmaxsz = *maxszp * 2;
    731  1.62    itojun 		nlp = realloc(*lp, sizeof(char *) * (newmaxsz + 1));
    732  1.62    itojun 		if (nlp == NULL) {
    733  1.47      manu 			logerror("Couldn't allocate line buffer");
    734  1.70   thorpej 			die(NULL);
    735  1.22       mrg 		}
    736  1.62    itojun 		*lp = nlp;
    737  1.62    itojun 		*maxszp = newmaxsz;
    738  1.22       mrg 	}
    739  1.50     lukem 	if (((*lp)[(*szp)++] = strdup(new)) == NULL) {
    740  1.50     lukem 		logerror("Couldn't allocate logpath");
    741  1.70   thorpej 		die(NULL);
    742  1.50     lukem 	}
    743  1.26      tron 	(*lp)[(*szp)] = NULL;		/* always keep it NULL terminated */
    744  1.22       mrg }
    745  1.22       mrg 
    746  1.22       mrg /* do a file of log sockets */
    747  1.22       mrg void
    748  1.53       wiz logpath_fileadd(char ***lp, int *szp, int *maxszp, char *file)
    749  1.22       mrg {
    750  1.22       mrg 	FILE *fp;
    751  1.22       mrg 	char *line;
    752  1.22       mrg 	size_t len;
    753  1.22       mrg 
    754  1.22       mrg 	fp = fopen(file, "r");
    755  1.22       mrg 	if (fp == NULL) {
    756  1.47      manu 		logerror("Could not open socket file list `%s'", file);
    757  1.70   thorpej 		die(NULL);
    758  1.22       mrg 	}
    759  1.22       mrg 
    760  1.22       mrg 	while ((line = fgetln(fp, &len))) {
    761  1.22       mrg 		line[len - 1] = 0;
    762  1.22       mrg 		logpath_add(lp, szp, maxszp, line);
    763  1.22       mrg 	}
    764  1.22       mrg 	fclose(fp);
    765  1.22       mrg }
    766  1.22       mrg 
    767  1.22       mrg /*
    768   1.1       cgd  * Take a raw input line, decode the message, and print the message
    769   1.1       cgd  * on the appropriate log files.
    770   1.1       cgd  */
    771   1.5     perry void
    772  1.53       wiz printline(char *hname, char *msg)
    773   1.1       cgd {
    774   1.5     perry 	int c, pri;
    775   1.5     perry 	char *p, *q, line[MAXLINE + 1];
    776  1.70   thorpej 	long n;
    777   1.1       cgd 
    778   1.1       cgd 	/* test for special codes */
    779   1.1       cgd 	pri = DEFUPRI;
    780   1.1       cgd 	p = msg;
    781   1.1       cgd 	if (*p == '<') {
    782  1.70   thorpej 		errno = 0;
    783  1.70   thorpej 		n = strtol(p + 1, &q, 10);
    784  1.70   thorpej 		if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) {
    785  1.70   thorpej 			p = q + 1;
    786  1.70   thorpej 			pri = (int)n;
    787  1.70   thorpej 		}
    788   1.1       cgd 	}
    789   1.1       cgd 	if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
    790   1.1       cgd 		pri = DEFUPRI;
    791   1.1       cgd 
    792  1.70   thorpej 	/*
    793  1.70   thorpej 	 * Don't allow users to log kernel messages.
    794  1.70   thorpej 	 * NOTE: Since LOG_KERN == 0, this will also match
    795  1.70   thorpej 	 *	 messages with no facility specified.
    796  1.70   thorpej 	 */
    797  1.70   thorpej 	if ((pri & LOG_FACMASK) == LOG_KERN)
    798   1.1       cgd 		pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
    799   1.1       cgd 
    800   1.1       cgd 	q = line;
    801   1.1       cgd 
    802  1.42  sommerfe 	while ((c = *p++) != '\0' &&
    803  1.42  sommerfe 	    q < &line[sizeof(line) - 2]) {
    804  1.42  sommerfe 		c &= 0177;
    805   1.1       cgd 		if (iscntrl(c))
    806   1.1       cgd 			if (c == '\n')
    807   1.1       cgd 				*q++ = ' ';
    808   1.1       cgd 			else if (c == '\t')
    809   1.1       cgd 				*q++ = '\t';
    810   1.1       cgd 			else {
    811   1.1       cgd 				*q++ = '^';
    812   1.1       cgd 				*q++ = c ^ 0100;
    813   1.1       cgd 			}
    814   1.1       cgd 		else
    815   1.1       cgd 			*q++ = c;
    816  1.42  sommerfe 	}
    817   1.1       cgd 	*q = '\0';
    818   1.1       cgd 
    819   1.1       cgd 	logmsg(pri, line, hname, 0);
    820   1.1       cgd }
    821   1.1       cgd 
    822   1.1       cgd /*
    823   1.1       cgd  * Take a raw input line from /dev/klog, split and format similar to syslog().
    824   1.1       cgd  */
    825   1.5     perry void
    826  1.53       wiz printsys(char *msg)
    827   1.1       cgd {
    828  1.70   thorpej 	int n, pri, flags, is_printf;
    829  1.70   thorpej 	char *p, *q;
    830   1.1       cgd 
    831   1.1       cgd 	for (p = msg; *p != '\0'; ) {
    832  1.70   thorpej 		flags = ISKERNEL | ADDDATE;
    833  1.66   mycroft 		if (SyncKernel)
    834  1.66   mycroft 			flags |= SYNC_FILE;
    835   1.1       cgd 		pri = DEFSPRI;
    836  1.70   thorpej 		is_printf = 1;
    837   1.1       cgd 		if (*p == '<') {
    838  1.70   thorpej 			errno = 0;
    839  1.70   thorpej 			n = (int)strtol(p + 1, &q, 10);
    840  1.70   thorpej 			if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) {
    841  1.70   thorpej 				p = q + 1;
    842  1.70   thorpej 				pri = n;
    843  1.70   thorpej 				is_printf = 0;
    844  1.70   thorpej 			}
    845  1.70   thorpej 		}
    846  1.70   thorpej 		if (is_printf) {
    847   1.1       cgd 			/* kernel printf's come out on console */
    848   1.1       cgd 			flags |= IGN_CONS;
    849   1.1       cgd 		}
    850   1.1       cgd 		if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
    851   1.1       cgd 			pri = DEFSPRI;
    852  1.70   thorpej 		for (q = p; *q != '\0' && *q != '\n'; q++)
    853  1.70   thorpej 			/* look for end of line */;
    854  1.70   thorpej 		if (*q != '\0')
    855  1.70   thorpej 			*q++ = '\0';
    856  1.70   thorpej 		logmsg(pri, p, LocalHostName, flags);
    857  1.70   thorpej 		p = q;
    858   1.1       cgd 	}
    859   1.1       cgd }
    860   1.1       cgd 
    861   1.1       cgd time_t	now;
    862   1.1       cgd 
    863   1.1       cgd /*
    864  1.70   thorpej  * Check to see if `name' matches the provided specification, using the
    865  1.70   thorpej  * specified strstr function.
    866  1.70   thorpej  */
    867  1.70   thorpej int
    868  1.70   thorpej matches_spec(const char *name, const char *spec,
    869  1.70   thorpej     char *(*check)(const char *, const char *))
    870  1.70   thorpej {
    871  1.70   thorpej 	const char *s;
    872  1.70   thorpej 	char prev, next;
    873  1.70   thorpej 
    874  1.70   thorpej 	if ((s = (*check)(spec, name)) != NULL) {
    875  1.70   thorpej 		prev = s == spec ? ',' : *(s - 1);
    876  1.70   thorpej 		next = *(s + strlen(name));
    877  1.70   thorpej 
    878  1.70   thorpej 		if (prev == ',' && (next == '\0' || next == ','))
    879  1.70   thorpej 			return (1);
    880  1.70   thorpej 	}
    881  1.70   thorpej 
    882  1.70   thorpej 	return (0);
    883  1.70   thorpej }
    884  1.70   thorpej 
    885  1.70   thorpej /*
    886   1.1       cgd  * Log a message to the appropriate log files, users, etc. based on
    887   1.1       cgd  * the priority.
    888   1.1       cgd  */
    889   1.5     perry void
    890  1.53       wiz logmsg(int pri, char *msg, char *from, int flags)
    891   1.1       cgd {
    892   1.5     perry 	struct filed *f;
    893  1.70   thorpej 	int fac, msglen, omask, prilev, i;
    894   1.1       cgd 	char *timestamp;
    895  1.70   thorpej 	char prog[NAME_MAX + 1];
    896  1.70   thorpej 	char buf[MAXLINE + 1];
    897   1.1       cgd 
    898   1.9       mrg 	dprintf("logmsg: pri 0%o, flags 0x%x, from %s, msg %s\n",
    899   1.1       cgd 	    pri, flags, from, msg);
    900   1.1       cgd 
    901   1.1       cgd 	omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
    902   1.1       cgd 
    903   1.1       cgd 	/*
    904   1.1       cgd 	 * Check to see if msg looks non-standard.
    905   1.1       cgd 	 */
    906   1.1       cgd 	msglen = strlen(msg);
    907   1.1       cgd 	if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
    908   1.1       cgd 	    msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
    909   1.1       cgd 		flags |= ADDDATE;
    910   1.1       cgd 
    911   1.5     perry 	(void)time(&now);
    912   1.1       cgd 	if (flags & ADDDATE)
    913   1.1       cgd 		timestamp = ctime(&now) + 4;
    914   1.1       cgd 	else {
    915   1.1       cgd 		timestamp = msg;
    916   1.1       cgd 		msg += 16;
    917   1.1       cgd 		msglen -= 16;
    918   1.1       cgd 	}
    919   1.1       cgd 
    920  1.70   thorpej 	/* skip leading whitespace */
    921  1.70   thorpej 	while (isspace((unsigned char)*msg)) {
    922  1.70   thorpej 		msg++;
    923  1.70   thorpej 		msglen--;
    924  1.70   thorpej 	}
    925  1.70   thorpej 
    926   1.1       cgd 	/* extract facility and priority level */
    927   1.1       cgd 	if (flags & MARK)
    928   1.1       cgd 		fac = LOG_NFACILITIES;
    929   1.1       cgd 	else
    930   1.1       cgd 		fac = LOG_FAC(pri);
    931   1.1       cgd 	prilev = LOG_PRI(pri);
    932   1.1       cgd 
    933  1.70   thorpej 	/* extract program name */
    934  1.70   thorpej 	for (i = 0; i < NAME_MAX; i++) {
    935  1.70   thorpej 		if (!isprint((unsigned char)msg[i]) ||
    936  1.70   thorpej 		    msg[i] == ':' || msg[i] == '[')
    937  1.70   thorpej 			break;
    938  1.70   thorpej 		prog[i] = msg[i];
    939  1.70   thorpej 	}
    940  1.70   thorpej 	prog[i] = '\0';
    941  1.70   thorpej 
    942  1.70   thorpej 	/* add kernel prefix for kernel messages */
    943  1.70   thorpej 	if (flags & ISKERNEL) {
    944  1.70   thorpej 		snprintf(buf, sizeof(buf), "%s: %s",
    945  1.70   thorpej 		    _PATH_UNIX, msg);
    946  1.70   thorpej 		msg = buf;
    947  1.70   thorpej 		msglen = strlen(buf);
    948  1.70   thorpej 	}
    949  1.70   thorpej 
    950   1.1       cgd 	/* log the message to the particular outputs */
    951   1.1       cgd 	if (!Initialized) {
    952   1.1       cgd 		f = &consfile;
    953   1.1       cgd 		f->f_file = open(ctty, O_WRONLY, 0);
    954   1.1       cgd 
    955   1.1       cgd 		if (f->f_file >= 0) {
    956  1.70   thorpej 			(void)strncpy(f->f_lasttime, timestamp, 15);
    957   1.1       cgd 			fprintlog(f, flags, msg);
    958   1.5     perry 			(void)close(f->f_file);
    959   1.1       cgd 		}
    960   1.5     perry 		(void)sigsetmask(omask);
    961   1.1       cgd 		return;
    962   1.1       cgd 	}
    963   1.1       cgd 	for (f = Files; f; f = f->f_next) {
    964   1.1       cgd 		/* skip messages that are incorrect priority */
    965  1.70   thorpej 		if (!(((f->f_pcmp[fac] & PRI_EQ) && (f->f_pmask[fac] == prilev))
    966  1.70   thorpej 		     ||((f->f_pcmp[fac] & PRI_LT) && (f->f_pmask[fac] < prilev))
    967  1.70   thorpej 		     ||((f->f_pcmp[fac] & PRI_GT) && (f->f_pmask[fac] > prilev))
    968  1.70   thorpej 		     )
    969  1.70   thorpej 		    || f->f_pmask[fac] == INTERNAL_NOPRI)
    970   1.1       cgd 			continue;
    971   1.1       cgd 
    972  1.70   thorpej 		/* skip messages with the incorrect host name */
    973  1.70   thorpej 		if (f->f_host != NULL) {
    974  1.70   thorpej 			switch (f->f_host[0]) {
    975  1.70   thorpej 			case '+':
    976  1.70   thorpej 				if (! matches_spec(from, f->f_host + 1,
    977  1.70   thorpej 						   strcasestr))
    978  1.70   thorpej 					continue;
    979  1.70   thorpej 				break;
    980  1.70   thorpej 			case '-':
    981  1.70   thorpej 				if (matches_spec(from, f->f_host + 1,
    982  1.70   thorpej 						 strcasestr))
    983  1.70   thorpej 					continue;
    984  1.70   thorpej 				break;
    985  1.70   thorpej 			}
    986  1.70   thorpej 		}
    987  1.70   thorpej 
    988  1.70   thorpej 		/* skip messages with the incorrect program name */
    989  1.70   thorpej 		if (f->f_program != NULL) {
    990  1.70   thorpej 			switch (f->f_program[0]) {
    991  1.70   thorpej 			case '+':
    992  1.70   thorpej 				if (! matches_spec(prog, f->f_program + 1,
    993  1.70   thorpej 						   strstr))
    994  1.70   thorpej 					continue;
    995  1.70   thorpej 				break;
    996  1.70   thorpej 			case '-':
    997  1.70   thorpej 				if (matches_spec(prog, f->f_program + 1,
    998  1.70   thorpej 						 strstr))
    999  1.70   thorpej 					continue;
   1000  1.70   thorpej 				break;
   1001  1.70   thorpej 			default:
   1002  1.70   thorpej 				if (! matches_spec(prog, f->f_program,
   1003  1.70   thorpej 						   strstr))
   1004  1.70   thorpej 					continue;
   1005  1.70   thorpej 				break;
   1006  1.70   thorpej 			}
   1007  1.70   thorpej 		}
   1008  1.70   thorpej 
   1009   1.1       cgd 		if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
   1010   1.1       cgd 			continue;
   1011   1.1       cgd 
   1012   1.1       cgd 		/* don't output marks to recently written files */
   1013   1.1       cgd 		if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
   1014   1.1       cgd 			continue;
   1015   1.1       cgd 
   1016   1.1       cgd 		/*
   1017  1.63     lukem 		 * suppress duplicate lines to this file unless NoRepeat
   1018   1.1       cgd 		 */
   1019   1.1       cgd 		if ((flags & MARK) == 0 && msglen == f->f_prevlen &&
   1020  1.63     lukem 		    !NoRepeat &&
   1021   1.1       cgd 		    !strcmp(msg, f->f_prevline) &&
   1022  1.70   thorpej 		    !strcasecmp(from, f->f_prevhost)) {
   1023   1.5     perry 			(void)strncpy(f->f_lasttime, timestamp, 15);
   1024   1.1       cgd 			f->f_prevcount++;
   1025  1.47      manu 			dprintf("Msg repeated %d times, %ld sec of %d\n",
   1026  1.12   thorpej 			    f->f_prevcount, (long)(now - f->f_time),
   1027   1.1       cgd 			    repeatinterval[f->f_repeatcount]);
   1028   1.1       cgd 			/*
   1029   1.1       cgd 			 * If domark would have logged this by now,
   1030   1.1       cgd 			 * flush it now (so we don't hold isolated messages),
   1031   1.1       cgd 			 * but back off so we'll flush less often
   1032   1.1       cgd 			 * in the future.
   1033   1.1       cgd 			 */
   1034   1.1       cgd 			if (now > REPEATTIME(f)) {
   1035   1.1       cgd 				fprintlog(f, flags, (char *)NULL);
   1036   1.1       cgd 				BACKOFF(f);
   1037   1.1       cgd 			}
   1038   1.1       cgd 		} else {
   1039   1.1       cgd 			/* new line, save it */
   1040   1.1       cgd 			if (f->f_prevcount)
   1041   1.1       cgd 				fprintlog(f, 0, (char *)NULL);
   1042   1.1       cgd 			f->f_repeatcount = 0;
   1043   1.3       cgd 			f->f_prevpri = pri;
   1044   1.5     perry 			(void)strncpy(f->f_lasttime, timestamp, 15);
   1045   1.5     perry 			(void)strncpy(f->f_prevhost, from,
   1046   1.1       cgd 					sizeof(f->f_prevhost));
   1047   1.1       cgd 			if (msglen < MAXSVLINE) {
   1048   1.1       cgd 				f->f_prevlen = msglen;
   1049  1.58    itojun 				(void)strlcpy(f->f_prevline, msg,
   1050  1.58    itojun 				    sizeof(f->f_prevline));
   1051   1.1       cgd 				fprintlog(f, flags, (char *)NULL);
   1052   1.1       cgd 			} else {
   1053   1.1       cgd 				f->f_prevline[0] = 0;
   1054   1.1       cgd 				f->f_prevlen = 0;
   1055   1.1       cgd 				fprintlog(f, flags, msg);
   1056   1.1       cgd 			}
   1057   1.1       cgd 		}
   1058   1.1       cgd 	}
   1059   1.5     perry 	(void)sigsetmask(omask);
   1060   1.1       cgd }
   1061   1.1       cgd 
   1062   1.5     perry void
   1063  1.53       wiz fprintlog(struct filed *f, int flags, char *msg)
   1064   1.1       cgd {
   1065  1.70   thorpej 	struct iovec iov[7];
   1066   1.5     perry 	struct iovec *v;
   1067  1.30    itojun 	struct addrinfo *r;
   1068  1.31    itojun 	int j, l, lsent;
   1069   1.1       cgd 	char line[MAXLINE + 1], repbuf[80], greetings[200];
   1070   1.1       cgd 
   1071   1.1       cgd 	v = iov;
   1072   1.1       cgd 	if (f->f_type == F_WALL) {
   1073   1.1       cgd 		v->iov_base = greetings;
   1074  1.17       mrg 		v->iov_len = snprintf(greetings, sizeof greetings,
   1075   1.1       cgd 		    "\r\n\7Message from syslogd@%s at %.24s ...\r\n",
   1076   1.1       cgd 		    f->f_prevhost, ctime(&now));
   1077   1.1       cgd 		v++;
   1078   1.1       cgd 		v->iov_base = "";
   1079   1.1       cgd 		v->iov_len = 0;
   1080   1.1       cgd 		v++;
   1081   1.1       cgd 	} else {
   1082   1.1       cgd 		v->iov_base = f->f_lasttime;
   1083   1.1       cgd 		v->iov_len = 15;
   1084   1.1       cgd 		v++;
   1085   1.1       cgd 		v->iov_base = " ";
   1086   1.1       cgd 		v->iov_len = 1;
   1087   1.1       cgd 		v++;
   1088   1.1       cgd 	}
   1089  1.70   thorpej 
   1090  1.70   thorpej 	if (LogFacPri) {
   1091  1.70   thorpej 		static char fp_buf[30];
   1092  1.70   thorpej 		const char *f_s = NULL, *p_s = NULL;
   1093  1.70   thorpej 		int fac = f->f_prevpri & LOG_FACMASK;
   1094  1.70   thorpej 		int pri = LOG_PRI(f->f_prevpri);
   1095  1.70   thorpej 		char f_n[5], p_n[5];
   1096  1.70   thorpej 
   1097  1.70   thorpej 		if (LogFacPri > 1) {
   1098  1.70   thorpej 			CODE *c;
   1099  1.70   thorpej 
   1100  1.70   thorpej 			for (c = facilitynames; c->c_name != NULL; c++) {
   1101  1.70   thorpej 				if (c->c_val == fac) {
   1102  1.70   thorpej 					f_s = c->c_name;
   1103  1.70   thorpej 					break;
   1104  1.70   thorpej 				}
   1105  1.70   thorpej 			}
   1106  1.70   thorpej 			for (c = prioritynames; c->c_name != NULL; c++) {
   1107  1.70   thorpej 				if (c->c_val == pri) {
   1108  1.70   thorpej 					p_s = c->c_name;
   1109  1.70   thorpej 					break;
   1110  1.70   thorpej 				}
   1111  1.70   thorpej 			}
   1112  1.70   thorpej 		}
   1113  1.70   thorpej 		if (f_s == NULL) {
   1114  1.70   thorpej 			snprintf(f_n, sizeof(f_n), "%d", LOG_FAC(fac));
   1115  1.70   thorpej 			f_s = f_n;
   1116  1.70   thorpej 		}
   1117  1.70   thorpej 		if (p_s == NULL) {
   1118  1.70   thorpej 			snprintf(p_n, sizeof(p_n), "%d", pri);
   1119  1.70   thorpej 			p_s = p_n;
   1120  1.70   thorpej 		}
   1121  1.70   thorpej 		snprintf(fp_buf, sizeof(fp_buf), "<%s.%s>", f_s, p_s);
   1122  1.70   thorpej 		v->iov_base = fp_buf;
   1123  1.70   thorpej 		v->iov_len = strlen(fp_buf);
   1124  1.70   thorpej 	} else {
   1125  1.70   thorpej 		v->iov_base = "";
   1126  1.70   thorpej 		v->iov_len = 0;
   1127  1.70   thorpej 	}
   1128  1.70   thorpej 	v++;
   1129  1.70   thorpej 
   1130   1.1       cgd 	v->iov_base = f->f_prevhost;
   1131   1.1       cgd 	v->iov_len = strlen(v->iov_base);
   1132   1.1       cgd 	v++;
   1133   1.1       cgd 	v->iov_base = " ";
   1134   1.1       cgd 	v->iov_len = 1;
   1135   1.1       cgd 	v++;
   1136   1.1       cgd 
   1137   1.1       cgd 	if (msg) {
   1138   1.1       cgd 		v->iov_base = msg;
   1139   1.1       cgd 		v->iov_len = strlen(msg);
   1140   1.1       cgd 	} else if (f->f_prevcount > 1) {
   1141   1.1       cgd 		v->iov_base = repbuf;
   1142  1.17       mrg 		v->iov_len = snprintf(repbuf, sizeof repbuf,
   1143  1.17       mrg 		    "last message repeated %d times", f->f_prevcount);
   1144   1.1       cgd 	} else {
   1145   1.1       cgd 		v->iov_base = f->f_prevline;
   1146   1.1       cgd 		v->iov_len = f->f_prevlen;
   1147   1.1       cgd 	}
   1148   1.1       cgd 	v++;
   1149   1.1       cgd 
   1150   1.1       cgd 	dprintf("Logging to %s", TypeNames[f->f_type]);
   1151   1.1       cgd 	f->f_time = now;
   1152   1.1       cgd 
   1153   1.1       cgd 	switch (f->f_type) {
   1154   1.1       cgd 	case F_UNUSED:
   1155   1.1       cgd 		dprintf("\n");
   1156   1.1       cgd 		break;
   1157   1.1       cgd 
   1158   1.1       cgd 	case F_FORW:
   1159   1.1       cgd 		dprintf(" %s\n", f->f_un.f_forw.f_hname);
   1160  1.34     lukem 			/*
   1161  1.34     lukem 			 * check for local vs remote messages
   1162  1.34     lukem 			 * (from FreeBSD PR#bin/7055)
   1163  1.34     lukem 			 */
   1164  1.70   thorpej 		if (strcasecmp(f->f_prevhost, LocalHostName)) {
   1165  1.21      tron 			l = snprintf(line, sizeof(line) - 1,
   1166  1.21      tron 				     "<%d>%.15s [%s]: %s",
   1167  1.21      tron 				     f->f_prevpri, (char *) iov[0].iov_base,
   1168  1.70   thorpej 				     f->f_prevhost, (char *) iov[5].iov_base);
   1169  1.21      tron 		} else {
   1170  1.21      tron 			l = snprintf(line, sizeof(line) - 1, "<%d>%.15s %s",
   1171  1.21      tron 				     f->f_prevpri, (char *) iov[0].iov_base,
   1172  1.70   thorpej 				     (char *) iov[5].iov_base);
   1173  1.21      tron 		}
   1174   1.1       cgd 		if (l > MAXLINE)
   1175   1.1       cgd 			l = MAXLINE;
   1176  1.34     lukem 		if (finet) {
   1177  1.30    itojun 			for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) {
   1178  1.31    itojun 				for (j = 0; j < *finet; j++) {
   1179  1.31    itojun #if 0
   1180  1.34     lukem 					/*
   1181  1.34     lukem 					 * should we check AF first, or just
   1182  1.34     lukem 					 * trial and error? FWD
   1183  1.34     lukem 					 */
   1184  1.34     lukem 					if (r->ai_family ==
   1185  1.34     lukem 					    address_family_of(finet[j+1]))
   1186  1.30    itojun #endif
   1187  1.34     lukem 					lsent = sendto(finet[j+1], line, l, 0,
   1188  1.34     lukem 					    r->ai_addr, r->ai_addrlen);
   1189  1.31    itojun 					if (lsent == l)
   1190  1.31    itojun 						break;
   1191  1.31    itojun 				}
   1192  1.30    itojun 			}
   1193  1.30    itojun 			if (lsent != l) {
   1194  1.30    itojun 				f->f_type = F_UNUSED;
   1195  1.47      manu 				logerror("sendto() failed");
   1196  1.30    itojun 			}
   1197   1.1       cgd 		}
   1198   1.1       cgd 		break;
   1199   1.1       cgd 
   1200  1.70   thorpej 	case F_PIPE:
   1201  1.70   thorpej 		dprintf(" %s\n", f->f_un.f_pipe.f_pname);
   1202  1.70   thorpej 		v->iov_base = "\n";
   1203  1.70   thorpej 		v->iov_len = 1;
   1204  1.70   thorpej 		if (f->f_un.f_pipe.f_pid == 0) {
   1205  1.70   thorpej 			if ((f->f_file = p_open(f->f_un.f_pipe.f_pname,
   1206  1.70   thorpej 						&f->f_un.f_pipe.f_pid)) < 0) {
   1207  1.70   thorpej 				f->f_type = F_UNUSED;
   1208  1.70   thorpej 				logerror(f->f_un.f_pipe.f_pname);
   1209  1.70   thorpej 				break;
   1210  1.70   thorpej 			}
   1211  1.70   thorpej 		}
   1212  1.70   thorpej 		if (writev(f->f_file, iov, 7) < 0) {
   1213  1.70   thorpej 			int e = errno;
   1214  1.70   thorpej 			if (f->f_un.f_pipe.f_pid > 0) {
   1215  1.70   thorpej 				(void) close(f->f_file);
   1216  1.70   thorpej 				deadq_enter(f->f_un.f_pipe.f_pid,
   1217  1.70   thorpej 					    f->f_un.f_pipe.f_pname);
   1218  1.70   thorpej 			}
   1219  1.70   thorpej 			f->f_un.f_pipe.f_pid = 0;
   1220  1.70   thorpej 			/*
   1221  1.70   thorpej 			 * If the error was EPIPE, then what is likely
   1222  1.70   thorpej 			 * has happened is we have a command that is
   1223  1.70   thorpej 			 * designed to take a single message line and
   1224  1.70   thorpej 			 * then exit, but we tried to feed it another
   1225  1.70   thorpej 			 * one before we reaped the child and thus
   1226  1.70   thorpej 			 * reset our state.
   1227  1.70   thorpej 			 *
   1228  1.70   thorpej 			 * Well, now we've reset our state, so try opening
   1229  1.70   thorpej 			 * the pipe and sending the message again if EPIPE
   1230  1.70   thorpej 			 * was the error.
   1231  1.70   thorpej 			 */
   1232  1.70   thorpej 			if (e == EPIPE) {
   1233  1.70   thorpej 				if ((f->f_file = p_open(f->f_un.f_pipe.f_pname,
   1234  1.70   thorpej 				     &f->f_un.f_pipe.f_pid)) < 0) {
   1235  1.70   thorpej 					f->f_type = F_UNUSED;
   1236  1.70   thorpej 					logerror(f->f_un.f_pipe.f_pname);
   1237  1.70   thorpej 					break;
   1238  1.70   thorpej 				}
   1239  1.70   thorpej 				if (writev(f->f_file, iov, 7) < 0) {
   1240  1.70   thorpej 					e = errno;
   1241  1.70   thorpej 					if (f->f_un.f_pipe.f_pid > 0) {
   1242  1.70   thorpej 					    (void) close(f->f_file);
   1243  1.70   thorpej 					    deadq_enter(f->f_un.f_pipe.f_pid,
   1244  1.70   thorpej 							f->f_un.f_pipe.f_pname);
   1245  1.70   thorpej 					}
   1246  1.70   thorpej 					f->f_un.f_pipe.f_pid = 0;
   1247  1.70   thorpej 				} else
   1248  1.70   thorpej 					e = 0;
   1249  1.70   thorpej 			}
   1250  1.70   thorpej 			if (e != 0) {
   1251  1.70   thorpej 				errno = e;
   1252  1.70   thorpej 				logerror(f->f_un.f_pipe.f_pname);
   1253  1.70   thorpej 			}
   1254  1.70   thorpej 		}
   1255  1.70   thorpej 		break;
   1256  1.70   thorpej 
   1257   1.1       cgd 	case F_CONSOLE:
   1258   1.1       cgd 		if (flags & IGN_CONS) {
   1259   1.1       cgd 			dprintf(" (ignored)\n");
   1260   1.1       cgd 			break;
   1261   1.1       cgd 		}
   1262   1.1       cgd 		/* FALLTHROUGH */
   1263   1.1       cgd 
   1264   1.1       cgd 	case F_TTY:
   1265   1.1       cgd 	case F_FILE:
   1266   1.1       cgd 		dprintf(" %s\n", f->f_un.f_fname);
   1267   1.1       cgd 		if (f->f_type != F_FILE) {
   1268   1.1       cgd 			v->iov_base = "\r\n";
   1269   1.1       cgd 			v->iov_len = 2;
   1270   1.1       cgd 		} else {
   1271   1.1       cgd 			v->iov_base = "\n";
   1272   1.1       cgd 			v->iov_len = 1;
   1273   1.1       cgd 		}
   1274   1.1       cgd 	again:
   1275  1.70   thorpej 		if (writev(f->f_file, iov, 7) < 0) {
   1276   1.1       cgd 			int e = errno;
   1277  1.74      matt 			if (f->f_type == F_FILE && e == ENOSPC) {
   1278  1.74      matt 				int lasterror = f->f_lasterror;
   1279  1.74      matt 				f->f_lasterror = e;
   1280  1.74      matt 				if (lasterror != e)
   1281  1.74      matt 					logerror(f->f_un.f_fname);
   1282  1.74      matt 				break;
   1283  1.74      matt 			}
   1284   1.5     perry 			(void)close(f->f_file);
   1285   1.1       cgd 			/*
   1286   1.1       cgd 			 * Check for errors on TTY's due to loss of tty
   1287   1.1       cgd 			 */
   1288   1.1       cgd 			if ((e == EIO || e == EBADF) && f->f_type != F_FILE) {
   1289   1.1       cgd 				f->f_file = open(f->f_un.f_fname,
   1290   1.1       cgd 				    O_WRONLY|O_APPEND, 0);
   1291   1.1       cgd 				if (f->f_file < 0) {
   1292   1.1       cgd 					f->f_type = F_UNUSED;
   1293   1.1       cgd 					logerror(f->f_un.f_fname);
   1294   1.1       cgd 				} else
   1295   1.1       cgd 					goto again;
   1296   1.1       cgd 			} else {
   1297   1.1       cgd 				f->f_type = F_UNUSED;
   1298   1.1       cgd 				errno = e;
   1299  1.74      matt 				f->f_lasterror = e;
   1300   1.1       cgd 				logerror(f->f_un.f_fname);
   1301   1.1       cgd 			}
   1302  1.74      matt 		} else {
   1303  1.74      matt 			f->f_lasterror = 0;
   1304  1.74      matt 			if ((flags & SYNC_FILE) && (f->f_flags & FFLAG_SYNC))
   1305  1.74      matt 				(void)fsync(f->f_file);
   1306  1.74      matt 		}
   1307   1.1       cgd 		break;
   1308   1.1       cgd 
   1309   1.1       cgd 	case F_USERS:
   1310   1.1       cgd 	case F_WALL:
   1311   1.1       cgd 		dprintf("\n");
   1312   1.1       cgd 		v->iov_base = "\r\n";
   1313   1.1       cgd 		v->iov_len = 2;
   1314   1.1       cgd 		wallmsg(f, iov);
   1315   1.1       cgd 		break;
   1316   1.1       cgd 	}
   1317   1.1       cgd 	f->f_prevcount = 0;
   1318   1.1       cgd }
   1319   1.1       cgd 
   1320   1.1       cgd /*
   1321   1.1       cgd  *  WALLMSG -- Write a message to the world at large
   1322   1.1       cgd  *
   1323   1.1       cgd  *	Write the specified message to either the entire
   1324   1.1       cgd  *	world, or a list of approved users.
   1325   1.1       cgd  */
   1326   1.5     perry void
   1327  1.53       wiz wallmsg(struct filed *f, struct iovec *iov)
   1328   1.1       cgd {
   1329   1.1       cgd 	static int reenter;			/* avoid calling ourselves */
   1330   1.5     perry 	int i;
   1331   1.5     perry 	char *p;
   1332  1.55  christos 	static struct utmpentry *ohead = NULL;
   1333  1.55  christos 	struct utmpentry *ep;
   1334   1.1       cgd 
   1335   1.1       cgd 	if (reenter++)
   1336   1.1       cgd 		return;
   1337  1.55  christos 
   1338  1.55  christos 	(void)getutentries(NULL, &ep);
   1339  1.55  christos 	if (ep != ohead) {
   1340  1.55  christos 		freeutentries(ohead);
   1341  1.55  christos 		ohead = ep;
   1342   1.1       cgd 	}
   1343   1.1       cgd 	/* NOSTRICT */
   1344  1.55  christos 	for (; ep; ep = ep->next) {
   1345   1.1       cgd 		if (f->f_type == F_WALL) {
   1346  1.70   thorpej 			if ((p = ttymsg(iov, 7, ep->line, TTYMSGTIME))
   1347  1.55  christos 			    != NULL) {
   1348   1.1       cgd 				errno = 0;	/* already in msg */
   1349   1.1       cgd 				logerror(p);
   1350   1.1       cgd 			}
   1351   1.1       cgd 			continue;
   1352   1.1       cgd 		}
   1353   1.1       cgd 		/* should we send the message to this user? */
   1354   1.1       cgd 		for (i = 0; i < MAXUNAMES; i++) {
   1355   1.1       cgd 			if (!f->f_un.f_uname[i][0])
   1356   1.1       cgd 				break;
   1357  1.55  christos 			if (strcmp(f->f_un.f_uname[i], ep->name) == 0) {
   1358  1.70   thorpej 				if ((p = ttymsg(iov, 7, ep->line, TTYMSGTIME))
   1359  1.55  christos 				    != NULL) {
   1360   1.1       cgd 					errno = 0;	/* already in msg */
   1361   1.1       cgd 					logerror(p);
   1362   1.1       cgd 				}
   1363   1.1       cgd 				break;
   1364   1.1       cgd 			}
   1365   1.1       cgd 		}
   1366   1.1       cgd 	}
   1367   1.1       cgd 	reenter = 0;
   1368   1.1       cgd }
   1369   1.1       cgd 
   1370   1.1       cgd void
   1371  1.70   thorpej reapchild(struct kevent *ev)
   1372  1.64    itojun {
   1373  1.70   thorpej 	int status;
   1374  1.70   thorpej 	pid_t pid;
   1375  1.70   thorpej 	struct filed *f;
   1376  1.64    itojun 
   1377  1.70   thorpej 	while ((pid = wait3(&status, WNOHANG, NULL)) > 0) {
   1378  1.70   thorpej 		if (!Initialized || ShuttingDown) {
   1379  1.70   thorpej 			/*
   1380  1.70   thorpej 			 * Be silent while we are initializing or
   1381  1.70   thorpej 			 * shutting down.
   1382  1.70   thorpej 			 */
   1383  1.70   thorpej 			continue;
   1384  1.70   thorpej 		}
   1385  1.64    itojun 
   1386  1.70   thorpej 		if (deadq_remove(pid))
   1387  1.70   thorpej 			continue;
   1388   1.1       cgd 
   1389  1.70   thorpej 		/* Now, look in the list of active processes. */
   1390  1.70   thorpej 		for (f = Files; f != NULL; f = f->f_next) {
   1391  1.70   thorpej 			if (f->f_type == F_PIPE &&
   1392  1.70   thorpej 			    f->f_un.f_pipe.f_pid == pid) {
   1393  1.70   thorpej 				(void) close(f->f_file);
   1394  1.70   thorpej 				f->f_un.f_pipe.f_pid = 0;
   1395  1.70   thorpej 				log_deadchild(pid, status,
   1396  1.70   thorpej 					      f->f_un.f_pipe.f_pname);
   1397  1.70   thorpej 				break;
   1398  1.70   thorpej 			}
   1399  1.70   thorpej 		}
   1400  1.70   thorpej 	}
   1401   1.1       cgd }
   1402   1.1       cgd 
   1403   1.1       cgd /*
   1404   1.1       cgd  * Return a printable representation of a host address.
   1405   1.1       cgd  */
   1406   1.1       cgd char *
   1407  1.53       wiz cvthname(struct sockaddr_storage *f)
   1408   1.1       cgd {
   1409  1.30    itojun 	int error;
   1410  1.30    itojun 	const int niflag = NI_DGRAM;
   1411  1.30    itojun 	static char host[NI_MAXHOST], ip[NI_MAXHOST];
   1412   1.1       cgd 
   1413  1.30    itojun 	error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len,
   1414  1.33    itojun 			ip, sizeof ip, NULL, 0, NI_NUMERICHOST|niflag);
   1415   1.1       cgd 
   1416  1.30    itojun 	dprintf("cvthname(%s)\n", ip);
   1417  1.30    itojun 
   1418  1.30    itojun 	if (error) {
   1419  1.30    itojun 		dprintf("Malformed from address %s\n", gai_strerror(error));
   1420   1.1       cgd 		return ("???");
   1421   1.1       cgd 	}
   1422  1.45       mrg 
   1423  1.45       mrg 	if (!UseNameService)
   1424  1.45       mrg 		return (ip);
   1425  1.30    itojun 
   1426  1.30    itojun 	error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len,
   1427  1.33    itojun 			host, sizeof host, NULL, 0, niflag);
   1428  1.30    itojun 	if (error) {
   1429  1.30    itojun 		dprintf("Host name for your address (%s) unknown\n", ip);
   1430  1.30    itojun 		return (ip);
   1431   1.1       cgd 	}
   1432  1.70   thorpej 
   1433  1.70   thorpej 	trim_localdomain(host);
   1434  1.70   thorpej 
   1435  1.30    itojun 	return (host);
   1436   1.1       cgd }
   1437   1.1       cgd 
   1438   1.1       cgd void
   1439  1.70   thorpej trim_localdomain(char *host)
   1440  1.70   thorpej {
   1441  1.70   thorpej 	size_t hl;
   1442  1.70   thorpej 
   1443  1.70   thorpej 	hl = strlen(host);
   1444  1.70   thorpej 	if (hl > 0 && host[hl - 1] == '.')
   1445  1.70   thorpej 		host[--hl] = '\0';
   1446  1.70   thorpej 
   1447  1.70   thorpej 	if (hl > LocalDomainLen && host[hl - LocalDomainLen - 1] == '.' &&
   1448  1.70   thorpej 	    strcasecmp(&host[hl - LocalDomainLen], LocalDomain) == 0)
   1449  1.70   thorpej 		host[hl - LocalDomainLen - 1] = '\0';
   1450  1.70   thorpej }
   1451  1.70   thorpej 
   1452  1.70   thorpej void
   1453  1.70   thorpej domark(struct kevent *ev)
   1454   1.1       cgd {
   1455   1.5     perry 	struct filed *f;
   1456  1.70   thorpej 	dq_t q, nextq;
   1457  1.70   thorpej 
   1458  1.70   thorpej 	/*
   1459  1.70   thorpej 	 * XXX Should we bother to adjust for the # of times the timer
   1460  1.70   thorpej 	 * has expired (i.e. in case we miss one?).  This information is
   1461  1.70   thorpej 	 * returned to us in ev->data.
   1462  1.70   thorpej 	 */
   1463   1.1       cgd 
   1464   1.1       cgd 	now = time((time_t *)NULL);
   1465   1.1       cgd 	MarkSeq += TIMERINTVL;
   1466   1.1       cgd 	if (MarkSeq >= MarkInterval) {
   1467   1.1       cgd 		logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
   1468   1.1       cgd 		MarkSeq = 0;
   1469   1.1       cgd 	}
   1470   1.1       cgd 
   1471   1.1       cgd 	for (f = Files; f; f = f->f_next) {
   1472   1.1       cgd 		if (f->f_prevcount && now >= REPEATTIME(f)) {
   1473  1.47      manu 			dprintf("Flush %s: repeated %d times, %d sec.\n",
   1474   1.1       cgd 			    TypeNames[f->f_type], f->f_prevcount,
   1475   1.1       cgd 			    repeatinterval[f->f_repeatcount]);
   1476   1.1       cgd 			fprintlog(f, 0, (char *)NULL);
   1477   1.1       cgd 			BACKOFF(f);
   1478   1.1       cgd 		}
   1479   1.1       cgd 	}
   1480  1.70   thorpej 
   1481  1.70   thorpej 	/* Walk the dead queue, and see if we should signal somebody. */
   1482  1.70   thorpej 	for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = nextq) {
   1483  1.70   thorpej 		nextq = TAILQ_NEXT(q, dq_entries);
   1484  1.70   thorpej 		switch (q->dq_timeout) {
   1485  1.70   thorpej 		case 0:
   1486  1.70   thorpej 			/* Already signalled once, try harder now. */
   1487  1.70   thorpej 			if (kill(q->dq_pid, SIGKILL) != 0)
   1488  1.70   thorpej 				(void) deadq_remove(q->dq_pid);
   1489  1.70   thorpej 			break;
   1490  1.70   thorpej 
   1491  1.70   thorpej 		case 1:
   1492  1.70   thorpej 			/*
   1493  1.70   thorpej 			 * Timed out on the dead queue, send terminate
   1494  1.70   thorpej 			 * signal.  Note that we leave the removal from
   1495  1.70   thorpej 			 * the dead queue to reapchild(), which will
   1496  1.70   thorpej 			 * also log the event (unless the process
   1497  1.70   thorpej 			 * didn't even really exist, in case we simply
   1498  1.70   thorpej 			 * drop it from the dead queue).
   1499  1.70   thorpej 			 */
   1500  1.70   thorpej 			if (kill(q->dq_pid, SIGTERM) != 0) {
   1501  1.70   thorpej 				(void) deadq_remove(q->dq_pid);
   1502  1.70   thorpej 				break;
   1503  1.70   thorpej 			}
   1504  1.70   thorpej 			/* FALLTHROUGH */
   1505  1.70   thorpej 
   1506  1.70   thorpej 		default:
   1507  1.70   thorpej 			q->dq_timeout--;
   1508  1.70   thorpej 		}
   1509  1.70   thorpej 	}
   1510   1.1       cgd }
   1511   1.1       cgd 
   1512   1.1       cgd /*
   1513   1.1       cgd  * Print syslogd errors some place.
   1514   1.1       cgd  */
   1515   1.5     perry void
   1516  1.47      manu logerror(const char *fmt, ...)
   1517   1.1       cgd {
   1518  1.70   thorpej 	static int logerror_running;
   1519  1.47      manu 	va_list ap;
   1520  1.47      manu 	char tmpbuf[BUFSIZ];
   1521  1.47      manu 	char buf[BUFSIZ];
   1522  1.47      manu 
   1523  1.70   thorpej 	/* If there's an error while trying to log an error, give up. */
   1524  1.70   thorpej 	if (logerror_running)
   1525  1.70   thorpej 		return;
   1526  1.70   thorpej 	logerror_running = 1;
   1527  1.70   thorpej 
   1528  1.47      manu 	va_start(ap, fmt);
   1529  1.47      manu 
   1530  1.47      manu 	(void)vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
   1531  1.47      manu 
   1532  1.47      manu 	va_end(ap);
   1533   1.1       cgd 
   1534   1.1       cgd 	if (errno)
   1535  1.47      manu 		(void)snprintf(buf, sizeof(buf), "syslogd: %s: %s",
   1536  1.47      manu 		    tmpbuf, strerror(errno));
   1537   1.1       cgd 	else
   1538  1.47      manu 		(void)snprintf(buf, sizeof(buf), "syslogd: %s", tmpbuf);
   1539  1.47      manu 
   1540  1.47      manu 	if (daemonized)
   1541  1.47      manu 		logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
   1542  1.47      manu 	if (!daemonized && Debug)
   1543  1.47      manu 		dprintf("%s\n", buf);
   1544  1.47      manu 	if (!daemonized && !Debug)
   1545  1.47      manu 		printf("%s\n", buf);
   1546  1.47      manu 
   1547  1.70   thorpej 	logerror_running = 0;
   1548   1.1       cgd }
   1549   1.1       cgd 
   1550   1.1       cgd void
   1551  1.70   thorpej die(struct kevent *ev)
   1552   1.1       cgd {
   1553   1.5     perry 	struct filed *f;
   1554  1.47      manu 	char **p;
   1555   1.1       cgd 
   1556  1.70   thorpej 	ShuttingDown = 1;	/* Don't log SIGCHLDs. */
   1557   1.1       cgd 	for (f = Files; f != NULL; f = f->f_next) {
   1558   1.1       cgd 		/* flush any pending output */
   1559   1.1       cgd 		if (f->f_prevcount)
   1560   1.1       cgd 			fprintlog(f, 0, (char *)NULL);
   1561  1.70   thorpej 		if (f->f_type == F_PIPE && f->f_un.f_pipe.f_pid > 0) {
   1562  1.70   thorpej 			(void) close(f->f_file);
   1563  1.70   thorpej 			f->f_un.f_pipe.f_pid = 0;
   1564  1.70   thorpej 		}
   1565   1.1       cgd 	}
   1566  1.47      manu 	errno = 0;
   1567  1.70   thorpej 	if (ev != NULL)
   1568  1.70   thorpej 		logerror("Exiting on signal %d", (int) ev->ident);
   1569  1.47      manu 	else
   1570  1.47      manu 		logerror("Fatal error, exiting");
   1571  1.22       mrg 	for (p = LogPaths; p && *p; p++)
   1572  1.22       mrg 		unlink(*p);
   1573   1.1       cgd 	exit(0);
   1574   1.1       cgd }
   1575   1.1       cgd 
   1576   1.1       cgd /*
   1577   1.1       cgd  *  INIT -- Initialize syslogd from configuration table
   1578   1.1       cgd  */
   1579   1.1       cgd void
   1580  1.70   thorpej init(struct kevent *ev)
   1581   1.1       cgd {
   1582   1.5     perry 	int i;
   1583   1.5     perry 	FILE *cf;
   1584   1.5     perry 	struct filed *f, *next, **nextp;
   1585   1.5     perry 	char *p;
   1586   1.5     perry 	char cline[LINE_MAX];
   1587  1.70   thorpej 	char prog[NAME_MAX + 1];
   1588  1.70   thorpej 	char host[MAXHOSTNAMELEN];
   1589  1.70   thorpej 	char hostMsg[2*MAXHOSTNAMELEN + 40];
   1590   1.1       cgd 
   1591   1.1       cgd 	dprintf("init\n");
   1592   1.1       cgd 
   1593  1.70   thorpej 	(void)strlcpy(oldLocalHostName, LocalHostName,
   1594  1.70   thorpej 		      sizeof(oldLocalHostName));
   1595  1.70   thorpej 	(void)gethostname(LocalHostName, sizeof(LocalHostName));
   1596  1.70   thorpej 	if ((p = strchr(LocalHostName, '.')) != NULL) {
   1597  1.70   thorpej 		*p++ = '\0';
   1598  1.70   thorpej 		LocalDomain = p;
   1599  1.70   thorpej 	} else
   1600  1.70   thorpej 		LocalDomain = "";
   1601  1.70   thorpej 	LocalDomainLen = strlen(LocalDomain);
   1602  1.70   thorpej 
   1603   1.1       cgd 	/*
   1604   1.1       cgd 	 *  Close all open log files.
   1605   1.1       cgd 	 */
   1606   1.1       cgd 	Initialized = 0;
   1607   1.1       cgd 	for (f = Files; f != NULL; f = next) {
   1608   1.1       cgd 		/* flush any pending output */
   1609   1.1       cgd 		if (f->f_prevcount)
   1610   1.1       cgd 			fprintlog(f, 0, (char *)NULL);
   1611   1.1       cgd 
   1612   1.1       cgd 		switch (f->f_type) {
   1613   1.5     perry 		case F_FILE:
   1614   1.5     perry 		case F_TTY:
   1615   1.5     perry 		case F_CONSOLE:
   1616   1.5     perry 			(void)close(f->f_file);
   1617  1.44    itojun 			break;
   1618  1.70   thorpej 		case F_PIPE:
   1619  1.70   thorpej 			if (f->f_un.f_pipe.f_pid > 0) {
   1620  1.70   thorpej 				(void)close(f->f_file);
   1621  1.70   thorpej 				deadq_enter(f->f_un.f_pipe.f_pid,
   1622  1.70   thorpej 					    f->f_un.f_pipe.f_pname);
   1623  1.70   thorpej 			}
   1624  1.70   thorpej 			f->f_un.f_pipe.f_pid = 0;
   1625  1.70   thorpej 			break;
   1626  1.44    itojun 		case F_FORW:
   1627  1.44    itojun 			if (f->f_un.f_forw.f_addr)
   1628  1.44    itojun 				freeaddrinfo(f->f_un.f_forw.f_addr);
   1629   1.1       cgd 			break;
   1630   1.1       cgd 		}
   1631   1.1       cgd 		next = f->f_next;
   1632  1.70   thorpej 		if (f->f_program != NULL)
   1633  1.70   thorpej 			free(f->f_program);
   1634  1.70   thorpej 		if (f->f_host != NULL)
   1635  1.70   thorpej 			free(f->f_host);
   1636   1.5     perry 		free((char *)f);
   1637   1.1       cgd 	}
   1638   1.1       cgd 	Files = NULL;
   1639   1.1       cgd 	nextp = &Files;
   1640   1.1       cgd 
   1641  1.38     jwise 	/*
   1642  1.38     jwise 	 *  Close all open sockets
   1643  1.38     jwise 	 */
   1644  1.38     jwise 
   1645  1.38     jwise 	if (finet) {
   1646  1.38     jwise 		for (i = 0; i < *finet; i++) {
   1647  1.38     jwise 			if (close(finet[i+1]) < 0) {
   1648  1.47      manu 				logerror("close() failed");
   1649  1.70   thorpej 				die(NULL);
   1650  1.38     jwise 			}
   1651  1.38     jwise 		}
   1652  1.38     jwise 	}
   1653  1.39     jwise 
   1654  1.39     jwise 	/*
   1655  1.39     jwise 	 *  Reset counter of forwarding actions
   1656  1.39     jwise 	 */
   1657  1.39     jwise 
   1658  1.39     jwise 	NumForwards=0;
   1659  1.38     jwise 
   1660   1.1       cgd 	/* open the configuration file */
   1661   1.1       cgd 	if ((cf = fopen(ConfFile, "r")) == NULL) {
   1662  1.47      manu 		dprintf("Cannot open `%s'\n", ConfFile);
   1663   1.1       cgd 		*nextp = (struct filed *)calloc(1, sizeof(*f));
   1664  1.70   thorpej 		cfline("*.ERR\t/dev/console", *nextp, "*", "*");
   1665   1.1       cgd 		(*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
   1666  1.70   thorpej 		cfline("*.PANIC\t*", (*nextp)->f_next, "*", "*");
   1667   1.1       cgd 		Initialized = 1;
   1668   1.1       cgd 		return;
   1669   1.1       cgd 	}
   1670   1.1       cgd 
   1671   1.1       cgd 	/*
   1672   1.1       cgd 	 *  Foreach line in the conf table, open that file.
   1673   1.1       cgd 	 */
   1674   1.1       cgd 	f = NULL;
   1675  1.70   thorpej 	strcpy(prog, "*");
   1676  1.70   thorpej 	strcpy(host, "*");
   1677   1.5     perry 	while (fgets(cline, sizeof(cline), cf) != NULL) {
   1678   1.1       cgd 		/*
   1679   1.1       cgd 		 * check for end-of-section, comments, strip off trailing
   1680  1.70   thorpej 		 * spaces and newline character.  #!prog is treated specially:
   1681  1.70   thorpej 		 * following lines apply only to that program.
   1682   1.1       cgd 		 */
   1683  1.69       dsl 		for (p = cline; isspace((unsigned char)*p); ++p)
   1684   1.5     perry 			continue;
   1685  1.70   thorpej 		if (*p == '\0')
   1686  1.70   thorpej 			continue;
   1687  1.70   thorpej 		if (*p == '#') {
   1688  1.70   thorpej 			p++;
   1689  1.70   thorpej 			if (*p != '!' && *p != '+' && *p != '-')
   1690  1.70   thorpej 				continue;
   1691  1.70   thorpej 		}
   1692  1.70   thorpej 		if (*p == '+' || *p == '-') {
   1693  1.70   thorpej 			host[0] = *p++;
   1694  1.70   thorpej 			while (isspace((unsigned char)*p))
   1695  1.70   thorpej 				p++;
   1696  1.70   thorpej 			if (*p == '\0' || *p == '*') {
   1697  1.70   thorpej 				strcpy(host, "*");
   1698  1.70   thorpej 				continue;
   1699  1.70   thorpej 			}
   1700  1.70   thorpej 			if (*p == '@')
   1701  1.70   thorpej 				p = LocalHostName;
   1702  1.70   thorpej 			for (i = 1; i < MAXHOSTNAMELEN - 1; i++) {
   1703  1.70   thorpej 				if (!isalnum((unsigned char)*p) &&
   1704  1.70   thorpej 				    *p != '.' && *p != '-' && *p != ',')
   1705  1.70   thorpej 					break;
   1706  1.70   thorpej 				host[i] = *p++;
   1707  1.70   thorpej 			}
   1708  1.70   thorpej 			host[i] = '\0';
   1709  1.70   thorpej 			continue;
   1710  1.70   thorpej 		}
   1711  1.70   thorpej 		if (*p == '!') {
   1712  1.70   thorpej 			p++;
   1713  1.70   thorpej 			while (isspace((unsigned char)*p))
   1714  1.70   thorpej 				p++;
   1715  1.70   thorpej 			if (*p == '\0' || *p == '*') {
   1716  1.70   thorpej 				strcpy(prog, "*");
   1717  1.70   thorpej 				continue;
   1718  1.70   thorpej 			}
   1719  1.70   thorpej 			for (i = 0; i < NAME_MAX; i++) {
   1720  1.70   thorpej 				if (!isprint((unsigned char)p[i]))
   1721  1.70   thorpej 					break;
   1722  1.70   thorpej 				prog[i] = p[i];
   1723  1.70   thorpej 			}
   1724  1.70   thorpej 			prog[i] = '\0';
   1725   1.1       cgd 			continue;
   1726  1.70   thorpej 		}
   1727  1.69       dsl 		for (p = strchr(cline, '\0'); isspace((unsigned char)*--p);)
   1728   1.5     perry 			continue;
   1729   1.1       cgd 		*++p = '\0';
   1730   1.1       cgd 		f = (struct filed *)calloc(1, sizeof(*f));
   1731   1.1       cgd 		*nextp = f;
   1732   1.1       cgd 		nextp = &f->f_next;
   1733  1.70   thorpej 		cfline(cline, f, prog, host);
   1734   1.1       cgd 	}
   1735   1.1       cgd 
   1736   1.1       cgd 	/* close the configuration file */
   1737   1.5     perry 	(void)fclose(cf);
   1738   1.1       cgd 
   1739   1.1       cgd 	Initialized = 1;
   1740   1.1       cgd 
   1741   1.1       cgd 	if (Debug) {
   1742   1.1       cgd 		for (f = Files; f; f = f->f_next) {
   1743   1.1       cgd 			for (i = 0; i <= LOG_NFACILITIES; i++)
   1744   1.1       cgd 				if (f->f_pmask[i] == INTERNAL_NOPRI)
   1745   1.1       cgd 					printf("X ");
   1746   1.1       cgd 				else
   1747   1.1       cgd 					printf("%d ", f->f_pmask[i]);
   1748   1.1       cgd 			printf("%s: ", TypeNames[f->f_type]);
   1749   1.1       cgd 			switch (f->f_type) {
   1750   1.1       cgd 			case F_FILE:
   1751   1.1       cgd 			case F_TTY:
   1752   1.1       cgd 			case F_CONSOLE:
   1753   1.1       cgd 				printf("%s", f->f_un.f_fname);
   1754   1.1       cgd 				break;
   1755   1.1       cgd 
   1756   1.1       cgd 			case F_FORW:
   1757   1.1       cgd 				printf("%s", f->f_un.f_forw.f_hname);
   1758   1.1       cgd 				break;
   1759   1.1       cgd 
   1760  1.70   thorpej 			case F_PIPE:
   1761  1.70   thorpej 				printf("%s", f->f_un.f_pipe.f_pname);
   1762  1.70   thorpej 				break;
   1763  1.70   thorpej 
   1764   1.1       cgd 			case F_USERS:
   1765  1.34     lukem 				for (i = 0;
   1766  1.34     lukem 				    i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
   1767   1.1       cgd 					printf("%s, ", f->f_un.f_uname[i]);
   1768   1.1       cgd 				break;
   1769   1.1       cgd 			}
   1770  1.70   thorpej 			if (f->f_program != NULL)
   1771  1.70   thorpej 				printf(" (%s)", f->f_program);
   1772   1.1       cgd 			printf("\n");
   1773   1.1       cgd 		}
   1774  1.38     jwise 	}
   1775  1.38     jwise 
   1776  1.38     jwise 	finet = socksetup(PF_UNSPEC);
   1777  1.38     jwise 	if (finet) {
   1778  1.38     jwise 		if (SecureMode) {
   1779  1.38     jwise 			for (i = 0; i < *finet; i++) {
   1780  1.38     jwise 				if (shutdown(finet[i+1], SHUT_RD) < 0) {
   1781  1.47      manu 					logerror("shutdown() failed");
   1782  1.70   thorpej 					die(NULL);
   1783  1.38     jwise 				}
   1784  1.38     jwise 			}
   1785  1.38     jwise 		} else
   1786  1.47      manu 			dprintf("Listening on inet and/or inet6 socket\n");
   1787  1.47      manu 		dprintf("Sending on inet and/or inet6 socket\n");
   1788   1.1       cgd 	}
   1789   1.1       cgd 
   1790   1.1       cgd 	logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
   1791   1.1       cgd 	dprintf("syslogd: restarted\n");
   1792  1.70   thorpej 	/*
   1793  1.70   thorpej 	 * Log a change in hostname, but only on a restart (we detect this
   1794  1.70   thorpej 	 * by checking to see if we're passed a kevent).
   1795  1.70   thorpej 	 */
   1796  1.70   thorpej 	if (ev != NULL && strcmp(oldLocalHostName, LocalHostName) != 0) {
   1797  1.70   thorpej 		(void)snprintf(hostMsg, sizeof(hostMsg),
   1798  1.70   thorpej 		    "syslogd: host name changed, \"%s\" to \"%s\"",
   1799  1.70   thorpej 		    oldLocalHostName, LocalHostName);
   1800  1.70   thorpej 		logmsg(LOG_SYSLOG|LOG_INFO, hostMsg, LocalHostName, ADDDATE);
   1801  1.70   thorpej 		dprintf("%s\n", hostMsg);
   1802  1.70   thorpej 	}
   1803   1.1       cgd }
   1804   1.1       cgd 
   1805   1.1       cgd /*
   1806   1.1       cgd  * Crack a configuration file line
   1807   1.1       cgd  */
   1808   1.5     perry void
   1809  1.70   thorpej cfline(char *line, struct filed *f, char *prog, char *host)
   1810   1.1       cgd {
   1811  1.30    itojun 	struct addrinfo hints, *res;
   1812  1.70   thorpej 	int    error, i, pri, syncfile;
   1813  1.30    itojun 	char   *bp, *p, *q;
   1814  1.47      manu 	char   buf[MAXLINE];
   1815   1.1       cgd 
   1816  1.70   thorpej 	dprintf("cfline(\"%s\", f, \"%s\", \"%s\")\n", line, prog, host);
   1817   1.1       cgd 
   1818   1.1       cgd 	errno = 0;	/* keep strerror() stuff out of logerror messages */
   1819   1.1       cgd 
   1820   1.1       cgd 	/* clear out file entry */
   1821   1.5     perry 	memset(f, 0, sizeof(*f));
   1822   1.1       cgd 	for (i = 0; i <= LOG_NFACILITIES; i++)
   1823   1.1       cgd 		f->f_pmask[i] = INTERNAL_NOPRI;
   1824  1.47      manu 
   1825  1.47      manu 	/*
   1826  1.47      manu 	 * There should not be any space before the log facility.
   1827  1.47      manu 	 * Check this is okay, complain and fix if it is not.
   1828  1.47      manu 	 */
   1829  1.47      manu 	q = line;
   1830  1.47      manu 	if (isblank((unsigned char)*line)) {
   1831  1.47      manu 		errno = 0;
   1832  1.47      manu 		logerror(
   1833  1.47      manu 		    "Warning: `%s' space or tab before the log facility",
   1834  1.47      manu 		    line);
   1835  1.47      manu 		/* Fix: strip all spaces/tabs before the log facility */
   1836  1.70   thorpej 		while (*q++ && isblank((unsigned char)*q))
   1837  1.70   thorpej 			/* skip blanks */;
   1838  1.47      manu 		line = q;
   1839  1.47      manu 	}
   1840  1.47      manu 
   1841  1.47      manu 	/*
   1842  1.47      manu 	 * q is now at the first char of the log facility
   1843  1.47      manu 	 * There should be at least one tab after the log facility
   1844  1.47      manu 	 * Check this is okay, and complain and fix if it is not.
   1845  1.47      manu 	 */
   1846  1.47      manu 	q = line + strlen(line);
   1847  1.47      manu 	while (!isblank((unsigned char)*q) && (q != line))
   1848  1.47      manu 		q--;
   1849  1.47      manu 	if ((q == line) && strlen(line)) {
   1850  1.47      manu 		/* No tabs or space in a non empty line: complain */
   1851  1.47      manu 		errno = 0;
   1852  1.47      manu 		logerror(
   1853  1.47      manu 		    "Error: `%s' log facility or log target missing",
   1854  1.47      manu 		    line);
   1855  1.70   thorpej 		return;
   1856  1.47      manu 	}
   1857  1.47      manu 
   1858  1.70   thorpej 	/* save host name, if any */
   1859  1.70   thorpej 	if (*host == '*')
   1860  1.70   thorpej 		f->f_host = NULL;
   1861  1.70   thorpej 	else {
   1862  1.70   thorpej 		f->f_host = strdup(host);
   1863  1.70   thorpej 		trim_localdomain(f->f_host);
   1864  1.70   thorpej 	}
   1865  1.70   thorpej 
   1866  1.70   thorpej 	/* save program name, if any */
   1867  1.70   thorpej 	if (*prog == '*')
   1868  1.70   thorpej 		f->f_program = NULL;
   1869  1.70   thorpej 	else
   1870  1.70   thorpej 		f->f_program = strdup(prog);
   1871   1.1       cgd 
   1872   1.1       cgd 	/* scan through the list of selectors */
   1873  1.70   thorpej 	for (p = line; *p && !isblank((unsigned char)*p);) {
   1874  1.70   thorpej 		int pri_done, pri_cmp, pri_invert;
   1875   1.1       cgd 
   1876   1.1       cgd 		/* find the end of this facility name list */
   1877  1.70   thorpej 		for (q = p; *q && !isblank((unsigned char)*q) && *q++ != '.'; )
   1878   1.1       cgd 			continue;
   1879   1.1       cgd 
   1880  1.70   thorpej 		/* get the priority comparison */
   1881  1.70   thorpej 		pri_cmp = 0;
   1882  1.70   thorpej 		pri_done = 0;
   1883  1.70   thorpej 		pri_invert = 0;
   1884  1.70   thorpej 		if (*q == '!') {
   1885  1.70   thorpej 			pri_invert = 1;
   1886  1.70   thorpej 			q++;
   1887  1.70   thorpej 		}
   1888  1.70   thorpej 		while (! pri_done) {
   1889  1.70   thorpej 			switch (*q) {
   1890  1.70   thorpej 			case '<':
   1891  1.70   thorpej 				pri_cmp = PRI_LT;
   1892  1.70   thorpej 				q++;
   1893  1.70   thorpej 				break;
   1894  1.70   thorpej 			case '=':
   1895  1.70   thorpej 				pri_cmp = PRI_EQ;
   1896  1.70   thorpej 				q++;
   1897  1.70   thorpej 				break;
   1898  1.70   thorpej 			case '>':
   1899  1.70   thorpej 				pri_cmp = PRI_GT;
   1900  1.70   thorpej 				q++;
   1901  1.70   thorpej 				break;
   1902  1.70   thorpej 			default:
   1903  1.70   thorpej 				pri_done = 1;
   1904  1.70   thorpej 				break;
   1905  1.70   thorpej 			}
   1906  1.70   thorpej 		}
   1907  1.70   thorpej 
   1908   1.1       cgd 		/* collect priority name */
   1909  1.70   thorpej 		for (bp = buf; *q && !strchr("\t ,;", *q); )
   1910   1.1       cgd 			*bp++ = *q++;
   1911   1.1       cgd 		*bp = '\0';
   1912   1.1       cgd 
   1913   1.1       cgd 		/* skip cruft */
   1914  1.70   thorpej 		while (strchr(",;", *q))
   1915   1.1       cgd 			q++;
   1916   1.1       cgd 
   1917   1.1       cgd 		/* decode priority name */
   1918  1.70   thorpej 		if (*buf == '*') {
   1919   1.1       cgd 			pri = LOG_PRIMASK + 1;
   1920  1.70   thorpej 			pri_cmp = PRI_LT | PRI_EQ | PRI_GT;
   1921  1.70   thorpej 		} else {
   1922   1.1       cgd 			pri = decode(buf, prioritynames);
   1923   1.1       cgd 			if (pri < 0) {
   1924  1.47      manu 				errno = 0;
   1925  1.47      manu 				logerror("Unknown priority name `%s'", buf);
   1926   1.1       cgd 				return;
   1927   1.1       cgd 			}
   1928   1.1       cgd 		}
   1929  1.70   thorpej 		if (pri_cmp == 0)
   1930  1.70   thorpej 			pri_cmp = UniquePriority ? PRI_EQ
   1931  1.70   thorpej 						 : PRI_EQ | PRI_GT;
   1932  1.70   thorpej 		if (pri_invert)
   1933  1.70   thorpej 			pri_cmp ^= PRI_LT | PRI_EQ | PRI_GT;
   1934   1.1       cgd 
   1935   1.1       cgd 		/* scan facilities */
   1936  1.70   thorpej 		while (*p && !strchr("\t .;", *p)) {
   1937  1.70   thorpej 			for (bp = buf; *p && !strchr("\t ,;.", *p); )
   1938   1.1       cgd 				*bp++ = *p++;
   1939   1.1       cgd 			*bp = '\0';
   1940   1.1       cgd 			if (*buf == '*')
   1941  1.70   thorpej 				for (i = 0; i < LOG_NFACILITIES; i++) {
   1942   1.1       cgd 					f->f_pmask[i] = pri;
   1943  1.70   thorpej 					f->f_pcmp[i] = pri_cmp;
   1944  1.70   thorpej 				}
   1945   1.1       cgd 			else {
   1946   1.1       cgd 				i = decode(buf, facilitynames);
   1947   1.1       cgd 				if (i < 0) {
   1948  1.47      manu 					errno = 0;
   1949  1.47      manu 					logerror("Unknown facility name `%s'",
   1950   1.1       cgd 					    buf);
   1951   1.1       cgd 					return;
   1952   1.1       cgd 				}
   1953   1.1       cgd 				f->f_pmask[i >> 3] = pri;
   1954  1.73   thorpej 				f->f_pcmp[i >> 3] = pri_cmp;
   1955   1.1       cgd 			}
   1956   1.1       cgd 			while (*p == ',' || *p == ' ')
   1957   1.1       cgd 				p++;
   1958   1.1       cgd 		}
   1959   1.1       cgd 
   1960   1.1       cgd 		p = q;
   1961   1.1       cgd 	}
   1962   1.1       cgd 
   1963   1.1       cgd 	/* skip to action part */
   1964  1.70   thorpej 	while (isblank((unsigned char)*p))
   1965   1.1       cgd 		p++;
   1966   1.1       cgd 
   1967  1.70   thorpej 	if (*p == '-') {
   1968  1.70   thorpej 		syncfile = 0;
   1969  1.70   thorpej 		p++;
   1970  1.70   thorpej 	} else
   1971  1.70   thorpej 		syncfile = 1;
   1972  1.70   thorpej 
   1973  1.70   thorpej 	switch (*p) {
   1974   1.1       cgd 	case '@':
   1975  1.58    itojun 		(void)strlcpy(f->f_un.f_forw.f_hname, ++p,
   1976  1.58    itojun 		    sizeof(f->f_un.f_forw.f_hname));
   1977  1.30    itojun 		memset(&hints, 0, sizeof(hints));
   1978  1.30    itojun 		hints.ai_family = AF_UNSPEC;
   1979  1.30    itojun 		hints.ai_socktype = SOCK_DGRAM;
   1980  1.30    itojun 		hints.ai_protocol = 0;
   1981  1.34     lukem 		error = getaddrinfo(f->f_un.f_forw.f_hname, "syslog", &hints,
   1982  1.34     lukem 		    &res);
   1983  1.30    itojun 		if (error) {
   1984  1.30    itojun 			logerror(gai_strerror(error));
   1985   1.1       cgd 			break;
   1986   1.1       cgd 		}
   1987  1.30    itojun 		f->f_un.f_forw.f_addr = res;
   1988   1.1       cgd 		f->f_type = F_FORW;
   1989  1.36     jwise 		NumForwards++;
   1990   1.1       cgd 		break;
   1991   1.1       cgd 
   1992   1.1       cgd 	case '/':
   1993  1.58    itojun 		(void)strlcpy(f->f_un.f_fname, p, sizeof(f->f_un.f_fname));
   1994   1.1       cgd 		if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
   1995  1.13     enami 			f->f_type = F_UNUSED;
   1996   1.1       cgd 			logerror(p);
   1997   1.1       cgd 			break;
   1998   1.1       cgd 		}
   1999  1.70   thorpej 		if (syncfile)
   2000  1.70   thorpej 			f->f_flags |= FFLAG_SYNC;
   2001   1.1       cgd 		if (isatty(f->f_file))
   2002   1.1       cgd 			f->f_type = F_TTY;
   2003   1.1       cgd 		else
   2004   1.1       cgd 			f->f_type = F_FILE;
   2005   1.1       cgd 		if (strcmp(p, ctty) == 0)
   2006   1.1       cgd 			f->f_type = F_CONSOLE;
   2007   1.1       cgd 		break;
   2008   1.1       cgd 
   2009  1.70   thorpej 	case '|':
   2010  1.70   thorpej 		f->f_un.f_pipe.f_pid = 0;
   2011  1.70   thorpej 		(void) strlcpy(f->f_un.f_pipe.f_pname, p + 1,
   2012  1.70   thorpej 		    sizeof(f->f_un.f_pipe.f_pname));
   2013  1.70   thorpej 		f->f_type = F_PIPE;
   2014  1.70   thorpej 		break;
   2015  1.70   thorpej 
   2016   1.1       cgd 	case '*':
   2017   1.1       cgd 		f->f_type = F_WALL;
   2018   1.1       cgd 		break;
   2019   1.1       cgd 
   2020   1.1       cgd 	default:
   2021   1.1       cgd 		for (i = 0; i < MAXUNAMES && *p; i++) {
   2022   1.1       cgd 			for (q = p; *q && *q != ','; )
   2023   1.1       cgd 				q++;
   2024   1.5     perry 			(void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE);
   2025   1.1       cgd 			if ((q - p) > UT_NAMESIZE)
   2026   1.1       cgd 				f->f_un.f_uname[i][UT_NAMESIZE] = '\0';
   2027   1.1       cgd 			else
   2028   1.1       cgd 				f->f_un.f_uname[i][q - p] = '\0';
   2029   1.1       cgd 			while (*q == ',' || *q == ' ')
   2030   1.1       cgd 				q++;
   2031   1.1       cgd 			p = q;
   2032   1.1       cgd 		}
   2033   1.1       cgd 		f->f_type = F_USERS;
   2034   1.1       cgd 		break;
   2035   1.1       cgd 	}
   2036   1.1       cgd }
   2037   1.1       cgd 
   2038   1.1       cgd 
   2039   1.1       cgd /*
   2040   1.1       cgd  *  Decode a symbolic name to a numeric value
   2041   1.1       cgd  */
   2042   1.5     perry int
   2043  1.53       wiz decode(const char *name, CODE *codetab)
   2044   1.1       cgd {
   2045   1.5     perry 	CODE *c;
   2046   1.5     perry 	char *p, buf[40];
   2047   1.1       cgd 
   2048  1.69       dsl 	if (isdigit((unsigned char)*name))
   2049   1.1       cgd 		return (atoi(name));
   2050   1.1       cgd 
   2051   1.5     perry 	for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) {
   2052  1.69       dsl 		if (isupper((unsigned char)*name))
   2053  1.69       dsl 			*p = tolower((unsigned char)*name);
   2054   1.5     perry 		else
   2055   1.5     perry 			*p = *name;
   2056   1.5     perry 	}
   2057   1.5     perry 	*p = '\0';
   2058   1.1       cgd 	for (c = codetab; c->c_name; c++)
   2059   1.1       cgd 		if (!strcmp(buf, c->c_name))
   2060   1.1       cgd 			return (c->c_val);
   2061   1.1       cgd 
   2062   1.1       cgd 	return (-1);
   2063  1.15       leo }
   2064  1.15       leo 
   2065  1.15       leo /*
   2066  1.15       leo  * Retrieve the size of the kernel message buffer, via sysctl.
   2067  1.15       leo  */
   2068  1.15       leo int
   2069  1.53       wiz getmsgbufsize(void)
   2070  1.15       leo {
   2071  1.15       leo 	int msgbufsize, mib[2];
   2072  1.15       leo 	size_t size;
   2073  1.15       leo 
   2074  1.15       leo 	mib[0] = CTL_KERN;
   2075  1.15       leo 	mib[1] = KERN_MSGBUFSIZE;
   2076  1.15       leo 	size = sizeof msgbufsize;
   2077  1.15       leo 	if (sysctl(mib, 2, &msgbufsize, &size, NULL, 0) == -1) {
   2078  1.47      manu 		dprintf("Couldn't get kern.msgbufsize\n");
   2079  1.15       leo 		return (0);
   2080  1.15       leo 	}
   2081  1.15       leo 	return (msgbufsize);
   2082  1.30    itojun }
   2083  1.30    itojun 
   2084  1.31    itojun int *
   2085  1.53       wiz socksetup(int af)
   2086  1.30    itojun {
   2087  1.31    itojun 	struct addrinfo hints, *res, *r;
   2088  1.70   thorpej 	struct kevent *ev;
   2089  1.31    itojun 	int error, maxs, *s, *socks;
   2090  1.56    itojun 	const int on = 1;
   2091  1.35     jwise 
   2092  1.36     jwise 	if(SecureMode && !NumForwards)
   2093  1.35     jwise 		return(NULL);
   2094  1.30    itojun 
   2095  1.30    itojun 	memset(&hints, 0, sizeof(hints));
   2096  1.30    itojun 	hints.ai_flags = AI_PASSIVE;
   2097  1.30    itojun 	hints.ai_family = af;
   2098  1.30    itojun 	hints.ai_socktype = SOCK_DGRAM;
   2099  1.30    itojun 	error = getaddrinfo(NULL, "syslog", &hints, &res);
   2100  1.30    itojun 	if (error) {
   2101  1.30    itojun 		logerror(gai_strerror(error));
   2102  1.30    itojun 		errno = 0;
   2103  1.70   thorpej 		die(NULL);
   2104  1.30    itojun 	}
   2105  1.31    itojun 
   2106  1.31    itojun 	/* Count max number of sockets we may open */
   2107  1.34     lukem 	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
   2108  1.34     lukem 		continue;
   2109  1.56    itojun 	socks = malloc((maxs+1) * sizeof(int));
   2110  1.31    itojun 	if (!socks) {
   2111  1.47      manu 		logerror("Couldn't allocate memory for sockets");
   2112  1.70   thorpej 		die(NULL);
   2113  1.31    itojun 	}
   2114  1.31    itojun 
   2115  1.31    itojun 	*socks = 0;   /* num of sockets counter at start of array */
   2116  1.56    itojun 	s = socks + 1;
   2117  1.31    itojun 	for (r = res; r; r = r->ai_next) {
   2118  1.31    itojun 		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
   2119  1.31    itojun 		if (*s < 0) {
   2120  1.47      manu 			logerror("socket() failed");
   2121  1.56    itojun 			continue;
   2122  1.56    itojun 		}
   2123  1.56    itojun 		if (r->ai_family == AF_INET6 && setsockopt(*s, IPPROTO_IPV6,
   2124  1.56    itojun 		    IPV6_V6ONLY, &on, sizeof(on)) < 0) {
   2125  1.56    itojun 			logerror("setsockopt(IPV6_V6ONLY) failed");
   2126  1.56    itojun 			close(*s);
   2127  1.31    itojun 			continue;
   2128  1.31    itojun 		}
   2129  1.70   thorpej 
   2130  1.70   thorpej 		if (!SecureMode) {
   2131  1.70   thorpej 			if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
   2132  1.70   thorpej 				logerror("bind() failed");
   2133  1.70   thorpej 				close(*s);
   2134  1.70   thorpej 				continue;
   2135  1.70   thorpej 			}
   2136  1.70   thorpej 			ev = allocevchange();
   2137  1.70   thorpej 			EV_SET(ev, *s, EVFILT_READ, EV_ADD | EV_ENABLE,
   2138  1.70   thorpej 			    0, 0, (intptr_t) dispatch_read_finet);
   2139  1.31    itojun 		}
   2140  1.31    itojun 
   2141  1.31    itojun 		*socks = *socks + 1;
   2142  1.31    itojun 		s++;
   2143  1.31    itojun 	}
   2144  1.31    itojun 
   2145  1.31    itojun 	if (*socks == 0) {
   2146  1.31    itojun 		free (socks);
   2147  1.31    itojun 		if(Debug)
   2148  1.31    itojun 			return(NULL);
   2149  1.31    itojun 		else
   2150  1.70   thorpej 			die(NULL);
   2151  1.30    itojun 	}
   2152  1.30    itojun 	if (res)
   2153  1.30    itojun 		freeaddrinfo(res);
   2154  1.30    itojun 
   2155  1.31    itojun 	return(socks);
   2156   1.1       cgd }
   2157  1.70   thorpej 
   2158  1.70   thorpej /*
   2159  1.70   thorpej  * Fairly similar to popen(3), but returns an open descriptor, as opposed
   2160  1.70   thorpej  * to a FILE *.
   2161  1.70   thorpej  */
   2162  1.70   thorpej int
   2163  1.70   thorpej p_open(char *prog, pid_t *rpid)
   2164  1.70   thorpej {
   2165  1.70   thorpej 	int pfd[2], nulldesc, i;
   2166  1.70   thorpej 	pid_t pid;
   2167  1.70   thorpej 	char *argv[4];	/* sh -c cmd NULL */
   2168  1.70   thorpej 	char errmsg[200];
   2169  1.70   thorpej 
   2170  1.70   thorpej 	if (pipe(pfd) == -1)
   2171  1.70   thorpej 		return (-1);
   2172  1.70   thorpej 	if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1) {
   2173  1.70   thorpej 		/* We are royally screwed anyway. */
   2174  1.70   thorpej 		return (-1);
   2175  1.70   thorpej 	}
   2176  1.70   thorpej 
   2177  1.70   thorpej 	switch ((pid = fork())) {
   2178  1.70   thorpej 	case -1:
   2179  1.70   thorpej 		(void) close(nulldesc);
   2180  1.70   thorpej 		return (-1);
   2181  1.70   thorpej 
   2182  1.70   thorpej 	case 0:
   2183  1.70   thorpej 		argv[0] = "sh";
   2184  1.70   thorpej 		argv[1] = "-c";
   2185  1.70   thorpej 		argv[2] = prog;
   2186  1.70   thorpej 		argv[3] = NULL;
   2187  1.70   thorpej 
   2188  1.70   thorpej 		(void) setsid();	/* avoid catching SIGHUPs. */
   2189  1.70   thorpej 
   2190  1.70   thorpej 		/*
   2191  1.70   thorpej 		 * Reset ignored signals to their default behavior.
   2192  1.70   thorpej 		 */
   2193  1.70   thorpej 		(void)signal(SIGTERM, SIG_DFL);
   2194  1.70   thorpej 		(void)signal(SIGINT, SIG_DFL);
   2195  1.70   thorpej 		(void)signal(SIGQUIT, SIG_DFL);
   2196  1.70   thorpej 		(void)signal(SIGPIPE, SIG_DFL);
   2197  1.70   thorpej 		(void)signal(SIGHUP, SIG_DFL);
   2198  1.70   thorpej 
   2199  1.70   thorpej 		dup2(pfd[0], STDIN_FILENO);
   2200  1.70   thorpej 		dup2(nulldesc, STDOUT_FILENO);
   2201  1.70   thorpej 		dup2(nulldesc, STDERR_FILENO);
   2202  1.70   thorpej 		for (i = getdtablesize(); i > 2; i--)
   2203  1.70   thorpej 			(void) close(i);
   2204  1.70   thorpej 
   2205  1.70   thorpej 		(void) execvp(_PATH_BSHELL, argv);
   2206  1.70   thorpej 		_exit(255);
   2207  1.70   thorpej 	}
   2208  1.70   thorpej 
   2209  1.70   thorpej 	(void) close(nulldesc);
   2210  1.70   thorpej 	(void) close(pfd[0]);
   2211  1.70   thorpej 
   2212  1.70   thorpej 	/*
   2213  1.70   thorpej 	 * Avoid blocking on a hung pipe.  With O_NONBLOCK, we are
   2214  1.70   thorpej 	 * supposed to get an EWOULDBLOCK on writev(2), which is
   2215  1.70   thorpej 	 * caught by the logic above anyway, which will in turn
   2216  1.70   thorpej 	 * close the pipe, and fork a new logging subprocess if
   2217  1.70   thorpej 	 * necessary.  The stale subprocess will be killed some
   2218  1.70   thorpej 	 * time later unless it terminated itself due to closing
   2219  1.70   thorpej 	 * its input pipe.
   2220  1.70   thorpej 	 */
   2221  1.70   thorpej 	if (fcntl(pfd[1], F_SETFL, O_NONBLOCK) == -1) {
   2222  1.70   thorpej 		/* This is bad. */
   2223  1.70   thorpej 		(void) snprintf(errmsg, sizeof(errmsg),
   2224  1.70   thorpej 		    "Warning: cannot change pipe to pid %d to "
   2225  1.70   thorpej 		    "non-blocking.", (int) pid);
   2226  1.70   thorpej 		logerror(errmsg);
   2227  1.70   thorpej 	}
   2228  1.70   thorpej 	*rpid = pid;
   2229  1.70   thorpej 	return (pfd[1]);
   2230  1.70   thorpej }
   2231  1.70   thorpej 
   2232  1.70   thorpej void
   2233  1.70   thorpej deadq_enter(pid_t pid, const char *name)
   2234  1.70   thorpej {
   2235  1.70   thorpej 	dq_t p;
   2236  1.70   thorpej 	int status;
   2237  1.70   thorpej 
   2238  1.70   thorpej 	/*
   2239  1.70   thorpej 	 * Be paranoid: if we can't signal the process, don't enter it
   2240  1.70   thorpej 	 * into the dead queue (perhaps it's already dead).  If possible,
   2241  1.70   thorpej 	 * we try to fetch and log the child's status.
   2242  1.70   thorpej 	 */
   2243  1.70   thorpej 	if (kill(pid, 0) != 0) {
   2244  1.70   thorpej 		if (waitpid(pid, &status, WNOHANG) > 0)
   2245  1.70   thorpej 			log_deadchild(pid, status, name);
   2246  1.70   thorpej 		return;
   2247  1.70   thorpej 	}
   2248  1.70   thorpej 
   2249  1.70   thorpej 	p = malloc(sizeof(*p));
   2250  1.70   thorpej 	if (p == NULL) {
   2251  1.70   thorpej 		errno = 0;
   2252  1.70   thorpej 		logerror("panic: out of memory!");
   2253  1.70   thorpej 		exit(1);
   2254  1.70   thorpej 	}
   2255  1.70   thorpej 
   2256  1.70   thorpej 	p->dq_pid = pid;
   2257  1.70   thorpej 	p->dq_timeout = DQ_TIMO_INIT;
   2258  1.70   thorpej 	TAILQ_INSERT_TAIL(&deadq_head, p, dq_entries);
   2259  1.70   thorpej }
   2260  1.70   thorpej 
   2261  1.70   thorpej int
   2262  1.70   thorpej deadq_remove(pid_t pid)
   2263  1.70   thorpej {
   2264  1.70   thorpej 	dq_t q;
   2265  1.70   thorpej 
   2266  1.70   thorpej 	for (q = TAILQ_FIRST(&deadq_head); q != NULL;
   2267  1.70   thorpej 	     q = TAILQ_NEXT(q, dq_entries)) {
   2268  1.70   thorpej 		if (q->dq_pid == pid) {
   2269  1.70   thorpej 			TAILQ_REMOVE(&deadq_head, q, dq_entries);
   2270  1.70   thorpej 			free(q);
   2271  1.70   thorpej 			return (1);
   2272  1.70   thorpej 		}
   2273  1.70   thorpej 	}
   2274  1.70   thorpej 	return (0);
   2275  1.70   thorpej }
   2276  1.70   thorpej 
   2277  1.70   thorpej void
   2278  1.70   thorpej log_deadchild(pid_t pid, int status, const char *name)
   2279  1.70   thorpej {
   2280  1.70   thorpej 	int code;
   2281  1.70   thorpej 	char buf[256];
   2282  1.70   thorpej 	const char *reason;
   2283  1.70   thorpej 
   2284  1.70   thorpej 	/* Keep strerror() struff out of logerror messages. */
   2285  1.70   thorpej 	errno = 0;
   2286  1.70   thorpej 	if (WIFSIGNALED(status)) {
   2287  1.70   thorpej 		reason = "due to signal";
   2288  1.70   thorpej 		code = WTERMSIG(status);
   2289  1.70   thorpej 	} else {
   2290  1.70   thorpej 		reason = "with status";
   2291  1.70   thorpej 		code = WEXITSTATUS(status);
   2292  1.70   thorpej 		if (code == 0)
   2293  1.70   thorpej 			return;
   2294  1.70   thorpej 	}
   2295  1.70   thorpej 	(void) snprintf(buf, sizeof(buf),
   2296  1.70   thorpej 	    "Logging subprocess %d (%s) exited %s %d.",
   2297  1.70   thorpej 	    pid, name, reason, code);
   2298  1.70   thorpej 	logerror(buf);
   2299  1.70   thorpej }
   2300  1.70   thorpej 
   2301  1.70   thorpej static struct kevent changebuf[8];
   2302  1.70   thorpej static int nchanges;
   2303  1.70   thorpej 
   2304  1.70   thorpej static struct kevent *
   2305  1.70   thorpej allocevchange(void)
   2306  1.70   thorpej {
   2307  1.70   thorpej 
   2308  1.70   thorpej 	if (nchanges == A_CNT(changebuf)) {
   2309  1.70   thorpej 		/* XXX Error handling could be improved. */
   2310  1.70   thorpej 		(void) wait_for_events(NULL, 0);
   2311  1.70   thorpej 	}
   2312  1.70   thorpej 
   2313  1.70   thorpej 	return (&changebuf[nchanges++]);
   2314  1.70   thorpej }
   2315  1.70   thorpej 
   2316  1.70   thorpej static int
   2317  1.70   thorpej wait_for_events(struct kevent *events, size_t nevents)
   2318  1.70   thorpej {
   2319  1.70   thorpej 	int rv;
   2320  1.70   thorpej 
   2321  1.70   thorpej 	rv = kevent(fkq, nchanges ? changebuf : NULL, nchanges,
   2322  1.70   thorpej 		    events, nevents, NULL);
   2323  1.70   thorpej 	nchanges = 0;
   2324  1.70   thorpej 	return (rv);
   2325  1.70   thorpej }
   2326