ladybird/Kernel/TTY/PTYMultiplexer.cpp
Andreas Kling 79fa9765ca Kernel: Replace KResult and KResultOr<T> with Error and ErrorOr<T>
We now use AK::Error and AK::ErrorOr<T> in both kernel and userspace!
This was a slightly tedious refactoring that took a long time, so it's
not unlikely that some bugs crept in.

Nevertheless, it does pass basic functionality testing, and it's just
real nice to finally see the same pattern in all contexts. :^)
2021-11-08 01:10:53 +01:00

68 lines
1.8 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Singleton.h>
#include <Kernel/Debug.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
#include <Kernel/Sections.h>
#include <Kernel/TTY/MasterPTY.h>
#include <Kernel/TTY/PTYMultiplexer.h>
#include <LibC/errno_numbers.h>
namespace Kernel {
static Singleton<PTYMultiplexer> s_the;
PTYMultiplexer& PTYMultiplexer::the()
{
return *s_the;
}
UNMAP_AFTER_INIT PTYMultiplexer::PTYMultiplexer()
: CharacterDevice(5, 2)
{
m_freelist.with_exclusive([&](auto& freelist) {
freelist.ensure_capacity(max_pty_pairs);
for (int i = max_pty_pairs; i > 0; --i)
freelist.unchecked_append(i - 1);
});
}
UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer()
{
}
void PTYMultiplexer::initialize()
{
the().after_inserting();
}
ErrorOr<NonnullRefPtr<OpenFileDescription>> PTYMultiplexer::open(int options)
{
return m_freelist.with_exclusive([&](auto& freelist) -> ErrorOr<NonnullRefPtr<OpenFileDescription>> {
if (freelist.is_empty())
return EBUSY;
auto master_index = freelist.take_last();
auto master = TRY(MasterPTY::try_create(master_index));
dbgln_if(PTMX_DEBUG, "PTYMultiplexer::open: Vending master {}", master->index());
auto description = TRY(OpenFileDescription::try_create(*master));
description->set_rw_mode(options);
description->set_file_flags(options);
return description;
});
}
void PTYMultiplexer::notify_master_destroyed(Badge<MasterPTY>, unsigned index)
{
m_freelist.with_exclusive([&](auto& freelist) {
freelist.append(index);
dbgln_if(PTMX_DEBUG, "PTYMultiplexer: {} added to freelist", index);
});
}
}