Beispiel #1
0
int
main(int argc, char **argv)
{
	ni_dhcp4_tester_t * tester = NULL;
	int c, status = NI_WICKED_RC_USAGE;

	ni_log_init();
	program_name = ni_basename(argv[0]);

	while ((c = getopt_long(argc, argv, "+", options, NULL)) != EOF) {
		switch (c) {
		/* common */
		case OPT_HELP:
			status = NI_WICKED_RC_SUCCESS;
		default:
		usage:
			fprintf(stderr,
				"%s [options]\n"
				"This command understands the following options\n"
				"  --help\n"
				"  --version\n"
				"  --config filename\n"
				"        Read configuration file <filename> instead of system default.\n"
				"  --log-level level\n"
				"        Set log level to <error|warning|notice|info|debug>.\n"
				"  --log-target target\n"
				"        Set log destination to <stderr|syslog>.\n"
				"  --debug facility\n"
				"        Enable debugging for debug <facility>.\n"
				"        Use '--debug help' for a list of facilities.\n"
				"  --foreground\n"
				"        Do not background the service.\n"
				"  --recover\n"
				"        Enable automatic recovery of daemon's state.\n"
				"  --systemd\n"
				"        Enables behavior required by systemd service\n"
				"\n"
				"  --test [test-options] <ifname>\n"
				"    test-options:\n"
				"       --test-request <request.xml>\n"
				"       --test-timeout <timeout in sec> (default: 20+10)\n"
				"       --test-output  <output file name>\n"
				"       --test-format  <leaseinfo|lease-xml>\n"
				, program_name);
			return status;

		case OPT_VERSION:
			printf("%s %s\n", program_name, PACKAGE_VERSION);
			return NI_WICKED_RC_SUCCESS;

		case OPT_CONFIGFILE:
			if (!ni_set_global_config_path(optarg)) {
				fprintf(stderr, "Unable to set config file '%s': %m\n",
						optarg);
				return NI_WICKED_RC_ERROR;
			}
			break;

		case OPT_DEBUG:
			if (!strcmp(optarg, "help")) {
				printf("Supported debug facilities:\n");
				ni_debug_help();
				return NI_WICKED_RC_SUCCESS;
			}
			if (ni_enable_debug(optarg) < 0) {
				fprintf(stderr, "Bad debug facility \"%s\"\n", optarg);
				goto usage;
			}
			break;

		case OPT_LOG_LEVEL:
			if (!ni_log_level_set(optarg)) {
				fprintf(stderr, "Bad log level \%s\"\n", optarg);
				goto usage;
			}
			break;

		case OPT_LOG_TARGET:
			opt_log_target = optarg;
			break;

		/* daemon */
		case OPT_FOREGROUND:
			opt_foreground = TRUE;
			break;

		/* specific */
		case OPT_RECOVER:
			opt_recover_state = TRUE;
			break;

		case OPT_SYSTEMD:
			opt_systemd = TRUE;
			break;

		/* test run */
		case OPT_TEST:
			opt_foreground = TRUE;
			tester = ni_dhcp4_tester_init();
			break;

		case OPT_TEST_REQUEST:
			if (!tester || ni_string_empty(optarg))
				goto usage;
			tester->request = optarg;
			break;

		case OPT_TEST_TIMEOUT:
			if (!tester || ni_parse_uint(optarg,
						&tester->timeout, 0) < 0)
				goto usage;
			break;

		case OPT_TEST_OUTPUT:
			if (!tester || ni_string_empty(optarg))
				goto usage;
			tester->output = optarg;
			break;

		case OPT_TEST_OUTFMT:
			if (!tester || !ni_dhcp4_tester_set_outfmt(optarg,
						&tester->outfmt))
				goto usage;
			break;
		}
	}
Beispiel #2
0
int
ni_do_test_dhcp4(const char *caller, int argc, char **argv)
{
	enum {
		OPT_HELP 	 = 'h',
		OPT_TEST_TIMEOUT = 't',
		OPT_TEST_REQUEST = 'r',
		OPT_TEST_OUTPUT	 = 'o',
		OPT_TEST_OUTFMT	 = 'F',
	};
	static struct option	options[] = {
		{ "help",	no_argument,		NULL,	OPT_HELP	},
		{ "request",	required_argument,	NULL,	OPT_TEST_REQUEST},
		{ "timeout",	required_argument,	NULL,	OPT_TEST_TIMEOUT},
		{ "output",	required_argument,	NULL,	OPT_TEST_OUTPUT	},
		{ "format",	required_argument,	NULL,	OPT_TEST_OUTFMT	},
		{ NULL,		no_argument,		NULL,	0		}
	};
	int opt = 0, status = NI_WICKED_RC_USAGE;
	char *program = NULL;
	ni_dhcp4_tester_t *tester;

	ni_string_printf(&program, "%s %s",	caller  ? caller  : "wicked",
						argv[0] ? argv[0] : "test");
	argv[0] = program;

	tester = ni_dhcp4_tester_init();
	if (tester == NULL) {
		fprintf(stderr, "Error: %s: unable to initialize dhcp4 tester\n", program);
		status = NI_WICKED_RC_ERROR;
		goto cleanup;
	}

	optind = 1;
	while ((opt = getopt_long(argc, argv, "+hr:t:o:F:", options, NULL)) != EOF) {
		switch (opt) {
		case OPT_HELP:
			status = NI_WICKED_RC_SUCCESS;
		default:
		usage:
			fprintf(stderr,
				"\nUsage:\n"
				"  %s [options] <ifname>\n"
				"\n"
				"Options:\n"
				"  --help, -h      show this help text and exit.\n"
				"\n"
				"  --timeout, -t   <timeout in sec> (default: 20+10)\n"
				"  --request, -r   <request.xml>\n"
				"  --output, -o    <output file name>\n"
				"  --format, -F    <leaseinfo|lease-xml>\n"
				"\n", program);
			goto cleanup;

		case OPT_TEST_TIMEOUT:
			if (ni_string_empty(optarg))
				goto usage;
			if (ni_parse_uint(optarg, &tester->timeout, 0) < 0) {
				fprintf(stderr, "%s: unable to parse timeout option '%s'\n",
						program, optarg);
				status = NI_WICKED_RC_ERROR;
				goto cleanup;
			}
			break;

		case OPT_TEST_REQUEST:
			if (ni_string_empty(optarg))
				goto usage;
			tester->request = optarg;
			break;

		case OPT_TEST_OUTPUT:
			if (ni_string_empty(optarg))
				goto usage;
			tester->output = optarg;
			break;

		case OPT_TEST_OUTFMT:
			if (ni_string_empty(optarg))
				goto usage;
			if (!ni_dhcp4_tester_set_outfmt(optarg, &tester->outfmt)) {
				fprintf(stderr, "%s: unable to parse output format option '%s'\n",
						program, optarg);
				status = NI_WICKED_RC_ERROR;
				goto cleanup;
			}
			break;
		}
	}

	if (optind >= argc || ni_string_empty(argv[optind])) {
		fprintf(stderr, "Error: %s: missing interface name argument\n", program);
		goto usage;
	} else
	if (optind + 1 != argc) {
		fprintf(stderr, "Error: %s: multiple interface names not supported\n", program);
		goto cleanup;
	}

	ni_netconfig_set_family_filter(ni_global_state_handle(0), AF_INET);
	ni_netconfig_set_discover_filter(ni_global_state_handle(0),
					NI_NETCONFIG_DISCOVER_LINK_EXTERN);

	tester->ifname = argv[optind];
	status = ni_dhcp4_tester_run(tester);

cleanup:
	ni_string_free(&program);
	return status;
}