diff --git a/Kernel/FileSystem/FIFO.cpp b/Kernel/FileSystem/FIFO.cpp index 42c76c33ab..002c220247 100644 --- a/Kernel/FileSystem/FIFO.cpp +++ b/Kernel/FileSystem/FIFO.cpp @@ -16,12 +16,10 @@ namespace Kernel { static Atomic s_next_fifo_id = 1; -RefPtr FIFO::try_create(UserID uid) +KResultOr> FIFO::try_create(UserID uid) { - auto buffer_or_error = DoubleBuffer::try_create(); - if (buffer_or_error.is_error()) - return {}; - return adopt_ref_if_nonnull(new (nothrow) FIFO(uid, buffer_or_error.release_value())); + auto buffer = TRY(DoubleBuffer::try_create()); + return adopt_nonnull_ref_or_enomem(new (nothrow) FIFO(uid, move(buffer))); } KResultOr> FIFO::open_direction(FIFO::Direction direction) diff --git a/Kernel/FileSystem/FIFO.h b/Kernel/FileSystem/FIFO.h index 55d2649a03..66b152a818 100644 --- a/Kernel/FileSystem/FIFO.h +++ b/Kernel/FileSystem/FIFO.h @@ -24,7 +24,7 @@ public: Writer }; - static RefPtr try_create(UserID); + static KResultOr> try_create(UserID); virtual ~FIFO() override; UserID uid() const { return m_uid; } diff --git a/Kernel/FileSystem/Inode.cpp b/Kernel/FileSystem/Inode.cpp index bfb7186e29..b301745df4 100644 --- a/Kernel/FileSystem/Inode.cpp +++ b/Kernel/FileSystem/Inode.cpp @@ -163,19 +163,15 @@ void Inode::unregister_watcher(Badge, InodeWatcher& watcher) m_watchers.remove(&watcher); } -NonnullRefPtr Inode::fifo() +KResultOr> Inode::fifo() { MutexLocker locker(m_inode_lock); VERIFY(metadata().is_fifo()); // FIXME: Release m_fifo when it is closed by all readers and writers - if (!m_fifo) { - m_fifo = FIFO::try_create(metadata().uid); - // FIXME: We need to be able to observe OOM here. - VERIFY(!m_fifo.is_null()); - } + if (!m_fifo) + m_fifo = TRY(FIFO::try_create(metadata().uid)); - VERIFY(m_fifo); return *m_fifo; } diff --git a/Kernel/FileSystem/Inode.h b/Kernel/FileSystem/Inode.h index 5c3db8f135..646cd068f1 100644 --- a/Kernel/FileSystem/Inode.h +++ b/Kernel/FileSystem/Inode.h @@ -94,7 +94,7 @@ public: void register_watcher(Badge, InodeWatcher&); void unregister_watcher(Badge, InodeWatcher&); - NonnullRefPtr fifo(); + KResultOr> fifo(); KResult can_apply_flock(OpenFileDescription const&, flock const&) const; KResult apply_flock(Process const&, OpenFileDescription const&, Userspace); diff --git a/Kernel/FileSystem/VirtualFileSystem.cpp b/Kernel/FileSystem/VirtualFileSystem.cpp index 1d1de17092..375355c313 100644 --- a/Kernel/FileSystem/VirtualFileSystem.cpp +++ b/Kernel/FileSystem/VirtualFileSystem.cpp @@ -250,7 +250,7 @@ KResultOr> VirtualFileSystem::open(StringView return *preopen_fd; if (metadata.is_fifo()) { - auto fifo = inode.fifo(); + auto fifo = TRY(inode.fifo()); if (options & O_WRONLY) { auto description = TRY(fifo->open_direction_blocking(FIFO::Direction::Writer)); description->set_rw_mode(options); diff --git a/Kernel/Syscalls/pipe.cpp b/Kernel/Syscalls/pipe.cpp index 1a2cc92128..7e31850481 100644 --- a/Kernel/Syscalls/pipe.cpp +++ b/Kernel/Syscalls/pipe.cpp @@ -20,9 +20,7 @@ KResultOr Process::sys$pipe(int pipefd[2], int flags) return EINVAL; u32 fd_flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0; - auto fifo = FIFO::try_create(uid()); - if (!fifo) - return ENOMEM; + auto fifo = TRY(FIFO::try_create(uid())); auto reader_fd_allocation = TRY(m_fds.allocate()); auto writer_fd_allocation = TRY(m_fds.allocate());