Users should get emails when the script dies because of a critical failure. 
Critical
failures are defined as any errors that could terminate the script, via die().

In order to add email support, this patch converts all die() to dodie() except:
 * when '-v' is used as an option to get the version of the script.
 * in Sig-Int handeler because it's not a fatal error to cancel the script.
 * the die() in dodie() itself actually kills the script.

Suggested-by: Dhaval Giani <dhaval.gi...@oracle.com>
Signed-off-by: Tim Tianyang Chen <tianyang.c...@oracle.com>
---
 ktest.pl | 88 +++++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 45 insertions(+), 43 deletions(-)

diff --git a/ktest.pl b/ktest.pl
index 2e38647..763a83e 100755
--- a/ktest.pl
+++ b/ktest.pl
@@ -464,9 +464,11 @@ $config_help{"REBOOT_SCRIPT"} = << "EOF"
 EOF
     ;
+sub dodie;
+
 sub _logit {
     if (defined($opt{"LOG_FILE"})) {
-       open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
+       open(OUT, ">> $opt{LOG_FILE}") or dodie "Can't write to $opt{LOG_FILE}";
        print OUT @_;
        close(OUT);
     }
@@ -751,7 +753,7 @@ sub set_value {
            if ($override) {
                $extra = "In the same override section!\n";
            }
-           die "$name: $.: Option $lvalue defined more than once!\n$extra";
+           dodie "$name: $.: Option $lvalue defined more than once!\n$extra";
        }
        ${$overrides}{$lvalue} = $prvalue;
     }
@@ -863,7 +865,7 @@ sub process_expression {
     if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
        my $ret = process_compare($1, $2, $3);
        if ($ret < 0) {
-           die "$name: $.: Unable to process comparison\n";
+           dodie "$name: $.: Unable to process comparison\n";
        }
        return $ret;
     }
@@ -882,7 +884,7 @@ sub process_expression {
        return 1;
     }
- die ("$name: $.: Undefined content $val in if statement\n");
+    dodie ("$name: $.: Undefined content $val in if statement\n");
 }
sub process_if {
@@ -899,7 +901,7 @@ sub __read_config {
     my ($config, $current_test_num) = @_;
my $in;
-    open($in, $config) || die "can't read file $config";
+    open($in, $config) || dodie "can't read file $config";
my $name = $config;
     $name =~ s,.*/(.*),$1,;
@@ -936,7 +938,7 @@ sub __read_config {
            if ($type eq "TEST_START") {
if ($num_tests_set) {
-                   die "$name: $.: Can not specify both NUM_TESTS and 
TEST_START\n";
+                   dodie "$name: $.: Can not specify both NUM_TESTS and 
TEST_START\n";
                }
$old_test_num = $test_num;
@@ -959,7 +961,7 @@ sub __read_config {
if ($rest =~ s/\sELSE\b//) {
                if (!$if) {
-                   die "$name: $.: ELSE found with out matching IF 
section\n$_";
+                   dodie "$name: $.: ELSE found with out matching IF 
section\n$_";
                }
                $if = 0;
@@ -997,7 +999,7 @@ sub __read_config {
            }
if (!$skip && $rest !~ /^\s*$/) {
-               die "$name: $.: Gargbage found after $type\n$_";
+               dodie "$name: $.: Gargbage found after $type\n$_";
            }
if ($skip && $type eq "TEST_START") {
@@ -1007,7 +1009,7 @@ sub __read_config {
} elsif (/^\s*ELSE\b(.*)$/) {
            if (!$if) {
-               die "$name: $.: ELSE found with out matching IF section\n$_";
+               dodie "$name: $.: ELSE found with out matching IF section\n$_";
            }
            $rest = $1;
            if ($if_set) {
@@ -1030,7 +1032,7 @@ sub __read_config {
            }
if ($rest !~ /^\s*$/) {
-               die "$name: $.: Gargbage found after DEFAULTS\n$_";
+               dodie "$name: $.: Gargbage found after DEFAULTS\n$_";
            }
} elsif (/^\s*INCLUDE\s+(\S+)/) {
@@ -1038,7 +1040,7 @@ sub __read_config {
            next if ($skip);
if (!$default) {
-               die "$name: $.: INCLUDE can only be done in default 
sections\n$_";
+               dodie "$name: $.: INCLUDE can only be done in default 
sections\n$_";
            }
my $file = process_variables($1);
@@ -1053,7 +1055,7 @@ sub __read_config {
            }
                
            if ( ! -r $file ) {
-               die "$name: $.: Can't read file $file\n$_";
+               dodie "$name: $.: Can't read file $file\n$_";
            }
if (__read_config($file, \$test_num)) {
@@ -1085,15 +1087,15 @@ sub __read_config {
                ($lvalue eq "NUM_TESTS" ||
                 $lvalue eq "LOG_FILE" ||
                 $lvalue eq "CLEAR_LOG")) {
-               die "$name: $.: $lvalue must be set in DEFAULTS section\n";
+               dodie "$name: $.: $lvalue must be set in DEFAULTS section\n";
            }
if ($lvalue eq "NUM_TESTS") {
                if ($test_num) {
-                   die "$name: $.: Can not specify both NUM_TESTS and 
TEST_START\n";
+                   dodie "$name: $.: Can not specify both NUM_TESTS and 
TEST_START\n";
                }
                if (!$default) {
-                   die "$name: $.: NUM_TESTS must be set in default section\n";
+                   dodie "$name: $.: NUM_TESTS must be set in default 
section\n";
                }
                $num_tests_set = 1;
            }
@@ -1125,7 +1127,7 @@ sub __read_config {
            set_variable($lvalue, $rvalue);
} else {
-           die "$name: $.: Garbage found in config\n$_";
+           dodie "$name: $.: Garbage found in config\n$_";
        }
     }
@@ -1311,7 +1313,7 @@ sub eval_option {
        # Check for recursive evaluations.
        # 100 deep should be more than enough.
        if ($r++ > 100) {
-           die "Over 100 evaluations accurred with $option\n" .
+           dodie "Over 100 evaluations accurred with $option\n" .
                "Check for recursive variables\n";
        }
        $prev = $option;
@@ -1442,7 +1444,7 @@ sub dodie {
        run_command $post_test;
     }
- die @_, "\n";
+    die "Aborted.\n";
 }
sub create_pty {
@@ -1484,7 +1486,7 @@ sub exec_console {
     close($pts);
exec $console or
-       die "Can't open console $console";
+       dodie "Can't open console $console";
 }
sub open_console {
@@ -1632,7 +1634,7 @@ sub save_logs {
if (!-d $dir) {
            mkpath($dir) or
-               die "can't create $dir";
+               dodie "can't create $dir";
        }
my %files = (
@@ -1645,7 +1647,7 @@ sub save_logs {
        while (my ($name, $source) = each(%files)) {
                if (-f "$source") {
                        cp "$source", "$dir/$name" or
-                               die "failed to copy $source";
+                               dodie "failed to copy $source";
                }
        }
@@ -1819,7 +1821,7 @@ sub get_grub2_index {
     $ssh_grub =~ s,\$SSH_COMMAND,cat $grub_file,g;
open(IN, "$ssh_grub |")
-       or die "unable to get $grub_file";
+       or dodie "unable to get $grub_file";
my $found = 0; @@ -1834,7 +1836,7 @@ sub get_grub2_index {
     }
     close(IN);
- die "Could not find '$grub_menu' in $grub_file on $machine"
+    dodie "Could not find '$grub_menu' in $grub_file on $machine"
        if (!$found);
     doprint "$grub_number\n";
     $last_grub_menu = $grub_menu;
@@ -1862,7 +1864,7 @@ sub get_grub_index {
     $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
open(IN, "$ssh_grub |")
-       or die "unable to get menu.lst";
+       or dodie "unable to get menu.lst";
my $found = 0; @@ -1877,7 +1879,7 @@ sub get_grub_index {
     }
     close(IN);
- die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
+    dodie "Could not find '$grub_menu' in /boot/grub/menu on $machine"
        if (!$found);
     doprint "$grub_number\n";
     $last_grub_menu = $grub_menu;
@@ -1990,7 +1992,7 @@ sub monitor {
     my $full_line = "";
open(DMESG, "> $dmesg") or
-       die "unable to write to $dmesg";
+       dodie "unable to write to $dmesg";
reboot_to; @@ -2878,9 +2880,9 @@ sub bisect { my $result; - die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
-    die "BISECT_BAD[$i] not defined\n"       if (!defined($bisect_bad));
-    die "BISECT_TYPE[$i] not defined\n"      if (!defined($bisect_type));
+    dodie "BISECT_GOOD[$i] not defined\n"    if (!defined($bisect_good));
+    dodie "BISECT_BAD[$i] not defined\n"     if (!defined($bisect_bad));
+    dodie "BISECT_TYPE[$i] not defined\n"    if (!defined($bisect_type));
my $good = $bisect_good;
     my $bad = $bisect_bad;
@@ -2966,7 +2968,7 @@ sub bisect {
# checkout where we started
        run_command "git checkout $head" or
-           die "Failed to checkout $head";
+           dodie "Failed to checkout $head";
     }
run_command "git bisect start$start_files" or
@@ -3423,9 +3425,9 @@ sub patchcheck_reboot {
 sub patchcheck {
     my ($i) = @_;
- die "PATCHCHECK_START[$i] not defined\n"
+    dodie "PATCHCHECK_START[$i] not defined\n"
        if (!defined($patchcheck_start));
-    die "PATCHCHECK_TYPE[$i] not defined\n"
+    dodie "PATCHCHECK_TYPE[$i] not defined\n"
        if (!defined($patchcheck_type));
my $start = $patchcheck_start;
@@ -3503,7 +3505,7 @@ sub patchcheck {
        doprint "\nProcessing commit \"$item\"\n\n";
run_command "git checkout $sha1" or
-           die "Failed to checkout $sha1";
+           dodie "Failed to checkout $sha1";
# only clean on the first and last patch
        if ($item eq $list[0] ||
@@ -3594,7 +3596,7 @@ sub read_kconfig {
     }
open(KIN, "$kconfig")
-       or die "Can't open $kconfig";
+       or dodie "Can't open $kconfig";
     while (<KIN>) {
        chomp;
@@ -3755,7 +3757,7 @@ sub get_depends { $dep =~ s/^[^$valid]*[$valid]+//;
        } else {
-           die "this should never happen";
+           dodie "this should never happen";
        }
     }
@@ -4016,7 +4018,7 @@ sub make_min_config {
            # update new ignore configs
            if (defined($ignore_config)) {
                open (OUT, ">$temp_config")
-                   or die "Can't write to $temp_config";
+                   or dodie "Can't write to $temp_config";
                foreach my $config (keys %save_configs) {
                    print OUT "$save_configs{$config}\n";
                }
@@ -4044,7 +4046,7 @@ sub make_min_config {
# Save off all the current mandatory configs
            open (OUT, ">$temp_config")
-               or die "Can't write to $temp_config";
+               or dodie "Can't write to $temp_config";
            foreach my $config (keys %keep_configs) {
                print OUT "$keep_configs{$config}\n";
            }
@@ -4113,7 +4115,7 @@ if ($#ARGV == 0) {
 if (! -f $ktest_config) {
     $newconfig = 1;
     get_test_case;
-    open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
+    open(OUT, ">$ktest_config") or dodie "Can not create $ktest_config";
     print OUT << "EOF"
 # Generated by ktest.pl
 #
@@ -4148,7 +4150,7 @@ if (defined($opt{"LOG_FILE"})) {
 my @new_configs = keys %entered_configs;
 if ($#new_configs >= 0) {
     print "\nAppending entered in configs to $ktest_config\n";
-    open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
+    open(OUT, ">>$ktest_config") or dodie "Can not append to $ktest_config";
     foreach my $config (@new_configs) {
        print OUT "$config = $entered_configs{$config}\n";
        $opt{$config} = process_variables($entered_configs{$config});
@@ -4286,11 +4288,11 @@ for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
     $outputdir = set_test_option("OUTPUT_DIR", $i);
     $builddir = set_test_option("BUILD_DIR", $i);
- chdir $builddir || die "can't change directory to $builddir";
+    chdir $builddir || dodie "can't change directory to $builddir";
if (!-d $outputdir) {
        mkpath($outputdir) or
-           die "can't create $outputdir";
+           dodie "can't create $outputdir";
     }
$make = "$makecmd O=$outputdir";
@@ -4321,7 +4323,7 @@ for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
if (!-d $tmpdir) {
        mkpath($tmpdir) or
-           die "can't create $tmpdir";
+           dodie "can't create $tmpdir";
     }
$ENV{"SSH_USER"} = $ssh_user;
@@ -4394,7 +4396,7 @@ for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
if (defined($checkout)) {
        run_command "git checkout $checkout" or
-           die "failed to checkout $checkout";
+           dodie "failed to checkout $checkout";
     }
$no_reboot = 0;
--
1.8.3.1

Reply via email to