mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 14:35:07 +00:00
Kernel: Make Process::FileDescriptions::allocate return KResultOr<int>
Modernize more error checking by utilizing KResultOr.
This commit is contained in:
parent
d2cee9cbf6
commit
ba03b6ad02
10 changed files with 48 additions and 37 deletions
|
@ -465,14 +465,14 @@ size_t Process::FileDescriptions::open_count() const
|
|||
return count;
|
||||
}
|
||||
|
||||
int Process::FileDescriptions::allocate(int first_candidate_fd)
|
||||
KResultOr<int> Process::FileDescriptions::allocate(int first_candidate_fd)
|
||||
{
|
||||
ScopedSpinLock lock(m_fds_lock);
|
||||
for (size_t i = first_candidate_fd; i < max_open(); ++i) {
|
||||
if (!m_fds_metadatas[i])
|
||||
return i;
|
||||
}
|
||||
return -EMFILE;
|
||||
return KResult(EMFILE);
|
||||
}
|
||||
|
||||
Time kgettimeofday()
|
||||
|
|
|
@ -641,7 +641,7 @@ public:
|
|||
void enumerate(Function<void(const FileDescriptionAndFlags&)>) const;
|
||||
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;
|
||||
|
||||
bool try_resize(size_t size) { return m_fds_metadatas.try_resize(size); }
|
||||
|
|
|
@ -25,10 +25,10 @@ KResultOr<FlatPtr> Process::sys$anon_create(size_t size, int options)
|
|||
if (size > NumericLimits<ssize_t>::max())
|
||||
return EINVAL;
|
||||
|
||||
int new_fd = m_fds.allocate();
|
||||
if (new_fd < 0)
|
||||
return new_fd;
|
||||
|
||||
auto new_fd_or_error = m_fds.allocate();
|
||||
if (new_fd_or_error.is_error())
|
||||
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);
|
||||
if (!vmobject)
|
||||
return ENOMEM;
|
||||
|
|
|
@ -591,7 +591,7 @@ KResult Process::do_exec(NonnullRefPtr<FileDescription> main_program_description
|
|||
|
||||
int main_program_fd = -1;
|
||||
if (interpreter_description) {
|
||||
main_program_fd = m_fds.allocate();
|
||||
main_program_fd = m_fds.allocate().value();
|
||||
VERIFY(main_program_fd >= 0);
|
||||
auto seek_result = main_program_description->seek(0, SEEK_SET);
|
||||
VERIFY(!seek_result.is_error());
|
||||
|
|
|
@ -25,9 +25,10 @@ KResultOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, u32 arg)
|
|||
int arg_fd = (int)arg;
|
||||
if (arg_fd < 0)
|
||||
return EINVAL;
|
||||
int new_fd = fds().allocate(arg_fd);
|
||||
if (new_fd < 0)
|
||||
return new_fd;
|
||||
auto new_fd_or_error = fds().allocate(arg_fd);
|
||||
if (new_fd_or_error.is_error())
|
||||
return new_fd_or_error.error();
|
||||
auto new_fd = new_fd_or_error.value();
|
||||
m_fds[new_fd].set(*description);
|
||||
return new_fd;
|
||||
}
|
||||
|
|
|
@ -18,9 +18,10 @@ KResultOr<FlatPtr> Process::sys$create_inode_watcher(u32 flags)
|
|||
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
||||
REQUIRE_PROMISE(rpath);
|
||||
|
||||
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();
|
||||
|
||||
auto watcher_or_error = InodeWatcher::create();
|
||||
if (watcher_or_error.is_error())
|
||||
|
|
|
@ -45,10 +45,11 @@ KResultOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> u
|
|||
return path.error();
|
||||
|
||||
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;
|
||||
if (dirfd == AT_FDCWD) {
|
||||
base = current_directory();
|
||||
|
|
|
@ -30,13 +30,19 @@ KResultOr<FlatPtr> Process::sys$pipe(int pipefd[2], int flags)
|
|||
if (open_writer_result.is_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].description()->set_readable(true);
|
||||
if (!copy_to_user(&pipefd[0], &reader_fd))
|
||||
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].description()->set_writable(true);
|
||||
if (!copy_to_user(&pipefd[1], &writer_fd))
|
||||
|
|
|
@ -46,9 +46,10 @@ KResultOr<FlatPtr> Process::sys$recvfd(int sockfd, int options)
|
|||
if (!socket.is_local())
|
||||
return EAFNOSUPPORT;
|
||||
|
||||
int new_fd = m_fds.allocate();
|
||||
if (new_fd < 0)
|
||||
return new_fd;
|
||||
auto new_fd_or_error = m_fds.allocate();
|
||||
if (new_fd_or_error.is_error())
|
||||
return new_fd_or_error.error();
|
||||
auto new_fd = new_fd_or_error.value();
|
||||
|
||||
auto& local_socket = static_cast<LocalSocket&>(socket);
|
||||
auto received_descriptor_or_error = local_socket.recvfd(*socket_description);
|
||||
|
|
|
@ -39,9 +39,10 @@ KResultOr<FlatPtr> Process::sys$socket(int domain, int type, int protocol)
|
|||
|
||||
if ((type & SOCK_TYPE_MASK) == SOCK_RAW && !is_superuser())
|
||||
return EACCES;
|
||||
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();
|
||||
auto result = Socket::create(domain, type, protocol);
|
||||
if (result.is_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)))
|
||||
return EFAULT;
|
||||
|
||||
int accepted_socket_fd = m_fds.allocate();
|
||||
if (accepted_socket_fd < 0)
|
||||
return accepted_socket_fd;
|
||||
auto accepted_socket_fd_or_error = m_fds.allocate();
|
||||
if (accepted_socket_fd_or_error.is_error())
|
||||
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);
|
||||
if (!accepting_socket_description)
|
||||
return EBADF;
|
||||
|
@ -411,23 +413,22 @@ KResultOr<FlatPtr> Process::sys$socketpair(Userspace<const Syscall::SC_socketpai
|
|||
auto pair = result.value();
|
||||
|
||||
int fds[2];
|
||||
fds[0] = m_fds.allocate();
|
||||
if (fds[0] < 0)
|
||||
return ENFILE;
|
||||
auto fd1_or_error = m_fds.allocate();
|
||||
if (fd1_or_error.is_error())
|
||||
return fd1_or_error.error();
|
||||
fds[0] = fd1_or_error.value();
|
||||
setup_socket_fd(fds[0], pair.description1, params.type);
|
||||
|
||||
fds[1] = m_fds.allocate();
|
||||
if (fds[1] < 0) {
|
||||
// FIXME: This leaks fds[0]
|
||||
return ENFILE;
|
||||
}
|
||||
auto fd2_or_error = m_fds.allocate();
|
||||
if (fd2_or_error.is_error())
|
||||
return fd2_or_error.error();
|
||||
fds[1] = fd2_or_error.value();
|
||||
setup_socket_fd(fds[1], pair.description2, params.type);
|
||||
|
||||
if (!copy_to_user(params.sv, fds, sizeof(fds))) {
|
||||
// FIXME: This leaks both file descriptors
|
||||
return EFAULT;
|
||||
}
|
||||
|
||||
return KSuccess;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue