ladybird/Kernel/TTY/PTYMultiplexer.cpp
Liav A f5de4f24b2 Kernel/Devices: Defer creation of SysFS component after the constructor
Instead of doing so in the constructor, let's do immediately after the
constructor, so we can safely pass a reference of a Device, so the
SysFSDeviceComponent constructor can use that object to identify whether
it's a block device or a character device.
This allows to us to not hold a device in SysFSDeviceComponent with a
RefPtr.
Also, we also call the before_removing method in both SlavePTY::unref
and File::unref, so because Device has that method being overrided, it
can ensure the device is removed always cleanly.
2021-09-11 11:41:14 +02: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();
}
KResultOr<NonnullRefPtr<OpenFileDescription>> PTYMultiplexer::open(int options)
{
return m_freelist.with_exclusive([&](auto& freelist) -> KResultOr<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);
});
}
}