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