-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkmi-gtk2.pl
executable file
·265 lines (233 loc) · 6.05 KB
/
pkmi-gtk2.pl
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Data::Dumper;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/;
# all info will be stored here
my $moduleList = {};
# Init module data in main structure
sub initModuleData {
my ($modname, $modtype) = @_;
$moduleList->{$modname} = {
type => "$modtype",
desc => "",
params => {},
} unless defined $moduleList->{$modname};
}
# Get loaded modules
sub getLoadedModules {
my $fh;
open $fh, "<", "/proc/modules" or $fh = undef;
# Try to use lsmod util
open $fh, "-|", "lsmod" or $fh = undef unless $fh;
return 0 unless $fh;
if ($fh) {
while (my $modname = <$fh>) {
next if $modname =~ m{Module.*Size.*Used}i;
$modname =~ s|^([^ ]+).*$|$1|is;
initModuleData($modname, "loaded");
}
close $fh;
}
}
# Get all from /sys
sub getFromSys {
# if /sys mounted
if ( -d "/sys/module") {
opendir D, "/sys/module";
while (my $modname = readdir D) {
# If directory and not "up-level"
if ( -d "/sys/module/$modname" && $modname !~ m{^\.\.?$} ) {
initModuleData($modname, "built-in");
# if module have params
if ( -d "/sys/module/$modname/parameters" ) {
opendir DP, "/sys/module/$modname/parameters";
while (my $param = readdir DP) {
if ( -f "/sys/module/$modname/parameters/$param" ) {
$moduleList->{$modname}->{params}->{$param} = {
value => "",
desc => "",
};
my $val;
{
#print "Reading /sys/module/$modname/parameters/$param\n";
open PV, "<", "/sys/module/$modname/parameters/$param" or next;
local $/ = undef;
$val = <PV>;
close PV;
}
chomp $val if $val;
$moduleList->{$modname}->{params}->{$param}->{value} = $val;
}
}
closedir DP;
}
}
}
closedir D;
}
}
# Try to load module list
eval {
getLoadedModules();
getFromSys();
# find loaded modules and split it for chunks
my $chunks = [];
my $chunk = [];
foreach my $modname (keys %{$moduleList}) {
if ($moduleList->{$modname}->{type} eq "loaded") {
push @$chunk, $modname;
if (@$chunk == 10) {
push @$chunks, $chunk;
$chunk = [];
}
}
}
push @$chunks, $chunk if @$chunk > 0;
# load module info
foreach my $chunk (@{$chunks}) {
#my $modname;
# Run modinfo
open F, "-|", "modinfo " . join(" ", @$chunk);
my $modinfoDataLine;
{
$/ = undef;
$modinfoDataLine = <F>;
}
my @modinfoData = split /filename: /is, $modinfoDataLine;
foreach my $dataLine (@modinfoData) {
# Try to find module name
if ($dataLine =~ m{name: +(.*?)(\n|$)}is) {
my $modname = $1;
# Something wrong
if (!defined($moduleList->{$modname})) {
next;
}
# Parse other data
foreach my $line (split /\n/, $dataLine) {
if ($line =~ m|^([^:]+): +(.*)$|is) {
my $key = $1;
my $val = $2;
chomp($val);
if ($key eq "description") {
$moduleList->{$modname}->{desc} = $val;
}
elsif ($key eq "parm") {
if ($val =~ m|^([^:]+):(.*)$|is) {
$moduleList->{$modname}->{params}->{$1}->{desc} = $2;
}
}
}
}
}
}
close F;
}
#print Dumper($moduleList);
};
if (my $error = $@) {
print "Can't load module list!\n";
exit(1);
}
#print Dumper($moduleList);
#exit();
# Build UI
my $modelMod = Gtk2::ListStore->new(
'Glib::String',
'Glib::String',
'Glib::String',
);
foreach my $modName (sort keys(%$moduleList)) {
my $iter = $modelMod->append();
$modelMod->set(
$iter,
0, $modName,
1, $moduleList->{$modName}->{type},
2, $moduleList->{$modName}->{desc}
);
}
my $modelParam = Gtk2::ListStore->new(
'Glib::String',
'Glib::String',
'Glib::String',
);
my $MainWindow = Gtk2::Window->new();
$MainWindow->set_title("Perl Kernel Module Info");
my $paned = Gtk2::HPaned->new;
my $listMod = Gtk2::TreeView->new($modelMod);
my $listParam = Gtk2::TreeView->new($modelParam);
foreach (['Module', 0], ['Type', 1], ['Description', 2]) {
$listMod->append_column(Gtk2::TreeViewColumn->new_with_attributes(
$_->[0],
Gtk2::CellRendererText->new,
text => $_->[1]
));
}
foreach (['Param', 0], ['Value', 1], ['Description', 2]) {
$listParam->append_column(Gtk2::TreeViewColumn->new_with_attributes(
$_->[0],
Gtk2::CellRendererText->new,
text => $_->[1]
));
}
my $scroll1 = Gtk2::ScrolledWindow->new;
$scroll1->set_size_request(360, -1);
$scroll1->add($listMod);
$paned->pack1($scroll1, 1, 0);
my $scroll2 = Gtk2::ScrolledWindow->new;
$scroll2->set_size_request(360, -1);
$scroll2->add($listParam);
$paned->pack2($scroll2, 1, 0);
# Create main menu
my $mainMenu = Gtk2::MenuBar->new;
my $menuFile = Gtk2::MenuItem->new('_File');
my $subMenuFile = Gtk2::Menu->new;
my $menuExit = Gtk2::MenuItem->new('Exit');
$menuExit->signal_connect(activate => sub {Gtk2->main_quit});
$subMenuFile->append($menuExit);
$menuFile->set_submenu($subMenuFile);
$mainMenu->append($menuFile);
my $menuHelp = Gtk2::MenuItem->new('_Help');
my $subMenuHelp = Gtk2::Menu->new;
my $menuAbout = Gtk2::MenuItem->new('About');
$menuAbout->signal_connect(activate => sub {
my $dialog = Gtk2::MessageDialog->new(
$MainWindow,
'modal',
'info',
'close',
'With this tool you can get info about loaded kernel modules'
);
$dialog->run;
$dialog->destroy;
});
$subMenuHelp->append($menuAbout);
$menuHelp->set_submenu($subMenuHelp);
$mainMenu->append($menuHelp);
# Pack UI
my $vbox = Gtk2::VBox->new;
$vbox->pack_start($mainMenu, 0, 0, 0);
$vbox->add($paned);
$MainWindow->add($vbox);
$MainWindow->set_default_size(720, 400);
$MainWindow->signal_connect(destroy => sub {Gtk2->main_quit});
$listMod->get_selection->signal_connect (changed => \&selectModule);
$MainWindow->show_all;
Gtk2->main;
sub selectModule {
my $selection = $listMod->get_selection();
my ($modelM, $iterM) = $selection->get_selected();
my $modName = $modelM->get_value($iterM, 0);
$modelParam->clear();
foreach (sort keys %{$moduleList->{$modName}->{params}}) {
my $iter = $modelParam->append();
$modelParam->set(
$iter,
0, $_,
1, $moduleList->{$modName}->{params}->{$_}->{value},
2, $moduleList->{$modName}->{params}->{$_}->{desc}
);
}
}