networkd-wait-online: convert to OPTION macros
Also fix a latent bug in parse_interface_with_operstate_range() where the global 'optarg' was used instead of the 'str' parameter when extracting the interface name; with getopt removed they would have diverged. The help strings are adjusted a bit to be grammatical and short so that the table formatting is easier. Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <getopt.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "sd-event.h"
|
||||
@@ -8,11 +7,13 @@
|
||||
#include "alloc-util.h"
|
||||
#include "build.h"
|
||||
#include "daemon-util.h"
|
||||
#include "format-table.h"
|
||||
#include "hashmap.h"
|
||||
#include "help-util.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "options.h"
|
||||
#include "parse-argument.h"
|
||||
#include "pretty-print.h"
|
||||
#include "socket-util.h"
|
||||
#include "strv.h"
|
||||
#include "time-util.h"
|
||||
@@ -31,32 +32,22 @@ STATIC_DESTRUCTOR_REGISTER(arg_interfaces, hashmap_freep);
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_ignore, strv_freep);
|
||||
|
||||
static int help(void) {
|
||||
_cleanup_free_ char *link = NULL;
|
||||
_cleanup_(table_unrefp) Table *options = NULL;
|
||||
int r;
|
||||
|
||||
r = terminal_urlify_man("systemd-networkd-wait-online.service", "8", &link);
|
||||
r = option_parser_get_help_table(&options);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
return r;
|
||||
|
||||
printf("%s [OPTIONS...]\n\n"
|
||||
"Block until network is configured.\n\n"
|
||||
" -h --help Show this help\n"
|
||||
" --version Print version string\n"
|
||||
" -q --quiet Do not show status information\n"
|
||||
" -i --interface=INTERFACE[:MIN_OPERSTATE[:MAX_OPERSTATE]]\n"
|
||||
" Block until at least these interfaces have appeared\n"
|
||||
" --ignore=INTERFACE Don't take these interfaces into account\n"
|
||||
" -o --operational-state=MIN_OPERSTATE[:MAX_OPERSTATE]\n"
|
||||
" Required operational state\n"
|
||||
" -4 --ipv4 Requires at least one IPv4 address\n"
|
||||
" -6 --ipv6 Requires at least one IPv6 address\n"
|
||||
" --any Wait until at least one of the interfaces is online\n"
|
||||
" --timeout=SECS Maximum time to wait for network connectivity\n"
|
||||
" --dns Requires at least one DNS server to be accessible\n"
|
||||
"\nSee the %s for details.\n",
|
||||
program_invocation_short_name,
|
||||
link);
|
||||
help_cmdline("[OPTIONS...]");
|
||||
help_abstract("Block until network is configured.");
|
||||
|
||||
help_section("Options");
|
||||
r = table_print_or_warn(options);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
help_man_page_reference("systemd-networkd-wait-online.service", "8");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -78,7 +69,7 @@ static int parse_interface_with_operstate_range(const char *str) {
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Invalid operational state range: %s", p + 1);
|
||||
|
||||
ifname = strndup(optarg, p - optarg);
|
||||
ifname = strndup(str, p - str);
|
||||
} else {
|
||||
*range = LINK_OPERSTATE_RANGE_INVALID;
|
||||
ifname = strdup(str);
|
||||
@@ -105,97 +96,70 @@ static int parse_interface_with_operstate_range(const char *str) {
|
||||
}
|
||||
|
||||
static int parse_argv(int argc, char *argv[]) {
|
||||
|
||||
enum {
|
||||
ARG_VERSION = 0x100,
|
||||
ARG_IGNORE,
|
||||
ARG_ANY,
|
||||
ARG_TIMEOUT,
|
||||
ARG_DNS,
|
||||
};
|
||||
|
||||
static const struct option options[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "version", no_argument, NULL, ARG_VERSION },
|
||||
{ "quiet", no_argument, NULL, 'q' },
|
||||
{ "interface", required_argument, NULL, 'i' },
|
||||
{ "ignore", required_argument, NULL, ARG_IGNORE },
|
||||
{ "operational-state", required_argument, NULL, 'o' },
|
||||
{ "ipv4", no_argument, NULL, '4' },
|
||||
{ "ipv6", no_argument, NULL, '6' },
|
||||
{ "any", no_argument, NULL, ARG_ANY },
|
||||
{ "timeout", required_argument, NULL, ARG_TIMEOUT },
|
||||
{ "dns", optional_argument, NULL, ARG_DNS },
|
||||
{}
|
||||
};
|
||||
|
||||
int c, r;
|
||||
int r;
|
||||
|
||||
assert(argc >= 0);
|
||||
assert(argv);
|
||||
|
||||
while ((c = getopt_long(argc, argv, "hi:qo:46", options, NULL)) >= 0)
|
||||
OptionParser opts = { argc, argv };
|
||||
|
||||
FOREACH_OPTION_OR_RETURN(c, &opts)
|
||||
switch (c) {
|
||||
|
||||
case 'h':
|
||||
help();
|
||||
return 0;
|
||||
OPTION_COMMON_HELP:
|
||||
return help();
|
||||
|
||||
case 'q':
|
||||
OPTION_COMMON_VERSION:
|
||||
return version();
|
||||
|
||||
OPTION('q', "quiet", NULL, "Do not show status information"):
|
||||
arg_quiet = true;
|
||||
break;
|
||||
|
||||
case ARG_VERSION:
|
||||
return version();
|
||||
|
||||
case 'i':
|
||||
r = parse_interface_with_operstate_range(optarg);
|
||||
OPTION('i', "interface", "IFNAME[:MIN[:MAX]]",
|
||||
"Block until at least these interfaces have appeared, "
|
||||
"in the operational state between MIN and MAX"):
|
||||
r = parse_interface_with_operstate_range(opts.arg);
|
||||
if (r < 0)
|
||||
return r;
|
||||
break;
|
||||
|
||||
case ARG_IGNORE:
|
||||
if (strv_extend(&arg_ignore, optarg) < 0)
|
||||
OPTION_LONG("ignore", "IFNAME", "Don't take these interfaces into account"):
|
||||
if (strv_extend(&arg_ignore, opts.arg) < 0)
|
||||
return log_oom();
|
||||
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
r = parse_operational_state_range(optarg, &arg_required_operstate);
|
||||
OPTION('o', "operational-state", "MIN[:MAX]",
|
||||
"Require operational state between MIN and MAX"):
|
||||
r = parse_operational_state_range(opts.arg, &arg_required_operstate);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Invalid operational state range '%s'", optarg);
|
||||
return log_error_errno(r, "Invalid operational state range '%s'", opts.arg);
|
||||
break;
|
||||
|
||||
case '4':
|
||||
OPTION('4', "ipv4", NULL, "Require at least one IPv4 address"):
|
||||
arg_required_family |= ADDRESS_FAMILY_IPV4;
|
||||
break;
|
||||
|
||||
case '6':
|
||||
OPTION('6', "ipv6", NULL, "Require at least one IPv6 address"):
|
||||
arg_required_family |= ADDRESS_FAMILY_IPV6;
|
||||
break;
|
||||
|
||||
case ARG_ANY:
|
||||
OPTION_LONG("any", NULL, "Wait until at least one of the interfaces is online"):
|
||||
arg_any = true;
|
||||
break;
|
||||
|
||||
case ARG_TIMEOUT:
|
||||
r = parse_sec(optarg, &arg_timeout);
|
||||
OPTION_LONG("timeout", "SECS", "Maximum time to wait for network connectivity"):
|
||||
r = parse_sec(opts.arg, &arg_timeout);
|
||||
if (r < 0)
|
||||
return r;
|
||||
break;
|
||||
|
||||
case ARG_DNS:
|
||||
r = parse_boolean_argument("--dns", optarg, &arg_requires_dns);
|
||||
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "dns", "BOOL",
|
||||
"Require at least one DNS server to be accessible"):
|
||||
r = parse_boolean_argument("--dns", opts.arg, &arg_requires_dns);
|
||||
if (r < 0)
|
||||
return r;
|
||||
break;
|
||||
|
||||
case '?':
|
||||
return -EINVAL;
|
||||
|
||||
default:
|
||||
assert_not_reached();
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
Reference in New Issue
Block a user