open.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Debug.h>
  7. #include <Kernel/FileSystem/Custody.h>
  8. #include <Kernel/FileSystem/VirtualFileSystem.h>
  9. #include <Kernel/Net/LocalSocket.h>
  10. #include <Kernel/Process.h>
  11. namespace Kernel {
  12. ErrorOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> user_params)
  13. {
  14. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  15. auto params = TRY(copy_typed_from_user(user_params));
  16. int dirfd = params.dirfd;
  17. int options = params.options;
  18. u16 mode = params.mode;
  19. if (options & O_NOFOLLOW_NOERROR)
  20. return EINVAL;
  21. if (options & O_UNLINK_INTERNAL)
  22. return EINVAL;
  23. if (options & O_WRONLY)
  24. TRY(require_promise(Pledge::wpath));
  25. else if (options & O_RDONLY)
  26. TRY(require_promise(Pledge::rpath));
  27. if (options & O_CREAT)
  28. TRY(require_promise(Pledge::cpath));
  29. // Ignore everything except permission bits.
  30. mode &= 0777;
  31. auto path = TRY(get_syscall_path_argument(params.path));
  32. dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path->view(), options, mode);
  33. auto fd_allocation = TRY(allocate_fd());
  34. RefPtr<Custody> base;
  35. if (dirfd == AT_FDCWD) {
  36. base = current_directory();
  37. } else {
  38. auto base_description = TRY(open_file_description(dirfd));
  39. if (!base_description->is_directory())
  40. return ENOTDIR;
  41. if (!base_description->custody())
  42. return EINVAL;
  43. base = base_description->custody();
  44. }
  45. auto description = TRY(VirtualFileSystem::the().open(path->view(), options, mode & ~umask(), *base));
  46. if (description->inode() && description->inode()->bound_socket())
  47. return ENXIO;
  48. return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> {
  49. u32 fd_flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
  50. fds[fd_allocation.fd].set(move(description), fd_flags);
  51. return fd_allocation.fd;
  52. });
  53. }
  54. ErrorOr<FlatPtr> Process::sys$close(int fd)
  55. {
  56. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  57. TRY(require_promise(Pledge::stdio));
  58. auto description = TRY(open_file_description(fd));
  59. auto result = description->close();
  60. m_fds.with_exclusive([fd](auto& fds) { fds[fd] = {}; });
  61. if (result.is_error())
  62. return result.release_error();
  63. return 0;
  64. }
  65. }