blob: 6e20562cecd4cd83275a9a3936f02bc377f33d4f [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 Hughes29ee6392015-12-07 11:07:15 -080055// TODO: when we no longer have to support both clang and GCC, we can simplify all this.
56#define SBUF_INIT {0,0}
57#if defined(__LP64__)
58#define MBSTATE_T_INIT {{0},{0}}
59#else
60#define MBSTATE_T_INIT {{0}}
61#endif
62#define WCHAR_IO_DATA_INIT {MBSTATE_T_INIT,MBSTATE_T_INIT,{0},0,0}
63
Elliott Hughesbb46afd2015-12-04 18:03:12 -080064static struct __sfileext __sFext[3] = {
Elliott Hughes29ee6392015-12-07 11:07:15 -080065 { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false },
66 { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false },
67 { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false },
Elliott Hughesbb46afd2015-12-04 18:03:12 -080068};
Elliott Hughesf0141df2015-10-12 12:44:23 -070069
70// __sF is exported for backwards compatibility. Until M, we didn't have symbols
71// for stdin/stdout/stderr; they were macros accessing __sF.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072FILE __sF[3] = {
Elliott Hughesbb46afd2015-12-04 18:03:12 -080073 std(__SRD, STDIN_FILENO),
74 std(__SWR, STDOUT_FILENO),
75 std(__SWR|__SNBF, STDERR_FILENO),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076};
Elliott Hughesf0141df2015-10-12 12:44:23 -070077
Elliott Hughes168667c2014-11-14 14:42:59 -080078FILE* stdin = &__sF[0];
79FILE* stdout = &__sF[1];
80FILE* stderr = &__sF[2];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
Elliott Hughesbb46afd2015-12-04 18:03:12 -080082struct glue __sglue = { NULL, 3, __sF };
83static struct glue* lastglue = &__sglue;
84
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085static struct glue *
86moreglue(int n)
87{
88 struct glue *g;
89 FILE *p;
90 struct __sfileext *pext;
91 static FILE empty;
92 char *data;
93
94 data = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE)
95 + n * sizeof(struct __sfileext));
96 if (data == NULL)
97 return (NULL);
98 g = (struct glue *)data;
99 p = (FILE *)ALIGN(data + sizeof(*g));
100 pext = (struct __sfileext *)
101 (ALIGN(data + sizeof(*g)) + n * sizeof(FILE));
102 g->next = NULL;
103 g->niobs = n;
104 g->iobs = p;
105 while (--n >= 0) {
106 *p = empty;
107 _FILEEXT_SETUP(p, pext);
108 p++;
109 pext++;
110 }
111 return (g);
112}
113
114/*
115 * Find a free FILE for fopen et al.
116 */
117FILE *
118__sfp(void)
119{
120 FILE *fp;
121 int n;
122 struct glue *g;
123
Kenny Rootf5823402011-02-12 07:13:44 -0800124 _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
125 for (g = &__sglue; g != NULL; g = g->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
127 if (fp->_flags == 0)
128 goto found;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129 }
Kenny Rootf5823402011-02-12 07:13:44 -0800130
131 /* release lock while mallocing */
132 _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
133 if ((g = moreglue(NDYNAMIC)) == NULL)
134 return (NULL);
135 _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
136 lastglue->next = g;
137 lastglue = g;
138 fp = g->iobs;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139found:
140 fp->_flags = 1; /* reserve this slot; caller sets real flags */
Kenny Rootf5823402011-02-12 07:13:44 -0800141 _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142 fp->_p = NULL; /* no current pointer */
143 fp->_w = 0; /* nothing to read or write */
144 fp->_r = 0;
145 fp->_bf._base = NULL; /* no buffer */
146 fp->_bf._size = 0;
147 fp->_lbfsize = 0; /* not line buffered */
148 fp->_file = -1; /* no file */
149/* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
150 fp->_lb._base = NULL; /* no line buffer */
151 fp->_lb._size = 0;
152 _FILEEXT_INIT(fp);
153 return (fp);
154}
155
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800156__LIBC_HIDDEN__ void __libc_stdio_cleanup(void) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157 /* (void) _fwalk(fclose); */
158 (void) _fwalk(__sflush); /* `cheating' */
159}