| 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, 2007-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | package IO::Async::Internals::ChildManager 0.805; | ||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use v5.14; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | # Not a notifier | ||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 40 | ▶ | use IO::Async::Stream; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 40 | ▶ | use IO::Async::OS; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 40 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 18 | 40 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 40 | ▶ | use POSIX qw( _exit dup dup2 nice ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 40 | ▶ | use constant LENGTH_OF_I => length( pack( "I", 0 ) ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | # Writing to variables of $> and $) have tricky ways to obtain error results | ||||||||||||||||||||||||||||||||||
| 25 | sub setuid | ||||||||||||||||||||||||||||||||||
| 26 | { | ||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my ( $uid ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
setuid: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 0 | $> = $uid; my $saved_errno = $!; | |||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $> == $uid and return 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | 0 | $! = $saved_errno; | |||||||||||||||||||||||||||||||||
| 33 | 0 | return undef; | |||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | sub setgid | ||||||||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my ( $gid ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
setgid: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | 0 | $) = $gid; my $saved_errno = $!; | |||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $) == $gid and return 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | 0 | $! = $saved_errno; | |||||||||||||||||||||||||||||||||
| 44 | 0 | return undef; | |||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | sub setgroups | ||||||||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my @groups = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
setgroups: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | 0 | my $gid = $)+0; | |||||||||||||||||||||||||||||||||
| 52 | # Put the primary GID as the first group in the supplementary list, because | ||||||||||||||||||||||||||||||||||
| 53 | # some operating systems ignore this position, expecting it to indeed be | ||||||||||||||||||||||||||||||||||
| 54 | # the primary GID. | ||||||||||||||||||||||||||||||||||
| 55 | # See | ||||||||||||||||||||||||||||||||||
| 56 | # https://rt.cpan.org/Ticket/Display.html?id=65127 | ||||||||||||||||||||||||||||||||||
| 57 | 0 | @groups = grep { $_ != $gid } @groups; | |||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | 0 | $) = "$gid $gid " . join " ", @groups; my $saved_errno = $!; | |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | # No easy way to detect success or failure. Just check that we have all and | ||||||||||||||||||||||||||||||||||
| 62 | # only the right groups | ||||||||||||||||||||||||||||||||||
| 63 | 0 | my %gotgroups = map { $_ => 1 } split ' ', "$)"; | |||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | 0 | $! = $saved_errno; | |||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $gotgroups{$_}-- or return undef for @groups; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | keys %gotgroups or return undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | 0 | return 1; | |||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | # Internal constructor | ||||||||||||||||||||||||||||||||||
| 73 | sub new | ||||||||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||||||||
| 75 | 40 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 76 | 40 | my ( %params ) = @_; | |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | 40 | ▶ | my $loop = delete $params{loop} or croak "Expected a 'loop'"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | 40 | my $self = bless { | |||||||||||||||||||||||||||||||||
| 81 | loop => $loop, | ||||||||||||||||||||||||||||||||||
| 82 | }, $class; | ||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 40 | weaken( $self->{loop} ); | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 40 | return $self; | |||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | sub spawn_child | ||||||||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||||||||
| 91 | 334 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
spawn_child: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 92 | 334 | my %params = @_; | |||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 334 | my $command = delete $params{command}; | |||||||||||||||||||||||||||||||||
| 95 | 334 | my $code = delete $params{code}; | |||||||||||||||||||||||||||||||||
| 96 | 334 | my $setup = delete $params{setup}; | |||||||||||||||||||||||||||||||||
| 97 | 334 | my $on_exit = delete $params{on_exit}; | |||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | 334 | ▶ | if( %params ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 100 | 4 | croak "Unrecognised options to spawn: " . join( ",", keys %params ); | |||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | 330 | ▶ | defined $command and defined $code and | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $command and defined $code
MC/DC100%2/2
defined $command and defined $code
defined $command
defined $code
Truth table100%3/3
A and B
A= defined $commandB= defined $code
| |||||||||||||||||||||||||||||||||||
| 104 | croak "Cannot pass both 'command' and 'code' to spawn"; | ||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | 326 | ▶ | defined $command or defined $code or | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $command or defined $code
MC/DC100%2/2
defined $command or defined $code
defined $command
defined $code
Truth table100%3/3
A or B
A= defined $commandB= defined $code
| |||||||||||||||||||||||||||||||||||
| 107 | croak "Must pass one of 'command' or 'code' to spawn"; | ||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | 322 | ▶ | my @setup = defined $setup ? $self->_check_setup_and_canonicise( $setup ) : (); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | 320 | my $loop = $self->{loop}; | |||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | 320 | my ( $readpipe, $writepipe ); | |||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||||||||
| 116 | # Ensure it's FD_CLOEXEC - this is a bit more portable than manually | ||||||||||||||||||||||||||||||||||
| 117 | # fiddling with F_GETFL and F_SETFL (e.g. MSWin32) | ||||||||||||||||||||||||||||||||||
| 118 | 320 | local $^F = -1; | |||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | 320 | ▶ | ( $readpipe, $writepipe ) = IO::Async::OS->pipepair or croak "Cannot pipe() - $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 121 | 320 | $readpipe->blocking( 0 ); | |||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | 320 | ▶ | if( defined $command ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 125 | 123 | ▶ | my @command = ref( $command ) ? @$command : ( $command ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | $code = sub { | ||||||||||||||||||||||||||||||||||
| 128 | 40 | ▶ | no warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 129 | 28 | ▶ | exec( @command ); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||
| 130 | 0 | return; | |||||||||||||||||||||||||||||||||
| 131 | 123 | }; | |||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | my $kid = $loop->fork( | ||||||||||||||||||||||||||||||||||
| 135 | code => sub { | ||||||||||||||||||||||||||||||||||
| 136 | # Child | ||||||||||||||||||||||||||||||||||
| 137 | 29 | ▶ | close( $readpipe ); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 138 | 29 | $self->_spawn_in_child( $writepipe, $code, \@setup ); | |||||||||||||||||||||||||||||||||
| 139 | }, | ||||||||||||||||||||||||||||||||||
| 140 | 320 | ); | |||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | # Parent | ||||||||||||||||||||||||||||||||||
| 143 | 291 | close( $writepipe ); | |||||||||||||||||||||||||||||||||
| 144 | 291 | return $self->_spawn_in_parent( $readpipe, $kid, $on_exit ); | |||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||
| 147 | sub _check_setup_and_canonicise | ||||||||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||||||||
| 149 | 299 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_check_setup_and_canonicise: called CC=30 SCAR=39.7
| |||||||||||||||||||||||||||||||||||
| 150 | 299 | my ( $setup ) = @_; | |||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | 299 | ▶ | ref $setup eq "ARRAY" or croak "'setup' must be an ARRAY reference"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | 298 | ▶ | return () if !@$setup; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||
| 156 | 245 | my @setup; | |||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | my $has_setgroups; | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | 245 | foreach my $i ( 0 .. $#$setup / 2 ) { | |||||||||||||||||||||||||||||||||
| 161 | 423 | my ( $key, $value ) = @$setup[$i*2, $i*2 + 1]; | |||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||
| 163 | # Rewrite stdin/stdout/stderr | ||||||||||||||||||||||||||||||||||
| 164 | 423 | ▶ | $key eq "stdin" and $key = "fd0"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 165 | 423 | ▶ | $key eq "stdout" and $key = "fd1"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 166 | 423 | ▶ | $key eq "stderr" and $key = "fd2"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | # Rewrite other filehandles | ||||||||||||||||||||||||||||||||||
| 169 | 423 | ▶ | ref $key and eval { $key->fileno; 1 } and $key = "fd" . $key->fileno; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $key and eval {
do {
$key->fileno;
1
}
}
MC/DC50%1/2
ref $key and eval {
do {
$key->fileno;
1
}
}
ref $key
eval {
do {
$key->fileno;
1
}
}
Truth table66%2/3
A and B
A= ref $keyB= eval {
do {
$key->fileno;
1
}
}
| |||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | 423 | ▶ | if( $key =~ m/^fd(\d+)$/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 172 | 417 | my $fd = $1; | |||||||||||||||||||||||||||||||||
| 173 | 417 | my $ref = ref $value; | |||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | 417 | ▶ | if( !$ref ) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 176 | 128 | $value = [ $value ]; | |||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||
| 178 | elsif( $ref eq "ARRAY" ) { | ||||||||||||||||||||||||||||||||||
| 179 | # Already OK | ||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||
| 181 | 2 | ▶ | elsif( $ref eq "GLOB" or eval { $value->isa( "IO::Handle" ) } ) { | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$ref eq "GLOB" or eval {
do {
$value->isa("IO::Handle")
}
}
MC/DC0%0/2
$ref eq "GLOB" or eval {
do {
$value->isa("IO::Handle")
}
}
$ref eq "GLOB"
eval {
do {
$value->isa("IO::Handle")
}
}
Truth table66%2/3
A or B
A= $ref eq "GLOB"B= eval {
do {
$value->isa("IO::Handle")
}
}
| |||||||||||||||||||||||||||||||||||
| 182 | 38 | $value = [ 'dup', $value ]; | |||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||
| 184 | else { | ||||||||||||||||||||||||||||||||||
| 185 | 0 | croak "Unrecognised reference type '$ref' for file descriptor $fd"; | |||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | 417 | my $operation = $value->[0]; | |||||||||||||||||||||||||||||||||
| 189 | 417 | ▶ | grep { $_ eq $operation } qw( open close dup keep ) or | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 190 | croak "Unrecognised operation '$operation' for file descriptor $fd"; | ||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||
| 192 | elsif( $key eq "env" ) { | ||||||||||||||||||||||||||||||||||
| 193 | 3 | ▶ | ref $value eq "HASH" or croak "Expected HASH reference for 'env' setup key"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||
| 195 | elsif( $key eq "nice" ) { | ||||||||||||||||||||||||||||||||||
| 196 | 1 | ▶ | $value =~ m/^\d+$/ or croak "Expected integer for 'nice' setup key"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||
| 198 | elsif( $key eq "chdir" ) { | ||||||||||||||||||||||||||||||||||
| 199 | # This isn't a purely watertight test, but it does guard against | ||||||||||||||||||||||||||||||||||
| 200 | # silly things like passing a reference - directories such as | ||||||||||||||||||||||||||||||||||
| 201 | # ARRAY(0x12345) are unlikely to exist | ||||||||||||||||||||||||||||||||||
| 202 | 1 | ▶ | -d $value or croak "Working directory '$value' does not exist"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||
| 204 | elsif( $key eq "setuid" ) { | ||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | $value =~ m/^\d+$/ or croak "Expected integer for 'setuid' setup key"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||
| 207 | elsif( $key eq "setgid" ) { | ||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | $value =~ m/^\d+$/ or croak "Expected integer for 'setgid' setup key"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | $has_setgroups and carp "It is suggested to 'setgid' before 'setgroups'"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||
| 211 | elsif( $key eq "setgroups" ) { | ||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | ref $value eq "ARRAY" or croak "Expected ARRAY reference for 'setgroups' setup key"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | m/^\d+$/ or croak "Expected integer in 'setgroups' array" for @$value; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 214 | 0 | $has_setgroups = 1; | |||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||
| 216 | else { | ||||||||||||||||||||||||||||||||||
| 217 | 1 | croak "Unrecognised setup operation '$key'"; | |||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||
| 220 | 422 | push @setup, $key => $value; | |||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||
| 223 | 244 | return @setup; | |||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||
| 226 | sub _spawn_in_parent | ||||||||||||||||||||||||||||||||||
| 227 | { | ||||||||||||||||||||||||||||||||||
| 228 | 291 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_spawn_in_parent: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 229 | 291 | my ( $readpipe, $kid, $on_exit ) = @_; | |||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||
| 231 | 291 | my $loop = $self->{loop}; | |||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||
| 233 | # We need to wait for both the errno pipe to close, and for waitpid | ||||||||||||||||||||||||||||||||||
| 234 | # to give us an exit code. We'll form two closures over these two | ||||||||||||||||||||||||||||||||||
| 235 | # variables so we can cope with those happening in either order | ||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||
| 237 | 291 | my $dollarbang; | |||||||||||||||||||||||||||||||||
| 238 | 291 | my ( $dollarat, $length_dollarat ); | |||||||||||||||||||||||||||||||||
| 239 | 291 | my $exitcode; | |||||||||||||||||||||||||||||||||
| 240 | 291 | my $pipeclosed = 0; | |||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||
| 242 | $loop->add( IO::Async::Stream->new( | ||||||||||||||||||||||||||||||||||
| 243 | notifier_name => "statuspipe,kid=$kid", | ||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||
| 245 | read_handle => $readpipe, | ||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||
| 247 | on_read => sub { | ||||||||||||||||||||||||||||||||||
| 248 | 658 | ▶ | my ( $self, $buffref, $eof ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||
| 250 | 658 | ▶ | if( !defined $dollarbang ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 251 | 284 | ▶ | if( length( $$buffref ) >= 2 * LENGTH_OF_I ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 252 | 190 | ( $dollarbang, $length_dollarat ) = unpack( "II", $$buffref ); | |||||||||||||||||||||||||||||||||
| 253 | 190 | substr( $$buffref, 0, 2 * LENGTH_OF_I, "" ); | |||||||||||||||||||||||||||||||||
| 254 | 190 | return 1; | |||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||
| 257 | elsif( !defined $dollarat ) { | ||||||||||||||||||||||||||||||||||
| 258 | 187 | ▶ | if( length( $$buffref ) >= $length_dollarat ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 259 | 187 | $dollarat = substr( $$buffref, 0, $length_dollarat, "" ); | |||||||||||||||||||||||||||||||||
| 260 | 187 | return 1; | |||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||
| 264 | 281 | ▶ | if( $eof ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 265 | 281 | ▶ | $dollarbang = 0 if !defined $dollarbang; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 266 | 281 | ▶ | if( !defined $length_dollarat ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 267 | 94 | $length_dollarat = 0; | |||||||||||||||||||||||||||||||||
| 268 | 94 | $dollarat = ""; | |||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||
| 271 | 281 | $pipeclosed = 1; | |||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||
| 273 | 281 | ▶ | if( defined $exitcode ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 274 | 87 | local $! = $dollarbang; | |||||||||||||||||||||||||||||||||
| 275 | 87 | $on_exit->( $kid, $exitcode, $!, $dollarat ); | |||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||
| 279 | 281 | return 0; | |||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||
| 281 | 291 | ) ); | |||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||
| 283 | $loop->watch_process( $kid => sub { | ||||||||||||||||||||||||||||||||||
| 284 | 281 | ▶ | ( my $kid, $exitcode ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||
| 286 | 281 | ▶ | if( $pipeclosed ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 287 | 192 | local $! = $dollarbang; | |||||||||||||||||||||||||||||||||
| 288 | 192 | $on_exit->( $kid, $exitcode, $!, $dollarat ); | |||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||
| 290 | 291 | } ); | |||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||
| 292 | 291 | return $kid; | |||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||
| 295 | sub _spawn_in_child | ||||||||||||||||||||||||||||||||||
| 296 | { | ||||||||||||||||||||||||||||||||||
| 297 | 29 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_spawn_in_child: called CC=39 SCAR=52.9
| |||||||||||||||||||||||||||||||||||
| 298 | 29 | my ( $writepipe, $code, $setup ) = @_; | |||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||
| 300 | 29 | my $exitvalue = eval { | |||||||||||||||||||||||||||||||||
| 301 | # Map of which handles will be in use by the end | ||||||||||||||||||||||||||||||||||
| 302 | 29 | my %fd_in_use = ( 0 => 1, 1 => 1, 2 => 1 ); # Keep STDIN, STDOUT, STDERR | |||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||
| 304 | # Count of how many times we'll need to use the current handles. | ||||||||||||||||||||||||||||||||||
| 305 | 29 | my %fds_refcount = %fd_in_use; | |||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||
| 307 | # To dup2() without clashes we might need to temporarily move some handles | ||||||||||||||||||||||||||||||||||
| 308 | 29 | my %dup_from; | |||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||
| 310 | 29 | my $max_fd = 0; | |||||||||||||||||||||||||||||||||
| 311 | 29 | my $writepipe_clashes = 0; | |||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||
| 313 | 29 | ▶ | if( @$setup ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 314 | # The writepipe might be in the way of a setup filedescriptor. If it | ||||||||||||||||||||||||||||||||||
| 315 | # is we'll have to dup2 it out of the way then close the original. | ||||||||||||||||||||||||||||||||||
| 316 | 22 | foreach my $i ( 0 .. $#$setup/2 ) { | |||||||||||||||||||||||||||||||||
| 317 | 41 | my ( $key, $value ) = @$setup[$i*2, $i*2 + 1]; | |||||||||||||||||||||||||||||||||
| 318 | 41 | ▶ | $key =~ m/^fd(\d+)$/ or next; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 319 | 41 | my $fd = $1; | |||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||
| 321 | 41 | ▶ | $max_fd = $fd if $fd > $max_fd; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 322 | 41 | ▶ | $writepipe_clashes = 1 if $fd == fileno $writepipe; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||
| 324 | 41 | my ( $operation, @params ) = @$value; | |||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||
| 326 | 41 | ▶ | $operation eq "close" and do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 327 | 0 | delete $fd_in_use{$fd}; | |||||||||||||||||||||||||||||||||
| 328 | 0 | delete $fds_refcount{$fd}; | |||||||||||||||||||||||||||||||||
| 329 | }; | ||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||
| 331 | 41 | ▶ | $operation eq "dup" and do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 332 | 39 | $fd_in_use{$fd} = 1; | |||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||
| 334 | 39 | my $fileno = fileno $params[0]; | |||||||||||||||||||||||||||||||||
| 335 | # Keep a count of how many times it will be dup'ed from so we | ||||||||||||||||||||||||||||||||||
| 336 | # can close it once we've finished | ||||||||||||||||||||||||||||||||||
| 337 | 39 | $fds_refcount{$fileno}++; | |||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||
| 339 | 39 | $dup_from{$fileno} = $fileno; | |||||||||||||||||||||||||||||||||
| 340 | }; | ||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||
| 342 | 41 | ▶ | $operation eq "keep" and do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 343 | 2 | $fds_refcount{$fd} = 1; | |||||||||||||||||||||||||||||||||
| 344 | }; | ||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||
| 348 | 29 | foreach ( IO::Async::OS->potentially_open_fds ) { | |||||||||||||||||||||||||||||||||
| 349 | 495 | ▶ | next if $fds_refcount{$_}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 350 | 367 | ▶ | next if $_ == fileno $writepipe; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 351 | 338 | POSIX::close( $_ ); | |||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||
| 354 | 29 | ▶ | if( @$setup ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 355 | 22 | ▶ | if( $writepipe_clashes ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 356 | 0 | $max_fd++; | |||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | dup2( fileno $writepipe, $max_fd ) or die "Cannot dup2(writepipe to $max_fd) - $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 359 | 0 | undef $writepipe; | |||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | open( $writepipe, ">&=$max_fd" ) or die "Cannot fdopen($max_fd) as writepipe - $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||
| 363 | 22 | foreach my $i ( 0 .. $#$setup/2 ) { | |||||||||||||||||||||||||||||||||
| 364 | 41 | my ( $key, $value ) = @$setup[$i*2, $i*2 + 1]; | |||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||
| 366 | 41 | ▶ | if( $key =~ m/^fd(\d+)$/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 367 | 41 | my $fd = $1; | |||||||||||||||||||||||||||||||||
| 368 | 41 | my( $operation, @params ) = @$value; | |||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||
| 370 | 41 | ▶ | $operation eq "dup" and do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 371 | 39 | my $from = fileno $params[0]; | |||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||
| 373 | 39 | ▶ | if( $from != $fd ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 374 | 39 | ▶ | if( exists $dup_from{$fd} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 375 | 0 | ▶ | defined( $dup_from{$fd} = dup( $fd ) ) or die "Cannot dup($fd) - $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||
| 378 | 39 | my $real_from = $dup_from{$from}; | |||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||
| 380 | 39 | POSIX::close( $fd ); | |||||||||||||||||||||||||||||||||
| 381 | 39 | ▶ | dup2( $real_from, $fd ) or die "Cannot dup2($real_from to $fd) - $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||
| 384 | 39 | $fds_refcount{$from}--; | |||||||||||||||||||||||||||||||||
| 385 | 39 | ▶ | if( !$fds_refcount{$from} and !$fd_in_use{$from} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$fds_refcount{$from} or $fd_in_use{$from}
MC/DC0%0/2
$fds_refcount{$from} or $fd_in_use{$from}
$fds_refcount{$from}
$fd_in_use{$from}
Truth table33%1/3
A or B
A= $fds_refcount{$from}B= $fd_in_use{$from}
| |||||||||||||||||||||||||||||||||||
| 386 | 39 | POSIX::close( $from ); | |||||||||||||||||||||||||||||||||
| 387 | 39 | delete $dup_from{$from}; | |||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||
| 389 | }; | ||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||
| 391 | 41 | ▶ | $operation eq "open" and do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 392 | 0 | my ( $mode, $filename ) = @params; | |||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | open( my $fh, $mode, $filename ) or die "Cannot open('$mode', '$filename') - $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||
| 395 | 0 | my $from = fileno $fh; | |||||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | dup2( $from, $fd ) or die "Cannot dup2($from to $fd) - $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||
| 398 | 0 | close $fh; | |||||||||||||||||||||||||||||||||
| 399 | }; | ||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||
| 401 | elsif( $key eq "env" ) { | ||||||||||||||||||||||||||||||||||
| 402 | 0 | %ENV = %$value; | |||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||
| 404 | elsif( $key eq "nice" ) { | ||||||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | nice( $value ) or die "Cannot nice($value) - $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||
| 407 | elsif( $key eq "chdir" ) { | ||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | chdir( $value ) or die "Cannot chdir('$value') - $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||
| 410 | elsif( $key eq "setuid" ) { | ||||||||||||||||||||||||||||||||||
| 411 | 0 | ▶ | setuid( $value ) or die "Cannot setuid('$value') - $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||||
| 413 | elsif( $key eq "setgid" ) { | ||||||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | setgid( $value ) or die "Cannot setgid('$value') - $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||
| 416 | elsif( $key eq "setgroups" ) { | ||||||||||||||||||||||||||||||||||
| 417 | 0 | ▶ | setgroups( @$value ) or die "Cannot setgroups() - $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||
| 422 | 29 | $code->(); | |||||||||||||||||||||||||||||||||
| 423 | }; | ||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||
| 425 | 0 | my $writebuffer = ""; | |||||||||||||||||||||||||||||||||
| 426 | 0 | $writebuffer .= pack( "I", $!+0 ); | |||||||||||||||||||||||||||||||||
| 427 | 0 | $writebuffer .= pack( "I", length( $@ ) ) . $@; | |||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||
| 429 | 0 | syswrite( $writepipe, $writebuffer ); | |||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||
| 431 | 0 | return $exitvalue; | |||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||
| 434 | 40 | 0x55AA; | |||||||||||||||||||||||||||||||||