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