xref: /openbmc/linux/tools/testing/ktest/ktest.pl (revision fee9d3e61d04422628a3d22ed5eb8370dcef259b)
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",
62600bbf0aSSteven Rostedt
63600bbf0aSSteven Rostedt# required, and we will ask users if they don't have them but we keep the default
64600bbf0aSSteven Rostedt# value something that is common.
654f43e0dcSSteven Rostedt    "REBOOT_TYPE"		=> "grub",
664f43e0dcSSteven Rostedt    "LOCALVERSION"		=> "-test",
674f43e0dcSSteven Rostedt    "SSH_USER"			=> "root",
684f43e0dcSSteven Rostedt    "BUILD_TARGET"	 	=> "arch/x86/boot/bzImage",
694f43e0dcSSteven Rostedt    "TARGET_IMAGE"		=> "/boot/vmlinuz-test",
709cc9e091SSteven Rostedt
719cc9e091SSteven Rostedt    "LOG_FILE"			=> undef,
729cc9e091SSteven Rostedt    "IGNORE_UNUSED"		=> 0,
734f43e0dcSSteven Rostedt);
742545eb61SSteven Rostedt
755269faadSSatoru Takeuchimy $ktest_config = "ktest.conf";
762545eb61SSteven Rostedtmy $version;
77683a3e64SSteven Rostedtmy $have_version = 0;
78a75fececSSteven Rostedtmy $machine;
79df5f7c66SSteven Rostedt (Red Hat)my $last_machine;
80e48c5293SSteven Rostedtmy $ssh_user;
81a75fececSSteven Rostedtmy $tmpdir;
82a75fececSSteven Rostedtmy $builddir;
83a75fececSSteven Rostedtmy $outputdir;
8451ad1dd1SSteven Rostedtmy $output_config;
85a75fececSSteven Rostedtmy $test_type;
867faafbd6SSteven Rostedtmy $build_type;
87a75fececSSteven Rostedtmy $build_options;
88921ed4c7SSteven Rostedtmy $final_post_ktest;
89921ed4c7SSteven Rostedtmy $pre_ktest;
90921ed4c7SSteven Rostedtmy $post_ktest;
91921ed4c7SSteven Rostedtmy $pre_test;
92921ed4c7SSteven Rostedtmy $post_test;
930bd6c1a3SSteven Rostedtmy $pre_build;
940bd6c1a3SSteven Rostedtmy $post_build;
950bd6c1a3SSteven Rostedtmy $pre_build_die;
960bd6c1a3SSteven Rostedtmy $post_build_die;
97a75fececSSteven Rostedtmy $reboot_type;
98a75fececSSteven Rostedtmy $reboot_script;
99a75fececSSteven Rostedtmy $power_cycle;
100e48c5293SSteven Rostedtmy $reboot;
101a75fececSSteven Rostedtmy $reboot_on_error;
102bc7c5803SSteven Rostedtmy $switch_to_good;
103bc7c5803SSteven Rostedtmy $switch_to_test;
104a75fececSSteven Rostedtmy $poweroff_on_error;
105648a182cSSteven Rostedtmy $reboot_on_success;
106a75fececSSteven Rostedtmy $die_on_failure;
107576f627cSSteven Rostedtmy $powercycle_after_reboot;
108576f627cSSteven Rostedtmy $poweroff_after_halt;
109407b95b7SSteven Rostedtmy $max_monitor_wait;
110e48c5293SSteven Rostedtmy $ssh_exec;
111e48c5293SSteven Rostedtmy $scp_to_target;
11202ad2617SSteven Rostedtmy $scp_to_target_install;
113a75fececSSteven Rostedtmy $power_off;
114a75fececSSteven Rostedtmy $grub_menu;
115752d9665SSteven Rostedt (Red Hat)my $last_grub_menu;
116a15ba913SSteven Rostedtmy $grub_file;
1172545eb61SSteven Rostedtmy $grub_number;
118a15ba913SSteven Rostedtmy $grub_reboot;
1197786954cSSteven Rostedtmy $syslinux;
1207786954cSSteven Rostedtmy $syslinux_path;
1217786954cSSteven Rostedtmy $syslinux_label;
1222545eb61SSteven Rostedtmy $target;
1232545eb61SSteven Rostedtmy $make;
124e5c2ec11SSteven Rostedtmy $pre_install;
1258b37ca8cSSteven Rostedtmy $post_install;
126e0a8742eSSteven Rostedtmy $no_install;
1275c42fc5bSSteven Rostedtmy $noclean;
1285f9b6cedSSteven Rostedtmy $minconfig;
1294c4ab120SSteven Rostedtmy $start_minconfig;
13035ce5952SSteven Rostedtmy $start_minconfig_defined;
1314c4ab120SSteven Rostedtmy $output_minconfig;
132ccc513b6SSteven Rostedtmy $minconfig_type;
13343de3316SSteven Rostedtmy $use_output_minconfig;
1344283b169SSteven Rostedt (Red Hat)my $warnings_file;
1354c4ab120SSteven Rostedtmy $ignore_config;
136be405f95SSteven Rostedtmy $ignore_errors;
1372b7d9b21SSteven Rostedtmy $addconfig;
1385f9b6cedSSteven Rostedtmy $in_bisect = 0;
139b5f4aea6SSteven Rostedtmy $bisect_bad_commit = "";
140d6ce2a0bSSteven Rostedtmy $reverse_bisect;
141c960bb9fSSteven Rostedtmy $bisect_manual;
142c23dca7cSSteven Rostedtmy $bisect_skip;
143961d9cacSSteven Rostedt (Red Hat)my $bisect_tries;
14430f75da5SSteven Rostedtmy $config_bisect_good;
145c5dacb88SSteven Rostedtmy $bisect_ret_good;
146c5dacb88SSteven Rostedtmy $bisect_ret_bad;
147c5dacb88SSteven Rostedtmy $bisect_ret_skip;
148c5dacb88SSteven Rostedtmy $bisect_ret_abort;
149c5dacb88SSteven Rostedtmy $bisect_ret_default;
1506c5ee0beSSteven Rostedtmy $in_patchcheck = 0;
1515a391fbfSSteven Rostedtmy $run_test;
1527faafbd6SSteven Rostedtmy $buildlog;
153a9dd5d63SRabin Vincentmy $testlog;
1547faafbd6SSteven Rostedtmy $dmesg;
1557faafbd6SSteven Rostedtmy $monitor_fp;
1567faafbd6SSteven Rostedtmy $monitor_pid;
1577faafbd6SSteven Rostedtmy $monitor_cnt = 0;
158a75fececSSteven Rostedtmy $sleep_time;
159a75fececSSteven Rostedtmy $bisect_sleep_time;
16027d934b2SSteven Rostedtmy $patchcheck_sleep_time;
1611990207dSSteven Rostedtmy $ignore_warnings;
162a75fececSSteven Rostedtmy $store_failures;
163de5b6e3bSRabin Vincentmy $store_successes;
1649064af52SSteven Rostedtmy $test_name;
165a75fececSSteven Rostedtmy $timeout;
166a75fececSSteven Rostedtmy $booted_timeout;
167f1a5b962SSteven Rostedtmy $detect_triplefault;
168a75fececSSteven Rostedtmy $console;
1695a5d8e48SSatoru Takeuchimy $close_console_signal;
1702b803365SSteven Rostedtmy $reboot_success_line;
171a75fececSSteven Rostedtmy $success_line;
1721c8a617aSSteven Rostedtmy $stop_after_success;
1731c8a617aSSteven Rostedtmy $stop_after_failure;
1742d01b26aSSteven Rostedtmy $stop_test_after;
175a75fececSSteven Rostedtmy $build_target;
176a75fececSSteven Rostedtmy $target_image;
177b5f4aea6SSteven Rostedtmy $checkout;
178a75fececSSteven Rostedtmy $localversion;
179576f627cSSteven Rostedtmy $iteration = 0;
180e48c5293SSteven Rostedtmy $successes = 0;
1812545eb61SSteven Rostedt
182b5f4aea6SSteven Rostedtmy $bisect_good;
183b5f4aea6SSteven Rostedtmy $bisect_bad;
184b5f4aea6SSteven Rostedtmy $bisect_type;
185b5f4aea6SSteven Rostedtmy $bisect_start;
186b5f4aea6SSteven Rostedtmy $bisect_replay;
187b5f4aea6SSteven Rostedtmy $bisect_files;
188b5f4aea6SSteven Rostedtmy $bisect_reverse;
189b5f4aea6SSteven Rostedtmy $bisect_check;
190b5f4aea6SSteven Rostedt
191b5f4aea6SSteven Rostedtmy $config_bisect;
192b5f4aea6SSteven Rostedtmy $config_bisect_type;
193b0918612SSteven Rostedtmy $config_bisect_check;
194b5f4aea6SSteven Rostedt
195b5f4aea6SSteven Rostedtmy $patchcheck_type;
196b5f4aea6SSteven Rostedtmy $patchcheck_start;
19723a0e161SSteven Rostedt (Red Hat)my $patchcheck_cherry;
198b5f4aea6SSteven Rostedtmy $patchcheck_end;
199b5f4aea6SSteven Rostedt
200165708b2SSteven Rostedt# set when a test is something other that just building or install
201bb8474b1SSteven Rostedt# which would require more options.
202bb8474b1SSteven Rostedtmy $buildonly = 1;
203bb8474b1SSteven Rostedt
2044283b169SSteven Rostedt (Red Hat)# tell build not to worry about warnings, even when WARNINGS_FILE is set
2054283b169SSteven Rostedt (Red Hat)my $warnings_ok = 0;
2064283b169SSteven Rostedt (Red Hat)
207dbd3783bSSteven Rostedt# set when creating a new config
208dbd3783bSSteven Rostedtmy $newconfig = 0;
209dbd3783bSSteven Rostedt
2108d1491baSSteven Rostedtmy %entered_configs;
2118d1491baSSteven Rostedtmy %config_help;
21277d942ceSSteven Rostedtmy %variable;
213cf79fab6SSteven Rostedt
214cf79fab6SSteven Rostedt# force_config is the list of configs that we force enabled (or disabled)
215cf79fab6SSteven Rostedt# in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
216fcb3f16aSSteven Rostedtmy %force_config;
2178d1491baSSteven Rostedt
2184ab1cce5SSteven Rostedt# do not force reboots on config problems
2194ab1cce5SSteven Rostedtmy $no_reboot = 1;
2204ab1cce5SSteven Rostedt
221759a3cc6SSteven Rostedt# reboot on success
222759a3cc6SSteven Rostedtmy $reboot_success = 0;
223759a3cc6SSteven Rostedt
2249cc9e091SSteven Rostedtmy %option_map = (
2259cc9e091SSteven Rostedt    "MACHINE"			=> \$machine,
2269cc9e091SSteven Rostedt    "SSH_USER"			=> \$ssh_user,
2279cc9e091SSteven Rostedt    "TMP_DIR"			=> \$tmpdir,
2289cc9e091SSteven Rostedt    "OUTPUT_DIR"		=> \$outputdir,
2299cc9e091SSteven Rostedt    "BUILD_DIR"			=> \$builddir,
2309cc9e091SSteven Rostedt    "TEST_TYPE"			=> \$test_type,
231921ed4c7SSteven Rostedt    "PRE_KTEST"			=> \$pre_ktest,
232921ed4c7SSteven Rostedt    "POST_KTEST"		=> \$post_ktest,
233921ed4c7SSteven Rostedt    "PRE_TEST"			=> \$pre_test,
234921ed4c7SSteven Rostedt    "POST_TEST"			=> \$post_test,
2359cc9e091SSteven Rostedt    "BUILD_TYPE"		=> \$build_type,
2369cc9e091SSteven Rostedt    "BUILD_OPTIONS"		=> \$build_options,
2379cc9e091SSteven Rostedt    "PRE_BUILD"			=> \$pre_build,
2389cc9e091SSteven Rostedt    "POST_BUILD"		=> \$post_build,
2399cc9e091SSteven Rostedt    "PRE_BUILD_DIE"		=> \$pre_build_die,
2409cc9e091SSteven Rostedt    "POST_BUILD_DIE"		=> \$post_build_die,
2419cc9e091SSteven Rostedt    "POWER_CYCLE"		=> \$power_cycle,
2429cc9e091SSteven Rostedt    "REBOOT"			=> \$reboot,
2439cc9e091SSteven Rostedt    "BUILD_NOCLEAN"		=> \$noclean,
2449cc9e091SSteven Rostedt    "MIN_CONFIG"		=> \$minconfig,
2459cc9e091SSteven Rostedt    "OUTPUT_MIN_CONFIG"		=> \$output_minconfig,
2469cc9e091SSteven Rostedt    "START_MIN_CONFIG"		=> \$start_minconfig,
247ccc513b6SSteven Rostedt    "MIN_CONFIG_TYPE"		=> \$minconfig_type,
24843de3316SSteven Rostedt    "USE_OUTPUT_MIN_CONFIG"	=> \$use_output_minconfig,
2494283b169SSteven Rostedt (Red Hat)    "WARNINGS_FILE"		=> \$warnings_file,
2509cc9e091SSteven Rostedt    "IGNORE_CONFIG"		=> \$ignore_config,
2519cc9e091SSteven Rostedt    "TEST"			=> \$run_test,
2529cc9e091SSteven Rostedt    "ADD_CONFIG"		=> \$addconfig,
2539cc9e091SSteven Rostedt    "REBOOT_TYPE"		=> \$reboot_type,
2549cc9e091SSteven Rostedt    "GRUB_MENU"			=> \$grub_menu,
255a15ba913SSteven Rostedt    "GRUB_FILE"			=> \$grub_file,
256a15ba913SSteven Rostedt    "GRUB_REBOOT"		=> \$grub_reboot,
2577786954cSSteven Rostedt    "SYSLINUX"			=> \$syslinux,
2587786954cSSteven Rostedt    "SYSLINUX_PATH"		=> \$syslinux_path,
2597786954cSSteven Rostedt    "SYSLINUX_LABEL"		=> \$syslinux_label,
260e5c2ec11SSteven Rostedt    "PRE_INSTALL"		=> \$pre_install,
2619cc9e091SSteven Rostedt    "POST_INSTALL"		=> \$post_install,
2629cc9e091SSteven Rostedt    "NO_INSTALL"		=> \$no_install,
2639cc9e091SSteven Rostedt    "REBOOT_SCRIPT"		=> \$reboot_script,
2649cc9e091SSteven Rostedt    "REBOOT_ON_ERROR"		=> \$reboot_on_error,
2659cc9e091SSteven Rostedt    "SWITCH_TO_GOOD"		=> \$switch_to_good,
2669cc9e091SSteven Rostedt    "SWITCH_TO_TEST"		=> \$switch_to_test,
2679cc9e091SSteven Rostedt    "POWEROFF_ON_ERROR"		=> \$poweroff_on_error,
268648a182cSSteven Rostedt    "REBOOT_ON_SUCCESS"		=> \$reboot_on_success,
2699cc9e091SSteven Rostedt    "DIE_ON_FAILURE"		=> \$die_on_failure,
2709cc9e091SSteven Rostedt    "POWER_OFF"			=> \$power_off,
2719cc9e091SSteven Rostedt    "POWERCYCLE_AFTER_REBOOT"	=> \$powercycle_after_reboot,
2729cc9e091SSteven Rostedt    "POWEROFF_AFTER_HALT"	=> \$poweroff_after_halt,
273407b95b7SSteven Rostedt    "MAX_MONITOR_WAIT"		=> \$max_monitor_wait,
2749cc9e091SSteven Rostedt    "SLEEP_TIME"		=> \$sleep_time,
2759cc9e091SSteven Rostedt    "BISECT_SLEEP_TIME"		=> \$bisect_sleep_time,
2769cc9e091SSteven Rostedt    "PATCHCHECK_SLEEP_TIME"	=> \$patchcheck_sleep_time,
2779cc9e091SSteven Rostedt    "IGNORE_WARNINGS"		=> \$ignore_warnings,
278be405f95SSteven Rostedt    "IGNORE_ERRORS"		=> \$ignore_errors,
2799cc9e091SSteven Rostedt    "BISECT_MANUAL"		=> \$bisect_manual,
2809cc9e091SSteven Rostedt    "BISECT_SKIP"		=> \$bisect_skip,
281961d9cacSSteven Rostedt (Red Hat)    "BISECT_TRIES"		=> \$bisect_tries,
2829cc9e091SSteven Rostedt    "CONFIG_BISECT_GOOD"	=> \$config_bisect_good,
2839cc9e091SSteven Rostedt    "BISECT_RET_GOOD"		=> \$bisect_ret_good,
2849cc9e091SSteven Rostedt    "BISECT_RET_BAD"		=> \$bisect_ret_bad,
2859cc9e091SSteven Rostedt    "BISECT_RET_SKIP"		=> \$bisect_ret_skip,
2869cc9e091SSteven Rostedt    "BISECT_RET_ABORT"		=> \$bisect_ret_abort,
2879cc9e091SSteven Rostedt    "BISECT_RET_DEFAULT"	=> \$bisect_ret_default,
2889cc9e091SSteven Rostedt    "STORE_FAILURES"		=> \$store_failures,
2899cc9e091SSteven Rostedt    "STORE_SUCCESSES"		=> \$store_successes,
2909cc9e091SSteven Rostedt    "TEST_NAME"			=> \$test_name,
2919cc9e091SSteven Rostedt    "TIMEOUT"			=> \$timeout,
2929cc9e091SSteven Rostedt    "BOOTED_TIMEOUT"		=> \$booted_timeout,
2939cc9e091SSteven Rostedt    "CONSOLE"			=> \$console,
2945a5d8e48SSatoru Takeuchi    "CLOSE_CONSOLE_SIGNAL"	=> \$close_console_signal,
2959cc9e091SSteven Rostedt    "DETECT_TRIPLE_FAULT"	=> \$detect_triplefault,
2969cc9e091SSteven Rostedt    "SUCCESS_LINE"		=> \$success_line,
2979cc9e091SSteven Rostedt    "REBOOT_SUCCESS_LINE"	=> \$reboot_success_line,
2989cc9e091SSteven Rostedt    "STOP_AFTER_SUCCESS"	=> \$stop_after_success,
2999cc9e091SSteven Rostedt    "STOP_AFTER_FAILURE"	=> \$stop_after_failure,
3009cc9e091SSteven Rostedt    "STOP_TEST_AFTER"		=> \$stop_test_after,
3019cc9e091SSteven Rostedt    "BUILD_TARGET"		=> \$build_target,
3029cc9e091SSteven Rostedt    "SSH_EXEC"			=> \$ssh_exec,
3039cc9e091SSteven Rostedt    "SCP_TO_TARGET"		=> \$scp_to_target,
30402ad2617SSteven Rostedt    "SCP_TO_TARGET_INSTALL"	=> \$scp_to_target_install,
3059cc9e091SSteven Rostedt    "CHECKOUT"			=> \$checkout,
3069cc9e091SSteven Rostedt    "TARGET_IMAGE"		=> \$target_image,
3079cc9e091SSteven Rostedt    "LOCALVERSION"		=> \$localversion,
3089cc9e091SSteven Rostedt
3099cc9e091SSteven Rostedt    "BISECT_GOOD"		=> \$bisect_good,
3109cc9e091SSteven Rostedt    "BISECT_BAD"		=> \$bisect_bad,
3119cc9e091SSteven Rostedt    "BISECT_TYPE"		=> \$bisect_type,
3129cc9e091SSteven Rostedt    "BISECT_START"		=> \$bisect_start,
3139cc9e091SSteven Rostedt    "BISECT_REPLAY"		=> \$bisect_replay,
3149cc9e091SSteven Rostedt    "BISECT_FILES"		=> \$bisect_files,
3159cc9e091SSteven Rostedt    "BISECT_REVERSE"		=> \$bisect_reverse,
3169cc9e091SSteven Rostedt    "BISECT_CHECK"		=> \$bisect_check,
3179cc9e091SSteven Rostedt
3189cc9e091SSteven Rostedt    "CONFIG_BISECT"		=> \$config_bisect,
3199cc9e091SSteven Rostedt    "CONFIG_BISECT_TYPE"	=> \$config_bisect_type,
320b0918612SSteven Rostedt    "CONFIG_BISECT_CHECK"	=> \$config_bisect_check,
3219cc9e091SSteven Rostedt
3229cc9e091SSteven Rostedt    "PATCHCHECK_TYPE"		=> \$patchcheck_type,
3239cc9e091SSteven Rostedt    "PATCHCHECK_START"		=> \$patchcheck_start,
32423a0e161SSteven Rostedt (Red Hat)    "PATCHCHECK_CHERRY"		=> \$patchcheck_cherry,
3259cc9e091SSteven Rostedt    "PATCHCHECK_END"		=> \$patchcheck_end,
3269cc9e091SSteven Rostedt);
3279cc9e091SSteven Rostedt
3289cc9e091SSteven Rostedt# Options may be used by other options, record them.
3299cc9e091SSteven Rostedtmy %used_options;
3309cc9e091SSteven Rostedt
3317bf51073SSteven Rostedt# default variables that can be used
3327bf51073SSteven Rostedtchomp ($variable{"PWD"} = `pwd`);
3337bf51073SSteven Rostedt
3348d1491baSSteven Rostedt$config_help{"MACHINE"} = << "EOF"
3358d1491baSSteven Rostedt The machine hostname that you will test.
336bb8474b1SSteven Rostedt For build only tests, it is still needed to differentiate log files.
3378d1491baSSteven RostedtEOF
3388d1491baSSteven Rostedt    ;
3398d1491baSSteven Rostedt$config_help{"SSH_USER"} = << "EOF"
3408d1491baSSteven Rostedt The box is expected to have ssh on normal bootup, provide the user
3418d1491baSSteven Rostedt  (most likely root, since you need privileged operations)
3428d1491baSSteven RostedtEOF
3438d1491baSSteven Rostedt    ;
3448d1491baSSteven Rostedt$config_help{"BUILD_DIR"} = << "EOF"
3458d1491baSSteven Rostedt The directory that contains the Linux source code (full path).
3460e7a22deSSteven Rostedt You can use \${PWD} that will be the path where ktest.pl is run, or use
3470e7a22deSSteven Rostedt \${THIS_DIR} which is assigned \${PWD} but may be changed later.
3488d1491baSSteven RostedtEOF
3498d1491baSSteven Rostedt    ;
3508d1491baSSteven Rostedt$config_help{"OUTPUT_DIR"} = << "EOF"
3518d1491baSSteven Rostedt The directory that the objects will be built (full path).
3528d1491baSSteven Rostedt (can not be same as BUILD_DIR)
3530e7a22deSSteven Rostedt You can use \${PWD} that will be the path where ktest.pl is run, or use
3540e7a22deSSteven Rostedt \${THIS_DIR} which is assigned \${PWD} but may be changed later.
3558d1491baSSteven RostedtEOF
3568d1491baSSteven Rostedt    ;
3578d1491baSSteven Rostedt$config_help{"BUILD_TARGET"} = << "EOF"
3588d1491baSSteven Rostedt The location of the compiled file to copy to the target.
3598d1491baSSteven Rostedt (relative to OUTPUT_DIR)
3608d1491baSSteven RostedtEOF
3618d1491baSSteven Rostedt    ;
362dbd3783bSSteven Rostedt$config_help{"BUILD_OPTIONS"} = << "EOF"
363dbd3783bSSteven Rostedt Options to add to \"make\" when building.
364dbd3783bSSteven Rostedt i.e.  -j20
365dbd3783bSSteven RostedtEOF
366dbd3783bSSteven Rostedt    ;
3678d1491baSSteven Rostedt$config_help{"TARGET_IMAGE"} = << "EOF"
3688d1491baSSteven Rostedt The place to put your image on the test machine.
3698d1491baSSteven RostedtEOF
3708d1491baSSteven Rostedt    ;
3718d1491baSSteven Rostedt$config_help{"POWER_CYCLE"} = << "EOF"
3728d1491baSSteven Rostedt A script or command to reboot the box.
3738d1491baSSteven Rostedt
3748d1491baSSteven Rostedt Here is a digital loggers power switch example
3758d1491baSSteven Rostedt POWER_CYCLE = wget --no-proxy -O /dev/null -q  --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
3768d1491baSSteven Rostedt
3778d1491baSSteven Rostedt Here is an example to reboot a virtual box on the current host
3788d1491baSSteven Rostedt with the name "Guest".
3798d1491baSSteven Rostedt POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
3808d1491baSSteven RostedtEOF
3818d1491baSSteven Rostedt    ;
3828d1491baSSteven Rostedt$config_help{"CONSOLE"} = << "EOF"
3838d1491baSSteven Rostedt The script or command that reads the console
3848d1491baSSteven Rostedt
3858d1491baSSteven Rostedt  If you use ttywatch server, something like the following would work.
3868d1491baSSteven RostedtCONSOLE = nc -d localhost 3001
3878d1491baSSteven Rostedt
3888d1491baSSteven Rostedt For a virtual machine with guest name "Guest".
3898d1491baSSteven RostedtCONSOLE =  virsh console Guest
3908d1491baSSteven RostedtEOF
3918d1491baSSteven Rostedt    ;
3928d1491baSSteven Rostedt$config_help{"LOCALVERSION"} = << "EOF"
3938d1491baSSteven Rostedt Required version ending to differentiate the test
3948d1491baSSteven Rostedt from other linux builds on the system.
3958d1491baSSteven RostedtEOF
3968d1491baSSteven Rostedt    ;
3978d1491baSSteven Rostedt$config_help{"REBOOT_TYPE"} = << "EOF"
3988d1491baSSteven Rostedt Way to reboot the box to the test kernel.
3997786954cSSteven Rostedt Only valid options so far are "grub", "grub2", "syslinux", and "script".
4008d1491baSSteven Rostedt
4018d1491baSSteven Rostedt If you specify grub, it will assume grub version 1
4028d1491baSSteven Rostedt and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
4038d1491baSSteven Rostedt and select that target to reboot to the kernel. If this is not
4048d1491baSSteven Rostedt your setup, then specify "script" and have a command or script
4058d1491baSSteven Rostedt specified in REBOOT_SCRIPT to boot to the target.
4068d1491baSSteven Rostedt
4078d1491baSSteven Rostedt The entry in /boot/grub/menu.lst must be entered in manually.
4088d1491baSSteven Rostedt The test will not modify that file.
409a15ba913SSteven Rostedt
410a15ba913SSteven Rostedt If you specify grub2, then you also need to specify both \$GRUB_MENU
411a15ba913SSteven Rostedt and \$GRUB_FILE.
4127786954cSSteven Rostedt
4137786954cSSteven Rostedt If you specify syslinux, then you may use SYSLINUX to define the syslinux
4147786954cSSteven Rostedt command (defaults to extlinux), and SYSLINUX_PATH to specify the path to
4157786954cSSteven Rostedt the syslinux install (defaults to /boot/extlinux). But you have to specify
4167786954cSSteven Rostedt SYSLINUX_LABEL to define the label to boot to for the test kernel.
4178d1491baSSteven RostedtEOF
4188d1491baSSteven Rostedt    ;
4198d1491baSSteven Rostedt$config_help{"GRUB_MENU"} = << "EOF"
4208d1491baSSteven Rostedt The grub title name for the test kernel to boot
421a15ba913SSteven Rostedt (Only mandatory if REBOOT_TYPE = grub or grub2)
4228d1491baSSteven Rostedt
4238d1491baSSteven Rostedt Note, ktest.pl will not update the grub menu.lst, you need to
4248d1491baSSteven Rostedt manually add an option for the test. ktest.pl will search
4258d1491baSSteven Rostedt the grub menu.lst for this option to find what kernel to
4268d1491baSSteven Rostedt reboot into.
4278d1491baSSteven Rostedt
4288d1491baSSteven Rostedt For example, if in the /boot/grub/menu.lst the test kernel title has:
4298d1491baSSteven Rostedt title Test Kernel
4308d1491baSSteven Rostedt kernel vmlinuz-test
4318d1491baSSteven Rostedt GRUB_MENU = Test Kernel
432a15ba913SSteven Rostedt
433a15ba913SSteven Rostedt For grub2, a search of \$GRUB_FILE is performed for the lines
434a15ba913SSteven Rostedt that begin with "menuentry". It will not detect submenus. The
435a15ba913SSteven Rostedt menu must be a non-nested menu. Add the quotes used in the menu
436a15ba913SSteven Rostedt to guarantee your selection, as the first menuentry with the content
437a15ba913SSteven Rostedt of \$GRUB_MENU that is found will be used.
438a15ba913SSteven RostedtEOF
439a15ba913SSteven Rostedt    ;
440a15ba913SSteven Rostedt$config_help{"GRUB_FILE"} = << "EOF"
441a15ba913SSteven Rostedt If grub2 is used, the full path for the grub.cfg file is placed
442a15ba913SSteven Rostedt here. Use something like /boot/grub2/grub.cfg to search.
4438d1491baSSteven RostedtEOF
4448d1491baSSteven Rostedt    ;
4457786954cSSteven Rostedt$config_help{"SYSLINUX_LABEL"} = << "EOF"
4467786954cSSteven Rostedt If syslinux is used, the label that boots the target kernel must
4477786954cSSteven Rostedt be specified with SYSLINUX_LABEL.
4487786954cSSteven RostedtEOF
4497786954cSSteven Rostedt    ;
4508d1491baSSteven Rostedt$config_help{"REBOOT_SCRIPT"} = << "EOF"
4518d1491baSSteven Rostedt A script to reboot the target into the test kernel
4528d1491baSSteven Rostedt (Only mandatory if REBOOT_TYPE = script)
4538d1491baSSteven RostedtEOF
4548d1491baSSteven Rostedt    ;
4558d1491baSSteven Rostedt
456c75d22d9SSteven Rostedt (Red Hat)sub _logit {
457c75d22d9SSteven Rostedt (Red Hat)    if (defined($opt{"LOG_FILE"})) {
458c75d22d9SSteven Rostedt (Red Hat)	open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
459c75d22d9SSteven Rostedt (Red Hat)	print OUT @_;
460c75d22d9SSteven Rostedt (Red Hat)	close(OUT);
461c75d22d9SSteven Rostedt (Red Hat)    }
462c75d22d9SSteven Rostedt (Red Hat)}
463c75d22d9SSteven Rostedt (Red Hat)
464c75d22d9SSteven Rostedt (Red Hat)sub logit {
465c75d22d9SSteven Rostedt (Red Hat)    if (defined($opt{"LOG_FILE"})) {
466c75d22d9SSteven Rostedt (Red Hat)	_logit @_;
467c75d22d9SSteven Rostedt (Red Hat)    } else {
468c75d22d9SSteven Rostedt (Red Hat)	print @_;
469c75d22d9SSteven Rostedt (Red Hat)    }
470c75d22d9SSteven Rostedt (Red Hat)}
471c75d22d9SSteven Rostedt (Red Hat)
472c75d22d9SSteven Rostedt (Red Hat)sub doprint {
473c75d22d9SSteven Rostedt (Red Hat)    print @_;
474c75d22d9SSteven Rostedt (Red Hat)    _logit @_;
475c75d22d9SSteven Rostedt (Red Hat)}
476c75d22d9SSteven Rostedt (Red Hat)
477dad98754SSteven Rostedtsub read_prompt {
478dad98754SSteven Rostedt    my ($cancel, $prompt) = @_;
47935ce5952SSteven Rostedt
48035ce5952SSteven Rostedt    my $ans;
48135ce5952SSteven Rostedt
48235ce5952SSteven Rostedt    for (;;) {
483dad98754SSteven Rostedt	if ($cancel) {
484dad98754SSteven Rostedt	    print "$prompt [y/n/C] ";
485dad98754SSteven Rostedt	} else {
48635ce5952SSteven Rostedt	    print "$prompt [Y/n] ";
487dad98754SSteven Rostedt	}
48835ce5952SSteven Rostedt	$ans = <STDIN>;
48935ce5952SSteven Rostedt	chomp $ans;
49035ce5952SSteven Rostedt	if ($ans =~ /^\s*$/) {
491dad98754SSteven Rostedt	    if ($cancel) {
492dad98754SSteven Rostedt		$ans = "c";
493dad98754SSteven Rostedt	    } else {
49435ce5952SSteven Rostedt		$ans = "y";
49535ce5952SSteven Rostedt	    }
496dad98754SSteven Rostedt	}
49735ce5952SSteven Rostedt	last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
498dad98754SSteven Rostedt	if ($cancel) {
499dad98754SSteven Rostedt	    last if ($ans =~ /^c$/i);
500dad98754SSteven Rostedt	    print "Please answer either 'y', 'n' or 'c'.\n";
501dad98754SSteven Rostedt	} else {
50235ce5952SSteven Rostedt	    print "Please answer either 'y' or 'n'.\n";
50335ce5952SSteven Rostedt	}
504dad98754SSteven Rostedt    }
505dad98754SSteven Rostedt    if ($ans =~ /^c/i) {
506dad98754SSteven Rostedt	exit;
507dad98754SSteven Rostedt    }
50835ce5952SSteven Rostedt    if ($ans !~ /^y$/i) {
50935ce5952SSteven Rostedt	return 0;
51035ce5952SSteven Rostedt    }
51135ce5952SSteven Rostedt    return 1;
51235ce5952SSteven Rostedt}
5138d1491baSSteven Rostedt
514dad98754SSteven Rostedtsub read_yn {
515dad98754SSteven Rostedt    my ($prompt) = @_;
516dad98754SSteven Rostedt
517dad98754SSteven Rostedt    return read_prompt 0, $prompt;
518dad98754SSteven Rostedt}
519dad98754SSteven Rostedt
520dad98754SSteven Rostedtsub read_ync {
521dad98754SSteven Rostedt    my ($prompt) = @_;
522dad98754SSteven Rostedt
523dad98754SSteven Rostedt    return read_prompt 1, $prompt;
524dad98754SSteven Rostedt}
525dad98754SSteven Rostedt
5265269faadSSatoru Takeuchisub get_mandatory_config {
5278d1491baSSteven Rostedt    my ($config) = @_;
528815e2bd7SSteven Rostedt    my $ans;
5298d1491baSSteven Rostedt
5308d1491baSSteven Rostedt    return if (defined($opt{$config}));
5318d1491baSSteven Rostedt
5328d1491baSSteven Rostedt    if (defined($config_help{$config})) {
5338d1491baSSteven Rostedt	print "\n";
5348d1491baSSteven Rostedt	print $config_help{$config};
5358d1491baSSteven Rostedt    }
5368d1491baSSteven Rostedt
5378d1491baSSteven Rostedt    for (;;) {
5388d1491baSSteven Rostedt	print "$config = ";
539dbd3783bSSteven Rostedt	if (defined($default{$config}) && length($default{$config})) {
5408d1491baSSteven Rostedt	    print "\[$default{$config}\] ";
5418d1491baSSteven Rostedt	}
542815e2bd7SSteven Rostedt	$ans = <STDIN>;
543815e2bd7SSteven Rostedt	$ans =~ s/^\s*(.*\S)\s*$/$1/;
544815e2bd7SSteven Rostedt	if ($ans =~ /^\s*$/) {
5458d1491baSSteven Rostedt	    if ($default{$config}) {
546815e2bd7SSteven Rostedt		$ans = $default{$config};
5478d1491baSSteven Rostedt	    } else {
5488d1491baSSteven Rostedt		print "Your answer can not be blank\n";
5498d1491baSSteven Rostedt		next;
5508d1491baSSteven Rostedt	    }
5518d1491baSSteven Rostedt	}
5520e7a22deSSteven Rostedt	$entered_configs{$config} = ${ans};
5538d1491baSSteven Rostedt	last;
5548d1491baSSteven Rostedt    }
5558d1491baSSteven Rostedt}
5568d1491baSSteven Rostedt
5575269faadSSatoru Takeuchisub get_mandatory_configs {
5585269faadSSatoru Takeuchi    get_mandatory_config("MACHINE");
5595269faadSSatoru Takeuchi    get_mandatory_config("BUILD_DIR");
5605269faadSSatoru Takeuchi    get_mandatory_config("OUTPUT_DIR");
561bb8474b1SSteven Rostedt
562dbd3783bSSteven Rostedt    if ($newconfig) {
5635269faadSSatoru Takeuchi	get_mandatory_config("BUILD_OPTIONS");
564dbd3783bSSteven Rostedt    }
565dbd3783bSSteven Rostedt
566bb8474b1SSteven Rostedt    # options required for other than just building a kernel
567bb8474b1SSteven Rostedt    if (!$buildonly) {
5685269faadSSatoru Takeuchi	get_mandatory_config("POWER_CYCLE");
5695269faadSSatoru Takeuchi	get_mandatory_config("CONSOLE");
570165708b2SSteven Rostedt    }
571165708b2SSteven Rostedt
572165708b2SSteven Rostedt    # options required for install and more
573165708b2SSteven Rostedt    if ($buildonly != 1) {
5745269faadSSatoru Takeuchi	get_mandatory_config("SSH_USER");
5755269faadSSatoru Takeuchi	get_mandatory_config("BUILD_TARGET");
5765269faadSSatoru Takeuchi	get_mandatory_config("TARGET_IMAGE");
577bb8474b1SSteven Rostedt    }
578bb8474b1SSteven Rostedt
5795269faadSSatoru Takeuchi    get_mandatory_config("LOCALVERSION");
5808d1491baSSteven Rostedt
581bb8474b1SSteven Rostedt    return if ($buildonly);
582bb8474b1SSteven Rostedt
5838d1491baSSteven Rostedt    my $rtype = $opt{"REBOOT_TYPE"};
5848d1491baSSteven Rostedt
5858d1491baSSteven Rostedt    if (!defined($rtype)) {
5868d1491baSSteven Rostedt	if (!defined($opt{"GRUB_MENU"})) {
5875269faadSSatoru Takeuchi	    get_mandatory_config("REBOOT_TYPE");
5888d1491baSSteven Rostedt	    $rtype = $entered_configs{"REBOOT_TYPE"};
5898d1491baSSteven Rostedt	} else {
5908d1491baSSteven Rostedt	    $rtype = "grub";
5918d1491baSSteven Rostedt	}
5928d1491baSSteven Rostedt    }
5938d1491baSSteven Rostedt
5948d1491baSSteven Rostedt    if ($rtype eq "grub") {
5955269faadSSatoru Takeuchi	get_mandatory_config("GRUB_MENU");
5968d1491baSSteven Rostedt    }
597a15ba913SSteven Rostedt
598a15ba913SSteven Rostedt    if ($rtype eq "grub2") {
5995269faadSSatoru Takeuchi	get_mandatory_config("GRUB_MENU");
6005269faadSSatoru Takeuchi	get_mandatory_config("GRUB_FILE");
601a15ba913SSteven Rostedt    }
6027786954cSSteven Rostedt
6037786954cSSteven Rostedt    if ($rtype eq "syslinux") {
6045269faadSSatoru Takeuchi	get_mandatory_config("SYSLINUX_LABEL");
6057786954cSSteven Rostedt    }
6068d1491baSSteven Rostedt}
6078d1491baSSteven Rostedt
60877d942ceSSteven Rostedtsub process_variables {
6098d735212SSteven Rostedt    my ($value, $remove_undef) = @_;
61077d942ceSSteven Rostedt    my $retval = "";
61177d942ceSSteven Rostedt
61277d942ceSSteven Rostedt    # We want to check for '\', and it is just easier
61377d942ceSSteven Rostedt    # to check the previous characet of '$' and not need
61477d942ceSSteven Rostedt    # to worry if '$' is the first character. By adding
61577d942ceSSteven Rostedt    # a space to $value, we can just check [^\\]\$ and
61677d942ceSSteven Rostedt    # it will still work.
61777d942ceSSteven Rostedt    $value = " $value";
61877d942ceSSteven Rostedt
61977d942ceSSteven Rostedt    while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
62077d942ceSSteven Rostedt	my $begin = $1;
62177d942ceSSteven Rostedt	my $var = $2;
62277d942ceSSteven Rostedt	my $end = $3;
62377d942ceSSteven Rostedt	# append beginning of value to retval
62477d942ceSSteven Rostedt	$retval = "$retval$begin";
62577d942ceSSteven Rostedt	if (defined($variable{$var})) {
62677d942ceSSteven Rostedt	    $retval = "$retval$variable{$var}";
6278d735212SSteven Rostedt	} elsif (defined($remove_undef) && $remove_undef) {
6288d735212SSteven Rostedt	    # for if statements, any variable that is not defined,
6298d735212SSteven Rostedt	    # we simple convert to 0
6308d735212SSteven Rostedt	    $retval = "${retval}0";
63177d942ceSSteven Rostedt	} else {
63277d942ceSSteven Rostedt	    # put back the origin piece.
63377d942ceSSteven Rostedt	    $retval = "$retval\$\{$var\}";
6349cc9e091SSteven Rostedt	    # This could be an option that is used later, save
6359cc9e091SSteven Rostedt	    # it so we don't warn if this option is not one of
6369cc9e091SSteven Rostedt	    # ktests options.
6379cc9e091SSteven Rostedt	    $used_options{$var} = 1;
63877d942ceSSteven Rostedt	}
63977d942ceSSteven Rostedt	$value = $end;
64077d942ceSSteven Rostedt    }
64177d942ceSSteven Rostedt    $retval = "$retval$value";
64277d942ceSSteven Rostedt
64377d942ceSSteven Rostedt    # remove the space added in the beginning
64477d942ceSSteven Rostedt    $retval =~ s/ //;
64577d942ceSSteven Rostedt
64677d942ceSSteven Rostedt    return "$retval"
64777d942ceSSteven Rostedt}
64877d942ceSSteven Rostedt
649a57419b3SSteven Rostedtsub set_value {
6503d1cc414SSteven Rostedt    my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
6512545eb61SSteven Rostedt
652cad96669SSteven Rostedt    my $prvalue = process_variables($rvalue);
653cad96669SSteven Rostedt
654cad96669SSteven Rostedt    if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
655bb8474b1SSteven Rostedt	# Note if a test is something other than build, then we
656bb8474b1SSteven Rostedt	# will need other manditory options.
657cad96669SSteven Rostedt	if ($prvalue ne "install") {
658319ab14fSSteven Rostedt (Red Hat)	    # for bisect, we need to check BISECT_TYPE
659319ab14fSSteven Rostedt (Red Hat)	    if ($prvalue ne "bisect") {
660319ab14fSSteven Rostedt (Red Hat)		$buildonly = 0;
661319ab14fSSteven Rostedt (Red Hat)	    }
662319ab14fSSteven Rostedt (Red Hat)	} else {
663319ab14fSSteven Rostedt (Red Hat)	    # install still limits some manditory options.
664319ab14fSSteven Rostedt (Red Hat)	    $buildonly = 2;
665319ab14fSSteven Rostedt (Red Hat)	}
666319ab14fSSteven Rostedt (Red Hat)    }
667319ab14fSSteven Rostedt (Red Hat)
668319ab14fSSteven Rostedt (Red Hat)    if ($buildonly && $lvalue =~ /^BISECT_TYPE(\[.*\])?$/ && $prvalue ne "build") {
669319ab14fSSteven Rostedt (Red Hat)	if ($prvalue ne "install") {
670bb8474b1SSteven Rostedt	    $buildonly = 0;
671165708b2SSteven Rostedt	} else {
672165708b2SSteven Rostedt	    # install still limits some manditory options.
673165708b2SSteven Rostedt	    $buildonly = 2;
674165708b2SSteven Rostedt	}
675bb8474b1SSteven Rostedt    }
676bb8474b1SSteven Rostedt
677a75fececSSteven Rostedt    if (defined($opt{$lvalue})) {
6783d1cc414SSteven Rostedt	if (!$override || defined(${$overrides}{$lvalue})) {
6793d1cc414SSteven Rostedt	    my $extra = "";
6803d1cc414SSteven Rostedt	    if ($override) {
6813d1cc414SSteven Rostedt		$extra = "In the same override section!\n";
6823d1cc414SSteven Rostedt	    }
6833d1cc414SSteven Rostedt	    die "$name: $.: Option $lvalue defined more than once!\n$extra";
6843d1cc414SSteven Rostedt	}
685cad96669SSteven Rostedt	${$overrides}{$lvalue} = $prvalue;
686a75fececSSteven Rostedt    }
68721a9679fSSteven Rostedt    if ($rvalue =~ /^\s*$/) {
68821a9679fSSteven Rostedt	delete $opt{$lvalue};
68921a9679fSSteven Rostedt    } else {
690cad96669SSteven Rostedt	$opt{$lvalue} = $prvalue;
69121a9679fSSteven Rostedt    }
6922545eb61SSteven Rostedt}
693a57419b3SSteven Rostedt
694c75d22d9SSteven Rostedt (Red Hat)sub set_eval {
695c75d22d9SSteven Rostedt (Red Hat)    my ($lvalue, $rvalue, $name) = @_;
696c75d22d9SSteven Rostedt (Red Hat)
697c75d22d9SSteven Rostedt (Red Hat)    my $prvalue = process_variables($rvalue);
698c75d22d9SSteven Rostedt (Red Hat)    my $arr;
699c75d22d9SSteven Rostedt (Red Hat)
700c75d22d9SSteven Rostedt (Red Hat)    if (defined($evals{$lvalue})) {
701c75d22d9SSteven Rostedt (Red Hat)	$arr = $evals{$lvalue};
702c75d22d9SSteven Rostedt (Red Hat)    } else {
703c75d22d9SSteven Rostedt (Red Hat)	$arr = [];
704c75d22d9SSteven Rostedt (Red Hat)	$evals{$lvalue} = $arr;
705c75d22d9SSteven Rostedt (Red Hat)    }
706c75d22d9SSteven Rostedt (Red Hat)
707c75d22d9SSteven Rostedt (Red Hat)    push @{$arr}, $rvalue;
708c75d22d9SSteven Rostedt (Red Hat)}
709c75d22d9SSteven Rostedt (Red Hat)
71077d942ceSSteven Rostedtsub set_variable {
71177d942ceSSteven Rostedt    my ($lvalue, $rvalue) = @_;
71277d942ceSSteven Rostedt
71377d942ceSSteven Rostedt    if ($rvalue =~ /^\s*$/) {
71477d942ceSSteven Rostedt	delete $variable{$lvalue};
71577d942ceSSteven Rostedt    } else {
71677d942ceSSteven Rostedt	$rvalue = process_variables($rvalue);
71777d942ceSSteven Rostedt	$variable{$lvalue} = $rvalue;
71877d942ceSSteven Rostedt    }
71977d942ceSSteven Rostedt}
72077d942ceSSteven Rostedt
721ab7a3f52SSteven Rostedtsub process_compare {
722ab7a3f52SSteven Rostedt    my ($lval, $cmp, $rval) = @_;
723ab7a3f52SSteven Rostedt
724ab7a3f52SSteven Rostedt    # remove whitespace
725ab7a3f52SSteven Rostedt
726ab7a3f52SSteven Rostedt    $lval =~ s/^\s*//;
727ab7a3f52SSteven Rostedt    $lval =~ s/\s*$//;
728ab7a3f52SSteven Rostedt
729ab7a3f52SSteven Rostedt    $rval =~ s/^\s*//;
730ab7a3f52SSteven Rostedt    $rval =~ s/\s*$//;
731ab7a3f52SSteven Rostedt
732ab7a3f52SSteven Rostedt    if ($cmp eq "==") {
733ab7a3f52SSteven Rostedt	return $lval eq $rval;
734ab7a3f52SSteven Rostedt    } elsif ($cmp eq "!=") {
735ab7a3f52SSteven Rostedt	return $lval ne $rval;
7368fddbe9bSSteven Rostedt    } elsif ($cmp eq "=~") {
7378fddbe9bSSteven Rostedt	return $lval =~ m/$rval/;
7388fddbe9bSSteven Rostedt    } elsif ($cmp eq "!~") {
7398fddbe9bSSteven Rostedt	return $lval !~ m/$rval/;
740ab7a3f52SSteven Rostedt    }
741ab7a3f52SSteven Rostedt
742ab7a3f52SSteven Rostedt    my $statement = "$lval $cmp $rval";
743ab7a3f52SSteven Rostedt    my $ret = eval $statement;
744ab7a3f52SSteven Rostedt
745ab7a3f52SSteven Rostedt    # $@ stores error of eval
746ab7a3f52SSteven Rostedt    if ($@) {
747ab7a3f52SSteven Rostedt	return -1;
748ab7a3f52SSteven Rostedt    }
749ab7a3f52SSteven Rostedt
750ab7a3f52SSteven Rostedt    return $ret;
751ab7a3f52SSteven Rostedt}
752ab7a3f52SSteven Rostedt
7539900b5dcSSteven Rostedtsub value_defined {
7549900b5dcSSteven Rostedt    my ($val) = @_;
7559900b5dcSSteven Rostedt
7569900b5dcSSteven Rostedt    return defined($variable{$2}) ||
7579900b5dcSSteven Rostedt	defined($opt{$2});
7589900b5dcSSteven Rostedt}
7599900b5dcSSteven Rostedt
7608d735212SSteven Rostedtmy $d = 0;
7618d735212SSteven Rostedtsub process_expression {
7628d735212SSteven Rostedt    my ($name, $val) = @_;
76345d73a5dSSteven Rostedt
7648d735212SSteven Rostedt    my $c = $d++;
7658d735212SSteven Rostedt
7668d735212SSteven Rostedt    while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
7678d735212SSteven Rostedt	my $express = $1;
7688d735212SSteven Rostedt
7698d735212SSteven Rostedt	if (process_expression($name, $express)) {
7708d735212SSteven Rostedt	    $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
7718d735212SSteven Rostedt	} else {
7728d735212SSteven Rostedt	    $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
7738d735212SSteven Rostedt	}
7748d735212SSteven Rostedt    }
7758d735212SSteven Rostedt
7768d735212SSteven Rostedt    $d--;
7778d735212SSteven Rostedt    my $OR = "\\|\\|";
7788d735212SSteven Rostedt    my $AND = "\\&\\&";
7798d735212SSteven Rostedt
7808d735212SSteven Rostedt    while ($val =~ s/^(.*?)($OR|$AND)//) {
7818d735212SSteven Rostedt	my $express = $1;
7828d735212SSteven Rostedt	my $op = $2;
7838d735212SSteven Rostedt
7848d735212SSteven Rostedt	if (process_expression($name, $express)) {
7858d735212SSteven Rostedt	    if ($op eq "||") {
7868d735212SSteven Rostedt		return 1;
7878d735212SSteven Rostedt	    }
7888d735212SSteven Rostedt	} else {
7898d735212SSteven Rostedt	    if ($op eq "&&") {
7908d735212SSteven Rostedt		return 0;
7918d735212SSteven Rostedt	    }
7928d735212SSteven Rostedt	}
7938d735212SSteven Rostedt    }
79445d73a5dSSteven Rostedt
7958fddbe9bSSteven Rostedt    if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
796ab7a3f52SSteven Rostedt	my $ret = process_compare($1, $2, $3);
797ab7a3f52SSteven Rostedt	if ($ret < 0) {
798ab7a3f52SSteven Rostedt	    die "$name: $.: Unable to process comparison\n";
799ab7a3f52SSteven Rostedt	}
800ab7a3f52SSteven Rostedt	return $ret;
801ab7a3f52SSteven Rostedt    }
802ab7a3f52SSteven Rostedt
8039900b5dcSSteven Rostedt    if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
8049900b5dcSSteven Rostedt	if (defined $1) {
8059900b5dcSSteven Rostedt	    return !value_defined($2);
8069900b5dcSSteven Rostedt	} else {
8079900b5dcSSteven Rostedt	    return value_defined($2);
8089900b5dcSSteven Rostedt	}
8099900b5dcSSteven Rostedt    }
8109900b5dcSSteven Rostedt
81145d73a5dSSteven Rostedt    if ($val =~ /^\s*0\s*$/) {
81245d73a5dSSteven Rostedt	return 0;
81345d73a5dSSteven Rostedt    } elsif ($val =~ /^\s*\d+\s*$/) {
81445d73a5dSSteven Rostedt	return 1;
81545d73a5dSSteven Rostedt    }
81645d73a5dSSteven Rostedt
8179900b5dcSSteven Rostedt    die ("$name: $.: Undefined content $val in if statement\n");
8188d735212SSteven Rostedt}
8198d735212SSteven Rostedt
8208d735212SSteven Rostedtsub process_if {
8218d735212SSteven Rostedt    my ($name, $value) = @_;
8228d735212SSteven Rostedt
8238d735212SSteven Rostedt    # Convert variables and replace undefined ones with 0
8248d735212SSteven Rostedt    my $val = process_variables($value, 1);
8258d735212SSteven Rostedt    my $ret = process_expression $name, $val;
8268d735212SSteven Rostedt
8278d735212SSteven Rostedt    return $ret;
82845d73a5dSSteven Rostedt}
82945d73a5dSSteven Rostedt
8302ed3b161SSteven Rostedtsub __read_config {
8312ed3b161SSteven Rostedt    my ($config, $current_test_num) = @_;
832a57419b3SSteven Rostedt
8332ed3b161SSteven Rostedt    my $in;
8342ed3b161SSteven Rostedt    open($in, $config) || die "can't read file $config";
835a57419b3SSteven Rostedt
836a57419b3SSteven Rostedt    my $name = $config;
837a57419b3SSteven Rostedt    $name =~ s,.*/(.*),$1,;
838a57419b3SSteven Rostedt
8392ed3b161SSteven Rostedt    my $test_num = $$current_test_num;
840a57419b3SSteven Rostedt    my $default = 1;
841a57419b3SSteven Rostedt    my $repeat = 1;
842a57419b3SSteven Rostedt    my $num_tests_set = 0;
843a57419b3SSteven Rostedt    my $skip = 0;
844a57419b3SSteven Rostedt    my $rest;
845a9f84424SSteven Rostedt    my $line;
8460df213caSSteven Rostedt    my $test_case = 0;
84745d73a5dSSteven Rostedt    my $if = 0;
84845d73a5dSSteven Rostedt    my $if_set = 0;
8493d1cc414SSteven Rostedt    my $override = 0;
8503d1cc414SSteven Rostedt
8513d1cc414SSteven Rostedt    my %overrides;
852a57419b3SSteven Rostedt
8532ed3b161SSteven Rostedt    while (<$in>) {
854a57419b3SSteven Rostedt
855a57419b3SSteven Rostedt	# ignore blank lines and comments
856a57419b3SSteven Rostedt	next if (/^\s*$/ || /\s*\#/);
857a57419b3SSteven Rostedt
8580050b6bbSSteven Rostedt	if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
859a57419b3SSteven Rostedt
8600050b6bbSSteven Rostedt	    my $type = $1;
8610050b6bbSSteven Rostedt	    $rest = $2;
862a9f84424SSteven Rostedt	    $line = $2;
8630050b6bbSSteven Rostedt
8640050b6bbSSteven Rostedt	    my $old_test_num;
8650050b6bbSSteven Rostedt	    my $old_repeat;
8663d1cc414SSteven Rostedt	    $override = 0;
8670050b6bbSSteven Rostedt
8680050b6bbSSteven Rostedt	    if ($type eq "TEST_START") {
869a57419b3SSteven Rostedt
870a57419b3SSteven Rostedt		if ($num_tests_set) {
871a57419b3SSteven Rostedt		    die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
872a57419b3SSteven Rostedt		}
873a57419b3SSteven Rostedt
8740050b6bbSSteven Rostedt		$old_test_num = $test_num;
8750050b6bbSSteven Rostedt		$old_repeat = $repeat;
876a57419b3SSteven Rostedt
877a57419b3SSteven Rostedt		$test_num += $repeat;
878a57419b3SSteven Rostedt		$default = 0;
879a57419b3SSteven Rostedt		$repeat = 1;
8800050b6bbSSteven Rostedt	    } else {
8810050b6bbSSteven Rostedt		$default = 1;
8820050b6bbSSteven Rostedt	    }
883a57419b3SSteven Rostedt
884a9f84424SSteven Rostedt	    # If SKIP is anywhere in the line, the command will be skipped
885a9f84424SSteven Rostedt	    if ($rest =~ s/\s+SKIP\b//) {
886a57419b3SSteven Rostedt		$skip = 1;
887a57419b3SSteven Rostedt	    } else {
8880df213caSSteven Rostedt		$test_case = 1;
889a57419b3SSteven Rostedt		$skip = 0;
890a57419b3SSteven Rostedt	    }
891a57419b3SSteven Rostedt
892a9f84424SSteven Rostedt	    if ($rest =~ s/\sELSE\b//) {
893a9f84424SSteven Rostedt		if (!$if) {
894a9f84424SSteven Rostedt		    die "$name: $.: ELSE found with out matching IF section\n$_";
895a57419b3SSteven Rostedt		}
896a9f84424SSteven Rostedt		$if = 0;
897a9f84424SSteven Rostedt
898a9f84424SSteven Rostedt		if ($if_set) {
899a9f84424SSteven Rostedt		    $skip = 1;
900a9f84424SSteven Rostedt		} else {
901a9f84424SSteven Rostedt		    $skip = 0;
9023d1cc414SSteven Rostedt		}
9033d1cc414SSteven Rostedt	    }
904a57419b3SSteven Rostedt
905a9f84424SSteven Rostedt	    if ($rest =~ s/\sIF\s+(.*)//) {
90645d73a5dSSteven Rostedt		if (process_if($name, $1)) {
90745d73a5dSSteven Rostedt		    $if_set = 1;
90845d73a5dSSteven Rostedt		} else {
909a57419b3SSteven Rostedt		    $skip = 1;
910a57419b3SSteven Rostedt		}
91145d73a5dSSteven Rostedt		$if = 1;
91245d73a5dSSteven Rostedt	    } else {
91345d73a5dSSteven Rostedt		$if = 0;
914a9f84424SSteven Rostedt		$if_set = 0;
91545d73a5dSSteven Rostedt	    }
916a57419b3SSteven Rostedt
917a9f84424SSteven Rostedt	    if (!$skip) {
918a9f84424SSteven Rostedt		if ($type eq "TEST_START") {
919a9f84424SSteven Rostedt		    if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
920a9f84424SSteven Rostedt			$repeat = $1;
921a9f84424SSteven Rostedt			$repeat_tests{"$test_num"} = $repeat;
922a9f84424SSteven Rostedt		    }
923a9f84424SSteven Rostedt		} elsif ($rest =~ s/\sOVERRIDE\b//) {
924a9f84424SSteven Rostedt		    # DEFAULT only
925a9f84424SSteven Rostedt		    $override = 1;
926a9f84424SSteven Rostedt		    # Clear previous overrides
927a9f84424SSteven Rostedt		    %overrides = ();
928a9f84424SSteven Rostedt		}
929a9f84424SSteven Rostedt	    }
930a9f84424SSteven Rostedt
931a9f84424SSteven Rostedt	    if (!$skip && $rest !~ /^\s*$/) {
9320050b6bbSSteven Rostedt		die "$name: $.: Gargbage found after $type\n$_";
933a57419b3SSteven Rostedt	    }
934a57419b3SSteven Rostedt
9350050b6bbSSteven Rostedt	    if ($skip && $type eq "TEST_START") {
936a57419b3SSteven Rostedt		$test_num = $old_test_num;
937e48c5293SSteven Rostedt		$repeat = $old_repeat;
938a57419b3SSteven Rostedt	    }
939a57419b3SSteven Rostedt
940ab7a3f52SSteven Rostedt	} elsif (/^\s*ELSE\b(.*)$/) {
94145d73a5dSSteven Rostedt	    if (!$if) {
94245d73a5dSSteven Rostedt		die "$name: $.: ELSE found with out matching IF section\n$_";
94345d73a5dSSteven Rostedt	    }
94445d73a5dSSteven Rostedt	    $rest = $1;
94545d73a5dSSteven Rostedt	    if ($if_set) {
94645d73a5dSSteven Rostedt		$skip = 1;
947ab7a3f52SSteven Rostedt		$rest = "";
94845d73a5dSSteven Rostedt	    } else {
94945d73a5dSSteven Rostedt		$skip = 0;
95045d73a5dSSteven Rostedt
951ab7a3f52SSteven Rostedt		if ($rest =~ /\sIF\s+(.*)/) {
95245d73a5dSSteven Rostedt		    # May be a ELSE IF section.
95395f57838SSteven Rostedt		    if (process_if($name, $1)) {
95495f57838SSteven Rostedt			$if_set = 1;
95595f57838SSteven Rostedt		    } else {
95645d73a5dSSteven Rostedt			$skip = 1;
95745d73a5dSSteven Rostedt		    }
958ab7a3f52SSteven Rostedt		    $rest = "";
95945d73a5dSSteven Rostedt		} else {
96045d73a5dSSteven Rostedt		    $if = 0;
96145d73a5dSSteven Rostedt		}
96245d73a5dSSteven Rostedt	    }
96345d73a5dSSteven Rostedt
964ab7a3f52SSteven Rostedt	    if ($rest !~ /^\s*$/) {
965ab7a3f52SSteven Rostedt		die "$name: $.: Gargbage found after DEFAULTS\n$_";
966ab7a3f52SSteven Rostedt	    }
967ab7a3f52SSteven Rostedt
9682ed3b161SSteven Rostedt	} elsif (/^\s*INCLUDE\s+(\S+)/) {
9692ed3b161SSteven Rostedt
9702ed3b161SSteven Rostedt	    next if ($skip);
9712ed3b161SSteven Rostedt
9722ed3b161SSteven Rostedt	    if (!$default) {
9732ed3b161SSteven Rostedt		die "$name: $.: INCLUDE can only be done in default sections\n$_";
9742ed3b161SSteven Rostedt	    }
9752ed3b161SSteven Rostedt
9762ed3b161SSteven Rostedt	    my $file = process_variables($1);
9772ed3b161SSteven Rostedt
9782ed3b161SSteven Rostedt	    if ($file !~ m,^/,) {
9792ed3b161SSteven Rostedt		# check the path of the config file first
9802ed3b161SSteven Rostedt		if ($config =~ m,(.*)/,) {
9812ed3b161SSteven Rostedt		    if (-f "$1/$file") {
9822ed3b161SSteven Rostedt			$file = "$1/$file";
9832ed3b161SSteven Rostedt		    }
9842ed3b161SSteven Rostedt		}
9852ed3b161SSteven Rostedt	    }
9862ed3b161SSteven Rostedt
9872ed3b161SSteven Rostedt	    if ( ! -r $file ) {
9882ed3b161SSteven Rostedt		die "$name: $.: Can't read file $file\n$_";
9892ed3b161SSteven Rostedt	    }
9902ed3b161SSteven Rostedt
9912ed3b161SSteven Rostedt	    if (__read_config($file, \$test_num)) {
9922ed3b161SSteven Rostedt		$test_case = 1;
9932ed3b161SSteven Rostedt	    }
9942ed3b161SSteven Rostedt
995c75d22d9SSteven Rostedt (Red Hat)	} elsif (/^\s*([A-Z_\[\]\d]+)\s*=~\s*(.*?)\s*$/) {
996c75d22d9SSteven Rostedt (Red Hat)
997c75d22d9SSteven Rostedt (Red Hat)	    next if ($skip);
998c75d22d9SSteven Rostedt (Red Hat)
999c75d22d9SSteven Rostedt (Red Hat)	    my $lvalue = $1;
1000c75d22d9SSteven Rostedt (Red Hat)	    my $rvalue = $2;
1001c75d22d9SSteven Rostedt (Red Hat)
1002c75d22d9SSteven Rostedt (Red Hat)	    if ($default || $lvalue =~ /\[\d+\]$/) {
1003c75d22d9SSteven Rostedt (Red Hat)		set_eval($lvalue, $rvalue, $name);
1004c75d22d9SSteven Rostedt (Red Hat)	    } else {
1005c75d22d9SSteven Rostedt (Red Hat)		my $val = "$lvalue\[$test_num\]";
1006c75d22d9SSteven Rostedt (Red Hat)		set_eval($val, $rvalue, $name);
1007c75d22d9SSteven Rostedt (Red Hat)	    }
1008c75d22d9SSteven Rostedt (Red Hat)
1009a57419b3SSteven Rostedt	} elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
1010a57419b3SSteven Rostedt
1011a57419b3SSteven Rostedt	    next if ($skip);
1012a57419b3SSteven Rostedt
1013a57419b3SSteven Rostedt	    my $lvalue = $1;
1014a57419b3SSteven Rostedt	    my $rvalue = $2;
1015a57419b3SSteven Rostedt
1016a57419b3SSteven Rostedt	    if (!$default &&
1017a57419b3SSteven Rostedt		($lvalue eq "NUM_TESTS" ||
1018a57419b3SSteven Rostedt		 $lvalue eq "LOG_FILE" ||
1019a57419b3SSteven Rostedt		 $lvalue eq "CLEAR_LOG")) {
1020a57419b3SSteven Rostedt		die "$name: $.: $lvalue must be set in DEFAULTS section\n";
1021a57419b3SSteven Rostedt	    }
1022a57419b3SSteven Rostedt
1023a57419b3SSteven Rostedt	    if ($lvalue eq "NUM_TESTS") {
1024a57419b3SSteven Rostedt		if ($test_num) {
1025a57419b3SSteven Rostedt		    die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
1026a57419b3SSteven Rostedt		}
1027a57419b3SSteven Rostedt		if (!$default) {
1028a57419b3SSteven Rostedt		    die "$name: $.: NUM_TESTS must be set in default section\n";
1029a57419b3SSteven Rostedt		}
1030a57419b3SSteven Rostedt		$num_tests_set = 1;
1031a57419b3SSteven Rostedt	    }
1032a57419b3SSteven Rostedt
1033a57419b3SSteven Rostedt	    if ($default || $lvalue =~ /\[\d+\]$/) {
10343d1cc414SSteven Rostedt		set_value($lvalue, $rvalue, $override, \%overrides, $name);
1035a57419b3SSteven Rostedt	    } else {
1036a57419b3SSteven Rostedt		my $val = "$lvalue\[$test_num\]";
10373d1cc414SSteven Rostedt		set_value($val, $rvalue, $override, \%overrides, $name);
1038a57419b3SSteven Rostedt
1039a57419b3SSteven Rostedt		if ($repeat > 1) {
1040a57419b3SSteven Rostedt		    $repeats{$val} = $repeat;
1041a57419b3SSteven Rostedt		}
1042a57419b3SSteven Rostedt	    }
104377d942ceSSteven Rostedt	} elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
104477d942ceSSteven Rostedt	    next if ($skip);
104577d942ceSSteven Rostedt
104677d942ceSSteven Rostedt	    my $lvalue = $1;
104777d942ceSSteven Rostedt	    my $rvalue = $2;
104877d942ceSSteven Rostedt
104977d942ceSSteven Rostedt	    # process config variables.
105077d942ceSSteven Rostedt	    # Config variables are only active while reading the
105177d942ceSSteven Rostedt	    # config and can be defined anywhere. They also ignore
105277d942ceSSteven Rostedt	    # TEST_START and DEFAULTS, but are skipped if they are in
105377d942ceSSteven Rostedt	    # on of these sections that have SKIP defined.
105477d942ceSSteven Rostedt	    # The save variable can be
105577d942ceSSteven Rostedt	    # defined multiple times and the new one simply overrides
105677d942ceSSteven Rostedt	    # the prevous one.
105777d942ceSSteven Rostedt	    set_variable($lvalue, $rvalue);
105877d942ceSSteven Rostedt
1059a57419b3SSteven Rostedt	} else {
1060a57419b3SSteven Rostedt	    die "$name: $.: Garbage found in config\n$_";
1061a57419b3SSteven Rostedt	}
10622545eb61SSteven Rostedt    }
10632545eb61SSteven Rostedt
1064a57419b3SSteven Rostedt    if ($test_num) {
1065a57419b3SSteven Rostedt	$test_num += $repeat - 1;
1066a57419b3SSteven Rostedt	$opt{"NUM_TESTS"} = $test_num;
1067a57419b3SSteven Rostedt    }
1068a57419b3SSteven Rostedt
10692ed3b161SSteven Rostedt    close($in);
10702ed3b161SSteven Rostedt
10712ed3b161SSteven Rostedt    $$current_test_num = $test_num;
10722ed3b161SSteven Rostedt
10732ed3b161SSteven Rostedt    return $test_case;
10742ed3b161SSteven Rostedt}
10752ed3b161SSteven Rostedt
1076c4261d0fSSteven Rostedtsub get_test_case {
1077c4261d0fSSteven Rostedt	print "What test case would you like to run?\n";
1078c4261d0fSSteven Rostedt	print " (build, install or boot)\n";
1079c4261d0fSSteven Rostedt	print " Other tests are available but require editing the config file\n";
1080c4261d0fSSteven Rostedt	my $ans = <STDIN>;
1081c4261d0fSSteven Rostedt	chomp $ans;
1082c4261d0fSSteven Rostedt	$default{"TEST_TYPE"} = $ans;
1083c4261d0fSSteven Rostedt}
1084c4261d0fSSteven Rostedt
10852ed3b161SSteven Rostedtsub read_config {
10862ed3b161SSteven Rostedt    my ($config) = @_;
10872ed3b161SSteven Rostedt
10882ed3b161SSteven Rostedt    my $test_case;
10892ed3b161SSteven Rostedt    my $test_num = 0;
10902ed3b161SSteven Rostedt
10912ed3b161SSteven Rostedt    $test_case = __read_config $config, \$test_num;
10922ed3b161SSteven Rostedt
10938d1491baSSteven Rostedt    # make sure we have all mandatory configs
10945269faadSSatoru Takeuchi    get_mandatory_configs;
10958d1491baSSteven Rostedt
10960df213caSSteven Rostedt    # was a test specified?
10970df213caSSteven Rostedt    if (!$test_case) {
10980df213caSSteven Rostedt	print "No test case specified.\n";
1099c4261d0fSSteven Rostedt	get_test_case;
11000df213caSSteven Rostedt    }
11010df213caSSteven Rostedt
1102a75fececSSteven Rostedt    # set any defaults
1103a75fececSSteven Rostedt
1104a75fececSSteven Rostedt    foreach my $default (keys %default) {
1105a75fececSSteven Rostedt	if (!defined($opt{$default})) {
1106a75fececSSteven Rostedt	    $opt{$default} = $default{$default};
1107a75fececSSteven Rostedt	}
1108a75fececSSteven Rostedt    }
11099cc9e091SSteven Rostedt
11109cc9e091SSteven Rostedt    if ($opt{"IGNORE_UNUSED"} == 1) {
11119cc9e091SSteven Rostedt	return;
11129cc9e091SSteven Rostedt    }
11139cc9e091SSteven Rostedt
11149cc9e091SSteven Rostedt    my %not_used;
11159cc9e091SSteven Rostedt
11169cc9e091SSteven Rostedt    # check if there are any stragglers (typos?)
11179cc9e091SSteven Rostedt    foreach my $option (keys %opt) {
11189cc9e091SSteven Rostedt	my $op = $option;
11199cc9e091SSteven Rostedt	# remove per test labels.
11209cc9e091SSteven Rostedt	$op =~ s/\[.*\]//;
11219cc9e091SSteven Rostedt	if (!exists($option_map{$op}) &&
11229cc9e091SSteven Rostedt	    !exists($default{$op}) &&
11239cc9e091SSteven Rostedt	    !exists($used_options{$op})) {
11249cc9e091SSteven Rostedt	    $not_used{$op} = 1;
11259cc9e091SSteven Rostedt	}
11269cc9e091SSteven Rostedt    }
11279cc9e091SSteven Rostedt
11289cc9e091SSteven Rostedt    if (%not_used) {
11299cc9e091SSteven Rostedt	my $s = "s are";
11309cc9e091SSteven Rostedt	$s = " is" if (keys %not_used == 1);
11319cc9e091SSteven Rostedt	print "The following option$s not used; could be a typo:\n";
11329cc9e091SSteven Rostedt	foreach my $option (keys %not_used) {
11339cc9e091SSteven Rostedt	    print "$option\n";
11349cc9e091SSteven Rostedt	}
11359cc9e091SSteven Rostedt	print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
11369cc9e091SSteven Rostedt	if (!read_yn "Do you want to continue?") {
11379cc9e091SSteven Rostedt	    exit -1;
11389cc9e091SSteven Rostedt	}
11399cc9e091SSteven Rostedt    }
11402545eb61SSteven Rostedt}
11412545eb61SSteven Rostedt
114223715c3cSSteven Rostedtsub __eval_option {
114304262be3SSteven Rostedt (Red Hat)    my ($name, $option, $i) = @_;
114423715c3cSSteven Rostedt
114523715c3cSSteven Rostedt    # Add space to evaluate the character before $
114623715c3cSSteven Rostedt    $option = " $option";
114723715c3cSSteven Rostedt    my $retval = "";
1148f9dfb65bSRabin Vincent    my $repeated = 0;
1149f9dfb65bSRabin Vincent    my $parent = 0;
1150f9dfb65bSRabin Vincent
1151f9dfb65bSRabin Vincent    foreach my $test (keys %repeat_tests) {
1152f9dfb65bSRabin Vincent	if ($i >= $test &&
1153f9dfb65bSRabin Vincent	    $i < $test + $repeat_tests{$test}) {
1154f9dfb65bSRabin Vincent
1155f9dfb65bSRabin Vincent	    $repeated = 1;
1156f9dfb65bSRabin Vincent	    $parent = $test;
1157f9dfb65bSRabin Vincent	    last;
1158f9dfb65bSRabin Vincent	}
1159f9dfb65bSRabin Vincent    }
116023715c3cSSteven Rostedt
116123715c3cSSteven Rostedt    while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
116223715c3cSSteven Rostedt	my $start = $1;
116323715c3cSSteven Rostedt	my $var = $2;
116423715c3cSSteven Rostedt	my $end = $3;
116523715c3cSSteven Rostedt
116623715c3cSSteven Rostedt	# Append beginning of line
116723715c3cSSteven Rostedt	$retval = "$retval$start";
116823715c3cSSteven Rostedt
116923715c3cSSteven Rostedt	# If the iteration option OPT[$i] exists, then use that.
117023715c3cSSteven Rostedt	# otherwise see if the default OPT (without [$i]) exists.
117123715c3cSSteven Rostedt
117223715c3cSSteven Rostedt	my $o = "$var\[$i\]";
1173f9dfb65bSRabin Vincent	my $parento = "$var\[$parent\]";
117423715c3cSSteven Rostedt
117504262be3SSteven Rostedt (Red Hat)	# If a variable contains itself, use the default var
117604262be3SSteven Rostedt (Red Hat)	if (($var eq $name) && defined($opt{$var})) {
117704262be3SSteven Rostedt (Red Hat)	    $o = $opt{$var};
117804262be3SSteven Rostedt (Red Hat)	    $retval = "$retval$o";
117904262be3SSteven Rostedt (Red Hat)	} elsif (defined($opt{$o})) {
118023715c3cSSteven Rostedt	    $o = $opt{$o};
118123715c3cSSteven Rostedt	    $retval = "$retval$o";
1182f9dfb65bSRabin Vincent	} elsif ($repeated && defined($opt{$parento})) {
1183f9dfb65bSRabin Vincent	    $o = $opt{$parento};
1184f9dfb65bSRabin Vincent	    $retval = "$retval$o";
118523715c3cSSteven Rostedt	} elsif (defined($opt{$var})) {
118623715c3cSSteven Rostedt	    $o = $opt{$var};
118723715c3cSSteven Rostedt	    $retval = "$retval$o";
11888e80bf05SSteven Rostedt (Red Hat)	} elsif ($var eq "KERNEL_VERSION" && defined($make)) {
11898e80bf05SSteven Rostedt (Red Hat)	    # special option KERNEL_VERSION uses kernel version
11908e80bf05SSteven Rostedt (Red Hat)	    get_version();
11918e80bf05SSteven Rostedt (Red Hat)	    $retval = "$retval$version";
119223715c3cSSteven Rostedt	} else {
119323715c3cSSteven Rostedt	    $retval = "$retval\$\{$var\}";
119423715c3cSSteven Rostedt	}
119523715c3cSSteven Rostedt
119623715c3cSSteven Rostedt	$option = $end;
119723715c3cSSteven Rostedt    }
119823715c3cSSteven Rostedt
119923715c3cSSteven Rostedt    $retval = "$retval$option";
120023715c3cSSteven Rostedt
120123715c3cSSteven Rostedt    $retval =~ s/^ //;
120223715c3cSSteven Rostedt
120323715c3cSSteven Rostedt    return $retval;
120423715c3cSSteven Rostedt}
120523715c3cSSteven Rostedt
1206c75d22d9SSteven Rostedt (Red Hat)sub process_evals {
1207c75d22d9SSteven Rostedt (Red Hat)    my ($name, $option, $i) = @_;
1208c75d22d9SSteven Rostedt (Red Hat)
1209c75d22d9SSteven Rostedt (Red Hat)    my $option_name = "$name\[$i\]";
1210c75d22d9SSteven Rostedt (Red Hat)    my $ev;
1211c75d22d9SSteven Rostedt (Red Hat)
1212c75d22d9SSteven Rostedt (Red Hat)    my $old_option = $option;
1213c75d22d9SSteven Rostedt (Red Hat)
1214c75d22d9SSteven Rostedt (Red Hat)    if (defined($evals{$option_name})) {
1215c75d22d9SSteven Rostedt (Red Hat)	$ev = $evals{$option_name};
1216c75d22d9SSteven Rostedt (Red Hat)    } elsif (defined($evals{$name})) {
1217c75d22d9SSteven Rostedt (Red Hat)	$ev = $evals{$name};
1218c75d22d9SSteven Rostedt (Red Hat)    } else {
1219c75d22d9SSteven Rostedt (Red Hat)	return $option;
1220c75d22d9SSteven Rostedt (Red Hat)    }
1221c75d22d9SSteven Rostedt (Red Hat)
1222c75d22d9SSteven Rostedt (Red Hat)    for my $e (@{$ev}) {
1223c75d22d9SSteven Rostedt (Red Hat)	eval "\$option =~ $e";
1224c75d22d9SSteven Rostedt (Red Hat)    }
1225c75d22d9SSteven Rostedt (Red Hat)
1226c75d22d9SSteven Rostedt (Red Hat)    if ($option ne $old_option) {
1227c75d22d9SSteven Rostedt (Red Hat)	doprint("$name changed from '$old_option' to '$option'\n");
1228c75d22d9SSteven Rostedt (Red Hat)    }
1229c75d22d9SSteven Rostedt (Red Hat)
1230c75d22d9SSteven Rostedt (Red Hat)    return $option;
1231c75d22d9SSteven Rostedt (Red Hat)}
1232c75d22d9SSteven Rostedt (Red Hat)
123323715c3cSSteven Rostedtsub eval_option {
123404262be3SSteven Rostedt (Red Hat)    my ($name, $option, $i) = @_;
123523715c3cSSteven Rostedt
123623715c3cSSteven Rostedt    my $prev = "";
123723715c3cSSteven Rostedt
123823715c3cSSteven Rostedt    # Since an option can evaluate to another option,
123923715c3cSSteven Rostedt    # keep iterating until we do not evaluate any more
124023715c3cSSteven Rostedt    # options.
124123715c3cSSteven Rostedt    my $r = 0;
124223715c3cSSteven Rostedt    while ($prev ne $option) {
124323715c3cSSteven Rostedt	# Check for recursive evaluations.
124423715c3cSSteven Rostedt	# 100 deep should be more than enough.
124523715c3cSSteven Rostedt	if ($r++ > 100) {
124623715c3cSSteven Rostedt	    die "Over 100 evaluations accurred with $option\n" .
124723715c3cSSteven Rostedt		"Check for recursive variables\n";
124823715c3cSSteven Rostedt	}
124923715c3cSSteven Rostedt	$prev = $option;
125004262be3SSteven Rostedt (Red Hat)	$option = __eval_option($name, $option, $i);
125123715c3cSSteven Rostedt    }
125223715c3cSSteven Rostedt
1253c75d22d9SSteven Rostedt (Red Hat)    $option = process_evals($name, $option, $i);
1254c75d22d9SSteven Rostedt (Red Hat)
125523715c3cSSteven Rostedt    return $option;
125623715c3cSSteven Rostedt}
125723715c3cSSteven Rostedt
12587faafbd6SSteven Rostedtsub run_command;
12592728be41SAndrew Jonessub start_monitor;
12602728be41SAndrew Jonessub end_monitor;
12612728be41SAndrew Jonessub wait_for_monitor;
12627faafbd6SSteven Rostedt
12637faafbd6SSteven Rostedtsub reboot {
12642728be41SAndrew Jones    my ($time) = @_;
12652728be41SAndrew Jones
1266a4968722SSteven Rostedt    # Make sure everything has been written to disk
1267a4968722SSteven Rostedt    run_ssh("sync");
1268a4968722SSteven Rostedt
12692b803365SSteven Rostedt    if (defined($time)) {
12702b803365SSteven Rostedt	start_monitor;
12712b803365SSteven Rostedt	# flush out current monitor
12722b803365SSteven Rostedt	# May contain the reboot success line
12732b803365SSteven Rostedt	wait_for_monitor 1;
12742b803365SSteven Rostedt    }
12752b803365SSteven Rostedt
12767faafbd6SSteven Rostedt    # try to reboot normally
1277e48c5293SSteven Rostedt    if (run_command $reboot) {
1278576f627cSSteven Rostedt	if (defined($powercycle_after_reboot)) {
1279576f627cSSteven Rostedt	    sleep $powercycle_after_reboot;
1280576f627cSSteven Rostedt	    run_command "$power_cycle";
1281576f627cSSteven Rostedt	}
1282576f627cSSteven Rostedt    } else {
12837faafbd6SSteven Rostedt	# nope? power cycle it.
1284a75fececSSteven Rostedt	run_command "$power_cycle";
12857faafbd6SSteven Rostedt    }
12862728be41SAndrew Jones
12872728be41SAndrew Jones    if (defined($time)) {
12884c0b67a2SSteven Rostedt (Red Hat)
12894c0b67a2SSteven Rostedt (Red Hat)	# We only want to get to the new kernel, don't fail
12904c0b67a2SSteven Rostedt (Red Hat)	# if we stumble over a call trace.
12914c0b67a2SSteven Rostedt (Red Hat)	my $save_ignore_errors = $ignore_errors;
12924c0b67a2SSteven Rostedt (Red Hat)	$ignore_errors = 1;
12934c0b67a2SSteven Rostedt (Red Hat)
1294d6845536SSteven Rostedt (Red Hat)	# Look for the good kernel to boot
1295d6845536SSteven Rostedt (Red Hat)	if (wait_for_monitor($time, "Linux version")) {
1296407b95b7SSteven Rostedt	    # reboot got stuck?
12978a80c727SSteven Rostedt	    doprint "Reboot did not finish. Forcing power cycle\n";
1298407b95b7SSteven Rostedt	    run_command "$power_cycle";
1299407b95b7SSteven Rostedt	}
1300d6845536SSteven Rostedt (Red Hat)
13014c0b67a2SSteven Rostedt (Red Hat)	$ignore_errors = $save_ignore_errors;
13024c0b67a2SSteven Rostedt (Red Hat)
1303d6845536SSteven Rostedt (Red Hat)	# Still need to wait for the reboot to finish
1304d6845536SSteven Rostedt (Red Hat)	wait_for_monitor($time, $reboot_success_line);
1305d6845536SSteven Rostedt (Red Hat)
13062728be41SAndrew Jones	end_monitor;
13072728be41SAndrew Jones    }
13087faafbd6SSteven Rostedt}
13097faafbd6SSteven Rostedt
1310bc7c5803SSteven Rostedtsub reboot_to_good {
1311bc7c5803SSteven Rostedt    my ($time) = @_;
1312bc7c5803SSteven Rostedt
1313bc7c5803SSteven Rostedt    if (defined($switch_to_good)) {
1314bc7c5803SSteven Rostedt	run_command $switch_to_good;
1315bc7c5803SSteven Rostedt    }
1316bc7c5803SSteven Rostedt
1317bc7c5803SSteven Rostedt    reboot $time;
1318bc7c5803SSteven Rostedt}
1319bc7c5803SSteven Rostedt
1320576f627cSSteven Rostedtsub do_not_reboot {
1321576f627cSSteven Rostedt    my $i = $iteration;
1322576f627cSSteven Rostedt
13234ab1cce5SSteven Rostedt    return $test_type eq "build" || $no_reboot ||
1324576f627cSSteven Rostedt	($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1325576f627cSSteven Rostedt	($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1326576f627cSSteven Rostedt}
1327576f627cSSteven Rostedt
13285c42fc5bSSteven Rostedtsub dodie {
13295a391fbfSSteven Rostedt    doprint "CRITICAL FAILURE... ", @_, "\n";
13305c42fc5bSSteven Rostedt
1331576f627cSSteven Rostedt    my $i = $iteration;
1332576f627cSSteven Rostedt
1333576f627cSSteven Rostedt    if ($reboot_on_error && !do_not_reboot) {
1334576f627cSSteven Rostedt
133575c3fda7SSteven Rostedt	doprint "REBOOTING\n";
1336bc7c5803SSteven Rostedt	reboot_to_good;
133775c3fda7SSteven Rostedt
1338a75fececSSteven Rostedt    } elsif ($poweroff_on_error && defined($power_off)) {
13395c42fc5bSSteven Rostedt	doprint "POWERING OFF\n";
1340a75fececSSteven Rostedt	`$power_off`;
13415c42fc5bSSteven Rostedt    }
134275c3fda7SSteven Rostedt
1343f80802cbSSteven Rostedt    if (defined($opt{"LOG_FILE"})) {
1344f80802cbSSteven Rostedt	print " See $opt{LOG_FILE} for more info.\n";
1345f80802cbSSteven Rostedt    }
1346f80802cbSSteven Rostedt
1347576f627cSSteven Rostedt    die @_, "\n";
13485c42fc5bSSteven Rostedt}
13495c42fc5bSSteven Rostedt
13507faafbd6SSteven Rostedtsub open_console {
13517faafbd6SSteven Rostedt    my ($fp) = @_;
13527faafbd6SSteven Rostedt
13537faafbd6SSteven Rostedt    my $flags;
13547faafbd6SSteven Rostedt
1355a75fececSSteven Rostedt    my $pid = open($fp, "$console|") or
1356a75fececSSteven Rostedt	dodie "Can't open console $console";
13577faafbd6SSteven Rostedt
13587faafbd6SSteven Rostedt    $flags = fcntl($fp, F_GETFL, 0) or
1359576f627cSSteven Rostedt	dodie "Can't get flags for the socket: $!";
13607faafbd6SSteven Rostedt    $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
1361576f627cSSteven Rostedt	dodie "Can't set flags for the socket: $!";
13627faafbd6SSteven Rostedt
13637faafbd6SSteven Rostedt    return $pid;
13647faafbd6SSteven Rostedt}
13657faafbd6SSteven Rostedt
13667faafbd6SSteven Rostedtsub close_console {
13677faafbd6SSteven Rostedt    my ($fp, $pid) = @_;
13687faafbd6SSteven Rostedt
13697faafbd6SSteven Rostedt    doprint "kill child process $pid\n";
13705a5d8e48SSatoru Takeuchi    kill $close_console_signal, $pid;
13717faafbd6SSteven Rostedt
13727faafbd6SSteven Rostedt    print "closing!\n";
13737faafbd6SSteven Rostedt    close($fp);
13747faafbd6SSteven Rostedt}
13757faafbd6SSteven Rostedt
13767faafbd6SSteven Rostedtsub start_monitor {
13777faafbd6SSteven Rostedt    if ($monitor_cnt++) {
13787faafbd6SSteven Rostedt	return;
13797faafbd6SSteven Rostedt    }
13807faafbd6SSteven Rostedt    $monitor_fp = \*MONFD;
13817faafbd6SSteven Rostedt    $monitor_pid = open_console $monitor_fp;
1382a75fececSSteven Rostedt
1383a75fececSSteven Rostedt    return;
1384a75fececSSteven Rostedt
1385a75fececSSteven Rostedt    open(MONFD, "Stop perl from warning about single use of MONFD");
13867faafbd6SSteven Rostedt}
13877faafbd6SSteven Rostedt
13887faafbd6SSteven Rostedtsub end_monitor {
1389319ab14fSSteven Rostedt (Red Hat)    return if (!defined $console);
13907faafbd6SSteven Rostedt    if (--$monitor_cnt) {
13917faafbd6SSteven Rostedt	return;
13927faafbd6SSteven Rostedt    }
13937faafbd6SSteven Rostedt    close_console($monitor_fp, $monitor_pid);
13947faafbd6SSteven Rostedt}
13957faafbd6SSteven Rostedt
13967faafbd6SSteven Rostedtsub wait_for_monitor {
13972b803365SSteven Rostedt    my ($time, $stop) = @_;
13982b803365SSteven Rostedt    my $full_line = "";
13997faafbd6SSteven Rostedt    my $line;
14002b803365SSteven Rostedt    my $booted = 0;
1401407b95b7SSteven Rostedt    my $start_time = time;
14028a80c727SSteven Rostedt    my $skip_call_trace = 0;
14038a80c727SSteven Rostedt    my $bug = 0;
14048a80c727SSteven Rostedt    my $bug_ignored = 0;
1405407b95b7SSteven Rostedt    my $now;
14067faafbd6SSteven Rostedt
1407a75fececSSteven Rostedt    doprint "** Wait for monitor to settle down **\n";
14087faafbd6SSteven Rostedt
14097faafbd6SSteven Rostedt    # read the monitor and wait for the system to calm down
14102b803365SSteven Rostedt    while (!$booted) {
14117faafbd6SSteven Rostedt	$line = wait_for_input($monitor_fp, $time);
14122b803365SSteven Rostedt	last if (!defined($line));
14132b803365SSteven Rostedt	print "$line";
14142b803365SSteven Rostedt	$full_line .= $line;
14152b803365SSteven Rostedt
14162b803365SSteven Rostedt	if (defined($stop) && $full_line =~ /$stop/) {
14172b803365SSteven Rostedt	    doprint "wait for monitor detected $stop\n";
14182b803365SSteven Rostedt	    $booted = 1;
14192b803365SSteven Rostedt	}
14202b803365SSteven Rostedt
14218a80c727SSteven Rostedt	if ($full_line =~ /\[ backtrace testing \]/) {
14228a80c727SSteven Rostedt	    $skip_call_trace = 1;
14238a80c727SSteven Rostedt	}
14248a80c727SSteven Rostedt
14258a80c727SSteven Rostedt	if ($full_line =~ /call trace:/i) {
14268a80c727SSteven Rostedt	    if (!$bug && !$skip_call_trace) {
14278a80c727SSteven Rostedt		if ($ignore_errors) {
14288a80c727SSteven Rostedt		    $bug_ignored = 1;
14298a80c727SSteven Rostedt		} else {
14308a80c727SSteven Rostedt		    $bug = 1;
14318a80c727SSteven Rostedt		}
14328a80c727SSteven Rostedt	    }
14338a80c727SSteven Rostedt	}
14348a80c727SSteven Rostedt
14358a80c727SSteven Rostedt	if ($full_line =~ /\[ end of backtrace testing \]/) {
14368a80c727SSteven Rostedt	    $skip_call_trace = 0;
14378a80c727SSteven Rostedt	}
14388a80c727SSteven Rostedt
14398a80c727SSteven Rostedt	if ($full_line =~ /Kernel panic -/) {
14408a80c727SSteven Rostedt	    $bug = 1;
14418a80c727SSteven Rostedt	}
14428a80c727SSteven Rostedt
14432b803365SSteven Rostedt	if ($line =~ /\n/) {
14442b803365SSteven Rostedt	    $full_line = "";
14452b803365SSteven Rostedt	}
1446407b95b7SSteven Rostedt	$now = time;
1447407b95b7SSteven Rostedt	if ($now - $start_time >= $max_monitor_wait) {
1448407b95b7SSteven Rostedt	    doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
1449407b95b7SSteven Rostedt	    return 1;
1450407b95b7SSteven Rostedt	}
14512b803365SSteven Rostedt    }
1452a75fececSSteven Rostedt    print "** Monitor flushed **\n";
14538a80c727SSteven Rostedt    return $bug;
14547faafbd6SSteven Rostedt}
14557faafbd6SSteven Rostedt
1456de5b6e3bSRabin Vincentsub save_logs {
1457de5b6e3bSRabin Vincent	my ($result, $basedir) = @_;
1458de5b6e3bSRabin Vincent	my @t = localtime;
1459de5b6e3bSRabin Vincent	my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1460de5b6e3bSRabin Vincent		1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1461de5b6e3bSRabin Vincent
1462de5b6e3bSRabin Vincent	my $type = $build_type;
1463de5b6e3bSRabin Vincent	if ($type =~ /useconfig/) {
1464de5b6e3bSRabin Vincent	    $type = "useconfig";
1465de5b6e3bSRabin Vincent	}
1466de5b6e3bSRabin Vincent
1467de5b6e3bSRabin Vincent	my $dir = "$machine-$test_type-$type-$result-$date";
1468de5b6e3bSRabin Vincent
1469de5b6e3bSRabin Vincent	$dir = "$basedir/$dir";
1470de5b6e3bSRabin Vincent
1471de5b6e3bSRabin Vincent	if (!-d $dir) {
1472de5b6e3bSRabin Vincent	    mkpath($dir) or
1473de5b6e3bSRabin Vincent		die "can't create $dir";
1474de5b6e3bSRabin Vincent	}
1475de5b6e3bSRabin Vincent
1476de5b6e3bSRabin Vincent	my %files = (
1477de5b6e3bSRabin Vincent		"config" => $output_config,
1478de5b6e3bSRabin Vincent		"buildlog" => $buildlog,
1479de5b6e3bSRabin Vincent		"dmesg" => $dmesg,
1480de5b6e3bSRabin Vincent		"testlog" => $testlog,
1481de5b6e3bSRabin Vincent	);
1482de5b6e3bSRabin Vincent
1483de5b6e3bSRabin Vincent	while (my ($name, $source) = each(%files)) {
1484de5b6e3bSRabin Vincent		if (-f "$source") {
1485de5b6e3bSRabin Vincent			cp "$source", "$dir/$name" or
1486de5b6e3bSRabin Vincent				die "failed to copy $source";
1487de5b6e3bSRabin Vincent		}
1488de5b6e3bSRabin Vincent	}
1489de5b6e3bSRabin Vincent
1490de5b6e3bSRabin Vincent	doprint "*** Saved info to $dir ***\n";
1491de5b6e3bSRabin Vincent}
1492de5b6e3bSRabin Vincent
14932b7d9b21SSteven Rostedtsub fail {
14942b7d9b21SSteven Rostedt
1495921ed4c7SSteven Rostedt	if (defined($post_test)) {
1496921ed4c7SSteven Rostedt		run_command $post_test;
1497921ed4c7SSteven Rostedt	}
1498921ed4c7SSteven Rostedt
1499a75fececSSteven Rostedt	if ($die_on_failure) {
15002b7d9b21SSteven Rostedt		dodie @_;
15012b7d9b21SSteven Rostedt	}
15022b7d9b21SSteven Rostedt
1503a75fececSSteven Rostedt	doprint "FAILED\n";
15047faafbd6SSteven Rostedt
1505576f627cSSteven Rostedt	my $i = $iteration;
1506576f627cSSteven Rostedt
1507a75fececSSteven Rostedt	# no need to reboot for just building.
1508576f627cSSteven Rostedt	if (!do_not_reboot) {
15097faafbd6SSteven Rostedt	    doprint "REBOOTING\n";
1510bc7c5803SSteven Rostedt	    reboot_to_good $sleep_time;
1511a75fececSSteven Rostedt	}
15127faafbd6SSteven Rostedt
15139064af52SSteven Rostedt	my $name = "";
15149064af52SSteven Rostedt
15159064af52SSteven Rostedt	if (defined($test_name)) {
15169064af52SSteven Rostedt	    $name = " ($test_name)";
15179064af52SSteven Rostedt	}
15189064af52SSteven Rostedt
1519576f627cSSteven Rostedt	doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1520576f627cSSteven Rostedt	doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
15219064af52SSteven Rostedt	doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
1522576f627cSSteven Rostedt	doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1523576f627cSSteven Rostedt	doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1524a75fececSSteven Rostedt
1525de5b6e3bSRabin Vincent	if (defined($store_failures)) {
1526de5b6e3bSRabin Vincent	    save_logs "fail", $store_failures;
1527cccae1a6SSteven Rostedt        }
1528cccae1a6SSteven Rostedt
15292b7d9b21SSteven Rostedt	return 1;
15302b7d9b21SSteven Rostedt}
15312b7d9b21SSteven Rostedt
15322545eb61SSteven Rostedtsub run_command {
1533f983a2bcSSatoru Takeuchi    my ($command, $redirect) = @_;
1534d6ce2a0bSSteven Rostedt    my $dolog = 0;
1535d6ce2a0bSSteven Rostedt    my $dord = 0;
1536d6ce2a0bSSteven Rostedt    my $pid;
1537d6ce2a0bSSteven Rostedt
1538e48c5293SSteven Rostedt    $command =~ s/\$SSH_USER/$ssh_user/g;
1539e48c5293SSteven Rostedt    $command =~ s/\$MACHINE/$machine/g;
1540e48c5293SSteven Rostedt
1541d6ce2a0bSSteven Rostedt    doprint("$command ... ");
1542d6ce2a0bSSteven Rostedt
1543d6ce2a0bSSteven Rostedt    $pid = open(CMD, "$command 2>&1 |") or
15442b7d9b21SSteven Rostedt	(fail "unable to exec $command" and return 0);
15452545eb61SSteven Rostedt
15462545eb61SSteven Rostedt    if (defined($opt{"LOG_FILE"})) {
1547d6ce2a0bSSteven Rostedt	open(LOG, ">>$opt{LOG_FILE}") or
1548d6ce2a0bSSteven Rostedt	    dodie "failed to write to log";
1549d6ce2a0bSSteven Rostedt	$dolog = 1;
15506c5ee0beSSteven Rostedt    }
15516c5ee0beSSteven Rostedt
15526c5ee0beSSteven Rostedt    if (defined($redirect)) {
1553d6ce2a0bSSteven Rostedt	open (RD, ">$redirect") or
1554d6ce2a0bSSteven Rostedt	    dodie "failed to write to redirect $redirect";
1555d6ce2a0bSSteven Rostedt	$dord = 1;
15562545eb61SSteven Rostedt    }
15572545eb61SSteven Rostedt
1558d6ce2a0bSSteven Rostedt    while (<CMD>) {
1559d6ce2a0bSSteven Rostedt	print LOG if ($dolog);
1560d6ce2a0bSSteven Rostedt	print RD  if ($dord);
1561d6ce2a0bSSteven Rostedt    }
15622545eb61SSteven Rostedt
1563d6ce2a0bSSteven Rostedt    waitpid($pid, 0);
15642545eb61SSteven Rostedt    my $failed = $?;
15652545eb61SSteven Rostedt
1566d6ce2a0bSSteven Rostedt    close(CMD);
1567d6ce2a0bSSteven Rostedt    close(LOG) if ($dolog);
1568d6ce2a0bSSteven Rostedt    close(RD)  if ($dord);
1569d6ce2a0bSSteven Rostedt
15702545eb61SSteven Rostedt    if ($failed) {
15712545eb61SSteven Rostedt	doprint "FAILED!\n";
15722545eb61SSteven Rostedt    } else {
15732545eb61SSteven Rostedt	doprint "SUCCESS\n";
15742545eb61SSteven Rostedt    }
15752545eb61SSteven Rostedt
15765f9b6cedSSteven Rostedt    return !$failed;
15775f9b6cedSSteven Rostedt}
15785f9b6cedSSteven Rostedt
1579e48c5293SSteven Rostedtsub run_ssh {
1580e48c5293SSteven Rostedt    my ($cmd) = @_;
1581e48c5293SSteven Rostedt    my $cp_exec = $ssh_exec;
1582e48c5293SSteven Rostedt
1583e48c5293SSteven Rostedt    $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1584e48c5293SSteven Rostedt    return run_command "$cp_exec";
1585e48c5293SSteven Rostedt}
1586e48c5293SSteven Rostedt
1587e48c5293SSteven Rostedtsub run_scp {
158802ad2617SSteven Rostedt    my ($src, $dst, $cp_scp) = @_;
1589e48c5293SSteven Rostedt
1590e48c5293SSteven Rostedt    $cp_scp =~ s/\$SRC_FILE/$src/g;
1591e48c5293SSteven Rostedt    $cp_scp =~ s/\$DST_FILE/$dst/g;
1592e48c5293SSteven Rostedt
1593e48c5293SSteven Rostedt    return run_command "$cp_scp";
1594e48c5293SSteven Rostedt}
1595e48c5293SSteven Rostedt
159602ad2617SSteven Rostedtsub run_scp_install {
159702ad2617SSteven Rostedt    my ($src, $dst) = @_;
159802ad2617SSteven Rostedt
159902ad2617SSteven Rostedt    my $cp_scp = $scp_to_target_install;
160002ad2617SSteven Rostedt
160102ad2617SSteven Rostedt    return run_scp($src, $dst, $cp_scp);
160202ad2617SSteven Rostedt}
160302ad2617SSteven Rostedt
160402ad2617SSteven Rostedtsub run_scp_mod {
160502ad2617SSteven Rostedt    my ($src, $dst) = @_;
160602ad2617SSteven Rostedt
160702ad2617SSteven Rostedt    my $cp_scp = $scp_to_target;
160802ad2617SSteven Rostedt
160902ad2617SSteven Rostedt    return run_scp($src, $dst, $cp_scp);
161002ad2617SSteven Rostedt}
161102ad2617SSteven Rostedt
1612a15ba913SSteven Rostedtsub get_grub2_index {
1613a15ba913SSteven Rostedt
1614752d9665SSteven Rostedt (Red Hat)    return if (defined($grub_number) && defined($last_grub_menu) &&
1615df5f7c66SSteven Rostedt (Red Hat)	       $last_grub_menu eq $grub_menu && defined($last_machine) &&
1616df5f7c66SSteven Rostedt (Red Hat)	       $last_machine eq $machine);
1617a15ba913SSteven Rostedt
1618a15ba913SSteven Rostedt    doprint "Find grub2 menu ... ";
1619a15ba913SSteven Rostedt    $grub_number = -1;
1620a15ba913SSteven Rostedt
1621a15ba913SSteven Rostedt    my $ssh_grub = $ssh_exec;
1622a15ba913SSteven Rostedt    $ssh_grub =~ s,\$SSH_COMMAND,cat $grub_file,g;
1623a15ba913SSteven Rostedt
1624a15ba913SSteven Rostedt    open(IN, "$ssh_grub |")
1625a15ba913SSteven Rostedt	or die "unable to get $grub_file";
1626a15ba913SSteven Rostedt
1627a15ba913SSteven Rostedt    my $found = 0;
1628a15ba913SSteven Rostedt
1629a15ba913SSteven Rostedt    while (<IN>) {
1630a15ba913SSteven Rostedt	if (/^menuentry.*$grub_menu/) {
1631a15ba913SSteven Rostedt	    $grub_number++;
1632a15ba913SSteven Rostedt	    $found = 1;
1633a15ba913SSteven Rostedt	    last;
1634a15ba913SSteven Rostedt	} elsif (/^menuentry\s/) {
1635a15ba913SSteven Rostedt	    $grub_number++;
1636a15ba913SSteven Rostedt	}
1637a15ba913SSteven Rostedt    }
1638a15ba913SSteven Rostedt    close(IN);
1639a15ba913SSteven Rostedt
1640a15ba913SSteven Rostedt    die "Could not find '$grub_menu' in $grub_file on $machine"
1641a15ba913SSteven Rostedt	if (!$found);
1642a15ba913SSteven Rostedt    doprint "$grub_number\n";
1643752d9665SSteven Rostedt (Red Hat)    $last_grub_menu = $grub_menu;
1644df5f7c66SSteven Rostedt (Red Hat)    $last_machine = $machine;
1645a15ba913SSteven Rostedt}
1646a15ba913SSteven Rostedt
16475f9b6cedSSteven Rostedtsub get_grub_index {
16485f9b6cedSSteven Rostedt
1649a15ba913SSteven Rostedt    if ($reboot_type eq "grub2") {
1650a15ba913SSteven Rostedt	get_grub2_index;
1651a15ba913SSteven Rostedt	return;
1652a15ba913SSteven Rostedt    }
1653a15ba913SSteven Rostedt
1654a75fececSSteven Rostedt    if ($reboot_type ne "grub") {
1655a75fececSSteven Rostedt	return;
1656a75fececSSteven Rostedt    }
1657752d9665SSteven Rostedt (Red Hat)    return if (defined($grub_number) && defined($last_grub_menu) &&
1658df5f7c66SSteven Rostedt (Red Hat)	       $last_grub_menu eq $grub_menu && defined($last_machine) &&
1659df5f7c66SSteven Rostedt (Red Hat)	       $last_machine eq $machine);
16605f9b6cedSSteven Rostedt
16615f9b6cedSSteven Rostedt    doprint "Find grub menu ... ";
16625f9b6cedSSteven Rostedt    $grub_number = -1;
1663e48c5293SSteven Rostedt
1664e48c5293SSteven Rostedt    my $ssh_grub = $ssh_exec;
1665e48c5293SSteven Rostedt    $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1666e48c5293SSteven Rostedt
1667e48c5293SSteven Rostedt    open(IN, "$ssh_grub |")
16685f9b6cedSSteven Rostedt	or die "unable to get menu.lst";
1669e48c5293SSteven Rostedt
1670eaa1fe25SSteven Rostedt    my $found = 0;
1671eaa1fe25SSteven Rostedt
16725f9b6cedSSteven Rostedt    while (<IN>) {
1673a75fececSSteven Rostedt	if (/^\s*title\s+$grub_menu\s*$/) {
16745f9b6cedSSteven Rostedt	    $grub_number++;
1675eaa1fe25SSteven Rostedt	    $found = 1;
16765f9b6cedSSteven Rostedt	    last;
16775f9b6cedSSteven Rostedt	} elsif (/^\s*title\s/) {
16785f9b6cedSSteven Rostedt	    $grub_number++;
16795f9b6cedSSteven Rostedt	}
16805f9b6cedSSteven Rostedt    }
16815f9b6cedSSteven Rostedt    close(IN);
16825f9b6cedSSteven Rostedt
1683a75fececSSteven Rostedt    die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
1684eaa1fe25SSteven Rostedt	if (!$found);
16855f9b6cedSSteven Rostedt    doprint "$grub_number\n";
1686752d9665SSteven Rostedt (Red Hat)    $last_grub_menu = $grub_menu;
1687df5f7c66SSteven Rostedt (Red Hat)    $last_machine = $machine;
16882545eb61SSteven Rostedt}
16892545eb61SSteven Rostedt
16902545eb61SSteven Rostedtsub wait_for_input
16912545eb61SSteven Rostedt{
16922545eb61SSteven Rostedt    my ($fp, $time) = @_;
16932545eb61SSteven Rostedt    my $rin;
16942545eb61SSteven Rostedt    my $ready;
16952545eb61SSteven Rostedt    my $line;
16962545eb61SSteven Rostedt    my $ch;
16972545eb61SSteven Rostedt
16982545eb61SSteven Rostedt    if (!defined($time)) {
16992545eb61SSteven Rostedt	$time = $timeout;
17002545eb61SSteven Rostedt    }
17012545eb61SSteven Rostedt
17022545eb61SSteven Rostedt    $rin = '';
17032545eb61SSteven Rostedt    vec($rin, fileno($fp), 1) = 1;
1704319ab14fSSteven Rostedt (Red Hat)    ($ready, $time) = select($rin, undef, undef, $time);
17052545eb61SSteven Rostedt
17062545eb61SSteven Rostedt    $line = "";
17072545eb61SSteven Rostedt
17082545eb61SSteven Rostedt    # try to read one char at a time
17092545eb61SSteven Rostedt    while (sysread $fp, $ch, 1) {
17102545eb61SSteven Rostedt	$line .= $ch;
17112545eb61SSteven Rostedt	last if ($ch eq "\n");
17122545eb61SSteven Rostedt    }
17132545eb61SSteven Rostedt
17142545eb61SSteven Rostedt    if (!length($line)) {
17152545eb61SSteven Rostedt	return undef;
17162545eb61SSteven Rostedt    }
17172545eb61SSteven Rostedt
17182545eb61SSteven Rostedt    return $line;
17192545eb61SSteven Rostedt}
17202545eb61SSteven Rostedt
172175c3fda7SSteven Rostedtsub reboot_to {
1722bc7c5803SSteven Rostedt    if (defined($switch_to_test)) {
1723bc7c5803SSteven Rostedt	run_command $switch_to_test;
1724bc7c5803SSteven Rostedt    }
1725bc7c5803SSteven Rostedt
1726a75fececSSteven Rostedt    if ($reboot_type eq "grub") {
1727c54367f9SSteven Rostedt	run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1728a15ba913SSteven Rostedt    } elsif ($reboot_type eq "grub2") {
1729a15ba913SSteven Rostedt	run_ssh "$grub_reboot $grub_number";
17307786954cSSteven Rostedt    } elsif ($reboot_type eq "syslinux") {
17317786954cSSteven Rostedt	run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
173296f6a0dfSSteven Rostedt    } elsif (defined $reboot_script) {
1733a75fececSSteven Rostedt	run_command "$reboot_script";
17342545eb61SSteven Rostedt    }
173596f6a0dfSSteven Rostedt    reboot;
173696f6a0dfSSteven Rostedt}
17372545eb61SSteven Rostedt
1738a57419b3SSteven Rostedtsub get_sha1 {
1739a57419b3SSteven Rostedt    my ($commit) = @_;
1740a57419b3SSteven Rostedt
1741a57419b3SSteven Rostedt    doprint "git rev-list --max-count=1 $commit ... ";
1742a57419b3SSteven Rostedt    my $sha1 = `git rev-list --max-count=1 $commit`;
1743a57419b3SSteven Rostedt    my $ret = $?;
1744a57419b3SSteven Rostedt
1745a57419b3SSteven Rostedt    logit $sha1;
1746a57419b3SSteven Rostedt
1747a57419b3SSteven Rostedt    if ($ret) {
1748a57419b3SSteven Rostedt	doprint "FAILED\n";
1749a57419b3SSteven Rostedt	dodie "Failed to get git $commit";
1750a57419b3SSteven Rostedt    }
1751a57419b3SSteven Rostedt
1752a57419b3SSteven Rostedt    print "SUCCESS\n";
1753a57419b3SSteven Rostedt
1754a57419b3SSteven Rostedt    chomp $sha1;
1755a57419b3SSteven Rostedt
1756a57419b3SSteven Rostedt    return $sha1;
1757a57419b3SSteven Rostedt}
1758a57419b3SSteven Rostedt
17595a391fbfSSteven Rostedtsub monitor {
17602545eb61SSteven Rostedt    my $booted = 0;
17612545eb61SSteven Rostedt    my $bug = 0;
17626ca996ccSSteven Rostedt    my $bug_ignored = 0;
17635c42fc5bSSteven Rostedt    my $skip_call_trace = 0;
17642b7d9b21SSteven Rostedt    my $loops;
17652545eb61SSteven Rostedt
17667faafbd6SSteven Rostedt    wait_for_monitor 5;
17672545eb61SSteven Rostedt
17682545eb61SSteven Rostedt    my $line;
17692545eb61SSteven Rostedt    my $full_line = "";
17702545eb61SSteven Rostedt
17717faafbd6SSteven Rostedt    open(DMESG, "> $dmesg") or
17727faafbd6SSteven Rostedt	die "unable to write to $dmesg";
17732545eb61SSteven Rostedt
177475c3fda7SSteven Rostedt    reboot_to;
17752545eb61SSteven Rostedt
17761c8a617aSSteven Rostedt    my $success_start;
17771c8a617aSSteven Rostedt    my $failure_start;
17782d01b26aSSteven Rostedt    my $monitor_start = time;
17792d01b26aSSteven Rostedt    my $done = 0;
1780f1a5b962SSteven Rostedt    my $version_found = 0;
17811c8a617aSSteven Rostedt
17822d01b26aSSteven Rostedt    while (!$done) {
17832545eb61SSteven Rostedt
1784ecaf8e52SSteven Rostedt	if ($bug && defined($stop_after_failure) &&
1785ecaf8e52SSteven Rostedt	    $stop_after_failure >= 0) {
1786ecaf8e52SSteven Rostedt	    my $time = $stop_after_failure - (time - $failure_start);
1787ecaf8e52SSteven Rostedt	    $line = wait_for_input($monitor_fp, $time);
1788ecaf8e52SSteven Rostedt	    if (!defined($line)) {
1789ecaf8e52SSteven Rostedt		doprint "bug timed out after $booted_timeout seconds\n";
1790ecaf8e52SSteven Rostedt		doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1791ecaf8e52SSteven Rostedt		last;
1792ecaf8e52SSteven Rostedt	    }
1793ecaf8e52SSteven Rostedt	} elsif ($booted) {
1794a75fececSSteven Rostedt	    $line = wait_for_input($monitor_fp, $booted_timeout);
1795cd4f1d53SSteven Rostedt	    if (!defined($line)) {
1796cd4f1d53SSteven Rostedt		my $s = $booted_timeout == 1 ? "" : "s";
1797cd4f1d53SSteven Rostedt		doprint "Successful boot found: break after $booted_timeout second$s\n";
1798cd4f1d53SSteven Rostedt		last;
1799cd4f1d53SSteven Rostedt	    }
18002b7d9b21SSteven Rostedt	} else {
18017faafbd6SSteven Rostedt	    $line = wait_for_input($monitor_fp);
1802cd4f1d53SSteven Rostedt	    if (!defined($line)) {
1803cd4f1d53SSteven Rostedt		my $s = $timeout == 1 ? "" : "s";
1804cd4f1d53SSteven Rostedt		doprint "Timed out after $timeout second$s\n";
1805cd4f1d53SSteven Rostedt		last;
18062b7d9b21SSteven Rostedt	    }
1807cd4f1d53SSteven Rostedt	}
18082545eb61SSteven Rostedt
18092545eb61SSteven Rostedt	doprint $line;
18107faafbd6SSteven Rostedt	print DMESG $line;
18112545eb61SSteven Rostedt
18122545eb61SSteven Rostedt	# we are not guaranteed to get a full line
18132545eb61SSteven Rostedt	$full_line .= $line;
18142545eb61SSteven Rostedt
1815a75fececSSteven Rostedt	if ($full_line =~ /$success_line/) {
18162545eb61SSteven Rostedt	    $booted = 1;
18171c8a617aSSteven Rostedt	    $success_start = time;
18181c8a617aSSteven Rostedt	}
18191c8a617aSSteven Rostedt
18201c8a617aSSteven Rostedt	if ($booted && defined($stop_after_success) &&
18211c8a617aSSteven Rostedt	    $stop_after_success >= 0) {
18221c8a617aSSteven Rostedt	    my $now = time;
18231c8a617aSSteven Rostedt	    if ($now - $success_start >= $stop_after_success) {
18241c8a617aSSteven Rostedt		doprint "Test forced to stop after $stop_after_success seconds after success\n";
18251c8a617aSSteven Rostedt		last;
18261c8a617aSSteven Rostedt	    }
18272545eb61SSteven Rostedt	}
18282545eb61SSteven Rostedt
18295c42fc5bSSteven Rostedt	if ($full_line =~ /\[ backtrace testing \]/) {
18305c42fc5bSSteven Rostedt	    $skip_call_trace = 1;
18315c42fc5bSSteven Rostedt	}
18325c42fc5bSSteven Rostedt
18332545eb61SSteven Rostedt	if ($full_line =~ /call trace:/i) {
18346ca996ccSSteven Rostedt	    if (!$bug && !$skip_call_trace) {
18356ca996ccSSteven Rostedt		if ($ignore_errors) {
18366ca996ccSSteven Rostedt		    $bug_ignored = 1;
18376ca996ccSSteven Rostedt		} else {
18381c8a617aSSteven Rostedt		    $bug = 1;
18391c8a617aSSteven Rostedt		    $failure_start = time;
18401c8a617aSSteven Rostedt		}
18411c8a617aSSteven Rostedt	    }
18426ca996ccSSteven Rostedt	}
18431c8a617aSSteven Rostedt
18441c8a617aSSteven Rostedt	if ($bug && defined($stop_after_failure) &&
18451c8a617aSSteven Rostedt	    $stop_after_failure >= 0) {
18461c8a617aSSteven Rostedt	    my $now = time;
18471c8a617aSSteven Rostedt	    if ($now - $failure_start >= $stop_after_failure) {
18481c8a617aSSteven Rostedt		doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
18491c8a617aSSteven Rostedt		last;
18501c8a617aSSteven Rostedt	    }
18515c42fc5bSSteven Rostedt	}
18525c42fc5bSSteven Rostedt
18535c42fc5bSSteven Rostedt	if ($full_line =~ /\[ end of backtrace testing \]/) {
18545c42fc5bSSteven Rostedt	    $skip_call_trace = 0;
18555c42fc5bSSteven Rostedt	}
18565c42fc5bSSteven Rostedt
18575c42fc5bSSteven Rostedt	if ($full_line =~ /Kernel panic -/) {
185810abf118SSteven Rostedt	    $failure_start = time;
18592545eb61SSteven Rostedt	    $bug = 1;
18602545eb61SSteven Rostedt	}
18612545eb61SSteven Rostedt
1862f1a5b962SSteven Rostedt	# Detect triple faults by testing the banner
1863f1a5b962SSteven Rostedt	if ($full_line =~ /\bLinux version (\S+).*\n/) {
1864f1a5b962SSteven Rostedt	    if ($1 eq $version) {
1865f1a5b962SSteven Rostedt		$version_found = 1;
1866f1a5b962SSteven Rostedt	    } elsif ($version_found && $detect_triplefault) {
1867f1a5b962SSteven Rostedt		# We already booted into the kernel we are testing,
1868f1a5b962SSteven Rostedt		# but now we booted into another kernel?
1869f1a5b962SSteven Rostedt		# Consider this a triple fault.
18708b513d0cSMasanari Iida		doprint "Already booted in Linux kernel $version, but now\n";
1871f1a5b962SSteven Rostedt		doprint "we booted into Linux kernel $1.\n";
1872f1a5b962SSteven Rostedt		doprint "Assuming that this is a triple fault.\n";
1873f1a5b962SSteven Rostedt		doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1874f1a5b962SSteven Rostedt		last;
1875f1a5b962SSteven Rostedt	    }
1876f1a5b962SSteven Rostedt	}
1877f1a5b962SSteven Rostedt
18782545eb61SSteven Rostedt	if ($line =~ /\n/) {
18792545eb61SSteven Rostedt	    $full_line = "";
18802545eb61SSteven Rostedt	}
18812d01b26aSSteven Rostedt
18822d01b26aSSteven Rostedt	if ($stop_test_after > 0 && !$booted && !$bug) {
18832d01b26aSSteven Rostedt	    if (time - $monitor_start > $stop_test_after) {
18844d62bf51SSteven Rostedt		doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
18852d01b26aSSteven Rostedt		$done = 1;
18862d01b26aSSteven Rostedt	    }
18872d01b26aSSteven Rostedt	}
18882545eb61SSteven Rostedt    }
18892545eb61SSteven Rostedt
18907faafbd6SSteven Rostedt    close(DMESG);
18912545eb61SSteven Rostedt
18922545eb61SSteven Rostedt    if ($bug) {
18932b7d9b21SSteven Rostedt	return 0 if ($in_bisect);
1894576f627cSSteven Rostedt	fail "failed - got a bug report" and return 0;
18952545eb61SSteven Rostedt    }
18965f9b6cedSSteven Rostedt
1897a75fececSSteven Rostedt    if (!$booted) {
1898a75fececSSteven Rostedt	return 0 if ($in_bisect);
1899576f627cSSteven Rostedt	fail "failed - never got a boot prompt." and return 0;
1900a75fececSSteven Rostedt    }
1901a75fececSSteven Rostedt
19026ca996ccSSteven Rostedt    if ($bug_ignored) {
19036ca996ccSSteven Rostedt	doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
19046ca996ccSSteven Rostedt    }
19056ca996ccSSteven Rostedt
19062b7d9b21SSteven Rostedt    return 1;
19072545eb61SSteven Rostedt}
19082545eb61SSteven Rostedt
19092b29b2f8SSteven Rostedtsub eval_kernel_version {
19102b29b2f8SSteven Rostedt    my ($option) = @_;
19112b29b2f8SSteven Rostedt
19122b29b2f8SSteven Rostedt    $option =~ s/\$KERNEL_VERSION/$version/g;
19132b29b2f8SSteven Rostedt
19142b29b2f8SSteven Rostedt    return $option;
19152b29b2f8SSteven Rostedt}
19162b29b2f8SSteven Rostedt
1917db05cfefSSteven Rostedtsub do_post_install {
1918db05cfefSSteven Rostedt
1919db05cfefSSteven Rostedt    return if (!defined($post_install));
1920db05cfefSSteven Rostedt
19212b29b2f8SSteven Rostedt    my $cp_post_install = eval_kernel_version $post_install;
1922db05cfefSSteven Rostedt    run_command "$cp_post_install" or
1923db05cfefSSteven Rostedt	dodie "Failed to run post install";
1924db05cfefSSteven Rostedt}
1925db05cfefSSteven Rostedt
1926e1a6c3d7SSteven Rostedt# Sometimes the reboot fails, and will hang. We try to ssh to the box
1927e1a6c3d7SSteven Rostedt# and if we fail, we force another reboot, that should powercycle it.
1928e1a6c3d7SSteven Rostedtsub test_booted {
1929e1a6c3d7SSteven Rostedt    if (!run_ssh "echo testing connection") {
1930e1a6c3d7SSteven Rostedt	reboot $sleep_time;
1931e1a6c3d7SSteven Rostedt    }
1932e1a6c3d7SSteven Rostedt}
1933e1a6c3d7SSteven Rostedt
19342545eb61SSteven Rostedtsub install {
19352545eb61SSteven Rostedt
1936e0a8742eSSteven Rostedt    return if ($no_install);
1937e0a8742eSSteven Rostedt
1938e5c2ec11SSteven Rostedt    if (defined($pre_install)) {
1939e5c2ec11SSteven Rostedt	my $cp_pre_install = eval_kernel_version $pre_install;
1940e5c2ec11SSteven Rostedt	run_command "$cp_pre_install" or
1941e5c2ec11SSteven Rostedt	    dodie "Failed to run pre install";
1942e5c2ec11SSteven Rostedt    }
1943e5c2ec11SSteven Rostedt
19442b29b2f8SSteven Rostedt    my $cp_target = eval_kernel_version $target_image;
19452b29b2f8SSteven Rostedt
1946e1a6c3d7SSteven Rostedt    test_booted;
1947e1a6c3d7SSteven Rostedt
194802ad2617SSteven Rostedt    run_scp_install "$outputdir/$build_target", "$cp_target" or
19495c42fc5bSSteven Rostedt	dodie "failed to copy image";
19505f9b6cedSSteven Rostedt
19515f9b6cedSSteven Rostedt    my $install_mods = 0;
19525f9b6cedSSteven Rostedt
19535f9b6cedSSteven Rostedt    # should we process modules?
19545f9b6cedSSteven Rostedt    $install_mods = 0;
195551ad1dd1SSteven Rostedt    open(IN, "$output_config") or dodie("Can't read config file");
19565f9b6cedSSteven Rostedt    while (<IN>) {
19575f9b6cedSSteven Rostedt	if (/CONFIG_MODULES(=y)?/) {
19588bc5e4eaSSteven Rostedt	    if (defined($1)) {
19598bc5e4eaSSteven Rostedt		$install_mods = 1;
19605f9b6cedSSteven Rostedt		last;
19615f9b6cedSSteven Rostedt	    }
19625f9b6cedSSteven Rostedt	}
19638bc5e4eaSSteven Rostedt    }
19645f9b6cedSSteven Rostedt    close(IN);
19655f9b6cedSSteven Rostedt
19665f9b6cedSSteven Rostedt    if (!$install_mods) {
1967db05cfefSSteven Rostedt	do_post_install;
19685f9b6cedSSteven Rostedt	doprint "No modules needed\n";
19695f9b6cedSSteven Rostedt	return;
19702545eb61SSteven Rostedt    }
19712545eb61SSteven Rostedt
1972627977d8SSteven Rostedt    run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
19735f9b6cedSSteven Rostedt	dodie "Failed to install modules";
19745f9b6cedSSteven Rostedt
19752545eb61SSteven Rostedt    my $modlib = "/lib/modules/$version";
1976a57419b3SSteven Rostedt    my $modtar = "ktest-mods.tar.bz2";
19772545eb61SSteven Rostedt
1978e48c5293SSteven Rostedt    run_ssh "rm -rf $modlib" or
19795c42fc5bSSteven Rostedt	dodie "failed to remove old mods: $modlib";
19802545eb61SSteven Rostedt
19815c42fc5bSSteven Rostedt    # would be nice if scp -r did not follow symbolic links
1982a75fececSSteven Rostedt    run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
19835c42fc5bSSteven Rostedt	dodie "making tarball";
19845c42fc5bSSteven Rostedt
198502ad2617SSteven Rostedt    run_scp_mod "$tmpdir/$modtar", "/tmp" or
19865c42fc5bSSteven Rostedt	dodie "failed to copy modules";
19875c42fc5bSSteven Rostedt
1988a75fececSSteven Rostedt    unlink "$tmpdir/$modtar";
19895c42fc5bSSteven Rostedt
1990e7b13441SSteven Rostedt    run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
19915c42fc5bSSteven Rostedt	dodie "failed to tar modules";
19925c42fc5bSSteven Rostedt
1993e48c5293SSteven Rostedt    run_ssh "rm -f /tmp/$modtar";
19948b37ca8cSSteven Rostedt
1995db05cfefSSteven Rostedt    do_post_install;
19962545eb61SSteven Rostedt}
19972545eb61SSteven Rostedt
1998ddf607e5SSteven Rostedtsub get_version {
1999ddf607e5SSteven Rostedt    # get the release name
2000683a3e64SSteven Rostedt    return if ($have_version);
2001ddf607e5SSteven Rostedt    doprint "$make kernelrelease ... ";
2002ddf607e5SSteven Rostedt    $version = `$make kernelrelease | tail -1`;
2003ddf607e5SSteven Rostedt    chomp($version);
2004ddf607e5SSteven Rostedt    doprint "$version\n";
2005683a3e64SSteven Rostedt    $have_version = 1;
2006ddf607e5SSteven Rostedt}
2007ddf607e5SSteven Rostedt
2008ddf607e5SSteven Rostedtsub start_monitor_and_boot {
20099f7424ccSSteven Rostedt    # Make sure the stable kernel has finished booting
2010319ab14fSSteven Rostedt (Red Hat)
2011319ab14fSSteven Rostedt (Red Hat)    # Install bisects, don't need console
2012319ab14fSSteven Rostedt (Red Hat)    if (defined $console) {
20139f7424ccSSteven Rostedt	start_monitor;
20149f7424ccSSteven Rostedt	wait_for_monitor 5;
20159f7424ccSSteven Rostedt	end_monitor;
2016319ab14fSSteven Rostedt (Red Hat)    }
20179f7424ccSSteven Rostedt
2018ddf607e5SSteven Rostedt    get_grub_index;
2019ddf607e5SSteven Rostedt    get_version;
2020ddf607e5SSteven Rostedt    install;
2021ddf607e5SSteven Rostedt
2022319ab14fSSteven Rostedt (Red Hat)    start_monitor if (defined $console);
2023ddf607e5SSteven Rostedt    return monitor;
2024ddf607e5SSteven Rostedt}
2025ddf607e5SSteven Rostedt
20264283b169SSteven Rostedt (Red Hat)my $check_build_re = ".*:.*(warning|error|Error):.*";
20274283b169SSteven Rostedt (Red Hat)my $utf8_quote = "\\x{e2}\\x{80}(\\x{98}|\\x{99})";
20284283b169SSteven Rostedt (Red Hat)
20297328735cSSteven Rostedt (Red Hat)sub process_warning_line {
20307328735cSSteven Rostedt (Red Hat)    my ($line) = @_;
20317328735cSSteven Rostedt (Red Hat)
20327328735cSSteven Rostedt (Red Hat)    chomp $line;
20337328735cSSteven Rostedt (Red Hat)
20347328735cSSteven Rostedt (Red Hat)    # for distcc heterogeneous systems, some compilers
20357328735cSSteven Rostedt (Red Hat)    # do things differently causing warning lines
20367328735cSSteven Rostedt (Red Hat)    # to be slightly different. This makes an attempt
20377328735cSSteven Rostedt (Red Hat)    # to fixe those issues.
20387328735cSSteven Rostedt (Red Hat)
20397328735cSSteven Rostedt (Red Hat)    # chop off the index into the line
20407328735cSSteven Rostedt (Red Hat)    # using distcc, some compilers give different indexes
20417328735cSSteven Rostedt (Red Hat)    # depending on white space
20427328735cSSteven Rostedt (Red Hat)    $line =~ s/^(\s*\S+:\d+:)\d+/$1/;
20437328735cSSteven Rostedt (Red Hat)
20447328735cSSteven Rostedt (Red Hat)    # Some compilers use UTF-8 extended for quotes and some don't.
20457328735cSSteven Rostedt (Red Hat)    $line =~ s/$utf8_quote/'/g;
20467328735cSSteven Rostedt (Red Hat)
20477328735cSSteven Rostedt (Red Hat)    return $line;
20487328735cSSteven Rostedt (Red Hat)}
20497328735cSSteven Rostedt (Red Hat)
20504283b169SSteven Rostedt (Red Hat)# Read buildlog and check against warnings file for any
20514283b169SSteven Rostedt (Red Hat)# new warnings.
20524283b169SSteven Rostedt (Red Hat)#
20534283b169SSteven Rostedt (Red Hat)# Returns 1 if OK
20544283b169SSteven Rostedt (Red Hat)#         0 otherwise
20556c5ee0beSSteven Rostedtsub check_buildlog {
20564283b169SSteven Rostedt (Red Hat)    return 1 if (!defined $warnings_file);
20574283b169SSteven Rostedt (Red Hat)
20584283b169SSteven Rostedt (Red Hat)    my %warnings_list;
20594283b169SSteven Rostedt (Red Hat)
20604283b169SSteven Rostedt (Red Hat)    # Failed builds should not reboot the target
20614283b169SSteven Rostedt (Red Hat)    my $save_no_reboot = $no_reboot;
20624283b169SSteven Rostedt (Red Hat)    $no_reboot = 1;
20634283b169SSteven Rostedt (Red Hat)
20644283b169SSteven Rostedt (Red Hat)    if (-f $warnings_file) {
20654283b169SSteven Rostedt (Red Hat)	open(IN, $warnings_file) or
20664283b169SSteven Rostedt (Red Hat)	    dodie "Error opening $warnings_file";
20674283b169SSteven Rostedt (Red Hat)
20684283b169SSteven Rostedt (Red Hat)	while (<IN>) {
20694283b169SSteven Rostedt (Red Hat)	    if (/$check_build_re/) {
20707328735cSSteven Rostedt (Red Hat)		my $warning = process_warning_line $_;
20717328735cSSteven Rostedt (Red Hat)
20727328735cSSteven Rostedt (Red Hat)		$warnings_list{$warning} = 1;
20734283b169SSteven Rostedt (Red Hat)	    }
20744283b169SSteven Rostedt (Red Hat)	}
20754283b169SSteven Rostedt (Red Hat)	close(IN);
20764283b169SSteven Rostedt (Red Hat)    }
20774283b169SSteven Rostedt (Red Hat)
20784283b169SSteven Rostedt (Red Hat)    # If warnings file didn't exist, and WARNINGS_FILE exist,
20794283b169SSteven Rostedt (Red Hat)    # then we fail on any warning!
20804283b169SSteven Rostedt (Red Hat)
20814283b169SSteven Rostedt (Red Hat)    open(IN, $buildlog) or dodie "Can't open $buildlog";
20824283b169SSteven Rostedt (Red Hat)    while (<IN>) {
20834283b169SSteven Rostedt (Red Hat)	if (/$check_build_re/) {
20847328735cSSteven Rostedt (Red Hat)	    my $warning = process_warning_line $_;
20854283b169SSteven Rostedt (Red Hat)
20867328735cSSteven Rostedt (Red Hat)	    if (!defined $warnings_list{$warning}) {
20874283b169SSteven Rostedt (Red Hat)		fail "New warning found (not in $warnings_file)\n$_\n";
20884283b169SSteven Rostedt (Red Hat)		$no_reboot = $save_no_reboot;
20894283b169SSteven Rostedt (Red Hat)		return 0;
20904283b169SSteven Rostedt (Red Hat)	    }
20914283b169SSteven Rostedt (Red Hat)	}
20924283b169SSteven Rostedt (Red Hat)    }
20934283b169SSteven Rostedt (Red Hat)    $no_reboot = $save_no_reboot;
20944283b169SSteven Rostedt (Red Hat)    close(IN);
20954283b169SSteven Rostedt (Red Hat)}
20964283b169SSteven Rostedt (Red Hat)
20974283b169SSteven Rostedt (Red Hat)sub check_patch_buildlog {
20986c5ee0beSSteven Rostedt    my ($patch) = @_;
20996c5ee0beSSteven Rostedt
21006c5ee0beSSteven Rostedt    my @files = `git show $patch | diffstat -l`;
21016c5ee0beSSteven Rostedt
210235275685SSteven Rostedt (Red Hat)    foreach my $file (@files) {
210335275685SSteven Rostedt (Red Hat)	chomp $file;
210435275685SSteven Rostedt (Red Hat)    }
210535275685SSteven Rostedt (Red Hat)
21066c5ee0beSSteven Rostedt    open(IN, "git show $patch |") or
21076c5ee0beSSteven Rostedt	dodie "failed to show $patch";
21086c5ee0beSSteven Rostedt    while (<IN>) {
21096c5ee0beSSteven Rostedt	if (m,^--- a/(.*),) {
21106c5ee0beSSteven Rostedt	    chomp $1;
21116c5ee0beSSteven Rostedt	    $files[$#files] = $1;
21126c5ee0beSSteven Rostedt	}
21136c5ee0beSSteven Rostedt    }
21146c5ee0beSSteven Rostedt    close(IN);
21156c5ee0beSSteven Rostedt
21166c5ee0beSSteven Rostedt    open(IN, $buildlog) or dodie "Can't open $buildlog";
21176c5ee0beSSteven Rostedt    while (<IN>) {
21186c5ee0beSSteven Rostedt	if (/^\s*(.*?):.*(warning|error)/) {
21196c5ee0beSSteven Rostedt	    my $err = $1;
21206c5ee0beSSteven Rostedt	    foreach my $file (@files) {
2121a75fececSSteven Rostedt		my $fullpath = "$builddir/$file";
21226c5ee0beSSteven Rostedt		if ($file eq $err || $fullpath eq $err) {
21232b7d9b21SSteven Rostedt		    fail "$file built with warnings" and return 0;
21246c5ee0beSSteven Rostedt		}
21256c5ee0beSSteven Rostedt	    }
21266c5ee0beSSteven Rostedt	}
21276c5ee0beSSteven Rostedt    }
21286c5ee0beSSteven Rostedt    close(IN);
21292b7d9b21SSteven Rostedt
21302b7d9b21SSteven Rostedt    return 1;
21316c5ee0beSSteven Rostedt}
21326c5ee0beSSteven Rostedt
2133fcb3f16aSSteven Rostedtsub apply_min_config {
2134fcb3f16aSSteven Rostedt    my $outconfig = "$output_config.new";
2135612b9e9bSSteven Rostedt
2136fcb3f16aSSteven Rostedt    # Read the config file and remove anything that
2137fcb3f16aSSteven Rostedt    # is in the force_config hash (from minconfig and others)
2138fcb3f16aSSteven Rostedt    # then add the force config back.
2139fcb3f16aSSteven Rostedt
2140fcb3f16aSSteven Rostedt    doprint "Applying minimum configurations into $output_config.new\n";
2141fcb3f16aSSteven Rostedt
2142fcb3f16aSSteven Rostedt    open (OUT, ">$outconfig") or
2143fcb3f16aSSteven Rostedt	dodie "Can't create $outconfig";
2144fcb3f16aSSteven Rostedt
2145fcb3f16aSSteven Rostedt    if (-f $output_config) {
2146fcb3f16aSSteven Rostedt	open (IN, $output_config) or
2147fcb3f16aSSteven Rostedt	    dodie "Failed to open $output_config";
2148fcb3f16aSSteven Rostedt	while (<IN>) {
2149fcb3f16aSSteven Rostedt	    if (/^(# )?(CONFIG_[^\s=]*)/) {
2150fcb3f16aSSteven Rostedt		next if (defined($force_config{$2}));
2151fcb3f16aSSteven Rostedt	    }
2152fcb3f16aSSteven Rostedt	    print OUT;
2153fcb3f16aSSteven Rostedt	}
2154fcb3f16aSSteven Rostedt	close IN;
2155fcb3f16aSSteven Rostedt    }
2156fcb3f16aSSteven Rostedt    foreach my $config (keys %force_config) {
2157fcb3f16aSSteven Rostedt	print OUT "$force_config{$config}\n";
2158fcb3f16aSSteven Rostedt    }
2159fcb3f16aSSteven Rostedt    close OUT;
2160fcb3f16aSSteven Rostedt
2161fcb3f16aSSteven Rostedt    run_command "mv $outconfig $output_config";
2162fcb3f16aSSteven Rostedt}
2163fcb3f16aSSteven Rostedt
2164fcb3f16aSSteven Rostedtsub make_oldconfig {
2165fcb3f16aSSteven Rostedt
21664c4ab120SSteven Rostedt    my @force_list = keys %force_config;
21674c4ab120SSteven Rostedt
21684c4ab120SSteven Rostedt    if ($#force_list >= 0) {
2169fcb3f16aSSteven Rostedt	apply_min_config;
21704c4ab120SSteven Rostedt    }
2171fcb3f16aSSteven Rostedt
2172fb16d891SAdam Lee    if (!run_command "$make olddefconfig") {
2173fb16d891SAdam Lee	# Perhaps olddefconfig doesn't exist in this version of the kernel
217418925170SSteven Rostedt	# try oldnoconfig
217518925170SSteven Rostedt	doprint "olddefconfig failed, trying make oldnoconfig\n";
217618925170SSteven Rostedt	if (!run_command "$make oldnoconfig") {
217718925170SSteven Rostedt	    doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
2178612b9e9bSSteven Rostedt	    # try a yes '' | oldconfig
2179fcb3f16aSSteven Rostedt	    run_command "yes '' | $make oldconfig" or
2180612b9e9bSSteven Rostedt		dodie "failed make config oldconfig";
2181612b9e9bSSteven Rostedt	}
2182612b9e9bSSteven Rostedt    }
218318925170SSteven Rostedt}
2184612b9e9bSSteven Rostedt
2185fcb3f16aSSteven Rostedt# read a config file and use this to force new configs.
2186fcb3f16aSSteven Rostedtsub load_force_config {
2187fcb3f16aSSteven Rostedt    my ($config) = @_;
2188fcb3f16aSSteven Rostedt
2189cf79fab6SSteven Rostedt    doprint "Loading force configs from $config\n";
2190fcb3f16aSSteven Rostedt    open(IN, $config) or
2191fcb3f16aSSteven Rostedt	dodie "failed to read $config";
2192fcb3f16aSSteven Rostedt    while (<IN>) {
2193fcb3f16aSSteven Rostedt	chomp;
2194fcb3f16aSSteven Rostedt	if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
2195fcb3f16aSSteven Rostedt	    $force_config{$1} = $_;
2196fcb3f16aSSteven Rostedt	} elsif (/^# (CONFIG_\S*) is not set/) {
2197fcb3f16aSSteven Rostedt	    $force_config{$1} = $_;
2198fcb3f16aSSteven Rostedt	}
2199fcb3f16aSSteven Rostedt    }
2200fcb3f16aSSteven Rostedt    close IN;
2201fcb3f16aSSteven Rostedt}
2202fcb3f16aSSteven Rostedt
22032545eb61SSteven Rostedtsub build {
22042545eb61SSteven Rostedt    my ($type) = @_;
22052545eb61SSteven Rostedt
22067faafbd6SSteven Rostedt    unlink $buildlog;
22077faafbd6SSteven Rostedt
22084ab1cce5SSteven Rostedt    # Failed builds should not reboot the target
22094ab1cce5SSteven Rostedt    my $save_no_reboot = $no_reboot;
22104ab1cce5SSteven Rostedt    $no_reboot = 1;
22114ab1cce5SSteven Rostedt
2212683a3e64SSteven Rostedt    # Calculate a new version from here.
2213683a3e64SSteven Rostedt    $have_version = 0;
2214683a3e64SSteven Rostedt
22150bd6c1a3SSteven Rostedt    if (defined($pre_build)) {
22160bd6c1a3SSteven Rostedt	my $ret = run_command $pre_build;
22170bd6c1a3SSteven Rostedt	if (!$ret && defined($pre_build_die) &&
22180bd6c1a3SSteven Rostedt	    $pre_build_die) {
22190bd6c1a3SSteven Rostedt	    dodie "failed to pre_build\n";
22200bd6c1a3SSteven Rostedt	}
22210bd6c1a3SSteven Rostedt    }
22220bd6c1a3SSteven Rostedt
222375c3fda7SSteven Rostedt    if ($type =~ /^useconfig:(.*)/) {
222451ad1dd1SSteven Rostedt	run_command "cp $1 $output_config" or
222575c3fda7SSteven Rostedt	    dodie "could not copy $1 to .config";
22265f9b6cedSSteven Rostedt
222775c3fda7SSteven Rostedt	$type = "oldconfig";
222875c3fda7SSteven Rostedt    }
222975c3fda7SSteven Rostedt
22305c42fc5bSSteven Rostedt    # old config can ask questions
22315c42fc5bSSteven Rostedt    if ($type eq "oldconfig") {
2232fb16d891SAdam Lee	$type = "olddefconfig";
223375c3fda7SSteven Rostedt
223475c3fda7SSteven Rostedt	# allow for empty configs
223551ad1dd1SSteven Rostedt	run_command "touch $output_config";
223675c3fda7SSteven Rostedt
223713488231SAndrew Jones	if (!$noclean) {
223851ad1dd1SSteven Rostedt	    run_command "mv $output_config $outputdir/config_temp" or
22395c42fc5bSSteven Rostedt		dodie "moving .config";
22405c42fc5bSSteven Rostedt
224113488231SAndrew Jones	    run_command "$make mrproper" or dodie "make mrproper";
22425c42fc5bSSteven Rostedt
224351ad1dd1SSteven Rostedt	    run_command "mv $outputdir/config_temp $output_config" or
22445c42fc5bSSteven Rostedt		dodie "moving config_temp";
224513488231SAndrew Jones	}
22465c42fc5bSSteven Rostedt
22475c42fc5bSSteven Rostedt    } elsif (!$noclean) {
224851ad1dd1SSteven Rostedt	unlink "$output_config";
22495f9b6cedSSteven Rostedt	run_command "$make mrproper" or
22505c42fc5bSSteven Rostedt	    dodie "make mrproper";
22515c42fc5bSSteven Rostedt    }
22522545eb61SSteven Rostedt
22532545eb61SSteven Rostedt    # add something to distinguish this build
2254a75fececSSteven Rostedt    open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
2255a75fececSSteven Rostedt    print OUT "$localversion\n";
22562545eb61SSteven Rostedt    close(OUT);
22572545eb61SSteven Rostedt
22585f9b6cedSSteven Rostedt    if (defined($minconfig)) {
2259fcb3f16aSSteven Rostedt	load_force_config($minconfig);
22602545eb61SSteven Rostedt    }
22612545eb61SSteven Rostedt
2262fb16d891SAdam Lee    if ($type ne "olddefconfig") {
2263fcb3f16aSSteven Rostedt	run_command "$make $type" or
22645c42fc5bSSteven Rostedt	    dodie "failed make config";
2265612b9e9bSSteven Rostedt    }
2266fcb3f16aSSteven Rostedt    # Run old config regardless, to enforce min configurations
2267fcb3f16aSSteven Rostedt    make_oldconfig;
22682545eb61SSteven Rostedt
2269f983a2bcSSatoru Takeuchi    my $build_ret = run_command "$make $build_options", $buildlog;
22700bd6c1a3SSteven Rostedt
22710bd6c1a3SSteven Rostedt    if (defined($post_build)) {
2272683a3e64SSteven Rostedt	# Because a post build may change the kernel version
2273683a3e64SSteven Rostedt	# do it now.
2274683a3e64SSteven Rostedt	get_version;
22750bd6c1a3SSteven Rostedt	my $ret = run_command $post_build;
22760bd6c1a3SSteven Rostedt	if (!$ret && defined($post_build_die) &&
22770bd6c1a3SSteven Rostedt	    $post_build_die) {
22780bd6c1a3SSteven Rostedt	    dodie "failed to post_build\n";
22790bd6c1a3SSteven Rostedt	}
22800bd6c1a3SSteven Rostedt    }
22810bd6c1a3SSteven Rostedt
22820bd6c1a3SSteven Rostedt    if (!$build_ret) {
22835f9b6cedSSteven Rostedt	# bisect may need this to pass
22844ab1cce5SSteven Rostedt	if ($in_bisect) {
22854ab1cce5SSteven Rostedt	    $no_reboot = $save_no_reboot;
22864ab1cce5SSteven Rostedt	    return 0;
22874ab1cce5SSteven Rostedt	}
22882b7d9b21SSteven Rostedt	fail "failed build" and return 0;
22892545eb61SSteven Rostedt    }
22905f9b6cedSSteven Rostedt
22914ab1cce5SSteven Rostedt    $no_reboot = $save_no_reboot;
22924ab1cce5SSteven Rostedt
22932b7d9b21SSteven Rostedt    return 1;
22942545eb61SSteven Rostedt}
22952545eb61SSteven Rostedt
229675c3fda7SSteven Rostedtsub halt {
2297e48c5293SSteven Rostedt    if (!run_ssh "halt" or defined($power_off)) {
2298576f627cSSteven Rostedt	if (defined($poweroff_after_halt)) {
2299576f627cSSteven Rostedt	    sleep $poweroff_after_halt;
2300576f627cSSteven Rostedt	    run_command "$power_off";
2301576f627cSSteven Rostedt	}
2302576f627cSSteven Rostedt    } else {
230375c3fda7SSteven Rostedt	# nope? the zap it!
2304a75fececSSteven Rostedt	run_command "$power_off";
230575c3fda7SSteven Rostedt    }
230675c3fda7SSteven Rostedt}
230775c3fda7SSteven Rostedt
23085f9b6cedSSteven Rostedtsub success {
23095f9b6cedSSteven Rostedt    my ($i) = @_;
23105f9b6cedSSteven Rostedt
2311921ed4c7SSteven Rostedt    if (defined($post_test)) {
2312921ed4c7SSteven Rostedt	run_command $post_test;
2313921ed4c7SSteven Rostedt    }
2314921ed4c7SSteven Rostedt
2315e48c5293SSteven Rostedt    $successes++;
2316e48c5293SSteven Rostedt
23179064af52SSteven Rostedt    my $name = "";
23189064af52SSteven Rostedt
23199064af52SSteven Rostedt    if (defined($test_name)) {
23209064af52SSteven Rostedt	$name = " ($test_name)";
23219064af52SSteven Rostedt    }
23229064af52SSteven Rostedt
23235f9b6cedSSteven Rostedt    doprint "\n\n*******************************************\n";
23245f9b6cedSSteven Rostedt    doprint     "*******************************************\n";
23259064af52SSteven Rostedt    doprint     "KTEST RESULT: TEST $i$name SUCCESS!!!!         **\n";
23265f9b6cedSSteven Rostedt    doprint     "*******************************************\n";
23275f9b6cedSSteven Rostedt    doprint     "*******************************************\n";
23285f9b6cedSSteven Rostedt
2329de5b6e3bSRabin Vincent    if (defined($store_successes)) {
2330de5b6e3bSRabin Vincent        save_logs "success", $store_successes;
2331de5b6e3bSRabin Vincent    }
2332de5b6e3bSRabin Vincent
2333576f627cSSteven Rostedt    if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
2334a75fececSSteven Rostedt	doprint "Reboot and wait $sleep_time seconds\n";
2335bc7c5803SSteven Rostedt	reboot_to_good $sleep_time;
23365f9b6cedSSteven Rostedt    }
23375f9b6cedSSteven Rostedt}
23385f9b6cedSSteven Rostedt
2339c960bb9fSSteven Rostedtsub answer_bisect {
2340c960bb9fSSteven Rostedt    for (;;) {
2341*fee9d3e6SChris J Arges	doprint "Pass, fail, or skip? [p/f/s]";
2342c960bb9fSSteven Rostedt	my $ans = <STDIN>;
2343c960bb9fSSteven Rostedt	chomp $ans;
2344c960bb9fSSteven Rostedt	if ($ans eq "p" || $ans eq "P") {
2345c960bb9fSSteven Rostedt	    return 1;
2346c960bb9fSSteven Rostedt	} elsif ($ans eq "f" || $ans eq "F") {
2347c960bb9fSSteven Rostedt	    return 0;
2348*fee9d3e6SChris J Arges	} elsif ($ans eq "s" || $ans eq "S") {
2349*fee9d3e6SChris J Arges	    return -1;
2350c960bb9fSSteven Rostedt	} else {
2351*fee9d3e6SChris J Arges	    print "Please answer 'p', 'f', or 's'\n";
2352c960bb9fSSteven Rostedt	}
2353c960bb9fSSteven Rostedt    }
2354c960bb9fSSteven Rostedt}
2355c960bb9fSSteven Rostedt
23565a391fbfSSteven Rostedtsub child_run_test {
23577faafbd6SSteven Rostedt    my $failed = 0;
23585a391fbfSSteven Rostedt
23597faafbd6SSteven Rostedt    # child should have no power
2360a75fececSSteven Rostedt    $reboot_on_error = 0;
2361a75fececSSteven Rostedt    $poweroff_on_error = 0;
2362a75fececSSteven Rostedt    $die_on_failure = 1;
23637faafbd6SSteven Rostedt
2364f983a2bcSSatoru Takeuchi    run_command $run_test, $testlog or $failed = 1;
2365a9dd5d63SRabin Vincent
23665a391fbfSSteven Rostedt    exit $failed;
23675a391fbfSSteven Rostedt}
23685a391fbfSSteven Rostedt
23695a391fbfSSteven Rostedtmy $child_done;
23705a391fbfSSteven Rostedt
23715a391fbfSSteven Rostedtsub child_finished {
23725a391fbfSSteven Rostedt    $child_done = 1;
23735a391fbfSSteven Rostedt}
23745a391fbfSSteven Rostedt
23755a391fbfSSteven Rostedtsub do_run_test {
23765a391fbfSSteven Rostedt    my $child_pid;
23775a391fbfSSteven Rostedt    my $child_exit;
23785a391fbfSSteven Rostedt    my $line;
23795a391fbfSSteven Rostedt    my $full_line;
23805a391fbfSSteven Rostedt    my $bug = 0;
23819b1d367dSSteven Rostedt    my $bug_ignored = 0;
23825a391fbfSSteven Rostedt
23837faafbd6SSteven Rostedt    wait_for_monitor 1;
23845a391fbfSSteven Rostedt
23857faafbd6SSteven Rostedt    doprint "run test $run_test\n";
23865a391fbfSSteven Rostedt
23875a391fbfSSteven Rostedt    $child_done = 0;
23885a391fbfSSteven Rostedt
23895a391fbfSSteven Rostedt    $SIG{CHLD} = qw(child_finished);
23905a391fbfSSteven Rostedt
23915a391fbfSSteven Rostedt    $child_pid = fork;
23925a391fbfSSteven Rostedt
23935a391fbfSSteven Rostedt    child_run_test if (!$child_pid);
23945a391fbfSSteven Rostedt
23955a391fbfSSteven Rostedt    $full_line = "";
23965a391fbfSSteven Rostedt
23975a391fbfSSteven Rostedt    do {
23987faafbd6SSteven Rostedt	$line = wait_for_input($monitor_fp, 1);
23995a391fbfSSteven Rostedt	if (defined($line)) {
24005a391fbfSSteven Rostedt
24015a391fbfSSteven Rostedt	    # we are not guaranteed to get a full line
24025a391fbfSSteven Rostedt	    $full_line .= $line;
24038ea0e063SSteven Rostedt	    doprint $line;
24045a391fbfSSteven Rostedt
24055a391fbfSSteven Rostedt	    if ($full_line =~ /call trace:/i) {
24069b1d367dSSteven Rostedt		if ($ignore_errors) {
24079b1d367dSSteven Rostedt		    $bug_ignored = 1;
24089b1d367dSSteven Rostedt		} else {
24095a391fbfSSteven Rostedt		    $bug = 1;
24105a391fbfSSteven Rostedt		}
24119b1d367dSSteven Rostedt	    }
24125a391fbfSSteven Rostedt
24135a391fbfSSteven Rostedt	    if ($full_line =~ /Kernel panic -/) {
24145a391fbfSSteven Rostedt		$bug = 1;
24155a391fbfSSteven Rostedt	    }
24165a391fbfSSteven Rostedt
24175a391fbfSSteven Rostedt	    if ($line =~ /\n/) {
24185a391fbfSSteven Rostedt		$full_line = "";
24195a391fbfSSteven Rostedt	    }
24205a391fbfSSteven Rostedt	}
24215a391fbfSSteven Rostedt    } while (!$child_done && !$bug);
24225a391fbfSSteven Rostedt
24239b1d367dSSteven Rostedt    if (!$bug && $bug_ignored) {
24249b1d367dSSteven Rostedt	doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
24259b1d367dSSteven Rostedt    }
24269b1d367dSSteven Rostedt
24275a391fbfSSteven Rostedt    if ($bug) {
24288ea0e063SSteven Rostedt	my $failure_start = time;
24298ea0e063SSteven Rostedt	my $now;
24308ea0e063SSteven Rostedt	do {
24318ea0e063SSteven Rostedt	    $line = wait_for_input($monitor_fp, 1);
24328ea0e063SSteven Rostedt	    if (defined($line)) {
24338ea0e063SSteven Rostedt		doprint $line;
24348ea0e063SSteven Rostedt	    }
24358ea0e063SSteven Rostedt	    $now = time;
24368ea0e063SSteven Rostedt	    if ($now - $failure_start >= $stop_after_failure) {
24378ea0e063SSteven Rostedt		last;
24388ea0e063SSteven Rostedt	    }
24398ea0e063SSteven Rostedt	} while (defined($line));
24408ea0e063SSteven Rostedt
24415a391fbfSSteven Rostedt	doprint "Detected kernel crash!\n";
24425a391fbfSSteven Rostedt	# kill the child with extreme prejudice
24435a391fbfSSteven Rostedt	kill 9, $child_pid;
24445a391fbfSSteven Rostedt    }
24455a391fbfSSteven Rostedt
24465a391fbfSSteven Rostedt    waitpid $child_pid, 0;
24475a391fbfSSteven Rostedt    $child_exit = $?;
24485a391fbfSSteven Rostedt
2449c5dacb88SSteven Rostedt    if (!$bug && $in_bisect) {
2450c5dacb88SSteven Rostedt	if (defined($bisect_ret_good)) {
2451c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_good) {
2452c5dacb88SSteven Rostedt		return 1;
2453c5dacb88SSteven Rostedt	    }
2454c5dacb88SSteven Rostedt	}
2455c5dacb88SSteven Rostedt	if (defined($bisect_ret_skip)) {
2456c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_skip) {
2457c5dacb88SSteven Rostedt		return -1;
2458c5dacb88SSteven Rostedt	    }
2459c5dacb88SSteven Rostedt	}
2460c5dacb88SSteven Rostedt	if (defined($bisect_ret_abort)) {
2461c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_abort) {
2462c5dacb88SSteven Rostedt		fail "test abort" and return -2;
2463c5dacb88SSteven Rostedt	    }
2464c5dacb88SSteven Rostedt	}
2465c5dacb88SSteven Rostedt	if (defined($bisect_ret_bad)) {
2466c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_skip) {
2467c5dacb88SSteven Rostedt		return 0;
2468c5dacb88SSteven Rostedt	    }
2469c5dacb88SSteven Rostedt	}
2470c5dacb88SSteven Rostedt	if (defined($bisect_ret_default)) {
2471c5dacb88SSteven Rostedt	    if ($bisect_ret_default eq "good") {
2472c5dacb88SSteven Rostedt		return 1;
2473c5dacb88SSteven Rostedt	    } elsif ($bisect_ret_default eq "bad") {
2474c5dacb88SSteven Rostedt		return 0;
2475c5dacb88SSteven Rostedt	    } elsif ($bisect_ret_default eq "skip") {
2476c5dacb88SSteven Rostedt		return -1;
2477c5dacb88SSteven Rostedt	    } elsif ($bisect_ret_default eq "abort") {
2478c5dacb88SSteven Rostedt		return -2;
2479c5dacb88SSteven Rostedt	    } else {
2480c5dacb88SSteven Rostedt		fail "unknown default action: $bisect_ret_default"
2481c5dacb88SSteven Rostedt		    and return -2;
2482c5dacb88SSteven Rostedt	    }
2483c5dacb88SSteven Rostedt	}
2484c5dacb88SSteven Rostedt    }
2485c5dacb88SSteven Rostedt
24865a391fbfSSteven Rostedt    if ($bug || $child_exit) {
24872b7d9b21SSteven Rostedt	return 0 if $in_bisect;
24882b7d9b21SSteven Rostedt	fail "test failed" and return 0;
24895a391fbfSSteven Rostedt    }
24902b7d9b21SSteven Rostedt    return 1;
24915a391fbfSSteven Rostedt}
24925a391fbfSSteven Rostedt
2493a75fececSSteven Rostedtsub run_git_bisect {
2494a75fececSSteven Rostedt    my ($command) = @_;
2495a75fececSSteven Rostedt
2496a75fececSSteven Rostedt    doprint "$command ... ";
2497a75fececSSteven Rostedt
2498a75fececSSteven Rostedt    my $output = `$command 2>&1`;
2499a75fececSSteven Rostedt    my $ret = $?;
2500a75fececSSteven Rostedt
2501a75fececSSteven Rostedt    logit $output;
2502a75fececSSteven Rostedt
2503a75fececSSteven Rostedt    if ($ret) {
2504a75fececSSteven Rostedt	doprint "FAILED\n";
2505a75fececSSteven Rostedt	dodie "Failed to git bisect";
2506a75fececSSteven Rostedt    }
2507a75fececSSteven Rostedt
2508a75fececSSteven Rostedt    doprint "SUCCESS\n";
2509a75fececSSteven Rostedt    if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2510a75fececSSteven Rostedt	doprint "$1 [$2]\n";
2511a75fececSSteven Rostedt    } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
2512b5f4aea6SSteven Rostedt	$bisect_bad_commit = $1;
2513a75fececSSteven Rostedt	doprint "Found bad commit... $1\n";
2514a75fececSSteven Rostedt	return 0;
2515a75fececSSteven Rostedt    } else {
2516a75fececSSteven Rostedt	# we already logged it, just print it now.
2517a75fececSSteven Rostedt	print $output;
2518a75fececSSteven Rostedt    }
2519a75fececSSteven Rostedt
2520a75fececSSteven Rostedt    return 1;
2521a75fececSSteven Rostedt}
2522a75fececSSteven Rostedt
2523c23dca7cSSteven Rostedtsub bisect_reboot {
2524c23dca7cSSteven Rostedt    doprint "Reboot and sleep $bisect_sleep_time seconds\n";
2525bc7c5803SSteven Rostedt    reboot_to_good $bisect_sleep_time;
2526c23dca7cSSteven Rostedt}
2527c23dca7cSSteven Rostedt
2528c23dca7cSSteven Rostedt# returns 1 on success, 0 on failure, -1 on skip
25290a05c769SSteven Rostedtsub run_bisect_test {
25300a05c769SSteven Rostedt    my ($type, $buildtype) = @_;
25315f9b6cedSSteven Rostedt
25322b7d9b21SSteven Rostedt    my $failed = 0;
25335f9b6cedSSteven Rostedt    my $result;
25345f9b6cedSSteven Rostedt    my $output;
25355f9b6cedSSteven Rostedt    my $ret;
25365f9b6cedSSteven Rostedt
25370a05c769SSteven Rostedt    $in_bisect = 1;
25380a05c769SSteven Rostedt
25390a05c769SSteven Rostedt    build $buildtype or $failed = 1;
25405f9b6cedSSteven Rostedt
25415f9b6cedSSteven Rostedt    if ($type ne "build") {
2542c23dca7cSSteven Rostedt	if ($failed && $bisect_skip) {
2543c23dca7cSSteven Rostedt	    $in_bisect = 0;
2544c23dca7cSSteven Rostedt	    return -1;
2545c23dca7cSSteven Rostedt	}
25467faafbd6SSteven Rostedt	dodie "Failed on build" if $failed;
25475f9b6cedSSteven Rostedt
25485f9b6cedSSteven Rostedt	# Now boot the box
2549ddf607e5SSteven Rostedt	start_monitor_and_boot or $failed = 1;
25505f9b6cedSSteven Rostedt
25515f9b6cedSSteven Rostedt	if ($type ne "boot") {
2552c23dca7cSSteven Rostedt	    if ($failed && $bisect_skip) {
2553c23dca7cSSteven Rostedt		end_monitor;
2554c23dca7cSSteven Rostedt		bisect_reboot;
2555c23dca7cSSteven Rostedt		$in_bisect = 0;
2556c23dca7cSSteven Rostedt		return -1;
2557c23dca7cSSteven Rostedt	    }
25587faafbd6SSteven Rostedt	    dodie "Failed on boot" if $failed;
25595a391fbfSSteven Rostedt
25602b7d9b21SSteven Rostedt	    do_run_test or $failed = 1;
25615f9b6cedSSteven Rostedt	}
25627faafbd6SSteven Rostedt	end_monitor;
25635f9b6cedSSteven Rostedt    }
25645f9b6cedSSteven Rostedt
25655f9b6cedSSteven Rostedt    if ($failed) {
25660a05c769SSteven Rostedt	$result = 0;
25675f9b6cedSSteven Rostedt    } else {
25680a05c769SSteven Rostedt	$result = 1;
25695f9b6cedSSteven Rostedt    }
25704025bc62SSteven Rostedt
25714025bc62SSteven Rostedt    # reboot the box to a kernel we can ssh to
25724025bc62SSteven Rostedt    if ($type ne "build") {
25734025bc62SSteven Rostedt	bisect_reboot;
25744025bc62SSteven Rostedt    }
25750a05c769SSteven Rostedt    $in_bisect = 0;
25760a05c769SSteven Rostedt
25770a05c769SSteven Rostedt    return $result;
25780a05c769SSteven Rostedt}
25790a05c769SSteven Rostedt
25800a05c769SSteven Rostedtsub run_bisect {
25810a05c769SSteven Rostedt    my ($type) = @_;
25820a05c769SSteven Rostedt    my $buildtype = "oldconfig";
25830a05c769SSteven Rostedt
25840a05c769SSteven Rostedt    # We should have a minconfig to use?
25850a05c769SSteven Rostedt    if (defined($minconfig)) {
25860a05c769SSteven Rostedt	$buildtype = "useconfig:$minconfig";
25870a05c769SSteven Rostedt    }
25880a05c769SSteven Rostedt
2589961d9cacSSteven Rostedt (Red Hat)    # If the user sets bisect_tries to less than 1, then no tries
2590961d9cacSSteven Rostedt (Red Hat)    # is a success.
2591961d9cacSSteven Rostedt (Red Hat)    my $ret = 1;
2592961d9cacSSteven Rostedt (Red Hat)
2593961d9cacSSteven Rostedt (Red Hat)    # Still let the user manually decide that though.
2594961d9cacSSteven Rostedt (Red Hat)    if ($bisect_tries < 1 && $bisect_manual) {
2595961d9cacSSteven Rostedt (Red Hat)	$ret = answer_bisect;
2596961d9cacSSteven Rostedt (Red Hat)    }
2597961d9cacSSteven Rostedt (Red Hat)
2598961d9cacSSteven Rostedt (Red Hat)    for (my $i = 0; $i < $bisect_tries; $i++) {
2599961d9cacSSteven Rostedt (Red Hat)	if ($bisect_tries > 1) {
2600961d9cacSSteven Rostedt (Red Hat)	    my $t = $i + 1;
2601961d9cacSSteven Rostedt (Red Hat)	    doprint("Running bisect trial $t of $bisect_tries:\n");
2602961d9cacSSteven Rostedt (Red Hat)	}
2603961d9cacSSteven Rostedt (Red Hat)	$ret = run_bisect_test $type, $buildtype;
26040a05c769SSteven Rostedt
2605c960bb9fSSteven Rostedt	if ($bisect_manual) {
2606c960bb9fSSteven Rostedt	    $ret = answer_bisect;
2607c960bb9fSSteven Rostedt	}
26085f9b6cedSSteven Rostedt
2609961d9cacSSteven Rostedt (Red Hat)	last if (!$ret);
2610961d9cacSSteven Rostedt (Red Hat)    }
2611961d9cacSSteven Rostedt (Red Hat)
2612d6ce2a0bSSteven Rostedt    # Are we looking for where it worked, not failed?
26135158ba3eSRuss Dill    if ($reverse_bisect && $ret >= 0) {
26140a05c769SSteven Rostedt	$ret = !$ret;
2615d6ce2a0bSSteven Rostedt    }
2616d6ce2a0bSSteven Rostedt
2617c23dca7cSSteven Rostedt    if ($ret > 0) {
26180a05c769SSteven Rostedt	return "good";
2619c23dca7cSSteven Rostedt    } elsif ($ret == 0) {
26200a05c769SSteven Rostedt	return  "bad";
2621c23dca7cSSteven Rostedt    } elsif ($bisect_skip) {
2622c23dca7cSSteven Rostedt	doprint "HIT A BAD COMMIT ... SKIPPING\n";
2623c23dca7cSSteven Rostedt	return "skip";
26240a05c769SSteven Rostedt    }
26255f9b6cedSSteven Rostedt}
26265f9b6cedSSteven Rostedt
2627dad98754SSteven Rostedtsub update_bisect_replay {
2628dad98754SSteven Rostedt    my $tmp_log = "$tmpdir/ktest_bisect_log";
2629dad98754SSteven Rostedt    run_command "git bisect log > $tmp_log" or
2630dad98754SSteven Rostedt	die "can't create bisect log";
2631dad98754SSteven Rostedt    return $tmp_log;
2632dad98754SSteven Rostedt}
2633dad98754SSteven Rostedt
26345f9b6cedSSteven Rostedtsub bisect {
26355f9b6cedSSteven Rostedt    my ($i) = @_;
26365f9b6cedSSteven Rostedt
26375f9b6cedSSteven Rostedt    my $result;
26385f9b6cedSSteven Rostedt
2639b5f4aea6SSteven Rostedt    die "BISECT_GOOD[$i] not defined\n"	if (!defined($bisect_good));
2640b5f4aea6SSteven Rostedt    die "BISECT_BAD[$i] not defined\n"	if (!defined($bisect_bad));
2641b5f4aea6SSteven Rostedt    die "BISECT_TYPE[$i] not defined\n"	if (!defined($bisect_type));
26425f9b6cedSSteven Rostedt
2643b5f4aea6SSteven Rostedt    my $good = $bisect_good;
2644b5f4aea6SSteven Rostedt    my $bad = $bisect_bad;
2645b5f4aea6SSteven Rostedt    my $type = $bisect_type;
2646b5f4aea6SSteven Rostedt    my $start = $bisect_start;
2647b5f4aea6SSteven Rostedt    my $replay = $bisect_replay;
2648b5f4aea6SSteven Rostedt    my $start_files = $bisect_files;
26493410f6fdSSteven Rostedt
26503410f6fdSSteven Rostedt    if (defined($start_files)) {
26513410f6fdSSteven Rostedt	$start_files = " -- " . $start_files;
26523410f6fdSSteven Rostedt    } else {
26533410f6fdSSteven Rostedt	$start_files = "";
26543410f6fdSSteven Rostedt    }
26555f9b6cedSSteven Rostedt
2656a57419b3SSteven Rostedt    # convert to true sha1's
2657a57419b3SSteven Rostedt    $good = get_sha1($good);
2658a57419b3SSteven Rostedt    $bad = get_sha1($bad);
2659a57419b3SSteven Rostedt
2660b5f4aea6SSteven Rostedt    if (defined($bisect_reverse) && $bisect_reverse == 1) {
2661d6ce2a0bSSteven Rostedt	doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2662d6ce2a0bSSteven Rostedt	$reverse_bisect = 1;
2663d6ce2a0bSSteven Rostedt    } else {
2664d6ce2a0bSSteven Rostedt	$reverse_bisect = 0;
2665d6ce2a0bSSteven Rostedt    }
2666d6ce2a0bSSteven Rostedt
26675a391fbfSSteven Rostedt    # Can't have a test without having a test to run
26685a391fbfSSteven Rostedt    if ($type eq "test" && !defined($run_test)) {
26695a391fbfSSteven Rostedt	$type = "boot";
26705a391fbfSSteven Rostedt    }
26715a391fbfSSteven Rostedt
2672dad98754SSteven Rostedt    # Check if a bisect was running
2673dad98754SSteven Rostedt    my $bisect_start_file = "$builddir/.git/BISECT_START";
2674dad98754SSteven Rostedt
2675b5f4aea6SSteven Rostedt    my $check = $bisect_check;
2676dad98754SSteven Rostedt    my $do_check = defined($check) && $check ne "0";
2677dad98754SSteven Rostedt
2678dad98754SSteven Rostedt    if ( -f $bisect_start_file ) {
2679dad98754SSteven Rostedt	print "Bisect in progress found\n";
2680dad98754SSteven Rostedt	if ($do_check) {
2681dad98754SSteven Rostedt	    print " If you say yes, then no checks of good or bad will be done\n";
2682dad98754SSteven Rostedt	}
2683dad98754SSteven Rostedt	if (defined($replay)) {
2684dad98754SSteven Rostedt	    print "** BISECT_REPLAY is defined in config file **";
2685dad98754SSteven Rostedt	    print " Ignore config option and perform new git bisect log?\n";
2686dad98754SSteven Rostedt	    if (read_ync " (yes, no, or cancel) ") {
2687dad98754SSteven Rostedt		$replay = update_bisect_replay;
2688dad98754SSteven Rostedt		$do_check = 0;
2689dad98754SSteven Rostedt	    }
2690dad98754SSteven Rostedt	} elsif (read_yn "read git log and continue?") {
2691dad98754SSteven Rostedt	    $replay = update_bisect_replay;
2692dad98754SSteven Rostedt	    $do_check = 0;
2693dad98754SSteven Rostedt	}
2694dad98754SSteven Rostedt    }
2695dad98754SSteven Rostedt
2696dad98754SSteven Rostedt    if ($do_check) {
2697a75fececSSteven Rostedt
2698a75fececSSteven Rostedt	# get current HEAD
2699a57419b3SSteven Rostedt	my $head = get_sha1("HEAD");
2700a75fececSSteven Rostedt
2701a75fececSSteven Rostedt	if ($check ne "good") {
2702a75fececSSteven Rostedt	    doprint "TESTING BISECT BAD [$bad]\n";
2703a75fececSSteven Rostedt	    run_command "git checkout $bad" or
2704a75fececSSteven Rostedt		die "Failed to checkout $bad";
2705a75fececSSteven Rostedt
2706a75fececSSteven Rostedt	    $result = run_bisect $type;
2707a75fececSSteven Rostedt
2708a75fececSSteven Rostedt	    if ($result ne "bad") {
2709a75fececSSteven Rostedt		fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2710a75fececSSteven Rostedt	    }
2711a75fececSSteven Rostedt	}
2712a75fececSSteven Rostedt
2713a75fececSSteven Rostedt	if ($check ne "bad") {
2714a75fececSSteven Rostedt	    doprint "TESTING BISECT GOOD [$good]\n";
2715a75fececSSteven Rostedt	    run_command "git checkout $good" or
2716a75fececSSteven Rostedt		die "Failed to checkout $good";
2717a75fececSSteven Rostedt
2718a75fececSSteven Rostedt	    $result = run_bisect $type;
2719a75fececSSteven Rostedt
2720a75fececSSteven Rostedt	    if ($result ne "good") {
2721a75fececSSteven Rostedt		fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2722a75fececSSteven Rostedt	    }
2723a75fececSSteven Rostedt	}
2724a75fececSSteven Rostedt
2725a75fececSSteven Rostedt	# checkout where we started
2726a75fececSSteven Rostedt	run_command "git checkout $head" or
2727a75fececSSteven Rostedt	    die "Failed to checkout $head";
2728a75fececSSteven Rostedt    }
2729a75fececSSteven Rostedt
27303410f6fdSSteven Rostedt    run_command "git bisect start$start_files" or
2731a75fececSSteven Rostedt	dodie "could not start bisect";
2732a75fececSSteven Rostedt
2733a75fececSSteven Rostedt    run_command "git bisect good $good" or
2734a75fececSSteven Rostedt	dodie "could not set bisect good to $good";
2735a75fececSSteven Rostedt
2736a75fececSSteven Rostedt    run_git_bisect "git bisect bad $bad" or
2737a75fececSSteven Rostedt	dodie "could not set bisect bad to $bad";
2738a75fececSSteven Rostedt
2739a75fececSSteven Rostedt    if (defined($replay)) {
2740a75fececSSteven Rostedt	run_command "git bisect replay $replay" or
2741a75fececSSteven Rostedt	    dodie "failed to run replay";
2742a75fececSSteven Rostedt    }
2743a75fececSSteven Rostedt
2744a75fececSSteven Rostedt    if (defined($start)) {
2745a75fececSSteven Rostedt	run_command "git checkout $start" or
2746a75fececSSteven Rostedt	    dodie "failed to checkout $start";
2747a75fececSSteven Rostedt    }
2748a75fececSSteven Rostedt
2749a75fececSSteven Rostedt    my $test;
27505f9b6cedSSteven Rostedt    do {
27515f9b6cedSSteven Rostedt	$result = run_bisect $type;
2752a75fececSSteven Rostedt	$test = run_git_bisect "git bisect $result";
2753a75fececSSteven Rostedt    } while ($test);
27545f9b6cedSSteven Rostedt
27555f9b6cedSSteven Rostedt    run_command "git bisect log" or
27565f9b6cedSSteven Rostedt	dodie "could not capture git bisect log";
27575f9b6cedSSteven Rostedt
27585f9b6cedSSteven Rostedt    run_command "git bisect reset" or
27595f9b6cedSSteven Rostedt	dodie "could not reset git bisect";
27605f9b6cedSSteven Rostedt
2761b5f4aea6SSteven Rostedt    doprint "Bad commit was [$bisect_bad_commit]\n";
27625f9b6cedSSteven Rostedt
27630a05c769SSteven Rostedt    success $i;
27640a05c769SSteven Rostedt}
27650a05c769SSteven Rostedt
2766cf79fab6SSteven Rostedt# config_ignore holds the configs that were set (or unset) for
2767cf79fab6SSteven Rostedt# a good config and we will ignore these configs for the rest
2768cf79fab6SSteven Rostedt# of a config bisect. These configs stay as they were.
27690a05c769SSteven Rostedtmy %config_ignore;
2770cf79fab6SSteven Rostedt
2771cf79fab6SSteven Rostedt# config_set holds what all configs were set as.
27720a05c769SSteven Rostedtmy %config_set;
27730a05c769SSteven Rostedt
2774cf79fab6SSteven Rostedt# config_off holds the set of configs that the bad config had disabled.
2775cf79fab6SSteven Rostedt# We need to record them and set them in the .config when running
2776fb16d891SAdam Lee# olddefconfig, because olddefconfig keeps the defaults.
2777cf79fab6SSteven Rostedtmy %config_off;
2778cf79fab6SSteven Rostedt
2779cf79fab6SSteven Rostedt# config_off_tmp holds a set of configs to turn off for now
2780cf79fab6SSteven Rostedtmy @config_off_tmp;
2781cf79fab6SSteven Rostedt
2782cf79fab6SSteven Rostedt# config_list is the set of configs that are being tested
27830a05c769SSteven Rostedtmy %config_list;
27840a05c769SSteven Rostedtmy %null_config;
27850a05c769SSteven Rostedt
27860a05c769SSteven Rostedtmy %dependency;
27870a05c769SSteven Rostedt
27884c4ab120SSteven Rostedtsub assign_configs {
27894c4ab120SSteven Rostedt    my ($hash, $config) = @_;
27900a05c769SSteven Rostedt
27916071c22eSSteven Rostedt (Red Hat)    doprint "Reading configs from $config\n";
27926071c22eSSteven Rostedt (Red Hat)
27930a05c769SSteven Rostedt    open (IN, $config)
27940a05c769SSteven Rostedt	or dodie "Failed to read $config";
27950a05c769SSteven Rostedt
27960a05c769SSteven Rostedt    while (<IN>) {
27976071c22eSSteven Rostedt (Red Hat)	chomp;
27989bf71749SSteven Rostedt	if (/^((CONFIG\S*)=.*)/) {
27994c4ab120SSteven Rostedt	    ${$hash}{$2} = $1;
28006071c22eSSteven Rostedt (Red Hat)	} elsif (/^(# (CONFIG\S*) is not set)/) {
28016071c22eSSteven Rostedt (Red Hat)	    ${$hash}{$2} = $1;
28020a05c769SSteven Rostedt	}
28030a05c769SSteven Rostedt    }
28040a05c769SSteven Rostedt
28050a05c769SSteven Rostedt    close(IN);
28060a05c769SSteven Rostedt}
28070a05c769SSteven Rostedt
28084c4ab120SSteven Rostedtsub process_config_ignore {
28094c4ab120SSteven Rostedt    my ($config) = @_;
28104c4ab120SSteven Rostedt
28114c4ab120SSteven Rostedt    assign_configs \%config_ignore, $config;
28124c4ab120SSteven Rostedt}
28134c4ab120SSteven Rostedt
28140a05c769SSteven Rostedtsub get_dependencies {
28150a05c769SSteven Rostedt    my ($config) = @_;
28160a05c769SSteven Rostedt
28170a05c769SSteven Rostedt    my $arr = $dependency{$config};
28180a05c769SSteven Rostedt    if (!defined($arr)) {
28190a05c769SSteven Rostedt	return ();
28200a05c769SSteven Rostedt    }
28210a05c769SSteven Rostedt
28220a05c769SSteven Rostedt    my @deps = @{$arr};
28230a05c769SSteven Rostedt
28240a05c769SSteven Rostedt    foreach my $dep (@{$arr}) {
28250a05c769SSteven Rostedt	print "ADD DEP $dep\n";
28260a05c769SSteven Rostedt	@deps = (@deps, get_dependencies $dep);
28270a05c769SSteven Rostedt    }
28280a05c769SSteven Rostedt
28290a05c769SSteven Rostedt    return @deps;
28300a05c769SSteven Rostedt}
28310a05c769SSteven Rostedt
28326071c22eSSteven Rostedt (Red Hat)sub save_config {
28336071c22eSSteven Rostedt (Red Hat)    my ($pc, $file) = @_;
28340a05c769SSteven Rostedt
28356071c22eSSteven Rostedt (Red Hat)    my %configs = %{$pc};
28360a05c769SSteven Rostedt
28376071c22eSSteven Rostedt (Red Hat)    doprint "Saving configs into $file\n";
28380a05c769SSteven Rostedt
28396071c22eSSteven Rostedt (Red Hat)    open(OUT, ">$file") or dodie "Can not write to $file";
2840cf79fab6SSteven Rostedt
28416071c22eSSteven Rostedt (Red Hat)    foreach my $config (keys %configs) {
28426071c22eSSteven Rostedt (Red Hat)	print OUT "$configs{$config}\n";
28430a05c769SSteven Rostedt    }
28440a05c769SSteven Rostedt    close(OUT);
28456071c22eSSteven Rostedt (Red Hat)}
28466071c22eSSteven Rostedt (Red Hat)
28476071c22eSSteven Rostedt (Red Hat)sub create_config {
28486071c22eSSteven Rostedt (Red Hat)    my ($name, $pc) = @_;
28496071c22eSSteven Rostedt (Red Hat)
28506071c22eSSteven Rostedt (Red Hat)    doprint "Creating old config from $name configs\n";
28516071c22eSSteven Rostedt (Red Hat)
28526071c22eSSteven Rostedt (Red Hat)    save_config $pc, $output_config;
28530a05c769SSteven Rostedt
2854fcb3f16aSSteven Rostedt    make_oldconfig;
28550a05c769SSteven Rostedt}
28560a05c769SSteven Rostedt
28576071c22eSSteven Rostedt (Red Hat)# compare two config hashes, and return configs with different vals.
28586071c22eSSteven Rostedt (Red Hat)# It returns B's config values, but you can use A to see what A was.
28596071c22eSSteven Rostedt (Red Hat)sub diff_config_vals {
28606071c22eSSteven Rostedt (Red Hat)    my ($pa, $pb) = @_;
28616071c22eSSteven Rostedt (Red Hat)
28626071c22eSSteven Rostedt (Red Hat)    # crappy Perl way to pass in hashes.
28636071c22eSSteven Rostedt (Red Hat)    my %a = %{$pa};
28646071c22eSSteven Rostedt (Red Hat)    my %b = %{$pb};
28656071c22eSSteven Rostedt (Red Hat)
28666071c22eSSteven Rostedt (Red Hat)    my %ret;
28676071c22eSSteven Rostedt (Red Hat)
28686071c22eSSteven Rostedt (Red Hat)    foreach my $item (keys %a) {
28696071c22eSSteven Rostedt (Red Hat)	if (defined($b{$item}) && $b{$item} ne $a{$item}) {
28706071c22eSSteven Rostedt (Red Hat)	    $ret{$item} = $b{$item};
28716071c22eSSteven Rostedt (Red Hat)	}
28726071c22eSSteven Rostedt (Red Hat)    }
28736071c22eSSteven Rostedt (Red Hat)
28746071c22eSSteven Rostedt (Red Hat)    return %ret;
28756071c22eSSteven Rostedt (Red Hat)}
28766071c22eSSteven Rostedt (Red Hat)
28776071c22eSSteven Rostedt (Red Hat)# compare two config hashes and return the configs in B but not A
28786071c22eSSteven Rostedt (Red Hat)sub diff_configs {
28796071c22eSSteven Rostedt (Red Hat)    my ($pa, $pb) = @_;
28806071c22eSSteven Rostedt (Red Hat)
28816071c22eSSteven Rostedt (Red Hat)    my %ret;
28826071c22eSSteven Rostedt (Red Hat)
28836071c22eSSteven Rostedt (Red Hat)    # crappy Perl way to pass in hashes.
28846071c22eSSteven Rostedt (Red Hat)    my %a = %{$pa};
28856071c22eSSteven Rostedt (Red Hat)    my %b = %{$pb};
28866071c22eSSteven Rostedt (Red Hat)
28876071c22eSSteven Rostedt (Red Hat)    foreach my $item (keys %b) {
28886071c22eSSteven Rostedt (Red Hat)	if (!defined($a{$item})) {
28896071c22eSSteven Rostedt (Red Hat)	    $ret{$item} = $b{$item};
28906071c22eSSteven Rostedt (Red Hat)	}
28916071c22eSSteven Rostedt (Red Hat)    }
28926071c22eSSteven Rostedt (Red Hat)
28936071c22eSSteven Rostedt (Red Hat)    return %ret;
28946071c22eSSteven Rostedt (Red Hat)}
28956071c22eSSteven Rostedt (Red Hat)
28966071c22eSSteven Rostedt (Red Hat)# return if two configs are equal or not
28976071c22eSSteven Rostedt (Red Hat)# 0 is equal +1 b has something a does not
28986071c22eSSteven Rostedt (Red Hat)# +1 if a and b have a different item.
28996071c22eSSteven Rostedt (Red Hat)# -1 if a has something b does not
29000a05c769SSteven Rostedtsub compare_configs {
29016071c22eSSteven Rostedt (Red Hat)    my ($pa, $pb) = @_;
29026071c22eSSteven Rostedt (Red Hat)
29036071c22eSSteven Rostedt (Red Hat)    my %ret;
29046071c22eSSteven Rostedt (Red Hat)
29056071c22eSSteven Rostedt (Red Hat)    # crappy Perl way to pass in hashes.
29066071c22eSSteven Rostedt (Red Hat)    my %a = %{$pa};
29076071c22eSSteven Rostedt (Red Hat)    my %b = %{$pb};
29086071c22eSSteven Rostedt (Red Hat)
29096071c22eSSteven Rostedt (Red Hat)    foreach my $item (keys %b) {
29106071c22eSSteven Rostedt (Red Hat)	if (!defined($a{$item})) {
29116071c22eSSteven Rostedt (Red Hat)	    return 1;
29126071c22eSSteven Rostedt (Red Hat)	}
29136071c22eSSteven Rostedt (Red Hat)	if ($a{$item} ne $b{$item}) {
29146071c22eSSteven Rostedt (Red Hat)	    return 1;
29156071c22eSSteven Rostedt (Red Hat)	}
29166071c22eSSteven Rostedt (Red Hat)    }
29170a05c769SSteven Rostedt
29180a05c769SSteven Rostedt    foreach my $item (keys %a) {
29190a05c769SSteven Rostedt	if (!defined($b{$item})) {
29206071c22eSSteven Rostedt (Red Hat)	    return -1;
29210a05c769SSteven Rostedt	}
29220a05c769SSteven Rostedt    }
29230a05c769SSteven Rostedt
29240a05c769SSteven Rostedt    return 0;
29250a05c769SSteven Rostedt}
29260a05c769SSteven Rostedt
29270a05c769SSteven Rostedtsub run_config_bisect_test {
29280a05c769SSteven Rostedt    my ($type) = @_;
29290a05c769SSteven Rostedt
29304cc559bdSSteven Rostedt (Red Hat)    my $ret = run_bisect_test $type, "oldconfig";
29314cc559bdSSteven Rostedt (Red Hat)
29324cc559bdSSteven Rostedt (Red Hat)    if ($bisect_manual) {
29334cc559bdSSteven Rostedt (Red Hat)	$ret = answer_bisect;
29344cc559bdSSteven Rostedt (Red Hat)    }
29354cc559bdSSteven Rostedt (Red Hat)
29364cc559bdSSteven Rostedt (Red Hat)    return $ret;
29370a05c769SSteven Rostedt}
29380a05c769SSteven Rostedt
29390a05c769SSteven Rostedtsub process_failed {
29400a05c769SSteven Rostedt    my ($config) = @_;
29410a05c769SSteven Rostedt
29420a05c769SSteven Rostedt    doprint "\n\n***************************************\n";
29430a05c769SSteven Rostedt    doprint "Found bad config: $config\n";
29440a05c769SSteven Rostedt    doprint "***************************************\n\n";
29450a05c769SSteven Rostedt}
29460a05c769SSteven Rostedt
29476071c22eSSteven Rostedt (Red Hat)# used for config bisecting
29486071c22eSSteven Rostedt (Red Hat)my $good_config;
29496071c22eSSteven Rostedt (Red Hat)my $bad_config;
29500a05c769SSteven Rostedt
29516071c22eSSteven Rostedt (Red Hat)sub process_new_config {
29526071c22eSSteven Rostedt (Red Hat)    my ($tc, $nc, $gc, $bc) = @_;
29530a05c769SSteven Rostedt
29546071c22eSSteven Rostedt (Red Hat)    my %tmp_config = %{$tc};
29556071c22eSSteven Rostedt (Red Hat)    my %good_configs = %{$gc};
29566071c22eSSteven Rostedt (Red Hat)    my %bad_configs = %{$bc};
29570a05c769SSteven Rostedt
29586071c22eSSteven Rostedt (Red Hat)    my %new_configs;
29596071c22eSSteven Rostedt (Red Hat)
29606071c22eSSteven Rostedt (Red Hat)    my $runtest = 1;
29610a05c769SSteven Rostedt    my $ret;
29620a05c769SSteven Rostedt
29636071c22eSSteven Rostedt (Red Hat)    create_config "tmp_configs", \%tmp_config;
29646071c22eSSteven Rostedt (Red Hat)    assign_configs \%new_configs, $output_config;
29650a05c769SSteven Rostedt
29666071c22eSSteven Rostedt (Red Hat)    $ret = compare_configs \%new_configs, \%bad_configs;
29676071c22eSSteven Rostedt (Red Hat)    if (!$ret) {
29686071c22eSSteven Rostedt (Red Hat)	doprint "New config equals bad config, try next test\n";
29696071c22eSSteven Rostedt (Red Hat)	$runtest = 0;
29706071c22eSSteven Rostedt (Red Hat)    }
29710a05c769SSteven Rostedt
29726071c22eSSteven Rostedt (Red Hat)    if ($runtest) {
29736071c22eSSteven Rostedt (Red Hat)	$ret = compare_configs \%new_configs, \%good_configs;
29746071c22eSSteven Rostedt (Red Hat)	if (!$ret) {
29756071c22eSSteven Rostedt (Red Hat)	    doprint "New config equals good config, try next test\n";
29766071c22eSSteven Rostedt (Red Hat)	    $runtest = 0;
29776071c22eSSteven Rostedt (Red Hat)	}
29786071c22eSSteven Rostedt (Red Hat)    }
29790a05c769SSteven Rostedt
29806071c22eSSteven Rostedt (Red Hat)    %{$nc} = %new_configs;
29816071c22eSSteven Rostedt (Red Hat)
29826071c22eSSteven Rostedt (Red Hat)    return $runtest;
29836071c22eSSteven Rostedt (Red Hat)}
29846071c22eSSteven Rostedt (Red Hat)
29856071c22eSSteven Rostedt (Red Hat)sub run_config_bisect {
29866071c22eSSteven Rostedt (Red Hat)    my ($pgood, $pbad) = @_;
29876071c22eSSteven Rostedt (Red Hat)
29886071c22eSSteven Rostedt (Red Hat)    my $type = $config_bisect_type;
29896071c22eSSteven Rostedt (Red Hat)
29906071c22eSSteven Rostedt (Red Hat)    my %good_configs = %{$pgood};
29916071c22eSSteven Rostedt (Red Hat)    my %bad_configs = %{$pbad};
29926071c22eSSteven Rostedt (Red Hat)
29936071c22eSSteven Rostedt (Red Hat)    my %diff_configs = diff_config_vals \%good_configs, \%bad_configs;
29946071c22eSSteven Rostedt (Red Hat)    my %b_configs = diff_configs \%good_configs, \%bad_configs;
29956071c22eSSteven Rostedt (Red Hat)    my %g_configs = diff_configs \%bad_configs, \%good_configs;
29966071c22eSSteven Rostedt (Red Hat)
29976071c22eSSteven Rostedt (Red Hat)    my @diff_arr = keys %diff_configs;
29986071c22eSSteven Rostedt (Red Hat)    my $len_diff = $#diff_arr + 1;
29996071c22eSSteven Rostedt (Red Hat)
30006071c22eSSteven Rostedt (Red Hat)    my @b_arr = keys %b_configs;
30016071c22eSSteven Rostedt (Red Hat)    my $len_b = $#b_arr + 1;
30026071c22eSSteven Rostedt (Red Hat)
30036071c22eSSteven Rostedt (Red Hat)    my @g_arr = keys %g_configs;
30046071c22eSSteven Rostedt (Red Hat)    my $len_g = $#g_arr + 1;
30056071c22eSSteven Rostedt (Red Hat)
30066071c22eSSteven Rostedt (Red Hat)    my $runtest = 1;
30076071c22eSSteven Rostedt (Red Hat)    my %new_configs;
30086071c22eSSteven Rostedt (Red Hat)    my $ret;
30096071c22eSSteven Rostedt (Red Hat)
30106071c22eSSteven Rostedt (Red Hat)    # First, lets get it down to a single subset.
30116071c22eSSteven Rostedt (Red Hat)    # Is the problem with a difference in values?
30126071c22eSSteven Rostedt (Red Hat)    # Is the problem with a missing config?
30136071c22eSSteven Rostedt (Red Hat)    # Is the problem with a config that breaks things?
30146071c22eSSteven Rostedt (Red Hat)
30156071c22eSSteven Rostedt (Red Hat)    # Enable all of one set and see if we get a new bad
30166071c22eSSteven Rostedt (Red Hat)    # or good config.
30176071c22eSSteven Rostedt (Red Hat)
30186071c22eSSteven Rostedt (Red Hat)    # first set the good config to the bad values.
30196071c22eSSteven Rostedt (Red Hat)
30206071c22eSSteven Rostedt (Red Hat)    doprint "d=$len_diff g=$len_g b=$len_b\n";
30216071c22eSSteven Rostedt (Red Hat)
30226071c22eSSteven Rostedt (Red Hat)    # first lets enable things in bad config that are enabled in good config
30236071c22eSSteven Rostedt (Red Hat)
30246071c22eSSteven Rostedt (Red Hat)    if ($len_diff > 0) {
30256071c22eSSteven Rostedt (Red Hat)	if ($len_b > 0 || $len_g > 0) {
30266071c22eSSteven Rostedt (Red Hat)	    my %tmp_config = %bad_configs;
30276071c22eSSteven Rostedt (Red Hat)
30286071c22eSSteven Rostedt (Red Hat)	    doprint "Set tmp config to be bad config with good config values\n";
30296071c22eSSteven Rostedt (Red Hat)	    foreach my $item (@diff_arr) {
30306071c22eSSteven Rostedt (Red Hat)		$tmp_config{$item} = $good_configs{$item};
30316071c22eSSteven Rostedt (Red Hat)	    }
30326071c22eSSteven Rostedt (Red Hat)
30336071c22eSSteven Rostedt (Red Hat)	    $runtest = process_new_config \%tmp_config, \%new_configs,
30346071c22eSSteven Rostedt (Red Hat)			    \%good_configs, \%bad_configs;
30356071c22eSSteven Rostedt (Red Hat)	}
30366071c22eSSteven Rostedt (Red Hat)    }
30376071c22eSSteven Rostedt (Red Hat)
30386071c22eSSteven Rostedt (Red Hat)    if (!$runtest && $len_diff > 0) {
30396071c22eSSteven Rostedt (Red Hat)
30406071c22eSSteven Rostedt (Red Hat)	if ($len_diff == 1) {
30414186cb45SSteven Rostedt (Red Hat)	    process_failed $diff_arr[0];
30426071c22eSSteven Rostedt (Red Hat)	    return 1;
30436071c22eSSteven Rostedt (Red Hat)	}
30446071c22eSSteven Rostedt (Red Hat)	my %tmp_config = %bad_configs;
30456071c22eSSteven Rostedt (Red Hat)
30466071c22eSSteven Rostedt (Red Hat)	my $half = int($#diff_arr / 2);
30476071c22eSSteven Rostedt (Red Hat)	my @tophalf = @diff_arr[0 .. $half];
30486071c22eSSteven Rostedt (Red Hat)
30496071c22eSSteven Rostedt (Red Hat)	doprint "Settings bisect with top half:\n";
30506071c22eSSteven Rostedt (Red Hat)	doprint "Set tmp config to be bad config with some good config values\n";
30516071c22eSSteven Rostedt (Red Hat)	foreach my $item (@tophalf) {
30526071c22eSSteven Rostedt (Red Hat)	    $tmp_config{$item} = $good_configs{$item};
30536071c22eSSteven Rostedt (Red Hat)	}
30546071c22eSSteven Rostedt (Red Hat)
30556071c22eSSteven Rostedt (Red Hat)	$runtest = process_new_config \%tmp_config, \%new_configs,
30566071c22eSSteven Rostedt (Red Hat)			    \%good_configs, \%bad_configs;
30576071c22eSSteven Rostedt (Red Hat)
30586071c22eSSteven Rostedt (Red Hat)	if (!$runtest) {
30596071c22eSSteven Rostedt (Red Hat)	    my %tmp_config = %bad_configs;
30606071c22eSSteven Rostedt (Red Hat)
30616071c22eSSteven Rostedt (Red Hat)	    doprint "Try bottom half\n";
30626071c22eSSteven Rostedt (Red Hat)
30636071c22eSSteven Rostedt (Red Hat)	    my @bottomhalf = @diff_arr[$half+1 .. $#diff_arr];
30646071c22eSSteven Rostedt (Red Hat)
30656071c22eSSteven Rostedt (Red Hat)	    foreach my $item (@bottomhalf) {
30666071c22eSSteven Rostedt (Red Hat)		$tmp_config{$item} = $good_configs{$item};
30676071c22eSSteven Rostedt (Red Hat)	    }
30686071c22eSSteven Rostedt (Red Hat)
30696071c22eSSteven Rostedt (Red Hat)	    $runtest = process_new_config \%tmp_config, \%new_configs,
30706071c22eSSteven Rostedt (Red Hat)			    \%good_configs, \%bad_configs;
30716071c22eSSteven Rostedt (Red Hat)	}
30726071c22eSSteven Rostedt (Red Hat)    }
30736071c22eSSteven Rostedt (Red Hat)
30746071c22eSSteven Rostedt (Red Hat)    if ($runtest) {
30756071c22eSSteven Rostedt (Red Hat)	$ret = run_config_bisect_test $type;
30766071c22eSSteven Rostedt (Red Hat)	if ($ret) {
30776071c22eSSteven Rostedt (Red Hat)	    doprint "NEW GOOD CONFIG\n";
30786071c22eSSteven Rostedt (Red Hat)	    %good_configs = %new_configs;
30796071c22eSSteven Rostedt (Red Hat)	    run_command "mv $good_config ${good_config}.last";
30806071c22eSSteven Rostedt (Red Hat)	    save_config \%good_configs, $good_config;
30816071c22eSSteven Rostedt (Red Hat)	    %{$pgood} = %good_configs;
3082cf79fab6SSteven Rostedt	} else {
30836071c22eSSteven Rostedt (Red Hat)	    doprint "NEW BAD CONFIG\n";
30846071c22eSSteven Rostedt (Red Hat)	    %bad_configs = %new_configs;
30856071c22eSSteven Rostedt (Red Hat)	    run_command "mv $bad_config ${bad_config}.last";
30866071c22eSSteven Rostedt (Red Hat)	    save_config \%bad_configs, $bad_config;
30876071c22eSSteven Rostedt (Red Hat)	    %{$pbad} = %bad_configs;
30886071c22eSSteven Rostedt (Red Hat)	}
30896071c22eSSteven Rostedt (Red Hat)	return 0;
3090cf79fab6SSteven Rostedt    }
3091cf79fab6SSteven Rostedt
30926071c22eSSteven Rostedt (Red Hat)    fail "Hmm, need to do a mix match?\n";
30930a05c769SSteven Rostedt    return -1;
30940a05c769SSteven Rostedt}
30950a05c769SSteven Rostedt
30960a05c769SSteven Rostedtsub config_bisect {
30970a05c769SSteven Rostedt    my ($i) = @_;
30980a05c769SSteven Rostedt
30996071c22eSSteven Rostedt (Red Hat)    my $type = $config_bisect_type;
3100c4d1d11fSSteven Rostedt (Red Hat)    my $ret;
31010a05c769SSteven Rostedt
31026071c22eSSteven Rostedt (Red Hat)    $bad_config = $config_bisect;
31030a05c769SSteven Rostedt
310430f75da5SSteven Rostedt    if (defined($config_bisect_good)) {
31056071c22eSSteven Rostedt (Red Hat)	$good_config = $config_bisect_good;
31066071c22eSSteven Rostedt (Red Hat)    } elsif (defined($minconfig)) {
31076071c22eSSteven Rostedt (Red Hat)	$good_config = $minconfig;
31080a05c769SSteven Rostedt    } else {
31096071c22eSSteven Rostedt (Red Hat)	doprint "No config specified, checking if defconfig works";
3110c4d1d11fSSteven Rostedt (Red Hat)	$ret = run_bisect_test $type, "defconfig";
31116071c22eSSteven Rostedt (Red Hat)	if (!$ret) {
31126071c22eSSteven Rostedt (Red Hat)	    fail "Have no good config to compare with, please set CONFIG_BISECT_GOOD";
31136071c22eSSteven Rostedt (Red Hat)	    return 1;
31146071c22eSSteven Rostedt (Red Hat)	}
31156071c22eSSteven Rostedt (Red Hat)	$good_config = $output_config;
31160a05c769SSteven Rostedt    }
31170a05c769SSteven Rostedt
31186071c22eSSteven Rostedt (Red Hat)    # we don't want min configs to cause issues here.
31196071c22eSSteven Rostedt (Red Hat)    doprint "Disabling 'MIN_CONFIG' for this test\n";
31206071c22eSSteven Rostedt (Red Hat)    undef $minconfig;
31210a05c769SSteven Rostedt
31226071c22eSSteven Rostedt (Red Hat)    my %good_configs;
31236071c22eSSteven Rostedt (Red Hat)    my %bad_configs;
31246071c22eSSteven Rostedt (Red Hat)    my %tmp_configs;
31250a05c769SSteven Rostedt
31266071c22eSSteven Rostedt (Red Hat)    doprint "Run good configs through make oldconfig\n";
31276071c22eSSteven Rostedt (Red Hat)    assign_configs \%tmp_configs, $good_config;
31286071c22eSSteven Rostedt (Red Hat)    create_config "$good_config", \%tmp_configs;
31296071c22eSSteven Rostedt (Red Hat)    assign_configs \%good_configs, $output_config;
31300a05c769SSteven Rostedt
31316071c22eSSteven Rostedt (Red Hat)    doprint "Run bad configs through make oldconfig\n";
31326071c22eSSteven Rostedt (Red Hat)    assign_configs \%tmp_configs, $bad_config;
31336071c22eSSteven Rostedt (Red Hat)    create_config "$bad_config", \%tmp_configs;
31346071c22eSSteven Rostedt (Red Hat)    assign_configs \%bad_configs, $output_config;
31350a05c769SSteven Rostedt
31366071c22eSSteven Rostedt (Red Hat)    $good_config = "$tmpdir/good_config";
31376071c22eSSteven Rostedt (Red Hat)    $bad_config = "$tmpdir/bad_config";
31380a05c769SSteven Rostedt
31396071c22eSSteven Rostedt (Red Hat)    save_config \%good_configs, $good_config;
31406071c22eSSteven Rostedt (Red Hat)    save_config \%bad_configs, $bad_config;
31410a05c769SSteven Rostedt
3142c4d1d11fSSteven Rostedt (Red Hat)
3143c4d1d11fSSteven Rostedt (Red Hat)    if (defined($config_bisect_check) && $config_bisect_check ne "0") {
3144c4d1d11fSSteven Rostedt (Red Hat)	if ($config_bisect_check ne "good") {
3145c4d1d11fSSteven Rostedt (Red Hat)	    doprint "Testing bad config\n";
3146c4d1d11fSSteven Rostedt (Red Hat)
3147c4d1d11fSSteven Rostedt (Red Hat)	    $ret = run_bisect_test $type, "useconfig:$bad_config";
3148c4d1d11fSSteven Rostedt (Red Hat)	    if ($ret) {
3149c4d1d11fSSteven Rostedt (Red Hat)		fail "Bad config succeeded when expected to fail!";
3150c4d1d11fSSteven Rostedt (Red Hat)		return 0;
3151c4d1d11fSSteven Rostedt (Red Hat)	    }
3152c4d1d11fSSteven Rostedt (Red Hat)	}
3153c4d1d11fSSteven Rostedt (Red Hat)	if ($config_bisect_check ne "bad") {
3154c4d1d11fSSteven Rostedt (Red Hat)	    doprint "Testing good config\n";
3155c4d1d11fSSteven Rostedt (Red Hat)
3156c4d1d11fSSteven Rostedt (Red Hat)	    $ret = run_bisect_test $type, "useconfig:$good_config";
3157c4d1d11fSSteven Rostedt (Red Hat)	    if (!$ret) {
3158c4d1d11fSSteven Rostedt (Red Hat)		fail "Good config failed when expected to succeed!";
3159c4d1d11fSSteven Rostedt (Red Hat)		return 0;
3160c4d1d11fSSteven Rostedt (Red Hat)	    }
3161c4d1d11fSSteven Rostedt (Red Hat)	}
3162c4d1d11fSSteven Rostedt (Red Hat)    }
3163b0918612SSteven Rostedt
31640a05c769SSteven Rostedt    do {
31656071c22eSSteven Rostedt (Red Hat)	$ret = run_config_bisect \%good_configs, \%bad_configs;
31660a05c769SSteven Rostedt    } while (!$ret);
31670a05c769SSteven Rostedt
31680a05c769SSteven Rostedt    return $ret if ($ret < 0);
31695f9b6cedSSteven Rostedt
31705f9b6cedSSteven Rostedt    success $i;
31715f9b6cedSSteven Rostedt}
31725f9b6cedSSteven Rostedt
317327d934b2SSteven Rostedtsub patchcheck_reboot {
317427d934b2SSteven Rostedt    doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
3175bc7c5803SSteven Rostedt    reboot_to_good $patchcheck_sleep_time;
317627d934b2SSteven Rostedt}
317727d934b2SSteven Rostedt
31786c5ee0beSSteven Rostedtsub patchcheck {
31796c5ee0beSSteven Rostedt    my ($i) = @_;
31806c5ee0beSSteven Rostedt
31816c5ee0beSSteven Rostedt    die "PATCHCHECK_START[$i] not defined\n"
3182b5f4aea6SSteven Rostedt	if (!defined($patchcheck_start));
31836c5ee0beSSteven Rostedt    die "PATCHCHECK_TYPE[$i] not defined\n"
3184b5f4aea6SSteven Rostedt	if (!defined($patchcheck_type));
31856c5ee0beSSteven Rostedt
3186b5f4aea6SSteven Rostedt    my $start = $patchcheck_start;
31876c5ee0beSSteven Rostedt
318823a0e161SSteven Rostedt (Red Hat)    my $cherry = $patchcheck_cherry;
318923a0e161SSteven Rostedt (Red Hat)    if (!defined($cherry)) {
319023a0e161SSteven Rostedt (Red Hat)	$cherry = 0;
319123a0e161SSteven Rostedt (Red Hat)    }
319223a0e161SSteven Rostedt (Red Hat)
31936c5ee0beSSteven Rostedt    my $end = "HEAD";
3194b5f4aea6SSteven Rostedt    if (defined($patchcheck_end)) {
3195b5f4aea6SSteven Rostedt	$end = $patchcheck_end;
319623a0e161SSteven Rostedt (Red Hat)    } elsif ($cherry) {
319723a0e161SSteven Rostedt (Red Hat)	die "PATCHCHECK_END must be defined with PATCHCHECK_CHERRY\n";
31986c5ee0beSSteven Rostedt    }
31996c5ee0beSSteven Rostedt
3200a57419b3SSteven Rostedt    # Get the true sha1's since we can use things like HEAD~3
3201a57419b3SSteven Rostedt    $start = get_sha1($start);
3202a57419b3SSteven Rostedt    $end = get_sha1($end);
3203a57419b3SSteven Rostedt
3204b5f4aea6SSteven Rostedt    my $type = $patchcheck_type;
32056c5ee0beSSteven Rostedt
32066c5ee0beSSteven Rostedt    # Can't have a test without having a test to run
32076c5ee0beSSteven Rostedt    if ($type eq "test" && !defined($run_test)) {
32086c5ee0beSSteven Rostedt	$type = "boot";
32096c5ee0beSSteven Rostedt    }
32106c5ee0beSSteven Rostedt
321123a0e161SSteven Rostedt (Red Hat)    if ($cherry) {
321223a0e161SSteven Rostedt (Red Hat)	open (IN, "git cherry -v $start $end|") or
321323a0e161SSteven Rostedt (Red Hat)	    dodie "could not get git list";
321423a0e161SSteven Rostedt (Red Hat)    } else {
32156c5ee0beSSteven Rostedt	open (IN, "git log --pretty=oneline $end|") or
32166c5ee0beSSteven Rostedt	    dodie "could not get git list";
321723a0e161SSteven Rostedt (Red Hat)    }
32186c5ee0beSSteven Rostedt
32196c5ee0beSSteven Rostedt    my @list;
32206c5ee0beSSteven Rostedt
32216c5ee0beSSteven Rostedt    while (<IN>) {
32226c5ee0beSSteven Rostedt	chomp;
322323a0e161SSteven Rostedt (Red Hat)	# git cherry adds a '+' we want to remove
322423a0e161SSteven Rostedt (Red Hat)	s/^\+ //;
32256c5ee0beSSteven Rostedt	$list[$#list+1] = $_;
32266c5ee0beSSteven Rostedt	last if (/^$start/);
32276c5ee0beSSteven Rostedt    }
32286c5ee0beSSteven Rostedt    close(IN);
32296c5ee0beSSteven Rostedt
323023a0e161SSteven Rostedt (Red Hat)    if (!$cherry) {
32316c5ee0beSSteven Rostedt	if ($list[$#list] !~ /^$start/) {
32322b7d9b21SSteven Rostedt	    fail "SHA1 $start not found";
32336c5ee0beSSteven Rostedt	}
32346c5ee0beSSteven Rostedt
32356c5ee0beSSteven Rostedt	# go backwards in the list
32366c5ee0beSSteven Rostedt	@list = reverse @list;
323723a0e161SSteven Rostedt (Red Hat)    }
323823a0e161SSteven Rostedt (Red Hat)
323923a0e161SSteven Rostedt (Red Hat)    doprint("Going to test the following commits:\n");
324023a0e161SSteven Rostedt (Red Hat)    foreach my $l (@list) {
324123a0e161SSteven Rostedt (Red Hat)	doprint "$l\n";
324223a0e161SSteven Rostedt (Red Hat)    }
32436c5ee0beSSteven Rostedt
32446c5ee0beSSteven Rostedt    my $save_clean = $noclean;
32451990207dSSteven Rostedt    my %ignored_warnings;
32461990207dSSteven Rostedt
32471990207dSSteven Rostedt    if (defined($ignore_warnings)) {
32481990207dSSteven Rostedt	foreach my $sha1 (split /\s+/, $ignore_warnings) {
32491990207dSSteven Rostedt	    $ignored_warnings{$sha1} = 1;
32501990207dSSteven Rostedt	}
32511990207dSSteven Rostedt    }
32526c5ee0beSSteven Rostedt
32536c5ee0beSSteven Rostedt    $in_patchcheck = 1;
32546c5ee0beSSteven Rostedt    foreach my $item (@list) {
32556c5ee0beSSteven Rostedt	my $sha1 = $item;
32566c5ee0beSSteven Rostedt	$sha1 =~ s/^([[:xdigit:]]+).*/$1/;
32576c5ee0beSSteven Rostedt
32586c5ee0beSSteven Rostedt	doprint "\nProcessing commit $item\n\n";
32596c5ee0beSSteven Rostedt
32606c5ee0beSSteven Rostedt	run_command "git checkout $sha1" or
32616c5ee0beSSteven Rostedt	    die "Failed to checkout $sha1";
32626c5ee0beSSteven Rostedt
32636c5ee0beSSteven Rostedt	# only clean on the first and last patch
32646c5ee0beSSteven Rostedt	if ($item eq $list[0] ||
32656c5ee0beSSteven Rostedt	    $item eq $list[$#list]) {
32666c5ee0beSSteven Rostedt	    $noclean = $save_clean;
32676c5ee0beSSteven Rostedt	} else {
32686c5ee0beSSteven Rostedt	    $noclean = 1;
32696c5ee0beSSteven Rostedt	}
32706c5ee0beSSteven Rostedt
32716c5ee0beSSteven Rostedt	if (defined($minconfig)) {
32722b7d9b21SSteven Rostedt	    build "useconfig:$minconfig" or return 0;
32736c5ee0beSSteven Rostedt	} else {
32746c5ee0beSSteven Rostedt	    # ?? no config to use?
32752b7d9b21SSteven Rostedt	    build "oldconfig" or return 0;
32766c5ee0beSSteven Rostedt	}
32776c5ee0beSSteven Rostedt
32784283b169SSteven Rostedt (Red Hat)	# No need to do per patch checking if warnings file exists
32794283b169SSteven Rostedt (Red Hat)	if (!defined($warnings_file) && !defined($ignored_warnings{$sha1})) {
32804283b169SSteven Rostedt (Red Hat)	    check_patch_buildlog $sha1 or return 0;
32811990207dSSteven Rostedt	}
32826c5ee0beSSteven Rostedt
32834283b169SSteven Rostedt (Red Hat)	check_buildlog or return 0;
32844283b169SSteven Rostedt (Red Hat)
32856c5ee0beSSteven Rostedt	next if ($type eq "build");
32866c5ee0beSSteven Rostedt
32877faafbd6SSteven Rostedt	my $failed = 0;
32887faafbd6SSteven Rostedt
3289ddf607e5SSteven Rostedt	start_monitor_and_boot or $failed = 1;
32907faafbd6SSteven Rostedt
32917faafbd6SSteven Rostedt	if (!$failed && $type ne "boot"){
32927faafbd6SSteven Rostedt	    do_run_test or $failed = 1;
32937faafbd6SSteven Rostedt	}
32947faafbd6SSteven Rostedt	end_monitor;
32957faafbd6SSteven Rostedt	return 0 if ($failed);
32967faafbd6SSteven Rostedt
329727d934b2SSteven Rostedt	patchcheck_reboot;
329827d934b2SSteven Rostedt
32996c5ee0beSSteven Rostedt    }
33006c5ee0beSSteven Rostedt    $in_patchcheck = 0;
33016c5ee0beSSteven Rostedt    success $i;
33022b7d9b21SSteven Rostedt
33032b7d9b21SSteven Rostedt    return 1;
33046c5ee0beSSteven Rostedt}
33056c5ee0beSSteven Rostedt
3306b9066f6cSSteven Rostedtmy %depends;
3307ac6974c7SSteven Rostedtmy %depcount;
3308b9066f6cSSteven Rostedtmy $iflevel = 0;
3309b9066f6cSSteven Rostedtmy @ifdeps;
3310b9066f6cSSteven Rostedt
3311b9066f6cSSteven Rostedt# prevent recursion
3312b9066f6cSSteven Rostedtmy %read_kconfigs;
3313b9066f6cSSteven Rostedt
3314ac6974c7SSteven Rostedtsub add_dep {
3315ac6974c7SSteven Rostedt    # $config depends on $dep
3316ac6974c7SSteven Rostedt    my ($config, $dep) = @_;
3317ac6974c7SSteven Rostedt
3318ac6974c7SSteven Rostedt    if (defined($depends{$config})) {
3319ac6974c7SSteven Rostedt	$depends{$config} .= " " . $dep;
3320ac6974c7SSteven Rostedt    } else {
3321ac6974c7SSteven Rostedt	$depends{$config} = $dep;
3322ac6974c7SSteven Rostedt    }
3323ac6974c7SSteven Rostedt
3324ac6974c7SSteven Rostedt    # record the number of configs depending on $dep
3325ac6974c7SSteven Rostedt    if (defined $depcount{$dep}) {
3326ac6974c7SSteven Rostedt	$depcount{$dep}++;
3327ac6974c7SSteven Rostedt    } else {
3328ac6974c7SSteven Rostedt	$depcount{$dep} = 1;
3329ac6974c7SSteven Rostedt    }
3330ac6974c7SSteven Rostedt}
3331ac6974c7SSteven Rostedt
3332b9066f6cSSteven Rostedt# taken from streamline_config.pl
3333b9066f6cSSteven Rostedtsub read_kconfig {
3334b9066f6cSSteven Rostedt    my ($kconfig) = @_;
3335b9066f6cSSteven Rostedt
3336b9066f6cSSteven Rostedt    my $state = "NONE";
3337b9066f6cSSteven Rostedt    my $config;
3338b9066f6cSSteven Rostedt    my @kconfigs;
3339b9066f6cSSteven Rostedt
3340b9066f6cSSteven Rostedt    my $cont = 0;
3341b9066f6cSSteven Rostedt    my $line;
3342b9066f6cSSteven Rostedt
3343b9066f6cSSteven Rostedt
3344b9066f6cSSteven Rostedt    if (! -f $kconfig) {
3345b9066f6cSSteven Rostedt	doprint "file $kconfig does not exist, skipping\n";
3346b9066f6cSSteven Rostedt	return;
3347b9066f6cSSteven Rostedt    }
3348b9066f6cSSteven Rostedt
3349b9066f6cSSteven Rostedt    open(KIN, "$kconfig")
3350b9066f6cSSteven Rostedt	or die "Can't open $kconfig";
3351b9066f6cSSteven Rostedt    while (<KIN>) {
3352b9066f6cSSteven Rostedt	chomp;
3353b9066f6cSSteven Rostedt
3354b9066f6cSSteven Rostedt	# Make sure that lines ending with \ continue
3355b9066f6cSSteven Rostedt	if ($cont) {
3356b9066f6cSSteven Rostedt	    $_ = $line . " " . $_;
3357b9066f6cSSteven Rostedt	}
3358b9066f6cSSteven Rostedt
3359b9066f6cSSteven Rostedt	if (s/\\$//) {
3360b9066f6cSSteven Rostedt	    $cont = 1;
3361b9066f6cSSteven Rostedt	    $line = $_;
3362b9066f6cSSteven Rostedt	    next;
3363b9066f6cSSteven Rostedt	}
3364b9066f6cSSteven Rostedt
3365b9066f6cSSteven Rostedt	$cont = 0;
3366b9066f6cSSteven Rostedt
3367b9066f6cSSteven Rostedt	# collect any Kconfig sources
3368b9066f6cSSteven Rostedt	if (/^source\s*"(.*)"/) {
3369b9066f6cSSteven Rostedt	    $kconfigs[$#kconfigs+1] = $1;
3370b9066f6cSSteven Rostedt	}
3371b9066f6cSSteven Rostedt
3372b9066f6cSSteven Rostedt	# configs found
3373b9066f6cSSteven Rostedt	if (/^\s*(menu)?config\s+(\S+)\s*$/) {
3374b9066f6cSSteven Rostedt	    $state = "NEW";
3375b9066f6cSSteven Rostedt	    $config = $2;
3376b9066f6cSSteven Rostedt
3377b9066f6cSSteven Rostedt	    for (my $i = 0; $i < $iflevel; $i++) {
3378ac6974c7SSteven Rostedt		add_dep $config, $ifdeps[$i];
3379b9066f6cSSteven Rostedt	    }
3380b9066f6cSSteven Rostedt
3381b9066f6cSSteven Rostedt	# collect the depends for the config
3382b9066f6cSSteven Rostedt	} elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
3383b9066f6cSSteven Rostedt
3384ac6974c7SSteven Rostedt	    add_dep $config, $1;
3385b9066f6cSSteven Rostedt
3386b9066f6cSSteven Rostedt	# Get the configs that select this config
3387ac6974c7SSteven Rostedt	} elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
3388ac6974c7SSteven Rostedt
3389ac6974c7SSteven Rostedt	    # selected by depends on config
3390ac6974c7SSteven Rostedt	    add_dep $1, $config;
3391b9066f6cSSteven Rostedt
3392b9066f6cSSteven Rostedt	# Check for if statements
3393b9066f6cSSteven Rostedt	} elsif (/^if\s+(.*\S)\s*$/) {
3394b9066f6cSSteven Rostedt	    my $deps = $1;
3395b9066f6cSSteven Rostedt	    # remove beginning and ending non text
3396b9066f6cSSteven Rostedt	    $deps =~ s/^[^a-zA-Z0-9_]*//;
3397b9066f6cSSteven Rostedt	    $deps =~ s/[^a-zA-Z0-9_]*$//;
3398b9066f6cSSteven Rostedt
3399b9066f6cSSteven Rostedt	    my @deps = split /[^a-zA-Z0-9_]+/, $deps;
3400b9066f6cSSteven Rostedt
3401b9066f6cSSteven Rostedt	    $ifdeps[$iflevel++] = join ':', @deps;
3402b9066f6cSSteven Rostedt
3403b9066f6cSSteven Rostedt	} elsif (/^endif/) {
3404b9066f6cSSteven Rostedt
3405b9066f6cSSteven Rostedt	    $iflevel-- if ($iflevel);
3406b9066f6cSSteven Rostedt
3407b9066f6cSSteven Rostedt	# stop on "help"
3408b9066f6cSSteven Rostedt	} elsif (/^\s*help\s*$/) {
3409b9066f6cSSteven Rostedt	    $state = "NONE";
3410b9066f6cSSteven Rostedt	}
3411b9066f6cSSteven Rostedt    }
3412b9066f6cSSteven Rostedt    close(KIN);
3413b9066f6cSSteven Rostedt
3414b9066f6cSSteven Rostedt    # read in any configs that were found.
3415b9066f6cSSteven Rostedt    foreach $kconfig (@kconfigs) {
3416b9066f6cSSteven Rostedt	if (!defined($read_kconfigs{$kconfig})) {
3417b9066f6cSSteven Rostedt	    $read_kconfigs{$kconfig} = 1;
3418b9066f6cSSteven Rostedt	    read_kconfig("$builddir/$kconfig");
3419b9066f6cSSteven Rostedt	}
3420b9066f6cSSteven Rostedt    }
3421b9066f6cSSteven Rostedt}
3422b9066f6cSSteven Rostedt
3423b9066f6cSSteven Rostedtsub read_depends {
3424b9066f6cSSteven Rostedt    # find out which arch this is by the kconfig file
3425b9066f6cSSteven Rostedt    open (IN, $output_config)
3426b9066f6cSSteven Rostedt	or dodie "Failed to read $output_config";
3427b9066f6cSSteven Rostedt    my $arch;
3428b9066f6cSSteven Rostedt    while (<IN>) {
3429b9066f6cSSteven Rostedt	if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
3430b9066f6cSSteven Rostedt	    $arch = $1;
3431b9066f6cSSteven Rostedt	    last;
3432b9066f6cSSteven Rostedt	}
3433b9066f6cSSteven Rostedt    }
3434b9066f6cSSteven Rostedt    close IN;
3435b9066f6cSSteven Rostedt
3436b9066f6cSSteven Rostedt    if (!defined($arch)) {
3437b9066f6cSSteven Rostedt	doprint "Could not find arch from config file\n";
3438b9066f6cSSteven Rostedt	doprint "no dependencies used\n";
3439b9066f6cSSteven Rostedt	return;
3440b9066f6cSSteven Rostedt    }
3441b9066f6cSSteven Rostedt
3442b9066f6cSSteven Rostedt    # arch is really the subarch, we need to know
3443b9066f6cSSteven Rostedt    # what directory to look at.
3444b9066f6cSSteven Rostedt    if ($arch eq "i386" || $arch eq "x86_64") {
3445b9066f6cSSteven Rostedt	$arch = "x86";
3446b9066f6cSSteven Rostedt    } elsif ($arch =~ /^tile/) {
3447b9066f6cSSteven Rostedt	$arch = "tile";
3448b9066f6cSSteven Rostedt    }
3449b9066f6cSSteven Rostedt
3450b9066f6cSSteven Rostedt    my $kconfig = "$builddir/arch/$arch/Kconfig";
3451b9066f6cSSteven Rostedt
3452b9066f6cSSteven Rostedt    if (! -f $kconfig && $arch =~ /\d$/) {
3453b9066f6cSSteven Rostedt	my $orig = $arch;
3454b9066f6cSSteven Rostedt 	# some subarchs have numbers, truncate them
3455b9066f6cSSteven Rostedt	$arch =~ s/\d*$//;
3456b9066f6cSSteven Rostedt	$kconfig = "$builddir/arch/$arch/Kconfig";
3457b9066f6cSSteven Rostedt	if (! -f $kconfig) {
3458b9066f6cSSteven Rostedt	    doprint "No idea what arch dir $orig is for\n";
3459b9066f6cSSteven Rostedt	    doprint "no dependencies used\n";
3460b9066f6cSSteven Rostedt	    return;
3461b9066f6cSSteven Rostedt	}
3462b9066f6cSSteven Rostedt    }
3463b9066f6cSSteven Rostedt
3464b9066f6cSSteven Rostedt    read_kconfig($kconfig);
3465b9066f6cSSteven Rostedt}
3466b9066f6cSSteven Rostedt
34674c4ab120SSteven Rostedtsub make_new_config {
34684c4ab120SSteven Rostedt    my @configs = @_;
34694c4ab120SSteven Rostedt
34704c4ab120SSteven Rostedt    open (OUT, ">$output_config")
34714c4ab120SSteven Rostedt	or dodie "Failed to write $output_config";
34724c4ab120SSteven Rostedt
34734c4ab120SSteven Rostedt    foreach my $config (@configs) {
34744c4ab120SSteven Rostedt	print OUT "$config\n";
34754c4ab120SSteven Rostedt    }
34764c4ab120SSteven Rostedt    close OUT;
34774c4ab120SSteven Rostedt}
34784c4ab120SSteven Rostedt
3479ac6974c7SSteven Rostedtsub chomp_config {
3480ac6974c7SSteven Rostedt    my ($config) = @_;
3481ac6974c7SSteven Rostedt
3482ac6974c7SSteven Rostedt    $config =~ s/CONFIG_//;
3483ac6974c7SSteven Rostedt
3484ac6974c7SSteven Rostedt    return $config;
3485ac6974c7SSteven Rostedt}
3486ac6974c7SSteven Rostedt
3487b9066f6cSSteven Rostedtsub get_depends {
3488b9066f6cSSteven Rostedt    my ($dep) = @_;
3489b9066f6cSSteven Rostedt
3490ac6974c7SSteven Rostedt    my $kconfig = chomp_config $dep;
3491b9066f6cSSteven Rostedt
3492b9066f6cSSteven Rostedt    $dep = $depends{"$kconfig"};
3493b9066f6cSSteven Rostedt
3494b9066f6cSSteven Rostedt    # the dep string we have saves the dependencies as they
3495b9066f6cSSteven Rostedt    # were found, including expressions like ! && ||. We
3496b9066f6cSSteven Rostedt    # want to split this out into just an array of configs.
3497b9066f6cSSteven Rostedt
3498b9066f6cSSteven Rostedt    my $valid = "A-Za-z_0-9";
3499b9066f6cSSteven Rostedt
3500b9066f6cSSteven Rostedt    my @configs;
3501b9066f6cSSteven Rostedt
3502b9066f6cSSteven Rostedt    while ($dep =~ /[$valid]/) {
3503b9066f6cSSteven Rostedt
3504b9066f6cSSteven Rostedt	if ($dep =~ /^[^$valid]*([$valid]+)/) {
3505b9066f6cSSteven Rostedt	    my $conf = "CONFIG_" . $1;
3506b9066f6cSSteven Rostedt
3507b9066f6cSSteven Rostedt	    $configs[$#configs + 1] = $conf;
3508b9066f6cSSteven Rostedt
3509b9066f6cSSteven Rostedt	    $dep =~ s/^[^$valid]*[$valid]+//;
3510b9066f6cSSteven Rostedt	} else {
3511b9066f6cSSteven Rostedt	    die "this should never happen";
3512b9066f6cSSteven Rostedt	}
3513b9066f6cSSteven Rostedt    }
3514b9066f6cSSteven Rostedt
3515b9066f6cSSteven Rostedt    return @configs;
3516b9066f6cSSteven Rostedt}
3517b9066f6cSSteven Rostedt
3518b9066f6cSSteven Rostedtmy %min_configs;
3519b9066f6cSSteven Rostedtmy %keep_configs;
352043d1b651SSteven Rostedtmy %save_configs;
3521b9066f6cSSteven Rostedtmy %processed_configs;
3522b9066f6cSSteven Rostedtmy %nochange_config;
3523b9066f6cSSteven Rostedt
3524b9066f6cSSteven Rostedtsub test_this_config {
3525b9066f6cSSteven Rostedt    my ($config) = @_;
3526b9066f6cSSteven Rostedt
3527b9066f6cSSteven Rostedt    my $found;
3528b9066f6cSSteven Rostedt
3529b9066f6cSSteven Rostedt    # if we already processed this config, skip it
3530b9066f6cSSteven Rostedt    if (defined($processed_configs{$config})) {
3531b9066f6cSSteven Rostedt	return undef;
3532b9066f6cSSteven Rostedt    }
3533b9066f6cSSteven Rostedt    $processed_configs{$config} = 1;
3534b9066f6cSSteven Rostedt
3535b9066f6cSSteven Rostedt    # if this config failed during this round, skip it
3536b9066f6cSSteven Rostedt    if (defined($nochange_config{$config})) {
3537b9066f6cSSteven Rostedt	return undef;
3538b9066f6cSSteven Rostedt    }
3539b9066f6cSSteven Rostedt
3540ac6974c7SSteven Rostedt    my $kconfig = chomp_config $config;
3541b9066f6cSSteven Rostedt
3542b9066f6cSSteven Rostedt    # Test dependencies first
3543b9066f6cSSteven Rostedt    if (defined($depends{"$kconfig"})) {
3544b9066f6cSSteven Rostedt	my @parents = get_depends $config;
3545b9066f6cSSteven Rostedt	foreach my $parent (@parents) {
3546b9066f6cSSteven Rostedt	    # if the parent is in the min config, check it first
3547b9066f6cSSteven Rostedt	    next if (!defined($min_configs{$parent}));
3548b9066f6cSSteven Rostedt	    $found = test_this_config($parent);
3549b9066f6cSSteven Rostedt	    if (defined($found)) {
3550b9066f6cSSteven Rostedt		return $found;
3551b9066f6cSSteven Rostedt	    }
3552b9066f6cSSteven Rostedt	}
3553b9066f6cSSteven Rostedt    }
3554b9066f6cSSteven Rostedt
3555b9066f6cSSteven Rostedt    # Remove this config from the list of configs
3556fb16d891SAdam Lee    # do a make olddefconfig and then read the resulting
3557b9066f6cSSteven Rostedt    # .config to make sure it is missing the config that
3558b9066f6cSSteven Rostedt    # we had before
3559b9066f6cSSteven Rostedt    my %configs = %min_configs;
3560b9066f6cSSteven Rostedt    delete $configs{$config};
3561b9066f6cSSteven Rostedt    make_new_config ((values %configs), (values %keep_configs));
3562b9066f6cSSteven Rostedt    make_oldconfig;
3563b9066f6cSSteven Rostedt    undef %configs;
3564b9066f6cSSteven Rostedt    assign_configs \%configs, $output_config;
3565b9066f6cSSteven Rostedt
3566b9066f6cSSteven Rostedt    return $config if (!defined($configs{$config}));
3567b9066f6cSSteven Rostedt
3568b9066f6cSSteven Rostedt    doprint "disabling config $config did not change .config\n";
3569b9066f6cSSteven Rostedt
3570b9066f6cSSteven Rostedt    $nochange_config{$config} = 1;
3571b9066f6cSSteven Rostedt
3572b9066f6cSSteven Rostedt    return undef;
3573b9066f6cSSteven Rostedt}
3574b9066f6cSSteven Rostedt
35754c4ab120SSteven Rostedtsub make_min_config {
35764c4ab120SSteven Rostedt    my ($i) = @_;
35774c4ab120SSteven Rostedt
3578ccc513b6SSteven Rostedt    my $type = $minconfig_type;
3579ccc513b6SSteven Rostedt    if ($type ne "boot" && $type ne "test") {
3580ccc513b6SSteven Rostedt	fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3581ccc513b6SSteven Rostedt	    " make_min_config works only with 'boot' and 'test'\n" and return;
3582ccc513b6SSteven Rostedt    }
3583ccc513b6SSteven Rostedt
35844c4ab120SSteven Rostedt    if (!defined($output_minconfig)) {
35854c4ab120SSteven Rostedt	fail "OUTPUT_MIN_CONFIG not defined" and return;
35864c4ab120SSteven Rostedt    }
358735ce5952SSteven Rostedt
358835ce5952SSteven Rostedt    # If output_minconfig exists, and the start_minconfig
358935ce5952SSteven Rostedt    # came from min_config, than ask if we should use
359035ce5952SSteven Rostedt    # that instead.
359135ce5952SSteven Rostedt    if (-f $output_minconfig && !$start_minconfig_defined) {
359235ce5952SSteven Rostedt	print "$output_minconfig exists\n";
359343de3316SSteven Rostedt	if (!defined($use_output_minconfig)) {
359435ce5952SSteven Rostedt	    if (read_yn " Use it as minconfig?") {
359535ce5952SSteven Rostedt		$start_minconfig = $output_minconfig;
359635ce5952SSteven Rostedt	    }
359743de3316SSteven Rostedt	} elsif ($use_output_minconfig > 0) {
359843de3316SSteven Rostedt	    doprint "Using $output_minconfig as MIN_CONFIG\n";
359943de3316SSteven Rostedt	    $start_minconfig = $output_minconfig;
360043de3316SSteven Rostedt	} else {
360143de3316SSteven Rostedt	    doprint "Set to still use MIN_CONFIG as starting point\n";
360243de3316SSteven Rostedt	}
360335ce5952SSteven Rostedt    }
360435ce5952SSteven Rostedt
36054c4ab120SSteven Rostedt    if (!defined($start_minconfig)) {
36064c4ab120SSteven Rostedt	fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
36074c4ab120SSteven Rostedt    }
36084c4ab120SSteven Rostedt
360935ce5952SSteven Rostedt    my $temp_config = "$tmpdir/temp_config";
361035ce5952SSteven Rostedt
36114c4ab120SSteven Rostedt    # First things first. We build an allnoconfig to find
36124c4ab120SSteven Rostedt    # out what the defaults are that we can't touch.
36134c4ab120SSteven Rostedt    # Some are selections, but we really can't handle selections.
36144c4ab120SSteven Rostedt
36154c4ab120SSteven Rostedt    my $save_minconfig = $minconfig;
36164c4ab120SSteven Rostedt    undef $minconfig;
36174c4ab120SSteven Rostedt
36184c4ab120SSteven Rostedt    run_command "$make allnoconfig" or return 0;
36194c4ab120SSteven Rostedt
3620b9066f6cSSteven Rostedt    read_depends;
3621b9066f6cSSteven Rostedt
36224c4ab120SSteven Rostedt    process_config_ignore $output_config;
3623b9066f6cSSteven Rostedt
362443d1b651SSteven Rostedt    undef %save_configs;
3625b9066f6cSSteven Rostedt    undef %min_configs;
36264c4ab120SSteven Rostedt
36274c4ab120SSteven Rostedt    if (defined($ignore_config)) {
36284c4ab120SSteven Rostedt	# make sure the file exists
36294c4ab120SSteven Rostedt	`touch $ignore_config`;
363043d1b651SSteven Rostedt	assign_configs \%save_configs, $ignore_config;
36314c4ab120SSteven Rostedt    }
36324c4ab120SSteven Rostedt
363343d1b651SSteven Rostedt    %keep_configs = %save_configs;
363443d1b651SSteven Rostedt
36354c4ab120SSteven Rostedt    doprint "Load initial configs from $start_minconfig\n";
36364c4ab120SSteven Rostedt
36374c4ab120SSteven Rostedt    # Look at the current min configs, and save off all the
36384c4ab120SSteven Rostedt    # ones that were set via the allnoconfig
36394c4ab120SSteven Rostedt    assign_configs \%min_configs, $start_minconfig;
36404c4ab120SSteven Rostedt
36414c4ab120SSteven Rostedt    my @config_keys = keys %min_configs;
36424c4ab120SSteven Rostedt
3643ac6974c7SSteven Rostedt    # All configs need a depcount
3644ac6974c7SSteven Rostedt    foreach my $config (@config_keys) {
3645ac6974c7SSteven Rostedt	my $kconfig = chomp_config $config;
3646ac6974c7SSteven Rostedt	if (!defined $depcount{$kconfig}) {
3647ac6974c7SSteven Rostedt		$depcount{$kconfig} = 0;
3648ac6974c7SSteven Rostedt	}
3649ac6974c7SSteven Rostedt    }
3650ac6974c7SSteven Rostedt
36514c4ab120SSteven Rostedt    # Remove anything that was set by the make allnoconfig
36524c4ab120SSteven Rostedt    # we shouldn't need them as they get set for us anyway.
36534c4ab120SSteven Rostedt    foreach my $config (@config_keys) {
36544c4ab120SSteven Rostedt	# Remove anything in the ignore_config
36554c4ab120SSteven Rostedt	if (defined($keep_configs{$config})) {
36564c4ab120SSteven Rostedt	    my $file = $ignore_config;
36574c4ab120SSteven Rostedt	    $file =~ s,.*/(.*?)$,$1,;
36584c4ab120SSteven Rostedt	    doprint "$config set by $file ... ignored\n";
36594c4ab120SSteven Rostedt	    delete $min_configs{$config};
36604c4ab120SSteven Rostedt	    next;
36614c4ab120SSteven Rostedt	}
36624c4ab120SSteven Rostedt	# But make sure the settings are the same. If a min config
36634c4ab120SSteven Rostedt	# sets a selection, we do not want to get rid of it if
36644c4ab120SSteven Rostedt	# it is not the same as what we have. Just move it into
36654c4ab120SSteven Rostedt	# the keep configs.
36664c4ab120SSteven Rostedt	if (defined($config_ignore{$config})) {
36674c4ab120SSteven Rostedt	    if ($config_ignore{$config} ne $min_configs{$config}) {
36684c4ab120SSteven Rostedt		doprint "$config is in allnoconfig as '$config_ignore{$config}'";
36694c4ab120SSteven Rostedt		doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
36704c4ab120SSteven Rostedt		$keep_configs{$config} = $min_configs{$config};
36714c4ab120SSteven Rostedt	    } else {
36724c4ab120SSteven Rostedt		doprint "$config set by allnoconfig ... ignored\n";
36734c4ab120SSteven Rostedt	    }
36744c4ab120SSteven Rostedt	    delete $min_configs{$config};
36754c4ab120SSteven Rostedt	}
36764c4ab120SSteven Rostedt    }
36774c4ab120SSteven Rostedt
36784c4ab120SSteven Rostedt    my $done = 0;
3679b9066f6cSSteven Rostedt    my $take_two = 0;
36804c4ab120SSteven Rostedt
36814c4ab120SSteven Rostedt    while (!$done) {
36824c4ab120SSteven Rostedt
36834c4ab120SSteven Rostedt	my $config;
36844c4ab120SSteven Rostedt	my $found;
36854c4ab120SSteven Rostedt
36864c4ab120SSteven Rostedt	# Now disable each config one by one and do a make oldconfig
36874c4ab120SSteven Rostedt	# till we find a config that changes our list.
36884c4ab120SSteven Rostedt
36894c4ab120SSteven Rostedt	my @test_configs = keys %min_configs;
3690ac6974c7SSteven Rostedt
3691ac6974c7SSteven Rostedt	# Sort keys by who is most dependent on
3692ac6974c7SSteven Rostedt	@test_configs = sort  { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3693ac6974c7SSteven Rostedt			  @test_configs ;
3694ac6974c7SSteven Rostedt
3695ac6974c7SSteven Rostedt	# Put configs that did not modify the config at the end.
36964c4ab120SSteven Rostedt	my $reset = 1;
36974c4ab120SSteven Rostedt	for (my $i = 0; $i < $#test_configs; $i++) {
36984c4ab120SSteven Rostedt	    if (!defined($nochange_config{$test_configs[0]})) {
36994c4ab120SSteven Rostedt		$reset = 0;
37004c4ab120SSteven Rostedt		last;
37014c4ab120SSteven Rostedt	    }
37024c4ab120SSteven Rostedt	    # This config didn't change the .config last time.
37034c4ab120SSteven Rostedt	    # Place it at the end
37044c4ab120SSteven Rostedt	    my $config = shift @test_configs;
37054c4ab120SSteven Rostedt	    push @test_configs, $config;
37064c4ab120SSteven Rostedt	}
37074c4ab120SSteven Rostedt
37084c4ab120SSteven Rostedt	# if every test config has failed to modify the .config file
37094c4ab120SSteven Rostedt	# in the past, then reset and start over.
37104c4ab120SSteven Rostedt	if ($reset) {
37114c4ab120SSteven Rostedt	    undef %nochange_config;
37124c4ab120SSteven Rostedt	}
37134c4ab120SSteven Rostedt
3714b9066f6cSSteven Rostedt	undef %processed_configs;
3715b9066f6cSSteven Rostedt
37164c4ab120SSteven Rostedt	foreach my $config (@test_configs) {
37174c4ab120SSteven Rostedt
3718b9066f6cSSteven Rostedt	    $found = test_this_config $config;
37194c4ab120SSteven Rostedt
3720b9066f6cSSteven Rostedt	    last if (defined($found));
37214c4ab120SSteven Rostedt
37224c4ab120SSteven Rostedt	    # oh well, try another config
37234c4ab120SSteven Rostedt	}
37244c4ab120SSteven Rostedt
37254c4ab120SSteven Rostedt	if (!defined($found)) {
3726b9066f6cSSteven Rostedt	    # we could have failed due to the nochange_config hash
3727b9066f6cSSteven Rostedt	    # reset and try again
3728b9066f6cSSteven Rostedt	    if (!$take_two) {
3729b9066f6cSSteven Rostedt		undef %nochange_config;
3730b9066f6cSSteven Rostedt		$take_two = 1;
3731b9066f6cSSteven Rostedt		next;
3732b9066f6cSSteven Rostedt	    }
37334c4ab120SSteven Rostedt	    doprint "No more configs found that we can disable\n";
37344c4ab120SSteven Rostedt	    $done = 1;
37354c4ab120SSteven Rostedt	    last;
37364c4ab120SSteven Rostedt	}
3737b9066f6cSSteven Rostedt	$take_two = 0;
37384c4ab120SSteven Rostedt
37394c4ab120SSteven Rostedt	$config = $found;
37404c4ab120SSteven Rostedt
37414c4ab120SSteven Rostedt	doprint "Test with $config disabled\n";
37424c4ab120SSteven Rostedt
37434c4ab120SSteven Rostedt	# set in_bisect to keep build and monitor from dieing
37444c4ab120SSteven Rostedt	$in_bisect = 1;
37454c4ab120SSteven Rostedt
37464c4ab120SSteven Rostedt	my $failed = 0;
3747bf1c95abSSteven Rostedt	build "oldconfig" or $failed = 1;
3748bf1c95abSSteven Rostedt	if (!$failed) {
37494c4ab120SSteven Rostedt		start_monitor_and_boot or $failed = 1;
3750ccc513b6SSteven Rostedt
3751ccc513b6SSteven Rostedt		if ($type eq "test" && !$failed) {
3752ccc513b6SSteven Rostedt		    do_run_test or $failed = 1;
3753ccc513b6SSteven Rostedt		}
3754ccc513b6SSteven Rostedt
37554c4ab120SSteven Rostedt		end_monitor;
3756bf1c95abSSteven Rostedt	}
37574c4ab120SSteven Rostedt
37584c4ab120SSteven Rostedt	$in_bisect = 0;
37594c4ab120SSteven Rostedt
37604c4ab120SSteven Rostedt	if ($failed) {
3761b9066f6cSSteven Rostedt	    doprint "$min_configs{$config} is needed to boot the box... keeping\n";
37624c4ab120SSteven Rostedt	    # this config is needed, add it to the ignore list.
37634c4ab120SSteven Rostedt	    $keep_configs{$config} = $min_configs{$config};
376443d1b651SSteven Rostedt	    $save_configs{$config} = $min_configs{$config};
37654c4ab120SSteven Rostedt	    delete $min_configs{$config};
376635ce5952SSteven Rostedt
376735ce5952SSteven Rostedt	    # update new ignore configs
376835ce5952SSteven Rostedt	    if (defined($ignore_config)) {
376935ce5952SSteven Rostedt		open (OUT, ">$temp_config")
377035ce5952SSteven Rostedt		    or die "Can't write to $temp_config";
377143d1b651SSteven Rostedt		foreach my $config (keys %save_configs) {
377243d1b651SSteven Rostedt		    print OUT "$save_configs{$config}\n";
377335ce5952SSteven Rostedt		}
377435ce5952SSteven Rostedt		close OUT;
377535ce5952SSteven Rostedt		run_command "mv $temp_config $ignore_config" or
377635ce5952SSteven Rostedt		    dodie "failed to copy update to $ignore_config";
377735ce5952SSteven Rostedt	    }
377835ce5952SSteven Rostedt
37794c4ab120SSteven Rostedt	} else {
37804c4ab120SSteven Rostedt	    # We booted without this config, remove it from the minconfigs.
37814c4ab120SSteven Rostedt	    doprint "$config is not needed, disabling\n";
37824c4ab120SSteven Rostedt
37834c4ab120SSteven Rostedt	    delete $min_configs{$config};
37844c4ab120SSteven Rostedt
37854c4ab120SSteven Rostedt	    # Also disable anything that is not enabled in this config
37864c4ab120SSteven Rostedt	    my %configs;
37874c4ab120SSteven Rostedt	    assign_configs \%configs, $output_config;
37884c4ab120SSteven Rostedt	    my @config_keys = keys %min_configs;
37894c4ab120SSteven Rostedt	    foreach my $config (@config_keys) {
37904c4ab120SSteven Rostedt		if (!defined($configs{$config})) {
37914c4ab120SSteven Rostedt		    doprint "$config is not set, disabling\n";
37924c4ab120SSteven Rostedt		    delete $min_configs{$config};
37934c4ab120SSteven Rostedt		}
37944c4ab120SSteven Rostedt	    }
37954c4ab120SSteven Rostedt
37964c4ab120SSteven Rostedt	    # Save off all the current mandidory configs
379735ce5952SSteven Rostedt	    open (OUT, ">$temp_config")
379835ce5952SSteven Rostedt		or die "Can't write to $temp_config";
37994c4ab120SSteven Rostedt	    foreach my $config (keys %keep_configs) {
38004c4ab120SSteven Rostedt		print OUT "$keep_configs{$config}\n";
38014c4ab120SSteven Rostedt	    }
38024c4ab120SSteven Rostedt	    foreach my $config (keys %min_configs) {
38034c4ab120SSteven Rostedt		print OUT "$min_configs{$config}\n";
38044c4ab120SSteven Rostedt	    }
38054c4ab120SSteven Rostedt	    close OUT;
380635ce5952SSteven Rostedt
380735ce5952SSteven Rostedt	    run_command "mv $temp_config $output_minconfig" or
380835ce5952SSteven Rostedt		dodie "failed to copy update to $output_minconfig";
38094c4ab120SSteven Rostedt	}
38104c4ab120SSteven Rostedt
38114c4ab120SSteven Rostedt	doprint "Reboot and wait $sleep_time seconds\n";
3812bc7c5803SSteven Rostedt	reboot_to_good $sleep_time;
38134c4ab120SSteven Rostedt    }
38144c4ab120SSteven Rostedt
38154c4ab120SSteven Rostedt    success $i;
38164c4ab120SSteven Rostedt    return 1;
38174c4ab120SSteven Rostedt}
38184c4ab120SSteven Rostedt
38194283b169SSteven Rostedt (Red Hat)sub make_warnings_file {
38204283b169SSteven Rostedt (Red Hat)    my ($i) = @_;
38214283b169SSteven Rostedt (Red Hat)
38224283b169SSteven Rostedt (Red Hat)    if (!defined($warnings_file)) {
38234283b169SSteven Rostedt (Red Hat)	dodie "Must define WARNINGS_FILE for make_warnings_file test";
38244283b169SSteven Rostedt (Red Hat)    }
38254283b169SSteven Rostedt (Red Hat)
38264283b169SSteven Rostedt (Red Hat)    if ($build_type eq "nobuild") {
38274283b169SSteven Rostedt (Red Hat)	dodie "BUILD_TYPE can not be 'nobuild' for make_warnings_file test";
38284283b169SSteven Rostedt (Red Hat)    }
38294283b169SSteven Rostedt (Red Hat)
38304283b169SSteven Rostedt (Red Hat)    build $build_type or dodie "Failed to build";
38314283b169SSteven Rostedt (Red Hat)
38324283b169SSteven Rostedt (Red Hat)    open(OUT, ">$warnings_file") or dodie "Can't create $warnings_file";
38334283b169SSteven Rostedt (Red Hat)
38344283b169SSteven Rostedt (Red Hat)    open(IN, $buildlog) or dodie "Can't open $buildlog";
38354283b169SSteven Rostedt (Red Hat)    while (<IN>) {
38364283b169SSteven Rostedt (Red Hat)
38374283b169SSteven Rostedt (Red Hat)	# Some compilers use UTF-8 extended for quotes
38384283b169SSteven Rostedt (Red Hat)	# for distcc heterogeneous systems, this causes issues
38394283b169SSteven Rostedt (Red Hat)	s/$utf8_quote/'/g;
38404283b169SSteven Rostedt (Red Hat)
38414283b169SSteven Rostedt (Red Hat)	if (/$check_build_re/) {
38424283b169SSteven Rostedt (Red Hat)	    print OUT;
38434283b169SSteven Rostedt (Red Hat)	}
38444283b169SSteven Rostedt (Red Hat)    }
38454283b169SSteven Rostedt (Red Hat)    close(IN);
38464283b169SSteven Rostedt (Red Hat)
38474283b169SSteven Rostedt (Red Hat)    close(OUT);
38484283b169SSteven Rostedt (Red Hat)
38494283b169SSteven Rostedt (Red Hat)    success $i;
38504283b169SSteven Rostedt (Red Hat)}
38514283b169SSteven Rostedt (Red Hat)
38525269faadSSatoru Takeuchi$#ARGV < 1 or die "ktest.pl version: $VERSION\n   usage: ktest.pl [config-file]\n";
38532545eb61SSteven Rostedt
38548d1491baSSteven Rostedtif ($#ARGV == 0) {
38558d1491baSSteven Rostedt    $ktest_config = $ARGV[0];
38568d1491baSSteven Rostedt    if (! -f $ktest_config) {
38578d1491baSSteven Rostedt	print "$ktest_config does not exist.\n";
385835ce5952SSteven Rostedt	if (!read_yn "Create it?") {
38598d1491baSSteven Rostedt	    exit 0;
38608d1491baSSteven Rostedt	}
38618d1491baSSteven Rostedt    }
38628d1491baSSteven Rostedt}
38638d1491baSSteven Rostedt
38648d1491baSSteven Rostedtif (! -f $ktest_config) {
3865dbd3783bSSteven Rostedt    $newconfig = 1;
3866c4261d0fSSteven Rostedt    get_test_case;
38678d1491baSSteven Rostedt    open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
38688d1491baSSteven Rostedt    print OUT << "EOF"
38698d1491baSSteven Rostedt# Generated by ktest.pl
38708d1491baSSteven Rostedt#
38710e7a22deSSteven Rostedt
38720e7a22deSSteven Rostedt# PWD is a ktest.pl variable that will result in the process working
38730e7a22deSSteven Rostedt# directory that ktest.pl is executed in.
38740e7a22deSSteven Rostedt
38750e7a22deSSteven Rostedt# THIS_DIR is automatically assigned the PWD of the path that generated
38760e7a22deSSteven Rostedt# the config file. It is best to use this variable when assigning other
38770e7a22deSSteven Rostedt# directory paths within this directory. This allows you to easily
38780e7a22deSSteven Rostedt# move the test cases to other locations or to other machines.
38790e7a22deSSteven Rostedt#
38800e7a22deSSteven RostedtTHIS_DIR := $variable{"PWD"}
38810e7a22deSSteven Rostedt
38828d1491baSSteven Rostedt# Define each test with TEST_START
38838d1491baSSteven Rostedt# The config options below it will override the defaults
38848d1491baSSteven RostedtTEST_START
3885c4261d0fSSteven RostedtTEST_TYPE = $default{"TEST_TYPE"}
38868d1491baSSteven Rostedt
38878d1491baSSteven RostedtDEFAULTS
38888d1491baSSteven RostedtEOF
38898d1491baSSteven Rostedt;
38908d1491baSSteven Rostedt    close(OUT);
38918d1491baSSteven Rostedt}
38928d1491baSSteven Rostedtread_config $ktest_config;
38938d1491baSSteven Rostedt
389423715c3cSSteven Rostedtif (defined($opt{"LOG_FILE"})) {
389504262be3SSteven Rostedt (Red Hat)    $opt{"LOG_FILE"} = eval_option("LOG_FILE", $opt{"LOG_FILE"}, -1);
389623715c3cSSteven Rostedt}
389723715c3cSSteven Rostedt
38988d1491baSSteven Rostedt# Append any configs entered in manually to the config file.
38998d1491baSSteven Rostedtmy @new_configs = keys %entered_configs;
39008d1491baSSteven Rostedtif ($#new_configs >= 0) {
39018d1491baSSteven Rostedt    print "\nAppending entered in configs to $ktest_config\n";
39028d1491baSSteven Rostedt    open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
39038d1491baSSteven Rostedt    foreach my $config (@new_configs) {
39048d1491baSSteven Rostedt	print OUT "$config = $entered_configs{$config}\n";
39050e7a22deSSteven Rostedt	$opt{$config} = process_variables($entered_configs{$config});
39068d1491baSSteven Rostedt    }
39078d1491baSSteven Rostedt}
39082545eb61SSteven Rostedt
39092b7d9b21SSteven Rostedtif ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
39102b7d9b21SSteven Rostedt    unlink $opt{"LOG_FILE"};
39112b7d9b21SSteven Rostedt}
39122545eb61SSteven Rostedt
39132b7d9b21SSteven Rostedtdoprint "\n\nSTARTING AUTOMATED TESTS\n\n";
39142b7d9b21SSteven Rostedt
3915a57419b3SSteven Rostedtfor (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3916a57419b3SSteven Rostedt
3917a57419b3SSteven Rostedt    if (!$i) {
3918a57419b3SSteven Rostedt	doprint "DEFAULT OPTIONS:\n";
3919a57419b3SSteven Rostedt    } else {
3920a57419b3SSteven Rostedt	doprint "\nTEST $i OPTIONS";
3921a57419b3SSteven Rostedt	if (defined($repeat_tests{$i})) {
3922a57419b3SSteven Rostedt	    $repeat = $repeat_tests{$i};
3923a57419b3SSteven Rostedt	    doprint " ITERATE $repeat";
3924a57419b3SSteven Rostedt	}
3925a57419b3SSteven Rostedt	doprint "\n";
3926a57419b3SSteven Rostedt    }
3927a57419b3SSteven Rostedt
39282b7d9b21SSteven Rostedt    foreach my $option (sort keys %opt) {
3929a57419b3SSteven Rostedt
3930a57419b3SSteven Rostedt	if ($option =~ /\[(\d+)\]$/) {
3931a57419b3SSteven Rostedt	    next if ($i != $1);
3932a57419b3SSteven Rostedt	} else {
3933a57419b3SSteven Rostedt	    next if ($i);
3934a57419b3SSteven Rostedt	}
3935a57419b3SSteven Rostedt
39362b7d9b21SSteven Rostedt	doprint "$option = $opt{$option}\n";
39372b7d9b21SSteven Rostedt    }
3938a57419b3SSteven Rostedt}
39392545eb61SSteven Rostedt
39402a62512bSSteven Rostedtsub __set_test_option {
39415a391fbfSSteven Rostedt    my ($name, $i) = @_;
39425a391fbfSSteven Rostedt
39435a391fbfSSteven Rostedt    my $option = "$name\[$i\]";
39445a391fbfSSteven Rostedt
39455a391fbfSSteven Rostedt    if (defined($opt{$option})) {
39465a391fbfSSteven Rostedt	return $opt{$option};
39475a391fbfSSteven Rostedt    }
39485a391fbfSSteven Rostedt
3949a57419b3SSteven Rostedt    foreach my $test (keys %repeat_tests) {
3950a57419b3SSteven Rostedt	if ($i >= $test &&
3951a57419b3SSteven Rostedt	    $i < $test + $repeat_tests{$test}) {
3952a57419b3SSteven Rostedt	    $option = "$name\[$test\]";
3953a57419b3SSteven Rostedt	    if (defined($opt{$option})) {
3954a57419b3SSteven Rostedt		return $opt{$option};
3955a57419b3SSteven Rostedt	    }
3956a57419b3SSteven Rostedt	}
3957a57419b3SSteven Rostedt    }
3958a57419b3SSteven Rostedt
39595a391fbfSSteven Rostedt    if (defined($opt{$name})) {
39605a391fbfSSteven Rostedt	return $opt{$name};
39615a391fbfSSteven Rostedt    }
39625a391fbfSSteven Rostedt
39635a391fbfSSteven Rostedt    return undef;
39645a391fbfSSteven Rostedt}
39655a391fbfSSteven Rostedt
39662a62512bSSteven Rostedtsub set_test_option {
39672a62512bSSteven Rostedt    my ($name, $i) = @_;
39682a62512bSSteven Rostedt
39692a62512bSSteven Rostedt    my $option = __set_test_option($name, $i);
39702a62512bSSteven Rostedt    return $option if (!defined($option));
39712a62512bSSteven Rostedt
397204262be3SSteven Rostedt (Red Hat)    return eval_option($name, $option, $i);
39732a62512bSSteven Rostedt}
39742a62512bSSteven Rostedt
39752545eb61SSteven Rostedt# First we need to do is the builds
3976a75fececSSteven Rostedtfor (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
39772545eb61SSteven Rostedt
39784ab1cce5SSteven Rostedt    # Do not reboot on failing test options
39794ab1cce5SSteven Rostedt    $no_reboot = 1;
3980759a3cc6SSteven Rostedt    $reboot_success = 0;
39814ab1cce5SSteven Rostedt
3982683a3e64SSteven Rostedt    $have_version = 0;
3983683a3e64SSteven Rostedt
3984576f627cSSteven Rostedt    $iteration = $i;
3985576f627cSSteven Rostedt
3986c1434dccSSteven Rostedt    undef %force_config;
3987c1434dccSSteven Rostedt
3988a75fececSSteven Rostedt    my $makecmd = set_test_option("MAKE_CMD", $i);
3989a75fececSSteven Rostedt
39908e80bf05SSteven Rostedt (Red Hat)    $outputdir = set_test_option("OUTPUT_DIR", $i);
39918e80bf05SSteven Rostedt (Red Hat)    $builddir = set_test_option("BUILD_DIR", $i);
39928e80bf05SSteven Rostedt (Red Hat)
39938e80bf05SSteven Rostedt (Red Hat)    chdir $builddir || die "can't change directory to $builddir";
39948e80bf05SSteven Rostedt (Red Hat)
39958e80bf05SSteven Rostedt (Red Hat)    if (!-d $outputdir) {
39968e80bf05SSteven Rostedt (Red Hat)	mkpath($outputdir) or
39978e80bf05SSteven Rostedt (Red Hat)	    die "can't create $outputdir";
39988e80bf05SSteven Rostedt (Red Hat)    }
39998e80bf05SSteven Rostedt (Red Hat)
40008e80bf05SSteven Rostedt (Red Hat)    $make = "$makecmd O=$outputdir";
40018e80bf05SSteven Rostedt (Red Hat)
40029cc9e091SSteven Rostedt    # Load all the options into their mapped variable names
40039cc9e091SSteven Rostedt    foreach my $opt (keys %option_map) {
40049cc9e091SSteven Rostedt	${$option_map{$opt}} = set_test_option($opt, $i);
40059cc9e091SSteven Rostedt    }
4006b5f4aea6SSteven Rostedt
400735ce5952SSteven Rostedt    $start_minconfig_defined = 1;
400835ce5952SSteven Rostedt
4009921ed4c7SSteven Rostedt    # The first test may override the PRE_KTEST option
4010921ed4c7SSteven Rostedt    if (defined($pre_ktest) && $i == 1) {
4011921ed4c7SSteven Rostedt	doprint "\n";
4012921ed4c7SSteven Rostedt	run_command $pre_ktest;
4013921ed4c7SSteven Rostedt    }
4014921ed4c7SSteven Rostedt
4015921ed4c7SSteven Rostedt    # Any test can override the POST_KTEST option
4016921ed4c7SSteven Rostedt    # The last test takes precedence.
4017921ed4c7SSteven Rostedt    if (defined($post_ktest)) {
4018921ed4c7SSteven Rostedt	$final_post_ktest = $post_ktest;
4019921ed4c7SSteven Rostedt    }
4020921ed4c7SSteven Rostedt
40214c4ab120SSteven Rostedt    if (!defined($start_minconfig)) {
402235ce5952SSteven Rostedt	$start_minconfig_defined = 0;
40234c4ab120SSteven Rostedt	$start_minconfig = $minconfig;
40244c4ab120SSteven Rostedt    }
40254c4ab120SSteven Rostedt
40268e80bf05SSteven Rostedt (Red Hat)    if (!-d $tmpdir) {
40278e80bf05SSteven Rostedt (Red Hat)	mkpath($tmpdir) or
40288e80bf05SSteven Rostedt (Red Hat)	    die "can't create $tmpdir";
4029a75fececSSteven Rostedt    }
4030a75fececSSteven Rostedt
4031e48c5293SSteven Rostedt    $ENV{"SSH_USER"} = $ssh_user;
4032e48c5293SSteven Rostedt    $ENV{"MACHINE"} = $machine;
4033e48c5293SSteven Rostedt
4034a75fececSSteven Rostedt    $buildlog = "$tmpdir/buildlog-$machine";
4035a9dd5d63SRabin Vincent    $testlog = "$tmpdir/testlog-$machine";
4036a75fececSSteven Rostedt    $dmesg = "$tmpdir/dmesg-$machine";
403751ad1dd1SSteven Rostedt    $output_config = "$outputdir/.config";
4038a75fececSSteven Rostedt
4039bb8474b1SSteven Rostedt    if (!$buildonly) {
4040bb8474b1SSteven Rostedt	$target = "$ssh_user\@$machine";
4041a75fececSSteven Rostedt	if ($reboot_type eq "grub") {
4042576f627cSSteven Rostedt	    dodie "GRUB_MENU not defined" if (!defined($grub_menu));
4043a15ba913SSteven Rostedt	} elsif ($reboot_type eq "grub2") {
4044a15ba913SSteven Rostedt	    dodie "GRUB_MENU not defined" if (!defined($grub_menu));
4045a15ba913SSteven Rostedt	    dodie "GRUB_FILE not defined" if (!defined($grub_file));
40467786954cSSteven Rostedt	} elsif ($reboot_type eq "syslinux") {
40477786954cSSteven Rostedt	    dodie "SYSLINUX_LABEL not defined" if (!defined($syslinux_label));
4048a75fececSSteven Rostedt	}
4049bb8474b1SSteven Rostedt    }
4050a75fececSSteven Rostedt
4051a75fececSSteven Rostedt    my $run_type = $build_type;
4052a75fececSSteven Rostedt    if ($test_type eq "patchcheck") {
4053b5f4aea6SSteven Rostedt	$run_type = $patchcheck_type;
4054a75fececSSteven Rostedt    } elsif ($test_type eq "bisect") {
4055b5f4aea6SSteven Rostedt	$run_type = $bisect_type;
40560a05c769SSteven Rostedt    } elsif ($test_type eq "config_bisect") {
4057b5f4aea6SSteven Rostedt	$run_type = $config_bisect_type;
40584283b169SSteven Rostedt (Red Hat)    } elsif ($test_type eq "make_min_config") {
40594283b169SSteven Rostedt (Red Hat)	$run_type = "";
40604283b169SSteven Rostedt (Red Hat)    } elsif ($test_type eq "make_warnings_file") {
40614c4ab120SSteven Rostedt	$run_type = "";
40624c4ab120SSteven Rostedt    }
40634c4ab120SSteven Rostedt
4064a75fececSSteven Rostedt    # mistake in config file?
4065a75fececSSteven Rostedt    if (!defined($run_type)) {
4066a75fececSSteven Rostedt	$run_type = "ERROR";
4067a75fececSSteven Rostedt    }
40682545eb61SSteven Rostedt
4069e0a8742eSSteven Rostedt    my $installme = "";
4070e0a8742eSSteven Rostedt    $installme = " no_install" if ($no_install);
4071e0a8742eSSteven Rostedt
40722545eb61SSteven Rostedt    doprint "\n\n";
4073e0a8742eSSteven Rostedt    doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
40747faafbd6SSteven Rostedt
4075921ed4c7SSteven Rostedt    if (defined($pre_test)) {
4076921ed4c7SSteven Rostedt	run_command $pre_test;
4077921ed4c7SSteven Rostedt    }
4078921ed4c7SSteven Rostedt
40797faafbd6SSteven Rostedt    unlink $dmesg;
40807faafbd6SSteven Rostedt    unlink $buildlog;
4081a9dd5d63SRabin Vincent    unlink $testlog;
40822545eb61SSteven Rostedt
4083250bae8bSSteven Rostedt    if (defined($addconfig)) {
4084250bae8bSSteven Rostedt	my $min = $minconfig;
40852b7d9b21SSteven Rostedt	if (!defined($minconfig)) {
4086250bae8bSSteven Rostedt	    $min = "";
4087250bae8bSSteven Rostedt	}
4088250bae8bSSteven Rostedt	run_command "cat $addconfig $min > $tmpdir/add_config" or
40892b7d9b21SSteven Rostedt	    dodie "Failed to create temp config";
40909be2e6b5SSteven Rostedt	$minconfig = "$tmpdir/add_config";
40912b7d9b21SSteven Rostedt    }
40922b7d9b21SSteven Rostedt
40936c5ee0beSSteven Rostedt    if (defined($checkout)) {
40946c5ee0beSSteven Rostedt	run_command "git checkout $checkout" or
40956c5ee0beSSteven Rostedt	    die "failed to checkout $checkout";
40966c5ee0beSSteven Rostedt    }
40976c5ee0beSSteven Rostedt
4098759a3cc6SSteven Rostedt    $no_reboot = 0;
4099759a3cc6SSteven Rostedt
4100648a182cSSteven Rostedt    # A test may opt to not reboot the box
4101648a182cSSteven Rostedt    if ($reboot_on_success) {
4102759a3cc6SSteven Rostedt	$reboot_success = 1;
4103648a182cSSteven Rostedt    }
41044ab1cce5SSteven Rostedt
4105a75fececSSteven Rostedt    if ($test_type eq "bisect") {
41065f9b6cedSSteven Rostedt	bisect $i;
41075f9b6cedSSteven Rostedt	next;
41080a05c769SSteven Rostedt    } elsif ($test_type eq "config_bisect") {
41090a05c769SSteven Rostedt	config_bisect $i;
41100a05c769SSteven Rostedt	next;
4111a75fececSSteven Rostedt    } elsif ($test_type eq "patchcheck") {
41126c5ee0beSSteven Rostedt	patchcheck $i;
41136c5ee0beSSteven Rostedt	next;
41144c4ab120SSteven Rostedt    } elsif ($test_type eq "make_min_config") {
41154c4ab120SSteven Rostedt	make_min_config $i;
41164c4ab120SSteven Rostedt	next;
41174283b169SSteven Rostedt (Red Hat)    } elsif ($test_type eq "make_warnings_file") {
41184283b169SSteven Rostedt (Red Hat)	$no_reboot = 1;
41194283b169SSteven Rostedt (Red Hat)	make_warnings_file $i;
41204283b169SSteven Rostedt (Red Hat)	next;
41215f9b6cedSSteven Rostedt    }
41225f9b6cedSSteven Rostedt
41237faafbd6SSteven Rostedt    if ($build_type ne "nobuild") {
41247faafbd6SSteven Rostedt	build $build_type or next;
41254283b169SSteven Rostedt (Red Hat)	check_buildlog or next;
41262545eb61SSteven Rostedt    }
41272545eb61SSteven Rostedt
4128cd8e368fSSteven Rostedt    if ($test_type eq "install") {
4129cd8e368fSSteven Rostedt	get_version;
4130cd8e368fSSteven Rostedt	install;
4131cd8e368fSSteven Rostedt	success $i;
4132cd8e368fSSteven Rostedt	next;
4133cd8e368fSSteven Rostedt    }
4134cd8e368fSSteven Rostedt
4135a75fececSSteven Rostedt    if ($test_type ne "build") {
41367faafbd6SSteven Rostedt	my $failed = 0;
4137ddf607e5SSteven Rostedt	start_monitor_and_boot or $failed = 1;
4138a75fececSSteven Rostedt
4139a75fececSSteven Rostedt	if (!$failed && $test_type ne "boot" && defined($run_test)) {
41407faafbd6SSteven Rostedt	    do_run_test or $failed = 1;
41415a391fbfSSteven Rostedt	}
41427faafbd6SSteven Rostedt	end_monitor;
41437faafbd6SSteven Rostedt	next if ($failed);
4144a75fececSSteven Rostedt    }
41455a391fbfSSteven Rostedt
41465f9b6cedSSteven Rostedt    success $i;
414775c3fda7SSteven Rostedt}
41482545eb61SSteven Rostedt
4149921ed4c7SSteven Rostedtif (defined($final_post_ktest)) {
4150921ed4c7SSteven Rostedt    run_command $final_post_ktest;
4151921ed4c7SSteven Rostedt}
4152921ed4c7SSteven Rostedt
41535c42fc5bSSteven Rostedtif ($opt{"POWEROFF_ON_SUCCESS"}) {
415475c3fda7SSteven Rostedt    halt;
4155759a3cc6SSteven Rostedt} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
4156bc7c5803SSteven Rostedt    reboot_to_good;
4157648a182cSSteven Rostedt} elsif (defined($switch_to_good)) {
4158648a182cSSteven Rostedt    # still need to get to the good kernel
4159648a182cSSteven Rostedt    run_command $switch_to_good;
41605c42fc5bSSteven Rostedt}
416175c3fda7SSteven Rostedt
4162648a182cSSteven Rostedt
4163e48c5293SSteven Rostedtdoprint "\n    $successes of $opt{NUM_TESTS} tests were successful\n\n";
4164e48c5293SSteven Rostedt
41652545eb61SSteven Rostedtexit 0;
4166