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