FIFO.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/StdLibExtras.h>
  28. #include <Kernel/FileSystem/FIFO.h>
  29. #include <Kernel/FileSystem/FileDescription.h>
  30. #include <Kernel/Lock.h>
  31. #include <Kernel/Process.h>
  32. #include <Kernel/Thread.h>
  33. //#define FIFO_DEBUG
  34. namespace Kernel {
  35. Lockable<HashTable<FIFO*>>& all_fifos()
  36. {
  37. static Lockable<HashTable<FIFO*>>* s_table;
  38. if (!s_table)
  39. s_table = new Lockable<HashTable<FIFO*>>;
  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. FIFO::FIFO(uid_t uid)
  55. : m_uid(uid)
  56. {
  57. LOCKER(all_fifos().lock());
  58. all_fifos().resource().set(this);
  59. m_fifo_id = ++s_next_fifo_id;
  60. }
  61. FIFO::~FIFO()
  62. {
  63. LOCKER(all_fifos().lock());
  64. all_fifos().resource().remove(this);
  65. }
  66. void FIFO::attach(Direction direction)
  67. {
  68. if (direction == Direction::Reader) {
  69. ++m_readers;
  70. #ifdef FIFO_DEBUG
  71. klog() << "open reader (" << m_readers << ")";
  72. #endif
  73. } else if (direction == Direction::Writer) {
  74. ++m_writers;
  75. #ifdef FIFO_DEBUG
  76. klog() << "open writer (" << m_writers << ")";
  77. #endif
  78. }
  79. }
  80. void FIFO::detach(Direction direction)
  81. {
  82. if (direction == Direction::Reader) {
  83. #ifdef FIFO_DEBUG
  84. klog() << "close reader (" << m_readers << " - 1)";
  85. #endif
  86. ASSERT(m_readers);
  87. --m_readers;
  88. } else if (direction == Direction::Writer) {
  89. #ifdef FIFO_DEBUG
  90. klog() << "close writer (" << m_writers << " - 1)";
  91. #endif
  92. ASSERT(m_writers);
  93. --m_writers;
  94. }
  95. }
  96. bool FIFO::can_read(const FileDescription&) const
  97. {
  98. return !m_buffer.is_empty() || !m_writers;
  99. }
  100. bool FIFO::can_write(const FileDescription&) const
  101. {
  102. return m_buffer.space_for_writing() || !m_readers;
  103. }
  104. ssize_t FIFO::read(FileDescription&, u8* buffer, ssize_t size)
  105. {
  106. if (!m_writers && m_buffer.is_empty())
  107. return 0;
  108. #ifdef FIFO_DEBUG
  109. dbg() << "fifo: read(" << size << ")\n";
  110. #endif
  111. ssize_t nread = m_buffer.read(buffer, size);
  112. #ifdef FIFO_DEBUG
  113. dbg() << " -> read (" << String::format("%c", buffer[0]) << ") " << nread;
  114. #endif
  115. return nread;
  116. }
  117. ssize_t FIFO::write(FileDescription&, const u8* buffer, ssize_t size)
  118. {
  119. if (!m_readers) {
  120. Thread::current->send_signal(SIGPIPE, Process::current);
  121. return -EPIPE;
  122. }
  123. #ifdef FIFO_DEBUG
  124. dbg() << "fifo: write(" << String::format("%p", buffer) << ", " << size << ")";
  125. #endif
  126. return m_buffer.write(buffer, size);
  127. }
  128. String FIFO::absolute_path(const FileDescription&) const
  129. {
  130. return String::format("fifo:%u", m_fifo_id);
  131. }
  132. }