| 1 |
package Test2::Harness::IPC; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use POSIX; |
| 8 |
|
| 9 |
use Config qw/%Config/; |
| 10 |
use Carp qw/croak confess/; |
| 11 |
use Time::HiRes qw/sleep time/; |
| 12 |
|
| 13 |
use Test2::Harness::Util::IPC qw/run_cmd USE_P_GROUPS/; |
| 14 |
|
| 15 |
use Test2::Harness::IPC::Process; |
| 16 |
|
| 17 |
BEGIN { |
| 18 |
my %SIG_MAP; |
| 19 |
my @SIGNAMES = split /\s+/, $Config{sig_name}; |
| 20 |
my @SIGNUMS = split /\s+/, $Config{sig_num}; |
| 21 |
while (@SIGNAMES) { |
| 22 |
$SIG_MAP{shift(@SIGNAMES)} = shift @SIGNUMS; |
| 23 |
} |
| 24 |
|
| 25 |
*SIG_MAP = sub() { \%SIG_MAP }; |
| 26 |
} |
| 27 |
|
| 28 |
use Test2::Harness::Util::HashBase qw{ |
| 29 |
<pid |
| 30 |
<handlers |
| 31 |
<procs |
| 32 |
<procs_by_cat |
| 33 |
<waiting |
| 34 |
<wait_time |
| 35 |
<started |
| 36 |
<sig_count |
| 37 |
}; |
| 38 |
|
| 39 |
sub init { |
| 40 |
my $self = shift; |
| 41 |
|
| 42 |
$self->{+PID} = $$; |
| 43 |
|
| 44 |
$self->{+PROCS} //= {}; |
| 45 |
$self->{+PROCS_BY_CAT} //= {}; |
| 46 |
|
| 47 |
$self->{+WAIT_TIME} = 0.02 unless defined $self->{+WAIT_TIME}; |
| 48 |
|
| 49 |
$self->{+HANDLERS} //= {}; |
| 50 |
$self->{+HANDLERS}->{CHLD} //= sub { 1 }; |
| 51 |
|
| 52 |
$self->{+SIG_COUNT} //= 0; |
| 53 |
} |
| 54 |
|
| 55 |
sub start { |
| 56 |
my $self = shift; |
| 57 |
|
| 58 |
my @caller = caller(1); |
| 59 |
|
| 60 |
return if $self->{+STARTED}; |
| 61 |
$self->{+STARTED} = 1; |
| 62 |
|
| 63 |
$self->check_for_fork(); |
| 64 |
|
| 65 |
for my $sig (qw/INT HUP TERM CHLD/) { |
| 66 |
croak "Signal '$sig' was already set by something else" |
| 67 |
if defined $SIG{$sig} |
| 68 |
&& $SIG{$sig} ne 'IGNORE' |
| 69 |
&& $SIG{$sig} ne 'DEFAULT'; |
| 70 |
$SIG{$sig} = sub { $self->handle_sig($sig) }; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
sub stop { |
| 75 |
my $self = shift; |
| 76 |
|
| 77 |
$self->wait(all => 1); |
| 78 |
|
| 79 |
delete $SIG{$_} for qw/INT HUP TERM CHLD/; |
| 80 |
|
| 81 |
$self->{+STARTED} = 0; |
| 82 |
} |
| 83 |
|
| 84 |
sub set_sig_handler { |
| 85 |
my $self = shift; |
| 86 |
my ($sig, $sub) = @_; |
| 87 |
$self->{+HANDLERS}->{$sig} = $sub; |
| 88 |
} |
| 89 |
|
| 90 |
sub handle_sig { |
| 91 |
my $self = shift; |
| 92 |
my ($sig) = @_; |
| 93 |
|
| 94 |
$self->{+SIG_COUNT}++ unless $sig eq 'CHLD'; |
| 95 |
|
| 96 |
return $self->{+HANDLERS}->{$sig}->($sig) if $self->{+HANDLERS}->{$sig}; |
| 97 |
|
| 98 |
$self->stop(); |
| 99 |
exit(SIG_MAP->{$sig}); |
| 100 |
} |
| 101 |
|
| 102 |
sub killall { |
| 103 |
my $self = shift; |
| 104 |
my ($sig) = @_; |
| 105 |
$sig //= 'TERM'; |
| 106 |
|
| 107 |
$self->check_for_fork(); |
| 108 |
|
| 109 |
if (USE_P_GROUPS) { |
| 110 |
kill($sig, map { -$_ } keys %{$self->{+PROCS}}); |
| 111 |
} |
| 112 |
else { |
| 113 |
kill($sig, keys %{$self->{+PROCS}}); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
sub check_timeouts {} |
| 118 |
|
| 119 |
sub check_for_fork { |
| 120 |
my $self = shift; |
| 121 |
|
| 122 |
return 0 if $self->{+PID} == $$; |
| 123 |
|
| 124 |
$self->{+PROCS} = {}; |
| 125 |
$self->{+PROCS_BY_CAT} = {}; |
| 126 |
$self->{+WAITING} = {}; |
| 127 |
$self->{+PID} = $$; |
| 128 |
|
| 129 |
return 1; |
| 130 |
} |
| 131 |
|
| 132 |
sub _bring_out_yer_dead { |
| 133 |
my $self = shift; |
| 134 |
|
| 135 |
my $procs = $self->{+PROCS} //= {}; |
| 136 |
my $waiting = $self->{+WAITING} //= {}; |
| 137 |
|
| 138 |
local $?; |
| 139 |
|
| 140 |
# Wait on any/all pids |
| 141 |
my $found = 0; |
| 142 |
while ((my $pid = waitpid(-1, WNOHANG)) > 0) { |
| 143 |
my $exit = $?; |
| 144 |
die "waitpid returned pid '$pid', but we are not monitoring that one!" unless $procs->{$pid}; |
| 145 |
$found++; |
| 146 |
$waiting->{$pid} = [$exit, time()]; |
| 147 |
} |
| 148 |
|
| 149 |
return $found; |
| 150 |
} |
| 151 |
|
| 152 |
sub _check_if_dead_yet { |
| 153 |
my $self = shift; |
| 154 |
|
| 155 |
my $procs = $self->{+PROCS} //= {}; |
| 156 |
my $cat_procs = $self->{+PROCS_BY_CAT} //= {}; |
| 157 |
my $waiting = $self->{+WAITING} //= {}; |
| 158 |
|
| 159 |
my $found = 0; |
| 160 |
for my $pid (keys %$waiting) { |
| 161 |
next if USE_P_GROUPS && kill(0, -$pid); |
| 162 |
$found++; |
| 163 |
my $args = delete $waiting->{$pid}; |
| 164 |
my $proc = delete $procs->{$pid}; |
| 165 |
delete $cat_procs->{$proc->category}->{$pid}; |
| 166 |
$self->set_proc_exit($proc, @$args); |
| 167 |
} |
| 168 |
|
| 169 |
return $found; |
| 170 |
} |
| 171 |
|
| 172 |
sub set_proc_exit { |
| 173 |
my $self = shift; |
| 174 |
my ($proc, @args) = @_; |
| 175 |
$proc->set_exit($self, @args); |
| 176 |
} |
| 177 |
|
| 178 |
sub _ex_parrots { |
| 179 |
my $self = shift; |
| 180 |
|
| 181 |
my $procs = $self->{+PROCS} //= {}; |
| 182 |
my $cat_procs = $self->{+PROCS_BY_CAT} //= {}; |
| 183 |
my $waiting = $self->{+WAITING} //= {}; |
| 184 |
|
| 185 |
my $found = 0; |
| 186 |
for my $pid (keys %$procs) { |
| 187 |
next if $waiting->{$pid}; |
| 188 |
next if kill(0, $pid); |
| 189 |
$found++; |
| 190 |
warn "Process $pid vanished!"; |
| 191 |
$waiting->{$pid} = [-1, time()]; |
| 192 |
} |
| 193 |
|
| 194 |
return $found; |
| 195 |
} |
| 196 |
|
| 197 |
sub wait { |
| 198 |
my $self = shift; |
| 199 |
my %params = @_; |
| 200 |
|
| 201 |
$self->check_for_fork(); |
| 202 |
|
| 203 |
my $sig_count = $self->{+SIG_COUNT}; |
| 204 |
|
| 205 |
my $procs = $self->{+PROCS} //= {}; |
| 206 |
my $cat_procs = $self->{+PROCS_BY_CAT} //= {}; |
| 207 |
my $waiting = $self->{+WAITING} //= {}; |
| 208 |
|
| 209 |
return 0 unless keys(%$procs) || keys(%$waiting); |
| 210 |
|
| 211 |
my $cat_total = $params{cat} ? keys %{$cat_procs->{$params{cat}}} : 0; |
| 212 |
|
| 213 |
my $start = time; |
| 214 |
|
| 215 |
my $count = 0; |
| 216 |
my $found = 0; |
| 217 |
while (1) { |
| 218 |
$self->check_timeouts; |
| 219 |
|
| 220 |
$found += $self->_bring_out_yer_dead(); |
| 221 |
$found += $self->_check_if_dead_yet(); |
| 222 |
|
| 223 |
return $found if $self->_wait_done($found, $start, \%params); |
| 224 |
|
| 225 |
if (my $cat = $params{cat}) { |
| 226 |
my $cur_total = keys %{$cat_procs->{$cat}}; |
| 227 |
return 0 unless $cur_total; |
| 228 |
my $delta = $cat_total - $cur_total; |
| 229 |
return $delta if $delta; |
| 230 |
} |
| 231 |
|
| 232 |
# This is expensive, so only do it if we are gonna end up waiting |
| 233 |
# anyway If we do find anything here do not bother waiting. |
| 234 |
next if $self->_ex_parrots(); |
| 235 |
|
| 236 |
# Break the loop if we had a signal come in since starting |
| 237 |
last if $self->{+SIG_COUNT} > $sig_count; |
| 238 |
|
| 239 |
sleep($self->{+WAIT_TIME}) if $self->{+WAIT_TIME}; |
| 240 |
} |
| 241 |
|
| 242 |
warn "We escaped the wait cycle"; |
| 243 |
return $found; |
| 244 |
} |
| 245 |
|
| 246 |
sub _wait_done { |
| 247 |
my $self = shift; |
| 248 |
my ($found, $start, $params) = @_; |
| 249 |
|
| 250 |
my $all = keys(%{$self->{+PROCS}}); |
| 251 |
return 1 unless $all; |
| 252 |
|
| 253 |
return 1 if $params->{timeout} && time - $start >= $params->{timeout}; |
| 254 |
|
| 255 |
return 0 if $all && $params->{all}; |
| 256 |
|
| 257 |
return 0 if $params->{all_cat} && keys %{$self->{+PROCS_BY_CAT}->{$params->{all_cat}}}; |
| 258 |
|
| 259 |
return 0 if $params->{block} && !$found; |
| 260 |
|
| 261 |
# This gets validated outside this loop |
| 262 |
return 0 if $params->{cat}; |
| 263 |
|
| 264 |
return 1; |
| 265 |
} |
| 266 |
|
| 267 |
sub watch_pid { |
| 268 |
my $self = shift; |
| 269 |
my ($pid) = @_; |
| 270 |
|
| 271 |
my $proc = Test2::Harness::IPC::Process->new(pid => $pid); |
| 272 |
|
| 273 |
return $self->watch($proc); |
| 274 |
} |
| 275 |
|
| 276 |
sub watch { |
| 277 |
my $self = shift; |
| 278 |
my ($proc) = @_; |
| 279 |
|
| 280 |
$self->check_for_fork(); |
| 281 |
|
| 282 |
my $pid = $proc->pid or confess "Process has no pid"; |
| 283 |
$pid = abs($pid) if USE_P_GROUPS; |
| 284 |
|
| 285 |
croak "Already watching pid $pid" if exists $self->{+PROCS}->{$pid}; |
| 286 |
|
| 287 |
$self->{+PROCS}->{$pid} = $proc; |
| 288 |
$self->{+PROCS_BY_CAT}->{$proc->category}->{$pid} = $proc; |
| 289 |
} |
| 290 |
|
| 291 |
sub spawn { |
| 292 |
my $self = shift; |
| 293 |
my ($proc, $params); |
| 294 |
if (@_ == 1) { |
| 295 |
$proc = shift(@_); |
| 296 |
$params = $proc->spawn_params; |
| 297 |
} |
| 298 |
else { |
| 299 |
$params = {@_}; |
| 300 |
my $class = $params->{process_class} // 'Test2::Harness::IPC::Process'; |
| 301 |
$proc = $class->new(); |
| 302 |
} |
| 303 |
|
| 304 |
croak "No 'command' specified" unless $params->{command}; |
| 305 |
|
| 306 |
my $caller1 = [caller()]; |
| 307 |
my $caller2 = [caller(1)]; |
| 308 |
|
| 309 |
my $env = $params->{env_vars} // {}; |
| 310 |
|
| 311 |
$self->check_for_fork(); |
| 312 |
|
| 313 |
my $pid = run_cmd(env => $env, caller1 => $caller1, caller2 => $caller2, %$params); |
| 314 |
$proc->set_pid($pid); |
| 315 |
|
| 316 |
$self->watch($proc); |
| 317 |
return $proc; |
| 318 |
} |
| 319 |
|
| 320 |
1; |
| 321 |
|
| 322 |
__END__ |
| 323 |
|
| 324 |
=pod |
| 325 |
|
| 326 |
=encoding UTF-8 |
| 327 |
|
| 328 |
=head1 NAME |
| 329 |
|
| 330 |
Test2::Harness::IPC - Base class for modules that control child processes. |
| 331 |
|
| 332 |
=head1 DESCRIPTION |
| 333 |
|
| 334 |
This module is the base class for all parts of L<Test2::Harness> that have to |
| 335 |
do process management. |
| 336 |
|
| 337 |
=head1 ATTRIBUTES |
| 338 |
|
| 339 |
=over 4 |
| 340 |
|
| 341 |
=item $pid = $ipc->pid |
| 342 |
|
| 343 |
The root PID of the IPC object. |
| 344 |
|
| 345 |
=item $hashref = $ipc->handlers |
| 346 |
|
| 347 |
Custom signal handlers specific to the IPC object. |
| 348 |
|
| 349 |
=item $hashref = $ipc->procs |
| 350 |
|
| 351 |
Hashref of C<< $pid => $proc >> where $proc is an instance of |
| 352 |
L<Test2::Harness::IPC::Proc>. |
| 353 |
|
| 354 |
=item $hashref = $ipc->procs_by_cat |
| 355 |
|
| 356 |
Hashref of C<< $category => { $pid => $proc } >>. |
| 357 |
|
| 358 |
=item $hashref = $ipc->waiting |
| 359 |
|
| 360 |
Hashref of processes that have finished, but have not been handled yet. |
| 361 |
|
| 362 |
This is an implementation detail you should not rely on. |
| 363 |
|
| 364 |
=item $float = $ipc->wait_time |
| 365 |
|
| 366 |
How long to sleep between loops when in a wait cycle. |
| 367 |
|
| 368 |
=item $bool = $ipc->started |
| 369 |
|
| 370 |
True if the IPC process has started. |
| 371 |
|
| 372 |
=item $ipc->sig_count |
| 373 |
|
| 374 |
Implementation detail, used to break wait loops when signals are received. |
| 375 |
|
| 376 |
=back |
| 377 |
|
| 378 |
=head1 METHODS |
| 379 |
|
| 380 |
=over 4 |
| 381 |
|
| 382 |
=item $ipc->start |
| 383 |
|
| 384 |
Start the IPC management (Insert signal handlers). |
| 385 |
|
| 386 |
=item $ipc->stop |
| 387 |
|
| 388 |
Stop the IPC management (Remove signal handlers). |
| 389 |
|
| 390 |
=item $ipc->set_sig_handler($sig, sub { ... }) |
| 391 |
|
| 392 |
Set a custom signal handler. This is a safer version of |
| 393 |
C<< local %SIG{$sig} >> for use with IPC. |
| 394 |
|
| 395 |
The callback will get exactly one argument, the name of the signal that was |
| 396 |
received. |
| 397 |
|
| 398 |
=item $ipc->handle_sig($sig) |
| 399 |
|
| 400 |
Handle the specified signal. Will cause process exit if the signal has no |
| 401 |
handler. |
| 402 |
|
| 403 |
=item $ipc->killall() |
| 404 |
|
| 405 |
=item $ipc->killall($sig) |
| 406 |
|
| 407 |
Kill all tracked child process with the given signal. C<TERM> is used if no |
| 408 |
signal is specified. |
| 409 |
|
| 410 |
This will not wait on the processes, you must call C<< $ipc->wait() >>. |
| 411 |
|
| 412 |
=item $ipc->check_timeouts |
| 413 |
|
| 414 |
This is a no-op on the IPC base class. This is called every loop of |
| 415 |
C<< $ipc->wait >>. If you subclass the IPC class you can fill this in to make |
| 416 |
processes timeout if needed. |
| 417 |
|
| 418 |
=item $ipc->check_for_fork |
| 419 |
|
| 420 |
This is used a lot internally to check if this is a forked process. If this is |
| 421 |
a forked process the IPC object is completely reset with no remaining internal |
| 422 |
state (except signal handlers). |
| 423 |
|
| 424 |
=item $ipc->set_proc_exit($proc, @args) |
| 425 |
|
| 426 |
Calls C<< $proc->set_exit(@args) >>. This is called by C<< $ipc->wait >>. You |
| 427 |
can override it to add custom tasks when a process exits. |
| 428 |
|
| 429 |
=item $int = $ipc->wait() |
| 430 |
|
| 431 |
=item $int = $ipc->wait(%params) |
| 432 |
|
| 433 |
Wait on processes, return the number found. |
| 434 |
|
| 435 |
Default is non-blocking. |
| 436 |
|
| 437 |
Options: |
| 438 |
|
| 439 |
=over 4 |
| 440 |
|
| 441 |
=item timeout => $float |
| 442 |
|
| 443 |
If a blocking parameter is provided this can be used to break the wait after a |
| 444 |
timeout. L<Time::HiRes> is used, so timeout is in seconds with decimals. |
| 445 |
|
| 446 |
=item all => $bool |
| 447 |
|
| 448 |
Block until B<ALL> processes are done. |
| 449 |
|
| 450 |
=item cat => $category |
| 451 |
|
| 452 |
Block until at least 1 process from the category is complete. |
| 453 |
|
| 454 |
=item all_cat => $category |
| 455 |
|
| 456 |
Block until B<ALL> processes from the category are complete. |
| 457 |
|
| 458 |
=item block => $bool |
| 459 |
|
| 460 |
Block until at least 1 process is complete. |
| 461 |
|
| 462 |
=back |
| 463 |
|
| 464 |
=item $ipc->watch($proc) |
| 465 |
|
| 466 |
Add a process to be monitored. |
| 467 |
|
| 468 |
=item $proc = $ipc->spawn($proc) |
| 469 |
|
| 470 |
=item $proc = $ipc->spawn(%params) |
| 471 |
|
| 472 |
In the first form $proc is an instance of L<Test2::Harness::IPC::Proc> that |
| 473 |
provides C<spawn_params()>. |
| 474 |
|
| 475 |
In the second form the following params are allowed: |
| 476 |
|
| 477 |
Anything supported by C<run_cmd()> in L<Test2::Harness::Util::IPC>. |
| 478 |
|
| 479 |
=over 4 |
| 480 |
|
| 481 |
=item process_class => $CLASS |
| 482 |
|
| 483 |
Default is L<Test2::Harness::IPC::Process>. |
| 484 |
|
| 485 |
=item command => $command |
| 486 |
|
| 487 |
Program command to call. This is required. |
| 488 |
|
| 489 |
=item env_vars => { ... } |
| 490 |
|
| 491 |
Specify custom environment variables for the new process. |
| 492 |
|
| 493 |
=back |
| 494 |
|
| 495 |
=back |
| 496 |
|
| 497 |
=head1 SOURCE |
| 498 |
|
| 499 |
The source code repository for Test2-Harness can be found at |
| 500 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 501 |
|
| 502 |
=head1 MAINTAINERS |
| 503 |
|
| 504 |
=over 4 |
| 505 |
|
| 506 |
=item Chad Granum E<lt>[email protected]<gt> |
| 507 |
|
| 508 |
=back |
| 509 |
|
| 510 |
=head1 AUTHORS |
| 511 |
|
| 512 |
=over 4 |
| 513 |
|
| 514 |
=item Chad Granum E<lt>[email protected]<gt> |
| 515 |
|
| 516 |
=back |
| 517 |
|
| 518 |
=head1 COPYRIGHT |
| 519 |
|
| 520 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 521 |
|
| 522 |
This program is free software; you can redistribute it and/or |
| 523 |
modify it under the same terms as Perl itself. |
| 524 |
|
| 525 |
See F<http://dev.perl.org/licenses/> |
| 526 |
|
| 527 |
=cut |