Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 12 Dec 2016 19:33:40 +0000 (UTC)
From:      Hiroki Sato <hrs@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r309933 - head/usr.sbin/syslogd
Message-ID:  <201612121933.uBCJXen2093959@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: hrs
Date: Mon Dec 12 19:33:40 2016
New Revision: 309933
URL: https://svnweb.freebsd.org/changeset/base/309933

Log:
  - Refactor listening socket list.  All of the listening sockets are
    now maintained in a single linked-list in a transport-independent manner.
  - Use queue.h for linked-list structure.
  - Use linked-list for AllowedPeers.
  - Use getaddrinfo(8) even for Unix Domain sockets.
  - Use macros to type-casting from/to struct sockaddr{,_in,_in6}.
  - Define fu_* macro for union f_un to shorten the member names.
  - Remove an extra #include <sys/type.h>.
  - Add "static" to non-exported symbols.
  - !INET support is still incomplete but will be fixed later.
  
  There is no functional change except for some minor debug messages.

Modified:
  head/usr.sbin/syslogd/Makefile
  head/usr.sbin/syslogd/syslogd.c

Modified: head/usr.sbin/syslogd/Makefile
==============================================================================
--- head/usr.sbin/syslogd/Makefile	Mon Dec 12 19:26:55 2016	(r309932)
+++ head/usr.sbin/syslogd/Makefile	Mon Dec 12 19:33:40 2016	(r309933)
@@ -13,6 +13,9 @@ LIBADD=	util
 
 WARNS?= 3
 
+.if ${MK_INET_SUPPORT} != "no"
+CFLAGS+= -DINET
+.endif
 .if ${MK_INET6_SUPPORT} != "no"
 CFLAGS+= -DINET6
 .endif

Modified: head/usr.sbin/syslogd/syslogd.c
==============================================================================
--- head/usr.sbin/syslogd/syslogd.c	Mon Dec 12 19:26:55 2016	(r309932)
+++ head/usr.sbin/syslogd/syslogd.c	Mon Dec 12 19:33:40 2016	(r309933)
@@ -88,7 +88,6 @@ __FBSDID("$FreeBSD$");
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <sys/syslimits.h>
-#include <sys/types.h>
 
 #include <netinet/in.h>
 #include <netdb.h>
@@ -116,43 +115,56 @@ __FBSDID("$FreeBSD$");
 #define SYSLOG_NAMES
 #include <sys/syslog.h>
 
-const char	*ConfFile = _PATH_LOGCONF;
-const char	*PidFile = _PATH_LOGPID;
-const char	ctty[] = _PATH_CONSOLE;
-static const char	include_str[] = "include";
-static const char	include_ext[] = ".conf";
+static const char *ConfFile = _PATH_LOGCONF;
+static const char *PidFile = _PATH_LOGPID;
+static const char ctty[] = _PATH_CONSOLE;
+static const char include_str[] = "include";
+static const char include_ext[] = ".conf";
 
 #define	dprintf		if (Debug) printf
 
 #define	MAXUNAMES	20	/* maximum number of user names */
 
-/*
- * List of hosts for binding.
- */
-static STAILQ_HEAD(, host) hqueue;
-struct host {
-	char			*name;
-	STAILQ_ENTRY(host)	next;
+#define	sstosa(ss)	((struct sockaddr *)(ss))
+#define	satosin6(sa)	((struct sockaddr_in6 *)(void *)(sa))
+#define	satosin(sa)	((struct sockaddr_in *)(void *)(sa))
+#define	s6_addr32	__u6_addr.__u6_addr32
+#define	IN6_ARE_MASKED_ADDR_EQUAL(d, a, m)	(	\
+	(((d)->s6_addr32[0] ^ (a)->s6_addr32[0]) & (m)->s6_addr32[0]) == 0 && \
+	(((d)->s6_addr32[1] ^ (a)->s6_addr32[1]) & (m)->s6_addr32[1]) == 0 && \
+	(((d)->s6_addr32[2] ^ (a)->s6_addr32[2]) & (m)->s6_addr32[2]) == 0 && \
+	(((d)->s6_addr32[3] ^ (a)->s6_addr32[3]) & (m)->s6_addr32[3]) == 0 )
+/*
+ * List of peers and sockets for binding.
+ */
+struct peer {
+	const char	*pe_name;
+	const char	*pe_serv;
+	mode_t		pe_mode;
+	STAILQ_ENTRY(peer)	next;
 };
-
-/*
- * Unix sockets.
- * We have two default sockets, one with 666 permissions,
- * and one for privileged programs.
- */
-struct funix {
-	int			s;
-	const char		*name;
-	mode_t			mode;
-	STAILQ_ENTRY(funix)	next;
+struct socklist {
+	struct sockaddr_storage	sl_ss;
+	int			sl_socket;
+	struct peer		*sl_peer;
+	STAILQ_ENTRY(socklist)	next;
 };
-struct funix funix_secure =	{ -1, _PATH_LOG_PRIV, S_IRUSR | S_IWUSR,
-				{ NULL } };
-struct funix funix_default =	{ -1, _PATH_LOG, DEFFILEMODE,
-				{ &funix_secure } };
+static STAILQ_HEAD(, socklist) shead = STAILQ_HEAD_INITIALIZER(shead);
 
-STAILQ_HEAD(, funix) funixes =	{ &funix_default,
-				&(funix_secure.next.stqe_next) };
+static struct peer funix_secure = {
+	.pe_name = _PATH_LOG_PRIV,
+	.pe_mode = S_IRUSR | S_IWUSR,
+	.next = {NULL},
+};
+static struct peer funix_default = {
+	.pe_name = _PATH_LOG,
+	.pe_mode = DEFFILEMODE,
+	.next = {&funix_secure},
+};
+static STAILQ_HEAD(, peer) pqueue = {
+	&funix_default,
+	&funix_secure.next.stqe_next,
+};
 
 /*
  * Flags to logmsg().
@@ -172,7 +184,7 @@ STAILQ_HEAD(, funix) funixes =	{ &funix_
  */
 
 struct filed {
-	struct	filed *f_next;		/* next in linked list */
+	STAILQ_ENTRY(filed)	next;	/* next in linked list */
 	short	f_type;			/* entry type, see below */
 	short	f_file;			/* file descriptor */
 	time_t	f_time;			/* time this was last written */
@@ -196,6 +208,12 @@ struct filed {
 			pid_t	f_pid;
 		} f_pipe;
 	} f_un;
+#define	fu_uname	f_un.f_uname
+#define	fu_forw_hname	f_un.f_forw.f_hname
+#define	fu_forw_addr	f_un.f_forw.f_addr
+#define	fu_fname	f_un.f_fname
+#define	fu_pipe_pname	f_un.f_pipe.f_pname
+#define	fu_pipe_pid	f_un.f_pipe.f_pid
 	char	f_prevline[MAXSVLINE];		/* last message logged */
 	char	f_lasttime[16];			/* time of last occurrence */
 	char	f_prevhost[MAXHOSTNAMELEN];	/* host from which recd. */
@@ -211,15 +229,12 @@ struct filed {
 /*
  * Queue of about-to-be dead processes we should watch out for.
  */
-
-TAILQ_HEAD(stailhead, deadq_entry) deadq_head;
-struct stailhead *deadq_headp;
-
 struct deadq_entry {
 	pid_t				dq_pid;
 	int				dq_timeout;
 	TAILQ_ENTRY(deadq_entry)	dq_entries;
 };
+static TAILQ_HEAD(, deadq_entry) deadq_head;
 
 /*
  * The timeout to apply to processes waiting on the dead queue.  Unit
@@ -249,7 +264,9 @@ struct allowedpeer {
 #define a_addr u.numeric.addr
 #define a_mask u.numeric.mask
 #define a_name u.name
+	STAILQ_ENTRY(allowedpeer)	next;
 };
+static STAILQ_HEAD(, allowedpeer) aphead = STAILQ_HEAD_INITIALIZER(aphead);
 
 
 /*
@@ -257,7 +274,7 @@ struct allowedpeer {
  * in seconds after previous message is logged.  After each flush,
  * we move to the next interval until we reach the largest.
  */
-int	repeatinterval[] = { 30, 120, 600 };	/* # of secs before flush */
+static int repeatinterval[] = { 30, 120, 600 };	/* # of secs before flush */
 #define	MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
 #define	REPEATTIME(f)	((f)->f_time + repeatinterval[(f)->f_repeatcount])
 #define	BACKOFF(f)	{ if (++(f)->f_repeatcount > MAXREPEAT) \
@@ -274,12 +291,13 @@ int	repeatinterval[] = { 30, 120, 600 };
 #define F_WALL		6		/* everyone logged on */
 #define F_PIPE		7		/* pipe to program */
 
-const char *TypeNames[8] = {
+static const char *TypeNames[8] = {
 	"UNUSED",	"FILE",		"TTY",		"CONSOLE",
 	"FORW",		"USERS",	"WALL",		"PIPE"
 };
 
-static struct filed *Files;	/* Log files that we write to */
+static STAILQ_HEAD(, filed) fhead =
+    STAILQ_HEAD_INITIALIZER(fhead);	/* Log files that we write to */
 static struct filed consfile;	/* Console */
 
 static int	Debug;		/* debug flag */
@@ -287,7 +305,6 @@ static int	Foreground = 0;	/* Run in for
 static int	resolve = 1;	/* resolve hostname */
 static char	LocalHostName[MAXHOSTNAMELEN];	/* our hostname */
 static const char *LocalDomain;	/* our local domain name */
-static int	*finet;		/* Internet datagram sockets */
 static int	fklog = -1;	/* /dev/klog */
 static int	Initialized;	/* set when we have initialized ourselves */
 static int	MarkInterval = 20 * 60;	/* interval between marks in seconds */
@@ -307,8 +324,6 @@ static int	logflags = O_WRONLY|O_APPEND;
 
 static char	bootfile[MAXLINE+1]; /* booted kernel file */
 
-struct allowedpeer *AllowedPeers; /* List of allowed peers */
-static int	NumAllowed;	/* Number of entries in AllowedPeers */
 static int	RemoteAddDate;	/* Always set the date on remote messages */
 
 static int	UniquePriority;	/* Only log specified priority? */
@@ -318,7 +333,7 @@ static int	KeepKernFac;	/* Keep remotely
 static int	needdofsync = 0; /* Are any file(s) waiting to be fsynced? */
 static struct pidfh *pfh;
 
-volatile sig_atomic_t MarkSet, WantDie;
+static volatile sig_atomic_t MarkSet, WantDie;
 
 static int	allowaddr(char *);
 static void	cfline(const char *, struct filed *,
@@ -332,12 +347,12 @@ static void	dodie(int);
 static void	dofsync(void);
 static void	domark(int);
 static void	fprintlog(struct filed *, int, const char *);
-static int	*socksetup(int, char *);
 static void	init(int);
 static void	logerror(const char *);
 static void	logmsg(int, const char *, const char *, int);
 static void	log_deadchild(pid_t, int, const char *);
 static void	markit(void);
+static int	socksetup(struct peer *);
 static int	skip_message(const char *, const char *, int);
 static void	printline(const char *, char *, int);
 static void	printsys(char *);
@@ -368,31 +383,31 @@ close_filed(struct filed *f)
 int
 main(int argc, char *argv[])
 {
-	int ch, i, fdsrmax = 0, l;
-	struct sockaddr_un sunx, fromunix;
-	struct sockaddr_storage frominet;
+	int ch, i, fdsrmax = 0;
+	struct sockaddr_storage ss;
 	fd_set *fdsr = NULL;
 	char line[MAXLINE + 1];
 	const char *hname;
 	struct timeval tv, *tvp;
 	struct sigaction sact;
-	struct host *host;
-	struct funix *fx, *fx1;
+	struct peer *pe;
+	struct socklist *sl;
 	sigset_t mask;
 	pid_t ppid = 1, spid;
-	socklen_t len;
+	socklen_t sslen;
+	char *p;
 
 	if (madvise(NULL, 0, MADV_PROTECT) != 0)
 		dprintf("madvise() failed: %s\n", strerror(errno));
 
-	STAILQ_INIT(&hqueue);
-
 	while ((ch = getopt(argc, argv, "468Aa:b:cCdf:Fkl:m:nNop:P:sS:Tuv"))
 	    != -1)
 		switch (ch) {
+#ifdef INET
 		case '4':
 			family = PF_INET;
 			break;
+#endif
 #ifdef INET6
 		case '6':
 			family = PF_INET6;
@@ -409,13 +424,21 @@ main(int argc, char *argv[])
 				usage();
 			break;
 		case 'b':
-		   {
-			if ((host = malloc(sizeof(struct host))) == NULL)
+			if ((pe = calloc(1, sizeof(*pe))) == NULL)
 				err(1, "malloc failed");
-			host->name = optarg;
-			STAILQ_INSERT_TAIL(&hqueue, host, next);
+			if ((p = strchr(optarg, ':')) == NULL) {
+				/* A hostname or filename only. */
+				pe->pe_name = optarg;
+				pe->pe_serv = "syslog";
+			} else {
+				/* The case of "name:service". */
+				*p++ = '\0';
+				pe->pe_serv = p;
+				pe->pe_name = (strlen(optarg) == 0) ?
+				    NULL : optarg;
+			}
+			STAILQ_INSERT_TAIL(&pqueue, pe, next);
 			break;
-		   }
 		case 'c':
 			no_compress++;
 			break;
@@ -458,17 +481,12 @@ main(int argc, char *argv[])
 				} else
 					errx(1, "invalid mode %s, exiting",
 					    optarg);
-			} else	/* doesn't begin with '/', and no ':' */
-				errx(1, "can't parse path %s", optarg);
-
-			if (strlen(name) >= sizeof(sunx.sun_path))
-				errx(1, "%s path too long, exiting", name);
-			if ((fx = malloc(sizeof(struct funix))) == NULL)
+			}
+			if ((pe = calloc(1, sizeof(*pe))) == NULL)
 				err(1, "malloc failed");
-			fx->s = -1;
-			fx->name = name;
-			fx->mode = mode;
-			STAILQ_INSERT_TAIL(&funixes, fx, next);
+			pe->pe_name = name;
+			pe->pe_mode = mode;
+			STAILQ_INSERT_TAIL(&pqueue, pe, next);
 			break;
 		   }
 		case 'm':		/* mark interval */
@@ -485,9 +503,7 @@ main(int argc, char *argv[])
 			use_bootfile = 1;
 			break;
 		case 'p':		/* path */
-			if (strlen(optarg) >= sizeof(sunx.sun_path))
-				errx(1, "%s path too long, exiting", optarg);
-			funix_default.name = optarg;
+			funix_default.pe_name = optarg;
 			break;
 		case 'P':		/* path for alt. PID */
 			PidFile = optarg;
@@ -496,9 +512,7 @@ main(int argc, char *argv[])
 			SecureMode++;
 			break;
 		case 'S':		/* path for privileged originator */
-			if (strlen(optarg) >= sizeof(sunx.sun_path))
-				errx(1, "%s path too long, exiting", optarg);
-			funix_secure.name = optarg;
+			funix_secure.pe_name = optarg;
 			break;
 		case 'T':
 			RemoteAddDate = 1;
@@ -514,6 +528,8 @@ main(int argc, char *argv[])
 		}
 	if ((argc -= optind) != 0)
 		usage();
+	STAILQ_FOREACH(pe, &pqueue, next)
+		socksetup(pe);
 
 	pfh = pidfile_open(PidFile, 0600, &spid);
 	if (pfh == NULL) {
@@ -529,16 +545,12 @@ main(int argc, char *argv[])
 			pidfile_remove(pfh);
 			exit(1);
 		}
-	} else if (Debug) {
+	} else if (Debug)
 		setlinebuf(stdout);
-	}
-
-	if (NumAllowed)
-		endservent();
 
 	consfile.f_type = F_CONSOLE;
-	(void)strlcpy(consfile.f_un.f_fname, ctty + sizeof _PATH_DEV - 1,
-	    sizeof(consfile.f_un.f_fname));
+	(void)strlcpy(consfile.fu_fname, ctty + sizeof _PATH_DEV - 1,
+	    sizeof(consfile.fu_fname));
 	(void)strlcpy(bootfile, getbootfile(), sizeof(bootfile));
 	(void)signal(SIGTERM, dodie);
 	(void)signal(SIGINT, Debug ? dodie : SIG_IGN);
@@ -561,69 +573,6 @@ main(int argc, char *argv[])
 
 	TAILQ_INIT(&deadq_head);
 
-#ifndef SUN_LEN
-#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
-#endif
-	STAILQ_FOREACH_SAFE(fx, &funixes, next, fx1) {
-		(void)unlink(fx->name);
-		memset(&sunx, 0, sizeof(sunx));
-		sunx.sun_family = AF_LOCAL;
-		(void)strlcpy(sunx.sun_path, fx->name, sizeof(sunx.sun_path));
-		fx->s = socket(PF_LOCAL, SOCK_DGRAM, 0);
-		if (fx->s < 0 ||
-		    bind(fx->s, (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 ||
-		    chmod(fx->name, fx->mode) < 0) {
-			(void)snprintf(line, sizeof line,
-					"cannot create %s", fx->name);
-			logerror(line);
-			dprintf("cannot create %s (%d)\n", fx->name, errno);
-			if (fx == &funix_default || fx == &funix_secure)
-				die(0);
-			else {
-				STAILQ_REMOVE(&funixes, fx, funix, next);
-				continue;
-			}
-		}
-		increase_rcvbuf(fx->s);
-	}
-	if (SecureMode <= 1) {
-		if (STAILQ_EMPTY(&hqueue))
-			finet = socksetup(family, NULL);
-		STAILQ_FOREACH(host, &hqueue, next) {
-			int *finet0, total;
-			finet0 = socksetup(family, host->name);
-			if (finet0 && !finet) {
-				finet = finet0;
-			} else if (finet0 && finet) {
-				total = *finet0 + *finet + 1;
-				finet = realloc(finet, total * sizeof(int));
-				if (finet == NULL)
-					err(1, "realloc failed");
-				for (i = 1; i <= *finet0; i++) {
-					finet[(*finet)+i] = finet0[i];
-				}
-				*finet = total - 1;
-				free(finet0);
-			}
-		}
-	}
-
-	if (finet) {
-		if (SecureMode) {
-			for (i = 0; i < *finet; i++) {
-				if (shutdown(finet[i+1], SHUT_RD) < 0 &&
-				    errno != ENOTCONN) {
-					logerror("shutdown");
-					if (!Debug)
-						die(0);
-				}
-			}
-		} else {
-			dprintf("listening on inet and/or inet6 socket\n");
-		}
-		dprintf("sending on inet and/or inet6 socket\n");
-	}
-
 	if ((fklog = open(_PATH_KLOG, O_RDONLY|O_NONBLOCK, 0)) < 0)
 		dprintf("can't open %s (%d)\n", _PATH_KLOG, errno);
 
@@ -646,15 +595,10 @@ main(int argc, char *argv[])
 
 	if (fklog != -1 && fklog > fdsrmax)
 		fdsrmax = fklog;
-	if (finet && !SecureMode) {
-		for (i = 0; i < *finet; i++) {
-		    if (finet[i+1] != -1 && finet[i+1] > fdsrmax)
-			fdsrmax = finet[i+1];
-		}
+	STAILQ_FOREACH(sl, &shead, next) {
+		if (sl->sl_socket > fdsrmax)
+			fdsrmax = sl->sl_socket;
 	}
-	STAILQ_FOREACH(fx, &funixes, next)
-		if (fx->s > fdsrmax)
-			fdsrmax = fx->s;
 
 	fdsr = (fd_set *)calloc(howmany(fdsrmax+1, NFDBITS),
 	    sizeof(fd_mask));
@@ -672,16 +616,11 @@ main(int argc, char *argv[])
 
 		if (fklog != -1)
 			FD_SET(fklog, fdsr);
-		if (finet && !SecureMode) {
-			for (i = 0; i < *finet; i++) {
-				if (finet[i+1] != -1)
-					FD_SET(finet[i+1], fdsr);
-			}
+		STAILQ_FOREACH(sl, &shead, next) {
+			if (sl->sl_socket != -1)
+				FD_SET(sl->sl_socket, fdsr);
 		}
-		STAILQ_FOREACH(fx, &funixes, next)
-			FD_SET(fx->s, fdsr);
-
-		i = select(fdsrmax+1, fdsr, NULL, NULL,
+		i = select(fdsrmax + 1, fdsr, NULL, NULL,
 		    needdofsync ? &tv : tvp);
 		switch (i) {
 		case 0:
@@ -700,34 +639,40 @@ main(int argc, char *argv[])
 		}
 		if (fklog != -1 && FD_ISSET(fklog, fdsr))
 			readklog();
-		if (finet && !SecureMode) {
-			for (i = 0; i < *finet; i++) {
-				if (FD_ISSET(finet[i+1], fdsr)) {
-					len = sizeof(frominet);
-					l = recvfrom(finet[i+1], line, MAXLINE,
-					     0, (struct sockaddr *)&frominet,
-					     &len);
-					if (l > 0) {
-						line[l] = '\0';
-						hname = cvthname((struct sockaddr *)&frominet);
-						unmapped((struct sockaddr *)&frominet);
-						if (validate((struct sockaddr *)&frominet, hname))
-							printline(hname, line, RemoteAddDate ? ADDDATE : 0);
-					} else if (l < 0 && errno != EINTR)
-						logerror("recvfrom inet");
+		STAILQ_FOREACH(sl, &shead, next) {
+			int date, len;
+
+			if (FD_ISSET(sl->sl_socket, fdsr)) {
+				sslen = sizeof(ss);
+				dprintf("sslen(1) = %d\n", sslen);
+				len = recvfrom(sl->sl_socket, line,
+				    sizeof(line) - 1, 0,
+				    sstosa(&ss), &sslen);
+				dprintf("sslen(2) = %d\n", sslen);
+				if (len == 0)
+					continue;
+				if (len < 0) {
+					if (errno != EINTR)
+						logerror("recvfrom");
+					continue;
 				}
-			}
-		}
-		STAILQ_FOREACH(fx, &funixes, next) {
-			if (FD_ISSET(fx->s, fdsr)) {
-				len = sizeof(fromunix);
-				l = recvfrom(fx->s, line, MAXLINE, 0,
-				    (struct sockaddr *)&fromunix, &len);
-				if (l > 0) {
-					line[l] = '\0';
-					printline(LocalHostName, line, 0);
-				} else if (l < 0 && errno != EINTR)
-					logerror("recvfrom unix");
+				/* Received valid data. */
+				line[len] = '\0';
+				if (sl->sl_ss.ss_family == AF_LOCAL) {
+					hname = LocalHostName;
+					date = 0;
+				} else {
+					hname = cvthname(sstosa(&ss));
+					unmapped(sstosa(&ss));
+					if (validate(sstosa(&ss), hname) == 0)
+						hname = NULL;
+					date = RemoteAddDate ? ADDDATE : 0;
+				}
+				if (hname != NULL)
+					printline(hname, line, date);
+				else
+					dprintf("Invalid msg from "
+					    "%s was ignored.", hname);
 			}
 		}
 	}
@@ -741,18 +686,17 @@ unmapped(struct sockaddr *sa)
 	struct sockaddr_in6 *sin6;
 	struct sockaddr_in sin4;
 
-	if (sa->sa_family != AF_INET6)
-		return;
-	if (sa->sa_len != sizeof(struct sockaddr_in6) ||
-	    sizeof(sin4) > sa->sa_len)
+	if (sa == NULL ||
+	    sa->sa_family != AF_INET6 ||
+	    sa->sa_len != sizeof(*sin6))
 		return;
-	sin6 = (struct sockaddr_in6 *)sa;
+	sin6 = satosin6(sa);
 	if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
 		return;
 
 	memset(&sin4, 0, sizeof(sin4));
 	sin4.sin_family = AF_INET;
-	sin4.sin_len = sizeof(struct sockaddr_in);
+	sin4.sin_len = sizeof(sin4);
 	memcpy(&sin4.sin_addr, &sin6->sin6_addr.s6_addr[12],
 	       sizeof(sin4.sin_addr));
 	sin4.sin_port = sin6->sin6_port;
@@ -1043,7 +987,7 @@ logmsg(int pri, const char *msg, const c
 		(void)sigsetmask(omask);
 		return;
 	}
-	for (f = Files; f; f = f->f_next) {
+	STAILQ_FOREACH(f, &fhead, next) {
 		/* skip messages that are incorrect priority */
 		if (!(((f->f_pcmp[fac] & PRI_EQ) && (f->f_pmask[fac] == prilev))
 		     ||((f->f_pcmp[fac] & PRI_LT) && (f->f_pmask[fac] < prilev))
@@ -1120,7 +1064,7 @@ dofsync(void)
 {
 	struct filed *f;
 
-	for (f = Files; f; f = f->f_next) {
+	STAILQ_FOREACH(f, &fhead, next) {
 		if ((f->f_type == F_FILE) &&
 		    (f->f_flags & FFLAG_NEEDSYNC)) {
 			f->f_flags &= ~FFLAG_NEEDSYNC;
@@ -1136,7 +1080,7 @@ fprintlog(struct filed *f, int flags, co
 	struct iovec iov[IOV_SIZE];
 	struct iovec *v;
 	struct addrinfo *r;
-	int i, l, lsent = 0;
+	int l, lsent = 0;
 	char line[MAXLINE + 1], repbuf[80], greetings[200], *wmsg = NULL;
 	char nul[] = "", space[] = " ", lf[] = "\n", crlf[] = "\r\n";
 	const char *msgret;
@@ -1244,12 +1188,11 @@ fprintlog(struct filed *f, int flags, co
 		break;
 
 	case F_FORW:
-		port = (int)ntohs(((struct sockaddr_in *)
-			    (f->f_un.f_forw.f_addr->ai_addr))->sin_port);
+		port = ntohs(satosin(f->fu_forw_addr->ai_addr)->sin_port);
 		if (port != 514) {
-			dprintf(" %s:%d\n", f->f_un.f_forw.f_hname, port);
+			dprintf(" %s:%d\n", f->fu_forw_hname, port);
 		} else {
-			dprintf(" %s\n", f->f_un.f_forw.f_hname);
+			dprintf(" %s\n", f->fu_forw_hname);
 		}
 		/* check for local vs remote messages */
 		if (strcasecmp(f->f_prevhost, LocalHostName))
@@ -1266,57 +1209,51 @@ fprintlog(struct filed *f, int flags, co
 		else if (l > MAXLINE)
 			l = MAXLINE;
 
-		if (finet) {
-			for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) {
-				for (i = 0; i < *finet; i++) {
-#if 0
-					/*
-					 * should we check AF first, or just
-					 * trial and error? FWD
-					 */
-					if (r->ai_family ==
-					    address_family_of(finet[i+1]))
-#endif
-					lsent = sendto(finet[i+1], line, l, 0,
-					    r->ai_addr, r->ai_addrlen);
-					if (lsent == l)
-						break;
-				}
-				if (lsent == l && !send_to_all)
+		for (r = f->fu_forw_addr; r; r = r->ai_next) {
+			struct socklist *sl;
+
+			STAILQ_FOREACH(sl, &shead, next) {
+				if (sl->sl_ss.ss_family == AF_LOCAL)
+					continue;
+				lsent = sendto(sl->sl_socket, line, l, 0,
+				    r->ai_addr, r->ai_addrlen);
+				if (lsent == l)
 					break;
 			}
-			dprintf("lsent/l: %d/%d\n", lsent, l);
-			if (lsent != l) {
-				int e = errno;
-				logerror("sendto");
-				errno = e;
-				switch (errno) {
-				case ENOBUFS:
-				case ENETDOWN:
-				case ENETUNREACH:
-				case EHOSTUNREACH:
-				case EHOSTDOWN:
-				case EADDRNOTAVAIL:
-					break;
-				/* case EBADF: */
-				/* case EACCES: */
-				/* case ENOTSOCK: */
-				/* case EFAULT: */
-				/* case EMSGSIZE: */
-				/* case EAGAIN: */
-				/* case ENOBUFS: */
-				/* case ECONNREFUSED: */
-				default:
-					dprintf("removing entry: errno=%d\n", e);
-					f->f_type = F_UNUSED;
-					break;
-				}
+			if (lsent == l && !send_to_all)
+				break;
+		}
+		dprintf("lsent/l: %d/%d\n", lsent, l);
+		if (lsent != l) {
+			int e = errno;
+			logerror("sendto");
+			errno = e;
+			switch (errno) {
+			case ENOBUFS:
+			case ENETDOWN:
+			case ENETUNREACH:
+			case EHOSTUNREACH:
+			case EHOSTDOWN:
+			case EADDRNOTAVAIL:
+				break;
+			/* case EBADF: */
+			/* case EACCES: */
+			/* case ENOTSOCK: */
+			/* case EFAULT: */
+			/* case EMSGSIZE: */
+			/* case EAGAIN: */
+			/* case ENOBUFS: */
+			/* case ECONNREFUSED: */
+			default:
+				dprintf("removing entry: errno=%d\n", e);
+				f->f_type = F_UNUSED;
+				break;
 			}
 		}
 		break;
 
 	case F_FILE:
-		dprintf(" %s\n", f->f_un.f_fname);
+		dprintf(" %s\n", f->fu_fname);
 		v->iov_base = lf;
 		v->iov_len = 1;
 		if (writev(f->f_file, iov, IOV_SIZE) < 0) {
@@ -1329,7 +1266,7 @@ fprintlog(struct filed *f, int flags, co
 				int e = errno;
 				close_filed(f);
 				errno = e;
-				logerror(f->f_un.f_fname);
+				logerror(f->fu_fname);
 			}
 		} else if ((flags & SYNC_FILE) && (f->f_flags & FFLAG_SYNC)) {
 			f->f_flags |= FFLAG_NEEDSYNC;
@@ -1338,26 +1275,26 @@ fprintlog(struct filed *f, int flags, co
 		break;
 
 	case F_PIPE:
-		dprintf(" %s\n", f->f_un.f_pipe.f_pname);
+		dprintf(" %s\n", f->fu_pipe_pname);
 		v->iov_base = lf;
 		v->iov_len = 1;
-		if (f->f_un.f_pipe.f_pid == 0) {
-			if ((f->f_file = p_open(f->f_un.f_pipe.f_pname,
-						&f->f_un.f_pipe.f_pid)) < 0) {
+		if (f->fu_pipe_pid == 0) {
+			if ((f->f_file = p_open(f->fu_pipe_pname,
+						&f->fu_pipe_pid)) < 0) {
 				f->f_type = F_UNUSED;
-				logerror(f->f_un.f_pipe.f_pname);
+				logerror(f->fu_pipe_pname);
 				break;
 			}
 		}
 		if (writev(f->f_file, iov, IOV_SIZE) < 0) {
 			int e = errno;
 			close_filed(f);
-			if (f->f_un.f_pipe.f_pid > 0)
-				deadq_enter(f->f_un.f_pipe.f_pid,
-					    f->f_un.f_pipe.f_pname);
-			f->f_un.f_pipe.f_pid = 0;
+			if (f->fu_pipe_pid > 0)
+				deadq_enter(f->fu_pipe_pid,
+					    f->fu_pipe_pname);
+			f->fu_pipe_pid = 0;
 			errno = e;
-			logerror(f->f_un.f_pipe.f_pname);
+			logerror(f->fu_pipe_pname);
 		}
 		break;
 
@@ -1369,12 +1306,12 @@ fprintlog(struct filed *f, int flags, co
 		/* FALLTHROUGH */
 
 	case F_TTY:
-		dprintf(" %s%s\n", _PATH_DEV, f->f_un.f_fname);
+		dprintf(" %s%s\n", _PATH_DEV, f->fu_fname);
 		v->iov_base = crlf;
 		v->iov_len = 2;
 
 		errno = 0;	/* ttymsg() only sometimes returns an errno */
-		if ((msgret = ttymsg(iov, IOV_SIZE, f->f_un.f_fname, 10))) {
+		if ((msgret = ttymsg(iov, IOV_SIZE, f->fu_fname, 10))) {
 			f->f_type = F_UNUSED;
 			logerror(msgret);
 		}
@@ -1423,9 +1360,9 @@ wallmsg(struct filed *f, struct iovec *i
 		}
 		/* should we send the message to this user? */
 		for (i = 0; i < MAXUNAMES; i++) {
-			if (!f->f_un.f_uname[i][0])
+			if (!f->fu_uname[i][0])
 				break;
-			if (!strcmp(f->f_un.f_uname[i], ut->ut_user)) {
+			if (!strcmp(f->fu_uname[i], ut->ut_user)) {
 				if ((p = ttymsg_check(iov, iovlen, ut->ut_line,
 				    TTYMSGTIME)) != NULL) {
 					errno = 0;	/* already in msg */
@@ -1476,20 +1413,18 @@ reapchild(int signo __unused)
 
 		/* First, look if it's a process from the dead queue. */
 		if (deadq_remove(pid))
-			goto oncemore;
+			continue;
 
 		/* Now, look in list of active processes. */
-		for (f = Files; f; f = f->f_next)
+		STAILQ_FOREACH(f, &fhead, next) {
 			if (f->f_type == F_PIPE &&
-			    f->f_un.f_pipe.f_pid == pid) {
+			    f->fu_pipe_pid == pid) {
 				close_filed(f);
-				f->f_un.f_pipe.f_pid = 0;
-				log_deadchild(pid, status,
-					      f->f_un.f_pipe.f_pname);
+				f->fu_pipe_pid = 0;
+				log_deadchild(pid, status, f->fu_pipe_pname);
 				break;
 			}
-	  oncemore:
-		continue;
+		}
 	}
 }
 
@@ -1503,9 +1438,9 @@ cvthname(struct sockaddr *f)
 	sigset_t omask, nmask;
 	static char hname[NI_MAXHOST], ip[NI_MAXHOST];
 
-	error = getnameinfo((struct sockaddr *)f,
-			    ((struct sockaddr *)f)->sa_len,
-			    ip, sizeof ip, NULL, 0, NI_NUMERICHOST);
+	dprintf("cvthname(%d) len = %d, %zu\n", f->sa_family, f->sa_len, sizeof(struct sockaddr_in6));
+	error = getnameinfo(f, f->sa_len, ip, sizeof(ip), NULL, 0,
+		    NI_NUMERICHOST);
 	dprintf("cvthname(%s)\n", ip);
 
 	if (error) {
@@ -1575,19 +1510,19 @@ static void
 die(int signo)
 {
 	struct filed *f;
-	struct funix *fx;
+	struct socklist *sl;
 	int was_initialized;
 	char buf[100];
 
 	was_initialized = Initialized;
 	Initialized = 0;	/* Don't log SIGCHLDs. */
-	for (f = Files; f != NULL; f = f->f_next) {
+	STAILQ_FOREACH(f, &fhead, next) {
 		/* flush any pending output */
 		if (f->f_prevcount)
 			fprintlog(f, 0, (char *)NULL);
-		if (f->f_type == F_PIPE && f->f_un.f_pipe.f_pid > 0) {
+		if (f->f_type == F_PIPE && f->fu_pipe_pid > 0) {
 			close_filed(f);
-			f->f_un.f_pipe.f_pid = 0;
+			f->fu_pipe_pid = 0;
 		}
 	}
 	Initialized = was_initialized;
@@ -1597,8 +1532,10 @@ die(int signo)
 		errno = 0;
 		logerror(buf);
 	}
-	STAILQ_FOREACH(fx, &funixes, next)
-		(void)unlink(fx->name);
+	STAILQ_FOREACH(sl, &shead, next) {
+		if (sl->sl_ss.ss_family == AF_LOCAL)
+			unlink(sl->sl_peer->pe_name);
+	}
 	pidfile_remove(pfh);
 
 	exit(1);
@@ -1626,7 +1563,7 @@ configfiles(const struct dirent *dp)
 }
 
 static void
-readconfigfile(FILE *cf, struct filed **nextp, int allow_includes)
+readconfigfile(FILE *cf, int allow_includes)
 {
 	FILE *cf2;
 	struct filed *f;
@@ -1686,7 +1623,7 @@ readconfigfile(FILE *cf, struct filed **
 				if (cf2 == NULL)
 					continue;
 				dprintf("reading %s\n", file);
-				readconfigfile(cf2, nextp, 0);
+				readconfigfile(cf2, 0);
 				fclose(cf2);
 			}
 			free(ent);
@@ -1749,8 +1686,7 @@ readconfigfile(FILE *cf, struct filed **
 			logerror("calloc");
 			exit(1);
 		}
-		*nextp = f;
-		nextp = &f->f_next;
+		STAILQ_INSERT_TAIL(&fhead, f, next);
 		cfline(cline, f, prog, host);
 	}
 }
@@ -1763,7 +1699,7 @@ init(int signo)
 {
 	int i;
 	FILE *cf;
-	struct filed *f, *next, **nextp;
+	struct filed *f;
 	char *p;
 	char oldLocalHostName[MAXHOSTNAMELEN];
 	char hostMsg[2*MAXHOSTNAMELEN+40];
@@ -1808,7 +1744,7 @@ init(int signo)
 	 *  Close all open log files.
 	 */
 	Initialized = 0;
-	for (f = Files; f != NULL; f = next) {
+	STAILQ_FOREACH(f, &fhead, next) {
 		/* flush any pending output */
 		if (f->f_prevcount)
 			fprintlog(f, 0, (char *)NULL);
@@ -1821,42 +1757,47 @@ init(int signo)
 			close_filed(f);
 			break;
 		case F_PIPE:
-			if (f->f_un.f_pipe.f_pid > 0) {
+			if (f->fu_pipe_pid > 0) {
 				close_filed(f);
-				deadq_enter(f->f_un.f_pipe.f_pid,
-					    f->f_un.f_pipe.f_pname);
+				deadq_enter(f->fu_pipe_pid,
+					    f->fu_pipe_pname);
 			}
-			f->f_un.f_pipe.f_pid = 0;
+			f->fu_pipe_pid = 0;
 			break;
 		}
-		next = f->f_next;
-		if (f->f_program) free(f->f_program);
-		if (f->f_host) free(f->f_host);
-		free((char *)f);
 	}
-	Files = NULL;
-	nextp = &Files;
+	while(!STAILQ_EMPTY(&fhead)) {
+		f = STAILQ_FIRST(&fhead);
+		STAILQ_REMOVE_HEAD(&fhead, next);
+		free(f->f_program);
+		free(f->f_host);
+		free(f);
+	}
 
 	/* open the configuration file */
 	if ((cf = fopen(ConfFile, "r")) == NULL) {
 		dprintf("cannot open %s\n", ConfFile);
-		*nextp = (struct filed *)calloc(1, sizeof(*f));
-		if (*nextp == NULL) {
+		f = calloc(1, sizeof(*f));
+		if (f == NULL) {
 			logerror("calloc");
 			exit(1);
 		}
-		cfline("*.ERR\t/dev/console", *nextp, "*", "*");
-		(*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
-		if ((*nextp)->f_next == NULL) {
+		cfline("*.ERR\t/dev/console", f, "*", "*");
+		STAILQ_INSERT_TAIL(&fhead, f, next);
+
+		f = calloc(1, sizeof(*f));
+		if (f == NULL) {
 			logerror("calloc");
 			exit(1);
 		}
-		cfline("*.PANIC\t*", (*nextp)->f_next, "*", "*");
+		cfline("*.PANIC\t*", f, "*", "*");
+		STAILQ_INSERT_TAIL(&fhead, f, next);
+
 		Initialized = 1;
 		return;
 	}
 
-	readconfigfile(cf, &Files, 1);
+	readconfigfile(cf, 1);
 
 	/* close the configuration file */
 	(void)fclose(cf);
@@ -1865,7 +1806,7 @@ init(int signo)
 
 	if (Debug) {
 		int port;
-		for (f = Files; f; f = f->f_next) {
+		STAILQ_FOREACH(f, &fhead, next) {
 			for (i = 0; i <= LOG_NFACILITIES; i++)
 				if (f->f_pmask[i] == INTERNAL_NOPRI)
 					printf("X ");
@@ -1874,32 +1815,31 @@ init(int signo)
 			printf("%s: ", TypeNames[f->f_type]);
 			switch (f->f_type) {
 			case F_FILE:
-				printf("%s", f->f_un.f_fname);
+				printf("%s", f->fu_fname);
 				break;
 
 			case F_CONSOLE:
 			case F_TTY:
-				printf("%s%s", _PATH_DEV, f->f_un.f_fname);
+				printf("%s%s", _PATH_DEV, f->fu_fname);
 				break;
 
 			case F_FORW:
-				port = (int)ntohs(((struct sockaddr_in *)
-				    (f->f_un.f_forw.f_addr->ai_addr))->sin_port);
+				port = ntohs(satosin(f->fu_forw_addr->ai_addr)->sin_port);
 				if (port != 514) {
 					printf("%s:%d",
-						f->f_un.f_forw.f_hname, port);
+						f->fu_forw_hname, port);
 				} else {
-					printf("%s", f->f_un.f_forw.f_hname);
+					printf("%s", f->fu_forw_hname);
 				}

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201612121933.uBCJXen2093959>