[4.1] 1ff5897 Make -a argument checking a two step process:

Poul-Henning Kamp phk at FreeBSD.org
Fri Sep 4 15:54:51 CEST 2015


commit 1ff5897b6d18954fc209b68270c637a510f30e79
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sun Jul 26 09:39:13 2015 +0000

    Make -a argument checking a two step process:
    
    We always do the DNS resolution when we hit -a arguments, but the
    test that we can bind to the address is postponed until after the -C
    argument processing.
    
    Fixes:	#1767

diff --git a/bin/varnishd/common/heritage.h b/bin/varnishd/common/heritage.h
index 05f340a..27c3a33 100644
--- a/bin/varnishd/common/heritage.h
+++ b/bin/varnishd/common/heritage.h
@@ -37,8 +37,8 @@ struct listen_sock {
 #define LISTEN_SOCK_MAGIC		0x999e4b57
 	VTAILQ_ENTRY(listen_sock)	list;
 	int				sock;
-	const char			*name;
-	const struct suckaddr		*addr;
+	char				*name;
+	struct suckaddr			*addr;
 	enum sess_step			first_step;
 	const char			*proto_name;
 };
diff --git a/bin/varnishd/mgt/mgt.h b/bin/varnishd/mgt/mgt.h
index 2b680c6..18356dc 100644
--- a/bin/varnishd/mgt/mgt.h
+++ b/bin/varnishd/mgt/mgt.h
@@ -42,6 +42,7 @@ extern int		exit_status;
 /* mgt_acceptor.c */
 
 void MAC_Arg(const char *);
+void MAC_Validate(void);
 void MAC_reopen_sockets(struct cli *);
 int MAC_sockets_ready(struct cli *);
 
diff --git a/bin/varnishd/mgt/mgt_acceptor.c b/bin/varnishd/mgt/mgt_acceptor.c
index b85c391..a956b27 100644
--- a/bin/varnishd/mgt/mgt_acceptor.c
+++ b/bin/varnishd/mgt/mgt_acceptor.c
@@ -112,9 +112,8 @@ MAC_sockets_ready(struct cli *cli)
 struct mac_help {
 	unsigned		magic;
 #define MAC_HELP_MAGIC		0x1e00a9d9
-	const char		*name;
 	int			good;
-	const char		**err;
+	const char		*name;
 	const char		*proto_name;
 	enum sess_step		first_step;
 };
@@ -124,47 +123,55 @@ mac_callback(void *priv, const struct suckaddr *sa)
 {
 	struct mac_help *mh;
 	struct listen_sock *ls;
-	int fail;
-	char abuf[VTCP_ADDRBUFSIZE], pbuf[VTCP_PORTBUFSIZE];
-	char nbuf[VTCP_ADDRBUFSIZE+VTCP_PORTBUFSIZE+2];
 
 	CAST_OBJ_NOTNULL(mh, priv, MAC_HELP_MAGIC);
 
 	ALLOC_OBJ(ls, LISTEN_SOCK_MAGIC);
 	AN(ls);
 	ls->sock = -1;
-	ls->addr = sa;
-	ls->proto_name = mh->proto_name;
-	ls->first_step = mh->first_step;
-	VJ_master(JAIL_MASTER_PRIVPORT);
-	fail = mac_opensocket(ls, NULL);
-	VJ_master(JAIL_MASTER_LOW);
-	if (ls->sock < 0) {
-		*(mh->err) = strerror(fail);
-		FREE_OBJ(ls);
-		return (0);
-	}
-	if (VSA_Port(sa) == 0) {
-		/*
-		 * If the port number is zero, we adopt whatever port number
-		 * this VTCP_bind() found us, as if specified by argument.
-		 */
-		ls->addr = VTCP_my_suckaddr(ls->sock);
-		VTCP_myname(ls->sock, abuf, sizeof abuf, pbuf, sizeof pbuf);
-		bprintf(nbuf, "%s:%s", abuf, pbuf);
-		ls->name = strdup(nbuf);
-	} else {
-		ls->addr = VSA_Clone(sa);
-		ls->name = strdup(mh->name);
-	}
+	ls->addr = VSA_Clone(sa);
 	AN(ls->addr);
+	ls->name = strdup(mh->name);
 	AN(ls->name);
+	ls->proto_name = mh->proto_name;
+	ls->first_step = mh->first_step;
 	VTAILQ_INSERT_TAIL(&heritage.socks, ls, list);
 	mh->good++;
 	return (0);
 }
 
 void
+MAC_Validate(void)
+{
+	struct listen_sock *ls;
+	int fail;
+	char abuf[VTCP_ADDRBUFSIZE], pbuf[VTCP_PORTBUFSIZE];
+	char nbuf[VTCP_ADDRBUFSIZE+VTCP_PORTBUFSIZE+2];
+
+	VTAILQ_FOREACH(ls, &heritage.socks, list) {
+		VJ_master(JAIL_MASTER_PRIVPORT);
+		fail = mac_opensocket(ls, NULL);
+		VJ_master(JAIL_MASTER_LOW);
+		if (ls->sock < 0)
+			ARGV_ERR("Cannot open socket: %s: %s\n",
+			    ls->name, strerror(fail));
+		if (VSA_Port(ls->addr) == 0) {
+			/*
+			 * If the port number is zero, we adopt whatever
+			 * port number this VTCP_bind() found us, as if
+			 * specified by argument.
+			 */
+			free(ls->addr);
+			ls->addr = VTCP_my_suckaddr(ls->sock);
+			VTCP_myname(ls->sock, abuf, sizeof abuf,
+			    pbuf, sizeof pbuf);
+			bprintf(nbuf, "%s:%s", abuf, pbuf);
+			REPLACE(ls->name, nbuf);
+		}
+	}
+}
+
+void
 MAC_Arg(const char *arg)
 {
 	char **av;
@@ -192,11 +199,9 @@ MAC_Arg(const char *arg)
 		ARGV_ERR("Unknown protocol '%s'\n", av[2]);
 	}
 
-	mh->err = &err;
 	error = VSS_resolver(av[1], "80", mac_callback, mh, &err);
-	if (mh->good == 0 || err != NULL)
-		ARGV_ERR("Could not bind to address %s: %s\n", av[1], err);
+	if (mh->good == 0 || error)
+		ARGV_ERR("socket %s didn't resolve \n", av[1]);
 	VAV_Free(av);
-	AZ(error);
 	FREE_OBJ(mh);
 }
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index 56c51b7..0ae5338 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -497,7 +497,7 @@ main(int argc, char * const *argv)
 	/* Create a cli for convenience in otherwise CLI functions */
 	INIT_OBJ(cli, CLI_MAGIC);
 	cli[0].sb = VSB_new_auto();
-	XXXAN(cli[0].sb);
+	AN(cli[0].sb);
 	cli[0].result = CLIS_OK;
 	clilim = 32768;
 	cli[0].limit = &clilim;
@@ -510,11 +510,6 @@ main(int argc, char * const *argv)
 	init_params(cli);
 	cli_check(cli);
 
-	if (argc == 1) {
-		jailed++;
-		VJ_Init(NULL);
-	}
-
 	while ((o = getopt(argc, argv,
 	    "a:b:Cdf:Fh:i:j:l:M:n:P:p:r:S:s:T:t:VW:x:")) != -1) {
 		/*
@@ -632,6 +627,9 @@ main(int argc, char * const *argv)
 		}
 	}
 
+	if (!jailed)
+		VJ_Init(NULL);
+
 	argc -= optind;
 	argv += optind;
 	if (argc != 0)
@@ -705,7 +703,9 @@ main(int argc, char * const *argv)
 		ARGV_ERR("-C only good with -b or -f\n");
 
 	if (VTAILQ_EMPTY(&heritage.socks))
-		MAC_Arg("*:80");
+		MAC_Arg(":80");
+
+	MAC_Validate();
 
 	assert(! VTAILQ_EMPTY(&heritage.socks));
 
diff --git a/bin/varnishtest/tests/c00003.vtc b/bin/varnishtest/tests/c00003.vtc
index 2acb66b..f2efcce 100644
--- a/bin/varnishtest/tests/c00003.vtc
+++ b/bin/varnishtest/tests/c00003.vtc
@@ -4,7 +4,11 @@ varnishtest "Check that we fail to start if any listen address does not work"
 # and are on Linux, ensure /proc/net/ipv4/ip_nonlocal_bind is set to 0.
 
 # All bad listen addresses
-err_shell "Could not bind to address 192.0.2.255:0" {${varnishd} -F -a "${bad_ip}:0" -b /// -n ${tmpdir} 2>&1 }
+err_shell "could not be resolved to an IP address" {
+	${varnishd} -F -a "${bad_ip}:0" -b /// -n ${tmpdir} 2>&1
+}
 
 # old style address list
-err_shell "Unknown protocol" {${varnishd} -F -a "127.0.0.1:0,${bad_ip}:0" -b /// -n ${tmpdir} 2>&1 }
+err_shell "Unknown protocol" {
+	${varnishd} -F -a "127.0.0.1:0,${bad_ip}:0" -b /// -n ${tmpdir} 2>&1
+}



More information about the varnish-commit mailing list