From: Rob H. <for...@us...> - 2002-12-31 06:31:17
|
Update of /cvsroot/sandweb/sandweb/lib/SandWeb In directory sc8-pr-cvs1:/tmp/cvs-serv28721/lib/SandWeb Modified Files: Shell.pm Log Message: cool, this is a great place to put the timeout. default timeout for shell commands is now 5 minutes, only thing now is to move this as a config option into sandweb.cfg ( hmm.. this *could* be user configurable I guess, will make it just system config tomorrow and think about user config ) Index: Shell.pm =================================================================== RCS file: /cvsroot/sandweb/sandweb/lib/SandWeb/Shell.pm,v retrieving revision 1.21 retrieving revision 1.22 diff -U2 -r1.21 -r1.22 --- Shell.pm 7 Nov 2002 08:01:07 -0000 1.21 +++ Shell.pm 31 Dec 2002 06:31:12 -0000 1.22 @@ -30,4 +30,11 @@ use SandWeb::File; +# +# FIXME - timeout should be config option +# +# default timeout for shell commands is 300 minutes ( aka 5 minutes ) +# +my $timeout = '300'; + sub new { my $class = shift; @@ -114,5 +121,24 @@ $log->debug("Using VCS username/password"); $log->debug("Full command is : $sandweb_expect system_vcs $system_username \'$system_password\' \'$vcs_password\' $command"); - my $raw_output = `$sandweb_expect system_vcs $system_username \'$system_password\' \'$vcs_password\' "$command" 2>&1`; + eval { + local $SIG{ALRM} = sub { die "alarm\n" }; + alarm $timeout; + my $raw_output = `$sandweb_expect system_vcs $system_username \'$system_password\' \'$vcs_password\' "$command" 2>&1` or $raw_output = "Timeout of $timeout seconds reached.\n"; + alarm 0; + }; + if ( "$@" eq "alarm" ) { + + my $output = ""; + my $error_message = "Timeout reached of $timeout seconds reached, operation aborted.\n"; + + my %return = ( + output => "$output", + error => "$error_message", + ); + + return %return; + } + + $error = $?; $raw_output =~ s/ /\n/g; @@ -123,5 +149,25 @@ } else { $log->debug("Not using VCS username/password"); - my $raw_output = `$sandweb_expect system $system_username $system_password $command`; + + eval { + local $SIG{ALRM} = sub { die "alarm\n" }; + alarm $timeout; + my $raw_output = `$sandweb_expect system $system_username $system_password $command` or $raw_output = "Timeout of $timeout seconds reached.\n"; + alarm 0; + }; + + if ( "$@" eq "alarm" ) { + + my $output = ""; + my $error_message = "Timeout reached of $timeout seconds reached, operation aborted.\n"; + + my %return = ( + output => "$output", + error => "$error_message", + ); + + return %return; + } + $error = $?; $raw_output =~ s/^M/\n/g; @@ -133,4 +179,5 @@ } elsif ( $vcs ) { $log->debug("Using VCS username/password"); + $log->debug("Full command is : $sandweb_expect vcs $users_dir/passwd \"$command\""); my $file = SandWeb::File->new( 'log_obj' => $log, @@ -143,8 +190,30 @@ ); - my $raw_output = `$sandweb_expect vcs $users_dir/passwd "$command" 2>&1`; - $file->delete(); + my $raw_output; + + eval { + local $SIG{ALRM} = sub { die "alarm\n" }; + alarm $timeout; + my $error_output; + $raw_output = `$sandweb_expect vcs $users_dir/passwd "$command" 2>&1`; + $file->delete(); + alarm 0; + }; + + if ( "$@" eq "alarm" ) { + + my $output = ""; + my $error_message = "Timeout reached of $timeout seconds reached, operation aborted.\n"; + + my %return = ( + output => "$output", + error => "$error_message", + ); + + return %return; + } $error = $?; + $raw_output =~ s/^M/\n/g; my @lined_output = split(/\n/, $raw_output); @@ -155,6 +224,25 @@ if ($log) { $log->debug("Not using VCS username/password"); + } + eval { + local $SIG{ALRM} = sub { die "alarm\n" }; + alarm $timeout; + $output = `$command 2>&1` or $raw_output = "Timeout of $timeout seconds reached.\n"; + + }; + + if ( "$@" eq "alarm" ) { + + my $output = ""; + my $error_message = "Timeout reached of $timeout seconds reached, operation aborted.\n"; + + my %return = ( + output => "$output", + error => "$error_message", + ); + + return %return; } - $output = `$command 2>&1`; + $error = $?; } @@ -184,5 +272,15 @@ if ($command) { - open (FILEHANDLE, "|`$command`"); + eval { + local $SIG{ALRM} = sub { die "alarm\n" }; + alarm $timeout; + open (FILEHANDLE, "|`$command`") or return 0; + alarm 0; + }; + + if ( "$@" eq "alarm" ) { + return 0; + } + return \*FILEHANDLE; } |