2020-07-30 23:38:15 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/NumericLimits.h>
|
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-01 13:49:16 +01:00
|
|
|
KResultOr<ssize_t> Process::sys$writev(int fd, Userspace<const struct iovec*> iov, int iov_count)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
if (iov_count < 0)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-02-11 20:38:39 +01:00
|
|
|
// Arbitrary pain threshold.
|
|
|
|
if (iov_count > (int)MiB)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EFAULT;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
u64 total_length = 0;
|
|
|
|
Vector<iovec, 32> vecs;
|
|
|
|
vecs.resize(iov_count);
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!copy_n_from_user(vecs.data(), iov, iov_count))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EFAULT;
|
2020-07-30 23:38:15 +02:00
|
|
|
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;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto description = file_description(fd);
|
|
|
|
if (!description)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
if (!description->is_writable())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
int nwritten = 0;
|
|
|
|
for (auto& vec : vecs) {
|
2020-09-11 21:11:07 -06:00
|
|
|
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;
|
|
|
|
auto result = do_write(*description, buffer.value(), vec.iov_len);
|
|
|
|
if (result.is_error()) {
|
2020-07-30 23:38:15 +02:00
|
|
|
if (nwritten == 0)
|
2021-03-01 13:49:16 +01:00
|
|
|
return result.error();
|
2020-07-30 23:38:15 +02:00
|
|
|
return nwritten;
|
|
|
|
}
|
2021-03-01 13:49:16 +01:00
|
|
|
nwritten += result.value();
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
2021-03-01 13:49:16 +01:00
|
|
|
KResultOr<ssize_t> Process::do_write(FileDescription& description, const UserOrKernelBuffer& data, size_t data_size)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2020-08-04 18:02:23 +02:00
|
|
|
ssize_t total_nwritten = 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!description.is_blocking()) {
|
|
|
|
if (!description.can_write())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EAGAIN;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:27:38 +03:00
|
|
|
if (description.should_append() && description.file().is_seekable()) {
|
2021-03-19 10:43:58 +01:00
|
|
|
auto seek_result = description.seek(0, SEEK_END);
|
|
|
|
if (seek_result.is_error())
|
|
|
|
return seek_result.error();
|
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2020-09-11 21:11:07 -06:00
|
|
|
while ((size_t)total_nwritten < data_size) {
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!description.can_write()) {
|
|
|
|
if (!description.is_blocking()) {
|
|
|
|
// Short write: We can no longer write to this non-blocking description.
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(total_nwritten > 0);
|
2020-08-04 18:02:23 +02:00
|
|
|
return total_nwritten;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
2020-11-29 16:05:27 -07:00
|
|
|
auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
|
2021-01-10 16:29:28 -07:00
|
|
|
if (Thread::current()->block<Thread::WriteBlocker>({}, description, unblock_flags).was_interrupted()) {
|
2020-08-04 18:02:23 +02:00
|
|
|
if (total_nwritten == 0)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINTR;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
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 nwritten_or_error = description.write(data.offset(total_nwritten), data_size - total_nwritten);
|
2020-08-04 18:02:23 +02:00
|
|
|
if (nwritten_or_error.is_error()) {
|
|
|
|
if (total_nwritten)
|
|
|
|
return total_nwritten;
|
|
|
|
return nwritten_or_error.error();
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
2020-08-04 18:02:23 +02:00
|
|
|
if (nwritten_or_error.value() == 0)
|
2020-07-30 23:38:15 +02:00
|
|
|
break;
|
2020-08-04 18:02:23 +02:00
|
|
|
total_nwritten += nwritten_or_error.value();
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
2020-08-04 18:02:23 +02:00
|
|
|
return total_nwritten;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 14:35:06 +01:00
|
|
|
KResultOr<ssize_t> Process::sys$write(int fd, Userspace<const u8*> data, ssize_t size)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
if (size < 0)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
2020-09-11 21:11:07 -06:00
|
|
|
|
2021-03-01 16:07:50 +01:00
|
|
|
dbgln_if(IO_DEBUG, "sys$write({}, {}, {})", fd, data.ptr(), size);
|
2020-07-30 23:38:15 +02:00
|
|
|
auto description = file_description(fd);
|
|
|
|
if (!description)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!description->is_writable())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-03-01 14:35:06 +01:00
|
|
|
auto buffer = UserOrKernelBuffer::for_user_buffer(data, static_cast<size_t>(size));
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!buffer.has_value())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EFAULT;
|
2020-09-11 21:11:07 -06:00
|
|
|
return do_write(*description, buffer.value(), size);
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|