shim_getcwd.c 3.2 KB

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