Fix a Proxy race condition
[openssl.git] / util / TLSProxy / Proxy.pm
index aaef753cd60ad5ae6edc94aefa8ba5a4c193a6fe..5c363e76eda5398f2f8d6d79ad6f6070ec0694a7 100644 (file)
@@ -49,6 +49,7 @@ sub new
         clientflags => "",
         serverconnects => 1,
         serverpid => 0,
+        clientpid => 0,
         reneg => 0,
         sessionfile => undef,
 
@@ -113,6 +114,7 @@ sub clearClient
     $self->{message_list} = [];
     $self->{clientflags} = "";
     $self->{sessionfile} = undef;
+    $self->{clientpid} = 0;
     $is_tls13 = 0;
     $ciphersuite = undef;
 
@@ -171,6 +173,9 @@ sub start
         if ($self->serverflags ne "") {
             $execcmd .= " ".$self->serverflags;
         }
+        if ($self->debug) {
+            print STDERR "Server command: $execcmd\n";
+        }
         exec($execcmd);
     }
     $self->serverpid($pid);
@@ -232,8 +237,12 @@ sub clientstart
             if (defined $self->sessionfile) {
                 $execcmd .= " -ign_eof";
             }
+            if ($self->debug) {
+                print STDERR "Client command: $execcmd\n";
+            }
             exec($execcmd);
         }
+        $self->clientpid($pid);
     }
 
     # Wait for incoming connection from client
@@ -332,6 +341,10 @@ sub clientstart
         waitpid( $self->serverpid, 0);
         die "exit code $? from server process\n" if $? != 0;
     }
+    die "clientpid is zero\n" if $self->clientpid == 0;
+    print "Waiting for client process to close: ".$self->clientpid."\n";
+    waitpid($self->clientpid, 0);
+
     return 1;
 }
 
@@ -525,6 +538,14 @@ sub serverpid
     }
     return $self->{serverpid};
 }
+sub clientpid
+{
+    my $self = shift;
+    if (@_) {
+        $self->{clientpid} = shift;
+    }
+    return $self->{clientpid};
+}
 
 sub fill_known_data
 {