Utilities: Add the lsdev utility

This utility lists all devices' major number allocations, for character
and block devices. It can help the user to figure out the DeviceMapper
service manages spawning of device nodes in /dev and other associated
files under the /tmp/system/devicemap directory.
This commit is contained in:
Liav A.
2024-05-04 19:34:18 +03:00
committed by Nico Weber
parent 5292bb3fe0
commit aa2d76cd78
3 changed files with 84 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
## Name
lsdev - list devices' major number allocations
## Synopsis
```**sh
$ lsdev
```
## Description
lsdev is a utility for displaying devices' major number allocations, for
character and block devices both. It shows the allocated number and its
family name per allocation.
## Files
* `/sys/kernel/chardev_major_allocs` - list of the major number allocations for character devices.
* `/sys/kernel/blockdev_major_allocs` - list of the major number allocations for block devices.
## Examples
```sh
$ lsdev
```

View File

@@ -7,7 +7,7 @@ list(APPEND REQUIRED_TARGETS
touch tr true umount uname uniq uptime w wc which whoami xargs yes
)
list(APPEND RECOMMENDED_TARGETS
aconv adjtime aplay abench asctl bt checksum chres cksum copy fortune gzip install keymap lsirq lsof lspci lzcat man mkfs.fat mknod mktemp
aconv adjtime aplay abench asctl bt checksum chres cksum copy fortune gzip install keymap lsdev lsirq lsof lspci lzcat man mkfs.fat mknod mktemp
nc netstat notify ntpquery open passwd pixelflut pls printf pro shot strings tar tt unzip wallpaper xzcat zip
)

View File

@@ -0,0 +1,57 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/NumberFormat.h>
#include <AK/StringView.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath"));
TRY(Core::System::unveil("/sys/kernel/chardev_major_allocs", "r"));
TRY(Core::System::unveil("/sys/kernel/blockdev_major_allocs", "r"));
TRY(Core::System::unveil(nullptr, nullptr));
Core::ArgsParser args_parser;
args_parser.set_general_help("List major device number allocations.");
args_parser.parse(arguments);
outln("Character devices:");
{
auto file = TRY(Core::File::open("/sys/kernel/chardev_major_allocs"sv, Core::File::OpenMode::Read));
auto file_contents = TRY(file->read_until_eof());
auto json_result = TRY(JsonValue::from_string(file_contents));
auto const& json = json_result.as_array();
json.for_each([&](auto& value) {
auto& major_number_allocation_object = value.as_object();
auto family_name = major_number_allocation_object.get_byte_string("family_name"sv).value_or({});
auto allocated_number = major_number_allocation_object.get_u64("allocated_number"sv).value_or(0);
outln("{:3d} {}", allocated_number, family_name);
});
}
outln();
outln("Block devices:");
{
auto file = TRY(Core::File::open("/sys/kernel/blockdev_major_allocs"sv, Core::File::OpenMode::Read));
auto file_contents = TRY(file->read_until_eof());
auto json_result = TRY(JsonValue::from_string(file_contents));
auto const& json = json_result.as_array();
json.for_each([&](auto& value) {
auto& major_number_allocation_object = value.as_object();
auto family_name = major_number_allocation_object.get_byte_string("family_name"sv).value_or({});
auto allocated_number = major_number_allocation_object.get_u64("allocated_number"sv).value_or(0);
outln("{:3d} {}", allocated_number, family_name);
});
}
return 0;
}