syscalls.c 6.1 KB

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