[sheepdog] [PATCH 2/3] collie: introduce do_generic_subcommand()

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


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

This helper will check required argument if SUBCMD_FLAG_NEED_ARG is set and
call update_node_list if SUBCMD_FLAG_NEED_NODELIST is set.

Argument check is fatal to fix segfault by later patch.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 collie/collie.c |    2 +-
 collie/collie.h |    2 ++
 collie/common.c |   29 +++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/collie/collie.c b/collie/collie.c
index ced376a..2394350 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -45,7 +45,7 @@ struct sd_vnode sd_vnodes[SD_MAX_VNODES];
 int sd_nodes_nr, sd_vnodes_nr;
 unsigned master_idx;
 
-static int update_node_list(int max_nodes, uint32_t epoch)
+int update_node_list(int max_nodes, uint32_t epoch)
 {
 	int fd, ret;
 	unsigned int size;
diff --git a/collie/collie.h b/collie/collie.h
index f71c573..3546290 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -71,6 +71,8 @@ int sd_write_object(uint64_t oid, uint64_t cow_oid, void *data,
 int send_light_req(struct sd_req *hdr, const char *host, int port);
 int send_light_req_get_response(struct sd_req *hdr, const char *host, int port);
 int collie_exec_req(int sockfd, struct sd_req *hdr, void *data);
+int do_generic_subcommand(struct subcommand *sub, int argc, char **argv);
+int update_node_list(int max_nodes, uint32_t epoch);
 
 extern struct command vdi_command;
 extern struct command node_command;
diff --git a/collie/common.c b/collie/common.c
index a599ca3..34ce624 100644
--- a/collie/common.c
+++ b/collie/common.c
@@ -245,3 +245,32 @@ int collie_exec_req(int sockfd, struct sd_req *hdr, void *data)
 	/* Retry hard for collie because we can't get the newest epoch */
 	return exec_req(sockfd, hdr, data, NULL, 0);
 }
+
+int do_generic_subcommand(struct subcommand *sub, int argc, char **argv)
+{
+	int i, ret;
+
+	for (i = 0; sub[i].name; i++) {
+		if (!strcmp(sub[i].name, argv[optind])) {
+			unsigned long flags = sub[i].flags;
+
+			if (flags & SUBCMD_FLAG_NEED_NODELIST) {
+				ret = update_node_list(SD_MAX_NODES, 0);
+				if (ret < 0) {
+					fprintf(stderr,
+						"Failed to get node list\n");
+					exit(EXIT_SYSFAIL);
+				}
+			}
+
+			if (flags & SUBCMD_FLAG_NEED_ARG
+			    && argc != 5)
+				subcommand_usage(argv[1], argv[2], EXIT_USAGE);
+			optind++;
+			return sub[i].fn(argc, argv);
+		}
+	}
+
+	subcommand_usage(argv[1], argv[2], EXIT_FAILURE);
+	return EXIT_FAILURE;
+}
-- 
1.7.9.5




More information about the sheepdog mailing list