2020-07-30 23:38:15 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 23:38:15 +02:00
|
|
|
*/
|
|
|
|
|
2021-01-25 16:07:10 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-07 03:01:11 -08:00
|
|
|
using BlockFlags = Thread::FileBlocker::BlockFlags;
|
|
|
|
|
2021-06-28 20:59:35 +02:00
|
|
|
KResultOr<FlatPtr> Process::sys$readv(int fd, Userspace<const struct iovec*> iov, int iov_count)
|
2021-02-12 05:40:03 +03:30
|
|
|
{
|
2021-07-18 11:20:12 -07:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-02-12 05:40:03 +03:30
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
if (iov_count < 0)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2021-02-12 05:40:03 +03:30
|
|
|
|
2021-02-15 21:06:18 +01:00
|
|
|
// Arbitrary pain threshold.
|
|
|
|
if (iov_count > (int)MiB)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EFAULT;
|
2021-02-12 05:40:03 +03:30
|
|
|
|
|
|
|
u64 total_length = 0;
|
|
|
|
Vector<iovec, 32> vecs;
|
2021-04-29 01:20:24 -07:00
|
|
|
if (!vecs.try_resize(iov_count))
|
|
|
|
return ENOMEM;
|
2021-02-12 05:40:03 +03:30
|
|
|
if (!copy_n_from_user(vecs.data(), iov, iov_count))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EFAULT;
|
2021-02-12 05:40:03 +03:30
|
|
|
for (auto& vec : vecs) {
|
|
|
|
total_length += vec.iov_len;
|
|
|
|
if (total_length > NumericLimits<i32>::max())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2021-02-12 05:40:03 +03:30
|
|
|
}
|
|
|
|
|
2021-06-22 21:22:17 +03:00
|
|
|
auto description = fds().file_description(fd);
|
2021-02-12 05:40:03 +03:30
|
|
|
if (!description)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2021-02-12 05:40:03 +03:30
|
|
|
|
|
|
|
if (!description->is_readable())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2021-02-12 05:40:03 +03:30
|
|
|
|
|
|
|
if (description->is_directory())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EISDIR;
|
2021-02-12 05:40:03 +03:30
|
|
|
|
|
|
|
int nread = 0;
|
|
|
|
for (auto& vec : vecs) {
|
|
|
|
if (description->is_blocking()) {
|
|
|
|
if (!description->can_read()) {
|
2021-03-07 03:01:11 -08:00
|
|
|
auto unblock_flags = BlockFlags::None;
|
2021-02-12 05:40:03 +03:30
|
|
|
if (Thread::current()->block<Thread::ReadBlocker>({}, *description, unblock_flags).was_interrupted())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINTR;
|
2021-03-07 03:01:11 -08:00
|
|
|
if (!has_flag(unblock_flags, BlockFlags::Read))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EAGAIN;
|
2021-02-12 05:40:03 +03:30
|
|
|
// TODO: handle exceptions in unblock_flags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto buffer = UserOrKernelBuffer::for_user_buffer((u8*)vec.iov_base, vec.iov_len);
|
|
|
|
if (!buffer.has_value())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EFAULT;
|
2021-02-12 05:40:03 +03:30
|
|
|
auto result = description->read(buffer.value(), vec.iov_len);
|
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
|
|
|
nread += result.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2021-06-28 20:59:35 +02:00
|
|
|
KResultOr<FlatPtr> Process::sys$read(int fd, Userspace<u8*> buffer, size_t size)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-07-18 11:20:12 -07:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 23:38:15 +02:00
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
2021-06-16 16:44:15 +02:00
|
|
|
if (size > NumericLimits<ssize_t>::max())
|
|
|
|
return EINVAL;
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(IO_DEBUG, "sys$read({}, {}, {})", fd, buffer.ptr(), size);
|
2021-06-22 21:22:17 +03:00
|
|
|
auto description = fds().file_description(fd);
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!description)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!description->is_readable())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (description->is_directory())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EISDIR;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (description->is_blocking()) {
|
|
|
|
if (!description->can_read()) {
|
2021-03-07 03:01:11 -08:00
|
|
|
auto unblock_flags = BlockFlags::None;
|
2021-01-10 16:29:28 -07:00
|
|
|
if (Thread::current()->block<Thread::ReadBlocker>({}, *description, unblock_flags).was_interrupted())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINTR;
|
2021-03-07 03:01:11 -08:00
|
|
|
if (!has_flag(unblock_flags, BlockFlags::Read))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EAGAIN;
|
2020-11-29 16:05:27 -07:00
|
|
|
// TODO: handle exceptions in unblock_flags
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-11 21:11:07 -06:00
|
|
|
auto user_buffer = UserOrKernelBuffer::for_user_buffer(buffer, size);
|
|
|
|
if (!user_buffer.has_value())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EFAULT;
|
2020-09-11 21:11:07 -06:00
|
|
|
auto result = description->read(user_buffer.value(), size);
|
2020-08-04 18:02:23 +02:00
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
|
|
|
return result.value();
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|