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