From 5a69da8d7af6a388a4ad5b3dd619017b0030031a Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 24 Sep 2014 11:14:36 -0700 Subject: Switch to OpenBSD fopen/fclose. This means all our stdio implementation is now the OpenBSD implementation. The only thing we lose is the STDIO_THREAD_LOCK calls but they were no-ops anyway. We should probably talk to upstream about this. Either fix the locking or, preferably, encourage them to move away from this pooling (especially since there's no eviction policy). Bug: 17154680 Change-Id: Ie2523e444a7d0965b8d141d57e3e11f6432d5b9a --- libc/Android.mk | 4 +- .../android/include/freebsd-compat.h | 11 --- libc/upstream-freebsd/lib/libc/stdio/fclose.c | 85 ------------------- libc/upstream-freebsd/lib/libc/stdio/fopen.c | 97 ---------------------- libc/upstream-openbsd/lib/libc/stdio/fclose.c | 63 ++++++++++++++ libc/upstream-openbsd/lib/libc/stdio/fopen.c | 86 +++++++++++++++++++ 6 files changed, 151 insertions(+), 195 deletions(-) delete mode 100644 libc/upstream-freebsd/lib/libc/stdio/fclose.c delete mode 100644 libc/upstream-freebsd/lib/libc/stdio/fopen.c create mode 100644 libc/upstream-openbsd/lib/libc/stdio/fclose.c create mode 100644 libc/upstream-openbsd/lib/libc/stdio/fopen.c diff --git a/libc/Android.mk b/libc/Android.mk index ce06096..fee54a2 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -242,8 +242,6 @@ libc_upstream_freebsd_src_files := \ upstream-freebsd/lib/libc/gen/ldexp.c \ upstream-freebsd/lib/libc/gen/sleep.c \ upstream-freebsd/lib/libc/gen/usleep.c \ - upstream-freebsd/lib/libc/stdio/fclose.c \ - upstream-freebsd/lib/libc/stdio/fopen.c \ upstream-freebsd/lib/libc/stdlib/abs.c \ upstream-freebsd/lib/libc/stdlib/getopt_long.c \ upstream-freebsd/lib/libc/stdlib/imaxabs.c \ @@ -388,6 +386,7 @@ libc_upstream_openbsd_src_files := \ upstream-openbsd/lib/libc/stdio/asprintf.c \ upstream-openbsd/lib/libc/stdio/clrerr.c \ upstream-openbsd/lib/libc/stdio/dprintf.c \ + upstream-openbsd/lib/libc/stdio/fclose.c \ upstream-openbsd/lib/libc/stdio/fdopen.c \ upstream-openbsd/lib/libc/stdio/feof.c \ upstream-openbsd/lib/libc/stdio/ferror.c \ @@ -402,6 +401,7 @@ libc_upstream_openbsd_src_files := \ upstream-openbsd/lib/libc/stdio/findfp.c \ upstream-openbsd/lib/libc/stdio/flags.c \ upstream-openbsd/lib/libc/stdio/fmemopen.c \ + upstream-openbsd/lib/libc/stdio/fopen.c \ upstream-openbsd/lib/libc/stdio/fprintf.c \ upstream-openbsd/lib/libc/stdio/fpurge.c \ upstream-openbsd/lib/libc/stdio/fputc.c \ diff --git a/libc/upstream-freebsd/android/include/freebsd-compat.h b/libc/upstream-freebsd/android/include/freebsd-compat.h index b44b94a..7acdf7c 100644 --- a/libc/upstream-freebsd/android/include/freebsd-compat.h +++ b/libc/upstream-freebsd/android/include/freebsd-compat.h @@ -38,17 +38,6 @@ #define __usleep usleep /* Redirect internal C library calls to the public function. */ -#define _close close -#define _fcntl fcntl -#define _fstat fstat #define _nanosleep nanosleep -#define _open open - -/* This one is only needed as long as we have a mix of OpenBSD and FreeBSD stdio. */ -#define _sseek __sseek - -/* This is in BSD's . */ -#include -extern uint32_t arc4random_uniform(uint32_t upper_bound); #endif diff --git a/libc/upstream-freebsd/lib/libc/stdio/fclose.c b/libc/upstream-freebsd/lib/libc/stdio/fclose.c deleted file mode 100644 index 5ed8b2c..0000000 --- a/libc/upstream-freebsd/lib/libc/stdio/fclose.c +++ /dev/null @@ -1,85 +0,0 @@ -/*- - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * 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. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)fclose.c 8.1 (Berkeley) 6/4/93"; -#endif /* LIBC_SCCS and not lint */ -#include -__FBSDID("$FreeBSD$"); - -#include "namespace.h" -#include -#include -#include -#include "un-namespace.h" -#include -#include "libc_private.h" -#include "local.h" - -int -fclose(FILE *fp) -{ - int r; - - if (fp->_flags == 0) { /* not open! */ - errno = EBADF; - return (EOF); - } - FLOCKFILE(fp); - r = fp->_flags & __SWR ? __sflush(fp) : 0; - if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) - r = EOF; - if (fp->_flags & __SMBF) - free((char *)fp->_bf._base); - if (HASUB(fp)) - FREEUB(fp); - if (HASLB(fp)) - FREELB(fp); - fp->_file = -1; - fp->_r = fp->_w = 0; /* Mess up if reaccessed. */ - - /* - * Lock the spinlock used to protect __sglue list walk in - * __sfp(). The __sfp() uses fp->_flags == 0 test as an - * indication of the unused FILE. - * - * Taking the lock prevents possible compiler or processor - * reordering of the writes performed before the final _flags - * cleanup, making sure that we are done with the FILE before - * it is considered available. - */ - STDIO_THREAD_LOCK(); - fp->_flags = 0; /* Release this FILE for reuse. */ - STDIO_THREAD_UNLOCK(); - FUNLOCKFILE(fp); - return (r); -} diff --git a/libc/upstream-freebsd/lib/libc/stdio/fopen.c b/libc/upstream-freebsd/lib/libc/stdio/fopen.c deleted file mode 100644 index b08e336..0000000 --- a/libc/upstream-freebsd/lib/libc/stdio/fopen.c +++ /dev/null @@ -1,97 +0,0 @@ -/*- - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * 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. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)fopen.c 8.1 (Berkeley) 6/4/93"; -#endif /* LIBC_SCCS and not lint */ -#include -__FBSDID("$FreeBSD$"); - -#include "namespace.h" -#include -#include -#include -#include -#include -#include -#include -#include "un-namespace.h" - -#include "local.h" - -FILE * -fopen(const char * __restrict file, const char * __restrict mode) -{ - FILE *fp; - int f; - int flags, oflags; - - if ((flags = __sflags(mode, &oflags)) == 0) - return (NULL); - if ((fp = __sfp()) == NULL) - return (NULL); - if ((f = _open(file, oflags, DEFFILEMODE)) < 0) { - fp->_flags = 0; /* release */ - return (NULL); - } - /* - * File descriptors are a full int, but _file is only a short. - * If we get a valid file descriptor that is greater than - * SHRT_MAX, then the fd will get sign-extended into an - * invalid file descriptor. Handle this case by failing the - * open. - */ - if (f > SHRT_MAX) { - fp->_flags = 0; /* release */ - _close(f); - errno = EMFILE; - return (NULL); - } - fp->_file = f; - fp->_flags = flags; - fp->_cookie = fp; - fp->_read = __sread; - fp->_write = __swrite; - fp->_seek = __sseek; - fp->_close = __sclose; - /* - * When opening in append mode, even though we use O_APPEND, - * we need to seek to the end so that ftell() gets the right - * answer. If the user then alters the seek pointer, or - * the file extends, this will fail, but there is not much - * we can do about this. (We could set __SAPP and check in - * fseek and ftell.) - */ - if (oflags & O_APPEND) - (void)_sseek(fp, (fpos_t)0, SEEK_END); - return (fp); -} diff --git a/libc/upstream-openbsd/lib/libc/stdio/fclose.c b/libc/upstream-openbsd/lib/libc/stdio/fclose.c new file mode 100644 index 0000000..c72af54 --- /dev/null +++ b/libc/upstream-openbsd/lib/libc/stdio/fclose.c @@ -0,0 +1,63 @@ +/* $OpenBSD: fclose.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * 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 "local.h" + +int +fclose(FILE *fp) +{ + int r; + + if (fp->_flags == 0) { /* not open! */ + errno = EBADF; + return (EOF); + } + FLOCKFILE(fp); + WCIO_FREE(fp); + r = fp->_flags & __SWR ? __sflush(fp) : 0; + if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) + r = EOF; + if (fp->_flags & __SMBF) + free((char *)fp->_bf._base); + if (HASUB(fp)) + FREEUB(fp); + if (HASLB(fp)) + FREELB(fp); + fp->_r = fp->_w = 0; /* Mess up if reaccessed. */ + fp->_flags = 0; /* Release this FILE for reuse. */ + FUNLOCKFILE(fp); + return (r); +} diff --git a/libc/upstream-openbsd/lib/libc/stdio/fopen.c b/libc/upstream-openbsd/lib/libc/stdio/fopen.c new file mode 100644 index 0000000..1465052 --- /dev/null +++ b/libc/upstream-openbsd/lib/libc/stdio/fopen.c @@ -0,0 +1,86 @@ +/* $OpenBSD: fopen.c,v 1.7 2008/05/03 18:46:41 chl Exp $ */ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * 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 +#include +#include +#include "local.h" + +FILE * +fopen(const char *file, const char *mode) +{ + FILE *fp; + int f; + int flags, oflags; + + if ((flags = __sflags(mode, &oflags)) == 0) + return (NULL); + if ((fp = __sfp()) == NULL) + return (NULL); + if ((f = open(file, oflags, DEFFILEMODE)) < 0) { + fp->_flags = 0; /* release */ + return (NULL); + } + + /* _file is only a short */ + if (f > SHRT_MAX) { + fp->_flags = 0; /* release */ + close(f); + errno = EMFILE; + return (NULL); + } + + fp->_file = f; + fp->_flags = flags; + fp->_cookie = fp; + fp->_read = __sread; + fp->_write = __swrite; + fp->_seek = __sseek; + fp->_close = __sclose; + + /* + * When opening in append mode, even though we use O_APPEND, + * we need to seek to the end so that ftell() gets the right + * answer. If the user then alters the seek pointer, or + * the file extends, this will fail, but there is not much + * we can do about this. (We could set __SAPP and check in + * fseek and ftell.) + */ + if (oflags & O_APPEND) + (void) __sseek((void *)fp, (fpos_t)0, SEEK_END); + return (fp); +} -- cgit v1.1