shim_getcwd.c 3.0 KB

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