file.c 5.7 KB

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