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