shim_getcwd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_getcwd.c
  15. *
  16. * Implementation of system call "getcwd", "chdir" and "fchdir".
  17. */
  18. #include <errno.h>
  19. #include <pal.h>
  20. #include <pal_error.h>
  21. #include <shim_fs.h>
  22. #include <shim_handle.h>
  23. #include <shim_internal.h>
  24. #include <shim_table.h>
  25. #include <shim_thread.h>
  26. #include <shim_utils.h>
  27. #ifndef ERANGE
  28. #define ERANGE 34
  29. #endif
  30. int shim_do_getcwd(char* buf, size_t len) {
  31. if (!buf || !len)
  32. return -EINVAL;
  33. if (test_user_memory(buf, len, true))
  34. return -EFAULT;
  35. struct shim_thread* thread = get_cur_thread();
  36. assert(thread);
  37. struct shim_dentry* cwd = thread->cwd;
  38. size_t plen;
  39. const char* path = dentry_get_path(cwd, true, &plen);
  40. int ret;
  41. if (plen >= MAX_PATH) {
  42. ret = -ENAMETOOLONG;
  43. } else if (plen + 1 > len) {
  44. ret = -ERANGE;
  45. } else {
  46. ret = plen + 1;
  47. memcpy(buf, path, plen + 1);
  48. }
  49. return ret;
  50. }
  51. int shim_do_chdir(const char* filename) {
  52. struct shim_thread* thread = get_cur_thread();
  53. assert(thread);
  54. struct shim_dentry* dent = NULL;
  55. int ret;
  56. if (!filename)
  57. return -EINVAL;
  58. if (test_user_string(filename))
  59. return -EFAULT;
  60. if (strnlen(filename, MAX_PATH + 1) == MAX_PATH + 1)
  61. return -ENAMETOOLONG;
  62. if ((ret = path_lookupat(NULL, filename, LOOKUP_OPEN, &dent, NULL)) < 0)
  63. return ret;
  64. if (!dent)
  65. return -ENOENT;
  66. if (!(dent->state & DENTRY_ISDIRECTORY)) {
  67. debug("%s is not a directory\n", dentry_get_path(dent, false, NULL));
  68. put_dentry(dent);
  69. return -ENOTDIR;
  70. }
  71. lock(&thread->lock);
  72. put_dentry(thread->cwd);
  73. thread->cwd = dent;
  74. unlock(&thread->lock);
  75. return 0;
  76. }
  77. int shim_do_fchdir(int fd) {
  78. struct shim_thread* thread = get_cur_thread();
  79. struct shim_handle* hdl = get_fd_handle(fd, NULL, thread->handle_map);
  80. if (!hdl)
  81. return -EBADF;
  82. struct shim_dentry* dent = hdl->dentry;
  83. if (!(dent->state & DENTRY_ISDIRECTORY)) {
  84. debug("%s is not a directory\n", dentry_get_path(dent, false, NULL));
  85. put_handle(hdl);
  86. return -ENOTDIR;
  87. }
  88. lock(&thread->lock);
  89. get_dentry(dent);
  90. put_dentry(thread->cwd);
  91. thread->cwd = dent;
  92. unlock(&thread->lock);
  93. put_handle(hdl);
  94. return 0;
  95. }