open.cpp 2.9 KB

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