line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tie::Sysctl; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
42676
|
use base 'Tie::Hash'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1191
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = 0.05; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub TIEHASH { |
8
|
0
|
|
|
0
|
|
|
my $cls = shift; |
9
|
0
|
|
|
|
|
|
my $self = {}; |
10
|
0
|
0
|
|
|
|
|
if ($^O ne 'linux') { warn "try linux instead:)\n";return } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
0
|
|
0
|
|
|
|
$self->{node} = $_[0] || '/'; |
12
|
0
|
|
|
|
|
|
$self->{basedir} = '/proc/sys'; |
13
|
0
|
0
|
|
|
|
|
unless (-d $self->{basedir}) { |
14
|
0
|
|
|
|
|
|
warn "basedir $self->{basedir} not mounted\n"; |
15
|
0
|
|
|
|
|
|
return; |
16
|
|
|
|
|
|
|
} |
17
|
0
|
|
|
|
|
|
bless $self => $cls; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub FETCH { |
21
|
0
|
|
|
0
|
|
|
my $self = shift; |
22
|
0
|
|
|
|
|
|
my $key = shift; |
23
|
0
|
|
|
|
|
|
my @files = grep {$_ eq $key} $self->ls; |
|
0
|
|
|
|
|
|
|
24
|
0
|
0
|
|
|
|
|
unless (defined $files[0]) { |
25
|
0
|
|
|
|
|
|
return; |
26
|
|
|
|
|
|
|
} |
27
|
0
|
|
|
|
|
|
my $t = $self->type($key); |
28
|
0
|
0
|
|
|
|
|
unless (defined $t) { |
29
|
0
|
|
|
|
|
|
return; |
30
|
|
|
|
|
|
|
} |
31
|
0
|
0
|
|
|
|
|
if ($t eq 'file') { |
32
|
0
|
|
|
|
|
|
my $data = $self->rd($key); |
33
|
0
|
|
|
|
|
|
chomp($data); |
34
|
0
|
|
|
|
|
|
return $data; |
35
|
|
|
|
|
|
|
} |
36
|
0
|
|
|
|
|
|
my %h; |
37
|
0
|
|
|
|
|
|
tie %h, __PACKAGE__, $self->{node}.'/'.$key; |
38
|
0
|
|
|
|
|
|
\%h; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub STORE { |
42
|
0
|
|
|
0
|
|
|
my $self = shift; |
43
|
0
|
|
|
|
|
|
my $key = shift; |
44
|
0
|
|
|
|
|
|
my $val = shift; |
45
|
0
|
0
|
|
|
|
|
unless ($self->type($key)eq'file') { return } |
|
0
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
$self->wrt($key,$val); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub type { |
50
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
51
|
0
|
0
|
|
|
|
|
if (-d $self->file(@_)) { |
|
|
0
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
return 'dir'; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
elsif (-f $self->file(@_)) { |
55
|
0
|
|
|
|
|
|
return 'file'; |
56
|
|
|
|
|
|
|
} |
57
|
0
|
|
|
|
|
|
return; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub file { |
61
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
62
|
0
|
|
|
|
|
|
my $f = $self->{basedir}.$self->{node}; |
63
|
0
|
0
|
|
|
|
|
if (@_) { |
64
|
0
|
|
|
|
|
|
map {$f .= '/'.$_} @_; |
|
0
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} |
66
|
0
|
|
|
|
|
|
$f =~ s{//}{/}g; |
67
|
0
|
|
|
|
|
|
$f; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub ls { |
71
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
72
|
0
|
0
|
|
|
|
|
unless ($self->type eq 'dir') { |
73
|
0
|
|
|
|
|
|
return (); |
74
|
|
|
|
|
|
|
} |
75
|
0
|
|
|
|
|
|
opendir(DIR,$self->file); |
76
|
0
|
|
|
|
|
|
my @d = readdir(DIR); |
77
|
0
|
|
|
|
|
|
closedir(DIR); |
78
|
0
|
|
|
|
|
|
shift@d;shift@d; |
|
0
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
@d; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub rd { |
83
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
84
|
0
|
0
|
|
|
|
|
unless ($self->type(@_)eq'file') { return } |
|
0
|
|
|
|
|
|
|
85
|
0
|
0
|
|
|
|
|
open(DUS,$self->file(@_)) or return; |
86
|
0
|
|
|
|
|
|
my $d; |
87
|
0
|
|
|
|
|
|
while () { $d .= $_ } |
|
0
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
close DUS; |
89
|
0
|
|
|
|
|
|
$d; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub wrt { |
93
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
94
|
0
|
|
|
|
|
|
my $data = pop; |
95
|
0
|
|
|
|
|
|
my $f = $self->file(@_); |
96
|
0
|
0
|
|
|
|
|
unless ($self->type(@_)eq'file') { return } |
|
0
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
open(DUS,">".$self->file(@_))or return; |
98
|
0
|
|
|
|
|
|
my $ret = print DUS $data; |
99
|
0
|
|
|
|
|
|
close DUS; |
100
|
0
|
|
|
|
|
|
$ret; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub FIRSTKEY { |
104
|
0
|
|
|
0
|
|
|
my $self = shift; |
105
|
0
|
|
|
|
|
|
$self->{_ls} = [$self->ls]; |
106
|
0
|
|
|
|
|
|
$self->NEXTKEY; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub NEXTKEY { |
110
|
0
|
|
|
0
|
|
|
my $self = shift; |
111
|
0
|
|
|
|
|
|
shift@{$self->{_ls}}; |
|
0
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
115
|
|
|
|
|
|
|
__END__ |