Webqq/Run.pm untested

filter: Statementstmt 0.0% 0 / 304 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 89 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 606 CC 58 SCAR 40.2
CC
58
Cov
0%
spawn
156.0
complete
132.0
watch
72.0
CRAP 55.9
1 package Mojo::Webqq::Run;
2 use List::Util qw(first);
3 use Mojo::Webqq::Base -base;
4 use bytes;
5 use Carp;
6 use Errno;
7 use Socket;
8 use Time::HiRes qw(time gettimeofday);
9 use Scalar::Util qw(blessed);
10 use Storable qw(thaw nfreeze);
11 use POSIX ":sys_wait_h";
12 use Mojo::Webqq::Log;
13 use Mojo::IOLoop;
14 use Mojo::Reactor;
15 has 'num_forks' => sub { 0 };
16 has 'max_forks' => sub { 0 };
17 has 'log' => sub { Mojo::Webqq::Log->new };
18 has 'ioloop' => sub { Mojo::IOLoop->singleton };
19 has [qw/reactor error is_child/];
20
21 our $VERSION = '0.3';
22
23 my $_obj = undef;
24
25 BEGIN {
26 *portable_pipe = sub () { my ($r, $w);
27 pipe $r, $w or return;
28
29 ($r, $w);
30 };
31 *portable_socketpair = sub () {
32 socketpair my $fh1, my $fh2, Socket::AF_UNIX(), Socket::SOCK_STREAM(), PF_UNSPEC
33 or return;
34 $fh1->autoflush(1);
35 $fh2->autoflush(1);
36
37 ($fh1, $fh2)
38 };
39 }
40
41 sub new {my $class = shift; __PACKAGE__->singleton(@_) }
42
43 sub singleton {
44 return $_obj if defined $_obj;
45 my $class = shift;
46 return $_obj = __PACKAGE__->_constructor(@_);
47 }
48
49 sub _constructor {
50 my $proto = shift;
51 my $class = ref($proto) || $proto;
52 my $self = $class->SUPER::new(@_);
53
54 bless $self => $class;
55
56 # install SIGCHLD handler
57 $SIG{'CHLD'} = sub { _sig_chld($self, @_) };
58
59 return $self;
60 }
61
62 sub log_level {
63 my ($self, $level) = @_;
64
65 $self->log->level($level) if defined $level;
66
67 return $self->log->level;
68 }
69
70 sub spawn {
71 my ($self, %opt) = @_;
72
73 unless (defined $self && blessed($self) && $self->isa(__PACKAGE__)) {
74 my $obj = __PACKAGE__->new;
75 return $obj->spawn(%opt);
76 }
77
78 $self->error('');
79
80 if ($self->max_forks > 0 && $self->num_forks >= $self->max_forks) {
81 $self->error("Unable to spawn another subprocess: "
82 ."Limit of " . $self->max_forks . " concurrently spawned process(es) is reached."
83 );
84 return 0;
85 }
86
87 # normalize and validate run parameters...
88 my $proc = $self->_getRunStruct(\%opt);
89 return 0 unless $self->_validateRunStruct($proc);
90
91 $self->log->debug("Spawning command "
92 ."timeout: "
93 .($proc->{exec_timeout} > 0 ? sprintf("%-.3f seconds]", $proc->{exec_timeout}) : "none")
94 ." : [$proc->{cmd}]"
95 );
96 my ($stdout_p, $stdout_c) = portable_socketpair;
97 my ($stderr_p, $stderr_c) = portable_socketpair;
98 my ($stdres_p, $stdres_c) = portable_socketpair;
99
100 $proc->{time_started} = time;
101 $proc->{running } = 1;
102 $proc->{hdr_stdout } = $stdout_c;
103 $proc->{hdr_stderr } = $stderr_c;
104 $proc->{hdr_stdres } = $stdres_c;
105
106 my $pid = fork;
107
108 if ($pid) {
109 # parent
110 $self->num_forks($self->num_forks + 1);
111
112 $self->log->debug("Subprocess spawned as pid $pid.");
113
114 $proc->{pid} = $pid;
115
116 # exec timeout
117 if (defined $proc->{exec_timeout} && $proc->{exec_timeout} > 0) {
118 $self->log->debug(
119 "[process $pid]: Setting execution timeout to " .
120 sprintf("%-.3f seconds.", $proc->{exec_timeout})
121 );
122 my $timer = $self->ioloop->timer(
123 $proc->{exec_timeout},
124 sub { _timeout_cb($self, $pid) }
125 );
126
127 # save timer
128 $proc->{id_timeout} = $timer;
129 }
130
131 $self->{_data}->{$pid} = $proc;
132
133 close $stdout_p;
134 close $stderr_p;
135 close $stdres_p;
136
137 $self->watch('stdout', $pid);
138 $self->watch('stderr', $pid);
139 $self->watch('stdres', $pid);
140 } else {
141 # child
142
143 $self->is_child(1);
144
145 close $stdout_c;
146 close $stderr_c;
147 close $stdres_c;
148
149 # Stdio should not be tied.
150 if (tied *STDOUT) {
151 carp "Cannot redirect into tied STDOUT. Untying it";
152 untie *STDOUT;
153 }
154 if (tied *STDERR) {
155 carp "Cannot redirect into tied STDERR. Untying it";
156 untie *STDERR;
157 }
158
159 # Redirect STDOUT
160 open STDOUT, ">&" . fileno($stdout_p)
161 or croak "can't redirect stdout in child pid $$: $!";
162 # Redirect STDERR
163 open STDERR, ">&" . fileno($stderr_p)
164 or croak "can't redirect stderr in child pid $$: $!";
165
166 select STDERR; $| = 1;
167 select STDOUT; $| = 1;
168
169 if (ref $proc->{cmd} eq 'CODE') {
170 my @rv = eval { $proc->{cmd}->($$, $proc->{param}); };
171
172 if ($@) {
173 carp "exec of coderef failed: $@\n";
174 exit 255;
175 }
176
177 print $stdres_p nfreeze(\ @rv);
178
179 } else {
180 exec(ref $proc->{cmd} eq 'ARRAY' ? @{ $proc->{cmd} } : $proc->{cmd}) or do {
181 carp "exec failed";
182 exit 255;
183 };
184 }
185
186 close $stdout_p;
187 close $stderr_p;
188 close $stdres_p;
189
190 exit 1;
191 }
192
193 return $pid;
194 }
195
196 sub start { shift->ioloop->start }
197
198 sub watch {
199 my $self = shift;
200 my $io = lc(shift || '');
201 my $pid = shift;
202
203 my $proc = $self->get_proc($pid);
204
205 $self->log->error('Cant start IO watcher off NULL process' ) and return unless $proc;
206 $self->log->error("[process $proc->{pid}]: IO ($io) is unsupported" ) and return unless first {$io eq $_} qw/stdout stderr stdres/;
207 $self->log->error("[process $proc->{pid}]: IO handler ($io) is EMPTY") and return unless $proc->{"hdr_$io"};
208
209 my $id = fileno $proc->{"hdr_$io"};
210
211 $self->ioloop->reactor->io($proc->{"hdr_$io"}, sub {
212 my $chunk = undef;
213 my $len = sysread $proc->{"hdr_$io"}, $chunk, 65536;
214
215 return unless defined $len or $! != Errno::EINTR;
216
217 if (!$len) {
218 $self->drop_handle($pid, $io);
219 return;
220 }
221
222 if (defined $proc->{"$io\_cb"}) {
223 $self->log->debug("[process $proc->{pid}]: (handle: $id) Invoking ".uc($io)." callback.");
224
225 eval { $proc->{"$io\_cb"}->($proc->{pid}, $chunk) };
226
227 if ($@) {
228 $self->log->error("[process $proc->{pid}]: (handle: $id) Exception in $io\_cb: $@");
229 }
230 }
231 #else {
232 {
233 # append to buffer
234 $self->log->debug("[process $proc->{pid}]: (handle: $id) Appending $len bytes to ".uc($io)." buffer.");
235 $proc->{"buf_$io"} .= $chunk;
236 }
237 })->watch($proc->{"hdr_$io"}, 1, 0);
238 }
239
240 sub drop_handle {
241 my $self = shift;
242 my $pid = shift;
243 my $io = lc(shift || '');
244
245 my $proc = $self->get_proc($pid);
246 return unless $proc;
247
248 $self->log->debug("[process $pid]: Got HUP for unmanaged handle ".$proc->{"hdr_$io"}."; ignoring.") and return
249 unless $proc->{"hdr_$io"};
250
251
252 $self->ioloop->remove( $proc->{"hdr_$io"} );
253 undef $proc->{"hdr_$io"};
254
255 $self->log->debug("[process $pid]: ".uc($io)." closed.");
256
257 $self->complete($pid);
258 }
259
260 sub get_proc {
261 my ($self, $pid) = @_;
262
263 no warnings;
264 my $err = "[process $pid]: Unable to get process data structure: ";
265
266 unless (defined $pid) {
267 $self->error($err . "Undefined pid.");
268 return undef;
269 }
270
271 unless (
272 exists $self->{_data}->{$pid}
273 && defined $self->{_data}->{$pid}
274 ) {
275 $self->error($err . "Non-managed process pid: $pid");
276 return undef;
277 }
278
279 return $self->{_data}->{$pid};
280 }
281
282 sub cleanup {
283 my ($self, $pid, $exit_val, $signum, $core) = @_;
284
285 my $proc = $self->get_proc($pid);
286 unless (defined $proc) {
287 no warnings;
288 $self->log->warn("Untracked process pid $pid exited with exit status $exit_val by signal $signum, core: $core.");
289 return 0;
290 }
291 return 0 if $proc->{cleanup};
292
293 $proc->{cleanup} = 1;
294
295 $self->log->debug("[process $pid]: Got SIGCHLD, "
296 . "exited with exit status: $exit_val by signal $signum"
297 . (($core) ? "with core dump." : ".")
298 );
299
300 if (defined $proc->{id_timeout}) {
301 $self->ioloop->remove($proc->{id_timeout});
302 $proc->{id_timeout} = undef;
303 }
304 if ($proc->{hard_kill}) {
305 for (qw/stderr stdout stdres/) {
306 $self->drop_handle($pid, $_) if $proc->{"hdr_$_"};
307 }
308 }
309 $proc->{exit_status} = $exit_val;
310 $proc->{exit_core } = $core;
311 $proc->{exit_signal} = $signum;
312
313 # command timings...
314 my $te = time;
315 $proc->{time_stopped } = $te;
316 $proc->{time_duration_exec} = $te - $proc->{time_started};
317
318 # this process is no longer running
319 $proc->{running} = 0;
320
321 $self->complete($pid);
322 }
323
324 sub complete {
325 my ($self, $pid, $force) = @_;
326
327 my $proc = $self->get_proc($pid);
328
329 return 0 if !$force
330 && (
331 $proc->{running}
332 || defined $proc->{hdr_stdout}
333 || defined $proc->{hdr_stdres}
334 || defined $proc->{hdr_stderr}
335 );
336
337
338 if ($proc && %$proc) {
339 $self->log->debug("[process $pid]: All streams closed, process execution complete.");
340
341 $proc->{time_duration_total} = time - $proc->{time_started};
342
343 # fire exit callback!
344 if (defined $proc->{exit_cb} && ref $proc->{exit_cb} eq 'CODE') {
345 my $result = eval { $proc->{buf_stdres} ? thaw($proc->{buf_stdres}) : undef};
346
347 if ($@) {
348 croak "Error de-serializing subprocess data: $@";
349 }
350
351 # prepare callback structure
352 my $cb_d = {
353 cmd => ref $proc->{cmd} eq 'CODE' ? 'CODE' :
354 ref $proc->{cmd} eq 'ARRAY' ? join(' ', @{$proc->{cmd}}) :
355 $proc->{cmd}
356 ,
357 param => $proc->{param},
358 is_timeout => $proc->{is_timeout},
359 exit_status => $proc->{exit_status},
360 exit_signal => $proc->{exit_signal},
361 exit_core => $proc->{exit_core},
362 stdout => $proc->{buf_stdout},
363 stderr => ($proc->{buf_stderr} ? $proc->{buf_stderr} : '').($proc->{stderr} ? $proc->{stderr} : ''),
364 result => $result,
365 time_started => $proc->{time_started},
366 time_stopped => $proc->{time_stopped},
367 time_duration_exec => $proc->{time_duration_exec},
368 time_duration_total => $proc->{time_duration_total},
369 };
370
371 # safely invoke callback
372 $self->log->debug("[process $pid]: invoking exit_cb callback.");
373 eval { $proc->{exit_cb}->($pid, $cb_d); };
374
375 $self->log->error("[process $pid]: Error running exit_cb: $@") if $@;
376 } else {
377 $self->log->error("[process $pid]: No exit_cb callback!");
378 }
379 }
380
381 delete $self->{_data}->{$pid};
382 $self->num_forks($self->num_forks - 1);
383 }
384
385 sub _sig_chld {
386 my ($self) = @_;
387
388 no strict 'subs';
389
390 my $i = 0;
391 while ((my $pid = waitpid(-1, WNOHANG)) > 0) {
392 $i++;
393 my $exit_val = $? >> 8;
394 my $signum = $? & 127;
395 my $core = $? & 128;
396
397 # do process cleanup
398 $self->cleanup($pid, $exit_val, $signum, $core);
399 }
400
401 $self->log->debug("SIGCHLD handler cleaned up after $i process(es).")
402 if $i > 0;
403 }
404
405 sub _getRunStruct {
406 my ($self, $opt) = @_;
407
408 my $s = {
409 pid => 0,
410 cmd => undef,
411 param => undef,
412 error => undef,
413 stdout_cb => undef,
414 stderr_cb => undef,
415 exit_cb => undef,
416 is_timeout => undef,
417 exec_timeout => 0,
418 buf_stdout => '',
419 buf_stderr => '',
420 buf_stdres => '',
421 hdr_stdout => undef,
422 hdr_stderr => undef,
423 hdr_stdres => undef,
424 };
425
426 # apply user defined vars...
427 $s->{$_} = $opt->{$_}
428 for grep { exists $s->{$_} } keys %$opt;
429
430 return $s;
431 }
432
433 sub _validateRunStruct {
434 my ($self, $s) = @_;
435
436 # command?
437 $self->error('Undefined command.') and return
438 unless defined $s->{cmd};
439
440 # check command...
441 my $cmd_ref = ref $s->{cmd};
442 $self->error('Zero-length command.') and return
443 if $cmd_ref eq '' && length $s->{cmd} == 0;
444
445 $self->error('Command can be pure scalar, arrayref or coderef.') and return
446 if $cmd_ref ne '' && not defined first {$cmd_ref eq $_} ('CODE', 'ARRAY');
447
448 # callbacks...
449 $self->error("STDOUT callback defined, but is not code reference.") and return
450 if defined $s->{stdout_cb} && ref $s->{stdout_cb} ne 'CODE';
451
452 $self->error("STDERR callback defined, but is not code reference.") and return
453 if defined $s->{stderr_cb} && ref $s->{stderr_cb} ne 'CODE';
454
455 $self->error("Process exit_cb callback defined, but is not code reference.") and return
456 if defined $s->{exit_cb} && ref($s->{exit_cb}) ne 'CODE';
457
458 # exec timeout
459 { no warnings; $s->{exec_timeout} += 0; }
460
461 return 1;
462 }
463
464 sub _timeout_cb {
465 my ($self, $pid) = @_;
466
467 my $proc = $self->get_proc($pid);
468 return 0 unless $proc;
469
470 # drop timer (can't hurt...)
471 if (defined $proc->{id_timeout}) {
472 $self->ioloop->remove($proc->{id_timeout});
473 $proc->{id_timeout} = undef;
474 }
475
476 # is process still alive?
477 return 0 unless kill 0, $pid;
478
479 $self->log->debug("[process $pid]: Execution timeout ("
480 .sprintf("%-.3f seconds).", $proc->{exec_timeout})
481 ." Killing process.");
482
483 $proc->{stderr} .= ";Execution timeout.";
484 $proc->{is_timeout} = 1;
485
486 # kill the motherfucker!
487
488 unless (CORE::kill(9, $pid)) {
489 $self->log->warn("[process $pid]: Unable to kill process: $!");
490 }
491 $proc->{hard_kill} = 1;
492 $self->cleanup($pid, 0, 9, 0);
493
494 return 1;
495 }
496
497 sub kill {
498 my ($self, $pid, $signal) = @_;
499 $signal = 15 unless defined $signal;
500
501 my $proc = $self->get_proc($pid);
502 return 0 unless $proc;
503
504 # kill the process...
505 unless (kill($signal, $pid)) {
506 $self->error("Unable to send signal $signal to process $pid: $!");
507 return 0;
508 }
509
510 return 1;
511 }
512
513 sub DESTROY {
514 my ($self) = @_;
515
516 # perform cleanup...
517 unless ($self->is_child) {
518 foreach my $pid (keys %{$self->{_data}}) {
519 my $proc = $self->{_data}->{$pid};
520
521 $self->log->debug("Killing subprocess $pid with SIGKILL") if $self->log;
522 # kill process (HARD!)
523 $self->kill($pid, 9);
524
525 next unless defined $self->ioloop;
526
527 # drop fds
528 $self->drop_handle($pid, $_) for grep {$proc->{"hdr_$_"}} qw/stdout stderr stdres/;
529
530 # fire exit callbacks (if any)
531 $self->complete($pid, 1);
532 }
533 }
534
535 # disable sigchld hander
536 $SIG{'CHLD'} = 'IGNORE';
537 }
538
539 1;