| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Ubic::Daemon::OS::Linux; | 
| 2 |  |  |  |  |  |  | $Ubic::Daemon::OS::Linux::VERSION = '1.60'; | 
| 3 | 33 |  |  | 33 |  | 110 | use strict; | 
|  | 33 |  |  |  |  | 40 |  | 
|  | 33 |  |  |  |  | 747 |  | 
| 4 | 33 |  |  | 33 |  | 94 | use warnings; | 
|  | 33 |  |  |  |  | 38 |  | 
|  | 33 |  |  |  |  | 674 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | # ABSTRACT: linux-specific daemonize helpers | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 33 |  |  | 33 |  | 102 | use POSIX; | 
|  | 33 |  |  |  |  | 34 |  | 
|  | 33 |  |  |  |  | 154 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 33 |  |  | 33 |  | 52318 | use parent qw(Ubic::Daemon::OS); | 
|  | 33 |  |  |  |  | 38 |  | 
|  | 33 |  |  |  |  | 184 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub pid2guid { | 
| 14 | 5 |  |  | 5 | 1 | 20 | my ($self, $pid) = @_; | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 5 | 50 |  |  |  | 85 | unless (-d "/proc/$pid") { | 
| 17 | 0 |  |  |  |  | 0 | return; # process not found | 
| 18 |  |  |  |  |  |  | } | 
| 19 | 5 |  |  |  |  | 95 | my $opened = open(my $fh, '<', "/proc/$pid/stat"); | 
| 20 | 5 | 50 |  |  |  | 15 | unless ($opened) { | 
| 21 |  |  |  |  |  |  | # open failed | 
| 22 | 0 |  |  |  |  | 0 | my $error = $!; | 
| 23 | 0 | 0 |  |  |  | 0 | unless (-d "/proc/$pid") { | 
| 24 | 0 |  |  |  |  | 0 | return; # process exited right now | 
| 25 |  |  |  |  |  |  | } | 
| 26 | 0 |  |  |  |  | 0 | die "Open /proc/$pid/stat failed: $!"; | 
| 27 |  |  |  |  |  |  | } | 
| 28 | 5 |  |  |  |  | 165 | 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 |  |  |  |  | 50 | $line =~ s/^\d+\s+\([^)]*\)\s+//; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 5 |  |  |  |  | 110 | my @fields = split /\s+/, $line; | 
| 34 | 5 |  |  |  |  | 10 | my $guid = $fields[19]; | 
| 35 | 5 |  |  |  |  | 45 | return $guid; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub pid2cmd { | 
| 39 | 5 |  |  | 5 | 1 | 10 | my ($self, $pid) = @_; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 5 |  |  |  |  | 5 | my $daemon_cmd_fh; | 
| 42 | 5 | 50 |  |  |  | 140 | unless (open $daemon_cmd_fh, '<', "/proc/$pid/cmdline") { | 
| 43 |  |  |  |  |  |  | # this can happen if pid got reused and now it belongs to the kernel process, e.g., [kthreadd] | 
| 44 | 0 |  |  |  |  | 0 | warn "Can't open daemon's cmdline: $!"; | 
| 45 | 0 |  |  |  |  | 0 | return 'unknown'; | 
| 46 |  |  |  |  |  |  | } | 
| 47 | 5 |  |  |  |  | 70 | my $daemon_cmd = <$daemon_cmd_fh>; | 
| 48 | 5 | 50 |  |  |  | 25 | unless ($daemon_cmd) { | 
| 49 |  |  |  |  |  |  | # strange, open succeeded but file is empty | 
| 50 |  |  |  |  |  |  | # this can happen, though, for example if pid belongs to the kernel thread | 
| 51 | 0 |  |  |  |  | 0 | warn "Can't read daemon cmdline"; | 
| 52 | 0 |  |  |  |  | 0 | return 'unknown'; | 
| 53 |  |  |  |  |  |  | } | 
| 54 | 5 |  |  |  |  | 35 | $daemon_cmd =~ s/\x{00}$//; | 
| 55 | 5 |  |  |  |  | 25 | $daemon_cmd =~ s/\x{00}/ /g; | 
| 56 | 5 |  |  |  |  | 25 | close $daemon_cmd_fh; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 5 |  |  |  |  | 35 | return $daemon_cmd; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub close_all_fh { | 
| 62 | 15 |  |  | 15 | 1 | 148 | my ($self, @except) = @_; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 15 |  |  |  |  | 3439 | my @fd_nums = map { s!^.*/!!; $_ } glob("/proc/$$/fd/*"); | 
|  | 107 |  |  |  |  | 709 |  | 
|  | 107 |  |  |  |  | 286 |  | 
| 65 | 15 |  |  |  |  | 187 | for my $fd (@fd_nums) { | 
| 66 | 107 | 100 |  |  |  | 164 | next if grep { $_ == $fd } @except; | 
|  | 107 |  |  |  |  | 638 |  | 
| 67 | 92 |  |  |  |  | 432 | POSIX::close($fd); | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub pid_exists { | 
| 72 | 5 |  |  | 5 | 1 | 15 | my ($self, $pid) = @_; | 
| 73 | 5 |  |  |  |  | 105 | return (-d "/proc/$pid"); | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | 1; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | __END__ |