only create pipe if command output needed release-0.11 0.11
authorZhang Qiang <qiang.z.zhang@intel.com>
Mon, 16 Dec 2013 08:03:46 +0000 (16:03 +0800)
committerZhang Qiang <qiang.z.zhang@intel.com>
Mon, 16 Dec 2013 08:35:34 +0000 (16:35 +0800)
This will make my_system more safe via avoid using pipe.

Change-Id: I386913ad4ee6be908004351628a47b370e96eae4

depanneur

index c4a3f6ac4d640a18447f1e9ff27388ef4de3c9b1..7f25f320555275ca362c3ca6a21eec8b44dac6fd 100755 (executable)
--- 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 = <PIPE>) {
-            print $line;
-            push @out, $line if wantarray;
+        if (wantarray) {
+            while (my $line = <PIPE>) {
+                print $line;
+                push @out, $line;
+            }
         }
         waitpid ($pid,0);
         $ret = $?;
-        close(PIPE);
+        close(PIPE) if wantarray;
 
         return wantarray ? ($ret, @out): $ret;
     }