FIFO.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/FileSystem/FIFO.h>
  31. #include <Kernel/FileSystem/FileDescription.h>
  32. #include <Kernel/Lock.h>
  33. #include <Kernel/Process.h>
  34. #include <Kernel/Thread.h>
  35. //#define FIFO_DEBUG
  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. NonnullRefPtr<FileDescription> FIFO::open_direction(FIFO::Direction direction)
  48. {
  49. auto description = FileDescription::create(*this);
  50. attach(direction);
  51. description->set_fifo_direction({}, direction);
  52. return description;
  53. }
  54. NonnullRefPtr<FileDescription> FIFO::open_direction_blocking(FIFO::Direction direction)
  55. {
  56. Locker locker(m_open_lock);
  57. auto description = open_direction(direction);
  58. if (direction == Direction::Reader) {
  59. m_read_open_queue.wake_all();
  60. if (m_writers == 0) {
  61. locker.unlock();
  62. Thread::current()->wait_on(m_write_open_queue, "FIFO");
  63. locker.lock();
  64. }
  65. }
  66. if (direction == Direction::Writer) {
  67. m_write_open_queue.wake_all();
  68. if (m_readers == 0) {
  69. locker.unlock();
  70. Thread::current()->wait_on(m_read_open_queue, "FIFO");
  71. locker.lock();
  72. }
  73. }
  74. return description;
  75. }
  76. FIFO::FIFO(uid_t uid)
  77. : m_uid(uid)
  78. {
  79. LOCKER(all_fifos().lock());
  80. all_fifos().resource().set(this);
  81. m_fifo_id = ++s_next_fifo_id;
  82. }
  83. FIFO::~FIFO()
  84. {
  85. LOCKER(all_fifos().lock());
  86. all_fifos().resource().remove(this);
  87. }
  88. void FIFO::attach(Direction direction)
  89. {
  90. if (direction == Direction::Reader) {
  91. ++m_readers;
  92. #ifdef FIFO_DEBUG
  93. klog() << "open reader (" << m_readers << ")";
  94. #endif
  95. } else if (direction == Direction::Writer) {
  96. ++m_writers;
  97. #ifdef FIFO_DEBUG
  98. klog() << "open writer (" << m_writers << ")";
  99. #endif
  100. }
  101. }
  102. void FIFO::detach(Direction direction)
  103. {
  104. if (direction == Direction::Reader) {
  105. #ifdef FIFO_DEBUG
  106. klog() << "close reader (" << m_readers << " - 1)";
  107. #endif
  108. ASSERT(m_readers);
  109. --m_readers;
  110. } else if (direction == Direction::Writer) {
  111. #ifdef FIFO_DEBUG
  112. klog() << "close writer (" << m_writers << " - 1)";
  113. #endif
  114. ASSERT(m_writers);
  115. --m_writers;
  116. }
  117. }
  118. bool FIFO::can_read(const FileDescription&, size_t) const
  119. {
  120. return !m_buffer.is_empty() || !m_writers;
  121. }
  122. bool FIFO::can_write(const FileDescription&, size_t) const
  123. {
  124. return m_buffer.space_for_writing() || !m_readers;
  125. }
  126. KResultOr<size_t> FIFO::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
  127. {
  128. if (!m_writers && m_buffer.is_empty())
  129. return 0;
  130. return m_buffer.read(buffer, size);
  131. }
  132. KResultOr<size_t> FIFO::write(FileDescription&, size_t, const UserOrKernelBuffer& buffer, size_t size)
  133. {
  134. if (!m_readers) {
  135. Thread::current()->send_signal(SIGPIPE, Process::current());
  136. return -EPIPE;
  137. }
  138. return m_buffer.write(buffer, size);
  139. }
  140. String FIFO::absolute_path(const FileDescription&) const
  141. {
  142. return String::format("fifo:%u", m_fifo_id);
  143. }
  144. KResult FIFO::stat(::stat& st) const
  145. {
  146. memset(&st, 0, sizeof(st));
  147. st.st_mode = S_IFIFO;
  148. return KSuccess;
  149. }
  150. }