From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dedi548.your-server.de (dedi548.your-server.de [85.10.215.148]) by sourceware.org (Postfix) with ESMTPS id A08FA383D81D for ; Thu, 12 May 2022 12:11:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A08FA383D81D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embedded-brains.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embedded-brains.de Received: from sslproxy06.your-server.de ([78.46.172.3]) by dedi548.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1np7fa-0001O9-7O for newlib@sourceware.org; Thu, 12 May 2022 14:11:50 +0200 Received: from [82.100.198.138] (helo=mail.embedded-brains.de) by sslproxy06.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1np7fa-0007CN-Bi for newlib@sourceware.org; Thu, 12 May 2022 14:11:50 +0200 Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 11DEC48016C for ; Thu, 12 May 2022 14:11:50 +0200 (CEST) Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id MiuD6MmVKlKc; Thu, 12 May 2022 14:11:49 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 791F0480183; Thu, 12 May 2022 14:11:49 +0200 (CEST) X-Virus-Scanned: amavisd-new at zimbra.eb.localhost Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id PMOAVj0aKDmw; Thu, 12 May 2022 14:11:49 +0200 (CEST) Received: from joyce-tux.eb.localhost (unknown [10.10.171.30]) by mail.embedded-brains.de (Postfix) with ESMTPSA id 46F43480141; Thu, 12 May 2022 14:11:49 +0200 (CEST) From: Matthew Joyce To: newlib@sourceware.org Subject: [PATCH v2 08/11] Add stdio_exit_handler() Date: Thu, 12 May 2022 14:11:40 +0200 Message-Id: <20220512121143.21473-9-matthew.joyce@embedded-brains.de> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220512121143.21473-1-matthew.joyce@embedded-brains.de> References: <20220512121143.21473-1-matthew.joyce@embedded-brains.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Authenticated-Sender: smtp-embedded@poldinet.de X-Virus-Scanned: Clear (ClamAV 0.103.5/26539/Thu May 12 10:04:41 2022) X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Thu, 12 May 2022 12:11:56 -0000 From: Matt Joyce Added a dedicated stdio exit handler to avoid using _GLOBAL_REENT in exit(). --- newlib/libc/include/sys/reent.h | 2 ++ newlib/libc/stdio/findfp.c | 11 ++++++++++- newlib/libc/stdlib/exit.c | 5 +++-- winsup/cygwin/signal.cc | 5 +++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/re= ent.h index 4b0d68610..067294dc7 100644 --- a/newlib/libc/include/sys/reent.h +++ b/newlib/libc/include/sys/reent.h @@ -837,6 +837,8 @@ extern struct _reent *_impure_ptr __ATTRIBUTE_IMPURE_= PTR__; =20 extern struct _reent _impure_data __ATTRIBUTE_IMPURE_DATA__; =20 +extern void (*__stdio_exit_handler) (void); + void _reclaim_reent (struct _reent *); =20 /* #define _REENT_ONLY define this to get only reentrant routines */ diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c index 5b0402ac1..719886e27 100644 --- a/newlib/libc/stdio/findfp.c +++ b/newlib/libc/stdio/findfp.c @@ -26,6 +26,8 @@ #include #include "local.h" =20 +void (*__stdio_exit_handler) (void); + #if defined(_REENT_SMALL) && !defined(_REENT_GLOBAL_STDIO_STREAMS) const struct __sFILE_fake __sf_fake_stdin =3D {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL}; @@ -153,6 +155,12 @@ sfmoreglue (struct _reent *d, int n) return &g->glue; } =20 +static void +stdio_exit_handler (void) +{ + (void) _fwalk_reent (_GLOBAL_REENT, CLEANUP_FILE); +} + /* * Find a free FILE for fopen et al. */ @@ -166,12 +174,13 @@ __sfp (struct _reent *d) =20 _newlib_sfp_lock_start (); =20 - if (_GLOBAL_REENT->__cleanup =3D=3D NULL) { + if (__stdio_exit_handler =3D=3D NULL) { #ifdef _REENT_GLOBAL_STDIO_STREAMS _GLOBAL_REENT->__sglue._niobs =3D 3; _GLOBAL_REENT->__sglue._iobs =3D &__sf[0]; #endif __sinit (_GLOBAL_REENT); + __stdio_exit_handler =3D stdio_exit_handler; } =20 for (g =3D &_GLOBAL_REENT->__sglue;; g =3D g->_next) diff --git a/newlib/libc/stdlib/exit.c b/newlib/libc/stdlib/exit.c index 3e618914e..9b7bd518b 100644 --- a/newlib/libc/stdlib/exit.c +++ b/newlib/libc/stdlib/exit.c @@ -59,7 +59,8 @@ exit (int code) #endif __call_exitprocs (code, NULL); =20 - if (_GLOBAL_REENT->__cleanup) - (*_GLOBAL_REENT->__cleanup) (_GLOBAL_REENT); + if (__stdio_exit_handler !=3D NULL) + (*__stdio_exit_handler) (); + _exit (code); } diff --git a/winsup/cygwin/signal.cc b/winsup/cygwin/signal.cc index 9b6c2509d..f5f2efdc7 100644 --- a/winsup/cygwin/signal.cc +++ b/winsup/cygwin/signal.cc @@ -13,6 +13,7 @@ details. */ #include #include #include +#include /* needed for __stdio_exit_handler declaration */ #include "pinfo.h" #include "sigproc.h" #include "cygtls.h" @@ -408,8 +409,8 @@ abort (void) _my_tls.call_signal_handler (); /* Call any signal handler */ =20 /* Flush all streams as per SUSv2. */ - if (_GLOBAL_REENT->__cleanup) - _GLOBAL_REENT->__cleanup (_GLOBAL_REENT); + if (__stdio_exit_handler !=3D NULL) + (*__stdio_exit_handler) (); do_exit (SIGABRT); /* signal handler didn't exit. Goodbye. */ } =20 --=20 2.31.1