shim_stat.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_stat.c
  17. *
  18. * Implementation of system call "stat", "lstat", "fstat" and "readlink".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_handle.h>
  23. #include <shim_fs.h>
  24. #include <shim_profile.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <errno.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, int 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. if (!buf || test_user_memory(buf, sizeof(*buf), true))
  121. return -EFAULT;
  122. memset(buf, 0, sizeof(*buf));
  123. buf->f_bsize = 4096;
  124. buf->f_blocks = 20000000;
  125. buf->f_bfree = 10000000;
  126. buf->f_bavail = 10000000;
  127. debug("statfs: %ld %ld %ld\n", buf->f_blocks, buf->f_bfree,
  128. buf->f_bavail);
  129. return 0;
  130. }
  131. int shim_do_statfs (const char * path, struct statfs * buf)
  132. {
  133. if (!path || test_user_string(path))
  134. return -EFAULT;
  135. int ret;
  136. struct shim_dentry * dent = NULL;
  137. if ((ret = path_lookupat(NULL, path, LOOKUP_ACCESS|LOOKUP_FOLLOW, &dent, NULL)) < 0)
  138. return ret;
  139. struct shim_mount * fs = dent->fs;
  140. put_dentry(dent);
  141. return __do_statfs (fs, buf);
  142. }
  143. int shim_do_fstatfs (int fd, struct statfs * buf)
  144. {
  145. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  146. if (!hdl)
  147. return -EBADF;
  148. struct shim_mount * fs = hdl->fs;
  149. put_handle(hdl);
  150. return __do_statfs (fs, buf);
  151. }