_stdio.c 13 KB

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