public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Grant Edwards <grant.b.edwards@gmail.com>
To: newlib@sourceware.org
Subject: Re: When/how is global "errno" variable from reent.c to be used?
Date: Fri, 16 Oct 2020 21:49:37 -0000 (UTC)	[thread overview]
Message-ID: <rmd4hh$16du$1@ciao.gmane.io> (raw)
In-Reply-To: <766f7b8d-6e68-9c33-a3bc-c7bbd868d0f3@Damon-Family.org>

On 2020-10-16, Richard Damon <Richard@Damon-Family.org> 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"
<whatever>_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 <<read>>.  It
    	takes a pointer to the global data block, which holds
    	<<errno>>.
    */
    
    _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.





      reply	other threads:[~2020-10-16 21:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-16 15:50 Grant Edwards
2020-10-16 16:38 ` Richard Damon
2020-10-16 18:10   ` Grant Edwards
2020-10-16 19:06     ` Richard Damon
2020-10-16 21:49       ` Grant Edwards [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='rmd4hh$16du$1@ciao.gmane.io' \
    --to=grant.b.edwards@gmail.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).