syscalls.c 5.5 KB

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