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