conf: Don't print usage via the logging subsystem

The message from usage() when given invalid options, or the -h / --help
option is currently printed by many calls to the info() function, also
used for runtime logging of informational messages.

That isn't useful: the usage message should always go to the terminal
(stdout or stderr), never syslog or a logfile.  It should never be
filtered by priority.  Really the only thing using the common logging
functions does is give more opportunities for something to go wrong.

Replace all the info() calls with direct fprintf() calls.  This does mean
manually adding "\n" to each message.  A little messy, but worth it for the
simplicity in other dimensions.  While we're there make much heavier use
of single strings containing multiple lines of output text.  That reduces
the number of fprintf calls, reducing visual clutter and making it easier
to see what the output will look like from the source.

Link: https://bugs.passt.top/show_bug.cgi?id=90
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2024-06-05 10:42:41 +10:00 committed by Stefano Brivio
parent e651197b5c
commit 7cb2088835

322
conf.c
View file

@ -706,193 +706,200 @@ static unsigned int conf_ip6(unsigned int ifi,
/** /**
* usage() - Print usage, exit with given status code * usage() - Print usage, exit with given status code
* @name: Executable name * @name: Executable name
* @f: Stream to print usage info to
* @status: Status code for exit() * @status: Status code for exit()
*/ */
static void usage(const char *name, int status) static void usage(const char *name, FILE *f, int status)
{ {
if (strstr(name, "pasta")) { if (strstr(name, "pasta")) {
info("Usage: %s [OPTION]... [COMMAND] [ARGS]...", name); fprintf(f, "Usage: %s [OPTION]... [COMMAND] [ARGS]...\n", name);
info(" %s [OPTION]... PID", name); fprintf(f, " %s [OPTION]... PID\n", name);
info(" %s [OPTION]... --netns [PATH|NAME]", name); fprintf(f, " %s [OPTION]... --netns [PATH|NAME]\n", name);
info(""); fprintf(f,
info("Without PID or --netns, run the given command or a"); "\n"
info("default shell in a new network and user namespace, and"); "Without PID or --netns, run the given command or a\n"
info("connect it via pasta."); "default shell in a new network and user namespace, and\n"
"connect it via pasta.\n");
} else { } else {
info("Usage: %s [OPTION]...", name); fprintf(f, "Usage: %s [OPTION]...\n", name);
} }
info("");
fprintf(f,
info( " -d, --debug Be verbose"); "\n"
info( " --trace Be extra verbose, implies --debug"); " -d, --debug Be verbose\n"
info( " -q, --quiet Don't print informational messages"); " --trace Be extra verbose, implies --debug\n"
info( " -f, --foreground Don't run in background"); " -q, --quiet Don't print informational messages\n"
info( " default: run in background if started from a TTY"); " -f, --foreground Don't run in background\n"
info( " -e, --stderr Log to stderr too"); " default: run in background if started from a TTY\n"
info( " default: log to system logger only if started from a TTY"); " -e, --stderr Log to stderr too\n"
info( " -l, --log-file PATH Log (only) to given file"); " default: log to system logger only if started from a TTY\n"
info( " --log-size BYTES Maximum size of log file"); " -l, --log-file PATH Log (only) to given file\n"
info( " default: 1 MiB"); " --log-size BYTES Maximum size of log file\n"
info( " --runas UID|UID:GID Run as given UID, GID, which can be"); " default: 1 MiB\n"
info( " numeric, or login and group names"); " --runas UID|UID:GID Run as given UID, GID, which can be\n"
info( " default: drop to user \"nobody\""); " numeric, or login and group names\n"
info( " -h, --help Display this help message and exit"); " default: drop to user \"nobody\"\n"
info( " --version Show version and exit"); " -h, --help Display this help message and exit\n"
" --version Show version and exit\n");
if (strstr(name, "pasta")) { if (strstr(name, "pasta")) {
info( " -I, --ns-ifname NAME namespace interface name"); fprintf(f,
info( " default: same interface name as external one"); " -I, --ns-ifname NAME namespace interface name\n"
" default: same interface name as external one\n");
} else { } else {
info( " -s, --socket PATH UNIX domain socket path"); fprintf(f,
info( " default: probe free path starting from " " -s, --socket PATH UNIX domain socket path\n"
UNIX_SOCK_PATH, 1); " default: probe free path starting from "
UNIX_SOCK_PATH "\n", 1);
} }
info( " -F, --fd FD Use FD as pre-opened connected socket"); fprintf(f,
info( " -p, --pcap FILE Log tap-facing traffic to pcap file"); " -F, --fd FD Use FD as pre-opened connected socket\n"
info( " -P, --pid FILE Write own PID to the given file"); " -p, --pcap FILE Log tap-facing traffic to pcap file\n"
info( " -m, --mtu MTU Assign MTU via DHCP/NDP"); " -P, --pid FILE Write own PID to the given file\n"
info( " a zero value disables assignment"); " -m, --mtu MTU Assign MTU via DHCP/NDP\n"
info( " default: 65520: maximum 802.3 MTU minus 802.3 header"); " a zero value disables assignment\n"
info( " length, rounded to 32 bits (IPv4 words)"); " default: 65520: maximum 802.3 MTU minus 802.3 header\n"
info( " -a, --address ADDR Assign IPv4 or IPv6 address ADDR"); " length, rounded to 32 bits (IPv4 words)\n"
info( " can be specified zero to two times (for IPv4 and IPv6)"); " -a, --address ADDR Assign IPv4 or IPv6 address ADDR\n"
info( " default: use addresses from interface with default route"); " can be specified zero to two times (for IPv4 and IPv6)\n"
info( " -n, --netmask MASK Assign IPv4 MASK, dot-decimal or bits"); " default: use addresses from interface with default route\n"
info( " default: netmask from matching address on the host"); " -n, --netmask MASK Assign IPv4 MASK, dot-decimal or bits\n"
info( " -M, --mac-addr ADDR Use source MAC address ADDR"); " default: netmask from matching address on the host\n"
info( " default: MAC address from interface with default route"); " -M, --mac-addr ADDR Use source MAC address ADDR\n"
info( " -g, --gateway ADDR Pass IPv4 or IPv6 address as gateway"); " default: MAC address from interface with default route\n"
info( " default: gateway from interface with default route"); " -g, --gateway ADDR Pass IPv4 or IPv6 address as gateway\n"
info( " -i, --interface NAME Interface for addresses and routes"); " default: gateway from interface with default route\n"
info( " default: from --outbound-if4 and --outbound-if6, if any"); " -i, --interface NAME Interface for addresses and routes\n"
info( " otherwise interface with first default route"); " default: from --outbound-if4 and --outbound-if6, if any\n"
info( " -o, --outbound ADDR Bind to address as outbound source"); " otherwise interface with first default route\n"
info( " can be specified zero to two times (for IPv4 and IPv6)"); " -o, --outbound ADDR Bind to address as outbound source\n"
info( " default: use source address from routing tables"); " can be specified zero to two times (for IPv4 and IPv6)\n"
info( " --outbound-if4 NAME Bind to outbound interface for IPv4"); " default: use source address from routing tables\n"
info( " default: use interface from default route"); " --outbound-if4 NAME Bind to outbound interface for IPv4\n"
info( " --outbound-if6 NAME Bind to outbound interface for IPv6"); " default: use interface from default route\n"
info( " default: use interface from default route"); " --outbound-if6 NAME Bind to outbound interface for IPv6\n"
info( " -D, --dns ADDR Use IPv4 or IPv6 address as DNS"); " default: use interface from default route\n"
info( " can be specified multiple times"); " -D, --dns ADDR Use IPv4 or IPv6 address as DNS\n"
info( " a single, empty option disables DNS information"); " can be specified multiple times\n"
" a single, empty option disables DNS information\n");
if (strstr(name, "pasta")) if (strstr(name, "pasta"))
info( " default: don't use any addresses"); fprintf(f, " default: don't use any addresses\n");
else else
info( " default: use addresses from /etc/resolv.conf"); fprintf(f, " default: use addresses from /etc/resolv.conf\n");
fprintf(f,
info( " -S, --search LIST Space-separated list, search domains"); " -S, --search LIST Space-separated list, search domains\n"
info( " a single, empty option disables the DNS search list"); " a single, empty option disables the DNS search list\n");
if (strstr(name, "pasta")) if (strstr(name, "pasta"))
info( " default: don't use any search list"); fprintf(f, " default: don't use any search list\n");
else else
info( " default: use search list from /etc/resolv.conf"); fprintf(f, " default: use search list from /etc/resolv.conf\n");
if (strstr(name, "pasta")) if (strstr(name, "pasta"))
info(" --dhcp-dns \tPass DNS list via DHCP/DHCPv6/NDP"); fprintf(f, " --dhcp-dns \tPass DNS list via DHCP/DHCPv6/NDP\n");
else else
info(" --no-dhcp-dns No DNS list in DHCP/DHCPv6/NDP"); fprintf(f, " --no-dhcp-dns No DNS list in DHCP/DHCPv6/NDP\n");
if (strstr(name, "pasta")) if (strstr(name, "pasta"))
info(" --dhcp-search Pass list via DHCP/DHCPv6/NDP"); fprintf(f, " --dhcp-search Pass list via DHCP/DHCPv6/NDP\n");
else else
info(" --no-dhcp-search No list in DHCP/DHCPv6/NDP"); fprintf(f, " --no-dhcp-search No list in DHCP/DHCPv6/NDP\n");
info( " --dns-forward ADDR Forward DNS queries sent to ADDR"); fprintf(f,
info( " can be specified zero to two times (for IPv4 and IPv6)"); " --dns-forward ADDR Forward DNS queries sent to ADDR\n"
info( " default: don't forward DNS queries"); " can be specified zero to two times (for IPv4 and IPv6)\n"
" default: don't forward DNS queries\n"
info( " --no-tcp Disable TCP protocol handler"); " --no-tcp Disable TCP protocol handler\n"
info( " --no-udp Disable UDP protocol handler"); " --no-udp Disable UDP protocol handler\n"
info( " --no-icmp Disable ICMP/ICMPv6 protocol handler"); " --no-icmp Disable ICMP/ICMPv6 protocol handler\n"
info( " --no-dhcp Disable DHCP server"); " --no-dhcp Disable DHCP server\n"
info( " --no-ndp Disable NDP responses"); " --no-ndp Disable NDP responses\n"
info( " --no-dhcpv6 Disable DHCPv6 server"); " --no-dhcpv6 Disable DHCPv6 server\n"
info( " --no-ra Disable router advertisements"); " --no-ra Disable router advertisements\n"
info( " --no-map-gw Don't map gateway address to host"); " --no-map-gw Don't map gateway address to host\n"
info( " -4, --ipv4-only Enable IPv4 operation only"); " -4, --ipv4-only Enable IPv4 operation only\n"
info( " -6, --ipv6-only Enable IPv6 operation only"); " -6, --ipv6-only Enable IPv6 operation only\n");
if (strstr(name, "pasta")) if (strstr(name, "pasta"))
goto pasta_opts; goto pasta_opts;
info( " -1, --one-off Quit after handling one single client"); fprintf(f,
info( " -t, --tcp-ports SPEC TCP port forwarding to guest"); " -1, --one-off Quit after handling one single client\n"
info( " can be specified multiple times"); " -t, --tcp-ports SPEC TCP port forwarding to guest\n"
info( " SPEC can be:"); " can be specified multiple times\n"
info( " 'none': don't forward any ports"); " SPEC can be:\n"
info( " 'all': forward all unbound, non-ephemeral ports"); " 'none': don't forward any ports\n"
info( " a comma-separated list, optionally ranged with '-'"); " 'all': forward all unbound, non-ephemeral ports\n"
info( " and optional target ports after ':', with optional"); " a comma-separated list, optionally ranged with '-'\n"
info( " address specification suffixed by '/' and optional"); " and optional target ports after ':', with optional\n"
info( " interface prefixed by '%%'. Ranges can be reduced by"); " address specification suffixed by '/' and optional\n"
info( " excluding ports or ranges prefixed by '~'"); " interface prefixed by '%%'. Ranges can be reduced by\n"
info( " Examples:"); " excluding ports or ranges prefixed by '~'\n"
info( " -t 22 Forward local port 22 to 22 on guest"); " Examples:\n"
info( " -t 22:23 Forward local port 22 to 23 on guest"); " -t 22 Forward local port 22 to 22 on guest\n"
info( " -t 22,25 Forward ports 22, 25 to ports 22, 25"); " -t 22:23 Forward local port 22 to 23 on guest\n"
info( " -t 22-80 Forward ports 22 to 80"); " -t 22,25 Forward ports 22, 25 to ports 22, 25\n"
info( " -t 22-80:32-90 Forward ports 22 to 80 to"); " -t 22-80 Forward ports 22 to 80\n"
info( " corresponding port numbers plus 10"); " -t 22-80:32-90 Forward ports 22 to 80 to\n"
info( " -t 192.0.2.1/5 Bind port 5 of 192.0.2.1 to guest"); " corresponding port numbers plus 10\n"
info( " -t 5-25,~10-20 Forward ports 5 to 9, and 21 to 25"); " -t 192.0.2.1/5 Bind port 5 of 192.0.2.1 to guest\n"
info( " -t ~25 Forward all ports except for 25"); " -t 5-25,~10-20 Forward ports 5 to 9, and 21 to 25\n"
info( " default: none"); " -t ~25 Forward all ports except for 25\n"
info( " -u, --udp-ports SPEC UDP port forwarding to guest"); " default: none\n"
info( " SPEC is as described for TCP above"); " -u, --udp-ports SPEC UDP port forwarding to guest\n"
info( " default: none"); " SPEC is as described for TCP above\n"
" default: none\n");
exit(status); exit(status);
pasta_opts: pasta_opts:
info( " -t, --tcp-ports SPEC TCP port forwarding to namespace"); fprintf(f,
info( " can be specified multiple times"); " -t, --tcp-ports SPEC TCP port forwarding to namespace\n"
info( " SPEC can be:"); " can be specified multiple times\n"
info( " 'none': don't forward any ports"); " SPEC can be:\n"
info( " 'auto': forward all ports currently bound in namespace"); " 'none': don't forward any ports\n"
info( " a comma-separated list, optionally ranged with '-'"); " 'auto': forward all ports currently bound in namespace\n"
info( " and optional target ports after ':', with optional"); " a comma-separated list, optionally ranged with '-'\n"
info( " address specification suffixed by '/' and optional"); " and optional target ports after ':', with optional\n"
info( " interface prefixed by '%%'. Examples:"); " address specification suffixed by '/' and optional\n"
info( " -t 22 Forward local port 22 to port 22 in netns"); " interface prefixed by '%%'. Examples:\n"
info( " -t 22:23 Forward local port 22 to port 23"); " -t 22 Forward local port 22 to port 22 in netns\n"
info( " -t 22,25 Forward ports 22, 25 to ports 22, 25"); " -t 22:23 Forward local port 22 to port 23\n"
info( " -t 22-80 Forward ports 22 to 80"); " -t 22,25 Forward ports 22, 25 to ports 22, 25\n"
info( " -t 22-80:32-90 Forward ports 22 to 80 to"); " -t 22-80 Forward ports 22 to 80\n"
info( " corresponding port numbers plus 10"); " -t 22-80:32-90 Forward ports 22 to 80 to\n"
info( " -t 192.0.2.1/5 Bind port 5 of 192.0.2.1 to namespace"); " corresponding port numbers plus 10\n"
info( " -t 5-25,~10-20 Forward ports 5 to 9, and 21 to 25"); " -t 192.0.2.1/5 Bind port 5 of 192.0.2.1 to namespace\n"
info( " -t ~25 Forward all bound ports except for 25"); " -t 5-25,~10-20 Forward ports 5 to 9, and 21 to 25\n"
info( " default: auto"); " -t ~25 Forward all bound ports except for 25\n"
info( " IPv6 bound ports are also forwarded for IPv4"); " default: auto\n"
info( " -u, --udp-ports SPEC UDP port forwarding to namespace"); " IPv6 bound ports are also forwarded for IPv4\n"
info( " SPEC is as described for TCP above"); " -u, --udp-ports SPEC UDP port forwarding to namespace\n"
info( " default: auto"); " SPEC is as described for TCP above\n"
info( " IPv6 bound ports are also forwarded for IPv4"); " default: auto\n"
info( " unless specified, with '-t auto', UDP ports with numbers"); " IPv6 bound ports are also forwarded for IPv4\n"
info( " corresponding to forwarded TCP port numbers are"); " unless specified, with '-t auto', UDP ports with numbers\n"
info( " forwarded too"); " corresponding to forwarded TCP port numbers are\n"
info( " -T, --tcp-ns SPEC TCP port forwarding to init namespace"); " forwarded too\n"
info( " SPEC is as described above"); " -T, --tcp-ns SPEC TCP port forwarding to init namespace\n"
info( " default: auto"); " SPEC is as described above\n"
info( " -U, --udp-ns SPEC UDP port forwarding to init namespace"); " default: auto\n"
info( " SPEC is as described above"); " -U, --udp-ns SPEC UDP port forwarding to init namespace\n"
info( " default: auto"); " SPEC is as described above\n"
info( " --userns NSPATH Target user namespace to join"); " default: auto\n"
info( " --netns PATH|NAME Target network namespace to join"); " --userns NSPATH Target user namespace to join\n"
info( " --netns-only Don't join existing user namespace"); " --netns PATH|NAME Target network namespace to join\n"
info( " implied if PATH or NAME are given without --userns"); " --netns-only Don't join existing user namespace\n"
info( " --no-netns-quit Don't quit if filesystem-bound target"); " implied if PATH or NAME are given without --userns\n"
info( " network namespace is deleted"); " --no-netns-quit Don't quit if filesystem-bound target\n"
info( " --config-net Configure tap interface in namespace"); " network namespace is deleted\n"
info( " --no-copy-routes DEPRECATED:"); " --config-net Configure tap interface in namespace\n"
info( " Don't copy all routes to namespace"); " --no-copy-routes DEPRECATED:\n"
info( " --no-copy-addrs DEPRECATED:"); " Don't copy all routes to namespace\n"
info( " Don't copy all addresses to namespace"); " --no-copy-addrs DEPRECATED:\n"
info( " --ns-mac-addr ADDR Set MAC address on tap interface"); " Don't copy all addresses to namespace\n"
" --ns-mac-addr ADDR Set MAC address on tap interface\n");
exit(status); exit(status);
} }
@ -1637,12 +1644,11 @@ void conf(struct ctx *c, int argc, char **argv)
/* Handle these later, once addresses are configured */ /* Handle these later, once addresses are configured */
break; break;
case 'h': case 'h':
log_to_stdout = 1; usage(argv[0], stdout, EXIT_SUCCESS);
usage(argv[0], EXIT_SUCCESS);
break; break;
case '?': case '?':
default: default:
usage(argv[0], EXIT_FAILURE); usage(argv[0], stderr, EXIT_FAILURE);
break; break;
} }
} while (name != -1); } while (name != -1);