From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12084 Path: news.gmane.org!.POSTED!not-for-mail From: Bobby Bingham Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] save/restore errno around pthread_atfork handlers Date: Fri, 10 Nov 2017 18:37:19 -0600 Message-ID: <20171111003719.GB22903@dora.lan> References: <20171110205829.18319-1-koorogi@koorogi.info> <20171110233134.GR1627@brightrain.aerifal.cx> <20171111000340.GA22903@dora.lan> <20171111001627.GS1627@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 X-Trace: blaine.gmane.org 1510360658 2762 195.159.176.226 (11 Nov 2017 00:37:38 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 11 Nov 2017 00:37:38 +0000 (UTC) User-Agent: Mutt/1.9.1 (2017-09-22) To: musl@lists.openwall.com Original-X-From: musl-return-12100-gllmg-musl=m.gmane.org@lists.openwall.com Sat Nov 11 01:37:30 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1eDJnc-0000JQ-Qs for gllmg-musl@m.gmane.org; Sat, 11 Nov 2017 01:37:28 +0100 Original-Received: (qmail 11456 invoked by uid 550); 11 Nov 2017 00:37:33 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 11428 invoked from network); 11 Nov 2017 00:37:32 -0000 Content-Disposition: inline In-Reply-To: <20171111001627.GS1627@brightrain.aerifal.cx> Xref: news.gmane.org gmane.linux.lib.musl.general:12084 Archived-At: On Fri, Nov 10, 2017 at 07:16:27PM -0500, Rich Felker wrote: > On Fri, Nov 10, 2017 at 06:03:40PM -0600, Bobby Bingham wrote: > > > I think the patch as written is incorrect, because it can set errno to > > > 0 after application code in the atfork handler set it to something > > > nonzero; doing so is non-conforming. > > > > Good point. It does make me wonder though: when libc invokes a callback > > and that callback sets errno to zero, is that a violation of the > > prohibition on library functions setting errno to zero? > > No, that's application code setting it to 0. The case I'm talking > about is when errno is 0 before fork is called, 0 gets stored in > olderr, the atfork handler sets errno to some nonzero value, and then > the implementation wrongly sets it back to 0. That's observable by the > application. I understood the case you were talking about. It just made me wonder about this other case as well. While callbacks are application code, they may not always be known to the code calling the library function. pthread_atfork is a perfect example -- libraries like libressl may use it behind the back of the main application. If they set errno to zero in their atfork handlers, it may _look_ to the main application like fork set errno to zero. I'm also trying to understand the rationale for library functions being barred from setting errno to zero. Right where C11 section 7.5 says errno is never set to zero by any library function, it has a footnote: Thus, a program that uses errno for error checking should set it to zero before a library function call, then inspect it before a subsequent library function call. This seems to imply that the purpose of the prohibition is so that application code can set errno to zero, call a function, and then look at errno to determine if an error occurred. Possibly call multiple functions and do error checking all at the end, like the ferror status in stdio. But of course, the standard also says "the value of errno may be set to nonzero by a library function call whether or not there is an error". So this doesn't work, and I'm left not understanding why the prohibition exists at all. There are a few specific functions where it's important for application code to set errno to zero first, like strtol and family, but otherwise I'm not sure why this footnote encourages applications to set errno to zero. Bobby