1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
10 /* This is pretty much straight from uClibc, but with one important
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.
21 /**********************************************************************/
23 #ifdef __UCLIBC_HAS_WCHAR__
24 #define __STDIO_FILE_INIT_WUNGOT { 0, 0 },
26 #define __STDIO_FILE_INIT_WUNGOT
29 #ifdef __STDIO_GETC_MACRO
30 # define __STDIO_FILE_INIT_BUFGETC(x) x,
32 # define __STDIO_FILE_INIT_BUFGETC(x)
35 #ifdef __STDIO_PUTC_MACRO
36 # define __STDIO_FILE_INIT_BUFPUTC(x) x,
38 # define __STDIO_FILE_INIT_BUFPUTC(x)
41 #ifdef __STDIO_HAS_OPENLIST
42 #define __STDIO_FILE_INIT_NEXT(next) (next),
44 #define __STDIO_FILE_INIT_NEXT(next)
47 #ifdef __STDIO_BUFFERS
48 #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
49 (buf), (buf)+(bufsize), (buf), (buf),
51 #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize)
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 },
58 #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream)
61 #ifdef __STDIO_MBSTATE
62 #define __STDIO_FILE_INIT_MBSTATE \
65 #define __STDIO_FILE_INIT_MBSTATE
68 #ifdef __UCLIBC_HAS_XLOCALE__
69 #define __STDIO_FILE_INIT_UNUSED \
72 #define __STDIO_FILE_INIT_UNUSED
75 #ifdef __UCLIBC_HAS_THREADS__
76 #ifdef __USE_STDIO_FUTEXES__
77 #define __STDIO_FILE_INIT_THREADSAFE \
78 2, _LIBC_LOCK_RECURSIVE_INITIALIZER,
80 #define __STDIO_FILE_INIT_THREADSAFE \
81 2, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
84 #define __STDIO_FILE_INIT_THREADSAFE
87 #define __STDIO_INIT_FILE_STRUCT(stream, flags, filedes, next, buf, bufsize) \
89 { 0, 0 }, /* ungot[2] (no wchar) or ungot_width[2] (wchar)*/ \
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 */
102 /**********************************************************************/
103 /* First we need the standard files. */
105 #ifdef __STDIO_BUFFERS
106 static unsigned char _fixed_buffers[2 * BUFSIZ];
109 static FILE _stdio_streams[] = {
110 __STDIO_INIT_FILE_STRUCT(_stdio_streams[0], \
111 __FLAG_LBF|__FLAG_READONLY, \
113 _stdio_streams + 1, \
116 __STDIO_INIT_FILE_STRUCT(_stdio_streams[1], \
117 __FLAG_LBF|__FLAG_WRITEONLY, \
119 _stdio_streams + 2, \
120 _fixed_buffers + BUFSIZ, \
122 __STDIO_INIT_FILE_STRUCT(_stdio_streams[2], \
123 __FLAG_NBF|__FLAG_WRITEONLY, \
130 FILE *stdin = _stdio_streams;
131 FILE *stdout = _stdio_streams + 1;
132 FILE *stderr = _stdio_streams + 2;
134 #ifdef __STDIO_GETC_MACRO
135 FILE *__stdin = _stdio_streams; /* For getchar() macro. */
137 #ifdef __STDIO_PUTC_MACRO
138 FILE *__stdout = _stdio_streams + 1; /* For putchar() macro. */
139 /* FILE *__stderr = _stdio_streams + 2; */
142 /**********************************************************************/
143 #ifdef __STDIO_HAS_OPENLIST
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.
156 FILE *_stdio_openlist = _stdio_streams;
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;
167 /**********************************************************************/
168 #ifdef __UCLIBC_HAS_THREADS__
170 /* 2 if threading not initialized and 0 otherwise; */
171 int _stdio_user_locking = 2;
173 #ifndef __USE_STDIO_FUTEXES__
174 void attribute_hidden __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m)
176 const __UCLIBC_MUTEX_STATIC(__stdio_mutex_initializer,
177 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
179 memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
184 /**********************************************************************/
186 /* We assume here that we are the only remaining thread. */
187 void _stdio_term(void)
189 #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
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.
197 STDIO_INIT_MUTEX(_stdio_openlist_add_lock);
199 #ifdef __STDIO_BUFFERS
200 STDIO_INIT_MUTEX(_stdio_openlist_del_lock);
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.
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.
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);
221 ptr->__user_locking = 1; /* Set locking mode to "by caller". */
222 STDIO_INIT_MUTEX(ptr->__lock); /* Shouldn't be necessary, but... */
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
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.
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);
242 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
243 /* Actually close all custom streams to perform any special cleanup. */
244 if (ptr->__cookie != &ptr->__filedes) {
253 #if defined __STDIO_BUFFERS || !defined __UCLIBC__
254 void _stdio_init(void)
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. */
261 _stdio_streams[0].__modeflags ^= __FLAG_LBF;
263 _stdio_streams[1].__modeflags ^= __FLAG_LBF;
264 __set_errno(old_errno);
267 # if defined(__STDIO_BUFFERS) && defined(__UCLIBC_HAS_THREADS__)
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;
280 /* _stdio_term is done automatically when exiting if stdio is used.
281 * See misc/internals/__uClibc_main.c and and stdlib/atexit.c. */
287 /**********************************************************************/
289 #if !(__MASK_READING & __FLAG_UNGOT)
290 #error Assumption violated about __MASK_READING and __FLAG_UNGOT
295 void attribute_hidden _stdio_validate_FILE(const FILE *stream)
297 #ifdef __UCLIBC_HAS_THREADS__
298 assert(((unsigned int)(stream->__user_locking)) <= 2);
301 #warning Define a constant for minimum possible valid __filedes?
302 assert(stream->__filedes >= -3);
304 if (stream->__filedes < 0) {
305 /* assert((stream->__filedes != -1) */
306 /* #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
307 /* || (stream->__cookie == &stream->__filedes) /\* custom *\/ */
310 /* assert((stream->__filedes == -1) || __STDIO_STREAM_IS_FBF(stream)); */
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));
320 #ifdef __STDIO_STREAM_IS_FAKE_VSWSCANF
321 assert(!__STDIO_STREAM_IS_FAKE_VSWSCANF(stream)
322 || __STDIO_STREAM_IS_WIDE(stream));
326 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
327 if (stream->__cookie != &stream->__filedes) { /* custom */
328 assert(stream->__filedes == -1);
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)));
337 /* The following impossible case is used to disable a stream. */
338 if ((stream->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
339 == (__FLAG_READONLY|__FLAG_WRITEONLY)
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);
349 # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
350 assert(stream->__bufgetc_u == stream->__bufstart);
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));
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));
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));
375 assert((stream->__modeflags & __MASK_BUFMODE) <= __FLAG_NBF);
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);
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);
391 assert(!(stream->__modeflags & __FLAG_UNGOT));
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);
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);
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);
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);
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);
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)
447 #ifdef __STDIO_BUFFERS
448 # ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
449 assert(stream->__bufputc_u == stream->__bufstart);
451 # ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
452 assert(stream->__bufgetc_u == stream->__bufstart);
457 /* TODO -- filepos? ungot_width? filedes? nextopen? */