From 8edbd165280b820b1579c0840f378c450ff657d7 Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Sat, 15 Feb 2025 10:16:59 +1000 Subject: [PATCH] Add a "help" command So that we can spawn the correct -t backend and have it print the usage information, including type specific arguments. Signed-off-by: Ronnie Sahlberg --- ublk.cpp | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/ublk.cpp b/ublk.cpp index 65917061..e3a3c1a8 100644 --- a/ublk.cpp +++ b/ublk.cpp @@ -23,21 +23,6 @@ static const struct ublksrv_tgt_type *ublksrv_find_tgt_type(const char *name) return NULL; } -static void ublksrv_for_each_tgt_type(void (*handle_tgt_type)(unsigned idx, - const struct ublksrv_tgt_type *type, void *data), - void *data) -{ - int i; - - for (i = 0; i < UBLKSRV_TGT_TYPE_MAX; i++) { - const struct ublksrv_tgt_type *type = tgt_list[i]; - - if (!type) - continue; - handle_tgt_type(i, type, data); - } -} - int ublksrv_register_tgt_type(struct ublksrv_tgt_type *type) { if (type->type < UBLKSRV_TGT_TYPE_MAX && !tgt_list[type->type]) { @@ -178,8 +163,8 @@ static void cmd_dev_add_usage(const char *cmd) { printf("%s add -t \n", cmd); ublksrv_print_std_opts(); - printf("\ttarget specific command line:\n"); - ublksrv_for_each_tgt_type(show_tgt_add_usage, NULL); + printf("\tFor type specific options, run:\n"); + printf("\t\tublk help -t \n"); } static int __cmd_dev_del(int number, bool log, bool async) @@ -553,10 +538,31 @@ static void cmd_usage(const char *cmd) cmd_dev_recover_usage(cmd); cmd_dev_get_features_help(cmd); + printf("%s help -t \n", cmd); printf("%s -v [--version]\n", cmd); printf("%s -h [--help]\n", cmd); } +static int cmd_dev_help(int argc, char *argv[]) +{ + struct ublksrv_dev_data data = {0}; + struct ublksrv_ctrl_dev *dev; + const struct ublksrv_tgt_type *tgt_type; + int ret; + const char *dump_buf; + + ublksrv_parse_std_opts(&data, argc, argv); + + if (data.tgt_type == NULL) { + cmd_usage("ublk"); + return EXIT_SUCCESS; + } + + ublksrv_execve_helper("help", data.tgt_type, argc, argv); + fprintf(stderr, "failed to spawn target\n"); + return EXIT_FAILURE; +} + int main(int argc, char *argv[]) { const char *prog_name = "ublk"; @@ -579,6 +585,8 @@ int main(int argc, char *argv[]) ret = cmd_dev_add(argc, argv); else if (!strcmp(cmd, "del")) ret = cmd_dev_del(argc, argv); + else if (!strcmp(cmd, "help")) + ret = cmd_dev_help(argc, argv); else if (!strcmp(cmd, "list")) ret = cmd_list_dev_info(argc, argv); else if (!strcmp(cmd, "recover"))