From c94bb21b3f7cc005642583eca720421af424288e Mon Sep 17 00:00:00 2001 From: Pawit Pornkitprasan Date: Thu, 14 Nov 2013 23:50:45 +0700 Subject: bionic: call stdio cleanup on exit As of 61e699a133a4807fe878a6cb0d7190d7c96e21f8, stdio clean up functions are no longer registered in atexit and must be called manually via __cleanup. The issue this fixes is some static binaries linked against bionic cannot output properly when piped or redirected because the buffer is not flushed before closing. This is done by pulling in exit.c (and other dependencies) from netbsd. Change-Id: I193e54a6d08900f291550029fe75ce76394d9e22 --- libc/Android.mk | 3 +- libc/stdlib/exit.c | 59 -------------------- libc/upstream-netbsd/libc/stdlib/exit.c | 63 ++++++++++++++++++++++ .../libc/thread-stub/__isthreaded.c | 37 +++++++++++++ 4 files changed, 102 insertions(+), 60 deletions(-) delete mode 100644 libc/stdlib/exit.c create mode 100644 libc/upstream-netbsd/libc/stdlib/exit.c create mode 100644 libc/upstream-netbsd/libc/thread-stub/__isthreaded.c diff --git a/libc/Android.mk b/libc/Android.mk index 9610c14..e258915 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -45,7 +45,6 @@ libc_common_src_files := \ stdio/wbuf.c \ stdlib/atexit.c \ stdlib/ctype_.c \ - stdlib/exit.c \ stdlib/getenv.c \ stdlib/putenv.c \ stdlib/setenv.c \ @@ -342,6 +341,7 @@ libc_upstream_netbsd_src_files := \ upstream-netbsd/libc/stdlib/div.c \ upstream-netbsd/libc/stdlib/drand48.c \ upstream-netbsd/libc/stdlib/erand48.c \ + upstream-netbsd/libc/stdlib/exit.c \ upstream-netbsd/libc/stdlib/jrand48.c \ upstream-netbsd/libc/stdlib/ldiv.c \ upstream-netbsd/libc/stdlib/lldiv.c \ @@ -358,6 +358,7 @@ libc_upstream_netbsd_src_files := \ upstream-netbsd/libc/string/strcasestr.c \ upstream-netbsd/libc/string/strcoll.c \ upstream-netbsd/libc/string/strxfrm.c \ + upstream-netbsd/libc/thread-stub/__isthreaded.c \ upstream-netbsd/libc/unistd/killpg.c \ # Architecture specific source files go here diff --git a/libc/stdlib/exit.c b/libc/stdlib/exit.c deleted file mode 100644 index 83fe3d2..0000000 --- a/libc/stdlib/exit.c +++ /dev/null @@ -1,59 +0,0 @@ -/* $OpenBSD: exit.c,v 1.12 2007/09/03 14:40:16 millert Exp $ */ -/*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include "atexit.h" -#include "thread_private.h" - -/* - * This variable is zero until a process has created a thread. - * It is used to avoid calling locking functions in libc when they - * are not required. By default, libc is intended to be(come) - * thread-safe, but without a (significant) penalty to non-threaded - * processes. - */ -int __isthreaded = 0; - -/* - * Exit, flushing stdio buffers if necessary. - */ -void -exit(int status) -{ - /* - * Call functions registered by atexit() or _cxa_atexit() - * (including the stdio cleanup routine) and then _exit(). - */ - __cxa_finalize(NULL); - _exit(status); -} diff --git a/libc/upstream-netbsd/libc/stdlib/exit.c b/libc/upstream-netbsd/libc/stdlib/exit.c new file mode 100644 index 0000000..67e6adf --- /dev/null +++ b/libc/upstream-netbsd/libc/stdlib/exit.c @@ -0,0 +1,63 @@ +/* $NetBSD: exit.c,v 1.15 2011/05/18 19:36:36 dsl Exp $ */ + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)exit.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: exit.c,v 1.15 2011/05/18 19:36:36 dsl Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#ifdef _LIBC +#include "reentrant.h" +#include "atexit.h" +#endif + +void (*__cleanup)(void); + +/* + * Exit, flushing stdio buffers if necessary. + */ +void +exit(int status) +{ + +#ifdef _LIBC + __cxa_finalize(NULL); +#endif + if (__cleanup) + (*__cleanup)(); + _exit(status); +} diff --git a/libc/upstream-netbsd/libc/thread-stub/__isthreaded.c b/libc/upstream-netbsd/libc/thread-stub/__isthreaded.c new file mode 100644 index 0000000..50c1b6f --- /dev/null +++ b/libc/upstream-netbsd/libc/thread-stub/__isthreaded.c @@ -0,0 +1,37 @@ +/* $NetBSD: __isthreaded.c,v 1.3 2009/12/01 01:33:25 explorer Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Michael Graff. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __isthreaded.c,v 1.3 2009/12/01 01:33:25 explorer Exp $"); +#endif /* LIBC_SCCS and not lint */ + +int __isthreaded = 0; -- cgit v1.1