changeset 5169:f1a2ce25a64b draft

dhcpcd: Fix separation of per interface and per family While here improve privsep proess titles for this.
author Roy Marples <roy@marples.name>
date Thu, 23 Apr 2020 23:45:38 +0100
parents 1f5dc6102f9b
children 8c6a66d852f4
files src/control.c src/control.h src/defs.h src/dhcpcd.c src/privsep-inet.c src/privsep-root.c
diffstat 6 files changed, 90 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/src/control.c	Thu Apr 23 14:15:41 2020 +0000
+++ b/src/control.c	Thu Apr 23 23:45:38 2020 +0100
@@ -193,7 +193,8 @@
 }
 
 static int
-make_sock(struct sockaddr_un *sa, const char *ifname, bool unpriv)
+make_sock(struct sockaddr_un *sa, const char *ifname, uint16_t family,
+    bool unpriv)
 {
 	int fd;
 
@@ -206,8 +207,21 @@
 	if (unpriv)
 		strlcpy(sa->sun_path, UNPRIVSOCKET, sizeof(sa->sun_path));
 	else {
+		const char *per;
+
+		switch(family) {
+		case AF_INET:
+			per = "-4";
+			break;
+		case AF_INET6:
+			per = "-6";
+			break;
+		default:
+			per = "";
+			break;
+		}
 		snprintf(sa->sun_path, sizeof(sa->sun_path), CONTROLSOCKET,
-		    ifname ? ifname : "", ifname ? "." : "");
+		    ifname ? ifname : "", ifname ? per : "", ifname ? "." : "");
 	}
 	return fd;
 }
@@ -216,14 +230,17 @@
 #define S_UNPRIV (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
 
 static int
-control_start1(struct dhcpcd_ctx *ctx, const char *ifname, mode_t fmode)
+control_start1(struct dhcpcd_ctx *ctx, const char *ifname, uint16_t family,
+    mode_t fmode)
 {
 	struct sockaddr_un sa;
 	int fd;
 	socklen_t len;
 
-	if ((fd = make_sock(&sa, ifname, (fmode & S_UNPRIV) == S_UNPRIV)) == -1)
+	fd = make_sock(&sa, ifname, family, (fmode & S_UNPRIV) == S_UNPRIV);
+	if (fd == -1)
 		return -1;
+
 	len = (socklen_t)SUN_LEN(&sa);
 	unlink(sa.sun_path);
 	if (bind(fd, (struct sockaddr *)&sa, len) == -1 ||
@@ -244,17 +261,19 @@
 }
 
 int
-control_start(struct dhcpcd_ctx *ctx, const char *ifname)
+control_start(struct dhcpcd_ctx *ctx, const char *ifname, uint16_t family)
 {
 	int fd;
 
-	if ((fd = control_start1(ctx, ifname, S_PRIV)) == -1)
+	if ((fd = control_start1(ctx, ifname, family, S_PRIV)) == -1)
 		return -1;
 
 	ctx->control_fd = fd;
 	eloop_event_add(ctx->eloop, fd, control_handle, ctx);
 
-	if (ifname == NULL && (fd = control_start1(ctx, NULL, S_UNPRIV)) != -1){
+	if (ifname == NULL &&
+	    (fd = control_start1(ctx, NULL, AF_UNSPEC, S_UNPRIV)) != -1)
+	{
 		/* We must be in master mode, so create an unprivileged socket
 		 * to allow normal users to learn the status of dhcpcd. */
 		ctx->control_unpriv_fd = fd;
@@ -318,12 +337,12 @@
 }
 
 int
-control_open(const char *ifname, bool unpriv)
+control_open(const char *ifname, uint16_t family, bool unpriv)
 {
 	struct sockaddr_un sa;
 	int fd;
 
-	if ((fd = make_sock(&sa, ifname, unpriv)) != -1) {
+	if ((fd = make_sock(&sa, ifname, family, unpriv)) != -1) {
 		socklen_t len;
 
 		len = (socklen_t)SUN_LEN(&sa);
--- a/src/control.h	Thu Apr 23 14:15:41 2020 +0000
+++ b/src/control.h	Thu Apr 23 23:45:38 2020 +0100
@@ -66,9 +66,9 @@
 #define FD_LISTEN	(1<<0)
 #define FD_UNPRIV	(1<<1)
 
-int control_start(struct dhcpcd_ctx *, const char *);
+int control_start(struct dhcpcd_ctx *, const char *, uint16_t);
 int control_stop(struct dhcpcd_ctx *);
-int control_open(const char *, bool);
+int control_open(const char *, uint16_t, bool);
 ssize_t control_send(struct dhcpcd_ctx *, int, char * const *);
 int control_queue(struct fd_list *, void *, size_t, bool);
 
--- a/src/defs.h	Thu Apr 23 14:15:41 2020 +0000
+++ b/src/defs.h	Thu Apr 23 23:45:38 2020 +0100
@@ -60,7 +60,7 @@
 # define PIDFILE		RUNDIR "/%s%s%spid"
 #endif
 #ifndef CONTROLSOCKET
-# define CONTROLSOCKET		RUNDIR "/%s%ssock"
+# define CONTROLSOCKET		RUNDIR "/%s%s%ssock"
 #endif
 #ifndef UNPRIVSOCKET
 # define UNPRIVSOCKET		RUNDIR "/unpriv.sock"
--- a/src/dhcpcd.c	Thu Apr 23 14:15:41 2020 +0000
+++ b/src/dhcpcd.c	Thu Apr 23 23:45:38 2020 +0100
@@ -1751,7 +1751,7 @@
 	struct ifaddrs *ifaddrs = NULL;
 	struct if_options *ifo;
 	struct interface *ifp;
-	uint16_t family = 0;
+	uint16_t family = AF_UNSPEC;
 	int opt, oi = 0, i;
 	unsigned int logopts, t;
 	ssize_t len;
@@ -2034,52 +2034,6 @@
 		goto exit_failure;
 	}
 
-	/* Test against siga instead of sig to avoid gcc
-	 * warning about a bogus potential signed overflow.
-	 * The end result will be the same. */
-	if ((siga == NULL || i == 4 || ctx.ifc != 0) &&
-	    !(ctx.options & DHCPCD_TEST))
-	{
-#endif
-		ctx.options |= DHCPCD_FORKED; /* avoid socket unlink */
-		if (!(ctx.options & DHCPCD_MASTER))
-			ctx.control_fd = control_open(argv[optind],
-			    ctx.options & DHCPCD_DUMPLEASE);
-		if (ctx.control_fd == -1)
-			ctx.control_fd = control_open(NULL,
-			    ctx.options & DHCPCD_DUMPLEASE);
-		if (ctx.control_fd != -1) {
-			if (!(ctx.options & DHCPCD_DUMPLEASE))
-				loginfox("sending commands to dhcpcd process");
-			len = control_send(&ctx, argc, argv);
-			if (len > 0)
-				logdebugx("send OK");
-			else {
-				logerr("%s: control_send", __func__);
-				goto exit_failure;
-			}
-			if (ctx.options & DHCPCD_DUMPLEASE) {
-				if (dhcpcd_readdump(&ctx) == -1) {
-					logerr("%s: dhcpcd_readdump", __func__);
-					goto exit_failure;
-				}
-			}
-			goto exit_success;
-		} else {
-			if (errno != ENOENT)
-				logerr("%s: control_open", __func__);
-			if (ctx.options & DHCPCD_DUMPLEASE) {
-				if (errno == ENOENT)
-					logerrx("dhcpcd is not running");
-				goto exit_failure;
-			}
-		}
-		ctx.options &= ~DHCPCD_FORKED;
-#ifdef USE_SIGNALS
-	}
-#endif
-
-#ifdef USE_SIGNALS
 	if (sig != 0) {
 		pid = pidfile_read(ctx.pidfile);
 		if (pid != 0 && pid != -1)
@@ -2112,6 +2066,49 @@
 			goto exit_failure;
 		}
 	}
+#endif
+
+	/* Test against siga instead of sig to avoid gcc
+	 * warning about a bogus potential signed overflow.
+	 * The end result will be the same. */
+	if ((siga == NULL || i == 4 || ctx.ifc != 0) &&
+	    !(ctx.options & DHCPCD_TEST))
+	{
+		ctx.options |= DHCPCD_FORKED; /* avoid socket unlink */
+		if (!(ctx.options & DHCPCD_MASTER))
+			ctx.control_fd = control_open(argv[optind], family,
+			    ctx.options & DHCPCD_DUMPLEASE);
+		if (ctx.control_fd == -1)
+			ctx.control_fd = control_open(NULL, AF_UNSPEC,
+			    ctx.options & DHCPCD_DUMPLEASE);
+		if (ctx.control_fd != -1) {
+			if (!(ctx.options & DHCPCD_DUMPLEASE))
+				loginfox("sending commands to dhcpcd process");
+			len = control_send(&ctx, argc, argv);
+			if (len > 0)
+				logdebugx("send OK");
+			else {
+				logerr("%s: control_send", __func__);
+				goto exit_failure;
+			}
+			if (ctx.options & DHCPCD_DUMPLEASE) {
+				if (dhcpcd_readdump(&ctx) == -1) {
+					logerr("%s: dhcpcd_readdump", __func__);
+					goto exit_failure;
+				}
+			}
+			goto exit_success;
+		} else {
+			if (errno != ENOENT)
+				logerr("%s: control_open", __func__);
+			if (ctx.options & DHCPCD_DUMPLEASE) {
+				if (errno == ENOENT)
+					logerrx("dhcpcd is not running");
+				goto exit_failure;
+			}
+		}
+		ctx.options &= ~DHCPCD_FORKED;
+	}
 
 	if (!(ctx.options & DHCPCD_TEST)) {
 		/* Ensure we have the needed directories */
@@ -2128,7 +2125,6 @@
 			goto exit_failure;
 		}
 	}
-#endif
 
 	loginfox(PACKAGE "-" VERSION " starting");
 	freopen(_PATH_DEVNULL, "r", stdin);
@@ -2211,7 +2207,7 @@
 
 	if (!(ctx.options & DHCPCD_TEST) &&
 	    control_start(&ctx,
-	    ctx.options & DHCPCD_MASTER ? NULL : argv[optind]) == -1)
+	    ctx.options & DHCPCD_MASTER ? NULL : argv[optind], family) == -1)
 	{
 		logerr("%s: control_start", __func__);
 		goto exit_failure;
--- a/src/privsep-inet.c	Thu Apr 23 14:15:41 2020 +0000
+++ b/src/privsep-inet.c	Thu Apr 23 23:45:38 2020 +0100
@@ -93,7 +93,13 @@
 	struct dhcpcd_ctx *ctx = arg;
 	int ret = 0;
 
-	setproctitle("[network proxy]");
+	if (ctx->options & DHCPCD_MASTER)
+		setproctitle("[network proxy]");
+	else
+		setproctitle("[network proxy] %s%s%s",
+		    ctx->ifv[0],
+		    ctx->options & DHCPCD_IPV4 ? " [ip4]" : "",
+		    ctx->options & DHCPCD_IPV6 ? " [ip6]" : "");
 
 	/* This end is the main engine, so it's useless for us. */
 	close(ctx->ps_data_fd);
--- a/src/privsep-root.c	Thu Apr 23 14:15:41 2020 +0000
+++ b/src/privsep-root.c	Thu Apr 23 23:45:38 2020 +0100
@@ -406,7 +406,13 @@
 {
 	struct dhcpcd_ctx *ctx = arg;
 
-	setproctitle("[privileged actioneer]");
+	if (ctx->options & DHCPCD_MASTER)
+		setproctitle("[privileged actioneer]");
+	else
+		setproctitle("[privileged actioneer] %s%s%s",
+		    ctx->ifv[0],
+		    ctx->options & DHCPCD_IPV4 ? " [ip4]" : "",
+		    ctx->options & DHCPCD_IPV6 ? " [ip6]" : "");
 	ctx->ps_root_pid = getpid();
 	return 0;
 }