The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* $OpenBSD: findfp.c,v 1.9 2005/08/08 08:05:36 espie Exp $ */ |
| 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 | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 42 | #include "private/thread_private.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 43 | |
Calin Juravle | c20de90 | 2014-03-20 15:21:32 +0000 | [diff] [blame] | 44 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
| 45 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES) |
| 46 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | int __sdidinit; |
| 48 | |
| 49 | #define NDYNAMIC 10 /* add ten more whenever necessary */ |
| 50 | |
| 51 | #define std(flags, file) \ |
| 52 | {0,0,0,flags,file,{0,0},0,__sF+file,__sclose,__sread,__sseek,__swrite, \ |
| 53 | {(unsigned char *)(__sFext+file), 0},NULL,0,{0,0,0},{0},{0,0},0,0} |
| 54 | /* p r w flags file _bf z cookie close read seek write |
| 55 | ext */ |
| 56 | |
André Goddard Rosa | a910abc | 2010-01-30 22:46:25 -0200 | [diff] [blame] | 57 | /* the usual - (stdin + stdout + stderr) */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 58 | static FILE usual[FOPEN_MAX - 3]; |
| 59 | static struct __sfileext usualext[FOPEN_MAX - 3]; |
| 60 | static struct glue uglue = { 0, FOPEN_MAX - 3, usual }; |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 61 | static struct glue *lastglue = &uglue; |
| 62 | _THREAD_PRIVATE_MUTEX(__sfp_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 63 | |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame^] | 64 | static struct __sfileext __sFext[3]; |
David 'Digit' Turner | 9831ad3 | 2011-08-29 21:43:46 +0200 | [diff] [blame] | 65 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 66 | FILE __sF[3] = { |
| 67 | std(__SRD, STDIN_FILENO), /* stdin */ |
| 68 | std(__SWR, STDOUT_FILENO), /* stdout */ |
| 69 | std(__SWR|__SNBF, STDERR_FILENO) /* stderr */ |
| 70 | }; |
| 71 | struct glue __sglue = { &uglue, 3, __sF }; |
| 72 | |
| 73 | static struct glue * |
| 74 | moreglue(int n) |
| 75 | { |
| 76 | struct glue *g; |
| 77 | FILE *p; |
| 78 | struct __sfileext *pext; |
| 79 | static FILE empty; |
| 80 | char *data; |
| 81 | |
| 82 | data = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE) |
| 83 | + n * sizeof(struct __sfileext)); |
| 84 | if (data == NULL) |
| 85 | return (NULL); |
| 86 | g = (struct glue *)data; |
| 87 | p = (FILE *)ALIGN(data + sizeof(*g)); |
| 88 | pext = (struct __sfileext *) |
| 89 | (ALIGN(data + sizeof(*g)) + n * sizeof(FILE)); |
| 90 | g->next = NULL; |
| 91 | g->niobs = n; |
| 92 | g->iobs = p; |
| 93 | while (--n >= 0) { |
| 94 | *p = empty; |
| 95 | _FILEEXT_SETUP(p, pext); |
| 96 | p++; |
| 97 | pext++; |
| 98 | } |
| 99 | return (g); |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Find a free FILE for fopen et al. |
| 104 | */ |
| 105 | FILE * |
| 106 | __sfp(void) |
| 107 | { |
| 108 | FILE *fp; |
| 109 | int n; |
| 110 | struct glue *g; |
| 111 | |
| 112 | if (!__sdidinit) |
| 113 | __sinit(); |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 114 | |
| 115 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 116 | for (g = &__sglue; g != NULL; g = g->next) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 117 | for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) |
| 118 | if (fp->_flags == 0) |
| 119 | goto found; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 120 | } |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 121 | |
| 122 | /* release lock while mallocing */ |
| 123 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
| 124 | if ((g = moreglue(NDYNAMIC)) == NULL) |
| 125 | return (NULL); |
| 126 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 127 | lastglue->next = g; |
| 128 | lastglue = g; |
| 129 | fp = g->iobs; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 130 | found: |
| 131 | fp->_flags = 1; /* reserve this slot; caller sets real flags */ |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 132 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 133 | fp->_p = NULL; /* no current pointer */ |
| 134 | fp->_w = 0; /* nothing to read or write */ |
| 135 | fp->_r = 0; |
| 136 | fp->_bf._base = NULL; /* no buffer */ |
| 137 | fp->_bf._size = 0; |
| 138 | fp->_lbfsize = 0; /* not line buffered */ |
| 139 | fp->_file = -1; /* no file */ |
| 140 | /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */ |
| 141 | fp->_lb._base = NULL; /* no line buffer */ |
| 142 | fp->_lb._size = 0; |
| 143 | _FILEEXT_INIT(fp); |
| 144 | return (fp); |
| 145 | } |
| 146 | |
| 147 | #if 0 |
| 148 | #define getdtablesize() sysconf(_SC_OPEN_MAX) |
| 149 | |
| 150 | /* |
| 151 | * XXX. Force immediate allocation of internal memory. Not used by stdio, |
| 152 | * but documented historically for certain applications. Bad applications. |
| 153 | */ |
| 154 | void |
| 155 | f_prealloc(void) |
| 156 | { |
| 157 | struct glue *g; |
| 158 | int n; |
| 159 | |
| 160 | n = getdtablesize() - FOPEN_MAX + 20; /* 20 for slop. */ |
| 161 | for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next) |
| 162 | /* void */; |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 163 | if (n > 0 && ((g = moreglue(n)) != NULL)) { |
| 164 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 165 | lastglue->next = g; |
| 166 | lastglue = g; |
| 167 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
| 168 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 169 | } |
| 170 | #endif |
| 171 | |
| 172 | /* |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 173 | * exit() calls _cleanup() through *__cleanup, set whenever we |
| 174 | * open or buffer a file. This chicanery is done so that programs |
| 175 | * that do not use stdio need not link it all in. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 176 | * |
| 177 | * The name `_cleanup' is, alas, fairly well known outside stdio. |
| 178 | */ |
| 179 | void |
| 180 | _cleanup(void) |
| 181 | { |
| 182 | /* (void) _fwalk(fclose); */ |
| 183 | (void) _fwalk(__sflush); /* `cheating' */ |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * __sinit() is called whenever stdio's internal variables must be set up. |
| 188 | */ |
| 189 | void |
| 190 | __sinit(void) |
| 191 | { |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 192 | _THREAD_PRIVATE_MUTEX(__sinit_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 193 | int i; |
| 194 | |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 195 | _THREAD_PRIVATE_MUTEX_LOCK(__sinit_mutex); |
| 196 | if (__sdidinit) |
| 197 | goto out; /* bail out if caller lost the race */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 198 | for (i = 0; i < FOPEN_MAX - 3; i++) { |
| 199 | _FILEEXT_SETUP(usual+i, usualext+i); |
| 200 | } |
| 201 | /* make sure we clean up on exit */ |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 202 | __cleanup = _cleanup; /* conservative */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 203 | __sdidinit = 1; |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 204 | out: |
| 205 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sinit_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 206 | } |