2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-07 11:39:11 +00:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2021-12-04 12:26:13 +00:00
|
|
|
#include <Kernel/PerformanceManager.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-07 11:01:11 +00:00
|
|
|
using BlockFlags = Thread::FileBlocker::BlockFlags;
|
|
|
|
|
2022-01-29 00:22:28 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<OpenFileDescription>> open_readable_file_description(auto& fds, int fd)
|
2021-10-12 12:59:49 +00:00
|
|
|
{
|
2022-01-29 00:29:07 +00:00
|
|
|
auto description = TRY(fds.with_shared([&](auto& fds) { return fds.open_file_description(fd); }));
|
2021-10-12 12:59:49 +00:00
|
|
|
if (!description->is_readable())
|
|
|
|
return EBADF;
|
|
|
|
if (description->is_directory())
|
|
|
|
return EISDIR;
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
static ErrorOr<void> check_blocked_read(OpenFileDescription* description)
|
2021-10-12 12:59:49 +00:00
|
|
|
{
|
|
|
|
if (description->is_blocking()) {
|
|
|
|
if (!description->can_read()) {
|
|
|
|
auto unblock_flags = BlockFlags::None;
|
|
|
|
if (Thread::current()->block<Thread::ReadBlocker>({}, *description, unblock_flags).was_interrupted())
|
|
|
|
return EINTR;
|
|
|
|
if (!has_flag(unblock_flags, BlockFlags::Read))
|
|
|
|
return EAGAIN;
|
|
|
|
// TODO: handle exceptions in unblock_flags
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-10-12 12:59:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$readv(int fd, Userspace<const struct iovec*> iov, int iov_count)
|
2021-02-12 02:10:03 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2021-02-12 02:10:03 +00:00
|
|
|
if (iov_count < 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2021-02-12 02:10:03 +00:00
|
|
|
|
2021-02-15 20:06:18 +00:00
|
|
|
// Arbitrary pain threshold.
|
|
|
|
if (iov_count > (int)MiB)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2021-02-12 02:10:03 +00:00
|
|
|
|
|
|
|
u64 total_length = 0;
|
|
|
|
Vector<iovec, 32> vecs;
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(vecs.try_resize(iov_count));
|
2021-09-05 15:38:37 +00:00
|
|
|
TRY(copy_n_from_user(vecs.data(), iov, iov_count));
|
2021-02-12 02:10:03 +00:00
|
|
|
for (auto& vec : vecs) {
|
|
|
|
total_length += vec.iov_len;
|
|
|
|
if (total_length > NumericLimits<i32>::max())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2021-02-12 02:10:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 12:59:49 +00:00
|
|
|
auto description = TRY(open_readable_file_description(fds(), fd));
|
2021-02-12 02:10:03 +00:00
|
|
|
|
|
|
|
int nread = 0;
|
|
|
|
for (auto& vec : vecs) {
|
2021-10-12 12:59:49 +00:00
|
|
|
TRY(check_blocked_read(description));
|
2021-11-21 11:24:32 +00:00
|
|
|
auto buffer = TRY(UserOrKernelBuffer::for_user_buffer((u8*)vec.iov_base, vec.iov_len));
|
|
|
|
auto nread_here = TRY(description->read(buffer, vec.iov_len));
|
2021-09-05 14:25:09 +00:00
|
|
|
nread += nread_here;
|
2021-02-12 02:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$read(int fd, Userspace<u8*> buffer, size_t size)
|
2021-12-04 12:26:13 +00:00
|
|
|
{
|
|
|
|
const auto start_timestamp = TimeManagement::the().uptime_ms();
|
|
|
|
const auto result = read_impl(fd, buffer, size);
|
|
|
|
|
|
|
|
if (Thread::current()->is_profiling_suppressed())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
auto description = TRY(open_readable_file_description(fds(), fd));
|
|
|
|
PerformanceManager::add_read_event(*Thread::current(), fd, size, description, start_timestamp, result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<FlatPtr> Process::read_impl(int fd, Userspace<u8*> buffer, size_t size)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2020-07-30 21:38:15 +00:00
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
2021-06-16 14:44:15 +00:00
|
|
|
if (size > NumericLimits<ssize_t>::max())
|
|
|
|
return EINVAL;
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(IO_DEBUG, "sys$read({}, {}, {})", fd, buffer.ptr(), size);
|
2021-10-12 12:59:49 +00:00
|
|
|
auto description = TRY(open_readable_file_description(fds(), fd));
|
2021-12-04 12:26:13 +00:00
|
|
|
|
2021-10-12 12:59:49 +00:00
|
|
|
TRY(check_blocked_read(description));
|
2021-11-21 11:24:32 +00:00
|
|
|
auto user_buffer = TRY(UserOrKernelBuffer::for_user_buffer(buffer, size));
|
|
|
|
return TRY(description->read(user_buffer, size));
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 11:33:22 +00:00
|
|
|
// NOTE: The offset is passed by pointer because off_t is 64bit,
|
|
|
|
// hence it can't be passed by register on 32bit platforms.
|
2021-12-17 07:34:27 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$pread(int fd, Userspace<u8*> buffer, size_t size, Userspace<off_t const*> userspace_offset)
|
2021-10-12 13:13:28 +00:00
|
|
|
{
|
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2021-10-12 13:13:28 +00:00
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
if (size > NumericLimits<ssize_t>::max())
|
|
|
|
return EINVAL;
|
2021-12-17 07:34:27 +00:00
|
|
|
auto offset = TRY(copy_typed_from_user(userspace_offset));
|
2021-10-12 13:13:28 +00:00
|
|
|
if (offset < 0)
|
|
|
|
return EINVAL;
|
|
|
|
dbgln_if(IO_DEBUG, "sys$pread({}, {}, {}, {})", fd, buffer.ptr(), size, offset);
|
|
|
|
auto description = TRY(open_readable_file_description(fds(), fd));
|
|
|
|
if (!description->file().is_seekable())
|
|
|
|
return EINVAL;
|
|
|
|
TRY(check_blocked_read(description));
|
2021-11-21 11:24:32 +00:00
|
|
|
auto user_buffer = TRY(UserOrKernelBuffer::for_user_buffer(buffer, size));
|
|
|
|
return TRY(description->read(user_buffer, offset, size));
|
2021-10-12 13:13:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|