open.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/Debug.h>
  27. #include <Kernel/FileSystem/Custody.h>
  28. #include <Kernel/FileSystem/FileDescription.h>
  29. #include <Kernel/FileSystem/VirtualFileSystem.h>
  30. #include <Kernel/Process.h>
  31. namespace Kernel {
  32. int Process::sys$open(Userspace<const Syscall::SC_open_params*> user_params)
  33. {
  34. Syscall::SC_open_params params;
  35. if (!copy_from_user(&params, user_params))
  36. return -EFAULT;
  37. int dirfd = params.dirfd;
  38. int options = params.options;
  39. u16 mode = params.mode;
  40. if (options & O_NOFOLLOW_NOERROR)
  41. return -EINVAL;
  42. if (options & O_UNLINK_INTERNAL)
  43. return -EINVAL;
  44. if (options & O_WRONLY)
  45. REQUIRE_PROMISE(wpath);
  46. else if (options & O_RDONLY)
  47. REQUIRE_PROMISE(rpath);
  48. if (options & O_CREAT)
  49. REQUIRE_PROMISE(cpath);
  50. // Ignore everything except permission bits.
  51. mode &= 0777;
  52. auto path = get_syscall_path_argument(params.path);
  53. if (path.is_error())
  54. return path.error();
  55. dbgln<IO_DEBUG>("sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path.value(), options, mode);
  56. int fd = alloc_fd();
  57. if (fd < 0)
  58. return fd;
  59. RefPtr<Custody> base;
  60. if (dirfd == AT_FDCWD) {
  61. base = current_directory();
  62. } else {
  63. auto base_description = file_description(dirfd);
  64. if (!base_description)
  65. return -EBADF;
  66. if (!base_description->is_directory())
  67. return -ENOTDIR;
  68. if (!base_description->custody())
  69. return -EINVAL;
  70. base = base_description->custody();
  71. }
  72. auto result = VFS::the().open(path.value(), options, mode & ~umask(), *base);
  73. if (result.is_error())
  74. return result.error();
  75. auto description = result.value();
  76. if (description->inode() && description->inode()->socket())
  77. return -ENXIO;
  78. u32 fd_flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
  79. m_fds[fd].set(move(description), fd_flags);
  80. return fd;
  81. }
  82. int Process::sys$close(int fd)
  83. {
  84. REQUIRE_PROMISE(stdio);
  85. auto description = file_description(fd);
  86. dbgln<IO_DEBUG>("sys$close({}) {}", fd, description.ptr());
  87. if (!description)
  88. return -EBADF;
  89. int rc = description->close();
  90. m_fds[fd] = {};
  91. return rc;
  92. }
  93. }