FIFO.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/HashTable.h>
  27. #include <AK/Singleton.h>
  28. #include <AK/StdLibExtras.h>
  29. #include <AK/StringView.h>
  30. #include <Kernel/Debug.h>
  31. #include <Kernel/FileSystem/FIFO.h>
  32. #include <Kernel/FileSystem/FileDescription.h>
  33. #include <Kernel/Lock.h>
  34. #include <Kernel/Process.h>
  35. #include <Kernel/Thread.h>
  36. namespace Kernel {
  37. static AK::Singleton<Lockable<HashTable<FIFO*>>> s_table;
  38. static Lockable<HashTable<FIFO*>>& all_fifos()
  39. {
  40. return *s_table;
  41. }
  42. static int s_next_fifo_id = 1;
  43. NonnullRefPtr<FIFO> FIFO::create(uid_t uid)
  44. {
  45. return adopt(*new FIFO(uid));
  46. }
  47. KResultOr<NonnullRefPtr<FileDescription>> FIFO::open_direction(FIFO::Direction direction)
  48. {
  49. auto description = FileDescription::create(*this);
  50. if (!description.is_error()) {
  51. attach(direction);
  52. description.value()->set_fifo_direction({}, direction);
  53. }
  54. return description;
  55. }
  56. KResultOr<NonnullRefPtr<FileDescription>> FIFO::open_direction_blocking(FIFO::Direction direction)
  57. {
  58. Locker locker(m_open_lock);
  59. auto description = open_direction(direction);
  60. if (description.is_error())
  61. return description;
  62. if (direction == Direction::Reader) {
  63. m_read_open_queue.wake_all();
  64. if (m_writers == 0) {
  65. locker.unlock();
  66. m_write_open_queue.wait_on({}, "FIFO");
  67. locker.lock();
  68. }
  69. }
  70. if (direction == Direction::Writer) {
  71. m_write_open_queue.wake_all();
  72. if (m_readers == 0) {
  73. locker.unlock();
  74. m_read_open_queue.wait_on({}, "FIFO");
  75. locker.lock();
  76. }
  77. }
  78. return description;
  79. }
  80. FIFO::FIFO(uid_t uid)
  81. : m_uid(uid)
  82. {
  83. LOCKER(all_fifos().lock());
  84. all_fifos().resource().set(this);
  85. m_fifo_id = ++s_next_fifo_id;
  86. // Use the same block condition for read and write
  87. m_buffer.set_unblock_callback([this]() {
  88. evaluate_block_conditions();
  89. });
  90. }
  91. FIFO::~FIFO()
  92. {
  93. LOCKER(all_fifos().lock());
  94. all_fifos().resource().remove(this);
  95. }
  96. void FIFO::attach(Direction direction)
  97. {
  98. if (direction == Direction::Reader) {
  99. ++m_readers;
  100. #if FIFO_DEBUG
  101. klog() << "open reader (" << m_readers << ")";
  102. #endif
  103. } else if (direction == Direction::Writer) {
  104. ++m_writers;
  105. #if FIFO_DEBUG
  106. klog() << "open writer (" << m_writers << ")";
  107. #endif
  108. }
  109. evaluate_block_conditions();
  110. }
  111. void FIFO::detach(Direction direction)
  112. {
  113. if (direction == Direction::Reader) {
  114. #if FIFO_DEBUG
  115. klog() << "close reader (" << m_readers << " - 1)";
  116. #endif
  117. ASSERT(m_readers);
  118. --m_readers;
  119. } else if (direction == Direction::Writer) {
  120. #if FIFO_DEBUG
  121. klog() << "close writer (" << m_writers << " - 1)";
  122. #endif
  123. ASSERT(m_writers);
  124. --m_writers;
  125. }
  126. evaluate_block_conditions();
  127. }
  128. bool FIFO::can_read(const FileDescription&, size_t) const
  129. {
  130. return !m_buffer.is_empty() || !m_writers;
  131. }
  132. bool FIFO::can_write(const FileDescription&, size_t) const
  133. {
  134. return m_buffer.space_for_writing() || !m_readers;
  135. }
  136. KResultOr<size_t> FIFO::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
  137. {
  138. if (!m_writers && m_buffer.is_empty())
  139. return 0;
  140. return m_buffer.read(buffer, size);
  141. }
  142. KResultOr<size_t> FIFO::write(FileDescription&, size_t, const UserOrKernelBuffer& buffer, size_t size)
  143. {
  144. if (!m_readers) {
  145. Thread::current()->send_signal(SIGPIPE, Process::current());
  146. return -EPIPE;
  147. }
  148. return m_buffer.write(buffer, size);
  149. }
  150. String FIFO::absolute_path(const FileDescription&) const
  151. {
  152. return String::format("fifo:%u", m_fifo_id);
  153. }
  154. KResult FIFO::stat(::stat& st) const
  155. {
  156. memset(&st, 0, sizeof(st));
  157. st.st_mode = S_IFIFO;
  158. return KSuccess;
  159. }
  160. }