file.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdbool.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/uio.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <dirent.h>
  16. #include <sched.h>
  17. #include <poll.h>
  18. #include <errno.h>
  19. int
  20. ocall_open(const char *pathname, int flags, bool has_mode, unsigned mode)
  21. {
  22. if (has_mode) {
  23. return open(pathname, flags, (mode_t)mode);
  24. }
  25. else {
  26. return open(pathname, flags);
  27. }
  28. }
  29. int
  30. ocall_openat(int dirfd, const char *pathname, int flags, bool has_mode,
  31. unsigned mode)
  32. {
  33. if (has_mode) {
  34. return openat(dirfd, pathname, flags, (mode_t)mode);
  35. }
  36. else {
  37. return openat(dirfd, pathname, flags);
  38. }
  39. }
  40. int
  41. ocall_close(int fd)
  42. {
  43. return close(fd);
  44. }
  45. ssize_t
  46. ocall_read(int fd, void *buf, size_t read_size)
  47. {
  48. if (buf != NULL) {
  49. return read(fd, buf, read_size);
  50. }
  51. else {
  52. return -1;
  53. }
  54. }
  55. off_t
  56. ocall_lseek(int fd, off_t offset, int whence)
  57. {
  58. return lseek(fd, offset, whence);
  59. }
  60. int
  61. ocall_ftruncate(int fd, off_t length)
  62. {
  63. return ftruncate(fd, length);
  64. }
  65. int
  66. ocall_fsync(int fd)
  67. {
  68. return fsync(fd);
  69. }
  70. int
  71. ocall_fdatasync(int fd)
  72. {
  73. return fdatasync(fd);
  74. }
  75. int
  76. ocall_isatty(int fd)
  77. {
  78. return isatty(fd);
  79. }
  80. void
  81. ocall_fdopendir(int fd, void **dirp)
  82. {
  83. if (dirp) {
  84. *(DIR **)dirp = fdopendir(fd);
  85. }
  86. }
  87. void *
  88. ocall_readdir(void *dirp)
  89. {
  90. DIR *p_dirp = (DIR *)dirp;
  91. return readdir(p_dirp);
  92. }
  93. void
  94. ocall_rewinddir(void *dirp)
  95. {
  96. DIR *p_dirp = (DIR *)dirp;
  97. if (p_dirp) {
  98. rewinddir(p_dirp);
  99. }
  100. }
  101. void
  102. ocall_seekdir(void *dirp, long loc)
  103. {
  104. DIR *p_dirp = (DIR *)dirp;
  105. if (p_dirp) {
  106. seekdir(p_dirp, loc);
  107. }
  108. }
  109. long
  110. ocall_telldir(void *dirp)
  111. {
  112. DIR *p_dirp = (DIR *)dirp;
  113. if (p_dirp) {
  114. return telldir(p_dirp);
  115. }
  116. return -1;
  117. }
  118. int
  119. ocall_closedir(void *dirp)
  120. {
  121. DIR *p_dirp = (DIR *)dirp;
  122. if (p_dirp) {
  123. return closedir(p_dirp);
  124. }
  125. return -1;
  126. }
  127. int
  128. ocall_stat(const char *pathname, void *buf, unsigned int buf_len)
  129. {
  130. return stat(pathname, (struct stat *)buf);
  131. }
  132. int
  133. ocall_fstat(int fd, void *buf, unsigned int buf_len)
  134. {
  135. return fstat(fd, (struct stat *)buf);
  136. }
  137. int
  138. ocall_fstatat(int dirfd, const char *pathname, void *buf, unsigned int buf_len,
  139. int flags)
  140. {
  141. return fstatat(dirfd, pathname, (struct stat *)buf, flags);
  142. }
  143. int
  144. ocall_mkdirat(int dirfd, const char *pathname, unsigned mode)
  145. {
  146. return mkdirat(dirfd, pathname, (mode_t)mode);
  147. }
  148. int
  149. ocall_link(const char *oldpath, const char *newpath)
  150. {
  151. return link(oldpath, newpath);
  152. }
  153. int
  154. ocall_linkat(int olddirfd, const char *oldpath, int newdirfd,
  155. const char *newpath, int flags)
  156. {
  157. return linkat(olddirfd, oldpath, newdirfd, newpath, flags);
  158. }
  159. int
  160. ocall_unlinkat(int dirfd, const char *pathname, int flags)
  161. {
  162. return unlinkat(dirfd, pathname, flags);
  163. }
  164. ssize_t
  165. ocall_readlink(const char *pathname, char *buf, size_t bufsiz)
  166. {
  167. return readlink(pathname, buf, bufsiz);
  168. }
  169. ssize_t
  170. ocall_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
  171. {
  172. return readlinkat(dirfd, pathname, buf, bufsiz);
  173. }
  174. int
  175. ocall_renameat(int olddirfd, const char *oldpath, int newdirfd,
  176. const char *newpath)
  177. {
  178. return renameat(olddirfd, oldpath, newdirfd, newpath);
  179. }
  180. int
  181. ocall_symlinkat(const char *target, int newdirfd, const char *linkpath)
  182. {
  183. return symlinkat(target, newdirfd, linkpath);
  184. }
  185. int
  186. ocall_ioctl(int fd, unsigned long request, void *arg, unsigned int arg_len)
  187. {
  188. /* support just int *arg temporally */
  189. return ioctl(fd, request, (int *)arg);
  190. }
  191. int
  192. ocall_fcntl(int fd, int cmd)
  193. {
  194. return fcntl(fd, cmd);
  195. }
  196. int
  197. ocall_fcntl_long(int fd, int cmd, long arg)
  198. {
  199. return fcntl(fd, cmd, arg);
  200. }
  201. ssize_t
  202. ocall_readv(int fd, char *iov_buf, unsigned int buf_size, int iovcnt,
  203. bool has_offset, off_t offset)
  204. {
  205. struct iovec *iov = (struct iovec *)iov_buf;
  206. ssize_t ret;
  207. int i;
  208. for (i = 0; i < iovcnt; i++) {
  209. iov[i].iov_base = iov_buf + (unsigned)(uintptr_t)iov[i].iov_base;
  210. }
  211. if (has_offset)
  212. ret = preadv(fd, iov, iovcnt, offset);
  213. else
  214. ret = readv(fd, iov, iovcnt);
  215. return ret;
  216. }
  217. ssize_t
  218. ocall_writev(int fd, char *iov_buf, unsigned int buf_size, int iovcnt,
  219. bool has_offset, off_t offset)
  220. {
  221. struct iovec *iov = (struct iovec *)iov_buf;
  222. int i;
  223. ssize_t ret;
  224. for (i = 0; i < iovcnt; i++) {
  225. iov[i].iov_base = iov_buf + (unsigned)(uintptr_t)iov[i].iov_base;
  226. }
  227. if (has_offset)
  228. ret = pwritev(fd, iov, iovcnt, offset);
  229. else
  230. ret = writev(fd, iov, iovcnt);
  231. return ret;
  232. }
  233. int
  234. ocall_realpath(const char *path, char *buf, unsigned int buf_len)
  235. {
  236. char *val = NULL;
  237. val = realpath(path, buf);
  238. if (val != NULL) {
  239. return 0;
  240. }
  241. return -1;
  242. }
  243. int
  244. ocall_posix_fallocate(int fd, off_t offset, off_t len)
  245. {
  246. return posix_fallocate(fd, offset, len);
  247. }
  248. int
  249. ocall_poll(void *fds, unsigned nfds, int timeout, unsigned int fds_len)
  250. {
  251. return poll((struct pollfd *)fds, (nfds_t)nfds, timeout);
  252. }
  253. int
  254. ocall_getopt(int argc, char *argv_buf, unsigned int argv_buf_len,
  255. const char *optstring)
  256. {
  257. int ret;
  258. int i;
  259. char **argv = (char **)argv_buf;
  260. for (i = 0; i < argc; i++) {
  261. argv[i] = argv_buf + (uintptr_t)argv[i];
  262. }
  263. return getopt(argc, argv, optstring);
  264. }
  265. int
  266. ocall_sched_yield()
  267. {
  268. return sched_yield();
  269. }
  270. int
  271. ocall_get_errno()
  272. {
  273. return errno;
  274. }