errno handling in nonreentrant syscalls (fwd)

Dmitry Timoshkov
Fri May 25 22:13:00 GMT 2001

"J. Johnston" <> wrote:

> You're right.  It's a bit of a mess caused by not being consistent in the past.  The system calls
> are not supplied by newlib and are either in libgloss, provided in a bsp library, or the OS
> them.  A number of libgloss implementations simply set the external errno value in a common trap
> routine.  Some implementations use some C code which include errno.h and set errno that way.
> Already you can see there is a discrepancy in how newlib should handle this.
> I think the first step is to create a new flag that states whether the non-reentrant system calls
> use an external errno or the C library errno from errno.h.  This allows us to dual-path the code
> at least makes it possible to hook up to which ever system libgloss is using.
> As well, it makes sense to go through the library and change all direct errno references to use
> __errno_r() instead of ptr->_errno.  Currently, __errno_r() is a macro which should be changed to
> a function in libc/errno/errno.c.  This allows us to override the function when necessary and
> specify where the errno values come from.

Do you plan to do the described above things for the next release?

The following sample program should work even with the current errno handling structure,
but it doesn't. Something wrong with it? How do you plan to make the user defined __errno()
visible for newlib?

static int myerrno = 0;

int *__errno(void)
    return &myerrno;

int main(void)
    char buf[256];
    int ret;
    /* provoke an error */
    ret = read(-1, buf, sizeof(buf));
    printf("ret=%d, myerrno=%d\n", ret, myerrno);
    return 0;

More information about the Newlib mailing list