Revert "Use open2 instead of open for s_server instance"
This reverts commit4439ed16c5
. Reviewed-by: Frederik Wedel-Heinen <fwh.openssl@gmail.com> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from https://github.com/openssl/openssl/pull/26374) (cherry picked from commitbf2e6e849d
)
This commit is contained in:
parent
579e09bdf5
commit
dfff5a8e22
1 changed files with 54 additions and 10 deletions
|
@ -7,7 +7,6 @@
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use POSIX ":sys_wait_h";
|
use POSIX ":sys_wait_h";
|
||||||
use IPC::Open2;
|
|
||||||
|
|
||||||
package TLSProxy::Proxy;
|
package TLSProxy::Proxy;
|
||||||
|
|
||||||
|
@ -205,7 +204,7 @@ sub connect_to_server
|
||||||
Proto => $self->{isdtls} ? 'udp' : 'tcp');
|
Proto => $self->{isdtls} ? 'udp' : 'tcp');
|
||||||
if (!defined($sock)) {
|
if (!defined($sock)) {
|
||||||
my $err = $!;
|
my $err = $!;
|
||||||
kill(3, $self->{serverpid});
|
kill(3, $self->{real_serverpid});
|
||||||
die "unable to connect: $err\n";
|
die "unable to connect: $err\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -291,13 +290,17 @@ sub start
|
||||||
print STDERR "Server command: $execcmd\n";
|
print STDERR "Server command: $execcmd\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$pid = IPC::Open2::open2(my $sout, my $sin, $execcmd) or die "Failed to $execcmd: $!\n";
|
open(my $savedin, "<&STDIN");
|
||||||
$self->{serverpid} = $pid;
|
|
||||||
|
# Temporarily replace STDIN so that sink process can inherit it...
|
||||||
|
$pid = open(STDIN, "$execcmd 2>&1 |") or die "Failed to $execcmd: $!\n";
|
||||||
|
$self->{real_serverpid} = $pid;
|
||||||
|
|
||||||
# Process the output from s_server until we find the ACCEPT line, which
|
# Process the output from s_server until we find the ACCEPT line, which
|
||||||
# tells us what the accepting address and port are.
|
# tells us what the accepting address and port are.
|
||||||
while (<$sout>) {
|
while (<>) {
|
||||||
chomp;
|
print;
|
||||||
|
s/\R$//; # Better chomp
|
||||||
next unless (/^ACCEPT\s.*:(\d+)$/);
|
next unless (/^ACCEPT\s.*:(\d+)$/);
|
||||||
$self->{server_port} = $1;
|
$self->{server_port} = $1;
|
||||||
last;
|
last;
|
||||||
|
@ -310,6 +313,38 @@ sub start
|
||||||
die "no ACCEPT detected in '$execcmd' output: $?\n";
|
die "no ACCEPT detected in '$execcmd' output: $?\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Just make sure everything else is simply printed [as separate lines].
|
||||||
|
# The sub process simply inherits our STD* and will keep consuming
|
||||||
|
# server's output and printing it as long as there is anything there,
|
||||||
|
# out of our way.
|
||||||
|
my $error;
|
||||||
|
$pid = undef;
|
||||||
|
if (eval { require Win32::Process; 1; }) {
|
||||||
|
if (Win32::Process::Create(my $h, $^X, "perl -ne print", 0, 0, ".")) {
|
||||||
|
$pid = $h->GetProcessID();
|
||||||
|
$self->{proc_handle} = $h; # hold handle till next round [or exit]
|
||||||
|
} else {
|
||||||
|
$error = Win32::FormatMessage(Win32::GetLastError());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (defined($pid = fork)) {
|
||||||
|
$pid or exec("$^X -ne print") or exit($!);
|
||||||
|
} else {
|
||||||
|
$error = $!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Change back to original stdin
|
||||||
|
open(STDIN, "<&", $savedin);
|
||||||
|
close($savedin);
|
||||||
|
|
||||||
|
if (!defined($pid)) {
|
||||||
|
kill(3, $self->{real_serverpid});
|
||||||
|
die "Failed to capture s_server's output: $error\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
$self->{serverpid} = $pid;
|
||||||
|
|
||||||
print STDERR "Server responds on ",
|
print STDERR "Server responds on ",
|
||||||
"$self->{server_addr}:$self->{server_port}\n";
|
"$self->{server_addr}:$self->{server_port}\n";
|
||||||
|
|
||||||
|
@ -366,7 +401,7 @@ sub clientstart
|
||||||
# dead-lock...
|
# dead-lock...
|
||||||
if (!($pid = open(STDOUT, "| $execcmd"))) {
|
if (!($pid = open(STDOUT, "| $execcmd"))) {
|
||||||
my $err = $!;
|
my $err = $!;
|
||||||
kill(3, $self->{serverpid});
|
kill(3, $self->{real_serverpid});
|
||||||
die "Failed to $execcmd: $err\n";
|
die "Failed to $execcmd: $err\n";
|
||||||
}
|
}
|
||||||
$self->{clientpid} = $pid;
|
$self->{clientpid} = $pid;
|
||||||
|
@ -382,7 +417,7 @@ sub clientstart
|
||||||
# Wait for incoming connection from client
|
# Wait for incoming connection from client
|
||||||
my $fdset = IO::Select->new($self->{proxy_sock});
|
my $fdset = IO::Select->new($self->{proxy_sock});
|
||||||
if (!$fdset->can_read(60)) {
|
if (!$fdset->can_read(60)) {
|
||||||
kill(3, $self->{serverpid});
|
kill(3, $self->{real_serverpid});
|
||||||
die "s_client didn't try to connect\n";
|
die "s_client didn't try to connect\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -441,14 +476,14 @@ sub clientstart
|
||||||
$server_sock->shutdown(SHUT_WR);
|
$server_sock->shutdown(SHUT_WR);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
kill(3, $self->{serverpid});
|
kill(3, $self->{real_serverpid});
|
||||||
die "Unexpected handle";
|
die "Unexpected handle";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($ctr >= 10) {
|
if ($ctr >= 10) {
|
||||||
kill(3, $self->{serverpid});
|
kill(3, $self->{real_serverpid});
|
||||||
print "No progress made\n";
|
print "No progress made\n";
|
||||||
$success = 0;
|
$success = 0;
|
||||||
}
|
}
|
||||||
|
@ -467,6 +502,15 @@ sub clientstart
|
||||||
my $pid;
|
my $pid;
|
||||||
if (--$self->{serverconnects} == 0) {
|
if (--$self->{serverconnects} == 0) {
|
||||||
$pid = $self->{serverpid};
|
$pid = $self->{serverpid};
|
||||||
|
print "Waiting for 'perl -ne print' process to close: $pid...\n";
|
||||||
|
$pid = waitpid($pid, 0);
|
||||||
|
if ($pid > 0) {
|
||||||
|
die "exit code $? from 'perl -ne print' process\n" if $? != 0;
|
||||||
|
} elsif ($pid == 0) {
|
||||||
|
kill(3, $self->{real_serverpid});
|
||||||
|
die "lost control over $self->{serverpid}?";
|
||||||
|
}
|
||||||
|
$pid = $self->{real_serverpid};
|
||||||
print "Waiting for s_server process to close: $pid...\n";
|
print "Waiting for s_server process to close: $pid...\n";
|
||||||
# it's done already, just collect the exit code [and reap]...
|
# it's done already, just collect the exit code [and reap]...
|
||||||
waitpid($pid, 0);
|
waitpid($pid, 0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue