conf, log: On -h / --help, print usage to stdout, not stderr

Erik suggests that this makes it easier to grep for options, and with
--help we're anyway printing usage information as expected, not as
part of an error report.

While at it: on -h, we should exit with 0.

Reported-by: Erik Sjölund <erik.sjolund@gmail.com>
Link: https://bugs.passt.top/show_bug.cgi?id=52
Link: https://bugs.passt.top/show_bug.cgi?id=53
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
Stefano Brivio 2023-06-04 07:14:49 +02:00
parent d072ac2434
commit 3c6d1b9bb2
3 changed files with 24 additions and 8 deletions

23
conf.c
View file

@ -717,10 +717,11 @@ static unsigned int conf_ip6(unsigned int ifi,
} }
/** /**
* usage() - Print usage and exit * print_usage() - Print usage, exit with given status code
* @name: Executable name * @name: Executable name
* @status: Status code for exit()
*/ */
static void usage(const char *name) static void print_usage(const char *name, int status)
{ {
if (strstr(name, "pasta")) { if (strstr(name, "pasta")) {
info("Usage: %s [OPTION]... [COMMAND] [ARGS]...", name); info("Usage: %s [OPTION]... [COMMAND] [ARGS]...", name);
@ -856,7 +857,7 @@ static void usage(const char *name)
info( " SPEC is as described for TCP above"); info( " SPEC is as described for TCP above");
info( " default: none"); info( " default: none");
exit(EXIT_FAILURE); exit(status);
pasta_opts: pasta_opts:
@ -906,7 +907,16 @@ pasta_opts:
info( " Don't copy all addresses to namespace"); info( " Don't copy all addresses to namespace");
info( " --ns-mac-addr ADDR Set MAC address on tap interface"); info( " --ns-mac-addr ADDR Set MAC address on tap interface");
exit(EXIT_FAILURE); exit(status);
}
/**
* usage() - Print usage and exit with failure
* @name: Executable name
*/
static void usage(const char *name)
{
print_usage(name, EXIT_FAILURE);
} }
/** /**
@ -1630,8 +1640,11 @@ void conf(struct ctx *c, int argc, char **argv)
case 'U': case 'U':
/* Handle these later, once addresses are configured */ /* Handle these later, once addresses are configured */
break; break;
case '?':
case 'h': case 'h':
log_to_stdout = 1;
print_usage(argv[0], EXIT_SUCCESS);
break;
case '?':
default: default:
usage(argv[0]); usage(argv[0]);
break; break;

8
log.c
View file

@ -43,17 +43,19 @@ static char log_header[BUFSIZ]; /* File header, written back on cuts */
static time_t log_start; /* Start timestamp */ static time_t log_start; /* Start timestamp */
int log_trace; /* --trace mode enabled */ int log_trace; /* --trace mode enabled */
int log_to_stdout; /* Print to stdout instead of stderr */
#define BEFORE_DAEMON (setlogmask(0) == LOG_MASK(LOG_EMERG)) #define BEFORE_DAEMON (setlogmask(0) == LOG_MASK(LOG_EMERG))
#define logfn(name, level) \ #define logfn(name, level) \
void name(const char *format, ...) { \ void name(const char *format, ...) { \
FILE *out = log_to_stdout ? stdout : stderr; \
struct timespec tp; \ struct timespec tp; \
va_list args; \ va_list args; \
\ \
if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) { \ if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) { \
clock_gettime(CLOCK_REALTIME, &tp); \ clock_gettime(CLOCK_REALTIME, &tp); \
fprintf(stderr, "%lli.%04lli: ", \ fprintf(out, "%lli.%04lli: ", \
(long long int)tp.tv_sec - log_start, \ (long long int)tp.tv_sec - log_start, \
(long long int)tp.tv_nsec / (100L * 1000)); \ (long long int)tp.tv_nsec / (100L * 1000)); \
} \ } \
@ -70,10 +72,10 @@ void name(const char *format, ...) { \
if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) || \ if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) || \
(BEFORE_DAEMON && !(log_opt & LOG_PERROR))) { \ (BEFORE_DAEMON && !(log_opt & LOG_PERROR))) { \
va_start(args, format); \ va_start(args, format); \
(void)vfprintf(stderr, format, args); \ (void)vfprintf(out, format, args); \
va_end(args); \ va_end(args); \
if (format[strlen(format)] != '\n') \ if (format[strlen(format)] != '\n') \
fprintf(stderr, "\n"); \ fprintf(out, "\n"); \
} \ } \
} }

1
log.h
View file

@ -22,6 +22,7 @@ void debug(const char *format, ...);
} while (0) } while (0)
extern int log_trace; extern int log_trace;
extern int log_to_stdout;
void trace_init(int enable); void trace_init(int enable);
#define trace(...) \ #define trace(...) \
do { \ do { \