FIFO.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashTable.h>
  7. #include <AK/Singleton.h>
  8. #include <AK/StdLibExtras.h>
  9. #include <AK/StringView.h>
  10. #include <Kernel/FileSystem/FIFO.h>
  11. #include <Kernel/FileSystem/FileDescription.h>
  12. #include <Kernel/Lock.h>
  13. #include <Kernel/Process.h>
  14. #include <Kernel/Thread.h>
  15. namespace Kernel {
  16. static AK::Singleton<Lockable<HashTable<FIFO*>>> s_table;
  17. static Lockable<HashTable<FIFO*>>& all_fifos()
  18. {
  19. return *s_table;
  20. }
  21. static int s_next_fifo_id = 1;
  22. NonnullRefPtr<FIFO> FIFO::create(uid_t uid)
  23. {
  24. return adopt_ref(*new FIFO(uid));
  25. }
  26. KResultOr<NonnullRefPtr<FileDescription>> FIFO::open_direction(FIFO::Direction direction)
  27. {
  28. auto description = FileDescription::create(*this);
  29. if (!description.is_error()) {
  30. attach(direction);
  31. description.value()->set_fifo_direction({}, direction);
  32. }
  33. return description;
  34. }
  35. KResultOr<NonnullRefPtr<FileDescription>> FIFO::open_direction_blocking(FIFO::Direction direction)
  36. {
  37. Locker locker(m_open_lock);
  38. auto description = open_direction(direction);
  39. if (description.is_error())
  40. return description;
  41. if (direction == Direction::Reader) {
  42. m_read_open_queue.wake_all();
  43. if (m_writers == 0) {
  44. locker.unlock();
  45. m_write_open_queue.wait_forever("FIFO");
  46. locker.lock();
  47. }
  48. }
  49. if (direction == Direction::Writer) {
  50. m_write_open_queue.wake_all();
  51. if (m_readers == 0) {
  52. locker.unlock();
  53. m_read_open_queue.wait_forever("FIFO");
  54. locker.lock();
  55. }
  56. }
  57. return description;
  58. }
  59. FIFO::FIFO(uid_t uid)
  60. : m_uid(uid)
  61. {
  62. LOCKER(all_fifos().lock());
  63. all_fifos().resource().set(this);
  64. m_fifo_id = ++s_next_fifo_id;
  65. // Use the same block condition for read and write
  66. m_buffer.set_unblock_callback([this]() {
  67. evaluate_block_conditions();
  68. });
  69. }
  70. FIFO::~FIFO()
  71. {
  72. LOCKER(all_fifos().lock());
  73. all_fifos().resource().remove(this);
  74. }
  75. void FIFO::attach(Direction direction)
  76. {
  77. if (direction == Direction::Reader) {
  78. ++m_readers;
  79. } else if (direction == Direction::Writer) {
  80. ++m_writers;
  81. }
  82. evaluate_block_conditions();
  83. }
  84. void FIFO::detach(Direction direction)
  85. {
  86. if (direction == Direction::Reader) {
  87. VERIFY(m_readers);
  88. --m_readers;
  89. } else if (direction == Direction::Writer) {
  90. VERIFY(m_writers);
  91. --m_writers;
  92. }
  93. evaluate_block_conditions();
  94. }
  95. bool FIFO::can_read(const FileDescription&, size_t) const
  96. {
  97. return !m_buffer.is_empty() || !m_writers;
  98. }
  99. bool FIFO::can_write(const FileDescription&, size_t) const
  100. {
  101. return m_buffer.space_for_writing() || !m_readers;
  102. }
  103. KResultOr<size_t> FIFO::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
  104. {
  105. if (!m_writers && m_buffer.is_empty())
  106. return 0;
  107. return m_buffer.read(buffer, size);
  108. }
  109. KResultOr<size_t> FIFO::write(FileDescription&, u64, const UserOrKernelBuffer& buffer, size_t size)
  110. {
  111. if (!m_readers) {
  112. Thread::current()->send_signal(SIGPIPE, Process::current());
  113. return EPIPE;
  114. }
  115. return m_buffer.write(buffer, size);
  116. }
  117. String FIFO::absolute_path(const FileDescription&) const
  118. {
  119. return String::formatted("fifo:{}", m_fifo_id);
  120. }
  121. KResult FIFO::stat(::stat& st) const
  122. {
  123. memset(&st, 0, sizeof(st));
  124. st.st_mode = S_IFIFO;
  125. return KSuccess;
  126. }
  127. }