]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/testing/ktest/ktest.pl
1dae000f79f87ef36ffd507b851ba4e2dc4df0af
[karo-tx-linux.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 %evals;
22
23 #default opts
24 my %default = (
25     "NUM_TESTS"                 => 1,
26     "TEST_TYPE"                 => "build",
27     "BUILD_TYPE"                => "randconfig",
28     "MAKE_CMD"                  => "make",
29     "CLOSE_CONSOLE_SIGNAL"      => "INT",
30     "TIMEOUT"                   => 120,
31     "TMP_DIR"                   => "/tmp/ktest/\${MACHINE}",
32     "SLEEP_TIME"                => 60,  # sleep time between tests
33     "BUILD_NOCLEAN"             => 0,
34     "REBOOT_ON_ERROR"           => 0,
35     "POWEROFF_ON_ERROR"         => 0,
36     "REBOOT_ON_SUCCESS"         => 1,
37     "POWEROFF_ON_SUCCESS"       => 0,
38     "BUILD_OPTIONS"             => "",
39     "BISECT_SLEEP_TIME"         => 60,   # sleep time between bisects
40     "PATCHCHECK_SLEEP_TIME"     => 60, # sleep time between patch checks
41     "CLEAR_LOG"                 => 0,
42     "BISECT_MANUAL"             => 0,
43     "BISECT_SKIP"               => 1,
44     "BISECT_TRIES"              => 1,
45     "MIN_CONFIG_TYPE"           => "boot",
46     "SUCCESS_LINE"              => "login:",
47     "DETECT_TRIPLE_FAULT"       => 1,
48     "NO_INSTALL"                => 0,
49     "BOOTED_TIMEOUT"            => 1,
50     "DIE_ON_FAILURE"            => 1,
51     "SSH_EXEC"                  => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
52     "SCP_TO_TARGET"             => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
53     "SCP_TO_TARGET_INSTALL"     => "\${SCP_TO_TARGET}",
54     "REBOOT"                    => "ssh \$SSH_USER\@\$MACHINE reboot",
55     "STOP_AFTER_SUCCESS"        => 10,
56     "STOP_AFTER_FAILURE"        => 60,
57     "STOP_TEST_AFTER"           => 600,
58     "MAX_MONITOR_WAIT"          => 1800,
59     "GRUB_REBOOT"               => "grub2-reboot",
60     "SYSLINUX"                  => "extlinux",
61     "SYSLINUX_PATH"             => "/boot/extlinux",
62
63 # required, and we will ask users if they don't have them but we keep the default
64 # value something that is common.
65     "REBOOT_TYPE"               => "grub",
66     "LOCALVERSION"              => "-test",
67     "SSH_USER"                  => "root",
68     "BUILD_TARGET"              => "arch/x86/boot/bzImage",
69     "TARGET_IMAGE"              => "/boot/vmlinuz-test",
70
71     "LOG_FILE"                  => undef,
72     "IGNORE_UNUSED"             => 0,
73 );
74
75 my $ktest_config = "ktest.conf";
76 my $version;
77 my $have_version = 0;
78 my $machine;
79 my $last_machine;
80 my $ssh_user;
81 my $tmpdir;
82 my $builddir;
83 my $outputdir;
84 my $output_config;
85 my $test_type;
86 my $build_type;
87 my $build_options;
88 my $final_post_ktest;
89 my $pre_ktest;
90 my $post_ktest;
91 my $pre_test;
92 my $post_test;
93 my $pre_build;
94 my $post_build;
95 my $pre_build_die;
96 my $post_build_die;
97 my $reboot_type;
98 my $reboot_script;
99 my $power_cycle;
100 my $reboot;
101 my $reboot_on_error;
102 my $switch_to_good;
103 my $switch_to_test;
104 my $poweroff_on_error;
105 my $reboot_on_success;
106 my $die_on_failure;
107 my $powercycle_after_reboot;
108 my $poweroff_after_halt;
109 my $max_monitor_wait;
110 my $ssh_exec;
111 my $scp_to_target;
112 my $scp_to_target_install;
113 my $power_off;
114 my $grub_menu;
115 my $last_grub_menu;
116 my $grub_file;
117 my $grub_number;
118 my $grub_reboot;
119 my $syslinux;
120 my $syslinux_path;
121 my $syslinux_label;
122 my $target;
123 my $make;
124 my $pre_install;
125 my $post_install;
126 my $no_install;
127 my $noclean;
128 my $minconfig;
129 my $start_minconfig;
130 my $start_minconfig_defined;
131 my $output_minconfig;
132 my $minconfig_type;
133 my $use_output_minconfig;
134 my $warnings_file;
135 my $ignore_config;
136 my $ignore_errors;
137 my $addconfig;
138 my $in_bisect = 0;
139 my $bisect_bad_commit = "";
140 my $reverse_bisect;
141 my $bisect_manual;
142 my $bisect_skip;
143 my $bisect_tries;
144 my $config_bisect_good;
145 my $bisect_ret_good;
146 my $bisect_ret_bad;
147 my $bisect_ret_skip;
148 my $bisect_ret_abort;
149 my $bisect_ret_default;
150 my $in_patchcheck = 0;
151 my $run_test;
152 my $buildlog;
153 my $testlog;
154 my $dmesg;
155 my $monitor_fp;
156 my $monitor_pid;
157 my $monitor_cnt = 0;
158 my $sleep_time;
159 my $bisect_sleep_time;
160 my $patchcheck_sleep_time;
161 my $ignore_warnings;
162 my $store_failures;
163 my $store_successes;
164 my $test_name;
165 my $timeout;
166 my $booted_timeout;
167 my $detect_triplefault;
168 my $console;
169 my $close_console_signal;
170 my $reboot_success_line;
171 my $success_line;
172 my $stop_after_success;
173 my $stop_after_failure;
174 my $stop_test_after;
175 my $build_target;
176 my $target_image;
177 my $checkout;
178 my $localversion;
179 my $iteration = 0;
180 my $successes = 0;
181 my $stty;
182
183 my $bisect_good;
184 my $bisect_bad;
185 my $bisect_type;
186 my $bisect_start;
187 my $bisect_replay;
188 my $bisect_files;
189 my $bisect_reverse;
190 my $bisect_check;
191
192 my $config_bisect;
193 my $config_bisect_type;
194 my $config_bisect_check;
195
196 my $patchcheck_type;
197 my $patchcheck_start;
198 my $patchcheck_cherry;
199 my $patchcheck_end;
200
201 # set when a test is something other that just building or install
202 # which would require more options.
203 my $buildonly = 1;
204
205 # tell build not to worry about warnings, even when WARNINGS_FILE is set
206 my $warnings_ok = 0;
207
208 # set when creating a new config
209 my $newconfig = 0;
210
211 my %entered_configs;
212 my %config_help;
213 my %variable;
214
215 # force_config is the list of configs that we force enabled (or disabled)
216 # in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
217 my %force_config;
218
219 # do not force reboots on config problems
220 my $no_reboot = 1;
221
222 # reboot on success
223 my $reboot_success = 0;
224
225 my %option_map = (
226     "MACHINE"                   => \$machine,
227     "SSH_USER"                  => \$ssh_user,
228     "TMP_DIR"                   => \$tmpdir,
229     "OUTPUT_DIR"                => \$outputdir,
230     "BUILD_DIR"                 => \$builddir,
231     "TEST_TYPE"                 => \$test_type,
232     "PRE_KTEST"                 => \$pre_ktest,
233     "POST_KTEST"                => \$post_ktest,
234     "PRE_TEST"                  => \$pre_test,
235     "POST_TEST"                 => \$post_test,
236     "BUILD_TYPE"                => \$build_type,
237     "BUILD_OPTIONS"             => \$build_options,
238     "PRE_BUILD"                 => \$pre_build,
239     "POST_BUILD"                => \$post_build,
240     "PRE_BUILD_DIE"             => \$pre_build_die,
241     "POST_BUILD_DIE"            => \$post_build_die,
242     "POWER_CYCLE"               => \$power_cycle,
243     "REBOOT"                    => \$reboot,
244     "BUILD_NOCLEAN"             => \$noclean,
245     "MIN_CONFIG"                => \$minconfig,
246     "OUTPUT_MIN_CONFIG"         => \$output_minconfig,
247     "START_MIN_CONFIG"          => \$start_minconfig,
248     "MIN_CONFIG_TYPE"           => \$minconfig_type,
249     "USE_OUTPUT_MIN_CONFIG"     => \$use_output_minconfig,
250     "WARNINGS_FILE"             => \$warnings_file,
251     "IGNORE_CONFIG"             => \$ignore_config,
252     "TEST"                      => \$run_test,
253     "ADD_CONFIG"                => \$addconfig,
254     "REBOOT_TYPE"               => \$reboot_type,
255     "GRUB_MENU"                 => \$grub_menu,
256     "GRUB_FILE"                 => \$grub_file,
257     "GRUB_REBOOT"               => \$grub_reboot,
258     "SYSLINUX"                  => \$syslinux,
259     "SYSLINUX_PATH"             => \$syslinux_path,
260     "SYSLINUX_LABEL"            => \$syslinux_label,
261     "PRE_INSTALL"               => \$pre_install,
262     "POST_INSTALL"              => \$post_install,
263     "NO_INSTALL"                => \$no_install,
264     "REBOOT_SCRIPT"             => \$reboot_script,
265     "REBOOT_ON_ERROR"           => \$reboot_on_error,
266     "SWITCH_TO_GOOD"            => \$switch_to_good,
267     "SWITCH_TO_TEST"            => \$switch_to_test,
268     "POWEROFF_ON_ERROR"         => \$poweroff_on_error,
269     "REBOOT_ON_SUCCESS"         => \$reboot_on_success,
270     "DIE_ON_FAILURE"            => \$die_on_failure,
271     "POWER_OFF"                 => \$power_off,
272     "POWERCYCLE_AFTER_REBOOT"   => \$powercycle_after_reboot,
273     "POWEROFF_AFTER_HALT"       => \$poweroff_after_halt,
274     "MAX_MONITOR_WAIT"          => \$max_monitor_wait,
275     "SLEEP_TIME"                => \$sleep_time,
276     "BISECT_SLEEP_TIME"         => \$bisect_sleep_time,
277     "PATCHCHECK_SLEEP_TIME"     => \$patchcheck_sleep_time,
278     "IGNORE_WARNINGS"           => \$ignore_warnings,
279     "IGNORE_ERRORS"             => \$ignore_errors,
280     "BISECT_MANUAL"             => \$bisect_manual,
281     "BISECT_SKIP"               => \$bisect_skip,
282     "BISECT_TRIES"              => \$bisect_tries,
283     "CONFIG_BISECT_GOOD"        => \$config_bisect_good,
284     "BISECT_RET_GOOD"           => \$bisect_ret_good,
285     "BISECT_RET_BAD"            => \$bisect_ret_bad,
286     "BISECT_RET_SKIP"           => \$bisect_ret_skip,
287     "BISECT_RET_ABORT"          => \$bisect_ret_abort,
288     "BISECT_RET_DEFAULT"        => \$bisect_ret_default,
289     "STORE_FAILURES"            => \$store_failures,
290     "STORE_SUCCESSES"           => \$store_successes,
291     "TEST_NAME"                 => \$test_name,
292     "TIMEOUT"                   => \$timeout,
293     "BOOTED_TIMEOUT"            => \$booted_timeout,
294     "CONSOLE"                   => \$console,
295     "CLOSE_CONSOLE_SIGNAL"      => \$close_console_signal,
296     "DETECT_TRIPLE_FAULT"       => \$detect_triplefault,
297     "SUCCESS_LINE"              => \$success_line,
298     "REBOOT_SUCCESS_LINE"       => \$reboot_success_line,
299     "STOP_AFTER_SUCCESS"        => \$stop_after_success,
300     "STOP_AFTER_FAILURE"        => \$stop_after_failure,
301     "STOP_TEST_AFTER"           => \$stop_test_after,
302     "BUILD_TARGET"              => \$build_target,
303     "SSH_EXEC"                  => \$ssh_exec,
304     "SCP_TO_TARGET"             => \$scp_to_target,
305     "SCP_TO_TARGET_INSTALL"     => \$scp_to_target_install,
306     "CHECKOUT"                  => \$checkout,
307     "TARGET_IMAGE"              => \$target_image,
308     "LOCALVERSION"              => \$localversion,
309
310     "BISECT_GOOD"               => \$bisect_good,
311     "BISECT_BAD"                => \$bisect_bad,
312     "BISECT_TYPE"               => \$bisect_type,
313     "BISECT_START"              => \$bisect_start,
314     "BISECT_REPLAY"             => \$bisect_replay,
315     "BISECT_FILES"              => \$bisect_files,
316     "BISECT_REVERSE"            => \$bisect_reverse,
317     "BISECT_CHECK"              => \$bisect_check,
318
319     "CONFIG_BISECT"             => \$config_bisect,
320     "CONFIG_BISECT_TYPE"        => \$config_bisect_type,
321     "CONFIG_BISECT_CHECK"       => \$config_bisect_check,
322
323     "PATCHCHECK_TYPE"           => \$patchcheck_type,
324     "PATCHCHECK_START"          => \$patchcheck_start,
325     "PATCHCHECK_CHERRY"         => \$patchcheck_cherry,
326     "PATCHCHECK_END"            => \$patchcheck_end,
327 );
328
329 # Options may be used by other options, record them.
330 my %used_options;
331
332 # default variables that can be used
333 chomp ($variable{"PWD"} = `pwd`);
334
335 $config_help{"MACHINE"} = << "EOF"
336  The machine hostname that you will test.
337  For build only tests, it is still needed to differentiate log files.
338 EOF
339     ;
340 $config_help{"SSH_USER"} = << "EOF"
341  The box is expected to have ssh on normal bootup, provide the user
342   (most likely root, since you need privileged operations)
343 EOF
344     ;
345 $config_help{"BUILD_DIR"} = << "EOF"
346  The directory that contains the Linux source code (full path).
347  You can use \${PWD} that will be the path where ktest.pl is run, or use
348  \${THIS_DIR} which is assigned \${PWD} but may be changed later.
349 EOF
350     ;
351 $config_help{"OUTPUT_DIR"} = << "EOF"
352  The directory that the objects will be built (full path).
353  (can not be same as BUILD_DIR)
354  You can use \${PWD} that will be the path where ktest.pl is run, or use
355  \${THIS_DIR} which is assigned \${PWD} but may be changed later.
356 EOF
357     ;
358 $config_help{"BUILD_TARGET"} = << "EOF"
359  The location of the compiled file to copy to the target.
360  (relative to OUTPUT_DIR)
361 EOF
362     ;
363 $config_help{"BUILD_OPTIONS"} = << "EOF"
364  Options to add to \"make\" when building.
365  i.e.  -j20
366 EOF
367     ;
368 $config_help{"TARGET_IMAGE"} = << "EOF"
369  The place to put your image on the test machine.
370 EOF
371     ;
372 $config_help{"POWER_CYCLE"} = << "EOF"
373  A script or command to reboot the box.
374
375  Here is a digital loggers power switch example
376  POWER_CYCLE = wget --no-proxy -O /dev/null -q  --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
377
378  Here is an example to reboot a virtual box on the current host
379  with the name "Guest".
380  POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
381 EOF
382     ;
383 $config_help{"CONSOLE"} = << "EOF"
384  The script or command that reads the console
385
386   If you use ttywatch server, something like the following would work.
387 CONSOLE = nc -d localhost 3001
388
389  For a virtual machine with guest name "Guest".
390 CONSOLE =  virsh console Guest
391 EOF
392     ;
393 $config_help{"LOCALVERSION"} = << "EOF"
394  Required version ending to differentiate the test
395  from other linux builds on the system.
396 EOF
397     ;
398 $config_help{"REBOOT_TYPE"} = << "EOF"
399  Way to reboot the box to the test kernel.
400  Only valid options so far are "grub", "grub2", "syslinux", and "script".
401
402  If you specify grub, it will assume grub version 1
403  and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
404  and select that target to reboot to the kernel. If this is not
405  your setup, then specify "script" and have a command or script
406  specified in REBOOT_SCRIPT to boot to the target.
407
408  The entry in /boot/grub/menu.lst must be entered in manually.
409  The test will not modify that file.
410
411  If you specify grub2, then you also need to specify both \$GRUB_MENU
412  and \$GRUB_FILE.
413
414  If you specify syslinux, then you may use SYSLINUX to define the syslinux
415  command (defaults to extlinux), and SYSLINUX_PATH to specify the path to
416  the syslinux install (defaults to /boot/extlinux). But you have to specify
417  SYSLINUX_LABEL to define the label to boot to for the test kernel.
418 EOF
419     ;
420 $config_help{"GRUB_MENU"} = << "EOF"
421  The grub title name for the test kernel to boot
422  (Only mandatory if REBOOT_TYPE = grub or grub2)
423
424  Note, ktest.pl will not update the grub menu.lst, you need to
425  manually add an option for the test. ktest.pl will search
426  the grub menu.lst for this option to find what kernel to
427  reboot into.
428
429  For example, if in the /boot/grub/menu.lst the test kernel title has:
430  title Test Kernel
431  kernel vmlinuz-test
432  GRUB_MENU = Test Kernel
433
434  For grub2, a search of \$GRUB_FILE is performed for the lines
435  that begin with "menuentry". It will not detect submenus. The
436  menu must be a non-nested menu. Add the quotes used in the menu
437  to guarantee your selection, as the first menuentry with the content
438  of \$GRUB_MENU that is found will be used.
439 EOF
440     ;
441 $config_help{"GRUB_FILE"} = << "EOF"
442  If grub2 is used, the full path for the grub.cfg file is placed
443  here. Use something like /boot/grub2/grub.cfg to search.
444 EOF
445     ;
446 $config_help{"SYSLINUX_LABEL"} = << "EOF"
447  If syslinux is used, the label that boots the target kernel must
448  be specified with SYSLINUX_LABEL.
449 EOF
450     ;
451 $config_help{"REBOOT_SCRIPT"} = << "EOF"
452  A script to reboot the target into the test kernel
453  (Only mandatory if REBOOT_TYPE = script)
454 EOF
455     ;
456
457 sub _logit {
458     if (defined($opt{"LOG_FILE"})) {
459         open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
460         print OUT @_;
461         close(OUT);
462     }
463 }
464
465 sub logit {
466     if (defined($opt{"LOG_FILE"})) {
467         _logit @_;
468     } else {
469         print @_;
470     }
471 }
472
473 sub doprint {
474     print @_;
475     _logit @_;
476 }
477
478 sub read_prompt {
479     my ($cancel, $prompt) = @_;
480
481     my $ans;
482
483     for (;;) {
484         if ($cancel) {
485             print "$prompt [y/n/C] ";
486         } else {
487             print "$prompt [Y/n] ";
488         }
489         $ans = <STDIN>;
490         chomp $ans;
491         if ($ans =~ /^\s*$/) {
492             if ($cancel) {
493                 $ans = "c";
494             } else {
495                 $ans = "y";
496             }
497         }
498         last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
499         if ($cancel) {
500             last if ($ans =~ /^c$/i);
501             print "Please answer either 'y', 'n' or 'c'.\n";
502         } else {
503             print "Please answer either 'y' or 'n'.\n";
504         }
505     }
506     if ($ans =~ /^c/i) {
507         exit;
508     }
509     if ($ans !~ /^y$/i) {
510         return 0;
511     }
512     return 1;
513 }
514
515 sub read_yn {
516     my ($prompt) = @_;
517
518     return read_prompt 0, $prompt;
519 }
520
521 sub read_ync {
522     my ($prompt) = @_;
523
524     return read_prompt 1, $prompt;
525 }
526
527 sub get_mandatory_config {
528     my ($config) = @_;
529     my $ans;
530
531     return if (defined($opt{$config}));
532
533     if (defined($config_help{$config})) {
534         print "\n";
535         print $config_help{$config};
536     }
537
538     for (;;) {
539         print "$config = ";
540         if (defined($default{$config}) && length($default{$config})) {
541             print "\[$default{$config}\] ";
542         }
543         $ans = <STDIN>;
544         $ans =~ s/^\s*(.*\S)\s*$/$1/;
545         if ($ans =~ /^\s*$/) {
546             if ($default{$config}) {
547                 $ans = $default{$config};
548             } else {
549                 print "Your answer can not be blank\n";
550                 next;
551             }
552         }
553         $entered_configs{$config} = ${ans};
554         last;
555     }
556 }
557
558 sub get_mandatory_configs {
559     get_mandatory_config("MACHINE");
560     get_mandatory_config("BUILD_DIR");
561     get_mandatory_config("OUTPUT_DIR");
562
563     if ($newconfig) {
564         get_mandatory_config("BUILD_OPTIONS");
565     }
566
567     # options required for other than just building a kernel
568     if (!$buildonly) {
569         get_mandatory_config("POWER_CYCLE");
570         get_mandatory_config("CONSOLE");
571     }
572
573     # options required for install and more
574     if ($buildonly != 1) {
575         get_mandatory_config("SSH_USER");
576         get_mandatory_config("BUILD_TARGET");
577         get_mandatory_config("TARGET_IMAGE");
578     }
579
580     get_mandatory_config("LOCALVERSION");
581
582     return if ($buildonly);
583
584     my $rtype = $opt{"REBOOT_TYPE"};
585
586     if (!defined($rtype)) {
587         if (!defined($opt{"GRUB_MENU"})) {
588             get_mandatory_config("REBOOT_TYPE");
589             $rtype = $entered_configs{"REBOOT_TYPE"};
590         } else {
591             $rtype = "grub";
592         }
593     }
594
595     if ($rtype eq "grub") {
596         get_mandatory_config("GRUB_MENU");
597     }
598
599     if ($rtype eq "grub2") {
600         get_mandatory_config("GRUB_MENU");
601         get_mandatory_config("GRUB_FILE");
602     }
603
604     if ($rtype eq "syslinux") {
605         get_mandatory_config("SYSLINUX_LABEL");
606     }
607 }
608
609 sub process_variables {
610     my ($value, $remove_undef) = @_;
611     my $retval = "";
612
613     # We want to check for '\', and it is just easier
614     # to check the previous characet of '$' and not need
615     # to worry if '$' is the first character. By adding
616     # a space to $value, we can just check [^\\]\$ and
617     # it will still work.
618     $value = " $value";
619
620     while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
621         my $begin = $1;
622         my $var = $2;
623         my $end = $3;
624         # append beginning of value to retval
625         $retval = "$retval$begin";
626         if (defined($variable{$var})) {
627             $retval = "$retval$variable{$var}";
628         } elsif (defined($remove_undef) && $remove_undef) {
629             # for if statements, any variable that is not defined,
630             # we simple convert to 0
631             $retval = "${retval}0";
632         } else {
633             # put back the origin piece.
634             $retval = "$retval\$\{$var\}";
635             # This could be an option that is used later, save
636             # it so we don't warn if this option is not one of
637             # ktests options.
638             $used_options{$var} = 1;
639         }
640         $value = $end;
641     }
642     $retval = "$retval$value";
643
644     # remove the space added in the beginning
645     $retval =~ s/ //;
646
647     return "$retval"
648 }
649
650 sub set_value {
651     my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
652
653     my $prvalue = process_variables($rvalue);
654
655     if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
656         # Note if a test is something other than build, then we
657         # will need other manditory options.
658         if ($prvalue ne "install") {
659             # for bisect, we need to check BISECT_TYPE
660             if ($prvalue ne "bisect") {
661                 $buildonly = 0;
662             }
663         } else {
664             # install still limits some manditory options.
665             $buildonly = 2;
666         }
667     }
668
669     if ($buildonly && $lvalue =~ /^BISECT_TYPE(\[.*\])?$/ && $prvalue ne "build") {
670         if ($prvalue ne "install") {
671             $buildonly = 0;
672         } else {
673             # install still limits some manditory options.
674             $buildonly = 2;
675         }
676     }
677
678     if (defined($opt{$lvalue})) {
679         if (!$override || defined(${$overrides}{$lvalue})) {
680             my $extra = "";
681             if ($override) {
682                 $extra = "In the same override section!\n";
683             }
684             die "$name: $.: Option $lvalue defined more than once!\n$extra";
685         }
686         ${$overrides}{$lvalue} = $prvalue;
687     }
688
689     $opt{$lvalue} = $prvalue;
690 }
691
692 sub set_eval {
693     my ($lvalue, $rvalue, $name) = @_;
694
695     my $prvalue = process_variables($rvalue);
696     my $arr;
697
698     if (defined($evals{$lvalue})) {
699         $arr = $evals{$lvalue};
700     } else {
701         $arr = [];
702         $evals{$lvalue} = $arr;
703     }
704
705     push @{$arr}, $rvalue;
706 }
707
708 sub set_variable {
709     my ($lvalue, $rvalue) = @_;
710
711     if ($rvalue =~ /^\s*$/) {
712         delete $variable{$lvalue};
713     } else {
714         $rvalue = process_variables($rvalue);
715         $variable{$lvalue} = $rvalue;
716     }
717 }
718
719 sub process_compare {
720     my ($lval, $cmp, $rval) = @_;
721
722     # remove whitespace
723
724     $lval =~ s/^\s*//;
725     $lval =~ s/\s*$//;
726
727     $rval =~ s/^\s*//;
728     $rval =~ s/\s*$//;
729
730     if ($cmp eq "==") {
731         return $lval eq $rval;
732     } elsif ($cmp eq "!=") {
733         return $lval ne $rval;
734     } elsif ($cmp eq "=~") {
735         return $lval =~ m/$rval/;
736     } elsif ($cmp eq "!~") {
737         return $lval !~ m/$rval/;
738     }
739
740     my $statement = "$lval $cmp $rval";
741     my $ret = eval $statement;
742
743     # $@ stores error of eval
744     if ($@) {
745         return -1;
746     }
747
748     return $ret;
749 }
750
751 sub value_defined {
752     my ($val) = @_;
753
754     return defined($variable{$2}) ||
755         defined($opt{$2});
756 }
757
758 my $d = 0;
759 sub process_expression {
760     my ($name, $val) = @_;
761
762     my $c = $d++;
763
764     while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
765         my $express = $1;
766
767         if (process_expression($name, $express)) {
768             $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
769         } else {
770             $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
771         }
772     }
773
774     $d--;
775     my $OR = "\\|\\|";
776     my $AND = "\\&\\&";
777
778     while ($val =~ s/^(.*?)($OR|$AND)//) {
779         my $express = $1;
780         my $op = $2;
781
782         if (process_expression($name, $express)) {
783             if ($op eq "||") {
784                 return 1;
785             }
786         } else {
787             if ($op eq "&&") {
788                 return 0;
789             }
790         }
791     }
792
793     if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
794         my $ret = process_compare($1, $2, $3);
795         if ($ret < 0) {
796             die "$name: $.: Unable to process comparison\n";
797         }
798         return $ret;
799     }
800
801     if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
802         if (defined $1) {
803             return !value_defined($2);
804         } else {
805             return value_defined($2);
806         }
807     }
808
809     if ($val =~ /^\s*0\s*$/) {
810         return 0;
811     } elsif ($val =~ /^\s*\d+\s*$/) {
812         return 1;
813     }
814
815     die ("$name: $.: Undefined content $val in if statement\n");
816 }
817
818 sub process_if {
819     my ($name, $value) = @_;
820
821     # Convert variables and replace undefined ones with 0
822     my $val = process_variables($value, 1);
823     my $ret = process_expression $name, $val;
824
825     return $ret;
826 }
827
828 sub __read_config {
829     my ($config, $current_test_num) = @_;
830
831     my $in;
832     open($in, $config) || die "can't read file $config";
833
834     my $name = $config;
835     $name =~ s,.*/(.*),$1,;
836
837     my $test_num = $$current_test_num;
838     my $default = 1;
839     my $repeat = 1;
840     my $num_tests_set = 0;
841     my $skip = 0;
842     my $rest;
843     my $line;
844     my $test_case = 0;
845     my $if = 0;
846     my $if_set = 0;
847     my $override = 0;
848
849     my %overrides;
850
851     while (<$in>) {
852
853         # ignore blank lines and comments
854         next if (/^\s*$/ || /\s*\#/);
855
856         if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
857
858             my $type = $1;
859             $rest = $2;
860             $line = $2;
861
862             my $old_test_num;
863             my $old_repeat;
864             $override = 0;
865
866             if ($type eq "TEST_START") {
867
868                 if ($num_tests_set) {
869                     die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
870                 }
871
872                 $old_test_num = $test_num;
873                 $old_repeat = $repeat;
874
875                 $test_num += $repeat;
876                 $default = 0;
877                 $repeat = 1;
878             } else {
879                 $default = 1;
880             }
881
882             # If SKIP is anywhere in the line, the command will be skipped
883             if ($rest =~ s/\s+SKIP\b//) {
884                 $skip = 1;
885             } else {
886                 $test_case = 1;
887                 $skip = 0;
888             }
889
890             if ($rest =~ s/\sELSE\b//) {
891                 if (!$if) {
892                     die "$name: $.: ELSE found with out matching IF section\n$_";
893                 }
894                 $if = 0;
895
896                 if ($if_set) {
897                     $skip = 1;
898                 } else {
899                     $skip = 0;
900                 }
901             }
902
903             if ($rest =~ s/\sIF\s+(.*)//) {
904                 if (process_if($name, $1)) {
905                     $if_set = 1;
906                 } else {
907                     $skip = 1;
908                 }
909                 $if = 1;
910             } else {
911                 $if = 0;
912                 $if_set = 0;
913             }
914
915             if (!$skip) {
916                 if ($type eq "TEST_START") {
917                     if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
918                         $repeat = $1;
919                         $repeat_tests{"$test_num"} = $repeat;
920                     }
921                 } elsif ($rest =~ s/\sOVERRIDE\b//) {
922                     # DEFAULT only
923                     $override = 1;
924                     # Clear previous overrides
925                     %overrides = ();
926                 }
927             }
928
929             if (!$skip && $rest !~ /^\s*$/) {
930                 die "$name: $.: Gargbage found after $type\n$_";
931             }
932
933             if ($skip && $type eq "TEST_START") {
934                 $test_num = $old_test_num;
935                 $repeat = $old_repeat;
936             }
937
938         } elsif (/^\s*ELSE\b(.*)$/) {
939             if (!$if) {
940                 die "$name: $.: ELSE found with out matching IF section\n$_";
941             }
942             $rest = $1;
943             if ($if_set) {
944                 $skip = 1;
945                 $rest = "";
946             } else {
947                 $skip = 0;
948
949                 if ($rest =~ /\sIF\s+(.*)/) {
950                     # May be a ELSE IF section.
951                     if (process_if($name, $1)) {
952                         $if_set = 1;
953                     } else {
954                         $skip = 1;
955                     }
956                     $rest = "";
957                 } else {
958                     $if = 0;
959                 }
960             }
961
962             if ($rest !~ /^\s*$/) {
963                 die "$name: $.: Gargbage found after DEFAULTS\n$_";
964             }
965
966         } elsif (/^\s*INCLUDE\s+(\S+)/) {
967
968             next if ($skip);
969
970             if (!$default) {
971                 die "$name: $.: INCLUDE can only be done in default sections\n$_";
972             }
973
974             my $file = process_variables($1);
975
976             if ($file !~ m,^/,) {
977                 # check the path of the config file first
978                 if ($config =~ m,(.*)/,) {
979                     if (-f "$1/$file") {
980                         $file = "$1/$file";
981                     }
982                 }
983             }
984                 
985             if ( ! -r $file ) {
986                 die "$name: $.: Can't read file $file\n$_";
987             }
988
989             if (__read_config($file, \$test_num)) {
990                 $test_case = 1;
991             }
992
993         } elsif (/^\s*([A-Z_\[\]\d]+)\s*=~\s*(.*?)\s*$/) {
994
995             next if ($skip);
996
997             my $lvalue = $1;
998             my $rvalue = $2;
999
1000             if ($default || $lvalue =~ /\[\d+\]$/) {
1001                 set_eval($lvalue, $rvalue, $name);
1002             } else {
1003                 my $val = "$lvalue\[$test_num\]";
1004                 set_eval($val, $rvalue, $name);
1005             }
1006
1007         } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
1008
1009             next if ($skip);
1010
1011             my $lvalue = $1;
1012             my $rvalue = $2;
1013
1014             if (!$default &&
1015                 ($lvalue eq "NUM_TESTS" ||
1016                  $lvalue eq "LOG_FILE" ||
1017                  $lvalue eq "CLEAR_LOG")) {
1018                 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
1019             }
1020
1021             if ($lvalue eq "NUM_TESTS") {
1022                 if ($test_num) {
1023                     die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
1024                 }
1025                 if (!$default) {
1026                     die "$name: $.: NUM_TESTS must be set in default section\n";
1027                 }
1028                 $num_tests_set = 1;
1029             }
1030
1031             if ($default || $lvalue =~ /\[\d+\]$/) {
1032                 set_value($lvalue, $rvalue, $override, \%overrides, $name);
1033             } else {
1034                 my $val = "$lvalue\[$test_num\]";
1035                 set_value($val, $rvalue, $override, \%overrides, $name);
1036
1037                 if ($repeat > 1) {
1038                     $repeats{$val} = $repeat;
1039                 }
1040             }
1041         } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
1042             next if ($skip);
1043
1044             my $lvalue = $1;
1045             my $rvalue = $2;
1046
1047             # process config variables.
1048             # Config variables are only active while reading the
1049             # config and can be defined anywhere. They also ignore
1050             # TEST_START and DEFAULTS, but are skipped if they are in
1051             # on of these sections that have SKIP defined.
1052             # The save variable can be
1053             # defined multiple times and the new one simply overrides
1054             # the prevous one.
1055             set_variable($lvalue, $rvalue);
1056
1057         } else {
1058             die "$name: $.: Garbage found in config\n$_";
1059         }
1060     }
1061
1062     if ($test_num) {
1063         $test_num += $repeat - 1;
1064         $opt{"NUM_TESTS"} = $test_num;
1065     }
1066
1067     close($in);
1068
1069     $$current_test_num = $test_num;
1070
1071     return $test_case;
1072 }
1073
1074 sub get_test_case {
1075         print "What test case would you like to run?\n";
1076         print " (build, install or boot)\n";
1077         print " Other tests are available but require editing the config file\n";
1078         my $ans = <STDIN>;
1079         chomp $ans;
1080         $default{"TEST_TYPE"} = $ans;
1081 }
1082
1083 sub read_config {
1084     my ($config) = @_;
1085
1086     my $test_case;
1087     my $test_num = 0;
1088
1089     $test_case = __read_config $config, \$test_num;
1090
1091     # make sure we have all mandatory configs
1092     get_mandatory_configs;
1093
1094     # was a test specified?
1095     if (!$test_case) {
1096         print "No test case specified.\n";
1097         get_test_case;
1098     }
1099
1100     # set any defaults
1101
1102     foreach my $default (keys %default) {
1103         if (!defined($opt{$default})) {
1104             $opt{$default} = $default{$default};
1105         }
1106     }
1107
1108     if ($opt{"IGNORE_UNUSED"} == 1) {
1109         return;
1110     }
1111
1112     my %not_used;
1113
1114     # check if there are any stragglers (typos?)
1115     foreach my $option (keys %opt) {
1116         my $op = $option;
1117         # remove per test labels.
1118         $op =~ s/\[.*\]//;
1119         if (!exists($option_map{$op}) &&
1120             !exists($default{$op}) &&
1121             !exists($used_options{$op})) {
1122             $not_used{$op} = 1;
1123         }
1124     }
1125
1126     if (%not_used) {
1127         my $s = "s are";
1128         $s = " is" if (keys %not_used == 1);
1129         print "The following option$s not used; could be a typo:\n";
1130         foreach my $option (keys %not_used) {
1131             print "$option\n";
1132         }
1133         print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
1134         if (!read_yn "Do you want to continue?") {
1135             exit -1;
1136         }
1137     }
1138 }
1139
1140 sub __eval_option {
1141     my ($name, $option, $i) = @_;
1142
1143     # Add space to evaluate the character before $
1144     $option = " $option";
1145     my $retval = "";
1146     my $repeated = 0;
1147     my $parent = 0;
1148
1149     foreach my $test (keys %repeat_tests) {
1150         if ($i >= $test &&
1151             $i < $test + $repeat_tests{$test}) {
1152
1153             $repeated = 1;
1154             $parent = $test;
1155             last;
1156         }
1157     }
1158
1159     while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
1160         my $start = $1;
1161         my $var = $2;
1162         my $end = $3;
1163
1164         # Append beginning of line
1165         $retval = "$retval$start";
1166
1167         # If the iteration option OPT[$i] exists, then use that.
1168         # otherwise see if the default OPT (without [$i]) exists.
1169
1170         my $o = "$var\[$i\]";
1171         my $parento = "$var\[$parent\]";
1172
1173         # If a variable contains itself, use the default var
1174         if (($var eq $name) && defined($opt{$var})) {
1175             $o = $opt{$var};
1176             $retval = "$retval$o";
1177         } elsif (defined($opt{$o})) {
1178             $o = $opt{$o};
1179             $retval = "$retval$o";
1180         } elsif ($repeated && defined($opt{$parento})) {
1181             $o = $opt{$parento};
1182             $retval = "$retval$o";
1183         } elsif (defined($opt{$var})) {
1184             $o = $opt{$var};
1185             $retval = "$retval$o";
1186         } elsif ($var eq "KERNEL_VERSION" && defined($make)) {
1187             # special option KERNEL_VERSION uses kernel version
1188             get_version();
1189             $retval = "$retval$version";
1190         } else {
1191             $retval = "$retval\$\{$var\}";
1192         }
1193
1194         $option = $end;
1195     }
1196
1197     $retval = "$retval$option";
1198
1199     $retval =~ s/^ //;
1200
1201     return $retval;
1202 }
1203
1204 sub process_evals {
1205     my ($name, $option, $i) = @_;
1206
1207     my $option_name = "$name\[$i\]";
1208     my $ev;
1209
1210     my $old_option = $option;
1211
1212     if (defined($evals{$option_name})) {
1213         $ev = $evals{$option_name};
1214     } elsif (defined($evals{$name})) {
1215         $ev = $evals{$name};
1216     } else {
1217         return $option;
1218     }
1219
1220     for my $e (@{$ev}) {
1221         eval "\$option =~ $e";
1222     }
1223
1224     if ($option ne $old_option) {
1225         doprint("$name changed from '$old_option' to '$option'\n");
1226     }
1227
1228     return $option;
1229 }
1230
1231 sub eval_option {
1232     my ($name, $option, $i) = @_;
1233
1234     my $prev = "";
1235
1236     # Since an option can evaluate to another option,
1237     # keep iterating until we do not evaluate any more
1238     # options.
1239     my $r = 0;
1240     while ($prev ne $option) {
1241         # Check for recursive evaluations.
1242         # 100 deep should be more than enough.
1243         if ($r++ > 100) {
1244             die "Over 100 evaluations accurred with $option\n" .
1245                 "Check for recursive variables\n";
1246         }
1247         $prev = $option;
1248         $option = __eval_option($name, $option, $i);
1249     }
1250
1251     $option = process_evals($name, $option, $i);
1252
1253     return $option;
1254 }
1255
1256 sub run_command;
1257 sub start_monitor;
1258 sub end_monitor;
1259 sub wait_for_monitor;
1260
1261 sub reboot {
1262     my ($time) = @_;
1263
1264     # Make sure everything has been written to disk
1265     run_ssh("sync");
1266
1267     if (defined($time)) {
1268         start_monitor;
1269         # flush out current monitor
1270         # May contain the reboot success line
1271         wait_for_monitor 1;
1272     }
1273
1274     # try to reboot normally
1275     if (run_command $reboot) {
1276         if (defined($powercycle_after_reboot)) {
1277             sleep $powercycle_after_reboot;
1278             run_command "$power_cycle";
1279         }
1280     } else {
1281         # nope? power cycle it.
1282         run_command "$power_cycle";
1283     }
1284
1285     if (defined($time)) {
1286
1287         # We only want to get to the new kernel, don't fail
1288         # if we stumble over a call trace.
1289         my $save_ignore_errors = $ignore_errors;
1290         $ignore_errors = 1;
1291
1292         # Look for the good kernel to boot
1293         if (wait_for_monitor($time, "Linux version")) {
1294             # reboot got stuck?
1295             doprint "Reboot did not finish. Forcing power cycle\n";
1296             run_command "$power_cycle";
1297         }
1298
1299         $ignore_errors = $save_ignore_errors;
1300
1301         # Still need to wait for the reboot to finish
1302         wait_for_monitor($time, $reboot_success_line);
1303
1304         end_monitor;
1305     }
1306 }
1307
1308 sub reboot_to_good {
1309     my ($time) = @_;
1310
1311     if (defined($switch_to_good)) {
1312         run_command $switch_to_good;
1313     }
1314
1315     reboot $time;
1316 }
1317
1318 sub do_not_reboot {
1319     my $i = $iteration;
1320
1321     return $test_type eq "build" || $no_reboot ||
1322         ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1323         ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1324 }
1325
1326 sub dodie {
1327     doprint "CRITICAL FAILURE... ", @_, "\n";
1328
1329     my $i = $iteration;
1330
1331     if ($reboot_on_error && !do_not_reboot) {
1332
1333         doprint "REBOOTING\n";
1334         reboot_to_good;
1335
1336     } elsif ($poweroff_on_error && defined($power_off)) {
1337         doprint "POWERING OFF\n";
1338         `$power_off`;
1339     }
1340
1341     if (defined($opt{"LOG_FILE"})) {
1342         print " See $opt{LOG_FILE} for more info.\n";
1343     }
1344
1345     die @_, "\n";
1346 }
1347
1348 sub open_console {
1349     my ($fp) = @_;
1350
1351     my $flags;
1352
1353     # save terminal settings
1354     $stty = `stty -g`;
1355
1356     my $pid = open($fp, "$console|") or
1357         dodie "Can't open console $console";
1358
1359     $flags = fcntl($fp, F_GETFL, 0) or
1360         dodie "Can't get flags for the socket: $!";
1361     $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
1362         dodie "Can't set flags for the socket: $!";
1363
1364     return $pid;
1365 }
1366
1367 sub close_console {
1368     my ($fp, $pid) = @_;
1369
1370     doprint "kill child process $pid\n";
1371     kill $close_console_signal, $pid;
1372
1373     print "closing!\n";
1374     close($fp);
1375
1376     # restore terminal settings
1377     system("stty $stty");
1378 }
1379
1380 sub start_monitor {
1381     if ($monitor_cnt++) {
1382         return;
1383     }
1384     $monitor_fp = \*MONFD;
1385     $monitor_pid = open_console $monitor_fp;
1386
1387     return;
1388
1389     open(MONFD, "Stop perl from warning about single use of MONFD");
1390 }
1391
1392 sub end_monitor {
1393     return if (!defined $console);
1394     if (--$monitor_cnt) {
1395         return;
1396     }
1397     close_console($monitor_fp, $monitor_pid);
1398 }
1399
1400 sub wait_for_monitor {
1401     my ($time, $stop) = @_;
1402     my $full_line = "";
1403     my $line;
1404     my $booted = 0;
1405     my $start_time = time;
1406     my $skip_call_trace = 0;
1407     my $bug = 0;
1408     my $bug_ignored = 0;
1409     my $now;
1410
1411     doprint "** Wait for monitor to settle down **\n";
1412
1413     # read the monitor and wait for the system to calm down
1414     while (!$booted) {
1415         $line = wait_for_input($monitor_fp, $time);
1416         last if (!defined($line));
1417         print "$line";
1418         $full_line .= $line;
1419
1420         if (defined($stop) && $full_line =~ /$stop/) {
1421             doprint "wait for monitor detected $stop\n";
1422             $booted = 1;
1423         }
1424
1425         if ($full_line =~ /\[ backtrace testing \]/) {
1426             $skip_call_trace = 1;
1427         }
1428
1429         if ($full_line =~ /call trace:/i) {
1430             if (!$bug && !$skip_call_trace) {
1431                 if ($ignore_errors) {
1432                     $bug_ignored = 1;
1433                 } else {
1434                     $bug = 1;
1435                 }
1436             }
1437         }
1438
1439         if ($full_line =~ /\[ end of backtrace testing \]/) {
1440             $skip_call_trace = 0;
1441         }
1442
1443         if ($full_line =~ /Kernel panic -/) {
1444             $bug = 1;
1445         }
1446
1447         if ($line =~ /\n/) {
1448             $full_line = "";
1449         }
1450         $now = time;
1451         if ($now - $start_time >= $max_monitor_wait) {
1452             doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
1453             return 1;
1454         }
1455     }
1456     print "** Monitor flushed **\n";
1457
1458     # if stop is defined but wasn't hit, return error
1459     # used by reboot (which wants to see a reboot)
1460     if (defined($stop) && !$booted) {
1461         $bug = 1;
1462     }
1463     return $bug;
1464 }
1465
1466 sub save_logs {
1467         my ($result, $basedir) = @_;
1468         my @t = localtime;
1469         my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1470                 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1471
1472         my $type = $build_type;
1473         if ($type =~ /useconfig/) {
1474             $type = "useconfig";
1475         }
1476
1477         my $dir = "$machine-$test_type-$type-$result-$date";
1478
1479         $dir = "$basedir/$dir";
1480
1481         if (!-d $dir) {
1482             mkpath($dir) or
1483                 die "can't create $dir";
1484         }
1485
1486         my %files = (
1487                 "config" => $output_config,
1488                 "buildlog" => $buildlog,
1489                 "dmesg" => $dmesg,
1490                 "testlog" => $testlog,
1491         );
1492
1493         while (my ($name, $source) = each(%files)) {
1494                 if (-f "$source") {
1495                         cp "$source", "$dir/$name" or
1496                                 die "failed to copy $source";
1497                 }
1498         }
1499
1500         doprint "*** Saved info to $dir ***\n";
1501 }
1502
1503 sub fail {
1504
1505         if (defined($post_test)) {
1506                 run_command $post_test;
1507         }
1508
1509         if ($die_on_failure) {
1510                 dodie @_;
1511         }
1512
1513         doprint "FAILED\n";
1514
1515         my $i = $iteration;
1516
1517         # no need to reboot for just building.
1518         if (!do_not_reboot) {
1519             doprint "REBOOTING\n";
1520             reboot_to_good $sleep_time;
1521         }
1522
1523         my $name = "";
1524
1525         if (defined($test_name)) {
1526             $name = " ($test_name)";
1527         }
1528
1529         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1530         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1531         doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
1532         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1533         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1534
1535         if (defined($store_failures)) {
1536             save_logs "fail", $store_failures;
1537         }
1538
1539         return 1;
1540 }
1541
1542 sub run_command {
1543     my ($command, $redirect) = @_;
1544     my $start_time;
1545     my $end_time;
1546     my $dolog = 0;
1547     my $dord = 0;
1548     my $pid;
1549
1550     $start_time = time;
1551
1552     $command =~ s/\$SSH_USER/$ssh_user/g;
1553     $command =~ s/\$MACHINE/$machine/g;
1554
1555     doprint("$command ... ");
1556
1557     $pid = open(CMD, "$command 2>&1 |") or
1558         (fail "unable to exec $command" and return 0);
1559
1560     if (defined($opt{"LOG_FILE"})) {
1561         open(LOG, ">>$opt{LOG_FILE}") or
1562             dodie "failed to write to log";
1563         $dolog = 1;
1564     }
1565
1566     if (defined($redirect)) {
1567         open (RD, ">$redirect") or
1568             dodie "failed to write to redirect $redirect";
1569         $dord = 1;
1570     }
1571
1572     while (<CMD>) {
1573         print LOG if ($dolog);
1574         print RD  if ($dord);
1575     }
1576
1577     waitpid($pid, 0);
1578     my $failed = $?;
1579
1580     close(CMD);
1581     close(LOG) if ($dolog);
1582     close(RD)  if ($dord);
1583
1584     $end_time = time;
1585     my $delta = $end_time - $start_time;
1586
1587     if ($delta == 1) {
1588         doprint "[1 second] ";
1589     } else {
1590         doprint "[$delta seconds] ";
1591     }
1592
1593     if ($failed) {
1594         doprint "FAILED!\n";
1595     } else {
1596         doprint "SUCCESS\n";
1597     }
1598
1599     return !$failed;
1600 }
1601
1602 sub run_ssh {
1603     my ($cmd) = @_;
1604     my $cp_exec = $ssh_exec;
1605
1606     $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1607     return run_command "$cp_exec";
1608 }
1609
1610 sub run_scp {
1611     my ($src, $dst, $cp_scp) = @_;
1612
1613     $cp_scp =~ s/\$SRC_FILE/$src/g;
1614     $cp_scp =~ s/\$DST_FILE/$dst/g;
1615
1616     return run_command "$cp_scp";
1617 }
1618
1619 sub run_scp_install {
1620     my ($src, $dst) = @_;
1621
1622     my $cp_scp = $scp_to_target_install;
1623
1624     return run_scp($src, $dst, $cp_scp);
1625 }
1626
1627 sub run_scp_mod {
1628     my ($src, $dst) = @_;
1629
1630     my $cp_scp = $scp_to_target;
1631
1632     return run_scp($src, $dst, $cp_scp);
1633 }
1634
1635 sub get_grub2_index {
1636
1637     return if (defined($grub_number) && defined($last_grub_menu) &&
1638                $last_grub_menu eq $grub_menu && defined($last_machine) &&
1639                $last_machine eq $machine);
1640
1641     doprint "Find grub2 menu ... ";
1642     $grub_number = -1;
1643
1644     my $ssh_grub = $ssh_exec;
1645     $ssh_grub =~ s,\$SSH_COMMAND,cat $grub_file,g;
1646
1647     open(IN, "$ssh_grub |")
1648         or die "unable to get $grub_file";
1649
1650     my $found = 0;
1651
1652     while (<IN>) {
1653         if (/^menuentry.*$grub_menu/) {
1654             $grub_number++;
1655             $found = 1;
1656             last;
1657         } elsif (/^menuentry\s/) {
1658             $grub_number++;
1659         }
1660     }
1661     close(IN);
1662
1663     die "Could not find '$grub_menu' in $grub_file on $machine"
1664         if (!$found);
1665     doprint "$grub_number\n";
1666     $last_grub_menu = $grub_menu;
1667     $last_machine = $machine;
1668 }
1669
1670 sub get_grub_index {
1671
1672     if ($reboot_type eq "grub2") {
1673         get_grub2_index;
1674         return;
1675     }
1676
1677     if ($reboot_type ne "grub") {
1678         return;
1679     }
1680     return if (defined($grub_number) && defined($last_grub_menu) &&
1681                $last_grub_menu eq $grub_menu && defined($last_machine) &&
1682                $last_machine eq $machine);
1683
1684     doprint "Find grub menu ... ";
1685     $grub_number = -1;
1686
1687     my $ssh_grub = $ssh_exec;
1688     $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1689
1690     open(IN, "$ssh_grub |")
1691         or die "unable to get menu.lst";
1692
1693     my $found = 0;
1694
1695     while (<IN>) {
1696         if (/^\s*title\s+$grub_menu\s*$/) {
1697             $grub_number++;
1698             $found = 1;
1699             last;
1700         } elsif (/^\s*title\s/) {
1701             $grub_number++;
1702         }
1703     }
1704     close(IN);
1705
1706     die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
1707         if (!$found);
1708     doprint "$grub_number\n";
1709     $last_grub_menu = $grub_menu;
1710     $last_machine = $machine;
1711 }
1712
1713 sub wait_for_input
1714 {
1715     my ($fp, $time) = @_;
1716     my $rin;
1717     my $ready;
1718     my $line;
1719     my $ch;
1720
1721     if (!defined($time)) {
1722         $time = $timeout;
1723     }
1724
1725     $rin = '';
1726     vec($rin, fileno($fp), 1) = 1;
1727     ($ready, $time) = select($rin, undef, undef, $time);
1728
1729     $line = "";
1730
1731     # try to read one char at a time
1732     while (sysread $fp, $ch, 1) {
1733         $line .= $ch;
1734         last if ($ch eq "\n");
1735     }
1736
1737     if (!length($line)) {
1738         return undef;
1739     }
1740
1741     return $line;
1742 }
1743
1744 sub reboot_to {
1745     if (defined($switch_to_test)) {
1746         run_command $switch_to_test;
1747     }
1748
1749     if ($reboot_type eq "grub") {
1750         run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1751     } elsif ($reboot_type eq "grub2") {
1752         run_ssh "$grub_reboot $grub_number";
1753     } elsif ($reboot_type eq "syslinux") {
1754         run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
1755     } elsif (defined $reboot_script) {
1756         run_command "$reboot_script";
1757     }
1758     reboot;
1759 }
1760
1761 sub get_sha1 {
1762     my ($commit) = @_;
1763
1764     doprint "git rev-list --max-count=1 $commit ... ";
1765     my $sha1 = `git rev-list --max-count=1 $commit`;
1766     my $ret = $?;
1767
1768     logit $sha1;
1769
1770     if ($ret) {
1771         doprint "FAILED\n";
1772         dodie "Failed to get git $commit";
1773     }
1774
1775     print "SUCCESS\n";
1776
1777     chomp $sha1;
1778
1779     return $sha1;
1780 }
1781
1782 sub monitor {
1783     my $booted = 0;
1784     my $bug = 0;
1785     my $bug_ignored = 0;
1786     my $skip_call_trace = 0;
1787     my $loops;
1788
1789     wait_for_monitor 5;
1790
1791     my $line;
1792     my $full_line = "";
1793
1794     open(DMESG, "> $dmesg") or
1795         die "unable to write to $dmesg";
1796
1797     reboot_to;
1798
1799     my $success_start;
1800     my $failure_start;
1801     my $monitor_start = time;
1802     my $done = 0;
1803     my $version_found = 0;
1804
1805     while (!$done) {
1806
1807         if ($bug && defined($stop_after_failure) &&
1808             $stop_after_failure >= 0) {
1809             my $time = $stop_after_failure - (time - $failure_start);
1810             $line = wait_for_input($monitor_fp, $time);
1811             if (!defined($line)) {
1812                 doprint "bug timed out after $booted_timeout seconds\n";
1813                 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1814                 last;
1815             }
1816         } elsif ($booted) {
1817             $line = wait_for_input($monitor_fp, $booted_timeout);
1818             if (!defined($line)) {
1819                 my $s = $booted_timeout == 1 ? "" : "s";
1820                 doprint "Successful boot found: break after $booted_timeout second$s\n";
1821                 last;
1822             }
1823         } else {
1824             $line = wait_for_input($monitor_fp);
1825             if (!defined($line)) {
1826                 my $s = $timeout == 1 ? "" : "s";
1827                 doprint "Timed out after $timeout second$s\n";
1828                 last;
1829             }
1830         }
1831
1832         doprint $line;
1833         print DMESG $line;
1834
1835         # we are not guaranteed to get a full line
1836         $full_line .= $line;
1837
1838         if ($full_line =~ /$success_line/) {
1839             $booted = 1;
1840             $success_start = time;
1841         }
1842
1843         if ($booted && defined($stop_after_success) &&
1844             $stop_after_success >= 0) {
1845             my $now = time;
1846             if ($now - $success_start >= $stop_after_success) {
1847                 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1848                 last;
1849             }
1850         }
1851
1852         if ($full_line =~ /\[ backtrace testing \]/) {
1853             $skip_call_trace = 1;
1854         }
1855
1856         if ($full_line =~ /call trace:/i) {
1857             if (!$bug && !$skip_call_trace) {
1858                 if ($ignore_errors) {
1859                     $bug_ignored = 1;
1860                 } else {
1861                     $bug = 1;
1862                     $failure_start = time;
1863                 }
1864             }
1865         }
1866
1867         if ($bug && defined($stop_after_failure) &&
1868             $stop_after_failure >= 0) {
1869             my $now = time;
1870             if ($now - $failure_start >= $stop_after_failure) {
1871                 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1872                 last;
1873             }
1874         }
1875
1876         if ($full_line =~ /\[ end of backtrace testing \]/) {
1877             $skip_call_trace = 0;
1878         }
1879
1880         if ($full_line =~ /Kernel panic -/) {
1881             $failure_start = time;
1882             $bug = 1;
1883         }
1884
1885         # Detect triple faults by testing the banner
1886         if ($full_line =~ /\bLinux version (\S+).*\n/) {
1887             if ($1 eq $version) {
1888                 $version_found = 1;
1889             } elsif ($version_found && $detect_triplefault) {
1890                 # We already booted into the kernel we are testing,
1891                 # but now we booted into another kernel?
1892                 # Consider this a triple fault.
1893                 doprint "Already booted in Linux kernel $version, but now\n";
1894                 doprint "we booted into Linux kernel $1.\n";
1895                 doprint "Assuming that this is a triple fault.\n";
1896                 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1897                 last;
1898             }
1899         }
1900
1901         if ($line =~ /\n/) {
1902             $full_line = "";
1903         }
1904
1905         if ($stop_test_after > 0 && !$booted && !$bug) {
1906             if (time - $monitor_start > $stop_test_after) {
1907                 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
1908                 $done = 1;
1909             }
1910         }
1911     }
1912
1913     close(DMESG);
1914
1915     if ($bug) {
1916         return 0 if ($in_bisect);
1917         fail "failed - got a bug report" and return 0;
1918     }
1919
1920     if (!$booted) {
1921         return 0 if ($in_bisect);
1922         fail "failed - never got a boot prompt." and return 0;
1923     }
1924
1925     if ($bug_ignored) {
1926         doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1927     }
1928
1929     return 1;
1930 }
1931
1932 sub eval_kernel_version {
1933     my ($option) = @_;
1934
1935     $option =~ s/\$KERNEL_VERSION/$version/g;
1936
1937     return $option;
1938 }
1939
1940 sub do_post_install {
1941
1942     return if (!defined($post_install));
1943
1944     my $cp_post_install = eval_kernel_version $post_install;
1945     run_command "$cp_post_install" or
1946         dodie "Failed to run post install";
1947 }
1948
1949 # Sometimes the reboot fails, and will hang. We try to ssh to the box
1950 # and if we fail, we force another reboot, that should powercycle it.
1951 sub test_booted {
1952     if (!run_ssh "echo testing connection") {
1953         reboot $sleep_time;
1954     }
1955 }
1956
1957 sub install {
1958
1959     return if ($no_install);
1960
1961     if (defined($pre_install)) {
1962         my $cp_pre_install = eval_kernel_version $pre_install;
1963         run_command "$cp_pre_install" or
1964             dodie "Failed to run pre install";
1965     }
1966
1967     my $cp_target = eval_kernel_version $target_image;
1968
1969     test_booted;
1970
1971     run_scp_install "$outputdir/$build_target", "$cp_target" or
1972         dodie "failed to copy image";
1973
1974     my $install_mods = 0;
1975
1976     # should we process modules?
1977     $install_mods = 0;
1978     open(IN, "$output_config") or dodie("Can't read config file");
1979     while (<IN>) {
1980         if (/CONFIG_MODULES(=y)?/) {
1981             if (defined($1)) {
1982                 $install_mods = 1;
1983                 last;
1984             }
1985         }
1986     }
1987     close(IN);
1988
1989     if (!$install_mods) {
1990         do_post_install;
1991         doprint "No modules needed\n";
1992         return;
1993     }
1994
1995     run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
1996         dodie "Failed to install modules";
1997
1998     my $modlib = "/lib/modules/$version";
1999     my $modtar = "ktest-mods.tar.bz2";
2000
2001     run_ssh "rm -rf $modlib" or
2002         dodie "failed to remove old mods: $modlib";
2003
2004     # would be nice if scp -r did not follow symbolic links
2005     run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
2006         dodie "making tarball";
2007
2008     run_scp_mod "$tmpdir/$modtar", "/tmp" or
2009         dodie "failed to copy modules";
2010
2011     unlink "$tmpdir/$modtar";
2012
2013     run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
2014         dodie "failed to tar modules";
2015
2016     run_ssh "rm -f /tmp/$modtar";
2017
2018     do_post_install;
2019 }
2020
2021 sub get_version {
2022     # get the release name
2023     return if ($have_version);
2024     doprint "$make kernelrelease ... ";
2025     $version = `$make -s kernelrelease | tail -1`;
2026     chomp($version);
2027     doprint "$version\n";
2028     $have_version = 1;
2029 }
2030
2031 sub start_monitor_and_boot {
2032     # Make sure the stable kernel has finished booting
2033
2034     # Install bisects, don't need console
2035     if (defined $console) {
2036         start_monitor;
2037         wait_for_monitor 5;
2038         end_monitor;
2039     }
2040
2041     get_grub_index;
2042     get_version;
2043     install;
2044
2045     start_monitor if (defined $console);
2046     return monitor;
2047 }
2048
2049 my $check_build_re = ".*:.*(warning|error|Error):.*";
2050 my $utf8_quote = "\\x{e2}\\x{80}(\\x{98}|\\x{99})";
2051
2052 sub process_warning_line {
2053     my ($line) = @_;
2054
2055     chomp $line;
2056
2057     # for distcc heterogeneous systems, some compilers
2058     # do things differently causing warning lines
2059     # to be slightly different. This makes an attempt
2060     # to fixe those issues.
2061
2062     # chop off the index into the line
2063     # using distcc, some compilers give different indexes
2064     # depending on white space
2065     $line =~ s/^(\s*\S+:\d+:)\d+/$1/;
2066
2067     # Some compilers use UTF-8 extended for quotes and some don't.
2068     $line =~ s/$utf8_quote/'/g;
2069
2070     return $line;
2071 }
2072
2073 # Read buildlog and check against warnings file for any
2074 # new warnings.
2075 #
2076 # Returns 1 if OK
2077 #         0 otherwise
2078 sub check_buildlog {
2079     return 1 if (!defined $warnings_file);
2080
2081     my %warnings_list;
2082
2083     # Failed builds should not reboot the target
2084     my $save_no_reboot = $no_reboot;
2085     $no_reboot = 1;
2086
2087     if (-f $warnings_file) {
2088         open(IN, $warnings_file) or
2089             dodie "Error opening $warnings_file";
2090
2091         while (<IN>) {
2092             if (/$check_build_re/) {
2093                 my $warning = process_warning_line $_;
2094                 
2095                 $warnings_list{$warning} = 1;
2096             }
2097         }
2098         close(IN);
2099     }
2100
2101     # If warnings file didn't exist, and WARNINGS_FILE exist,
2102     # then we fail on any warning!
2103
2104     open(IN, $buildlog) or dodie "Can't open $buildlog";
2105     while (<IN>) {
2106         if (/$check_build_re/) {
2107             my $warning = process_warning_line $_;
2108
2109             if (!defined $warnings_list{$warning}) {
2110                 fail "New warning found (not in $warnings_file)\n$_\n";
2111                 $no_reboot = $save_no_reboot;
2112                 return 0;
2113             }
2114         }
2115     }
2116     $no_reboot = $save_no_reboot;
2117     close(IN);
2118 }
2119
2120 sub check_patch_buildlog {
2121     my ($patch) = @_;
2122
2123     my @files = `git show $patch | diffstat -l`;
2124
2125     foreach my $file (@files) {
2126         chomp $file;
2127     }
2128
2129     open(IN, "git show $patch |") or
2130         dodie "failed to show $patch";
2131     while (<IN>) {
2132         if (m,^--- a/(.*),) {
2133             chomp $1;
2134             $files[$#files] = $1;
2135         }
2136     }
2137     close(IN);
2138
2139     open(IN, $buildlog) or dodie "Can't open $buildlog";
2140     while (<IN>) {
2141         if (/^\s*(.*?):.*(warning|error)/) {
2142             my $err = $1;
2143             foreach my $file (@files) {
2144                 my $fullpath = "$builddir/$file";
2145                 if ($file eq $err || $fullpath eq $err) {
2146                     fail "$file built with warnings" and return 0;
2147                 }
2148             }
2149         }
2150     }
2151     close(IN);
2152
2153     return 1;
2154 }
2155
2156 sub apply_min_config {
2157     my $outconfig = "$output_config.new";
2158
2159     # Read the config file and remove anything that
2160     # is in the force_config hash (from minconfig and others)
2161     # then add the force config back.
2162
2163     doprint "Applying minimum configurations into $output_config.new\n";
2164
2165     open (OUT, ">$outconfig") or
2166         dodie "Can't create $outconfig";
2167
2168     if (-f $output_config) {
2169         open (IN, $output_config) or
2170             dodie "Failed to open $output_config";
2171         while (<IN>) {
2172             if (/^(# )?(CONFIG_[^\s=]*)/) {
2173                 next if (defined($force_config{$2}));
2174             }
2175             print OUT;
2176         }
2177         close IN;
2178     }
2179     foreach my $config (keys %force_config) {
2180         print OUT "$force_config{$config}\n";
2181     }
2182     close OUT;
2183
2184     run_command "mv $outconfig $output_config";
2185 }
2186
2187 sub make_oldconfig {
2188
2189     my @force_list = keys %force_config;
2190
2191     if ($#force_list >= 0) {
2192         apply_min_config;
2193     }
2194
2195     if (!run_command "$make olddefconfig") {
2196         # Perhaps olddefconfig doesn't exist in this version of the kernel
2197         # try oldnoconfig
2198         doprint "olddefconfig failed, trying make oldnoconfig\n";
2199         if (!run_command "$make oldnoconfig") {
2200             doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
2201             # try a yes '' | oldconfig
2202             run_command "yes '' | $make oldconfig" or
2203                 dodie "failed make config oldconfig";
2204         }
2205     }
2206 }
2207
2208 # read a config file and use this to force new configs.
2209 sub load_force_config {
2210     my ($config) = @_;
2211
2212     doprint "Loading force configs from $config\n";
2213     open(IN, $config) or
2214         dodie "failed to read $config";
2215     while (<IN>) {
2216         chomp;
2217         if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
2218             $force_config{$1} = $_;
2219         } elsif (/^# (CONFIG_\S*) is not set/) {
2220             $force_config{$1} = $_;
2221         }
2222     }
2223     close IN;
2224 }
2225
2226 sub build {
2227     my ($type) = @_;
2228
2229     unlink $buildlog;
2230
2231     # Failed builds should not reboot the target
2232     my $save_no_reboot = $no_reboot;
2233     $no_reboot = 1;
2234
2235     # Calculate a new version from here.
2236     $have_version = 0;
2237
2238     if (defined($pre_build)) {
2239         my $ret = run_command $pre_build;
2240         if (!$ret && defined($pre_build_die) &&
2241             $pre_build_die) {
2242             dodie "failed to pre_build\n";
2243         }
2244     }
2245
2246     if ($type =~ /^useconfig:(.*)/) {
2247         run_command "cp $1 $output_config" or
2248             dodie "could not copy $1 to .config";
2249
2250         $type = "oldconfig";
2251     }
2252
2253     # old config can ask questions
2254     if ($type eq "oldconfig") {
2255         $type = "olddefconfig";
2256
2257         # allow for empty configs
2258         run_command "touch $output_config";
2259
2260         if (!$noclean) {
2261             run_command "mv $output_config $outputdir/config_temp" or
2262                 dodie "moving .config";
2263
2264             run_command "$make mrproper" or dodie "make mrproper";
2265
2266             run_command "mv $outputdir/config_temp $output_config" or
2267                 dodie "moving config_temp";
2268         }
2269
2270     } elsif (!$noclean) {
2271         unlink "$output_config";
2272         run_command "$make mrproper" or
2273             dodie "make mrproper";
2274     }
2275
2276     # add something to distinguish this build
2277     open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
2278     print OUT "$localversion\n";
2279     close(OUT);
2280
2281     if (defined($minconfig)) {
2282         load_force_config($minconfig);
2283     }
2284
2285     if ($type ne "olddefconfig") {
2286         run_command "$make $type" or
2287             dodie "failed make config";
2288     }
2289     # Run old config regardless, to enforce min configurations
2290     make_oldconfig;
2291
2292     my $build_ret = run_command "$make $build_options", $buildlog;
2293
2294     if (defined($post_build)) {
2295         # Because a post build may change the kernel version
2296         # do it now.
2297         get_version;
2298         my $ret = run_command $post_build;
2299         if (!$ret && defined($post_build_die) &&
2300             $post_build_die) {
2301             dodie "failed to post_build\n";
2302         }
2303     }
2304
2305     if (!$build_ret) {
2306         # bisect may need this to pass
2307         if ($in_bisect) {
2308             $no_reboot = $save_no_reboot;
2309             return 0;
2310         }
2311         fail "failed build" and return 0;
2312     }
2313
2314     $no_reboot = $save_no_reboot;
2315
2316     return 1;
2317 }
2318
2319 sub halt {
2320     if (!run_ssh "halt" or defined($power_off)) {
2321         if (defined($poweroff_after_halt)) {
2322             sleep $poweroff_after_halt;
2323             run_command "$power_off";
2324         }
2325     } else {
2326         # nope? the zap it!
2327         run_command "$power_off";
2328     }
2329 }
2330
2331 sub success {
2332     my ($i) = @_;
2333
2334     if (defined($post_test)) {
2335         run_command $post_test;
2336     }
2337
2338     $successes++;
2339
2340     my $name = "";
2341
2342     if (defined($test_name)) {
2343         $name = " ($test_name)";
2344     }
2345
2346     doprint "\n\n*******************************************\n";
2347     doprint     "*******************************************\n";
2348     doprint     "KTEST RESULT: TEST $i$name SUCCESS!!!!         **\n";
2349     doprint     "*******************************************\n";
2350     doprint     "*******************************************\n";
2351
2352     if (defined($store_successes)) {
2353         save_logs "success", $store_successes;
2354     }
2355
2356     if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
2357         doprint "Reboot and wait $sleep_time seconds\n";
2358         reboot_to_good $sleep_time;
2359     }
2360 }
2361
2362 sub answer_bisect {
2363     for (;;) {
2364         doprint "Pass, fail, or skip? [p/f/s]";
2365         my $ans = <STDIN>;
2366         chomp $ans;
2367         if ($ans eq "p" || $ans eq "P") {
2368             return 1;
2369         } elsif ($ans eq "f" || $ans eq "F") {
2370             return 0;
2371         } elsif ($ans eq "s" || $ans eq "S") {
2372             return -1;
2373         } else {
2374             print "Please answer 'p', 'f', or 's'\n";
2375         }
2376     }
2377 }
2378
2379 sub child_run_test {
2380     my $failed = 0;
2381
2382     # child should have no power
2383     $reboot_on_error = 0;
2384     $poweroff_on_error = 0;
2385     $die_on_failure = 1;
2386
2387     run_command $run_test, $testlog or $failed = 1;
2388
2389     exit $failed;
2390 }
2391
2392 my $child_done;
2393
2394 sub child_finished {
2395     $child_done = 1;
2396 }
2397
2398 sub do_run_test {
2399     my $child_pid;
2400     my $child_exit;
2401     my $line;
2402     my $full_line;
2403     my $bug = 0;
2404     my $bug_ignored = 0;
2405
2406     wait_for_monitor 1;
2407
2408     doprint "run test $run_test\n";
2409
2410     $child_done = 0;
2411
2412     $SIG{CHLD} = qw(child_finished);
2413
2414     $child_pid = fork;
2415
2416     child_run_test if (!$child_pid);
2417
2418     $full_line = "";
2419
2420     do {
2421         $line = wait_for_input($monitor_fp, 1);
2422         if (defined($line)) {
2423
2424             # we are not guaranteed to get a full line
2425             $full_line .= $line;
2426             doprint $line;
2427
2428             if ($full_line =~ /call trace:/i) {
2429                 if ($ignore_errors) {
2430                     $bug_ignored = 1;
2431                 } else {
2432                     $bug = 1;
2433                 }
2434             }
2435
2436             if ($full_line =~ /Kernel panic -/) {
2437                 $bug = 1;
2438             }
2439
2440             if ($line =~ /\n/) {
2441                 $full_line = "";
2442             }
2443         }
2444     } while (!$child_done && !$bug);
2445
2446     if (!$bug && $bug_ignored) {
2447         doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
2448     }
2449
2450     if ($bug) {
2451         my $failure_start = time;
2452         my $now;
2453         do {
2454             $line = wait_for_input($monitor_fp, 1);
2455             if (defined($line)) {
2456                 doprint $line;
2457             }
2458             $now = time;
2459             if ($now - $failure_start >= $stop_after_failure) {
2460                 last;
2461             }
2462         } while (defined($line));
2463
2464         doprint "Detected kernel crash!\n";
2465         # kill the child with extreme prejudice
2466         kill 9, $child_pid;
2467     }
2468
2469     waitpid $child_pid, 0;
2470     $child_exit = $?;
2471
2472     if (!$bug && $in_bisect) {
2473         if (defined($bisect_ret_good)) {
2474             if ($child_exit == $bisect_ret_good) {
2475                 return 1;
2476             }
2477         }
2478         if (defined($bisect_ret_skip)) {
2479             if ($child_exit == $bisect_ret_skip) {
2480                 return -1;
2481             }
2482         }
2483         if (defined($bisect_ret_abort)) {
2484             if ($child_exit == $bisect_ret_abort) {
2485                 fail "test abort" and return -2;
2486             }
2487         }
2488         if (defined($bisect_ret_bad)) {
2489             if ($child_exit == $bisect_ret_skip) {
2490                 return 0;
2491             }
2492         }
2493         if (defined($bisect_ret_default)) {
2494             if ($bisect_ret_default eq "good") {
2495                 return 1;
2496             } elsif ($bisect_ret_default eq "bad") {
2497                 return 0;
2498             } elsif ($bisect_ret_default eq "skip") {
2499                 return -1;
2500             } elsif ($bisect_ret_default eq "abort") {
2501                 return -2;
2502             } else {
2503                 fail "unknown default action: $bisect_ret_default"
2504                     and return -2;
2505             }
2506         }
2507     }
2508
2509     if ($bug || $child_exit) {
2510         return 0 if $in_bisect;
2511         fail "test failed" and return 0;
2512     }
2513     return 1;
2514 }
2515
2516 sub run_git_bisect {
2517     my ($command) = @_;
2518
2519     doprint "$command ... ";
2520
2521     my $output = `$command 2>&1`;
2522     my $ret = $?;
2523
2524     logit $output;
2525
2526     if ($ret) {
2527         doprint "FAILED\n";
2528         dodie "Failed to git bisect";
2529     }
2530
2531     doprint "SUCCESS\n";
2532     if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2533         doprint "$1 [$2]\n";
2534     } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
2535         $bisect_bad_commit = $1;
2536         doprint "Found bad commit... $1\n";
2537         return 0;
2538     } else {
2539         # we already logged it, just print it now.
2540         print $output;
2541     }
2542
2543     return 1;
2544 }
2545
2546 sub bisect_reboot {
2547     doprint "Reboot and sleep $bisect_sleep_time seconds\n";
2548     reboot_to_good $bisect_sleep_time;
2549 }
2550
2551 # returns 1 on success, 0 on failure, -1 on skip
2552 sub run_bisect_test {
2553     my ($type, $buildtype) = @_;
2554
2555     my $failed = 0;
2556     my $result;
2557     my $output;
2558     my $ret;
2559
2560     $in_bisect = 1;
2561
2562     build $buildtype or $failed = 1;
2563
2564     if ($type ne "build") {
2565         if ($failed && $bisect_skip) {
2566             $in_bisect = 0;
2567             return -1;
2568         }
2569         dodie "Failed on build" if $failed;
2570
2571         # Now boot the box
2572         start_monitor_and_boot or $failed = 1;
2573
2574         if ($type ne "boot") {
2575             if ($failed && $bisect_skip) {
2576                 end_monitor;
2577                 bisect_reboot;
2578                 $in_bisect = 0;
2579                 return -1;
2580             }
2581             dodie "Failed on boot" if $failed;
2582
2583             do_run_test or $failed = 1;
2584         }
2585         end_monitor;
2586     }
2587
2588     if ($failed) {
2589         $result = 0;
2590     } else {
2591         $result = 1;
2592     }
2593
2594     # reboot the box to a kernel we can ssh to
2595     if ($type ne "build") {
2596         bisect_reboot;
2597     }
2598     $in_bisect = 0;
2599
2600     return $result;
2601 }
2602
2603 sub run_bisect {
2604     my ($type) = @_;
2605     my $buildtype = "oldconfig";
2606
2607     # We should have a minconfig to use?
2608     if (defined($minconfig)) {
2609         $buildtype = "useconfig:$minconfig";
2610     }
2611
2612     # If the user sets bisect_tries to less than 1, then no tries
2613     # is a success.
2614     my $ret = 1;
2615
2616     # Still let the user manually decide that though.
2617     if ($bisect_tries < 1 && $bisect_manual) {
2618         $ret = answer_bisect;
2619     }
2620
2621     for (my $i = 0; $i < $bisect_tries; $i++) {
2622         if ($bisect_tries > 1) {
2623             my $t = $i + 1;
2624             doprint("Running bisect trial $t of $bisect_tries:\n");
2625         }
2626         $ret = run_bisect_test $type, $buildtype;
2627
2628         if ($bisect_manual) {
2629             $ret = answer_bisect;
2630         }
2631
2632         last if (!$ret);
2633     }
2634
2635     # Are we looking for where it worked, not failed?
2636     if ($reverse_bisect && $ret >= 0) {
2637         $ret = !$ret;
2638     }
2639
2640     if ($ret > 0) {
2641         return "good";
2642     } elsif ($ret == 0) {
2643         return  "bad";
2644     } elsif ($bisect_skip) {
2645         doprint "HIT A BAD COMMIT ... SKIPPING\n";
2646         return "skip";
2647     }
2648 }
2649
2650 sub update_bisect_replay {
2651     my $tmp_log = "$tmpdir/ktest_bisect_log";
2652     run_command "git bisect log > $tmp_log" or
2653         die "can't create bisect log";
2654     return $tmp_log;
2655 }
2656
2657 sub bisect {
2658     my ($i) = @_;
2659
2660     my $result;
2661
2662     die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2663     die "BISECT_BAD[$i] not defined\n"  if (!defined($bisect_bad));
2664     die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
2665
2666     my $good = $bisect_good;
2667     my $bad = $bisect_bad;
2668     my $type = $bisect_type;
2669     my $start = $bisect_start;
2670     my $replay = $bisect_replay;
2671     my $start_files = $bisect_files;
2672
2673     if (defined($start_files)) {
2674         $start_files = " -- " . $start_files;
2675     } else {
2676         $start_files = "";
2677     }
2678
2679     # convert to true sha1's
2680     $good = get_sha1($good);
2681     $bad = get_sha1($bad);
2682
2683     if (defined($bisect_reverse) && $bisect_reverse == 1) {
2684         doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2685         $reverse_bisect = 1;
2686     } else {
2687         $reverse_bisect = 0;
2688     }
2689
2690     # Can't have a test without having a test to run
2691     if ($type eq "test" && !defined($run_test)) {
2692         $type = "boot";
2693     }
2694
2695     # Check if a bisect was running
2696     my $bisect_start_file = "$builddir/.git/BISECT_START";
2697
2698     my $check = $bisect_check;
2699     my $do_check = defined($check) && $check ne "0";
2700
2701     if ( -f $bisect_start_file ) {
2702         print "Bisect in progress found\n";
2703         if ($do_check) {
2704             print " If you say yes, then no checks of good or bad will be done\n";
2705         }
2706         if (defined($replay)) {
2707             print "** BISECT_REPLAY is defined in config file **";
2708             print " Ignore config option and perform new git bisect log?\n";
2709             if (read_ync " (yes, no, or cancel) ") {
2710                 $replay = update_bisect_replay;
2711                 $do_check = 0;
2712             }
2713         } elsif (read_yn "read git log and continue?") {
2714             $replay = update_bisect_replay;
2715             $do_check = 0;
2716         }
2717     }
2718
2719     if ($do_check) {
2720
2721         # get current HEAD
2722         my $head = get_sha1("HEAD");
2723
2724         if ($check ne "good") {
2725             doprint "TESTING BISECT BAD [$bad]\n";
2726             run_command "git checkout $bad" or
2727                 die "Failed to checkout $bad";
2728
2729             $result = run_bisect $type;
2730
2731             if ($result ne "bad") {
2732                 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2733             }
2734         }
2735
2736         if ($check ne "bad") {
2737             doprint "TESTING BISECT GOOD [$good]\n";
2738             run_command "git checkout $good" or
2739                 die "Failed to checkout $good";
2740
2741             $result = run_bisect $type;
2742
2743             if ($result ne "good") {
2744                 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2745             }
2746         }
2747
2748         # checkout where we started
2749         run_command "git checkout $head" or
2750             die "Failed to checkout $head";
2751     }
2752
2753     run_command "git bisect start$start_files" or
2754         dodie "could not start bisect";
2755
2756     if (defined($replay)) {
2757         run_command "git bisect replay $replay" or
2758             dodie "failed to run replay";
2759     } else {
2760
2761         run_command "git bisect good $good" or
2762             dodie "could not set bisect good to $good";
2763
2764         run_git_bisect "git bisect bad $bad" or
2765             dodie "could not set bisect bad to $bad";
2766
2767     }
2768
2769     if (defined($start)) {
2770         run_command "git checkout $start" or
2771             dodie "failed to checkout $start";
2772     }
2773
2774     my $test;
2775     do {
2776         $result = run_bisect $type;
2777         $test = run_git_bisect "git bisect $result";
2778     } while ($test);
2779
2780     run_command "git bisect log" or
2781         dodie "could not capture git bisect log";
2782
2783     run_command "git bisect reset" or
2784         dodie "could not reset git bisect";
2785
2786     doprint "Bad commit was [$bisect_bad_commit]\n";
2787
2788     success $i;
2789 }
2790
2791 # config_ignore holds the configs that were set (or unset) for
2792 # a good config and we will ignore these configs for the rest
2793 # of a config bisect. These configs stay as they were.
2794 my %config_ignore;
2795
2796 # config_set holds what all configs were set as.
2797 my %config_set;
2798
2799 # config_off holds the set of configs that the bad config had disabled.
2800 # We need to record them and set them in the .config when running
2801 # olddefconfig, because olddefconfig keeps the defaults.
2802 my %config_off;
2803
2804 # config_off_tmp holds a set of configs to turn off for now
2805 my @config_off_tmp;
2806
2807 # config_list is the set of configs that are being tested
2808 my %config_list;
2809 my %null_config;
2810
2811 my %dependency;
2812
2813 sub assign_configs {
2814     my ($hash, $config) = @_;
2815
2816     doprint "Reading configs from $config\n";
2817
2818     open (IN, $config)
2819         or dodie "Failed to read $config";
2820
2821     while (<IN>) {
2822         chomp;
2823         if (/^((CONFIG\S*)=.*)/) {
2824             ${$hash}{$2} = $1;
2825         } elsif (/^(# (CONFIG\S*) is not set)/) {
2826             ${$hash}{$2} = $1;
2827         }
2828     }
2829
2830     close(IN);
2831 }
2832
2833 sub process_config_ignore {
2834     my ($config) = @_;
2835
2836     assign_configs \%config_ignore, $config;
2837 }
2838
2839 sub get_dependencies {
2840     my ($config) = @_;
2841
2842     my $arr = $dependency{$config};
2843     if (!defined($arr)) {
2844         return ();
2845     }
2846
2847     my @deps = @{$arr};
2848
2849     foreach my $dep (@{$arr}) {
2850         print "ADD DEP $dep\n";
2851         @deps = (@deps, get_dependencies $dep);
2852     }
2853
2854     return @deps;
2855 }
2856
2857 sub save_config {
2858     my ($pc, $file) = @_;
2859
2860     my %configs = %{$pc};
2861
2862     doprint "Saving configs into $file\n";
2863
2864     open(OUT, ">$file") or dodie "Can not write to $file";
2865
2866     foreach my $config (keys %configs) {
2867         print OUT "$configs{$config}\n";
2868     }
2869     close(OUT);
2870 }
2871
2872 sub create_config {
2873     my ($name, $pc) = @_;
2874
2875     doprint "Creating old config from $name configs\n";
2876
2877     save_config $pc, $output_config;
2878
2879     make_oldconfig;
2880 }
2881
2882 # compare two config hashes, and return configs with different vals.
2883 # It returns B's config values, but you can use A to see what A was.
2884 sub diff_config_vals {
2885     my ($pa, $pb) = @_;
2886
2887     # crappy Perl way to pass in hashes.
2888     my %a = %{$pa};
2889     my %b = %{$pb};
2890
2891     my %ret;
2892
2893     foreach my $item (keys %a) {
2894         if (defined($b{$item}) && $b{$item} ne $a{$item}) {
2895             $ret{$item} = $b{$item};
2896         }
2897     }
2898
2899     return %ret;
2900 }
2901
2902 # compare two config hashes and return the configs in B but not A
2903 sub diff_configs {
2904     my ($pa, $pb) = @_;
2905
2906     my %ret;
2907
2908     # crappy Perl way to pass in hashes.
2909     my %a = %{$pa};
2910     my %b = %{$pb};
2911
2912     foreach my $item (keys %b) {
2913         if (!defined($a{$item})) {
2914             $ret{$item} = $b{$item};
2915         }
2916     }
2917
2918     return %ret;
2919 }
2920
2921 # return if two configs are equal or not
2922 # 0 is equal +1 b has something a does not
2923 # +1 if a and b have a different item.
2924 # -1 if a has something b does not
2925 sub compare_configs {
2926     my ($pa, $pb) = @_;
2927
2928     my %ret;
2929
2930     # crappy Perl way to pass in hashes.
2931     my %a = %{$pa};
2932     my %b = %{$pb};
2933
2934     foreach my $item (keys %b) {
2935         if (!defined($a{$item})) {
2936             return 1;
2937         }
2938         if ($a{$item} ne $b{$item}) {
2939             return 1;
2940         }
2941     }
2942
2943     foreach my $item (keys %a) {
2944         if (!defined($b{$item})) {
2945             return -1;
2946         }
2947     }
2948
2949     return 0;
2950 }
2951
2952 sub run_config_bisect_test {
2953     my ($type) = @_;
2954
2955     my $ret = run_bisect_test $type, "oldconfig";
2956
2957     if ($bisect_manual) {
2958         $ret = answer_bisect;
2959     }
2960
2961     return $ret;
2962 }
2963
2964 sub process_failed {
2965     my ($config) = @_;
2966
2967     doprint "\n\n***************************************\n";
2968     doprint "Found bad config: $config\n";
2969     doprint "***************************************\n\n";
2970 }
2971
2972 # used for config bisecting
2973 my $good_config;
2974 my $bad_config;
2975
2976 sub process_new_config {
2977     my ($tc, $nc, $gc, $bc) = @_;
2978
2979     my %tmp_config = %{$tc};
2980     my %good_configs = %{$gc};
2981     my %bad_configs = %{$bc};
2982
2983     my %new_configs;
2984
2985     my $runtest = 1;
2986     my $ret;
2987
2988     create_config "tmp_configs", \%tmp_config;
2989     assign_configs \%new_configs, $output_config;
2990
2991     $ret = compare_configs \%new_configs, \%bad_configs;
2992     if (!$ret) {
2993         doprint "New config equals bad config, try next test\n";
2994         $runtest = 0;
2995     }
2996
2997     if ($runtest) {
2998         $ret = compare_configs \%new_configs, \%good_configs;
2999         if (!$ret) {
3000             doprint "New config equals good config, try next test\n";
3001             $runtest = 0;
3002         }
3003     }
3004
3005     %{$nc} = %new_configs;
3006
3007     return $runtest;
3008 }
3009
3010 sub run_config_bisect {
3011     my ($pgood, $pbad) = @_;
3012
3013     my $type = $config_bisect_type;
3014
3015     my %good_configs = %{$pgood};
3016     my %bad_configs = %{$pbad};
3017
3018     my %diff_configs = diff_config_vals \%good_configs, \%bad_configs;
3019     my %b_configs = diff_configs \%good_configs, \%bad_configs;
3020     my %g_configs = diff_configs \%bad_configs, \%good_configs;
3021
3022     my @diff_arr = keys %diff_configs;
3023     my $len_diff = $#diff_arr + 1;
3024
3025     my @b_arr = keys %b_configs;
3026     my $len_b = $#b_arr + 1;
3027
3028     my @g_arr = keys %g_configs;
3029     my $len_g = $#g_arr + 1;
3030
3031     my $runtest = 1;
3032     my %new_configs;
3033     my $ret;
3034
3035     # First, lets get it down to a single subset.
3036     # Is the problem with a difference in values?
3037     # Is the problem with a missing config?
3038     # Is the problem with a config that breaks things?
3039
3040     # Enable all of one set and see if we get a new bad
3041     # or good config.
3042
3043     # first set the good config to the bad values.
3044
3045     doprint "d=$len_diff g=$len_g b=$len_b\n";
3046
3047     # first lets enable things in bad config that are enabled in good config
3048
3049     if ($len_diff > 0) {
3050         if ($len_b > 0 || $len_g > 0) {
3051             my %tmp_config = %bad_configs;
3052
3053             doprint "Set tmp config to be bad config with good config values\n";
3054             foreach my $item (@diff_arr) {
3055                 $tmp_config{$item} = $good_configs{$item};
3056             }
3057
3058             $runtest = process_new_config \%tmp_config, \%new_configs,
3059                             \%good_configs, \%bad_configs;
3060         }
3061     }
3062
3063     if (!$runtest && $len_diff > 0) {
3064
3065         if ($len_diff == 1) {
3066             process_failed $diff_arr[0];
3067             return 1;
3068         }
3069         my %tmp_config = %bad_configs;
3070
3071         my $half = int($#diff_arr / 2);
3072         my @tophalf = @diff_arr[0 .. $half];
3073
3074         doprint "Settings bisect with top half:\n";
3075         doprint "Set tmp config to be bad config with some good config values\n";
3076         foreach my $item (@tophalf) {
3077             $tmp_config{$item} = $good_configs{$item};
3078         }
3079
3080         $runtest = process_new_config \%tmp_config, \%new_configs,
3081                             \%good_configs, \%bad_configs;
3082
3083         if (!$runtest) {
3084             my %tmp_config = %bad_configs;
3085
3086             doprint "Try bottom half\n";
3087
3088             my @bottomhalf = @diff_arr[$half+1 .. $#diff_arr];
3089
3090             foreach my $item (@bottomhalf) {
3091                 $tmp_config{$item} = $good_configs{$item};
3092             }
3093
3094             $runtest = process_new_config \%tmp_config, \%new_configs,
3095                             \%good_configs, \%bad_configs;
3096         }
3097     }
3098
3099     if ($runtest) {
3100         $ret = run_config_bisect_test $type;
3101         if ($ret) {
3102             doprint "NEW GOOD CONFIG\n";
3103             %good_configs = %new_configs;
3104             run_command "mv $good_config ${good_config}.last";
3105             save_config \%good_configs, $good_config;
3106             %{$pgood} = %good_configs;
3107         } else {
3108             doprint "NEW BAD CONFIG\n";
3109             %bad_configs = %new_configs;
3110             run_command "mv $bad_config ${bad_config}.last";
3111             save_config \%bad_configs, $bad_config;
3112             %{$pbad} = %bad_configs;
3113         }
3114         return 0;
3115     }
3116
3117     fail "Hmm, need to do a mix match?\n";
3118     return -1;
3119 }
3120
3121 sub config_bisect {
3122     my ($i) = @_;
3123
3124     my $type = $config_bisect_type;
3125     my $ret;
3126
3127     $bad_config = $config_bisect;
3128
3129     if (defined($config_bisect_good)) {
3130         $good_config = $config_bisect_good;
3131     } elsif (defined($minconfig)) {
3132         $good_config = $minconfig;
3133     } else {
3134         doprint "No config specified, checking if defconfig works";
3135         $ret = run_bisect_test $type, "defconfig";
3136         if (!$ret) {
3137             fail "Have no good config to compare with, please set CONFIG_BISECT_GOOD";
3138             return 1;
3139         }
3140         $good_config = $output_config;
3141     }
3142
3143     # we don't want min configs to cause issues here.
3144     doprint "Disabling 'MIN_CONFIG' for this test\n";
3145     undef $minconfig;
3146
3147     my %good_configs;
3148     my %bad_configs;
3149     my %tmp_configs;
3150
3151     doprint "Run good configs through make oldconfig\n";
3152     assign_configs \%tmp_configs, $good_config;
3153     create_config "$good_config", \%tmp_configs;
3154     assign_configs \%good_configs, $output_config;
3155
3156     doprint "Run bad configs through make oldconfig\n";
3157     assign_configs \%tmp_configs, $bad_config;
3158     create_config "$bad_config", \%tmp_configs;
3159     assign_configs \%bad_configs, $output_config;
3160
3161     $good_config = "$tmpdir/good_config";
3162     $bad_config = "$tmpdir/bad_config";
3163
3164     save_config \%good_configs, $good_config;
3165     save_config \%bad_configs, $bad_config;
3166
3167
3168     if (defined($config_bisect_check) && $config_bisect_check ne "0") {
3169         if ($config_bisect_check ne "good") {
3170             doprint "Testing bad config\n";
3171
3172             $ret = run_bisect_test $type, "useconfig:$bad_config";
3173             if ($ret) {
3174                 fail "Bad config succeeded when expected to fail!";
3175                 return 0;
3176             }
3177         }
3178         if ($config_bisect_check ne "bad") {
3179             doprint "Testing good config\n";
3180
3181             $ret = run_bisect_test $type, "useconfig:$good_config";
3182             if (!$ret) {
3183                 fail "Good config failed when expected to succeed!";
3184                 return 0;
3185             }
3186         }
3187     }
3188
3189     do {
3190         $ret = run_config_bisect \%good_configs, \%bad_configs;
3191     } while (!$ret);
3192
3193     return $ret if ($ret < 0);
3194
3195     success $i;
3196 }
3197
3198 sub patchcheck_reboot {
3199     doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
3200     reboot_to_good $patchcheck_sleep_time;
3201 }
3202
3203 sub patchcheck {
3204     my ($i) = @_;
3205
3206     die "PATCHCHECK_START[$i] not defined\n"
3207         if (!defined($patchcheck_start));
3208     die "PATCHCHECK_TYPE[$i] not defined\n"
3209         if (!defined($patchcheck_type));
3210
3211     my $start = $patchcheck_start;
3212
3213     my $cherry = $patchcheck_cherry;
3214     if (!defined($cherry)) {
3215         $cherry = 0;
3216     }
3217
3218     my $end = "HEAD";
3219     if (defined($patchcheck_end)) {
3220         $end = $patchcheck_end;
3221     } elsif ($cherry) {
3222         die "PATCHCHECK_END must be defined with PATCHCHECK_CHERRY\n";
3223     }
3224
3225     # Get the true sha1's since we can use things like HEAD~3
3226     $start = get_sha1($start);
3227     $end = get_sha1($end);
3228
3229     my $type = $patchcheck_type;
3230
3231     # Can't have a test without having a test to run
3232     if ($type eq "test" && !defined($run_test)) {
3233         $type = "boot";
3234     }
3235
3236     if ($cherry) {
3237         open (IN, "git cherry -v $start $end|") or
3238             dodie "could not get git list";
3239     } else {
3240         open (IN, "git log --pretty=oneline $end|") or
3241             dodie "could not get git list";
3242     }
3243
3244     my @list;
3245
3246     while (<IN>) {
3247         chomp;
3248         # git cherry adds a '+' we want to remove
3249         s/^\+ //;
3250         $list[$#list+1] = $_;
3251         last if (/^$start/);
3252     }
3253     close(IN);
3254
3255     if (!$cherry) {
3256         if ($list[$#list] !~ /^$start/) {
3257             fail "SHA1 $start not found";
3258         }
3259
3260         # go backwards in the list
3261         @list = reverse @list;
3262     }
3263
3264     doprint("Going to test the following commits:\n");
3265     foreach my $l (@list) {
3266         doprint "$l\n";
3267     }
3268
3269     my $save_clean = $noclean;
3270     my %ignored_warnings;
3271
3272     if (defined($ignore_warnings)) {
3273         foreach my $sha1 (split /\s+/, $ignore_warnings) {
3274             $ignored_warnings{$sha1} = 1;
3275         }
3276     }
3277
3278     $in_patchcheck = 1;
3279     foreach my $item (@list) {
3280         my $sha1 = $item;
3281         $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
3282
3283         doprint "\nProcessing commit $item\n\n";
3284
3285         run_command "git checkout $sha1" or
3286             die "Failed to checkout $sha1";
3287
3288         # only clean on the first and last patch
3289         if ($item eq $list[0] ||
3290             $item eq $list[$#list]) {
3291             $noclean = $save_clean;
3292         } else {
3293             $noclean = 1;
3294         }
3295
3296         if (defined($minconfig)) {
3297             build "useconfig:$minconfig" or return 0;
3298         } else {
3299             # ?? no config to use?
3300             build "oldconfig" or return 0;
3301         }
3302
3303         # No need to do per patch checking if warnings file exists
3304         if (!defined($warnings_file) && !defined($ignored_warnings{$sha1})) {
3305             check_patch_buildlog $sha1 or return 0;
3306         }
3307
3308         check_buildlog or return 0;
3309
3310         next if ($type eq "build");
3311
3312         my $failed = 0;
3313
3314         start_monitor_and_boot or $failed = 1;
3315
3316         if (!$failed && $type ne "boot"){
3317             do_run_test or $failed = 1;
3318         }
3319         end_monitor;
3320         return 0 if ($failed);
3321
3322         patchcheck_reboot;
3323
3324     }
3325     $in_patchcheck = 0;
3326     success $i;
3327
3328     return 1;
3329 }
3330
3331 my %depends;
3332 my %depcount;
3333 my $iflevel = 0;
3334 my @ifdeps;
3335
3336 # prevent recursion
3337 my %read_kconfigs;
3338
3339 sub add_dep {
3340     # $config depends on $dep
3341     my ($config, $dep) = @_;
3342
3343     if (defined($depends{$config})) {
3344         $depends{$config} .= " " . $dep;
3345     } else {
3346         $depends{$config} = $dep;
3347     }
3348
3349     # record the number of configs depending on $dep
3350     if (defined $depcount{$dep}) {
3351         $depcount{$dep}++;
3352     } else {
3353         $depcount{$dep} = 1;
3354     } 
3355 }
3356
3357 # taken from streamline_config.pl
3358 sub read_kconfig {
3359     my ($kconfig) = @_;
3360
3361     my $state = "NONE";
3362     my $config;
3363     my @kconfigs;
3364
3365     my $cont = 0;
3366     my $line;
3367
3368
3369     if (! -f $kconfig) {
3370         doprint "file $kconfig does not exist, skipping\n";
3371         return;
3372     }
3373
3374     open(KIN, "$kconfig")
3375         or die "Can't open $kconfig";
3376     while (<KIN>) {
3377         chomp;
3378
3379         # Make sure that lines ending with \ continue
3380         if ($cont) {
3381             $_ = $line . " " . $_;
3382         }
3383
3384         if (s/\\$//) {
3385             $cont = 1;
3386             $line = $_;
3387             next;
3388         }
3389
3390         $cont = 0;
3391
3392         # collect any Kconfig sources
3393         if (/^source\s*"(.*)"/) {
3394             $kconfigs[$#kconfigs+1] = $1;
3395         }
3396
3397         # configs found
3398         if (/^\s*(menu)?config\s+(\S+)\s*$/) {
3399             $state = "NEW";
3400             $config = $2;
3401
3402             for (my $i = 0; $i < $iflevel; $i++) {
3403                 add_dep $config, $ifdeps[$i];
3404             }
3405
3406         # collect the depends for the config
3407         } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
3408
3409             add_dep $config, $1;
3410
3411         # Get the configs that select this config
3412         } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
3413
3414             # selected by depends on config
3415             add_dep $1, $config;
3416
3417         # Check for if statements
3418         } elsif (/^if\s+(.*\S)\s*$/) {
3419             my $deps = $1;
3420             # remove beginning and ending non text
3421             $deps =~ s/^[^a-zA-Z0-9_]*//;
3422             $deps =~ s/[^a-zA-Z0-9_]*$//;
3423
3424             my @deps = split /[^a-zA-Z0-9_]+/, $deps;
3425
3426             $ifdeps[$iflevel++] = join ':', @deps;
3427
3428         } elsif (/^endif/) {
3429
3430             $iflevel-- if ($iflevel);
3431
3432         # stop on "help"
3433         } elsif (/^\s*help\s*$/) {
3434             $state = "NONE";
3435         }
3436     }
3437     close(KIN);
3438
3439     # read in any configs that were found.
3440     foreach $kconfig (@kconfigs) {
3441         if (!defined($read_kconfigs{$kconfig})) {
3442             $read_kconfigs{$kconfig} = 1;
3443             read_kconfig("$builddir/$kconfig");
3444         }
3445     }
3446 }
3447
3448 sub read_depends {
3449     # find out which arch this is by the kconfig file
3450     open (IN, $output_config)
3451         or dodie "Failed to read $output_config";
3452     my $arch;
3453     while (<IN>) {
3454         if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
3455             $arch = $1;
3456             last;
3457         }
3458     }
3459     close IN;
3460
3461     if (!defined($arch)) {
3462         doprint "Could not find arch from config file\n";
3463         doprint "no dependencies used\n";
3464         return;
3465     }
3466
3467     # arch is really the subarch, we need to know
3468     # what directory to look at.
3469     if ($arch eq "i386" || $arch eq "x86_64") {
3470         $arch = "x86";
3471     } elsif ($arch =~ /^tile/) {
3472         $arch = "tile";
3473     }
3474
3475     my $kconfig = "$builddir/arch/$arch/Kconfig";
3476
3477     if (! -f $kconfig && $arch =~ /\d$/) {
3478         my $orig = $arch;
3479         # some subarchs have numbers, truncate them
3480         $arch =~ s/\d*$//;
3481         $kconfig = "$builddir/arch/$arch/Kconfig";
3482         if (! -f $kconfig) {
3483             doprint "No idea what arch dir $orig is for\n";
3484             doprint "no dependencies used\n";
3485             return;
3486         }
3487     }
3488
3489     read_kconfig($kconfig);
3490 }
3491
3492 sub make_new_config {
3493     my @configs = @_;
3494
3495     open (OUT, ">$output_config")
3496         or dodie "Failed to write $output_config";
3497
3498     foreach my $config (@configs) {
3499         print OUT "$config\n";
3500     }
3501     close OUT;
3502 }
3503
3504 sub chomp_config {
3505     my ($config) = @_;
3506
3507     $config =~ s/CONFIG_//;
3508
3509     return $config;
3510 }
3511
3512 sub get_depends {
3513     my ($dep) = @_;
3514
3515     my $kconfig = chomp_config $dep;
3516
3517     $dep = $depends{"$kconfig"};
3518
3519     # the dep string we have saves the dependencies as they
3520     # were found, including expressions like ! && ||. We
3521     # want to split this out into just an array of configs.
3522
3523     my $valid = "A-Za-z_0-9";
3524
3525     my @configs;
3526
3527     while ($dep =~ /[$valid]/) {
3528
3529         if ($dep =~ /^[^$valid]*([$valid]+)/) {
3530             my $conf = "CONFIG_" . $1;
3531
3532             $configs[$#configs + 1] = $conf;
3533
3534             $dep =~ s/^[^$valid]*[$valid]+//;
3535         } else {
3536             die "this should never happen";
3537         }
3538     }
3539
3540     return @configs;
3541 }
3542
3543 my %min_configs;
3544 my %keep_configs;
3545 my %save_configs;
3546 my %processed_configs;
3547 my %nochange_config;
3548
3549 sub test_this_config {
3550     my ($config) = @_;
3551
3552     my $found;
3553
3554     # if we already processed this config, skip it
3555     if (defined($processed_configs{$config})) {
3556         return undef;
3557     }
3558     $processed_configs{$config} = 1;
3559
3560     # if this config failed during this round, skip it
3561     if (defined($nochange_config{$config})) {
3562         return undef;
3563     }
3564
3565     my $kconfig = chomp_config $config;
3566
3567     # Test dependencies first
3568     if (defined($depends{"$kconfig"})) {
3569         my @parents = get_depends $config;
3570         foreach my $parent (@parents) {
3571             # if the parent is in the min config, check it first
3572             next if (!defined($min_configs{$parent}));
3573             $found = test_this_config($parent);
3574             if (defined($found)) {
3575                 return $found;
3576             }
3577         }
3578     }
3579
3580     # Remove this config from the list of configs
3581     # do a make olddefconfig and then read the resulting
3582     # .config to make sure it is missing the config that
3583     # we had before
3584     my %configs = %min_configs;
3585     delete $configs{$config};
3586     make_new_config ((values %configs), (values %keep_configs));
3587     make_oldconfig;
3588     undef %configs;
3589     assign_configs \%configs, $output_config;
3590
3591     if (!defined($configs{$config}) || $configs{$config} =~ /^#/) {
3592         return $config;
3593     }
3594
3595     doprint "disabling config $config did not change .config\n";
3596
3597     $nochange_config{$config} = 1;
3598
3599     return undef;
3600 }
3601
3602 sub make_min_config {
3603     my ($i) = @_;
3604
3605     my $type = $minconfig_type;
3606     if ($type ne "boot" && $type ne "test") {
3607         fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3608             " make_min_config works only with 'boot' and 'test'\n" and return;
3609     }
3610
3611     if (!defined($output_minconfig)) {
3612         fail "OUTPUT_MIN_CONFIG not defined" and return;
3613     }
3614
3615     # If output_minconfig exists, and the start_minconfig
3616     # came from min_config, than ask if we should use
3617     # that instead.
3618     if (-f $output_minconfig && !$start_minconfig_defined) {
3619         print "$output_minconfig exists\n";
3620         if (!defined($use_output_minconfig)) {
3621             if (read_yn " Use it as minconfig?") {
3622                 $start_minconfig = $output_minconfig;
3623             }
3624         } elsif ($use_output_minconfig > 0) {
3625             doprint "Using $output_minconfig as MIN_CONFIG\n";
3626             $start_minconfig = $output_minconfig;
3627         } else {
3628             doprint "Set to still use MIN_CONFIG as starting point\n";
3629         }
3630     }
3631
3632     if (!defined($start_minconfig)) {
3633         fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3634     }
3635
3636     my $temp_config = "$tmpdir/temp_config";
3637
3638     # First things first. We build an allnoconfig to find
3639     # out what the defaults are that we can't touch.
3640     # Some are selections, but we really can't handle selections.
3641
3642     my $save_minconfig = $minconfig;
3643     undef $minconfig;
3644
3645     run_command "$make allnoconfig" or return 0;
3646
3647     read_depends;
3648
3649     process_config_ignore $output_config;
3650
3651     undef %save_configs;
3652     undef %min_configs;
3653
3654     if (defined($ignore_config)) {
3655         # make sure the file exists
3656         `touch $ignore_config`;
3657         assign_configs \%save_configs, $ignore_config;
3658     }
3659
3660     %keep_configs = %save_configs;
3661
3662     doprint "Load initial configs from $start_minconfig\n";
3663
3664     # Look at the current min configs, and save off all the
3665     # ones that were set via the allnoconfig
3666     assign_configs \%min_configs, $start_minconfig;
3667
3668     my @config_keys = keys %min_configs;
3669
3670     # All configs need a depcount
3671     foreach my $config (@config_keys) {
3672         my $kconfig = chomp_config $config;
3673         if (!defined $depcount{$kconfig}) {
3674                 $depcount{$kconfig} = 0;
3675         }
3676     }
3677
3678     # Remove anything that was set by the make allnoconfig
3679     # we shouldn't need them as they get set for us anyway.
3680     foreach my $config (@config_keys) {
3681         # Remove anything in the ignore_config
3682         if (defined($keep_configs{$config})) {
3683             my $file = $ignore_config;
3684             $file =~ s,.*/(.*?)$,$1,;
3685             doprint "$config set by $file ... ignored\n";
3686             delete $min_configs{$config};
3687             next;
3688         }
3689         # But make sure the settings are the same. If a min config
3690         # sets a selection, we do not want to get rid of it if
3691         # it is not the same as what we have. Just move it into
3692         # the keep configs.
3693         if (defined($config_ignore{$config})) {
3694             if ($config_ignore{$config} ne $min_configs{$config}) {
3695                 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3696                 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3697                 $keep_configs{$config} = $min_configs{$config};
3698             } else {
3699                 doprint "$config set by allnoconfig ... ignored\n";
3700             }
3701             delete $min_configs{$config};
3702         }
3703     }
3704
3705     my $done = 0;
3706     my $take_two = 0;
3707
3708     while (!$done) {
3709
3710         my $config;
3711         my $found;
3712
3713         # Now disable each config one by one and do a make oldconfig
3714         # till we find a config that changes our list.
3715
3716         my @test_configs = keys %min_configs;
3717
3718         # Sort keys by who is most dependent on
3719         @test_configs = sort  { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3720                           @test_configs ;
3721
3722         # Put configs that did not modify the config at the end.
3723         my $reset = 1;
3724         for (my $i = 0; $i < $#test_configs; $i++) {
3725             if (!defined($nochange_config{$test_configs[0]})) {
3726                 $reset = 0;
3727                 last;
3728             }
3729             # This config didn't change the .config last time.
3730             # Place it at the end
3731             my $config = shift @test_configs;
3732             push @test_configs, $config;
3733         }
3734
3735         # if every test config has failed to modify the .config file
3736         # in the past, then reset and start over.
3737         if ($reset) {
3738             undef %nochange_config;
3739         }
3740
3741         undef %processed_configs;
3742
3743         foreach my $config (@test_configs) {
3744
3745             $found = test_this_config $config;
3746
3747             last if (defined($found));
3748
3749             # oh well, try another config
3750         }
3751
3752         if (!defined($found)) {
3753             # we could have failed due to the nochange_config hash
3754             # reset and try again
3755             if (!$take_two) {
3756                 undef %nochange_config;
3757                 $take_two = 1;
3758                 next;
3759             }
3760             doprint "No more configs found that we can disable\n";
3761             $done = 1;
3762             last;
3763         }
3764         $take_two = 0;
3765
3766         $config = $found;
3767
3768         doprint "Test with $config disabled\n";
3769
3770         # set in_bisect to keep build and monitor from dieing
3771         $in_bisect = 1;
3772
3773         my $failed = 0;
3774         build "oldconfig" or $failed = 1;
3775         if (!$failed) {
3776                 start_monitor_and_boot or $failed = 1;
3777
3778                 if ($type eq "test" && !$failed) {
3779                     do_run_test or $failed = 1;
3780                 }
3781
3782                 end_monitor;
3783         }
3784
3785         $in_bisect = 0;
3786
3787         if ($failed) {
3788             doprint "$min_configs{$config} is needed to boot the box... keeping\n";
3789             # this config is needed, add it to the ignore list.
3790             $keep_configs{$config} = $min_configs{$config};
3791             $save_configs{$config} = $min_configs{$config};
3792             delete $min_configs{$config};
3793
3794             # update new ignore configs
3795             if (defined($ignore_config)) {
3796                 open (OUT, ">$temp_config")
3797                     or die "Can't write to $temp_config";
3798                 foreach my $config (keys %save_configs) {
3799                     print OUT "$save_configs{$config}\n";
3800                 }
3801                 close OUT;
3802                 run_command "mv $temp_config $ignore_config" or
3803                     dodie "failed to copy update to $ignore_config";
3804             }
3805
3806         } else {
3807             # We booted without this config, remove it from the minconfigs.
3808             doprint "$config is not needed, disabling\n";
3809
3810             delete $min_configs{$config};
3811
3812             # Also disable anything that is not enabled in this config
3813             my %configs;
3814             assign_configs \%configs, $output_config;
3815             my @config_keys = keys %min_configs;
3816             foreach my $config (@config_keys) {
3817                 if (!defined($configs{$config})) {
3818                     doprint "$config is not set, disabling\n";
3819                     delete $min_configs{$config};
3820                 }
3821             }
3822
3823             # Save off all the current mandidory configs
3824             open (OUT, ">$temp_config")
3825                 or die "Can't write to $temp_config";
3826             foreach my $config (keys %keep_configs) {
3827                 print OUT "$keep_configs{$config}\n";
3828             }
3829             foreach my $config (keys %min_configs) {
3830                 print OUT "$min_configs{$config}\n";
3831             }
3832             close OUT;
3833
3834             run_command "mv $temp_config $output_minconfig" or
3835                 dodie "failed to copy update to $output_minconfig";
3836         }
3837
3838         doprint "Reboot and wait $sleep_time seconds\n";
3839         reboot_to_good $sleep_time;
3840     }
3841
3842     success $i;
3843     return 1;
3844 }
3845
3846 sub make_warnings_file {
3847     my ($i) = @_;
3848
3849     if (!defined($warnings_file)) {
3850         dodie "Must define WARNINGS_FILE for make_warnings_file test";
3851     }
3852
3853     if ($build_type eq "nobuild") {
3854         dodie "BUILD_TYPE can not be 'nobuild' for make_warnings_file test";
3855     }
3856
3857     build $build_type or dodie "Failed to build";
3858
3859     open(OUT, ">$warnings_file") or dodie "Can't create $warnings_file";
3860
3861     open(IN, $buildlog) or dodie "Can't open $buildlog";
3862     while (<IN>) {
3863
3864         # Some compilers use UTF-8 extended for quotes
3865         # for distcc heterogeneous systems, this causes issues
3866         s/$utf8_quote/'/g;
3867
3868         if (/$check_build_re/) {
3869             print OUT;
3870         }
3871     }
3872     close(IN);
3873
3874     close(OUT);
3875
3876     success $i;
3877 }
3878
3879 $#ARGV < 1 or die "ktest.pl version: $VERSION\n   usage: ktest.pl [config-file]\n";
3880
3881 if ($#ARGV == 0) {
3882     $ktest_config = $ARGV[0];
3883     if (! -f $ktest_config) {
3884         print "$ktest_config does not exist.\n";
3885         if (!read_yn "Create it?") {
3886             exit 0;
3887         }
3888     }
3889 }
3890
3891 if (! -f $ktest_config) {
3892     $newconfig = 1;
3893     get_test_case;
3894     open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3895     print OUT << "EOF"
3896 # Generated by ktest.pl
3897 #
3898
3899 # PWD is a ktest.pl variable that will result in the process working
3900 # directory that ktest.pl is executed in.
3901
3902 # THIS_DIR is automatically assigned the PWD of the path that generated
3903 # the config file. It is best to use this variable when assigning other
3904 # directory paths within this directory. This allows you to easily
3905 # move the test cases to other locations or to other machines.
3906 #
3907 THIS_DIR := $variable{"PWD"}
3908
3909 # Define each test with TEST_START
3910 # The config options below it will override the defaults
3911 TEST_START
3912 TEST_TYPE = $default{"TEST_TYPE"}
3913
3914 DEFAULTS
3915 EOF
3916 ;
3917     close(OUT);
3918 }
3919 read_config $ktest_config;
3920
3921 if (defined($opt{"LOG_FILE"})) {
3922     $opt{"LOG_FILE"} = eval_option("LOG_FILE", $opt{"LOG_FILE"}, -1);
3923 }
3924
3925 # Append any configs entered in manually to the config file.
3926 my @new_configs = keys %entered_configs;
3927 if ($#new_configs >= 0) {
3928     print "\nAppending entered in configs to $ktest_config\n";
3929     open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3930     foreach my $config (@new_configs) {
3931         print OUT "$config = $entered_configs{$config}\n";
3932         $opt{$config} = process_variables($entered_configs{$config});
3933     }
3934 }
3935
3936 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3937     unlink $opt{"LOG_FILE"};
3938 }
3939
3940 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3941
3942 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3943
3944     if (!$i) {
3945         doprint "DEFAULT OPTIONS:\n";
3946     } else {
3947         doprint "\nTEST $i OPTIONS";
3948         if (defined($repeat_tests{$i})) {
3949             $repeat = $repeat_tests{$i};
3950             doprint " ITERATE $repeat";
3951         }
3952         doprint "\n";
3953     }
3954
3955     foreach my $option (sort keys %opt) {
3956
3957         if ($option =~ /\[(\d+)\]$/) {
3958             next if ($i != $1);
3959         } else {
3960             next if ($i);
3961         }
3962
3963         doprint "$option = $opt{$option}\n";
3964     }
3965 }
3966
3967 sub option_defined {
3968     my ($option) = @_;
3969
3970     if (defined($opt{$option}) && $opt{$option} !~ /^\s*$/) {
3971         return 1;
3972     }
3973
3974     return 0;
3975 }
3976
3977 sub __set_test_option {
3978     my ($name, $i) = @_;
3979
3980     my $option = "$name\[$i\]";
3981
3982     if (option_defined($option)) {
3983         return $opt{$option};
3984     }
3985
3986     foreach my $test (keys %repeat_tests) {
3987         if ($i >= $test &&
3988             $i < $test + $repeat_tests{$test}) {
3989             $option = "$name\[$test\]";
3990             if (option_defined($option)) {
3991                 return $opt{$option};
3992             }
3993         }
3994     }
3995
3996     if (option_defined($name)) {
3997         return $opt{$name};
3998     }
3999
4000     return undef;
4001 }
4002
4003 sub set_test_option {
4004     my ($name, $i) = @_;
4005
4006     my $option = __set_test_option($name, $i);
4007     return $option if (!defined($option));
4008
4009     return eval_option($name, $option, $i);
4010 }
4011
4012 # First we need to do is the builds
4013 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
4014
4015     # Do not reboot on failing test options
4016     $no_reboot = 1;
4017     $reboot_success = 0;
4018
4019     $have_version = 0;
4020
4021     $iteration = $i;
4022
4023     undef %force_config;
4024
4025     my $makecmd = set_test_option("MAKE_CMD", $i);
4026
4027     $outputdir = set_test_option("OUTPUT_DIR", $i);
4028     $builddir = set_test_option("BUILD_DIR", $i);
4029
4030     chdir $builddir || die "can't change directory to $builddir";
4031
4032     if (!-d $outputdir) {
4033         mkpath($outputdir) or
4034             die "can't create $outputdir";
4035     }
4036
4037     $make = "$makecmd O=$outputdir";
4038
4039     # Load all the options into their mapped variable names
4040     foreach my $opt (keys %option_map) {
4041         ${$option_map{$opt}} = set_test_option($opt, $i);
4042     }
4043
4044     $start_minconfig_defined = 1;
4045
4046     # The first test may override the PRE_KTEST option
4047     if (defined($pre_ktest) && $i == 1) {
4048         doprint "\n";
4049         run_command $pre_ktest;
4050     }
4051
4052     # Any test can override the POST_KTEST option
4053     # The last test takes precedence.
4054     if (defined($post_ktest)) {
4055         $final_post_ktest = $post_ktest;
4056     }
4057
4058     if (!defined($start_minconfig)) {
4059         $start_minconfig_defined = 0;
4060         $start_minconfig = $minconfig;
4061     }
4062
4063     if (!-d $tmpdir) {
4064         mkpath($tmpdir) or
4065             die "can't create $tmpdir";
4066     }
4067
4068     $ENV{"SSH_USER"} = $ssh_user;
4069     $ENV{"MACHINE"} = $machine;
4070
4071     $buildlog = "$tmpdir/buildlog-$machine";
4072     $testlog = "$tmpdir/testlog-$machine";
4073     $dmesg = "$tmpdir/dmesg-$machine";
4074     $output_config = "$outputdir/.config";
4075
4076     if (!$buildonly) {
4077         $target = "$ssh_user\@$machine";
4078         if ($reboot_type eq "grub") {
4079             dodie "GRUB_MENU not defined" if (!defined($grub_menu));
4080         } elsif ($reboot_type eq "grub2") {
4081             dodie "GRUB_MENU not defined" if (!defined($grub_menu));
4082             dodie "GRUB_FILE not defined" if (!defined($grub_file));
4083         } elsif ($reboot_type eq "syslinux") {
4084             dodie "SYSLINUX_LABEL not defined" if (!defined($syslinux_label));
4085         }
4086     }
4087
4088     my $run_type = $build_type;
4089     if ($test_type eq "patchcheck") {
4090         $run_type = $patchcheck_type;
4091     } elsif ($test_type eq "bisect") {
4092         $run_type = $bisect_type;
4093     } elsif ($test_type eq "config_bisect") {
4094         $run_type = $config_bisect_type;
4095     } elsif ($test_type eq "make_min_config") {
4096         $run_type = "";
4097     } elsif ($test_type eq "make_warnings_file") {
4098         $run_type = "";
4099     }
4100
4101     # mistake in config file?
4102     if (!defined($run_type)) {
4103         $run_type = "ERROR";
4104     }
4105
4106     my $installme = "";
4107     $installme = " no_install" if ($no_install);
4108
4109     my $name = "";
4110
4111     if (defined($test_name)) {
4112         $name = " ($test_name)";
4113     }
4114
4115     doprint "\n\n";
4116     doprint "RUNNING TEST $i of $opt{NUM_TESTS}$name with option $test_type $run_type$installme\n\n";
4117
4118     if (defined($pre_test)) {
4119         run_command $pre_test;
4120     }
4121
4122     unlink $dmesg;
4123     unlink $buildlog;
4124     unlink $testlog;
4125
4126     if (defined($addconfig)) {
4127         my $min = $minconfig;
4128         if (!defined($minconfig)) {
4129             $min = "";
4130         }
4131         run_command "cat $addconfig $min > $tmpdir/add_config" or
4132             dodie "Failed to create temp config";
4133         $minconfig = "$tmpdir/add_config";
4134     }
4135
4136     if (defined($checkout)) {
4137         run_command "git checkout $checkout" or
4138             die "failed to checkout $checkout";
4139     }
4140
4141     $no_reboot = 0;
4142
4143     # A test may opt to not reboot the box
4144     if ($reboot_on_success) {
4145         $reboot_success = 1;
4146     }
4147
4148     if ($test_type eq "bisect") {
4149         bisect $i;
4150         next;
4151     } elsif ($test_type eq "config_bisect") {
4152         config_bisect $i;
4153         next;
4154     } elsif ($test_type eq "patchcheck") {
4155         patchcheck $i;
4156         next;
4157     } elsif ($test_type eq "make_min_config") {
4158         make_min_config $i;
4159         next;
4160     } elsif ($test_type eq "make_warnings_file") {
4161         $no_reboot = 1;
4162         make_warnings_file $i;
4163         next;
4164     }
4165
4166     if ($build_type ne "nobuild") {
4167         build $build_type or next;
4168         check_buildlog or next;
4169     }
4170
4171     if ($test_type eq "install") {
4172         get_version;
4173         install;
4174         success $i;
4175         next;
4176     }
4177
4178     if ($test_type ne "build") {
4179         my $failed = 0;
4180         start_monitor_and_boot or $failed = 1;
4181
4182         if (!$failed && $test_type ne "boot" && defined($run_test)) {
4183             do_run_test or $failed = 1;
4184         }
4185         end_monitor;
4186         next if ($failed);
4187     }
4188
4189     success $i;
4190 }
4191
4192 if (defined($final_post_ktest)) {
4193     run_command $final_post_ktest;
4194 }
4195
4196 if ($opt{"POWEROFF_ON_SUCCESS"}) {
4197     halt;
4198 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
4199     reboot_to_good;
4200 } elsif (defined($switch_to_good)) {
4201     # still need to get to the good kernel
4202     run_command $switch_to_good;
4203 }
4204
4205
4206 doprint "\n    $successes of $opt{NUM_TESTS} tests were successful\n\n";
4207
4208 exit 0;