| 1 | package JIP::Daemon; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use JIP::ClassField 0.05; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '0.041'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | my $maybe_set_subname = sub { $ARG[1]; }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | # Supported on Perl 5.22+ | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | require Sub::Util; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | if (my $set_subname = Sub::Util->can('set_subname')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | $maybe_set_subname = $set_subname; | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 18 | ▶ | my $default_log_callback = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_log_callback: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 18 | my ($self, @params) = @ARG; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 18 | ▶ | if (defined(my $logger = $self->logger)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 10 | my $msg; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 10 | ▶ | if (@params == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 5 | $msg = shift @params; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | elsif (@params) { | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | my $format = shift @params; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 4 | $msg = sprintf $format, @params; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 10 | ▶ | $logger->info($msg) if defined $msg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | has [qw( | |||||||||||||||||||||||||||||||||||||||||
| 44 | pid | ||||||||||||||||||||||||||||||||||||||||||
| 45 | uid | ||||||||||||||||||||||||||||||||||||||||||
| 46 | gid | ||||||||||||||||||||||||||||||||||||||||||
| 47 | cwd | ||||||||||||||||||||||||||||||||||||||||||
| 48 | umask | ||||||||||||||||||||||||||||||||||||||||||
| 49 | logger | ||||||||||||||||||||||||||||||||||||||||||
| 50 | dry_run | ||||||||||||||||||||||||||||||||||||||||||
| 51 | is_detached | ||||||||||||||||||||||||||||||||||||||||||
| 52 | log_callback | ||||||||||||||||||||||||||||||||||||||||||
| 53 | on_fork_callback | ||||||||||||||||||||||||||||||||||||||||||
| 54 | stdout | ||||||||||||||||||||||||||||||||||||||||||
| 55 | stderr | ||||||||||||||||||||||||||||||||||||||||||
| 56 | program_name | ||||||||||||||||||||||||||||||||||||||||||
| 57 | )] => (get => q{+}, set => q{-}); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | has devnull => (get => q{+}, set => q{-}, default => File::Spec->devnull); | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 45 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=34 SCAR=35.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 45 | my ($class, %param) = @ARG; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # Perform a trial run with no changes made (foreground if dry_run) | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 45 | ▶ | my $dry_run = (exists $param{'dry_run'} and $param{'dry_run'}) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $param{"dry_run"} and $param{"dry_run"}
MC/DC50%1/2
exists $param{"dry_run"} and $param{"dry_run"}
exists $param{"dry_run"}
$param{"dry_run"}
Truth table66%2/3
A and B
A= exists $param{"dry_run"}B= $param{"dry_run"}
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 45 | my $uid; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 45 | ▶ | if (exists $param{'uid'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 4 | $uid = $param{'uid'}; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | croak q{Bad argument "uid"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $uid and $uid =~ /^\d+$/x
MC/DC100%2/2
defined $uid and $uid =~ /^\d+$/x
defined $uid
$uid =~ /^\d+$/x
Truth table100%3/3
A and B
A= defined $uidB= $uid =~ /^\d+$/x
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | unless defined $uid and $uid =~ m{^\d+$}x; | ||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 43 | my $gid; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 43 | ▶ | if (exists $param{'gid'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 4 | $gid = $param{'gid'}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | ▶ | croak q{Bad argument "gid"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $gid and $gid =~ /^\d+$/x
MC/DC100%2/2
defined $gid and $gid =~ /^\d+$/x
defined $gid
$gid =~ /^\d+$/x
Truth table100%3/3
A and B
A= defined $gidB= $gid =~ /^\d+$/x
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | unless defined $gid and $gid =~ m{^\d+$}x; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 41 | my $cwd; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 41 | ▶ | if (exists $param{'cwd'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | $cwd = $param{'cwd'}; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | croak q{Bad argument "cwd"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $cwd and length $cwd
MC/DC100%2/2
defined $cwd and length $cwd
defined $cwd
length $cwd
Truth table100%3/3
A and B
A= defined $cwdB= length $cwd
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | unless defined $cwd and length $cwd; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 39 | my $umask; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 39 | ▶ | if (exists $param{'umask'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | $umask = $param{'umask'}; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | ▶ | croak q{Bad argument "umask"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $umask and length $umask
MC/DC100%2/2
defined $umask and length $umask
defined $umask
length $umask
Truth table100%3/3
A and B
A= defined $umaskB= length $umask
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | unless defined $umask and length $umask; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 37 | my $logger; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 37 | ▶ | if (exists $param{'logger'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | $logger = $param{'logger'}; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | croak q{Bad argument "logger"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $logger and ref $logger and $logger->can("info")
defined $logger and ref $logger
MC/DC66%2/3
defined $logger and ref $logger and $logger->can("info")
defined $logger
ref $logger
$logger->can("info")
Truth table75%3/4
A and B and C
A= defined $loggerB= ref $loggerC= $logger->can("info")
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | unless defined $logger and ref $logger and $logger->can('info'); | ||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 35 | my $log_callback; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 35 | ▶ | if (exists $param{'log_callback'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 6 | $log_callback = $param{'log_callback'}; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 6 | ▶ | croak q{Bad argument "log_callback"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $log_callback and ref $log_callback eq "CODE"
MC/DC100%2/2
defined $log_callback and ref $log_callback eq "CODE"
defined $log_callback
ref $log_callback eq "CODE"
Truth table100%3/3
A and B
A= defined $log_callbackB= ref $log_callback eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | unless defined $log_callback and ref($log_callback) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | $log_callback = $maybe_set_subname->('custom_log_callback', $log_callback); | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 29 | $log_callback = $maybe_set_subname->('default_log_callback', $default_log_callback); | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 33 | my $on_fork_callback; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 33 | ▶ | if (exists $param{'on_fork_callback'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 3 | $on_fork_callback = $param{'on_fork_callback'}; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | croak q{Bad argument "on_fork_callback"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $on_fork_callback and ref $on_fork_callback eq "CODE"
MC/DC100%2/2
defined $on_fork_callback and ref $on_fork_callback eq "CODE"
defined $on_fork_callback
ref $on_fork_callback eq "CODE"
Truth table100%3/3
A and B
A= defined $on_fork_callbackB= ref $on_fork_callback eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | unless defined $on_fork_callback and ref($on_fork_callback) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | $on_fork_callback = $maybe_set_subname->('on_fork_callback', $on_fork_callback); | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 31 | my $stdout; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 31 | ▶ | if (exists $param{'stdout'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 2 | $stdout = $param{'stdout'}; | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | croak q{Bad argument "stdout"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $stdout and length $stdout
MC/DC0%0/2
defined $stdout and length $stdout
defined $stdout
length $stdout
Truth table66%2/3
A and B
A= defined $stdoutB= length $stdout
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | unless defined $stdout and length $stdout; | ||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 29 | my $stderr; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 29 | ▶ | if (exists $param{'stderr'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 2 | $stderr = $param{'stderr'}; | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | ▶ | croak q{Bad argument "stderr"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $stderr and length $stderr
MC/DC0%0/2
defined $stderr and length $stderr
defined $stderr
length $stderr
Truth table66%2/3
A and B
A= defined $stderrB= length $stderr
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | unless defined $stderr and length $stderr; | ||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 27 | my $program_name = $PROGRAM_NAME; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 27 | ▶ | if (exists $param{'program_name'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 4 | $program_name = $param{'program_name'}; | |||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 4 | ▶ | croak q{Bad argument "program_name"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $program_name and length $program_name
MC/DC100%2/2
defined $program_name and length $program_name
defined $program_name
length $program_name
Truth table100%3/3
A and B
A= defined $program_nameB= length $program_name
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | unless defined $program_name and length $program_name; | ||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 25 | return bless({}, $class) | |||||||||||||||||||||||||||||||||||||||||
| 155 | ->_set_dry_run($dry_run) | ||||||||||||||||||||||||||||||||||||||||||
| 156 | ->_set_uid($uid) | ||||||||||||||||||||||||||||||||||||||||||
| 157 | ->_set_gid($gid) | ||||||||||||||||||||||||||||||||||||||||||
| 158 | ->_set_cwd($cwd) | ||||||||||||||||||||||||||||||||||||||||||
| 159 | ->_set_umask($umask) | ||||||||||||||||||||||||||||||||||||||||||
| 160 | ->_set_logger($logger) | ||||||||||||||||||||||||||||||||||||||||||
| 161 | ->_set_log_callback($log_callback) | ||||||||||||||||||||||||||||||||||||||||||
| 162 | ->_set_on_fork_callback($on_fork_callback) | ||||||||||||||||||||||||||||||||||||||||||
| 163 | ->_set_pid($PROCESS_ID) | ||||||||||||||||||||||||||||||||||||||||||
| 164 | ->_set_is_detached(0) | ||||||||||||||||||||||||||||||||||||||||||
| 165 | ->_set_stdout($stdout) | ||||||||||||||||||||||||||||||||||||||||||
| 166 | ->_set_stderr($stderr) | ||||||||||||||||||||||||||||||||||||||||||
| 167 | ->_set_program_name($program_name) | ||||||||||||||||||||||||||||||||||||||||||
| 168 | ->_set_devnull; | ||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 9 | ▶ | sub daemonize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
daemonize: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 9 | ▶ | return $self if $self->is_detached; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | # Fork and kill parent | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 7 | ▶ | if (not $self->dry_run) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 5 | $self->_log('Daemonizing the process'); | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 5 | my $pid = POSIX::fork(); # returns child pid to the parent and 0 to the child | |||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 5 | ▶ | croak q{Can't fork} if not defined $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | # fork returned 0, so this branch is the child | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 4 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 2 | ▶ | POSIX::setsid() | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | or croak(sprintf q{Can't start a new session: %s}, $OS_ERROR); | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 1 | $self->reopen_std; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 1 | $self->change_program_name; | |||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 1 | $self->_set_pid(POSIX::getpid())->_set_is_detached(1); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | # this branch is the parent | ||||||||||||||||||||||||||||||||||||||||||
| 196 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 2 | $self->_log('Spawned process pid=%d. Parent exiting', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 198 | 2 | $self->_set_pid($pid)->_set_is_detached(1); | |||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 2 | ▶ | if (defined(my $cb = $self->on_fork_callback)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | $cb->($self); | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 2 | POSIX::exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 2 | $self->_set_pid($PROCESS_ID); | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 5 | return $self->drop_privileges; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 1 | ▶ | sub reopen_std { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reopen_std: called CC=6 SCAR=20.1
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | my $stdin = q{<}. $self->devnull; | |||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 1 | my $stdout; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 1 | ▶ | if (defined $self->stdout) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $stdout = $self->stdout; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | $self->_log('Reopen STDOUT to: %s', $stdout); | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | $stdout = q{+>}. $self->devnull; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 1 | my $stderr; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 1 | ▶ | if (defined $self->stderr) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | $stderr = $self->stderr; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | $self->_log('Reopen STDERR to: %s', $stderr); | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | $stderr = q{+>}. $self->devnull; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | open STDIN, $stdin or croak(sprintf q{Can't reopen STDIN: %s}, $OS_ERROR); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 1 | ▶ | open STDOUT, $stdout or croak(sprintf q{Can't reopen STDOUT: %s}, $OS_ERROR); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 1 | ▶ | open STDERR, $stderr or croak(sprintf q{Can't reopen STDERR: %s}, $OS_ERROR); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 9 | ▶ | sub drop_privileges { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
drop_privileges: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 9 | ▶ | if (defined(my $uid = $self->uid)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 2 | $self->_log('Set uid=%d', $uid); | |||||||||||||||||||||||||||||||||||||||||
| 249 | 2 | ▶ | POSIX::setuid($uid) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | or croak(sprintf q{Can't set uid "%s": %s}, $uid, $OS_ERROR); | ||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 8 | ▶ | if (defined(my $gid = $self->gid)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | $self->_log('Set gid=%d', $gid); | |||||||||||||||||||||||||||||||||||||||||
| 255 | 2 | ▶ | POSIX::setgid($gid) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | or croak(sprintf q{Can't set gid "%s": %s}, $gid, $OS_ERROR); | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 7 | ▶ | if (defined(my $umask = $self->umask)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 2 | $self->_log('Set umask=%s', $umask); | |||||||||||||||||||||||||||||||||||||||||
| 261 | 2 | ▶ | POSIX::umask($umask) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | or croak(sprintf q{Can't set umask "%s": %s}, $umask, $OS_ERROR); | ||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 6 | ▶ | if (defined(my $cwd = $self->cwd)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 2 | $self->_log('Set cwd=%s', $cwd); | |||||||||||||||||||||||||||||||||||||||||
| 267 | 2 | ▶ | POSIX::chdir($cwd) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | or croak(sprintf q{Can't chdir to "%s": %s}, $cwd, $OS_ERROR); | ||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 5 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 3 | ▶ | sub try_kill { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
try_kill: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 3 | my ($self, $signal) = @ARG; | |||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 3 | ▶ | if (defined(my $pid = $self->pid)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | # parameter order in POSIX.pm | ||||||||||||||||||||||||||||||||||||||||||
| 279 | # CORE::kill($signal, $pid); | ||||||||||||||||||||||||||||||||||||||||||
| 280 | # POSIX::kill($pid, $signal); | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 2 | ▶ | return POSIX::kill($pid, defined $signal ? $signal : q{0}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | carp q{No subprocess running}; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | ▶ | sub status { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
status: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 1 | my $pid = $self->pid; | |||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | ▶ | return $pid, POSIX::kill($pid, 0) ? 1 : 0, $self->is_detached; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 3 | ▶ | sub change_program_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
change_program_name: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | 3 | my $old_program_name = $PROGRAM_NAME; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 3 | my $new_program_name = $self->program_name; | |||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 3 | ▶ | if ($new_program_name ne $old_program_name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 2 | $self->_log( | |||||||||||||||||||||||||||||||||||||||||
| 304 | 'The program name changed from %s to %s', | ||||||||||||||||||||||||||||||||||||||||||
| 305 | $old_program_name, | ||||||||||||||||||||||||||||||||||||||||||
| 306 | $new_program_name, | ||||||||||||||||||||||||||||||||||||||||||
| 307 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 2 | $PROGRAM_NAME = $new_program_name; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | # private methods | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 22 | ▶ | sub _log { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_log: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 22 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | 22 | $self->log_callback->($self, @ARG); | |||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 22 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | __END__ | ||||||||||||||||||||||||||||||||||||||||||