shim_stat.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_stat.c
  15. *
  16. * Implementation of system call "stat", "lstat", "fstat" and "readlink".
  17. */
  18. #include <errno.h>
  19. #include <linux/fcntl.h>
  20. #include <pal.h>
  21. #include <pal_error.h>
  22. #include <shim_fs.h>
  23. #include <shim_handle.h>
  24. #include <shim_internal.h>
  25. #include <shim_profile.h>
  26. #include <shim_table.h>
  27. #include <shim_thread.h>
  28. int shim_do_stat(const char* file, struct stat* stat) {
  29. if (!file || test_user_string(file))
  30. return -EFAULT;
  31. if (!stat || test_user_memory(stat, sizeof(*stat), true))
  32. return -EFAULT;
  33. int ret;
  34. struct shim_dentry* dent = NULL;
  35. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS | LOOKUP_FOLLOW, &dent, NULL)) < 0)
  36. goto out;
  37. struct shim_mount* fs = dent->fs;
  38. if (!fs->d_ops || !fs->d_ops->stat) {
  39. ret = -EACCES;
  40. goto out_dentry;
  41. }
  42. ret = fs->d_ops->stat(dent, stat);
  43. out_dentry:
  44. put_dentry(dent);
  45. out:
  46. return ret;
  47. }
  48. int shim_do_lstat(const char* file, struct stat* stat) {
  49. if (!file || test_user_string(file))
  50. return -EFAULT;
  51. if (!stat || test_user_memory(stat, sizeof(*stat), true))
  52. return -EFAULT;
  53. int ret;
  54. struct shim_dentry* dent = NULL;
  55. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS, &dent, NULL)) < 0)
  56. goto out;
  57. struct shim_mount* fs = dent->fs;
  58. if (!fs->d_ops || !fs->d_ops->stat) {
  59. ret = -EACCES;
  60. goto out_dentry;
  61. }
  62. ret = fs->d_ops->stat(dent, stat);
  63. out_dentry:
  64. put_dentry(dent);
  65. out:
  66. return ret;
  67. }
  68. int shim_do_fstat(int fd, struct stat* stat) {
  69. struct shim_handle* hdl = get_fd_handle(fd, NULL, NULL);
  70. if (!hdl)
  71. return -EBADF;
  72. int ret = -EACCES;
  73. struct shim_mount* fs = hdl->fs;
  74. if (!fs || !fs->fs_ops)
  75. goto out;
  76. if (!fs->fs_ops->hstat)
  77. goto out;
  78. ret = fs->fs_ops->hstat(hdl, stat);
  79. out:
  80. put_handle(hdl);
  81. return ret;
  82. }
  83. int shim_do_readlink(const char* file, char* buf, size_t bufsize) {
  84. if (!file || test_user_string(file))
  85. return -EFAULT;
  86. if (!buf || !bufsize || test_user_memory(buf, bufsize, true))
  87. return -EFAULT;
  88. if (bufsize <= 0)
  89. return -EINVAL;
  90. int ret;
  91. struct shim_dentry* dent = NULL;
  92. struct shim_qstr qstr = QSTR_INIT;
  93. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS, &dent, NULL)) < 0)
  94. return ret;
  95. ret = -EINVAL;
  96. /* The correct behavior is to return -EINVAL if file is not a
  97. symbolic link */
  98. if (!(dent->state & DENTRY_ISLINK))
  99. goto out;
  100. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->follow_link)
  101. goto out;
  102. ret = dent->fs->d_ops->follow_link(dent, &qstr);
  103. if (ret < 0)
  104. goto out;
  105. ret = -ENAMETOOLONG;
  106. if (qstr.len >= bufsize)
  107. goto out;
  108. memcpy(buf, qstrgetstr(&qstr), qstr.len);
  109. ret = qstr.len;
  110. out:
  111. put_dentry(dent);
  112. return ret;
  113. }
  114. static int __do_statfs(struct shim_mount* fs, struct statfs* buf) {
  115. __UNUSED(fs);
  116. if (!buf || test_user_memory(buf, sizeof(*buf), true))
  117. return -EFAULT;
  118. memset(buf, 0, sizeof(*buf));
  119. buf->f_bsize = 4096;
  120. buf->f_blocks = 20000000;
  121. buf->f_bfree = 10000000;
  122. buf->f_bavail = 10000000;
  123. debug("statfs: %ld %ld %ld\n", buf->f_blocks, buf->f_bfree, buf->f_bavail);
  124. return 0;
  125. }
  126. int shim_do_statfs(const char* path, struct statfs* buf) {
  127. if (!path || test_user_string(path))
  128. return -EFAULT;
  129. int ret;
  130. struct shim_dentry* dent = NULL;
  131. if ((ret = path_lookupat(NULL, path, LOOKUP_ACCESS | LOOKUP_FOLLOW, &dent, NULL)) < 0)
  132. return ret;
  133. struct shim_mount* fs = dent->fs;
  134. put_dentry(dent);
  135. return __do_statfs(fs, buf);
  136. }
  137. int shim_do_fstatfs(int fd, struct statfs* buf) {
  138. struct shim_handle* hdl = get_fd_handle(fd, NULL, NULL);
  139. if (!hdl)
  140. return -EBADF;
  141. struct shim_mount* fs = hdl->fs;
  142. put_handle(hdl);
  143. return __do_statfs(fs, buf);
  144. }
  145. int shim_do_newfstatat(int dirfd, const char* pathname, struct stat* statbuf, int flags) {
  146. if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW))
  147. return -EINVAL;
  148. if (test_user_string(pathname))
  149. return -EFAULT;
  150. if (test_user_memory(statbuf, sizeof(*statbuf), true))
  151. return -EFAULT;
  152. int lookup_flags = LOOKUP_ACCESS | LOOKUP_FOLLOW;
  153. if (flags & AT_SYMLINK_NOFOLLOW)
  154. lookup_flags &= ~LOOKUP_FOLLOW;
  155. if (flags & AT_NO_AUTOMOUNT) {
  156. /* Do nothing as automount isn't supported */
  157. debug("ignoring AT_NO_AUTOMOUNT.");
  158. }
  159. if (!*pathname) {
  160. if (!(flags & AT_EMPTY_PATH))
  161. return -ENOENT;
  162. if (dirfd == AT_FDCWD) {
  163. struct shim_dentry* cwd = get_cur_thread()->cwd;
  164. struct shim_d_ops* d_ops = cwd->fs->d_ops;
  165. if (d_ops && d_ops->stat)
  166. return d_ops->stat(cwd, statbuf);
  167. return -EACCES;
  168. }
  169. return shim_do_fstat(dirfd, statbuf);
  170. }
  171. struct shim_dentry* dir = NULL;
  172. if (*pathname != '/') {
  173. int ret = get_dirfd_dentry(dirfd, &dir);
  174. if (ret < 0)
  175. return ret;
  176. }
  177. struct shim_dentry* dent;
  178. int ret = path_lookupat(dir, pathname, lookup_flags, &dent, NULL);
  179. if (ret >= 0) {
  180. struct shim_d_ops* d_ops = dent->fs->d_ops;
  181. if (d_ops && d_ops->stat)
  182. ret = d_ops->stat(dent, statbuf);
  183. else
  184. ret = -EACCES;
  185. put_dentry(dent);
  186. }
  187. if (dir)
  188. put_dentry(dir);
  189. return ret;
  190. }