blob: 35de07235587226260cbd3b3ffbb751dc904b137 [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 Hughes6a03abc2014-11-03 12:32:17 -080042#include "private/thread_private.h"
43
44#define ALIGNBYTES (sizeof(uintptr_t) - 1)
45#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
Calin Juravlec20de902014-03-20 15:21:32 +000046
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047#define NDYNAMIC 10 /* add ten more whenever necessary */
48
49#define std(flags, file) \
Chih-Hung Hsiehdc6599e2014-11-04 12:09:35 -080050 {0,0,0,flags,file,{0,0},0,__sF+file,__sclose,__sread,__sseek,__swrite, \
51 {(unsigned char *)(__sFext+file), 0},NULL,0,{0},{0},{0,0},0,0}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
Kenny Rootf5823402011-02-12 07:13:44 -080053_THREAD_PRIVATE_MUTEX(__sfp_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054
Elliott Hughesbb46afd2015-12-04 18:03:12 -080055static struct __sfileext __sFext[3] = {
56 { { NULL, 0 }, {}, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false },
57 { { NULL, 0 }, {}, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false },
58 { { NULL, 0 }, {}, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false },
59};
Elliott Hughesf0141df2015-10-12 12:44:23 -070060
61// __sF is exported for backwards compatibility. Until M, we didn't have symbols
62// for stdin/stdout/stderr; they were macros accessing __sF.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063FILE __sF[3] = {
Elliott Hughesbb46afd2015-12-04 18:03:12 -080064 std(__SRD, STDIN_FILENO),
65 std(__SWR, STDOUT_FILENO),
66 std(__SWR|__SNBF, STDERR_FILENO),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067};
Elliott Hughesf0141df2015-10-12 12:44:23 -070068
Elliott Hughes168667c2014-11-14 14:42:59 -080069FILE* stdin = &__sF[0];
70FILE* stdout = &__sF[1];
71FILE* stderr = &__sF[2];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072
Elliott Hughesbb46afd2015-12-04 18:03:12 -080073struct glue __sglue = { NULL, 3, __sF };
74static struct glue* lastglue = &__sglue;
75
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076static struct glue *
77moreglue(int n)
78{
79 struct glue *g;
80 FILE *p;
81 struct __sfileext *pext;
82 static FILE empty;
83 char *data;
84
85 data = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE)
86 + n * sizeof(struct __sfileext));
87 if (data == NULL)
88 return (NULL);
89 g = (struct glue *)data;
90 p = (FILE *)ALIGN(data + sizeof(*g));
91 pext = (struct __sfileext *)
92 (ALIGN(data + sizeof(*g)) + n * sizeof(FILE));
93 g->next = NULL;
94 g->niobs = n;
95 g->iobs = p;
96 while (--n >= 0) {
97 *p = empty;
98 _FILEEXT_SETUP(p, pext);
99 p++;
100 pext++;
101 }
102 return (g);
103}
104
105/*
106 * Find a free FILE for fopen et al.
107 */
108FILE *
109__sfp(void)
110{
111 FILE *fp;
112 int n;
113 struct glue *g;
114
Kenny Rootf5823402011-02-12 07:13:44 -0800115 _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
116 for (g = &__sglue; g != NULL; g = g->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
118 if (fp->_flags == 0)
119 goto found;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120 }
Kenny Rootf5823402011-02-12 07:13:44 -0800121
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 Project1dc9e472009-03-03 19:28:35 -0800130found:
131 fp->_flags = 1; /* reserve this slot; caller sets real flags */
Kenny Rootf5823402011-02-12 07:13:44 -0800132 _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133 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
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800147__LIBC_HIDDEN__ void __libc_stdio_cleanup(void) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148 /* (void) _fwalk(fclose); */
149 (void) _fwalk(__sflush); /* `cheating' */
150}