blib/lib/Linux/Fuser.pm

filter: Statementstmt 94.3% 100 / 106 Branchbran 70.8% 17 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 19 / 19 Podpod 100% 2 / 2 totaltotal 89.4% 144 / 161 CC 17 SCAR 13.1
CC
17
Cov
89%
fuser
9.1
new
3.1
AUTOLOAD
3.0
CRAP 3.7
↑ Summary
1 #*****************************************************************************
2 #* *
3 #* Gellyfish Software *
4 #* *
5 #* *
6 #*****************************************************************************
7 #* *
8 #* PROGRAM : Linux::Fuser *
9 #* *
10 #* AUTHOR : JNS *
11 #* *
12 #* DESCRIPTION : Provide an 'fuser' like facility in Perl *
13 #* *
14 #* *
15 #*****************************************************************************
16 #* *
17 #* $Id$
18 #* *
19 #*****************************************************************************
20  
21 package Linux::Fuser;
22  
23 =head1 NAME
24  
25 Linux::Fuser - Determine which processes have a file open
26  
27 =head1 SYNOPSIS
28  
29 use Linux::Fuser;
30  
31 my $fuser = Linux::Fuser->new();
32  
33 my @procs = $fuser->fuser('foo');
34  
35 foreach my $proc ( @procs )
36 {
37 print $proc->pid(),"\t", $proc->user(),"\n",@{$proc->cmd()},"\n";
38 }
39  
40 =head1 DESCRIPTION
41  
42 This module provides information similar to the Unix command 'fuser' about
43 which processes have a particular file open. The way that this works is
44 highly unlikely to work on any other OS other than Linux and even then it
45 may not work on other than 2.2.* kernels. Some features may not work
46 correctly on kernel versions older than 2.6.22
47  
48 It should also be borne in mind that this may not produce entirely accurate
49 results unless you are running the program as the Superuser as the module
50 will require access to files in /proc that may only be readable by their
51 owner.
52  
53 =head2 METHODS
54  
55 =over 4
56  
57 =cut
58  
59 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 2 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
62 $VERSION
63 @ISA
64 2 );
65  
66 2 $VERSION = '1.6';
67  
68 =item new
69  
70 The constructor of the object. It takes no arguments and returns a blessed
71 reference suitable for calling the methods on.
72  
73 =cut
74  
75 sub new
76 2 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod documented
77 2 my ( $proto, @args ) = @_;
78  
79 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
80  
81 2 my $self = {};
82  
83 2 bless $self, $class;
84  
85 2 return $self;
86  
87 }
88  
89 =item fuser SCALAR $file
90  
91 Given the name of a file it will return a list of Linux::Fuser::Procinfo
92 objects, one for each process that has the file open - this will be the
93 empty list if no processes have the file open or undef if the file doesnt
94 exist.
95  
96 =cut
97  
98 sub fuser
99 2 ▶{
Subroutine fuser: called CC=9 SCAR=22.0
Pod documented
100 2 my ( $self, $file, @args ) = @_;
101  
102 2 ▶ return () unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 1 1 2
103  
104 1 my @procinfo = ();
105  
106 1 my ( $dev, $ino, @ostuff ) = stat($file);
107  
108 1 ▶ opendir PROC, '/proc' or die "Can't access /proc - $!\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir PROC, "/proc" or die "Can't access /proc - $!\n" 1 0 1
109  
110 1 my @procs = grep /^\d+$/, readdir PROC;
111  
112 1 closedir PROC;
113  
114 1 foreach my $proc (@procs)
115 {
116 9 ▶ opendir FD, "/proc/$proc/fd" or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir FD, "/proc/$proc/fd" or next 9 0 9
117  
118 9 my @fds = map { ["/proc/$proc/fd/$_",$_] } grep /^\d+$/, readdir FD;
119  
120 9 closedir FD;
121  
122 9 foreach my $fd_data (@fds)
123 {
124 45 my $fd = $fd_data->[0];
125 45 my $fd_no = $fd_data->[1];
126  
127 45 ▶ if ( my @statinfo = stat $fd )
Branch: 1 branch
BranchTrueFalseTotal
if (my(@statinfo) = stat $fd) 45 0 45
128 {
129 45 ▶ if ( ( $dev == $statinfo[0] ) && ( $ino == $statinfo[1] ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($dev == $statinfo[0] and $ino == $statinfo[1]) 1 44 45
Condition100%3/3
$dev == $statinfo[0] and $ino == $statinfo[1]
!ll&&!rl&&r
4041
MC/DC100%2/2
$dev == $statinfo[0] and $ino == $statinfo[1]
$dev == $statinfo[0] $ino == $statinfo[1]
Truth table100%3/3
A and B
A= $dev == $statinfo[0]B= $ino == $statinfo[1]
ABresult
0X0
100
111
130 {
131 1 push @procinfo,Linux::Fuser::Procinfo->new($proc, $fd_data);
132 }
133 }
134 }
135 }
136 1 return @procinfo;
137 }
138  
139 2 1;
140  
141 package Linux::Fuser::Procinfo;
142  
143 =back
144  
145 =head2 PER PROCESS METHODS
146  
147 The fuser() method will return a list of objects of type Linux::Fuser::Procinfo
148 which itself has methods to return information about the process.
149  
150 =over 2
151  
152 =item user
153  
154 The login name of the user that started this process ( or more precisely
155 that owns the file descriptor that the file is open on ).
156  
157 =item pid
158  
159 The process id of the process that has the file open.
160  
161 =item cmd
162  
163 The command line of the program that opened the file. This actually returns
164 a reference to an array containing the individual elements of the command
165 line.
166  
167 =item filedes
168  
169 A Linux::Fuser::FileDescriptor object that has details of the file as
170 the process has it opened - see below.
171  
172 =back
173  
174  
175 =cut
176  
177 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
179  
180 2 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
181  
182 sub new
183 1 ▶{
Subroutine new: called CC=2 SCAR=6.9
184 1 my ( $class, $pid, $fd_data ) = @_;
185  
186 1 my $fd = $fd_data->[0];
187 1 my $fd_no = $fd_data->[1];
188  
189 1 my $user = getpwuid( ( lstat($fd) )[4] );
190  
191 1 my @cmd = ('');
192  
193 1 ▶ if ( open CMD, "/proc/$pid/cmdline" )
Branch: 1 branch
BranchTrueFalseTotal
if (open CMD, "/proc/$pid/cmdline") 1 0 1
194 {
195 1 chomp( @cmd = <CMD> );
196 }
197  
198 1 my $filedes = Linux::Fuser::FileDescriptor->new($pid, $fd_no);
199  
200 1 my $procinfo = {
201 pid => $pid,
202 user => $user,
203 cmd => \@cmd,
204 filedes => $filedes
205 };
206  
207 1 bless $procinfo, $class;
208  
209 1 return $procinfo;
210  
211 }
212  
213 sub AUTOLOAD
214 4 ▶{
Subroutine AUTOLOAD: called CC=3 SCAR=11.1
215 4 my ( $self, @args ) = @_;
216  
217 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
218  
219 4 ( my $method = $AUTOLOAD ) =~ s/.*://;
220  
221 4 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 1 3 4
222  
223 3 ▶ if ( exists $self->{$method} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$method}) { } 3 0 3
224 {
225 3 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
226 3 my ( $self, @args ) = @_;
227 3 return $self->{$method};
228 3 };
229 }
230 else
231 {
232 0 my $pack = ref($self);
233 0 croak "Can't find method $method via package $self";
234 }
235  
236 3 goto &{$AUTOLOAD};
237  
238 }
239  
240 2 1;
241  
242 package Linux::Fuser::FileDescriptor;
243  
244 =head2 Linux::Fuser::FileDescriptor
245  
246 This is returned by the filedes method of the Linux::Fuser::Procinfo and
247 contains the information about the file descriptor that the process has the
248 file open under.
249  
250 The information which this is based on is only available from Linux Kernel
251 version 2.6.22 onwards so will not be available on earlier kernels (except
252 the 'fd'.)
253  
254 It has the following methods (though future versions of the Linux Kernel may
255 provide different or fuller information via /proc/$pid/fdinfo):
256  
257 =over 2
258  
259 =item fd
260  
261 The file descriptor that this file is opened under - this will be unique
262 within a process (if a file is opened more than once by a process) but not
263 within the system.
264  
265 =item flags
266  
267 The flags with which the file was opened (by open or creat) as a long integer.
268  
269 =item pos
270  
271 The location (in bytes) of the file pointer within the file.
272  
273 =back
274  
275 =cut
276  
277 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
278 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
279 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
280  
281 2 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
282  
283 sub new
284 1 ▶{
Subroutine new: called CC=3 SCAR=11.3
285 1 my ( $class, $pid, $fd_no ) = @_;
286  
287  
288 1 my $self = {
289 fd => $fd_no
290 };
291  
292 1 ▶ if ( open FDINFO,'<',"/proc/$pid/fdinfo/$fd_no" )
Branch: 1 branch
BranchTrueFalseTotal
if (open FDINFO, '<', "/proc/$pid/fdinfo/$fd_no") { } 1 0 1
293 {
294 1 ▶ while(my $fd_info = <FDINFO>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fd_info = readline FDINFO)) 4 1 5
295 {
296 4 chomp($fd_info);
297 4 my ($key, $value ) = split /:\s+/, $fd_info;
298 4 $self->{$key} = $value;
299 }
300 }
301 else
302 {
303 0 $self->{'pos'} = undef;
304 0 $self->{'flags'} = undef;
305 }
306  
307 1 return bless $self, $class;
308 }
309  
310 sub AUTOLOAD
311 2 ▶{
Subroutine AUTOLOAD: called CC=3 SCAR=11.1
312 2 my ( $self, @args ) = @_;
313  
314 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
315  
316 2 ( my $method = $AUTOLOAD ) =~ s/.*://;
317  
318 2 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 1 1 2
319  
320 1 ▶ if ( exists $self->{$method} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$method}) { } 1 0 1
321 {
322 1 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
323 1 my ( $self, @args ) = @_;
324 1 return $self->{$method};
325 1 };
326 }
327 else
328 {
329 0 my $pack = ref($self);
330 0 croak "Can\'t find method $method via package $self";
331 }
332  
333 1 goto &{$AUTOLOAD};
334  
335 }
336  
337 2 1;
338 __END__
↑ Summary