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