syscalls.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include "libc.h"
  74. #ifdef RT_USING_DFS
  75. #include <dfs_posix.h>
  76. #endif
  77. #ifdef RT_USING_MODULE
  78. #include <dlmodule.h>
  79. #endif
  80. /* Reentrant versions of system calls. */
  81. #ifndef _REENT_ONLY
  82. int *__errno ()
  83. {
  84. return _rt_errno();
  85. }
  86. #endif
  87. int _getpid_r(struct _reent *ptr)
  88. {
  89. return 0;
  90. }
  91. int _close_r(struct _reent *ptr, int fd)
  92. {
  93. #ifndef RT_USING_DFS
  94. /* return "not supported" */
  95. ptr->_errno = ENOTSUP;
  96. return -1;
  97. #else
  98. return close(fd);
  99. #endif
  100. }
  101. int _execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
  102. {
  103. /* return "not supported" */
  104. ptr->_errno = ENOTSUP;
  105. return -1;
  106. }
  107. int _fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
  108. {
  109. /* return "not supported" */
  110. ptr->_errno = ENOTSUP;
  111. return -1;
  112. }
  113. int _fork_r(struct _reent *ptr)
  114. {
  115. /* return "not supported" */
  116. ptr->_errno = ENOTSUP;
  117. return -1;
  118. }
  119. int _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
  120. {
  121. /* return "not supported" */
  122. ptr->_errno = ENOTSUP;
  123. return -1;
  124. }
  125. int _isatty_r(struct _reent *ptr, int fd)
  126. {
  127. if (fd >=0 && fd < 3)
  128. {
  129. return 1;
  130. }
  131. else
  132. {
  133. return 0;
  134. }
  135. }
  136. int _kill_r(struct _reent *ptr, int pid, int sig)
  137. {
  138. /* return "not supported" */
  139. ptr->_errno = ENOTSUP;
  140. return -1;
  141. }
  142. int _link_r(struct _reent *ptr, const char *old, const char *new)
  143. {
  144. /* return "not supported" */
  145. ptr->_errno = ENOTSUP;
  146. return -1;
  147. }
  148. _off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
  149. {
  150. #ifndef RT_USING_DFS
  151. /* return "not supported" */
  152. ptr->_errno = ENOTSUP;
  153. return -1;
  154. #else
  155. _off_t rc;
  156. rc = lseek(fd, pos, whence);
  157. return rc;
  158. #endif
  159. }
  160. int _mkdir_r(struct _reent *ptr, const char *name, int mode)
  161. {
  162. #ifndef RT_USING_DFS
  163. /* return "not supported" */
  164. ptr->_errno = ENOTSUP;
  165. return -1;
  166. #else
  167. int rc;
  168. rc = mkdir(name, mode);
  169. return rc;
  170. #endif
  171. }
  172. int _open_r(struct _reent *ptr, const char *file, int flags, int mode)
  173. {
  174. #ifndef RT_USING_DFS
  175. /* return "not supported" */
  176. ptr->_errno = ENOTSUP;
  177. return -1;
  178. #else
  179. int rc;
  180. rc = open(file, flags, mode);
  181. return rc;
  182. #endif
  183. }
  184. _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
  185. {
  186. #ifndef RT_USING_DFS
  187. /* return "not supported" */
  188. ptr->_errno = ENOTSUP;
  189. return -1;
  190. #else
  191. _ssize_t rc;
  192. if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO)
  193. {
  194. LOG_W("Do not invoke standard input before initializing libc");
  195. return 0;
  196. }
  197. rc = read(fd, buf, nbytes);
  198. return rc;
  199. #endif
  200. }
  201. int _rename_r(struct _reent *ptr, const char *old, const char *new)
  202. {
  203. #ifndef RT_USING_DFS
  204. /* return "not supported" */
  205. ptr->_errno = ENOTSUP;
  206. return -1;
  207. #else
  208. int rc;
  209. rc = rename(old, new);
  210. return rc;
  211. #endif
  212. }
  213. int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
  214. {
  215. #ifndef RT_USING_DFS
  216. /* return "not supported" */
  217. ptr->_errno = ENOTSUP;
  218. return -1;
  219. #else
  220. int rc;
  221. rc = stat(file, pstat);
  222. return rc;
  223. #endif
  224. }
  225. int _unlink_r(struct _reent *ptr, const char *file)
  226. {
  227. #ifndef RT_USING_DFS
  228. /* return "not supported" */
  229. ptr->_errno = ENOTSUP;
  230. return -1;
  231. #else
  232. return unlink(file);
  233. #endif
  234. }
  235. int _wait_r(struct _reent *ptr, int *status)
  236. {
  237. /* return "not supported" */
  238. ptr->_errno = ENOTSUP;
  239. return -1;
  240. }
  241. _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
  242. {
  243. #ifndef RT_USING_DFS
  244. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  245. if (STDOUT_FILENO == fd)
  246. {
  247. rt_device_t console;
  248. console = rt_console_get_device();
  249. if (console) return rt_device_write(console, -1, buf, nbytes);
  250. }
  251. return 0;
  252. #else
  253. /* return "not supported" */
  254. ptr->_errno = ENOTSUP;
  255. return -1;
  256. #endif /*RT_USING_DEVICE*/
  257. #else
  258. _ssize_t rc;
  259. if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO)
  260. {
  261. LOG_W("Do not invoke standard output before initializing libc");
  262. return 0;
  263. }
  264. rc = write(fd, buf, nbytes);
  265. return rc;
  266. #endif
  267. }
  268. void _system(const char *s)
  269. {
  270. extern int __rt_libc_system(const char *string);
  271. __rt_libc_system(s);
  272. }
  273. /* for exit() and abort() */
  274. __attribute__ ((noreturn)) void _exit (int status)
  275. {
  276. extern void __rt_libc_exit(int status);
  277. __rt_libc_exit(status);
  278. while(1);
  279. }
  280. mode_t umask(mode_t mask)
  281. {
  282. return 022;
  283. }
  284. int flock(int fd, int operation)
  285. {
  286. return 0;
  287. }
  288. /*
  289. These functions are implemented and replaced by the 'common/time.c' file
  290. int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
  291. _CLOCK_T_ _times_r(struct _reent *ptr, struct tms *ptms);
  292. */
  293. #endif /* RT_USING_LIBC */