From 7417082248a6d9071032e9ad78414ad2fe811170 Mon Sep 17 00:00:00 2001 From: Zhang Qiang Date: Mon, 16 Dec 2013 16:03:46 +0800 Subject: [PATCH] only create pipe if command output needed This will make my_system more safe via avoid using pipe. Change-Id: I386913ad4ee6be908004351628a47b370e96eae4 --- depanneur | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/depanneur b/depanneur index c4a3f6a..7f25f32 100755 --- a/depanneur +++ b/depanneur @@ -274,21 +274,28 @@ sub my_system { my $cmd = shift; debug("my_system: $cmd"); my $ret; + my $pid; my @out = (); + if (wantarray) { + defined($pid=open(PIPE, "-|")) or die "Can not fork: $!\n"; + } else { + defined($pid=fork) or die "Can not fork: $!\n"; + } - defined(my $pid=open(PIPE, "-|")) or die "Can not fork: $!\n"; unless ($pid) { # Child open(STDERR, ">&STDOUT"); exec ($cmd); exit -1; } else { # Parent - while (my $line = ) { - print $line; - push @out, $line if wantarray; + if (wantarray) { + while (my $line = ) { + print $line; + push @out, $line; + } } waitpid ($pid,0); $ret = $?; - close(PIPE); + close(PIPE) if wantarray; return wantarray ? ($ret, @out): $ret; } -- 2.34.1