shim_stat.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <shim_internal.h>
  19. #include <shim_table.h>
  20. #include <shim_handle.h>
  21. #include <shim_fs.h>
  22. #include <shim_profile.h>
  23. #include <shim_thread.h>
  24. #include <pal.h>
  25. #include <pal_error.h>
  26. #include <errno.h>
  27. #include <linux/fcntl.h>
  28. int shim_do_stat (const char * file, struct stat * stat)
  29. {
  30. if (!file || test_user_string(file))
  31. return -EFAULT;
  32. if (!stat || test_user_memory(stat, sizeof(*stat), true))
  33. return -EFAULT;
  34. int ret;
  35. struct shim_dentry * dent = NULL;
  36. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS|LOOKUP_FOLLOW, &dent, NULL)) < 0)
  37. goto out;
  38. struct shim_mount * fs = dent->fs;
  39. if (!fs->d_ops || !fs->d_ops->stat) {
  40. ret = -EACCES;
  41. goto out_dentry;
  42. }
  43. ret = fs->d_ops->stat(dent, stat);
  44. out_dentry:
  45. put_dentry(dent);
  46. out:
  47. return ret;
  48. }
  49. int shim_do_lstat (const char * file, struct stat * stat)
  50. {
  51. if (!file || test_user_string(file))
  52. return -EFAULT;
  53. if (!stat || test_user_memory(stat, sizeof(*stat), true))
  54. return -EFAULT;
  55. int ret;
  56. struct shim_dentry * dent = NULL;
  57. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS, &dent, NULL)) < 0)
  58. goto out;
  59. struct shim_mount * fs = dent->fs;
  60. if (!fs->d_ops || !fs->d_ops->stat) {
  61. ret = -EACCES;
  62. goto out_dentry;
  63. }
  64. ret = fs->d_ops->stat(dent, stat);
  65. out_dentry:
  66. put_dentry(dent);
  67. out:
  68. return ret;
  69. }
  70. int shim_do_fstat (int fd, struct stat * stat)
  71. {
  72. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  73. if (!hdl)
  74. return -EBADF;
  75. int ret = -EACCES;
  76. struct shim_mount * fs = hdl->fs;
  77. if (!fs || !fs->fs_ops)
  78. goto out;
  79. if (!fs->fs_ops->hstat)
  80. goto out;
  81. ret = fs->fs_ops->hstat(hdl, stat);
  82. out:
  83. put_handle(hdl);
  84. return ret;
  85. }
  86. int shim_do_readlink (const char * file, char * buf, size_t bufsize)
  87. {
  88. if (!file || test_user_string(file))
  89. return -EFAULT;
  90. if (!buf || !bufsize || test_user_memory(buf, bufsize, true))
  91. return -EFAULT;
  92. if (bufsize <= 0)
  93. return -EINVAL;
  94. int ret;
  95. struct shim_dentry * dent = NULL;
  96. struct shim_qstr qstr = QSTR_INIT;
  97. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS, &dent, NULL)) < 0)
  98. return ret;
  99. ret = -EINVAL;
  100. /* The correct behavior is to return -EINVAL if file is not a
  101. symbolic link */
  102. if (!(dent->state & DENTRY_ISLINK))
  103. goto out;
  104. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->follow_link)
  105. goto out;
  106. ret = dent->fs->d_ops->follow_link(dent, &qstr);
  107. if (ret < 0)
  108. goto out;
  109. ret = -ENAMETOOLONG;
  110. if (qstr.len >= bufsize)
  111. goto out;
  112. memcpy(buf, qstrgetstr(&qstr), qstr.len);
  113. ret = qstr.len;
  114. out:
  115. put_dentry(dent);
  116. return ret;
  117. }
  118. static int __do_statfs (struct shim_mount * fs, struct statfs * buf)
  119. {
  120. __UNUSED(fs);
  121. if (!buf || test_user_memory(buf, sizeof(*buf), true))
  122. return -EFAULT;
  123. memset(buf, 0, sizeof(*buf));
  124. buf->f_bsize = 4096;
  125. buf->f_blocks = 20000000;
  126. buf->f_bfree = 10000000;
  127. buf->f_bavail = 10000000;
  128. debug("statfs: %ld %ld %ld\n", buf->f_blocks, buf->f_bfree,
  129. buf->f_bavail);
  130. return 0;
  131. }
  132. int shim_do_statfs (const char * path, struct statfs * buf)
  133. {
  134. if (!path || test_user_string(path))
  135. return -EFAULT;
  136. int ret;
  137. struct shim_dentry * dent = NULL;
  138. if ((ret = path_lookupat(NULL, path, LOOKUP_ACCESS|LOOKUP_FOLLOW, &dent, NULL)) < 0)
  139. return ret;
  140. struct shim_mount * fs = dent->fs;
  141. put_dentry(dent);
  142. return __do_statfs (fs, buf);
  143. }
  144. int shim_do_fstatfs (int fd, struct statfs * buf)
  145. {
  146. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  147. if (!hdl)
  148. return -EBADF;
  149. struct shim_mount * fs = hdl->fs;
  150. put_handle(hdl);
  151. return __do_statfs (fs, buf);
  152. }
  153. int shim_do_newfstatat(int dirfd, const char* pathname,
  154. struct stat* statbuf, int flags)
  155. {
  156. if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW))
  157. return -EINVAL;
  158. if (test_user_string(pathname))
  159. return -EFAULT;
  160. if (test_user_memory(statbuf, sizeof(*statbuf), true))
  161. return -EFAULT;
  162. int lookup_flags = LOOKUP_ACCESS | LOOKUP_FOLLOW;
  163. if (flags & AT_SYMLINK_NOFOLLOW)
  164. lookup_flags &= ~LOOKUP_FOLLOW;
  165. if (flags & AT_NO_AUTOMOUNT) {
  166. /* Do nothing as automount isn't supported */
  167. debug("ignoring AT_NO_AUTOMOUNT.");
  168. }
  169. if (!*pathname){
  170. if (!(flags & AT_EMPTY_PATH))
  171. return -ENOENT;
  172. if (dirfd == AT_FDCWD) {
  173. struct shim_dentry * cwd = get_cur_thread()->cwd;
  174. struct shim_d_ops* d_ops = cwd->fs->d_ops;
  175. if (d_ops && d_ops->stat)
  176. return d_ops->stat(cwd, statbuf);
  177. return -EACCES;
  178. }
  179. return shim_do_fstat(dirfd, statbuf);
  180. }
  181. struct shim_dentry* dir = NULL;
  182. if (*pathname != '/') {
  183. int ret = get_dirfd_dentry(dirfd, &dir);
  184. if (ret < 0)
  185. return ret;
  186. }
  187. struct shim_dentry* dent;
  188. int ret = path_lookupat(dir, pathname, lookup_flags, &dent, NULL);
  189. if (ret >= 0) {
  190. struct shim_d_ops* d_ops = dent->fs->d_ops;
  191. if (d_ops && d_ops->stat)
  192. ret = d_ops->stat(dent, statbuf);
  193. else
  194. ret = -EACCES;
  195. put_dentry(dent);
  196. }
  197. if (dir)
  198. put_dentry(dir);
  199. return ret;
  200. }