diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-05 18:34:28 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-05 18:36:13 +0200 |
commit | a9204510a4c0c37588a38201f305d7e9b4e762a9 (patch) | |
tree | 752b1077191f2608755104f6cca2a19bc9f4591c /Kernel | |
parent | 2d2ea05c97e97848f431b5c9e5f709fe276eb7c8 (diff) | |
download | serenity-a9204510a4c0c37588a38201f305d7e9b4e762a9.zip |
Kernel: Make file description lookup return KResultOr
Instead of checking it at every call site (to generate EBADF), we make
file_description(fd) return a KResultOr<NonnullRefPtr<FileDescription>>.
This allows us to wrap all the calls in TRY(). :^)
The only place that got a little bit messier from this is sys$mount(),
and there's a whole bunch of things there in need of cleanup.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Process.cpp | 13 | ||||
-rw-r--r-- | Kernel/Process.h | 3 | ||||
-rw-r--r-- | Kernel/ProcessSpecificExposed.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/chdir.cpp | 7 | ||||
-rw-r--r-- | Kernel/Syscalls/chmod.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/chown.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/dup2.cpp | 6 | ||||
-rw-r--r-- | Kernel/Syscalls/fcntl.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/ftruncate.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/get_dir_entries.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/inode_watcher.cpp | 8 | ||||
-rw-r--r-- | Kernel/Syscalls/ioctl.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/lseek.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/mmap.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/mount.cpp | 21 | ||||
-rw-r--r-- | Kernel/Syscalls/open.cpp | 9 | ||||
-rw-r--r-- | Kernel/Syscalls/read.cpp | 10 | ||||
-rw-r--r-- | Kernel/Syscalls/select.cpp | 16 | ||||
-rw-r--r-- | Kernel/Syscalls/sendfd.cpp | 13 | ||||
-rw-r--r-- | Kernel/Syscalls/socket.cpp | 45 | ||||
-rw-r--r-- | Kernel/Syscalls/stat.cpp | 8 | ||||
-rw-r--r-- | Kernel/Syscalls/statvfs.cpp | 5 | ||||
-rw-r--r-- | Kernel/Syscalls/ttyname.cpp | 8 | ||||
-rw-r--r-- | Kernel/Syscalls/write.cpp | 9 |
24 files changed, 62 insertions, 155 deletions
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 949a7c5f71..2c5fdf6f4e 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -452,14 +452,17 @@ Process::FileDescriptionAndFlags& Process::FileDescriptions::at(size_t i) return m_fds_metadatas[i]; } -RefPtr<FileDescription> Process::FileDescriptions::file_description(int fd) const +KResultOr<NonnullRefPtr<FileDescription>> Process::FileDescriptions::file_description(int fd) const { SpinlockLocker lock(m_fds_lock); if (fd < 0) - return nullptr; - if (static_cast<size_t>(fd) < m_fds_metadatas.size()) - return m_fds_metadatas[fd].description(); - return nullptr; + return EBADF; + if (static_cast<size_t>(fd) >= m_fds_metadatas.size()) + return EBADF; + RefPtr description = m_fds_metadatas[fd].description(); + if (!description) + return EBADF; + return description.release_nonnull(); } void Process::FileDescriptions::enumerate(Function<void(const FileDescriptionAndFlags&)> callback) const diff --git a/Kernel/Process.h b/Kernel/Process.h index 72cbd88580..6eda1f0d1a 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -676,8 +676,7 @@ public: m_fds_metadatas.clear(); } - // FIXME: Consider to remove this somehow - RefPtr<FileDescription> file_description(int fd) const; + KResultOr<NonnullRefPtr<FileDescription>> file_description(int fd) const; private: FileDescriptions() = default; diff --git a/Kernel/ProcessSpecificExposed.cpp b/Kernel/ProcessSpecificExposed.cpp index dbd9a716a5..1f6adcaaf7 100644 --- a/Kernel/ProcessSpecificExposed.cpp +++ b/Kernel/ProcessSpecificExposed.cpp @@ -79,9 +79,7 @@ KResultOr<NonnullRefPtr<Inode>> Process::lookup_stacks_directory(const ProcFS& p KResultOr<size_t> Process::procfs_get_file_description_link(unsigned fd, KBufferBuilder& builder) const { - auto file_description = m_fds.file_description(fd); - if (!file_description) - return EBADF; + auto file_description = TRY(m_fds.file_description(fd)); auto data = file_description->absolute_path(); builder.append(data); return data.length(); diff --git a/Kernel/Syscalls/chdir.cpp b/Kernel/Syscalls/chdir.cpp index 5b5da7acfc..7b200e9f25 100644 --- a/Kernel/Syscalls/chdir.cpp +++ b/Kernel/Syscalls/chdir.cpp @@ -23,16 +23,11 @@ KResultOr<FlatPtr> Process::sys$fchdir(int fd) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this); REQUIRE_PROMISE(stdio); - auto description = fds().file_description(fd); - if (!description) - return EBADF; - + auto description = TRY(fds().file_description(fd)); if (!description->is_directory()) return ENOTDIR; - if (!description->metadata().may_execute(*this)) return EACCES; - m_cwd = description->custody(); return 0; } diff --git a/Kernel/Syscalls/chmod.cpp b/Kernel/Syscalls/chmod.cpp index f7fd0c439d..8cc9b8a863 100644 --- a/Kernel/Syscalls/chmod.cpp +++ b/Kernel/Syscalls/chmod.cpp @@ -22,9 +22,7 @@ KResultOr<FlatPtr> Process::sys$fchmod(int fd, mode_t mode) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this); REQUIRE_PROMISE(fattr); - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); return description->chmod(mode); } diff --git a/Kernel/Syscalls/chown.cpp b/Kernel/Syscalls/chown.cpp index 5c0803bea3..1a014e07fb 100644 --- a/Kernel/Syscalls/chown.cpp +++ b/Kernel/Syscalls/chown.cpp @@ -13,9 +13,7 @@ KResultOr<FlatPtr> Process::sys$fchown(int fd, UserID uid, GroupID gid) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this); REQUIRE_PROMISE(chown); - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); return description->chown(uid, gid); } diff --git a/Kernel/Syscalls/dup2.cpp b/Kernel/Syscalls/dup2.cpp index b836f1282e..3ccef1dcb9 100644 --- a/Kernel/Syscalls/dup2.cpp +++ b/Kernel/Syscalls/dup2.cpp @@ -13,16 +13,14 @@ KResultOr<FlatPtr> Process::sys$dup2(int old_fd, int new_fd) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this); REQUIRE_PROMISE(stdio); - auto description = fds().file_description(old_fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(old_fd)); if (old_fd == new_fd) return new_fd; if (new_fd < 0 || static_cast<size_t>(new_fd) >= fds().max_open()) return EINVAL; if (!m_fds.m_fds_metadatas[new_fd].is_allocated()) m_fds.m_fds_metadatas[new_fd].allocate(); - m_fds[new_fd].set(*description); + m_fds[new_fd].set(move(description)); return new_fd; } diff --git a/Kernel/Syscalls/fcntl.cpp b/Kernel/Syscalls/fcntl.cpp index 33265302ff..af54a9ff2e 100644 --- a/Kernel/Syscalls/fcntl.cpp +++ b/Kernel/Syscalls/fcntl.cpp @@ -15,9 +15,7 @@ KResultOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, u32 arg) VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this); REQUIRE_PROMISE(stdio); dbgln_if(IO_DEBUG, "sys$fcntl: fd={}, cmd={}, arg={}", fd, cmd, arg); - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); // NOTE: The FD flags are not shared between FileDescription objects. // This means that dup() doesn't copy the FD_CLOEXEC flag! switch (cmd) { diff --git a/Kernel/Syscalls/ftruncate.cpp b/Kernel/Syscalls/ftruncate.cpp index b01780c848..f0a4e148d9 100644 --- a/Kernel/Syscalls/ftruncate.cpp +++ b/Kernel/Syscalls/ftruncate.cpp @@ -17,9 +17,7 @@ KResultOr<FlatPtr> Process::sys$ftruncate(int fd, Userspace<off_t*> userspace_le TRY(copy_from_user(&length, userspace_length)); if (length < 0) return EINVAL; - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); if (!description->is_writable()) return EBADF; return description->truncate(static_cast<u64>(length)); diff --git a/Kernel/Syscalls/get_dir_entries.cpp b/Kernel/Syscalls/get_dir_entries.cpp index 2f98386858..6cbd6c6fc7 100644 --- a/Kernel/Syscalls/get_dir_entries.cpp +++ b/Kernel/Syscalls/get_dir_entries.cpp @@ -15,9 +15,7 @@ KResultOr<FlatPtr> Process::sys$get_dir_entries(int fd, Userspace<void*> user_bu REQUIRE_PROMISE(stdio); if (user_size > NumericLimits<ssize_t>::max()) return EINVAL; - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); auto buffer = UserOrKernelBuffer::for_user_buffer(user_buffer, static_cast<size_t>(user_size)); if (!buffer.has_value()) return EFAULT; diff --git a/Kernel/Syscalls/inode_watcher.cpp b/Kernel/Syscalls/inode_watcher.cpp index 9623634df3..17d7039018 100644 --- a/Kernel/Syscalls/inode_watcher.cpp +++ b/Kernel/Syscalls/inode_watcher.cpp @@ -48,9 +48,7 @@ KResultOr<FlatPtr> Process::sys$inode_watcher_add_watch(Userspace<const Syscall: REQUIRE_PROMISE(rpath); auto params = TRY(copy_typed_from_user(user_params)); - auto description = fds().file_description(params.fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(params.fd)); if (!description->is_inode_watcher()) return EBADF; auto inode_watcher = description->inode_watcher(); @@ -76,9 +74,7 @@ KResultOr<FlatPtr> Process::sys$inode_watcher_add_watch(Userspace<const Syscall: KResultOr<FlatPtr> Process::sys$inode_watcher_remove_watch(int fd, int wd) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); if (!description->is_inode_watcher()) return EBADF; auto inode_watcher = description->inode_watcher(); diff --git a/Kernel/Syscalls/ioctl.cpp b/Kernel/Syscalls/ioctl.cpp index 5a2b740b08..9f9582d6e1 100644 --- a/Kernel/Syscalls/ioctl.cpp +++ b/Kernel/Syscalls/ioctl.cpp @@ -13,9 +13,7 @@ namespace Kernel { KResultOr<FlatPtr> Process::sys$ioctl(int fd, unsigned request, FlatPtr arg) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); if (request == FIONBIO) { description->set_blocking(arg == 0); return KSuccess; diff --git a/Kernel/Syscalls/lseek.cpp b/Kernel/Syscalls/lseek.cpp index a25cd63072..b2c1ddedb0 100644 --- a/Kernel/Syscalls/lseek.cpp +++ b/Kernel/Syscalls/lseek.cpp @@ -13,9 +13,7 @@ KResultOr<FlatPtr> Process::sys$lseek(int fd, Userspace<off_t*> userspace_offset { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) REQUIRE_PROMISE(stdio); - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); off_t offset; TRY(copy_from_user(&offset, userspace_offset)); auto seek_result = TRY(description->seek(offset, whence)); diff --git a/Kernel/Syscalls/mmap.cpp b/Kernel/Syscalls/mmap.cpp index 92e6340a81..7b0e691fb5 100644 --- a/Kernel/Syscalls/mmap.cpp +++ b/Kernel/Syscalls/mmap.cpp @@ -223,9 +223,7 @@ KResultOr<FlatPtr> Process::sys$mmap(Userspace<const Syscall::SC_mmap_params*> u return EINVAL; if (static_cast<size_t>(offset) & ~PAGE_MASK) return EINVAL; - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); if (description->is_directory()) return ENODEV; // Require read access even when read protection is not requested. diff --git a/Kernel/Syscalls/mount.cpp b/Kernel/Syscalls/mount.cpp index 5885d2e28a..c1dffb76cd 100644 --- a/Kernel/Syscalls/mount.cpp +++ b/Kernel/Syscalls/mount.cpp @@ -32,8 +32,8 @@ KResultOr<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*> auto fs_type_string = TRY(try_copy_kstring_from_user(params.fs_type)); auto fs_type = fs_type_string->view(); - auto description = fds().file_description(source_fd); - if (!description.is_null()) + auto description_or_error = fds().file_description(source_fd); + if (!description_or_error.is_error()) dbgln("mount {}: source fd {} @ {}", fs_type, source_fd, target); else dbgln("mount {} @ {}", fs_type, target); @@ -47,8 +47,9 @@ KResultOr<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*> if (params.flags & MS_BIND) { // We're doing a bind mount. - if (description.is_null()) - return EBADF; + if (description_or_error.is_error()) + return description_or_error.error(); + auto description = description_or_error.release_value(); if (!description->custody()) { // We only support bind-mounting inodes, not arbitrary files. return ENODEV; @@ -59,8 +60,9 @@ KResultOr<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*> RefPtr<FileSystem> fs; if (fs_type == "ext2"sv || fs_type == "Ext2FS"sv) { - if (description.is_null()) + if (description_or_error.is_error()) return EBADF; + auto description = description_or_error.release_value(); if (!description->file().is_block_device()) return ENOTBLK; if (!description->file().is_seekable()) { @@ -72,9 +74,9 @@ KResultOr<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*> fs = Ext2FS::create(*description); } else if (fs_type == "9p"sv || fs_type == "Plan9FS"sv) { - if (description.is_null()) + if (description_or_error.is_error()) return EBADF; - + auto description = description_or_error.release_value(); fs = Plan9FS::create(*description); } else if (fs_type == "proc"sv || fs_type == "ProcFS"sv) { fs = TRY(ProcFS::try_create()); @@ -87,15 +89,14 @@ KResultOr<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*> } else if (fs_type == "tmp"sv || fs_type == "TmpFS"sv) { fs = TmpFS::create(); } else if (fs_type == "iso9660"sv || fs_type == "ISO9660FS"sv) { - if (description.is_null()) + if (description_or_error.is_error()) return EBADF; + auto description = description_or_error.release_value(); if (!description->file().is_seekable()) { dbgln("mount: this is not a seekable file"); return ENODEV; } - dbgln("mount: attempting to mount {} on {}", description->absolute_path(), target); - fs = TRY(ISO9660FS::try_create(*description)); } else { return ENODEV; diff --git a/Kernel/Syscalls/open.cpp b/Kernel/Syscalls/open.cpp index 86775e436c..100a077931 100644 --- a/Kernel/Syscalls/open.cpp +++ b/Kernel/Syscalls/open.cpp @@ -46,9 +46,7 @@ KResultOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> u if (dirfd == AT_FDCWD) { base = current_directory(); } else { - auto base_description = fds().file_description(dirfd); - if (!base_description) - return EBADF; + auto base_description = TRY(fds().file_description(dirfd)); if (!base_description->is_directory()) return ENOTDIR; if (!base_description->custody()) @@ -70,10 +68,7 @@ KResultOr<FlatPtr> Process::sys$close(int fd) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) REQUIRE_PROMISE(stdio); - auto description = fds().file_description(fd); - dbgln_if(IO_DEBUG, "sys$close({}) {}", fd, description.ptr()); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); auto result = description->close(); m_fds[fd] = {}; return result; diff --git a/Kernel/Syscalls/read.cpp b/Kernel/Syscalls/read.cpp index 19bb1e1db8..99308cbab5 100644 --- a/Kernel/Syscalls/read.cpp +++ b/Kernel/Syscalls/read.cpp @@ -34,13 +34,9 @@ KResultOr<FlatPtr> Process::sys$readv(int fd, Userspace<const struct iovec*> iov return EINVAL; } - auto description = fds().file_description(fd); - if (!description) - return EBADF; - + auto description = TRY(fds().file_description(fd)); if (!description->is_readable()) return EBADF; - if (description->is_directory()) return EISDIR; @@ -75,9 +71,7 @@ KResultOr<FlatPtr> Process::sys$read(int fd, Userspace<u8*> buffer, size_t size) if (size > NumericLimits<ssize_t>::max()) return EINVAL; dbgln_if(IO_DEBUG, "sys$read({}, {}, {})", fd, buffer.ptr(), size); - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); if (!description->is_readable()) return EBADF; if (description->is_directory()) diff --git a/Kernel/Syscalls/select.cpp b/Kernel/Syscalls/select.cpp index 1e3d4b0b31..9ece4b8463 100644 --- a/Kernel/Syscalls/select.cpp +++ b/Kernel/Syscalls/select.cpp @@ -73,12 +73,8 @@ KResultOr<FlatPtr> Process::sys$select(Userspace<const Syscall::SC_select_params if (block_flags == BlockFlags::None) continue; - auto description = fds().file_description(fd); - if (!description) { - dbgln("sys$select: Bad fd number {}", fd); - return EBADF; - } - if (!fds_info.try_append({ description.release_nonnull(), block_flags })) + auto description = TRY(fds().file_description(fd)); + if (!fds_info.try_append({ move(description), block_flags })) return ENOMEM; if (!selected_fds.try_append(fd)) return ENOMEM; @@ -162,11 +158,7 @@ KResultOr<FlatPtr> Process::sys$poll(Userspace<const Syscall::SC_poll_params*> u Thread::SelectBlocker::FDVector fds_info; for (size_t i = 0; i < params.nfds; i++) { auto& pfd = fds_copy[i]; - auto description = fds().file_description(pfd.fd); - if (!description) { - dbgln("sys$poll: Bad fd number {}", pfd.fd); - return EBADF; - } + auto description = TRY(fds().file_description(pfd.fd)); BlockFlags block_flags = BlockFlags::Exception; // always want POLLERR, POLLHUP, POLLNVAL if (pfd.events & POLLIN) block_flags |= BlockFlags::Read; @@ -174,7 +166,7 @@ KResultOr<FlatPtr> Process::sys$poll(Userspace<const Syscall::SC_poll_params*> u block_flags |= BlockFlags::Write; if (pfd.events & POLLPRI) block_flags |= BlockFlags::ReadPriority; - if (!fds_info.try_append({ description.release_nonnull(), block_flags })) + if (!fds_info.try_append({ move(description), block_flags })) return ENOMEM; } diff --git a/Kernel/Syscalls/sendfd.cpp b/Kernel/Syscalls/sendfd.cpp index fd82027f4f..6c0fed3cac 100644 --- a/Kernel/Syscalls/sendfd.cpp +++ b/Kernel/Syscalls/sendfd.cpp @@ -14,9 +14,7 @@ KResultOr<FlatPtr> Process::sys$sendfd(int sockfd, int fd) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) REQUIRE_PROMISE(sendfd); - auto socket_description = fds().file_description(sockfd); - if (!socket_description) - return EBADF; + auto socket_description = TRY(fds().file_description(sockfd)); if (!socket_description->is_socket()) return ENOTSOCK; auto& socket = *socket_description->socket(); @@ -25,10 +23,7 @@ KResultOr<FlatPtr> Process::sys$sendfd(int sockfd, int fd) if (!socket.is_connected()) return ENOTCONN; - auto passing_descriptor = fds().file_description(fd); - if (!passing_descriptor) - return EBADF; - + auto passing_descriptor = TRY(fds().file_description(fd)); auto& local_socket = static_cast<LocalSocket&>(socket); return local_socket.sendfd(*socket_description, *passing_descriptor); } @@ -37,9 +32,7 @@ KResultOr<FlatPtr> Process::sys$recvfd(int sockfd, int options) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) REQUIRE_PROMISE(recvfd); - auto socket_description = fds().file_description(sockfd); - if (!socket_description) - return EBADF; + auto socket_description = TRY(fds().file_description(sockfd)); if (!socket_description->is_socket()) return ENOTSOCK; auto& socket = *socket_description->socket(); diff --git a/Kernel/Syscalls/socket.cpp b/Kernel/Syscalls/socket.cpp index 179562784d..2794f907b6 100644 --- a/Kernel/Syscalls/socket.cpp +++ b/Kernel/Syscalls/socket.cpp @@ -48,9 +48,7 @@ KResultOr<FlatPtr> Process::sys$socket(int domain, int type, int protocol) KResultOr<FlatPtr> Process::sys$bind(int sockfd, Userspace<const sockaddr*> address, socklen_t address_length) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) - auto description = fds().file_description(sockfd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(sockfd)); if (!description->is_socket()) return ENOTSOCK; auto& socket = *description->socket(); @@ -63,9 +61,7 @@ KResultOr<FlatPtr> Process::sys$listen(int sockfd, int backlog) VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) if (backlog < 0) return EINVAL; - auto description = fds().file_description(sockfd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(sockfd)); if (!description->is_socket()) return ENOTSOCK; auto& socket = *description->socket(); @@ -92,9 +88,7 @@ KResultOr<FlatPtr> Process::sys$accept4(Userspace<const Syscall::SC_accept4_para } auto fd_allocation = TRY(m_fds.allocate()); - auto accepting_socket_description = fds().file_description(accepting_socket_fd); - if (!accepting_socket_description) - return EBADF; + auto accepting_socket_description = TRY(fds().file_description(accepting_socket_fd)); if (!accepting_socket_description->is_socket()) return ENOTSOCK; auto& socket = *accepting_socket_description->socket(); @@ -138,15 +132,11 @@ KResultOr<FlatPtr> Process::sys$accept4(Userspace<const Syscall::SC_accept4_para KResultOr<FlatPtr> Process::sys$connect(int sockfd, Userspace<const sockaddr*> user_address, socklen_t user_address_size) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) - auto description = fds().file_description(sockfd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(sockfd)); if (!description->is_socket()) return ENOTSOCK; - auto& socket = *description->socket(); REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain()); - return socket.connect(*description, user_address, user_address_size, description->is_blocking() ? ShouldBlock::Yes : ShouldBlock::No); } @@ -156,12 +146,9 @@ KResultOr<FlatPtr> Process::sys$shutdown(int sockfd, int how) REQUIRE_PROMISE(stdio); if (how & ~SHUT_RDWR) return EINVAL; - auto description = fds().file_description(sockfd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(sockfd)); if (!description->is_socket()) return ENOTSOCK; - auto& socket = *description->socket(); REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain()); return socket.shutdown(how); @@ -186,9 +173,7 @@ KResultOr<FlatPtr> Process::sys$sendmsg(int sockfd, Userspace<const struct msghd Userspace<const sockaddr*> user_addr((FlatPtr)msg.msg_name); socklen_t addr_length = msg.msg_namelen; - auto description = fds().file_description(sockfd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(sockfd)); if (!description->is_socket()) return ENOTSOCK; auto& socket = *description->socket(); @@ -222,9 +207,7 @@ KResultOr<FlatPtr> Process::sys$recvmsg(int sockfd, Userspace<struct msghdr*> us Userspace<sockaddr*> user_addr((FlatPtr)msg.msg_name); Userspace<socklen_t*> user_addr_length(msg.msg_name ? (FlatPtr)&user_msg.unsafe_userspace_ptr()->msg_namelen : 0); - auto description = fds().file_description(sockfd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(sockfd)); if (!description->is_socket()) return ENOTSOCK; auto& socket = *description->socket(); @@ -282,10 +265,7 @@ KResult Process::get_sock_or_peer_name(const Params& params) if (addrlen_value <= 0) return EINVAL; - auto description = fds().file_description(params.sockfd); - if (!description) - return EBADF; - + auto description = TRY(fds().file_description(params.sockfd)); if (!description->is_socket()) return ENOTSOCK; @@ -330,13 +310,10 @@ KResultOr<FlatPtr> Process::sys$getsockopt(Userspace<const Syscall::SC_getsockop socklen_t value_size; TRY(copy_from_user(&value_size, params.value_size, sizeof(socklen_t))); - auto description = fds().file_description(sockfd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(sockfd)); if (!description->is_socket()) return ENOTSOCK; auto& socket = *description->socket(); - REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain()); return socket.getsockopt(*description, level, option, user_value, user_value_size); } @@ -347,9 +324,7 @@ KResultOr<FlatPtr> Process::sys$setsockopt(Userspace<const Syscall::SC_setsockop auto params = TRY(copy_typed_from_user(user_params)); Userspace<const void*> user_value((FlatPtr)params.value); - auto description = fds().file_description(params.sockfd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(params.sockfd)); if (!description->is_socket()) return ENOTSOCK; auto& socket = *description->socket(); diff --git a/Kernel/Syscalls/stat.cpp b/Kernel/Syscalls/stat.cpp index dc601d9f08..31d0cd9c6e 100644 --- a/Kernel/Syscalls/stat.cpp +++ b/Kernel/Syscalls/stat.cpp @@ -15,9 +15,7 @@ KResultOr<FlatPtr> Process::sys$fstat(int fd, Userspace<stat*> user_statbuf) { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) REQUIRE_PROMISE(stdio); - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); stat buffer = {}; TRY(description->stat(buffer)); return copy_to_user(user_statbuf, &buffer); @@ -35,9 +33,7 @@ KResultOr<FlatPtr> Process::sys$stat(Userspace<const Syscall::SC_stat_params*> u if (params.dirfd == AT_FDCWD) { base = current_directory(); } else { - auto base_description = fds().file_description(params.dirfd); - if (!base_description) - return EBADF; + auto base_description = TRY(fds().file_description(params.dirfd)); if (!base_description->is_directory()) return ENOTDIR; if (!base_description->custody()) diff --git a/Kernel/Syscalls/statvfs.cpp b/Kernel/Syscalls/statvfs.cpp index e9a91bc502..a530266c86 100644 --- a/Kernel/Syscalls/statvfs.cpp +++ b/Kernel/Syscalls/statvfs.cpp @@ -76,10 +76,7 @@ KResultOr<FlatPtr> Process::sys$fstatvfs(int fd, statvfs* buf) VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) REQUIRE_PROMISE(stdio); - auto description = fds().file_description(fd); - if (!description) - return EBADF; - + auto description = TRY(fds().file_description(fd)); return do_statvfs(description->absolute_path(), buf); } diff --git a/Kernel/Syscalls/ttyname.cpp b/Kernel/Syscalls/ttyname.cpp index 5e795d9ff6..1b0cb09309 100644 --- a/Kernel/Syscalls/ttyname.cpp +++ b/Kernel/Syscalls/ttyname.cpp @@ -15,9 +15,7 @@ KResultOr<FlatPtr> Process::sys$ttyname(int fd, Userspace<char*> buffer, size_t { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) REQUIRE_PROMISE(tty); - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); if (!description->is_tty()) return ENOTTY; auto tty_name = description->tty()->tty_name(); @@ -30,9 +28,7 @@ KResultOr<FlatPtr> Process::sys$ptsname(int fd, Userspace<char*> buffer, size_t { VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) REQUIRE_PROMISE(tty); - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); auto* master_pty = description->master_pty(); if (!master_pty) return ENOTTY; diff --git a/Kernel/Syscalls/write.cpp b/Kernel/Syscalls/write.cpp index 4329666d3c..aaec7dc66f 100644 --- a/Kernel/Syscalls/write.cpp +++ b/Kernel/Syscalls/write.cpp @@ -33,10 +33,7 @@ KResultOr<FlatPtr> Process::sys$writev(int fd, Userspace<const struct iovec*> io return EINVAL; } - auto description = fds().file_description(fd); - if (!description) - return EBADF; - + auto description = TRY(fds().file_description(fd)); if (!description->is_writable()) return EBADF; @@ -104,9 +101,7 @@ KResultOr<FlatPtr> Process::sys$write(int fd, Userspace<const u8*> data, size_t return EINVAL; dbgln_if(IO_DEBUG, "sys$write({}, {}, {})", fd, data.ptr(), size); - auto description = fds().file_description(fd); - if (!description) - return EBADF; + auto description = TRY(fds().file_description(fd)); if (!description->is_writable()) return EBADF; |