Commit 938183c9 authored by Pawel Wodkowski's avatar Pawel Wodkowski Committed by Jim Harris
Browse files

app: remove max cmdline options limitation



Change-Id: I60cc27dc3fbecf1d3dce8f66c91961513401dad3
Signed-off-by: default avatarPawel Wodkowski <pawelx.wodkowski@intel.com>
Reviewed-on: https://review.gerrithub.io/422267


Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
parent 24d20caf
Loading
Loading
Loading
Loading
+19 −19
Original line number Diff line number Diff line
@@ -41,10 +41,10 @@
#include "spdk/trace.h"
#include "spdk/string.h"
#include "spdk/rpc.h"
#include "spdk/util.h"

#define SPDK_APP_DEFAULT_LOG_LEVEL		SPDK_LOG_NOTICE
#define SPDK_APP_DEFAULT_LOG_PRINT_LEVEL	SPDK_LOG_INFO
#define SPDK_APP_MAX_CMDLINE_OPTIONS		64

#define SPDK_APP_DPDK_DEFAULT_MEM_SIZE		-1
#define SPDK_APP_DPDK_DEFAULT_MASTER_CORE	-1
@@ -74,7 +74,7 @@ spdk_app_get_shm_id(void)
}

/* append one empty option to indicate the end of the array */
static struct option g_cmdline_options[SPDK_APP_MAX_CMDLINE_OPTIONS + 1] = {
static const struct option g_cmdline_options[] = {
#define CONFIG_FILE_OPT_IDX	'c'
	{"config",			required_argument,	NULL, CONFIG_FILE_OPT_IDX},
#define LIMIT_COREDUMP_OPT_IDX 'd'
@@ -111,11 +111,8 @@ static struct option g_cmdline_options[SPDK_APP_MAX_CMDLINE_OPTIONS + 1] = {
	{"silence-noticelog",		no_argument,		NULL, SILENCE_NOTICELOG_OPT_IDX},
#define WAIT_FOR_RPC_OPT_IDX	258
	{"wait-for-rpc",		no_argument,		NULL, WAIT_FOR_RPC_OPT_IDX},
	{NULL,				no_argument,		NULL, 0}
};

static char g_cmdline_short_opts[2 * SPDK_APP_MAX_CMDLINE_OPTIONS + 1];

/* Global section */
#define GLOBAL_CONFIG_TMPL \
"# Configuration file\n" \
@@ -731,6 +728,8 @@ spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts,
		    void (*app_usage)(void))
{
	int ch, rc, opt_idx, global_long_opts_len, app_long_opts_len;
	struct option *cmdline_options;
	char *cmdline_short_opts = NULL;
	enum spdk_app_parse_args_rvals retval = SPDK_APP_PARSE_ARGS_FAIL;

	memcpy(&g_default_opts, opts, sizeof(g_default_opts));
@@ -747,20 +746,17 @@ spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts,
		     app_long_opts_len++);
	}

	for (global_long_opts_len = 0;
	     g_cmdline_options[global_long_opts_len].name != NULL;
	     global_long_opts_len++);
	global_long_opts_len = SPDK_COUNTOF(g_cmdline_options);

	if (app_long_opts_len + global_long_opts_len > SPDK_APP_MAX_CMDLINE_OPTIONS) {
		fprintf(stderr, "Too many parseable command line options in %s()."
			" (got %d, max %d)\n", __func__,
			app_long_opts_len + global_long_opts_len,
			SPDK_APP_MAX_CMDLINE_OPTIONS);
		goto out;
	cmdline_options = calloc(global_long_opts_len + app_long_opts_len + 1, sizeof(*cmdline_options));
	if (!cmdline_options) {
		fprintf(stderr, "Out of memory\n");
		return SPDK_APP_PARSE_ARGS_FAIL;
	}

	memcpy(&cmdline_options[0], g_cmdline_options, sizeof(g_cmdline_options));
	if (app_long_opts) {
		memcpy(&g_cmdline_options[global_long_opts_len], app_long_opts,
		memcpy(&cmdline_options[global_long_opts_len], app_long_opts,
		       app_long_opts_len * sizeof(*app_long_opts));
	}

@@ -773,13 +769,15 @@ spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts,
		}
	}

	snprintf(g_cmdline_short_opts, sizeof(g_cmdline_short_opts),
		 "%s%s", app_getopt_str, SPDK_APP_GETOPT_STRING);
	cmdline_short_opts = spdk_sprintf_alloc("%s%s", app_getopt_str, SPDK_APP_GETOPT_STRING);
	if (!cmdline_short_opts) {
		fprintf(stderr, "Out of memory\n");
		goto out;
	}

	g_executable_name = argv[0];

	while ((ch = getopt_long(argc, argv, g_cmdline_short_opts,
				 g_cmdline_options, &opt_idx)) != -1) {
	while ((ch = getopt_long(argc, argv, cmdline_short_opts, cmdline_options, &opt_idx)) != -1) {
		switch (ch) {
		case CONFIG_FILE_OPT_IDX:
			opts->config_file = optarg;
@@ -926,6 +924,8 @@ spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts,

	retval = SPDK_APP_PARSE_ARGS_SUCCESS;
out:
	free(cmdline_short_opts);
	free(cmdline_options);
	return retval;
}

+1 −11
Original line number Diff line number Diff line
@@ -75,10 +75,8 @@ static void
test_spdk_app_parse_args(void)
{
	spdk_app_parse_args_rvals_t rc;
	int i;
	struct spdk_app_opts opts = {};
	struct option my_options[SPDK_APP_MAX_CMDLINE_OPTIONS + 1] = {{0}};
	char name_teaser[5] = "SPDK";
	struct option my_options[2] = {};
	char *valid_argv[test_argc] = {"app_ut",
				       "--wait-for-rpc",
				       "-d",
@@ -152,14 +150,6 @@ test_spdk_app_parse_args(void)
	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
	optind = 1;

	/* Test too many long options. Expected result: FAIL */
	for (i = 0; i < SPDK_APP_MAX_CMDLINE_OPTIONS; i++) {
		my_options[i].name = name_teaser;
	}
	rc = spdk_app_parse_args(test_argc, valid_argv, &opts, "", my_options, unittest_parse_args, NULL);
	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
	optind = 1;

	/* Specify -B and -W options at the same time. Expected result: FAIL */
	rc = spdk_app_parse_args(test_argc, invalid_argv_BW, &opts, "", NULL, unittest_parse_args, NULL);
	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);