syscalls.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 0;
  170. #else
  171. int rc;
  172. rc = unlink(file);
  173. return rc;
  174. #endif
  175. }
  176. int
  177. _wait_r(struct _reent *ptr, int *status)
  178. {
  179. /* return "not supported" */
  180. ptr->_errno = ENOTSUP;
  181. return -1;
  182. }
  183. #ifdef RT_USING_DEVICE
  184. _ssize_t
  185. _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
  186. {
  187. #ifndef RT_USING_DFS
  188. if (fileno(stdout) == fd)
  189. {
  190. rt_device_t console;
  191. console = rt_console_get_device();
  192. if (console) return rt_device_write(console, -1, buf, nbytes);
  193. }
  194. return 0;
  195. #else
  196. _ssize_t rc;
  197. rc = write(fd, buf, nbytes);
  198. return rc;
  199. #endif
  200. }
  201. #endif
  202. /* Memory routine */
  203. void *
  204. _malloc_r (struct _reent *ptr, size_t size)
  205. {
  206. void* result;
  207. result = (void*)rt_malloc (size);
  208. if (result == RT_NULL)
  209. {
  210. ptr->_errno = ENOMEM;
  211. }
  212. return result;
  213. }
  214. void *
  215. _realloc_r (struct _reent *ptr, void *old, size_t newlen)
  216. {
  217. void* result;
  218. result = (void*)rt_realloc (old, newlen);
  219. if (result == RT_NULL)
  220. {
  221. ptr->_errno = ENOMEM;
  222. }
  223. return result;
  224. }
  225. void *_calloc_r (struct _reent *ptr, size_t size, size_t len)
  226. {
  227. void* result;
  228. result = (void*)rt_calloc (size, len);
  229. if (result == RT_NULL)
  230. {
  231. ptr->_errno = ENOMEM;
  232. }
  233. return result;
  234. }
  235. void
  236. _free_r (struct _reent *ptr, void *addr)
  237. {
  238. rt_free (addr);
  239. }
  240. void
  241. exit (int status)
  242. {
  243. rt_thread_t self = rt_thread_self();
  244. #ifdef RT_USING_MODULE
  245. if (dlmodule_self())
  246. {
  247. dlmodule_exit(status);
  248. }
  249. #endif
  250. if (self != RT_NULL)
  251. {
  252. rt_kprintf("thread:%-8.*s exit:%d!\n", RT_NAME_MAX, self->name, status);
  253. rt_thread_suspend(self);
  254. rt_schedule();
  255. }
  256. while(1); /* noreturn */
  257. }
  258. void
  259. _system(const char *s)
  260. {
  261. /* not support this call */
  262. return;
  263. }
  264. void __libc_init_array(void)
  265. {
  266. /* we not use __libc init_aray to initialize C++ objects */
  267. }
  268. void abort(void)
  269. {
  270. rt_thread_t self = rt_thread_self();
  271. #ifdef RT_USING_MODULE
  272. if (dlmodule_self())
  273. {
  274. dlmodule_exit(-1);
  275. }
  276. #endif
  277. if (self != RT_NULL)
  278. {
  279. rt_kprintf("thread:%-8.*s abort!\n", RT_NAME_MAX, self->name);
  280. rt_thread_suspend(self);
  281. rt_schedule();
  282. }
  283. while(1); /* noreturn */
  284. }
  285. uid_t getuid(void)
  286. {
  287. return 0;
  288. }
  289. mode_t umask(mode_t mask)
  290. {
  291. return 022;
  292. }
  293. int flock(int fd, int operation)
  294. {
  295. return 0;
  296. }
  297. /*
  298. These functions will be implemented and replaced by the 'common/time.c' file
  299. int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
  300. _CLOCK_T_ _times_r(struct _reent *ptr, struct tms *ptms);
  301. */