line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Udev::FFI - Copyright (C) 2017 Ilya Pavlov |
2
|
|
|
|
|
|
|
# Udev::FFI is licensed under the |
3
|
|
|
|
|
|
|
# GNU Lesser General Public License v2.1 |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Udev::FFI; |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
66575
|
use strict; |
|
2
|
|
|
|
|
15
|
|
|
2
|
|
|
|
|
58
|
|
8
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
60
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
636
|
use Udev::FFI::FFIFunctions; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
524
|
|
11
|
2
|
|
|
2
|
|
510
|
use Udev::FFI::Device; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
48
|
|
12
|
2
|
|
|
2
|
|
451
|
use Udev::FFI::Monitor; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
46
|
|
13
|
2
|
|
|
2
|
|
502
|
use Udev::FFI::Enumerate; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
50
|
|
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
2
|
|
757
|
use IPC::Cmd qw(can_run run); |
|
2
|
|
|
|
|
76536
|
|
|
2
|
|
|
|
|
191
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$Udev::FFI::VERSION = '0.098005'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use constant { |
22
|
2
|
|
|
|
|
1654
|
UDEVADM_LOCATIONS => [ |
23
|
|
|
|
|
|
|
'/bin/udevadm' |
24
|
|
|
|
|
|
|
] |
25
|
2
|
|
|
2
|
|
21
|
}; |
|
2
|
|
|
|
|
6
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub udev_version { |
30
|
1
|
|
|
1
|
0
|
96
|
my $full_path = can_run('udevadm'); |
31
|
|
|
|
|
|
|
|
32
|
1
|
50
|
|
|
|
85957
|
if(!$full_path) { |
33
|
1
|
|
|
|
|
2
|
for(@{ +UDEVADM_LOCATIONS }) { |
|
1
|
|
|
|
|
3
|
|
34
|
1
|
50
|
|
|
|
6
|
if(-f) { |
35
|
0
|
|
|
|
|
0
|
$full_path = $_; |
36
|
0
|
|
|
|
|
0
|
last; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
1
|
50
|
|
|
|
2
|
if(!$full_path) { |
42
|
1
|
|
|
|
|
2
|
$@ = "Can't find udevadm utility"; |
43
|
1
|
|
|
|
|
4
|
return undef; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
0
|
my ( $success, $error_message, undef, $stdout_buf, $stderr_buf ) = |
48
|
|
|
|
|
|
|
run( command => [$full_path, '--version'], timeout => 60, verbose => 0 ); |
49
|
|
|
|
|
|
|
|
50
|
0
|
0
|
|
|
|
0
|
if(!$success) { |
51
|
0
|
|
|
|
|
0
|
$@ = $error_message; |
52
|
0
|
|
|
|
|
0
|
return undef; |
53
|
|
|
|
|
|
|
} |
54
|
0
|
0
|
|
|
|
0
|
if($stdout_buf->[0] !~ /^(\d+)\s*$/) { |
55
|
0
|
|
|
|
|
0
|
$@ = "Can't get udev version from udevadm utility"; |
56
|
0
|
|
|
|
|
0
|
return undef; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
0
|
return $1; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub new { |
65
|
1
|
|
|
1
|
1
|
1720
|
my $class = shift; |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
|
|
3
|
my $self = {}; |
68
|
|
|
|
|
|
|
|
69
|
1
|
50
|
|
|
|
7
|
if(0 == Udev::FFI::FFIFunctions->load_lib()) { |
70
|
0
|
|
|
|
|
0
|
$@ = "Can't find udev library"; |
71
|
0
|
|
|
|
|
0
|
return undef; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
81
|
$self->{_context} = udev_new(); |
75
|
1
|
50
|
|
|
|
7
|
if(!defined($self->{_context})) { |
76
|
0
|
|
|
|
|
0
|
$@ = "Can't create udev context"; |
77
|
0
|
|
|
|
|
0
|
return undef; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
1
|
|
|
|
|
3
|
bless $self, $class; |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
6
|
return $self; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub new_device_from_syspath { |
89
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
90
|
0
|
|
|
|
|
0
|
my $syspath = shift; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
0
|
my $device = udev_device_new_from_syspath($self->{_context}, $syspath); |
93
|
0
|
0
|
|
|
|
0
|
if(defined($device)) { |
94
|
0
|
|
|
|
|
0
|
return Udev::FFI::Device->new( $device ); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
0
|
return undef; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub new_device_from_devnum { |
103
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
104
|
0
|
|
|
|
|
0
|
my $type = shift; |
105
|
0
|
|
|
|
|
0
|
my $devnum = shift; |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
my $device = udev_device_new_from_devnum($self->{_context}, ord($type), $devnum); |
108
|
0
|
0
|
|
|
|
0
|
if(defined($device)) { |
109
|
0
|
|
|
|
|
0
|
return Udev::FFI::Device->new( $device ); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
0
|
return undef; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub new_device_from_subsystem_sysname { |
118
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
119
|
0
|
|
|
|
|
0
|
my $subsystem = shift; |
120
|
0
|
|
|
|
|
0
|
my $sysname = shift; |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
0
|
my $device = udev_device_new_from_subsystem_sysname($self->{_context}, $subsystem, $sysname); |
123
|
0
|
0
|
|
|
|
0
|
if(defined($device)) { |
124
|
0
|
|
|
|
|
0
|
return Udev::FFI::Device->new( $device ); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
0
|
return undef; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub new_device_from_device_id { |
133
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
134
|
0
|
|
|
|
|
0
|
my $id = shift; |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
0
|
my $device = udev_device_new_from_device_id($self->{_context}, $id); |
137
|
0
|
0
|
|
|
|
0
|
if(defined($device)) { |
138
|
0
|
|
|
|
|
0
|
return Udev::FFI::Device->new( $device ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
0
|
return undef; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub new_device_from_environment { |
147
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
0
|
my $device = udev_device_new_from_environment($self->{_context}); |
150
|
0
|
0
|
|
|
|
0
|
if(defined($device)) { |
151
|
0
|
|
|
|
|
0
|
return Udev::FFI::Device->new( $device ); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
0
|
return undef; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub new_monitor { |
160
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
161
|
0
|
|
0
|
|
|
0
|
my $source = shift || 'udev'; |
162
|
|
|
|
|
|
|
|
163
|
0
|
0
|
0
|
|
|
0
|
if($source ne 'udev' && $source ne 'kernel') { |
164
|
0
|
|
|
|
|
0
|
$@ = 'Valid sources identifiers are "udev" and "kernel"'; |
165
|
0
|
|
|
|
|
0
|
return undef; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
0
|
my $monitor = udev_monitor_new_from_netlink($self->{_context}, $source); |
169
|
0
|
0
|
|
|
|
0
|
unless(defined($monitor)) { |
170
|
0
|
|
|
|
|
0
|
$@ = "Can't create udev monitor from netlink"; |
171
|
0
|
|
|
|
|
0
|
return undef; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
0
|
return Udev::FFI::Monitor->new($monitor); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub new_enumerate { |
180
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
0
|
my $enumerate = udev_enumerate_new($self->{_context}); |
183
|
0
|
0
|
|
|
|
0
|
unless(defined($enumerate)) { |
184
|
0
|
|
|
|
|
0
|
$@ = "Can't create enumerate context"; |
185
|
0
|
|
|
|
|
0
|
return undef; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
0
|
return Udev::FFI::Enumerate->new($enumerate); |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub DESTROY { |
194
|
1
|
|
|
1
|
|
417
|
my $self = shift; |
195
|
|
|
|
|
|
|
|
196
|
1
|
|
|
|
|
51
|
udev_unref( $self->{_context} ); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
1; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
__END__ |