Dmitriy Ivanov | 623b0d0 | 2014-05-14 23:11:05 -0700 | [diff] [blame] | 1 | /* $OpenBSD: findfp.c,v 1.15 2013/12/17 16:33:27 deraadt Exp $ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2 | /*- |
| 3 | * Copyright (c) 1990, 1993 |
| 4 | * The Regents of the University of California. All rights reserved. |
| 5 | * |
| 6 | * This code is derived from software contributed to Berkeley by |
| 7 | * Chris Torek. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include <sys/param.h> |
| 35 | #include <unistd.h> |
| 36 | #include <stdio.h> |
| 37 | #include <errno.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include "local.h" |
| 41 | #include "glue.h" |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 42 | #include "private/thread_private.h" |
| 43 | |
| 44 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
| 45 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES) |
Calin Juravle | c20de90 | 2014-03-20 15:21:32 +0000 | [diff] [blame] | 46 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | #define NDYNAMIC 10 /* add ten more whenever necessary */ |
| 48 | |
| 49 | #define std(flags, file) \ |
Chih-Hung Hsieh | dc6599e | 2014-11-04 12:09:35 -0800 | [diff] [blame] | 50 | {0,0,0,flags,file,{0,0},0,__sF+file,__sclose,__sread,__sseek,__swrite, \ |
| 51 | {(unsigned char *)(__sFext+file), 0},NULL,0,{0},{0},{0,0},0,0} |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 52 | |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 53 | _THREAD_PRIVATE_MUTEX(__sfp_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 54 | |
Elliott Hughes | 29ee639 | 2015-12-07 11:07:15 -0800 | [diff] [blame^] | 55 | // TODO: when we no longer have to support both clang and GCC, we can simplify all this. |
| 56 | #define SBUF_INIT {0,0} |
| 57 | #if defined(__LP64__) |
| 58 | #define MBSTATE_T_INIT {{0},{0}} |
| 59 | #else |
| 60 | #define MBSTATE_T_INIT {{0}} |
| 61 | #endif |
| 62 | #define WCHAR_IO_DATA_INIT {MBSTATE_T_INIT,MBSTATE_T_INIT,{0},0,0} |
| 63 | |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 64 | static struct __sfileext __sFext[3] = { |
Elliott Hughes | 29ee639 | 2015-12-07 11:07:15 -0800 | [diff] [blame^] | 65 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false }, |
| 66 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false }, |
| 67 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false }, |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 68 | }; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 69 | |
| 70 | // __sF is exported for backwards compatibility. Until M, we didn't have symbols |
| 71 | // for stdin/stdout/stderr; they were macros accessing __sF. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 72 | FILE __sF[3] = { |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 73 | std(__SRD, STDIN_FILENO), |
| 74 | std(__SWR, STDOUT_FILENO), |
| 75 | std(__SWR|__SNBF, STDERR_FILENO), |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 76 | }; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 77 | |
Elliott Hughes | 168667c | 2014-11-14 14:42:59 -0800 | [diff] [blame] | 78 | FILE* stdin = &__sF[0]; |
| 79 | FILE* stdout = &__sF[1]; |
| 80 | FILE* stderr = &__sF[2]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 81 | |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 82 | struct glue __sglue = { NULL, 3, __sF }; |
| 83 | static struct glue* lastglue = &__sglue; |
| 84 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 85 | static struct glue * |
| 86 | moreglue(int n) |
| 87 | { |
| 88 | struct glue *g; |
| 89 | FILE *p; |
| 90 | struct __sfileext *pext; |
| 91 | static FILE empty; |
| 92 | char *data; |
| 93 | |
| 94 | data = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE) |
| 95 | + n * sizeof(struct __sfileext)); |
| 96 | if (data == NULL) |
| 97 | return (NULL); |
| 98 | g = (struct glue *)data; |
| 99 | p = (FILE *)ALIGN(data + sizeof(*g)); |
| 100 | pext = (struct __sfileext *) |
| 101 | (ALIGN(data + sizeof(*g)) + n * sizeof(FILE)); |
| 102 | g->next = NULL; |
| 103 | g->niobs = n; |
| 104 | g->iobs = p; |
| 105 | while (--n >= 0) { |
| 106 | *p = empty; |
| 107 | _FILEEXT_SETUP(p, pext); |
| 108 | p++; |
| 109 | pext++; |
| 110 | } |
| 111 | return (g); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Find a free FILE for fopen et al. |
| 116 | */ |
| 117 | FILE * |
| 118 | __sfp(void) |
| 119 | { |
| 120 | FILE *fp; |
| 121 | int n; |
| 122 | struct glue *g; |
| 123 | |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 124 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 125 | for (g = &__sglue; g != NULL; g = g->next) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 126 | for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) |
| 127 | if (fp->_flags == 0) |
| 128 | goto found; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 129 | } |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 130 | |
| 131 | /* release lock while mallocing */ |
| 132 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
| 133 | if ((g = moreglue(NDYNAMIC)) == NULL) |
| 134 | return (NULL); |
| 135 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 136 | lastglue->next = g; |
| 137 | lastglue = g; |
| 138 | fp = g->iobs; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 139 | found: |
| 140 | fp->_flags = 1; /* reserve this slot; caller sets real flags */ |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 141 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 142 | fp->_p = NULL; /* no current pointer */ |
| 143 | fp->_w = 0; /* nothing to read or write */ |
| 144 | fp->_r = 0; |
| 145 | fp->_bf._base = NULL; /* no buffer */ |
| 146 | fp->_bf._size = 0; |
| 147 | fp->_lbfsize = 0; /* not line buffered */ |
| 148 | fp->_file = -1; /* no file */ |
| 149 | /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */ |
| 150 | fp->_lb._base = NULL; /* no line buffer */ |
| 151 | fp->_lb._size = 0; |
| 152 | _FILEEXT_INIT(fp); |
| 153 | return (fp); |
| 154 | } |
| 155 | |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 156 | __LIBC_HIDDEN__ void __libc_stdio_cleanup(void) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 157 | /* (void) _fwalk(fclose); */ |
| 158 | (void) _fwalk(__sflush); /* `cheating' */ |
| 159 | } |