File Coverage

blib/lib/Udev/FFI.pm
Criterion Covered Total %
statement 41 102 40.2
branch 5 30 16.6
condition 0 5 0.0
subroutine 11 18 61.1
pod 0 9 0.0
total 57 164 34.7


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   55460 use strict;
  2         5  
  2         54  
8 2     2   9 use warnings;
  2         5  
  2         52  
9              
10 2     2   555 use Udev::FFI::FFIFunctions;
  2         9  
  2         529  
11 2     2   694 use Udev::FFI::Device;
  2         5  
  2         50  
12 2     2   459 use Udev::FFI::Monitor;
  2         7  
  2         96  
13 2     2   596 use Udev::FFI::Enumerate;
  2         7  
  2         70  
14              
15 2     2   896 use IPC::Cmd qw(can_run run);
  2         100709  
  2         241  
16              
17              
18             $Udev::FFI::VERSION = '0.098000';
19              
20              
21             use constant {
22 2         1365 UDEVADM_LOCATIONS => [
23             '/bin/udevadm'
24             ]
25 2     2   32 };
  2         8  
26              
27              
28              
29             sub udev_version {
30 1     1 0 76 my $full_path = can_run('udevadm');
31              
32 1 50       83005 if(!$full_path) {
33 1         2 for(@{ +UDEVADM_LOCATIONS }) {
  1         5  
34 1 50       6 if(-f) {
35 0         0 $full_path = $_;
36 0         0 last;
37             }
38             }
39             }
40              
41 1 50       4 if(!$full_path) {
42 1         3 $@ = "Can't find udevadm utility";
43 1         51 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 0 3 my $class = shift;
66              
67 1         2 my $self = {};
68              
69 1 50       10 if(0 == Udev::FFI::FFIFunctions->load_lib()) {
70 0         0 $@ = "Can't find udev library";
71 0         0 return undef;
72             }
73              
74 1         65 $self->{_context} = udev_new();
75 1 50       5 if(!defined($self->{_context})) {
76 0         0 $@ = "Can't create udev context";
77 0         0 return undef;
78             }
79              
80              
81 1         4 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   635 my $self = shift;
195              
196 1         66 udev_unref( $self->{_context} );
197             }
198              
199              
200              
201             1;
202              
203              
204              
205             __END__