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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/NumericLimits.h>
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-01 12:49:16 +00:00
|
|
|
KResultOr<ssize_t> Process::sys$writev(int fd, Userspace<const struct iovec*> iov, int iov_count)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
if (iov_count < 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-02-11 19:38:39 +00:00
|
|
|
// Arbitrary pain threshold.
|
|
|
|
if (iov_count > (int)MiB)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
u64 total_length = 0;
|
|
|
|
Vector<iovec, 32> vecs;
|
2021-04-29 08:20:24 +00:00
|
|
|
if (!vecs.try_resize(iov_count))
|
|
|
|
return ENOMEM;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_n_from_user(vecs.data(), iov, iov_count))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +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;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto description = file_description(fd);
|
|
|
|
if (!description)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EBADF;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
if (!description->is_writable())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EBADF;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
int nwritten = 0;
|
|
|
|
for (auto& vec : vecs) {
|
2020-09-12 03:11:07 +00:00
|
|
|
auto buffer = UserOrKernelBuffer::for_user_buffer((u8*)vec.iov_base, vec.iov_len);
|
|
|
|
if (!buffer.has_value())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
|
|
|
auto result = do_write(*description, buffer.value(), vec.iov_len);
|
|
|
|
if (result.is_error()) {
|
2020-07-30 21:38:15 +00:00
|
|
|
if (nwritten == 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return result.error();
|
2020-07-30 21:38:15 +00:00
|
|
|
return nwritten;
|
|
|
|
}
|
2021-03-01 12:49:16 +00:00
|
|
|
nwritten += result.value();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
2021-03-01 12:49:16 +00:00
|
|
|
KResultOr<ssize_t> Process::do_write(FileDescription& description, const UserOrKernelBuffer& data, size_t data_size)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2020-08-04 16:02:23 +00:00
|
|
|
ssize_t total_nwritten = 0;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-03-29 12:27:38 +00:00
|
|
|
if (description.should_append() && description.file().is_seekable()) {
|
2021-03-19 09:43:58 +00:00
|
|
|
auto seek_result = description.seek(0, SEEK_END);
|
|
|
|
if (seek_result.is_error())
|
|
|
|
return seek_result.error();
|
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
while ((size_t)total_nwritten < data_size) {
|
2021-05-18 11:49:34 +00:00
|
|
|
while (!description.can_write()) {
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!description.is_blocking()) {
|
2021-05-18 11:49:34 +00:00
|
|
|
if (total_nwritten > 0)
|
|
|
|
return total_nwritten;
|
|
|
|
else
|
|
|
|
return EAGAIN;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
|
2021-01-10 23:29:28 +00:00
|
|
|
if (Thread::current()->block<Thread::WriteBlocker>({}, description, unblock_flags).was_interrupted()) {
|
2020-08-04 16:02:23 +00:00
|
|
|
if (total_nwritten == 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINTR;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
// TODO: handle exceptions in unblock_flags
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-09-12 03:11:07 +00:00
|
|
|
auto nwritten_or_error = description.write(data.offset(total_nwritten), data_size - total_nwritten);
|
2020-08-04 16:02:23 +00:00
|
|
|
if (nwritten_or_error.is_error()) {
|
2021-05-18 11:49:34 +00:00
|
|
|
if (total_nwritten > 0)
|
2020-08-04 16:02:23 +00:00
|
|
|
return total_nwritten;
|
2021-05-18 11:49:34 +00:00
|
|
|
if (nwritten_or_error.error() == EAGAIN)
|
|
|
|
continue;
|
2020-08-04 16:02:23 +00:00
|
|
|
return nwritten_or_error.error();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2021-05-18 11:49:34 +00:00
|
|
|
VERIFY(nwritten_or_error.value() > 0);
|
2020-08-04 16:02:23 +00:00
|
|
|
total_nwritten += nwritten_or_error.value();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-08-04 16:02:23 +00:00
|
|
|
return total_nwritten;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 13:35:06 +00:00
|
|
|
KResultOr<ssize_t> Process::sys$write(int fd, Userspace<const u8*> data, ssize_t size)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
if (size < 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
2020-09-12 03:11:07 +00:00
|
|
|
|
2021-03-01 15:07:50 +00:00
|
|
|
dbgln_if(IO_DEBUG, "sys$write({}, {}, {})", fd, data.ptr(), size);
|
2020-07-30 21:38:15 +00:00
|
|
|
auto description = file_description(fd);
|
|
|
|
if (!description)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EBADF;
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!description->is_writable())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EBADF;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-03-01 13:35:06 +00:00
|
|
|
auto buffer = UserOrKernelBuffer::for_user_buffer(data, static_cast<size_t>(size));
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!buffer.has_value())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-09-12 03:11:07 +00:00
|
|
|
return do_write(*description, buffer.value(), size);
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|