* [LTP] [PATCH] tools/apicmd: add tst_get_unused_port
@ 2014-03-18 11:01 Alexey Kodanev
2014-03-18 12:17 ` chrubis
0 siblings, 1 reply; 2+ messages in thread
From: Alexey Kodanev @ 2014-03-18 11:01 UTC (permalink / raw)
To: ltp-list; +Cc: vasily.isaenko
Signed-off-by: Alexey Kodanev <alexey.kodanev@oracle.com>
---
tools/apicmds/.gitignore | 1 +
tools/apicmds/Makefile | 2 +-
tools/apicmds/ltpapicmd.c | 37 +++++++++++++++++++++++++++++++++++++
3 files changed, 39 insertions(+), 1 deletions(-)
diff --git a/tools/apicmds/.gitignore b/tools/apicmds/.gitignore
index a6d73d3..0c1a896 100644
--- a/tools/apicmds/.gitignore
+++ b/tools/apicmds/.gitignore
@@ -2,6 +2,7 @@ tst_brk
tst_brkm
tst_exit
tst_flush
+tst_get_unused_port
tst_kvercmp
tst_kvercmp2
tst_ncpus
diff --git a/tools/apicmds/Makefile b/tools/apicmds/Makefile
index 3cadc3f..9ecf570 100644
--- a/tools/apicmds/Makefile
+++ b/tools/apicmds/Makefile
@@ -27,7 +27,7 @@ include $(top_srcdir)/include/mk/testcases.mk
CPPFLAGS += -D_GNU_SOURCE
MAKE_TARGETS := $(addprefix tst_,brk brkm exit flush kvercmp kvercmp2 \
- res resm ncpus ncpus_max)
+ res resm ncpus ncpus_max get_unused_port)
include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/tools/apicmds/ltpapicmd.c b/tools/apicmds/ltpapicmd.c
index 8fae62a..140fbe2 100644
--- a/tools/apicmds/ltpapicmd.c
+++ b/tools/apicmds/ltpapicmd.c
@@ -69,6 +69,7 @@
*
*/
+#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -289,6 +290,40 @@ void apicmd_kvercmp2(int argc, char *argv[])
exit(exit_value);
}
+struct param_pair {
+ char *cmd;
+ 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 }, { 0, 0 }},
+ {{ "stream", SOCK_STREAM }, { "dgram", SOCK_DGRAM }, { 0, 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);
+}
+
/*
* Function: main - entry point of this program
*
@@ -371,6 +406,8 @@ int main(int argc, char *argv[])
printf("%li\n", tst_ncpus());
} 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));
}
exit(0);
--
1.7.1
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [LTP] [PATCH] tools/apicmd: add tst_get_unused_port
2014-03-18 11:01 [LTP] [PATCH] tools/apicmd: add tst_get_unused_port Alexey Kodanev
@ 2014-03-18 12:17 ` chrubis
0 siblings, 0 replies; 2+ messages in thread
From: chrubis @ 2014-03-18 12:17 UTC (permalink / raw)
To: Alexey Kodanev; +Cc: vasily.isaenko, ltp-list
Hi!
> +#include <sys/socket.h>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> @@ -289,6 +290,40 @@ void apicmd_kvercmp2(int argc, char *argv[])
> exit(exit_value);
> }
>
> +struct param_pair {
> + char *cmd;
> + 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 }, { 0, 0 }},
^
I would rather see NULL here.
Because I'm not 100% sure that the automatic cast will do the right
thing on all implementations.
Otherwise it's fine.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2014-03-18 12:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-18 11:01 [LTP] [PATCH] tools/apicmd: add tst_get_unused_port Alexey Kodanev
2014-03-18 12:17 ` chrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox