[PATCH 08/11] Add stdio atexit handler

Matthew Joyce matthew.joyce@embedded-brains.de
Tue May 10 08:09:24 GMT 2022


From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a dedicated stdio atexit handler to avoid using _GLOBAL_REENT in exit().
---
 newlib/libc/stdio/findfp.c | 11 ++++++++++-
 newlib/libc/stdio/local.h  |  1 +
 newlib/libc/stdlib/exit.c  |  6 ++++--
 winsup/cygwin/signal.cc    |  5 +++--
 4 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index 5b0402ac1..022a298df 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -26,6 +26,8 @@
 #include <sys/lock.h>
 #include "local.h"
 
+void (*__stdio_atexit) (void);
+
 #if defined(_REENT_SMALL) && !defined(_REENT_GLOBAL_STDIO_STREAMS)
 const struct __sFILE_fake __sf_fake_stdin =
     {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
@@ -153,6 +155,12 @@ sfmoreglue (struct _reent *d, int n)
   return &g->glue;
 }
 
+static void
+stdio_atexit (void)
+{
+  (void) _fwalk_reent (_GLOBAL_REENT, CLEANUP_FILE);
+}
+
 /*
  * Find a free FILE for fopen et al.
  */
@@ -166,12 +174,13 @@ __sfp (struct _reent *d)
 
   _newlib_sfp_lock_start ();
 
-  if (_GLOBAL_REENT->__cleanup == NULL) {
+  if (__stdio_atexit == NULL) {
 #ifdef _REENT_GLOBAL_STDIO_STREAMS
     _GLOBAL_REENT->__sglue._niobs = 3;
     _GLOBAL_REENT->__sglue._iobs = &__sf[0];
 #endif
     __sinit (_GLOBAL_REENT);
+    __stdio_atexit = stdio_atexit;
   }
 
   for (g = &_GLOBAL_REENT->__sglue;; g = g->_next)
diff --git a/newlib/libc/stdio/local.h b/newlib/libc/stdio/local.h
index 9c6f63fdb..c892f8378 100644
--- a/newlib/libc/stdio/local.h
+++ b/newlib/libc/stdio/local.h
@@ -180,6 +180,7 @@ extern _fpos_t __sseek (struct _reent *, void *, _fpos_t, int);
 extern int    __sclose (struct _reent *, void *);
 extern int    __stextmode (int);
 extern void   __sinit (struct _reent *);
+extern void (*__stdio_atexit) (void);
 extern void   __smakebuf_r (struct _reent *, FILE *);
 extern int    __swhatbuf_r (struct _reent *, FILE *, size_t *, int *);
 extern int    _fwalk_reent (struct _reent *, int (*)(struct _reent *, FILE *));
diff --git a/newlib/libc/stdlib/exit.c b/newlib/libc/stdlib/exit.c
index 3e618914e..741cf9cbe 100644
--- a/newlib/libc/stdlib/exit.c
+++ b/newlib/libc/stdlib/exit.c
@@ -44,6 +44,7 @@ Supporting OS subroutines required: <<_exit>>.
 #include <unistd.h>	/* for _exit() declaration */
 #include <reent.h>
 #include "atexit.h"
+#include "../stdio/local.h"
 
 /*
  * Exit, flushing stdio buffers if necessary.
@@ -59,7 +60,8 @@ exit (int code)
 #endif
     __call_exitprocs (code, NULL);
 
-  if (_GLOBAL_REENT->__cleanup)
-    (*_GLOBAL_REENT->__cleanup) (_GLOBAL_REENT);
+  if (__stdio_atexit != NULL)
+    (*__stdio_atexit) ();
+
   _exit (code);
 }
diff --git a/winsup/cygwin/signal.cc b/winsup/cygwin/signal.cc
index 9b6c2509d..b7f624cc3 100644
--- a/winsup/cygwin/signal.cc
+++ b/winsup/cygwin/signal.cc
@@ -22,6 +22,7 @@ details. */
 #include "cygheap.h"
 #include "cygwait.h"
 #include "posix_timer.h"
+#include "../../newlib/libc/stdio/local.h"
 
 #define _SA_NORESTART	0x8000
 
@@ -408,8 +409,8 @@ abort (void)
   _my_tls.call_signal_handler (); /* Call any signal handler */
 
   /* Flush all streams as per SUSv2.  */
-  if (_GLOBAL_REENT->__cleanup)
-    _GLOBAL_REENT->__cleanup (_GLOBAL_REENT);
+  if (__stdio_atexit != NULL)
+    (*__stdio_atexit) ();
   do_exit (SIGABRT);	/* signal handler didn't exit.  Goodbye. */
 }
 
-- 
2.31.1



More information about the Newlib mailing list