syscalls.c 5.8 KB

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