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