From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ciao.gmane.io (static.214.254.202.116.clients.your-server.de [116.202.254.214]) by sourceware.org (Postfix) with ESMTPS id 89EB63857C6C for ; Fri, 16 Oct 2020 21:49:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 89EB63857C6C Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1kTXbZ-000AMZ-Ts for newlib@sourceware.org; Fri, 16 Oct 2020 23:49:41 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: newlib@sourceware.org From: Grant Edwards Subject: Re: When/how is global "errno" variable from reent.c to be used? Date: Fri, 16 Oct 2020 21:49:37 -0000 (UTC) Message-ID: References: <766f7b8d-6e68-9c33-a3bc-c7bbd868d0f3@Damon-Family.org> User-Agent: slrn/1.0.3 (Linux) X-Spam-Status: No, score=0.9 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, NML_ADSP_CUSTOM_MED, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Oct 2020 21:49:44 -0000 On 2020-10-16, Richard Damon wrote: > First, newlib considers itself as part of 'the implementation', as its > purpose is to provide the 'standard library' for an implementation, thus > it can do what it wants here (as long as the net result meets the > specifications). Right. I didn't mean to imply that newlib having a globally visible integer variable named 'errno' was wrong. I was just asking what it was for. > I would have to dig into the actual implementation details to tell > for sure, but first check if when the statement int errno; is > compiled, is errno currently a macro, and after replacing that macro > with its definition what the statement actually defines, it might be > creating the pointer that is used to access the effective errno. Sorry, I don't really understand that sentence. The file newlib/libc/reent/reent.c defines a globally visible integer variable named 'errno'. I was asking what that globally visible 'int errno' was for. I've done some additional code browsing, and it appears to be used by other files in newlib/libc/reent/ to provide fake "reentrant" _r() versions of system calls for which there aren't actual _r() versions. Except I don't see how those _r() functions could actaully _be_ rentrant, since they all use a single shared, global, errno variable. The description/comments claiming they are rentrant seems to be deceptive. For example from readr.c (edited for time and to fit your screen): /* Reentrant versions of read system call. */ [...] /* We use the errno variable used by the system dependent layer. */ #undef errno extern int errno; /* [...] DESCRIPTION This is a reentrant version of <>. It takes a pointer to the global data block, which holds <>. */ _ssize_t _read_r (struct _reent *ptr, int fd, void *buf, size_t cnt) { _ssize_t ret; errno = 0; if ((ret = (_ssize_t)_read (fd, buf, cnt)) == -1 && errno != 0) ptr->_errno = errno; return ret; } I just don't see how that function can be called reentrant. > It could also be creating a global variable just to limit the breakage > that programs that do it wrong incur I don't think so. It appears to actually be used when making calls to the system-dependent layer. > (though, I might prefer getting the link error errno not defined > rather than code that ends up checking the wrong errno). Agreed.