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