ladybird/Kernel/Devices/CharacterDevice.cpp
Liav A 3af70cb0fc Kernel/Devices: Abstract SysFS Device add/remove methods more properly
It is starting to get a little messy with how each device can try to add
or remove itself to either /sys/dev/block or /sys/dev/char directories.

To better do this, we introduce 4 virtual methods to take care of that,
so until we ensure all nodes in /sys/dev/block and /sys/dev/char are
actual symlinks, we allow the Device base class to call virtual methods
upon insertion or before being destroying, so it add itself elegantly to
either of these directories or remove itself when needed.

For special cases where we need to create symlinks, we have two virtual
methods to be called otherwise to do almost the same thing mentioned
before, but to use symlinks instead.
2022-07-19 11:02:37 +01:00

48 lines
1.6 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Devices/CharacterDevice.h>
#include <Kernel/FileSystem/SysFS/Subsystems/DeviceIdentifiers/CharacterDevicesDirectory.h>
namespace Kernel {
CharacterDevice::~CharacterDevice() = default;
void CharacterDevice::after_inserting_add_symlink_to_device_identifier_directory()
{
VERIFY(m_symlink_sysfs_component);
SysFSCharacterDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
list.append(*m_symlink_sysfs_component);
});
}
void CharacterDevice::before_will_be_destroyed_remove_symlink_from_device_identifier_directory()
{
VERIFY(m_symlink_sysfs_component);
SysFSCharacterDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
list.remove(*m_symlink_sysfs_component);
});
}
// FIXME: This method will be eventually removed after all nodes in /sys/dev/char/ are symlinks
void CharacterDevice::after_inserting_add_to_device_identifier_directory()
{
VERIFY(m_sysfs_component);
SysFSCharacterDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
list.append(*m_sysfs_component);
});
}
// FIXME: This method will be eventually removed after all nodes in /sys/dev/char/ are symlinks
void CharacterDevice::before_will_be_destroyed_remove_from_device_identifier_directory()
{
VERIFY(m_sysfs_component);
SysFSCharacterDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
list.remove(*m_sysfs_component);
});
}
}