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