shim_stat.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <pal.h>
  24. #include <pal_error.h>
  25. #include <errno.h>
  26. int shim_do_stat (const char * file, struct stat * stat)
  27. {
  28. if (!file || test_user_string(file))
  29. return -EFAULT;
  30. if (!stat || test_user_memory(stat, sizeof(*stat), true))
  31. return -EFAULT;
  32. int ret;
  33. struct shim_dentry * dent = NULL;
  34. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS|LOOKUP_FOLLOW, &dent, NULL)) < 0)
  35. goto out;
  36. struct shim_mount * fs = dent->fs;
  37. if (!fs->d_ops || !fs->d_ops->stat) {
  38. ret = -EACCES;
  39. goto out_dentry;
  40. }
  41. ret = fs->d_ops->stat(dent, stat);
  42. out_dentry:
  43. put_dentry(dent);
  44. out:
  45. return ret;
  46. }
  47. int shim_do_lstat (const char * file, struct stat * stat)
  48. {
  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. {
  70. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  71. if (!hdl)
  72. return -EBADF;
  73. int ret = -EACCES;
  74. struct shim_mount * fs = hdl->fs;
  75. if (!fs || !fs->fs_ops)
  76. goto out;
  77. if (!fs->fs_ops->hstat)
  78. goto out;
  79. ret = fs->fs_ops->hstat(hdl, stat);
  80. out:
  81. put_handle(hdl);
  82. return ret;
  83. }
  84. int shim_do_readlink (const char * file, char * buf, int bufsize)
  85. {
  86. if (!file || test_user_string(file))
  87. return -EFAULT;
  88. if (!buf || !bufsize || test_user_memory(buf, bufsize, true))
  89. return -EFAULT;
  90. if (bufsize <= 0)
  91. return -EINVAL;
  92. int ret;
  93. struct shim_dentry * dent = NULL;
  94. struct shim_qstr qstr = QSTR_INIT;
  95. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS, &dent, NULL)) < 0)
  96. return ret;
  97. ret = -EINVAL;
  98. /* The correct behavior is to return -EINVAL if file is not a
  99. symbolic link */
  100. if (!(dent->state & DENTRY_ISLINK))
  101. goto out;
  102. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->follow_link)
  103. goto out;
  104. ret = dent->fs->d_ops->follow_link(dent, &qstr);
  105. if (ret < 0)
  106. goto out;
  107. ret = -ENAMETOOLONG;
  108. if (qstr.len >= bufsize)
  109. goto out;
  110. memcpy(buf, qstrgetstr(&qstr), qstr.len);
  111. ret = qstr.len;
  112. out:
  113. put_dentry(dent);
  114. return ret;
  115. }
  116. static int __do_statfs (struct shim_mount * fs, struct statfs * buf)
  117. {
  118. if (!buf || test_user_memory(buf, sizeof(*buf), true))
  119. return -EFAULT;
  120. memset(buf, 0, sizeof(*buf));
  121. buf->f_bsize = 4096;
  122. buf->f_blocks = 20000000;
  123. buf->f_bfree = 10000000;
  124. buf->f_bavail = 10000000;
  125. debug("statfs: %ld %ld %ld\n", buf->f_blocks, buf->f_bfree,
  126. buf->f_bavail);
  127. return 0;
  128. }
  129. int shim_do_statfs (const char * path, struct statfs * buf)
  130. {
  131. if (!path || test_user_string(path))
  132. return -EFAULT;
  133. int ret;
  134. struct shim_dentry * dent = NULL;
  135. if ((ret = path_lookupat(NULL, path, LOOKUP_ACCESS|LOOKUP_FOLLOW, &dent, NULL)) < 0)
  136. return ret;
  137. struct shim_mount * fs = dent->fs;
  138. put_dentry(dent);
  139. return __do_statfs (fs, buf);
  140. }
  141. int shim_do_fstatfs (int fd, struct statfs * buf)
  142. {
  143. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  144. if (!hdl)
  145. return -EBADF;
  146. struct shim_mount * fs = hdl->fs;
  147. put_handle(hdl);
  148. return __do_statfs (fs, buf);
  149. }