zpcictl: Fix command line parsing for invalid options

Currently zpcictl silently accepts incorrect command line options while
it should be displaying an error message for invalid ones. There is a
check for the case when no arguments are supplied, but invalid arguments
or only specifying a device without an action does not display an error
nor give a failure exit code.

Fix this by changing parse_cmdline() to return a boolean indicating if
any arguments were supplied while exiting with an error and message when
invalid options are detected.

Reviewed-by: Niklas Schnelle <schnelle@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Jan Höppner
2025-05-14 12:19:51 +02:00
parent bc511f84fe
commit 7bc0fb809d

View File

@@ -9,6 +9,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <time.h>
@@ -139,6 +140,13 @@ static void fwrite_err(FILE *fp, char *path)
exit(EXIT_FAILURE);
}
static void missing_option_err(void)
{
warnx("No option specified");
util_prg_print_parse_error();
exit(EXIT_FAILURE);
}
#define READ_CHUNK_SIZE 512
static char *collect_smart_data(struct zpci_device *pdev)
@@ -357,15 +365,20 @@ static void sclp_report_error(struct zpci_device *pdev)
sclp_issue_action(pdev, SCLP_ERRNOTIFY_AQ_REPORT_ERR);
}
static void parse_cmdline(int argc, char *argv[], struct options *opts)
static bool parse_cmdline(int argc, char *argv[], struct options *opts)
{
bool opt_specified = false;
int cmd;
util_prg_init(&prg);
util_opt_init(opt_vec, NULL);
do {
while (1) {
cmd = util_opt_getopt_long(argc, argv);
if (cmd == -1)
break;
opt_specified = true;
switch (cmd) {
case OPT_RESET:
@@ -387,16 +400,13 @@ static void parse_cmdline(int argc, char *argv[], struct options *opts)
case 'v':
util_prg_print_version();
exit(EXIT_SUCCESS);
case -1:
/* End of options string */
if (argc == 1) {
errx(EXIT_FAILURE,
"Use '%s --help' for more information",
argv[0]);
}
break;
default:
util_opt_print_parse_error(cmd, argv);
exit(EXIT_FAILURE);
}
} while (cmd != -1);
}
return opt_specified;
}
int main(int argc, char *argv[])
@@ -404,7 +414,8 @@ int main(int argc, char *argv[])
struct zpci_device pdev = { 0 };
struct options opts = { 0 };
parse_cmdline(argc, argv, &opts);
if (!parse_cmdline(argc, argv, &opts))
missing_option_err();
if (optind >= argc)
errx(EXIT_FAILURE, "No device specified");