05f429d68857d57468b1456784b8fba310153a2f
[linux-2.6-block.git] / tools / testing / ktest / ktest.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
4 # Licensed under the terms of the GNU GPL License version 2
5 #
6
7 use strict;
8 use IPC::Open2;
9 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
10 use File::Path qw(mkpath);
11 use File::Copy qw(cp);
12 use FileHandle;
13
14 my $VERSION = "0.2";
15
16 $| = 1;
17
18 my %opt;
19 my %repeat_tests;
20 my %repeats;
21 my %default;
22
23 #default opts
24 $default{"NUM_TESTS"}           = 1;
25 $default{"TEST_TYPE"}           = "test";
26 $default{"BUILD_TYPE"}          = "randconfig";
27 $default{"MAKE_CMD"}            = "make";
28 $default{"TIMEOUT"}             = 120;
29 $default{"TMP_DIR"}             = "/tmp/ktest/\${MACHINE}";
30 $default{"SLEEP_TIME"}          = 60;   # sleep time between tests
31 $default{"BUILD_NOCLEAN"}       = 0;
32 $default{"REBOOT_ON_ERROR"}     = 0;
33 $default{"POWEROFF_ON_ERROR"}   = 0;
34 $default{"REBOOT_ON_SUCCESS"}   = 1;
35 $default{"POWEROFF_ON_SUCCESS"} = 0;
36 $default{"BUILD_OPTIONS"}       = "";
37 $default{"BISECT_SLEEP_TIME"}   = 60;   # sleep time between bisects
38 $default{"PATCHCHECK_SLEEP_TIME"} = 60; # sleep time between patch checks
39 $default{"CLEAR_LOG"}           = 0;
40 $default{"BISECT_MANUAL"}       = 0;
41 $default{"BISECT_SKIP"}         = 1;
42 $default{"SUCCESS_LINE"}        = "login:";
43 $default{"DETECT_TRIPLE_FAULT"} = 1;
44 $default{"NO_INSTALL"}          = 0;
45 $default{"BOOTED_TIMEOUT"}      = 1;
46 $default{"DIE_ON_FAILURE"}      = 1;
47 $default{"SSH_EXEC"}            = "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND";
48 $default{"SCP_TO_TARGET"}       = "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE";
49 $default{"REBOOT"}              = "ssh \$SSH_USER\@\$MACHINE reboot";
50 $default{"STOP_AFTER_SUCCESS"}  = 10;
51 $default{"STOP_AFTER_FAILURE"}  = 60;
52 $default{"STOP_TEST_AFTER"}     = 600;
53
54 # required, and we will ask users if they don't have them but we keep the default
55 # value something that is common.
56 $default{"REBOOT_TYPE"}         = "grub";
57 $default{"LOCALVERSION"}        = "-test";
58 $default{"SSH_USER"}            = "root";
59 $default{"BUILD_TARGET"}        = "arch/x86/boot/bzImage";
60 $default{"TARGET_IMAGE"}        = "/boot/vmlinuz-test";
61
62 my $ktest_config;
63 my $version;
64 my $machine;
65 my $ssh_user;
66 my $tmpdir;
67 my $builddir;
68 my $outputdir;
69 my $output_config;
70 my $test_type;
71 my $build_type;
72 my $build_options;
73 my $pre_build;
74 my $post_build;
75 my $pre_build_die;
76 my $post_build_die;
77 my $reboot_type;
78 my $reboot_script;
79 my $power_cycle;
80 my $reboot;
81 my $reboot_on_error;
82 my $poweroff_on_error;
83 my $die_on_failure;
84 my $powercycle_after_reboot;
85 my $poweroff_after_halt;
86 my $ssh_exec;
87 my $scp_to_target;
88 my $power_off;
89 my $grub_menu;
90 my $grub_number;
91 my $target;
92 my $make;
93 my $post_install;
94 my $no_install;
95 my $noclean;
96 my $minconfig;
97 my $start_minconfig;
98 my $start_minconfig_defined;
99 my $output_minconfig;
100 my $ignore_config;
101 my $addconfig;
102 my $in_bisect = 0;
103 my $bisect_bad = "";
104 my $reverse_bisect;
105 my $bisect_manual;
106 my $bisect_skip;
107 my $config_bisect_good;
108 my $in_patchcheck = 0;
109 my $run_test;
110 my $redirect;
111 my $buildlog;
112 my $testlog;
113 my $dmesg;
114 my $monitor_fp;
115 my $monitor_pid;
116 my $monitor_cnt = 0;
117 my $sleep_time;
118 my $bisect_sleep_time;
119 my $patchcheck_sleep_time;
120 my $ignore_warnings;
121 my $store_failures;
122 my $store_successes;
123 my $test_name;
124 my $timeout;
125 my $booted_timeout;
126 my $detect_triplefault;
127 my $console;
128 my $reboot_success_line;
129 my $success_line;
130 my $stop_after_success;
131 my $stop_after_failure;
132 my $stop_test_after;
133 my $build_target;
134 my $target_image;
135 my $localversion;
136 my $iteration = 0;
137 my $successes = 0;
138
139 my %entered_configs;
140 my %config_help;
141 my %variable;
142 my %force_config;
143
144 # do not force reboots on config problems
145 my $no_reboot = 1;
146
147 # default variables that can be used
148 chomp ($variable{"PWD"} = `pwd`);
149
150 $config_help{"MACHINE"} = << "EOF"
151  The machine hostname that you will test.
152 EOF
153     ;
154 $config_help{"SSH_USER"} = << "EOF"
155  The box is expected to have ssh on normal bootup, provide the user
156   (most likely root, since you need privileged operations)
157 EOF
158     ;
159 $config_help{"BUILD_DIR"} = << "EOF"
160  The directory that contains the Linux source code (full path).
161 EOF
162     ;
163 $config_help{"OUTPUT_DIR"} = << "EOF"
164  The directory that the objects will be built (full path).
165  (can not be same as BUILD_DIR)
166 EOF
167     ;
168 $config_help{"BUILD_TARGET"} = << "EOF"
169  The location of the compiled file to copy to the target.
170  (relative to OUTPUT_DIR)
171 EOF
172     ;
173 $config_help{"TARGET_IMAGE"} = << "EOF"
174  The place to put your image on the test machine.
175 EOF
176     ;
177 $config_help{"POWER_CYCLE"} = << "EOF"
178  A script or command to reboot the box.
179
180  Here is a digital loggers power switch example
181  POWER_CYCLE = wget --no-proxy -O /dev/null -q  --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
182
183  Here is an example to reboot a virtual box on the current host
184  with the name "Guest".
185  POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
186 EOF
187     ;
188 $config_help{"CONSOLE"} = << "EOF"
189  The script or command that reads the console
190
191   If you use ttywatch server, something like the following would work.
192 CONSOLE = nc -d localhost 3001
193
194  For a virtual machine with guest name "Guest".
195 CONSOLE =  virsh console Guest
196 EOF
197     ;
198 $config_help{"LOCALVERSION"} = << "EOF"
199  Required version ending to differentiate the test
200  from other linux builds on the system.
201 EOF
202     ;
203 $config_help{"REBOOT_TYPE"} = << "EOF"
204  Way to reboot the box to the test kernel.
205  Only valid options so far are "grub" and "script".
206
207  If you specify grub, it will assume grub version 1
208  and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
209  and select that target to reboot to the kernel. If this is not
210  your setup, then specify "script" and have a command or script
211  specified in REBOOT_SCRIPT to boot to the target.
212
213  The entry in /boot/grub/menu.lst must be entered in manually.
214  The test will not modify that file.
215 EOF
216     ;
217 $config_help{"GRUB_MENU"} = << "EOF"
218  The grub title name for the test kernel to boot
219  (Only mandatory if REBOOT_TYPE = grub)
220
221  Note, ktest.pl will not update the grub menu.lst, you need to
222  manually add an option for the test. ktest.pl will search
223  the grub menu.lst for this option to find what kernel to
224  reboot into.
225
226  For example, if in the /boot/grub/menu.lst the test kernel title has:
227  title Test Kernel
228  kernel vmlinuz-test
229  GRUB_MENU = Test Kernel
230 EOF
231     ;
232 $config_help{"REBOOT_SCRIPT"} = << "EOF"
233  A script to reboot the target into the test kernel
234  (Only mandatory if REBOOT_TYPE = script)
235 EOF
236     ;
237
238 sub read_yn {
239     my ($prompt) = @_;
240
241     my $ans;
242
243     for (;;) {
244         print "$prompt [Y/n] ";
245         $ans = <STDIN>;
246         chomp $ans;
247         if ($ans =~ /^\s*$/) {
248             $ans = "y";
249         }
250         last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
251         print "Please answer either 'y' or 'n'.\n";
252     }
253     if ($ans !~ /^y$/i) {
254         return 0;
255     }
256     return 1;
257 }
258
259 sub get_ktest_config {
260     my ($config) = @_;
261     my $ans;
262
263     return if (defined($opt{$config}));
264
265     if (defined($config_help{$config})) {
266         print "\n";
267         print $config_help{$config};
268     }
269
270     for (;;) {
271         print "$config = ";
272         if (defined($default{$config})) {
273             print "\[$default{$config}\] ";
274         }
275         $ans = <STDIN>;
276         $ans =~ s/^\s*(.*\S)\s*$/$1/;
277         if ($ans =~ /^\s*$/) {
278             if ($default{$config}) {
279                 $ans = $default{$config};
280             } else {
281                 print "Your answer can not be blank\n";
282                 next;
283             }
284         }
285         $entered_configs{$config} = process_variables($ans);
286         last;
287     }
288 }
289
290 sub get_ktest_configs {
291     get_ktest_config("MACHINE");
292     get_ktest_config("SSH_USER");
293     get_ktest_config("BUILD_DIR");
294     get_ktest_config("OUTPUT_DIR");
295     get_ktest_config("BUILD_TARGET");
296     get_ktest_config("TARGET_IMAGE");
297     get_ktest_config("POWER_CYCLE");
298     get_ktest_config("CONSOLE");
299     get_ktest_config("LOCALVERSION");
300
301     my $rtype = $opt{"REBOOT_TYPE"};
302
303     if (!defined($rtype)) {
304         if (!defined($opt{"GRUB_MENU"})) {
305             get_ktest_config("REBOOT_TYPE");
306             $rtype = $entered_configs{"REBOOT_TYPE"};
307         } else {
308             $rtype = "grub";
309         }
310     }
311
312     if ($rtype eq "grub") {
313         get_ktest_config("GRUB_MENU");
314     } else {
315         get_ktest_config("REBOOT_SCRIPT");
316     }
317 }
318
319 sub process_variables {
320     my ($value, $remove_undef) = @_;
321     my $retval = "";
322
323     # We want to check for '\', and it is just easier
324     # to check the previous characet of '$' and not need
325     # to worry if '$' is the first character. By adding
326     # a space to $value, we can just check [^\\]\$ and
327     # it will still work.
328     $value = " $value";
329
330     while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
331         my $begin = $1;
332         my $var = $2;
333         my $end = $3;
334         # append beginning of value to retval
335         $retval = "$retval$begin";
336         if (defined($variable{$var})) {
337             $retval = "$retval$variable{$var}";
338         } elsif (defined($remove_undef) && $remove_undef) {
339             # for if statements, any variable that is not defined,
340             # we simple convert to 0
341             $retval = "${retval}0";
342         } else {
343             # put back the origin piece.
344             $retval = "$retval\$\{$var\}";
345         }
346         $value = $end;
347     }
348     $retval = "$retval$value";
349
350     # remove the space added in the beginning
351     $retval =~ s/ //;
352
353     return "$retval"
354 }
355
356 sub set_value {
357     my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
358
359     if (defined($opt{$lvalue})) {
360         if (!$override || defined(${$overrides}{$lvalue})) {
361             my $extra = "";
362             if ($override) {
363                 $extra = "In the same override section!\n";
364             }
365             die "$name: $.: Option $lvalue defined more than once!\n$extra";
366         }
367         ${$overrides}{$lvalue} = $rvalue;
368     }
369     if ($rvalue =~ /^\s*$/) {
370         delete $opt{$lvalue};
371     } else {
372         $rvalue = process_variables($rvalue);
373         $opt{$lvalue} = $rvalue;
374     }
375 }
376
377 sub set_variable {
378     my ($lvalue, $rvalue) = @_;
379
380     if ($rvalue =~ /^\s*$/) {
381         delete $variable{$lvalue};
382     } else {
383         $rvalue = process_variables($rvalue);
384         $variable{$lvalue} = $rvalue;
385     }
386 }
387
388 sub process_compare {
389     my ($lval, $cmp, $rval) = @_;
390
391     # remove whitespace
392
393     $lval =~ s/^\s*//;
394     $lval =~ s/\s*$//;
395
396     $rval =~ s/^\s*//;
397     $rval =~ s/\s*$//;
398
399     if ($cmp eq "==") {
400         return $lval eq $rval;
401     } elsif ($cmp eq "!=") {
402         return $lval ne $rval;
403     }
404
405     my $statement = "$lval $cmp $rval";
406     my $ret = eval $statement;
407
408     # $@ stores error of eval
409     if ($@) {
410         return -1;
411     }
412
413     return $ret;
414 }
415
416 sub value_defined {
417     my ($val) = @_;
418
419     return defined($variable{$2}) ||
420         defined($opt{$2});
421 }
422
423 my $d = 0;
424 sub process_expression {
425     my ($name, $val) = @_;
426
427     my $c = $d++;
428
429     while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
430         my $express = $1;
431
432         if (process_expression($name, $express)) {
433             $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
434         } else {
435             $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
436         }
437     }
438
439     $d--;
440     my $OR = "\\|\\|";
441     my $AND = "\\&\\&";
442
443     while ($val =~ s/^(.*?)($OR|$AND)//) {
444         my $express = $1;
445         my $op = $2;
446
447         if (process_expression($name, $express)) {
448             if ($op eq "||") {
449                 return 1;
450             }
451         } else {
452             if ($op eq "&&") {
453                 return 0;
454             }
455         }
456     }
457
458     if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
459         my $ret = process_compare($1, $2, $3);
460         if ($ret < 0) {
461             die "$name: $.: Unable to process comparison\n";
462         }
463         return $ret;
464     }
465
466     if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
467         if (defined $1) {
468             return !value_defined($2);
469         } else {
470             return value_defined($2);
471         }
472     }
473
474     if ($val =~ /^\s*0\s*$/) {
475         return 0;
476     } elsif ($val =~ /^\s*\d+\s*$/) {
477         return 1;
478     }
479
480     die ("$name: $.: Undefined content $val in if statement\n");
481 }
482
483 sub process_if {
484     my ($name, $value) = @_;
485
486     # Convert variables and replace undefined ones with 0
487     my $val = process_variables($value, 1);
488     my $ret = process_expression $name, $val;
489
490     return $ret;
491 }
492
493 sub __read_config {
494     my ($config, $current_test_num) = @_;
495
496     my $in;
497     open($in, $config) || die "can't read file $config";
498
499     my $name = $config;
500     $name =~ s,.*/(.*),$1,;
501
502     my $test_num = $$current_test_num;
503     my $default = 1;
504     my $repeat = 1;
505     my $num_tests_set = 0;
506     my $skip = 0;
507     my $rest;
508     my $line;
509     my $test_case = 0;
510     my $if = 0;
511     my $if_set = 0;
512     my $override = 0;
513
514     my %overrides;
515
516     while (<$in>) {
517
518         # ignore blank lines and comments
519         next if (/^\s*$/ || /\s*\#/);
520
521         if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
522
523             my $type = $1;
524             $rest = $2;
525             $line = $2;
526
527             my $old_test_num;
528             my $old_repeat;
529             $override = 0;
530
531             if ($type eq "TEST_START") {
532
533                 if ($num_tests_set) {
534                     die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
535                 }
536
537                 $old_test_num = $test_num;
538                 $old_repeat = $repeat;
539
540                 $test_num += $repeat;
541                 $default = 0;
542                 $repeat = 1;
543             } else {
544                 $default = 1;
545             }
546
547             # If SKIP is anywhere in the line, the command will be skipped
548             if ($rest =~ s/\s+SKIP\b//) {
549                 $skip = 1;
550             } else {
551                 $test_case = 1;
552                 $skip = 0;
553             }
554
555             if ($rest =~ s/\sELSE\b//) {
556                 if (!$if) {
557                     die "$name: $.: ELSE found with out matching IF section\n$_";
558                 }
559                 $if = 0;
560
561                 if ($if_set) {
562                     $skip = 1;
563                 } else {
564                     $skip = 0;
565                 }
566             }
567
568             if ($rest =~ s/\sIF\s+(.*)//) {
569                 if (process_if($name, $1)) {
570                     $if_set = 1;
571                 } else {
572                     $skip = 1;
573                 }
574                 $if = 1;
575             } else {
576                 $if = 0;
577                 $if_set = 0;
578             }
579
580             if (!$skip) {
581                 if ($type eq "TEST_START") {
582                     if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
583                         $repeat = $1;
584                         $repeat_tests{"$test_num"} = $repeat;
585                     }
586                 } elsif ($rest =~ s/\sOVERRIDE\b//) {
587                     # DEFAULT only
588                     $override = 1;
589                     # Clear previous overrides
590                     %overrides = ();
591                 }
592             }
593
594             if (!$skip && $rest !~ /^\s*$/) {
595                 die "$name: $.: Gargbage found after $type\n$_";
596             }
597
598             if ($skip && $type eq "TEST_START") {
599                 $test_num = $old_test_num;
600                 $repeat = $old_repeat;
601             }
602
603         } elsif (/^\s*ELSE\b(.*)$/) {
604             if (!$if) {
605                 die "$name: $.: ELSE found with out matching IF section\n$_";
606             }
607             $rest = $1;
608             if ($if_set) {
609                 $skip = 1;
610                 $rest = "";
611             } else {
612                 $skip = 0;
613
614                 if ($rest =~ /\sIF\s+(.*)/) {
615                     # May be a ELSE IF section.
616                     if (!process_if($name, $1)) {
617                         $skip = 1;
618                     }
619                     $rest = "";
620                 } else {
621                     $if = 0;
622                 }
623             }
624
625             if ($rest !~ /^\s*$/) {
626                 die "$name: $.: Gargbage found after DEFAULTS\n$_";
627             }
628
629         } elsif (/^\s*INCLUDE\s+(\S+)/) {
630
631             next if ($skip);
632
633             if (!$default) {
634                 die "$name: $.: INCLUDE can only be done in default sections\n$_";
635             }
636
637             my $file = process_variables($1);
638
639             if ($file !~ m,^/,) {
640                 # check the path of the config file first
641                 if ($config =~ m,(.*)/,) {
642                     if (-f "$1/$file") {
643                         $file = "$1/$file";
644                     }
645                 }
646             }
647                 
648             if ( ! -r $file ) {
649                 die "$name: $.: Can't read file $file\n$_";
650             }
651
652             if (__read_config($file, \$test_num)) {
653                 $test_case = 1;
654             }
655
656         } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
657
658             next if ($skip);
659
660             my $lvalue = $1;
661             my $rvalue = $2;
662
663             if (!$default &&
664                 ($lvalue eq "NUM_TESTS" ||
665                  $lvalue eq "LOG_FILE" ||
666                  $lvalue eq "CLEAR_LOG")) {
667                 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
668             }
669
670             if ($lvalue eq "NUM_TESTS") {
671                 if ($test_num) {
672                     die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
673                 }
674                 if (!$default) {
675                     die "$name: $.: NUM_TESTS must be set in default section\n";
676                 }
677                 $num_tests_set = 1;
678             }
679
680             if ($default || $lvalue =~ /\[\d+\]$/) {
681                 set_value($lvalue, $rvalue, $override, \%overrides, $name);
682             } else {
683                 my $val = "$lvalue\[$test_num\]";
684                 set_value($val, $rvalue, $override, \%overrides, $name);
685
686                 if ($repeat > 1) {
687                     $repeats{$val} = $repeat;
688                 }
689             }
690         } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
691             next if ($skip);
692
693             my $lvalue = $1;
694             my $rvalue = $2;
695
696             # process config variables.
697             # Config variables are only active while reading the
698             # config and can be defined anywhere. They also ignore
699             # TEST_START and DEFAULTS, but are skipped if they are in
700             # on of these sections that have SKIP defined.
701             # The save variable can be
702             # defined multiple times and the new one simply overrides
703             # the prevous one.
704             set_variable($lvalue, $rvalue);
705
706         } else {
707             die "$name: $.: Garbage found in config\n$_";
708         }
709     }
710
711     if ($test_num) {
712         $test_num += $repeat - 1;
713         $opt{"NUM_TESTS"} = $test_num;
714     }
715
716     close($in);
717
718     $$current_test_num = $test_num;
719
720     return $test_case;
721 }
722
723 sub read_config {
724     my ($config) = @_;
725
726     my $test_case;
727     my $test_num = 0;
728
729     $test_case = __read_config $config, \$test_num;
730
731     # make sure we have all mandatory configs
732     get_ktest_configs;
733
734     # was a test specified?
735     if (!$test_case) {
736         print "No test case specified.\n";
737         print "What test case would you like to run?\n";
738         my $ans = <STDIN>;
739         chomp $ans;
740         $default{"TEST_TYPE"} = $ans;
741     }
742
743     # set any defaults
744
745     foreach my $default (keys %default) {
746         if (!defined($opt{$default})) {
747             $opt{$default} = $default{$default};
748         }
749     }
750 }
751
752 sub __eval_option {
753     my ($option, $i) = @_;
754
755     # Add space to evaluate the character before $
756     $option = " $option";
757     my $retval = "";
758     my $repeated = 0;
759     my $parent = 0;
760
761     foreach my $test (keys %repeat_tests) {
762         if ($i >= $test &&
763             $i < $test + $repeat_tests{$test}) {
764
765             $repeated = 1;
766             $parent = $test;
767             last;
768         }
769     }
770
771     while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
772         my $start = $1;
773         my $var = $2;
774         my $end = $3;
775
776         # Append beginning of line
777         $retval = "$retval$start";
778
779         # If the iteration option OPT[$i] exists, then use that.
780         # otherwise see if the default OPT (without [$i]) exists.
781
782         my $o = "$var\[$i\]";
783         my $parento = "$var\[$parent\]";
784
785         if (defined($opt{$o})) {
786             $o = $opt{$o};
787             $retval = "$retval$o";
788         } elsif ($repeated && defined($opt{$parento})) {
789             $o = $opt{$parento};
790             $retval = "$retval$o";
791         } elsif (defined($opt{$var})) {
792             $o = $opt{$var};
793             $retval = "$retval$o";
794         } else {
795             $retval = "$retval\$\{$var\}";
796         }
797
798         $option = $end;
799     }
800
801     $retval = "$retval$option";
802
803     $retval =~ s/^ //;
804
805     return $retval;
806 }
807
808 sub eval_option {
809     my ($option, $i) = @_;
810
811     my $prev = "";
812
813     # Since an option can evaluate to another option,
814     # keep iterating until we do not evaluate any more
815     # options.
816     my $r = 0;
817     while ($prev ne $option) {
818         # Check for recursive evaluations.
819         # 100 deep should be more than enough.
820         if ($r++ > 100) {
821             die "Over 100 evaluations accurred with $option\n" .
822                 "Check for recursive variables\n";
823         }
824         $prev = $option;
825         $option = __eval_option($option, $i);
826     }
827
828     return $option;
829 }
830
831 sub _logit {
832     if (defined($opt{"LOG_FILE"})) {
833         open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
834         print OUT @_;
835         close(OUT);
836     }
837 }
838
839 sub logit {
840     if (defined($opt{"LOG_FILE"})) {
841         _logit @_;
842     } else {
843         print @_;
844     }
845 }
846
847 sub doprint {
848     print @_;
849     _logit @_;
850 }
851
852 sub run_command;
853 sub start_monitor;
854 sub end_monitor;
855 sub wait_for_monitor;
856
857 sub reboot {
858     my ($time) = @_;
859
860     if (defined($time)) {
861         start_monitor;
862         # flush out current monitor
863         # May contain the reboot success line
864         wait_for_monitor 1;
865     }
866
867     # try to reboot normally
868     if (run_command $reboot) {
869         if (defined($powercycle_after_reboot)) {
870             sleep $powercycle_after_reboot;
871             run_command "$power_cycle";
872         }
873     } else {
874         # nope? power cycle it.
875         run_command "$power_cycle";
876     }
877
878     if (defined($time)) {
879         wait_for_monitor($time, $reboot_success_line);
880         end_monitor;
881     }
882 }
883
884 sub do_not_reboot {
885     my $i = $iteration;
886
887     return $test_type eq "build" || $no_reboot ||
888         ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
889         ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
890 }
891
892 sub dodie {
893     doprint "CRITICAL FAILURE... ", @_, "\n";
894
895     my $i = $iteration;
896
897     if ($reboot_on_error && !do_not_reboot) {
898
899         doprint "REBOOTING\n";
900         reboot;
901
902     } elsif ($poweroff_on_error && defined($power_off)) {
903         doprint "POWERING OFF\n";
904         `$power_off`;
905     }
906
907     if (defined($opt{"LOG_FILE"})) {
908         print " See $opt{LOG_FILE} for more info.\n";
909     }
910
911     die @_, "\n";
912 }
913
914 sub open_console {
915     my ($fp) = @_;
916
917     my $flags;
918
919     my $pid = open($fp, "$console|") or
920         dodie "Can't open console $console";
921
922     $flags = fcntl($fp, F_GETFL, 0) or
923         dodie "Can't get flags for the socket: $!";
924     $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
925         dodie "Can't set flags for the socket: $!";
926
927     return $pid;
928 }
929
930 sub close_console {
931     my ($fp, $pid) = @_;
932
933     doprint "kill child process $pid\n";
934     kill 2, $pid;
935
936     print "closing!\n";
937     close($fp);
938 }
939
940 sub start_monitor {
941     if ($monitor_cnt++) {
942         return;
943     }
944     $monitor_fp = \*MONFD;
945     $monitor_pid = open_console $monitor_fp;
946
947     return;
948
949     open(MONFD, "Stop perl from warning about single use of MONFD");
950 }
951
952 sub end_monitor {
953     if (--$monitor_cnt) {
954         return;
955     }
956     close_console($monitor_fp, $monitor_pid);
957 }
958
959 sub wait_for_monitor {
960     my ($time, $stop) = @_;
961     my $full_line = "";
962     my $line;
963     my $booted = 0;
964
965     doprint "** Wait for monitor to settle down **\n";
966
967     # read the monitor and wait for the system to calm down
968     while (!$booted) {
969         $line = wait_for_input($monitor_fp, $time);
970         last if (!defined($line));
971         print "$line";
972         $full_line .= $line;
973
974         if (defined($stop) && $full_line =~ /$stop/) {
975             doprint "wait for monitor detected $stop\n";
976             $booted = 1;
977         }
978
979         if ($line =~ /\n/) {
980             $full_line = "";
981         }
982     }
983     print "** Monitor flushed **\n";
984 }
985
986 sub save_logs {
987         my ($result, $basedir) = @_;
988         my @t = localtime;
989         my $date = sprintf "%04d%02d%02d%02d%02d%02d",
990                 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
991
992         my $type = $build_type;
993         if ($type =~ /useconfig/) {
994             $type = "useconfig";
995         }
996
997         my $dir = "$machine-$test_type-$type-$result-$date";
998
999         $dir = "$basedir/$dir";
1000
1001         if (!-d $dir) {
1002             mkpath($dir) or
1003                 die "can't create $dir";
1004         }
1005
1006         my %files = (
1007                 "config" => $output_config,
1008                 "buildlog" => $buildlog,
1009                 "dmesg" => $dmesg,
1010                 "testlog" => $testlog,
1011         );
1012
1013         while (my ($name, $source) = each(%files)) {
1014                 if (-f "$source") {
1015                         cp "$source", "$dir/$name" or
1016                                 die "failed to copy $source";
1017                 }
1018         }
1019
1020         doprint "*** Saved info to $dir ***\n";
1021 }
1022
1023 sub fail {
1024
1025         if ($die_on_failure) {
1026                 dodie @_;
1027         }
1028
1029         doprint "FAILED\n";
1030
1031         my $i = $iteration;
1032
1033         # no need to reboot for just building.
1034         if (!do_not_reboot) {
1035             doprint "REBOOTING\n";
1036             reboot $sleep_time;
1037         }
1038
1039         my $name = "";
1040
1041         if (defined($test_name)) {
1042             $name = " ($test_name)";
1043         }
1044
1045         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1046         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1047         doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
1048         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1049         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1050
1051         if (defined($store_failures)) {
1052             save_logs "fail", $store_failures;
1053         }
1054
1055         return 1;
1056 }
1057
1058 sub run_command {
1059     my ($command) = @_;
1060     my $dolog = 0;
1061     my $dord = 0;
1062     my $pid;
1063
1064     $command =~ s/\$SSH_USER/$ssh_user/g;
1065     $command =~ s/\$MACHINE/$machine/g;
1066
1067     doprint("$command ... ");
1068
1069     $pid = open(CMD, "$command 2>&1 |") or
1070         (fail "unable to exec $command" and return 0);
1071
1072     if (defined($opt{"LOG_FILE"})) {
1073         open(LOG, ">>$opt{LOG_FILE}") or
1074             dodie "failed to write to log";
1075         $dolog = 1;
1076     }
1077
1078     if (defined($redirect)) {
1079         open (RD, ">$redirect") or
1080             dodie "failed to write to redirect $redirect";
1081         $dord = 1;
1082     }
1083
1084     while (<CMD>) {
1085         print LOG if ($dolog);
1086         print RD  if ($dord);
1087     }
1088
1089     waitpid($pid, 0);
1090     my $failed = $?;
1091
1092     close(CMD);
1093     close(LOG) if ($dolog);
1094     close(RD)  if ($dord);
1095
1096     if ($failed) {
1097         doprint "FAILED!\n";
1098     } else {
1099         doprint "SUCCESS\n";
1100     }
1101
1102     return !$failed;
1103 }
1104
1105 sub run_ssh {
1106     my ($cmd) = @_;
1107     my $cp_exec = $ssh_exec;
1108
1109     $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1110     return run_command "$cp_exec";
1111 }
1112
1113 sub run_scp {
1114     my ($src, $dst) = @_;
1115     my $cp_scp = $scp_to_target;
1116
1117     $cp_scp =~ s/\$SRC_FILE/$src/g;
1118     $cp_scp =~ s/\$DST_FILE/$dst/g;
1119
1120     return run_command "$cp_scp";
1121 }
1122
1123 sub get_grub_index {
1124
1125     if ($reboot_type ne "grub") {
1126         return;
1127     }
1128     return if (defined($grub_number));
1129
1130     doprint "Find grub menu ... ";
1131     $grub_number = -1;
1132
1133     my $ssh_grub = $ssh_exec;
1134     $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1135
1136     open(IN, "$ssh_grub |")
1137         or die "unable to get menu.lst";
1138
1139     my $found = 0;
1140
1141     while (<IN>) {
1142         if (/^\s*title\s+$grub_menu\s*$/) {
1143             $grub_number++;
1144             $found = 1;
1145             last;
1146         } elsif (/^\s*title\s/) {
1147             $grub_number++;
1148         }
1149     }
1150     close(IN);
1151
1152     die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
1153         if (!$found);
1154     doprint "$grub_number\n";
1155 }
1156
1157 sub wait_for_input
1158 {
1159     my ($fp, $time) = @_;
1160     my $rin;
1161     my $ready;
1162     my $line;
1163     my $ch;
1164
1165     if (!defined($time)) {
1166         $time = $timeout;
1167     }
1168
1169     $rin = '';
1170     vec($rin, fileno($fp), 1) = 1;
1171     $ready = select($rin, undef, undef, $time);
1172
1173     $line = "";
1174
1175     # try to read one char at a time
1176     while (sysread $fp, $ch, 1) {
1177         $line .= $ch;
1178         last if ($ch eq "\n");
1179     }
1180
1181     if (!length($line)) {
1182         return undef;
1183     }
1184
1185     return $line;
1186 }
1187
1188 sub reboot_to {
1189     if ($reboot_type eq "grub") {
1190         run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1191         reboot;
1192         return;
1193     }
1194
1195     run_command "$reboot_script";
1196 }
1197
1198 sub get_sha1 {
1199     my ($commit) = @_;
1200
1201     doprint "git rev-list --max-count=1 $commit ... ";
1202     my $sha1 = `git rev-list --max-count=1 $commit`;
1203     my $ret = $?;
1204
1205     logit $sha1;
1206
1207     if ($ret) {
1208         doprint "FAILED\n";
1209         dodie "Failed to get git $commit";
1210     }
1211
1212     print "SUCCESS\n";
1213
1214     chomp $sha1;
1215
1216     return $sha1;
1217 }
1218
1219 sub monitor {
1220     my $booted = 0;
1221     my $bug = 0;
1222     my $skip_call_trace = 0;
1223     my $loops;
1224
1225     wait_for_monitor 5;
1226
1227     my $line;
1228     my $full_line = "";
1229
1230     open(DMESG, "> $dmesg") or
1231         die "unable to write to $dmesg";
1232
1233     reboot_to;
1234
1235     my $success_start;
1236     my $failure_start;
1237     my $monitor_start = time;
1238     my $done = 0;
1239     my $version_found = 0;
1240
1241     while (!$done) {
1242
1243         if ($bug && defined($stop_after_failure) &&
1244             $stop_after_failure >= 0) {
1245             my $time = $stop_after_failure - (time - $failure_start);
1246             $line = wait_for_input($monitor_fp, $time);
1247             if (!defined($line)) {
1248                 doprint "bug timed out after $booted_timeout seconds\n";
1249                 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1250                 last;
1251             }
1252         } elsif ($booted) {
1253             $line = wait_for_input($monitor_fp, $booted_timeout);
1254             if (!defined($line)) {
1255                 my $s = $booted_timeout == 1 ? "" : "s";
1256                 doprint "Successful boot found: break after $booted_timeout second$s\n";
1257                 last;
1258             }
1259         } else {
1260             $line = wait_for_input($monitor_fp);
1261             if (!defined($line)) {
1262                 my $s = $timeout == 1 ? "" : "s";
1263                 doprint "Timed out after $timeout second$s\n";
1264                 last;
1265             }
1266         }
1267
1268         doprint $line;
1269         print DMESG $line;
1270
1271         # we are not guaranteed to get a full line
1272         $full_line .= $line;
1273
1274         if ($full_line =~ /$success_line/) {
1275             $booted = 1;
1276             $success_start = time;
1277         }
1278
1279         if ($booted && defined($stop_after_success) &&
1280             $stop_after_success >= 0) {
1281             my $now = time;
1282             if ($now - $success_start >= $stop_after_success) {
1283                 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1284                 last;
1285             }
1286         }
1287
1288         if ($full_line =~ /\[ backtrace testing \]/) {
1289             $skip_call_trace = 1;
1290         }
1291
1292         if ($full_line =~ /call trace:/i) {
1293             if (!$bug && !$skip_call_trace) {
1294                 $bug = 1;
1295                 $failure_start = time;
1296             }
1297         }
1298
1299         if ($bug && defined($stop_after_failure) &&
1300             $stop_after_failure >= 0) {
1301             my $now = time;
1302             if ($now - $failure_start >= $stop_after_failure) {
1303                 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1304                 last;
1305             }
1306         }
1307
1308         if ($full_line =~ /\[ end of backtrace testing \]/) {
1309             $skip_call_trace = 0;
1310         }
1311
1312         if ($full_line =~ /Kernel panic -/) {
1313             $failure_start = time;
1314             $bug = 1;
1315         }
1316
1317         # Detect triple faults by testing the banner
1318         if ($full_line =~ /\bLinux version (\S+).*\n/) {
1319             if ($1 eq $version) {
1320                 $version_found = 1;
1321             } elsif ($version_found && $detect_triplefault) {
1322                 # We already booted into the kernel we are testing,
1323                 # but now we booted into another kernel?
1324                 # Consider this a triple fault.
1325                 doprint "Aleady booted in Linux kernel $version, but now\n";
1326                 doprint "we booted into Linux kernel $1.\n";
1327                 doprint "Assuming that this is a triple fault.\n";
1328                 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1329                 last;
1330             }
1331         }
1332
1333         if ($line =~ /\n/) {
1334             $full_line = "";
1335         }
1336
1337         if ($stop_test_after > 0 && !$booted && !$bug) {
1338             if (time - $monitor_start > $stop_test_after) {
1339                 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
1340                 $done = 1;
1341             }
1342         }
1343     }
1344
1345     close(DMESG);
1346
1347     if ($bug) {
1348         return 0 if ($in_bisect);
1349         fail "failed - got a bug report" and return 0;
1350     }
1351
1352     if (!$booted) {
1353         return 0 if ($in_bisect);
1354         fail "failed - never got a boot prompt." and return 0;
1355     }
1356
1357     return 1;
1358 }
1359
1360 sub do_post_install {
1361
1362     return if (!defined($post_install));
1363
1364     my $cp_post_install = $post_install;
1365     $cp_post_install =~ s/\$KERNEL_VERSION/$version/g;
1366     run_command "$cp_post_install" or
1367         dodie "Failed to run post install";
1368 }
1369
1370 sub install {
1371
1372     return if ($no_install);
1373
1374     run_scp "$outputdir/$build_target", "$target_image" or
1375         dodie "failed to copy image";
1376
1377     my $install_mods = 0;
1378
1379     # should we process modules?
1380     $install_mods = 0;
1381     open(IN, "$output_config") or dodie("Can't read config file");
1382     while (<IN>) {
1383         if (/CONFIG_MODULES(=y)?/) {
1384             $install_mods = 1 if (defined($1));
1385             last;
1386         }
1387     }
1388     close(IN);
1389
1390     if (!$install_mods) {
1391         do_post_install;
1392         doprint "No modules needed\n";
1393         return;
1394     }
1395
1396     run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
1397         dodie "Failed to install modules";
1398
1399     my $modlib = "/lib/modules/$version";
1400     my $modtar = "ktest-mods.tar.bz2";
1401
1402     run_ssh "rm -rf $modlib" or
1403         dodie "failed to remove old mods: $modlib";
1404
1405     # would be nice if scp -r did not follow symbolic links
1406     run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
1407         dodie "making tarball";
1408
1409     run_scp "$tmpdir/$modtar", "/tmp" or
1410         dodie "failed to copy modules";
1411
1412     unlink "$tmpdir/$modtar";
1413
1414     run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
1415         dodie "failed to tar modules";
1416
1417     run_ssh "rm -f /tmp/$modtar";
1418
1419     do_post_install;
1420 }
1421
1422 sub get_version {
1423     # get the release name
1424     doprint "$make kernelrelease ... ";
1425     $version = `$make kernelrelease | tail -1`;
1426     chomp($version);
1427     doprint "$version\n";
1428 }
1429
1430 sub start_monitor_and_boot {
1431     # Make sure the stable kernel has finished booting
1432     start_monitor;
1433     wait_for_monitor 5;
1434     end_monitor;
1435
1436     get_grub_index;
1437     get_version;
1438     install;
1439
1440     start_monitor;
1441     return monitor;
1442 }
1443
1444 sub check_buildlog {
1445     my ($patch) = @_;
1446
1447     my @files = `git show $patch | diffstat -l`;
1448
1449     open(IN, "git show $patch |") or
1450         dodie "failed to show $patch";
1451     while (<IN>) {
1452         if (m,^--- a/(.*),) {
1453             chomp $1;
1454             $files[$#files] = $1;
1455         }
1456     }
1457     close(IN);
1458
1459     open(IN, $buildlog) or dodie "Can't open $buildlog";
1460     while (<IN>) {
1461         if (/^\s*(.*?):.*(warning|error)/) {
1462             my $err = $1;
1463             foreach my $file (@files) {
1464                 my $fullpath = "$builddir/$file";
1465                 if ($file eq $err || $fullpath eq $err) {
1466                     fail "$file built with warnings" and return 0;
1467                 }
1468             }
1469         }
1470     }
1471     close(IN);
1472
1473     return 1;
1474 }
1475
1476 sub apply_min_config {
1477     my $outconfig = "$output_config.new";
1478
1479     # Read the config file and remove anything that
1480     # is in the force_config hash (from minconfig and others)
1481     # then add the force config back.
1482
1483     doprint "Applying minimum configurations into $output_config.new\n";
1484
1485     open (OUT, ">$outconfig") or
1486         dodie "Can't create $outconfig";
1487
1488     if (-f $output_config) {
1489         open (IN, $output_config) or
1490             dodie "Failed to open $output_config";
1491         while (<IN>) {
1492             if (/^(# )?(CONFIG_[^\s=]*)/) {
1493                 next if (defined($force_config{$2}));
1494             }
1495             print OUT;
1496         }
1497         close IN;
1498     }
1499     foreach my $config (keys %force_config) {
1500         print OUT "$force_config{$config}\n";
1501     }
1502     close OUT;
1503
1504     run_command "mv $outconfig $output_config";
1505 }
1506
1507 sub make_oldconfig {
1508
1509     my @force_list = keys %force_config;
1510
1511     if ($#force_list >= 0) {
1512         apply_min_config;
1513     }
1514
1515     if (!run_command "$make oldnoconfig") {
1516         # Perhaps oldnoconfig doesn't exist in this version of the kernel
1517         # try a yes '' | oldconfig
1518         doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
1519         run_command "yes '' | $make oldconfig" or
1520             dodie "failed make config oldconfig";
1521     }
1522 }
1523
1524 # read a config file and use this to force new configs.
1525 sub load_force_config {
1526     my ($config) = @_;
1527
1528     open(IN, $config) or
1529         dodie "failed to read $config";
1530     while (<IN>) {
1531         chomp;
1532         if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1533             $force_config{$1} = $_;
1534         } elsif (/^# (CONFIG_\S*) is not set/) {
1535             $force_config{$1} = $_;
1536         }
1537     }
1538     close IN;
1539 }
1540
1541 sub build {
1542     my ($type) = @_;
1543
1544     unlink $buildlog;
1545
1546     # Failed builds should not reboot the target
1547     my $save_no_reboot = $no_reboot;
1548     $no_reboot = 1;
1549
1550     if (defined($pre_build)) {
1551         my $ret = run_command $pre_build;
1552         if (!$ret && defined($pre_build_die) &&
1553             $pre_build_die) {
1554             dodie "failed to pre_build\n";
1555         }
1556     }
1557
1558     if ($type =~ /^useconfig:(.*)/) {
1559         run_command "cp $1 $output_config" or
1560             dodie "could not copy $1 to .config";
1561
1562         $type = "oldconfig";
1563     }
1564
1565     # old config can ask questions
1566     if ($type eq "oldconfig") {
1567         $type = "oldnoconfig";
1568
1569         # allow for empty configs
1570         run_command "touch $output_config";
1571
1572         if (!$noclean) {
1573             run_command "mv $output_config $outputdir/config_temp" or
1574                 dodie "moving .config";
1575
1576             run_command "$make mrproper" or dodie "make mrproper";
1577
1578             run_command "mv $outputdir/config_temp $output_config" or
1579                 dodie "moving config_temp";
1580         }
1581
1582     } elsif (!$noclean) {
1583         unlink "$output_config";
1584         run_command "$make mrproper" or
1585             dodie "make mrproper";
1586     }
1587
1588     # add something to distinguish this build
1589     open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1590     print OUT "$localversion\n";
1591     close(OUT);
1592
1593     if (defined($minconfig)) {
1594         load_force_config($minconfig);
1595     }
1596
1597     if ($type ne "oldnoconfig") {
1598         run_command "$make $type" or
1599             dodie "failed make config";
1600     }
1601     # Run old config regardless, to enforce min configurations
1602     make_oldconfig;
1603
1604     $redirect = "$buildlog";
1605     my $build_ret = run_command "$make $build_options";
1606     undef $redirect;
1607
1608     if (defined($post_build)) {
1609         my $ret = run_command $post_build;
1610         if (!$ret && defined($post_build_die) &&
1611             $post_build_die) {
1612             dodie "failed to post_build\n";
1613         }
1614     }
1615
1616     if (!$build_ret) {
1617         # bisect may need this to pass
1618         if ($in_bisect) {
1619             $no_reboot = $save_no_reboot;
1620             return 0;
1621         }
1622         fail "failed build" and return 0;
1623     }
1624
1625     $no_reboot = $save_no_reboot;
1626
1627     return 1;
1628 }
1629
1630 sub halt {
1631     if (!run_ssh "halt" or defined($power_off)) {
1632         if (defined($poweroff_after_halt)) {
1633             sleep $poweroff_after_halt;
1634             run_command "$power_off";
1635         }
1636     } else {
1637         # nope? the zap it!
1638         run_command "$power_off";
1639     }
1640 }
1641
1642 sub success {
1643     my ($i) = @_;
1644
1645     $successes++;
1646
1647     my $name = "";
1648
1649     if (defined($test_name)) {
1650         $name = " ($test_name)";
1651     }
1652
1653     doprint "\n\n*******************************************\n";
1654     doprint     "*******************************************\n";
1655     doprint     "KTEST RESULT: TEST $i$name SUCCESS!!!!         **\n";
1656     doprint     "*******************************************\n";
1657     doprint     "*******************************************\n";
1658
1659     if (defined($store_successes)) {
1660         save_logs "success", $store_successes;
1661     }
1662
1663     if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
1664         doprint "Reboot and wait $sleep_time seconds\n";
1665         reboot $sleep_time;
1666     }
1667 }
1668
1669 sub answer_bisect {
1670     for (;;) {
1671         doprint "Pass or fail? [p/f]";
1672         my $ans = <STDIN>;
1673         chomp $ans;
1674         if ($ans eq "p" || $ans eq "P") {
1675             return 1;
1676         } elsif ($ans eq "f" || $ans eq "F") {
1677             return 0;
1678         } else {
1679             print "Please answer 'P' or 'F'\n";
1680         }
1681     }
1682 }
1683
1684 sub child_run_test {
1685     my $failed = 0;
1686
1687     # child should have no power
1688     $reboot_on_error = 0;
1689     $poweroff_on_error = 0;
1690     $die_on_failure = 1;
1691
1692     $redirect = "$testlog";
1693     run_command $run_test or $failed = 1;
1694     undef $redirect;
1695
1696     exit $failed;
1697 }
1698
1699 my $child_done;
1700
1701 sub child_finished {
1702     $child_done = 1;
1703 }
1704
1705 sub do_run_test {
1706     my $child_pid;
1707     my $child_exit;
1708     my $line;
1709     my $full_line;
1710     my $bug = 0;
1711
1712     wait_for_monitor 1;
1713
1714     doprint "run test $run_test\n";
1715
1716     $child_done = 0;
1717
1718     $SIG{CHLD} = qw(child_finished);
1719
1720     $child_pid = fork;
1721
1722     child_run_test if (!$child_pid);
1723
1724     $full_line = "";
1725
1726     do {
1727         $line = wait_for_input($monitor_fp, 1);
1728         if (defined($line)) {
1729
1730             # we are not guaranteed to get a full line
1731             $full_line .= $line;
1732             doprint $line;
1733
1734             if ($full_line =~ /call trace:/i) {
1735                 $bug = 1;
1736             }
1737
1738             if ($full_line =~ /Kernel panic -/) {
1739                 $bug = 1;
1740             }
1741
1742             if ($line =~ /\n/) {
1743                 $full_line = "";
1744             }
1745         }
1746     } while (!$child_done && !$bug);
1747
1748     if ($bug) {
1749         my $failure_start = time;
1750         my $now;
1751         do {
1752             $line = wait_for_input($monitor_fp, 1);
1753             if (defined($line)) {
1754                 doprint $line;
1755             }
1756             $now = time;
1757             if ($now - $failure_start >= $stop_after_failure) {
1758                 last;
1759             }
1760         } while (defined($line));
1761
1762         doprint "Detected kernel crash!\n";
1763         # kill the child with extreme prejudice
1764         kill 9, $child_pid;
1765     }
1766
1767     waitpid $child_pid, 0;
1768     $child_exit = $?;
1769
1770     if ($bug || $child_exit) {
1771         return 0 if $in_bisect;
1772         fail "test failed" and return 0;
1773     }
1774     return 1;
1775 }
1776
1777 sub run_git_bisect {
1778     my ($command) = @_;
1779
1780     doprint "$command ... ";
1781
1782     my $output = `$command 2>&1`;
1783     my $ret = $?;
1784
1785     logit $output;
1786
1787     if ($ret) {
1788         doprint "FAILED\n";
1789         dodie "Failed to git bisect";
1790     }
1791
1792     doprint "SUCCESS\n";
1793     if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1794         doprint "$1 [$2]\n";
1795     } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1796         $bisect_bad = $1;
1797         doprint "Found bad commit... $1\n";
1798         return 0;
1799     } else {
1800         # we already logged it, just print it now.
1801         print $output;
1802     }
1803
1804     return 1;
1805 }
1806
1807 sub bisect_reboot {
1808     doprint "Reboot and sleep $bisect_sleep_time seconds\n";
1809     reboot $bisect_sleep_time;
1810 }
1811
1812 # returns 1 on success, 0 on failure, -1 on skip
1813 sub run_bisect_test {
1814     my ($type, $buildtype) = @_;
1815
1816     my $failed = 0;
1817     my $result;
1818     my $output;
1819     my $ret;
1820
1821     $in_bisect = 1;
1822
1823     build $buildtype or $failed = 1;
1824
1825     if ($type ne "build") {
1826         if ($failed && $bisect_skip) {
1827             $in_bisect = 0;
1828             return -1;
1829         }
1830         dodie "Failed on build" if $failed;
1831
1832         # Now boot the box
1833         start_monitor_and_boot or $failed = 1;
1834
1835         if ($type ne "boot") {
1836             if ($failed && $bisect_skip) {
1837                 end_monitor;
1838                 bisect_reboot;
1839                 $in_bisect = 0;
1840                 return -1;
1841             }
1842             dodie "Failed on boot" if $failed;
1843
1844             do_run_test or $failed = 1;
1845         }
1846         end_monitor;
1847     }
1848
1849     if ($failed) {
1850         $result = 0;
1851     } else {
1852         $result = 1;
1853     }
1854
1855     # reboot the box to a kernel we can ssh to
1856     if ($type ne "build") {
1857         bisect_reboot;
1858     }
1859     $in_bisect = 0;
1860
1861     return $result;
1862 }
1863
1864 sub run_bisect {
1865     my ($type) = @_;
1866     my $buildtype = "oldconfig";
1867
1868     # We should have a minconfig to use?
1869     if (defined($minconfig)) {
1870         $buildtype = "useconfig:$minconfig";
1871     }
1872
1873     my $ret = run_bisect_test $type, $buildtype;
1874
1875     if ($bisect_manual) {
1876         $ret = answer_bisect;
1877     }
1878
1879     # Are we looking for where it worked, not failed?
1880     if ($reverse_bisect) {
1881         $ret = !$ret;
1882     }
1883
1884     if ($ret > 0) {
1885         return "good";
1886     } elsif ($ret == 0) {
1887         return  "bad";
1888     } elsif ($bisect_skip) {
1889         doprint "HIT A BAD COMMIT ... SKIPPING\n";
1890         return "skip";
1891     }
1892 }
1893
1894 sub bisect {
1895     my ($i) = @_;
1896
1897     my $result;
1898
1899     die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1900     die "BISECT_BAD[$i] not defined\n"  if (!defined($opt{"BISECT_BAD[$i]"}));
1901     die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1902
1903     my $good = $opt{"BISECT_GOOD[$i]"};
1904     my $bad = $opt{"BISECT_BAD[$i]"};
1905     my $type = $opt{"BISECT_TYPE[$i]"};
1906     my $start = $opt{"BISECT_START[$i]"};
1907     my $replay = $opt{"BISECT_REPLAY[$i]"};
1908     my $start_files = $opt{"BISECT_FILES[$i]"};
1909
1910     if (defined($start_files)) {
1911         $start_files = " -- " . $start_files;
1912     } else {
1913         $start_files = "";
1914     }
1915
1916     # convert to true sha1's
1917     $good = get_sha1($good);
1918     $bad = get_sha1($bad);
1919
1920     if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1921         $opt{"BISECT_REVERSE[$i]"} == 1) {
1922         doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
1923         $reverse_bisect = 1;
1924     } else {
1925         $reverse_bisect = 0;
1926     }
1927
1928     # Can't have a test without having a test to run
1929     if ($type eq "test" && !defined($run_test)) {
1930         $type = "boot";
1931     }
1932
1933     my $check = $opt{"BISECT_CHECK[$i]"};
1934     if (defined($check) && $check ne "0") {
1935
1936         # get current HEAD
1937         my $head = get_sha1("HEAD");
1938
1939         if ($check ne "good") {
1940             doprint "TESTING BISECT BAD [$bad]\n";
1941             run_command "git checkout $bad" or
1942                 die "Failed to checkout $bad";
1943
1944             $result = run_bisect $type;
1945
1946             if ($result ne "bad") {
1947                 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1948             }
1949         }
1950
1951         if ($check ne "bad") {
1952             doprint "TESTING BISECT GOOD [$good]\n";
1953             run_command "git checkout $good" or
1954                 die "Failed to checkout $good";
1955
1956             $result = run_bisect $type;
1957
1958             if ($result ne "good") {
1959                 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1960             }
1961         }
1962
1963         # checkout where we started
1964         run_command "git checkout $head" or
1965             die "Failed to checkout $head";
1966     }
1967
1968     run_command "git bisect start$start_files" or
1969         dodie "could not start bisect";
1970
1971     run_command "git bisect good $good" or
1972         dodie "could not set bisect good to $good";
1973
1974     run_git_bisect "git bisect bad $bad" or
1975         dodie "could not set bisect bad to $bad";
1976
1977     if (defined($replay)) {
1978         run_command "git bisect replay $replay" or
1979             dodie "failed to run replay";
1980     }
1981
1982     if (defined($start)) {
1983         run_command "git checkout $start" or
1984             dodie "failed to checkout $start";
1985     }
1986
1987     my $test;
1988     do {
1989         $result = run_bisect $type;
1990         $test = run_git_bisect "git bisect $result";
1991     } while ($test);
1992
1993     run_command "git bisect log" or
1994         dodie "could not capture git bisect log";
1995
1996     run_command "git bisect reset" or
1997         dodie "could not reset git bisect";
1998
1999     doprint "Bad commit was [$bisect_bad]\n";
2000
2001     success $i;
2002 }
2003
2004 my %config_ignore;
2005 my %config_set;
2006
2007 my %config_list;
2008 my %null_config;
2009
2010 my %dependency;
2011
2012 sub assign_configs {
2013     my ($hash, $config) = @_;
2014
2015     open (IN, $config)
2016         or dodie "Failed to read $config";
2017
2018     while (<IN>) {
2019         if (/^((CONFIG\S*)=.*)/) {
2020             ${$hash}{$2} = $1;
2021         }
2022     }
2023
2024     close(IN);
2025 }
2026
2027 sub process_config_ignore {
2028     my ($config) = @_;
2029
2030     assign_configs \%config_ignore, $config;
2031 }
2032
2033 sub read_current_config {
2034     my ($config_ref) = @_;
2035
2036     %{$config_ref} = ();
2037     undef %{$config_ref};
2038
2039     my @key = keys %{$config_ref};
2040     if ($#key >= 0) {
2041         print "did not delete!\n";
2042         exit;
2043     }
2044     open (IN, "$output_config");
2045
2046     while (<IN>) {
2047         if (/^(CONFIG\S+)=(.*)/) {
2048             ${$config_ref}{$1} = $2;
2049         }
2050     }
2051     close(IN);
2052 }
2053
2054 sub get_dependencies {
2055     my ($config) = @_;
2056
2057     my $arr = $dependency{$config};
2058     if (!defined($arr)) {
2059         return ();
2060     }
2061
2062     my @deps = @{$arr};
2063
2064     foreach my $dep (@{$arr}) {
2065         print "ADD DEP $dep\n";
2066         @deps = (@deps, get_dependencies $dep);
2067     }
2068
2069     return @deps;
2070 }
2071
2072 sub create_config {
2073     my @configs = @_;
2074
2075     open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2076
2077     foreach my $config (@configs) {
2078         print OUT "$config_set{$config}\n";
2079         my @deps = get_dependencies $config;
2080         foreach my $dep (@deps) {
2081             print OUT "$config_set{$dep}\n";
2082         }
2083     }
2084
2085     foreach my $config (keys %config_ignore) {
2086         print OUT "$config_ignore{$config}\n";
2087     }
2088     close(OUT);
2089
2090 #    exit;
2091     make_oldconfig;
2092 }
2093
2094 sub compare_configs {
2095     my (%a, %b) = @_;
2096
2097     foreach my $item (keys %a) {
2098         if (!defined($b{$item})) {
2099             print "diff $item\n";
2100             return 1;
2101         }
2102         delete $b{$item};
2103     }
2104
2105     my @keys = keys %b;
2106     if ($#keys) {
2107         print "diff2 $keys[0]\n";
2108     }
2109     return -1 if ($#keys >= 0);
2110
2111     return 0;
2112 }
2113
2114 sub run_config_bisect_test {
2115     my ($type) = @_;
2116
2117     return run_bisect_test $type, "oldconfig";
2118 }
2119
2120 sub process_passed {
2121     my (%configs) = @_;
2122
2123     doprint "These configs had no failure: (Enabling them for further compiles)\n";
2124     # Passed! All these configs are part of a good compile.
2125     # Add them to the min options.
2126     foreach my $config (keys %configs) {
2127         if (defined($config_list{$config})) {
2128             doprint " removing $config\n";
2129             $config_ignore{$config} = $config_list{$config};
2130             delete $config_list{$config};
2131         }
2132     }
2133     doprint "config copied to $outputdir/config_good\n";
2134     run_command "cp -f $output_config $outputdir/config_good";
2135 }
2136
2137 sub process_failed {
2138     my ($config) = @_;
2139
2140     doprint "\n\n***************************************\n";
2141     doprint "Found bad config: $config\n";
2142     doprint "***************************************\n\n";
2143 }
2144
2145 sub run_config_bisect {
2146
2147     my @start_list = keys %config_list;
2148
2149     if ($#start_list < 0) {
2150         doprint "No more configs to test!!!\n";
2151         return -1;
2152     }
2153
2154     doprint "***** RUN TEST ***\n";
2155     my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2156     my $ret;
2157     my %current_config;
2158
2159     my $count = $#start_list + 1;
2160     doprint "  $count configs to test\n";
2161
2162     my $half = int($#start_list / 2);
2163
2164     do {
2165         my @tophalf = @start_list[0 .. $half];
2166
2167         create_config @tophalf;
2168         read_current_config \%current_config;
2169
2170         $count = $#tophalf + 1;
2171         doprint "Testing $count configs\n";
2172         my $found = 0;
2173         # make sure we test something
2174         foreach my $config (@tophalf) {
2175             if (defined($current_config{$config})) {
2176                 logit " $config\n";
2177                 $found = 1;
2178             }
2179         }
2180         if (!$found) {
2181             # try the other half
2182             doprint "Top half produced no set configs, trying bottom half\n";
2183             @tophalf = @start_list[$half + 1 .. $#start_list];
2184             create_config @tophalf;
2185             read_current_config \%current_config;
2186             foreach my $config (@tophalf) {
2187                 if (defined($current_config{$config})) {
2188                     logit " $config\n";
2189                     $found = 1;
2190                 }
2191             }
2192             if (!$found) {
2193                 doprint "Failed: Can't make new config with current configs\n";
2194                 foreach my $config (@start_list) {
2195                     doprint "  CONFIG: $config\n";
2196                 }
2197                 return -1;
2198             }
2199             $count = $#tophalf + 1;
2200             doprint "Testing $count configs\n";
2201         }
2202
2203         $ret = run_config_bisect_test $type;
2204         if ($bisect_manual) {
2205             $ret = answer_bisect;
2206         }
2207         if ($ret) {
2208             process_passed %current_config;
2209             return 0;
2210         }
2211
2212         doprint "This config had a failure.\n";
2213         doprint "Removing these configs that were not set in this config:\n";
2214         doprint "config copied to $outputdir/config_bad\n";
2215         run_command "cp -f $output_config $outputdir/config_bad";
2216
2217         # A config exists in this group that was bad.
2218         foreach my $config (keys %config_list) {
2219             if (!defined($current_config{$config})) {
2220                 doprint " removing $config\n";
2221                 delete $config_list{$config};
2222             }
2223         }
2224
2225         @start_list = @tophalf;
2226
2227         if ($#start_list == 0) {
2228             process_failed $start_list[0];
2229             return 1;
2230         }
2231
2232         # remove half the configs we are looking at and see if
2233         # they are good.
2234         $half = int($#start_list / 2);
2235     } while ($#start_list > 0);
2236
2237     # we found a single config, try it again unless we are running manually
2238
2239     if ($bisect_manual) {
2240         process_failed $start_list[0];
2241         return 1;
2242     }
2243
2244     my @tophalf = @start_list[0 .. 0];
2245
2246     $ret = run_config_bisect_test $type;
2247     if ($ret) {
2248         process_passed %current_config;
2249         return 0;
2250     }
2251
2252     process_failed $start_list[0];
2253     return 1;
2254 }
2255
2256 sub config_bisect {
2257     my ($i) = @_;
2258
2259     my $start_config = $opt{"CONFIG_BISECT[$i]"};
2260
2261     my $tmpconfig = "$tmpdir/use_config";
2262
2263     if (defined($config_bisect_good)) {
2264         process_config_ignore $config_bisect_good;
2265     }
2266
2267     # Make the file with the bad config and the min config
2268     if (defined($minconfig)) {
2269         # read the min config for things to ignore
2270         run_command "cp $minconfig $tmpconfig" or
2271             dodie "failed to copy $minconfig to $tmpconfig";
2272     } else {
2273         unlink $tmpconfig;
2274     }
2275
2276     if (-f $tmpconfig) {
2277         load_force_config($tmpconfig);
2278         process_config_ignore $tmpconfig;
2279     }
2280
2281     # now process the start config
2282     run_command "cp $start_config $output_config" or
2283         dodie "failed to copy $start_config to $output_config";
2284
2285     # read directly what we want to check
2286     my %config_check;
2287     open (IN, $output_config)
2288         or dodie "faied to open $output_config";
2289
2290     while (<IN>) {
2291         if (/^((CONFIG\S*)=.*)/) {
2292             $config_check{$2} = $1;
2293         }
2294     }
2295     close(IN);
2296
2297     # Now run oldconfig with the minconfig
2298     make_oldconfig;
2299
2300     # check to see what we lost (or gained)
2301     open (IN, $output_config)
2302         or dodie "Failed to read $start_config";
2303
2304     my %removed_configs;
2305     my %added_configs;
2306
2307     while (<IN>) {
2308         if (/^((CONFIG\S*)=.*)/) {
2309             # save off all options
2310             $config_set{$2} = $1;
2311             if (defined($config_check{$2})) {
2312                 if (defined($config_ignore{$2})) {
2313                     $removed_configs{$2} = $1;
2314                 } else {
2315                     $config_list{$2} = $1;
2316                 }
2317             } elsif (!defined($config_ignore{$2})) {
2318                 $added_configs{$2} = $1;
2319                 $config_list{$2} = $1;
2320             }
2321         }
2322     }
2323     close(IN);
2324
2325     my @confs = keys %removed_configs;
2326     if ($#confs >= 0) {
2327         doprint "Configs overridden by default configs and removed from check:\n";
2328         foreach my $config (@confs) {
2329             doprint " $config\n";
2330         }
2331     }
2332     @confs = keys %added_configs;
2333     if ($#confs >= 0) {
2334         doprint "Configs appearing in make oldconfig and added:\n";
2335         foreach my $config (@confs) {
2336             doprint " $config\n";
2337         }
2338     }
2339
2340     my %config_test;
2341     my $once = 0;
2342
2343     # Sometimes kconfig does weird things. We must make sure
2344     # that the config we autocreate has everything we need
2345     # to test, otherwise we may miss testing configs, or
2346     # may not be able to create a new config.
2347     # Here we create a config with everything set.
2348     create_config (keys %config_list);
2349     read_current_config \%config_test;
2350     foreach my $config (keys %config_list) {
2351         if (!defined($config_test{$config})) {
2352             if (!$once) {
2353                 $once = 1;
2354                 doprint "Configs not produced by kconfig (will not be checked):\n";
2355             }
2356             doprint "  $config\n";
2357             delete $config_list{$config};
2358         }
2359     }
2360     my $ret;
2361     do {
2362         $ret = run_config_bisect;
2363     } while (!$ret);
2364
2365     return $ret if ($ret < 0);
2366
2367     success $i;
2368 }
2369
2370 sub patchcheck_reboot {
2371     doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
2372     reboot $patchcheck_sleep_time;
2373 }
2374
2375 sub patchcheck {
2376     my ($i) = @_;
2377
2378     die "PATCHCHECK_START[$i] not defined\n"
2379         if (!defined($opt{"PATCHCHECK_START[$i]"}));
2380     die "PATCHCHECK_TYPE[$i] not defined\n"
2381         if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2382
2383     my $start = $opt{"PATCHCHECK_START[$i]"};
2384
2385     my $end = "HEAD";
2386     if (defined($opt{"PATCHCHECK_END[$i]"})) {
2387         $end = $opt{"PATCHCHECK_END[$i]"};
2388     }
2389
2390     # Get the true sha1's since we can use things like HEAD~3
2391     $start = get_sha1($start);
2392     $end = get_sha1($end);
2393
2394     my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2395
2396     # Can't have a test without having a test to run
2397     if ($type eq "test" && !defined($run_test)) {
2398         $type = "boot";
2399     }
2400
2401     open (IN, "git log --pretty=oneline $end|") or
2402         dodie "could not get git list";
2403
2404     my @list;
2405
2406     while (<IN>) {
2407         chomp;
2408         $list[$#list+1] = $_;
2409         last if (/^$start/);
2410     }
2411     close(IN);
2412
2413     if ($list[$#list] !~ /^$start/) {
2414         fail "SHA1 $start not found";
2415     }
2416
2417     # go backwards in the list
2418     @list = reverse @list;
2419
2420     my $save_clean = $noclean;
2421     my %ignored_warnings;
2422
2423     if (defined($ignore_warnings)) {
2424         foreach my $sha1 (split /\s+/, $ignore_warnings) {
2425             $ignored_warnings{$sha1} = 1;
2426         }
2427     }
2428
2429     $in_patchcheck = 1;
2430     foreach my $item (@list) {
2431         my $sha1 = $item;
2432         $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2433
2434         doprint "\nProcessing commit $item\n\n";
2435
2436         run_command "git checkout $sha1" or
2437             die "Failed to checkout $sha1";
2438
2439         # only clean on the first and last patch
2440         if ($item eq $list[0] ||
2441             $item eq $list[$#list]) {
2442             $noclean = $save_clean;
2443         } else {
2444             $noclean = 1;
2445         }
2446
2447         if (defined($minconfig)) {
2448             build "useconfig:$minconfig" or return 0;
2449         } else {
2450             # ?? no config to use?
2451             build "oldconfig" or return 0;
2452         }
2453
2454
2455         if (!defined($ignored_warnings{$sha1})) {
2456             check_buildlog $sha1 or return 0;
2457         }
2458
2459         next if ($type eq "build");
2460
2461         my $failed = 0;
2462
2463         start_monitor_and_boot or $failed = 1;
2464
2465         if (!$failed && $type ne "boot"){
2466             do_run_test or $failed = 1;
2467         }
2468         end_monitor;
2469         return 0 if ($failed);
2470
2471         patchcheck_reboot;
2472
2473     }
2474     $in_patchcheck = 0;
2475     success $i;
2476
2477     return 1;
2478 }
2479
2480 my %depends;
2481 my %depcount;
2482 my $iflevel = 0;
2483 my @ifdeps;
2484
2485 # prevent recursion
2486 my %read_kconfigs;
2487
2488 sub add_dep {
2489     # $config depends on $dep
2490     my ($config, $dep) = @_;
2491
2492     if (defined($depends{$config})) {
2493         $depends{$config} .= " " . $dep;
2494     } else {
2495         $depends{$config} = $dep;
2496     }
2497
2498     # record the number of configs depending on $dep
2499     if (defined $depcount{$dep}) {
2500         $depcount{$dep}++;
2501     } else {
2502         $depcount{$dep} = 1;
2503     } 
2504 }
2505
2506 # taken from streamline_config.pl
2507 sub read_kconfig {
2508     my ($kconfig) = @_;
2509
2510     my $state = "NONE";
2511     my $config;
2512     my @kconfigs;
2513
2514     my $cont = 0;
2515     my $line;
2516
2517
2518     if (! -f $kconfig) {
2519         doprint "file $kconfig does not exist, skipping\n";
2520         return;
2521     }
2522
2523     open(KIN, "$kconfig")
2524         or die "Can't open $kconfig";
2525     while (<KIN>) {
2526         chomp;
2527
2528         # Make sure that lines ending with \ continue
2529         if ($cont) {
2530             $_ = $line . " " . $_;
2531         }
2532
2533         if (s/\\$//) {
2534             $cont = 1;
2535             $line = $_;
2536             next;
2537         }
2538
2539         $cont = 0;
2540
2541         # collect any Kconfig sources
2542         if (/^source\s*"(.*)"/) {
2543             $kconfigs[$#kconfigs+1] = $1;
2544         }
2545
2546         # configs found
2547         if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2548             $state = "NEW";
2549             $config = $2;
2550
2551             for (my $i = 0; $i < $iflevel; $i++) {
2552                 add_dep $config, $ifdeps[$i];
2553             }
2554
2555         # collect the depends for the config
2556         } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2557
2558             add_dep $config, $1;
2559
2560         # Get the configs that select this config
2561         } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2562
2563             # selected by depends on config
2564             add_dep $1, $config;
2565
2566         # Check for if statements
2567         } elsif (/^if\s+(.*\S)\s*$/) {
2568             my $deps = $1;
2569             # remove beginning and ending non text
2570             $deps =~ s/^[^a-zA-Z0-9_]*//;
2571             $deps =~ s/[^a-zA-Z0-9_]*$//;
2572
2573             my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2574
2575             $ifdeps[$iflevel++] = join ':', @deps;
2576
2577         } elsif (/^endif/) {
2578
2579             $iflevel-- if ($iflevel);
2580
2581         # stop on "help"
2582         } elsif (/^\s*help\s*$/) {
2583             $state = "NONE";
2584         }
2585     }
2586     close(KIN);
2587
2588     # read in any configs that were found.
2589     foreach $kconfig (@kconfigs) {
2590         if (!defined($read_kconfigs{$kconfig})) {
2591             $read_kconfigs{$kconfig} = 1;
2592             read_kconfig("$builddir/$kconfig");
2593         }
2594     }
2595 }
2596
2597 sub read_depends {
2598     # find out which arch this is by the kconfig file
2599     open (IN, $output_config)
2600         or dodie "Failed to read $output_config";
2601     my $arch;
2602     while (<IN>) {
2603         if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2604             $arch = $1;
2605             last;
2606         }
2607     }
2608     close IN;
2609
2610     if (!defined($arch)) {
2611         doprint "Could not find arch from config file\n";
2612         doprint "no dependencies used\n";
2613         return;
2614     }
2615
2616     # arch is really the subarch, we need to know
2617     # what directory to look at.
2618     if ($arch eq "i386" || $arch eq "x86_64") {
2619         $arch = "x86";
2620     } elsif ($arch =~ /^tile/) {
2621         $arch = "tile";
2622     }
2623
2624     my $kconfig = "$builddir/arch/$arch/Kconfig";
2625
2626     if (! -f $kconfig && $arch =~ /\d$/) {
2627         my $orig = $arch;
2628         # some subarchs have numbers, truncate them
2629         $arch =~ s/\d*$//;
2630         $kconfig = "$builddir/arch/$arch/Kconfig";
2631         if (! -f $kconfig) {
2632             doprint "No idea what arch dir $orig is for\n";
2633             doprint "no dependencies used\n";
2634             return;
2635         }
2636     }
2637
2638     read_kconfig($kconfig);
2639 }
2640
2641 sub read_config_list {
2642     my ($config) = @_;
2643
2644     open (IN, $config)
2645         or dodie "Failed to read $config";
2646
2647     while (<IN>) {
2648         if (/^((CONFIG\S*)=.*)/) {
2649             if (!defined($config_ignore{$2})) {
2650                 $config_list{$2} = $1;
2651             }
2652         }
2653     }
2654
2655     close(IN);
2656 }
2657
2658 sub read_output_config {
2659     my ($config) = @_;
2660
2661     assign_configs \%config_ignore, $config;
2662 }
2663
2664 sub make_new_config {
2665     my @configs = @_;
2666
2667     open (OUT, ">$output_config")
2668         or dodie "Failed to write $output_config";
2669
2670     foreach my $config (@configs) {
2671         print OUT "$config\n";
2672     }
2673     close OUT;
2674 }
2675
2676 sub chomp_config {
2677     my ($config) = @_;
2678
2679     $config =~ s/CONFIG_//;
2680
2681     return $config;
2682 }
2683
2684 sub get_depends {
2685     my ($dep) = @_;
2686
2687     my $kconfig = chomp_config $dep;
2688
2689     $dep = $depends{"$kconfig"};
2690
2691     # the dep string we have saves the dependencies as they
2692     # were found, including expressions like ! && ||. We
2693     # want to split this out into just an array of configs.
2694
2695     my $valid = "A-Za-z_0-9";
2696
2697     my @configs;
2698
2699     while ($dep =~ /[$valid]/) {
2700
2701         if ($dep =~ /^[^$valid]*([$valid]+)/) {
2702             my $conf = "CONFIG_" . $1;
2703
2704             $configs[$#configs + 1] = $conf;
2705
2706             $dep =~ s/^[^$valid]*[$valid]+//;
2707         } else {
2708             die "this should never happen";
2709         }
2710     }
2711
2712     return @configs;
2713 }
2714
2715 my %min_configs;
2716 my %keep_configs;
2717 my %save_configs;
2718 my %processed_configs;
2719 my %nochange_config;
2720
2721 sub test_this_config {
2722     my ($config) = @_;
2723
2724     my $found;
2725
2726     # if we already processed this config, skip it
2727     if (defined($processed_configs{$config})) {
2728         return undef;
2729     }
2730     $processed_configs{$config} = 1;
2731
2732     # if this config failed during this round, skip it
2733     if (defined($nochange_config{$config})) {
2734         return undef;
2735     }
2736
2737     my $kconfig = chomp_config $config;
2738
2739     # Test dependencies first
2740     if (defined($depends{"$kconfig"})) {
2741         my @parents = get_depends $config;
2742         foreach my $parent (@parents) {
2743             # if the parent is in the min config, check it first
2744             next if (!defined($min_configs{$parent}));
2745             $found = test_this_config($parent);
2746             if (defined($found)) {
2747                 return $found;
2748             }
2749         }
2750     }
2751
2752     # Remove this config from the list of configs
2753     # do a make oldnoconfig and then read the resulting
2754     # .config to make sure it is missing the config that
2755     # we had before
2756     my %configs = %min_configs;
2757     delete $configs{$config};
2758     make_new_config ((values %configs), (values %keep_configs));
2759     make_oldconfig;
2760     undef %configs;
2761     assign_configs \%configs, $output_config;
2762
2763     return $config if (!defined($configs{$config}));
2764
2765     doprint "disabling config $config did not change .config\n";
2766
2767     $nochange_config{$config} = 1;
2768
2769     return undef;
2770 }
2771
2772 sub make_min_config {
2773     my ($i) = @_;
2774
2775     if (!defined($output_minconfig)) {
2776         fail "OUTPUT_MIN_CONFIG not defined" and return;
2777     }
2778
2779     # If output_minconfig exists, and the start_minconfig
2780     # came from min_config, than ask if we should use
2781     # that instead.
2782     if (-f $output_minconfig && !$start_minconfig_defined) {
2783         print "$output_minconfig exists\n";
2784         if (read_yn " Use it as minconfig?") {
2785             $start_minconfig = $output_minconfig;
2786         }
2787     }
2788
2789     if (!defined($start_minconfig)) {
2790         fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2791     }
2792
2793     my $temp_config = "$tmpdir/temp_config";
2794
2795     # First things first. We build an allnoconfig to find
2796     # out what the defaults are that we can't touch.
2797     # Some are selections, but we really can't handle selections.
2798
2799     my $save_minconfig = $minconfig;
2800     undef $minconfig;
2801
2802     run_command "$make allnoconfig" or return 0;
2803
2804     read_depends;
2805
2806     process_config_ignore $output_config;
2807
2808     undef %save_configs;
2809     undef %min_configs;
2810
2811     if (defined($ignore_config)) {
2812         # make sure the file exists
2813         `touch $ignore_config`;
2814         assign_configs \%save_configs, $ignore_config;
2815     }
2816
2817     %keep_configs = %save_configs;
2818
2819     doprint "Load initial configs from $start_minconfig\n";
2820
2821     # Look at the current min configs, and save off all the
2822     # ones that were set via the allnoconfig
2823     assign_configs \%min_configs, $start_minconfig;
2824
2825     my @config_keys = keys %min_configs;
2826
2827     # All configs need a depcount
2828     foreach my $config (@config_keys) {
2829         my $kconfig = chomp_config $config;
2830         if (!defined $depcount{$kconfig}) {
2831                 $depcount{$kconfig} = 0;
2832         }
2833     }
2834
2835     # Remove anything that was set by the make allnoconfig
2836     # we shouldn't need them as they get set for us anyway.
2837     foreach my $config (@config_keys) {
2838         # Remove anything in the ignore_config
2839         if (defined($keep_configs{$config})) {
2840             my $file = $ignore_config;
2841             $file =~ s,.*/(.*?)$,$1,;
2842             doprint "$config set by $file ... ignored\n";
2843             delete $min_configs{$config};
2844             next;
2845         }
2846         # But make sure the settings are the same. If a min config
2847         # sets a selection, we do not want to get rid of it if
2848         # it is not the same as what we have. Just move it into
2849         # the keep configs.
2850         if (defined($config_ignore{$config})) {
2851             if ($config_ignore{$config} ne $min_configs{$config}) {
2852                 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
2853                 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
2854                 $keep_configs{$config} = $min_configs{$config};
2855             } else {
2856                 doprint "$config set by allnoconfig ... ignored\n";
2857             }
2858             delete $min_configs{$config};
2859         }
2860     }
2861
2862     my $done = 0;
2863     my $take_two = 0;
2864
2865     while (!$done) {
2866
2867         my $config;
2868         my $found;
2869
2870         # Now disable each config one by one and do a make oldconfig
2871         # till we find a config that changes our list.
2872
2873         my @test_configs = keys %min_configs;
2874
2875         # Sort keys by who is most dependent on
2876         @test_configs = sort  { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
2877                           @test_configs ;
2878
2879         # Put configs that did not modify the config at the end.
2880         my $reset = 1;
2881         for (my $i = 0; $i < $#test_configs; $i++) {
2882             if (!defined($nochange_config{$test_configs[0]})) {
2883                 $reset = 0;
2884                 last;
2885             }
2886             # This config didn't change the .config last time.
2887             # Place it at the end
2888             my $config = shift @test_configs;
2889             push @test_configs, $config;
2890         }
2891
2892         # if every test config has failed to modify the .config file
2893         # in the past, then reset and start over.
2894         if ($reset) {
2895             undef %nochange_config;
2896         }
2897
2898         undef %processed_configs;
2899
2900         foreach my $config (@test_configs) {
2901
2902             $found = test_this_config $config;
2903
2904             last if (defined($found));
2905
2906             # oh well, try another config
2907         }
2908
2909         if (!defined($found)) {
2910             # we could have failed due to the nochange_config hash
2911             # reset and try again
2912             if (!$take_two) {
2913                 undef %nochange_config;
2914                 $take_two = 1;
2915                 next;
2916             }
2917             doprint "No more configs found that we can disable\n";
2918             $done = 1;
2919             last;
2920         }
2921         $take_two = 0;
2922
2923         $config = $found;
2924
2925         doprint "Test with $config disabled\n";
2926
2927         # set in_bisect to keep build and monitor from dieing
2928         $in_bisect = 1;
2929
2930         my $failed = 0;
2931         build "oldconfig";
2932         start_monitor_and_boot or $failed = 1;
2933         end_monitor;
2934
2935         $in_bisect = 0;
2936
2937         if ($failed) {
2938             doprint "$min_configs{$config} is needed to boot the box... keeping\n";
2939             # this config is needed, add it to the ignore list.
2940             $keep_configs{$config} = $min_configs{$config};
2941             $save_configs{$config} = $min_configs{$config};
2942             delete $min_configs{$config};
2943
2944             # update new ignore configs
2945             if (defined($ignore_config)) {
2946                 open (OUT, ">$temp_config")
2947                     or die "Can't write to $temp_config";
2948                 foreach my $config (keys %save_configs) {
2949                     print OUT "$save_configs{$config}\n";
2950                 }
2951                 close OUT;
2952                 run_command "mv $temp_config $ignore_config" or
2953                     dodie "failed to copy update to $ignore_config";
2954             }
2955
2956         } else {
2957             # We booted without this config, remove it from the minconfigs.
2958             doprint "$config is not needed, disabling\n";
2959
2960             delete $min_configs{$config};
2961
2962             # Also disable anything that is not enabled in this config
2963             my %configs;
2964             assign_configs \%configs, $output_config;
2965             my @config_keys = keys %min_configs;
2966             foreach my $config (@config_keys) {
2967                 if (!defined($configs{$config})) {
2968                     doprint "$config is not set, disabling\n";
2969                     delete $min_configs{$config};
2970                 }
2971             }
2972
2973             # Save off all the current mandidory configs
2974             open (OUT, ">$temp_config")
2975                 or die "Can't write to $temp_config";
2976             foreach my $config (keys %keep_configs) {
2977                 print OUT "$keep_configs{$config}\n";
2978             }
2979             foreach my $config (keys %min_configs) {
2980                 print OUT "$min_configs{$config}\n";
2981             }
2982             close OUT;
2983
2984             run_command "mv $temp_config $output_minconfig" or
2985                 dodie "failed to copy update to $output_minconfig";
2986         }
2987
2988         doprint "Reboot and wait $sleep_time seconds\n";
2989         reboot $sleep_time;
2990     }
2991
2992     success $i;
2993     return 1;
2994 }
2995
2996 $#ARGV < 1 or die "ktest.pl version: $VERSION\n   usage: ktest.pl config-file\n";
2997
2998 if ($#ARGV == 0) {
2999     $ktest_config = $ARGV[0];
3000     if (! -f $ktest_config) {
3001         print "$ktest_config does not exist.\n";
3002         if (!read_yn "Create it?") {
3003             exit 0;
3004         }
3005     }
3006 } else {
3007     $ktest_config = "ktest.conf";
3008 }
3009
3010 if (! -f $ktest_config) {
3011     open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3012     print OUT << "EOF"
3013 # Generated by ktest.pl
3014 #
3015 # Define each test with TEST_START
3016 # The config options below it will override the defaults
3017 TEST_START
3018
3019 DEFAULTS
3020 EOF
3021 ;
3022     close(OUT);
3023 }
3024 read_config $ktest_config;
3025
3026 if (defined($opt{"LOG_FILE"})) {
3027     $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3028 }
3029
3030 # Append any configs entered in manually to the config file.
3031 my @new_configs = keys %entered_configs;
3032 if ($#new_configs >= 0) {
3033     print "\nAppending entered in configs to $ktest_config\n";
3034     open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3035     foreach my $config (@new_configs) {
3036         print OUT "$config = $entered_configs{$config}\n";
3037         $opt{$config} = $entered_configs{$config};
3038     }
3039 }
3040
3041 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3042     unlink $opt{"LOG_FILE"};
3043 }
3044
3045 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3046
3047 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3048
3049     if (!$i) {
3050         doprint "DEFAULT OPTIONS:\n";
3051     } else {
3052         doprint "\nTEST $i OPTIONS";
3053         if (defined($repeat_tests{$i})) {
3054             $repeat = $repeat_tests{$i};
3055             doprint " ITERATE $repeat";
3056         }
3057         doprint "\n";
3058     }
3059
3060     foreach my $option (sort keys %opt) {
3061
3062         if ($option =~ /\[(\d+)\]$/) {
3063             next if ($i != $1);
3064         } else {
3065             next if ($i);
3066         }
3067
3068         doprint "$option = $opt{$option}\n";
3069     }
3070 }
3071
3072 sub __set_test_option {
3073     my ($name, $i) = @_;
3074
3075     my $option = "$name\[$i\]";
3076
3077     if (defined($opt{$option})) {
3078         return $opt{$option};
3079     }
3080
3081     foreach my $test (keys %repeat_tests) {
3082         if ($i >= $test &&
3083             $i < $test + $repeat_tests{$test}) {
3084             $option = "$name\[$test\]";
3085             if (defined($opt{$option})) {
3086                 return $opt{$option};
3087             }
3088         }
3089     }
3090
3091     if (defined($opt{$name})) {
3092         return $opt{$name};
3093     }
3094
3095     return undef;
3096 }
3097
3098 sub set_test_option {
3099     my ($name, $i) = @_;
3100
3101     my $option = __set_test_option($name, $i);
3102     return $option if (!defined($option));
3103
3104     return eval_option($option, $i);
3105 }
3106
3107 # First we need to do is the builds
3108 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
3109
3110     # Do not reboot on failing test options
3111     $no_reboot = 1;
3112
3113     $iteration = $i;
3114
3115     my $makecmd = set_test_option("MAKE_CMD", $i);
3116
3117     $machine = set_test_option("MACHINE", $i);
3118     $ssh_user = set_test_option("SSH_USER", $i);
3119     $tmpdir = set_test_option("TMP_DIR", $i);
3120     $outputdir = set_test_option("OUTPUT_DIR", $i);
3121     $builddir = set_test_option("BUILD_DIR", $i);
3122     $test_type = set_test_option("TEST_TYPE", $i);
3123     $build_type = set_test_option("BUILD_TYPE", $i);
3124     $build_options = set_test_option("BUILD_OPTIONS", $i);
3125     $pre_build = set_test_option("PRE_BUILD", $i);
3126     $post_build = set_test_option("POST_BUILD", $i);
3127     $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3128     $post_build_die = set_test_option("POST_BUILD_DIE", $i);
3129     $power_cycle = set_test_option("POWER_CYCLE", $i);
3130     $reboot = set_test_option("REBOOT", $i);
3131     $noclean = set_test_option("BUILD_NOCLEAN", $i);
3132     $minconfig = set_test_option("MIN_CONFIG", $i);
3133     $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3134     $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3135     $ignore_config = set_test_option("IGNORE_CONFIG", $i);
3136     $run_test = set_test_option("TEST", $i);
3137     $addconfig = set_test_option("ADD_CONFIG", $i);
3138     $reboot_type = set_test_option("REBOOT_TYPE", $i);
3139     $grub_menu = set_test_option("GRUB_MENU", $i);
3140     $post_install = set_test_option("POST_INSTALL", $i);
3141     $no_install = set_test_option("NO_INSTALL", $i);
3142     $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3143     $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
3144     $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3145     $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3146     $power_off = set_test_option("POWER_OFF", $i);
3147     $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3148     $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
3149     $sleep_time = set_test_option("SLEEP_TIME", $i);
3150     $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
3151     $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
3152     $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
3153     $bisect_manual = set_test_option("BISECT_MANUAL", $i);
3154     $bisect_skip = set_test_option("BISECT_SKIP", $i);
3155     $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
3156     $store_failures = set_test_option("STORE_FAILURES", $i);
3157     $store_successes = set_test_option("STORE_SUCCESSES", $i);
3158     $test_name = set_test_option("TEST_NAME", $i);
3159     $timeout = set_test_option("TIMEOUT", $i);
3160     $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3161     $console = set_test_option("CONSOLE", $i);
3162     $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
3163     $success_line = set_test_option("SUCCESS_LINE", $i);
3164     $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
3165     $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3166     $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
3167     $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
3168     $build_target = set_test_option("BUILD_TARGET", $i);
3169     $ssh_exec = set_test_option("SSH_EXEC", $i);
3170     $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
3171     $target_image = set_test_option("TARGET_IMAGE", $i);
3172     $localversion = set_test_option("LOCALVERSION", $i);
3173
3174     $start_minconfig_defined = 1;
3175
3176     if (!defined($start_minconfig)) {
3177         $start_minconfig_defined = 0;
3178         $start_minconfig = $minconfig;
3179     }
3180
3181     chdir $builddir || die "can't change directory to $builddir";
3182
3183     foreach my $dir ($tmpdir, $outputdir) {
3184         if (!-d $dir) {
3185             mkpath($dir) or
3186                 die "can't create $dir";
3187         }
3188     }
3189
3190     $ENV{"SSH_USER"} = $ssh_user;
3191     $ENV{"MACHINE"} = $machine;
3192
3193     $target = "$ssh_user\@$machine";
3194
3195     $buildlog = "$tmpdir/buildlog-$machine";
3196     $testlog = "$tmpdir/testlog-$machine";
3197     $dmesg = "$tmpdir/dmesg-$machine";
3198     $make = "$makecmd O=$outputdir";
3199     $output_config = "$outputdir/.config";
3200
3201     if ($reboot_type eq "grub") {
3202         dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3203     } elsif (!defined($reboot_script)) {
3204         dodie "REBOOT_SCRIPT not defined"
3205     }
3206
3207     my $run_type = $build_type;
3208     if ($test_type eq "patchcheck") {
3209         $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3210     } elsif ($test_type eq "bisect") {
3211         $run_type = $opt{"BISECT_TYPE[$i]"};
3212     } elsif ($test_type eq "config_bisect") {
3213         $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
3214     }
3215
3216     if ($test_type eq "make_min_config") {
3217         $run_type = "";
3218     }
3219
3220     # mistake in config file?
3221     if (!defined($run_type)) {
3222         $run_type = "ERROR";
3223     }
3224
3225     my $installme = "";
3226     $installme = " no_install" if ($no_install);
3227
3228     doprint "\n\n";
3229     doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
3230
3231     unlink $dmesg;
3232     unlink $buildlog;
3233     unlink $testlog;
3234
3235     if (defined($addconfig)) {
3236         my $min = $minconfig;
3237         if (!defined($minconfig)) {
3238             $min = "";
3239         }
3240         run_command "cat $addconfig $min > $tmpdir/add_config" or
3241             dodie "Failed to create temp config";
3242         $minconfig = "$tmpdir/add_config";
3243     }
3244
3245     my $checkout = $opt{"CHECKOUT[$i]"};
3246     if (defined($checkout)) {
3247         run_command "git checkout $checkout" or
3248             die "failed to checkout $checkout";
3249     }
3250
3251     $no_reboot = 0;
3252
3253
3254     if ($test_type eq "bisect") {
3255         bisect $i;
3256         next;
3257     } elsif ($test_type eq "config_bisect") {
3258         config_bisect $i;
3259         next;
3260     } elsif ($test_type eq "patchcheck") {
3261         patchcheck $i;
3262         next;
3263     } elsif ($test_type eq "make_min_config") {
3264         make_min_config $i;
3265         next;
3266     }
3267
3268     if ($build_type ne "nobuild") {
3269         build $build_type or next;
3270     }
3271
3272     if ($test_type eq "install") {
3273         get_version;
3274         install;
3275         success $i;
3276         next;
3277     }
3278
3279     if ($test_type ne "build") {
3280         my $failed = 0;
3281         start_monitor_and_boot or $failed = 1;
3282
3283         if (!$failed && $test_type ne "boot" && defined($run_test)) {
3284             do_run_test or $failed = 1;
3285         }
3286         end_monitor;
3287         next if ($failed);
3288     }
3289
3290     success $i;
3291 }
3292
3293 if ($opt{"POWEROFF_ON_SUCCESS"}) {
3294     halt;
3295 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
3296     reboot;
3297 }
3298
3299 doprint "\n    $successes of $opt{NUM_TESTS} tests were successful\n\n";
3300
3301 exit 0;