lib/Ubic.pm

filter: Statementstmt 75.5% 229 / 303 Branchbran 42.8% 30 / 70 Conditioncond 64.2% 9 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 81.9% 50 / 61 Podpod 100% 33 / 33 totaltotal 72.3% 354 / 489 SCAR 45.4
CC
42
Cov
69%
compl_services
56.0
forked_call
56.0
reload
12.0
CRAP 93.3
« lib/Ubic/Ping.pm ↑ Summary lib/Ubic/Cmd.pm »
1 package Ubic;
2 154 $Ubic::VERSION = '1.59';
3 154 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 154 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: polymorphic service manager
7  
8  
9 154 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 154 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 154 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 154 ▶use Storable qw(freeze thaw);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 154 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 154 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 154 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 154 ▶use Ubic::Result qw(result);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 154 ▶use Ubic::Multiservice::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 154 ▶use Ubic::AccessGuard;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 154 ▶use Ubic::Credentials;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 154 ▶use Ubic::Persistent;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 154 ▶use Ubic::AtomicFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 154 ▶use Ubic::SingletonLock;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 154 ▶use Ubic::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 154 our $SINGLETON;
27  
28 154 my $service_name_re = qr{^[\w-]+(?:\.[\w-]+)*$};
29 154 my $validate_service = { type => SCALAR, regex => $service_name_re };
30  
31 # singleton constructor
32 sub _obj {
33 2368 ▶ my ($param) = validate_pos(@_, 1);
Subroutine _obj: called CC=4 SCAR=14.2
34 2368 ▶ if (blessed($param)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($param)) 1831 537 2368
35 1831 return $param;
36 }
37 537 ▶ if ($param eq 'Ubic') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "Ubic") 537 0 537
38 # method called as a class method => singleton
39 537 ▶ $SINGLETON ||= Ubic->new({});
Condition66%2/3
$Ubic::SINGLETON ||= "Ubic"->new({})
l!l&&r!l&&!r
4001370
MC/DC0%0/2
$Ubic::SINGLETON ||= "Ubic"->new({})
$Ubic::SINGLETON "Ubic"->new({})
Truth table66%2/3
A ||= B
A= $Ubic::SINGLETONB= "Ubic"->new({})
ABresult
1X1
011
000
40 537 return $SINGLETON;
41 }
42 0 die "Unknown argument '$param'";
43 }
44  
45 sub new {
46 137 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.0
Pod documented
47 137 my $options = validate(@_, {
48 service_dir => { type => SCALAR, optional => 1 },
49 data_dir => { type => SCALAR, optional => 1 },
50 });
51  
52 137 ▶ if (caller ne 'Ubic') {
Branch: 1 branch
BranchTrueFalseTotal
if (caller ne "Ubic") 0 137 137
53 0 warn "Using Ubic->new constructor is discouraged. Just call methods as class methods.";
54 }
55  
56 137 for my $key (qw/ service_dir data_dir /) {
57 274 ▶ Ubic::Settings->$key($options->{ $key }) if defined $options->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{$key} 0 274 274
58 }
59  
60 137 Ubic::Settings->check_settings;
61  
62 137 my $self = {};
63 137 $self->{data_dir} = Ubic::Settings->data_dir;
64 137 $self->{service_dir} = Ubic::Settings->service_dir;
65  
66 137 $self->{status_dir} = "$self->{data_dir}/status";
67 137 $self->{lock_dir} = "$self->{data_dir}/lock";
68 137 $self->{tmp_dir} = "$self->{data_dir}/tmp";
69  
70 137 $self->{service_cache} = {};
71 137 return bless $self => $class;
72 }
73  
74 sub start($$) {
75 24 ▶ my $self = _obj(shift);
Subroutine start: called CC=1 SCAR=0.0
Pod documented
76 24 my ($name) = validate_pos(@_, $validate_service);
77 24 my $lock = $self->lock($name);
78  
79 23 $self->enable($name);
80 23 my $result = $self->do_cmd($name, 'start');
81 14 $self->set_cached_status($name, $result);
82 14 return $result;
83 }
84  
85 sub stop($$) {
86 34 ▶ my $self = _obj(shift);
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
87 34 my ($name) = validate_pos(@_, $validate_service);
88 34 my $lock = $self->lock($name);
89  
90 34 $self->disable($name);
91  
92 # FIXME - 'stop' command can fail, in this case daemon will keep running.
93 # This is bad.
94 # We probably need to implement the same logic as when starting:
95 # retry stop attempts until actual status matches desired status.
96 34 my $result = $self->do_cmd($name, 'stop');
97 28 return $result;
98 }
99  
100 sub restart($$) {
101 3 ▶ my $self = _obj(shift);
Subroutine restart: called CC=1 SCAR=0.0
Pod documented
102 3 my ($name) = validate_pos(@_, $validate_service);
103 3 my $lock = $self->lock($name);
104  
105 3 $self->enable($name);
106 3 my $result = $self->do_cmd($name, 'stop');
107 3 $result = $self->do_cmd($name, 'start');
108  
109 2 $self->set_cached_status($name, $result);
110 2 return result('restarted'); # FIXME - should return original status
111 }
112  
113 sub try_restart($$) {
114 6 ▶ my $self = _obj(shift);
Subroutine try_restart: called CC=2 SCAR=7.1
Pod documented
115 6 my ($name) = validate_pos(@_, $validate_service);
116 6 my $lock = $self->lock($name);
117  
118 6 ▶ unless ($self->is_enabled($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_enabled($name)) 0 6 6
119 0 return result('down');
120 }
121 6 $self->do_cmd($name, 'stop');
122 6 $self->do_cmd($name, 'start');
123 3 return result('restarted');
124 }
125  
126 sub reload($$) {
127 0 ▶ my $self = _obj(shift);
Subroutine reload: not called CC=3 SCAR=24.8
Pod documented
128 0 my ($name) = validate_pos(@_, $validate_service);
129 0 my $lock = $self->lock($name);
130  
131 0 ▶ unless ($self->is_enabled($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_enabled($name)) 0 0 0
132 0 return result('down');
133 }
134  
135 # if reload isn't implemented, do nothing
136 # TODO - would it be better to execute reload as force-reload always? but it would be incompatible with LSB specification...
137 0 my $result = $self->do_cmd($name, 'reload');
138 0 ▶ unless ($result->action eq 'reloaded') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->action eq "reloaded") 0 0 0
139 0 die $result;
140 }
141 0 return $result;
142 }
143  
144 sub force_reload($$) {
145 6 ▶ my $self = _obj(shift);
Subroutine force_reload: called CC=3 SCAR=11.4
Pod documented
146 6 my ($name) = validate_pos(@_, $validate_service);
147 6 my $lock = $self->lock($name);
148  
149 6 ▶ unless ($self->is_enabled($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_enabled($name)) 0 6 6
150 0 return result('down');
151 }
152  
153 6 my $result = $self->do_cmd($name, 'reload');
154 6 ▶ return $result if $result->action eq 'reloaded';
Branch: 1 branch
BranchTrueFalseTotal
if $result->action eq "reloaded" 0 6 6
155  
156 6 $self->try_restart($name);
157 }
158  
159 sub status($$) {
160 11 ▶ my $self = _obj(shift);
Subroutine status: called CC=1 SCAR=0.0
Pod documented
161 11 my ($name) = validate_pos(@_, $validate_service);
162 11 my $lock = $self->lock($name);
163  
164 11 return $self->do_cmd($name, 'status');
165 }
166  
167 sub enable($$) {
168 27 ▶ my $self = _obj(shift);
Subroutine enable: called CC=1 SCAR=0.0
Pod documented
169 27 my ($name) = validate_pos(@_, $validate_service);
170 27 my $lock = $self->lock($name);
171 27 my $guard = $self->access_guard($name);
172  
173 27 my $status_obj = $self->status_obj($name);
174 27 $status_obj->{status} = 'unknown';
175 27 $status_obj->{enabled} = 1;
176 27 $status_obj->commit;
177 27 return result('unknown');
178 }
179  
180 sub is_enabled($$) {
181 117 ▶ my $self = _obj(shift);
Subroutine is_enabled: called CC=5 SCAR=16.2
Pod documented
182 117 my ($name) = validate_pos(@_, $validate_service);
183  
184 117 ▶ die "Service '$name' not found" unless $self->root_service->has_service($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->root_service->has_service($name) 0 117 117
185 117 ▶ unless (-e $self->status_file($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->status_file($name)) 83 34 117
186 83 return $self->service($name)->auto_start();
187 }
188  
189 34 my $status_obj = $self->status_obj_ro($name);
190 34 ▶ if ($status_obj->{enabled} or not exists $status_obj->{enabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status_obj->{"enabled"} or not exists $status_obj->{"enabled"}) 27 7 34
Condition66%2/3
$status_obj->{"enabled"} or not exists $status_obj->{"enabled"}
l!l&&r!l&&!r
2707
MC/DC50%1/2
$status_obj->{"enabled"} or not exists $status_obj->{"enabled"}
$status_obj->{"enabled"} not exists $status_obj->{"enabled"}
Truth table66%2/3
A or not(B)
A= $status_obj->{"enabled"}B= not exists $status_obj->{"enabled"}
ABresult
1X1
011
000
191 27 return 1;
192 }
193 7 return;
194 }
195  
196 sub disable($$) {
197 36 ▶ my $self = _obj(shift);
Subroutine disable: called CC=1 SCAR=0.0
Pod documented
198 36 my ($name) = validate_pos(@_, $validate_service);
199 36 my $lock = $self->lock($name);
200 36 my $guard = $self->access_guard($name);
201  
202 36 my $status_obj = $self->status_obj($name);
203 36 delete $status_obj->{status};
204 36 $status_obj->{enabled} = 0;
205 36 $status_obj->commit;
206 }
207  
208  
209 sub cached_status($$) {
210 5 ▶ my ($self) = _obj(shift);
Subroutine cached_status: called CC=3 SCAR=11.1
Pod documented
211 5 my ($name) = validate_pos(@_, $validate_service);
212  
213 5 my $type;
214 5 ▶ if (not $self->is_enabled($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->is_enabled($name)) { } 0 5 5
elsif (-e $self->status_file($name)) { } 2 3 5
215 0 $type = 'disabled';
216 }
217 elsif (-e $self->status_file($name)) {
218 2 $type = $self->status_obj_ro($name)->{status};
219 } else {
220 3 $type = 'autostarting';
221 }
222 5 return Ubic::Result::Class->new({ type => $type, cached => 1 });
223 }
224  
225 sub do_custom_command($$) {
226 2 ▶ my ($self) = _obj(shift);
Subroutine do_custom_command: called CC=1 SCAR=0.0
Pod documented
227 2 my ($name, $command) = validate_pos(@_, $validate_service, 1);
228  
229 # TODO - do all custom commands require locks?
230 # they can be distinguished in future by some custom_commands_ext method which will provide hash { command => properties }, i think...
231 2 my $lock = $self->lock($name);
232  
233 # TODO - check custom_command presence by custom_commands() method first?
234 $self->do_sub(sub {
235 2 ▶ $self->service($name)->do_custom_command($command); # can custom commands require custom arguments?
Subroutine __ANON__: called CC=1 SCAR=0.0
236 2 });
237 }
238  
239 sub service($$) {
240 574 ▶ my $self = _obj(shift);
Subroutine service: called CC=2 SCAR=6.9
Pod documented
241 574 my ($name) = validate_pos(@_, $validate_service);
242 # this guarantees that : will be unambiguous separator in status filename (what??)
243 574 ▶ unless ($self->{service_cache}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"service_cache"}{$name}) 158 416 574
244 # Service construction is a memory-leaking operation (because of package name randomization in Ubic::Multiservice::Dir),
245 # so we need to cache each service which we create.
246 158 $self->{service_cache}{$name} = $self->root_service->service($name);
247 }
248 573 return $self->{service_cache}{$name};
249 }
250  
251 sub has_service($$) {
252 0 ▶ my $self = _obj(shift);
Subroutine has_service: not called CC=1 SCAR=6.9
Pod documented
253 0 my ($name) = validate_pos(@_, $validate_service);
254 # TODO - it would be safer to do this check without actual service construction
255 # but it would require cron-based script which maintains list of all services
256 0 return $self->root_service->has_service($name);
257 }
258  
259 sub services($) {
260 1 ▶ my $self = _obj(shift);
Subroutine services: called CC=1 SCAR=0.0
Pod documented
261 1 return $self->root_service->services();
262 }
263  
264 sub service_names($) {
265 0 ▶ my $self = _obj(shift);
Subroutine service_names: not called CC=1 SCAR=6.9
Pod documented
266 0 return $self->root_service->service_names();
267 }
268  
269 sub root_service($) {
270 399 ▶ my $self = _obj(shift);
Subroutine root_service: called CC=2 SCAR=6.9
Pod documented
271 399 ▶ unless (defined $self->{root}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"root"}) 141 258 399
272 141 $self->{root} = Ubic::Multiservice::Dir->new($self->{service_dir}, { protected => 1 });
273 }
274 399 return $self->{root};
275 }
276  
277 sub compl_services($$) {
278 0 ▶ my $self = _obj(shift);
Subroutine compl_services: not called CC=7 SCAR=40.3
Pod documented
279 0 my $line = shift;
280 0 my @parts = split /\./, $line;
281 0 ▶ if ($line =~ /\.$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\.$/) 0 0 0
282 0 push @parts, '';
283 }
284 0 ▶ if (@parts == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts == 0) 0 0 0
285 0 return $self->service_names;
286 }
287 0 my $node = $self->root_service;
288 0 my $is_subservice = (@parts > 1);
289 0 ▶ while (@parts > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 1) 0 0 0
290 0 ▶ unless ($node->isa('Ubic::Multiservice')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->isa("Ubic::Multiservice")) 0 0 0
291 0 return;
292 }
293 0 my $part = shift @parts;
294 0 ▶ return unless $node->has_service($part); # no such service
Branch: 1 branch
BranchTrueFalseTotal
unless $node->has_service($part) 0 0 0
295 0 $node = $node->service($part);
296 }
297  
298 0 my @variants = $node->service_names;
299 return
300 map {
301 0 ▶ ( $is_subservice ? $node->full_name.".".$_ : $_ )
Branch: 1 branch
BranchTrueFalseTotal
$is_subservice ? : 0 0 0
302 }
303 grep {
304 0 $_ =~ m{^\Q$parts[0]\E}
305 }
306 @variants;
307 }
308  
309 sub set_cached_status($$$) {
310 22 ▶ my $self = _obj(shift);
Subroutine set_cached_status: called CC=5 SCAR=16.1
Pod documented
311 22 my ($name, $status) = validate_pos(@_, $validate_service, 1);
312 22 my $guard = $self->access_guard($name);
313  
314 22 ▶ if (blessed $status) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($status)) 22 0 22
315 22 ▶ croak "Wrong status param '$status'" unless $status->isa('Ubic::Result::Class');
Branch: 1 branch
BranchTrueFalseTotal
unless $status->isa("Ubic::Result::Class") 0 22 22
316 22 $status = $status->status;
317 }
318 22 my $lock = $self->lock($name);
319  
320 22 ▶ if (-e $self->status_file($name) and $self->status_obj_ro($name)->{status} eq $status) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->status_file($name) and $self->status_obj_ro($name)->{"status"} eq $status) 3 19 22
Condition100%3/3
-e $self->status_file($name) and $self->status_obj_ro($name)->{"status"} eq $status
!ll&&!rl&&r
2173
MC/DC100%2/2
-e $self->status_file($name) and $self->status_obj_ro($name)->{"status"} eq $status
-e $self->status_file($name) $self->status_obj_ro($name)->{"status"} eq $status
Truth table100%3/3
A and B
A= -e $self->status_file($name)B= $self->status_obj_ro($name)->{"status"} eq $status
ABresult
0X0
100
111
321 # optimization - don't update status if nothing changed
322 3 return;
323 }
324  
325 19 my $status_obj = $self->status_obj($name);
326 19 $status_obj->{status} = $status;
327 19 $status_obj->commit;
328 }
329  
330 sub get_data_dir($) {
331 78 ▶ my $self = _obj(shift);
Subroutine get_data_dir: called CC=1 SCAR=0.0
Pod documented
332 78 validate_pos(@_);
333 78 return $self->{data_dir};
334 }
335  
336 sub set_data_dir($$) {
337 58 ▶ my ($arg, $dir) = validate_pos(@_, 1, 1);
Subroutine set_data_dir: called CC=3 SCAR=11.2
Pod documented
338  
339 my $md = sub {
340 580 ▶ my $new_dir = shift;
Subroutine __ANON__: called CC=3 SCAR=13.3
341 580 ▶ mkdir $new_dir or die "mkdir $new_dir failed: $!" unless -d $new_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $new_dir 580 0 580
Condition40%2/5
-d $new_dir || (mkdir($new_dir) || die("mkdir $new_dir failed: $!"))
l!l&&r!l&&!r
05800
mkdir $new_dir or die "mkdir $new_dir failed: $!"
l!l
5800
MC/DC0%0/2
-d $new_dir || (mkdir($new_dir) || die("mkdir $new_dir failed: $!"))
-d $new_dir mkdir $new_dir
Truth table33%1/3
A || B
A= -d $new_dirB= mkdir $new_dir
ABresult
1X1
011
000
342 58 };
343  
344 58 $md->($dir);
345 # FIXME - directory list is copy-pasted from Ubic::Admin::Setup
346 58 for my $subdir (qw[
347 status simple-daemon simple-daemon/pid lock ubic-daemon tmp watchdog watchdog/lock watchdog/status
348 ]) {
349 522 $md->("$dir/$subdir");
350 }
351  
352 58 Ubic::Settings->data_dir($dir);
353 58 ▶ if ($SINGLETON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Ubic::SINGLETON) 17 41 58
354 17 $SINGLETON->{lock_dir} = "$dir/lock";
355 17 $SINGLETON->{status_dir} = "$dir/status";
356 17 $SINGLETON->{tmp_dir} = "$dir/tmp";
357 17 $SINGLETON->{data_dir} = $dir;
358 }
359 }
360  
361 sub set_ubic_dir($$);
362 154 *set_ubic_dir = \&set_data_dir;
363  
364 sub set_default_user($$) {
365 58 ▶ my ($arg, $user) = validate_pos(@_, 1, 1);
Subroutine set_default_user: called CC=1 SCAR=0.0
Pod documented
366  
367 58 Ubic::Settings->default_user($user);
368 }
369  
370 sub get_service_dir($) {
371 0 ▶ my $self = _obj(shift);
Subroutine get_service_dir: not called CC=1 SCAR=6.9
Pod documented
372 0 validate_pos(@_);
373 0 return $self->{service_dir};
374 }
375  
376 sub set_service_dir($$) {
377 58 ▶ my ($arg, $dir) = validate_pos(@_, 1, 1);
Subroutine set_service_dir: called CC=2 SCAR=6.9
Pod documented
378 58 Ubic::Settings->service_dir($dir);
379 58 ▶ if ($SINGLETON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Ubic::SINGLETON) 17 41 58
380 17 $SINGLETON->{service_dir} = $dir;
381 17 undef $SINGLETON->{root}; # force lazy regeneration
382 }
383 }
384  
385 sub status_file($$) {
386 282 ▶ my $self = _obj(shift);
Subroutine status_file: called CC=1 SCAR=0.0
Pod documented
387 282 my ($name) = validate_pos(@_, $validate_service);
388 282 return "$self->{status_dir}/".$name;
389 }
390  
391 sub status_obj($$) {
392 82 ▶ my $self = _obj(shift);
Subroutine status_obj: called CC=1 SCAR=0.0
Pod documented
393 82 my ($name) = validate_pos(@_, $validate_service);
394 82 return Ubic::Persistent->new($self->status_file($name));
395 }
396  
397 sub status_obj_ro($$) {
398 56 ▶ my $self = _obj(shift);
Subroutine status_obj_ro: called CC=1 SCAR=0.0
Pod documented
399 56 my ($name) = validate_pos(@_, $validate_service);
400 56 return Ubic::Persistent->load($self->status_file($name));
401 }
402  
403 sub access_guard($$) {
404 344 ▶ my $self = _obj(shift);
Subroutine access_guard: called CC=1 SCAR=0.0
Pod documented
405 344 my ($name) = validate_pos(@_, $validate_service);
406 344 return Ubic::AccessGuard->new(
407 Ubic::Credentials->new(service => $self->service($name))
408 );
409 }
410  
411 sub lock($$) {
412 259 ▶ my ($self) = _obj(shift);
Subroutine lock: called CC=1 SCAR=0.0
Pod documented
413 259 my ($name) = validate_pos(@_, $validate_service);
414  
415 259 my $lock = do {
416 259 my $guard = $self->access_guard($name);
417 258 Ubic::SingletonLock->new($self->{lock_dir}."/".$name);
418 };
419 258 return $lock;
420 }
421  
422 sub do_sub($$) {
423 94 ▶ my ($self, $code) = @_;
Subroutine do_sub: called CC=1 SCAR=0.0
Pod documented
424 my $result = try {
425 94 ▶ $code->();
Subroutine __ANON__: called CC=1 SCAR=0.0
426 } catch {
427 7 ▶ die result($_);
Subroutine __ANON__: called CC=1 SCAR=0.0
428 94 };
429 75 return result($result);
430 }
431  
432 sub do_cmd($$$) {
433 92 ▶ my ($self, $name, $cmd) = @_;
Subroutine do_cmd: called CC=1 SCAR=0.5
Pod documented
434 $self->do_sub(sub {
435 92 ▶ my $service = $self->service($name);
Subroutine __ANON__: called CC=2 SCAR=8.5
436  
437 92 my $creds = Ubic::Credentials->new( service => $service );
438  
439 92 ▶ if ($creds->eq(Ubic::Credentials->new)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($creds->eq("Ubic::Credentials"->new)) 92 0 92
440 # current credentials fit service expectations
441 92 return $service->$cmd();
442 }
443  
444 # setting just effective uid is not enough, because:
445 # - we can accidentally enter tainted mode, and service authors don't expect this
446 # - local administrator may want to allow everyone to write their own services, and leaving root as real uid is an obvious security breach
447 # (ubic will have to learn to compare service user with service file's owner for such policy to be safe, though - this is not implemented yet)
448 $self->forked_call(sub {
449 0 ▶ $creds->set();
Subroutine __ANON__: not called CC=1 SCAR=6.9
450 0 return $service->$cmd();
451 0 });
452 92 });
453 }
454  
455 sub forked_call {
456 0 ▶ my ($self, $callback) = @_;
Subroutine forked_call: not called CC=7 SCAR=40.3
Pod documented
457 0 my $tmp_file = $self->{tmp_dir}."/".time.".$$.".rand(1000000);
458 0 my $child;
459 0 ▶ unless ($child = fork) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child = fork) 0 0 0
460 0 ▶ unless (defined $child) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child) 0 0 0
461 0 die "fork failed";
462 }
463 0 my $result;
464 try {
465 0 ▶ $result = { ok => $callback->() };
Subroutine __ANON__: not called CC=1 SCAR=6.9
466 }
467 catch {
468 0 ▶ $result = { error => $_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
469 0 };
470  
471 try {
472 0 ▶ Ubic::AtomicFile::store( freeze($result) => $tmp_file );
Subroutine __ANON__: not called CC=1 SCAR=6.9
473 0 STDOUT->flush;
474 0 STDERR->flush;
475 0 POSIX::_exit(0); # don't allow to lock to be released - this process was forked from unknown environment, don't want to run unknown destructors
476 }
477 catch {
478 # probably tmp_file is not writable
479 0 ▶ warn $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
480 0 POSIX::_exit(1);
481 0 };
482 }
483 0 waitpid($child, 0);
484 0 ▶ unless (-e $tmp_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $tmp_file) 0 0 0
485 0 die "temp file $tmp_file not found after fork";
486 }
487 0 ▶ open my $fh, '<', $tmp_file or die "Can't read $tmp_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $tmp_file or die "Can't read ${tmp_file}: $!" 0 0 0
488 0 my $content = do { local $/; <$fh>; };
489 0 ▶ close $fh or die "Can't close $tmp_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close ${tmp_file}: $!" 0 0 0
490 0 unlink $tmp_file;
491 0 my $result = thaw($content);
492 0 ▶ if ($result->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'error'}) { } 0 0 0
493 0 die $result->{error};
494 }
495 else {
496 0 return $result->{ok};
497 }
498 }
499  
500  
501 154 1;
502  
503 __END__
« lib/Ubic/Ping.pm ↑ Summary lib/Ubic/Cmd.pm »