[sheepdog] [PATCH 3/3] collie: fix segfault when required argument is not set

Liu Yuan namei.unix at gmail.com
Thu Apr 4 14:52:30 CEST 2013


From: Liu Yuan <tailai.ly at taobao.com>

Currently, command like 'collie node md plug' without arugment will cause
segfault because the lack of the argument check.

Use do_generic_subcommand() helper to fix this problem.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 collie/cluster.c |   10 +---------
 collie/node.c    |   21 +++++----------------
 collie/vdi.c     |   18 ++++--------------
 3 files changed, 10 insertions(+), 39 deletions(-)

diff --git a/collie/cluster.c b/collie/cluster.c
index b38091b..d7de019 100644
--- a/collie/cluster.c
+++ b/collie/cluster.c
@@ -439,15 +439,7 @@ static struct subcommand cluster_recover_cmd[] = {
 
 static int cluster_recover(int argc, char **argv)
 {
-	int i;
-
-	for (i = 0; cluster_recover_cmd[i].name; i++) {
-		if (!strcmp(cluster_recover_cmd[i].name, argv[optind]))
-			return cluster_recover_cmd[i].fn(argc, argv);
-	}
-
-	subcommand_usage(argv[1], argv[2], EXIT_FAILURE);
-	return EXIT_FAILURE;
+	return do_generic_subcommand(cluster_recover_cmd, argc, argv);
 }
 
 static struct subcommand cluster_cmd[] = {
diff --git a/collie/node.c b/collie/node.c
index 524c0e9..16f4f07 100644
--- a/collie/node.c
+++ b/collie/node.c
@@ -329,27 +329,17 @@ static int md_unplug(int argc, char **argv)
 
 static struct subcommand node_md_cmd[] = {
 	{"info", NULL, NULL, "show multi-disk information",
-	 NULL, 0, md_info},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST, md_info},
 	{"plug", NULL, NULL, "plug more disk(s) into node",
 	 NULL, SUBCMD_FLAG_NEED_ARG, md_plug},
 	{"unplug", NULL, NULL, "unplug disk(s) from node",
-	 NULL, 0, md_unplug},
+	 NULL, SUBCMD_FLAG_NEED_ARG, md_unplug},
 	{NULL},
 };
 
 static int node_md(int argc, char **argv)
 {
-	int i;
-
-	for (i = 0; node_md_cmd[i].name; i++) {
-		if (!strcmp(node_md_cmd[i].name, argv[optind])) {
-			optind++;
-			return node_md_cmd[i].fn(argc, argv);
-		}
-	}
-
-	subcommand_usage(argv[1], argv[2], EXIT_FAILURE);
-	return EXIT_FAILURE;
+	return do_generic_subcommand(node_md_cmd, argc, argv);
 }
 
 
@@ -381,9 +371,8 @@ static struct subcommand node_cmd[] = {
 	 SUBCMD_FLAG_NEED_NODELIST, node_recovery},
 	{"cache", "<cache size>", "aprh", "specify max cache size", NULL,
 	 SUBCMD_FLAG_NEED_ARG, node_cache},
-	{"md", NULL, "apAh", "See 'collie node md' for more information",
-	 node_md_cmd, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_ARG,
-	 node_md, node_options},
+	{"md", "[disks]", "apAh", "See 'collie node md' for more information",
+	 node_md_cmd, SUBCMD_FLAG_NEED_ARG, node_md, node_options},
 	{NULL,},
 };
 
diff --git a/collie/vdi.c b/collie/vdi.c
index ec4833b..67c96d1 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -1918,25 +1918,15 @@ out:
 
 static struct subcommand vdi_cache_cmd[] = {
 	{"flush", NULL, NULL, "flush the cache of the vdi specified.",
-	 NULL, 0, vdi_cache_flush},
+	 NULL, SUBCMD_FLAG_NEED_ARG, vdi_cache_flush},
 	{"delete", NULL, NULL, "delete the cache of the vdi specified in all nodes.",
-	 NULL, 0, vdi_cache_delete},
+	 NULL, SUBCMD_FLAG_NEED_ARG, vdi_cache_delete},
 	{NULL,},
 };
 
 static int vdi_cache(int argc, char **argv)
 {
-	int i;
-
-	for (i = 0; vdi_cache_cmd[i].name; i++) {
-		if (!strcmp(vdi_cache_cmd[i].name, argv[optind])) {
-			optind++;
-			return vdi_cache_cmd[i].fn(argc, argv);
-		}
-	}
-
-	subcommand_usage(argv[1], argv[2], EXIT_FAILURE);
-	return EXIT_FAILURE;
+	return do_generic_subcommand(vdi_cache_cmd, argc, argv);
 }
 
 static struct subcommand vdi_cmd[] = {
@@ -1994,7 +1984,7 @@ static struct subcommand vdi_cmd[] = {
 	{"restore", "<vdiname> <backup>", "saph", "restore snapshot images from a backup",
 	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_ARG,
 	 vdi_restore, vdi_options},
-	{"cache", NULL, "saph", "Run 'collie vdi cache' for more information\n",
+	{"cache", "<vdiname>", "saph", "Run 'collie vdi cache' for more information\n",
 	 vdi_cache_cmd, SUBCMD_FLAG_NEED_ARG,
 	 vdi_cache, vdi_options},
 	{NULL,},
-- 
1.7.9.5




More information about the sheepdog mailing list