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