[Patch 3/3] tabled: use "auto" keyword

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

This should be built after both CLD and Chunk implement "auto" keyword.

Signed-off-by: Pete Zaitcev <zaitcev@xxxxxxxxxx>

diff --git a/server/config.c b/server/config.c
index 505524c..c7a994f 100644
--- a/server/config.c
+++ b/server/config.c
@@ -75,13 +75,8 @@ static void cfg_elm_end_listen(struct config_context *cc)
 		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;
 	}
 
@@ -270,7 +265,8 @@ static void cfg_elm_end (GMarkupParseContext *context,
 
 		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 {
diff --git a/server/server.c b/server/server.c
index f1c49be..d9320ec 100644
--- a/server/server.c
+++ b/server/server.c
@@ -1480,15 +1480,32 @@ static int net_open_socket(int addr_fam, int sock_type, int sock_prot,
 	return fd;
 }
 
+static int net_write_port(const char *port_file,
+			  const char *host, const char *port)
+{
+	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:%s\n", tabled_srv.ourhost, port);
+	fclose(portf);
+	return 0;
+}
+
 /*
  * This, annoyingly, has to have a side effect: it fills out tabled_srv.port
  * so that we can later export it into CLD.
  */
-static int net_open_any(char *portfile)
+static int net_open_any(void)
 {
 	struct sockaddr_in addr4;
 	struct sockaddr_in6 addr6;
-	FILE *portf;
 	int fd4, fd6;
 	socklen_t addr_len;
 	unsigned short port;
@@ -1533,7 +1550,7 @@ static int net_open_any(char *portfile)
 		port = ntohs(addr4.sin_port);
 	}
 
-	applog(LOG_INFO, "Listening on port %u file %s", port, portfile);
+	applog(LOG_INFO, "Listening on port %u", port);
 
 	rc = asprintf(&tabled_srv.port, "%u", port);
 	if (rc < 0) {
@@ -1541,17 +1558,6 @@ static int net_open_any(char *portfile)
 		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, "%s:%u\n", tabled_srv.ourhost, port);
-	fclose(portf);
-
-	tabled_srv.state_net = ST_NET_OPEN;
 	return 0;
 }
 
@@ -1623,10 +1629,24 @@ err_addr:
 
 static int net_open(void)
 {
-	if (tabled_srv.port_file)
-		return net_open_any(tabled_srv.port_file);
+	int rc;
+
+	if (!strcmp(tabled_srv.port, "auto"))
+		rc = net_open_any();
 	else
-		return net_open_known(tabled_srv.port);
+		rc = net_open_known(tabled_srv.port);
+	if (rc)
+		return rc;
+
+	if (tabled_srv.port_file) {
+		rc = net_write_port(tabled_srv.port_file,
+				    tabled_srv.ourhost, tabled_srv.port);
+		if (rc)
+			return rc;
+	}
+
+	tabled_srv.state_net = ST_NET_OPEN;
+	return 0;
 }
 
 static void net_listen(void)
diff --git a/test/chunkd-test.conf b/test/chunkd-test.conf
index 3785eb5..00cf024 100644
--- a/test/chunkd-test.conf
+++ b/test/chunkd-test.conf
@@ -1,6 +1,6 @@
 
 <Listen>
-  <PortFile>/dev/null</PortFile>
+  <Port>auto</Port>
 </Listen>
 <PID>chunkd.pid</PID>
 <Path>data/chunk</Path>
diff --git a/test/start-daemon b/test/start-daemon
index 589bfe5..2761d55 100755
--- a/test/start-daemon
+++ b/test/start-daemon
@@ -17,7 +17,7 @@ then
 fi
 
 # May be different on Solaris... like /usr/libexec or such.
-cld -d data/cld -P cld.pid --port-file=cld.port -E
+cld -d data/cld -P cld.pid -p auto --port-file=cld.port -E
 chunkd -C $top_srcdir/test/chunkd-test.conf -E
 ../server/tabled -C $top_srcdir/test/tabled-test.conf -E -D
 
diff --git a/test/tabled-test.conf b/test/tabled-test.conf
index 2de599c..068ed00 100644
--- a/test/tabled-test.conf
+++ b/test/tabled-test.conf
@@ -1,7 +1,10 @@
 
 <PID>tabled.pid</PID>
 <ForceHost>localhost.localdomain</ForceHost>
-<Listen><PortFile>tabled.acc</PortFile></Listen>
+<Listen>
+  <Port>auto</Port>
+  <PortFile>tabled.acc</PortFile>
+</Listen>
 <TDB>data/tdb</TDB>
 <TDBRepPort>18083</TDBRepPort>
 
--
To unsubscribe from this list: send the line "unsubscribe hail-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Fedora Clound]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux