Skip to contentSkip to conversation

chunkd: use "auto" keyword

PATCH Applied Pete Zaitcev 2 participants 2 messages Sep 29, 2009 – Sep 30, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 chunkd: use "auto" keyword

The current implementation of automatic listening ports was configured implicitly by adding <PortFile> clause. This turned out to be a silly idea. A special keyword for <Port> is more convenient. Also, we do not need to use /dev/null in <PortFile> anymore.

The configuration is not strictly speaking compatible, but since nobody should be using auto ports in production anyway, impact on installations should be nil. But this must be built after the support for "-p auto" is added to CLD.

doc/setup.txt +9 -0
diff -urp -X dontdiff chunkd-m/doc/setup.txt chunkd-tip/doc/setup.txt
@@ -39,6 +39,15 @@ _cld._udp.phx2.ex.com has SRV record 10 
    not a great idea to specify interfaces, since they often use IPv6 or
    acquire IP addresses from DHCP. So, just specify a port.
 
+   An option exists to let the server to listen on a random port with
+   the "auto" keyword:
+
+	<Listen>auto</Listen>
+
+   This works with CLD-aware clients that find the connection information
+   from records like /chunk-picbak/*. It's not commonly used in real clouds
+   but may be beneficial in chroots and other odd environments.
+
 *) choose pathname (dir + filename) where daemon should store its
    PID file. Default is /var/run/chunkd.pid, but it limits you to
    one Chunk daemon per node, which is usual in clouds.
server/config.c +4 -8
diff -urp -X dontdiff chunkd-m/server/config.c chunkd-tip/server/config.c
@@ -131,13 +131,8 @@ static void cfg_elm_end_listen(struct co
 		return;
 	}
 
-	if (cc->tmp_listen.port && cc->tmp_listen.port_file) {
-		applog(LOG_ERR, "cfgfile: Listen with both Port and PortFile");
-		goto err;
-	}
-
-	if (!cc->tmp_listen.port && !cc->tmp_listen.port_file) {
-		applog(LOG_ERR, "cfgfile: Listen with no Port or PortFile");
+	if (!cc->tmp_listen.port) {
+		applog(LOG_ERR, "cfgfile: Listen with no Port");
 		goto err;
 	}
 
@@ -318,7 +313,8 @@ static void cfg_elm_end (GMarkupParseCon
 
 		if (cc->in_listen) {
 			n = strtol(cc->text, NULL, 10);
-			if (n > 0 && n < 65536) {
+			if ((n > 0 && n < 65536) ||
+			   !strcmp(cc->text, "auto")) {
 				free(cc->tmp_listen.port);
 				cc->tmp_listen.port = cc->text;
 			} else {
server/server.c +25 -15
diff -urp -X dontdiff chunkd-m/server/server.c chunkd-tip/server/server.c
@@ -1018,6 +1018,23 @@ err_out:
 	cli_free(cli);
 }
 
+static int net_write_port(const char *port_file, const char *port_str)
+{
+	FILE *portf;
+	int rc;
+
+	portf = fopen(port_file, "w");
+	if (portf == NULL) {
+		rc = errno;
+		applog(LOG_INFO, "Cannot create port file %s: %s",
+		       port_file, strerror(rc));
+		return -rc;
+	}
+	fprintf(portf, "%s\n", port_str);
+	fclose(portf);
+	return 0;
+}
+
 static int net_open_socket(const struct listen_cfg *cfg,
 			   int addr_fam, int sock_type, int sock_prot,
 			   int addr_len, void *addr_ptr)
@@ -1089,10 +1106,8 @@ static int net_open_socket(const struct 
  */
 static int net_open_any(struct listen_cfg *cfg)
 {
-	char *portfile = cfg->port_file;
 	struct sockaddr_in addr4;
 	struct sockaddr_in6 addr6;
-	FILE *portf;
 	int fd4, fd6;
 	socklen_t addr_len;
 	unsigned short port;
@@ -1139,24 +1154,17 @@ static int net_open_any(struct listen_cf
 		port = ntohs(addr4.sin_port);
 	}
 
-	applog(LOG_INFO, "Listening on port %u file %s", port, portfile);
+	applog(LOG_INFO, "Listening on auto port %u", port);
 
+	free(cfg->port);
 	rc = asprintf(&cfg->port, "%u", port);
 	if (rc < 0) {
 		applog(LOG_ERR, "OOM");
 		return -ENOMEM;
 	}
 
-	portf = fopen(portfile, "w");
-	if (portf == NULL) {
-		rc = errno;
-		applog(LOG_INFO, "Cannot create port file %s: %s",
-		       portfile, strerror(rc));
-		return -rc;
-	}
-	fprintf(portf, "%u\n", port);
-	fclose(portf);
-
+	if (cfg->port_file)
+		return net_write_port(cfg->port_file, cfg->port);
 	return 0;
 }
 
@@ -1213,12 +1221,14 @@ static int net_open_known(const struct l
 			    listen_serv, sizeof(listen_serv),
 			    NI_NUMERICHOST | NI_NUMERICSERV);
 
-		applog(LOG_INFO, "Listening on %s port %s",
+		applog(LOG_INFO, "Listening on host %s port %s",
 		       listen_host, listen_serv);
 	}
 
 	freeaddrinfo(res0);
 
+	if (cfg->port_file)
+		net_write_port(cfg->port_file, cfg->port);
 	return 0;
 
 err_out:
@@ -1227,7 +1237,7 @@ err_out:
 
 static int net_open(struct listen_cfg *cfg)
 {
-	if (cfg->port_file)
+	if (!strcmp(cfg->port, "auto"))
 		return net_open_any(cfg);
 	else
 		return net_open_known(cfg);
test/server-test.cfg +2 -0
diff -urp -X dontdiff chunkd-m/test/server-test.cfg chunkd-tip/test/server-test.cfg
@@ -5,10 +5,12 @@
 </SSL>
 
 <Listen>
+	<Port>auto</Port>
 	<PortFile>chunkd.port</PortFile>
 </Listen>
 
 <Listen>
+	<Port>auto</Port>
 	<PortFile>chunkd-ssl.port</PortFile>
 	<Encrypt>true</Encrypt>
 </Listen>
test/start-daemon.real +1 -1
diff -urp -X dontdiff chunkd-m/test/start-daemon.real chunkd-tip/test/start-daemon.real
@@ -11,7 +11,7 @@ then
 	exit 1
 fi
 
-/usr/sbin/cld -d data/cld -P cld.pid --port-file=cld.port  -E
+/usr/sbin/cld -d data/cld -P cld.pid -p auto --port-file=cld.port -E
 
 ../server/chunkd -C $top_srcdir/test/server-test.cfg -E $*
 
test/start-daemon.real.in +1 -1
diff -urp -X dontdiff chunkd-m/test/start-daemon.real.in chunkd-tip/test/start-daemon.real.in
@@ -11,7 +11,7 @@ then
 	exit 1
 fi
 
-@CLDC_CMD@ -d data/cld -P cld.pid --port-file=cld.port  -E
+@CLDC_CMD@ -d data/cld -P cld.pid -p auto --port-file=cld.port -E
 
 ../server/chunkd -C $top_srcdir/test/server-test.cfg -E $*
 
Applied
#1 lore raw
Quoted 11 lines · Pete Zaitcev

The current implementation of automatic listening ports was configured implicitly by adding<PortFile> clause. This turned out to be a silly idea. A special keyword for<Port> is more convenient. Also, we do not need to use /dev/null in<PortFile> anymore.

The configuration is not strictly speaking compatible, but since nobody should be using auto ports in production anyway, impact on installations should be nil. But this must be built after the support for "-p auto" is added to CLD.

applied, sans the extraneous patch of start-daemon.real