file.c 5.9 KB

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