Skip to contentSkip to conversation

chunkd: implement auto ports

PATCH Applied Pete Zaitcev 1 participant 1 message Sep 11, 2009 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 chunkd: implement auto ports

This patch helps us to build inside Fedora build system where port conflicts from simultaneous builds are an issue. Most of the code is taken from a similar patch in CLD, although there are differences. In particular, we use the configuration file instead of command line.

The whole deal is left underdocumented.

Also, this patch changes the invocation of cld in start-daemon, and therefore requires changes in the installed cld. Thus we have a one-way flag day.

include/chunkc.h +2 -0
index 49c52ae..8b7df15 100644
@@ -63,4 +63,6 @@ extern bool stc_ping(struct st_client *stc);
 
 extern struct st_keylist *stc_keys(struct st_client *stc);
 
+extern int stc_readport(const char *fname);
+
 #endif /* __STC_H__ */
lib/chunkdc.c +30 -0
index e8c6391..121b589 100644
@@ -6,6 +6,7 @@
 #include <sys/ioctl.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <fcntl.h>
 #include <string.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
@@ -864,3 +865,32 @@ void stc_init(void)
 	srand(time(NULL) ^ getpid());	// for __cld_rand64 et.al.
 }
 
+/*
+ * Read a port number from a port file, return the value or negative error.
+ * A 100% copy of cld_readport for now, but permits us not to link libcldc.
+ */
+int stc_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/chunkd.h +1 -0
index 754c250..b7ad09d 100644
@@ -115,6 +115,7 @@ enum st_cld {
 struct listen_cfg {
 	char			*node;
 	char			*port;
+	char			*port_file;
 	bool			encrypt;
 };
 
server/config.c +60 -11

114 lines not on this page · raw message

server/server.c +156 -67

268 lines not on this page · raw message

server/util.c +6 -4
index dd31ddd..76dea5b 100644
@@ -133,8 +133,9 @@ int fsetflags(const char *prefix, int fd, int or_flags)
 	/* get current flags */
 	old_flags = fcntl(fd, F_GETFL);
 	if (old_flags < 0) {
-		applog(LOG_ERR, "%s F_GETFL: %s", prefix, strerror(errno));
-		return -errno;
+		rc = errno;
+		applog(LOG_ERR, "%s F_GETFL: %s", prefix, strerror(rc));
+		return -rc;
 	}
 
 	/* add or_flags */
@@ -144,8 +145,9 @@ int fsetflags(const char *prefix, int fd, int or_flags)
 	/* set new flags */
 	if (flags != old_flags)
 		if (fcntl(fd, F_SETFL, flags) < 0) {
-			applog(LOG_ERR, "%s F_SETFL: %s", prefix, strerror(errno));
-			rc = -errno;
+			rc = errno;
+			applog(LOG_ERR, "%s F_SETFL: %s", prefix, strerror(rc));
+			rc = -rc;
 		}
 
 	return rc;
test/auth.c +6 -4
index 1a8dab1..107b988 100644
@@ -14,6 +14,7 @@ static void test(bool encrypt)
 	struct st_object *obj;
 	struct st_keylist *klist;
 	struct st_client *stc1, *stc2;
+	int port;
 	bool rcb;
 	char val1[] = "my first value";
 	char val2[] = "my second value";
@@ -22,12 +23,13 @@ static void test(bool encrypt)
 	size_t len = 0;
 	void *mem;
 
-	stc1 = stc_new(TEST_HOST, encrypt ? TEST_SSL_PORT : TEST_PORT,
-		      TEST_USER, TEST_USER_KEY, encrypt);
+	port = stc_readport(encrypt ? TEST_PORTFILE_SSL : TEST_PORTFILE);
+	OK(port > 0);
+
+	stc1 = stc_new(TEST_HOST, port, TEST_USER, TEST_USER_KEY, encrypt);
 	OK(stc1);
 
-	stc2 = stc_new(TEST_HOST, encrypt ? TEST_SSL_PORT : TEST_PORT,
-		      TEST_USER2, TEST_USER2_KEY, encrypt);
+	stc2 = stc_new(TEST_HOST, port, TEST_USER2, TEST_USER2_KEY, encrypt);
 	OK(stc2);
 
 	/* store object 1 */
test/basic-object.c +5 -2
index ee4e7c5..7abb1d7 100644
@@ -14,14 +14,17 @@ static void test(bool encrypt)
 	struct st_object *obj;
 	struct st_keylist *klist;
 	struct st_client *stc;
+	int port;
 	bool rcb;
 	char val[] = "my first value";
 	char key[64] = "deadbeef";
 	size_t len = 0;
 	void *mem;
 
-	stc = stc_new(TEST_HOST, encrypt ? TEST_SSL_PORT : TEST_PORT,
-		      TEST_USER, TEST_USER_KEY, encrypt);
+	port = stc_readport(encrypt ? TEST_PORTFILE_SSL : TEST_PORTFILE);
+	OK(port > 0);
+
+	stc = stc_new(TEST_HOST, port, TEST_USER, TEST_USER_KEY, encrypt);
 	OK(stc);
 
 	/* store object */
test/it-works.c +5 -2
index cb53ed1..ef923d8 100644
@@ -12,10 +12,13 @@
 static void test(bool ssl)
 {
 	struct st_client *stc;
+	int port;
 	bool rcb;
 
-	stc = stc_new(TEST_HOST, ssl ? TEST_SSL_PORT : TEST_PORT,
-		      TEST_USER, TEST_USER_KEY, ssl);
+	port = stc_readport(ssl ? TEST_PORTFILE_SSL : TEST_PORTFILE);
+	OK(port > 0);
+
+	stc = stc_new(TEST_HOST, port, TEST_USER, TEST_USER_KEY, ssl);
 	OK(stc);
 
 	rcb = stc_ping(stc);
test/large-object.c +5 -2
index ed8d73a..d97ce21 100644
@@ -75,6 +75,7 @@ static void test(bool encrypt)
 	struct st_object *obj;
 	struct st_keylist *klist;
 	struct st_client *stc;
+	int port;
 	bool rcb;
 	char key[64] = "deadbeef";
 	uint64_t len = 0;
@@ -86,8 +87,10 @@ static void test(bool encrypt)
 
 	memset(data, 0xdeadbeef, sizeof(data));
 
-	stc = stc_new(TEST_HOST, encrypt ? TEST_SSL_PORT : TEST_PORT,
-		      TEST_USER, TEST_USER_KEY, encrypt);
+	port = stc_readport(encrypt ? TEST_PORTFILE_SSL : TEST_PORTFILE);
+	OK(port > 0);
+
+	stc = stc_new(TEST_HOST, port, TEST_USER, TEST_USER_KEY, encrypt);
 	OK(stc);
 
 	sync();
test/lotsa-objects.c +5 -2
index 9bcb01c..a26ecc0 100644
@@ -18,6 +18,7 @@ static void test(int n_objects, bool encrypt)
 {
 	struct st_keylist *klist;
 	struct st_client *stc;
+	int port;
 	bool rcb;
 	char val[] = "my first value";
 	char key[64] = "";
@@ -26,8 +27,10 @@ static void test(int n_objects, bool encrypt)
 	char *k;
 	struct timeval ta, tb;
 
-	stc = stc_new(TEST_HOST, encrypt ? TEST_SSL_PORT : TEST_PORT,
-		      TEST_USER, TEST_USER_KEY, encrypt);
+	port = stc_readport(encrypt ? TEST_PORTFILE_SSL : TEST_PORTFILE);
+	OK(port > 0);
+
+	stc = stc_new(TEST_HOST, port, TEST_USER, TEST_USER_KEY, encrypt);
 	OK(stc);
 
 	fprintf(stderr, "      lotsa-objects syncing...\n");
test/nop.c +5 -2
index 7ef11a1..c683dc4 100644
@@ -17,12 +17,15 @@ enum {
 static void test(int n_nops, bool encrypt)
 {
 	struct st_client *stc;
+	int port;
 	bool rcb;
 	int i;
 	struct timeval ta, tb;
 
-	stc = stc_new(TEST_HOST, encrypt ? TEST_SSL_PORT : TEST_PORT,
-		      TEST_USER, TEST_USER_KEY, encrypt);
+	port = stc_readport(encrypt ? TEST_PORTFILE_SSL : TEST_PORTFILE);
+	OK(port > 0);
+
+	stc = stc_new(TEST_HOST, port, TEST_USER, TEST_USER_KEY, encrypt);
 	OK(stc);
 
 	gettimeofday(&ta, NULL);
test/server-test.cfg +3 -3
index 02c39d4..034f677 100644
@@ -5,11 +5,11 @@
 </SSL>
 
 <Listen>
-	<Port>18082</Port>
+	<PortFile>chunkd.port</PortFile>
 </Listen>
 
 <Listen>
-	<Port>28082</Port>
+	<PortFile>chunkd-ssl.port</PortFile>
 	<Encrypt>true</Encrypt>
 </Listen>
 
@@ -20,7 +20,7 @@
 <NID>1</NID>
 
 <CLD>
-  <Port>18081</Port>
+  <PortFile>cld.port</PortFile>
   <Host>localhost</Host>
 </CLD>
 
test/start-daemon.real.in +1 -1
index 052360a..cd23a2f 100644
@@ -11,7 +11,7 @@ then
 	exit 1
 fi
 
-@CLDC_CMD@ -d data/cld -P cld.pid -p 18081 -E
+@CLDC_CMD@ -d data/cld -P cld.pid --port-file=cld.port  -E
 
 ../server/chunkd -C $top_srcdir/test/server-test.cfg -E $*
 
test/stop-daemon +2 -0
index df14776..87eaf8b 100755
@@ -35,4 +35,6 @@ else
 	killpid cld.pid || ret=1
 fi
 
+rm -f chunkd.port chunkd-ssl.port cld.port
+
 exit "$ret"
test/test.h +3 -2
index d1bf601..dd99843 100644
@@ -14,8 +14,9 @@
 #define TEST_USER2 "testuser2"
 #define TEST_USER2_KEY "testuser2"
 
-#define TEST_PORT 18082
-#define TEST_SSL_PORT 28082
+#define TEST_PORTFILE_CLD	"cld.port"
+#define TEST_PORTFILE		"chunkd.port"
+#define TEST_PORTFILE_SSL	"chunkd-ssl.port"
 
 #define OK(expr)				\
 	do {					\