| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package IO::Async::PID 0.805; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use v5.14; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use base qw( IO::Async::Notifier ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | C<IO::Async::PID> - event callback on exit of a child process | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =for highlighter language=perl | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | use IO::Async::PID; | ||||||||||||||||||||
| 23 | use POSIX qw( WEXITSTATUS ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | use IO::Async::Loop; | ||||||||||||||||||||
| 26 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | my $kid = $loop->fork( | ||||||||||||||||||||
| 29 | code => sub { | ||||||||||||||||||||
| 30 | print "Child sleeping..\n"; | ||||||||||||||||||||
| 31 | sleep 10; | ||||||||||||||||||||
| 32 | print "Child exiting\n"; | ||||||||||||||||||||
| 33 | return 20; | ||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | print "Child process $kid started\n"; | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | my $pid = IO::Async::PID->new( | ||||||||||||||||||||
| 40 | pid => $kid, | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | on_exit => sub { | ||||||||||||||||||||
| 43 | my ( $self, $exitcode ) = @_; | ||||||||||||||||||||
| 44 | printf "Child process %d exited with status %d\n", | ||||||||||||||||||||
| 45 | $self->pid, WEXITSTATUS($exitcode); | ||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | $loop->add( $pid ); | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | $loop->run; | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =head1 DESCRIPTION | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | This subclass of L<IO::Async::Notifier> invokes its callback when a process | ||||||||||||||||||||
| 56 | exits. | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | For most use cases, a L<IO::Async::Process> object provides more control of | ||||||||||||||||||||
| 59 | setting up the process, connecting filehandles to it, sending data to and | ||||||||||||||||||||
| 60 | receiving data from it. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =head1 EVENTS | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | The following events are invoked, either using subclass methods or CODE | ||||||||||||||||||||
| 67 | references in parameters: | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head2 on_exit $exitcode | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | Invoked when the watched process exits. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =head1 PARAMETERS | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =head2 pid => INT | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | The process ID to watch. Must be given before the object has been added to the | ||||||||||||||||||||
| 82 | containing L<IO::Async::Loop> object. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =head2 on_exit => CODE | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | CODE reference for the C<on_exit> event. | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | Once the C<on_exit> continuation has been invoked, the C<IO::Async::PID> | ||||||||||||||||||||
| 89 | object is removed from the containing L<IO::Async::Loop> object. | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | sub configure | ||||||||||||||||||||
| 94 | { | ||||||||||||||||||||
| 95 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
configure: called CC=5 SCAR=17.4
Pod
documented
| |||||||||||||||||||||
| 96 | 2 | my %params = @_; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 2 | ▶ | if( exists $params{pid} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 2 | ▶ | $self->loop and croak "Cannot configure 'pid' after adding to Loop"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 2 | $self->{pid} = delete $params{pid}; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 2 | ▶ | if( exists $params{on_exit} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 2 | $self->{on_exit} = delete $params{on_exit}; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 2 | undef $self->{cb}; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 2 | ▶ | if( my $loop = $self->loop ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 0 | $self->_remove_from_loop( $loop ); | |||||||||||||||||||
| 110 | 0 | $self->_add_to_loop( $loop ); | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 2 | $self->SUPER::configure( %params ); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | sub _add_to_loop | ||||||||||||||||||||
| 118 | { | ||||||||||||||||||||
| 119 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_add_to_loop: called CC=3 SCAR=11.4
| |||||||||||||||||||||
| 120 | 2 | my ( $loop ) = @_; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 2 | ▶ | $self->pid or croak "Require a 'pid' in $self"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 2 | $self->SUPER::_add_to_loop( @_ ); | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | # on_exit continuation gets passed PID value; need to replace that with | ||||||||||||||||||||
| 127 | # $self | ||||||||||||||||||||
| 128 | $self->{cb} ||= $self->_replace_weakself( sub { | ||||||||||||||||||||
| 129 | 2 | ▶ | my $self = shift or return; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 2 | my ( $exitcode ) = @_; | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 2 | $self->invoke_event( on_exit => $exitcode ); | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | # Since this is a oneshot, we'll have to remove it from the loop or | ||||||||||||||||||||
| 135 | # parent Notifier | ||||||||||||||||||||
| 136 | 2 | $self->remove_from_parent; | |||||||||||||||||||
| 137 | 2 | ▶ | } ); | ||||||||||||||||||
|
Condition33%1/3
$self->{"cb"} ||= $self->_replace_weakself(sub {
my $self = shift() or return;
my($exitcode) = @_;
$self->invoke_event("on_exit", $exitcode);
$self->remove_from_parent;
}
)
MC/DC0%0/2
$self->{"cb"} ||= $self->_replace_weakself(sub {
my $self = shift() or return;
my($exitcode) = @_;
$self->invoke_event("on_exit", $exitcode);
$self->remove_from_parent;
}
)
$self->{"cb"}
$self->_replace_weakself(sub {
my $self = shift() or return;
my($exitcode) = @_;
$self->invoke_event("on_exit", $exitcode);
$self->remove_from_parent;
}
)
Truth table33%1/3
A ||= B
A= $self->{"cb"}B= $self->_replace_weakself(sub {
my $self = shift() or return;
my($exitcode) = @_;
$self->invoke_event("on_exit", $exitcode);
$self->remove_from_parent;
}
)
| |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 2 | $loop->watch_process( $self->pid, $self->{cb} ); | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | sub _remove_from_loop | ||||||||||||||||||||
| 143 | { | ||||||||||||||||||||
| 144 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_remove_from_loop: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 145 | 2 | my ( $loop ) = @_; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 2 | $loop->unwatch_process( $self->pid ); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | sub notifier_name | ||||||||||||||||||||
| 151 | { | ||||||||||||||||||||
| 152 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
notifier_name: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 153 | 1 | ▶ | if( length( my $name = $self->SUPER::notifier_name ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 0 | return $name; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 1 | return $self->{pid}; | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =head1 METHODS | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 pid | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | $process_id = $pid->pid; | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | Returns the underlying process ID | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =cut | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | sub pid | ||||||||||||||||||||
| 173 | { | ||||||||||||||||||||
| 174 | 8 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
pid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 175 | 8 | return $self->{pid}; | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head2 kill | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | $pid->kill( $signal ); | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | Sends a signal to the process | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | sub kill | ||||||||||||||||||||
| 187 | { | ||||||||||||||||||||
| 188 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
kill: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 189 | 1 | my ( $signal ) = @_; | |||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 1 | ▶ | kill $signal, $self->pid or croak "Cannot kill() - $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =head1 AUTHOR | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =cut | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 2 | 0x55AA; | |||||||||||||||||||