Skip to contentSkip to conversation

lib: Migrate tst_get_unused_port to new API

PATCH New Petr Vorel 2 participants 4 messages May 20, 2019

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH 1/1 lib: Migrate tst_get_unused_port to new API

+ sort .gitignore in testcases/lib

---
 testcases/lib/.gitignore            | 11 +++---
 testcases/lib/Makefile              |  2 +-
 testcases/lib/tst_get_unused_port.c | 58 +++++++++++++++++++++++++++++
 tools/apicmds/.gitignore            |  1 -
 tools/apicmds/ltpapicmd.c           | 31 ---------------
 5 files changed, 65 insertions(+), 38 deletions(-)
 create mode 100644 testcases/lib/tst_get_unused_port.c
testcases/lib/.gitignore +6 -5
index d83a48e2a..52f99dc45 100644
@@ -1,12 +1,13 @@
-/tst_sleep
-/tst_random
 /tst_check_drivers
 /tst_checkpoint
-/tst_rod
-/tst_kvcmp
 /tst_device
+/tst_getconf
+/tst_get_unused_port
+/tst_kvcmp
 /tst_net_iface_prefix
 /tst_net_ip_prefix
 /tst_net_vars
-/tst_getconf
+/tst_random
+/tst_rod
+/tst_sleep
 /tst_supported_fs
testcases/lib/Makefile +1 -1
index e1dea3b05..4616e24c0 100644
@@ -28,6 +28,6 @@ INSTALL_TARGETS		:= *.sh
 
 MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
 			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\
-			   tst_getconf tst_supported_fs tst_check_drivers
+			   tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/lib/tst_get_unused_port.c new file +58 -0

58 lines not on this page · raw message

tools/apicmds/.gitignore +0 -1
index a9ca9ec6d..54a1d664d 100644
@@ -1,7 +1,6 @@
 tst_brk
 tst_brkm
 tst_exit
-tst_get_unused_port
 tst_ncpus
 tst_ncpus_conf
 tst_ncpus_max
tools/apicmds/ltpapicmd.c +0 -31
index d94061242..f7e2a5eab 100644
@@ -193,35 +193,6 @@ struct param_pair {
 	int value;
 };
 
-unsigned short apicmd_get_unused_port(int argc, char *argv[])
-{
-	if (argc != 3)
-		goto err;
-
-	const struct param_pair params[][3] = {
-		{{"ipv4", AF_INET}, {"ipv6", AF_INET6}, {NULL, 0}},
-		{{"stream", SOCK_STREAM}, {"dgram", SOCK_DGRAM}, {NULL, 0}}
-	};
-
-	int i;
-	const struct param_pair *p[2];
-	for (i = 0; i < 2; ++i) {
-		for (p[i] = params[i]; p[i]->cmd; ++p[i]) {
-			if (!strcmp(p[i]->cmd, argv[i]))
-				break;
-		}
-		if (!p[i]->cmd)
-			goto err;
-	}
-	return  TST_GET_UNUSED_PORT(NULL, p[0]->value, p[1]->value);
-
-err:
-	fprintf(stderr, "Usage: tst_get_unused_port FAMILY TYPE\n"
-		"where FAMILY := { ipv4 | ipv6 }\n"
-		"      TYPE := { stream | dgram }\n");
-	exit(1);
-}
-
 int apicmd_fs_has_free(int argc, char *argv[])
 {
 	if (argc != 3) {
@@ -336,8 +307,6 @@ int main(int argc, char *argv[])
 		printf("%li\n", tst_ncpus_conf());
 	} else if (strcmp(cmd_name, "tst_ncpus_max") == 0) {
 		printf("%li\n", tst_ncpus_max());
-	} else if (strcmp(cmd_name, "tst_get_unused_port") == 0) {
-		printf("%u\n", apicmd_get_unused_port(argc, argv));
 	} else if (strcmp(cmd_name, "tst_fs_has_free") == 0) {
 		apicmd_fs_has_free(argc, argv);
 	}
Signature · 1 line
2.21.0
author
#2 lore raw

PATCH 1/1 lib: Migrate tst_get_unused_port to new API

Hi,

Quoted 2 lines

Hi!
Acked.

Thanks.

I'm sorry, current implementation is wrong as it does not print port :). I'll push it with this diff (I guess we don't need new line for port, although that's not problematic for shell usage like this:

port=$(tst_get_unused_port ipv${TST_IPVER} stream)

Kind regards,
Petr

testcases/lib/tst_get_unused_port.c +2 -1
@@ -54,5 +54,6 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	return TST_GET_UNUSED_PORT(family, type);
+	printf("%d", TST_GET_UNUSED_PORT(family, type));
+	return 0;
 }
author
#3 lore raw

PATCH 1/1 lib: Migrate tst_get_unused_port to new API

Hi,

Pushed with following diff:

testcases/lib/tst_get_unused_port.c int main(int argc, char *argv[]) · line 59

Quoted 4 lines
testcases/lib/tst_get_unused_port.c +2 -1
-	return TST_GET_UNUSED_PORT(family, type);
+	printf("%d", TST_GET_UNUSED_PORT(family, type));
+	return 0;
 }

Kind regards,
Petr