| 1 | package Ubic::Daemon::OS::POSIX; | ||||||||||
| 2 | 1 | $Ubic::Daemon::OS::POSIX::VERSION = '1.59'; | |||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # ABSTRACT: POSIX-compatible daemonize helpers | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Params::Validate qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use POSIX qw(:unistd_h); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use parent qw(Ubic::Daemon::OS); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | sub pid2guid { | ||||||||||
| 14 | 0 | ▶ | my ($self, $pid) = validate_pos(@_, 1, { type => SCALAR, regex => qr/^\d+$/ }); | ||||||||
|
Subroutine
pid2guid: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | return unless $self->pid_exists($pid); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | |||||||||||
| 18 | 0 | return 'NULL'; # no guid on mac os x yet | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | sub pid2cmd { | ||||||||||
| 22 | 0 | ▶ | my ($self, $pid) = validate_pos(@_, 1, { type => SCALAR, regex => qr/^\d+$/ }); | ||||||||
|
Subroutine
pid2cmd: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 23 | |||||||||||
| 24 | # see POSIX specification - http://pubs.opengroup.org/onlinepubs/009696799/utilities/ps.html | ||||||||||
| 25 | 0 | my $result = qx(ps -p $pid -o pid,comm 2>/dev/null); | |||||||||
| 26 | 0 | $result =~ s/^.*\n//; # drop first line | |||||||||
| 27 | 0 | my ($ps_pid, $ps_command) = $result =~ /^\s*(\d+)\s+(.*)$/; | |||||||||
| 28 | 0 | ▶ | unless ($ps_pid) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | warn "Daemon $pid not found"; | |||||||||
| 30 | 0 | return 'unknown'; | |||||||||
| 31 | } | ||||||||||
| 32 | 0 | ▶ | unless ($ps_pid == $pid) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | die "Internal error, expected pid $pid, got pid $ps_pid"; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 0 | return $ps_command; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | sub close_all_fh { | ||||||||||
| 40 | 0 | ▶ | my ($self, @except) = @_; | ||||||||
|
Subroutine
close_all_fh: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 41 | |||||||||||
| 42 | 0 | for my $fd (0 .. POSIX::sysconf(POSIX::_SC_OPEN_MAX)) { | |||||||||
| 43 | 0 | ▶ | next if grep { $_ == $fd } @except; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | POSIX::close($fd); | |||||||||
| 45 | } | ||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | sub pid_exists { | ||||||||||
| 49 | 0 | ▶ | my ($self, $pid) = validate_pos(@_, 1, { type => SCALAR, regex => qr/^\d+$/ }); | ||||||||
|
Subroutine
pid_exists: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 50 | 0 | my $result = qx(ps -p $pid -o pid 2>/dev/null); | |||||||||
| 51 | 0 | ▶ | if ($result =~ /\d/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | return 1; | |||||||||
| 53 | } | ||||||||||
| 54 | 0 | return; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 1 | 1; | |||||||||
| 58 | |||||||||||
| 59 | __END__ | ||||||||||