write.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/NumericLimits.h>
  27. #include <Kernel/Debug.h>
  28. #include <Kernel/FileSystem/FileDescription.h>
  29. #include <Kernel/Process.h>
  30. namespace Kernel {
  31. ssize_t Process::sys$writev(int fd, Userspace<const struct iovec*> iov, int iov_count)
  32. {
  33. REQUIRE_PROMISE(stdio);
  34. if (iov_count < 0)
  35. return -EINVAL;
  36. {
  37. Checked checked_iov_count = sizeof(iovec);
  38. checked_iov_count *= iov_count;
  39. if (checked_iov_count.has_overflow())
  40. return -EFAULT;
  41. }
  42. u64 total_length = 0;
  43. Vector<iovec, 32> vecs;
  44. vecs.resize(iov_count);
  45. if (!copy_n_from_user(vecs.data(), iov, iov_count))
  46. return -EFAULT;
  47. for (auto& vec : vecs) {
  48. total_length += vec.iov_len;
  49. if (total_length > NumericLimits<i32>::max())
  50. return -EINVAL;
  51. }
  52. auto description = file_description(fd);
  53. if (!description)
  54. return -EBADF;
  55. if (!description->is_writable())
  56. return -EBADF;
  57. int nwritten = 0;
  58. for (auto& vec : vecs) {
  59. auto buffer = UserOrKernelBuffer::for_user_buffer((u8*)vec.iov_base, vec.iov_len);
  60. if (!buffer.has_value())
  61. return -EFAULT;
  62. int rc = do_write(*description, buffer.value(), vec.iov_len);
  63. if (rc < 0) {
  64. if (nwritten == 0)
  65. return rc;
  66. return nwritten;
  67. }
  68. nwritten += rc;
  69. }
  70. return nwritten;
  71. }
  72. ssize_t Process::do_write(FileDescription& description, const UserOrKernelBuffer& data, size_t data_size)
  73. {
  74. ssize_t total_nwritten = 0;
  75. if (!description.is_blocking()) {
  76. if (!description.can_write())
  77. return -EAGAIN;
  78. }
  79. if (description.should_append())
  80. description.seek(0, SEEK_END);
  81. while ((size_t)total_nwritten < data_size) {
  82. if (!description.can_write()) {
  83. if (!description.is_blocking()) {
  84. // Short write: We can no longer write to this non-blocking description.
  85. ASSERT(total_nwritten > 0);
  86. return total_nwritten;
  87. }
  88. auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
  89. if (Thread::current()->block<Thread::WriteBlocker>({}, description, unblock_flags).was_interrupted()) {
  90. if (total_nwritten == 0)
  91. return -EINTR;
  92. }
  93. // TODO: handle exceptions in unblock_flags
  94. }
  95. auto nwritten_or_error = description.write(data.offset(total_nwritten), data_size - total_nwritten);
  96. if (nwritten_or_error.is_error()) {
  97. if (total_nwritten)
  98. return total_nwritten;
  99. return nwritten_or_error.error();
  100. }
  101. if (nwritten_or_error.value() == 0)
  102. break;
  103. total_nwritten += nwritten_or_error.value();
  104. }
  105. return total_nwritten;
  106. }
  107. ssize_t Process::sys$write(int fd, const u8* data, ssize_t size)
  108. {
  109. REQUIRE_PROMISE(stdio);
  110. if (size < 0)
  111. return -EINVAL;
  112. if (size == 0)
  113. return 0;
  114. dbgln<IO_DEBUG>("sys$write({}, {}, {})", fd, data, size);
  115. auto description = file_description(fd);
  116. if (!description)
  117. return -EBADF;
  118. if (!description->is_writable())
  119. return -EBADF;
  120. auto buffer = UserOrKernelBuffer::for_user_buffer(const_cast<u8*>(data), (size_t)size);
  121. if (!buffer.has_value())
  122. return -EFAULT;
  123. return do_write(*description, buffer.value(), size);
  124. }
  125. }