lib/Ubic/Daemon.pm

filter: Statementstmt 61.9% 194 / 313 Branchbran 46.5% 82 / 176 Conditioncond 45.4% 10 / 22 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 78.5% 22 / 28 Podpod 100% 3 / 3 totaltotal 56.2% 312 / 555 SCAR 69.0
CC
102
Cov
56%
start_daemon
668.0
_log_exit_code
42.0
check_daemon
14.9
CRAP 990.1
« blib/lib/Ubic/Credentials/OS/MacOSX.pm ↑ Summary lib/Ubic/Credentials/OS/MacOSX.pm »
1 package Ubic::Daemon;
2 163 $Ubic::Daemon::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: daemon management utilities
7  
8  
9 163 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 163 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 163 ▶use POSIX qw(setsid :sys_wait_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 163 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 163 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 163 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 163 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 163 ▶use Ubic::Lockf;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 163 ▶use Ubic::AccessGuard;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 163 ▶use Ubic::Daemon::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 163 ▶use Ubic::Daemon::PidState;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 163 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 163 our @EXPORT_OK = qw(start_daemon stop_daemon check_daemon);
24 163 our %EXPORT_TAGS = (
25 all => \@EXPORT_OK,
26 );
27  
28 163 our $OS;
29 sub import {
30 373 ▶ my %module = (
Subroutine import: called CC=4 SCAR=15.5
31 linux => 'Linux',
32 );
33  
34 # UBIC_DAEMON_OS support is here only for tests
35 373 ▶ my $module = $ENV{UBIC_DAEMON_OS} || $module{$^O} || 'POSIX';
Condition40%2/5
$ENV{'UBIC_DAEMON_OS'} || $module{$^O} || 'POSIX'
l!l
3730
$ENV{"UBIC_DAEMON_OS"} or $module{$^O}
l!l&&r!l&&!r
03730
MC/DC0%0/2
$ENV{'UBIC_DAEMON_OS'} || $module{$^O} || 'POSIX'
$ENV{"UBIC_DAEMON_OS"} $module{$^O}
Truth table33%1/3
A or B
A= $ENV{"UBIC_DAEMON_OS"}B= $module{$^O}
ABresult
1X1
011
000
36  
37 373 require "Ubic/Daemon/OS/$module.pm";
38 373 $OS = eval "Ubic::Daemon::OS::$module->new";
39 373 ▶ unless ($OS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Ubic::Daemon::OS) 0 373 373
40 0 die "failed to initialize OS-specific module $module: $@";
41 }
42 373 __PACKAGE__->export_to_level(1, @_);
43 }
44  
45 {
46 163 my @signame;
47 sub _signame {
48 0 ▶ my $signum = shift;
Subroutine _signame: not called CC=2 SCAR=17.9
49 0 ▶ unless (@signame) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@signame) 0 0 0
50 0 @signame = split /\s+/, $Config{sig_name};
51 }
52 0 return $signame[$signum];
53  
54 }
55 }
56  
57 163 sub _log {
58 87 ▶ my $fh = shift;
Subroutine _log: called CC=2 SCAR=6.9
59 87 ▶ return unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 48 39 87
60 39 print {$fh} '[', scalar(localtime), "]\t$$\t", @_, "\n";
61 }
62  
63 sub _log_exit_code {
64 0 ▶ my ($fh, $code, $pid) = @_;
Subroutine _log_exit_code: not called CC=6 SCAR=37.4
65 0 ▶ if ($code == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 0) 0 0 0
66 0 _log($fh, "daemon $pid exited");
67 0 return;
68 }
69  
70 0 my $msg = "daemon $pid failed with \$? = $?";
71 0 ▶ if (my $signal = $? & 127) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $signal = $? & 127) { } 0 0 0
elsif ($? & 128) { } 0 0 0
elsif (my $code = $? >> 8) { } 0 0 0
72 0 my $signame = _signame($signal);
73 0 ▶ if (defined $signame) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $signame) { } 0 0 0
74 0 $msg = "daemon $pid failed with signal $signame ($signal)";
75 }
76 else {
77 0 $msg = "daemon $pid failed with signal $signal";
78 }
79 }
80 elsif ($? & 128) {
81 0 $msg = "daemon $pid failed, core dumped";
82 }
83 elsif (my $code = $? >> 8) {
84 0 $msg = "daemon $pid failed, exit code $code";
85 }
86 0 _log($fh, $msg);
87 }
88  
89 sub stop_daemon($;@) {
90 98 ▶ my ($pidfile, @tail) = validate_pos(@_, { type => SCALAR }, 0);
Subroutine stop_daemon: called CC=9 SCAR=22.4
Pod documented
91 95 my $options = validate(@tail, {
92 timeout => { default => 30, regex => qr/^\d+$/ },
93 });
94 92 ▶ my $timeout = $options->{timeout} if defined $options->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"timeout"} 92 0 92
95  
96 # TODO - move this check into Ubic::Daemon::PidState
97 92 my $pid_state = Ubic::Daemon::PidState->new($pidfile);
98 92 ▶ return 'not running' if $pid_state->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $pid_state->is_empty 14 78 92
99  
100 78 my $piddata = $pid_state->read;
101 78 ▶ unless ($piddata) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($piddata) 0 78 78
102 0 return 'not running';
103 }
104 78 my $pid = $piddata->{pid};
105  
106 78 ▶ unless (check_daemon($pidfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (check_daemon($pidfile)) 0 78 78
107 0 return 'not running';
108 }
109 78 kill 15 => $pid;
110 78 my $trial = 1;
111 {
112 78 my $sleep = 0.1;
113 78 my $total_sleep = 0;
114 78 while (1) {
115 206 ▶ unless (check_daemon($pidfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (check_daemon($pidfile)) 75 131 206
116 75 return 'stopped';
117 }
118 131 ▶ last if $total_sleep >= $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $total_sleep >= $timeout 3 128 131
119 128 sleep($sleep);
120 128 $total_sleep += $sleep;
121 128 ▶ $sleep += 0.1 * $trial if $sleep < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $sleep < 1 115 13 128
122 128 $trial++;
123 }
124 }
125 3 ▶ unless (check_daemon($pidfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (check_daemon($pidfile)) 0 3 3
126 0 return 'stopped';
127 }
128 3 die "failed to stop daemon with pidfile '$pidfile' (pid $pid, timeout $timeout, trials $trial)";
129 }
130  
131 sub start_daemon($) {
132 193 ▶ my %options = validate(@_, {
Subroutine start_daemon: called CC=65 SCAR=65.0
Pod documented
133 bin => { type => SCALAR | ARRAYREF, optional => 1 },
134 function => { type => CODEREF, optional => 1 },
135 name => { type => SCALAR, optional => 1 },
136 pidfile => { type => SCALAR },
137 stdout => { type => SCALAR, default => '/dev/null' },
138 stderr => { type => SCALAR, default => '/dev/null' },
139 ubic_log => { type => SCALAR, optional => 1 },
140 term_timeout => { type => SCALAR, default => 10, regex => qr/^\d+$/ },
141 cwd => { type => SCALAR, optional => 1 },
142 env => { type => HASHREF, optional => 1 },
143 proxy_logs => { type => BOOLEAN, optional => 1 },
144 credentials => { isa => 'Ubic::Credentials', optional => 1 },
145 start_hook => { type => CODEREF, optional => 1 },
146 });
147 my ($bin, $function, $name, $pidfile, $stdout, $stderr, $ubic_log, $term_timeout, $cwd, $env, $credentials, $start_hook, $proxy_logs)
148 192 = @options{qw/ bin function name pidfile stdout stderr ubic_log term_timeout cwd env credentials start_hook proxy_logs /};
149 192 ▶ if (not defined $bin and not defined $function) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bin or defined $function) 0 192 192
Condition66%2/3
defined $bin or defined $function
l!l&&r!l&&!r
156360
MC/DC0%0/2
defined $bin or defined $function
defined $bin defined $function
Truth table66%2/3
A or B
A= defined $binB= defined $function
ABresult
1X1
011
000
150 0 croak "One of 'bin' and 'function' should be specified";
151 }
152 192 ▶ if (defined $bin and defined $function) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bin and defined $function) 0 192 192
Condition66%2/3
defined $bin and defined $function
!ll&&!rl&&r
361560
MC/DC0%0/2
defined $bin and defined $function
defined $bin defined $function
Truth table66%2/3
A and B
A= defined $binB= defined $function
ABresult
0X0
100
111
153 0 croak "Only one of 'bin' and 'function' should be specified";
154 }
155 192 ▶ unless (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 154 38 192
156 154 ▶ if (ref $bin) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bin) { } 83 71 154
157 83 $name = join ' ', @$bin;
158 }
159 else {
160 71 ▶ $name = $bin || 'anonymous';
Condition50%1/2
$bin || 'anonymous'
l!l
710
MC/DC0%0/1
$bin || 'anonymous'
$bin
Truth table50%1/2
A
A= $bin
Aresult
11
00
161 }
162 }
163  
164 192 ▶ if (check_daemon($pidfile)) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_daemon($pidfile)) 12 180 192
165 12 croak "Daemon with pidfile $pidfile already running, can't start";
166 }
167  
168 180 my $pid_state = Ubic::Daemon::PidState->new($pidfile);
169 180 $pid_state->init;
170  
171 180 ▶ pipe my ($read_pipe, $write_pipe) or die "pipe failed";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $read_pipe, my $write_pipe or die "pipe failed" 0 180 180
172 180 my $child;
173  
174 180 ▶ unless ($child = fork) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child = fork) 29 151 180
175 29 ▶ die "fork failed" unless defined $child;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child 0 29 29
176  
177 29 my $ubic_fh;
178 my $lock;
179 my $instant_exit = sub {
180 0 ▶ close($ubic_fh) if $ubic_fh;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $ubic_fh 0 0 0
181 0 STDOUT->flush;
182 0 STDERR->flush;
183 0 undef $lock;
184 0 POSIX::_exit(0); # don't allow any cleanup to happen - this process was forked from unknown environment, don't want to run unknown destructors
185 29 };
186  
187 29 eval {
188 29 ▶ close($read_pipe) or die "Can't close read pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $read_pipe or die "Can't close read pipe: $!" 0 29 29
189 # forking child - will reopen standard streams, daemonize itself, fork into daemon binary and wait for it
190  
191 {
192 29 ▶ my $tmp_pid = fork() and POSIX::_exit(0); # detach from parent process
Branch: 1 branch
BranchTrueFalseTotal
my $tmp_pid = fork and POSIX::_exit(0) 0 29 29
193 29 ▶ die "fork failed" unless defined $tmp_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmp_pid 0 29 29
194 }
195  
196 # Close all inherited filehandles except $write_pipe (it will be closed explicitly).
197 # Do not close fh if uses 'function' option instead of 'bin'
198 # ('function' support should be removed altogether because of this, actually; it's evil).
199 29 ▶ if ($bin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bin) 27 2 29
200 27 my $write_pipe_fd_num = fileno($write_pipe);
201 27 $OS->close_all_fh($write_pipe_fd_num); # except pipe
202 }
203  
204 my $open_handles_sub = sub {
205 29 ▶ my $guard;
Subroutine __ANON__: called CC=6 SCAR=18.5
206 29 ▶ $guard = Ubic::AccessGuard->new($credentials) if $credentials;
Branch: 1 branch
BranchTrueFalseTotal
if $credentials 0 29 29
207 29 ▶ open STDOUT, ">>", $stdout or die "Can't write to '$stdout': $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">>", $stdout or die "Can't write to '${stdout}': $!" 0 29 29
208 29 ▶ open STDERR, ">>", $stderr or die "Can't write to '$stderr': $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">>", $stderr or die "Can't write to '${stderr}': $!" 0 29 29
209 29 STDOUT->autoflush(1);
210 29 STDERR->autoflush(1);
211 29 ▶ if (defined $ubic_log) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ubic_log) 13 16 29
212 13 ▶ open $ubic_fh, ">>", $ubic_log or die "Can't write to '$ubic_log': $!";
Branch: 1 branch
BranchTrueFalseTotal
open $ubic_fh, ">>", $ubic_log or die "Can't write to '${ubic_log}': $!" 0 13 13
213 13 $ubic_fh->autoflush(1);
214 }
215 29 };
216 29 $open_handles_sub->();
217 29 my $stdin = '/dev/null';
218 29 ▶ open STDIN, "<", $stdin or die "Can't read from '$stdin': $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", $stdin or die "Can't read from '${stdin}': $!" 0 29 29
219  
220 29 $SIG{HUP} = 'ignore';
221 29 $0 = "ubic-guardian $name";
222 29 setsid; # ubic-daemon gets it's own session
223 29 _log($ubic_fh, "guardian name: $0");
224  
225 29 _log($ubic_fh, "obtaining lock...");
226  
227 # We're passing 'timeout' option to lockf call to get rid of races.
228 # There should be no races when Ubic::Daemon is used in context of
229 # ubic service, because services have an additional lock, but
230 # Ubic::Daemon can be useful without services as well.
231 29 ▶ $lock = $pid_state->lock(5) or die "Can't lock $pid_state";
Branch: 1 branch
BranchTrueFalseTotal
$lock = $pid_state->lock(5) or die "Can't lock $pid_state" 0 29 29
232  
233 29 $pid_state->remove;
234 29 _log($ubic_fh, "got lock");
235  
236 29 my %daemon_pipes;
237 29 ▶ if (defined $proxy_logs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proxy_logs) 2 27 29
238 2 for my $handle (qw/stdout stderr/) {
239 4 ▶ pipe my ($read, $write) or die "pipe for daemon $handle failed";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $read, my $write or die "pipe for daemon $handle failed" 0 4 4
240 4 $daemon_pipes{$handle} = {read => $read, write => $write};
241 }
242 }
243 29 my $child;
244 29 ▶ if ($child = fork) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child = fork) { } 0 29 29
245 # guardian
246  
247 0 _log($ubic_fh, "guardian pid: $$");
248 0 _log($ubic_fh, "daemon pid: $child");
249  
250 0 my $child_guid = $OS->pid2guid($child);
251 0 ▶ unless ($child_guid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child_guid) 0 0 0
252 0 ▶ if ($OS->pid_exists($child)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Ubic::Daemon::OS->pid_exists($child)) 0 0 0
253 0 die "Can't detect guid";
254 }
255 0 $? = 0;
256 0 ▶ unless (waitpid($child, WNOHANG) == $child) {
Branch: 1 branch
BranchTrueFalseTotal
unless (waitpid($child, 1) == $child) 0 0 0
257 0 die "No pid $child but waitpid didn't collect $child status";
258 }
259 0 _log_exit_code($ubic_fh, $?, $child);
260 0 $pid_state->remove();
261 0 die "daemon exited immediately";
262 }
263 0 _log($ubic_fh, "child guid: $child_guid");
264 0 $pid_state->write({ pid => $child, guid => $child_guid });
265  
266 my $kill_sub = sub {
267 0 ▶ if ($term_timeout) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($term_timeout) 0 0 0
268 0 _log($ubic_fh, "SIGTERM timeouted after $term_timeout second(s)");
269 }
270 0 _log($ubic_fh, "sending SIGKILL to $child");
271 0 kill -9 => $child;
272 0 _log($ubic_fh, "daemon $child probably killed by SIGKILL");
273 0 $pid_state->remove();
274 0 $instant_exit->();
275 0 };
276  
277 0 my $sigterm_sent;
278 $SIG{TERM} = sub {
279 0 ▶ if ($term_timeout > 0) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($term_timeout > 0) { } 0 0 0
280 0 $SIG{ALRM} = $kill_sub;
281 0 alarm($term_timeout);
282 0 _log($ubic_fh, "sending SIGTERM to $child");
283 0 kill -15 => $child;
284 0 $sigterm_sent = 1;
285 }
286 else {
287 0 $kill_sub->();
288 }
289 0 };
290 0 ▶ print {$write_pipe} "pidfile written\n" or die "Can't write to pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$write_pipe;} "pidfile written\n" or die "Can't write to pipe: $!" 0 0 0
291 0 ▶ close $write_pipe or die "Can't close pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $write_pipe or die "Can't close pipe: $!" 0 0 0
292 0 undef $write_pipe;
293  
294 0 ▶ if (defined $proxy_logs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proxy_logs) 0 0 0
295 $SIG{HUP} = sub {
296 0 ▶ eval { $open_handles_sub->() };
Subroutine __ANON__: not called CC=2 SCAR=17.9
297 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
298 0 _log($ubic_fh, "failed to reopen stdout/stderr handles: $@");
299 0 $kill_sub->();
300 }
301 else {
302 0 _log($ubic_fh, "reopened stdout/stderr");
303 }
304 0 };
305  
306 0 for my $handle (qw/stdout stderr/) {
307 0 ▶ close($daemon_pipes{$handle}{write}) or do {
Branch: 1 branch
BranchTrueFalseTotal
close $daemon_pipes{$handle}{"write"} or do { _log($ubic_fh, "Can't close $handle write: $!"); die "Can't close $handle write: $!" } 0 0 0
308 0 _log($ubic_fh, "Can't close $handle write: $!");
309 0 die "Can't close $handle write: $!"
310 };
311 }
312 0 my $sel = IO::Select->new();
313 0 $sel->add($daemon_pipes{stdout}{read}, $daemon_pipes{stderr}{read});
314 0 my $BUFF_SIZE = 4096;
315 READ:
316 0 ▶ while ($OS->pid_exists($child)) { # this loop is needed because of timeout in can_read
Branch: 1 branch
BranchTrueFalseTotal
if ($Ubic::Daemon::OS->pid_exists($child)) 0 0 0
317 0 ▶ while (my @ready = $sel->can_read(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $sel->can_read(1)) 0 0 0
318 0 my $exhausted = 0;
319 0 for my $handle (@ready) {
320 0 my $data;
321 0 my $bytes_read = sysread($handle, $data, $BUFF_SIZE);
322 0 ▶ die "Can't poll $handle: $!" unless defined $bytes_read; # handle EWOULDBLOCK?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes_read 0 0 0
323 0 ▶ $exhausted += 1 if $bytes_read == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes_read == 0 0 0 0
324 0 ▶ if (fileno $handle == fileno $daemon_pipes{stdout}{read}) {
Branch: 1 branch
BranchTrueFalseTotal
if (fileno $handle == fileno $daemon_pipes{"stdout"}{"read"}) 0 0 0
325 0 print STDOUT $data;
326 }
327 0 ▶ if (fileno $handle == fileno $daemon_pipes{stderr}{read}) {
Branch: 1 branch
BranchTrueFalseTotal
if (fileno $handle == fileno $daemon_pipes{"stderr"}{"read"}) 0 0 0
328 0 print STDERR $data;
329 }
330 }
331 0 ▶ last READ if $exhausted == @ready;
Branch: 1 branch
BranchTrueFalseTotal
if $exhausted == @ready 0 0 0
332 }
333 }
334 }
335  
336 0 $? = 0;
337 0 waitpid($child, 0);
338 0 my $code = $?;
339 0 ▶ if ($sigterm_sent and ($code & 127) == &POSIX::SIGTERM) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigterm_sent and ($code & 127) == &POSIX::SIGTERM) { } 0 0 0
Condition0%0/3
$sigterm_sent and ($code & 127) == &POSIX::SIGTERM
!ll&&!rl&&r
000
MC/DC0%0/2
$sigterm_sent and ($code & 127) == &POSIX::SIGTERM
$sigterm_sent ($code & 127) == &POSIX::SIGTERM
Truth table0%0/3
A and B
A= $sigterm_sentB= ($code & 127) == &POSIX::SIGTERM
ABresult
0X0
100
111
340 # it's ok, we probably sent this signal ourselves
341 0 _log($ubic_fh, "daemon $child exited by sigterm");
342 }
343 else {
344 0 _log_exit_code($ubic_fh, $code, $child);
345 }
346 0 $pid_state->remove;
347 }
348 else {
349 # daemon
350  
351 29 ▶ die "fork failed" unless defined $child;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child 0 29 29
352  
353 # start new process group - become immune to kills at parent group and at the same time be able to kill all processes below
354 29 setpgrp;
355 29 $0 = "ubic-daemon $name";
356  
357 29 ▶ if (defined $cwd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cwd) 1 28 29
358 1 ▶ chdir $cwd or die "chdir to '$cwd' failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or die "chdir to '${cwd}' failed: $!" 0 1 1
359 }
360 29 ▶ if (defined $env) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env) 0 29 29
361 0 for my $key (keys %{ $env }) {
362 0 $ENV{$key} = $env->{$key};
363 }
364 }
365 29 ▶ $start_hook->() if $start_hook;
Branch: 1 branch
BranchTrueFalseTotal
if $start_hook 1 28 29
366 29 ▶ $credentials->set() if $credentials;
Branch: 1 branch
BranchTrueFalseTotal
if $credentials 0 29 29
367  
368 29 ▶ close($ubic_fh) if defined $ubic_fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ubic_fh 13 16 29
369 29 $lock->dissolve;
370  
371 29 ▶ print {$write_pipe} "execing into daemon\n" or die "Can't write to pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$write_pipe;} "execing into daemon\n" or die "Can't write to pipe: $!" 0 29 29
372 29 ▶ close($write_pipe) or die "Can't close pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $write_pipe or die "Can't close pipe: $!" 0 29 29
373 29 undef $write_pipe;
374  
375 29 ▶ if (defined $proxy_logs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proxy_logs) 2 27 29
376 # redirecting standard streams to pipes
377 2 ▶ close($daemon_pipes{$_}{read}) or die "Can't close $_ read: $!" for qw/stdout stderr/;
Branch: 1 branch
BranchTrueFalseTotal
close $daemon_pipes{$_}{"read"} or die "Can't close $_ read: $!" 0 4 4
378 2 ▶ open STDOUT, '>&=', $daemon_pipes{stdout}{write} or die "Can't open stdout write: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&=", $daemon_pipes{"stdout"}{"write"} or die "Can't open stdout write: $!" 0 2 2
379 2 ▶ open STDERR, '>&=', $daemon_pipes{stderr}{write} or die "Can't open stderr write: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&=", $daemon_pipes{"stderr"}{"write"} or die "Can't open stderr write: $!" 0 2 2
380 }
381  
382 # finally, run underlying binary
383 29 ▶ if (ref $bin) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bin) { } 13 16 29
elsif ($bin) { } 14 2 16
384 13 ▶ exec(@$bin) or die "exec failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
exec @$bin or die "exec failed: $!" 0 0 0
385 }
386 elsif ($bin) {
387 14 ▶ exec($bin) or die "exec failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
exec $bin or die "exec failed: $!" 0 0 0
388 }
389 else {
390 2 $function->();
391 }
392 }
393 };
394 0 ▶ if ($write_pipe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write_pipe) 0 0 0
395 0 print {$write_pipe} "Error: $@\n";
396 0 $write_pipe->flush;
397 }
398 0 $instant_exit->();
399 }
400 151 waitpid($child, 0); # child should've exited immediately
401 151 ▶ close($write_pipe) or die "Can't close write_pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $write_pipe or die "Can't close write_pipe: $!" 0 151 151
402  
403 151 my $out = '';
404 151 ▶ while ( my $data = <$read_pipe>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $data = readline $read_pipe)) 302 151 453
405 302 $out .= $data;
406 }
407 151 ▶ close($read_pipe) or die "Can't close read_pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $read_pipe or die "Can't close read_pipe: $!" 0 151 151
408 151 ▶ if ($out =~ /^execing into daemon$/m and $out =~ /^pidfile written$/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /^execing into daemon$/m and $out =~ /^pidfile written$/m) 140 11 151
Condition66%2/3
$out =~ /^execing into daemon$/m and $out =~ /^pidfile written$/m
!ll&&!rl&&r
110140
MC/DC50%1/2
$out =~ /^execing into daemon$/m and $out =~ /^pidfile written$/m
$out =~ /^execing into daemon$/m $out =~ /^pidfile written$/m
Truth table66%2/3
A and B
A= $out =~ /^execing into daemon$/mB= $out =~ /^pidfile written$/m
ABresult
0X0
100
111
409 # TODO - check daemon's name to make sure that exec happened
410 140 return;
411 }
412 11 die "Failed to create daemon: '$out'";
413 }
414  
415 sub check_daemon {
416 802 ▶ my $pidfile = shift;
Subroutine check_daemon: called CC=10 SCAR=27.0
Pod documented
417 802 my $options = validate(@_, {
418 quiet => { optional => 1 },
419 });
420  
421 my $print = sub {
422 10 ▶ print @_, "\n" unless $options->{quiet};
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"quiet"} 0 10 10
423 802 };
424  
425 802 my $pid_state = Ubic::Daemon::PidState->new($pidfile);
426 802 ▶ return undef if $pid_state->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $pid_state->is_empty 433 369 802
427  
428 369 my $lock = $pid_state->lock;
429 369 my $piddata = $pid_state->read;
430 369 ▶ unless ($lock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lock) 364 5 369
431 # locked => daemon is alive
432 364 return Ubic::Daemon::Status->new({ pid => $piddata->{daemon}, guardian_pid => $piddata->{pid} });
433 }
434  
435 5 ▶ unless ($piddata) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($piddata) 0 5 5
436 0 return undef;
437 }
438  
439 # acquired lock when pidfile exists
440 # checking whether just ubic-guardian died or whole process group
441 5 ▶ if ($piddata->{format} and $piddata->{format} eq 'old') {
Branch: 1 branch
BranchTrueFalseTotal
if ($piddata->{"format"} and $piddata->{"format"} eq "old") 0 5 5
Condition33%1/3
$piddata->{"format"} and $piddata->{"format"} eq "old"
!ll&&!rl&&r
050
MC/DC0%0/2
$piddata->{"format"} and $piddata->{"format"} eq "old"
$piddata->{"format"} $piddata->{"format"} eq "old"
Truth table33%1/3
A and B
A= $piddata->{"format"}B= $piddata->{"format"} eq "old"
ABresult
0X0
100
111
442 0 die "deprecated pidfile format detected\n";
443 }
444 5 ▶ unless ($piddata->{daemon}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($piddata->{"daemon"}) 0 5 5
445 163 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
446 0 die "pidfile $pidfile exists, but daemon pid is not saved in it, so existing unguarded daemon can't be killed (piddata: ".Dumper($piddata).")";
447 }
448 5 ▶ unless ($OS->pid_exists($piddata->{daemon})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Ubic::Daemon::OS->pid_exists($piddata->{"daemon"})) 0 5 5
449 0 $pid_state->remove;
450 0 $print->("pidfile $pidfile removed - daemon with cached pid $piddata->{daemon} not found");
451 0 return undef;
452 }
453  
454 # TODO - wrap in eval and return undef if pid2cmd fails?
455 5 my $daemon_cmd = $OS->pid2cmd($piddata->{daemon});
456  
457 5 my $guid = $OS->pid2guid($piddata->{daemon});
458 5 ▶ unless ($guid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($guid) 0 5 5
459 0 $print->("daemon '$daemon_cmd' from $pidfile just disappeared");
460 0 return undef;
461 }
462 5 ▶ if ($guid eq $piddata->{guid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guid eq $piddata->{"guid"}) 5 0 5
463 5 $print->("killing unguarded daemon '$daemon_cmd' with pid $piddata->{daemon} from $pidfile");
464 5 kill -9 => $piddata->{daemon};
465 5 $pid_state->remove;
466 5 $print->("pidfile $pidfile removed");
467 5 return undef;
468 }
469 0 $print->("daemon pid $piddata->{daemon} cached in pidfile $pidfile, ubic-guardian not found");
470 0 $print->("current process '$daemon_cmd' with pid $piddata->{daemon} has wrong guid ($piddata->{guid}, expected $guid) and will not be killed");
471 0 $print->("removing pidfile $pidfile");
472 0 $pid_state->remove;
473 0 return undef;
474 }
475  
476  
477 163 1;
478  
479 __END__
« blib/lib/Ubic/Credentials/OS/MacOSX.pm ↑ Summary lib/Ubic/Credentials/OS/MacOSX.pm »