Home | History | Annotate | Download | only in stdio
      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"
     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},0,__sF+file,__sclose,__sread,__sseek,__swrite, \
     50 	 {(unsigned char *)(__sFext+file), 0},NULL,0,{0,0,0},{0},{0,0},0,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 _THREAD_PRIVATE_MUTEX(__sfp_mutex);
     60 
     61 static struct __sfileext __sFext[3] = {
     62 	_FILEEXT_INITIALIZER,
     63 	_FILEEXT_INITIALIZER,
     64 	_FILEEXT_INITIALIZER,
     65 };
     66 
     67 FILE __sF[3] = {
     68 	std(__SRD, STDIN_FILENO),		/* stdin */
     69 	std(__SWR, STDOUT_FILENO),		/* stdout */
     70 	std(__SWR|__SNBF, STDERR_FILENO)	/* stderr */
     71 };
     72 struct glue __sglue = { &uglue, 3, __sF };
     73 
     74 static struct glue *
     75 moreglue(int n)
     76 {
     77 	struct glue *g;
     78 	FILE *p;
     79 	struct __sfileext *pext;
     80 	static FILE empty;
     81 	char *data;
     82 
     83 	data = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE)
     84 	    + n * sizeof(struct __sfileext));
     85 	if (data == NULL)
     86 		return (NULL);
     87 	g = (struct glue *)data;
     88 	p = (FILE *)ALIGN(data + sizeof(*g));
     89 	pext = (struct __sfileext *)
     90 	    (ALIGN(data + sizeof(*g)) + n * sizeof(FILE));
     91 	g->next = NULL;
     92 	g->niobs = n;
     93 	g->iobs = p;
     94 	while (--n >= 0) {
     95 		*p = empty;
     96 		_FILEEXT_SETUP(p, pext);
     97 		p++;
     98 		pext++;
     99 	}
    100 	return (g);
    101 }
    102 
    103 /*
    104  * Find a free FILE for fopen et al.
    105  */
    106 FILE *
    107 __sfp(void)
    108 {
    109 	FILE *fp;
    110 	int n;
    111 	struct glue *g;
    112 
    113 	if (!__sdidinit)
    114 		__sinit();
    115 
    116 	_THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
    117 	for (g = &__sglue; g != NULL; g = g->next) {
    118 		for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
    119 			if (fp->_flags == 0)
    120 				goto found;
    121 	}
    122 
    123 	/* release lock while mallocing */
    124 	_THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
    125 	if ((g = moreglue(NDYNAMIC)) == NULL)
    126 		return (NULL);
    127 	_THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
    128 	lastglue->next = g;
    129 	lastglue = g;
    130 	fp = g->iobs;
    131 found:
    132 	fp->_flags = 1;		/* reserve this slot; caller sets real flags */
    133 	_THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
    134 	fp->_p = NULL;		/* no current pointer */
    135 	fp->_w = 0;		/* nothing to read or write */
    136 	fp->_r = 0;
    137 	fp->_bf._base = NULL;	/* no buffer */
    138 	fp->_bf._size = 0;
    139 	fp->_lbfsize = 0;	/* not line buffered */
    140 	fp->_file = -1;		/* no file */
    141 /*	fp->_cookie = <any>; */	/* caller sets cookie, _read/_write etc */
    142 	fp->_lb._base = NULL;	/* no line buffer */
    143 	fp->_lb._size = 0;
    144 	_FILEEXT_INIT(fp);
    145 	return (fp);
    146 }
    147 
    148 #if 0
    149 #define getdtablesize()	sysconf(_SC_OPEN_MAX)
    150 
    151 /*
    152  * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
    153  * but documented historically for certain applications.  Bad applications.
    154  */
    155 void
    156 f_prealloc(void)
    157 {
    158 	struct glue *g;
    159 	int n;
    160 
    161 	n = getdtablesize() - FOPEN_MAX + 20;		/* 20 for slop. */
    162 	for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
    163 		/* void */;
    164 	if (n > 0 && ((g = moreglue(n)) != NULL)) {
    165 		_THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
    166 		lastglue->next = g;
    167 		lastglue = g;
    168 		_THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
    169 	}
    170 }
    171 #endif
    172 
    173 /*
    174  * exit() calls _cleanup() through *__cleanup, set whenever we
    175  * open or buffer a file.  This chicanery is done so that programs
    176  * that do not use stdio need not link it all in.
    177  *
    178  * The name `_cleanup' is, alas, fairly well known outside stdio.
    179  */
    180 void
    181 _cleanup(void)
    182 {
    183 	/* (void) _fwalk(fclose); */
    184 	(void) _fwalk(__sflush);		/* `cheating' */
    185 }
    186 
    187 /*
    188  * __sinit() is called whenever stdio's internal variables must be set up.
    189  */
    190 void
    191 __sinit(void)
    192 {
    193 	_THREAD_PRIVATE_MUTEX(__sinit_mutex);
    194 	int i;
    195 
    196 	_THREAD_PRIVATE_MUTEX_LOCK(__sinit_mutex);
    197 	if (__sdidinit)
    198 		goto out;	/* bail out if caller lost the race */
    199 	for (i = 0; i < FOPEN_MAX - 3; i++) {
    200 		_FILEEXT_SETUP(usual+i, usualext+i);
    201 	}
    202 	/* make sure we clean up on exit */
    203 	__cleanup = _cleanup; /* conservative */
    204 	__sdidinit = 1;
    205 out:
    206 	_THREAD_PRIVATE_MUTEX_UNLOCK(__sinit_mutex);
    207 }
    208