1
0

syscalls.c 5.2 KB

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