syscalls.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-02-11 Meco Man remove _gettimeofday_r() and _times_r()
  9. * 2020-02-13 Meco Man re-implement exit() and abort()
  10. * 2020-02-21 Meco Man improve and beautify syscalls
  11. * 2020-02-24 Meco Man fix bug of _isatty_r()
  12. */
  13. #include <reent.h>
  14. #include <rtthread.h>
  15. #include <stddef.h>
  16. #include <unistd.h>
  17. #include <sys/errno.h>
  18. #define DBG_TAG "newlib.syscalls"
  19. #define DBG_LVL DBG_INFO
  20. #include <rtdbg.h>
  21. #ifdef RT_USING_HEAP /* Memory routine */
  22. void *_malloc_r (struct _reent *ptr, size_t size)
  23. {
  24. void* result;
  25. result = (void*)rt_malloc (size);
  26. if (result == RT_NULL)
  27. {
  28. ptr->_errno = ENOMEM;
  29. }
  30. return result;
  31. }
  32. void *_realloc_r (struct _reent *ptr, void *old, size_t newlen)
  33. {
  34. void* result;
  35. result = (void*)rt_realloc (old, newlen);
  36. if (result == RT_NULL)
  37. {
  38. ptr->_errno = ENOMEM;
  39. }
  40. return result;
  41. }
  42. void *_calloc_r (struct _reent *ptr, size_t size, size_t len)
  43. {
  44. void* result;
  45. result = (void*)rt_calloc (size, len);
  46. if (result == RT_NULL)
  47. {
  48. ptr->_errno = ENOMEM;
  49. }
  50. return result;
  51. }
  52. void _free_r (struct _reent *ptr, void *addr)
  53. {
  54. rt_free (addr);
  55. }
  56. #else
  57. void *
  58. _sbrk_r(struct _reent *ptr, ptrdiff_t incr)
  59. {
  60. LOG_E("Please enable RT_USING_HEAP or RT_USING_LIBC");
  61. RT_ASSERT(0);
  62. return RT_NULL;
  63. }
  64. #endif /*RT_USING_HEAP*/
  65. void __libc_init_array(void)
  66. {
  67. /* we not use __libc init_aray to initialize C++ objects */
  68. /* __libc_init_array is ARM code, not Thumb; it will cause a hardfault. */
  69. }
  70. #ifdef RT_USING_LIBC
  71. #include <reent.h>
  72. #include <stdio.h>
  73. #ifdef RT_USING_DFS
  74. #include <dfs_posix.h>
  75. #endif
  76. #ifdef RT_USING_MODULE
  77. #include <dlmodule.h>
  78. #endif
  79. #define DBG_TAG "newlib.syscalls"
  80. #define DBG_LVL DBG_INFO
  81. #include <rtdbg.h>
  82. /* Reentrant versions of system calls. */
  83. #ifndef _REENT_ONLY
  84. int *__errno ()
  85. {
  86. return _rt_errno();
  87. }
  88. #endif
  89. int _getpid_r(struct _reent *ptr)
  90. {
  91. return 0;
  92. }
  93. int _close_r(struct _reent *ptr, int fd)
  94. {
  95. #ifndef RT_USING_DFS
  96. /* return "not supported" */
  97. ptr->_errno = ENOTSUP;
  98. return -1;
  99. #else
  100. return close(fd);
  101. #endif
  102. }
  103. int _execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
  104. {
  105. /* return "not supported" */
  106. ptr->_errno = ENOTSUP;
  107. return -1;
  108. }
  109. int _fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
  110. {
  111. /* return "not supported" */
  112. ptr->_errno = ENOTSUP;
  113. return -1;
  114. }
  115. int _fork_r(struct _reent *ptr)
  116. {
  117. /* return "not supported" */
  118. ptr->_errno = ENOTSUP;
  119. return -1;
  120. }
  121. int _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
  122. {
  123. /* return "not supported" */
  124. ptr->_errno = ENOTSUP;
  125. return -1;
  126. }
  127. int _isatty_r(struct _reent *ptr, int fd)
  128. {
  129. if (fd >=0 && fd < 3)
  130. {
  131. return 1;
  132. }
  133. else
  134. {
  135. return 0;
  136. }
  137. }
  138. int _kill_r(struct _reent *ptr, int pid, int sig)
  139. {
  140. /* return "not supported" */
  141. ptr->_errno = ENOTSUP;
  142. return -1;
  143. }
  144. int _link_r(struct _reent *ptr, const char *old, const char *new)
  145. {
  146. /* return "not supported" */
  147. ptr->_errno = ENOTSUP;
  148. return -1;
  149. }
  150. _off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
  151. {
  152. #ifndef RT_USING_DFS
  153. /* return "not supported" */
  154. ptr->_errno = ENOTSUP;
  155. return -1;
  156. #else
  157. _off_t rc;
  158. rc = lseek(fd, pos, whence);
  159. return rc;
  160. #endif
  161. }
  162. int _mkdir_r(struct _reent *ptr, const char *name, int mode)
  163. {
  164. #ifndef RT_USING_DFS
  165. /* return "not supported" */
  166. ptr->_errno = ENOTSUP;
  167. return -1;
  168. #else
  169. int rc;
  170. rc = mkdir(name, mode);
  171. return rc;
  172. #endif
  173. }
  174. int _open_r(struct _reent *ptr, const char *file, int flags, int mode)
  175. {
  176. #ifndef RT_USING_DFS
  177. /* return "not supported" */
  178. ptr->_errno = ENOTSUP;
  179. return -1;
  180. #else
  181. int rc;
  182. rc = open(file, flags, mode);
  183. return rc;
  184. #endif
  185. }
  186. _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
  187. {
  188. #ifndef RT_USING_DFS
  189. /* return "not supported" */
  190. ptr->_errno = ENOTSUP;
  191. return -1;
  192. #else
  193. _ssize_t rc;
  194. if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO)
  195. {
  196. LOG_W("Do not invoke standard input before initializing libc");
  197. return 0;
  198. }
  199. rc = read(fd, buf, nbytes);
  200. return rc;
  201. #endif
  202. }
  203. int _rename_r(struct _reent *ptr, const char *old, const char *new)
  204. {
  205. #ifndef RT_USING_DFS
  206. /* return "not supported" */
  207. ptr->_errno = ENOTSUP;
  208. return -1;
  209. #else
  210. int rc;
  211. rc = rename(old, new);
  212. return rc;
  213. #endif
  214. }
  215. int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
  216. {
  217. #ifndef RT_USING_DFS
  218. /* return "not supported" */
  219. ptr->_errno = ENOTSUP;
  220. return -1;
  221. #else
  222. int rc;
  223. rc = stat(file, pstat);
  224. return rc;
  225. #endif
  226. }
  227. int _unlink_r(struct _reent *ptr, const char *file)
  228. {
  229. #ifndef RT_USING_DFS
  230. /* return "not supported" */
  231. ptr->_errno = ENOTSUP;
  232. return -1;
  233. #else
  234. return unlink(file);
  235. #endif
  236. }
  237. int _wait_r(struct _reent *ptr, int *status)
  238. {
  239. /* return "not supported" */
  240. ptr->_errno = ENOTSUP;
  241. return -1;
  242. }
  243. _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
  244. {
  245. #ifndef RT_USING_DFS
  246. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  247. if (STDOUT_FILENO == fd)
  248. {
  249. rt_device_t console;
  250. console = rt_console_get_device();
  251. if (console) return rt_device_write(console, -1, buf, nbytes);
  252. }
  253. return 0;
  254. #else
  255. /* return "not supported" */
  256. ptr->_errno = ENOTSUP;
  257. return -1;
  258. #endif /*RT_USING_DEVICE*/
  259. #else
  260. _ssize_t rc;
  261. if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO)
  262. {
  263. LOG_W("Do not invoke standard output before initializing libc");
  264. return 0;
  265. }
  266. rc = write(fd, buf, nbytes);
  267. return rc;
  268. #endif
  269. }
  270. void _system(const char *s)
  271. {
  272. extern int __rt_libc_system(const char *string);
  273. __rt_libc_system(s);
  274. }
  275. /* for exit() and abort() */
  276. __attribute__ ((noreturn)) void _exit (int status)
  277. {
  278. extern void __rt_libc_exit(int status);
  279. __rt_libc_exit(status);
  280. while(1);
  281. }
  282. mode_t umask(mode_t mask)
  283. {
  284. return 022;
  285. }
  286. int flock(int fd, int operation)
  287. {
  288. return 0;
  289. }
  290. /*
  291. These functions are implemented and replaced by the 'common/time.c' file
  292. int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
  293. _CLOCK_T_ _times_r(struct _reent *ptr, struct tms *ptms);
  294. */
  295. #endif /* RT_USING_LIBC */