From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id DCDD23858D34 for ; Sun, 12 Sep 2021 21:00:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DCDD23858D34 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x0b.wildebeest.org [172.31.17.141]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 57D0930006CF; Sun, 12 Sep 2021 23:00:36 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id 03BC62E81595; Sun, 12 Sep 2021 23:00:35 +0200 (CEST) Date: Sun, 12 Sep 2021 23:00:35 +0200 From: Mark Wielaard To: Colin Cross Cc: elfutils-devel@sourceware.org Subject: Re: [PATCH] lib: Make error.c more like error(3) Message-ID: References: <6098de6222cc54410231586581d08cf442e00f56.camel@klomp.org> <20210910180716.1464674-1-ccross@google.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2aKFrxYStXZI17MW" Content-Disposition: inline In-Reply-To: <20210910180716.1464674-1-ccross@google.com> X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 12 Sep 2021 21:00:39 -0000 --2aKFrxYStXZI17MW Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi Colin, On Fri, Sep 10, 2021 at 11:07:16AM -0700, Colin Cross via Elfutils-devel wrote: > Fix some issues with the error reimplementation to make it match > the specification for error(3). > > Flush stdout before printing to stderr. Also flush stderr afterwards, > which is not specified in the man page for error(3), but is what > bionic does. > > error(3) prints strerror(errnum) if and only if errnum is nonzero, > but verr prints strerror(errno) unconditionaly. When errnum is nonzero > copy it to errno and use verr, and when it is not set use verrx that > doesn't print errno. > > error(3) only exits if status is nonzero, but verr exits uncondtionally. > Use vwarn/vwarnx when status is zero, which don't exit. > > Signed-off-by: Colin Cross This is really nice. Thanks. I pushed it with a ChangeLog entry and reformatted the patch to GNU style as attached. Note that there are still some small differences between "real" error and our error implementation, which don't seem to matter in practice, but do cause some testsuite failures because of silly formatting. e.g. run-stack-i.sh fails with: -stack: tid 13654: shown max number of frames (6, use -n 0 for unlimited) +/home/mark/src/elfutils/src/stack: tid 13654: shown max number of frames (6, use -n 0 for unlimited) I have not tries to fix these. The testcases probably should be slightly less picky about the exact error message format. Cheers, Mark --2aKFrxYStXZI17MW Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-lib-Make-error.c-more-like-error-3.patch" >From 7582a0d3e09ee154961bbba9285a224e5d09f407 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Fri, 10 Sep 2021 11:07:16 -0700 Subject: [PATCH] lib: Make error.c more like error(3) Fix some issues with the error reimplementation to make it match the specification for error(3). Flush stdout before printing to stderr. Also flush stderr afterwards, which is not specified in the man page for error(3), but is what bionic does. error(3) prints strerror(errnum) if and only if errnum is nonzero, but verr prints strerror(errno) unconditionaly. When errnum is nonzero copy it to errno and use verr, and when it is not set use verrx that doesn't print errno. error(3) only exits if status is nonzero, but verr exits uncondtionally. Use vwarn/vwarnx when status is zero, which don't exit. Signed-off-by: Colin Cross --- lib/ChangeLog | 5 +++++ lib/error.c | 32 +++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index 96eaa330..c72452b1 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,8 @@ +2021-09-10 Colin Cross + + * error.c (error): Call fflush on stdout and stderr. Setup errno and + call verr, verrx, vwarn or vwarnx based on status and errnum. + 2021-09-06 Dmitry V. Levin * color.c (parse_opt): Replace asprintf followed by error(EXIT_FAILURE) diff --git a/lib/error.c b/lib/error.c index 75e964fd..5186fc15 100644 --- a/lib/error.c +++ b/lib/error.c @@ -29,7 +29,9 @@ #include #if !defined(HAVE_ERROR_H) && defined(HAVE_ERR_H) +#include #include +#include #include #include @@ -37,13 +39,37 @@ unsigned int error_message_count = 0; void error(int status, int errnum, const char *format, ...) { va_list argp; + int saved_errno = errno; + + fflush (stdout); va_start(argp, format); - verr(status, format, argp); + if (status) + { + if (errnum) + { + errno = errnum; + verr (status, format, argp); + } + else + verrx (status, format, argp); + } + else + { + if (errnum) + { + errno = errnum; + vwarn (format, argp); + } + else + vwarnx (format, argp); + } va_end(argp); - if (status) - exit(status); + fflush (stderr); + ++error_message_count; + + errno = saved_errno; } #endif -- 2.32.0 --2aKFrxYStXZI17MW--