FIFO.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. Lockable<HashTable<FIFO*>>& all_fifos()
  35. {
  36. static Lockable<HashTable<FIFO*>>* s_table;
  37. if (!s_table)
  38. s_table = new Lockable<HashTable<FIFO*>>;
  39. return *s_table;
  40. }
  41. static int s_next_fifo_id = 1;
  42. NonnullRefPtr<FIFO> FIFO::create(uid_t uid)
  43. {
  44. return adopt(*new FIFO(uid));
  45. }
  46. NonnullRefPtr<FileDescription> FIFO::open_direction(FIFO::Direction direction)
  47. {
  48. auto description = FileDescription::create(*this);
  49. attach(direction);
  50. description->set_fifo_direction({}, direction);
  51. return description;
  52. }
  53. FIFO::FIFO(uid_t uid)
  54. : m_uid(uid)
  55. {
  56. LOCKER(all_fifos().lock());
  57. all_fifos().resource().set(this);
  58. m_fifo_id = ++s_next_fifo_id;
  59. }
  60. FIFO::~FIFO()
  61. {
  62. LOCKER(all_fifos().lock());
  63. all_fifos().resource().remove(this);
  64. }
  65. void FIFO::attach(Direction direction)
  66. {
  67. if (direction == Direction::Reader) {
  68. ++m_readers;
  69. #ifdef FIFO_DEBUG
  70. kprintf("open reader (%u)\n", m_readers);
  71. #endif
  72. } else if (direction == Direction::Writer) {
  73. ++m_writers;
  74. #ifdef FIFO_DEBUG
  75. kprintf("open writer (%u)\n", m_writers);
  76. #endif
  77. }
  78. }
  79. void FIFO::detach(Direction direction)
  80. {
  81. if (direction == Direction::Reader) {
  82. #ifdef FIFO_DEBUG
  83. kprintf("close reader (%u - 1)\n", m_readers);
  84. #endif
  85. ASSERT(m_readers);
  86. --m_readers;
  87. } else if (direction == Direction::Writer) {
  88. #ifdef FIFO_DEBUG
  89. kprintf("close writer (%u - 1)\n", m_writers);
  90. #endif
  91. ASSERT(m_writers);
  92. --m_writers;
  93. }
  94. }
  95. bool FIFO::can_read(const FileDescription&) const
  96. {
  97. return !m_buffer.is_empty() || !m_writers;
  98. }
  99. bool FIFO::can_write(const FileDescription&) const
  100. {
  101. return m_buffer.space_for_writing() || !m_readers;
  102. }
  103. ssize_t FIFO::read(FileDescription&, u8* buffer, ssize_t size)
  104. {
  105. if (!m_writers && m_buffer.is_empty())
  106. return 0;
  107. #ifdef FIFO_DEBUG
  108. dbgprintf("fifo: read(%u)\n", size);
  109. #endif
  110. ssize_t nread = m_buffer.read(buffer, size);
  111. #ifdef FIFO_DEBUG
  112. dbgprintf(" -> read (%c) %u\n", buffer[0], nread);
  113. #endif
  114. return nread;
  115. }
  116. ssize_t FIFO::write(FileDescription&, const u8* buffer, ssize_t size)
  117. {
  118. if (!m_readers) {
  119. current->process().send_signal(SIGPIPE, &current->process());
  120. return -EPIPE;
  121. }
  122. #ifdef FIFO_DEBUG
  123. dbgprintf("fifo: write(%p, %u)\n", buffer, size);
  124. #endif
  125. return m_buffer.write(buffer, size);
  126. }
  127. String FIFO::absolute_path(const FileDescription&) const
  128. {
  129. return String::format("fifo:%u", m_fifo_id);
  130. }