1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-22 19:25:07 +00:00

Kernel: Make Process::FileDescriptions::allocate return KResultOr<int>

Modernize more error checking by utilizing KResultOr.
This commit is contained in:
Brian Gianforcaro 2021-07-27 02:12:51 -07:00 committed by Andreas Kling
parent d2cee9cbf6
commit ba03b6ad02
10 changed files with 48 additions and 37 deletions

View file

@ -465,14 +465,14 @@ size_t Process::FileDescriptions::open_count() const
return count; return count;
} }
int Process::FileDescriptions::allocate(int first_candidate_fd) KResultOr<int> Process::FileDescriptions::allocate(int first_candidate_fd)
{ {
ScopedSpinLock lock(m_fds_lock); ScopedSpinLock lock(m_fds_lock);
for (size_t i = first_candidate_fd; i < max_open(); ++i) { for (size_t i = first_candidate_fd; i < max_open(); ++i) {
if (!m_fds_metadatas[i]) if (!m_fds_metadatas[i])
return i; return i;
} }
return -EMFILE; return KResult(EMFILE);
} }
Time kgettimeofday() Time kgettimeofday()

View file

@ -641,7 +641,7 @@ public:
void enumerate(Function<void(const FileDescriptionAndFlags&)>) const; void enumerate(Function<void(const FileDescriptionAndFlags&)>) const;
void change_each(Function<void(FileDescriptionAndFlags&)>); void change_each(Function<void(FileDescriptionAndFlags&)>);
int allocate(int first_candidate_fd = 0); KResultOr<int> allocate(int first_candidate_fd = 0);
size_t open_count() const; size_t open_count() const;
bool try_resize(size_t size) { return m_fds_metadatas.try_resize(size); } bool try_resize(size_t size) { return m_fds_metadatas.try_resize(size); }

View file

@ -25,10 +25,10 @@ KResultOr<FlatPtr> Process::sys$anon_create(size_t size, int options)
if (size > NumericLimits<ssize_t>::max()) if (size > NumericLimits<ssize_t>::max())
return EINVAL; return EINVAL;
int new_fd = m_fds.allocate(); auto new_fd_or_error = m_fds.allocate();
if (new_fd < 0) if (new_fd_or_error.is_error())
return new_fd; return new_fd_or_error.error();
auto new_fd = new_fd_or_error.value();
auto vmobject = AnonymousVMObject::try_create_purgeable_with_size(size, AllocationStrategy::Reserve); auto vmobject = AnonymousVMObject::try_create_purgeable_with_size(size, AllocationStrategy::Reserve);
if (!vmobject) if (!vmobject)
return ENOMEM; return ENOMEM;

View file

@ -591,7 +591,7 @@ KResult Process::do_exec(NonnullRefPtr<FileDescription> main_program_description
int main_program_fd = -1; int main_program_fd = -1;
if (interpreter_description) { if (interpreter_description) {
main_program_fd = m_fds.allocate(); main_program_fd = m_fds.allocate().value();
VERIFY(main_program_fd >= 0); VERIFY(main_program_fd >= 0);
auto seek_result = main_program_description->seek(0, SEEK_SET); auto seek_result = main_program_description->seek(0, SEEK_SET);
VERIFY(!seek_result.is_error()); VERIFY(!seek_result.is_error());

View file

@ -25,9 +25,10 @@ KResultOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, u32 arg)
int arg_fd = (int)arg; int arg_fd = (int)arg;
if (arg_fd < 0) if (arg_fd < 0)
return EINVAL; return EINVAL;
int new_fd = fds().allocate(arg_fd); auto new_fd_or_error = fds().allocate(arg_fd);
if (new_fd < 0) if (new_fd_or_error.is_error())
return new_fd; return new_fd_or_error.error();
auto new_fd = new_fd_or_error.value();
m_fds[new_fd].set(*description); m_fds[new_fd].set(*description);
return new_fd; return new_fd;
} }

View file

@ -18,9 +18,10 @@ KResultOr<FlatPtr> Process::sys$create_inode_watcher(u32 flags)
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
REQUIRE_PROMISE(rpath); REQUIRE_PROMISE(rpath);
int fd = m_fds.allocate(); auto fd_or_error = m_fds.allocate();
if (fd < 0) if (fd_or_error.is_error())
return fd; return fd_or_error.error();
auto fd = fd_or_error.value();
auto watcher_or_error = InodeWatcher::create(); auto watcher_or_error = InodeWatcher::create();
if (watcher_or_error.is_error()) if (watcher_or_error.is_error())

View file

@ -45,10 +45,11 @@ KResultOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> u
return path.error(); return path.error();
dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path.value()->view(), options, mode); dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path.value()->view(), options, mode);
int fd = m_fds.allocate();
if (fd < 0)
return fd;
auto fd_or_error = m_fds.allocate();
if (fd_or_error.is_error())
return fd_or_error.error();
auto fd = fd_or_error.value();
RefPtr<Custody> base; RefPtr<Custody> base;
if (dirfd == AT_FDCWD) { if (dirfd == AT_FDCWD) {
base = current_directory(); base = current_directory();

View file

@ -30,13 +30,19 @@ KResultOr<FlatPtr> Process::sys$pipe(int pipefd[2], int flags)
if (open_writer_result.is_error()) if (open_writer_result.is_error())
return open_writer_result.error(); return open_writer_result.error();
int reader_fd = m_fds.allocate(); auto reader_fd_or_error = m_fds.allocate();
if (reader_fd_or_error.is_error())
return reader_fd_or_error.error();
auto reader_fd = reader_fd_or_error.value();
m_fds[reader_fd].set(open_reader_result.release_value(), fd_flags); m_fds[reader_fd].set(open_reader_result.release_value(), fd_flags);
m_fds[reader_fd].description()->set_readable(true); m_fds[reader_fd].description()->set_readable(true);
if (!copy_to_user(&pipefd[0], &reader_fd)) if (!copy_to_user(&pipefd[0], &reader_fd))
return EFAULT; return EFAULT;
int writer_fd = m_fds.allocate(); auto writer_fd_or_error = m_fds.allocate();
if (writer_fd_or_error.is_error())
return writer_fd_or_error.error();
auto writer_fd = writer_fd_or_error.value();
m_fds[writer_fd].set(open_writer_result.release_value(), fd_flags); m_fds[writer_fd].set(open_writer_result.release_value(), fd_flags);
m_fds[writer_fd].description()->set_writable(true); m_fds[writer_fd].description()->set_writable(true);
if (!copy_to_user(&pipefd[1], &writer_fd)) if (!copy_to_user(&pipefd[1], &writer_fd))

View file

@ -46,9 +46,10 @@ KResultOr<FlatPtr> Process::sys$recvfd(int sockfd, int options)
if (!socket.is_local()) if (!socket.is_local())
return EAFNOSUPPORT; return EAFNOSUPPORT;
int new_fd = m_fds.allocate(); auto new_fd_or_error = m_fds.allocate();
if (new_fd < 0) if (new_fd_or_error.is_error())
return new_fd; return new_fd_or_error.error();
auto new_fd = new_fd_or_error.value();
auto& local_socket = static_cast<LocalSocket&>(socket); auto& local_socket = static_cast<LocalSocket&>(socket);
auto received_descriptor_or_error = local_socket.recvfd(*socket_description); auto received_descriptor_or_error = local_socket.recvfd(*socket_description);

View file

@ -39,9 +39,10 @@ KResultOr<FlatPtr> Process::sys$socket(int domain, int type, int protocol)
if ((type & SOCK_TYPE_MASK) == SOCK_RAW && !is_superuser()) if ((type & SOCK_TYPE_MASK) == SOCK_RAW && !is_superuser())
return EACCES; return EACCES;
int fd = m_fds.allocate(); auto fd_or_error = m_fds.allocate();
if (fd < 0) if (fd_or_error.is_error())
return fd; return fd_or_error.error();
auto fd = fd_or_error.value();
auto result = Socket::create(domain, type, protocol); auto result = Socket::create(domain, type, protocol);
if (result.is_error()) if (result.is_error())
return result.error(); return result.error();
@ -100,9 +101,10 @@ KResultOr<FlatPtr> Process::sys$accept4(Userspace<const Syscall::SC_accept4_para
if (user_address && !copy_from_user(&address_size, static_ptr_cast<const socklen_t*>(user_address_size))) if (user_address && !copy_from_user(&address_size, static_ptr_cast<const socklen_t*>(user_address_size)))
return EFAULT; return EFAULT;
int accepted_socket_fd = m_fds.allocate(); auto accepted_socket_fd_or_error = m_fds.allocate();
if (accepted_socket_fd < 0) if (accepted_socket_fd_or_error.is_error())
return accepted_socket_fd; return accepted_socket_fd_or_error.error();
auto accepted_socket_fd = accepted_socket_fd_or_error.value();
auto accepting_socket_description = fds().file_description(accepting_socket_fd); auto accepting_socket_description = fds().file_description(accepting_socket_fd);
if (!accepting_socket_description) if (!accepting_socket_description)
return EBADF; return EBADF;
@ -411,23 +413,22 @@ KResultOr<FlatPtr> Process::sys$socketpair(Userspace<const Syscall::SC_socketpai
auto pair = result.value(); auto pair = result.value();
int fds[2]; int fds[2];
fds[0] = m_fds.allocate(); auto fd1_or_error = m_fds.allocate();
if (fds[0] < 0) if (fd1_or_error.is_error())
return ENFILE; return fd1_or_error.error();
fds[0] = fd1_or_error.value();
setup_socket_fd(fds[0], pair.description1, params.type); setup_socket_fd(fds[0], pair.description1, params.type);
fds[1] = m_fds.allocate(); auto fd2_or_error = m_fds.allocate();
if (fds[1] < 0) { if (fd2_or_error.is_error())
// FIXME: This leaks fds[0] return fd2_or_error.error();
return ENFILE; fds[1] = fd2_or_error.value();
}
setup_socket_fd(fds[1], pair.description2, params.type); setup_socket_fd(fds[1], pair.description2, params.type);
if (!copy_to_user(params.sv, fds, sizeof(fds))) { if (!copy_to_user(params.sv, fds, sizeof(fds))) {
// FIXME: This leaks both file descriptors // FIXME: This leaks both file descriptors
return EFAULT; return EFAULT;
} }
return KSuccess; return KSuccess;
} }
} }