Skip to contentSkip to conversation

cld: implement dynamic port

PATCH In review Pete Zaitcev 2 participants 6 messages Sep 3, 2009 – Sep 16, 2009 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 cld: implement dynamic port

This patch is needed to build on Fedora. The buildsystem can run two builds on the same system in the same time (for example, one for 32-bit buildroot, and another for 64-bit buildroot). Since we include "make check" step, port numbers used by simultaneous builds conflict, causing the build to fail.

Keeping a patch for this in Fedora is additional labor placed on maintainer, and there's a remote possibility that it may be useful for someone else, so let's include this feature.

include/cld_msg.h +1 -0
index e4c8f28..641b857 100644
@@ -258,5 +258,6 @@ struct cld_msg_event {
 extern unsigned long long cld_sid2llu(const uint8_t *sid);
 extern void __cld_rand64(void *p);
 extern const char *cld_errstr(enum cle_err_codes ecode);
+extern int cld_readport(const char *fname);
 
 #endif /* __CLD_MSG_H__ */
lib/common.c +31 -0
index a4eda54..68f60f8 100644
@@ -1,5 +1,8 @@
 
 #include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
 #include <glib.h>
 #include <cld-private.h>
 #include "cld_msg.h"
@@ -53,3 +56,31 @@ const char *cld_errstr(enum cle_err_codes ecode)
 	return cld_errlist[ecode];
 }
 
+/*
+ * Read a port number from a port file, return the value or negative error.
+ */
+int cld_readport(const char *fname)
+{
+	enum { LEN = 11 };
+	char buf[LEN+1];
+	long port;
+	int fd;
+	int rc;
+
+	if ((fd = open(fname, O_RDONLY)) == -1)
+		return -errno;
+	rc = read(fd, buf, LEN);
+	close(fd);
+	if (rc < 0)
+		return -errno;
+	if (rc == 0)
+		return -EPIPE;
+	buf[rc] = 0;
+
+	port = strtol(buf, NULL, 10);
+	if (port <= 0 || port >= 65636)
+		return -EDOM;
+
+	return (int)port;
+}
+
server/cld.h +2 -0
index 537f07a..e5ea9aa 100644
@@ -113,6 +113,8 @@ struct server {
 	int			pid_fd;
 
 	char			*port;		/* bind port */
+	char			*port_file;	/* Port file to write */
+	bool			port_set;
 
 	struct cldb		cldb;		/* database info */
 
server/server.c +139 -43

245 lines not on this page · raw message

test/it-works.c +8 -1
index 9bec2a7..bd2f965 100644
@@ -72,9 +72,16 @@ static struct cldc_ops ops = {
 static int init(void)
 {
 	int rc;
+	int port;
 	struct cldc_call_opts copts;
 
-	rc = cldc_udp_new("localhost", 18181, &udp);
+	port = cld_readport("cld.port");	/* FIXME need test.h */
+	if (port < 0)
+		return port;
+	if (port == 0)
+		return -1;
+
+	rc = cldc_udp_new("localhost", port, &udp);
 	if (rc)
 		return rc;
 
test/load-file-event.c +8 -1
index 3274787..1620508 100644
@@ -188,6 +188,7 @@ static struct cldc_ops ops = {
 static int init(char *name)
 {
 	int rc;
+	int port;
 	struct cldc_call_opts copts;
 
 	run.fname = name;
@@ -202,7 +203,13 @@ static int init(char *name)
 	run.len = rc;
 #endif
 
-	rc = cldc_udp_new("localhost", 18181, &run.udp);
+	port = cld_readport("cld.port");	/* FIXME need test.h */
+	if (port < 0)
+		return port;
+	if (port == 0)
+		return -1;
+
+	rc = cldc_udp_new("localhost", port, &run.udp);
 	if (rc)
 		return rc;
 
test/lock-file-event.c +8 -1
index cbf1647..c69da66 100644
@@ -208,11 +208,18 @@ static struct cldc_ops ops = {
 static int init(void)
 {
 	int rc;
+	int port;
 	struct cldc_call_opts copts;
 
 	memcpy(run.buf, TESTSTR, TESTLEN);
 
-	rc = cldc_udp_new("localhost", 18181, &run.udp);
+	port = cld_readport("cld.port");	/* FIXME need test.h */
+	if (port < 0)
+		return port;
+	if (port == 0)
+		return -1;
+
+	rc = cldc_udp_new("localhost", port, &run.udp);
 	if (rc)
 		return rc;
 
test/pid-exists +7 -1
index 351b4f1..34c7258 100755
@@ -2,7 +2,13 @@
 
 if [ ! -f cld.pid ]
 then
-	echo "pid file not found."
+	echo "pid file not found." >&2
+	exit 1
+fi
+
+if [ ! -f cld.port ]
+then
+	echo "port file not found." >&2
 	exit 1
 fi
 
test/save-file-event.c +8 -1
index 01a0331..1b3418a 100644
@@ -191,6 +191,7 @@ static struct cldc_ops ops = {
 static int init(char *name)
 {
 	int rc;
+	int port;
 	struct cldc_call_opts copts;
 
 	run.fname = name;
@@ -208,7 +209,13 @@ static int init(char *name)
 	run.len = TESTLEN;
 #endif
 
-	rc = cldc_udp_new("localhost", 18181, &run.udp);
+	port = cld_readport("cld.port");	/* FIXME need test.h */
+	if (port < 0)
+		return port;
+	if (port == 0)
+		return -1;
+
+	rc = cldc_udp_new("localhost", port, &run.udp);
 	if (rc)
 		return rc;
 
test/start-daemon +6 -1
index 4cb9fd7..25cdab4 100755
@@ -6,7 +6,12 @@ then
 	exit 1
 fi
 
-../server/cld -P cld.pid -d "$PWD/data" -p 18181 -E
+## Static port
+cldport=18181
+echo $cldport > cld.port
+../server/cld -P cld.pid -d "$PWD/data" -p $cldport -E
+## Dynamic port
+# ../server/cld -P cld.pid -d "$PWD/data" --port-file=cld.port -E
 
 sleep 3
 
test/stop-daemon +2 -0
index 27d985e..1949dc4 100755
@@ -1,5 +1,7 @@
 #!/bin/sh
 
+rm -f cld.port
+
 if [ ! -f cld.pid ]
 then
 	echo no daemon pid file found.
#1 lore raw
Quoted 11 lines · Pete Zaitcev

This patch is needed to build on Fedora. The buildsystem can run two builds on the same system in the same time (for example, one for 32-bit buildroot, and another for 64-bit buildroot). Since we include "make check" step, port numbers used by simultaneous builds conflict, causing the build to fail.

Keeping a patch for this in Fedora is additional labor placed on maintainer, and there's a remote possibility that it may be useful for someone else, so let's include this feature.

applied 1-2

though for uniformity, I lean towards thinking that the daemon should always write a port file, regardless of whether the port was manually specified or automatically bound.

that enables scripts to work regardless of the locally chosen site policy.

Jeff

#2 lore raw
Quoted 3 lines · Jeff Garzik

though for uniformity, I lean towards thinking that the daemon should always write a port file, regardless of whether the port was manually specified or automatically bound.

I have a better idea. Let's split PortFile from the enabling of auto ports. We can have <Port> to with a special string "auto", and let <PortFile> to be set when static port is configured.

-- Pete

#3 lore raw
Quoted 9 lines · Pete Zaitcev
Quoted 3 lines · Jeff Garzik

though for uniformity, I lean towards thinking that the daemon should always write a port file, regardless of whether the port was manually specified or automatically bound.

I have a better idea. Let's split PortFile from the enabling of auto ports. We can have<Port> to with a special string "auto", and let<PortFile> to be set when static port is configured.

I think that's fair...

Jeff

#4 lore raw
Quoted 7 lines · Jeff Garzik
Quoted 6 lines · Pete Zaitcev
Quoted 3 lines · Jeff Garzik

though for uniformity, I lean towards thinking that the daemon should always write a port file, regardless of whether the port was manually specified or automatically bound.

Quoted 5 lines
Quoted 3 lines

I have a better idea. Let's split PortFile from the enabling of auto ports. We can have<Port> to with a special string "auto", and let<PortFile> to be set when static port is configured.

I think that's fair...

I have posted tabled already, so we have all three in exactly the same, old scheme now. I'll patch it over. Not sure if I can make it compatible though... There may be another flag day. Sorry.

I realized my folly when <PortFile>/dev/null</PortFile> appeared. Probably would've fixed it anyway.

-- Pete

#5 lore raw
Quoted 12 lines · Pete Zaitcev
Quoted 11 lines · Jeff Garzik
Quoted 9 lines · Pete Zaitcev
Quoted 3 lines · Jeff Garzik

though for uniformity, I lean towards thinking that the daemon should always write a port file, regardless of whether the port was manually specified or automatically bound.

I have a better idea. Let's split PortFile from the enabling
of auto ports. We can have<Port> to with a special string "auto",
and let<PortFile> to be set when static port is configured.

Quoted 5 lines
Quoted 1 line

I think that's fair...

I have posted tabled already, so we have all three in exactly the same, old scheme now. I'll patch it over. Not sure if I can make it compatible though... There may be another flag day. Sorry.

Flag days are just fine, this early in the project.

Jeff