2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <AK/HashTable.h>
|
2020-08-24 19:35:19 -06:00
|
|
|
#include <AK/Singleton.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-03-23 13:45:10 +01:00
|
|
|
#include <AK/StringView.h>
|
2019-04-06 20:29:48 +02:00
|
|
|
#include <Kernel/FileSystem/FIFO.h>
|
2019-06-07 09:36:51 +02:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-04-25 13:52:07 +02:00
|
|
|
#include <Kernel/Lock.h>
|
2019-06-06 10:54:26 +02:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/Thread.h>
|
2018-11-12 01:28:46 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
static AK::Singleton<Lockable<HashTable<FIFO*>>> s_table;
|
|
|
|
|
Kernel: Mark compilation-unit-only functions as static
This enables a nice warning in case a function becomes dead code. Also, in case
of signal_trampoline_dummy, marking it external (non-static) prevents it from
being 'optimized away', which would lead to surprising and weird linker errors.
I found these places by using -Wmissing-declarations.
The Kernel still shows these issues, which I think are false-positives,
but don't want to touch:
- Kernel/Arch/i386/CPU.cpp:1081:17: void Kernel::enter_thread_context(Kernel::Thread*, Kernel::Thread*)
- Kernel/Arch/i386/CPU.cpp:1170:17: void Kernel::context_first_init(Kernel::Thread*, Kernel::Thread*, Kernel::TrapFrame*)
- Kernel/Arch/i386/CPU.cpp:1304:16: u32 Kernel::do_init_context(Kernel::Thread*, u32)
- Kernel/Arch/i386/CPU.cpp:1347:17: void Kernel::pre_init_finished()
- Kernel/Arch/i386/CPU.cpp:1360:17: void Kernel::post_init_finished()
No idea, not gonna touch it.
- Kernel/init.cpp:104:30: void Kernel::init()
- Kernel/init.cpp:167:30: void Kernel::init_ap(u32, Kernel::Processor*)
- Kernel/init.cpp:184:17: void Kernel::init_finished(u32)
Called by boot.S.
- Kernel/init.cpp:383:16: int Kernel::__cxa_atexit(void (*)(void*), void*, void*)
- Kernel/StdLib.cpp:285:19: void __cxa_pure_virtual()
- Kernel/StdLib.cpp:300:19: void __stack_chk_fail()
- Kernel/StdLib.cpp:305:19: void __stack_chk_fail_local()
Not sure how to tell the compiler that the compiler is already using them.
Also, maybe __cxa_atexit should go into StdLib.cpp?
- Kernel/Modules/TestModule.cpp:31:17: void module_init()
- Kernel/Modules/TestModule.cpp:40:17: void module_fini()
Could maybe go into a new header. This would also provide type-checking for new modules.
2020-08-10 21:12:13 +02:00
|
|
|
static Lockable<HashTable<FIFO*>>& all_fifos()
|
2018-11-12 01:28:46 +01:00
|
|
|
{
|
2019-04-25 13:52:07 +02:00
|
|
|
return *s_table;
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
|
|
|
|
2020-01-07 07:29:50 +01:00
|
|
|
static int s_next_fifo_id = 1;
|
2019-04-25 13:52:07 +02:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
NonnullRefPtr<FIFO> FIFO::create(uid_t uid)
|
2019-04-25 13:52:07 +02:00
|
|
|
{
|
|
|
|
return adopt(*new FIFO(uid));
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:51:09 -06:00
|
|
|
KResultOr<NonnullRefPtr<FileDescription>> FIFO::open_direction(FIFO::Direction direction)
|
2019-04-29 04:55:54 +02:00
|
|
|
{
|
2019-08-11 09:32:21 +02:00
|
|
|
auto description = FileDescription::create(*this);
|
2020-09-17 13:51:09 -06:00
|
|
|
if (!description.is_error()) {
|
|
|
|
attach(direction);
|
|
|
|
description.value()->set_fifo_direction({}, direction);
|
|
|
|
}
|
2019-06-13 22:03:04 +02:00
|
|
|
return description;
|
2019-04-29 04:55:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 13:51:09 -06:00
|
|
|
KResultOr<NonnullRefPtr<FileDescription>> FIFO::open_direction_blocking(FIFO::Direction direction)
|
2020-07-16 15:23:03 -06:00
|
|
|
{
|
|
|
|
Locker locker(m_open_lock);
|
|
|
|
|
|
|
|
auto description = open_direction(direction);
|
2020-09-17 13:51:09 -06:00
|
|
|
if (description.is_error())
|
|
|
|
return description;
|
2020-07-16 15:23:03 -06:00
|
|
|
|
|
|
|
if (direction == Direction::Reader) {
|
|
|
|
m_read_open_queue.wake_all();
|
|
|
|
|
|
|
|
if (m_writers == 0) {
|
|
|
|
locker.unlock();
|
2021-02-14 15:02:14 -08:00
|
|
|
m_write_open_queue.wait_forever("FIFO");
|
2020-07-16 15:23:03 -06:00
|
|
|
locker.lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction == Direction::Writer) {
|
|
|
|
m_write_open_queue.wake_all();
|
|
|
|
|
|
|
|
if (m_readers == 0) {
|
|
|
|
locker.unlock();
|
2021-02-14 15:02:14 -08:00
|
|
|
m_read_open_queue.wait_forever("FIFO");
|
2020-07-16 15:23:03 -06:00
|
|
|
locker.lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:52:07 +02:00
|
|
|
FIFO::FIFO(uid_t uid)
|
|
|
|
: m_uid(uid)
|
|
|
|
{
|
|
|
|
LOCKER(all_fifos().lock());
|
|
|
|
all_fifos().resource().set(this);
|
2020-01-07 07:29:50 +01:00
|
|
|
m_fifo_id = ++s_next_fifo_id;
|
2020-11-29 16:05:27 -07:00
|
|
|
|
|
|
|
// Use the same block condition for read and write
|
|
|
|
m_buffer.set_unblock_callback([this]() {
|
|
|
|
evaluate_block_conditions();
|
|
|
|
});
|
2019-04-25 13:52:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FIFO::~FIFO()
|
|
|
|
{
|
|
|
|
LOCKER(all_fifos().lock());
|
|
|
|
all_fifos().resource().remove(this);
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
|
|
|
|
2019-04-29 04:55:54 +02:00
|
|
|
void FIFO::attach(Direction direction)
|
2018-11-12 01:28:46 +01:00
|
|
|
{
|
2019-05-18 04:14:22 +02:00
|
|
|
if (direction == Direction::Reader) {
|
2018-11-12 01:28:46 +01:00
|
|
|
++m_readers;
|
2019-05-18 04:14:22 +02:00
|
|
|
} else if (direction == Direction::Writer) {
|
2018-11-12 01:28:46 +01:00
|
|
|
++m_writers;
|
|
|
|
}
|
2020-11-29 16:05:27 -07:00
|
|
|
|
|
|
|
evaluate_block_conditions();
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
|
|
|
|
2019-04-29 04:55:54 +02:00
|
|
|
void FIFO::detach(Direction direction)
|
2018-11-12 01:28:46 +01:00
|
|
|
{
|
2019-05-18 04:14:22 +02:00
|
|
|
if (direction == Direction::Reader) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_readers);
|
2018-11-12 01:28:46 +01:00
|
|
|
--m_readers;
|
2019-05-18 04:14:22 +02:00
|
|
|
} else if (direction == Direction::Writer) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_writers);
|
2018-11-12 01:28:46 +01:00
|
|
|
--m_writers;
|
|
|
|
}
|
2020-11-29 16:05:27 -07:00
|
|
|
|
|
|
|
evaluate_block_conditions();
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 19:44:42 +10:00
|
|
|
bool FIFO::can_read(const FileDescription&, size_t) const
|
2018-11-12 01:28:46 +01:00
|
|
|
{
|
2018-12-03 02:24:11 +01:00
|
|
|
return !m_buffer.is_empty() || !m_writers;
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 19:44:42 +10:00
|
|
|
bool FIFO::can_write(const FileDescription&, size_t) const
|
2018-11-12 01:28:46 +01:00
|
|
|
{
|
2020-01-20 16:08:49 +01:00
|
|
|
return m_buffer.space_for_writing() || !m_readers;
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-17 13:18:51 +01:00
|
|
|
KResultOr<size_t> FIFO::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
|
2018-11-12 01:28:46 +01:00
|
|
|
{
|
2018-12-03 02:24:11 +01:00
|
|
|
if (!m_writers && m_buffer.is_empty())
|
2018-11-12 01:28:46 +01:00
|
|
|
return 0;
|
2020-08-04 18:02:23 +02:00
|
|
|
return m_buffer.read(buffer, size);
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-17 13:18:51 +01:00
|
|
|
KResultOr<size_t> FIFO::write(FileDescription&, u64, const UserOrKernelBuffer& buffer, size_t size)
|
2018-11-12 01:28:46 +01:00
|
|
|
{
|
2019-06-06 10:54:26 +02:00
|
|
|
if (!m_readers) {
|
2020-06-28 15:34:31 -06:00
|
|
|
Thread::current()->send_signal(SIGPIPE, Process::current());
|
2021-03-15 09:04:04 +01:00
|
|
|
return EPIPE;
|
2019-06-06 10:54:26 +02:00
|
|
|
}
|
2021-03-15 09:04:04 +01:00
|
|
|
|
2018-12-03 02:24:11 +01:00
|
|
|
return m_buffer.write(buffer, size);
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
2019-04-29 04:55:54 +02:00
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
String FIFO::absolute_path(const FileDescription&) const
|
2019-04-29 04:55:54 +02:00
|
|
|
{
|
2020-01-07 07:29:50 +01:00
|
|
|
return String::format("fifo:%u", m_fifo_id);
|
2019-04-29 04:55:54 +02:00
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2020-09-06 18:31:51 +02:00
|
|
|
KResult FIFO::stat(::stat& st) const
|
|
|
|
{
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
|
|
st.st_mode = S_IFIFO;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|