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