blob: 0c2ee7cee2884cf98c5a597b5db9a13b987cd184 [file] [log] [blame]
Dmitriy Ivanov623b0d02014-05-14 23:11:05 -07001/* $OpenBSD: findfp.c,v 1.15 2013/12/17 16:33:27 deraadt Exp $ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002/*-
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 Hugheseb847bc2013-10-09 15:50:50 -070042#include "private/thread_private.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043
Calin Juravlec20de902014-03-20 15:21:32 +000044#define ALIGNBYTES (sizeof(uintptr_t) - 1)
45#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
46
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047int __sdidinit;
48
49#define NDYNAMIC 10 /* add ten more whenever necessary */
50
51#define std(flags, file) \
Elliott Hughesd18f4b22014-11-03 12:32:17 -080052 {0,0,0,flags,file,{0},0,__sF+file,__sclose,__sread,__sseek,__swrite, \
53 {(unsigned char *)(__sFext+file), 0},NULL,0,{0},{0},{0},0,0}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054
Dmitriy Ivanov623b0d02014-05-14 23:11:05 -070055 /* the usual - (stdin + stdout + stderr) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056static FILE usual[FOPEN_MAX - 3];
57static struct __sfileext usualext[FOPEN_MAX - 3];
58static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
Kenny Rootf5823402011-02-12 07:13:44 -080059static struct glue *lastglue = &uglue;
60_THREAD_PRIVATE_MUTEX(__sfp_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061
Elliott Hughes01ae00f2014-04-29 16:28:56 -070062static struct __sfileext __sFext[3];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063FILE __sF[3] = {
64 std(__SRD, STDIN_FILENO), /* stdin */
65 std(__SWR, STDOUT_FILENO), /* stdout */
66 std(__SWR|__SNBF, STDERR_FILENO) /* stderr */
67};
68struct glue __sglue = { &uglue, 3, __sF };
69
70static struct glue *
71moreglue(int n)
72{
73 struct glue *g;
74 FILE *p;
75 struct __sfileext *pext;
76 static FILE empty;
77 char *data;
78
79 data = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE)
80 + n * sizeof(struct __sfileext));
81 if (data == NULL)
82 return (NULL);
83 g = (struct glue *)data;
84 p = (FILE *)ALIGN(data + sizeof(*g));
85 pext = (struct __sfileext *)
86 (ALIGN(data + sizeof(*g)) + n * sizeof(FILE));
87 g->next = NULL;
88 g->niobs = n;
89 g->iobs = p;
90 while (--n >= 0) {
91 *p = empty;
92 _FILEEXT_SETUP(p, pext);
93 p++;
94 pext++;
95 }
96 return (g);
97}
98
99/*
100 * Find a free FILE for fopen et al.
101 */
102FILE *
103__sfp(void)
104{
105 FILE *fp;
106 int n;
107 struct glue *g;
108
109 if (!__sdidinit)
110 __sinit();
Kenny Rootf5823402011-02-12 07:13:44 -0800111
112 _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
113 for (g = &__sglue; g != NULL; g = g->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
115 if (fp->_flags == 0)
116 goto found;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117 }
Kenny Rootf5823402011-02-12 07:13:44 -0800118
119 /* release lock while mallocing */
120 _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
121 if ((g = moreglue(NDYNAMIC)) == NULL)
122 return (NULL);
123 _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
124 lastglue->next = g;
125 lastglue = g;
126 fp = g->iobs;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127found:
128 fp->_flags = 1; /* reserve this slot; caller sets real flags */
Kenny Rootf5823402011-02-12 07:13:44 -0800129 _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130 fp->_p = NULL; /* no current pointer */
131 fp->_w = 0; /* nothing to read or write */
132 fp->_r = 0;
133 fp->_bf._base = NULL; /* no buffer */
134 fp->_bf._size = 0;
135 fp->_lbfsize = 0; /* not line buffered */
136 fp->_file = -1; /* no file */
137/* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
138 fp->_lb._base = NULL; /* no line buffer */
139 fp->_lb._size = 0;
140 _FILEEXT_INIT(fp);
141 return (fp);
142}
143
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144/*
Dmitriy Ivanov623b0d02014-05-14 23:11:05 -0700145 * exit() and abort() call _cleanup() through the callback registered
146 * with __atexit_register_cleanup(), set whenever we open or buffer a
147 * file. This chicanery is done so that programs that do not use stdio
148 * need not link it all in.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149 *
150 * The name `_cleanup' is, alas, fairly well known outside stdio.
151 */
152void
153_cleanup(void)
154{
155 /* (void) _fwalk(fclose); */
156 (void) _fwalk(__sflush); /* `cheating' */
157}
158
159/*
160 * __sinit() is called whenever stdio's internal variables must be set up.
161 */
162void
163__sinit(void)
164{
Kenny Rootf5823402011-02-12 07:13:44 -0800165 _THREAD_PRIVATE_MUTEX(__sinit_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800166
Kenny Rootf5823402011-02-12 07:13:44 -0800167 _THREAD_PRIVATE_MUTEX_LOCK(__sinit_mutex);
Elliott Hughesd18f4b22014-11-03 12:32:17 -0800168 if (__sdidinit) {
169 /* bail out if caller lost the race */
170 _THREAD_PRIVATE_MUTEX_UNLOCK(__sinit_mutex);
171 return;
172 }
173
174 /* Initialize stdin/stdout/stderr (for the recursive mutex). http://b/18208568. */
175 for (size_t i = 0; i < 3; ++i) {
176 _FILEEXT_SETUP(__sF+i, __sFext+i);
177 }
178 /* Initialize the pre-allocated (but initially unused) streams. */
179 for (size_t i = 0; i < FOPEN_MAX - 3; ++i) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180 _FILEEXT_SETUP(usual+i, usualext+i);
181 }
Elliott Hughesd18f4b22014-11-03 12:32:17 -0800182
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183 /* make sure we clean up on exit */
Dmitriy Ivanov623b0d02014-05-14 23:11:05 -0700184 __atexit_register_cleanup(_cleanup); /* conservative */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185 __sdidinit = 1;
Elliott Hughesd18f4b22014-11-03 12:32:17 -0800186
Kenny Rootf5823402011-02-12 07:13:44 -0800187 _THREAD_PRIVATE_MUTEX_UNLOCK(__sinit_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188}