1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
From: Hilko Bengen <bengen@debian.org>
Date: Mon, 25 Feb 2019 09:47:35 +0100
Subject: Eliminate race condition in runcmd
---
osutils.pm | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/osutils.pm b/osutils.pm
index b06a042..05423c3 100644
--- a/osutils.pm
+++ b/osutils.pm
@@ -93,19 +93,10 @@ sub quote {
# Open a process to run external program and check its return status
sub runcmd {
diag "running " . join(' ', @_);
- my ($pid, $status);
-
- local $SIG{CHLD} = sub {
- local ($!, $?);
- while ((my $child = waitpid(-1, WNOHANG)) > 0) {
- diag "runcmd pid $pid returned $child";
- $status = $?;
- }
- };
my ($wtr, $rdr, $err);
$err = gensym;
- $pid = open3($wtr, $rdr, $err, @_);
+ my $pid = open3($wtr, $rdr, $err, @_);
die "couldn't open: $!" unless defined $pid;
close($wtr) or die "couldn't close fh: $!";
@@ -125,6 +116,11 @@ sub runcmd {
close($rdr) or die "couldn't close fh: $!";
close($err) or die "couldn't close fh: $!";
+ my $status;
+ waitpid $pid, 0;
+ $status = $?;
+ diag "runcmd pid $pid returned $status";
+
my $exit_code = $status >> 8;
die "runcmd failed with exit code $exit_code" unless ($exit_code == 0);
return $exit_code;
|