changeset 2954:32b237987cef draft

Move the pidfile into the dhcpcd context. When dumping a lease by filename, store the filename in the pidfile. Fixes an issue where the filename overflows the interface name size.
author Roy Marples <roy@marples.name>
date Fri, 20 Feb 2015 08:28:04 +0000
parents 3d8a77ba7ca6
children 17d1f58bc244
files dhcp-common.c dhcp.c dhcp6.c dhcpcd.c dhcpcd.h
diffstat 5 files changed, 40 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/dhcp-common.c	Wed Feb 18 20:29:42 2015 +0000
+++ b/dhcp-common.c	Fri Feb 20 08:28:04 2015 +0000
@@ -737,6 +737,11 @@
 {
 	char ssid[len];
 
+	if (ifp->name[0] == '\0') {
+		strlcpy(leasefile, ifp->ctx->pidfile, len);
+		return 0;
+	}
+
 	switch (family) {
 	case AF_INET:
 	case AF_INET6:
--- a/dhcp.c	Wed Feb 18 20:29:42 2015 +0000
+++ b/dhcp.c	Fri Feb 20 08:28:04 2015 +0000
@@ -2919,10 +2919,6 @@
 	dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile),
 	    AF_INET, ifp, "");
 	state->new = read_lease(ifp);
-	if (state->new == NULL && errno == ENOENT) {
-		strlcpy(state->leasefile, ifp->name, sizeof(state->leasefile));
-		state->new = read_lease(ifp);
-	}
 	if (state->new == NULL) {
 		if (errno == ENOENT)
 			syslog(LOG_ERR, "%s: no lease to dump", ifp->name);
--- a/dhcp6.c	Wed Feb 18 20:29:42 2015 +0000
+++ b/dhcp6.c	Fri Feb 20 08:28:04 2015 +0000
@@ -1966,13 +1966,15 @@
 			if (memcmp(&ifo->ia[j].iaid, iaid, sizeof(iaid)) == 0)
 				break;
 		}
-		if (j == ifo->ia_len) {
+		if (j == ifo->ia_len &&
+		    !(ifo->ia_len == 0 && ifp->ctx->options & DHCPCD_DUMPLEASE))
+		{
 			syslog(LOG_DEBUG, "%s: ignoring unrequested IAID %s",
 			    ifp->name,
 			    hwaddr_ntoa(iaid, sizeof(iaid), buf, sizeof(buf)));
 			continue;
 		}
-		if (ifo->ia[j].ia_type != code) {
+		if ( j < ifo->ia_len && ifo->ia[j].ia_type != code) {
 			syslog(LOG_ERR, "%s: IAID %s: option type mismatch",
 			    ifp->name,
 			    hwaddr_ntoa(iaid, sizeof(iaid), buf, sizeof(buf)));
@@ -3530,17 +3532,15 @@
 	int r;
 
 	ifp->if_data[IF_DATA_DHCP6] = state = calloc(1, sizeof(*state));
-	if (state == NULL)
-		goto eexit;
+	if (state == NULL) {
+		syslog(LOG_ERR, "%s: %m", __func__);
+		return -1;
+	}
 	TAILQ_INIT(&state->addrs);
 	dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile),
 	    AF_INET6, ifp,
 	    ifp->options->options & DHCPCD_PFXDLGONLY ? ".pd" : "");
 	r = dhcp6_readlease(ifp);
-	if (r == -1 && errno == ENOENT) {
-		strlcpy(state->leasefile, ifp->name, sizeof(state->leasefile));
-		r = dhcp6_readlease(ifp);
-	}
 	if (r == -1) {
 		if (errno == ENOENT)
 			syslog(LOG_ERR, "%s: no lease to dump", ifp->name);
@@ -3550,8 +3550,4 @@
 	}
 	state->reason = "DUMP6";
 	return script_runreason(ifp, state->reason);
-
-eexit:
-	syslog(LOG_ERR, "%s: %m", __func__);
-	return -1;
 }
--- a/dhcpcd.c	Wed Feb 18 20:29:42 2015 +0000
+++ b/dhcpcd.c	Fri Feb 20 08:28:04 2015 +0000
@@ -468,7 +468,9 @@
 	}
 
 #ifdef INET6
-	if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6) {
+	if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6 &&
+	    ifp->name[0] != '\0')
+	{
 		ifo->ia = malloc(sizeof(*ifo->ia));
 		if (ifo->ia == NULL)
 			syslog(LOG_ERR, "%s: %m", __func__);
@@ -1284,7 +1286,6 @@
 main(int argc, char **argv)
 {
 	struct dhcpcd_ctx ctx;
-	char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1];
 	struct if_options *ifo;
 	struct interface *ifp;
 	uint16_t family = 0;
@@ -1491,10 +1492,11 @@
 			default:
 				per = "";
 			}
-			snprintf(pidfile, sizeof(pidfile),
+			snprintf(ctx.pidfile, sizeof(ctx.pidfile),
 			    PIDFILE, "-", argv[optind], per);
 		} else {
-			snprintf(pidfile, sizeof(pidfile), PIDFILE, "", "", "");
+			snprintf(ctx.pidfile, sizeof(ctx.pidfile),
+			    PIDFILE, "", "", "");
 			ctx.options |= DHCPCD_MASTER;
 		}
 	}
@@ -1530,9 +1532,15 @@
 				syslog(LOG_ERR, "%s: %m", __func__);
 				goto exit_failure;
 			}
-			strlcpy(ifp->name, argv[optind], sizeof(ifp->name));
+			strlcpy(ctx.pidfile, argv[optind], sizeof(ctx.pidfile));
 			ifp->ctx = &ctx;
 			TAILQ_INSERT_HEAD(ctx.ifaces, ifp, next);
+			if (family == 0) {
+				if (ctx.pidfile[strlen(ctx.pidfile) - 1] == '6')
+					family = AF_INET6;
+				else
+					family = AF_INET;
+			}
 		}
 		configure_interface(ifp, ctx.argc, ctx.argv);
 		if (ctx.options & DHCPCD_PFXDLGONLY)
@@ -1587,7 +1595,7 @@
 
 #ifdef USE_SIGNALS
 	if (sig != 0) {
-		pid = read_pid(pidfile);
+		pid = read_pid(ctx.pidfile);
 		if (pid != 0)
 			syslog(LOG_INFO, "sending signal %s to pid %d",
 			    siga, pid);
@@ -1598,7 +1606,7 @@
 				syslog(LOG_ERR, "kill: %m");
 				goto exit_failure;
 			}
-			unlink(pidfile);
+			unlink(ctx.pidfile);
 			if (sig != SIGHUP)
 				goto exit_failure;
 		} else {
@@ -1610,7 +1618,7 @@
 			ts.tv_nsec = 100000000; /* 10th of a second */
 			for(i = 0; i < 100; i++) {
 				nanosleep(&ts, NULL);
-				if (read_pid(pidfile) == 0)
+				if (read_pid(ctx.pidfile) == 0)
 					goto exit_success;
 			}
 			syslog(LOG_ERR, "pid %d failed to exit", pid);
@@ -1619,12 +1627,12 @@
 	}
 
 	if (!(ctx.options & DHCPCD_TEST)) {
-		if ((pid = read_pid(pidfile)) > 0 &&
+		if ((pid = read_pid(ctx.pidfile)) > 0 &&
 		    kill(pid, 0) == 0)
 		{
 			syslog(LOG_ERR, ""PACKAGE
 			    " already running on pid %d (%s)",
-			    pid, pidfile);
+			    pid, ctx.pidfile);
 			goto exit_failure;
 		}
 
@@ -1638,15 +1646,15 @@
 #ifdef O_CLOEXEC
 		opt |= O_CLOEXEC;
 #endif
-		ctx.pid_fd = open(pidfile, opt, 0664);
+		ctx.pid_fd = open(ctx.pidfile, opt, 0664);
 		if (ctx.pid_fd == -1)
-			syslog(LOG_ERR, "open `%s': %m", pidfile);
+			syslog(LOG_ERR, "open `%s': %m", ctx.pidfile);
 		else {
 #ifdef LOCK_EX
 			/* Lock the file so that only one instance of dhcpcd
 			 * runs on an interface */
 			if (flock(ctx.pid_fd, LOCK_EX | LOCK_NB) == -1) {
-				syslog(LOG_ERR, "flock `%s': %m", pidfile);
+				syslog(LOG_ERR, "flock `%s': %m", ctx.pidfile);
 				close(ctx.pid_fd);
 				ctx.pid_fd = -1;
 				goto exit_failure;
@@ -1814,7 +1822,7 @@
 		syslog(LOG_ERR, "control_stop: %m:");
 	if (ctx.pid_fd != -1) {
 		close(ctx.pid_fd);
-		unlink(pidfile);
+		unlink(ctx.pidfile);
 	}
 	eloop_free(ctx.eloop);
 
--- a/dhcpcd.h	Wed Feb 18 20:29:42 2015 +0000
+++ b/dhcpcd.h	Fri Feb 20 08:28:04 2015 +0000
@@ -84,9 +84,8 @@
 TAILQ_HEAD(if_head, interface);
 
 struct dhcpcd_ctx {
-#ifdef USE_SIGNALS
-	sigset_t sigset;
-#endif
+	int pid_fd;
+	char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1];
 	const char *cffile;
 	unsigned long long options;
 	int argc;
@@ -101,10 +100,12 @@
 	char **ifcv;	/* configured interfaces */
 	unsigned char *duid;
 	size_t duid_len;
-	int pid_fd;
 	int link_fd;
 	struct if_head *ifaces;
 
+#ifdef USE_SIGNALS
+	sigset_t sigset;
+#endif
 	struct eloop_ctx *eloop;
 
 	int control_fd;