forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsdev.cpp
57 lines (51 loc) · 2.22 KB
/
lsdev.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Copyright (c) 2022, Liav A. <[email protected]>
*
* 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;
}