mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 14:28:12 +00:00
Kernel: Make all syscall functions return KResultOr<T>
This makes it a lot easier to return errors since we no longer have to worry about negating EFOO errors and can just return them flat.
This commit is contained in:
parent
9af1e1a3bf
commit
ac71775de5
70 changed files with 747 additions and 742 deletions
|
@ -31,57 +31,57 @@
|
|||
|
||||
namespace Kernel {
|
||||
|
||||
ssize_t Process::sys$writev(int fd, Userspace<const struct iovec*> iov, int iov_count)
|
||||
KResultOr<ssize_t> Process::sys$writev(int fd, Userspace<const struct iovec*> iov, int iov_count)
|
||||
{
|
||||
REQUIRE_PROMISE(stdio);
|
||||
if (iov_count < 0)
|
||||
return -EINVAL;
|
||||
return EINVAL;
|
||||
|
||||
// Arbitrary pain threshold.
|
||||
if (iov_count > (int)MiB)
|
||||
return -EFAULT;
|
||||
return EFAULT;
|
||||
|
||||
u64 total_length = 0;
|
||||
Vector<iovec, 32> vecs;
|
||||
vecs.resize(iov_count);
|
||||
if (!copy_n_from_user(vecs.data(), iov, iov_count))
|
||||
return -EFAULT;
|
||||
return EFAULT;
|
||||
for (auto& vec : vecs) {
|
||||
total_length += vec.iov_len;
|
||||
if (total_length > NumericLimits<i32>::max())
|
||||
return -EINVAL;
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
auto description = file_description(fd);
|
||||
if (!description)
|
||||
return -EBADF;
|
||||
return EBADF;
|
||||
|
||||
if (!description->is_writable())
|
||||
return -EBADF;
|
||||
return EBADF;
|
||||
|
||||
int nwritten = 0;
|
||||
for (auto& vec : vecs) {
|
||||
auto buffer = UserOrKernelBuffer::for_user_buffer((u8*)vec.iov_base, vec.iov_len);
|
||||
if (!buffer.has_value())
|
||||
return -EFAULT;
|
||||
int rc = do_write(*description, buffer.value(), vec.iov_len);
|
||||
if (rc < 0) {
|
||||
return EFAULT;
|
||||
auto result = do_write(*description, buffer.value(), vec.iov_len);
|
||||
if (result.is_error()) {
|
||||
if (nwritten == 0)
|
||||
return rc;
|
||||
return result.error();
|
||||
return nwritten;
|
||||
}
|
||||
nwritten += rc;
|
||||
nwritten += result.value();
|
||||
}
|
||||
|
||||
return nwritten;
|
||||
}
|
||||
|
||||
ssize_t Process::do_write(FileDescription& description, const UserOrKernelBuffer& data, size_t data_size)
|
||||
KResultOr<ssize_t> Process::do_write(FileDescription& description, const UserOrKernelBuffer& data, size_t data_size)
|
||||
{
|
||||
ssize_t total_nwritten = 0;
|
||||
if (!description.is_blocking()) {
|
||||
if (!description.can_write())
|
||||
return -EAGAIN;
|
||||
return EAGAIN;
|
||||
}
|
||||
|
||||
if (description.should_append())
|
||||
|
@ -97,7 +97,7 @@ ssize_t Process::do_write(FileDescription& description, const UserOrKernelBuffer
|
|||
auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
|
||||
if (Thread::current()->block<Thread::WriteBlocker>({}, description, unblock_flags).was_interrupted()) {
|
||||
if (total_nwritten == 0)
|
||||
return -EINTR;
|
||||
return EINTR;
|
||||
}
|
||||
// TODO: handle exceptions in unblock_flags
|
||||
}
|
||||
|
@ -114,24 +114,24 @@ ssize_t Process::do_write(FileDescription& description, const UserOrKernelBuffer
|
|||
return total_nwritten;
|
||||
}
|
||||
|
||||
ssize_t Process::sys$write(int fd, const u8* data, ssize_t size)
|
||||
KResultOr<ssize_t> Process::sys$write(int fd, const u8* data, ssize_t size)
|
||||
{
|
||||
REQUIRE_PROMISE(stdio);
|
||||
if (size < 0)
|
||||
return -EINVAL;
|
||||
return EINVAL;
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
dbgln_if(IO_DEBUG, "sys$write({}, {}, {})", fd, data, size);
|
||||
auto description = file_description(fd);
|
||||
if (!description)
|
||||
return -EBADF;
|
||||
return EBADF;
|
||||
if (!description->is_writable())
|
||||
return -EBADF;
|
||||
return EBADF;
|
||||
|
||||
auto buffer = UserOrKernelBuffer::for_user_buffer(const_cast<u8*>(data), (size_t)size);
|
||||
if (!buffer.has_value())
|
||||
return -EFAULT;
|
||||
return EFAULT;
|
||||
return do_write(*description, buffer.value(), size);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue