xref: /openbmc/linux/tools/testing/ktest/ktest.pl (revision a9adc261e978b9214fd505030c24e75b07dcf52f)
12545eb61SSteven Rostedt#!/usr/bin/perl -w
2d6ce2a0bSSteven Rostedt#
3cce1dac8SUwe Kleine-König# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
4d6ce2a0bSSteven Rostedt# Licensed under the terms of the GNU GPL License version 2
5d6ce2a0bSSteven Rostedt#
62545eb61SSteven Rostedt
72545eb61SSteven Rostedtuse strict;
82545eb61SSteven Rostedtuse IPC::Open2;
92545eb61SSteven Rostedtuse Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
107faafbd6SSteven Rostedtuse File::Path qw(mkpath);
117faafbd6SSteven Rostedtuse File::Copy qw(cp);
122545eb61SSteven Rostedtuse FileHandle;
132545eb61SSteven Rostedt
14e48c5293SSteven Rostedtmy $VERSION = "0.2";
15e48c5293SSteven Rostedt
162545eb61SSteven Rostedt$| = 1;
172545eb61SSteven Rostedt
182545eb61SSteven Rostedtmy %opt;
19a57419b3SSteven Rostedtmy %repeat_tests;
20a57419b3SSteven Rostedtmy %repeats;
21c75d22d9SSteven Rostedt (Red Hat)my %evals;
222545eb61SSteven Rostedt
232545eb61SSteven Rostedt#default opts
244f43e0dcSSteven Rostedtmy %default = (
254f43e0dcSSteven Rostedt    "NUM_TESTS"			=> 1,
264f43e0dcSSteven Rostedt    "TEST_TYPE"			=> "build",
274f43e0dcSSteven Rostedt    "BUILD_TYPE"		=> "randconfig",
284f43e0dcSSteven Rostedt    "MAKE_CMD"			=> "make",
295a5d8e48SSatoru Takeuchi    "CLOSE_CONSOLE_SIGNAL"	=> "INT",
304f43e0dcSSteven Rostedt    "TIMEOUT"			=> 120,
314f43e0dcSSteven Rostedt    "TMP_DIR"			=> "/tmp/ktest/\${MACHINE}",
324f43e0dcSSteven Rostedt    "SLEEP_TIME"		=> 60,	# sleep time between tests
334f43e0dcSSteven Rostedt    "BUILD_NOCLEAN"		=> 0,
344f43e0dcSSteven Rostedt    "REBOOT_ON_ERROR"		=> 0,
354f43e0dcSSteven Rostedt    "POWEROFF_ON_ERROR"		=> 0,
364f43e0dcSSteven Rostedt    "REBOOT_ON_SUCCESS"		=> 1,
374f43e0dcSSteven Rostedt    "POWEROFF_ON_SUCCESS"	=> 0,
384f43e0dcSSteven Rostedt    "BUILD_OPTIONS"		=> "",
394f43e0dcSSteven Rostedt    "BISECT_SLEEP_TIME"		=> 60,   # sleep time between bisects
404f43e0dcSSteven Rostedt    "PATCHCHECK_SLEEP_TIME"	=> 60, # sleep time between patch checks
414f43e0dcSSteven Rostedt    "CLEAR_LOG"			=> 0,
424f43e0dcSSteven Rostedt    "BISECT_MANUAL"		=> 0,
434f43e0dcSSteven Rostedt    "BISECT_SKIP"		=> 1,
44961d9cacSSteven Rostedt (Red Hat)    "BISECT_TRIES"		=> 1,
45ccc513b6SSteven Rostedt    "MIN_CONFIG_TYPE"		=> "boot",
464f43e0dcSSteven Rostedt    "SUCCESS_LINE"		=> "login:",
474f43e0dcSSteven Rostedt    "DETECT_TRIPLE_FAULT"	=> 1,
484f43e0dcSSteven Rostedt    "NO_INSTALL"		=> 0,
494f43e0dcSSteven Rostedt    "BOOTED_TIMEOUT"		=> 1,
504f43e0dcSSteven Rostedt    "DIE_ON_FAILURE"		=> 1,
514f43e0dcSSteven Rostedt    "SSH_EXEC"			=> "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
524f43e0dcSSteven Rostedt    "SCP_TO_TARGET"		=> "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
5302ad2617SSteven Rostedt    "SCP_TO_TARGET_INSTALL"	=> "\${SCP_TO_TARGET}",
544f43e0dcSSteven Rostedt    "REBOOT"			=> "ssh \$SSH_USER\@\$MACHINE reboot",
554f43e0dcSSteven Rostedt    "STOP_AFTER_SUCCESS"	=> 10,
564f43e0dcSSteven Rostedt    "STOP_AFTER_FAILURE"	=> 60,
574f43e0dcSSteven Rostedt    "STOP_TEST_AFTER"		=> 600,
58407b95b7SSteven Rostedt    "MAX_MONITOR_WAIT"		=> 1800,
59a15ba913SSteven Rostedt    "GRUB_REBOOT"		=> "grub2-reboot",
607786954cSSteven Rostedt    "SYSLINUX"			=> "extlinux",
617786954cSSteven Rostedt    "SYSLINUX_PATH"		=> "/boot/extlinux",
623e1d3678SSteven Rostedt (VMware)    "CONNECT_TIMEOUT"		=> 25,
63600bbf0aSSteven Rostedt
64600bbf0aSSteven Rostedt# required, and we will ask users if they don't have them but we keep the default
65600bbf0aSSteven Rostedt# value something that is common.
664f43e0dcSSteven Rostedt    "REBOOT_TYPE"		=> "grub",
674f43e0dcSSteven Rostedt    "LOCALVERSION"		=> "-test",
684f43e0dcSSteven Rostedt    "SSH_USER"			=> "root",
694f43e0dcSSteven Rostedt    "BUILD_TARGET"	 	=> "arch/x86/boot/bzImage",
704f43e0dcSSteven Rostedt    "TARGET_IMAGE"		=> "/boot/vmlinuz-test",
719cc9e091SSteven Rostedt
729cc9e091SSteven Rostedt    "LOG_FILE"			=> undef,
739cc9e091SSteven Rostedt    "IGNORE_UNUSED"		=> 0,
744f43e0dcSSteven Rostedt);
752545eb61SSteven Rostedt
765269faadSSatoru Takeuchimy $ktest_config = "ktest.conf";
772545eb61SSteven Rostedtmy $version;
78683a3e64SSteven Rostedtmy $have_version = 0;
79a75fececSSteven Rostedtmy $machine;
80df5f7c66SSteven Rostedt (Red Hat)my $last_machine;
81e48c5293SSteven Rostedtmy $ssh_user;
82a75fececSSteven Rostedtmy $tmpdir;
83a75fececSSteven Rostedtmy $builddir;
84a75fececSSteven Rostedtmy $outputdir;
8551ad1dd1SSteven Rostedtmy $output_config;
86a75fececSSteven Rostedtmy $test_type;
877faafbd6SSteven Rostedtmy $build_type;
88a75fececSSteven Rostedtmy $build_options;
89921ed4c7SSteven Rostedtmy $final_post_ktest;
90921ed4c7SSteven Rostedtmy $pre_ktest;
91921ed4c7SSteven Rostedtmy $post_ktest;
92921ed4c7SSteven Rostedtmy $pre_test;
93921ed4c7SSteven Rostedtmy $post_test;
940bd6c1a3SSteven Rostedtmy $pre_build;
950bd6c1a3SSteven Rostedtmy $post_build;
960bd6c1a3SSteven Rostedtmy $pre_build_die;
970bd6c1a3SSteven Rostedtmy $post_build_die;
98a75fececSSteven Rostedtmy $reboot_type;
99a75fececSSteven Rostedtmy $reboot_script;
100a75fececSSteven Rostedtmy $power_cycle;
101e48c5293SSteven Rostedtmy $reboot;
102a75fececSSteven Rostedtmy $reboot_on_error;
103bc7c5803SSteven Rostedtmy $switch_to_good;
104bc7c5803SSteven Rostedtmy $switch_to_test;
105a75fececSSteven Rostedtmy $poweroff_on_error;
106648a182cSSteven Rostedtmy $reboot_on_success;
107a75fececSSteven Rostedtmy $die_on_failure;
108576f627cSSteven Rostedtmy $powercycle_after_reboot;
109576f627cSSteven Rostedtmy $poweroff_after_halt;
110407b95b7SSteven Rostedtmy $max_monitor_wait;
111e48c5293SSteven Rostedtmy $ssh_exec;
112e48c5293SSteven Rostedtmy $scp_to_target;
11302ad2617SSteven Rostedtmy $scp_to_target_install;
114a75fececSSteven Rostedtmy $power_off;
115a75fececSSteven Rostedtmy $grub_menu;
116752d9665SSteven Rostedt (Red Hat)my $last_grub_menu;
117a15ba913SSteven Rostedtmy $grub_file;
1182545eb61SSteven Rostedtmy $grub_number;
119a15ba913SSteven Rostedtmy $grub_reboot;
1207786954cSSteven Rostedtmy $syslinux;
1217786954cSSteven Rostedtmy $syslinux_path;
1227786954cSSteven Rostedtmy $syslinux_label;
1232545eb61SSteven Rostedtmy $target;
1242545eb61SSteven Rostedtmy $make;
125e5c2ec11SSteven Rostedtmy $pre_install;
1268b37ca8cSSteven Rostedtmy $post_install;
127e0a8742eSSteven Rostedtmy $no_install;
1285c42fc5bSSteven Rostedtmy $noclean;
1295f9b6cedSSteven Rostedtmy $minconfig;
1304c4ab120SSteven Rostedtmy $start_minconfig;
13135ce5952SSteven Rostedtmy $start_minconfig_defined;
1324c4ab120SSteven Rostedtmy $output_minconfig;
133ccc513b6SSteven Rostedtmy $minconfig_type;
13443de3316SSteven Rostedtmy $use_output_minconfig;
1354283b169SSteven Rostedt (Red Hat)my $warnings_file;
1364c4ab120SSteven Rostedtmy $ignore_config;
137be405f95SSteven Rostedtmy $ignore_errors;
1382b7d9b21SSteven Rostedtmy $addconfig;
1395f9b6cedSSteven Rostedtmy $in_bisect = 0;
140b5f4aea6SSteven Rostedtmy $bisect_bad_commit = "";
141d6ce2a0bSSteven Rostedtmy $reverse_bisect;
142c960bb9fSSteven Rostedtmy $bisect_manual;
143c23dca7cSSteven Rostedtmy $bisect_skip;
144961d9cacSSteven Rostedt (Red Hat)my $bisect_tries;
14530f75da5SSteven Rostedtmy $config_bisect_good;
146c5dacb88SSteven Rostedtmy $bisect_ret_good;
147c5dacb88SSteven Rostedtmy $bisect_ret_bad;
148c5dacb88SSteven Rostedtmy $bisect_ret_skip;
149c5dacb88SSteven Rostedtmy $bisect_ret_abort;
150c5dacb88SSteven Rostedtmy $bisect_ret_default;
1516c5ee0beSSteven Rostedtmy $in_patchcheck = 0;
1525a391fbfSSteven Rostedtmy $run_test;
1537faafbd6SSteven Rostedtmy $buildlog;
154a9dd5d63SRabin Vincentmy $testlog;
1557faafbd6SSteven Rostedtmy $dmesg;
1567faafbd6SSteven Rostedtmy $monitor_fp;
1577faafbd6SSteven Rostedtmy $monitor_pid;
1587faafbd6SSteven Rostedtmy $monitor_cnt = 0;
159a75fececSSteven Rostedtmy $sleep_time;
160a75fececSSteven Rostedtmy $bisect_sleep_time;
16127d934b2SSteven Rostedtmy $patchcheck_sleep_time;
1621990207dSSteven Rostedtmy $ignore_warnings;
163a75fececSSteven Rostedtmy $store_failures;
164de5b6e3bSRabin Vincentmy $store_successes;
1659064af52SSteven Rostedtmy $test_name;
166a75fececSSteven Rostedtmy $timeout;
1673e1d3678SSteven Rostedt (VMware)my $connect_timeout;
168a75fececSSteven Rostedtmy $booted_timeout;
169f1a5b962SSteven Rostedtmy $detect_triplefault;
170a75fececSSteven Rostedtmy $console;
1715a5d8e48SSatoru Takeuchimy $close_console_signal;
1722b803365SSteven Rostedtmy $reboot_success_line;
173a75fececSSteven Rostedtmy $success_line;
1741c8a617aSSteven Rostedtmy $stop_after_success;
1751c8a617aSSteven Rostedtmy $stop_after_failure;
1762d01b26aSSteven Rostedtmy $stop_test_after;
177a75fececSSteven Rostedtmy $build_target;
178a75fececSSteven Rostedtmy $target_image;
179b5f4aea6SSteven Rostedtmy $checkout;
180a75fececSSteven Rostedtmy $localversion;
181576f627cSSteven Rostedtmy $iteration = 0;
182e48c5293SSteven Rostedtmy $successes = 0;
1839d2f7f05SJosh Poimboeufmy $stty_orig;
1845739438bSSteven Rostedt (VMware)my $run_command_status = 0;
1852545eb61SSteven Rostedt
186b5f4aea6SSteven Rostedtmy $bisect_good;
187b5f4aea6SSteven Rostedtmy $bisect_bad;
188b5f4aea6SSteven Rostedtmy $bisect_type;
189b5f4aea6SSteven Rostedtmy $bisect_start;
190b5f4aea6SSteven Rostedtmy $bisect_replay;
191b5f4aea6SSteven Rostedtmy $bisect_files;
192b5f4aea6SSteven Rostedtmy $bisect_reverse;
193b5f4aea6SSteven Rostedtmy $bisect_check;
194b5f4aea6SSteven Rostedt
195b5f4aea6SSteven Rostedtmy $config_bisect;
196b5f4aea6SSteven Rostedtmy $config_bisect_type;
197b0918612SSteven Rostedtmy $config_bisect_check;
198b5f4aea6SSteven Rostedt
199b5f4aea6SSteven Rostedtmy $patchcheck_type;
200b5f4aea6SSteven Rostedtmy $patchcheck_start;
20123a0e161SSteven Rostedt (Red Hat)my $patchcheck_cherry;
202b5f4aea6SSteven Rostedtmy $patchcheck_end;
203b5f4aea6SSteven Rostedt
20438fa3dc1SSteven Rostedt (Red Hat)my $build_time;
20538fa3dc1SSteven Rostedt (Red Hat)my $install_time;
20638fa3dc1SSteven Rostedt (Red Hat)my $reboot_time;
20738fa3dc1SSteven Rostedt (Red Hat)my $test_time;
20838fa3dc1SSteven Rostedt (Red Hat)
209165708b2SSteven Rostedt# set when a test is something other that just building or install
210bb8474b1SSteven Rostedt# which would require more options.
211bb8474b1SSteven Rostedtmy $buildonly = 1;
212bb8474b1SSteven Rostedt
2134283b169SSteven Rostedt (Red Hat)# tell build not to worry about warnings, even when WARNINGS_FILE is set
2144283b169SSteven Rostedt (Red Hat)my $warnings_ok = 0;
2154283b169SSteven Rostedt (Red Hat)
216dbd3783bSSteven Rostedt# set when creating a new config
217dbd3783bSSteven Rostedtmy $newconfig = 0;
218dbd3783bSSteven Rostedt
2198d1491baSSteven Rostedtmy %entered_configs;
2208d1491baSSteven Rostedtmy %config_help;
22177d942ceSSteven Rostedtmy %variable;
222cf79fab6SSteven Rostedt
223cf79fab6SSteven Rostedt# force_config is the list of configs that we force enabled (or disabled)
224cf79fab6SSteven Rostedt# in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
225fcb3f16aSSteven Rostedtmy %force_config;
2268d1491baSSteven Rostedt
2274ab1cce5SSteven Rostedt# do not force reboots on config problems
2284ab1cce5SSteven Rostedtmy $no_reboot = 1;
2294ab1cce5SSteven Rostedt
230759a3cc6SSteven Rostedt# reboot on success
231759a3cc6SSteven Rostedtmy $reboot_success = 0;
232759a3cc6SSteven Rostedt
2339cc9e091SSteven Rostedtmy %option_map = (
2349cc9e091SSteven Rostedt    "MACHINE"			=> \$machine,
2359cc9e091SSteven Rostedt    "SSH_USER"			=> \$ssh_user,
2369cc9e091SSteven Rostedt    "TMP_DIR"			=> \$tmpdir,
2379cc9e091SSteven Rostedt    "OUTPUT_DIR"		=> \$outputdir,
2389cc9e091SSteven Rostedt    "BUILD_DIR"			=> \$builddir,
2399cc9e091SSteven Rostedt    "TEST_TYPE"			=> \$test_type,
240921ed4c7SSteven Rostedt    "PRE_KTEST"			=> \$pre_ktest,
241921ed4c7SSteven Rostedt    "POST_KTEST"		=> \$post_ktest,
242921ed4c7SSteven Rostedt    "PRE_TEST"			=> \$pre_test,
243921ed4c7SSteven Rostedt    "POST_TEST"			=> \$post_test,
2449cc9e091SSteven Rostedt    "BUILD_TYPE"		=> \$build_type,
2459cc9e091SSteven Rostedt    "BUILD_OPTIONS"		=> \$build_options,
2469cc9e091SSteven Rostedt    "PRE_BUILD"			=> \$pre_build,
2479cc9e091SSteven Rostedt    "POST_BUILD"		=> \$post_build,
2489cc9e091SSteven Rostedt    "PRE_BUILD_DIE"		=> \$pre_build_die,
2499cc9e091SSteven Rostedt    "POST_BUILD_DIE"		=> \$post_build_die,
2509cc9e091SSteven Rostedt    "POWER_CYCLE"		=> \$power_cycle,
2519cc9e091SSteven Rostedt    "REBOOT"			=> \$reboot,
2529cc9e091SSteven Rostedt    "BUILD_NOCLEAN"		=> \$noclean,
2539cc9e091SSteven Rostedt    "MIN_CONFIG"		=> \$minconfig,
2549cc9e091SSteven Rostedt    "OUTPUT_MIN_CONFIG"		=> \$output_minconfig,
2559cc9e091SSteven Rostedt    "START_MIN_CONFIG"		=> \$start_minconfig,
256ccc513b6SSteven Rostedt    "MIN_CONFIG_TYPE"		=> \$minconfig_type,
25743de3316SSteven Rostedt    "USE_OUTPUT_MIN_CONFIG"	=> \$use_output_minconfig,
2584283b169SSteven Rostedt (Red Hat)    "WARNINGS_FILE"		=> \$warnings_file,
2599cc9e091SSteven Rostedt    "IGNORE_CONFIG"		=> \$ignore_config,
2609cc9e091SSteven Rostedt    "TEST"			=> \$run_test,
2619cc9e091SSteven Rostedt    "ADD_CONFIG"		=> \$addconfig,
2629cc9e091SSteven Rostedt    "REBOOT_TYPE"		=> \$reboot_type,
2639cc9e091SSteven Rostedt    "GRUB_MENU"			=> \$grub_menu,
264a15ba913SSteven Rostedt    "GRUB_FILE"			=> \$grub_file,
265a15ba913SSteven Rostedt    "GRUB_REBOOT"		=> \$grub_reboot,
2667786954cSSteven Rostedt    "SYSLINUX"			=> \$syslinux,
2677786954cSSteven Rostedt    "SYSLINUX_PATH"		=> \$syslinux_path,
2687786954cSSteven Rostedt    "SYSLINUX_LABEL"		=> \$syslinux_label,
269e5c2ec11SSteven Rostedt    "PRE_INSTALL"		=> \$pre_install,
2709cc9e091SSteven Rostedt    "POST_INSTALL"		=> \$post_install,
2719cc9e091SSteven Rostedt    "NO_INSTALL"		=> \$no_install,
2729cc9e091SSteven Rostedt    "REBOOT_SCRIPT"		=> \$reboot_script,
2739cc9e091SSteven Rostedt    "REBOOT_ON_ERROR"		=> \$reboot_on_error,
2749cc9e091SSteven Rostedt    "SWITCH_TO_GOOD"		=> \$switch_to_good,
2759cc9e091SSteven Rostedt    "SWITCH_TO_TEST"		=> \$switch_to_test,
2769cc9e091SSteven Rostedt    "POWEROFF_ON_ERROR"		=> \$poweroff_on_error,
277648a182cSSteven Rostedt    "REBOOT_ON_SUCCESS"		=> \$reboot_on_success,
2789cc9e091SSteven Rostedt    "DIE_ON_FAILURE"		=> \$die_on_failure,
2799cc9e091SSteven Rostedt    "POWER_OFF"			=> \$power_off,
2809cc9e091SSteven Rostedt    "POWERCYCLE_AFTER_REBOOT"	=> \$powercycle_after_reboot,
2819cc9e091SSteven Rostedt    "POWEROFF_AFTER_HALT"	=> \$poweroff_after_halt,
282407b95b7SSteven Rostedt    "MAX_MONITOR_WAIT"		=> \$max_monitor_wait,
2839cc9e091SSteven Rostedt    "SLEEP_TIME"		=> \$sleep_time,
2849cc9e091SSteven Rostedt    "BISECT_SLEEP_TIME"		=> \$bisect_sleep_time,
2859cc9e091SSteven Rostedt    "PATCHCHECK_SLEEP_TIME"	=> \$patchcheck_sleep_time,
2869cc9e091SSteven Rostedt    "IGNORE_WARNINGS"		=> \$ignore_warnings,
287be405f95SSteven Rostedt    "IGNORE_ERRORS"		=> \$ignore_errors,
2889cc9e091SSteven Rostedt    "BISECT_MANUAL"		=> \$bisect_manual,
2899cc9e091SSteven Rostedt    "BISECT_SKIP"		=> \$bisect_skip,
290961d9cacSSteven Rostedt (Red Hat)    "BISECT_TRIES"		=> \$bisect_tries,
2919cc9e091SSteven Rostedt    "CONFIG_BISECT_GOOD"	=> \$config_bisect_good,
2929cc9e091SSteven Rostedt    "BISECT_RET_GOOD"		=> \$bisect_ret_good,
2939cc9e091SSteven Rostedt    "BISECT_RET_BAD"		=> \$bisect_ret_bad,
2949cc9e091SSteven Rostedt    "BISECT_RET_SKIP"		=> \$bisect_ret_skip,
2959cc9e091SSteven Rostedt    "BISECT_RET_ABORT"		=> \$bisect_ret_abort,
2969cc9e091SSteven Rostedt    "BISECT_RET_DEFAULT"	=> \$bisect_ret_default,
2979cc9e091SSteven Rostedt    "STORE_FAILURES"		=> \$store_failures,
2989cc9e091SSteven Rostedt    "STORE_SUCCESSES"		=> \$store_successes,
2999cc9e091SSteven Rostedt    "TEST_NAME"			=> \$test_name,
3009cc9e091SSteven Rostedt    "TIMEOUT"			=> \$timeout,
3013e1d3678SSteven Rostedt (VMware)    "CONNECT_TIMEOUT"		=> \$connect_timeout,
3029cc9e091SSteven Rostedt    "BOOTED_TIMEOUT"		=> \$booted_timeout,
3039cc9e091SSteven Rostedt    "CONSOLE"			=> \$console,
3045a5d8e48SSatoru Takeuchi    "CLOSE_CONSOLE_SIGNAL"	=> \$close_console_signal,
3059cc9e091SSteven Rostedt    "DETECT_TRIPLE_FAULT"	=> \$detect_triplefault,
3069cc9e091SSteven Rostedt    "SUCCESS_LINE"		=> \$success_line,
3079cc9e091SSteven Rostedt    "REBOOT_SUCCESS_LINE"	=> \$reboot_success_line,
3089cc9e091SSteven Rostedt    "STOP_AFTER_SUCCESS"	=> \$stop_after_success,
3099cc9e091SSteven Rostedt    "STOP_AFTER_FAILURE"	=> \$stop_after_failure,
3109cc9e091SSteven Rostedt    "STOP_TEST_AFTER"		=> \$stop_test_after,
3119cc9e091SSteven Rostedt    "BUILD_TARGET"		=> \$build_target,
3129cc9e091SSteven Rostedt    "SSH_EXEC"			=> \$ssh_exec,
3139cc9e091SSteven Rostedt    "SCP_TO_TARGET"		=> \$scp_to_target,
31402ad2617SSteven Rostedt    "SCP_TO_TARGET_INSTALL"	=> \$scp_to_target_install,
3159cc9e091SSteven Rostedt    "CHECKOUT"			=> \$checkout,
3169cc9e091SSteven Rostedt    "TARGET_IMAGE"		=> \$target_image,
3179cc9e091SSteven Rostedt    "LOCALVERSION"		=> \$localversion,
3189cc9e091SSteven Rostedt
3199cc9e091SSteven Rostedt    "BISECT_GOOD"		=> \$bisect_good,
3209cc9e091SSteven Rostedt    "BISECT_BAD"		=> \$bisect_bad,
3219cc9e091SSteven Rostedt    "BISECT_TYPE"		=> \$bisect_type,
3229cc9e091SSteven Rostedt    "BISECT_START"		=> \$bisect_start,
3239cc9e091SSteven Rostedt    "BISECT_REPLAY"		=> \$bisect_replay,
3249cc9e091SSteven Rostedt    "BISECT_FILES"		=> \$bisect_files,
3259cc9e091SSteven Rostedt    "BISECT_REVERSE"		=> \$bisect_reverse,
3269cc9e091SSteven Rostedt    "BISECT_CHECK"		=> \$bisect_check,
3279cc9e091SSteven Rostedt
3289cc9e091SSteven Rostedt    "CONFIG_BISECT"		=> \$config_bisect,
3299cc9e091SSteven Rostedt    "CONFIG_BISECT_TYPE"	=> \$config_bisect_type,
330b0918612SSteven Rostedt    "CONFIG_BISECT_CHECK"	=> \$config_bisect_check,
3319cc9e091SSteven Rostedt
3329cc9e091SSteven Rostedt    "PATCHCHECK_TYPE"		=> \$patchcheck_type,
3339cc9e091SSteven Rostedt    "PATCHCHECK_START"		=> \$patchcheck_start,
33423a0e161SSteven Rostedt (Red Hat)    "PATCHCHECK_CHERRY"		=> \$patchcheck_cherry,
3359cc9e091SSteven Rostedt    "PATCHCHECK_END"		=> \$patchcheck_end,
3369cc9e091SSteven Rostedt);
3379cc9e091SSteven Rostedt
3389cc9e091SSteven Rostedt# Options may be used by other options, record them.
3399cc9e091SSteven Rostedtmy %used_options;
3409cc9e091SSteven Rostedt
3417bf51073SSteven Rostedt# default variables that can be used
3427bf51073SSteven Rostedtchomp ($variable{"PWD"} = `pwd`);
3437bf51073SSteven Rostedt
3448d1491baSSteven Rostedt$config_help{"MACHINE"} = << "EOF"
3458d1491baSSteven Rostedt The machine hostname that you will test.
346bb8474b1SSteven Rostedt For build only tests, it is still needed to differentiate log files.
3478d1491baSSteven RostedtEOF
3488d1491baSSteven Rostedt    ;
3498d1491baSSteven Rostedt$config_help{"SSH_USER"} = << "EOF"
3508d1491baSSteven Rostedt The box is expected to have ssh on normal bootup, provide the user
3518d1491baSSteven Rostedt  (most likely root, since you need privileged operations)
3528d1491baSSteven RostedtEOF
3538d1491baSSteven Rostedt    ;
3548d1491baSSteven Rostedt$config_help{"BUILD_DIR"} = << "EOF"
3558d1491baSSteven Rostedt The directory that contains the Linux source code (full path).
3560e7a22deSSteven Rostedt You can use \${PWD} that will be the path where ktest.pl is run, or use
3570e7a22deSSteven Rostedt \${THIS_DIR} which is assigned \${PWD} but may be changed later.
3588d1491baSSteven RostedtEOF
3598d1491baSSteven Rostedt    ;
3608d1491baSSteven Rostedt$config_help{"OUTPUT_DIR"} = << "EOF"
3618d1491baSSteven Rostedt The directory that the objects will be built (full path).
3628d1491baSSteven Rostedt (can not be same as BUILD_DIR)
3630e7a22deSSteven Rostedt You can use \${PWD} that will be the path where ktest.pl is run, or use
3640e7a22deSSteven Rostedt \${THIS_DIR} which is assigned \${PWD} but may be changed later.
3658d1491baSSteven RostedtEOF
3668d1491baSSteven Rostedt    ;
3678d1491baSSteven Rostedt$config_help{"BUILD_TARGET"} = << "EOF"
3688d1491baSSteven Rostedt The location of the compiled file to copy to the target.
3698d1491baSSteven Rostedt (relative to OUTPUT_DIR)
3708d1491baSSteven RostedtEOF
3718d1491baSSteven Rostedt    ;
372dbd3783bSSteven Rostedt$config_help{"BUILD_OPTIONS"} = << "EOF"
373dbd3783bSSteven Rostedt Options to add to \"make\" when building.
374dbd3783bSSteven Rostedt i.e.  -j20
375dbd3783bSSteven RostedtEOF
376dbd3783bSSteven Rostedt    ;
3778d1491baSSteven Rostedt$config_help{"TARGET_IMAGE"} = << "EOF"
3788d1491baSSteven Rostedt The place to put your image on the test machine.
3798d1491baSSteven RostedtEOF
3808d1491baSSteven Rostedt    ;
3818d1491baSSteven Rostedt$config_help{"POWER_CYCLE"} = << "EOF"
3828d1491baSSteven Rostedt A script or command to reboot the box.
3838d1491baSSteven Rostedt
3848d1491baSSteven Rostedt Here is a digital loggers power switch example
3858d1491baSSteven Rostedt POWER_CYCLE = wget --no-proxy -O /dev/null -q  --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
3868d1491baSSteven Rostedt
3878d1491baSSteven Rostedt Here is an example to reboot a virtual box on the current host
3888d1491baSSteven Rostedt with the name "Guest".
3898d1491baSSteven Rostedt POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
3908d1491baSSteven RostedtEOF
3918d1491baSSteven Rostedt    ;
3928d1491baSSteven Rostedt$config_help{"CONSOLE"} = << "EOF"
3938d1491baSSteven Rostedt The script or command that reads the console
3948d1491baSSteven Rostedt
3958d1491baSSteven Rostedt  If you use ttywatch server, something like the following would work.
3968d1491baSSteven RostedtCONSOLE = nc -d localhost 3001
3978d1491baSSteven Rostedt
3988d1491baSSteven Rostedt For a virtual machine with guest name "Guest".
3998d1491baSSteven RostedtCONSOLE =  virsh console Guest
4008d1491baSSteven RostedtEOF
4018d1491baSSteven Rostedt    ;
4028d1491baSSteven Rostedt$config_help{"LOCALVERSION"} = << "EOF"
4038d1491baSSteven Rostedt Required version ending to differentiate the test
4048d1491baSSteven Rostedt from other linux builds on the system.
4058d1491baSSteven RostedtEOF
4068d1491baSSteven Rostedt    ;
4078d1491baSSteven Rostedt$config_help{"REBOOT_TYPE"} = << "EOF"
4088d1491baSSteven Rostedt Way to reboot the box to the test kernel.
4097786954cSSteven Rostedt Only valid options so far are "grub", "grub2", "syslinux", and "script".
4108d1491baSSteven Rostedt
4118d1491baSSteven Rostedt If you specify grub, it will assume grub version 1
4128d1491baSSteven Rostedt and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
4138d1491baSSteven Rostedt and select that target to reboot to the kernel. If this is not
4148d1491baSSteven Rostedt your setup, then specify "script" and have a command or script
4158d1491baSSteven Rostedt specified in REBOOT_SCRIPT to boot to the target.
4168d1491baSSteven Rostedt
4178d1491baSSteven Rostedt The entry in /boot/grub/menu.lst must be entered in manually.
4188d1491baSSteven Rostedt The test will not modify that file.
419a15ba913SSteven Rostedt
420a15ba913SSteven Rostedt If you specify grub2, then you also need to specify both \$GRUB_MENU
421a15ba913SSteven Rostedt and \$GRUB_FILE.
4227786954cSSteven Rostedt
4237786954cSSteven Rostedt If you specify syslinux, then you may use SYSLINUX to define the syslinux
4247786954cSSteven Rostedt command (defaults to extlinux), and SYSLINUX_PATH to specify the path to
4257786954cSSteven Rostedt the syslinux install (defaults to /boot/extlinux). But you have to specify
4267786954cSSteven Rostedt SYSLINUX_LABEL to define the label to boot to for the test kernel.
4278d1491baSSteven RostedtEOF
4288d1491baSSteven Rostedt    ;
4298d1491baSSteven Rostedt$config_help{"GRUB_MENU"} = << "EOF"
4308d1491baSSteven Rostedt The grub title name for the test kernel to boot
431a15ba913SSteven Rostedt (Only mandatory if REBOOT_TYPE = grub or grub2)
4328d1491baSSteven Rostedt
4338d1491baSSteven Rostedt Note, ktest.pl will not update the grub menu.lst, you need to
4348d1491baSSteven Rostedt manually add an option for the test. ktest.pl will search
4358d1491baSSteven Rostedt the grub menu.lst for this option to find what kernel to
4368d1491baSSteven Rostedt reboot into.
4378d1491baSSteven Rostedt
4388d1491baSSteven Rostedt For example, if in the /boot/grub/menu.lst the test kernel title has:
4398d1491baSSteven Rostedt title Test Kernel
4408d1491baSSteven Rostedt kernel vmlinuz-test
4418d1491baSSteven Rostedt GRUB_MENU = Test Kernel
442a15ba913SSteven Rostedt
443a15ba913SSteven Rostedt For grub2, a search of \$GRUB_FILE is performed for the lines
444a15ba913SSteven Rostedt that begin with "menuentry". It will not detect submenus. The
445a15ba913SSteven Rostedt menu must be a non-nested menu. Add the quotes used in the menu
446a15ba913SSteven Rostedt to guarantee your selection, as the first menuentry with the content
447a15ba913SSteven Rostedt of \$GRUB_MENU that is found will be used.
448a15ba913SSteven RostedtEOF
449a15ba913SSteven Rostedt    ;
450a15ba913SSteven Rostedt$config_help{"GRUB_FILE"} = << "EOF"
451a15ba913SSteven Rostedt If grub2 is used, the full path for the grub.cfg file is placed
452a15ba913SSteven Rostedt here. Use something like /boot/grub2/grub.cfg to search.
4538d1491baSSteven RostedtEOF
4548d1491baSSteven Rostedt    ;
4557786954cSSteven Rostedt$config_help{"SYSLINUX_LABEL"} = << "EOF"
4567786954cSSteven Rostedt If syslinux is used, the label that boots the target kernel must
4577786954cSSteven Rostedt be specified with SYSLINUX_LABEL.
4587786954cSSteven RostedtEOF
4597786954cSSteven Rostedt    ;
4608d1491baSSteven Rostedt$config_help{"REBOOT_SCRIPT"} = << "EOF"
4618d1491baSSteven Rostedt A script to reboot the target into the test kernel
4628d1491baSSteven Rostedt (Only mandatory if REBOOT_TYPE = script)
4638d1491baSSteven RostedtEOF
4648d1491baSSteven Rostedt    ;
4658d1491baSSteven Rostedt
466c75d22d9SSteven Rostedt (Red Hat)sub _logit {
467c75d22d9SSteven Rostedt (Red Hat)    if (defined($opt{"LOG_FILE"})) {
468c75d22d9SSteven Rostedt (Red Hat)	open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
469c75d22d9SSteven Rostedt (Red Hat)	print OUT @_;
470c75d22d9SSteven Rostedt (Red Hat)	close(OUT);
471c75d22d9SSteven Rostedt (Red Hat)    }
472c75d22d9SSteven Rostedt (Red Hat)}
473c75d22d9SSteven Rostedt (Red Hat)
474c75d22d9SSteven Rostedt (Red Hat)sub logit {
475c75d22d9SSteven Rostedt (Red Hat)    if (defined($opt{"LOG_FILE"})) {
476c75d22d9SSteven Rostedt (Red Hat)	_logit @_;
477c75d22d9SSteven Rostedt (Red Hat)    } else {
478c75d22d9SSteven Rostedt (Red Hat)	print @_;
479c75d22d9SSteven Rostedt (Red Hat)    }
480c75d22d9SSteven Rostedt (Red Hat)}
481c75d22d9SSteven Rostedt (Red Hat)
482c75d22d9SSteven Rostedt (Red Hat)sub doprint {
483c75d22d9SSteven Rostedt (Red Hat)    print @_;
484c75d22d9SSteven Rostedt (Red Hat)    _logit @_;
485c75d22d9SSteven Rostedt (Red Hat)}
486c75d22d9SSteven Rostedt (Red Hat)
487dad98754SSteven Rostedtsub read_prompt {
488dad98754SSteven Rostedt    my ($cancel, $prompt) = @_;
48935ce5952SSteven Rostedt
49035ce5952SSteven Rostedt    my $ans;
49135ce5952SSteven Rostedt
49235ce5952SSteven Rostedt    for (;;) {
493dad98754SSteven Rostedt	if ($cancel) {
494dad98754SSteven Rostedt	    print "$prompt [y/n/C] ";
495dad98754SSteven Rostedt	} else {
49635ce5952SSteven Rostedt	    print "$prompt [Y/n] ";
497dad98754SSteven Rostedt	}
49835ce5952SSteven Rostedt	$ans = <STDIN>;
49935ce5952SSteven Rostedt	chomp $ans;
50035ce5952SSteven Rostedt	if ($ans =~ /^\s*$/) {
501dad98754SSteven Rostedt	    if ($cancel) {
502dad98754SSteven Rostedt		$ans = "c";
503dad98754SSteven Rostedt	    } else {
50435ce5952SSteven Rostedt		$ans = "y";
50535ce5952SSteven Rostedt	    }
506dad98754SSteven Rostedt	}
50735ce5952SSteven Rostedt	last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
508dad98754SSteven Rostedt	if ($cancel) {
509dad98754SSteven Rostedt	    last if ($ans =~ /^c$/i);
510dad98754SSteven Rostedt	    print "Please answer either 'y', 'n' or 'c'.\n";
511dad98754SSteven Rostedt	} else {
51235ce5952SSteven Rostedt	    print "Please answer either 'y' or 'n'.\n";
51335ce5952SSteven Rostedt	}
514dad98754SSteven Rostedt    }
515dad98754SSteven Rostedt    if ($ans =~ /^c/i) {
516dad98754SSteven Rostedt	exit;
517dad98754SSteven Rostedt    }
51835ce5952SSteven Rostedt    if ($ans !~ /^y$/i) {
51935ce5952SSteven Rostedt	return 0;
52035ce5952SSteven Rostedt    }
52135ce5952SSteven Rostedt    return 1;
52235ce5952SSteven Rostedt}
5238d1491baSSteven Rostedt
524dad98754SSteven Rostedtsub read_yn {
525dad98754SSteven Rostedt    my ($prompt) = @_;
526dad98754SSteven Rostedt
527dad98754SSteven Rostedt    return read_prompt 0, $prompt;
528dad98754SSteven Rostedt}
529dad98754SSteven Rostedt
530dad98754SSteven Rostedtsub read_ync {
531dad98754SSteven Rostedt    my ($prompt) = @_;
532dad98754SSteven Rostedt
533dad98754SSteven Rostedt    return read_prompt 1, $prompt;
534dad98754SSteven Rostedt}
535dad98754SSteven Rostedt
5365269faadSSatoru Takeuchisub get_mandatory_config {
5378d1491baSSteven Rostedt    my ($config) = @_;
538815e2bd7SSteven Rostedt    my $ans;
5398d1491baSSteven Rostedt
5408d1491baSSteven Rostedt    return if (defined($opt{$config}));
5418d1491baSSteven Rostedt
5428d1491baSSteven Rostedt    if (defined($config_help{$config})) {
5438d1491baSSteven Rostedt	print "\n";
5448d1491baSSteven Rostedt	print $config_help{$config};
5458d1491baSSteven Rostedt    }
5468d1491baSSteven Rostedt
5478d1491baSSteven Rostedt    for (;;) {
5488d1491baSSteven Rostedt	print "$config = ";
549dbd3783bSSteven Rostedt	if (defined($default{$config}) && length($default{$config})) {
5508d1491baSSteven Rostedt	    print "\[$default{$config}\] ";
5518d1491baSSteven Rostedt	}
552815e2bd7SSteven Rostedt	$ans = <STDIN>;
553815e2bd7SSteven Rostedt	$ans =~ s/^\s*(.*\S)\s*$/$1/;
554815e2bd7SSteven Rostedt	if ($ans =~ /^\s*$/) {
5558d1491baSSteven Rostedt	    if ($default{$config}) {
556815e2bd7SSteven Rostedt		$ans = $default{$config};
5578d1491baSSteven Rostedt	    } else {
5588d1491baSSteven Rostedt		print "Your answer can not be blank\n";
5598d1491baSSteven Rostedt		next;
5608d1491baSSteven Rostedt	    }
5618d1491baSSteven Rostedt	}
5620e7a22deSSteven Rostedt	$entered_configs{$config} = ${ans};
5638d1491baSSteven Rostedt	last;
5648d1491baSSteven Rostedt    }
5658d1491baSSteven Rostedt}
5668d1491baSSteven Rostedt
56738fa3dc1SSteven Rostedt (Red Hat)sub show_time {
56838fa3dc1SSteven Rostedt (Red Hat)    my ($time) = @_;
56938fa3dc1SSteven Rostedt (Red Hat)
57038fa3dc1SSteven Rostedt (Red Hat)    my $hours = 0;
57138fa3dc1SSteven Rostedt (Red Hat)    my $minutes = 0;
57238fa3dc1SSteven Rostedt (Red Hat)
57338fa3dc1SSteven Rostedt (Red Hat)    if ($time > 3600) {
57438fa3dc1SSteven Rostedt (Red Hat)	$hours = int($time / 3600);
57538fa3dc1SSteven Rostedt (Red Hat)	$time -= $hours * 3600;
57638fa3dc1SSteven Rostedt (Red Hat)    }
57738fa3dc1SSteven Rostedt (Red Hat)    if ($time > 60) {
57838fa3dc1SSteven Rostedt (Red Hat)	$minutes = int($time / 60);
57938fa3dc1SSteven Rostedt (Red Hat)	$time -= $minutes * 60;
58038fa3dc1SSteven Rostedt (Red Hat)    }
58138fa3dc1SSteven Rostedt (Red Hat)
58238fa3dc1SSteven Rostedt (Red Hat)    if ($hours > 0) {
58338fa3dc1SSteven Rostedt (Red Hat)	doprint "$hours hour";
58438fa3dc1SSteven Rostedt (Red Hat)	doprint "s" if ($hours > 1);
58538fa3dc1SSteven Rostedt (Red Hat)	doprint " ";
58638fa3dc1SSteven Rostedt (Red Hat)    }
58738fa3dc1SSteven Rostedt (Red Hat)
58838fa3dc1SSteven Rostedt (Red Hat)    if ($minutes > 0) {
58938fa3dc1SSteven Rostedt (Red Hat)	doprint "$minutes minute";
59038fa3dc1SSteven Rostedt (Red Hat)	doprint "s" if ($minutes > 1);
59138fa3dc1SSteven Rostedt (Red Hat)	doprint " ";
59238fa3dc1SSteven Rostedt (Red Hat)    }
59338fa3dc1SSteven Rostedt (Red Hat)
59438fa3dc1SSteven Rostedt (Red Hat)    doprint "$time second";
59538fa3dc1SSteven Rostedt (Red Hat)    doprint "s" if ($time != 1);
59638fa3dc1SSteven Rostedt (Red Hat)}
59738fa3dc1SSteven Rostedt (Red Hat)
59838fa3dc1SSteven Rostedt (Red Hat)sub print_times {
59938fa3dc1SSteven Rostedt (Red Hat)    doprint "\n";
60038fa3dc1SSteven Rostedt (Red Hat)    if ($build_time) {
60138fa3dc1SSteven Rostedt (Red Hat)	doprint "Build time:   ";
60238fa3dc1SSteven Rostedt (Red Hat)	show_time($build_time);
60338fa3dc1SSteven Rostedt (Red Hat)	doprint "\n";
60438fa3dc1SSteven Rostedt (Red Hat)    }
60538fa3dc1SSteven Rostedt (Red Hat)    if ($install_time) {
60638fa3dc1SSteven Rostedt (Red Hat)	doprint "Install time: ";
60738fa3dc1SSteven Rostedt (Red Hat)	show_time($install_time);
60838fa3dc1SSteven Rostedt (Red Hat)	doprint "\n";
60938fa3dc1SSteven Rostedt (Red Hat)    }
61038fa3dc1SSteven Rostedt (Red Hat)    if ($reboot_time) {
61138fa3dc1SSteven Rostedt (Red Hat)	doprint "Reboot time:  ";
61238fa3dc1SSteven Rostedt (Red Hat)	show_time($reboot_time);
61338fa3dc1SSteven Rostedt (Red Hat)	doprint "\n";
61438fa3dc1SSteven Rostedt (Red Hat)    }
61538fa3dc1SSteven Rostedt (Red Hat)    if ($test_time) {
61638fa3dc1SSteven Rostedt (Red Hat)	doprint "Test time:    ";
61738fa3dc1SSteven Rostedt (Red Hat)	show_time($test_time);
61838fa3dc1SSteven Rostedt (Red Hat)	doprint "\n";
61938fa3dc1SSteven Rostedt (Red Hat)    }
62038fa3dc1SSteven Rostedt (Red Hat)    # reset for iterations like bisect
62138fa3dc1SSteven Rostedt (Red Hat)    $build_time = 0;
62238fa3dc1SSteven Rostedt (Red Hat)    $install_time = 0;
62338fa3dc1SSteven Rostedt (Red Hat)    $reboot_time = 0;
62438fa3dc1SSteven Rostedt (Red Hat)    $test_time = 0;
62538fa3dc1SSteven Rostedt (Red Hat)}
62638fa3dc1SSteven Rostedt (Red Hat)
6275269faadSSatoru Takeuchisub get_mandatory_configs {
6285269faadSSatoru Takeuchi    get_mandatory_config("MACHINE");
6295269faadSSatoru Takeuchi    get_mandatory_config("BUILD_DIR");
6305269faadSSatoru Takeuchi    get_mandatory_config("OUTPUT_DIR");
631bb8474b1SSteven Rostedt
632dbd3783bSSteven Rostedt    if ($newconfig) {
6335269faadSSatoru Takeuchi	get_mandatory_config("BUILD_OPTIONS");
634dbd3783bSSteven Rostedt    }
635dbd3783bSSteven Rostedt
636bb8474b1SSteven Rostedt    # options required for other than just building a kernel
637bb8474b1SSteven Rostedt    if (!$buildonly) {
6385269faadSSatoru Takeuchi	get_mandatory_config("POWER_CYCLE");
6395269faadSSatoru Takeuchi	get_mandatory_config("CONSOLE");
640165708b2SSteven Rostedt    }
641165708b2SSteven Rostedt
642165708b2SSteven Rostedt    # options required for install and more
643165708b2SSteven Rostedt    if ($buildonly != 1) {
6445269faadSSatoru Takeuchi	get_mandatory_config("SSH_USER");
6455269faadSSatoru Takeuchi	get_mandatory_config("BUILD_TARGET");
6465269faadSSatoru Takeuchi	get_mandatory_config("TARGET_IMAGE");
647bb8474b1SSteven Rostedt    }
648bb8474b1SSteven Rostedt
6495269faadSSatoru Takeuchi    get_mandatory_config("LOCALVERSION");
6508d1491baSSteven Rostedt
651bb8474b1SSteven Rostedt    return if ($buildonly);
652bb8474b1SSteven Rostedt
6538d1491baSSteven Rostedt    my $rtype = $opt{"REBOOT_TYPE"};
6548d1491baSSteven Rostedt
6558d1491baSSteven Rostedt    if (!defined($rtype)) {
6568d1491baSSteven Rostedt	if (!defined($opt{"GRUB_MENU"})) {
6575269faadSSatoru Takeuchi	    get_mandatory_config("REBOOT_TYPE");
6588d1491baSSteven Rostedt	    $rtype = $entered_configs{"REBOOT_TYPE"};
6598d1491baSSteven Rostedt	} else {
6608d1491baSSteven Rostedt	    $rtype = "grub";
6618d1491baSSteven Rostedt	}
6628d1491baSSteven Rostedt    }
6638d1491baSSteven Rostedt
6648d1491baSSteven Rostedt    if ($rtype eq "grub") {
6655269faadSSatoru Takeuchi	get_mandatory_config("GRUB_MENU");
6668d1491baSSteven Rostedt    }
667a15ba913SSteven Rostedt
668a15ba913SSteven Rostedt    if ($rtype eq "grub2") {
6695269faadSSatoru Takeuchi	get_mandatory_config("GRUB_MENU");
6705269faadSSatoru Takeuchi	get_mandatory_config("GRUB_FILE");
671a15ba913SSteven Rostedt    }
6727786954cSSteven Rostedt
6737786954cSSteven Rostedt    if ($rtype eq "syslinux") {
6745269faadSSatoru Takeuchi	get_mandatory_config("SYSLINUX_LABEL");
6757786954cSSteven Rostedt    }
6768d1491baSSteven Rostedt}
6778d1491baSSteven Rostedt
67877d942ceSSteven Rostedtsub process_variables {
6798d735212SSteven Rostedt    my ($value, $remove_undef) = @_;
68077d942ceSSteven Rostedt    my $retval = "";
68177d942ceSSteven Rostedt
68277d942ceSSteven Rostedt    # We want to check for '\', and it is just easier
68377d942ceSSteven Rostedt    # to check the previous characet of '$' and not need
68477d942ceSSteven Rostedt    # to worry if '$' is the first character. By adding
68577d942ceSSteven Rostedt    # a space to $value, we can just check [^\\]\$ and
68677d942ceSSteven Rostedt    # it will still work.
68777d942ceSSteven Rostedt    $value = " $value";
68877d942ceSSteven Rostedt
68977d942ceSSteven Rostedt    while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
69077d942ceSSteven Rostedt	my $begin = $1;
69177d942ceSSteven Rostedt	my $var = $2;
69277d942ceSSteven Rostedt	my $end = $3;
69377d942ceSSteven Rostedt	# append beginning of value to retval
69477d942ceSSteven Rostedt	$retval = "$retval$begin";
69577d942ceSSteven Rostedt	if (defined($variable{$var})) {
69677d942ceSSteven Rostedt	    $retval = "$retval$variable{$var}";
6978d735212SSteven Rostedt	} elsif (defined($remove_undef) && $remove_undef) {
6988d735212SSteven Rostedt	    # for if statements, any variable that is not defined,
6998d735212SSteven Rostedt	    # we simple convert to 0
7008d735212SSteven Rostedt	    $retval = "${retval}0";
70177d942ceSSteven Rostedt	} else {
70277d942ceSSteven Rostedt	    # put back the origin piece.
70377d942ceSSteven Rostedt	    $retval = "$retval\$\{$var\}";
7049cc9e091SSteven Rostedt	    # This could be an option that is used later, save
7059cc9e091SSteven Rostedt	    # it so we don't warn if this option is not one of
7069cc9e091SSteven Rostedt	    # ktests options.
7079cc9e091SSteven Rostedt	    $used_options{$var} = 1;
70877d942ceSSteven Rostedt	}
70977d942ceSSteven Rostedt	$value = $end;
71077d942ceSSteven Rostedt    }
71177d942ceSSteven Rostedt    $retval = "$retval$value";
71277d942ceSSteven Rostedt
71377d942ceSSteven Rostedt    # remove the space added in the beginning
71477d942ceSSteven Rostedt    $retval =~ s/ //;
71577d942ceSSteven Rostedt
71677d942ceSSteven Rostedt    return "$retval"
71777d942ceSSteven Rostedt}
71877d942ceSSteven Rostedt
719a57419b3SSteven Rostedtsub set_value {
7203d1cc414SSteven Rostedt    my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
7212545eb61SSteven Rostedt
722cad96669SSteven Rostedt    my $prvalue = process_variables($rvalue);
723cad96669SSteven Rostedt
72425bc70faSScott Wood    if ($lvalue =~ /^(TEST|BISECT|CONFIG_BISECT)_TYPE(\[.*\])?$/ &&
72525bc70faSScott Wood	$prvalue !~ /^(config_|)bisect$/ &&
72625bc70faSScott Wood	$prvalue !~ /^build$/ &&
72725bc70faSScott Wood	$buildonly) {
72825bc70faSScott Wood
729bb8474b1SSteven Rostedt	# Note if a test is something other than build, then we
73022722799SPavel Machek	# will need other mandatory options.
731cad96669SSteven Rostedt	if ($prvalue ne "install") {
732bb8474b1SSteven Rostedt	    $buildonly = 0;
733165708b2SSteven Rostedt	} else {
73422722799SPavel Machek	    # install still limits some mandatory options.
735165708b2SSteven Rostedt	    $buildonly = 2;
736165708b2SSteven Rostedt	}
737bb8474b1SSteven Rostedt    }
738bb8474b1SSteven Rostedt
739a75fececSSteven Rostedt    if (defined($opt{$lvalue})) {
7403d1cc414SSteven Rostedt	if (!$override || defined(${$overrides}{$lvalue})) {
7413d1cc414SSteven Rostedt	    my $extra = "";
7423d1cc414SSteven Rostedt	    if ($override) {
7433d1cc414SSteven Rostedt		$extra = "In the same override section!\n";
7443d1cc414SSteven Rostedt	    }
7453d1cc414SSteven Rostedt	    die "$name: $.: Option $lvalue defined more than once!\n$extra";
7463d1cc414SSteven Rostedt	}
747cad96669SSteven Rostedt	${$overrides}{$lvalue} = $prvalue;
748a75fececSSteven Rostedt    }
74922c37a9aSSteven Rostedt (Red Hat)
750cad96669SSteven Rostedt    $opt{$lvalue} = $prvalue;
75121a9679fSSteven Rostedt}
752a57419b3SSteven Rostedt
753c75d22d9SSteven Rostedt (Red Hat)sub set_eval {
754c75d22d9SSteven Rostedt (Red Hat)    my ($lvalue, $rvalue, $name) = @_;
755c75d22d9SSteven Rostedt (Red Hat)
756c75d22d9SSteven Rostedt (Red Hat)    my $prvalue = process_variables($rvalue);
757c75d22d9SSteven Rostedt (Red Hat)    my $arr;
758c75d22d9SSteven Rostedt (Red Hat)
759c75d22d9SSteven Rostedt (Red Hat)    if (defined($evals{$lvalue})) {
760c75d22d9SSteven Rostedt (Red Hat)	$arr = $evals{$lvalue};
761c75d22d9SSteven Rostedt (Red Hat)    } else {
762c75d22d9SSteven Rostedt (Red Hat)	$arr = [];
763c75d22d9SSteven Rostedt (Red Hat)	$evals{$lvalue} = $arr;
764c75d22d9SSteven Rostedt (Red Hat)    }
765c75d22d9SSteven Rostedt (Red Hat)
766c75d22d9SSteven Rostedt (Red Hat)    push @{$arr}, $rvalue;
767c75d22d9SSteven Rostedt (Red Hat)}
768c75d22d9SSteven Rostedt (Red Hat)
76977d942ceSSteven Rostedtsub set_variable {
77077d942ceSSteven Rostedt    my ($lvalue, $rvalue) = @_;
77177d942ceSSteven Rostedt
77277d942ceSSteven Rostedt    if ($rvalue =~ /^\s*$/) {
77377d942ceSSteven Rostedt	delete $variable{$lvalue};
77477d942ceSSteven Rostedt    } else {
77577d942ceSSteven Rostedt	$rvalue = process_variables($rvalue);
77677d942ceSSteven Rostedt	$variable{$lvalue} = $rvalue;
77777d942ceSSteven Rostedt    }
77877d942ceSSteven Rostedt}
77977d942ceSSteven Rostedt
780ab7a3f52SSteven Rostedtsub process_compare {
781ab7a3f52SSteven Rostedt    my ($lval, $cmp, $rval) = @_;
782ab7a3f52SSteven Rostedt
783ab7a3f52SSteven Rostedt    # remove whitespace
784ab7a3f52SSteven Rostedt
785ab7a3f52SSteven Rostedt    $lval =~ s/^\s*//;
786ab7a3f52SSteven Rostedt    $lval =~ s/\s*$//;
787ab7a3f52SSteven Rostedt
788ab7a3f52SSteven Rostedt    $rval =~ s/^\s*//;
789ab7a3f52SSteven Rostedt    $rval =~ s/\s*$//;
790ab7a3f52SSteven Rostedt
791ab7a3f52SSteven Rostedt    if ($cmp eq "==") {
792ab7a3f52SSteven Rostedt	return $lval eq $rval;
793ab7a3f52SSteven Rostedt    } elsif ($cmp eq "!=") {
794ab7a3f52SSteven Rostedt	return $lval ne $rval;
7958fddbe9bSSteven Rostedt    } elsif ($cmp eq "=~") {
7968fddbe9bSSteven Rostedt	return $lval =~ m/$rval/;
7978fddbe9bSSteven Rostedt    } elsif ($cmp eq "!~") {
7988fddbe9bSSteven Rostedt	return $lval !~ m/$rval/;
799ab7a3f52SSteven Rostedt    }
800ab7a3f52SSteven Rostedt
801ab7a3f52SSteven Rostedt    my $statement = "$lval $cmp $rval";
802ab7a3f52SSteven Rostedt    my $ret = eval $statement;
803ab7a3f52SSteven Rostedt
804ab7a3f52SSteven Rostedt    # $@ stores error of eval
805ab7a3f52SSteven Rostedt    if ($@) {
806ab7a3f52SSteven Rostedt	return -1;
807ab7a3f52SSteven Rostedt    }
808ab7a3f52SSteven Rostedt
809ab7a3f52SSteven Rostedt    return $ret;
810ab7a3f52SSteven Rostedt}
811ab7a3f52SSteven Rostedt
8129900b5dcSSteven Rostedtsub value_defined {
8139900b5dcSSteven Rostedt    my ($val) = @_;
8149900b5dcSSteven Rostedt
8159900b5dcSSteven Rostedt    return defined($variable{$2}) ||
8169900b5dcSSteven Rostedt	defined($opt{$2});
8179900b5dcSSteven Rostedt}
8189900b5dcSSteven Rostedt
8198d735212SSteven Rostedtmy $d = 0;
8208d735212SSteven Rostedtsub process_expression {
8218d735212SSteven Rostedt    my ($name, $val) = @_;
82245d73a5dSSteven Rostedt
8238d735212SSteven Rostedt    my $c = $d++;
8248d735212SSteven Rostedt
8258d735212SSteven Rostedt    while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
8268d735212SSteven Rostedt	my $express = $1;
8278d735212SSteven Rostedt
8288d735212SSteven Rostedt	if (process_expression($name, $express)) {
8298d735212SSteven Rostedt	    $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
8308d735212SSteven Rostedt	} else {
8318d735212SSteven Rostedt	    $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
8328d735212SSteven Rostedt	}
8338d735212SSteven Rostedt    }
8348d735212SSteven Rostedt
8358d735212SSteven Rostedt    $d--;
8368d735212SSteven Rostedt    my $OR = "\\|\\|";
8378d735212SSteven Rostedt    my $AND = "\\&\\&";
8388d735212SSteven Rostedt
8398d735212SSteven Rostedt    while ($val =~ s/^(.*?)($OR|$AND)//) {
8408d735212SSteven Rostedt	my $express = $1;
8418d735212SSteven Rostedt	my $op = $2;
8428d735212SSteven Rostedt
8438d735212SSteven Rostedt	if (process_expression($name, $express)) {
8448d735212SSteven Rostedt	    if ($op eq "||") {
8458d735212SSteven Rostedt		return 1;
8468d735212SSteven Rostedt	    }
8478d735212SSteven Rostedt	} else {
8488d735212SSteven Rostedt	    if ($op eq "&&") {
8498d735212SSteven Rostedt		return 0;
8508d735212SSteven Rostedt	    }
8518d735212SSteven Rostedt	}
8528d735212SSteven Rostedt    }
85345d73a5dSSteven Rostedt
8548fddbe9bSSteven Rostedt    if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
855ab7a3f52SSteven Rostedt	my $ret = process_compare($1, $2, $3);
856ab7a3f52SSteven Rostedt	if ($ret < 0) {
857ab7a3f52SSteven Rostedt	    die "$name: $.: Unable to process comparison\n";
858ab7a3f52SSteven Rostedt	}
859ab7a3f52SSteven Rostedt	return $ret;
860ab7a3f52SSteven Rostedt    }
861ab7a3f52SSteven Rostedt
8629900b5dcSSteven Rostedt    if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
8639900b5dcSSteven Rostedt	if (defined $1) {
8649900b5dcSSteven Rostedt	    return !value_defined($2);
8659900b5dcSSteven Rostedt	} else {
8669900b5dcSSteven Rostedt	    return value_defined($2);
8679900b5dcSSteven Rostedt	}
8689900b5dcSSteven Rostedt    }
8699900b5dcSSteven Rostedt
87045d73a5dSSteven Rostedt    if ($val =~ /^\s*0\s*$/) {
87145d73a5dSSteven Rostedt	return 0;
87245d73a5dSSteven Rostedt    } elsif ($val =~ /^\s*\d+\s*$/) {
87345d73a5dSSteven Rostedt	return 1;
87445d73a5dSSteven Rostedt    }
87545d73a5dSSteven Rostedt
8769900b5dcSSteven Rostedt    die ("$name: $.: Undefined content $val in if statement\n");
8778d735212SSteven Rostedt}
8788d735212SSteven Rostedt
8798d735212SSteven Rostedtsub process_if {
8808d735212SSteven Rostedt    my ($name, $value) = @_;
8818d735212SSteven Rostedt
8828d735212SSteven Rostedt    # Convert variables and replace undefined ones with 0
8838d735212SSteven Rostedt    my $val = process_variables($value, 1);
8848d735212SSteven Rostedt    my $ret = process_expression $name, $val;
8858d735212SSteven Rostedt
8868d735212SSteven Rostedt    return $ret;
88745d73a5dSSteven Rostedt}
88845d73a5dSSteven Rostedt
8892ed3b161SSteven Rostedtsub __read_config {
8902ed3b161SSteven Rostedt    my ($config, $current_test_num) = @_;
891a57419b3SSteven Rostedt
8922ed3b161SSteven Rostedt    my $in;
8932ed3b161SSteven Rostedt    open($in, $config) || die "can't read file $config";
894a57419b3SSteven Rostedt
895a57419b3SSteven Rostedt    my $name = $config;
896a57419b3SSteven Rostedt    $name =~ s,.*/(.*),$1,;
897a57419b3SSteven Rostedt
8982ed3b161SSteven Rostedt    my $test_num = $$current_test_num;
899a57419b3SSteven Rostedt    my $default = 1;
900a57419b3SSteven Rostedt    my $repeat = 1;
901a57419b3SSteven Rostedt    my $num_tests_set = 0;
902a57419b3SSteven Rostedt    my $skip = 0;
903a57419b3SSteven Rostedt    my $rest;
904a9f84424SSteven Rostedt    my $line;
9050df213caSSteven Rostedt    my $test_case = 0;
90645d73a5dSSteven Rostedt    my $if = 0;
90745d73a5dSSteven Rostedt    my $if_set = 0;
9083d1cc414SSteven Rostedt    my $override = 0;
9093d1cc414SSteven Rostedt
9103d1cc414SSteven Rostedt    my %overrides;
911a57419b3SSteven Rostedt
9122ed3b161SSteven Rostedt    while (<$in>) {
913a57419b3SSteven Rostedt
914a57419b3SSteven Rostedt	# ignore blank lines and comments
915a57419b3SSteven Rostedt	next if (/^\s*$/ || /\s*\#/);
916a57419b3SSteven Rostedt
9170050b6bbSSteven Rostedt	if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
918a57419b3SSteven Rostedt
9190050b6bbSSteven Rostedt	    my $type = $1;
9200050b6bbSSteven Rostedt	    $rest = $2;
921a9f84424SSteven Rostedt	    $line = $2;
9220050b6bbSSteven Rostedt
9230050b6bbSSteven Rostedt	    my $old_test_num;
9240050b6bbSSteven Rostedt	    my $old_repeat;
9253d1cc414SSteven Rostedt	    $override = 0;
9260050b6bbSSteven Rostedt
9270050b6bbSSteven Rostedt	    if ($type eq "TEST_START") {
928a57419b3SSteven Rostedt
929a57419b3SSteven Rostedt		if ($num_tests_set) {
930a57419b3SSteven Rostedt		    die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
931a57419b3SSteven Rostedt		}
932a57419b3SSteven Rostedt
9330050b6bbSSteven Rostedt		$old_test_num = $test_num;
9340050b6bbSSteven Rostedt		$old_repeat = $repeat;
935a57419b3SSteven Rostedt
936a57419b3SSteven Rostedt		$test_num += $repeat;
937a57419b3SSteven Rostedt		$default = 0;
938a57419b3SSteven Rostedt		$repeat = 1;
9390050b6bbSSteven Rostedt	    } else {
9400050b6bbSSteven Rostedt		$default = 1;
9410050b6bbSSteven Rostedt	    }
942a57419b3SSteven Rostedt
943a9f84424SSteven Rostedt	    # If SKIP is anywhere in the line, the command will be skipped
944a9f84424SSteven Rostedt	    if ($rest =~ s/\s+SKIP\b//) {
945a57419b3SSteven Rostedt		$skip = 1;
946a57419b3SSteven Rostedt	    } else {
9470df213caSSteven Rostedt		$test_case = 1;
948a57419b3SSteven Rostedt		$skip = 0;
949a57419b3SSteven Rostedt	    }
950a57419b3SSteven Rostedt
951a9f84424SSteven Rostedt	    if ($rest =~ s/\sELSE\b//) {
952a9f84424SSteven Rostedt		if (!$if) {
953a9f84424SSteven Rostedt		    die "$name: $.: ELSE found with out matching IF section\n$_";
954a57419b3SSteven Rostedt		}
955a9f84424SSteven Rostedt		$if = 0;
956a9f84424SSteven Rostedt
957a9f84424SSteven Rostedt		if ($if_set) {
958a9f84424SSteven Rostedt		    $skip = 1;
959a9f84424SSteven Rostedt		} else {
960a9f84424SSteven Rostedt		    $skip = 0;
9613d1cc414SSteven Rostedt		}
9623d1cc414SSteven Rostedt	    }
963a57419b3SSteven Rostedt
964a9f84424SSteven Rostedt	    if ($rest =~ s/\sIF\s+(.*)//) {
96545d73a5dSSteven Rostedt		if (process_if($name, $1)) {
96645d73a5dSSteven Rostedt		    $if_set = 1;
96745d73a5dSSteven Rostedt		} else {
968a57419b3SSteven Rostedt		    $skip = 1;
969a57419b3SSteven Rostedt		}
97045d73a5dSSteven Rostedt		$if = 1;
97145d73a5dSSteven Rostedt	    } else {
97245d73a5dSSteven Rostedt		$if = 0;
973a9f84424SSteven Rostedt		$if_set = 0;
97445d73a5dSSteven Rostedt	    }
975a57419b3SSteven Rostedt
976a9f84424SSteven Rostedt	    if (!$skip) {
977a9f84424SSteven Rostedt		if ($type eq "TEST_START") {
978a9f84424SSteven Rostedt		    if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
979a9f84424SSteven Rostedt			$repeat = $1;
980a9f84424SSteven Rostedt			$repeat_tests{"$test_num"} = $repeat;
981a9f84424SSteven Rostedt		    }
982a9f84424SSteven Rostedt		} elsif ($rest =~ s/\sOVERRIDE\b//) {
983a9f84424SSteven Rostedt		    # DEFAULT only
984a9f84424SSteven Rostedt		    $override = 1;
985a9f84424SSteven Rostedt		    # Clear previous overrides
986a9f84424SSteven Rostedt		    %overrides = ();
987a9f84424SSteven Rostedt		}
988a9f84424SSteven Rostedt	    }
989a9f84424SSteven Rostedt
990a9f84424SSteven Rostedt	    if (!$skip && $rest !~ /^\s*$/) {
9910050b6bbSSteven Rostedt		die "$name: $.: Gargbage found after $type\n$_";
992a57419b3SSteven Rostedt	    }
993a57419b3SSteven Rostedt
9940050b6bbSSteven Rostedt	    if ($skip && $type eq "TEST_START") {
995a57419b3SSteven Rostedt		$test_num = $old_test_num;
996e48c5293SSteven Rostedt		$repeat = $old_repeat;
997a57419b3SSteven Rostedt	    }
998a57419b3SSteven Rostedt
999ab7a3f52SSteven Rostedt	} elsif (/^\s*ELSE\b(.*)$/) {
100045d73a5dSSteven Rostedt	    if (!$if) {
100145d73a5dSSteven Rostedt		die "$name: $.: ELSE found with out matching IF section\n$_";
100245d73a5dSSteven Rostedt	    }
100345d73a5dSSteven Rostedt	    $rest = $1;
100445d73a5dSSteven Rostedt	    if ($if_set) {
100545d73a5dSSteven Rostedt		$skip = 1;
1006ab7a3f52SSteven Rostedt		$rest = "";
100745d73a5dSSteven Rostedt	    } else {
100845d73a5dSSteven Rostedt		$skip = 0;
100945d73a5dSSteven Rostedt
1010ab7a3f52SSteven Rostedt		if ($rest =~ /\sIF\s+(.*)/) {
101145d73a5dSSteven Rostedt		    # May be a ELSE IF section.
101295f57838SSteven Rostedt		    if (process_if($name, $1)) {
101395f57838SSteven Rostedt			$if_set = 1;
101495f57838SSteven Rostedt		    } else {
101545d73a5dSSteven Rostedt			$skip = 1;
101645d73a5dSSteven Rostedt		    }
1017ab7a3f52SSteven Rostedt		    $rest = "";
101845d73a5dSSteven Rostedt		} else {
101945d73a5dSSteven Rostedt		    $if = 0;
102045d73a5dSSteven Rostedt		}
102145d73a5dSSteven Rostedt	    }
102245d73a5dSSteven Rostedt
1023ab7a3f52SSteven Rostedt	    if ($rest !~ /^\s*$/) {
1024ab7a3f52SSteven Rostedt		die "$name: $.: Gargbage found after DEFAULTS\n$_";
1025ab7a3f52SSteven Rostedt	    }
1026ab7a3f52SSteven Rostedt
10272ed3b161SSteven Rostedt	} elsif (/^\s*INCLUDE\s+(\S+)/) {
10282ed3b161SSteven Rostedt
10292ed3b161SSteven Rostedt	    next if ($skip);
10302ed3b161SSteven Rostedt
10312ed3b161SSteven Rostedt	    if (!$default) {
10322ed3b161SSteven Rostedt		die "$name: $.: INCLUDE can only be done in default sections\n$_";
10332ed3b161SSteven Rostedt	    }
10342ed3b161SSteven Rostedt
10352ed3b161SSteven Rostedt	    my $file = process_variables($1);
10362ed3b161SSteven Rostedt
10372ed3b161SSteven Rostedt	    if ($file !~ m,^/,) {
10382ed3b161SSteven Rostedt		# check the path of the config file first
10392ed3b161SSteven Rostedt		if ($config =~ m,(.*)/,) {
10402ed3b161SSteven Rostedt		    if (-f "$1/$file") {
10412ed3b161SSteven Rostedt			$file = "$1/$file";
10422ed3b161SSteven Rostedt		    }
10432ed3b161SSteven Rostedt		}
10442ed3b161SSteven Rostedt	    }
10452ed3b161SSteven Rostedt
10462ed3b161SSteven Rostedt	    if ( ! -r $file ) {
10472ed3b161SSteven Rostedt		die "$name: $.: Can't read file $file\n$_";
10482ed3b161SSteven Rostedt	    }
10492ed3b161SSteven Rostedt
10502ed3b161SSteven Rostedt	    if (__read_config($file, \$test_num)) {
10512ed3b161SSteven Rostedt		$test_case = 1;
10522ed3b161SSteven Rostedt	    }
10532ed3b161SSteven Rostedt
1054c75d22d9SSteven Rostedt (Red Hat)	} elsif (/^\s*([A-Z_\[\]\d]+)\s*=~\s*(.*?)\s*$/) {
1055c75d22d9SSteven Rostedt (Red Hat)
1056c75d22d9SSteven Rostedt (Red Hat)	    next if ($skip);
1057c75d22d9SSteven Rostedt (Red Hat)
1058c75d22d9SSteven Rostedt (Red Hat)	    my $lvalue = $1;
1059c75d22d9SSteven Rostedt (Red Hat)	    my $rvalue = $2;
1060c75d22d9SSteven Rostedt (Red Hat)
1061c75d22d9SSteven Rostedt (Red Hat)	    if ($default || $lvalue =~ /\[\d+\]$/) {
1062c75d22d9SSteven Rostedt (Red Hat)		set_eval($lvalue, $rvalue, $name);
1063c75d22d9SSteven Rostedt (Red Hat)	    } else {
1064c75d22d9SSteven Rostedt (Red Hat)		my $val = "$lvalue\[$test_num\]";
1065c75d22d9SSteven Rostedt (Red Hat)		set_eval($val, $rvalue, $name);
1066c75d22d9SSteven Rostedt (Red Hat)	    }
1067c75d22d9SSteven Rostedt (Red Hat)
1068a57419b3SSteven Rostedt	} elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
1069a57419b3SSteven Rostedt
1070a57419b3SSteven Rostedt	    next if ($skip);
1071a57419b3SSteven Rostedt
1072a57419b3SSteven Rostedt	    my $lvalue = $1;
1073a57419b3SSteven Rostedt	    my $rvalue = $2;
1074a57419b3SSteven Rostedt
1075a57419b3SSteven Rostedt	    if (!$default &&
1076a57419b3SSteven Rostedt		($lvalue eq "NUM_TESTS" ||
1077a57419b3SSteven Rostedt		 $lvalue eq "LOG_FILE" ||
1078a57419b3SSteven Rostedt		 $lvalue eq "CLEAR_LOG")) {
1079a57419b3SSteven Rostedt		die "$name: $.: $lvalue must be set in DEFAULTS section\n";
1080a57419b3SSteven Rostedt	    }
1081a57419b3SSteven Rostedt
1082a57419b3SSteven Rostedt	    if ($lvalue eq "NUM_TESTS") {
1083a57419b3SSteven Rostedt		if ($test_num) {
1084a57419b3SSteven Rostedt		    die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
1085a57419b3SSteven Rostedt		}
1086a57419b3SSteven Rostedt		if (!$default) {
1087a57419b3SSteven Rostedt		    die "$name: $.: NUM_TESTS must be set in default section\n";
1088a57419b3SSteven Rostedt		}
1089a57419b3SSteven Rostedt		$num_tests_set = 1;
1090a57419b3SSteven Rostedt	    }
1091a57419b3SSteven Rostedt
1092a57419b3SSteven Rostedt	    if ($default || $lvalue =~ /\[\d+\]$/) {
10933d1cc414SSteven Rostedt		set_value($lvalue, $rvalue, $override, \%overrides, $name);
1094a57419b3SSteven Rostedt	    } else {
1095a57419b3SSteven Rostedt		my $val = "$lvalue\[$test_num\]";
10963d1cc414SSteven Rostedt		set_value($val, $rvalue, $override, \%overrides, $name);
1097a57419b3SSteven Rostedt
1098a57419b3SSteven Rostedt		if ($repeat > 1) {
1099a57419b3SSteven Rostedt		    $repeats{$val} = $repeat;
1100a57419b3SSteven Rostedt		}
1101a57419b3SSteven Rostedt	    }
110277d942ceSSteven Rostedt	} elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
110377d942ceSSteven Rostedt	    next if ($skip);
110477d942ceSSteven Rostedt
110577d942ceSSteven Rostedt	    my $lvalue = $1;
110677d942ceSSteven Rostedt	    my $rvalue = $2;
110777d942ceSSteven Rostedt
110877d942ceSSteven Rostedt	    # process config variables.
110977d942ceSSteven Rostedt	    # Config variables are only active while reading the
111077d942ceSSteven Rostedt	    # config and can be defined anywhere. They also ignore
111177d942ceSSteven Rostedt	    # TEST_START and DEFAULTS, but are skipped if they are in
111277d942ceSSteven Rostedt	    # on of these sections that have SKIP defined.
111377d942ceSSteven Rostedt	    # The save variable can be
111477d942ceSSteven Rostedt	    # defined multiple times and the new one simply overrides
111577d942ceSSteven Rostedt	    # the prevous one.
111677d942ceSSteven Rostedt	    set_variable($lvalue, $rvalue);
111777d942ceSSteven Rostedt
1118a57419b3SSteven Rostedt	} else {
1119a57419b3SSteven Rostedt	    die "$name: $.: Garbage found in config\n$_";
1120a57419b3SSteven Rostedt	}
11212545eb61SSteven Rostedt    }
11222545eb61SSteven Rostedt
1123a57419b3SSteven Rostedt    if ($test_num) {
1124a57419b3SSteven Rostedt	$test_num += $repeat - 1;
1125a57419b3SSteven Rostedt	$opt{"NUM_TESTS"} = $test_num;
1126a57419b3SSteven Rostedt    }
1127a57419b3SSteven Rostedt
11282ed3b161SSteven Rostedt    close($in);
11292ed3b161SSteven Rostedt
11302ed3b161SSteven Rostedt    $$current_test_num = $test_num;
11312ed3b161SSteven Rostedt
11322ed3b161SSteven Rostedt    return $test_case;
11332ed3b161SSteven Rostedt}
11342ed3b161SSteven Rostedt
1135c4261d0fSSteven Rostedtsub get_test_case {
1136c4261d0fSSteven Rostedt	print "What test case would you like to run?\n";
1137c4261d0fSSteven Rostedt	print " (build, install or boot)\n";
1138edbd0edeSScott Wood	print " Other tests are available but require editing ktest.conf\n";
1139edbd0edeSScott Wood	print " (see tools/testing/ktest/sample.conf)\n";
1140c4261d0fSSteven Rostedt	my $ans = <STDIN>;
1141c4261d0fSSteven Rostedt	chomp $ans;
1142c4261d0fSSteven Rostedt	$default{"TEST_TYPE"} = $ans;
1143c4261d0fSSteven Rostedt}
1144c4261d0fSSteven Rostedt
11452ed3b161SSteven Rostedtsub read_config {
11462ed3b161SSteven Rostedt    my ($config) = @_;
11472ed3b161SSteven Rostedt
11482ed3b161SSteven Rostedt    my $test_case;
11492ed3b161SSteven Rostedt    my $test_num = 0;
11502ed3b161SSteven Rostedt
11512ed3b161SSteven Rostedt    $test_case = __read_config $config, \$test_num;
11522ed3b161SSteven Rostedt
11538d1491baSSteven Rostedt    # make sure we have all mandatory configs
11545269faadSSatoru Takeuchi    get_mandatory_configs;
11558d1491baSSteven Rostedt
11560df213caSSteven Rostedt    # was a test specified?
11570df213caSSteven Rostedt    if (!$test_case) {
11580df213caSSteven Rostedt	print "No test case specified.\n";
1159c4261d0fSSteven Rostedt	get_test_case;
11600df213caSSteven Rostedt    }
11610df213caSSteven Rostedt
1162a75fececSSteven Rostedt    # set any defaults
1163a75fececSSteven Rostedt
1164a75fececSSteven Rostedt    foreach my $default (keys %default) {
1165a75fececSSteven Rostedt	if (!defined($opt{$default})) {
1166a75fececSSteven Rostedt	    $opt{$default} = $default{$default};
1167a75fececSSteven Rostedt	}
1168a75fececSSteven Rostedt    }
11699cc9e091SSteven Rostedt
11709cc9e091SSteven Rostedt    if ($opt{"IGNORE_UNUSED"} == 1) {
11719cc9e091SSteven Rostedt	return;
11729cc9e091SSteven Rostedt    }
11739cc9e091SSteven Rostedt
11749cc9e091SSteven Rostedt    my %not_used;
11759cc9e091SSteven Rostedt
11769cc9e091SSteven Rostedt    # check if there are any stragglers (typos?)
11779cc9e091SSteven Rostedt    foreach my $option (keys %opt) {
11789cc9e091SSteven Rostedt	my $op = $option;
11799cc9e091SSteven Rostedt	# remove per test labels.
11809cc9e091SSteven Rostedt	$op =~ s/\[.*\]//;
11819cc9e091SSteven Rostedt	if (!exists($option_map{$op}) &&
11829cc9e091SSteven Rostedt	    !exists($default{$op}) &&
11839cc9e091SSteven Rostedt	    !exists($used_options{$op})) {
11849cc9e091SSteven Rostedt	    $not_used{$op} = 1;
11859cc9e091SSteven Rostedt	}
11869cc9e091SSteven Rostedt    }
11879cc9e091SSteven Rostedt
11889cc9e091SSteven Rostedt    if (%not_used) {
11899cc9e091SSteven Rostedt	my $s = "s are";
11909cc9e091SSteven Rostedt	$s = " is" if (keys %not_used == 1);
11919cc9e091SSteven Rostedt	print "The following option$s not used; could be a typo:\n";
11929cc9e091SSteven Rostedt	foreach my $option (keys %not_used) {
11939cc9e091SSteven Rostedt	    print "$option\n";
11949cc9e091SSteven Rostedt	}
11959cc9e091SSteven Rostedt	print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
11969cc9e091SSteven Rostedt	if (!read_yn "Do you want to continue?") {
11979cc9e091SSteven Rostedt	    exit -1;
11989cc9e091SSteven Rostedt	}
11999cc9e091SSteven Rostedt    }
12002545eb61SSteven Rostedt}
12012545eb61SSteven Rostedt
120223715c3cSSteven Rostedtsub __eval_option {
120304262be3SSteven Rostedt (Red Hat)    my ($name, $option, $i) = @_;
120423715c3cSSteven Rostedt
120523715c3cSSteven Rostedt    # Add space to evaluate the character before $
120623715c3cSSteven Rostedt    $option = " $option";
120723715c3cSSteven Rostedt    my $retval = "";
1208f9dfb65bSRabin Vincent    my $repeated = 0;
1209f9dfb65bSRabin Vincent    my $parent = 0;
1210f9dfb65bSRabin Vincent
1211f9dfb65bSRabin Vincent    foreach my $test (keys %repeat_tests) {
1212f9dfb65bSRabin Vincent	if ($i >= $test &&
1213f9dfb65bSRabin Vincent	    $i < $test + $repeat_tests{$test}) {
1214f9dfb65bSRabin Vincent
1215f9dfb65bSRabin Vincent	    $repeated = 1;
1216f9dfb65bSRabin Vincent	    $parent = $test;
1217f9dfb65bSRabin Vincent	    last;
1218f9dfb65bSRabin Vincent	}
1219f9dfb65bSRabin Vincent    }
122023715c3cSSteven Rostedt
122123715c3cSSteven Rostedt    while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
122223715c3cSSteven Rostedt	my $start = $1;
122323715c3cSSteven Rostedt	my $var = $2;
122423715c3cSSteven Rostedt	my $end = $3;
122523715c3cSSteven Rostedt
122623715c3cSSteven Rostedt	# Append beginning of line
122723715c3cSSteven Rostedt	$retval = "$retval$start";
122823715c3cSSteven Rostedt
122923715c3cSSteven Rostedt	# If the iteration option OPT[$i] exists, then use that.
123023715c3cSSteven Rostedt	# otherwise see if the default OPT (without [$i]) exists.
123123715c3cSSteven Rostedt
123223715c3cSSteven Rostedt	my $o = "$var\[$i\]";
1233f9dfb65bSRabin Vincent	my $parento = "$var\[$parent\]";
123423715c3cSSteven Rostedt
123504262be3SSteven Rostedt (Red Hat)	# If a variable contains itself, use the default var
123604262be3SSteven Rostedt (Red Hat)	if (($var eq $name) && defined($opt{$var})) {
123704262be3SSteven Rostedt (Red Hat)	    $o = $opt{$var};
123804262be3SSteven Rostedt (Red Hat)	    $retval = "$retval$o";
123904262be3SSteven Rostedt (Red Hat)	} elsif (defined($opt{$o})) {
124023715c3cSSteven Rostedt	    $o = $opt{$o};
124123715c3cSSteven Rostedt	    $retval = "$retval$o";
1242f9dfb65bSRabin Vincent	} elsif ($repeated && defined($opt{$parento})) {
1243f9dfb65bSRabin Vincent	    $o = $opt{$parento};
1244f9dfb65bSRabin Vincent	    $retval = "$retval$o";
124523715c3cSSteven Rostedt	} elsif (defined($opt{$var})) {
124623715c3cSSteven Rostedt	    $o = $opt{$var};
124723715c3cSSteven Rostedt	    $retval = "$retval$o";
12488e80bf05SSteven Rostedt (Red Hat)	} elsif ($var eq "KERNEL_VERSION" && defined($make)) {
12498e80bf05SSteven Rostedt (Red Hat)	    # special option KERNEL_VERSION uses kernel version
12508e80bf05SSteven Rostedt (Red Hat)	    get_version();
12518e80bf05SSteven Rostedt (Red Hat)	    $retval = "$retval$version";
125223715c3cSSteven Rostedt	} else {
125323715c3cSSteven Rostedt	    $retval = "$retval\$\{$var\}";
125423715c3cSSteven Rostedt	}
125523715c3cSSteven Rostedt
125623715c3cSSteven Rostedt	$option = $end;
125723715c3cSSteven Rostedt    }
125823715c3cSSteven Rostedt
125923715c3cSSteven Rostedt    $retval = "$retval$option";
126023715c3cSSteven Rostedt
126123715c3cSSteven Rostedt    $retval =~ s/^ //;
126223715c3cSSteven Rostedt
126323715c3cSSteven Rostedt    return $retval;
126423715c3cSSteven Rostedt}
126523715c3cSSteven Rostedt
1266c75d22d9SSteven Rostedt (Red Hat)sub process_evals {
1267c75d22d9SSteven Rostedt (Red Hat)    my ($name, $option, $i) = @_;
1268c75d22d9SSteven Rostedt (Red Hat)
1269c75d22d9SSteven Rostedt (Red Hat)    my $option_name = "$name\[$i\]";
1270c75d22d9SSteven Rostedt (Red Hat)    my $ev;
1271c75d22d9SSteven Rostedt (Red Hat)
1272c75d22d9SSteven Rostedt (Red Hat)    my $old_option = $option;
1273c75d22d9SSteven Rostedt (Red Hat)
1274c75d22d9SSteven Rostedt (Red Hat)    if (defined($evals{$option_name})) {
1275c75d22d9SSteven Rostedt (Red Hat)	$ev = $evals{$option_name};
1276c75d22d9SSteven Rostedt (Red Hat)    } elsif (defined($evals{$name})) {
1277c75d22d9SSteven Rostedt (Red Hat)	$ev = $evals{$name};
1278c75d22d9SSteven Rostedt (Red Hat)    } else {
1279c75d22d9SSteven Rostedt (Red Hat)	return $option;
1280c75d22d9SSteven Rostedt (Red Hat)    }
1281c75d22d9SSteven Rostedt (Red Hat)
1282c75d22d9SSteven Rostedt (Red Hat)    for my $e (@{$ev}) {
1283c75d22d9SSteven Rostedt (Red Hat)	eval "\$option =~ $e";
1284c75d22d9SSteven Rostedt (Red Hat)    }
1285c75d22d9SSteven Rostedt (Red Hat)
1286c75d22d9SSteven Rostedt (Red Hat)    if ($option ne $old_option) {
1287c75d22d9SSteven Rostedt (Red Hat)	doprint("$name changed from '$old_option' to '$option'\n");
1288c75d22d9SSteven Rostedt (Red Hat)    }
1289c75d22d9SSteven Rostedt (Red Hat)
1290c75d22d9SSteven Rostedt (Red Hat)    return $option;
1291c75d22d9SSteven Rostedt (Red Hat)}
1292c75d22d9SSteven Rostedt (Red Hat)
129323715c3cSSteven Rostedtsub eval_option {
129404262be3SSteven Rostedt (Red Hat)    my ($name, $option, $i) = @_;
129523715c3cSSteven Rostedt
129623715c3cSSteven Rostedt    my $prev = "";
129723715c3cSSteven Rostedt
129823715c3cSSteven Rostedt    # Since an option can evaluate to another option,
129923715c3cSSteven Rostedt    # keep iterating until we do not evaluate any more
130023715c3cSSteven Rostedt    # options.
130123715c3cSSteven Rostedt    my $r = 0;
130223715c3cSSteven Rostedt    while ($prev ne $option) {
130323715c3cSSteven Rostedt	# Check for recursive evaluations.
130423715c3cSSteven Rostedt	# 100 deep should be more than enough.
130523715c3cSSteven Rostedt	if ($r++ > 100) {
130623715c3cSSteven Rostedt	    die "Over 100 evaluations accurred with $option\n" .
130723715c3cSSteven Rostedt		"Check for recursive variables\n";
130823715c3cSSteven Rostedt	}
130923715c3cSSteven Rostedt	$prev = $option;
131004262be3SSteven Rostedt (Red Hat)	$option = __eval_option($name, $option, $i);
131123715c3cSSteven Rostedt    }
131223715c3cSSteven Rostedt
1313c75d22d9SSteven Rostedt (Red Hat)    $option = process_evals($name, $option, $i);
1314c75d22d9SSteven Rostedt (Red Hat)
131523715c3cSSteven Rostedt    return $option;
131623715c3cSSteven Rostedt}
131723715c3cSSteven Rostedt
13187faafbd6SSteven Rostedtsub run_command;
13192728be41SAndrew Jonessub start_monitor;
13202728be41SAndrew Jonessub end_monitor;
13212728be41SAndrew Jonessub wait_for_monitor;
13227faafbd6SSteven Rostedt
13237faafbd6SSteven Rostedtsub reboot {
13242728be41SAndrew Jones    my ($time) = @_;
13256474ace9SSteven Rostedt (VMware)    my $powercycle = 0;
13262728be41SAndrew Jones
13273e1d3678SSteven Rostedt (VMware)    # test if the machine can be connected to within a few seconds
13283e1d3678SSteven Rostedt (VMware)    my $stat = run_ssh("echo check machine status", $connect_timeout);
13296474ace9SSteven Rostedt (VMware)    if (!$stat) {
13306474ace9SSteven Rostedt (VMware)	doprint("power cycle\n");
13316474ace9SSteven Rostedt (VMware)	$powercycle = 1;
13326474ace9SSteven Rostedt (VMware)    }
13336474ace9SSteven Rostedt (VMware)
13346474ace9SSteven Rostedt (VMware)    if ($powercycle) {
13356474ace9SSteven Rostedt (VMware)	run_command "$power_cycle";
13366474ace9SSteven Rostedt (VMware)
13376474ace9SSteven Rostedt (VMware)	start_monitor;
13386474ace9SSteven Rostedt (VMware)	# flush out current monitor
13396474ace9SSteven Rostedt (VMware)	# May contain the reboot success line
13406474ace9SSteven Rostedt (VMware)	wait_for_monitor 1;
13416474ace9SSteven Rostedt (VMware)
13426474ace9SSteven Rostedt (VMware)    } else {
1343a4968722SSteven Rostedt	# Make sure everything has been written to disk
1344a4968722SSteven Rostedt	run_ssh("sync");
1345a4968722SSteven Rostedt
13462b803365SSteven Rostedt	if (defined($time)) {
13472b803365SSteven Rostedt	    start_monitor;
13482b803365SSteven Rostedt	    # flush out current monitor
13492b803365SSteven Rostedt	    # May contain the reboot success line
13502b803365SSteven Rostedt	    wait_for_monitor 1;
13512b803365SSteven Rostedt	}
13522b803365SSteven Rostedt
13537faafbd6SSteven Rostedt	# try to reboot normally
1354e48c5293SSteven Rostedt	if (run_command $reboot) {
1355576f627cSSteven Rostedt	    if (defined($powercycle_after_reboot)) {
1356576f627cSSteven Rostedt		sleep $powercycle_after_reboot;
1357576f627cSSteven Rostedt		run_command "$power_cycle";
1358576f627cSSteven Rostedt	    }
1359576f627cSSteven Rostedt	} else {
13607faafbd6SSteven Rostedt	    # nope? power cycle it.
1361a75fececSSteven Rostedt	    run_command "$power_cycle";
13627faafbd6SSteven Rostedt	}
13636474ace9SSteven Rostedt (VMware)    }
13642728be41SAndrew Jones
13652728be41SAndrew Jones    if (defined($time)) {
13664c0b67a2SSteven Rostedt (Red Hat)
13674c0b67a2SSteven Rostedt (Red Hat)	# We only want to get to the new kernel, don't fail
13684c0b67a2SSteven Rostedt (Red Hat)	# if we stumble over a call trace.
13694c0b67a2SSteven Rostedt (Red Hat)	my $save_ignore_errors = $ignore_errors;
13704c0b67a2SSteven Rostedt (Red Hat)	$ignore_errors = 1;
13714c0b67a2SSteven Rostedt (Red Hat)
1372d6845536SSteven Rostedt (Red Hat)	# Look for the good kernel to boot
1373d6845536SSteven Rostedt (Red Hat)	if (wait_for_monitor($time, "Linux version")) {
1374407b95b7SSteven Rostedt	    # reboot got stuck?
13758a80c727SSteven Rostedt	    doprint "Reboot did not finish. Forcing power cycle\n";
1376407b95b7SSteven Rostedt	    run_command "$power_cycle";
1377407b95b7SSteven Rostedt	}
1378d6845536SSteven Rostedt (Red Hat)
13794c0b67a2SSteven Rostedt (Red Hat)	$ignore_errors = $save_ignore_errors;
13804c0b67a2SSteven Rostedt (Red Hat)
1381d6845536SSteven Rostedt (Red Hat)	# Still need to wait for the reboot to finish
1382d6845536SSteven Rostedt (Red Hat)	wait_for_monitor($time, $reboot_success_line);
1383d6845536SSteven Rostedt (Red Hat)
13842728be41SAndrew Jones	end_monitor;
13852728be41SAndrew Jones    }
13867faafbd6SSteven Rostedt}
13877faafbd6SSteven Rostedt
1388bc7c5803SSteven Rostedtsub reboot_to_good {
1389bc7c5803SSteven Rostedt    my ($time) = @_;
1390bc7c5803SSteven Rostedt
1391bc7c5803SSteven Rostedt    if (defined($switch_to_good)) {
1392bc7c5803SSteven Rostedt	run_command $switch_to_good;
1393bc7c5803SSteven Rostedt    }
1394bc7c5803SSteven Rostedt
1395bc7c5803SSteven Rostedt    reboot $time;
1396bc7c5803SSteven Rostedt}
1397bc7c5803SSteven Rostedt
1398576f627cSSteven Rostedtsub do_not_reboot {
1399576f627cSSteven Rostedt    my $i = $iteration;
1400576f627cSSteven Rostedt
14014ab1cce5SSteven Rostedt    return $test_type eq "build" || $no_reboot ||
1402576f627cSSteven Rostedt	($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1403649c0f12SScott Wood	($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build") ||
1404649c0f12SScott Wood	($test_type eq "config_bisect" && $opt{"CONFIG_BISECT_TYPE[$i]"} eq "build");
1405576f627cSSteven Rostedt}
1406576f627cSSteven Rostedt
14075c42fc5bSSteven Rostedtsub dodie {
14085a391fbfSSteven Rostedt    doprint "CRITICAL FAILURE... ", @_, "\n";
14095c42fc5bSSteven Rostedt
1410576f627cSSteven Rostedt    my $i = $iteration;
1411576f627cSSteven Rostedt
1412576f627cSSteven Rostedt    if ($reboot_on_error && !do_not_reboot) {
1413576f627cSSteven Rostedt
141475c3fda7SSteven Rostedt	doprint "REBOOTING\n";
1415bc7c5803SSteven Rostedt	reboot_to_good;
141675c3fda7SSteven Rostedt
1417a75fececSSteven Rostedt    } elsif ($poweroff_on_error && defined($power_off)) {
14185c42fc5bSSteven Rostedt	doprint "POWERING OFF\n";
1419a75fececSSteven Rostedt	`$power_off`;
14205c42fc5bSSteven Rostedt    }
142175c3fda7SSteven Rostedt
1422f80802cbSSteven Rostedt    if (defined($opt{"LOG_FILE"})) {
1423f80802cbSSteven Rostedt	print " See $opt{LOG_FILE} for more info.\n";
1424f80802cbSSteven Rostedt    }
1425f80802cbSSteven Rostedt
14261cb9e642SJosh Poimboeuf    if ($monitor_cnt) {
14271cb9e642SJosh Poimboeuf	    # restore terminal settings
14281cb9e642SJosh Poimboeuf	    system("stty $stty_orig");
14291cb9e642SJosh Poimboeuf    }
14301cb9e642SJosh Poimboeuf
14312e07c9f5SSteven Rostedt (VMware)    if (defined($post_test)) {
14322e07c9f5SSteven Rostedt (VMware)	run_command $post_test;
14332e07c9f5SSteven Rostedt (VMware)    }
14342e07c9f5SSteven Rostedt (VMware)
1435576f627cSSteven Rostedt    die @_, "\n";
14365c42fc5bSSteven Rostedt}
14375c42fc5bSSteven Rostedt
14389f2cdcbbSJosh Poimboeufsub create_pty {
14399f2cdcbbSJosh Poimboeuf    my ($ptm, $pts) = @_;
14409f2cdcbbSJosh Poimboeuf    my $tmp;
14419f2cdcbbSJosh Poimboeuf    my $TIOCSPTLCK = 0x40045431;
14429f2cdcbbSJosh Poimboeuf    my $TIOCGPTN = 0x80045430;
14437faafbd6SSteven Rostedt
14449f2cdcbbSJosh Poimboeuf    sysopen($ptm, "/dev/ptmx", O_RDWR | O_NONBLOCK) or
14459f2cdcbbSJosh Poimboeuf	dodie "Cant open /dev/ptmx";
14469f2cdcbbSJosh Poimboeuf
14479f2cdcbbSJosh Poimboeuf    # unlockpt()
14489f2cdcbbSJosh Poimboeuf    $tmp = pack("i", 0);
14499f2cdcbbSJosh Poimboeuf    ioctl($ptm, $TIOCSPTLCK, $tmp) or
14509f2cdcbbSJosh Poimboeuf	dodie "ioctl TIOCSPTLCK for /dev/ptmx failed";
14519f2cdcbbSJosh Poimboeuf
14529f2cdcbbSJosh Poimboeuf    # ptsname()
14539f2cdcbbSJosh Poimboeuf    ioctl($ptm, $TIOCGPTN, $tmp) or
14549f2cdcbbSJosh Poimboeuf	dodie "ioctl TIOCGPTN for /dev/ptmx failed";
14559f2cdcbbSJosh Poimboeuf    $tmp = unpack("i", $tmp);
14569f2cdcbbSJosh Poimboeuf
14579f2cdcbbSJosh Poimboeuf    sysopen($pts, "/dev/pts/$tmp", O_RDWR | O_NONBLOCK) or
14589f2cdcbbSJosh Poimboeuf	dodie "Can't open /dev/pts/$tmp";
14599f2cdcbbSJosh Poimboeuf}
14609f2cdcbbSJosh Poimboeuf
14619f2cdcbbSJosh Poimboeufsub exec_console {
14629f2cdcbbSJosh Poimboeuf    my ($ptm, $pts) = @_;
14639f2cdcbbSJosh Poimboeuf
14649f2cdcbbSJosh Poimboeuf    close($ptm);
14659f2cdcbbSJosh Poimboeuf
14669f2cdcbbSJosh Poimboeuf    close(\*STDIN);
14679f2cdcbbSJosh Poimboeuf    close(\*STDOUT);
14689f2cdcbbSJosh Poimboeuf    close(\*STDERR);
14699f2cdcbbSJosh Poimboeuf
14709f2cdcbbSJosh Poimboeuf    open(\*STDIN, '<&', $pts);
14719f2cdcbbSJosh Poimboeuf    open(\*STDOUT, '>&', $pts);
14729f2cdcbbSJosh Poimboeuf    open(\*STDERR, '>&', $pts);
14739f2cdcbbSJosh Poimboeuf
14749f2cdcbbSJosh Poimboeuf    close($pts);
14759f2cdcbbSJosh Poimboeuf
14769f2cdcbbSJosh Poimboeuf    exec $console or
14771cb9e642SJosh Poimboeuf	die "Can't open console $console";
14789f2cdcbbSJosh Poimboeuf}
14799f2cdcbbSJosh Poimboeuf
14809f2cdcbbSJosh Poimboeufsub open_console {
14819f2cdcbbSJosh Poimboeuf    my ($ptm) = @_;
14829f2cdcbbSJosh Poimboeuf    my $pts = \*PTSFD;
14839f2cdcbbSJosh Poimboeuf    my $pid;
14847faafbd6SSteven Rostedt
148598842782SJosh Poimboeuf    # save terminal settings
14869d2f7f05SJosh Poimboeuf    $stty_orig = `stty -g`;
14879d2f7f05SJosh Poimboeuf
14889d2f7f05SJosh Poimboeuf    # place terminal in cbreak mode so that stdin can be read one character at
14899d2f7f05SJosh Poimboeuf    # a time without having to wait for a newline
14909d2f7f05SJosh Poimboeuf    system("stty -icanon -echo -icrnl");
149198842782SJosh Poimboeuf
14929f2cdcbbSJosh Poimboeuf    create_pty($ptm, $pts);
14937faafbd6SSteven Rostedt
14949f2cdcbbSJosh Poimboeuf    $pid = fork;
14959f2cdcbbSJosh Poimboeuf
14969f2cdcbbSJosh Poimboeuf    if (!$pid) {
14979f2cdcbbSJosh Poimboeuf	# child
14989f2cdcbbSJosh Poimboeuf	exec_console($ptm, $pts)
14999f2cdcbbSJosh Poimboeuf    }
15009f2cdcbbSJosh Poimboeuf
15019f2cdcbbSJosh Poimboeuf    # parent
15029f2cdcbbSJosh Poimboeuf    close($pts);
15037faafbd6SSteven Rostedt
15047faafbd6SSteven Rostedt    return $pid;
15059f2cdcbbSJosh Poimboeuf
15069f2cdcbbSJosh Poimboeuf    open(PTSFD, "Stop perl from warning about single use of PTSFD");
15077faafbd6SSteven Rostedt}
15087faafbd6SSteven Rostedt
15097faafbd6SSteven Rostedtsub close_console {
15107faafbd6SSteven Rostedt    my ($fp, $pid) = @_;
15117faafbd6SSteven Rostedt
15127faafbd6SSteven Rostedt    doprint "kill child process $pid\n";
15135a5d8e48SSatoru Takeuchi    kill $close_console_signal, $pid;
15147faafbd6SSteven Rostedt
15159f23b129SSteven Rostedt (VMware)    doprint "wait for child process $pid to exit\n";
15169f23b129SSteven Rostedt (VMware)    waitpid($pid, 0);
15179f23b129SSteven Rostedt (VMware)
15187faafbd6SSteven Rostedt    print "closing!\n";
15197faafbd6SSteven Rostedt    close($fp);
152098842782SJosh Poimboeuf
152198842782SJosh Poimboeuf    # restore terminal settings
15229d2f7f05SJosh Poimboeuf    system("stty $stty_orig");
15237faafbd6SSteven Rostedt}
15247faafbd6SSteven Rostedt
15257faafbd6SSteven Rostedtsub start_monitor {
15267faafbd6SSteven Rostedt    if ($monitor_cnt++) {
15277faafbd6SSteven Rostedt	return;
15287faafbd6SSteven Rostedt    }
15297faafbd6SSteven Rostedt    $monitor_fp = \*MONFD;
15307faafbd6SSteven Rostedt    $monitor_pid = open_console $monitor_fp;
1531a75fececSSteven Rostedt
1532a75fececSSteven Rostedt    return;
1533a75fececSSteven Rostedt
1534a75fececSSteven Rostedt    open(MONFD, "Stop perl from warning about single use of MONFD");
15357faafbd6SSteven Rostedt}
15367faafbd6SSteven Rostedt
15377faafbd6SSteven Rostedtsub end_monitor {
1538319ab14fSSteven Rostedt (Red Hat)    return if (!defined $console);
15397faafbd6SSteven Rostedt    if (--$monitor_cnt) {
15407faafbd6SSteven Rostedt	return;
15417faafbd6SSteven Rostedt    }
15427faafbd6SSteven Rostedt    close_console($monitor_fp, $monitor_pid);
15437faafbd6SSteven Rostedt}
15447faafbd6SSteven Rostedt
15457faafbd6SSteven Rostedtsub wait_for_monitor {
15462b803365SSteven Rostedt    my ($time, $stop) = @_;
15472b803365SSteven Rostedt    my $full_line = "";
15487faafbd6SSteven Rostedt    my $line;
15492b803365SSteven Rostedt    my $booted = 0;
1550407b95b7SSteven Rostedt    my $start_time = time;
15518a80c727SSteven Rostedt    my $skip_call_trace = 0;
15528a80c727SSteven Rostedt    my $bug = 0;
15538a80c727SSteven Rostedt    my $bug_ignored = 0;
1554407b95b7SSteven Rostedt    my $now;
15557faafbd6SSteven Rostedt
1556a75fececSSteven Rostedt    doprint "** Wait for monitor to settle down **\n";
15577faafbd6SSteven Rostedt
15587faafbd6SSteven Rostedt    # read the monitor and wait for the system to calm down
15592b803365SSteven Rostedt    while (!$booted) {
15607faafbd6SSteven Rostedt	$line = wait_for_input($monitor_fp, $time);
15612b803365SSteven Rostedt	last if (!defined($line));
15622b803365SSteven Rostedt	print "$line";
15632b803365SSteven Rostedt	$full_line .= $line;
15642b803365SSteven Rostedt
15652b803365SSteven Rostedt	if (defined($stop) && $full_line =~ /$stop/) {
15662b803365SSteven Rostedt	    doprint "wait for monitor detected $stop\n";
15672b803365SSteven Rostedt	    $booted = 1;
15682b803365SSteven Rostedt	}
15692b803365SSteven Rostedt
15708a80c727SSteven Rostedt	if ($full_line =~ /\[ backtrace testing \]/) {
15718a80c727SSteven Rostedt	    $skip_call_trace = 1;
15728a80c727SSteven Rostedt	}
15738a80c727SSteven Rostedt
15748a80c727SSteven Rostedt	if ($full_line =~ /call trace:/i) {
15758a80c727SSteven Rostedt	    if (!$bug && !$skip_call_trace) {
15768a80c727SSteven Rostedt		if ($ignore_errors) {
15778a80c727SSteven Rostedt		    $bug_ignored = 1;
15788a80c727SSteven Rostedt		} else {
15798a80c727SSteven Rostedt		    $bug = 1;
15808a80c727SSteven Rostedt		}
15818a80c727SSteven Rostedt	    }
15828a80c727SSteven Rostedt	}
15838a80c727SSteven Rostedt
15848a80c727SSteven Rostedt	if ($full_line =~ /\[ end of backtrace testing \]/) {
15858a80c727SSteven Rostedt	    $skip_call_trace = 0;
15868a80c727SSteven Rostedt	}
15878a80c727SSteven Rostedt
15888a80c727SSteven Rostedt	if ($full_line =~ /Kernel panic -/) {
15898a80c727SSteven Rostedt	    $bug = 1;
15908a80c727SSteven Rostedt	}
15918a80c727SSteven Rostedt
15922b803365SSteven Rostedt	if ($line =~ /\n/) {
15932b803365SSteven Rostedt	    $full_line = "";
15942b803365SSteven Rostedt	}
1595407b95b7SSteven Rostedt	$now = time;
1596407b95b7SSteven Rostedt	if ($now - $start_time >= $max_monitor_wait) {
1597407b95b7SSteven Rostedt	    doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
1598407b95b7SSteven Rostedt	    return 1;
1599407b95b7SSteven Rostedt	}
16002b803365SSteven Rostedt    }
1601a75fececSSteven Rostedt    print "** Monitor flushed **\n";
1602995bc431SSteven Rostedt (Red Hat)
1603995bc431SSteven Rostedt (Red Hat)    # if stop is defined but wasn't hit, return error
1604995bc431SSteven Rostedt (Red Hat)    # used by reboot (which wants to see a reboot)
1605995bc431SSteven Rostedt (Red Hat)    if (defined($stop) && !$booted) {
1606995bc431SSteven Rostedt (Red Hat)	$bug = 1;
1607995bc431SSteven Rostedt (Red Hat)    }
16088a80c727SSteven Rostedt    return $bug;
16097faafbd6SSteven Rostedt}
16107faafbd6SSteven Rostedt
1611de5b6e3bSRabin Vincentsub save_logs {
1612de5b6e3bSRabin Vincent	my ($result, $basedir) = @_;
1613de5b6e3bSRabin Vincent	my @t = localtime;
1614de5b6e3bSRabin Vincent	my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1615de5b6e3bSRabin Vincent		1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1616de5b6e3bSRabin Vincent
1617de5b6e3bSRabin Vincent	my $type = $build_type;
1618de5b6e3bSRabin Vincent	if ($type =~ /useconfig/) {
1619de5b6e3bSRabin Vincent	    $type = "useconfig";
1620de5b6e3bSRabin Vincent	}
1621de5b6e3bSRabin Vincent
1622de5b6e3bSRabin Vincent	my $dir = "$machine-$test_type-$type-$result-$date";
1623de5b6e3bSRabin Vincent
1624de5b6e3bSRabin Vincent	$dir = "$basedir/$dir";
1625de5b6e3bSRabin Vincent
1626de5b6e3bSRabin Vincent	if (!-d $dir) {
1627de5b6e3bSRabin Vincent	    mkpath($dir) or
1628de5b6e3bSRabin Vincent		die "can't create $dir";
1629de5b6e3bSRabin Vincent	}
1630de5b6e3bSRabin Vincent
1631de5b6e3bSRabin Vincent	my %files = (
1632de5b6e3bSRabin Vincent		"config" => $output_config,
1633de5b6e3bSRabin Vincent		"buildlog" => $buildlog,
1634de5b6e3bSRabin Vincent		"dmesg" => $dmesg,
1635de5b6e3bSRabin Vincent		"testlog" => $testlog,
1636de5b6e3bSRabin Vincent	);
1637de5b6e3bSRabin Vincent
1638de5b6e3bSRabin Vincent	while (my ($name, $source) = each(%files)) {
1639de5b6e3bSRabin Vincent		if (-f "$source") {
1640de5b6e3bSRabin Vincent			cp "$source", "$dir/$name" or
1641de5b6e3bSRabin Vincent				die "failed to copy $source";
1642de5b6e3bSRabin Vincent		}
1643de5b6e3bSRabin Vincent	}
1644de5b6e3bSRabin Vincent
1645de5b6e3bSRabin Vincent	doprint "*** Saved info to $dir ***\n";
1646de5b6e3bSRabin Vincent}
1647de5b6e3bSRabin Vincent
16482b7d9b21SSteven Rostedtsub fail {
16492b7d9b21SSteven Rostedt
1650a75fececSSteven Rostedt	if ($die_on_failure) {
16512b7d9b21SSteven Rostedt		dodie @_;
16522b7d9b21SSteven Rostedt	}
16532b7d9b21SSteven Rostedt
1654a75fececSSteven Rostedt	doprint "FAILED\n";
16557faafbd6SSteven Rostedt
1656576f627cSSteven Rostedt	my $i = $iteration;
1657576f627cSSteven Rostedt
1658a75fececSSteven Rostedt	# no need to reboot for just building.
1659576f627cSSteven Rostedt	if (!do_not_reboot) {
16607faafbd6SSteven Rostedt	    doprint "REBOOTING\n";
1661bc7c5803SSteven Rostedt	    reboot_to_good $sleep_time;
1662a75fececSSteven Rostedt	}
16637faafbd6SSteven Rostedt
16649064af52SSteven Rostedt	my $name = "";
16659064af52SSteven Rostedt
16669064af52SSteven Rostedt	if (defined($test_name)) {
16679064af52SSteven Rostedt	    $name = " ($test_name)";
16689064af52SSteven Rostedt	}
16699064af52SSteven Rostedt
16704bf6e1fcSSteven Rostedt (Red Hat)	print_times;
16714bf6e1fcSSteven Rostedt (Red Hat)
1672576f627cSSteven Rostedt	doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1673576f627cSSteven Rostedt	doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
16749064af52SSteven Rostedt	doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
1675576f627cSSteven Rostedt	doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1676576f627cSSteven Rostedt	doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1677a75fececSSteven Rostedt
1678de5b6e3bSRabin Vincent	if (defined($store_failures)) {
1679de5b6e3bSRabin Vincent	    save_logs "fail", $store_failures;
1680cccae1a6SSteven Rostedt        }
1681cccae1a6SSteven Rostedt
16822e07c9f5SSteven Rostedt (VMware)	if (defined($post_test)) {
16832e07c9f5SSteven Rostedt (VMware)		run_command $post_test;
16842e07c9f5SSteven Rostedt (VMware)	}
16852e07c9f5SSteven Rostedt (VMware)
16862b7d9b21SSteven Rostedt	return 1;
16872b7d9b21SSteven Rostedt}
16882b7d9b21SSteven Rostedt
16892545eb61SSteven Rostedtsub run_command {
16906e98d1b4SSteven Rostedt (VMware)    my ($command, $redirect, $timeout) = @_;
1691b53486e0SSteven Rostedt (Red Hat)    my $start_time;
1692b53486e0SSteven Rostedt (Red Hat)    my $end_time;
1693d6ce2a0bSSteven Rostedt    my $dolog = 0;
1694d6ce2a0bSSteven Rostedt    my $dord = 0;
1695d6ce2a0bSSteven Rostedt    my $pid;
1696d6ce2a0bSSteven Rostedt
1697e48c5293SSteven Rostedt    $command =~ s/\$SSH_USER/$ssh_user/g;
1698e48c5293SSteven Rostedt    $command =~ s/\$MACHINE/$machine/g;
1699e48c5293SSteven Rostedt
1700d6ce2a0bSSteven Rostedt    doprint("$command ... ");
17016e98d1b4SSteven Rostedt (VMware)    $start_time = time;
1702d6ce2a0bSSteven Rostedt
1703d6ce2a0bSSteven Rostedt    $pid = open(CMD, "$command 2>&1 |") or
17042b7d9b21SSteven Rostedt	(fail "unable to exec $command" and return 0);
17052545eb61SSteven Rostedt
17062545eb61SSteven Rostedt    if (defined($opt{"LOG_FILE"})) {
1707d6ce2a0bSSteven Rostedt	open(LOG, ">>$opt{LOG_FILE}") or
1708d6ce2a0bSSteven Rostedt	    dodie "failed to write to log";
1709d6ce2a0bSSteven Rostedt	$dolog = 1;
17106c5ee0beSSteven Rostedt    }
17116c5ee0beSSteven Rostedt
17126c5ee0beSSteven Rostedt    if (defined($redirect)) {
1713d6ce2a0bSSteven Rostedt	open (RD, ">$redirect") or
1714d6ce2a0bSSteven Rostedt	    dodie "failed to write to redirect $redirect";
1715d6ce2a0bSSteven Rostedt	$dord = 1;
17162545eb61SSteven Rostedt    }
17172545eb61SSteven Rostedt
17186e98d1b4SSteven Rostedt (VMware)    my $hit_timeout = 0;
17196e98d1b4SSteven Rostedt (VMware)
17206e98d1b4SSteven Rostedt (VMware)    while (1) {
17216e98d1b4SSteven Rostedt (VMware)	my $fp = \*CMD;
17226e98d1b4SSteven Rostedt (VMware)	if (defined($timeout)) {
17236e98d1b4SSteven Rostedt (VMware)	    doprint "timeout = $timeout\n";
17246e98d1b4SSteven Rostedt (VMware)	}
17256e98d1b4SSteven Rostedt (VMware)	my $line = wait_for_input($fp, $timeout);
17266e98d1b4SSteven Rostedt (VMware)	if (!defined($line)) {
17276e98d1b4SSteven Rostedt (VMware)	    my $now = time;
17286e98d1b4SSteven Rostedt (VMware)	    if (defined($timeout) && (($now - $start_time) >= $timeout)) {
17296e98d1b4SSteven Rostedt (VMware)		doprint "Hit timeout of $timeout, killing process\n";
17306e98d1b4SSteven Rostedt (VMware)		$hit_timeout = 1;
17316e98d1b4SSteven Rostedt (VMware)		kill 9, $pid;
17326e98d1b4SSteven Rostedt (VMware)	    }
17336e98d1b4SSteven Rostedt (VMware)	    last;
17346e98d1b4SSteven Rostedt (VMware)	}
17356e98d1b4SSteven Rostedt (VMware)	print LOG $line if ($dolog);
17366e98d1b4SSteven Rostedt (VMware)	print RD $line if ($dord);
1737d6ce2a0bSSteven Rostedt    }
17382545eb61SSteven Rostedt
1739d6ce2a0bSSteven Rostedt    waitpid($pid, 0);
17405739438bSSteven Rostedt (VMware)    # shift 8 for real exit status
17415739438bSSteven Rostedt (VMware)    $run_command_status = $? >> 8;
17422545eb61SSteven Rostedt
1743d6ce2a0bSSteven Rostedt    close(CMD);
1744d6ce2a0bSSteven Rostedt    close(LOG) if ($dolog);
1745d6ce2a0bSSteven Rostedt    close(RD)  if ($dord);
1746d6ce2a0bSSteven Rostedt
1747b53486e0SSteven Rostedt (Red Hat)    $end_time = time;
1748b53486e0SSteven Rostedt (Red Hat)    my $delta = $end_time - $start_time;
1749b53486e0SSteven Rostedt (Red Hat)
1750b53486e0SSteven Rostedt (Red Hat)    if ($delta == 1) {
1751b53486e0SSteven Rostedt (Red Hat)	doprint "[1 second] ";
1752b53486e0SSteven Rostedt (Red Hat)    } else {
1753b53486e0SSteven Rostedt (Red Hat)	doprint "[$delta seconds] ";
1754b53486e0SSteven Rostedt (Red Hat)    }
1755b53486e0SSteven Rostedt (Red Hat)
17565739438bSSteven Rostedt (VMware)    if ($hit_timeout) {
17575739438bSSteven Rostedt (VMware)	$run_command_status = 1;
17585739438bSSteven Rostedt (VMware)    }
17595739438bSSteven Rostedt (VMware)
17605739438bSSteven Rostedt (VMware)    if ($run_command_status) {
17612545eb61SSteven Rostedt	doprint "FAILED!\n";
17622545eb61SSteven Rostedt    } else {
17632545eb61SSteven Rostedt	doprint "SUCCESS\n";
17642545eb61SSteven Rostedt    }
17652545eb61SSteven Rostedt
17665739438bSSteven Rostedt (VMware)    return !$run_command_status;
17675f9b6cedSSteven Rostedt}
17685f9b6cedSSteven Rostedt
1769e48c5293SSteven Rostedtsub run_ssh {
17706e98d1b4SSteven Rostedt (VMware)    my ($cmd, $timeout) = @_;
1771e48c5293SSteven Rostedt    my $cp_exec = $ssh_exec;
1772e48c5293SSteven Rostedt
1773e48c5293SSteven Rostedt    $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
17746e98d1b4SSteven Rostedt (VMware)    return run_command "$cp_exec", undef , $timeout;
1775e48c5293SSteven Rostedt}
1776e48c5293SSteven Rostedt
1777e48c5293SSteven Rostedtsub run_scp {
177802ad2617SSteven Rostedt    my ($src, $dst, $cp_scp) = @_;
1779e48c5293SSteven Rostedt
1780e48c5293SSteven Rostedt    $cp_scp =~ s/\$SRC_FILE/$src/g;
1781e48c5293SSteven Rostedt    $cp_scp =~ s/\$DST_FILE/$dst/g;
1782e48c5293SSteven Rostedt
1783e48c5293SSteven Rostedt    return run_command "$cp_scp";
1784e48c5293SSteven Rostedt}
1785e48c5293SSteven Rostedt
178602ad2617SSteven Rostedtsub run_scp_install {
178702ad2617SSteven Rostedt    my ($src, $dst) = @_;
178802ad2617SSteven Rostedt
178902ad2617SSteven Rostedt    my $cp_scp = $scp_to_target_install;
179002ad2617SSteven Rostedt
179102ad2617SSteven Rostedt    return run_scp($src, $dst, $cp_scp);
179202ad2617SSteven Rostedt}
179302ad2617SSteven Rostedt
179402ad2617SSteven Rostedtsub run_scp_mod {
179502ad2617SSteven Rostedt    my ($src, $dst) = @_;
179602ad2617SSteven Rostedt
179702ad2617SSteven Rostedt    my $cp_scp = $scp_to_target;
179802ad2617SSteven Rostedt
179902ad2617SSteven Rostedt    return run_scp($src, $dst, $cp_scp);
180002ad2617SSteven Rostedt}
180102ad2617SSteven Rostedt
1802a15ba913SSteven Rostedtsub get_grub2_index {
1803a15ba913SSteven Rostedt
1804752d9665SSteven Rostedt (Red Hat)    return if (defined($grub_number) && defined($last_grub_menu) &&
1805df5f7c66SSteven Rostedt (Red Hat)	       $last_grub_menu eq $grub_menu && defined($last_machine) &&
1806df5f7c66SSteven Rostedt (Red Hat)	       $last_machine eq $machine);
1807a15ba913SSteven Rostedt
1808a15ba913SSteven Rostedt    doprint "Find grub2 menu ... ";
1809a15ba913SSteven Rostedt    $grub_number = -1;
1810a15ba913SSteven Rostedt
1811a15ba913SSteven Rostedt    my $ssh_grub = $ssh_exec;
1812a15ba913SSteven Rostedt    $ssh_grub =~ s,\$SSH_COMMAND,cat $grub_file,g;
1813a15ba913SSteven Rostedt
1814a15ba913SSteven Rostedt    open(IN, "$ssh_grub |")
1815a15ba913SSteven Rostedt	or die "unable to get $grub_file";
1816a15ba913SSteven Rostedt
1817a15ba913SSteven Rostedt    my $found = 0;
1818a15ba913SSteven Rostedt
1819a15ba913SSteven Rostedt    while (<IN>) {
1820a15ba913SSteven Rostedt	if (/^menuentry.*$grub_menu/) {
1821a15ba913SSteven Rostedt	    $grub_number++;
1822a15ba913SSteven Rostedt	    $found = 1;
1823a15ba913SSteven Rostedt	    last;
1824a15ba913SSteven Rostedt	} elsif (/^menuentry\s/) {
1825a15ba913SSteven Rostedt	    $grub_number++;
1826a15ba913SSteven Rostedt	}
1827a15ba913SSteven Rostedt    }
1828a15ba913SSteven Rostedt    close(IN);
1829a15ba913SSteven Rostedt
1830a15ba913SSteven Rostedt    die "Could not find '$grub_menu' in $grub_file on $machine"
1831a15ba913SSteven Rostedt	if (!$found);
1832a15ba913SSteven Rostedt    doprint "$grub_number\n";
1833752d9665SSteven Rostedt (Red Hat)    $last_grub_menu = $grub_menu;
1834df5f7c66SSteven Rostedt (Red Hat)    $last_machine = $machine;
1835a15ba913SSteven Rostedt}
1836a15ba913SSteven Rostedt
18375f9b6cedSSteven Rostedtsub get_grub_index {
18385f9b6cedSSteven Rostedt
1839a15ba913SSteven Rostedt    if ($reboot_type eq "grub2") {
1840a15ba913SSteven Rostedt	get_grub2_index;
1841a15ba913SSteven Rostedt	return;
1842a15ba913SSteven Rostedt    }
1843a15ba913SSteven Rostedt
1844a75fececSSteven Rostedt    if ($reboot_type ne "grub") {
1845a75fececSSteven Rostedt	return;
1846a75fececSSteven Rostedt    }
1847752d9665SSteven Rostedt (Red Hat)    return if (defined($grub_number) && defined($last_grub_menu) &&
1848df5f7c66SSteven Rostedt (Red Hat)	       $last_grub_menu eq $grub_menu && defined($last_machine) &&
1849df5f7c66SSteven Rostedt (Red Hat)	       $last_machine eq $machine);
18505f9b6cedSSteven Rostedt
18515f9b6cedSSteven Rostedt    doprint "Find grub menu ... ";
18525f9b6cedSSteven Rostedt    $grub_number = -1;
1853e48c5293SSteven Rostedt
1854e48c5293SSteven Rostedt    my $ssh_grub = $ssh_exec;
1855e48c5293SSteven Rostedt    $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1856e48c5293SSteven Rostedt
1857e48c5293SSteven Rostedt    open(IN, "$ssh_grub |")
18585f9b6cedSSteven Rostedt	or die "unable to get menu.lst";
1859e48c5293SSteven Rostedt
1860eaa1fe25SSteven Rostedt    my $found = 0;
1861eaa1fe25SSteven Rostedt
18625f9b6cedSSteven Rostedt    while (<IN>) {
1863a75fececSSteven Rostedt	if (/^\s*title\s+$grub_menu\s*$/) {
18645f9b6cedSSteven Rostedt	    $grub_number++;
1865eaa1fe25SSteven Rostedt	    $found = 1;
18665f9b6cedSSteven Rostedt	    last;
18675f9b6cedSSteven Rostedt	} elsif (/^\s*title\s/) {
18685f9b6cedSSteven Rostedt	    $grub_number++;
18695f9b6cedSSteven Rostedt	}
18705f9b6cedSSteven Rostedt    }
18715f9b6cedSSteven Rostedt    close(IN);
18725f9b6cedSSteven Rostedt
1873a75fececSSteven Rostedt    die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
1874eaa1fe25SSteven Rostedt	if (!$found);
18755f9b6cedSSteven Rostedt    doprint "$grub_number\n";
1876752d9665SSteven Rostedt (Red Hat)    $last_grub_menu = $grub_menu;
1877df5f7c66SSteven Rostedt (Red Hat)    $last_machine = $machine;
18782545eb61SSteven Rostedt}
18792545eb61SSteven Rostedt
18802545eb61SSteven Rostedtsub wait_for_input
18812545eb61SSteven Rostedt{
18822545eb61SSteven Rostedt    my ($fp, $time) = @_;
1883f7c6401fSSteven Rostedt (VMware)    my $start_time;
18842545eb61SSteven Rostedt    my $rin;
18859d2f7f05SJosh Poimboeuf    my $rout;
18869d2f7f05SJosh Poimboeuf    my $nr;
18879d2f7f05SJosh Poimboeuf    my $buf;
18882545eb61SSteven Rostedt    my $line;
18892545eb61SSteven Rostedt    my $ch;
18902545eb61SSteven Rostedt
18912545eb61SSteven Rostedt    if (!defined($time)) {
18922545eb61SSteven Rostedt	$time = $timeout;
18932545eb61SSteven Rostedt    }
18942545eb61SSteven Rostedt
18952545eb61SSteven Rostedt    $rin = '';
18962545eb61SSteven Rostedt    vec($rin, fileno($fp), 1) = 1;
18979d2f7f05SJosh Poimboeuf    vec($rin, fileno(\*STDIN), 1) = 1;
18989d2f7f05SJosh Poimboeuf
1899f7c6401fSSteven Rostedt (VMware)    $start_time = time;
1900f7c6401fSSteven Rostedt (VMware)
19019d2f7f05SJosh Poimboeuf    while (1) {
19029d2f7f05SJosh Poimboeuf	$nr = select($rout=$rin, undef, undef, $time);
19039d2f7f05SJosh Poimboeuf
1904f7c6401fSSteven Rostedt (VMware)	last if ($nr <= 0);
19059d2f7f05SJosh Poimboeuf
19069d2f7f05SJosh Poimboeuf	# copy data from stdin to the console
19079d2f7f05SJosh Poimboeuf	if (vec($rout, fileno(\*STDIN), 1) == 1) {
190899c014a8SSteven Rostedt (VMware)	    $nr = sysread(\*STDIN, $buf, 1000);
190999c014a8SSteven Rostedt (VMware)	    syswrite($fp, $buf, $nr) if ($nr > 0);
19109d2f7f05SJosh Poimboeuf	}
19112545eb61SSteven Rostedt
1912f7c6401fSSteven Rostedt (VMware)	# The timeout is based on time waiting for the fp data
1913f7c6401fSSteven Rostedt (VMware)	if (vec($rout, fileno($fp), 1) != 1) {
1914f7c6401fSSteven Rostedt (VMware)	    last if (defined($time) && (time - $start_time > $time));
19152545eb61SSteven Rostedt	    next;
19162545eb61SSteven Rostedt	}
19172545eb61SSteven Rostedt
19182545eb61SSteven Rostedt	$line = "";
19192545eb61SSteven Rostedt
19202545eb61SSteven Rostedt	# try to read one char at a time
19212545eb61SSteven Rostedt	while (sysread $fp, $ch, 1) {
19222545eb61SSteven Rostedt	    $line .= $ch;
19232545eb61SSteven Rostedt	    last if ($ch eq "\n");
19242545eb61SSteven Rostedt	}
19252545eb61SSteven Rostedt
1926f7c6401fSSteven Rostedt (VMware)	last if (!length($line));
19272545eb61SSteven Rostedt
19282545eb61SSteven Rostedt	return $line;
19292545eb61SSteven Rostedt    }
1930f7c6401fSSteven Rostedt (VMware)    return undef;
19319d2f7f05SJosh Poimboeuf}
19322545eb61SSteven Rostedt
193375c3fda7SSteven Rostedtsub reboot_to {
1934bc7c5803SSteven Rostedt    if (defined($switch_to_test)) {
1935bc7c5803SSteven Rostedt	run_command $switch_to_test;
1936bc7c5803SSteven Rostedt    }
1937bc7c5803SSteven Rostedt
1938a75fececSSteven Rostedt    if ($reboot_type eq "grub") {
1939c54367f9SSteven Rostedt	run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1940a15ba913SSteven Rostedt    } elsif ($reboot_type eq "grub2") {
1941a15ba913SSteven Rostedt	run_ssh "$grub_reboot $grub_number";
19427786954cSSteven Rostedt    } elsif ($reboot_type eq "syslinux") {
19437786954cSSteven Rostedt	run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
194496f6a0dfSSteven Rostedt    } elsif (defined $reboot_script) {
1945a75fececSSteven Rostedt	run_command "$reboot_script";
19462545eb61SSteven Rostedt    }
194796f6a0dfSSteven Rostedt    reboot;
194896f6a0dfSSteven Rostedt}
19492545eb61SSteven Rostedt
1950a57419b3SSteven Rostedtsub get_sha1 {
1951a57419b3SSteven Rostedt    my ($commit) = @_;
1952a57419b3SSteven Rostedt
1953a57419b3SSteven Rostedt    doprint "git rev-list --max-count=1 $commit ... ";
1954a57419b3SSteven Rostedt    my $sha1 = `git rev-list --max-count=1 $commit`;
1955a57419b3SSteven Rostedt    my $ret = $?;
1956a57419b3SSteven Rostedt
1957a57419b3SSteven Rostedt    logit $sha1;
1958a57419b3SSteven Rostedt
1959a57419b3SSteven Rostedt    if ($ret) {
1960a57419b3SSteven Rostedt	doprint "FAILED\n";
1961a57419b3SSteven Rostedt	dodie "Failed to get git $commit";
1962a57419b3SSteven Rostedt    }
1963a57419b3SSteven Rostedt
1964a57419b3SSteven Rostedt    print "SUCCESS\n";
1965a57419b3SSteven Rostedt
1966a57419b3SSteven Rostedt    chomp $sha1;
1967a57419b3SSteven Rostedt
1968a57419b3SSteven Rostedt    return $sha1;
1969a57419b3SSteven Rostedt}
1970a57419b3SSteven Rostedt
19715a391fbfSSteven Rostedtsub monitor {
19722545eb61SSteven Rostedt    my $booted = 0;
19732545eb61SSteven Rostedt    my $bug = 0;
19746ca996ccSSteven Rostedt    my $bug_ignored = 0;
19755c42fc5bSSteven Rostedt    my $skip_call_trace = 0;
19762b7d9b21SSteven Rostedt    my $loops;
19772545eb61SSteven Rostedt
197838fa3dc1SSteven Rostedt (Red Hat)    my $start_time = time;
197938fa3dc1SSteven Rostedt (Red Hat)
19807faafbd6SSteven Rostedt    wait_for_monitor 5;
19812545eb61SSteven Rostedt
19822545eb61SSteven Rostedt    my $line;
19832545eb61SSteven Rostedt    my $full_line = "";
19842545eb61SSteven Rostedt
19857faafbd6SSteven Rostedt    open(DMESG, "> $dmesg") or
19867faafbd6SSteven Rostedt	die "unable to write to $dmesg";
19872545eb61SSteven Rostedt
198875c3fda7SSteven Rostedt    reboot_to;
19892545eb61SSteven Rostedt
19901c8a617aSSteven Rostedt    my $success_start;
19911c8a617aSSteven Rostedt    my $failure_start;
19922d01b26aSSteven Rostedt    my $monitor_start = time;
19932d01b26aSSteven Rostedt    my $done = 0;
1994f1a5b962SSteven Rostedt    my $version_found = 0;
19951c8a617aSSteven Rostedt
19962d01b26aSSteven Rostedt    while (!$done) {
19972545eb61SSteven Rostedt
1998ecaf8e52SSteven Rostedt	if ($bug && defined($stop_after_failure) &&
1999ecaf8e52SSteven Rostedt	    $stop_after_failure >= 0) {
2000ecaf8e52SSteven Rostedt	    my $time = $stop_after_failure - (time - $failure_start);
2001ecaf8e52SSteven Rostedt	    $line = wait_for_input($monitor_fp, $time);
2002ecaf8e52SSteven Rostedt	    if (!defined($line)) {
2003ecaf8e52SSteven Rostedt		doprint "bug timed out after $booted_timeout seconds\n";
2004ecaf8e52SSteven Rostedt		doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
2005ecaf8e52SSteven Rostedt		last;
2006ecaf8e52SSteven Rostedt	    }
2007ecaf8e52SSteven Rostedt	} elsif ($booted) {
2008a75fececSSteven Rostedt	    $line = wait_for_input($monitor_fp, $booted_timeout);
2009cd4f1d53SSteven Rostedt	    if (!defined($line)) {
2010cd4f1d53SSteven Rostedt		my $s = $booted_timeout == 1 ? "" : "s";
2011cd4f1d53SSteven Rostedt		doprint "Successful boot found: break after $booted_timeout second$s\n";
2012cd4f1d53SSteven Rostedt		last;
2013cd4f1d53SSteven Rostedt	    }
20142b7d9b21SSteven Rostedt	} else {
20157faafbd6SSteven Rostedt	    $line = wait_for_input($monitor_fp);
2016cd4f1d53SSteven Rostedt	    if (!defined($line)) {
2017cd4f1d53SSteven Rostedt		my $s = $timeout == 1 ? "" : "s";
2018cd4f1d53SSteven Rostedt		doprint "Timed out after $timeout second$s\n";
2019cd4f1d53SSteven Rostedt		last;
20202b7d9b21SSteven Rostedt	    }
2021cd4f1d53SSteven Rostedt	}
20222545eb61SSteven Rostedt
20232545eb61SSteven Rostedt	doprint $line;
20247faafbd6SSteven Rostedt	print DMESG $line;
20252545eb61SSteven Rostedt
20262545eb61SSteven Rostedt	# we are not guaranteed to get a full line
20272545eb61SSteven Rostedt	$full_line .= $line;
20282545eb61SSteven Rostedt
2029a75fececSSteven Rostedt	if ($full_line =~ /$success_line/) {
20302545eb61SSteven Rostedt	    $booted = 1;
20311c8a617aSSteven Rostedt	    $success_start = time;
20321c8a617aSSteven Rostedt	}
20331c8a617aSSteven Rostedt
20341c8a617aSSteven Rostedt	if ($booted && defined($stop_after_success) &&
20351c8a617aSSteven Rostedt	    $stop_after_success >= 0) {
20361c8a617aSSteven Rostedt	    my $now = time;
20371c8a617aSSteven Rostedt	    if ($now - $success_start >= $stop_after_success) {
20381c8a617aSSteven Rostedt		doprint "Test forced to stop after $stop_after_success seconds after success\n";
20391c8a617aSSteven Rostedt		last;
20401c8a617aSSteven Rostedt	    }
20412545eb61SSteven Rostedt	}
20422545eb61SSteven Rostedt
20435c42fc5bSSteven Rostedt	if ($full_line =~ /\[ backtrace testing \]/) {
20445c42fc5bSSteven Rostedt	    $skip_call_trace = 1;
20455c42fc5bSSteven Rostedt	}
20465c42fc5bSSteven Rostedt
20472545eb61SSteven Rostedt	if ($full_line =~ /call trace:/i) {
20486ca996ccSSteven Rostedt	    if (!$bug && !$skip_call_trace) {
20496ca996ccSSteven Rostedt		if ($ignore_errors) {
20506ca996ccSSteven Rostedt		    $bug_ignored = 1;
20516ca996ccSSteven Rostedt		} else {
20521c8a617aSSteven Rostedt		    $bug = 1;
20531c8a617aSSteven Rostedt		    $failure_start = time;
20541c8a617aSSteven Rostedt		}
20551c8a617aSSteven Rostedt	    }
20566ca996ccSSteven Rostedt	}
20571c8a617aSSteven Rostedt
20581c8a617aSSteven Rostedt	if ($bug && defined($stop_after_failure) &&
20591c8a617aSSteven Rostedt	    $stop_after_failure >= 0) {
20601c8a617aSSteven Rostedt	    my $now = time;
20611c8a617aSSteven Rostedt	    if ($now - $failure_start >= $stop_after_failure) {
20621c8a617aSSteven Rostedt		doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
20631c8a617aSSteven Rostedt		last;
20641c8a617aSSteven Rostedt	    }
20655c42fc5bSSteven Rostedt	}
20665c42fc5bSSteven Rostedt
20675c42fc5bSSteven Rostedt	if ($full_line =~ /\[ end of backtrace testing \]/) {
20685c42fc5bSSteven Rostedt	    $skip_call_trace = 0;
20695c42fc5bSSteven Rostedt	}
20705c42fc5bSSteven Rostedt
20715c42fc5bSSteven Rostedt	if ($full_line =~ /Kernel panic -/) {
207210abf118SSteven Rostedt	    $failure_start = time;
20732545eb61SSteven Rostedt	    $bug = 1;
20742545eb61SSteven Rostedt	}
20752545eb61SSteven Rostedt
2076f1a5b962SSteven Rostedt	# Detect triple faults by testing the banner
2077f1a5b962SSteven Rostedt	if ($full_line =~ /\bLinux version (\S+).*\n/) {
2078f1a5b962SSteven Rostedt	    if ($1 eq $version) {
2079f1a5b962SSteven Rostedt		$version_found = 1;
2080f1a5b962SSteven Rostedt	    } elsif ($version_found && $detect_triplefault) {
2081f1a5b962SSteven Rostedt		# We already booted into the kernel we are testing,
2082f1a5b962SSteven Rostedt		# but now we booted into another kernel?
2083f1a5b962SSteven Rostedt		# Consider this a triple fault.
20848b513d0cSMasanari Iida		doprint "Already booted in Linux kernel $version, but now\n";
2085f1a5b962SSteven Rostedt		doprint "we booted into Linux kernel $1.\n";
2086f1a5b962SSteven Rostedt		doprint "Assuming that this is a triple fault.\n";
2087f1a5b962SSteven Rostedt		doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
2088f1a5b962SSteven Rostedt		last;
2089f1a5b962SSteven Rostedt	    }
2090f1a5b962SSteven Rostedt	}
2091f1a5b962SSteven Rostedt
20922545eb61SSteven Rostedt	if ($line =~ /\n/) {
20932545eb61SSteven Rostedt	    $full_line = "";
20942545eb61SSteven Rostedt	}
20952d01b26aSSteven Rostedt
20962d01b26aSSteven Rostedt	if ($stop_test_after > 0 && !$booted && !$bug) {
20972d01b26aSSteven Rostedt	    if (time - $monitor_start > $stop_test_after) {
20984d62bf51SSteven Rostedt		doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
20992d01b26aSSteven Rostedt		$done = 1;
21002d01b26aSSteven Rostedt	    }
21012d01b26aSSteven Rostedt	}
21022545eb61SSteven Rostedt    }
21032545eb61SSteven Rostedt
210438fa3dc1SSteven Rostedt (Red Hat)    my $end_time = time;
210538fa3dc1SSteven Rostedt (Red Hat)    $reboot_time = $end_time - $start_time;
210638fa3dc1SSteven Rostedt (Red Hat)
21077faafbd6SSteven Rostedt    close(DMESG);
21082545eb61SSteven Rostedt
21092545eb61SSteven Rostedt    if ($bug) {
21102b7d9b21SSteven Rostedt	return 0 if ($in_bisect);
2111576f627cSSteven Rostedt	fail "failed - got a bug report" and return 0;
21122545eb61SSteven Rostedt    }
21135f9b6cedSSteven Rostedt
2114a75fececSSteven Rostedt    if (!$booted) {
2115a75fececSSteven Rostedt	return 0 if ($in_bisect);
2116576f627cSSteven Rostedt	fail "failed - never got a boot prompt." and return 0;
2117a75fececSSteven Rostedt    }
2118a75fececSSteven Rostedt
21196ca996ccSSteven Rostedt    if ($bug_ignored) {
21206ca996ccSSteven Rostedt	doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
21216ca996ccSSteven Rostedt    }
21226ca996ccSSteven Rostedt
21232b7d9b21SSteven Rostedt    return 1;
21242545eb61SSteven Rostedt}
21252545eb61SSteven Rostedt
21262b29b2f8SSteven Rostedtsub eval_kernel_version {
21272b29b2f8SSteven Rostedt    my ($option) = @_;
21282b29b2f8SSteven Rostedt
21292b29b2f8SSteven Rostedt    $option =~ s/\$KERNEL_VERSION/$version/g;
21302b29b2f8SSteven Rostedt
21312b29b2f8SSteven Rostedt    return $option;
21322b29b2f8SSteven Rostedt}
21332b29b2f8SSteven Rostedt
2134db05cfefSSteven Rostedtsub do_post_install {
2135db05cfefSSteven Rostedt
2136db05cfefSSteven Rostedt    return if (!defined($post_install));
2137db05cfefSSteven Rostedt
21382b29b2f8SSteven Rostedt    my $cp_post_install = eval_kernel_version $post_install;
2139db05cfefSSteven Rostedt    run_command "$cp_post_install" or
2140db05cfefSSteven Rostedt	dodie "Failed to run post install";
2141db05cfefSSteven Rostedt}
2142db05cfefSSteven Rostedt
2143e1a6c3d7SSteven Rostedt# Sometimes the reboot fails, and will hang. We try to ssh to the box
2144e1a6c3d7SSteven Rostedt# and if we fail, we force another reboot, that should powercycle it.
2145e1a6c3d7SSteven Rostedtsub test_booted {
2146e1a6c3d7SSteven Rostedt    if (!run_ssh "echo testing connection") {
2147e1a6c3d7SSteven Rostedt	reboot $sleep_time;
2148e1a6c3d7SSteven Rostedt    }
2149e1a6c3d7SSteven Rostedt}
2150e1a6c3d7SSteven Rostedt
21512545eb61SSteven Rostedtsub install {
21522545eb61SSteven Rostedt
2153e0a8742eSSteven Rostedt    return if ($no_install);
2154e0a8742eSSteven Rostedt
215538fa3dc1SSteven Rostedt (Red Hat)    my $start_time = time;
215638fa3dc1SSteven Rostedt (Red Hat)
2157e5c2ec11SSteven Rostedt    if (defined($pre_install)) {
2158e5c2ec11SSteven Rostedt	my $cp_pre_install = eval_kernel_version $pre_install;
2159e5c2ec11SSteven Rostedt	run_command "$cp_pre_install" or
2160e5c2ec11SSteven Rostedt	    dodie "Failed to run pre install";
2161e5c2ec11SSteven Rostedt    }
2162e5c2ec11SSteven Rostedt
21632b29b2f8SSteven Rostedt    my $cp_target = eval_kernel_version $target_image;
21642b29b2f8SSteven Rostedt
2165e1a6c3d7SSteven Rostedt    test_booted;
2166e1a6c3d7SSteven Rostedt
216702ad2617SSteven Rostedt    run_scp_install "$outputdir/$build_target", "$cp_target" or
21685c42fc5bSSteven Rostedt	dodie "failed to copy image";
21695f9b6cedSSteven Rostedt
21705f9b6cedSSteven Rostedt    my $install_mods = 0;
21715f9b6cedSSteven Rostedt
21725f9b6cedSSteven Rostedt    # should we process modules?
21735f9b6cedSSteven Rostedt    $install_mods = 0;
217451ad1dd1SSteven Rostedt    open(IN, "$output_config") or dodie("Can't read config file");
21755f9b6cedSSteven Rostedt    while (<IN>) {
21765f9b6cedSSteven Rostedt	if (/CONFIG_MODULES(=y)?/) {
21778bc5e4eaSSteven Rostedt	    if (defined($1)) {
21788bc5e4eaSSteven Rostedt		$install_mods = 1;
21795f9b6cedSSteven Rostedt		last;
21805f9b6cedSSteven Rostedt	    }
21815f9b6cedSSteven Rostedt	}
21828bc5e4eaSSteven Rostedt    }
21835f9b6cedSSteven Rostedt    close(IN);
21845f9b6cedSSteven Rostedt
21855f9b6cedSSteven Rostedt    if (!$install_mods) {
2186db05cfefSSteven Rostedt	do_post_install;
21875f9b6cedSSteven Rostedt	doprint "No modules needed\n";
218838fa3dc1SSteven Rostedt (Red Hat)	my $end_time = time;
218938fa3dc1SSteven Rostedt (Red Hat)	$install_time = $end_time - $start_time;
21905f9b6cedSSteven Rostedt	return;
21912545eb61SSteven Rostedt    }
21922545eb61SSteven Rostedt
2193627977d8SSteven Rostedt    run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
21945f9b6cedSSteven Rostedt	dodie "Failed to install modules";
21955f9b6cedSSteven Rostedt
21962545eb61SSteven Rostedt    my $modlib = "/lib/modules/$version";
2197a57419b3SSteven Rostedt    my $modtar = "ktest-mods.tar.bz2";
21982545eb61SSteven Rostedt
2199e48c5293SSteven Rostedt    run_ssh "rm -rf $modlib" or
22005c42fc5bSSteven Rostedt	dodie "failed to remove old mods: $modlib";
22012545eb61SSteven Rostedt
22025c42fc5bSSteven Rostedt    # would be nice if scp -r did not follow symbolic links
2203a75fececSSteven Rostedt    run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
22045c42fc5bSSteven Rostedt	dodie "making tarball";
22055c42fc5bSSteven Rostedt
220602ad2617SSteven Rostedt    run_scp_mod "$tmpdir/$modtar", "/tmp" or
22075c42fc5bSSteven Rostedt	dodie "failed to copy modules";
22085c42fc5bSSteven Rostedt
2209a75fececSSteven Rostedt    unlink "$tmpdir/$modtar";
22105c42fc5bSSteven Rostedt
2211e7b13441SSteven Rostedt    run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
22125c42fc5bSSteven Rostedt	dodie "failed to tar modules";
22135c42fc5bSSteven Rostedt
2214e48c5293SSteven Rostedt    run_ssh "rm -f /tmp/$modtar";
22158b37ca8cSSteven Rostedt
2216db05cfefSSteven Rostedt    do_post_install;
221738fa3dc1SSteven Rostedt (Red Hat)
221838fa3dc1SSteven Rostedt (Red Hat)    my $end_time = time;
221938fa3dc1SSteven Rostedt (Red Hat)    $install_time = $end_time - $start_time;
22202545eb61SSteven Rostedt}
22212545eb61SSteven Rostedt
2222ddf607e5SSteven Rostedtsub get_version {
2223ddf607e5SSteven Rostedt    # get the release name
2224683a3e64SSteven Rostedt    return if ($have_version);
2225ddf607e5SSteven Rostedt    doprint "$make kernelrelease ... ";
222617150fefSSteven Rostedt (Red Hat)    $version = `$make -s kernelrelease | tail -1`;
2227ddf607e5SSteven Rostedt    chomp($version);
2228ddf607e5SSteven Rostedt    doprint "$version\n";
2229683a3e64SSteven Rostedt    $have_version = 1;
2230ddf607e5SSteven Rostedt}
2231ddf607e5SSteven Rostedt
223264d98283SSteven Rostedt (Red Hat)sub start_monitor_and_install {
22339f7424ccSSteven Rostedt    # Make sure the stable kernel has finished booting
2234319ab14fSSteven Rostedt (Red Hat)
2235319ab14fSSteven Rostedt (Red Hat)    # Install bisects, don't need console
2236319ab14fSSteven Rostedt (Red Hat)    if (defined $console) {
22379f7424ccSSteven Rostedt	start_monitor;
22389f7424ccSSteven Rostedt	wait_for_monitor 5;
22399f7424ccSSteven Rostedt	end_monitor;
2240319ab14fSSteven Rostedt (Red Hat)    }
22419f7424ccSSteven Rostedt
2242ddf607e5SSteven Rostedt    get_grub_index;
2243ddf607e5SSteven Rostedt    get_version;
2244ddf607e5SSteven Rostedt    install;
2245ddf607e5SSteven Rostedt
2246319ab14fSSteven Rostedt (Red Hat)    start_monitor if (defined $console);
2247ddf607e5SSteven Rostedt    return monitor;
2248ddf607e5SSteven Rostedt}
2249ddf607e5SSteven Rostedt
22504283b169SSteven Rostedt (Red Hat)my $check_build_re = ".*:.*(warning|error|Error):.*";
22514283b169SSteven Rostedt (Red Hat)my $utf8_quote = "\\x{e2}\\x{80}(\\x{98}|\\x{99})";
22524283b169SSteven Rostedt (Red Hat)
22537328735cSSteven Rostedt (Red Hat)sub process_warning_line {
22547328735cSSteven Rostedt (Red Hat)    my ($line) = @_;
22557328735cSSteven Rostedt (Red Hat)
22567328735cSSteven Rostedt (Red Hat)    chomp $line;
22577328735cSSteven Rostedt (Red Hat)
22587328735cSSteven Rostedt (Red Hat)    # for distcc heterogeneous systems, some compilers
22597328735cSSteven Rostedt (Red Hat)    # do things differently causing warning lines
22607328735cSSteven Rostedt (Red Hat)    # to be slightly different. This makes an attempt
22617328735cSSteven Rostedt (Red Hat)    # to fixe those issues.
22627328735cSSteven Rostedt (Red Hat)
22637328735cSSteven Rostedt (Red Hat)    # chop off the index into the line
22647328735cSSteven Rostedt (Red Hat)    # using distcc, some compilers give different indexes
22657328735cSSteven Rostedt (Red Hat)    # depending on white space
22667328735cSSteven Rostedt (Red Hat)    $line =~ s/^(\s*\S+:\d+:)\d+/$1/;
22677328735cSSteven Rostedt (Red Hat)
22687328735cSSteven Rostedt (Red Hat)    # Some compilers use UTF-8 extended for quotes and some don't.
22697328735cSSteven Rostedt (Red Hat)    $line =~ s/$utf8_quote/'/g;
22707328735cSSteven Rostedt (Red Hat)
22717328735cSSteven Rostedt (Red Hat)    return $line;
22727328735cSSteven Rostedt (Red Hat)}
22737328735cSSteven Rostedt (Red Hat)
22744283b169SSteven Rostedt (Red Hat)# Read buildlog and check against warnings file for any
22754283b169SSteven Rostedt (Red Hat)# new warnings.
22764283b169SSteven Rostedt (Red Hat)#
22774283b169SSteven Rostedt (Red Hat)# Returns 1 if OK
22784283b169SSteven Rostedt (Red Hat)#         0 otherwise
22796c5ee0beSSteven Rostedtsub check_buildlog {
22804283b169SSteven Rostedt (Red Hat)    return 1 if (!defined $warnings_file);
22814283b169SSteven Rostedt (Red Hat)
22824283b169SSteven Rostedt (Red Hat)    my %warnings_list;
22834283b169SSteven Rostedt (Red Hat)
22844283b169SSteven Rostedt (Red Hat)    # Failed builds should not reboot the target
22854283b169SSteven Rostedt (Red Hat)    my $save_no_reboot = $no_reboot;
22864283b169SSteven Rostedt (Red Hat)    $no_reboot = 1;
22874283b169SSteven Rostedt (Red Hat)
22884283b169SSteven Rostedt (Red Hat)    if (-f $warnings_file) {
22894283b169SSteven Rostedt (Red Hat)	open(IN, $warnings_file) or
22904283b169SSteven Rostedt (Red Hat)	    dodie "Error opening $warnings_file";
22914283b169SSteven Rostedt (Red Hat)
22924283b169SSteven Rostedt (Red Hat)	while (<IN>) {
22934283b169SSteven Rostedt (Red Hat)	    if (/$check_build_re/) {
22947328735cSSteven Rostedt (Red Hat)		my $warning = process_warning_line $_;
22957328735cSSteven Rostedt (Red Hat)
22967328735cSSteven Rostedt (Red Hat)		$warnings_list{$warning} = 1;
22974283b169SSteven Rostedt (Red Hat)	    }
22984283b169SSteven Rostedt (Red Hat)	}
22994283b169SSteven Rostedt (Red Hat)	close(IN);
23004283b169SSteven Rostedt (Red Hat)    }
23014283b169SSteven Rostedt (Red Hat)
23024283b169SSteven Rostedt (Red Hat)    # If warnings file didn't exist, and WARNINGS_FILE exist,
23034283b169SSteven Rostedt (Red Hat)    # then we fail on any warning!
23044283b169SSteven Rostedt (Red Hat)
23054283b169SSteven Rostedt (Red Hat)    open(IN, $buildlog) or dodie "Can't open $buildlog";
23064283b169SSteven Rostedt (Red Hat)    while (<IN>) {
23074283b169SSteven Rostedt (Red Hat)	if (/$check_build_re/) {
23087328735cSSteven Rostedt (Red Hat)	    my $warning = process_warning_line $_;
23094283b169SSteven Rostedt (Red Hat)
23107328735cSSteven Rostedt (Red Hat)	    if (!defined $warnings_list{$warning}) {
23114283b169SSteven Rostedt (Red Hat)		fail "New warning found (not in $warnings_file)\n$_\n";
23124283b169SSteven Rostedt (Red Hat)		$no_reboot = $save_no_reboot;
23134283b169SSteven Rostedt (Red Hat)		return 0;
23144283b169SSteven Rostedt (Red Hat)	    }
23154283b169SSteven Rostedt (Red Hat)	}
23164283b169SSteven Rostedt (Red Hat)    }
23174283b169SSteven Rostedt (Red Hat)    $no_reboot = $save_no_reboot;
23184283b169SSteven Rostedt (Red Hat)    close(IN);
23194283b169SSteven Rostedt (Red Hat)}
23204283b169SSteven Rostedt (Red Hat)
23214283b169SSteven Rostedt (Red Hat)sub check_patch_buildlog {
23226c5ee0beSSteven Rostedt    my ($patch) = @_;
23236c5ee0beSSteven Rostedt
23246c5ee0beSSteven Rostedt    my @files = `git show $patch | diffstat -l`;
23256c5ee0beSSteven Rostedt
232635275685SSteven Rostedt (Red Hat)    foreach my $file (@files) {
232735275685SSteven Rostedt (Red Hat)	chomp $file;
232835275685SSteven Rostedt (Red Hat)    }
232935275685SSteven Rostedt (Red Hat)
23306c5ee0beSSteven Rostedt    open(IN, "git show $patch |") or
23316c5ee0beSSteven Rostedt	dodie "failed to show $patch";
23326c5ee0beSSteven Rostedt    while (<IN>) {
23336c5ee0beSSteven Rostedt	if (m,^--- a/(.*),) {
23346c5ee0beSSteven Rostedt	    chomp $1;
23356c5ee0beSSteven Rostedt	    $files[$#files] = $1;
23366c5ee0beSSteven Rostedt	}
23376c5ee0beSSteven Rostedt    }
23386c5ee0beSSteven Rostedt    close(IN);
23396c5ee0beSSteven Rostedt
23406c5ee0beSSteven Rostedt    open(IN, $buildlog) or dodie "Can't open $buildlog";
23416c5ee0beSSteven Rostedt    while (<IN>) {
23426c5ee0beSSteven Rostedt	if (/^\s*(.*?):.*(warning|error)/) {
23436c5ee0beSSteven Rostedt	    my $err = $1;
23446c5ee0beSSteven Rostedt	    foreach my $file (@files) {
2345a75fececSSteven Rostedt		my $fullpath = "$builddir/$file";
23466c5ee0beSSteven Rostedt		if ($file eq $err || $fullpath eq $err) {
23472b7d9b21SSteven Rostedt		    fail "$file built with warnings" and return 0;
23486c5ee0beSSteven Rostedt		}
23496c5ee0beSSteven Rostedt	    }
23506c5ee0beSSteven Rostedt	}
23516c5ee0beSSteven Rostedt    }
23526c5ee0beSSteven Rostedt    close(IN);
23532b7d9b21SSteven Rostedt
23542b7d9b21SSteven Rostedt    return 1;
23556c5ee0beSSteven Rostedt}
23566c5ee0beSSteven Rostedt
2357fcb3f16aSSteven Rostedtsub apply_min_config {
2358fcb3f16aSSteven Rostedt    my $outconfig = "$output_config.new";
2359612b9e9bSSteven Rostedt
2360fcb3f16aSSteven Rostedt    # Read the config file and remove anything that
2361fcb3f16aSSteven Rostedt    # is in the force_config hash (from minconfig and others)
2362fcb3f16aSSteven Rostedt    # then add the force config back.
2363fcb3f16aSSteven Rostedt
2364fcb3f16aSSteven Rostedt    doprint "Applying minimum configurations into $output_config.new\n";
2365fcb3f16aSSteven Rostedt
2366fcb3f16aSSteven Rostedt    open (OUT, ">$outconfig") or
2367fcb3f16aSSteven Rostedt	dodie "Can't create $outconfig";
2368fcb3f16aSSteven Rostedt
2369fcb3f16aSSteven Rostedt    if (-f $output_config) {
2370fcb3f16aSSteven Rostedt	open (IN, $output_config) or
2371fcb3f16aSSteven Rostedt	    dodie "Failed to open $output_config";
2372fcb3f16aSSteven Rostedt	while (<IN>) {
2373fcb3f16aSSteven Rostedt	    if (/^(# )?(CONFIG_[^\s=]*)/) {
2374fcb3f16aSSteven Rostedt		next if (defined($force_config{$2}));
2375fcb3f16aSSteven Rostedt	    }
2376fcb3f16aSSteven Rostedt	    print OUT;
2377fcb3f16aSSteven Rostedt	}
2378fcb3f16aSSteven Rostedt	close IN;
2379fcb3f16aSSteven Rostedt    }
2380fcb3f16aSSteven Rostedt    foreach my $config (keys %force_config) {
2381fcb3f16aSSteven Rostedt	print OUT "$force_config{$config}\n";
2382fcb3f16aSSteven Rostedt    }
2383fcb3f16aSSteven Rostedt    close OUT;
2384fcb3f16aSSteven Rostedt
2385fcb3f16aSSteven Rostedt    run_command "mv $outconfig $output_config";
2386fcb3f16aSSteven Rostedt}
2387fcb3f16aSSteven Rostedt
2388fcb3f16aSSteven Rostedtsub make_oldconfig {
2389fcb3f16aSSteven Rostedt
23904c4ab120SSteven Rostedt    my @force_list = keys %force_config;
23914c4ab120SSteven Rostedt
23924c4ab120SSteven Rostedt    if ($#force_list >= 0) {
2393fcb3f16aSSteven Rostedt	apply_min_config;
23944c4ab120SSteven Rostedt    }
2395fcb3f16aSSteven Rostedt
2396fb16d891SAdam Lee    if (!run_command "$make olddefconfig") {
2397fb16d891SAdam Lee	# Perhaps olddefconfig doesn't exist in this version of the kernel
239818925170SSteven Rostedt	# try oldnoconfig
239918925170SSteven Rostedt	doprint "olddefconfig failed, trying make oldnoconfig\n";
240018925170SSteven Rostedt	if (!run_command "$make oldnoconfig") {
240118925170SSteven Rostedt	    doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
2402612b9e9bSSteven Rostedt	    # try a yes '' | oldconfig
2403fcb3f16aSSteven Rostedt	    run_command "yes '' | $make oldconfig" or
2404612b9e9bSSteven Rostedt		dodie "failed make config oldconfig";
2405612b9e9bSSteven Rostedt	}
2406612b9e9bSSteven Rostedt    }
240718925170SSteven Rostedt}
2408612b9e9bSSteven Rostedt
2409fcb3f16aSSteven Rostedt# read a config file and use this to force new configs.
2410fcb3f16aSSteven Rostedtsub load_force_config {
2411fcb3f16aSSteven Rostedt    my ($config) = @_;
2412fcb3f16aSSteven Rostedt
2413cf79fab6SSteven Rostedt    doprint "Loading force configs from $config\n";
2414fcb3f16aSSteven Rostedt    open(IN, $config) or
2415fcb3f16aSSteven Rostedt	dodie "failed to read $config";
2416fcb3f16aSSteven Rostedt    while (<IN>) {
2417fcb3f16aSSteven Rostedt	chomp;
2418fcb3f16aSSteven Rostedt	if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
2419fcb3f16aSSteven Rostedt	    $force_config{$1} = $_;
2420fcb3f16aSSteven Rostedt	} elsif (/^# (CONFIG_\S*) is not set/) {
2421fcb3f16aSSteven Rostedt	    $force_config{$1} = $_;
2422fcb3f16aSSteven Rostedt	}
2423fcb3f16aSSteven Rostedt    }
2424fcb3f16aSSteven Rostedt    close IN;
2425fcb3f16aSSteven Rostedt}
2426fcb3f16aSSteven Rostedt
24272545eb61SSteven Rostedtsub build {
24282545eb61SSteven Rostedt    my ($type) = @_;
24292545eb61SSteven Rostedt
24307faafbd6SSteven Rostedt    unlink $buildlog;
24317faafbd6SSteven Rostedt
243238fa3dc1SSteven Rostedt (Red Hat)    my $start_time = time;
243338fa3dc1SSteven Rostedt (Red Hat)
24344ab1cce5SSteven Rostedt    # Failed builds should not reboot the target
24354ab1cce5SSteven Rostedt    my $save_no_reboot = $no_reboot;
24364ab1cce5SSteven Rostedt    $no_reboot = 1;
24374ab1cce5SSteven Rostedt
2438683a3e64SSteven Rostedt    # Calculate a new version from here.
2439683a3e64SSteven Rostedt    $have_version = 0;
2440683a3e64SSteven Rostedt
24410bd6c1a3SSteven Rostedt    if (defined($pre_build)) {
24420bd6c1a3SSteven Rostedt	my $ret = run_command $pre_build;
24430bd6c1a3SSteven Rostedt	if (!$ret && defined($pre_build_die) &&
24440bd6c1a3SSteven Rostedt	    $pre_build_die) {
24450bd6c1a3SSteven Rostedt	    dodie "failed to pre_build\n";
24460bd6c1a3SSteven Rostedt	}
24470bd6c1a3SSteven Rostedt    }
24480bd6c1a3SSteven Rostedt
244975c3fda7SSteven Rostedt    if ($type =~ /^useconfig:(.*)/) {
245051ad1dd1SSteven Rostedt	run_command "cp $1 $output_config" or
245175c3fda7SSteven Rostedt	    dodie "could not copy $1 to .config";
24525f9b6cedSSteven Rostedt
245375c3fda7SSteven Rostedt	$type = "oldconfig";
245475c3fda7SSteven Rostedt    }
245575c3fda7SSteven Rostedt
24565c42fc5bSSteven Rostedt    # old config can ask questions
24575c42fc5bSSteven Rostedt    if ($type eq "oldconfig") {
2458fb16d891SAdam Lee	$type = "olddefconfig";
245975c3fda7SSteven Rostedt
246075c3fda7SSteven Rostedt	# allow for empty configs
246151ad1dd1SSteven Rostedt	run_command "touch $output_config";
246275c3fda7SSteven Rostedt
246313488231SAndrew Jones	if (!$noclean) {
246451ad1dd1SSteven Rostedt	    run_command "mv $output_config $outputdir/config_temp" or
24655c42fc5bSSteven Rostedt		dodie "moving .config";
24665c42fc5bSSteven Rostedt
246713488231SAndrew Jones	    run_command "$make mrproper" or dodie "make mrproper";
24685c42fc5bSSteven Rostedt
246951ad1dd1SSteven Rostedt	    run_command "mv $outputdir/config_temp $output_config" or
24705c42fc5bSSteven Rostedt		dodie "moving config_temp";
247113488231SAndrew Jones	}
24725c42fc5bSSteven Rostedt
24735c42fc5bSSteven Rostedt    } elsif (!$noclean) {
247451ad1dd1SSteven Rostedt	unlink "$output_config";
24755f9b6cedSSteven Rostedt	run_command "$make mrproper" or
24765c42fc5bSSteven Rostedt	    dodie "make mrproper";
24775c42fc5bSSteven Rostedt    }
24782545eb61SSteven Rostedt
24792545eb61SSteven Rostedt    # add something to distinguish this build
2480a75fececSSteven Rostedt    open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
2481a75fececSSteven Rostedt    print OUT "$localversion\n";
24822545eb61SSteven Rostedt    close(OUT);
24832545eb61SSteven Rostedt
24845f9b6cedSSteven Rostedt    if (defined($minconfig)) {
2485fcb3f16aSSteven Rostedt	load_force_config($minconfig);
24862545eb61SSteven Rostedt    }
24872545eb61SSteven Rostedt
2488fb16d891SAdam Lee    if ($type ne "olddefconfig") {
2489fcb3f16aSSteven Rostedt	run_command "$make $type" or
24905c42fc5bSSteven Rostedt	    dodie "failed make config";
2491612b9e9bSSteven Rostedt    }
2492fcb3f16aSSteven Rostedt    # Run old config regardless, to enforce min configurations
2493fcb3f16aSSteven Rostedt    make_oldconfig;
24942545eb61SSteven Rostedt
2495f983a2bcSSatoru Takeuchi    my $build_ret = run_command "$make $build_options", $buildlog;
24960bd6c1a3SSteven Rostedt
24970bd6c1a3SSteven Rostedt    if (defined($post_build)) {
2498683a3e64SSteven Rostedt	# Because a post build may change the kernel version
2499683a3e64SSteven Rostedt	# do it now.
2500683a3e64SSteven Rostedt	get_version;
25010bd6c1a3SSteven Rostedt	my $ret = run_command $post_build;
25020bd6c1a3SSteven Rostedt	if (!$ret && defined($post_build_die) &&
25030bd6c1a3SSteven Rostedt	    $post_build_die) {
25040bd6c1a3SSteven Rostedt	    dodie "failed to post_build\n";
25050bd6c1a3SSteven Rostedt	}
25060bd6c1a3SSteven Rostedt    }
25070bd6c1a3SSteven Rostedt
25080bd6c1a3SSteven Rostedt    if (!$build_ret) {
25095f9b6cedSSteven Rostedt	# bisect may need this to pass
25104ab1cce5SSteven Rostedt	if ($in_bisect) {
25114ab1cce5SSteven Rostedt	    $no_reboot = $save_no_reboot;
25124ab1cce5SSteven Rostedt	    return 0;
25134ab1cce5SSteven Rostedt	}
25142b7d9b21SSteven Rostedt	fail "failed build" and return 0;
25152545eb61SSteven Rostedt    }
25165f9b6cedSSteven Rostedt
25174ab1cce5SSteven Rostedt    $no_reboot = $save_no_reboot;
25184ab1cce5SSteven Rostedt
251938fa3dc1SSteven Rostedt (Red Hat)    my $end_time = time;
252038fa3dc1SSteven Rostedt (Red Hat)    $build_time = $end_time - $start_time;
252138fa3dc1SSteven Rostedt (Red Hat)
25222b7d9b21SSteven Rostedt    return 1;
25232545eb61SSteven Rostedt}
25242545eb61SSteven Rostedt
252575c3fda7SSteven Rostedtsub halt {
2526e48c5293SSteven Rostedt    if (!run_ssh "halt" or defined($power_off)) {
2527576f627cSSteven Rostedt	if (defined($poweroff_after_halt)) {
2528576f627cSSteven Rostedt	    sleep $poweroff_after_halt;
2529576f627cSSteven Rostedt	    run_command "$power_off";
2530576f627cSSteven Rostedt	}
2531576f627cSSteven Rostedt    } else {
253275c3fda7SSteven Rostedt	# nope? the zap it!
2533a75fececSSteven Rostedt	run_command "$power_off";
253475c3fda7SSteven Rostedt    }
253575c3fda7SSteven Rostedt}
253675c3fda7SSteven Rostedt
25375f9b6cedSSteven Rostedtsub success {
25385f9b6cedSSteven Rostedt    my ($i) = @_;
25395f9b6cedSSteven Rostedt
2540e48c5293SSteven Rostedt    $successes++;
2541e48c5293SSteven Rostedt
25429064af52SSteven Rostedt    my $name = "";
25439064af52SSteven Rostedt
25449064af52SSteven Rostedt    if (defined($test_name)) {
25459064af52SSteven Rostedt	$name = " ($test_name)";
25469064af52SSteven Rostedt    }
25479064af52SSteven Rostedt
25484bf6e1fcSSteven Rostedt (Red Hat)    print_times;
25494bf6e1fcSSteven Rostedt (Red Hat)
25505f9b6cedSSteven Rostedt    doprint "\n\n*******************************************\n";
25515f9b6cedSSteven Rostedt    doprint     "*******************************************\n";
25529064af52SSteven Rostedt    doprint     "KTEST RESULT: TEST $i$name SUCCESS!!!!         **\n";
25535f9b6cedSSteven Rostedt    doprint     "*******************************************\n";
25545f9b6cedSSteven Rostedt    doprint     "*******************************************\n";
25555f9b6cedSSteven Rostedt
2556de5b6e3bSRabin Vincent    if (defined($store_successes)) {
2557de5b6e3bSRabin Vincent        save_logs "success", $store_successes;
2558de5b6e3bSRabin Vincent    }
2559de5b6e3bSRabin Vincent
2560576f627cSSteven Rostedt    if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
2561a75fececSSteven Rostedt	doprint "Reboot and wait $sleep_time seconds\n";
2562bc7c5803SSteven Rostedt	reboot_to_good $sleep_time;
25635f9b6cedSSteven Rostedt    }
25642e07c9f5SSteven Rostedt (VMware)
25652e07c9f5SSteven Rostedt (VMware)    if (defined($post_test)) {
25662e07c9f5SSteven Rostedt (VMware)	run_command $post_test;
25672e07c9f5SSteven Rostedt (VMware)    }
25685f9b6cedSSteven Rostedt}
25695f9b6cedSSteven Rostedt
2570c960bb9fSSteven Rostedtsub answer_bisect {
2571c960bb9fSSteven Rostedt    for (;;) {
2572fee9d3e6SChris J Arges	doprint "Pass, fail, or skip? [p/f/s]";
2573c960bb9fSSteven Rostedt	my $ans = <STDIN>;
2574c960bb9fSSteven Rostedt	chomp $ans;
2575c960bb9fSSteven Rostedt	if ($ans eq "p" || $ans eq "P") {
2576c960bb9fSSteven Rostedt	    return 1;
2577c960bb9fSSteven Rostedt	} elsif ($ans eq "f" || $ans eq "F") {
2578c960bb9fSSteven Rostedt	    return 0;
2579fee9d3e6SChris J Arges	} elsif ($ans eq "s" || $ans eq "S") {
2580fee9d3e6SChris J Arges	    return -1;
2581c960bb9fSSteven Rostedt	} else {
2582fee9d3e6SChris J Arges	    print "Please answer 'p', 'f', or 's'\n";
2583c960bb9fSSteven Rostedt	}
2584c960bb9fSSteven Rostedt    }
2585c960bb9fSSteven Rostedt}
2586c960bb9fSSteven Rostedt
25875a391fbfSSteven Rostedtsub child_run_test {
25885a391fbfSSteven Rostedt
25897faafbd6SSteven Rostedt    # child should have no power
2590a75fececSSteven Rostedt    $reboot_on_error = 0;
2591a75fececSSteven Rostedt    $poweroff_on_error = 0;
2592a75fececSSteven Rostedt    $die_on_failure = 1;
25937faafbd6SSteven Rostedt
25945739438bSSteven Rostedt (VMware)    run_command $run_test, $testlog;
2595a9dd5d63SRabin Vincent
25965739438bSSteven Rostedt (VMware)    exit $run_command_status;
25975a391fbfSSteven Rostedt}
25985a391fbfSSteven Rostedt
25995a391fbfSSteven Rostedtmy $child_done;
26005a391fbfSSteven Rostedt
26015a391fbfSSteven Rostedtsub child_finished {
26025a391fbfSSteven Rostedt    $child_done = 1;
26035a391fbfSSteven Rostedt}
26045a391fbfSSteven Rostedt
26055a391fbfSSteven Rostedtsub do_run_test {
26065a391fbfSSteven Rostedt    my $child_pid;
26075a391fbfSSteven Rostedt    my $child_exit;
26085a391fbfSSteven Rostedt    my $line;
26095a391fbfSSteven Rostedt    my $full_line;
26105a391fbfSSteven Rostedt    my $bug = 0;
26119b1d367dSSteven Rostedt    my $bug_ignored = 0;
26125a391fbfSSteven Rostedt
261338fa3dc1SSteven Rostedt (Red Hat)    my $start_time = time;
261438fa3dc1SSteven Rostedt (Red Hat)
26157faafbd6SSteven Rostedt    wait_for_monitor 1;
26165a391fbfSSteven Rostedt
26177faafbd6SSteven Rostedt    doprint "run test $run_test\n";
26185a391fbfSSteven Rostedt
26195a391fbfSSteven Rostedt    $child_done = 0;
26205a391fbfSSteven Rostedt
26215a391fbfSSteven Rostedt    $SIG{CHLD} = qw(child_finished);
26225a391fbfSSteven Rostedt
26235a391fbfSSteven Rostedt    $child_pid = fork;
26245a391fbfSSteven Rostedt
26255a391fbfSSteven Rostedt    child_run_test if (!$child_pid);
26265a391fbfSSteven Rostedt
26275a391fbfSSteven Rostedt    $full_line = "";
26285a391fbfSSteven Rostedt
26295a391fbfSSteven Rostedt    do {
26307faafbd6SSteven Rostedt	$line = wait_for_input($monitor_fp, 1);
26315a391fbfSSteven Rostedt	if (defined($line)) {
26325a391fbfSSteven Rostedt
26335a391fbfSSteven Rostedt	    # we are not guaranteed to get a full line
26345a391fbfSSteven Rostedt	    $full_line .= $line;
26358ea0e063SSteven Rostedt	    doprint $line;
26365a391fbfSSteven Rostedt
26375a391fbfSSteven Rostedt	    if ($full_line =~ /call trace:/i) {
26389b1d367dSSteven Rostedt		if ($ignore_errors) {
26399b1d367dSSteven Rostedt		    $bug_ignored = 1;
26409b1d367dSSteven Rostedt		} else {
26415a391fbfSSteven Rostedt		    $bug = 1;
26425a391fbfSSteven Rostedt		}
26439b1d367dSSteven Rostedt	    }
26445a391fbfSSteven Rostedt
26455a391fbfSSteven Rostedt	    if ($full_line =~ /Kernel panic -/) {
26465a391fbfSSteven Rostedt		$bug = 1;
26475a391fbfSSteven Rostedt	    }
26485a391fbfSSteven Rostedt
26495a391fbfSSteven Rostedt	    if ($line =~ /\n/) {
26505a391fbfSSteven Rostedt		$full_line = "";
26515a391fbfSSteven Rostedt	    }
26525a391fbfSSteven Rostedt	}
26535a391fbfSSteven Rostedt    } while (!$child_done && !$bug);
26545a391fbfSSteven Rostedt
26559b1d367dSSteven Rostedt    if (!$bug && $bug_ignored) {
26569b1d367dSSteven Rostedt	doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
26579b1d367dSSteven Rostedt    }
26589b1d367dSSteven Rostedt
26595a391fbfSSteven Rostedt    if ($bug) {
26608ea0e063SSteven Rostedt	my $failure_start = time;
26618ea0e063SSteven Rostedt	my $now;
26628ea0e063SSteven Rostedt	do {
26638ea0e063SSteven Rostedt	    $line = wait_for_input($monitor_fp, 1);
26648ea0e063SSteven Rostedt	    if (defined($line)) {
26658ea0e063SSteven Rostedt		doprint $line;
26668ea0e063SSteven Rostedt	    }
26678ea0e063SSteven Rostedt	    $now = time;
26688ea0e063SSteven Rostedt	    if ($now - $failure_start >= $stop_after_failure) {
26698ea0e063SSteven Rostedt		last;
26708ea0e063SSteven Rostedt	    }
26718ea0e063SSteven Rostedt	} while (defined($line));
26728ea0e063SSteven Rostedt
26735a391fbfSSteven Rostedt	doprint "Detected kernel crash!\n";
26745a391fbfSSteven Rostedt	# kill the child with extreme prejudice
26755a391fbfSSteven Rostedt	kill 9, $child_pid;
26765a391fbfSSteven Rostedt    }
26775a391fbfSSteven Rostedt
26785a391fbfSSteven Rostedt    waitpid $child_pid, 0;
267932677207SSteven Rostedt (VMware)    $child_exit = $? >> 8;
26805a391fbfSSteven Rostedt
268138fa3dc1SSteven Rostedt (Red Hat)    my $end_time = time;
268238fa3dc1SSteven Rostedt (Red Hat)    $test_time = $end_time - $start_time;
268338fa3dc1SSteven Rostedt (Red Hat)
2684c5dacb88SSteven Rostedt    if (!$bug && $in_bisect) {
2685c5dacb88SSteven Rostedt	if (defined($bisect_ret_good)) {
2686c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_good) {
2687c5dacb88SSteven Rostedt		return 1;
2688c5dacb88SSteven Rostedt	    }
2689c5dacb88SSteven Rostedt	}
2690c5dacb88SSteven Rostedt	if (defined($bisect_ret_skip)) {
2691c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_skip) {
2692c5dacb88SSteven Rostedt		return -1;
2693c5dacb88SSteven Rostedt	    }
2694c5dacb88SSteven Rostedt	}
2695c5dacb88SSteven Rostedt	if (defined($bisect_ret_abort)) {
2696c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_abort) {
2697c5dacb88SSteven Rostedt		fail "test abort" and return -2;
2698c5dacb88SSteven Rostedt	    }
2699c5dacb88SSteven Rostedt	}
2700c5dacb88SSteven Rostedt	if (defined($bisect_ret_bad)) {
2701c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_skip) {
2702c5dacb88SSteven Rostedt		return 0;
2703c5dacb88SSteven Rostedt	    }
2704c5dacb88SSteven Rostedt	}
2705c5dacb88SSteven Rostedt	if (defined($bisect_ret_default)) {
2706c5dacb88SSteven Rostedt	    if ($bisect_ret_default eq "good") {
2707c5dacb88SSteven Rostedt		return 1;
2708c5dacb88SSteven Rostedt	    } elsif ($bisect_ret_default eq "bad") {
2709c5dacb88SSteven Rostedt		return 0;
2710c5dacb88SSteven Rostedt	    } elsif ($bisect_ret_default eq "skip") {
2711c5dacb88SSteven Rostedt		return -1;
2712c5dacb88SSteven Rostedt	    } elsif ($bisect_ret_default eq "abort") {
2713c5dacb88SSteven Rostedt		return -2;
2714c5dacb88SSteven Rostedt	    } else {
2715c5dacb88SSteven Rostedt		fail "unknown default action: $bisect_ret_default"
2716c5dacb88SSteven Rostedt		    and return -2;
2717c5dacb88SSteven Rostedt	    }
2718c5dacb88SSteven Rostedt	}
2719c5dacb88SSteven Rostedt    }
2720c5dacb88SSteven Rostedt
27215a391fbfSSteven Rostedt    if ($bug || $child_exit) {
27222b7d9b21SSteven Rostedt	return 0 if $in_bisect;
27232b7d9b21SSteven Rostedt	fail "test failed" and return 0;
27245a391fbfSSteven Rostedt    }
27252b7d9b21SSteven Rostedt    return 1;
27265a391fbfSSteven Rostedt}
27275a391fbfSSteven Rostedt
2728a75fececSSteven Rostedtsub run_git_bisect {
2729a75fececSSteven Rostedt    my ($command) = @_;
2730a75fececSSteven Rostedt
2731a75fececSSteven Rostedt    doprint "$command ... ";
2732a75fececSSteven Rostedt
2733a75fececSSteven Rostedt    my $output = `$command 2>&1`;
2734a75fececSSteven Rostedt    my $ret = $?;
2735a75fececSSteven Rostedt
2736a75fececSSteven Rostedt    logit $output;
2737a75fececSSteven Rostedt
2738a75fececSSteven Rostedt    if ($ret) {
2739a75fececSSteven Rostedt	doprint "FAILED\n";
2740a75fececSSteven Rostedt	dodie "Failed to git bisect";
2741a75fececSSteven Rostedt    }
2742a75fececSSteven Rostedt
2743a75fececSSteven Rostedt    doprint "SUCCESS\n";
2744a75fececSSteven Rostedt    if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2745a75fececSSteven Rostedt	doprint "$1 [$2]\n";
2746a75fececSSteven Rostedt    } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
2747b5f4aea6SSteven Rostedt	$bisect_bad_commit = $1;
2748a75fececSSteven Rostedt	doprint "Found bad commit... $1\n";
2749a75fececSSteven Rostedt	return 0;
2750a75fececSSteven Rostedt    } else {
2751a75fececSSteven Rostedt	# we already logged it, just print it now.
2752a75fececSSteven Rostedt	print $output;
2753a75fececSSteven Rostedt    }
2754a75fececSSteven Rostedt
2755a75fececSSteven Rostedt    return 1;
2756a75fececSSteven Rostedt}
2757a75fececSSteven Rostedt
2758c23dca7cSSteven Rostedtsub bisect_reboot {
2759c23dca7cSSteven Rostedt    doprint "Reboot and sleep $bisect_sleep_time seconds\n";
2760bc7c5803SSteven Rostedt    reboot_to_good $bisect_sleep_time;
2761c23dca7cSSteven Rostedt}
2762c23dca7cSSteven Rostedt
2763c23dca7cSSteven Rostedt# returns 1 on success, 0 on failure, -1 on skip
27640a05c769SSteven Rostedtsub run_bisect_test {
27650a05c769SSteven Rostedt    my ($type, $buildtype) = @_;
27665f9b6cedSSteven Rostedt
27672b7d9b21SSteven Rostedt    my $failed = 0;
27685f9b6cedSSteven Rostedt    my $result;
27695f9b6cedSSteven Rostedt    my $output;
27705f9b6cedSSteven Rostedt    my $ret;
27715f9b6cedSSteven Rostedt
27720a05c769SSteven Rostedt    $in_bisect = 1;
27730a05c769SSteven Rostedt
27740a05c769SSteven Rostedt    build $buildtype or $failed = 1;
27755f9b6cedSSteven Rostedt
27765f9b6cedSSteven Rostedt    if ($type ne "build") {
2777c23dca7cSSteven Rostedt	if ($failed && $bisect_skip) {
2778c23dca7cSSteven Rostedt	    $in_bisect = 0;
2779c23dca7cSSteven Rostedt	    return -1;
2780c23dca7cSSteven Rostedt	}
27817faafbd6SSteven Rostedt	dodie "Failed on build" if $failed;
27825f9b6cedSSteven Rostedt
27835f9b6cedSSteven Rostedt	# Now boot the box
278464d98283SSteven Rostedt (Red Hat)	start_monitor_and_install or $failed = 1;
27855f9b6cedSSteven Rostedt
27865f9b6cedSSteven Rostedt	if ($type ne "boot") {
2787c23dca7cSSteven Rostedt	    if ($failed && $bisect_skip) {
2788c23dca7cSSteven Rostedt		end_monitor;
2789c23dca7cSSteven Rostedt		bisect_reboot;
2790c23dca7cSSteven Rostedt		$in_bisect = 0;
2791c23dca7cSSteven Rostedt		return -1;
2792c23dca7cSSteven Rostedt	    }
27937faafbd6SSteven Rostedt	    dodie "Failed on boot" if $failed;
27945a391fbfSSteven Rostedt
27952b7d9b21SSteven Rostedt	    do_run_test or $failed = 1;
27965f9b6cedSSteven Rostedt	}
27977faafbd6SSteven Rostedt	end_monitor;
27985f9b6cedSSteven Rostedt    }
27995f9b6cedSSteven Rostedt
28005f9b6cedSSteven Rostedt    if ($failed) {
28010a05c769SSteven Rostedt	$result = 0;
28025f9b6cedSSteven Rostedt    } else {
28030a05c769SSteven Rostedt	$result = 1;
28045f9b6cedSSteven Rostedt    }
28054025bc62SSteven Rostedt
28064025bc62SSteven Rostedt    # reboot the box to a kernel we can ssh to
28074025bc62SSteven Rostedt    if ($type ne "build") {
28084025bc62SSteven Rostedt	bisect_reboot;
28094025bc62SSteven Rostedt    }
28100a05c769SSteven Rostedt    $in_bisect = 0;
28110a05c769SSteven Rostedt
28120a05c769SSteven Rostedt    return $result;
28130a05c769SSteven Rostedt}
28140a05c769SSteven Rostedt
28150a05c769SSteven Rostedtsub run_bisect {
28160a05c769SSteven Rostedt    my ($type) = @_;
28170a05c769SSteven Rostedt    my $buildtype = "oldconfig";
28180a05c769SSteven Rostedt
28190a05c769SSteven Rostedt    # We should have a minconfig to use?
28200a05c769SSteven Rostedt    if (defined($minconfig)) {
28210a05c769SSteven Rostedt	$buildtype = "useconfig:$minconfig";
28220a05c769SSteven Rostedt    }
28230a05c769SSteven Rostedt
2824961d9cacSSteven Rostedt (Red Hat)    # If the user sets bisect_tries to less than 1, then no tries
2825961d9cacSSteven Rostedt (Red Hat)    # is a success.
2826961d9cacSSteven Rostedt (Red Hat)    my $ret = 1;
2827961d9cacSSteven Rostedt (Red Hat)
2828961d9cacSSteven Rostedt (Red Hat)    # Still let the user manually decide that though.
2829961d9cacSSteven Rostedt (Red Hat)    if ($bisect_tries < 1 && $bisect_manual) {
2830961d9cacSSteven Rostedt (Red Hat)	$ret = answer_bisect;
2831961d9cacSSteven Rostedt (Red Hat)    }
2832961d9cacSSteven Rostedt (Red Hat)
2833961d9cacSSteven Rostedt (Red Hat)    for (my $i = 0; $i < $bisect_tries; $i++) {
2834961d9cacSSteven Rostedt (Red Hat)	if ($bisect_tries > 1) {
2835961d9cacSSteven Rostedt (Red Hat)	    my $t = $i + 1;
2836961d9cacSSteven Rostedt (Red Hat)	    doprint("Running bisect trial $t of $bisect_tries:\n");
2837961d9cacSSteven Rostedt (Red Hat)	}
2838961d9cacSSteven Rostedt (Red Hat)	$ret = run_bisect_test $type, $buildtype;
28390a05c769SSteven Rostedt
2840c960bb9fSSteven Rostedt	if ($bisect_manual) {
2841c960bb9fSSteven Rostedt	    $ret = answer_bisect;
2842c960bb9fSSteven Rostedt	}
28435f9b6cedSSteven Rostedt
2844961d9cacSSteven Rostedt (Red Hat)	last if (!$ret);
2845961d9cacSSteven Rostedt (Red Hat)    }
2846961d9cacSSteven Rostedt (Red Hat)
2847d6ce2a0bSSteven Rostedt    # Are we looking for where it worked, not failed?
28485158ba3eSRuss Dill    if ($reverse_bisect && $ret >= 0) {
28490a05c769SSteven Rostedt	$ret = !$ret;
2850d6ce2a0bSSteven Rostedt    }
2851d6ce2a0bSSteven Rostedt
2852c23dca7cSSteven Rostedt    if ($ret > 0) {
28530a05c769SSteven Rostedt	return "good";
2854c23dca7cSSteven Rostedt    } elsif ($ret == 0) {
28550a05c769SSteven Rostedt	return  "bad";
2856c23dca7cSSteven Rostedt    } elsif ($bisect_skip) {
2857c23dca7cSSteven Rostedt	doprint "HIT A BAD COMMIT ... SKIPPING\n";
2858c23dca7cSSteven Rostedt	return "skip";
28590a05c769SSteven Rostedt    }
28605f9b6cedSSteven Rostedt}
28615f9b6cedSSteven Rostedt
2862dad98754SSteven Rostedtsub update_bisect_replay {
2863dad98754SSteven Rostedt    my $tmp_log = "$tmpdir/ktest_bisect_log";
2864dad98754SSteven Rostedt    run_command "git bisect log > $tmp_log" or
2865dad98754SSteven Rostedt	die "can't create bisect log";
2866dad98754SSteven Rostedt    return $tmp_log;
2867dad98754SSteven Rostedt}
2868dad98754SSteven Rostedt
28695f9b6cedSSteven Rostedtsub bisect {
28705f9b6cedSSteven Rostedt    my ($i) = @_;
28715f9b6cedSSteven Rostedt
28725f9b6cedSSteven Rostedt    my $result;
28735f9b6cedSSteven Rostedt
2874b5f4aea6SSteven Rostedt    die "BISECT_GOOD[$i] not defined\n"	if (!defined($bisect_good));
2875b5f4aea6SSteven Rostedt    die "BISECT_BAD[$i] not defined\n"	if (!defined($bisect_bad));
2876b5f4aea6SSteven Rostedt    die "BISECT_TYPE[$i] not defined\n"	if (!defined($bisect_type));
28775f9b6cedSSteven Rostedt
2878b5f4aea6SSteven Rostedt    my $good = $bisect_good;
2879b5f4aea6SSteven Rostedt    my $bad = $bisect_bad;
2880b5f4aea6SSteven Rostedt    my $type = $bisect_type;
2881b5f4aea6SSteven Rostedt    my $start = $bisect_start;
2882b5f4aea6SSteven Rostedt    my $replay = $bisect_replay;
2883b5f4aea6SSteven Rostedt    my $start_files = $bisect_files;
28843410f6fdSSteven Rostedt
28853410f6fdSSteven Rostedt    if (defined($start_files)) {
28863410f6fdSSteven Rostedt	$start_files = " -- " . $start_files;
28873410f6fdSSteven Rostedt    } else {
28883410f6fdSSteven Rostedt	$start_files = "";
28893410f6fdSSteven Rostedt    }
28905f9b6cedSSteven Rostedt
2891a57419b3SSteven Rostedt    # convert to true sha1's
2892a57419b3SSteven Rostedt    $good = get_sha1($good);
2893a57419b3SSteven Rostedt    $bad = get_sha1($bad);
2894a57419b3SSteven Rostedt
2895b5f4aea6SSteven Rostedt    if (defined($bisect_reverse) && $bisect_reverse == 1) {
2896d6ce2a0bSSteven Rostedt	doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2897d6ce2a0bSSteven Rostedt	$reverse_bisect = 1;
2898d6ce2a0bSSteven Rostedt    } else {
2899d6ce2a0bSSteven Rostedt	$reverse_bisect = 0;
2900d6ce2a0bSSteven Rostedt    }
2901d6ce2a0bSSteven Rostedt
29025a391fbfSSteven Rostedt    # Can't have a test without having a test to run
29035a391fbfSSteven Rostedt    if ($type eq "test" && !defined($run_test)) {
29045a391fbfSSteven Rostedt	$type = "boot";
29055a391fbfSSteven Rostedt    }
29065a391fbfSSteven Rostedt
2907dad98754SSteven Rostedt    # Check if a bisect was running
2908dad98754SSteven Rostedt    my $bisect_start_file = "$builddir/.git/BISECT_START";
2909dad98754SSteven Rostedt
2910b5f4aea6SSteven Rostedt    my $check = $bisect_check;
2911dad98754SSteven Rostedt    my $do_check = defined($check) && $check ne "0";
2912dad98754SSteven Rostedt
2913dad98754SSteven Rostedt    if ( -f $bisect_start_file ) {
2914dad98754SSteven Rostedt	print "Bisect in progress found\n";
2915dad98754SSteven Rostedt	if ($do_check) {
2916dad98754SSteven Rostedt	    print " If you say yes, then no checks of good or bad will be done\n";
2917dad98754SSteven Rostedt	}
2918dad98754SSteven Rostedt	if (defined($replay)) {
2919dad98754SSteven Rostedt	    print "** BISECT_REPLAY is defined in config file **";
2920dad98754SSteven Rostedt	    print " Ignore config option and perform new git bisect log?\n";
2921dad98754SSteven Rostedt	    if (read_ync " (yes, no, or cancel) ") {
2922dad98754SSteven Rostedt		$replay = update_bisect_replay;
2923dad98754SSteven Rostedt		$do_check = 0;
2924dad98754SSteven Rostedt	    }
2925dad98754SSteven Rostedt	} elsif (read_yn "read git log and continue?") {
2926dad98754SSteven Rostedt	    $replay = update_bisect_replay;
2927dad98754SSteven Rostedt	    $do_check = 0;
2928dad98754SSteven Rostedt	}
2929dad98754SSteven Rostedt    }
2930dad98754SSteven Rostedt
2931dad98754SSteven Rostedt    if ($do_check) {
2932a75fececSSteven Rostedt
2933a75fececSSteven Rostedt	# get current HEAD
2934a57419b3SSteven Rostedt	my $head = get_sha1("HEAD");
2935a75fececSSteven Rostedt
2936a75fececSSteven Rostedt	if ($check ne "good") {
2937a75fececSSteven Rostedt	    doprint "TESTING BISECT BAD [$bad]\n";
2938a75fececSSteven Rostedt	    run_command "git checkout $bad" or
2939a75fececSSteven Rostedt		die "Failed to checkout $bad";
2940a75fececSSteven Rostedt
2941a75fececSSteven Rostedt	    $result = run_bisect $type;
2942a75fececSSteven Rostedt
2943a75fececSSteven Rostedt	    if ($result ne "bad") {
2944a75fececSSteven Rostedt		fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2945a75fececSSteven Rostedt	    }
2946a75fececSSteven Rostedt	}
2947a75fececSSteven Rostedt
2948a75fececSSteven Rostedt	if ($check ne "bad") {
2949a75fececSSteven Rostedt	    doprint "TESTING BISECT GOOD [$good]\n";
2950a75fececSSteven Rostedt	    run_command "git checkout $good" or
2951a75fececSSteven Rostedt		die "Failed to checkout $good";
2952a75fececSSteven Rostedt
2953a75fececSSteven Rostedt	    $result = run_bisect $type;
2954a75fececSSteven Rostedt
2955a75fececSSteven Rostedt	    if ($result ne "good") {
2956a75fececSSteven Rostedt		fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2957a75fececSSteven Rostedt	    }
2958a75fececSSteven Rostedt	}
2959a75fececSSteven Rostedt
2960a75fececSSteven Rostedt	# checkout where we started
2961a75fececSSteven Rostedt	run_command "git checkout $head" or
2962a75fececSSteven Rostedt	    die "Failed to checkout $head";
2963a75fececSSteven Rostedt    }
2964a75fececSSteven Rostedt
29653410f6fdSSteven Rostedt    run_command "git bisect start$start_files" or
2966a75fececSSteven Rostedt	dodie "could not start bisect";
2967a75fececSSteven Rostedt
2968d832d743SSteven Rostedt (Red Hat)    if (defined($replay)) {
2969d832d743SSteven Rostedt (Red Hat)	run_command "git bisect replay $replay" or
2970d832d743SSteven Rostedt (Red Hat)	    dodie "failed to run replay";
2971d832d743SSteven Rostedt (Red Hat)    } else {
2972d832d743SSteven Rostedt (Red Hat)
2973a75fececSSteven Rostedt	run_command "git bisect good $good" or
2974a75fececSSteven Rostedt	    dodie "could not set bisect good to $good";
2975a75fececSSteven Rostedt
2976a75fececSSteven Rostedt	run_git_bisect "git bisect bad $bad" or
2977a75fececSSteven Rostedt	    dodie "could not set bisect bad to $bad";
2978a75fececSSteven Rostedt
2979a75fececSSteven Rostedt    }
2980a75fececSSteven Rostedt
2981a75fececSSteven Rostedt    if (defined($start)) {
2982a75fececSSteven Rostedt	run_command "git checkout $start" or
2983a75fececSSteven Rostedt	    dodie "failed to checkout $start";
2984a75fececSSteven Rostedt    }
2985a75fececSSteven Rostedt
2986a75fececSSteven Rostedt    my $test;
29875f9b6cedSSteven Rostedt    do {
29885f9b6cedSSteven Rostedt	$result = run_bisect $type;
2989a75fececSSteven Rostedt	$test = run_git_bisect "git bisect $result";
299038fa3dc1SSteven Rostedt (Red Hat)	print_times;
2991a75fececSSteven Rostedt    } while ($test);
29925f9b6cedSSteven Rostedt
29935f9b6cedSSteven Rostedt    run_command "git bisect log" or
29945f9b6cedSSteven Rostedt	dodie "could not capture git bisect log";
29955f9b6cedSSteven Rostedt
29965f9b6cedSSteven Rostedt    run_command "git bisect reset" or
29975f9b6cedSSteven Rostedt	dodie "could not reset git bisect";
29985f9b6cedSSteven Rostedt
2999b5f4aea6SSteven Rostedt    doprint "Bad commit was [$bisect_bad_commit]\n";
30005f9b6cedSSteven Rostedt
30010a05c769SSteven Rostedt    success $i;
30020a05c769SSteven Rostedt}
30030a05c769SSteven Rostedt
3004cf79fab6SSteven Rostedt# config_ignore holds the configs that were set (or unset) for
3005cf79fab6SSteven Rostedt# a good config and we will ignore these configs for the rest
3006cf79fab6SSteven Rostedt# of a config bisect. These configs stay as they were.
30070a05c769SSteven Rostedtmy %config_ignore;
3008cf79fab6SSteven Rostedt
3009cf79fab6SSteven Rostedt# config_set holds what all configs were set as.
30100a05c769SSteven Rostedtmy %config_set;
30110a05c769SSteven Rostedt
3012cf79fab6SSteven Rostedt# config_off holds the set of configs that the bad config had disabled.
3013cf79fab6SSteven Rostedt# We need to record them and set them in the .config when running
3014fb16d891SAdam Lee# olddefconfig, because olddefconfig keeps the defaults.
3015cf79fab6SSteven Rostedtmy %config_off;
3016cf79fab6SSteven Rostedt
3017cf79fab6SSteven Rostedt# config_off_tmp holds a set of configs to turn off for now
3018cf79fab6SSteven Rostedtmy @config_off_tmp;
3019cf79fab6SSteven Rostedt
3020cf79fab6SSteven Rostedt# config_list is the set of configs that are being tested
30210a05c769SSteven Rostedtmy %config_list;
30220a05c769SSteven Rostedtmy %null_config;
30230a05c769SSteven Rostedt
30240a05c769SSteven Rostedtmy %dependency;
30250a05c769SSteven Rostedt
30264c4ab120SSteven Rostedtsub assign_configs {
30274c4ab120SSteven Rostedt    my ($hash, $config) = @_;
30280a05c769SSteven Rostedt
30296071c22eSSteven Rostedt (Red Hat)    doprint "Reading configs from $config\n";
30306071c22eSSteven Rostedt (Red Hat)
30310a05c769SSteven Rostedt    open (IN, $config)
30320a05c769SSteven Rostedt	or dodie "Failed to read $config";
30330a05c769SSteven Rostedt
30340a05c769SSteven Rostedt    while (<IN>) {
30356071c22eSSteven Rostedt (Red Hat)	chomp;
30369bf71749SSteven Rostedt	if (/^((CONFIG\S*)=.*)/) {
30374c4ab120SSteven Rostedt	    ${$hash}{$2} = $1;
30386071c22eSSteven Rostedt (Red Hat)	} elsif (/^(# (CONFIG\S*) is not set)/) {
30396071c22eSSteven Rostedt (Red Hat)	    ${$hash}{$2} = $1;
30400a05c769SSteven Rostedt	}
30410a05c769SSteven Rostedt    }
30420a05c769SSteven Rostedt
30430a05c769SSteven Rostedt    close(IN);
30440a05c769SSteven Rostedt}
30450a05c769SSteven Rostedt
30464c4ab120SSteven Rostedtsub process_config_ignore {
30474c4ab120SSteven Rostedt    my ($config) = @_;
30484c4ab120SSteven Rostedt
30494c4ab120SSteven Rostedt    assign_configs \%config_ignore, $config;
30504c4ab120SSteven Rostedt}
30514c4ab120SSteven Rostedt
30520a05c769SSteven Rostedtsub get_dependencies {
30530a05c769SSteven Rostedt    my ($config) = @_;
30540a05c769SSteven Rostedt
30550a05c769SSteven Rostedt    my $arr = $dependency{$config};
30560a05c769SSteven Rostedt    if (!defined($arr)) {
30570a05c769SSteven Rostedt	return ();
30580a05c769SSteven Rostedt    }
30590a05c769SSteven Rostedt
30600a05c769SSteven Rostedt    my @deps = @{$arr};
30610a05c769SSteven Rostedt
30620a05c769SSteven Rostedt    foreach my $dep (@{$arr}) {
30630a05c769SSteven Rostedt	print "ADD DEP $dep\n";
30640a05c769SSteven Rostedt	@deps = (@deps, get_dependencies $dep);
30650a05c769SSteven Rostedt    }
30660a05c769SSteven Rostedt
30670a05c769SSteven Rostedt    return @deps;
30680a05c769SSteven Rostedt}
30690a05c769SSteven Rostedt
30706071c22eSSteven Rostedt (Red Hat)sub save_config {
30716071c22eSSteven Rostedt (Red Hat)    my ($pc, $file) = @_;
30720a05c769SSteven Rostedt
30736071c22eSSteven Rostedt (Red Hat)    my %configs = %{$pc};
30740a05c769SSteven Rostedt
30756071c22eSSteven Rostedt (Red Hat)    doprint "Saving configs into $file\n";
30760a05c769SSteven Rostedt
30776071c22eSSteven Rostedt (Red Hat)    open(OUT, ">$file") or dodie "Can not write to $file";
3078cf79fab6SSteven Rostedt
30796071c22eSSteven Rostedt (Red Hat)    foreach my $config (keys %configs) {
30806071c22eSSteven Rostedt (Red Hat)	print OUT "$configs{$config}\n";
30810a05c769SSteven Rostedt    }
30820a05c769SSteven Rostedt    close(OUT);
30836071c22eSSteven Rostedt (Red Hat)}
30846071c22eSSteven Rostedt (Red Hat)
30856071c22eSSteven Rostedt (Red Hat)sub create_config {
30866071c22eSSteven Rostedt (Red Hat)    my ($name, $pc) = @_;
30876071c22eSSteven Rostedt (Red Hat)
30886071c22eSSteven Rostedt (Red Hat)    doprint "Creating old config from $name configs\n";
30896071c22eSSteven Rostedt (Red Hat)
30906071c22eSSteven Rostedt (Red Hat)    save_config $pc, $output_config;
30910a05c769SSteven Rostedt
3092fcb3f16aSSteven Rostedt    make_oldconfig;
30930a05c769SSteven Rostedt}
30940a05c769SSteven Rostedt
30950a05c769SSteven Rostedtsub run_config_bisect_test {
30960a05c769SSteven Rostedt    my ($type) = @_;
30970a05c769SSteven Rostedt
30984cc559bdSSteven Rostedt (Red Hat)    my $ret = run_bisect_test $type, "oldconfig";
30994cc559bdSSteven Rostedt (Red Hat)
31004cc559bdSSteven Rostedt (Red Hat)    if ($bisect_manual) {
31014cc559bdSSteven Rostedt (Red Hat)	$ret = answer_bisect;
31024cc559bdSSteven Rostedt (Red Hat)    }
31034cc559bdSSteven Rostedt (Red Hat)
31044cc559bdSSteven Rostedt (Red Hat)    return $ret;
31050a05c769SSteven Rostedt}
31060a05c769SSteven Rostedt
3107*a9adc261SScott Woodsub config_bisect_end {
3108*a9adc261SScott Wood    my ($good, $bad) = @_;
31090a05c769SSteven Rostedt
31100a05c769SSteven Rostedt    doprint "\n\n***************************************\n";
3111*a9adc261SScott Wood    doprint "No more config bisecting possible.\n";
3112*a9adc261SScott Wood    doprint `diff -u $good $bad`;
31130a05c769SSteven Rostedt    doprint "***************************************\n\n";
31140a05c769SSteven Rostedt}
31150a05c769SSteven Rostedt
31166071c22eSSteven Rostedt (Red Hat)sub run_config_bisect {
3117*a9adc261SScott Wood    my ($good, $bad, $last_result) = @_;
3118*a9adc261SScott Wood    my $cmd;
31196071c22eSSteven Rostedt (Red Hat)    my $ret;
31206071c22eSSteven Rostedt (Red Hat)
3121*a9adc261SScott Wood    run_command "$builddir/tools/testing/ktest/config-bisect.pl -b $outputdir $good $bad $last_result";
31226071c22eSSteven Rostedt (Red Hat)
3123*a9adc261SScott Wood    # config-bisect returns:
3124*a9adc261SScott Wood    #   0 if there is more to bisect
3125*a9adc261SScott Wood    #   1 for finding a good config
3126*a9adc261SScott Wood    #   2 if it can not find any more configs
3127*a9adc261SScott Wood    #  -1 (255) on error
3128*a9adc261SScott Wood    if ($run_command_status) {
3129*a9adc261SScott Wood	return $run_command_status;
31306071c22eSSteven Rostedt (Red Hat)    }
31316071c22eSSteven Rostedt (Red Hat)
3132*a9adc261SScott Wood    $ret = run_config_bisect_test $config_bisect_type;
31336071c22eSSteven Rostedt (Red Hat)    if ($ret) {
31346071c22eSSteven Rostedt (Red Hat)        doprint "NEW GOOD CONFIG\n";
3135*a9adc261SScott Wood	# Return 3 for good config
3136*a9adc261SScott Wood	return 3;
3137cf79fab6SSteven Rostedt    } else {
31386071c22eSSteven Rostedt (Red Hat)        doprint "NEW BAD CONFIG\n";
3139*a9adc261SScott Wood	# Return 4 for bad config
3140*a9adc261SScott Wood	return 4;
31416071c22eSSteven Rostedt (Red Hat)    }
31420a05c769SSteven Rostedt}
31430a05c769SSteven Rostedt
31440a05c769SSteven Rostedtsub config_bisect {
31450a05c769SSteven Rostedt    my ($i) = @_;
31460a05c769SSteven Rostedt
3147*a9adc261SScott Wood    my $good_config;
3148*a9adc261SScott Wood    my $bad_config;
3149*a9adc261SScott Wood
31506071c22eSSteven Rostedt (Red Hat)    my $type = $config_bisect_type;
3151c4d1d11fSSteven Rostedt (Red Hat)    my $ret;
31520a05c769SSteven Rostedt
31536071c22eSSteven Rostedt (Red Hat)    $bad_config = $config_bisect;
31540a05c769SSteven Rostedt
315530f75da5SSteven Rostedt    if (defined($config_bisect_good)) {
31566071c22eSSteven Rostedt (Red Hat)	$good_config = $config_bisect_good;
31576071c22eSSteven Rostedt (Red Hat)    } elsif (defined($minconfig)) {
31586071c22eSSteven Rostedt (Red Hat)	$good_config = $minconfig;
31590a05c769SSteven Rostedt    } else {
31606071c22eSSteven Rostedt (Red Hat)	doprint "No config specified, checking if defconfig works";
3161c4d1d11fSSteven Rostedt (Red Hat)	$ret = run_bisect_test $type, "defconfig";
31626071c22eSSteven Rostedt (Red Hat)	if (!$ret) {
31636071c22eSSteven Rostedt (Red Hat)	    fail "Have no good config to compare with, please set CONFIG_BISECT_GOOD";
31646071c22eSSteven Rostedt (Red Hat)	    return 1;
31656071c22eSSteven Rostedt (Red Hat)	}
31666071c22eSSteven Rostedt (Red Hat)	$good_config = $output_config;
31670a05c769SSteven Rostedt    }
31680a05c769SSteven Rostedt
31696071c22eSSteven Rostedt (Red Hat)    # we don't want min configs to cause issues here.
31706071c22eSSteven Rostedt (Red Hat)    doprint "Disabling 'MIN_CONFIG' for this test\n";
31716071c22eSSteven Rostedt (Red Hat)    undef $minconfig;
31720a05c769SSteven Rostedt
31736071c22eSSteven Rostedt (Red Hat)    my %good_configs;
31746071c22eSSteven Rostedt (Red Hat)    my %bad_configs;
31756071c22eSSteven Rostedt (Red Hat)    my %tmp_configs;
31760a05c769SSteven Rostedt
31776071c22eSSteven Rostedt (Red Hat)    doprint "Run good configs through make oldconfig\n";
31786071c22eSSteven Rostedt (Red Hat)    assign_configs \%tmp_configs, $good_config;
31796071c22eSSteven Rostedt (Red Hat)    create_config "$good_config", \%tmp_configs;
3180*a9adc261SScott Wood    $good_config = "$tmpdir/good_config";
3181*a9adc261SScott Wood    system("cp $output_config $good_config") == 0 or dodie "cp good config";
31820a05c769SSteven Rostedt
31836071c22eSSteven Rostedt (Red Hat)    doprint "Run bad configs through make oldconfig\n";
31846071c22eSSteven Rostedt (Red Hat)    assign_configs \%tmp_configs, $bad_config;
31856071c22eSSteven Rostedt (Red Hat)    create_config "$bad_config", \%tmp_configs;
31866071c22eSSteven Rostedt (Red Hat)    $bad_config = "$tmpdir/bad_config";
3187*a9adc261SScott Wood    system("cp $output_config $bad_config") == 0 or dodie "cp bad config";
31880a05c769SSteven Rostedt
3189c4d1d11fSSteven Rostedt (Red Hat)    if (defined($config_bisect_check) && $config_bisect_check ne "0") {
3190c4d1d11fSSteven Rostedt (Red Hat)	if ($config_bisect_check ne "good") {
3191c4d1d11fSSteven Rostedt (Red Hat)	    doprint "Testing bad config\n";
3192c4d1d11fSSteven Rostedt (Red Hat)
3193c4d1d11fSSteven Rostedt (Red Hat)	    $ret = run_bisect_test $type, "useconfig:$bad_config";
3194c4d1d11fSSteven Rostedt (Red Hat)	    if ($ret) {
3195c4d1d11fSSteven Rostedt (Red Hat)		fail "Bad config succeeded when expected to fail!";
3196c4d1d11fSSteven Rostedt (Red Hat)		return 0;
3197c4d1d11fSSteven Rostedt (Red Hat)	    }
3198c4d1d11fSSteven Rostedt (Red Hat)	}
3199c4d1d11fSSteven Rostedt (Red Hat)	if ($config_bisect_check ne "bad") {
3200c4d1d11fSSteven Rostedt (Red Hat)	    doprint "Testing good config\n";
3201c4d1d11fSSteven Rostedt (Red Hat)
3202c4d1d11fSSteven Rostedt (Red Hat)	    $ret = run_bisect_test $type, "useconfig:$good_config";
3203c4d1d11fSSteven Rostedt (Red Hat)	    if (!$ret) {
3204c4d1d11fSSteven Rostedt (Red Hat)		fail "Good config failed when expected to succeed!";
3205c4d1d11fSSteven Rostedt (Red Hat)		return 0;
3206c4d1d11fSSteven Rostedt (Red Hat)	    }
3207c4d1d11fSSteven Rostedt (Red Hat)	}
3208c4d1d11fSSteven Rostedt (Red Hat)    }
3209b0918612SSteven Rostedt
3210*a9adc261SScott Wood    my $last_run = "";
3211*a9adc261SScott Wood
32120a05c769SSteven Rostedt    do {
3213*a9adc261SScott Wood	$ret = run_config_bisect $good_config, $bad_config, $last_run;
3214*a9adc261SScott Wood	if ($ret == 3) {
3215*a9adc261SScott Wood	    $last_run = "good";
3216*a9adc261SScott Wood	} elsif ($ret == 4) {
3217*a9adc261SScott Wood	    $last_run = "bad";
3218*a9adc261SScott Wood	}
321938fa3dc1SSteven Rostedt (Red Hat)	print_times;
3220*a9adc261SScott Wood    } while ($ret == 3 || $ret == 4);
3221*a9adc261SScott Wood
3222*a9adc261SScott Wood    if ($ret == 2) {
3223*a9adc261SScott Wood        config_bisect_end "$good_config.tmp", "$bad_config.tmp";
3224*a9adc261SScott Wood    }
32250a05c769SSteven Rostedt
32260a05c769SSteven Rostedt    return $ret if ($ret < 0);
32275f9b6cedSSteven Rostedt
32285f9b6cedSSteven Rostedt    success $i;
32295f9b6cedSSteven Rostedt}
32305f9b6cedSSteven Rostedt
323127d934b2SSteven Rostedtsub patchcheck_reboot {
323227d934b2SSteven Rostedt    doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
3233bc7c5803SSteven Rostedt    reboot_to_good $patchcheck_sleep_time;
323427d934b2SSteven Rostedt}
323527d934b2SSteven Rostedt
32366c5ee0beSSteven Rostedtsub patchcheck {
32376c5ee0beSSteven Rostedt    my ($i) = @_;
32386c5ee0beSSteven Rostedt
32396c5ee0beSSteven Rostedt    die "PATCHCHECK_START[$i] not defined\n"
3240b5f4aea6SSteven Rostedt	if (!defined($patchcheck_start));
32416c5ee0beSSteven Rostedt    die "PATCHCHECK_TYPE[$i] not defined\n"
3242b5f4aea6SSteven Rostedt	if (!defined($patchcheck_type));
32436c5ee0beSSteven Rostedt
3244b5f4aea6SSteven Rostedt    my $start = $patchcheck_start;
32456c5ee0beSSteven Rostedt
324623a0e161SSteven Rostedt (Red Hat)    my $cherry = $patchcheck_cherry;
324723a0e161SSteven Rostedt (Red Hat)    if (!defined($cherry)) {
324823a0e161SSteven Rostedt (Red Hat)	$cherry = 0;
324923a0e161SSteven Rostedt (Red Hat)    }
325023a0e161SSteven Rostedt (Red Hat)
32516c5ee0beSSteven Rostedt    my $end = "HEAD";
3252b5f4aea6SSteven Rostedt    if (defined($patchcheck_end)) {
3253b5f4aea6SSteven Rostedt	$end = $patchcheck_end;
325423a0e161SSteven Rostedt (Red Hat)    } elsif ($cherry) {
325523a0e161SSteven Rostedt (Red Hat)	die "PATCHCHECK_END must be defined with PATCHCHECK_CHERRY\n";
32566c5ee0beSSteven Rostedt    }
32576c5ee0beSSteven Rostedt
3258a57419b3SSteven Rostedt    # Get the true sha1's since we can use things like HEAD~3
3259a57419b3SSteven Rostedt    $start = get_sha1($start);
3260a57419b3SSteven Rostedt    $end = get_sha1($end);
3261a57419b3SSteven Rostedt
3262b5f4aea6SSteven Rostedt    my $type = $patchcheck_type;
32636c5ee0beSSteven Rostedt
32646c5ee0beSSteven Rostedt    # Can't have a test without having a test to run
32656c5ee0beSSteven Rostedt    if ($type eq "test" && !defined($run_test)) {
32666c5ee0beSSteven Rostedt	$type = "boot";
32676c5ee0beSSteven Rostedt    }
32686c5ee0beSSteven Rostedt
326923a0e161SSteven Rostedt (Red Hat)    if ($cherry) {
327023a0e161SSteven Rostedt (Red Hat)	open (IN, "git cherry -v $start $end|") or
327123a0e161SSteven Rostedt (Red Hat)	    dodie "could not get git list";
327223a0e161SSteven Rostedt (Red Hat)    } else {
32736c5ee0beSSteven Rostedt	open (IN, "git log --pretty=oneline $end|") or
32746c5ee0beSSteven Rostedt	    dodie "could not get git list";
327523a0e161SSteven Rostedt (Red Hat)    }
32766c5ee0beSSteven Rostedt
32776c5ee0beSSteven Rostedt    my @list;
32786c5ee0beSSteven Rostedt
32796c5ee0beSSteven Rostedt    while (<IN>) {
32806c5ee0beSSteven Rostedt	chomp;
328123a0e161SSteven Rostedt (Red Hat)	# git cherry adds a '+' we want to remove
328223a0e161SSteven Rostedt (Red Hat)	s/^\+ //;
32836c5ee0beSSteven Rostedt	$list[$#list+1] = $_;
32846c5ee0beSSteven Rostedt	last if (/^$start/);
32856c5ee0beSSteven Rostedt    }
32866c5ee0beSSteven Rostedt    close(IN);
32876c5ee0beSSteven Rostedt
328823a0e161SSteven Rostedt (Red Hat)    if (!$cherry) {
32896c5ee0beSSteven Rostedt	if ($list[$#list] !~ /^$start/) {
32902b7d9b21SSteven Rostedt	    fail "SHA1 $start not found";
32916c5ee0beSSteven Rostedt	}
32926c5ee0beSSteven Rostedt
32936c5ee0beSSteven Rostedt	# go backwards in the list
32946c5ee0beSSteven Rostedt	@list = reverse @list;
329523a0e161SSteven Rostedt (Red Hat)    }
329623a0e161SSteven Rostedt (Red Hat)
329723a0e161SSteven Rostedt (Red Hat)    doprint("Going to test the following commits:\n");
329823a0e161SSteven Rostedt (Red Hat)    foreach my $l (@list) {
329923a0e161SSteven Rostedt (Red Hat)	doprint "$l\n";
330023a0e161SSteven Rostedt (Red Hat)    }
33016c5ee0beSSteven Rostedt
33026c5ee0beSSteven Rostedt    my $save_clean = $noclean;
33031990207dSSteven Rostedt    my %ignored_warnings;
33041990207dSSteven Rostedt
33051990207dSSteven Rostedt    if (defined($ignore_warnings)) {
33061990207dSSteven Rostedt	foreach my $sha1 (split /\s+/, $ignore_warnings) {
33071990207dSSteven Rostedt	    $ignored_warnings{$sha1} = 1;
33081990207dSSteven Rostedt	}
33091990207dSSteven Rostedt    }
33106c5ee0beSSteven Rostedt
33116c5ee0beSSteven Rostedt    $in_patchcheck = 1;
33126c5ee0beSSteven Rostedt    foreach my $item (@list) {
33136c5ee0beSSteven Rostedt	my $sha1 = $item;
33146c5ee0beSSteven Rostedt	$sha1 =~ s/^([[:xdigit:]]+).*/$1/;
33156c5ee0beSSteven Rostedt
33167c2c49ecSSteven Rostedt (Red Hat)	doprint "\nProcessing commit \"$item\"\n\n";
33176c5ee0beSSteven Rostedt
33186c5ee0beSSteven Rostedt	run_command "git checkout $sha1" or
33196c5ee0beSSteven Rostedt	    die "Failed to checkout $sha1";
33206c5ee0beSSteven Rostedt
33216c5ee0beSSteven Rostedt	# only clean on the first and last patch
33226c5ee0beSSteven Rostedt	if ($item eq $list[0] ||
33236c5ee0beSSteven Rostedt	    $item eq $list[$#list]) {
33246c5ee0beSSteven Rostedt	    $noclean = $save_clean;
33256c5ee0beSSteven Rostedt	} else {
33266c5ee0beSSteven Rostedt	    $noclean = 1;
33276c5ee0beSSteven Rostedt	}
33286c5ee0beSSteven Rostedt
33296c5ee0beSSteven Rostedt	if (defined($minconfig)) {
33302b7d9b21SSteven Rostedt	    build "useconfig:$minconfig" or return 0;
33316c5ee0beSSteven Rostedt	} else {
33326c5ee0beSSteven Rostedt	    # ?? no config to use?
33332b7d9b21SSteven Rostedt	    build "oldconfig" or return 0;
33346c5ee0beSSteven Rostedt	}
33356c5ee0beSSteven Rostedt
33364283b169SSteven Rostedt (Red Hat)	# No need to do per patch checking if warnings file exists
33374283b169SSteven Rostedt (Red Hat)	if (!defined($warnings_file) && !defined($ignored_warnings{$sha1})) {
33384283b169SSteven Rostedt (Red Hat)	    check_patch_buildlog $sha1 or return 0;
33391990207dSSteven Rostedt	}
33406c5ee0beSSteven Rostedt
33414283b169SSteven Rostedt (Red Hat)	check_buildlog or return 0;
33424283b169SSteven Rostedt (Red Hat)
33436c5ee0beSSteven Rostedt	next if ($type eq "build");
33446c5ee0beSSteven Rostedt
33457faafbd6SSteven Rostedt	my $failed = 0;
33467faafbd6SSteven Rostedt
334764d98283SSteven Rostedt (Red Hat)	start_monitor_and_install or $failed = 1;
33487faafbd6SSteven Rostedt
33497faafbd6SSteven Rostedt	if (!$failed && $type ne "boot"){
33507faafbd6SSteven Rostedt	    do_run_test or $failed = 1;
33517faafbd6SSteven Rostedt	}
33527faafbd6SSteven Rostedt	end_monitor;
335338fa3dc1SSteven Rostedt (Red Hat)	if ($failed) {
335438fa3dc1SSteven Rostedt (Red Hat)	    print_times;
335538fa3dc1SSteven Rostedt (Red Hat)	    return 0;
335638fa3dc1SSteven Rostedt (Red Hat)	}
335727d934b2SSteven Rostedt	patchcheck_reboot;
335838fa3dc1SSteven Rostedt (Red Hat)	print_times;
33596c5ee0beSSteven Rostedt    }
33606c5ee0beSSteven Rostedt    $in_patchcheck = 0;
33616c5ee0beSSteven Rostedt    success $i;
33622b7d9b21SSteven Rostedt
33632b7d9b21SSteven Rostedt    return 1;
33646c5ee0beSSteven Rostedt}
33656c5ee0beSSteven Rostedt
3366b9066f6cSSteven Rostedtmy %depends;
3367ac6974c7SSteven Rostedtmy %depcount;
3368b9066f6cSSteven Rostedtmy $iflevel = 0;
3369b9066f6cSSteven Rostedtmy @ifdeps;
3370b9066f6cSSteven Rostedt
3371b9066f6cSSteven Rostedt# prevent recursion
3372b9066f6cSSteven Rostedtmy %read_kconfigs;
3373b9066f6cSSteven Rostedt
3374ac6974c7SSteven Rostedtsub add_dep {
3375ac6974c7SSteven Rostedt    # $config depends on $dep
3376ac6974c7SSteven Rostedt    my ($config, $dep) = @_;
3377ac6974c7SSteven Rostedt
3378ac6974c7SSteven Rostedt    if (defined($depends{$config})) {
3379ac6974c7SSteven Rostedt	$depends{$config} .= " " . $dep;
3380ac6974c7SSteven Rostedt    } else {
3381ac6974c7SSteven Rostedt	$depends{$config} = $dep;
3382ac6974c7SSteven Rostedt    }
3383ac6974c7SSteven Rostedt
3384ac6974c7SSteven Rostedt    # record the number of configs depending on $dep
3385ac6974c7SSteven Rostedt    if (defined $depcount{$dep}) {
3386ac6974c7SSteven Rostedt	$depcount{$dep}++;
3387ac6974c7SSteven Rostedt    } else {
3388ac6974c7SSteven Rostedt	$depcount{$dep} = 1;
3389ac6974c7SSteven Rostedt    }
3390ac6974c7SSteven Rostedt}
3391ac6974c7SSteven Rostedt
3392b9066f6cSSteven Rostedt# taken from streamline_config.pl
3393b9066f6cSSteven Rostedtsub read_kconfig {
3394b9066f6cSSteven Rostedt    my ($kconfig) = @_;
3395b9066f6cSSteven Rostedt
3396b9066f6cSSteven Rostedt    my $state = "NONE";
3397b9066f6cSSteven Rostedt    my $config;
3398b9066f6cSSteven Rostedt    my @kconfigs;
3399b9066f6cSSteven Rostedt
3400b9066f6cSSteven Rostedt    my $cont = 0;
3401b9066f6cSSteven Rostedt    my $line;
3402b9066f6cSSteven Rostedt
3403b9066f6cSSteven Rostedt
3404b9066f6cSSteven Rostedt    if (! -f $kconfig) {
3405b9066f6cSSteven Rostedt	doprint "file $kconfig does not exist, skipping\n";
3406b9066f6cSSteven Rostedt	return;
3407b9066f6cSSteven Rostedt    }
3408b9066f6cSSteven Rostedt
3409b9066f6cSSteven Rostedt    open(KIN, "$kconfig")
3410b9066f6cSSteven Rostedt	or die "Can't open $kconfig";
3411b9066f6cSSteven Rostedt    while (<KIN>) {
3412b9066f6cSSteven Rostedt	chomp;
3413b9066f6cSSteven Rostedt
3414b9066f6cSSteven Rostedt	# Make sure that lines ending with \ continue
3415b9066f6cSSteven Rostedt	if ($cont) {
3416b9066f6cSSteven Rostedt	    $_ = $line . " " . $_;
3417b9066f6cSSteven Rostedt	}
3418b9066f6cSSteven Rostedt
3419b9066f6cSSteven Rostedt	if (s/\\$//) {
3420b9066f6cSSteven Rostedt	    $cont = 1;
3421b9066f6cSSteven Rostedt	    $line = $_;
3422b9066f6cSSteven Rostedt	    next;
3423b9066f6cSSteven Rostedt	}
3424b9066f6cSSteven Rostedt
3425b9066f6cSSteven Rostedt	$cont = 0;
3426b9066f6cSSteven Rostedt
3427b9066f6cSSteven Rostedt	# collect any Kconfig sources
3428b9066f6cSSteven Rostedt	if (/^source\s*"(.*)"/) {
3429b9066f6cSSteven Rostedt	    $kconfigs[$#kconfigs+1] = $1;
3430b9066f6cSSteven Rostedt	}
3431b9066f6cSSteven Rostedt
3432b9066f6cSSteven Rostedt	# configs found
3433b9066f6cSSteven Rostedt	if (/^\s*(menu)?config\s+(\S+)\s*$/) {
3434b9066f6cSSteven Rostedt	    $state = "NEW";
3435b9066f6cSSteven Rostedt	    $config = $2;
3436b9066f6cSSteven Rostedt
3437b9066f6cSSteven Rostedt	    for (my $i = 0; $i < $iflevel; $i++) {
3438ac6974c7SSteven Rostedt		add_dep $config, $ifdeps[$i];
3439b9066f6cSSteven Rostedt	    }
3440b9066f6cSSteven Rostedt
3441b9066f6cSSteven Rostedt	# collect the depends for the config
3442b9066f6cSSteven Rostedt	} elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
3443b9066f6cSSteven Rostedt
3444ac6974c7SSteven Rostedt	    add_dep $config, $1;
3445b9066f6cSSteven Rostedt
3446b9066f6cSSteven Rostedt	# Get the configs that select this config
3447ac6974c7SSteven Rostedt	} elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
3448ac6974c7SSteven Rostedt
3449ac6974c7SSteven Rostedt	    # selected by depends on config
3450ac6974c7SSteven Rostedt	    add_dep $1, $config;
3451b9066f6cSSteven Rostedt
3452b9066f6cSSteven Rostedt	# Check for if statements
3453b9066f6cSSteven Rostedt	} elsif (/^if\s+(.*\S)\s*$/) {
3454b9066f6cSSteven Rostedt	    my $deps = $1;
3455b9066f6cSSteven Rostedt	    # remove beginning and ending non text
3456b9066f6cSSteven Rostedt	    $deps =~ s/^[^a-zA-Z0-9_]*//;
3457b9066f6cSSteven Rostedt	    $deps =~ s/[^a-zA-Z0-9_]*$//;
3458b9066f6cSSteven Rostedt
3459b9066f6cSSteven Rostedt	    my @deps = split /[^a-zA-Z0-9_]+/, $deps;
3460b9066f6cSSteven Rostedt
3461b9066f6cSSteven Rostedt	    $ifdeps[$iflevel++] = join ':', @deps;
3462b9066f6cSSteven Rostedt
3463b9066f6cSSteven Rostedt	} elsif (/^endif/) {
3464b9066f6cSSteven Rostedt
3465b9066f6cSSteven Rostedt	    $iflevel-- if ($iflevel);
3466b9066f6cSSteven Rostedt
3467b9066f6cSSteven Rostedt	# stop on "help"
3468b9066f6cSSteven Rostedt	} elsif (/^\s*help\s*$/) {
3469b9066f6cSSteven Rostedt	    $state = "NONE";
3470b9066f6cSSteven Rostedt	}
3471b9066f6cSSteven Rostedt    }
3472b9066f6cSSteven Rostedt    close(KIN);
3473b9066f6cSSteven Rostedt
3474b9066f6cSSteven Rostedt    # read in any configs that were found.
3475b9066f6cSSteven Rostedt    foreach $kconfig (@kconfigs) {
3476b9066f6cSSteven Rostedt	if (!defined($read_kconfigs{$kconfig})) {
3477b9066f6cSSteven Rostedt	    $read_kconfigs{$kconfig} = 1;
3478b9066f6cSSteven Rostedt	    read_kconfig("$builddir/$kconfig");
3479b9066f6cSSteven Rostedt	}
3480b9066f6cSSteven Rostedt    }
3481b9066f6cSSteven Rostedt}
3482b9066f6cSSteven Rostedt
3483b9066f6cSSteven Rostedtsub read_depends {
3484b9066f6cSSteven Rostedt    # find out which arch this is by the kconfig file
3485b9066f6cSSteven Rostedt    open (IN, $output_config)
3486b9066f6cSSteven Rostedt	or dodie "Failed to read $output_config";
3487b9066f6cSSteven Rostedt    my $arch;
3488b9066f6cSSteven Rostedt    while (<IN>) {
3489b9066f6cSSteven Rostedt	if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
3490b9066f6cSSteven Rostedt	    $arch = $1;
3491b9066f6cSSteven Rostedt	    last;
3492b9066f6cSSteven Rostedt	}
3493b9066f6cSSteven Rostedt    }
3494b9066f6cSSteven Rostedt    close IN;
3495b9066f6cSSteven Rostedt
3496b9066f6cSSteven Rostedt    if (!defined($arch)) {
3497b9066f6cSSteven Rostedt	doprint "Could not find arch from config file\n";
3498b9066f6cSSteven Rostedt	doprint "no dependencies used\n";
3499b9066f6cSSteven Rostedt	return;
3500b9066f6cSSteven Rostedt    }
3501b9066f6cSSteven Rostedt
3502b9066f6cSSteven Rostedt    # arch is really the subarch, we need to know
3503b9066f6cSSteven Rostedt    # what directory to look at.
3504b9066f6cSSteven Rostedt    if ($arch eq "i386" || $arch eq "x86_64") {
3505b9066f6cSSteven Rostedt	$arch = "x86";
3506b9066f6cSSteven Rostedt    } elsif ($arch =~ /^tile/) {
3507b9066f6cSSteven Rostedt	$arch = "tile";
3508b9066f6cSSteven Rostedt    }
3509b9066f6cSSteven Rostedt
3510b9066f6cSSteven Rostedt    my $kconfig = "$builddir/arch/$arch/Kconfig";
3511b9066f6cSSteven Rostedt
3512b9066f6cSSteven Rostedt    if (! -f $kconfig && $arch =~ /\d$/) {
3513b9066f6cSSteven Rostedt	my $orig = $arch;
3514b9066f6cSSteven Rostedt 	# some subarchs have numbers, truncate them
3515b9066f6cSSteven Rostedt	$arch =~ s/\d*$//;
3516b9066f6cSSteven Rostedt	$kconfig = "$builddir/arch/$arch/Kconfig";
3517b9066f6cSSteven Rostedt	if (! -f $kconfig) {
3518b9066f6cSSteven Rostedt	    doprint "No idea what arch dir $orig is for\n";
3519b9066f6cSSteven Rostedt	    doprint "no dependencies used\n";
3520b9066f6cSSteven Rostedt	    return;
3521b9066f6cSSteven Rostedt	}
3522b9066f6cSSteven Rostedt    }
3523b9066f6cSSteven Rostedt
3524b9066f6cSSteven Rostedt    read_kconfig($kconfig);
3525b9066f6cSSteven Rostedt}
3526b9066f6cSSteven Rostedt
35274c4ab120SSteven Rostedtsub make_new_config {
35284c4ab120SSteven Rostedt    my @configs = @_;
35294c4ab120SSteven Rostedt
35304c4ab120SSteven Rostedt    open (OUT, ">$output_config")
35314c4ab120SSteven Rostedt	or dodie "Failed to write $output_config";
35324c4ab120SSteven Rostedt
35334c4ab120SSteven Rostedt    foreach my $config (@configs) {
35344c4ab120SSteven Rostedt	print OUT "$config\n";
35354c4ab120SSteven Rostedt    }
35364c4ab120SSteven Rostedt    close OUT;
35374c4ab120SSteven Rostedt}
35384c4ab120SSteven Rostedt
3539ac6974c7SSteven Rostedtsub chomp_config {
3540ac6974c7SSteven Rostedt    my ($config) = @_;
3541ac6974c7SSteven Rostedt
3542ac6974c7SSteven Rostedt    $config =~ s/CONFIG_//;
3543ac6974c7SSteven Rostedt
3544ac6974c7SSteven Rostedt    return $config;
3545ac6974c7SSteven Rostedt}
3546ac6974c7SSteven Rostedt
3547b9066f6cSSteven Rostedtsub get_depends {
3548b9066f6cSSteven Rostedt    my ($dep) = @_;
3549b9066f6cSSteven Rostedt
3550ac6974c7SSteven Rostedt    my $kconfig = chomp_config $dep;
3551b9066f6cSSteven Rostedt
3552b9066f6cSSteven Rostedt    $dep = $depends{"$kconfig"};
3553b9066f6cSSteven Rostedt
3554b9066f6cSSteven Rostedt    # the dep string we have saves the dependencies as they
3555b9066f6cSSteven Rostedt    # were found, including expressions like ! && ||. We
3556b9066f6cSSteven Rostedt    # want to split this out into just an array of configs.
3557b9066f6cSSteven Rostedt
3558b9066f6cSSteven Rostedt    my $valid = "A-Za-z_0-9";
3559b9066f6cSSteven Rostedt
3560b9066f6cSSteven Rostedt    my @configs;
3561b9066f6cSSteven Rostedt
3562b9066f6cSSteven Rostedt    while ($dep =~ /[$valid]/) {
3563b9066f6cSSteven Rostedt
3564b9066f6cSSteven Rostedt	if ($dep =~ /^[^$valid]*([$valid]+)/) {
3565b9066f6cSSteven Rostedt	    my $conf = "CONFIG_" . $1;
3566b9066f6cSSteven Rostedt
3567b9066f6cSSteven Rostedt	    $configs[$#configs + 1] = $conf;
3568b9066f6cSSteven Rostedt
3569b9066f6cSSteven Rostedt	    $dep =~ s/^[^$valid]*[$valid]+//;
3570b9066f6cSSteven Rostedt	} else {
3571b9066f6cSSteven Rostedt	    die "this should never happen";
3572b9066f6cSSteven Rostedt	}
3573b9066f6cSSteven Rostedt    }
3574b9066f6cSSteven Rostedt
3575b9066f6cSSteven Rostedt    return @configs;
3576b9066f6cSSteven Rostedt}
3577b9066f6cSSteven Rostedt
3578b9066f6cSSteven Rostedtmy %min_configs;
3579b9066f6cSSteven Rostedtmy %keep_configs;
358043d1b651SSteven Rostedtmy %save_configs;
3581b9066f6cSSteven Rostedtmy %processed_configs;
3582b9066f6cSSteven Rostedtmy %nochange_config;
3583b9066f6cSSteven Rostedt
3584b9066f6cSSteven Rostedtsub test_this_config {
3585b9066f6cSSteven Rostedt    my ($config) = @_;
3586b9066f6cSSteven Rostedt
3587b9066f6cSSteven Rostedt    my $found;
3588b9066f6cSSteven Rostedt
3589b9066f6cSSteven Rostedt    # if we already processed this config, skip it
3590b9066f6cSSteven Rostedt    if (defined($processed_configs{$config})) {
3591b9066f6cSSteven Rostedt	return undef;
3592b9066f6cSSteven Rostedt    }
3593b9066f6cSSteven Rostedt    $processed_configs{$config} = 1;
3594b9066f6cSSteven Rostedt
3595b9066f6cSSteven Rostedt    # if this config failed during this round, skip it
3596b9066f6cSSteven Rostedt    if (defined($nochange_config{$config})) {
3597b9066f6cSSteven Rostedt	return undef;
3598b9066f6cSSteven Rostedt    }
3599b9066f6cSSteven Rostedt
3600ac6974c7SSteven Rostedt    my $kconfig = chomp_config $config;
3601b9066f6cSSteven Rostedt
3602b9066f6cSSteven Rostedt    # Test dependencies first
3603b9066f6cSSteven Rostedt    if (defined($depends{"$kconfig"})) {
3604b9066f6cSSteven Rostedt	my @parents = get_depends $config;
3605b9066f6cSSteven Rostedt	foreach my $parent (@parents) {
3606b9066f6cSSteven Rostedt	    # if the parent is in the min config, check it first
3607b9066f6cSSteven Rostedt	    next if (!defined($min_configs{$parent}));
3608b9066f6cSSteven Rostedt	    $found = test_this_config($parent);
3609b9066f6cSSteven Rostedt	    if (defined($found)) {
3610b9066f6cSSteven Rostedt		return $found;
3611b9066f6cSSteven Rostedt	    }
3612b9066f6cSSteven Rostedt	}
3613b9066f6cSSteven Rostedt    }
3614b9066f6cSSteven Rostedt
3615b9066f6cSSteven Rostedt    # Remove this config from the list of configs
3616fb16d891SAdam Lee    # do a make olddefconfig and then read the resulting
3617b9066f6cSSteven Rostedt    # .config to make sure it is missing the config that
3618b9066f6cSSteven Rostedt    # we had before
3619b9066f6cSSteven Rostedt    my %configs = %min_configs;
3620b9066f6cSSteven Rostedt    delete $configs{$config};
3621b9066f6cSSteven Rostedt    make_new_config ((values %configs), (values %keep_configs));
3622b9066f6cSSteven Rostedt    make_oldconfig;
3623b9066f6cSSteven Rostedt    undef %configs;
3624b9066f6cSSteven Rostedt    assign_configs \%configs, $output_config;
3625b9066f6cSSteven Rostedt
36269972fc0bSSteven Rostedt (Red Hat)    if (!defined($configs{$config}) || $configs{$config} =~ /^#/) {
36279972fc0bSSteven Rostedt (Red Hat)	return $config;
36289972fc0bSSteven Rostedt (Red Hat)    }
3629b9066f6cSSteven Rostedt
3630b9066f6cSSteven Rostedt    doprint "disabling config $config did not change .config\n";
3631b9066f6cSSteven Rostedt
3632b9066f6cSSteven Rostedt    $nochange_config{$config} = 1;
3633b9066f6cSSteven Rostedt
3634b9066f6cSSteven Rostedt    return undef;
3635b9066f6cSSteven Rostedt}
3636b9066f6cSSteven Rostedt
36374c4ab120SSteven Rostedtsub make_min_config {
36384c4ab120SSteven Rostedt    my ($i) = @_;
36394c4ab120SSteven Rostedt
3640ccc513b6SSteven Rostedt    my $type = $minconfig_type;
3641ccc513b6SSteven Rostedt    if ($type ne "boot" && $type ne "test") {
3642ccc513b6SSteven Rostedt	fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3643ccc513b6SSteven Rostedt	    " make_min_config works only with 'boot' and 'test'\n" and return;
3644ccc513b6SSteven Rostedt    }
3645ccc513b6SSteven Rostedt
36464c4ab120SSteven Rostedt    if (!defined($output_minconfig)) {
36474c4ab120SSteven Rostedt	fail "OUTPUT_MIN_CONFIG not defined" and return;
36484c4ab120SSteven Rostedt    }
364935ce5952SSteven Rostedt
365035ce5952SSteven Rostedt    # If output_minconfig exists, and the start_minconfig
365135ce5952SSteven Rostedt    # came from min_config, than ask if we should use
365235ce5952SSteven Rostedt    # that instead.
365335ce5952SSteven Rostedt    if (-f $output_minconfig && !$start_minconfig_defined) {
365435ce5952SSteven Rostedt	print "$output_minconfig exists\n";
365543de3316SSteven Rostedt	if (!defined($use_output_minconfig)) {
365635ce5952SSteven Rostedt	    if (read_yn " Use it as minconfig?") {
365735ce5952SSteven Rostedt		$start_minconfig = $output_minconfig;
365835ce5952SSteven Rostedt	    }
365943de3316SSteven Rostedt	} elsif ($use_output_minconfig > 0) {
366043de3316SSteven Rostedt	    doprint "Using $output_minconfig as MIN_CONFIG\n";
366143de3316SSteven Rostedt	    $start_minconfig = $output_minconfig;
366243de3316SSteven Rostedt	} else {
366343de3316SSteven Rostedt	    doprint "Set to still use MIN_CONFIG as starting point\n";
366443de3316SSteven Rostedt	}
366535ce5952SSteven Rostedt    }
366635ce5952SSteven Rostedt
36674c4ab120SSteven Rostedt    if (!defined($start_minconfig)) {
36684c4ab120SSteven Rostedt	fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
36694c4ab120SSteven Rostedt    }
36704c4ab120SSteven Rostedt
367135ce5952SSteven Rostedt    my $temp_config = "$tmpdir/temp_config";
367235ce5952SSteven Rostedt
36734c4ab120SSteven Rostedt    # First things first. We build an allnoconfig to find
36744c4ab120SSteven Rostedt    # out what the defaults are that we can't touch.
36754c4ab120SSteven Rostedt    # Some are selections, but we really can't handle selections.
36764c4ab120SSteven Rostedt
36774c4ab120SSteven Rostedt    my $save_minconfig = $minconfig;
36784c4ab120SSteven Rostedt    undef $minconfig;
36794c4ab120SSteven Rostedt
36804c4ab120SSteven Rostedt    run_command "$make allnoconfig" or return 0;
36814c4ab120SSteven Rostedt
3682b9066f6cSSteven Rostedt    read_depends;
3683b9066f6cSSteven Rostedt
36844c4ab120SSteven Rostedt    process_config_ignore $output_config;
3685b9066f6cSSteven Rostedt
368643d1b651SSteven Rostedt    undef %save_configs;
3687b9066f6cSSteven Rostedt    undef %min_configs;
36884c4ab120SSteven Rostedt
36894c4ab120SSteven Rostedt    if (defined($ignore_config)) {
36904c4ab120SSteven Rostedt	# make sure the file exists
36914c4ab120SSteven Rostedt	`touch $ignore_config`;
369243d1b651SSteven Rostedt	assign_configs \%save_configs, $ignore_config;
36934c4ab120SSteven Rostedt    }
36944c4ab120SSteven Rostedt
369543d1b651SSteven Rostedt    %keep_configs = %save_configs;
369643d1b651SSteven Rostedt
36974c4ab120SSteven Rostedt    doprint "Load initial configs from $start_minconfig\n";
36984c4ab120SSteven Rostedt
36994c4ab120SSteven Rostedt    # Look at the current min configs, and save off all the
37004c4ab120SSteven Rostedt    # ones that were set via the allnoconfig
37014c4ab120SSteven Rostedt    assign_configs \%min_configs, $start_minconfig;
37024c4ab120SSteven Rostedt
37034c4ab120SSteven Rostedt    my @config_keys = keys %min_configs;
37044c4ab120SSteven Rostedt
3705ac6974c7SSteven Rostedt    # All configs need a depcount
3706ac6974c7SSteven Rostedt    foreach my $config (@config_keys) {
3707ac6974c7SSteven Rostedt	my $kconfig = chomp_config $config;
3708ac6974c7SSteven Rostedt	if (!defined $depcount{$kconfig}) {
3709ac6974c7SSteven Rostedt		$depcount{$kconfig} = 0;
3710ac6974c7SSteven Rostedt	}
3711ac6974c7SSteven Rostedt    }
3712ac6974c7SSteven Rostedt
37134c4ab120SSteven Rostedt    # Remove anything that was set by the make allnoconfig
37144c4ab120SSteven Rostedt    # we shouldn't need them as they get set for us anyway.
37154c4ab120SSteven Rostedt    foreach my $config (@config_keys) {
37164c4ab120SSteven Rostedt	# Remove anything in the ignore_config
37174c4ab120SSteven Rostedt	if (defined($keep_configs{$config})) {
37184c4ab120SSteven Rostedt	    my $file = $ignore_config;
37194c4ab120SSteven Rostedt	    $file =~ s,.*/(.*?)$,$1,;
37204c4ab120SSteven Rostedt	    doprint "$config set by $file ... ignored\n";
37214c4ab120SSteven Rostedt	    delete $min_configs{$config};
37224c4ab120SSteven Rostedt	    next;
37234c4ab120SSteven Rostedt	}
37244c4ab120SSteven Rostedt	# But make sure the settings are the same. If a min config
37254c4ab120SSteven Rostedt	# sets a selection, we do not want to get rid of it if
37264c4ab120SSteven Rostedt	# it is not the same as what we have. Just move it into
37274c4ab120SSteven Rostedt	# the keep configs.
37284c4ab120SSteven Rostedt	if (defined($config_ignore{$config})) {
37294c4ab120SSteven Rostedt	    if ($config_ignore{$config} ne $min_configs{$config}) {
37304c4ab120SSteven Rostedt		doprint "$config is in allnoconfig as '$config_ignore{$config}'";
37314c4ab120SSteven Rostedt		doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
37324c4ab120SSteven Rostedt		$keep_configs{$config} = $min_configs{$config};
37334c4ab120SSteven Rostedt	    } else {
37344c4ab120SSteven Rostedt		doprint "$config set by allnoconfig ... ignored\n";
37354c4ab120SSteven Rostedt	    }
37364c4ab120SSteven Rostedt	    delete $min_configs{$config};
37374c4ab120SSteven Rostedt	}
37384c4ab120SSteven Rostedt    }
37394c4ab120SSteven Rostedt
37404c4ab120SSteven Rostedt    my $done = 0;
3741b9066f6cSSteven Rostedt    my $take_two = 0;
37424c4ab120SSteven Rostedt
37434c4ab120SSteven Rostedt    while (!$done) {
37444c4ab120SSteven Rostedt
37454c4ab120SSteven Rostedt	my $config;
37464c4ab120SSteven Rostedt	my $found;
37474c4ab120SSteven Rostedt
37484c4ab120SSteven Rostedt	# Now disable each config one by one and do a make oldconfig
37494c4ab120SSteven Rostedt	# till we find a config that changes our list.
37504c4ab120SSteven Rostedt
37514c4ab120SSteven Rostedt	my @test_configs = keys %min_configs;
3752ac6974c7SSteven Rostedt
3753ac6974c7SSteven Rostedt	# Sort keys by who is most dependent on
3754ac6974c7SSteven Rostedt	@test_configs = sort  { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3755ac6974c7SSteven Rostedt			  @test_configs ;
3756ac6974c7SSteven Rostedt
3757ac6974c7SSteven Rostedt	# Put configs that did not modify the config at the end.
37584c4ab120SSteven Rostedt	my $reset = 1;
37594c4ab120SSteven Rostedt	for (my $i = 0; $i < $#test_configs; $i++) {
37604c4ab120SSteven Rostedt	    if (!defined($nochange_config{$test_configs[0]})) {
37614c4ab120SSteven Rostedt		$reset = 0;
37624c4ab120SSteven Rostedt		last;
37634c4ab120SSteven Rostedt	    }
37644c4ab120SSteven Rostedt	    # This config didn't change the .config last time.
37654c4ab120SSteven Rostedt	    # Place it at the end
37664c4ab120SSteven Rostedt	    my $config = shift @test_configs;
37674c4ab120SSteven Rostedt	    push @test_configs, $config;
37684c4ab120SSteven Rostedt	}
37694c4ab120SSteven Rostedt
37704c4ab120SSteven Rostedt	# if every test config has failed to modify the .config file
37714c4ab120SSteven Rostedt	# in the past, then reset and start over.
37724c4ab120SSteven Rostedt	if ($reset) {
37734c4ab120SSteven Rostedt	    undef %nochange_config;
37744c4ab120SSteven Rostedt	}
37754c4ab120SSteven Rostedt
3776b9066f6cSSteven Rostedt	undef %processed_configs;
3777b9066f6cSSteven Rostedt
37784c4ab120SSteven Rostedt	foreach my $config (@test_configs) {
37794c4ab120SSteven Rostedt
3780b9066f6cSSteven Rostedt	    $found = test_this_config $config;
37814c4ab120SSteven Rostedt
3782b9066f6cSSteven Rostedt	    last if (defined($found));
37834c4ab120SSteven Rostedt
37844c4ab120SSteven Rostedt	    # oh well, try another config
37854c4ab120SSteven Rostedt	}
37864c4ab120SSteven Rostedt
37874c4ab120SSteven Rostedt	if (!defined($found)) {
3788b9066f6cSSteven Rostedt	    # we could have failed due to the nochange_config hash
3789b9066f6cSSteven Rostedt	    # reset and try again
3790b9066f6cSSteven Rostedt	    if (!$take_two) {
3791b9066f6cSSteven Rostedt		undef %nochange_config;
3792b9066f6cSSteven Rostedt		$take_two = 1;
3793b9066f6cSSteven Rostedt		next;
3794b9066f6cSSteven Rostedt	    }
37954c4ab120SSteven Rostedt	    doprint "No more configs found that we can disable\n";
37964c4ab120SSteven Rostedt	    $done = 1;
37974c4ab120SSteven Rostedt	    last;
37984c4ab120SSteven Rostedt	}
3799b9066f6cSSteven Rostedt	$take_two = 0;
38004c4ab120SSteven Rostedt
38014c4ab120SSteven Rostedt	$config = $found;
38024c4ab120SSteven Rostedt
38034c4ab120SSteven Rostedt	doprint "Test with $config disabled\n";
38044c4ab120SSteven Rostedt
38054c4ab120SSteven Rostedt	# set in_bisect to keep build and monitor from dieing
38064c4ab120SSteven Rostedt	$in_bisect = 1;
38074c4ab120SSteven Rostedt
38084c4ab120SSteven Rostedt	my $failed = 0;
3809bf1c95abSSteven Rostedt	build "oldconfig" or $failed = 1;
3810bf1c95abSSteven Rostedt	if (!$failed) {
381164d98283SSteven Rostedt (Red Hat)		start_monitor_and_install or $failed = 1;
3812ccc513b6SSteven Rostedt
3813ccc513b6SSteven Rostedt		if ($type eq "test" && !$failed) {
3814ccc513b6SSteven Rostedt		    do_run_test or $failed = 1;
3815ccc513b6SSteven Rostedt		}
3816ccc513b6SSteven Rostedt
38174c4ab120SSteven Rostedt		end_monitor;
3818bf1c95abSSteven Rostedt	}
38194c4ab120SSteven Rostedt
38204c4ab120SSteven Rostedt	$in_bisect = 0;
38214c4ab120SSteven Rostedt
38224c4ab120SSteven Rostedt	if ($failed) {
3823b9066f6cSSteven Rostedt	    doprint "$min_configs{$config} is needed to boot the box... keeping\n";
38244c4ab120SSteven Rostedt	    # this config is needed, add it to the ignore list.
38254c4ab120SSteven Rostedt	    $keep_configs{$config} = $min_configs{$config};
382643d1b651SSteven Rostedt	    $save_configs{$config} = $min_configs{$config};
38274c4ab120SSteven Rostedt	    delete $min_configs{$config};
382835ce5952SSteven Rostedt
382935ce5952SSteven Rostedt	    # update new ignore configs
383035ce5952SSteven Rostedt	    if (defined($ignore_config)) {
383135ce5952SSteven Rostedt		open (OUT, ">$temp_config")
383235ce5952SSteven Rostedt		    or die "Can't write to $temp_config";
383343d1b651SSteven Rostedt		foreach my $config (keys %save_configs) {
383443d1b651SSteven Rostedt		    print OUT "$save_configs{$config}\n";
383535ce5952SSteven Rostedt		}
383635ce5952SSteven Rostedt		close OUT;
383735ce5952SSteven Rostedt		run_command "mv $temp_config $ignore_config" or
383835ce5952SSteven Rostedt		    dodie "failed to copy update to $ignore_config";
383935ce5952SSteven Rostedt	    }
384035ce5952SSteven Rostedt
38414c4ab120SSteven Rostedt	} else {
38424c4ab120SSteven Rostedt	    # We booted without this config, remove it from the minconfigs.
38434c4ab120SSteven Rostedt	    doprint "$config is not needed, disabling\n";
38444c4ab120SSteven Rostedt
38454c4ab120SSteven Rostedt	    delete $min_configs{$config};
38464c4ab120SSteven Rostedt
38474c4ab120SSteven Rostedt	    # Also disable anything that is not enabled in this config
38484c4ab120SSteven Rostedt	    my %configs;
38494c4ab120SSteven Rostedt	    assign_configs \%configs, $output_config;
38504c4ab120SSteven Rostedt	    my @config_keys = keys %min_configs;
38514c4ab120SSteven Rostedt	    foreach my $config (@config_keys) {
38524c4ab120SSteven Rostedt		if (!defined($configs{$config})) {
38534c4ab120SSteven Rostedt		    doprint "$config is not set, disabling\n";
38544c4ab120SSteven Rostedt		    delete $min_configs{$config};
38554c4ab120SSteven Rostedt		}
38564c4ab120SSteven Rostedt	    }
38574c4ab120SSteven Rostedt
385822722799SPavel Machek	    # Save off all the current mandatory configs
385935ce5952SSteven Rostedt	    open (OUT, ">$temp_config")
386035ce5952SSteven Rostedt		or die "Can't write to $temp_config";
38614c4ab120SSteven Rostedt	    foreach my $config (keys %keep_configs) {
38624c4ab120SSteven Rostedt		print OUT "$keep_configs{$config}\n";
38634c4ab120SSteven Rostedt	    }
38644c4ab120SSteven Rostedt	    foreach my $config (keys %min_configs) {
38654c4ab120SSteven Rostedt		print OUT "$min_configs{$config}\n";
38664c4ab120SSteven Rostedt	    }
38674c4ab120SSteven Rostedt	    close OUT;
386835ce5952SSteven Rostedt
386935ce5952SSteven Rostedt	    run_command "mv $temp_config $output_minconfig" or
387035ce5952SSteven Rostedt		dodie "failed to copy update to $output_minconfig";
38714c4ab120SSteven Rostedt	}
38724c4ab120SSteven Rostedt
38734c4ab120SSteven Rostedt	doprint "Reboot and wait $sleep_time seconds\n";
3874bc7c5803SSteven Rostedt	reboot_to_good $sleep_time;
38754c4ab120SSteven Rostedt    }
38764c4ab120SSteven Rostedt
38774c4ab120SSteven Rostedt    success $i;
38784c4ab120SSteven Rostedt    return 1;
38794c4ab120SSteven Rostedt}
38804c4ab120SSteven Rostedt
38814283b169SSteven Rostedt (Red Hat)sub make_warnings_file {
38824283b169SSteven Rostedt (Red Hat)    my ($i) = @_;
38834283b169SSteven Rostedt (Red Hat)
38844283b169SSteven Rostedt (Red Hat)    if (!defined($warnings_file)) {
38854283b169SSteven Rostedt (Red Hat)	dodie "Must define WARNINGS_FILE for make_warnings_file test";
38864283b169SSteven Rostedt (Red Hat)    }
38874283b169SSteven Rostedt (Red Hat)
38884283b169SSteven Rostedt (Red Hat)    if ($build_type eq "nobuild") {
38894283b169SSteven Rostedt (Red Hat)	dodie "BUILD_TYPE can not be 'nobuild' for make_warnings_file test";
38904283b169SSteven Rostedt (Red Hat)    }
38914283b169SSteven Rostedt (Red Hat)
38924283b169SSteven Rostedt (Red Hat)    build $build_type or dodie "Failed to build";
38934283b169SSteven Rostedt (Red Hat)
38944283b169SSteven Rostedt (Red Hat)    open(OUT, ">$warnings_file") or dodie "Can't create $warnings_file";
38954283b169SSteven Rostedt (Red Hat)
38964283b169SSteven Rostedt (Red Hat)    open(IN, $buildlog) or dodie "Can't open $buildlog";
38974283b169SSteven Rostedt (Red Hat)    while (<IN>) {
38984283b169SSteven Rostedt (Red Hat)
38994283b169SSteven Rostedt (Red Hat)	# Some compilers use UTF-8 extended for quotes
39004283b169SSteven Rostedt (Red Hat)	# for distcc heterogeneous systems, this causes issues
39014283b169SSteven Rostedt (Red Hat)	s/$utf8_quote/'/g;
39024283b169SSteven Rostedt (Red Hat)
39034283b169SSteven Rostedt (Red Hat)	if (/$check_build_re/) {
39044283b169SSteven Rostedt (Red Hat)	    print OUT;
39054283b169SSteven Rostedt (Red Hat)	}
39064283b169SSteven Rostedt (Red Hat)    }
39074283b169SSteven Rostedt (Red Hat)    close(IN);
39084283b169SSteven Rostedt (Red Hat)
39094283b169SSteven Rostedt (Red Hat)    close(OUT);
39104283b169SSteven Rostedt (Red Hat)
39114283b169SSteven Rostedt (Red Hat)    success $i;
39124283b169SSteven Rostedt (Red Hat)}
39134283b169SSteven Rostedt (Red Hat)
39145269faadSSatoru Takeuchi$#ARGV < 1 or die "ktest.pl version: $VERSION\n   usage: ktest.pl [config-file]\n";
39152545eb61SSteven Rostedt
39168d1491baSSteven Rostedtif ($#ARGV == 0) {
39178d1491baSSteven Rostedt    $ktest_config = $ARGV[0];
39188d1491baSSteven Rostedt    if (! -f $ktest_config) {
39198d1491baSSteven Rostedt	print "$ktest_config does not exist.\n";
392035ce5952SSteven Rostedt	if (!read_yn "Create it?") {
39218d1491baSSteven Rostedt	    exit 0;
39228d1491baSSteven Rostedt	}
39238d1491baSSteven Rostedt    }
39248d1491baSSteven Rostedt}
39258d1491baSSteven Rostedt
39268d1491baSSteven Rostedtif (! -f $ktest_config) {
3927dbd3783bSSteven Rostedt    $newconfig = 1;
3928c4261d0fSSteven Rostedt    get_test_case;
39298d1491baSSteven Rostedt    open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
39308d1491baSSteven Rostedt    print OUT << "EOF"
39318d1491baSSteven Rostedt# Generated by ktest.pl
39328d1491baSSteven Rostedt#
39330e7a22deSSteven Rostedt
39340e7a22deSSteven Rostedt# PWD is a ktest.pl variable that will result in the process working
39350e7a22deSSteven Rostedt# directory that ktest.pl is executed in.
39360e7a22deSSteven Rostedt
39370e7a22deSSteven Rostedt# THIS_DIR is automatically assigned the PWD of the path that generated
39380e7a22deSSteven Rostedt# the config file. It is best to use this variable when assigning other
39390e7a22deSSteven Rostedt# directory paths within this directory. This allows you to easily
39400e7a22deSSteven Rostedt# move the test cases to other locations or to other machines.
39410e7a22deSSteven Rostedt#
39420e7a22deSSteven RostedtTHIS_DIR := $variable{"PWD"}
39430e7a22deSSteven Rostedt
39448d1491baSSteven Rostedt# Define each test with TEST_START
39458d1491baSSteven Rostedt# The config options below it will override the defaults
39468d1491baSSteven RostedtTEST_START
3947c4261d0fSSteven RostedtTEST_TYPE = $default{"TEST_TYPE"}
39488d1491baSSteven Rostedt
39498d1491baSSteven RostedtDEFAULTS
39508d1491baSSteven RostedtEOF
39518d1491baSSteven Rostedt;
39528d1491baSSteven Rostedt    close(OUT);
39538d1491baSSteven Rostedt}
39548d1491baSSteven Rostedtread_config $ktest_config;
39558d1491baSSteven Rostedt
395623715c3cSSteven Rostedtif (defined($opt{"LOG_FILE"})) {
395704262be3SSteven Rostedt (Red Hat)    $opt{"LOG_FILE"} = eval_option("LOG_FILE", $opt{"LOG_FILE"}, -1);
395823715c3cSSteven Rostedt}
395923715c3cSSteven Rostedt
39608d1491baSSteven Rostedt# Append any configs entered in manually to the config file.
39618d1491baSSteven Rostedtmy @new_configs = keys %entered_configs;
39628d1491baSSteven Rostedtif ($#new_configs >= 0) {
39638d1491baSSteven Rostedt    print "\nAppending entered in configs to $ktest_config\n";
39648d1491baSSteven Rostedt    open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
39658d1491baSSteven Rostedt    foreach my $config (@new_configs) {
39668d1491baSSteven Rostedt	print OUT "$config = $entered_configs{$config}\n";
39670e7a22deSSteven Rostedt	$opt{$config} = process_variables($entered_configs{$config});
39688d1491baSSteven Rostedt    }
39698d1491baSSteven Rostedt}
39702545eb61SSteven Rostedt
39712b7d9b21SSteven Rostedtif ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
39722b7d9b21SSteven Rostedt    unlink $opt{"LOG_FILE"};
39732b7d9b21SSteven Rostedt}
39742545eb61SSteven Rostedt
39752b7d9b21SSteven Rostedtdoprint "\n\nSTARTING AUTOMATED TESTS\n\n";
39762b7d9b21SSteven Rostedt
3977a57419b3SSteven Rostedtfor (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3978a57419b3SSteven Rostedt
3979a57419b3SSteven Rostedt    if (!$i) {
3980a57419b3SSteven Rostedt	doprint "DEFAULT OPTIONS:\n";
3981a57419b3SSteven Rostedt    } else {
3982a57419b3SSteven Rostedt	doprint "\nTEST $i OPTIONS";
3983a57419b3SSteven Rostedt	if (defined($repeat_tests{$i})) {
3984a57419b3SSteven Rostedt	    $repeat = $repeat_tests{$i};
3985a57419b3SSteven Rostedt	    doprint " ITERATE $repeat";
3986a57419b3SSteven Rostedt	}
3987a57419b3SSteven Rostedt	doprint "\n";
3988a57419b3SSteven Rostedt    }
3989a57419b3SSteven Rostedt
39902b7d9b21SSteven Rostedt    foreach my $option (sort keys %opt) {
3991a57419b3SSteven Rostedt
3992a57419b3SSteven Rostedt	if ($option =~ /\[(\d+)\]$/) {
3993a57419b3SSteven Rostedt	    next if ($i != $1);
3994a57419b3SSteven Rostedt	} else {
3995a57419b3SSteven Rostedt	    next if ($i);
3996a57419b3SSteven Rostedt	}
3997a57419b3SSteven Rostedt
39982b7d9b21SSteven Rostedt	doprint "$option = $opt{$option}\n";
39992b7d9b21SSteven Rostedt    }
4000a57419b3SSteven Rostedt}
40012545eb61SSteven Rostedt
400222c37a9aSSteven Rostedt (Red Hat)sub option_defined {
400322c37a9aSSteven Rostedt (Red Hat)    my ($option) = @_;
400422c37a9aSSteven Rostedt (Red Hat)
400522c37a9aSSteven Rostedt (Red Hat)    if (defined($opt{$option}) && $opt{$option} !~ /^\s*$/) {
400622c37a9aSSteven Rostedt (Red Hat)	return 1;
400722c37a9aSSteven Rostedt (Red Hat)    }
400822c37a9aSSteven Rostedt (Red Hat)
400922c37a9aSSteven Rostedt (Red Hat)    return 0;
401022c37a9aSSteven Rostedt (Red Hat)}
401122c37a9aSSteven Rostedt (Red Hat)
40122a62512bSSteven Rostedtsub __set_test_option {
40135a391fbfSSteven Rostedt    my ($name, $i) = @_;
40145a391fbfSSteven Rostedt
40155a391fbfSSteven Rostedt    my $option = "$name\[$i\]";
40165a391fbfSSteven Rostedt
401722c37a9aSSteven Rostedt (Red Hat)    if (option_defined($option)) {
40185a391fbfSSteven Rostedt	return $opt{$option};
40195a391fbfSSteven Rostedt    }
40205a391fbfSSteven Rostedt
4021a57419b3SSteven Rostedt    foreach my $test (keys %repeat_tests) {
4022a57419b3SSteven Rostedt	if ($i >= $test &&
4023a57419b3SSteven Rostedt	    $i < $test + $repeat_tests{$test}) {
4024a57419b3SSteven Rostedt	    $option = "$name\[$test\]";
402522c37a9aSSteven Rostedt (Red Hat)	    if (option_defined($option)) {
4026a57419b3SSteven Rostedt		return $opt{$option};
4027a57419b3SSteven Rostedt	    }
4028a57419b3SSteven Rostedt	}
4029a57419b3SSteven Rostedt    }
4030a57419b3SSteven Rostedt
403122c37a9aSSteven Rostedt (Red Hat)    if (option_defined($name)) {
40325a391fbfSSteven Rostedt	return $opt{$name};
40335a391fbfSSteven Rostedt    }
40345a391fbfSSteven Rostedt
40355a391fbfSSteven Rostedt    return undef;
40365a391fbfSSteven Rostedt}
40375a391fbfSSteven Rostedt
40382a62512bSSteven Rostedtsub set_test_option {
40392a62512bSSteven Rostedt    my ($name, $i) = @_;
40402a62512bSSteven Rostedt
40412a62512bSSteven Rostedt    my $option = __set_test_option($name, $i);
40422a62512bSSteven Rostedt    return $option if (!defined($option));
40432a62512bSSteven Rostedt
404404262be3SSteven Rostedt (Red Hat)    return eval_option($name, $option, $i);
40452a62512bSSteven Rostedt}
40462a62512bSSteven Rostedt
40472545eb61SSteven Rostedt# First we need to do is the builds
4048a75fececSSteven Rostedtfor (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
40492545eb61SSteven Rostedt
40504ab1cce5SSteven Rostedt    # Do not reboot on failing test options
40514ab1cce5SSteven Rostedt    $no_reboot = 1;
4052759a3cc6SSteven Rostedt    $reboot_success = 0;
40534ab1cce5SSteven Rostedt
4054683a3e64SSteven Rostedt    $have_version = 0;
4055683a3e64SSteven Rostedt
4056576f627cSSteven Rostedt    $iteration = $i;
4057576f627cSSteven Rostedt
405838fa3dc1SSteven Rostedt (Red Hat)    $build_time = 0;
405938fa3dc1SSteven Rostedt (Red Hat)    $install_time = 0;
406038fa3dc1SSteven Rostedt (Red Hat)    $reboot_time = 0;
406138fa3dc1SSteven Rostedt (Red Hat)    $test_time = 0;
406238fa3dc1SSteven Rostedt (Red Hat)
4063c1434dccSSteven Rostedt    undef %force_config;
4064c1434dccSSteven Rostedt
4065a75fececSSteven Rostedt    my $makecmd = set_test_option("MAKE_CMD", $i);
4066a75fececSSteven Rostedt
40678e80bf05SSteven Rostedt (Red Hat)    $outputdir = set_test_option("OUTPUT_DIR", $i);
40688e80bf05SSteven Rostedt (Red Hat)    $builddir = set_test_option("BUILD_DIR", $i);
40698e80bf05SSteven Rostedt (Red Hat)
40708e80bf05SSteven Rostedt (Red Hat)    chdir $builddir || die "can't change directory to $builddir";
40718e80bf05SSteven Rostedt (Red Hat)
40728e80bf05SSteven Rostedt (Red Hat)    if (!-d $outputdir) {
40738e80bf05SSteven Rostedt (Red Hat)	mkpath($outputdir) or
40748e80bf05SSteven Rostedt (Red Hat)	    die "can't create $outputdir";
40758e80bf05SSteven Rostedt (Red Hat)    }
40768e80bf05SSteven Rostedt (Red Hat)
40778e80bf05SSteven Rostedt (Red Hat)    $make = "$makecmd O=$outputdir";
40788e80bf05SSteven Rostedt (Red Hat)
40799cc9e091SSteven Rostedt    # Load all the options into their mapped variable names
40809cc9e091SSteven Rostedt    foreach my $opt (keys %option_map) {
40819cc9e091SSteven Rostedt	${$option_map{$opt}} = set_test_option($opt, $i);
40829cc9e091SSteven Rostedt    }
4083b5f4aea6SSteven Rostedt
408435ce5952SSteven Rostedt    $start_minconfig_defined = 1;
408535ce5952SSteven Rostedt
4086921ed4c7SSteven Rostedt    # The first test may override the PRE_KTEST option
4087921ed4c7SSteven Rostedt    if (defined($pre_ktest) && $i == 1) {
4088921ed4c7SSteven Rostedt	doprint "\n";
4089921ed4c7SSteven Rostedt	run_command $pre_ktest;
4090921ed4c7SSteven Rostedt    }
4091921ed4c7SSteven Rostedt
4092921ed4c7SSteven Rostedt    # Any test can override the POST_KTEST option
4093921ed4c7SSteven Rostedt    # The last test takes precedence.
4094921ed4c7SSteven Rostedt    if (defined($post_ktest)) {
4095921ed4c7SSteven Rostedt	$final_post_ktest = $post_ktest;
4096921ed4c7SSteven Rostedt    }
4097921ed4c7SSteven Rostedt
40984c4ab120SSteven Rostedt    if (!defined($start_minconfig)) {
409935ce5952SSteven Rostedt	$start_minconfig_defined = 0;
41004c4ab120SSteven Rostedt	$start_minconfig = $minconfig;
41014c4ab120SSteven Rostedt    }
41024c4ab120SSteven Rostedt
41038e80bf05SSteven Rostedt (Red Hat)    if (!-d $tmpdir) {
41048e80bf05SSteven Rostedt (Red Hat)	mkpath($tmpdir) or
41058e80bf05SSteven Rostedt (Red Hat)	    die "can't create $tmpdir";
4106a75fececSSteven Rostedt    }
4107a75fececSSteven Rostedt
4108e48c5293SSteven Rostedt    $ENV{"SSH_USER"} = $ssh_user;
4109e48c5293SSteven Rostedt    $ENV{"MACHINE"} = $machine;
4110e48c5293SSteven Rostedt
4111a75fececSSteven Rostedt    $buildlog = "$tmpdir/buildlog-$machine";
4112a9dd5d63SRabin Vincent    $testlog = "$tmpdir/testlog-$machine";
4113a75fececSSteven Rostedt    $dmesg = "$tmpdir/dmesg-$machine";
411451ad1dd1SSteven Rostedt    $output_config = "$outputdir/.config";
4115a75fececSSteven Rostedt
4116bb8474b1SSteven Rostedt    if (!$buildonly) {
4117bb8474b1SSteven Rostedt	$target = "$ssh_user\@$machine";
4118a75fececSSteven Rostedt	if ($reboot_type eq "grub") {
4119576f627cSSteven Rostedt	    dodie "GRUB_MENU not defined" if (!defined($grub_menu));
4120a15ba913SSteven Rostedt	} elsif ($reboot_type eq "grub2") {
4121a15ba913SSteven Rostedt	    dodie "GRUB_MENU not defined" if (!defined($grub_menu));
4122a15ba913SSteven Rostedt	    dodie "GRUB_FILE not defined" if (!defined($grub_file));
41237786954cSSteven Rostedt	} elsif ($reboot_type eq "syslinux") {
41247786954cSSteven Rostedt	    dodie "SYSLINUX_LABEL not defined" if (!defined($syslinux_label));
4125a75fececSSteven Rostedt	}
4126bb8474b1SSteven Rostedt    }
4127a75fececSSteven Rostedt
4128a75fececSSteven Rostedt    my $run_type = $build_type;
4129a75fececSSteven Rostedt    if ($test_type eq "patchcheck") {
4130b5f4aea6SSteven Rostedt	$run_type = $patchcheck_type;
4131a75fececSSteven Rostedt    } elsif ($test_type eq "bisect") {
4132b5f4aea6SSteven Rostedt	$run_type = $bisect_type;
41330a05c769SSteven Rostedt    } elsif ($test_type eq "config_bisect") {
4134b5f4aea6SSteven Rostedt	$run_type = $config_bisect_type;
41354283b169SSteven Rostedt (Red Hat)    } elsif ($test_type eq "make_min_config") {
41364283b169SSteven Rostedt (Red Hat)	$run_type = "";
41374283b169SSteven Rostedt (Red Hat)    } elsif ($test_type eq "make_warnings_file") {
41384c4ab120SSteven Rostedt	$run_type = "";
41394c4ab120SSteven Rostedt    }
41404c4ab120SSteven Rostedt
4141a75fececSSteven Rostedt    # mistake in config file?
4142a75fececSSteven Rostedt    if (!defined($run_type)) {
4143a75fececSSteven Rostedt	$run_type = "ERROR";
4144a75fececSSteven Rostedt    }
41452545eb61SSteven Rostedt
4146e0a8742eSSteven Rostedt    my $installme = "";
4147e0a8742eSSteven Rostedt    $installme = " no_install" if ($no_install);
4148e0a8742eSSteven Rostedt
414918656c70SSteven Rostedt (Red Hat)    my $name = "";
415018656c70SSteven Rostedt (Red Hat)
415118656c70SSteven Rostedt (Red Hat)    if (defined($test_name)) {
415218656c70SSteven Rostedt (Red Hat)	$name = " ($test_name)";
415318656c70SSteven Rostedt (Red Hat)    }
415418656c70SSteven Rostedt (Red Hat)
41552545eb61SSteven Rostedt    doprint "\n\n";
415618656c70SSteven Rostedt (Red Hat)    doprint "RUNNING TEST $i of $opt{NUM_TESTS}$name with option $test_type $run_type$installme\n\n";
41577faafbd6SSteven Rostedt
4158921ed4c7SSteven Rostedt    if (defined($pre_test)) {
4159921ed4c7SSteven Rostedt	run_command $pre_test;
4160921ed4c7SSteven Rostedt    }
4161921ed4c7SSteven Rostedt
41627faafbd6SSteven Rostedt    unlink $dmesg;
41637faafbd6SSteven Rostedt    unlink $buildlog;
4164a9dd5d63SRabin Vincent    unlink $testlog;
41652545eb61SSteven Rostedt
4166250bae8bSSteven Rostedt    if (defined($addconfig)) {
4167250bae8bSSteven Rostedt	my $min = $minconfig;
41682b7d9b21SSteven Rostedt	if (!defined($minconfig)) {
4169250bae8bSSteven Rostedt	    $min = "";
4170250bae8bSSteven Rostedt	}
4171250bae8bSSteven Rostedt	run_command "cat $addconfig $min > $tmpdir/add_config" or
41722b7d9b21SSteven Rostedt	    dodie "Failed to create temp config";
41739be2e6b5SSteven Rostedt	$minconfig = "$tmpdir/add_config";
41742b7d9b21SSteven Rostedt    }
41752b7d9b21SSteven Rostedt
41766c5ee0beSSteven Rostedt    if (defined($checkout)) {
41776c5ee0beSSteven Rostedt	run_command "git checkout $checkout" or
41786c5ee0beSSteven Rostedt	    die "failed to checkout $checkout";
41796c5ee0beSSteven Rostedt    }
41806c5ee0beSSteven Rostedt
4181759a3cc6SSteven Rostedt    $no_reboot = 0;
4182759a3cc6SSteven Rostedt
4183648a182cSSteven Rostedt    # A test may opt to not reboot the box
4184648a182cSSteven Rostedt    if ($reboot_on_success) {
4185759a3cc6SSteven Rostedt	$reboot_success = 1;
4186648a182cSSteven Rostedt    }
41874ab1cce5SSteven Rostedt
4188a75fececSSteven Rostedt    if ($test_type eq "bisect") {
41895f9b6cedSSteven Rostedt	bisect $i;
41905f9b6cedSSteven Rostedt	next;
41910a05c769SSteven Rostedt    } elsif ($test_type eq "config_bisect") {
41920a05c769SSteven Rostedt	config_bisect $i;
41930a05c769SSteven Rostedt	next;
4194a75fececSSteven Rostedt    } elsif ($test_type eq "patchcheck") {
41956c5ee0beSSteven Rostedt	patchcheck $i;
41966c5ee0beSSteven Rostedt	next;
41974c4ab120SSteven Rostedt    } elsif ($test_type eq "make_min_config") {
41984c4ab120SSteven Rostedt	make_min_config $i;
41994c4ab120SSteven Rostedt	next;
42004283b169SSteven Rostedt (Red Hat)    } elsif ($test_type eq "make_warnings_file") {
42014283b169SSteven Rostedt (Red Hat)	$no_reboot = 1;
42024283b169SSteven Rostedt (Red Hat)	make_warnings_file $i;
42034283b169SSteven Rostedt (Red Hat)	next;
42045f9b6cedSSteven Rostedt    }
42055f9b6cedSSteven Rostedt
42067faafbd6SSteven Rostedt    if ($build_type ne "nobuild") {
42077faafbd6SSteven Rostedt	build $build_type or next;
42084283b169SSteven Rostedt (Red Hat)	check_buildlog or next;
42092545eb61SSteven Rostedt    }
42102545eb61SSteven Rostedt
4211cd8e368fSSteven Rostedt    if ($test_type eq "install") {
4212cd8e368fSSteven Rostedt	get_version;
4213cd8e368fSSteven Rostedt	install;
4214cd8e368fSSteven Rostedt	success $i;
4215cd8e368fSSteven Rostedt	next;
4216cd8e368fSSteven Rostedt    }
4217cd8e368fSSteven Rostedt
4218a75fececSSteven Rostedt    if ($test_type ne "build") {
42197faafbd6SSteven Rostedt	my $failed = 0;
422064d98283SSteven Rostedt (Red Hat)	start_monitor_and_install or $failed = 1;
4221a75fececSSteven Rostedt
4222a75fececSSteven Rostedt	if (!$failed && $test_type ne "boot" && defined($run_test)) {
42237faafbd6SSteven Rostedt	    do_run_test or $failed = 1;
42245a391fbfSSteven Rostedt	}
42257faafbd6SSteven Rostedt	end_monitor;
422638fa3dc1SSteven Rostedt (Red Hat)	if ($failed) {
422738fa3dc1SSteven Rostedt (Red Hat)	    print_times;
422838fa3dc1SSteven Rostedt (Red Hat)	    next;
4229a75fececSSteven Rostedt	}
423038fa3dc1SSteven Rostedt (Red Hat)    }
423138fa3dc1SSteven Rostedt (Red Hat)
423238fa3dc1SSteven Rostedt (Red Hat)    print_times;
42335a391fbfSSteven Rostedt
42345f9b6cedSSteven Rostedt    success $i;
423575c3fda7SSteven Rostedt}
42362545eb61SSteven Rostedt
4237921ed4c7SSteven Rostedtif (defined($final_post_ktest)) {
4238921ed4c7SSteven Rostedt    run_command $final_post_ktest;
4239921ed4c7SSteven Rostedt}
4240921ed4c7SSteven Rostedt
42415c42fc5bSSteven Rostedtif ($opt{"POWEROFF_ON_SUCCESS"}) {
424275c3fda7SSteven Rostedt    halt;
4243759a3cc6SSteven Rostedt} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
4244bc7c5803SSteven Rostedt    reboot_to_good;
4245648a182cSSteven Rostedt} elsif (defined($switch_to_good)) {
4246648a182cSSteven Rostedt    # still need to get to the good kernel
4247648a182cSSteven Rostedt    run_command $switch_to_good;
42485c42fc5bSSteven Rostedt}
424975c3fda7SSteven Rostedt
4250648a182cSSteven Rostedt
4251e48c5293SSteven Rostedtdoprint "\n    $successes of $opt{NUM_TESTS} tests were successful\n\n";
4252e48c5293SSteven Rostedt
42532545eb61SSteven Rostedtexit 0;
4254