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>
|
2021-09-07 11:39:11 +00:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$writev(int fd, Userspace<const struct iovec*> iov, int iov_count)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2022-08-17 20:03:04 +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 (iov_count < 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2022-05-02 18:08:17 +00:00
|
|
|
if (iov_count > IOV_MAX)
|
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-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));
|
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
|
|
|
}
|
|
|
|
|
2022-01-29 00:22:28 +00:00
|
|
|
auto description = TRY(open_file_description(fd));
|
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) {
|
2021-11-21 11:24:32 +00:00
|
|
|
auto buffer = TRY(UserOrKernelBuffer::for_user_buffer((u8*)vec.iov_base, vec.iov_len));
|
|
|
|
auto result = do_write(*description, buffer, vec.iov_len);
|
2021-03-01 12:49:16 +00:00
|
|
|
if (result.is_error()) {
|
2020-07-30 21:38:15 +00:00
|
|
|
if (nwritten == 0)
|
2021-11-07 23:51:39 +00:00
|
|
|
return result.release_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;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<FlatPtr> Process::do_write(OpenFileDescription& description, UserOrKernelBuffer const& data, size_t data_size)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-06-16 14:44:15 +00:00
|
|
|
size_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-09-05 16:10:27 +00:00
|
|
|
TRY(description.seek(0, SEEK_END));
|
2021-03-19 09:43:58 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-06-17 09:15:55 +00:00
|
|
|
while (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;
|
2021-12-18 17:34:24 +00:00
|
|
|
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-11-07 23:51:39 +00:00
|
|
|
if (nwritten_or_error.error().code() == EAGAIN)
|
2021-05-18 11:49:34 +00:00
|
|
|
continue;
|
2021-11-07 23:51:39 +00:00
|
|
|
return nwritten_or_error.release_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
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$write(int fd, Userspace<u8 const*> data, size_t size)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2022-08-17 20:03:04 +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;
|
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);
|
2022-01-29 00:22:28 +00:00
|
|
|
auto description = TRY(open_file_description(fd));
|
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-11-21 11:24:32 +00:00
|
|
|
auto buffer = TRY(UserOrKernelBuffer::for_user_buffer(data, static_cast<size_t>(size)));
|
|
|
|
return do_write(*description, buffer, size);
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|