shim_getcwd.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. int shim_do_getcwd (char * buf, size_t len)
  30. {
  31. int ret = 0;
  32. if (buf == NULL || len == 0) {
  33. ret = -EINVAL;
  34. goto out;
  35. }
  36. struct shim_thread * thread = get_cur_thread();
  37. assert(thread);
  38. struct shim_dentry * cwd = thread->cwd;
  39. int plen;
  40. const char * path = dentry_get_path(cwd, true, &plen);
  41. if (plen > len) {
  42. ret = -ENAMETOOLONG;
  43. goto out;
  44. } else
  45. ret = plen;
  46. memcpy(buf, path, plen + 1);
  47. out:
  48. return ret;
  49. }
  50. int shim_do_chdir (const char * filename)
  51. {
  52. struct shim_thread * thread = get_cur_thread();
  53. assert(thread);
  54. struct shim_dentry * dent = NULL;
  55. int ret;
  56. if ((ret = path_lookupat(NULL, filename, LOOKUP_OPEN, &dent, NULL)) < 0)
  57. return ret;
  58. if (!dent)
  59. return -ENOENT;
  60. if (!(dent->state & DENTRY_ISDIRECTORY)) {
  61. debug("%s is not a directory\n", dentry_get_path(dent, false, NULL));
  62. put_dentry(dent);
  63. return -ENOTDIR;
  64. }
  65. lock(thread->lock);
  66. put_dentry(thread->cwd);
  67. thread->cwd = dent;
  68. unlock(thread->lock);
  69. return 0;
  70. }
  71. int shim_do_fchdir (int fd)
  72. {
  73. struct shim_thread * thread = get_cur_thread();
  74. struct shim_handle * hdl = get_fd_handle(fd, NULL, thread->handle_map);
  75. if (!hdl)
  76. return -EBADF;
  77. struct shim_dentry * dent = hdl->dentry;
  78. if (!(dent->state & DENTRY_ISDIRECTORY)) {
  79. debug("%s is not a directory\n", dentry_get_path(dent, false, NULL));
  80. put_handle(hdl);
  81. return -ENOTDIR;
  82. }
  83. lock(thread->lock);
  84. get_dentry(dent);
  85. put_dentry(thread->cwd);
  86. thread->cwd = dent;
  87. unlock(thread->lock);
  88. put_handle(hdl);
  89. return 0;
  90. }