1 /* $OpenBSD: findfp.c,v 1.15 2013/12/17 16:33:27 deraadt 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" 42 #include "private/thread_private.h" 43 44 #define ALIGNBYTES (sizeof(uintptr_t) - 1) 45 #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES) 46 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 57 /* the usual - (stdin + stdout + stderr) */ 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 }; 61 static struct glue *lastglue = &uglue; 62 _THREAD_PRIVATE_MUTEX(__sfp_mutex); 63 64 static struct __sfileext __sFext[3]; 65 FILE __sF[3] = { 66 std(__SRD, STDIN_FILENO), /* stdin */ 67 std(__SWR, STDOUT_FILENO), /* stdout */ 68 std(__SWR|__SNBF, STDERR_FILENO) /* stderr */ 69 }; 70 struct glue __sglue = { &uglue, 3, __sF }; 71 72 static struct glue * 73 moreglue(int n) 74 { 75 struct glue *g; 76 FILE *p; 77 struct __sfileext *pext; 78 static FILE empty; 79 char *data; 80 81 data = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE) 82 + n * sizeof(struct __sfileext)); 83 if (data == NULL) 84 return (NULL); 85 g = (struct glue *)data; 86 p = (FILE *)ALIGN(data + sizeof(*g)); 87 pext = (struct __sfileext *) 88 (ALIGN(data + sizeof(*g)) + n * sizeof(FILE)); 89 g->next = NULL; 90 g->niobs = n; 91 g->iobs = p; 92 while (--n >= 0) { 93 *p = empty; 94 _FILEEXT_SETUP(p, pext); 95 p++; 96 pext++; 97 } 98 return (g); 99 } 100 101 /* 102 * Find a free FILE for fopen et al. 103 */ 104 FILE * 105 __sfp(void) 106 { 107 FILE *fp; 108 int n; 109 struct glue *g; 110 111 if (!__sdidinit) 112 __sinit(); 113 114 _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); 115 for (g = &__sglue; g != NULL; g = g->next) { 116 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) 117 if (fp->_flags == 0) 118 goto found; 119 } 120 121 /* release lock while mallocing */ 122 _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); 123 if ((g = moreglue(NDYNAMIC)) == NULL) 124 return (NULL); 125 _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); 126 lastglue->next = g; 127 lastglue = g; 128 fp = g->iobs; 129 found: 130 fp->_flags = 1; /* reserve this slot; caller sets real flags */ 131 _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); 132 fp->_p = NULL; /* no current pointer */ 133 fp->_w = 0; /* nothing to read or write */ 134 fp->_r = 0; 135 fp->_bf._base = NULL; /* no buffer */ 136 fp->_bf._size = 0; 137 fp->_lbfsize = 0; /* not line buffered */ 138 fp->_file = -1; /* no file */ 139 /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */ 140 fp->_lb._base = NULL; /* no line buffer */ 141 fp->_lb._size = 0; 142 _FILEEXT_INIT(fp); 143 return (fp); 144 } 145 146 /* 147 * exit() and abort() call _cleanup() through the callback registered 148 * with __atexit_register_cleanup(), set whenever we open or buffer a 149 * file. This chicanery is done so that programs that do not use stdio 150 * need not link it all in. 151 * 152 * The name `_cleanup' is, alas, fairly well known outside stdio. 153 */ 154 void 155 _cleanup(void) 156 { 157 /* (void) _fwalk(fclose); */ 158 (void) _fwalk(__sflush); /* `cheating' */ 159 } 160 161 /* 162 * __sinit() is called whenever stdio's internal variables must be set up. 163 */ 164 void 165 __sinit(void) 166 { 167 _THREAD_PRIVATE_MUTEX(__sinit_mutex); 168 int i; 169 170 _THREAD_PRIVATE_MUTEX_LOCK(__sinit_mutex); 171 if (__sdidinit) 172 goto out; /* bail out if caller lost the race */ 173 for (i = 0; i < FOPEN_MAX - 3; i++) { 174 _FILEEXT_SETUP(usual+i, usualext+i); 175 } 176 /* make sure we clean up on exit */ 177 __atexit_register_cleanup(_cleanup); /* conservative */ 178 __sdidinit = 1; 179 out: 180 _THREAD_PRIVATE_MUTEX_UNLOCK(__sinit_mutex); 181 } 182