syscalls.c 6.1 KB

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