Index: /gsdl/trunk/perllib/downloaders/WgetDownload.pm
===================================================================
--- /gsdl/trunk/perllib/downloaders/WgetDownload.pm	(revision 17839)
+++ /gsdl/trunk/perllib/downloaders/WgetDownload.pm	(revision 17840)
@@ -35,5 +35,5 @@
 use strict;
 use Cwd;
-use IPC::Open2;
+use IPC::Open3;
 use IO::Select;
 use IO::Socket;
@@ -199,31 +199,4 @@
 }
 
-sub get_childpid {
-    my ($self, $pid) = @_;
-    my $os = $^O;
-    # for windows, we do nothing special
-    if ($os =~ m/mswin/i) {
-	return $pid;
-    }
-    
-    # else $os is macos or linux
-    # This means the wget child process may have been spawned from a subshell
-    # (the real child) that was launched by this perl script. However, we want
-    # the pid of the wget process, since that is what we want to terminate.
-
-    sleep(2); # give it some time to start up, else we'd have finished searching beforehand
-
-    # Look through any processes spawned immediately/soon after the subshell.
-    # Look for any pid greater than the subshell's, but within a certain limit (10)
-    my $child = $pid;
-    for(my $i = 1; $i <= 10; $i++) {
-	$child = $child+$i;
-	if(kill(0, $child)) { # this process exists
-	    return $child;
-	}
-    }
-    
-    return $pid; # could not find any process within limit, so return the original process id?
-}
 
 sub useWget
@@ -266,11 +239,28 @@
 
     my $wget_file_path = &util::filename_cat($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "wget");
-    $command = "\"$wget_file_path\" $cmdWget 2>&1";
-    # print STDERR "Command is: $command\n";
-    $childpid = open2($chld_out, $chld_in, $command) || die "wget request failed: $!\n";
-    
-    # for linux the wget process starts off as achild of a subshell launched as the child of 
-    # this perl script. We want the pid of the wget process, not that of the subshell. 
-    $childpid = $self->get_childpid($childpid);
+    $command = "\"$wget_file_path\" $cmdWget";
+    #print STDOUT "Command is: $command\n";
+
+    # Wget's output needs to be monitored to find out when it has naturally termianted.
+    # Wget's output is sent to its STDERR so we can't use open2 without doing 2>&1.
+    # On linux, 2>&1 launches a subshell which then launches wget, meaning that killing
+    # the childpid does not kill wget on Linux but the subshell that launched it instead. 
+    # Therefore, we use open3. Though the child process wget sends output only to its stdout, 
+    # using open3 says chld_err is undefined and the output of wget only comes in chld_out(!)
+    # However that may be, it works with open3. But to avoid the confusion of managing and
+    # closing an extra unused handle, a single handle is used instead for both the child's
+    # stderr and stdout.
+
+    # Both open2 and open3 don't return on failure, but raise an exception. The handling
+    # of the exception is described on p.568 of the Perl Cookbook
+    eval { 
+	$childpid = open3($chld_in, $chld_out, $chld_out, $command);
+    };
+    if ($@) {
+	if($@ =~ m/^open3/) {
+	    die "open3 failed in $0: $!\n$@\n";	    
+	}
+	die "Tried to launch open3 in $0, got unexpected exception: $@";
+    }
 
     my $loop = 1;
@@ -341,9 +331,9 @@
 			    close($rh); 	        #close($serverSocket);
 			    print $client "Perl terminated wget and is about to exit\n";
+			    last;                           # out of inner for loop
 			}
 			else { # the process may just be starting up, wait
 			    sleep(1);
 			}
-			last;                           # out of inner for loop
 		    }
 		    last;                               # out of foreach loop
@@ -398,12 +388,16 @@
 
     my $wget_file_path = &util::filename_cat($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "wget");
-    #my $command = "\"$wget_file_path\" $cmdWget 2>&1 |"; #open(*WIN,$command) || die "wget request failed: $!\n";
-    my $command = "\"$wget_file_path\" $cmdWget 2>&1";
-    # print STDERR "Command is: $command\n";
-    $childpid = open2($chld_out, $chld_in, $command) || die "wget request failed: $!\n";
-
-    # for linux the wget process starts off as achild of a subshell launched as the child of 
-    # this perl script. We want the pid of the wget process, not that of the subshell. 
-    $childpid = $self->get_childpid($childpid);
+    my $command = "\"$wget_file_path\" $cmdWget";
+    #print STDOUT "Command is: $command\n";
+
+    eval {     # see p.568 of Perl Cookbook
+	$childpid = open3($chld_in, $chld_out, $chld_out, $command);
+    };
+    if ($@) {
+	if($@ =~ m/^open3/) {
+	    die "open3 failed in $0: $!\n$@\n";	    
+	}
+	die "Tried to launch open3 in $0, got unexpected exception: $@";
+    }
 
     my $full_text = "";
@@ -495,9 +489,9 @@
 			    close($rh); 	        #close($serverSocket);
 			    print $client "Perl terminated wget and is about to exit\n";
+			    last;                           # out of inner for loop
 			}
 			else { # the process may just be starting up, wait
 			    sleep(1);
 			}
-			last;                           # out of inner for loop
 		    }
 		    last;                               # out of foreach loop
