chunkd: implement auto ports
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.
Signed-off-by Pete Zaitcev <[email protected]>
include/chunkc.h +2 -0
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 { \