1 /* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7 
8 #include "_stdio.h"
9 
10 /* This is pretty much straight from uClibc, but with one important
11  * difference.
12  *
13  * We now initialize the locking flag to user locking instead of
14  * auto locking (i.e. FSETLOCKING_BYCALLER vs FSETLOCKING_INTERNAL).
15  * This greatly benefits non-threading applications linked to a
16  * shared thread-enabled library.  In threading applications, we
17  * walk the stdio open file list and reset the locking mode
18  * appropriately when the thread subsystem is initialized.
19  */
20 
21 /**********************************************************************/
22 
23 #ifdef __UCLIBC_HAS_WCHAR__
24 #define __STDIO_FILE_INIT_WUNGOT		{ 0, 0 },
25 #else
26 #define __STDIO_FILE_INIT_WUNGOT
27 #endif
28 
29 #ifdef __STDIO_GETC_MACRO
30 # define __STDIO_FILE_INIT_BUFGETC(x) x,
31 #else
32 # define __STDIO_FILE_INIT_BUFGETC(x)
33 #endif
34 
35 #ifdef __STDIO_PUTC_MACRO
36 # define __STDIO_FILE_INIT_BUFPUTC(x) x,
37 #else
38 # define __STDIO_FILE_INIT_BUFPUTC(x)
39 #endif
40 
41 #ifdef __STDIO_HAS_OPENLIST
42 #define __STDIO_FILE_INIT_NEXT(next)	(next),
43 #else
44 #define __STDIO_FILE_INIT_NEXT(next)
45 #endif
46 
47 #ifdef __STDIO_BUFFERS
48 #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
49 	(buf), (buf)+(bufsize), (buf), (buf),
50 #else
51 #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize)
52 #endif
53 
54 #ifdef __STDIO_MBSTATE
55 #define __STDIO_FILE_INIT_MBSTATE \
56 	{ 0, 0 },
57 #else
58 #define __STDIO_FILE_INIT_MBSTATE
59 #endif
60 
61 #ifdef __UCLIBC_HAS_XLOCALE__
62 #define __STDIO_FILE_INIT_UNUSED \
63 	NULL,
64 #else
65 #define __STDIO_FILE_INIT_UNUSED
66 #endif
67 
68 #ifdef __UCLIBC_HAS_THREADS__
69 #ifdef __USE_STDIO_FUTEXES__
70 #define __STDIO_FILE_INIT_THREADSAFE \
71 	2, _LIBC_LOCK_RECURSIVE_INITIALIZER,
72 #else
73 #define __STDIO_FILE_INIT_THREADSAFE \
74 	2, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
75 #endif
76 #else
77 #define __STDIO_FILE_INIT_THREADSAFE
78 #endif
79 
80 #define __STDIO_INIT_FILE_STRUCT(stream, flags, filedes, next, buf, bufsize) \
81 	{ (flags), \
82 	{ 0, 0 }, /* ungot[2] (no wchar) or ungot_width[2] (wchar)*/ \
83 	(filedes), \
84 	__STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
85 	__STDIO_FILE_INIT_BUFGETC((buf)) \
86 	__STDIO_FILE_INIT_BUFPUTC((buf)) \
87 	__STDIO_FILE_INIT_NEXT(next) \
88 	__STDIO_FILE_INIT_WUNGOT \
89 	__STDIO_FILE_INIT_MBSTATE \
90 	__STDIO_FILE_INIT_UNUSED \
91 	__STDIO_FILE_INIT_THREADSAFE \
92 } /* TODO: builtin buf */
93 
94 /**********************************************************************/
95 /* First we need the standard files. */
96 
97 #ifdef __STDIO_BUFFERS
98 static unsigned char _fixed_buffers[2 * BUFSIZ];
99 #endif
100 
101 static FILE _stdio_streams[] = {
102 	__STDIO_INIT_FILE_STRUCT(_stdio_streams[0], \
103 							 __FLAG_LBF|__FLAG_READONLY, \
104 							 0, \
105 							 _stdio_streams + 1, \
106 							 _fixed_buffers, \
107 							 BUFSIZ ),
108 	__STDIO_INIT_FILE_STRUCT(_stdio_streams[1], \
109 							 __FLAG_LBF|__FLAG_WRITEONLY, \
110 							 1, \
111 							 _stdio_streams + 2, \
112 							 _fixed_buffers + BUFSIZ, \
113 							 BUFSIZ ),
114 	__STDIO_INIT_FILE_STRUCT(_stdio_streams[2], \
115 							 __FLAG_NBF|__FLAG_WRITEONLY, \
116 							 2, \
117 							 NULL, \
118 							 NULL, \
119 							 0 )
120 };
121 
122 FILE *stdin  = _stdio_streams;
123 FILE *stdout = _stdio_streams + 1;
124 FILE *stderr = _stdio_streams + 2;
125 
126 #ifdef __STDIO_GETC_MACRO
127 FILE *__stdin = _stdio_streams;		 /* For getchar() macro. */
128 #endif
129 #ifdef __STDIO_PUTC_MACRO
130 FILE *__stdout = _stdio_streams + 1; /* For putchar() macro. */
131 /* FILE *__stderr = _stdio_streams + 2; */
132 #endif
133 
134 /**********************************************************************/
135 #ifdef __STDIO_HAS_OPENLIST
136 
137 /* In certain configurations, we need to keep a list of open files.
138  * 1) buffering enabled - We need to initialize the buffering mode
139  *       (full or line buffering) of stdin and stdout.  We also
140  *       need to flush all write buffers prior to normal termination.
141  * 2) custom streams - Even if we aren't buffering in the library
142  *       itself, we need to fclose() all custom streams when terminating
143  *       so that any special cleanup is done.
144  * 3) threads enabled - We need to be able to reset the locking mode
145  *       of all open streams when the threading system is initialized.
146  */
147 
148 FILE *_stdio_openlist = _stdio_streams;
149 
150 # ifdef __UCLIBC_HAS_THREADS__
151 __UCLIBC_IO_MUTEX_INIT(_stdio_openlist_add_lock);
152 #  ifdef __STDIO_BUFFERS
153 __UCLIBC_IO_MUTEX_INIT(_stdio_openlist_del_lock);
154 volatile int _stdio_openlist_use_count = 0;
155 int _stdio_openlist_del_count = 0;
156 #  endif
157 # endif
158 #endif
159 /**********************************************************************/
160 #ifdef __UCLIBC_HAS_THREADS__
161 
162 /* 2 if threading not initialized and 0 otherwise; */
163 int _stdio_user_locking = 2;
164 
165 #ifndef __USE_STDIO_FUTEXES__
__stdio_init_mutex(__UCLIBC_MUTEX_TYPE * m)166 void attribute_hidden __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m)
167 {
168 	const __UCLIBC_MUTEX_STATIC(__stdio_mutex_initializer,
169 		PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
170 
171 	memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
172 }
173 #endif
174 
175 #endif
176 /**********************************************************************/
177 
178 /* We assume here that we are the only remaining thread. */
_stdio_term(void)179 void _stdio_term(void)
180 {
181 #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
182 	register FILE *ptr;
183 
184 #ifdef __UCLIBC_HAS_THREADS__
185 	/* First, make sure the open file list is unlocked.  If it was
186 	 * locked, then I suppose there is a chance that a pointer in the
187 	 * chain might be corrupt due to a partial store.
188 	 */
189 	STDIO_INIT_MUTEX(_stdio_openlist_add_lock);
190 #ifdef __STDIO_BUFFERS
191 	STDIO_INIT_MUTEX(_stdio_openlist_del_lock);
192 #endif
193 
194 	/* Next we need to worry about the streams themselves.  If a stream
195 	 * is currently locked, then it may be in an invalid state.  So we
196 	 * 'disable' it in case a custom stream is stacked on top of it.
197 	 * Then we reinitialize the locks.
198 	 */
199 	for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
200 		if (__STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(ptr)) {
201 			/* The stream is already locked, so we don't want to touch it.
202 			 * However, if we have custom streams, we can't just close it
203 			 * or leave it locked since a custom stream may be stacked
204 			 * on top of it.  So we do unlock it, while also disabling it.
205 			 */
206 			ptr->__modeflags = (__FLAG_READONLY|__FLAG_WRITEONLY);
207 			__STDIO_STREAM_DISABLE_GETC(ptr);
208 			__STDIO_STREAM_DISABLE_PUTC(ptr);
209 			__STDIO_STREAM_INIT_BUFREAD_BUFPOS(ptr);
210 		}
211 
212 		ptr->__user_locking = 1; /* Set locking mode to "by caller". */
213 		STDIO_INIT_MUTEX(ptr->__lock); /* Shouldn't be necessary, but... */
214 	}
215 #endif
216 
217 	/* Finally, flush all writing streams and shut down all custom streams.
218 	 * NOTE: We assume that any stacking by custom streams is done on top
219 	 *       of streams previously allocated, and hence further down the
220 	 *       list.  Otherwise we have no way of knowing the order in which
221 	 *       to shut them down.
222 	 *       Remember that freopen() counts as a new allocation here, even
223 	 *       though the stream is reused.  That's because it moves the
224 	 *       stream to the head of the list.
225 	 */
226 	for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
227 #ifdef __STDIO_BUFFERS
228 		/* Write any pending buffered chars. */
229 		if (__STDIO_STREAM_IS_WRITING(ptr)) {
230 			__STDIO_COMMIT_WRITE_BUFFER(ptr);
231 		}
232 #endif
233 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
234 		/* Actually close all custom streams to perform any special cleanup. */
235 		if (__STDIO_STREAM_IS_CUSTOM(ptr)) {
236 			__CLOSE(ptr);
237 		}
238 #endif
239 	}
240 
241 #endif
242 }
243 
244 #if defined __STDIO_BUFFERS || !defined __UCLIBC__
_stdio_init(void)245 void _stdio_init(void)
246 {
247 #ifdef __STDIO_BUFFERS
248 	int old_errno = errno;
249 	/* stdin and stdout uses line buffering when connected to a tty. */
250 	if (!isatty(0))
251 		_stdio_streams[0].__modeflags ^= __FLAG_LBF;
252 	if (!isatty(1))
253 		_stdio_streams[1].__modeflags ^= __FLAG_LBF;
254 	__set_errno(old_errno);
255 #endif
256 #ifndef __UCLIBC__
257 	/* _stdio_term is done automatically when exiting if stdio is used.
258 	 * See misc/internals/__uClibc_main.c and and stdlib/atexit.c. */
259 	atexit(_stdio_term);
260 #endif
261 }
262 #endif
263 
264 /**********************************************************************/
265 
266 #if !(__MASK_READING & __FLAG_UNGOT)
267 #error Assumption violated about __MASK_READING and __FLAG_UNGOT
268 #endif
269 
270 #ifndef NDEBUG
271 
_stdio_validate_FILE(const FILE * stream)272 void attribute_hidden _stdio_validate_FILE(const FILE *stream)
273 {
274 #ifdef __UCLIBC_HAS_THREADS__
275 	assert(((unsigned int)(stream->__user_locking)) <= 2);
276 #endif
277 
278 #warning Define a constant for minimum possible valid __filedes?
279 	assert(stream->__filedes >= -4);
280 
281 	if (stream->__filedes < 0) {
282 /* 		assert((stream->__filedes == -1) || __STDIO_STREAM_IS_FBF(stream)); */
283 
284 		assert(!__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream)
285 			   || __STDIO_STREAM_IS_NARROW(stream));
286 		assert(!__STDIO_STREAM_IS_FAKE_VSSCANF(stream)
287 			   || __STDIO_STREAM_IS_NARROW(stream));
288 #ifdef __STDIO_STREAM_IS_FAKE_VSWPRINTF
289 		assert(!__STDIO_STREAM_IS_FAKE_VSWPRINTF(stream)
290 			   || __STDIO_STREAM_IS_WIDE(stream));
291 #endif
292 #ifdef __STDIO_STREAM_IS_FAKE_VSWSCANF
293 		assert(!__STDIO_STREAM_IS_FAKE_VSWSCANF(stream)
294 			   || __STDIO_STREAM_IS_WIDE(stream));
295 #endif
296 	}
297 
298 	/* Can not be both narrow and wide oriented at the same time. */
299 	assert(!(__STDIO_STREAM_IS_NARROW(stream)
300 			 && __STDIO_STREAM_IS_WIDE(stream)));
301 
302 
303 	/* The following impossible case is used to disable a stream. */
304 	if ((stream->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
305 		== (__FLAG_READONLY|__FLAG_WRITEONLY)
306 		) {
307 		assert(stream->__modeflags == (__FLAG_READONLY|__FLAG_WRITEONLY));
308 		assert(stream->__filedes == -1);
309 #ifdef __STDIO_BUFFERS
310 		assert(stream->__bufpos == stream->__bufstart);
311 		assert(stream->__bufread == stream->__bufstart);
312 # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
313 		assert(stream->__bufputc_u == stream->__bufstart);
314 # endif
315 # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
316 		assert(stream->__bufgetc_u == stream->__bufstart);
317 # endif
318 #endif
319 	}
320 
321 	if (__STDIO_STREAM_IS_READONLY(stream)) {
322 /* 		assert(!__STDIO_STREAM_IS_WRITEONLY(stream)); */
323 		assert(!__STDIO_STREAM_IS_WRITING(stream));
324 		if (stream->__modeflags & __FLAG_UNGOT) {
325 			assert(((unsigned)(stream->__ungot[1])) <= 1);
326 			assert(!__FEOF_UNLOCKED(stream));
327 		}
328 	}
329 
330 	if (__STDIO_STREAM_IS_WRITEONLY(stream)) {
331 /* 		assert(!__STDIO_STREAM_IS_READONLY(stream)); */
332 		assert(!__STDIO_STREAM_IS_READING(stream));
333 		assert(!(stream->__modeflags & __FLAG_UNGOT));
334 	}
335 
336 	if (__STDIO_STREAM_IS_NBF(stream)) {
337 		/* We require that all non buffered streams have no buffer. */
338 		assert(!__STDIO_STREAM_BUFFER_SIZE(stream));
339 	}
340 
341 	assert((stream->__modeflags & __MASK_BUFMODE) <= __FLAG_NBF);
342 
343 #ifdef __STDIO_BUFFERS
344 	/* Ensure __bufstart <= __bufpos <= __bufend. */
345 	assert(stream->__bufpos >= stream->__bufstart);
346 	assert(stream->__bufpos <= stream->__bufend);
347 	/* Ensure __bufstart <= __bufread <= __bufend. */
348 	assert(stream->__bufread >= stream->__bufstart);
349 	assert(stream->__bufread <= stream->__bufend);
350 #endif
351 
352 	/* If EOF, then we must have no buffered readable or ungots. */
353 	if (__FEOF_UNLOCKED(stream)) {
354 #ifdef __STDIO_BUFFERS
355 		assert(stream->__bufpos == stream->__bufread);
356 #endif
357 		assert(!(stream->__modeflags & __FLAG_UNGOT));
358 	}
359 
360 
361 	if (!__STDIO_STREAM_IS_WRITING(stream)) {
362 #ifdef __STDIO_BUFFERS
363 		/* If not writing, then putc macro must be disabled. */
364 # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
365 		assert(stream->__bufputc_u == stream->__bufstart);
366 # endif
367 #endif
368 	}
369 
370 	if (!__STDIO_STREAM_IS_READING(stream)) {
371 		/* If not reading, then can not have ungots. */
372 		assert(!(stream->__modeflags & __FLAG_UNGOT));
373 #ifdef __STDIO_BUFFERS
374 		/* Ensure __bufread == __bufstart. */
375 		assert(stream->__bufread == stream->__bufstart);
376 		/* If not reading, then getc macro must be disabled. */
377 # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
378 		assert(stream->__bufgetc_u == stream->__bufstart);
379 # endif
380 #endif
381 	}
382 
383 	if (__STDIO_STREAM_IS_READING(stream)) {
384 		assert(!__STDIO_STREAM_IS_WRITING(stream));
385 #ifdef __STDIO_BUFFERS
386 		/* Ensure __bufpos <= __bufread. */
387 		assert(stream->__bufpos <= stream->__bufread);
388 
389 		/* Ensure __bufgetc_u is valid. */
390 # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
391 		assert(stream->__bufgetc_u >= stream->__bufstart);
392 		assert(stream->__bufgetc_u <= stream->__bufread);
393 # endif
394 
395 #endif
396 	}
397 
398 	if (__STDIO_STREAM_IS_WRITING(stream)) {
399 		assert(!__STDIO_STREAM_IS_READING(stream));
400 #ifdef __STDIO_BUFFERS
401 # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
402 		assert(stream->__bufputc_u >= stream->__bufstart);
403 		assert(stream->__bufputc_u <= stream->__bufend);
404 # endif
405 #endif
406 	}
407 
408 	/* If have an ungotten char, then getc (and putc) must be disabled. */
409 	/* Also, wide streams must have the getc/putc macros disabled. */
410 	if ((stream->__modeflags & __FLAG_UNGOT)
411 		|| __STDIO_STREAM_IS_WIDE(stream)
412 		) {
413 #ifdef __STDIO_BUFFERS
414 # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
415 		assert(stream->__bufputc_u == stream->__bufstart);
416 # endif
417 # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
418 		assert(stream->__bufgetc_u == stream->__bufstart);
419 # endif
420 #endif
421 	}
422 
423 	/* TODO -- filepos?  ungot_width?  filedes?  nextopen? */
424 }
425 
426 #endif
427