From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 2B4B35A026D for ; Mon, 5 Jun 2023 11:31:32 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4QZSzK6fD0z4x4B; Mon, 5 Jun 2023 19:31:25 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1685957485; bh=ZTmBTUxWO7JJ0wQL2G43ECg3sfKrsNM5c4b/N7WTm84=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Y2sF0gos5NWygKy80ljxANEM50QvwXE25ITQUNEn/t1Y5Xx/dH8Yp4jHG6j8N51p2 +zqKHdadqtlRSYFwfvJgX4NkMOrcpMii4g4xmQoslVnkngP3vN+eDpE1gfTmEBsUPE 8wvSq2uPB5coUnNLuEHWuR2gNgMwUB2jKBhb44kQ= Date: Mon, 5 Jun 2023 17:00:46 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v2] conf, log: On -h / --help, print usage to stdout, not stderr Message-ID: References: <20230605051950.685102-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="C9IrwZ9cOZkYr7Q+" Content-Disposition: inline In-Reply-To: <20230605051950.685102-1-sbrivio@redhat.com> Message-ID-Hash: AY3QWYENQDRJQHSCHEVLZIYVGBA65SAW X-Message-ID-Hash: AY3QWYENQDRJQHSCHEVLZIYVGBA65SAW X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, Erik =?iso-8859-1?Q?Sj=F6lund?= X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --C9IrwZ9cOZkYr7Q+ Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Jun 05, 2023 at 07:19:50AM +0200, Stefano Brivio wrote: > 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. >=20 > While at it: on -h, we should exit with 0. >=20 > Reported-by: Erik Sj=F6lund > Link: https://bugs.passt.top/show_bug.cgi?id=3D52 > Link: https://bugs.passt.top/show_bug.cgi?id=3D53 > Signed-off-by: Stefano Brivio > --- > v2: Exit with 0, non with 1 as also reported by Erik Still kinda nasty, and it'll still do. Reviewed-by: David Gibson >=20 > conf.c | 23 ++++++++++++++++++----- > log.c | 8 +++++--- > log.h | 1 + > 3 files changed, 24 insertions(+), 8 deletions(-) >=20 > diff --git a/conf.c b/conf.c > index ffff235..87dec50 100644 > --- a/conf.c > +++ b/conf.c > @@ -717,10 +717,11 @@ static unsigned int conf_ip6(unsigned int ifi, > } > =20 > /** > - * usage() - Print usage and exit > + * print_usage() - Print usage, exit with given status code > * @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")) { > 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( " default: none"); > =20 > - exit(EXIT_FAILURE); > + exit(status); > =20 > pasta_opts: > =20 > @@ -906,7 +907,16 @@ pasta_opts: > info( " Don't copy all addresses to namespace"); > info( " --ns-mac-addr ADDR Set MAC address on tap interface"); > =20 > - 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); > } > =20 > /** > @@ -1630,8 +1640,11 @@ void conf(struct ctx *c, int argc, char **argv) > case 'U': > /* Handle these later, once addresses are configured */ > break; > - case '?': > case 'h': > + log_to_stdout =3D 1; > + print_usage(argv[0], EXIT_SUCCESS); > + break; > + case '?': > default: > usage(argv[0]); > break; > diff --git a/log.c b/log.c > index 3a3d101..63d7801 100644 > --- a/log.c > +++ b/log.c > @@ -43,17 +43,19 @@ static char log_header[BUFSIZ]; /* File header, writt= en back on cuts */ > =20 > static time_t log_start; /* Start timestamp */ > int log_trace; /* --trace mode enabled */ > +int log_to_stdout; /* Print to stdout instead of stderr */ > =20 > #define BEFORE_DAEMON (setlogmask(0) =3D=3D LOG_MASK(LOG_EMERG)) > =20 > #define logfn(name, level) \ > void name(const char *format, ...) { \ > + FILE *out =3D log_to_stdout ? stdout : stderr; \ > struct timespec tp; \ > va_list args; \ > \ > if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file =3D=3D -1) { \ > 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_nsec / (100L * 1000)); \ > } \ > @@ -70,10 +72,10 @@ void name(const char *format, ...) { \ > if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file =3D=3D -1) || \ > (BEFORE_DAEMON && !(log_opt & LOG_PERROR))) { \ > va_start(args, format); \ > - (void)vfprintf(stderr, format, args); \ > + (void)vfprintf(out, format, args); \ > va_end(args); \ > if (format[strlen(format)] !=3D '\n') \ > - fprintf(stderr, "\n"); \ > + fprintf(out, "\n"); \ > } \ > } > =20 > diff --git a/log.h b/log.h > index 3aab29d..a17171a 100644 > --- a/log.h > +++ b/log.h > @@ -22,6 +22,7 @@ void debug(const char *format, ...); > } while (0) > =20 > extern int log_trace; > +extern int log_to_stdout; > void trace_init(int enable); > #define trace(...) \ > do { \ --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --C9IrwZ9cOZkYr7Q+ Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmR9iBUACgkQzQJF27ox 2GfyGhAAlQPJ/FqLPnNK3AJdwIE+anhJYWYvrBqESLUBwtFYAWYL2IUQcohtfTiP w2vmI520EMfadUDXxrwS1V8te4NKbYme3uPVN5/VSi2/HFQSq3EBppE3AXYDVRfv k4CdPD/00bn8sTKJzAgpJBaht2bIaT3DYWuH0yPbDG4YhrPjbEbdEmeEsDFqxbIp Mcuo9dXwvnr38boYHM/5KeEQEtTX6ZS3UFwL+5Qs7rfV8N/e8t7Ioky6xF445BAf UDiKDiTog8kJa3hjzFKjI4YmHU2jBw9olE6cr9g/GAhowpVcWh31cUa56+UdD3kG fMugThsngF6Oh12xH7DTJ9fDYXOs7g7PRv6JG/Zxi6O4GY3FL4w4uiJCBIOTHPt1 cWDzNnmheUXlIEHRGsbCNbl1E6f1CsPHTAdx5tmJczxATlIlWZlktEATza/1oKTy kLOHaZHMdlxhcWsHr7xk73Oh8kWGzULfqiykqlqZ+ltfTpQrLWICrKSQ2WaXxdyH wf2mHY5x5PAwg6JvcdNX0s1JHI1C/QvlOSSby37BdMWWlS+LNn5uxhCYFJo+wPXO BBYNyp8NM5RyYHpIL043WgPuXa7KOUQfOj6sHMuy0K91TCqDGB1KFRRTCll4LTz2 EpwqJRTaBlaUF6znnXctQjJp21iJavL6GiCjEh/ML7rzmII8yiU= =igRD -----END PGP SIGNATURE----- --C9IrwZ9cOZkYr7Q+--