2020-07-31 00:38:15 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 00:38:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/NumericLimits.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2023-02-24 20:45:37 +03:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2023-08-11 11:47:31 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$pwritev(int fd, Userspace<const struct iovec*> iov, int iov_count, off_t base_offset)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-17 23:03:04 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2020-07-31 00:38:15 +03:00
|
|
|
if (iov_count < 0)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2022-05-02 21:08:17 +03:00
|
|
|
if (iov_count > IOV_MAX)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
u64 total_length = 0;
|
|
|
|
Vector<iovec, 32> vecs;
|
2021-11-10 13:55:37 +03:00
|
|
|
TRY(vecs.try_resize(iov_count));
|
2021-09-05 18:38:37 +03:00
|
|
|
TRY(copy_n_from_user(vecs.data(), iov, iov_count));
|
2020-07-31 00:38:15 +03:00
|
|
|
for (auto& vec : vecs) {
|
|
|
|
total_length += vec.iov_len;
|
|
|
|
if (total_length > NumericLimits<i32>::max())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2022-01-29 03:22:28 +03:00
|
|
|
auto description = TRY(open_file_description(fd));
|
2020-07-31 00:38:15 +03:00
|
|
|
if (!description->is_writable())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EBADF;
|
2023-08-11 11:47:31 +03:00
|
|
|
// NOTE: Negative offset means "operate like writev" which seeks the file.
|
2022-10-01 16:21:20 +03:00
|
|
|
if (base_offset >= 0 && !description->file().is_seekable())
|
|
|
|
return EINVAL;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
int nwritten = 0;
|
2022-10-01 16:21:20 +03:00
|
|
|
off_t current_offset = base_offset;
|
2020-07-31 00:38:15 +03:00
|
|
|
for (auto& vec : vecs) {
|
2021-11-21 14:24:32 +03:00
|
|
|
auto buffer = TRY(UserOrKernelBuffer::for_user_buffer((u8*)vec.iov_base, vec.iov_len));
|
2022-10-01 16:21:20 +03:00
|
|
|
auto result = do_write(*description, buffer, vec.iov_len, base_offset >= 0 ? current_offset : Optional<off_t> {});
|
2021-03-01 15:49:16 +03:00
|
|
|
if (result.is_error()) {
|
2020-07-31 00:38:15 +03:00
|
|
|
if (nwritten == 0)
|
2021-11-08 02:51:39 +03:00
|
|
|
return result.release_error();
|
2020-07-31 00:38:15 +03:00
|
|
|
return nwritten;
|
|
|
|
}
|
2021-03-01 15:49:16 +03:00
|
|
|
nwritten += result.value();
|
2022-10-01 16:21:20 +03:00
|
|
|
current_offset += result.value();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
2022-10-01 16:21:20 +03:00
|
|
|
ErrorOr<FlatPtr> Process::do_write(OpenFileDescription& description, UserOrKernelBuffer const& data, size_t data_size, Optional<off_t> offset)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-06-16 17:44:15 +03:00
|
|
|
size_t total_nwritten = 0;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-03-29 15:27:38 +03:00
|
|
|
if (description.should_append() && description.file().is_seekable()) {
|
2021-09-05 19:10:27 +03:00
|
|
|
TRY(description.seek(0, SEEK_END));
|
2021-03-19 12:43:58 +03:00
|
|
|
}
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-06-17 12:15:55 +03:00
|
|
|
while (total_nwritten < data_size) {
|
2021-05-18 14:49:34 +03:00
|
|
|
while (!description.can_write()) {
|
2020-07-31 00:38:15 +03:00
|
|
|
if (!description.is_blocking()) {
|
2021-05-18 14:49:34 +03:00
|
|
|
if (total_nwritten > 0)
|
|
|
|
return total_nwritten;
|
2021-12-18 20:34:24 +03:00
|
|
|
return EAGAIN;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
|
2021-01-11 02:29:28 +03:00
|
|
|
if (Thread::current()->block<Thread::WriteBlocker>({}, description, unblock_flags).was_interrupted()) {
|
2020-08-04 19:02:23 +03:00
|
|
|
if (total_nwritten == 0)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINTR;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
// TODO: handle exceptions in unblock_flags
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2022-10-01 16:21:20 +03:00
|
|
|
auto nwritten_or_error = offset.has_value()
|
|
|
|
? description.write(offset.value() + total_nwritten, data.offset(total_nwritten), data_size - total_nwritten)
|
|
|
|
: description.write(data.offset(total_nwritten), data_size - total_nwritten);
|
2020-08-04 19:02:23 +03:00
|
|
|
if (nwritten_or_error.is_error()) {
|
2021-05-18 14:49:34 +03:00
|
|
|
if (total_nwritten > 0)
|
2020-08-04 19:02:23 +03:00
|
|
|
return total_nwritten;
|
2021-11-08 02:51:39 +03:00
|
|
|
if (nwritten_or_error.error().code() == EAGAIN)
|
2021-05-18 14:49:34 +03:00
|
|
|
continue;
|
2022-10-23 11:30:12 +03:00
|
|
|
if (nwritten_or_error.error().code() == EPIPE)
|
|
|
|
Thread::current()->send_signal(SIGPIPE, &Process::current());
|
2021-11-08 02:51:39 +03:00
|
|
|
return nwritten_or_error.release_error();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2021-05-18 14:49:34 +03:00
|
|
|
VERIFY(nwritten_or_error.value() > 0);
|
2020-08-04 19:02:23 +03:00
|
|
|
total_nwritten += nwritten_or_error.value();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2020-08-04 19:02:23 +03:00
|
|
|
return total_nwritten;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$write(int fd, Userspace<u8 const*> data, size_t size)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-17 23:03:04 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2020-07-31 00:38:15 +03:00
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
2021-06-16 17:44:15 +03:00
|
|
|
if (size > NumericLimits<ssize_t>::max())
|
|
|
|
return EINVAL;
|
2020-09-12 06:11:07 +03:00
|
|
|
|
2021-03-01 18:07:50 +03:00
|
|
|
dbgln_if(IO_DEBUG, "sys$write({}, {}, {})", fd, data.ptr(), size);
|
2022-01-29 03:22:28 +03:00
|
|
|
auto description = TRY(open_file_description(fd));
|
2020-07-31 00:38:15 +03:00
|
|
|
if (!description->is_writable())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EBADF;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-11-21 14:24:32 +03:00
|
|
|
auto buffer = TRY(UserOrKernelBuffer::for_user_buffer(data, static_cast<size_t>(size)));
|
|
|
|
return do_write(*description, buffer, size);
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|