lib/Ubic/Daemon/OS/Linux.pm

filter: Statementstmt 82.3% 42 / 51 Branchbran 50.0% 6 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 80.0% 60 / 75 SCAR 21.8
CC
8
Cov
76%
pid2guid
5.5
pid2cmd
3.4
close_all_fh
3.0
CRAP 8.9
« blib/lib/Ubic/Run.pm ↑ Summary lib/Ubic/Ping/Service.pm »
1 package Ubic::Daemon::OS::Linux;
2 163 $Ubic::Daemon::OS::Linux::VERSION = '1.59';
3 163 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 163 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: linux-specific daemonize helpers
7  
8  
9 163 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 163 ▶use parent qw(Ubic::Daemon::OS);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub pid2guid {
14 5 ▶ my ($self, $pid) = @_;
Subroutine pid2guid: called CC=4 SCAR=17.0
Pod documented
15  
16 5 ▶ unless (-d "/proc/$pid") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d "/proc/$pid") 0 5 5
17 0 return; # process not found
18 }
19 5 my $opened = open(my $fh, '<', "/proc/$pid/stat");
20 5 ▶ unless ($opened) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opened) 0 5 5
21 # open failed
22 0 my $error = $!;
23 0 ▶ unless (-d "/proc/$pid") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d "/proc/$pid") 0 0 0
24 0 return; # process exited right now
25 }
26 0 die "Open /proc/$pid/stat failed: $!";
27 }
28 5 my $line = <$fh>;
29 # cut first two fields (pid and process name)
30 # since process name can contain spaces, we can't just split line by \s+
31 5 $line =~ s/^\d+\s+\([^)]*\)\s+//;
32  
33 5 my @fields = split /\s+/, $line;
34 5 my $guid = $fields[19];
35 5 return $guid;
36 }
37  
38 sub pid2cmd {
39 5 ▶ my ($self, $pid) = @_;
Subroutine pid2cmd: called CC=3 SCAR=12.2
Pod documented
40  
41 5 my $daemon_cmd_fh;
42 5 ▶ unless (open $daemon_cmd_fh, '<', "/proc/$pid/cmdline") {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $daemon_cmd_fh, "<", "/proc/$pid/cmdline") 0 5 5
43 # this can happen if pid got reused and now it belongs to the kernel process, e.g., [kthreadd]
44 0 warn "Can't open daemon's cmdline: $!";
45 0 return 'unknown';
46 }
47 5 my $daemon_cmd = <$daemon_cmd_fh>;
48 5 ▶ unless ($daemon_cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($daemon_cmd) 0 5 5
49 # strange, open succeeded but file is empty
50 # this can happen, though, for example if pid belongs to the kernel thread
51 0 warn "Can't read daemon cmdline";
52 0 return 'unknown';
53 }
54 5 $daemon_cmd =~ s/\x{00}$//;
55 5 $daemon_cmd =~ s/\x{00}/ /g;
56 5 close $daemon_cmd_fh;
57  
58 5 return $daemon_cmd;
59 }
60  
61 sub close_all_fh {
62 27 ▶ my ($self, @except) = @_;
Subroutine close_all_fh: called CC=3 SCAR=11.0
Pod documented
63  
64 27 my @fd_nums = map { s!^.*/!!; $_ } glob("/proc/$$/fd/*");
65 27 for my $fd (@fd_nums) {
66 223 ▶ next if grep { $_ == $fd } @except;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $fd;} @except 27 196 223
67 196 POSIX::close($fd);
68 }
69 }
70  
71 sub pid_exists {
72 5 ▶ my ($self, $pid) = @_;
Subroutine pid_exists: called CC=1 SCAR=0.0
Pod documented
73 5 return (-d "/proc/$pid");
74 }
75  
76 163 1;
77  
78 __END__
« blib/lib/Ubic/Run.pm ↑ Summary lib/Ubic/Ping/Service.pm »