_stdio.c 13 KB

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