This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: syscalls, libgloss, errno


Vincent Rivière wrote:
Jeff, many thanks for your answer !

But there are still things I can't understand.

Let's talk about a very simple configuration. The system is single threaded, so there are no reentrancy issues. The libgloss implementation is the standard libnosys, which returns ENOSYS in every function call.
I am in the configuration #2 from reent.h :
MISSING_SYSCALL_NAMES is not defined
and syscall_dir=syscalls
REENTRANT_SYSCALLS_PROVIDED is not defined


I want to :
- #include <errno.h>
- call fopen() and find ENOSYS in errno
- call open() and find ENOSYS in errno

1) The case of fopen()
fopen() will call _open_r().
_open_r() is implemented in libc/reent/openr.c and calls _open()
_open() is implemented in libgloss/libnosys/open.c

2) The case of open()
open() is implemented in syscalls/sysopen.c and calls _open()
_open() is implemented in libgloss/libnosys/open.c

So in every case, we end up to _open() in libgloss. Good.

Now, let's talk about errno.

_open(), in libnosys, returns the error code in "extern int errno".
This is not a problem because we are on a single threaded environment.

open() just calls _open().
So open() returns the error code in "extern int errno".

_open_r() calls _open(), and copies the error code from "extern int errno" to __errno_r(_REENT). That's good.
So _open_r() returns the error code in __errno_r(_REENT).


fopen() calls _open_r(), and doesn't mess errno.
So fopen() returns the error code in __errno_r(_REENT).

The application #include <errno.h> and uses errno.
In errno.h: #define errno (*__errno())
In errno.c: int * __errno () { return &_REENT->_errno; }
So the application finds the error code in __errno_r(_REENT).

Finally :
When the application calls fopen(), it finds the correct error code in errno.
But when it calls open(), it will not find the error code in errno (which is actually __errno_r(_REENT)), because open() returns the error code into "extern int errno" !


The problem is in the implementation of open() and other syscalls in libc/syscalls/*
If REENTRANT_SYSCALLS_PROVIDED is defined (which is not the case here), open() calls _open_r() and the error is returned into __errno_r(_REENT)) as expected.
But if REENTRANT_SYSCALLS_PROVIDED is not defined (the case here), it should call _open() and translate the error from "extern int errno" to __errno_r(_REENT), just like the implementation of _open_r() does.
Another solution to implement open() would be to unconditionally call _open_r(), just like fopen().


Conclusion :
Either I misunderstood something, either there is a problem in syscalls/sys*.c. In the latter case, I provided 2 simple solutions.

You are correct. The libc/reent directory provides default implementations of the _r syscalls if the platform doesn't supply them with the REENTRANT_SYSCALLS_PROVIDED. The syscalls directory should call these unconditionally.

I have attached a patch for this. If there are no problems, I'll check it in.

-- Jeff J.
Index: sysclose.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysclose.c,v
retrieving revision 1.2
diff -u -p -r1.2 sysclose.c
--- sysclose.c	3 Jun 2003 19:48:08 -0000	1.2
+++ sysclose.c	2 Jan 2008 22:20:28 -0000
@@ -6,9 +6,5 @@ int
 _DEFUN (close, (fd),
      int fd)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _close_r (_REENT, fd);
-#else
-  return _close (fd);
-#endif
 }
Index: sysexecve.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysexecve.c,v
retrieving revision 1.2
diff -u -p -r1.2 sysexecve.c
--- sysexecve.c	3 Jun 2003 19:48:08 -0000	1.2
+++ sysexecve.c	2 Jan 2008 22:20:28 -0000
@@ -8,9 +8,5 @@ _DEFUN (execve, (name, argv, env),
      char **argv _AND
      char **env)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _execve_r (_REENT, name, argv, env);
-#else
-  return _execve (name, argv, env);
-#endif
 }
Index: sysfcntl.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysfcntl.c,v
retrieving revision 1.4
diff -u -p -r1.4 sysfcntl.c
--- sysfcntl.c	3 Jun 2003 19:48:08 -0000	1.4
+++ sysfcntl.c	2 Jan 2008 22:20:28 -0000
@@ -11,11 +11,7 @@ _DEFUN (fcntl, (fd, flag, arg),
      int arg)
 {
 #ifdef HAVE_FCNTL
-# ifdef REENTRANT_SYSCALLS_PROVIDED
   return _fcntl_r (_REENT, fd, flag, arg);
-# else
-  return _fcntl (fd, flag, arg);
-# endif
 #else /* !HAVE_FCNTL */
   errno = ENOSYS;
   return -1;
Index: sysfork.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysfork.c,v
retrieving revision 1.2
diff -u -p -r1.2 sysfork.c
--- sysfork.c	3 Jun 2003 19:48:08 -0000	1.2
+++ sysfork.c	2 Jan 2008 22:20:28 -0000
@@ -9,11 +9,7 @@
 int
 _DEFUN_VOID (fork)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _fork_r (_REENT);
-#else
-  return _fork ();
-#endif
 }
 
 #endif
Index: sysfstat.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysfstat.c,v
retrieving revision 1.2
diff -u -p -r1.2 sysfstat.c
--- sysfstat.c	3 Jun 2003 19:48:08 -0000	1.2
+++ sysfstat.c	2 Jan 2008 22:20:28 -0000
@@ -8,9 +8,5 @@ _DEFUN (fstat, (fd, pstat),
      int fd _AND
      struct stat *pstat)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _fstat_r (_REENT, fd, pstat);
-#else
-  return _fstat (fd, pstat);
-#endif
 }
Index: sysgetpid.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysgetpid.c,v
retrieving revision 1.2
diff -u -p -r1.2 sysgetpid.c
--- sysgetpid.c	3 Jun 2003 19:48:08 -0000	1.2
+++ sysgetpid.c	2 Jan 2008 22:20:28 -0000
@@ -5,9 +5,5 @@
 int
 _DEFUN_VOID (getpid)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _getpid_r (_REENT);
-#else
-  return _getpid ();
-#endif
 }
Index: sysgettod.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysgettod.c,v
retrieving revision 1.2
diff -u -p -r1.2 sysgettod.c
--- sysgettod.c	3 Jun 2003 19:48:08 -0000	1.2
+++ sysgettod.c	2 Jan 2008 22:20:28 -0000
@@ -12,9 +12,5 @@ _DEFUN (gettimeofday, (ptimeval, ptimezo
      struct timeval *ptimeval _AND
      struct timezone *ptimezone)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _gettimeofday_r (_REENT, ptimeval, ptimezone);
-#else
-  return _gettimeofday (ptimeval, ptimezone);
-#endif
 }
Index: syskill.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/syskill.c,v
retrieving revision 1.2
diff -u -p -r1.2 syskill.c
--- syskill.c	3 Jun 2003 19:48:08 -0000	1.2
+++ syskill.c	2 Jan 2008 22:20:28 -0000
@@ -7,9 +7,5 @@ _DEFUN (kill, (pid, sig),
      int pid _AND
      int sig)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _kill_r (_REENT, pid, sig);
-#else
-  return _kill (pid, sig);
-#endif
 }
Index: syslink.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/syslink.c,v
retrieving revision 1.2
diff -u -p -r1.2 syslink.c
--- syslink.c	3 Jun 2003 19:48:08 -0000	1.2
+++ syslink.c	2 Jan 2008 22:20:28 -0000
@@ -7,9 +7,5 @@ _DEFUN (link, (old, new),
      char *old _AND
      char *new)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _link_r (_REENT, old, new);
-#else
-  return _link (old, new);
-#endif
 }
Index: syslseek.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/syslseek.c,v
retrieving revision 1.2
diff -u -p -r1.2 syslseek.c
--- syslseek.c	3 Jun 2003 19:48:08 -0000	1.2
+++ syslseek.c	2 Jan 2008 22:20:28 -0000
@@ -9,9 +9,5 @@ _DEFUN (lseek, (fd, pos, whence),
      off_t pos _AND
      int whence)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _lseek_r (_REENT, fd, pos, whence);
-#else
-  return _lseek (fd, pos, whence);
-#endif
 }
Index: sysopen.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysopen.c,v
retrieving revision 1.2
diff -u -p -r1.2 sysopen.c
--- sysopen.c	3 Jun 2003 19:48:08 -0000	1.2
+++ sysopen.c	2 Jan 2008 22:20:28 -0000
@@ -18,11 +18,7 @@ _DEFUN (open, (file, flags, ...),
   int ret;
 
   va_start (ap, flags);
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   ret = _open_r (_REENT, file, flags, va_arg (ap, int));
-#else
-  ret = _open (file, flags, va_arg (ap, int));
-#endif
   va_end (ap);
   return ret;
 }
@@ -35,11 +31,7 @@ open (file, flags, mode)
      int flags;
      int mode;
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _open_r (_REENT, file, flags, mode);
-#else
-  return _open (file, flags, mode);
-#endif
 }
 
 #endif /* ! _HAVE_STDC */
Index: sysread.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysread.c,v
retrieving revision 1.3
diff -u -p -r1.3 sysread.c
--- sysread.c	3 Jun 2003 19:48:08 -0000	1.3
+++ sysread.c	2 Jan 2008 22:20:28 -0000
@@ -9,9 +9,5 @@ _DEFUN (read, (fd, buf, cnt),
      void *buf _AND
      size_t cnt)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _read_r (_REENT, fd, buf, cnt);
-#else
-  return _read (fd, buf, cnt);
-#endif
 }
Index: syssbrk.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/syssbrk.c,v
retrieving revision 1.3
diff -u -p -r1.3 syssbrk.c
--- syssbrk.c	3 Jun 2003 19:48:08 -0000	1.3
+++ syssbrk.c	2 Jan 2008 22:20:28 -0000
@@ -10,9 +10,5 @@ void *
 _DEFUN (sbrk, (incr),
      ptrdiff_t incr)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _sbrk_r (_REENT, incr);
-#else
-  return _sbrk (incr);
-#endif
 }
Index: sysstat.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysstat.c,v
retrieving revision 1.2
diff -u -p -r1.2 sysstat.c
--- sysstat.c	3 Jun 2003 19:48:08 -0000	1.2
+++ sysstat.c	2 Jan 2008 22:20:28 -0000
@@ -8,9 +8,5 @@ _DEFUN (stat, (file, pstat),
      char *file _AND
      struct stat *pstat)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _stat_r (_REENT, file, pstat);
-#else
-  return _stat (file, pstat);
-#endif
 }
Index: systimes.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/systimes.c,v
retrieving revision 1.2
diff -u -p -r1.2 systimes.c
--- systimes.c	3 Jun 2003 19:48:08 -0000	1.2
+++ systimes.c	2 Jan 2008 22:20:28 -0000
@@ -7,9 +7,5 @@ clock_t
 _DEFUN (times, (buf),
      struct tms *buf)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _times_r (_REENT, buf);
-#else
-  return _times (buf);
-#endif
 }
Index: sysunlink.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/sysunlink.c,v
retrieving revision 1.2
diff -u -p -r1.2 sysunlink.c
--- sysunlink.c	3 Jun 2003 19:48:08 -0000	1.2
+++ sysunlink.c	2 Jan 2008 22:20:28 -0000
@@ -6,9 +6,5 @@ int
 _DEFUN (unlink, (file),
         char *file)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _unlink_r (_REENT, file);
-#else
-  return _unlink (file);
-#endif
 }
Index: syswait.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/syswait.c,v
retrieving revision 1.2
diff -u -p -r1.2 syswait.c
--- syswait.c	3 Jun 2003 19:48:08 -0000	1.2
+++ syswait.c	2 Jan 2008 22:20:28 -0000
@@ -6,9 +6,5 @@ int
 _DEFUN (wait, (status),
         int *status)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _wait_r (_REENT, status);
-#else
-  return _wait (status);
-#endif
 }
Index: syswrite.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/syscalls/syswrite.c,v
retrieving revision 1.3
diff -u -p -r1.3 syswrite.c
--- syswrite.c	3 Jun 2003 19:48:08 -0000	1.3
+++ syswrite.c	2 Jan 2008 22:20:28 -0000
@@ -9,9 +9,5 @@ _DEFUN (write, (fd, buf, cnt),
      const void *buf _AND
      size_t cnt)
 {
-#ifdef REENTRANT_SYSCALLS_PROVIDED
   return _write_r (_REENT, fd, buf, cnt);
-#else
-  return _write (fd, buf, cnt);
-#endif
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]