Kernel+Services: Enable barebones hot-plug handling capabilities

Userspace initially didn't have any sort of mechanism to handle
device hotplug (either removing or inserting a device).
This meant that after a short term of scanning all known devices, by
fetching device events (DeviceEvent packets) from /dev/devctl, we
basically never try to read it again after SystemServer initialization
code.

To accommodate hotplug needs, we change SystemServer by ensuring it will
generate a known set of device nodes at their location during the its
main initialization code. This includes devices like /dev/mem, /dev/zero
and /dev/full, etc.

The actual responsible userspace program to handle hotplug events is a
new userspace program called DeviceMapper, with following key points:
- Its current task is to to constantly read the /dev/devctl device node.
  Because we already created generic devices, we only handle devices
  that are dynamically-generated in nature, like storage devices, audio
  channels, etc.

- Since dynamically-generated device nodes could have an infinite minor
  numbers, but major numbers are decoded to a device type, we create an
  internal registry based on two structures - DeviceNodeFamily, and
  RegisteredDeviceNode. DeviceNodeFamily objects are attached in the
  main logic code, when handling a DeviceEvent device insertion packet.
  A DeviceNodeFamily object has an internal HashTable to hold objects of
  RegisteredDeviceNode class.

- Because some device nodes could still share the same major number (TTY
  and serial TTY devices), we have two modes of allocation - limited
  allocation (so a range is defined for a major number), or infinite
  range. Therefore, two (or more) separate DeviceNodeFamily objects can
  can exist albeit sharing the same major number, but they are required
  to allocate from a different minor numbers' range to ensure there are
  no collisions.

- As for KCOV, we handle this device differently. In case the user
  compiled the kernel with such support - this happens to be a singular
  device node that we usually don't need, so it's dynamically-generated
  too, and because it has only one instance, we don't register it in our
  internal registry to not make it complicated needlessly.

The Kernel code is modified to allow proper blocking in case of no
events in the DeviceControlDevice class, because otherwise we will need
to poll periodically the device to check if a new event is available,
which would waste CPU time for no good reason.
This commit is contained in:
Liav A 2023-04-29 21:17:20 +03:00 committed by Andrew Kaster
parent 9dbd22b555
commit 446200d6f3
12 changed files with 535 additions and 348 deletions

View file

@ -52,14 +52,6 @@ Device* DeviceManagement::get_device(MajorNumber major, MinorNumber minor)
});
}
Optional<DeviceEvent> DeviceManagement::dequeue_top_device_event(Badge<DeviceControlDevice>)
{
SpinlockLocker locker(m_event_queue_lock);
if (m_event_queue.is_empty())
return {};
return m_event_queue.dequeue();
}
void DeviceManagement::before_device_removal(Badge<Device>, Device& device)
{
u64 device_id = encoded_device(device.major(), device.minor());
@ -68,15 +60,20 @@ void DeviceManagement::before_device_removal(Badge<Device>, Device& device)
map.remove(encoded_device(device.major(), device.minor()));
});
{
m_event_queue.with([&](auto& queue) {
DeviceEvent event { DeviceEvent::State::Removed, device.is_block_device(), device.major().value(), device.minor().value() };
SpinlockLocker locker(m_event_queue_lock);
m_event_queue.enqueue(event);
}
queue.enqueue(event);
});
if (m_device_control_device)
m_device_control_device->evaluate_block_conditions();
}
SpinlockProtected<CircularQueue<DeviceEvent, 100>, LockRank::None>& DeviceManagement::event_queue(Badge<DeviceControlDevice>)
{
return m_event_queue;
}
void DeviceManagement::after_inserting_device(Badge<Device>, Device& device)
{
u64 device_id = encoded_device(device.major(), device.minor());
@ -92,11 +89,11 @@ void DeviceManagement::after_inserting_device(Badge<Device>, Device& device)
}
});
{
m_event_queue.with([&](auto& queue) {
DeviceEvent event { DeviceEvent::State::Inserted, device.is_block_device(), device.major().value(), device.minor().value() };
SpinlockLocker locker(m_event_queue_lock);
m_event_queue.enqueue(event);
}
queue.enqueue(event);
});
if (m_device_control_device)
m_device_control_device->evaluate_block_conditions();
}

View file

@ -36,8 +36,6 @@ public:
bool is_console_device_attached() const { return !m_console_device.is_null(); }
void attach_console_device(ConsoleDevice const&);
Optional<DeviceEvent> dequeue_top_device_event(Badge<DeviceControlDevice>);
void after_inserting_device(Badge<Device>, Device&);
void before_device_removal(Badge<Device>, Device&);
@ -68,14 +66,14 @@ public:
return device;
}
SpinlockProtected<CircularQueue<DeviceEvent, 100>, LockRank::None>& event_queue(Badge<DeviceControlDevice>);
private:
LockRefPtr<NullDevice> m_null_device;
LockRefPtr<ConsoleDevice> m_console_device;
LockRefPtr<DeviceControlDevice> m_device_control_device;
SpinlockProtected<HashMap<u64, Device*>, LockRank::None> m_devices {};
mutable Spinlock<LockRank::None> m_event_queue_lock {};
CircularQueue<DeviceEvent, 100> m_event_queue;
SpinlockProtected<CircularQueue<DeviceEvent, 100>, LockRank::None> m_event_queue {};
};
}

View file

@ -19,7 +19,9 @@ UNMAP_AFTER_INIT NonnullLockRefPtr<DeviceControlDevice> DeviceControlDevice::mus
bool DeviceControlDevice::can_read(OpenFileDescription const&, u64) const
{
return true;
return DeviceManagement::the().event_queue({}).with([](auto& queue) -> bool {
return !queue.is_empty();
});
}
UNMAP_AFTER_INIT DeviceControlDevice::DeviceControlDevice()
@ -29,18 +31,24 @@ UNMAP_AFTER_INIT DeviceControlDevice::DeviceControlDevice()
UNMAP_AFTER_INIT DeviceControlDevice::~DeviceControlDevice() = default;
ErrorOr<size_t> DeviceControlDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
ErrorOr<size_t> DeviceControlDevice::read(OpenFileDescription&, u64 offset, UserOrKernelBuffer& buffer, size_t size)
{
auto device_event = DeviceManagement::the().dequeue_top_device_event({});
if (!device_event.has_value())
return 0;
if (size < sizeof(DeviceEvent))
if (offset != 0)
return Error::from_errno(EINVAL);
if ((size % sizeof(DeviceEvent)) != 0)
return Error::from_errno(EOVERFLOW);
size_t nread = 0;
TRY(buffer.write(&device_event.value(), nread, sizeof(DeviceEvent)));
nread += sizeof(DeviceEvent);
return nread;
return DeviceManagement::the().event_queue({}).with([&](auto& queue) -> ErrorOr<size_t> {
size_t nread = 0;
for (size_t event_index = 0; event_index < (size / sizeof(DeviceEvent)); event_index++) {
if (queue.is_empty())
break;
auto event = queue.dequeue();
TRY(buffer.write(&event, nread, sizeof(DeviceEvent)));
nread += sizeof(DeviceEvent);
}
return nread;
});
}
ErrorOr<void> DeviceControlDevice::ioctl(OpenFileDescription&, unsigned, Userspace<void*>)

View file

@ -10,6 +10,7 @@ if (SERENITYOS)
add_subdirectory(ChessEngine)
add_subdirectory(Clipboard)
add_subdirectory(CrashDaemon)
add_subdirectory(DeviceMapper)
add_subdirectory(DHCPClient)
add_subdirectory(FileSystemAccessServer)
add_subdirectory(KeyboardPreferenceLoader)

View file

@ -0,0 +1,13 @@
serenity_component(
DeviceMapper
REQUIRED
TARGETS DeviceMapper
)
set(SOURCES
main.cpp
DeviceEventLoop.cpp
)
serenity_bin(DeviceMapper)
target_link_libraries(DeviceMapper PRIVATE LibCore LibFileSystem LibMain)

View file

@ -0,0 +1,243 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "DeviceEventLoop.h"
#include <AK/Debug.h>
#include <LibCore/DirIterator.h>
#include <LibCore/System.h>
#include <LibIPC/MultiServer.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
namespace DeviceMapper {
DeviceEventLoop::DeviceEventLoop(NonnullOwnPtr<Core::File> devctl_file)
: m_devctl_file(move(devctl_file))
{
}
using MinorNumberAllocationType = DeviceEventLoop::MinorNumberAllocationType;
static constexpr DeviceEventLoop::DeviceNodeMatch s_matchers[] = {
{ "audio"sv, "audio"sv, "audio/%digit"sv, DeviceNodeFamily::Type::CharacterDevice, 116, MinorNumberAllocationType::SequentialUnlimited, 0, 0, 0220 },
{ {}, "render"sv, "gpu/render%digit"sv, DeviceNodeFamily::Type::CharacterDevice, 28, MinorNumberAllocationType::SequentialUnlimited, 0, 0, 0666 },
{ "window"sv, "gpu-connector"sv, "gpu/connector%digit"sv, DeviceNodeFamily::Type::CharacterDevice, 226, MinorNumberAllocationType::SequentialUnlimited, 0, 0, 0660 },
{ {}, "virtio-console"sv, "hvc0p%digit"sv, DeviceNodeFamily::Type::CharacterDevice, 229, MinorNumberAllocationType::SequentialUnlimited, 0, 0, 0666 },
{ "phys"sv, "hid-mouse"sv, "input/mouse/%digit"sv, DeviceNodeFamily::Type::CharacterDevice, 10, MinorNumberAllocationType::SequentialUnlimited, 0, 0, 0666 },
{ "phys"sv, "hid-keyboard"sv, "input/keyboard/%digit"sv, DeviceNodeFamily::Type::CharacterDevice, 85, MinorNumberAllocationType::SequentialUnlimited, 0, 0, 0666 },
{ {}, "storage"sv, "hd%letter"sv, DeviceNodeFamily::Type::BlockDevice, 3, MinorNumberAllocationType::SequentialUnlimited, 0, 0, 0600 },
{ "tty"sv, "console"sv, "tty%digit"sv, DeviceNodeFamily::Type::CharacterDevice, 4, MinorNumberAllocationType::SequentialLimited, 0, 63, 0620 },
{ "tty"sv, "console"sv, "ttyS%digit"sv, DeviceNodeFamily::Type::CharacterDevice, 4, MinorNumberAllocationType::SequentialLimited, 64, 127, 0620 },
};
static bool is_in_minor_number_range(DeviceEventLoop::DeviceNodeMatch const& matcher, MinorNumber minor_number)
{
if (matcher.minor_number_allocation_type == MinorNumberAllocationType::SequentialUnlimited)
return true;
return matcher.minor_number_start <= minor_number && static_cast<MinorNumber>(matcher.minor_number_start.value() + matcher.minor_number_range_size) >= minor_number;
}
static Optional<DeviceEventLoop::DeviceNodeMatch const&> device_node_family_to_match_type(DeviceNodeFamily::Type unix_device_type, MajorNumber major_number, MinorNumber minor_number)
{
for (auto& matcher : s_matchers) {
if (matcher.major_number == major_number
&& unix_device_type == matcher.unix_device_type
&& is_in_minor_number_range(matcher, minor_number))
return matcher;
}
return {};
}
static bool is_in_family_minor_number_range(DeviceNodeFamily const& family, MinorNumber minor_number)
{
return family.base_minor_number() <= minor_number && static_cast<MinorNumber>(family.base_minor_number().value() + family.devices_symbol_suffix_allocation_map().size()) >= minor_number;
}
Optional<DeviceNodeFamily&> DeviceEventLoop::find_device_node_family(DeviceNodeFamily::Type unix_device_type, MajorNumber major_number, MinorNumber minor_number) const
{
for (auto const& family : m_device_node_families) {
if (family->major_number() == major_number && family->type() == unix_device_type && is_in_family_minor_number_range(*family, minor_number))
return *family.ptr();
}
return {};
}
ErrorOr<NonnullRefPtr<DeviceNodeFamily>> DeviceEventLoop::find_or_register_new_device_node_family(DeviceNodeMatch const& match, DeviceNodeFamily::Type unix_device_type, MajorNumber major_number, MinorNumber minor_number)
{
if (auto possible_family = find_device_node_family(unix_device_type, major_number, minor_number); possible_family.has_value())
return possible_family.release_value();
unsigned allocation_map_size = 1024;
if (match.minor_number_allocation_type == MinorNumberAllocationType::SequentialLimited)
allocation_map_size = match.minor_number_range_size;
auto bitmap = TRY(Bitmap::create(allocation_map_size, false));
auto node = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DeviceNodeFamily(move(bitmap),
match.family_type_literal,
unix_device_type,
major_number,
minor_number)));
TRY(m_device_node_families.try_append(node));
return node;
}
static ErrorOr<String> build_suffix_with_letters(size_t allocation_index)
{
auto base_string = TRY(String::from_utf8(""sv));
while (true) {
base_string = TRY(String::formatted("{:c}{}", 'a' + (allocation_index % 26), base_string));
allocation_index = (allocation_index / 26);
if (allocation_index == 0)
break;
allocation_index = allocation_index - 1;
}
return base_string;
}
static ErrorOr<String> build_suffix_with_numbers(size_t allocation_index)
{
return String::number(allocation_index);
}
static ErrorOr<void> prepare_permissions_after_populating_devtmpfs(StringView path, DeviceEventLoop::DeviceNodeMatch const& match)
{
if (match.permission_group.is_null())
return {};
auto group = TRY(Core::System::getgrnam(match.permission_group));
VERIFY(group.has_value());
TRY(Core::System::endgrent());
TRY(Core::System::chown(path, 0, group.value().gr_gid));
return {};
}
ErrorOr<void> DeviceEventLoop::register_new_device(DeviceNodeFamily::Type unix_device_type, MajorNumber major_number, MinorNumber minor_number)
{
auto possible_match = device_node_family_to_match_type(unix_device_type, major_number, minor_number);
if (!possible_match.has_value())
return {};
auto const& match = possible_match.release_value();
auto device_node_family = TRY(find_or_register_new_device_node_family(match, unix_device_type, major_number, minor_number));
static constexpr StringView devtmpfs_base_path = "/dev/"sv;
auto path_pattern = TRY(String::from_utf8(match.path_pattern));
auto& allocation_map = device_node_family->devices_symbol_suffix_allocation_map();
auto possible_allocated_suffix_index = allocation_map.find_first_unset();
if (!possible_allocated_suffix_index.has_value()) {
// FIXME: Make the allocation map bigger?
return Error::from_errno(ERANGE);
}
auto allocated_suffix_index = possible_allocated_suffix_index.release_value();
auto path = TRY(String::from_utf8(path_pattern));
if (match.path_pattern.contains("%digit"sv)) {
auto replacement = TRY(build_suffix_with_numbers(allocated_suffix_index));
path = TRY(path.replace("%digit"sv, replacement, ReplaceMode::All));
}
if (match.path_pattern.contains("%letter"sv)) {
auto replacement = TRY(build_suffix_with_letters(allocated_suffix_index));
path = TRY(path.replace("%letter"sv, replacement, ReplaceMode::All));
}
VERIFY(!path.is_empty());
path = TRY(String::formatted("{}{}", devtmpfs_base_path, path));
mode_t old_mask = umask(0);
if (unix_device_type == DeviceNodeFamily::Type::BlockDevice)
TRY(Core::System::create_block_device(path.bytes_as_string_view(), match.create_mode, major_number.value(), minor_number.value()));
else
TRY(Core::System::create_char_device(path.bytes_as_string_view(), match.create_mode, major_number.value(), minor_number.value()));
umask(old_mask);
TRY(prepare_permissions_after_populating_devtmpfs(path.bytes_as_string_view(), match));
auto result = TRY(device_node_family->registered_nodes().try_set(RegisteredDeviceNode { move(path), minor_number }, AK::HashSetExistingEntryBehavior::Keep));
VERIFY(result != HashSetResult::ReplacedExistingEntry);
if (result == HashSetResult::KeptExistingEntry) {
// FIXME: Handle this case properly.
return Error::from_errno(EEXIST);
}
allocation_map.set(allocated_suffix_index, true);
return {};
}
ErrorOr<void> DeviceEventLoop::unregister_device(DeviceNodeFamily::Type unix_device_type, MajorNumber major_number, MinorNumber minor_number)
{
if (!device_node_family_to_match_type(unix_device_type, major_number, minor_number).has_value())
return {};
auto possible_family = find_device_node_family(unix_device_type, major_number, minor_number);
if (!possible_family.has_value()) {
// FIXME: Handle cases where we can't remove a device node.
// This could happen when the DeviceMapper program was restarted
// so the previous state was not preserved and a device was removed.
return Error::from_errno(ENODEV);
}
auto& family = possible_family.release_value();
for (auto& node : family.registered_nodes()) {
if (node.minor_number() == minor_number)
TRY(Core::System::unlink(node.device_path()));
}
auto removed_anything = family.registered_nodes().remove_all_matching([minor_number](auto& device) { return device.minor_number() == minor_number; });
if (!removed_anything) {
// FIXME: Handle cases where we can't remove a device node.
// This could happen when the DeviceMapper program was restarted
// so the previous state was not preserved and a device was removed.
return Error::from_errno(ENODEV);
}
return {};
}
static ErrorOr<void> create_kcov_device_node()
{
mode_t old_mask = umask(0);
ScopeGuard umask_guard([old_mask] { umask(old_mask); });
TRY(Core::System::create_char_device("/dev/kcov"sv, 0666, 30, 0));
return {};
}
ErrorOr<void> DeviceEventLoop::read_one_or_eof(DeviceEvent& event)
{
if (m_devctl_file->read_until_filled({ bit_cast<u8*>(&event), sizeof(DeviceEvent) }).is_error()) {
// Bad! Kernel and SystemServer apparently disagree on the record size,
// which means that previous data is likely to be invalid.
return Error::from_string_view("File ended after incomplete record? /dev/devctl seems broken!"sv);
}
return {};
}
ErrorOr<void> DeviceEventLoop::drain_events_from_devctl()
{
for (;;) {
DeviceEvent event;
TRY(read_one_or_eof(event));
// NOTE: Ignore any event related to /dev/devctl device node - normally
// it should never disappear from the system and we already use it in this
// code.
if (event.major_number == 2 && event.minor_number == 10 && !event.is_block_device)
continue;
if (event.state == DeviceEvent::State::Inserted) {
// NOTE: We have a special function for the KCOV device, because we don't
// want to create a new MinorNumberAllocationType (e.g. SingleInstance).
// Instead, just blindly create that device node and assume we will never
// have to worry about it, so we don't need to register that!
if (event.major_number == 30 && event.minor_number == 0 && !event.is_block_device) {
TRY(create_kcov_device_node());
continue;
}
VERIFY(event.is_block_device == 1 || event.is_block_device == 0);
TRY(register_new_device(event.is_block_device ? DeviceNodeFamily::Type::BlockDevice : DeviceNodeFamily::Type::CharacterDevice, event.major_number, event.minor_number));
} else if (event.state == DeviceEvent::State::Removed) {
if (event.major_number == 30 && event.minor_number == 0 && !event.is_block_device) {
dbgln("DeviceMapper: unregistering device failed: kcov tried to de-register itself!?");
continue;
}
if (auto error_or_void = unregister_device(event.is_block_device ? DeviceNodeFamily::Type::BlockDevice : DeviceNodeFamily::Type::CharacterDevice, event.major_number, event.minor_number); error_or_void.is_error())
dbgln("DeviceMapper: unregistering device failed: {}", error_or_void.error());
} else {
dbgln("DeviceMapper: Unhandled device event ({:x})!", event.state);
}
}
VERIFY_NOT_REACHED();
}
}

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include "DeviceNodeFamily.h"
#include <AK/ByteBuffer.h>
#include <Kernel/API/DeviceEvent.h>
#include <Kernel/API/DeviceFileTypes.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibCore/Notifier.h>
namespace DeviceMapper {
class DeviceEventLoop {
public:
enum class MinorNumberAllocationType {
SequentialUnlimited,
SequentialLimited,
};
enum class UnixDeviceType {
BlockDevice,
CharacterDevice,
};
struct DeviceNodeMatch {
StringView permission_group;
StringView family_type_literal;
StringView path_pattern;
DeviceNodeFamily::Type unix_device_type;
MajorNumber major_number;
MinorNumberAllocationType minor_number_allocation_type;
MinorNumber minor_number_start;
size_t minor_number_range_size;
mode_t create_mode;
};
DeviceEventLoop(NonnullOwnPtr<Core::File>);
virtual ~DeviceEventLoop() = default;
ErrorOr<void> drain_events_from_devctl();
private:
Optional<DeviceNodeFamily&> find_device_node_family(DeviceNodeFamily::Type unix_device_type, MajorNumber major_number, MinorNumber minor_number) const;
ErrorOr<NonnullRefPtr<DeviceNodeFamily>> find_or_register_new_device_node_family(DeviceNodeMatch const& match, DeviceNodeFamily::Type unix_device_type, MajorNumber major_number, MinorNumber minor_number);
ErrorOr<void> register_new_device(DeviceNodeFamily::Type unix_device_type, MajorNumber major_number, MinorNumber minor_number);
ErrorOr<void> unregister_device(DeviceNodeFamily::Type unix_device_type, MajorNumber major_number, MinorNumber minor_number);
ErrorOr<void> read_one_or_eof(DeviceEvent& event);
Vector<NonnullRefPtr<DeviceNodeFamily>> m_device_node_families;
NonnullOwnPtr<Core::File> const m_devctl_file;
};
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include "RegisteredDeviceNode.h"
#include <AK/Bitmap.h>
#include <AK/HashTable.h>
#include <AK/Types.h>
#include <Kernel/API/DeviceFileTypes.h>
namespace DeviceMapper {
class DeviceNodeFamily : public RefCounted<DeviceNodeFamily> {
public:
enum class Type {
BlockDevice,
CharacterDevice,
};
DeviceNodeFamily(Bitmap devices_symbol_suffix_allocation_map, StringView literal_device_family, Type type, MajorNumber major, MinorNumber base_minor)
: m_literal_device_family(literal_device_family)
, m_type(type)
, m_major(major)
, m_base_minor(base_minor)
, m_devices_symbol_suffix_allocation_map(move(devices_symbol_suffix_allocation_map))
{
}
StringView literal_device_family() const { return m_literal_device_family; }
MajorNumber major_number() const { return m_major; }
MinorNumber base_minor_number() const { return m_base_minor; }
Type type() const { return m_type; }
HashTable<RegisteredDeviceNode>& registered_nodes() { return m_registered_nodes; }
Bitmap& devices_symbol_suffix_allocation_map() { return m_devices_symbol_suffix_allocation_map; }
Bitmap const& devices_symbol_suffix_allocation_map() const { return m_devices_symbol_suffix_allocation_map; }
private:
StringView m_literal_device_family;
Type m_type { Type::CharacterDevice };
MajorNumber m_major { 0 };
MinorNumber m_base_minor { 0 };
HashTable<RegisteredDeviceNode> m_registered_nodes;
Bitmap m_devices_symbol_suffix_allocation_map;
};
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Bitmap.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <Kernel/API/DeviceFileTypes.h>
namespace DeviceMapper {
class RegisteredDeviceNode {
public:
RegisteredDeviceNode(String device_path, MinorNumber minor)
: m_device_path(move(device_path))
, m_minor(minor)
{
}
StringView device_path() const { return m_device_path.bytes_as_string_view(); }
MinorNumber minor_number() const { return m_minor; }
private:
String m_device_path;
MinorNumber m_minor { 0 };
};
}
namespace AK {
template<>
struct Traits<DeviceMapper::RegisteredDeviceNode> : public GenericTraits<DeviceMapper::RegisteredDeviceNode> {
static unsigned hash(DeviceMapper::RegisteredDeviceNode const& node)
{
return int_hash(node.minor_number().value());
}
static bool equals(DeviceMapper::RegisteredDeviceNode const& a, DeviceMapper::RegisteredDeviceNode const& b)
{
return a.minor_number() == b.minor_number();
}
};
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "DeviceEventLoop.h"
#include <AK/Assertions.h>
#include <AK/ByteBuffer.h>
#include <AK/Debug.h>
#include <AK/String.h>
#include <Kernel/API/DeviceEvent.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/DirIterator.h>
#include <LibCore/Event.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <unistd.h>
ErrorOr<int> serenity_main(Main::Arguments)
{
TRY(Core::System::unveil("/dev/"sv, "rwc"sv));
TRY(Core::System::unveil("/etc/group"sv, "rw"sv));
TRY(Core::System::unveil(nullptr, nullptr));
TRY(Core::System::pledge("stdio rpath dpath wpath cpath chown fattr"));
auto file_or_error = Core::File::open("/dev/devctl"sv, Core::File::OpenMode::Read);
if (file_or_error.is_error()) {
warnln("Failed to open /dev/devctl - {}", file_or_error.error());
VERIFY_NOT_REACHED();
}
auto file = file_or_error.release_value();
DeviceMapper::DeviceEventLoop device_event_loop(move(file));
if (auto result = device_event_loop.drain_events_from_devctl(); result.is_error())
dbgln("DeviceMapper: Fatal error: {}", result.release_error());
// NOTE: If we return from drain_events_from_devctl, it must be an error
// so we should always return 1!
return 1;
}

View file

@ -364,7 +364,6 @@ ErrorOr<NonnullRefPtr<Service>> Service::try_create(Core::ConfigFile const& conf
bool Service::is_enabled_for_system_mode(StringView mode) const
{
extern DeprecatedString g_system_mode;
return m_system_modes.contains_slow(mode);
}

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -17,6 +18,7 @@
#include <LibCore/Event.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibCore/Process.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <errno.h>
@ -67,6 +69,8 @@ static void sigchld_handler(int)
}
}
namespace SystemServer {
static ErrorOr<void> determine_system_mode()
{
ArmedScopeGuard declare_text_mode_on_failure([&] {
@ -93,280 +97,6 @@ static ErrorOr<void> determine_system_mode()
declare_text_mode_on_failure.disarm();
dbgln("Read system_mode: {}", g_system_mode);
struct stat file_state;
int rc = lstat("/dev/gpu/connector0", &file_state);
if (rc != 0 && g_system_mode == "graphical") {
dbgln("WARNING: No device nodes at /dev/gpu/ directory. This is probably a sign of disabled graphics functionality.");
dbgln("To cope with this, I'll turn off graphical mode.");
g_system_mode = "text";
}
return {};
}
static ErrorOr<void> chown_all_matching_device_nodes_under_specific_directory(StringView directory, group const& group)
{
struct stat cur_file_stat;
Core::DirIterator di(directory, Core::DirIterator::SkipParentAndBaseDir);
if (di.has_error())
VERIFY_NOT_REACHED();
while (di.has_next()) {
auto entry_name = di.next_full_path();
auto rc = stat(entry_name.characters(), &cur_file_stat);
if (rc < 0)
continue;
TRY(Core::System::chown(entry_name, 0, group.gr_gid));
}
return {};
}
static ErrorOr<void> chown_all_matching_device_nodes(group const& group, unsigned major_number)
{
struct stat cur_file_stat;
Core::DirIterator di("/dev/", Core::DirIterator::SkipParentAndBaseDir);
if (di.has_error())
VERIFY_NOT_REACHED();
while (di.has_next()) {
auto entry_name = di.next_full_path();
auto rc = stat(entry_name.characters(), &cur_file_stat);
if (rc < 0)
continue;
if (major(cur_file_stat.st_rdev) != major_number)
continue;
TRY(Core::System::chown(entry_name, 0, group.gr_gid));
}
return {};
}
inline char offset_character_with_number(char base_char, u8 offset)
{
char offsetted_char = base_char;
VERIFY(static_cast<size_t>(offsetted_char) + static_cast<size_t>(offset) < 256);
offsetted_char += offset;
return offsetted_char;
}
static ErrorOr<void> create_devtmpfs_block_device(StringView name, mode_t mode, unsigned major, unsigned minor)
{
return Core::System::mknod(name, mode | S_IFBLK, makedev(major, minor));
}
static ErrorOr<void> create_devtmpfs_char_device(StringView name, mode_t mode, unsigned major, unsigned minor)
{
return Core::System::mknod(name, mode | S_IFCHR, makedev(major, minor));
}
static ErrorOr<bool> read_one_or_eof(NonnullOwnPtr<Core::File>& file, DeviceEvent& event)
{
auto const read_buf = TRY(file->read_some({ (u8*)&event, sizeof(DeviceEvent) }));
if (read_buf.size() == sizeof(DeviceEvent)) {
// Good! We could read another DeviceEvent.
return true;
}
if (file->is_eof()) {
// Good! We have reached the "natural" end of the file.
return false;
}
// Bad! Kernel and SystemServer apparently disagree on the record size,
// which means that previous data is likely to be invalid.
return Error::from_string_view("File ended after incomplete record? /dev/devctl seems broken!"sv);
}
static ErrorOr<void> populate_devtmpfs_devices_based_on_devctl()
{
auto file_or_error = Core::File::open("/dev/devctl"sv, Core::File::OpenMode::Read);
if (file_or_error.is_error()) {
warnln("Failed to open /dev/devctl - {}", file_or_error.error());
VERIFY_NOT_REACHED();
}
auto file = file_or_error.release_value();
DeviceEvent event;
while (TRY(read_one_or_eof(file, event))) {
if (event.state != DeviceEvent::Inserted)
continue;
auto major_number = event.major_number;
auto minor_number = event.minor_number;
bool is_block_device = (event.is_block_device == 1);
switch (major_number) {
case 116: {
if (!is_block_device) {
auto name = TRY(String::formatted("/dev/audio/{}", minor_number));
TRY(create_devtmpfs_char_device(name.bytes_as_string_view(), 0220, 116, minor_number));
break;
}
break;
}
case 28: {
auto name = TRY(String::formatted("/dev/gpu/render{}", minor_number));
TRY(create_devtmpfs_block_device(name.bytes_as_string_view(), 0666, 28, minor_number));
break;
}
case 226: {
auto name = TRY(String::formatted("/dev/gpu/connector{}", minor_number));
TRY(create_devtmpfs_char_device(name.bytes_as_string_view(), 0666, 226, minor_number));
break;
}
case 229: {
if (!is_block_device) {
auto name = TRY(String::formatted("/dev/hvc0p{}", minor_number));
TRY(create_devtmpfs_char_device(name.bytes_as_string_view(), 0666, 229, minor_number));
}
break;
}
case 10: {
if (!is_block_device) {
switch (minor_number) {
case 0: {
TRY(create_devtmpfs_char_device("/dev/input/mouse/0"sv, 0666, 10, 0));
break;
}
default:
warnln("Unknown character device {}:{}", major_number, minor_number);
}
}
break;
}
case 85: {
if (!is_block_device) {
switch (minor_number) {
case 0: {
TRY(create_devtmpfs_char_device("/dev/input/keyboard/0"sv, 0666, 85, 0));
break;
}
default:
warnln("Unknown character device {}:{}", major_number, minor_number);
}
}
break;
}
case 1: {
if (!is_block_device) {
switch (minor_number) {
case 5: {
TRY(create_devtmpfs_char_device("/dev/zero"sv, 0666, 1, 5));
break;
}
case 1: {
TRY(create_devtmpfs_char_device("/dev/mem"sv, 0666, 1, 1));
break;
}
case 3: {
TRY(create_devtmpfs_char_device("/dev/null"sv, 0666, 1, 3));
break;
}
case 7: {
TRY(create_devtmpfs_char_device("/dev/full"sv, 0666, 1, 7));
break;
}
case 8: {
TRY(create_devtmpfs_char_device("/dev/random"sv, 0666, 1, 8));
break;
}
default:
warnln("Unknown character device {}:{}", major_number, minor_number);
break;
}
}
break;
}
case 30: {
if (!is_block_device) {
auto name = TRY(String::formatted("/dev/kcov{}", minor_number));
TRY(create_devtmpfs_char_device(name.bytes_as_string_view(), 0666, 30, minor_number));
}
break;
}
case 3: {
if (is_block_device) {
auto name = TRY(String::formatted("/dev/hd{}", offset_character_with_number('a', minor_number)));
TRY(create_devtmpfs_block_device(name.bytes_as_string_view(), 0600, 3, minor_number));
}
break;
}
case 5: {
if (!is_block_device) {
switch (minor_number) {
case 1: {
TRY(create_devtmpfs_char_device("/dev/console"sv, 0666, 5, 1));
break;
}
case 2: {
TRY(create_devtmpfs_char_device("/dev/ptmx"sv, 0666, 5, 2));
break;
}
case 0: {
TRY(create_devtmpfs_char_device("/dev/tty"sv, 0666, 5, 0));
break;
}
default:
warnln("Unknown character device {}:{}", major_number, minor_number);
}
}
break;
}
case 4: {
if (!is_block_device) {
switch (minor_number) {
case 0: {
TRY(create_devtmpfs_char_device("/dev/tty0"sv, 0620, 4, 0));
break;
}
case 1: {
TRY(create_devtmpfs_char_device("/dev/tty1"sv, 0620, 4, 1));
break;
}
case 2: {
TRY(create_devtmpfs_char_device("/dev/tty2"sv, 0620, 4, 2));
break;
}
case 3: {
TRY(create_devtmpfs_char_device("/dev/tty3"sv, 0620, 4, 3));
break;
}
case 64: {
TRY(create_devtmpfs_char_device("/dev/ttyS0"sv, 0620, 4, 64));
break;
}
case 65: {
TRY(create_devtmpfs_char_device("/dev/ttyS1"sv, 0620, 4, 65));
break;
}
case 66: {
TRY(create_devtmpfs_char_device("/dev/ttyS2"sv, 0620, 4, 66));
break;
}
case 67: {
TRY(create_devtmpfs_char_device("/dev/ttyS3"sv, 0666, 4, 67));
break;
}
default:
warnln("Unknown character device {}:{}", major_number, minor_number);
}
}
break;
}
default:
if (!is_block_device)
warnln("Unknown character device {}:{}", major_number, minor_number);
else
warnln("Unknown block device {}:{}", major_number, minor_number);
break;
}
}
return {};
}
static ErrorOr<void> populate_devtmpfs()
{
mode_t old_mask = umask(0);
printf("Changing umask %#o\n", old_mask);
TRY(create_devtmpfs_char_device("/dev/devctl"sv, 0660, 2, 10));
TRY(populate_devtmpfs_devices_based_on_devctl());
umask(old_mask);
TRY(Core::System::symlink("/dev/random"sv, "/dev/urandom"sv));
return {};
}
@ -395,40 +125,26 @@ static ErrorOr<void> prepare_bare_minimum_devtmpfs_directory_structure()
TRY(Core::System::mkdir("/dev/gpu"sv, 0755));
TRY(Core::System::mkdir("/dev/pts"sv, 0755));
TRY(Core::System::mount(-1, "/dev/pts"sv, "devpts"sv, 0));
mode_t old_mask = umask(0);
TRY(Core::System::create_char_device("/dev/devctl"sv, 0660, 2, 10));
TRY(Core::System::create_char_device("/dev/zero"sv, 0666, 1, 5));
TRY(Core::System::create_char_device("/dev/mem"sv, 0600, 1, 1));
TRY(Core::System::create_char_device("/dev/null"sv, 0666, 3, 0));
TRY(Core::System::create_char_device("/dev/full"sv, 0666, 7, 0));
TRY(Core::System::create_char_device("/dev/random"sv, 0666, 8, 0));
TRY(Core::System::create_char_device("/dev/console"sv, 0666, 5, 1));
TRY(Core::System::create_char_device("/dev/ptmx"sv, 0666, 5, 2));
TRY(Core::System::create_char_device("/dev/tty"sv, 0666, 5, 0));
umask(old_mask);
TRY(Core::System::symlink("/dev/random"sv, "/dev/urandom"sv));
TRY(Core::System::chmod("/dev/urandom"sv, 0666));
return {};
}
static ErrorOr<void> prepare_permissions_after_populating_devtmpfs()
static ErrorOr<void> spawn_device_mapper_process()
{
TRY(Core::System::chmod("/dev/urandom"sv, 0666));
auto phys_group = TRY(Core::System::getgrnam("phys"sv));
VERIFY(phys_group.has_value());
// FIXME: Try to find a way to not hardcode the major number of display connector device nodes.
TRY(chown_all_matching_device_nodes(phys_group.value(), 29));
auto const filter_chown_ENOENT = [](ErrorOr<void> result) -> ErrorOr<void> {
auto const chown_enoent = Error::from_syscall("chown"sv, -ENOENT);
if (result.is_error() && result.error() == chown_enoent) {
dbgln("{}", result.release_error());
return {};
}
return result;
};
TRY(filter_chown_ENOENT(Core::System::chown("/dev/input/keyboard/0"sv, 0, phys_group.value().gr_gid)));
TRY(filter_chown_ENOENT(Core::System::chown("/dev/input/mouse/0"sv, 0, phys_group.value().gr_gid)));
auto tty_group = TRY(Core::System::getgrnam("tty"sv));
VERIFY(tty_group.has_value());
// FIXME: Try to find a way to not hardcode the major number of tty nodes.
TRY(chown_all_matching_device_nodes(tty_group.release_value(), 4));
auto audio_group = TRY(Core::System::getgrnam("audio"sv));
VERIFY(audio_group.has_value());
TRY(Core::System::chown("/dev/audio"sv, 0, audio_group->gr_gid));
TRY(chown_all_matching_device_nodes_under_specific_directory("/dev/audio"sv, audio_group.release_value()));
TRY(Core::System::endgrent());
TRY(Core::Process::spawn("/bin/DeviceMapper"sv, ReadonlySpan<StringView> {}, {}, Core::Process::KeepAsChild::No));
return {};
}
@ -436,8 +152,7 @@ static ErrorOr<void> prepare_synthetic_filesystems()
{
TRY(prepare_bare_minimum_filesystem_mounts());
TRY(prepare_bare_minimum_devtmpfs_directory_structure());
TRY(populate_devtmpfs());
TRY(prepare_permissions_after_populating_devtmpfs());
TRY(spawn_device_mapper_process());
return {};
}
@ -557,6 +272,8 @@ static ErrorOr<void> activate_user_services_based_on_system_mode()
return {};
}
};
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
bool user = false;
@ -565,18 +282,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.parse(arguments);
if (!user) {
TRY(mount_all_filesystems());
TRY(prepare_synthetic_filesystems());
TRY(reopen_base_file_descriptors());
TRY(SystemServer::mount_all_filesystems());
TRY(SystemServer::prepare_synthetic_filesystems());
TRY(SystemServer::reopen_base_file_descriptors());
}
TRY(Core::System::pledge("stdio proc exec tty accept unix rpath wpath cpath chown fattr id sigaction"));
if (!user) {
TRY(create_tmp_coredump_directory());
TRY(set_default_coredump_directory());
TRY(create_tmp_semaphore_directory());
TRY(determine_system_mode());
TRY(SystemServer::create_tmp_coredump_directory());
TRY(SystemServer::set_default_coredump_directory());
TRY(SystemServer::create_tmp_semaphore_directory());
TRY(SystemServer::determine_system_mode());
}
Core::EventLoop event_loop;
@ -585,9 +302,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
event_loop.register_signal(SIGTERM, sigterm_handler);
if (!user) {
TRY(activate_base_services_based_on_system_mode());
TRY(SystemServer::activate_base_services_based_on_system_mode());
} else {
TRY(activate_user_services_based_on_system_mode());
TRY(SystemServer::activate_user_services_based_on_system_mode());
}
return event_loop.exec();