| 201 | | sub get_childpid { |
|---|
| 202 | | my ($self, $pid) = @_; |
|---|
| 203 | | my $os = $^O; |
|---|
| 204 | | # for windows, we do nothing special |
|---|
| 205 | | if ($os =~ m/mswin/i) { |
|---|
| 206 | | return $pid; |
|---|
| 207 | | } |
|---|
| 208 | | |
|---|
| 209 | | # else $os is macos or linux |
|---|
| 210 | | # This means the wget child process may have been spawned from a subshell |
|---|
| 211 | | # (the real child) that was launched by this perl script. However, we want |
|---|
| 212 | | # the pid of the wget process, since that is what we want to terminate. |
|---|
| 213 | | |
|---|
| 214 | | sleep(2); # give it some time to start up, else we'd have finished searching beforehand |
|---|
| 215 | | |
|---|
| 216 | | # Look through any processes spawned immediately/soon after the subshell. |
|---|
| 217 | | # Look for any pid greater than the subshell's, but within a certain limit (10) |
|---|
| 218 | | my $child = $pid; |
|---|
| 219 | | for(my $i = 1; $i <= 10; $i++) { |
|---|
| 220 | | $child = $child+$i; |
|---|
| 221 | | if(kill(0, $child)) { # this process exists |
|---|
| 222 | | return $child; |
|---|
| 223 | | } |
|---|
| 224 | | } |
|---|
| 225 | | |
|---|
| 226 | | return $pid; # could not find any process within limit, so return the original process id? |
|---|
| 227 | | } |
|---|
| 268 | | $command = "\"$wget_file_path\" $cmdWget 2>&1"; |
|---|
| 269 | | # print STDERR "Command is: $command\n"; |
|---|
| 270 | | $childpid = open2($chld_out, $chld_in, $command) || die "wget request failed: $!\n"; |
|---|
| 271 | | |
|---|
| 272 | | # for linux the wget process starts off as achild of a subshell launched as the child of |
|---|
| 273 | | # this perl script. We want the pid of the wget process, not that of the subshell. |
|---|
| 274 | | $childpid = $self->get_childpid($childpid); |
|---|
| | 241 | $command = "\"$wget_file_path\" $cmdWget"; |
|---|
| | 242 | #print STDOUT "Command is: $command\n"; |
|---|
| | 243 | |
|---|
| | 244 | # Wget's output needs to be monitored to find out when it has naturally termianted. |
|---|
| | 245 | # Wget's output is sent to its STDERR so we can't use open2 without doing 2>&1. |
|---|
| | 246 | # On linux, 2>&1 launches a subshell which then launches wget, meaning that killing |
|---|
| | 247 | # the childpid does not kill wget on Linux but the subshell that launched it instead. |
|---|
| | 248 | # Therefore, we use open3. Though the child process wget sends output only to its stdout, |
|---|
| | 249 | # using open3 says chld_err is undefined and the output of wget only comes in chld_out(!) |
|---|
| | 250 | # However that may be, it works with open3. But to avoid the confusion of managing and |
|---|
| | 251 | # closing an extra unused handle, a single handle is used instead for both the child's |
|---|
| | 252 | # stderr and stdout. |
|---|
| | 253 | |
|---|
| | 254 | # Both open2 and open3 don't return on failure, but raise an exception. The handling |
|---|
| | 255 | # of the exception is described on p.568 of the Perl Cookbook |
|---|
| | 256 | eval { |
|---|
| | 257 | $childpid = open3($chld_in, $chld_out, $chld_out, $command); |
|---|
| | 258 | }; |
|---|
| | 259 | if ($@) { |
|---|
| | 260 | if($@ =~ m/^open3/) { |
|---|
| | 261 | die "open3 failed in $0: $!\n$@\n"; |
|---|
| | 262 | } |
|---|
| | 263 | die "Tried to launch open3 in $0, got unexpected exception: $@"; |
|---|
| | 264 | } |
|---|
| 400 | | #my $command = "\"$wget_file_path\" $cmdWget 2>&1 |"; #open(*WIN,$command) || die "wget request failed: $!\n"; |
|---|
| 401 | | my $command = "\"$wget_file_path\" $cmdWget 2>&1"; |
|---|
| 402 | | # print STDERR "Command is: $command\n"; |
|---|
| 403 | | $childpid = open2($chld_out, $chld_in, $command) || die "wget request failed: $!\n"; |
|---|
| 404 | | |
|---|
| 405 | | # for linux the wget process starts off as achild of a subshell launched as the child of |
|---|
| 406 | | # this perl script. We want the pid of the wget process, not that of the subshell. |
|---|
| 407 | | $childpid = $self->get_childpid($childpid); |
|---|
| | 390 | my $command = "\"$wget_file_path\" $cmdWget"; |
|---|
| | 391 | #print STDOUT "Command is: $command\n"; |
|---|
| | 392 | |
|---|
| | 393 | eval { # see p.568 of Perl Cookbook |
|---|
| | 394 | $childpid = open3($chld_in, $chld_out, $chld_out, $command); |
|---|
| | 395 | }; |
|---|
| | 396 | if ($@) { |
|---|
| | 397 | if($@ =~ m/^open3/) { |
|---|
| | 398 | die "open3 failed in $0: $!\n$@\n"; |
|---|
| | 399 | } |
|---|
| | 400 | die "Tried to launch open3 in $0, got unexpected exception: $@"; |
|---|
| | 401 | } |
|---|