Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 9 Jul 2012 08:11:17 +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: r238282 - in head: etc/rc.d usr.sbin/rarpd
Message-ID:  <201207090811.q698BHEt004552@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: hrs
Date: Mon Jul  9 08:11:16 2012
New Revision: 238282
URL: http://svn.freebsd.org/changeset/base/238282

Log:
  - Add IFT_L2VLAN (vlan(4)) support.
  - Add -P option to support PID file.  When -a is specified /var/run/rarpd.pid
    is used, and when an interface is specified /var/run/rarpd.<ifname>.pid is
    used by default.

Modified:
  head/etc/rc.d/rarpd
  head/usr.sbin/rarpd/Makefile
  head/usr.sbin/rarpd/rarpd.8
  head/usr.sbin/rarpd/rarpd.c

Modified: head/etc/rc.d/rarpd
==============================================================================
--- head/etc/rc.d/rarpd	Mon Jul  9 07:37:10 2012	(r238281)
+++ head/etc/rc.d/rarpd	Mon Jul  9 08:11:16 2012	(r238282)
@@ -13,8 +13,9 @@
 name="rarpd"
 rcvar="rarpd_enable"
 command="/usr/sbin/${name}"
-pidfile="/var/run/${name}.pid"
 required_files="/etc/ethers"
 
 load_rc_config $name
+pidfile="${rarpd_pidfile:-/var/run/${name}.pid}"
+
 run_rc_command "$1"

Modified: head/usr.sbin/rarpd/Makefile
==============================================================================
--- head/usr.sbin/rarpd/Makefile	Mon Jul  9 07:37:10 2012	(r238281)
+++ head/usr.sbin/rarpd/Makefile	Mon Jul  9 08:11:16 2012	(r238282)
@@ -4,6 +4,9 @@
 PROG=	rarpd
 MAN=	rarpd.8
 
+DPADD=	${LIBUTIL}
+LDADD=	-lutil
+
 WARNS?=	3
 # This breaks with format strings returned by expand_syslog_m().. argh!
 #FORMAT_AUDIT?=	1

Modified: head/usr.sbin/rarpd/rarpd.8
==============================================================================
--- head/usr.sbin/rarpd/rarpd.8	Mon Jul  9 07:37:10 2012	(r238281)
+++ head/usr.sbin/rarpd/rarpd.8	Mon Jul  9 08:11:16 2012	(r238282)
@@ -16,7 +16,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd November 16, 2001
+.Dd July 9, 2012
 .Dt RARPD 8
 .Os
 .Sh NAME
@@ -27,9 +27,11 @@
 .Fl a
 .Op Fl dfsv
 .Op Fl t Ar directory
+.Op Fl P Ar pidfile
 .Nm
 .Op Fl dfsv
 .Op Fl t Ar directory
+.Op Fl P Ar pidfile
 .Ar interface
 .Sh DESCRIPTION
 The
@@ -92,6 +94,15 @@ instead of via
 .Xr syslog 3 .
 .It Fl f
 Run in the foreground.
+.It Fl P
+Specify the pathname of the PID file.
+If not specified, 
+.Pa /var/run/rarpd.pid
+or
+.Pa /var/run/rarpd.ifname.pid
+will be used depending on the
+.Fl a
+flag or the specified interface name.
 .It Fl s
 Supply a response to any RARP request for which an ethernet to IP address
 mapping exists; do not depend on the existence of
@@ -115,6 +126,7 @@ Enable verbose syslogging.
 .It Pa /etc/ethers
 .It Pa /etc/hosts
 .It Pa /tftpboot
+.It Pa /var/run/rarpd.pid
 .El
 .Sh SEE ALSO
 .Xr bpf 4

Modified: head/usr.sbin/rarpd/rarpd.c
==============================================================================
--- head/usr.sbin/rarpd/rarpd.c	Mon Jul  9 07:37:10 2012	(r238281)
+++ head/usr.sbin/rarpd/rarpd.c	Mon Jul  9 08:11:16 2012	(r238282)
@@ -27,8 +27,8 @@ __FBSDID("$FreeBSD$");
 /*
  * rarpd - Reverse ARP Daemon
  *
- * Usage:	rarpd -a [-dfsv] [-t directory] [hostname]
- *		rarpd [-dfsv] [-t directory] interface [hostname]
+ * Usage:	rarpd -a [-dfsv] [-t directory] [-P pidfile] [hostname]
+ *		rarpd [-dfsv] [-t directory] [-P pidfile] interface [hostname]
  *
  * 'hostname' is optional solely for backwards compatibility with Sun's rarpd.
  * Currently, the argument is ignored.
@@ -61,6 +61,7 @@ __FBSDID("$FreeBSD$");
 #include <syslog.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <libutil.h>
 
 /* Cast a struct sockaddr to a struct sockaddr_in */
 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
@@ -99,6 +100,11 @@ int sflag;			/* ignore /tftpboot */
 
 static	u_char zero[6];
 
+static char pidfile_buf[PATH_MAX];
+static char *pidfile;
+#define	RARPD_PIDFILE	"/var/run/rarpd.%s.pid"
+static struct pidfh *pidfile_fh;
+
 static int	bpf_open(void);
 static in_addr_t	choose_ipaddr(in_addr_t **, in_addr_t, in_addr_t);
 static char	*eatoa(u_char *);
@@ -140,7 +146,7 @@ main(int argc, char *argv[])
 	openlog(name, LOG_PID | LOG_CONS, LOG_DAEMON);
 
 	opterr = 0;
-	while ((op = getopt(argc, argv, "adfst:v")) != -1)
+	while ((op = getopt(argc, argv, "adfsP:t:v")) != -1)
 		switch (op) {
 		case 'a':
 			++aflag;
@@ -158,6 +164,12 @@ main(int argc, char *argv[])
 			++sflag;
 			break;
 
+		case 'P':
+			strncpy(pidfile_buf, optarg, sizeof(pidfile_buf) - 1);
+			pidfile_buf[sizeof(pidfile_buf) - 1] = '\0';
+			pidfile = pidfile_buf;
+			break;
+
 		case 't':
 			tftp_dir = optarg;
 			break;
@@ -181,10 +193,23 @@ main(int argc, char *argv[])
 	init(ifname);
 
 	if (!fflag) {
+		if (pidfile == NULL && ifname != NULL && aflag == 0) {
+			snprintf(pidfile_buf, sizeof(pidfile_buf) - 1,
+			    RARPD_PIDFILE, ifname);
+			pidfile_buf[sizeof(pidfile_buf) - 1] = '\0';
+			pidfile = pidfile_buf;
+		}
+		/* If pidfile == NULL, /var/run/<progname>.pid will be used. */
+		pidfile_fh = pidfile_open(pidfile, 0600, NULL);
+		if (pidfile_fh == NULL)
+			logmsg(LOG_ERR, "Cannot open or create pidfile: %s",
+			    (pidfile == NULL) ? "/var/run/rarpd.pid" : pidfile);
 		if (daemon(0,0)) {
 			logmsg(LOG_ERR, "cannot fork");
+			pidfile_remove(pidfile_fh);
 			exit(1);
 		}
+		pidfile_write(pidfile_fh);
 	}
 	rarp_loop();
 	return(0);
@@ -234,6 +259,7 @@ init_one(struct ifaddrs *ifa, char *targ
 		ii = (struct if_info *)malloc(sizeof(*ii));
 		if (ii == NULL) {
 			logmsg(LOG_ERR, "malloc: %m");
+			pidfile_remove(pidfile_fh);
 			exit(1);
 		}
 		bzero(ii, sizeof(*ii));
@@ -251,6 +277,7 @@ init_one(struct ifaddrs *ifa, char *targ
 		ii2 = (struct if_info *)malloc(sizeof(*ii2));
 		if (ii2 == NULL) {
 			logmsg(LOG_ERR, "malloc: %m");
+			pidfile_remove(pidfile_fh);
 			exit(1);
 		}
 		memcpy(ii2, ii, sizeof(*ii2));
@@ -273,8 +300,11 @@ init_one(struct ifaddrs *ifa, char *targ
 
 	case AF_LINK:
 		ll = (struct sockaddr_dl *)ifa->ifa_addr;
-		if (ll->sdl_type == IFT_ETHER)
+		switch (ll->sdl_type) {
+		case IFT_ETHER:
+		case IFT_L2VLAN:
 			bcopy(LLADDR(ll), ii->ii_eaddr, 6);
+		}
 		break;
 	}
 }
@@ -293,6 +323,7 @@ init(char *target)
 	error = getifaddrs(&ifhead);
 	if (error) {
 		logmsg(LOG_ERR, "getifaddrs: %m");
+		pidfile_remove(pidfile_fh);
 		exit(1);
 	}
 	/*
@@ -339,8 +370,8 @@ static void
 usage(void)
 {
 	(void)fprintf(stderr, "%s\n%s\n",
-	    "usage: rarpd -a [-dfsv] [-t directory]",
-	    "       rarpd [-dfsv] [-t directory] interface");
+	    "usage: rarpd -a [-dfsv] [-t directory] [-P pidfile]",
+	    "       rarpd [-dfsv] [-t directory] [-P pidfile] interface");
 	exit(1);
 }
 
@@ -361,6 +392,7 @@ bpf_open(void)
 
 	if (fd == -1) {
 		logmsg(LOG_ERR, "%s: %m", device);
+		pidfile_remove(pidfile_fh);
 		exit(1);
 	}
 	return fd;
@@ -399,12 +431,12 @@ rarp_open(char *device)
 	immediate = 1;
 	if (ioctl(fd, BIOCIMMEDIATE, &immediate) == -1) {
 		logmsg(LOG_ERR, "BIOCIMMEDIATE: %m");
-		exit(1);
+		goto rarp_open_err;
 	}
 	strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
 	if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) == -1) {
 		logmsg(LOG_ERR, "BIOCSETIF: %m");
-		exit(1);
+		goto rarp_open_err;
 	}
 	/*
 	 * Check that the data link layer is an Ethernet; this code won't
@@ -412,20 +444,24 @@ rarp_open(char *device)
 	 */
 	if (ioctl(fd, BIOCGDLT, (caddr_t)&dlt) == -1) {
 		logmsg(LOG_ERR, "BIOCGDLT: %m");
-		exit(1);
+		goto rarp_open_err;
 	}
 	if (dlt != DLT_EN10MB) {
 		logmsg(LOG_ERR, "%s is not an ethernet", device);
-		exit(1);
+		goto rarp_open_err;
 	}
 	/*
 	 * Set filter program.
 	 */
 	if (ioctl(fd, BIOCSETF, (caddr_t)&filter) == -1) {
 		logmsg(LOG_ERR, "BIOCSETF: %m");
-		exit(1);
+		goto rarp_open_err;
 	}
 	return fd;
+
+rarp_open_err:
+	pidfile_remove(pidfile_fh);
+	exit(1);
 }
 
 /*
@@ -480,16 +516,16 @@ rarp_loop(void)
 
 	if (iflist == NULL) {
 		logmsg(LOG_ERR, "no interfaces");
-		exit(1);
+		goto rarpd_loop_err;
 	}
 	if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t)&bufsize) == -1) {
 		logmsg(LOG_ERR, "BIOCGBLEN: %m");
-		exit(1);
+		goto rarpd_loop_err;
 	}
 	buf = malloc(bufsize);
 	if (buf == NULL) {
 		logmsg(LOG_ERR, "malloc: %m");
-		exit(1);
+		goto rarpd_loop_err;
 	}
 
 	while (1) {
@@ -509,7 +545,7 @@ rarp_loop(void)
 			if (errno == EINTR)
 				continue;
 			logmsg(LOG_ERR, "select: %m");
-			exit(1);
+			goto rarpd_loop_err;
 		}
 		for (ii = iflist; ii != NULL; ii = ii->ii_next) {
 			fd = ii->ii_fd;
@@ -537,6 +573,11 @@ rarp_loop(void)
 		}
 	}
 #undef bhp
+	return;
+
+rarpd_loop_err:
+	pidfile_remove(pidfile_fh);
+	exit(1);
 }
 
 /*
@@ -562,12 +603,12 @@ rarp_bootable(in_addr_t addr)
 	else {
 		if (chdir(tftp_dir) == -1) {
 			logmsg(LOG_ERR, "chdir: %s: %m", tftp_dir);
-			exit(1);
+			goto rarp_bootable_err;
 		}
 		d = opendir(".");
 		if (d == NULL) {
 			logmsg(LOG_ERR, "opendir: %m");
-			exit(1);
+			goto rarp_bootable_err;
 		}
 		dd = d;
 	}
@@ -575,6 +616,10 @@ rarp_bootable(in_addr_t addr)
 		if (strncmp(dent->d_name, ipname, 8) == 0)
 			return 1;
 	return 0;
+
+rarp_bootable_err:
+	pidfile_remove(pidfile_fh);
+	exit(1);
 }
 
 /*
@@ -678,6 +723,7 @@ update_arptab(u_char *ep, in_addr_t ipad
 	r = socket(PF_ROUTE, SOCK_RAW, 0);
 	if (r == -1) {
 		logmsg(LOG_ERR, "raw route socket: %m");
+		pidfile_remove(pidfile_fh);
 		exit(1);
 	}
 	pid = getpid();



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