xref: /openbmc/linux/tools/testing/ktest/ktest.pl (revision 7b7fd0ac7dc1ffcaf24d9bca0f051b0168e43cd4)
12545eb61SSteven Rostedt#!/usr/bin/perl -w
24f19048fSThomas Gleixner# SPDX-License-Identifier: GPL-2.0-only
3d6ce2a0bSSteven Rostedt#
4cce1dac8SUwe Kleine-König# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
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;
1340667fb5SSteven Rostedt (VMware)use FindBin;
14eefb9d2bSSteven Rostedt (VMware)use IO::Handle;
152545eb61SSteven Rostedt
16e48c5293SSteven Rostedtmy $VERSION = "0.2";
17e48c5293SSteven Rostedt
182545eb61SSteven Rostedt$| = 1;
192545eb61SSteven Rostedt
202545eb61SSteven Rostedtmy %opt;
21a57419b3SSteven Rostedtmy %repeat_tests;
22a57419b3SSteven Rostedtmy %repeats;
23c75d22d9SSteven Rostedt (Red Hat)my %evals;
242545eb61SSteven Rostedt
252545eb61SSteven Rostedt#default opts
264f43e0dcSSteven Rostedtmy %default = (
272ceb2d85STim Tianyang Chen    "MAILER"			=> "sendmail",	# default mailer
282ceb2d85STim Tianyang Chen    "EMAIL_ON_ERROR"		=> 1,
292ceb2d85STim Tianyang Chen    "EMAIL_WHEN_FINISHED"	=> 1,
302ceb2d85STim Tianyang Chen    "EMAIL_WHEN_CANCELED"	=> 0,
312ceb2d85STim Tianyang Chen    "EMAIL_WHEN_STARTED"	=> 0,
324f43e0dcSSteven Rostedt    "NUM_TESTS"			=> 1,
334f43e0dcSSteven Rostedt    "TEST_TYPE"			=> "build",
34791dc9d6SSteven Rostedt (VMware)    "BUILD_TYPE"		=> "oldconfig",
354f43e0dcSSteven Rostedt    "MAKE_CMD"			=> "make",
365a5d8e48SSatoru Takeuchi    "CLOSE_CONSOLE_SIGNAL"	=> "INT",
374f43e0dcSSteven Rostedt    "TIMEOUT"			=> 120,
384f43e0dcSSteven Rostedt    "TMP_DIR"			=> "/tmp/ktest/\${MACHINE}",
394f43e0dcSSteven Rostedt    "SLEEP_TIME"		=> 60,		# sleep time between tests
404f43e0dcSSteven Rostedt    "BUILD_NOCLEAN"		=> 0,
414f43e0dcSSteven Rostedt    "REBOOT_ON_ERROR"		=> 0,
424f43e0dcSSteven Rostedt    "POWEROFF_ON_ERROR"		=> 0,
434f43e0dcSSteven Rostedt    "REBOOT_ON_SUCCESS"		=> 1,
444f43e0dcSSteven Rostedt    "POWEROFF_ON_SUCCESS"	=> 0,
454f43e0dcSSteven Rostedt    "BUILD_OPTIONS"		=> "",
464f43e0dcSSteven Rostedt    "BISECT_SLEEP_TIME"		=> 60,		# sleep time between bisects
474f43e0dcSSteven Rostedt    "PATCHCHECK_SLEEP_TIME"	=> 60, 		# sleep time between patch checks
484f43e0dcSSteven Rostedt    "CLEAR_LOG"			=> 0,
494f43e0dcSSteven Rostedt    "BISECT_MANUAL"		=> 0,
504f43e0dcSSteven Rostedt    "BISECT_SKIP"		=> 1,
51961d9cacSSteven Rostedt (Red Hat)    "BISECT_TRIES"		=> 1,
52ccc513b6SSteven Rostedt    "MIN_CONFIG_TYPE"		=> "boot",
534f43e0dcSSteven Rostedt    "SUCCESS_LINE"		=> "login:",
544f43e0dcSSteven Rostedt    "DETECT_TRIPLE_FAULT"	=> 1,
554f43e0dcSSteven Rostedt    "NO_INSTALL"		=> 0,
564f43e0dcSSteven Rostedt    "BOOTED_TIMEOUT"		=> 1,
574f43e0dcSSteven Rostedt    "DIE_ON_FAILURE"		=> 1,
584f43e0dcSSteven Rostedt    "SSH_EXEC"			=> "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
594f43e0dcSSteven Rostedt    "SCP_TO_TARGET"		=> "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
6002ad2617SSteven Rostedt    "SCP_TO_TARGET_INSTALL"	=> "\${SCP_TO_TARGET}",
614f43e0dcSSteven Rostedt    "REBOOT"			=> "ssh \$SSH_USER\@\$MACHINE reboot",
6237e16773SMasayoshi Mizuma    "REBOOT_RETURN_CODE"	=> 255,
634f43e0dcSSteven Rostedt    "STOP_AFTER_SUCCESS"	=> 10,
644f43e0dcSSteven Rostedt    "STOP_AFTER_FAILURE"	=> 60,
654f43e0dcSSteven Rostedt    "STOP_TEST_AFTER"		=> 600,
66407b95b7SSteven Rostedt    "MAX_MONITOR_WAIT"		=> 1800,
67a15ba913SSteven Rostedt    "GRUB_REBOOT"		=> "grub2-reboot",
68ac246645SMasayoshi Mizuma    "GRUB_BLS_GET"		=> "grubby --info=ALL",
697786954cSSteven Rostedt    "SYSLINUX"			=> "extlinux",
707786954cSSteven Rostedt    "SYSLINUX_PATH"		=> "/boot/extlinux",
713e1d3678SSteven Rostedt (VMware)    "CONNECT_TIMEOUT"		=> 25,
72600bbf0aSSteven Rostedt
73600bbf0aSSteven Rostedt# required, and we will ask users if they don't have them but we keep the default
74600bbf0aSSteven Rostedt# value something that is common.
754f43e0dcSSteven Rostedt    "REBOOT_TYPE"		=> "grub",
764f43e0dcSSteven Rostedt    "LOCALVERSION"		=> "-test",
774f43e0dcSSteven Rostedt    "SSH_USER"			=> "root",
784f43e0dcSSteven Rostedt    "BUILD_TARGET"	 	=> "arch/x86/boot/bzImage",
794f43e0dcSSteven Rostedt    "TARGET_IMAGE"		=> "/boot/vmlinuz-test",
809cc9e091SSteven Rostedt
819cc9e091SSteven Rostedt    "LOG_FILE"			=> undef,
829cc9e091SSteven Rostedt    "IGNORE_UNUSED"		=> 0,
834f43e0dcSSteven Rostedt);
842545eb61SSteven Rostedt
8534148b13SSteven Rostedt (VMware)my $test_log_start = 0;
8634148b13SSteven Rostedt (VMware)
875269faadSSatoru Takeuchimy $ktest_config = "ktest.conf";
882545eb61SSteven Rostedtmy $version;
89683a3e64SSteven Rostedtmy $have_version = 0;
90a75fececSSteven Rostedtmy $machine;
91df5f7c66SSteven Rostedt (Red Hat)my $last_machine;
92e48c5293SSteven Rostedtmy $ssh_user;
93a75fececSSteven Rostedtmy $tmpdir;
94a75fececSSteven Rostedtmy $builddir;
95a75fececSSteven Rostedtmy $outputdir;
9651ad1dd1SSteven Rostedtmy $output_config;
97a75fececSSteven Rostedtmy $test_type;
987faafbd6SSteven Rostedtmy $build_type;
99a75fececSSteven Rostedtmy $build_options;
100921ed4c7SSteven Rostedtmy $final_post_ktest;
101921ed4c7SSteven Rostedtmy $pre_ktest;
102921ed4c7SSteven Rostedtmy $post_ktest;
103921ed4c7SSteven Rostedtmy $pre_test;
104d53cdda3SSteven Rostedt (VMware)my $pre_test_die;
105921ed4c7SSteven Rostedtmy $post_test;
1060bd6c1a3SSteven Rostedtmy $pre_build;
1070bd6c1a3SSteven Rostedtmy $post_build;
1080bd6c1a3SSteven Rostedtmy $pre_build_die;
1090bd6c1a3SSteven Rostedtmy $post_build_die;
110a75fececSSteven Rostedtmy $reboot_type;
111a75fececSSteven Rostedtmy $reboot_script;
112a75fececSSteven Rostedtmy $power_cycle;
113e48c5293SSteven Rostedtmy $reboot;
11437e16773SMasayoshi Mizumamy $reboot_return_code;
115a75fececSSteven Rostedtmy $reboot_on_error;
116bc7c5803SSteven Rostedtmy $switch_to_good;
117bc7c5803SSteven Rostedtmy $switch_to_test;
118a75fececSSteven Rostedtmy $poweroff_on_error;
119648a182cSSteven Rostedtmy $reboot_on_success;
120a75fececSSteven Rostedtmy $die_on_failure;
121576f627cSSteven Rostedtmy $powercycle_after_reboot;
122576f627cSSteven Rostedtmy $poweroff_after_halt;
123407b95b7SSteven Rostedtmy $max_monitor_wait;
124e48c5293SSteven Rostedtmy $ssh_exec;
125e48c5293SSteven Rostedtmy $scp_to_target;
12602ad2617SSteven Rostedtmy $scp_to_target_install;
127a75fececSSteven Rostedtmy $power_off;
128a75fececSSteven Rostedtmy $grub_menu;
129752d9665SSteven Rostedt (Red Hat)my $last_grub_menu;
130a15ba913SSteven Rostedtmy $grub_file;
1312545eb61SSteven Rostedtmy $grub_number;
132a15ba913SSteven Rostedtmy $grub_reboot;
133ac246645SMasayoshi Mizumamy $grub_bls_get;
1347786954cSSteven Rostedtmy $syslinux;
1357786954cSSteven Rostedtmy $syslinux_path;
1367786954cSSteven Rostedtmy $syslinux_label;
1372545eb61SSteven Rostedtmy $target;
1382545eb61SSteven Rostedtmy $make;
139e5c2ec11SSteven Rostedtmy $pre_install;
1408b37ca8cSSteven Rostedtmy $post_install;
141e0a8742eSSteven Rostedtmy $no_install;
1425c42fc5bSSteven Rostedtmy $noclean;
1435f9b6cedSSteven Rostedtmy $minconfig;
1444c4ab120SSteven Rostedtmy $start_minconfig;
14535ce5952SSteven Rostedtmy $start_minconfig_defined;
1464c4ab120SSteven Rostedtmy $output_minconfig;
147ccc513b6SSteven Rostedtmy $minconfig_type;
14843de3316SSteven Rostedtmy $use_output_minconfig;
1494283b169SSteven Rostedt (Red Hat)my $warnings_file;
1504c4ab120SSteven Rostedtmy $ignore_config;
151be405f95SSteven Rostedtmy $ignore_errors;
1522b7d9b21SSteven Rostedtmy $addconfig;
1535f9b6cedSSteven Rostedtmy $in_bisect = 0;
154b5f4aea6SSteven Rostedtmy $bisect_bad_commit = "";
155d6ce2a0bSSteven Rostedtmy $reverse_bisect;
156c960bb9fSSteven Rostedtmy $bisect_manual;
157c23dca7cSSteven Rostedtmy $bisect_skip;
158961d9cacSSteven Rostedt (Red Hat)my $bisect_tries;
15930f75da5SSteven Rostedtmy $config_bisect_good;
160c5dacb88SSteven Rostedtmy $bisect_ret_good;
161c5dacb88SSteven Rostedtmy $bisect_ret_bad;
162c5dacb88SSteven Rostedtmy $bisect_ret_skip;
163c5dacb88SSteven Rostedtmy $bisect_ret_abort;
164c5dacb88SSteven Rostedtmy $bisect_ret_default;
1656c5ee0beSSteven Rostedtmy $in_patchcheck = 0;
1665a391fbfSSteven Rostedtmy $run_test;
1677faafbd6SSteven Rostedtmy $buildlog;
168a9dd5d63SRabin Vincentmy $testlog;
1697faafbd6SSteven Rostedtmy $dmesg;
1707faafbd6SSteven Rostedtmy $monitor_fp;
1717faafbd6SSteven Rostedtmy $monitor_pid;
1727faafbd6SSteven Rostedtmy $monitor_cnt = 0;
173a75fececSSteven Rostedtmy $sleep_time;
174a75fececSSteven Rostedtmy $bisect_sleep_time;
17527d934b2SSteven Rostedtmy $patchcheck_sleep_time;
1761990207dSSteven Rostedtmy $ignore_warnings;
177a75fececSSteven Rostedtmy $store_failures;
178de5b6e3bSRabin Vincentmy $store_successes;
1799064af52SSteven Rostedtmy $test_name;
180a75fececSSteven Rostedtmy $timeout;
1814e7d2a8fSSteven Rostedtmy $run_timeout;
1823e1d3678SSteven Rostedt (VMware)my $connect_timeout;
18340667fb5SSteven Rostedt (VMware)my $config_bisect_exec;
184a75fececSSteven Rostedtmy $booted_timeout;
185f1a5b962SSteven Rostedtmy $detect_triplefault;
186a75fececSSteven Rostedtmy $console;
1875a5d8e48SSatoru Takeuchimy $close_console_signal;
1882b803365SSteven Rostedtmy $reboot_success_line;
189a75fececSSteven Rostedtmy $success_line;
1901c8a617aSSteven Rostedtmy $stop_after_success;
1911c8a617aSSteven Rostedtmy $stop_after_failure;
1922d01b26aSSteven Rostedtmy $stop_test_after;
193a75fececSSteven Rostedtmy $build_target;
194a75fececSSteven Rostedtmy $target_image;
195b5f4aea6SSteven Rostedtmy $checkout;
196a75fececSSteven Rostedtmy $localversion;
197576f627cSSteven Rostedtmy $iteration = 0;
198e48c5293SSteven Rostedtmy $successes = 0;
1999d2f7f05SJosh Poimboeufmy $stty_orig;
2005739438bSSteven Rostedt (VMware)my $run_command_status = 0;
2012545eb61SSteven Rostedt
202b5f4aea6SSteven Rostedtmy $bisect_good;
203b5f4aea6SSteven Rostedtmy $bisect_bad;
204b5f4aea6SSteven Rostedtmy $bisect_type;
205b5f4aea6SSteven Rostedtmy $bisect_start;
206b5f4aea6SSteven Rostedtmy $bisect_replay;
207b5f4aea6SSteven Rostedtmy $bisect_files;
208b5f4aea6SSteven Rostedtmy $bisect_reverse;
209b5f4aea6SSteven Rostedtmy $bisect_check;
210b5f4aea6SSteven Rostedt
211b5f4aea6SSteven Rostedtmy $config_bisect;
212b5f4aea6SSteven Rostedtmy $config_bisect_type;
213b0918612SSteven Rostedtmy $config_bisect_check;
214b5f4aea6SSteven Rostedt
215b5f4aea6SSteven Rostedtmy $patchcheck_type;
216b5f4aea6SSteven Rostedtmy $patchcheck_start;
21723a0e161SSteven Rostedt (Red Hat)my $patchcheck_cherry;
218b5f4aea6SSteven Rostedtmy $patchcheck_end;
219b5f4aea6SSteven Rostedt
22038fa3dc1SSteven Rostedt (Red Hat)my $build_time;
22138fa3dc1SSteven Rostedt (Red Hat)my $install_time;
22238fa3dc1SSteven Rostedt (Red Hat)my $reboot_time;
22338fa3dc1SSteven Rostedt (Red Hat)my $test_time;
22438fa3dc1SSteven Rostedt (Red Hat)
22540667fb5SSteven Rostedt (VMware)my $pwd;
22640667fb5SSteven Rostedt (VMware)my $dirname = $FindBin::Bin;
22740667fb5SSteven Rostedt (VMware)
2282ceb2d85STim Tianyang Chenmy $mailto;
2292ceb2d85STim Tianyang Chenmy $mailer;
230be1546b8SSteven Rostedt (VMware)my $mail_path;
231f9869002SSteven Rostedt (VMware)my $mail_max_size;
232c2d84ddbSSteven Rostedt (VMware)my $mail_command;
2332ceb2d85STim Tianyang Chenmy $email_on_error;
2342ceb2d85STim Tianyang Chenmy $email_when_finished;
2352ceb2d85STim Tianyang Chenmy $email_when_started;
2362ceb2d85STim Tianyang Chenmy $email_when_canceled;
2372ceb2d85STim Tianyang Chen
2382ceb2d85STim Tianyang Chenmy $script_start_time = localtime();
2392ceb2d85STim Tianyang Chen
240165708b2SSteven Rostedt# set when a test is something other that just building or install
241bb8474b1SSteven Rostedt# which would require more options.
242bb8474b1SSteven Rostedtmy $buildonly = 1;
243bb8474b1SSteven Rostedt
2444283b169SSteven Rostedt (Red Hat)# tell build not to worry about warnings, even when WARNINGS_FILE is set
2454283b169SSteven Rostedt (Red Hat)my $warnings_ok = 0;
2464283b169SSteven Rostedt (Red Hat)
247dbd3783bSSteven Rostedt# set when creating a new config
248dbd3783bSSteven Rostedtmy $newconfig = 0;
249dbd3783bSSteven Rostedt
2508d1491baSSteven Rostedtmy %entered_configs;
2518d1491baSSteven Rostedtmy %config_help;
25277d942ceSSteven Rostedtmy %variable;
253cf79fab6SSteven Rostedt
254cf79fab6SSteven Rostedt# force_config is the list of configs that we force enabled (or disabled)
255cf79fab6SSteven Rostedt# in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
256fcb3f16aSSteven Rostedtmy %force_config;
2578d1491baSSteven Rostedt
2584ab1cce5SSteven Rostedt# do not force reboots on config problems
2594ab1cce5SSteven Rostedtmy $no_reboot = 1;
2604ab1cce5SSteven Rostedt
261759a3cc6SSteven Rostedt# reboot on success
262759a3cc6SSteven Rostedtmy $reboot_success = 0;
263759a3cc6SSteven Rostedt
2649cc9e091SSteven Rostedtmy %option_map = (
2652ceb2d85STim Tianyang Chen    "MAILTO"			=> \$mailto,
2662ceb2d85STim Tianyang Chen    "MAILER"			=> \$mailer,
267be1546b8SSteven Rostedt (VMware)    "MAIL_PATH"			=> \$mail_path,
268f9869002SSteven Rostedt (VMware)    "MAIL_MAX_SIZE"		=> \$mail_max_size,
269c2d84ddbSSteven Rostedt (VMware)    "MAIL_COMMAND"		=> \$mail_command,
2702ceb2d85STim Tianyang Chen    "EMAIL_ON_ERROR"		=> \$email_on_error,
2712ceb2d85STim Tianyang Chen    "EMAIL_WHEN_FINISHED"	=> \$email_when_finished,
2722ceb2d85STim Tianyang Chen    "EMAIL_WHEN_STARTED"	=> \$email_when_started,
2732ceb2d85STim Tianyang Chen    "EMAIL_WHEN_CANCELED"	=> \$email_when_canceled,
2749cc9e091SSteven Rostedt    "MACHINE"			=> \$machine,
2759cc9e091SSteven Rostedt    "SSH_USER"			=> \$ssh_user,
2769cc9e091SSteven Rostedt    "TMP_DIR"			=> \$tmpdir,
2779cc9e091SSteven Rostedt    "OUTPUT_DIR"		=> \$outputdir,
2789cc9e091SSteven Rostedt    "BUILD_DIR"			=> \$builddir,
2799cc9e091SSteven Rostedt    "TEST_TYPE"			=> \$test_type,
280921ed4c7SSteven Rostedt    "PRE_KTEST"			=> \$pre_ktest,
281921ed4c7SSteven Rostedt    "POST_KTEST"		=> \$post_ktest,
282921ed4c7SSteven Rostedt    "PRE_TEST"			=> \$pre_test,
283d53cdda3SSteven Rostedt (VMware)    "PRE_TEST_DIE"		=> \$pre_test_die,
284921ed4c7SSteven Rostedt    "POST_TEST"			=> \$post_test,
2859cc9e091SSteven Rostedt    "BUILD_TYPE"		=> \$build_type,
2869cc9e091SSteven Rostedt    "BUILD_OPTIONS"		=> \$build_options,
2879cc9e091SSteven Rostedt    "PRE_BUILD"			=> \$pre_build,
2889cc9e091SSteven Rostedt    "POST_BUILD"		=> \$post_build,
2899cc9e091SSteven Rostedt    "PRE_BUILD_DIE"		=> \$pre_build_die,
2909cc9e091SSteven Rostedt    "POST_BUILD_DIE"		=> \$post_build_die,
2919cc9e091SSteven Rostedt    "POWER_CYCLE"		=> \$power_cycle,
2929cc9e091SSteven Rostedt    "REBOOT"			=> \$reboot,
29337e16773SMasayoshi Mizuma    "REBOOT_RETURN_CODE"	=> \$reboot_return_code,
2949cc9e091SSteven Rostedt    "BUILD_NOCLEAN"		=> \$noclean,
2959cc9e091SSteven Rostedt    "MIN_CONFIG"		=> \$minconfig,
2969cc9e091SSteven Rostedt    "OUTPUT_MIN_CONFIG"		=> \$output_minconfig,
2979cc9e091SSteven Rostedt    "START_MIN_CONFIG"		=> \$start_minconfig,
298ccc513b6SSteven Rostedt    "MIN_CONFIG_TYPE"		=> \$minconfig_type,
29943de3316SSteven Rostedt    "USE_OUTPUT_MIN_CONFIG"	=> \$use_output_minconfig,
3004283b169SSteven Rostedt (Red Hat)    "WARNINGS_FILE"		=> \$warnings_file,
3019cc9e091SSteven Rostedt    "IGNORE_CONFIG"		=> \$ignore_config,
3029cc9e091SSteven Rostedt    "TEST"			=> \$run_test,
3039cc9e091SSteven Rostedt    "ADD_CONFIG"		=> \$addconfig,
3049cc9e091SSteven Rostedt    "REBOOT_TYPE"		=> \$reboot_type,
3059cc9e091SSteven Rostedt    "GRUB_MENU"			=> \$grub_menu,
306a15ba913SSteven Rostedt    "GRUB_FILE"			=> \$grub_file,
307a15ba913SSteven Rostedt    "GRUB_REBOOT"		=> \$grub_reboot,
308ac246645SMasayoshi Mizuma    "GRUB_BLS_GET"		=> \$grub_bls_get,
3097786954cSSteven Rostedt    "SYSLINUX"			=> \$syslinux,
3107786954cSSteven Rostedt    "SYSLINUX_PATH"		=> \$syslinux_path,
3117786954cSSteven Rostedt    "SYSLINUX_LABEL"		=> \$syslinux_label,
312e5c2ec11SSteven Rostedt    "PRE_INSTALL"		=> \$pre_install,
3139cc9e091SSteven Rostedt    "POST_INSTALL"		=> \$post_install,
3149cc9e091SSteven Rostedt    "NO_INSTALL"		=> \$no_install,
3159cc9e091SSteven Rostedt    "REBOOT_SCRIPT"		=> \$reboot_script,
3169cc9e091SSteven Rostedt    "REBOOT_ON_ERROR"		=> \$reboot_on_error,
3179cc9e091SSteven Rostedt    "SWITCH_TO_GOOD"		=> \$switch_to_good,
3189cc9e091SSteven Rostedt    "SWITCH_TO_TEST"		=> \$switch_to_test,
3199cc9e091SSteven Rostedt    "POWEROFF_ON_ERROR"		=> \$poweroff_on_error,
320648a182cSSteven Rostedt    "REBOOT_ON_SUCCESS"		=> \$reboot_on_success,
3219cc9e091SSteven Rostedt    "DIE_ON_FAILURE"		=> \$die_on_failure,
3229cc9e091SSteven Rostedt    "POWER_OFF"			=> \$power_off,
3239cc9e091SSteven Rostedt    "POWERCYCLE_AFTER_REBOOT"	=> \$powercycle_after_reboot,
3249cc9e091SSteven Rostedt    "POWEROFF_AFTER_HALT"	=> \$poweroff_after_halt,
325407b95b7SSteven Rostedt    "MAX_MONITOR_WAIT"		=> \$max_monitor_wait,
3269cc9e091SSteven Rostedt    "SLEEP_TIME"		=> \$sleep_time,
3279cc9e091SSteven Rostedt    "BISECT_SLEEP_TIME"		=> \$bisect_sleep_time,
3289cc9e091SSteven Rostedt    "PATCHCHECK_SLEEP_TIME"	=> \$patchcheck_sleep_time,
3299cc9e091SSteven Rostedt    "IGNORE_WARNINGS"		=> \$ignore_warnings,
330be405f95SSteven Rostedt    "IGNORE_ERRORS"		=> \$ignore_errors,
3319cc9e091SSteven Rostedt    "BISECT_MANUAL"		=> \$bisect_manual,
3329cc9e091SSteven Rostedt    "BISECT_SKIP"		=> \$bisect_skip,
333961d9cacSSteven Rostedt (Red Hat)    "BISECT_TRIES"		=> \$bisect_tries,
3349cc9e091SSteven Rostedt    "CONFIG_BISECT_GOOD"	=> \$config_bisect_good,
3359cc9e091SSteven Rostedt    "BISECT_RET_GOOD"		=> \$bisect_ret_good,
3369cc9e091SSteven Rostedt    "BISECT_RET_BAD"		=> \$bisect_ret_bad,
3379cc9e091SSteven Rostedt    "BISECT_RET_SKIP"		=> \$bisect_ret_skip,
3389cc9e091SSteven Rostedt    "BISECT_RET_ABORT"		=> \$bisect_ret_abort,
3399cc9e091SSteven Rostedt    "BISECT_RET_DEFAULT"	=> \$bisect_ret_default,
3409cc9e091SSteven Rostedt    "STORE_FAILURES"		=> \$store_failures,
3419cc9e091SSteven Rostedt    "STORE_SUCCESSES"		=> \$store_successes,
3429cc9e091SSteven Rostedt    "TEST_NAME"			=> \$test_name,
3439cc9e091SSteven Rostedt    "TIMEOUT"			=> \$timeout,
3444e7d2a8fSSteven Rostedt    "RUN_TIMEOUT"		=> \$run_timeout,
3453e1d3678SSteven Rostedt (VMware)    "CONNECT_TIMEOUT"		=> \$connect_timeout,
34640667fb5SSteven Rostedt (VMware)    "CONFIG_BISECT_EXEC"	=> \$config_bisect_exec,
3479cc9e091SSteven Rostedt    "BOOTED_TIMEOUT"		=> \$booted_timeout,
3489cc9e091SSteven Rostedt    "CONSOLE"			=> \$console,
3495a5d8e48SSatoru Takeuchi    "CLOSE_CONSOLE_SIGNAL"	=> \$close_console_signal,
3509cc9e091SSteven Rostedt    "DETECT_TRIPLE_FAULT"	=> \$detect_triplefault,
3519cc9e091SSteven Rostedt    "SUCCESS_LINE"		=> \$success_line,
3529cc9e091SSteven Rostedt    "REBOOT_SUCCESS_LINE"	=> \$reboot_success_line,
3539cc9e091SSteven Rostedt    "STOP_AFTER_SUCCESS"	=> \$stop_after_success,
3549cc9e091SSteven Rostedt    "STOP_AFTER_FAILURE"	=> \$stop_after_failure,
3559cc9e091SSteven Rostedt    "STOP_TEST_AFTER"		=> \$stop_test_after,
3569cc9e091SSteven Rostedt    "BUILD_TARGET"		=> \$build_target,
3579cc9e091SSteven Rostedt    "SSH_EXEC"			=> \$ssh_exec,
3589cc9e091SSteven Rostedt    "SCP_TO_TARGET"		=> \$scp_to_target,
35902ad2617SSteven Rostedt    "SCP_TO_TARGET_INSTALL"	=> \$scp_to_target_install,
3609cc9e091SSteven Rostedt    "CHECKOUT"			=> \$checkout,
3619cc9e091SSteven Rostedt    "TARGET_IMAGE"		=> \$target_image,
3629cc9e091SSteven Rostedt    "LOCALVERSION"		=> \$localversion,
3639cc9e091SSteven Rostedt
3649cc9e091SSteven Rostedt    "BISECT_GOOD"		=> \$bisect_good,
3659cc9e091SSteven Rostedt    "BISECT_BAD"		=> \$bisect_bad,
3669cc9e091SSteven Rostedt    "BISECT_TYPE"		=> \$bisect_type,
3679cc9e091SSteven Rostedt    "BISECT_START"		=> \$bisect_start,
3689cc9e091SSteven Rostedt    "BISECT_REPLAY"		=> \$bisect_replay,
3699cc9e091SSteven Rostedt    "BISECT_FILES"		=> \$bisect_files,
3709cc9e091SSteven Rostedt    "BISECT_REVERSE"		=> \$bisect_reverse,
3719cc9e091SSteven Rostedt    "BISECT_CHECK"		=> \$bisect_check,
3729cc9e091SSteven Rostedt
3739cc9e091SSteven Rostedt    "CONFIG_BISECT"		=> \$config_bisect,
3749cc9e091SSteven Rostedt    "CONFIG_BISECT_TYPE"	=> \$config_bisect_type,
375b0918612SSteven Rostedt    "CONFIG_BISECT_CHECK"	=> \$config_bisect_check,
3769cc9e091SSteven Rostedt
3779cc9e091SSteven Rostedt    "PATCHCHECK_TYPE"		=> \$patchcheck_type,
3789cc9e091SSteven Rostedt    "PATCHCHECK_START"		=> \$patchcheck_start,
37923a0e161SSteven Rostedt (Red Hat)    "PATCHCHECK_CHERRY"		=> \$patchcheck_cherry,
3809cc9e091SSteven Rostedt    "PATCHCHECK_END"		=> \$patchcheck_end,
3819cc9e091SSteven Rostedt);
3829cc9e091SSteven Rostedt
3839cc9e091SSteven Rostedt# Options may be used by other options, record them.
3849cc9e091SSteven Rostedtmy %used_options;
3859cc9e091SSteven Rostedt
3867bf51073SSteven Rostedt# default variables that can be used
3877bf51073SSteven Rostedtchomp ($variable{"PWD"} = `pwd`);
38840667fb5SSteven Rostedt (VMware)$pwd = $variable{"PWD"};
3897bf51073SSteven Rostedt
3908d1491baSSteven Rostedt$config_help{"MACHINE"} = << "EOF"
3918d1491baSSteven Rostedt The machine hostname that you will test.
392bb8474b1SSteven Rostedt For build only tests, it is still needed to differentiate log files.
3938d1491baSSteven RostedtEOF
3948d1491baSSteven Rostedt    ;
3958d1491baSSteven Rostedt$config_help{"SSH_USER"} = << "EOF"
3968d1491baSSteven Rostedt The box is expected to have ssh on normal bootup, provide the user
3978d1491baSSteven Rostedt  (most likely root, since you need privileged operations)
3988d1491baSSteven RostedtEOF
3998d1491baSSteven Rostedt    ;
4008d1491baSSteven Rostedt$config_help{"BUILD_DIR"} = << "EOF"
4018d1491baSSteven Rostedt The directory that contains the Linux source code (full path).
4020e7a22deSSteven Rostedt You can use \${PWD} that will be the path where ktest.pl is run, or use
4030e7a22deSSteven Rostedt \${THIS_DIR} which is assigned \${PWD} but may be changed later.
4048d1491baSSteven RostedtEOF
4058d1491baSSteven Rostedt    ;
4068d1491baSSteven Rostedt$config_help{"OUTPUT_DIR"} = << "EOF"
4078d1491baSSteven Rostedt The directory that the objects will be built (full path).
4088d1491baSSteven Rostedt (can not be same as BUILD_DIR)
4090e7a22deSSteven Rostedt You can use \${PWD} that will be the path where ktest.pl is run, or use
4100e7a22deSSteven Rostedt \${THIS_DIR} which is assigned \${PWD} but may be changed later.
4118d1491baSSteven RostedtEOF
4128d1491baSSteven Rostedt    ;
4138d1491baSSteven Rostedt$config_help{"BUILD_TARGET"} = << "EOF"
4148d1491baSSteven Rostedt The location of the compiled file to copy to the target.
4158d1491baSSteven Rostedt (relative to OUTPUT_DIR)
4168d1491baSSteven RostedtEOF
4178d1491baSSteven Rostedt    ;
418dbd3783bSSteven Rostedt$config_help{"BUILD_OPTIONS"} = << "EOF"
419dbd3783bSSteven Rostedt Options to add to \"make\" when building.
420dbd3783bSSteven Rostedt i.e.  -j20
421dbd3783bSSteven RostedtEOF
422dbd3783bSSteven Rostedt    ;
4238d1491baSSteven Rostedt$config_help{"TARGET_IMAGE"} = << "EOF"
4248d1491baSSteven Rostedt The place to put your image on the test machine.
4258d1491baSSteven RostedtEOF
4268d1491baSSteven Rostedt    ;
4278d1491baSSteven Rostedt$config_help{"POWER_CYCLE"} = << "EOF"
4288d1491baSSteven Rostedt A script or command to reboot the box.
4298d1491baSSteven Rostedt
4308d1491baSSteven Rostedt Here is a digital loggers power switch example
4318d1491baSSteven Rostedt POWER_CYCLE = wget --no-proxy -O /dev/null -q  --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
4328d1491baSSteven Rostedt
4338d1491baSSteven Rostedt Here is an example to reboot a virtual box on the current host
4348d1491baSSteven Rostedt with the name "Guest".
4358d1491baSSteven Rostedt POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
4368d1491baSSteven RostedtEOF
4378d1491baSSteven Rostedt    ;
4388d1491baSSteven Rostedt$config_help{"CONSOLE"} = << "EOF"
4398d1491baSSteven Rostedt The script or command that reads the console
4408d1491baSSteven Rostedt
4418d1491baSSteven Rostedt  If you use ttywatch server, something like the following would work.
4428d1491baSSteven RostedtCONSOLE = nc -d localhost 3001
4438d1491baSSteven Rostedt
4448d1491baSSteven Rostedt For a virtual machine with guest name "Guest".
4458d1491baSSteven RostedtCONSOLE =  virsh console Guest
4468d1491baSSteven RostedtEOF
4478d1491baSSteven Rostedt    ;
4488d1491baSSteven Rostedt$config_help{"LOCALVERSION"} = << "EOF"
4498d1491baSSteven Rostedt Required version ending to differentiate the test
4508d1491baSSteven Rostedt from other linux builds on the system.
4518d1491baSSteven RostedtEOF
4528d1491baSSteven Rostedt    ;
4538d1491baSSteven Rostedt$config_help{"REBOOT_TYPE"} = << "EOF"
4548d1491baSSteven Rostedt Way to reboot the box to the test kernel.
455ac246645SMasayoshi Mizuma Only valid options so far are "grub", "grub2", "grub2bls", "syslinux", and "script".
4568d1491baSSteven Rostedt
4578d1491baSSteven Rostedt If you specify grub, it will assume grub version 1
4588d1491baSSteven Rostedt and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
4598d1491baSSteven Rostedt and select that target to reboot to the kernel. If this is not
4608d1491baSSteven Rostedt your setup, then specify "script" and have a command or script
4618d1491baSSteven Rostedt specified in REBOOT_SCRIPT to boot to the target.
4628d1491baSSteven Rostedt
4638d1491baSSteven Rostedt The entry in /boot/grub/menu.lst must be entered in manually.
4648d1491baSSteven Rostedt The test will not modify that file.
465a15ba913SSteven Rostedt
466a15ba913SSteven Rostedt If you specify grub2, then you also need to specify both \$GRUB_MENU
467a15ba913SSteven Rostedt and \$GRUB_FILE.
4687786954cSSteven Rostedt
469ac246645SMasayoshi Mizuma If you specify grub2bls, then you also need to specify \$GRUB_MENU.
470ac246645SMasayoshi Mizuma
4717786954cSSteven Rostedt If you specify syslinux, then you may use SYSLINUX to define the syslinux
4727786954cSSteven Rostedt command (defaults to extlinux), and SYSLINUX_PATH to specify the path to
4737786954cSSteven Rostedt the syslinux install (defaults to /boot/extlinux). But you have to specify
4747786954cSSteven Rostedt SYSLINUX_LABEL to define the label to boot to for the test kernel.
4758d1491baSSteven RostedtEOF
4768d1491baSSteven Rostedt    ;
4778d1491baSSteven Rostedt$config_help{"GRUB_MENU"} = << "EOF"
4788d1491baSSteven Rostedt The grub title name for the test kernel to boot
479a15ba913SSteven Rostedt (Only mandatory if REBOOT_TYPE = grub or grub2)
4808d1491baSSteven Rostedt
4818d1491baSSteven Rostedt Note, ktest.pl will not update the grub menu.lst, you need to
4828d1491baSSteven Rostedt manually add an option for the test. ktest.pl will search
4838d1491baSSteven Rostedt the grub menu.lst for this option to find what kernel to
4848d1491baSSteven Rostedt reboot into.
4858d1491baSSteven Rostedt
4868d1491baSSteven Rostedt For example, if in the /boot/grub/menu.lst the test kernel title has:
4878d1491baSSteven Rostedt title Test Kernel
4888d1491baSSteven Rostedt kernel vmlinuz-test
4898d1491baSSteven Rostedt GRUB_MENU = Test Kernel
490a15ba913SSteven Rostedt
491a15ba913SSteven Rostedt For grub2, a search of \$GRUB_FILE is performed for the lines
492a15ba913SSteven Rostedt that begin with "menuentry". It will not detect submenus. The
493a15ba913SSteven Rostedt menu must be a non-nested menu. Add the quotes used in the menu
494a15ba913SSteven Rostedt to guarantee your selection, as the first menuentry with the content
495a15ba913SSteven Rostedt of \$GRUB_MENU that is found will be used.
496ac246645SMasayoshi Mizuma
497ac246645SMasayoshi Mizuma For grub2bls, \$GRUB_MENU is searched on the result of \$GRUB_BLS_GET
498ac246645SMasayoshi Mizuma command for the lines that begin with "title".
499a15ba913SSteven RostedtEOF
500a15ba913SSteven Rostedt    ;
501a15ba913SSteven Rostedt$config_help{"GRUB_FILE"} = << "EOF"
502a15ba913SSteven Rostedt If grub2 is used, the full path for the grub.cfg file is placed
503a15ba913SSteven Rostedt here. Use something like /boot/grub2/grub.cfg to search.
5048d1491baSSteven RostedtEOF
5058d1491baSSteven Rostedt    ;
5067786954cSSteven Rostedt$config_help{"SYSLINUX_LABEL"} = << "EOF"
5077786954cSSteven Rostedt If syslinux is used, the label that boots the target kernel must
5087786954cSSteven Rostedt be specified with SYSLINUX_LABEL.
5097786954cSSteven RostedtEOF
5107786954cSSteven Rostedt    ;
5118d1491baSSteven Rostedt$config_help{"REBOOT_SCRIPT"} = << "EOF"
5128d1491baSSteven Rostedt A script to reboot the target into the test kernel
5138d1491baSSteven Rostedt (Only mandatory if REBOOT_TYPE = script)
5148d1491baSSteven RostedtEOF
5158d1491baSSteven Rostedt    ;
5168d1491baSSteven Rostedt
5176a0f3652SJohn 'Warthog9' Hawley (VMware)# used with process_expression()
5186a0f3652SJohn 'Warthog9' Hawley (VMware)my $d = 0;
5196a0f3652SJohn 'Warthog9' Hawley (VMware)
5206a0f3652SJohn 'Warthog9' Hawley (VMware)# defined before get_test_name()
5216a0f3652SJohn 'Warthog9' Hawley (VMware)my $in_die = 0;
5226a0f3652SJohn 'Warthog9' Hawley (VMware)
5236a0f3652SJohn 'Warthog9' Hawley (VMware)# defined before process_warning_line()
5246a0f3652SJohn 'Warthog9' Hawley (VMware)my $check_build_re = ".*:.*(warning|error|Error):.*";
5256a0f3652SJohn 'Warthog9' Hawley (VMware)my $utf8_quote = "\\x{e2}\\x{80}(\\x{98}|\\x{99})";
5266a0f3652SJohn 'Warthog9' Hawley (VMware)
5276a0f3652SJohn 'Warthog9' Hawley (VMware)# defined before child_finished()
5286a0f3652SJohn 'Warthog9' Hawley (VMware)my $child_done;
5296a0f3652SJohn 'Warthog9' Hawley (VMware)
5306a0f3652SJohn 'Warthog9' Hawley (VMware)# config_ignore holds the configs that were set (or unset) for
5316a0f3652SJohn 'Warthog9' Hawley (VMware)# a good config and we will ignore these configs for the rest
5326a0f3652SJohn 'Warthog9' Hawley (VMware)# of a config bisect. These configs stay as they were.
5336a0f3652SJohn 'Warthog9' Hawley (VMware)my %config_ignore;
5346a0f3652SJohn 'Warthog9' Hawley (VMware)
5356a0f3652SJohn 'Warthog9' Hawley (VMware)# config_set holds what all configs were set as.
5366a0f3652SJohn 'Warthog9' Hawley (VMware)my %config_set;
5376a0f3652SJohn 'Warthog9' Hawley (VMware)
5386a0f3652SJohn 'Warthog9' Hawley (VMware)# config_off holds the set of configs that the bad config had disabled.
5396a0f3652SJohn 'Warthog9' Hawley (VMware)# We need to record them and set them in the .config when running
5406a0f3652SJohn 'Warthog9' Hawley (VMware)# olddefconfig, because olddefconfig keeps the defaults.
5416a0f3652SJohn 'Warthog9' Hawley (VMware)my %config_off;
5426a0f3652SJohn 'Warthog9' Hawley (VMware)
5436a0f3652SJohn 'Warthog9' Hawley (VMware)# config_off_tmp holds a set of configs to turn off for now
5446a0f3652SJohn 'Warthog9' Hawley (VMware)my @config_off_tmp;
5456a0f3652SJohn 'Warthog9' Hawley (VMware)
5466a0f3652SJohn 'Warthog9' Hawley (VMware)# config_list is the set of configs that are being tested
5476a0f3652SJohn 'Warthog9' Hawley (VMware)my %config_list;
5486a0f3652SJohn 'Warthog9' Hawley (VMware)my %null_config;
5496a0f3652SJohn 'Warthog9' Hawley (VMware)
5506a0f3652SJohn 'Warthog9' Hawley (VMware)my %dependency;
5516a0f3652SJohn 'Warthog9' Hawley (VMware)
5526a0f3652SJohn 'Warthog9' Hawley (VMware)# found above run_config_bisect()
5536a0f3652SJohn 'Warthog9' Hawley (VMware)my $pass = 1;
5546a0f3652SJohn 'Warthog9' Hawley (VMware)
5556a0f3652SJohn 'Warthog9' Hawley (VMware)# found above add_dep()
5566a0f3652SJohn 'Warthog9' Hawley (VMware)
5576a0f3652SJohn 'Warthog9' Hawley (VMware)my %depends;
5586a0f3652SJohn 'Warthog9' Hawley (VMware)my %depcount;
5596a0f3652SJohn 'Warthog9' Hawley (VMware)my $iflevel = 0;
5606a0f3652SJohn 'Warthog9' Hawley (VMware)my @ifdeps;
5616a0f3652SJohn 'Warthog9' Hawley (VMware)
5626a0f3652SJohn 'Warthog9' Hawley (VMware)# prevent recursion
5636a0f3652SJohn 'Warthog9' Hawley (VMware)my %read_kconfigs;
5646a0f3652SJohn 'Warthog9' Hawley (VMware)
5656a0f3652SJohn 'Warthog9' Hawley (VMware)# found above test_this_config()
5666a0f3652SJohn 'Warthog9' Hawley (VMware)my %min_configs;
5676a0f3652SJohn 'Warthog9' Hawley (VMware)my %keep_configs;
5686a0f3652SJohn 'Warthog9' Hawley (VMware)my %save_configs;
5696a0f3652SJohn 'Warthog9' Hawley (VMware)my %processed_configs;
5706a0f3652SJohn 'Warthog9' Hawley (VMware)my %nochange_config;
5716a0f3652SJohn 'Warthog9' Hawley (VMware)
5726a0f3652SJohn 'Warthog9' Hawley (VMware)#
5736a0f3652SJohn 'Warthog9' Hawley (VMware)# These are first defined here, main function later on
5746a0f3652SJohn 'Warthog9' Hawley (VMware)#
5756a0f3652SJohn 'Warthog9' Hawley (VMware)sub run_command;
5766a0f3652SJohn 'Warthog9' Hawley (VMware)sub start_monitor;
5776a0f3652SJohn 'Warthog9' Hawley (VMware)sub end_monitor;
5786a0f3652SJohn 'Warthog9' Hawley (VMware)sub wait_for_monitor;
5796a0f3652SJohn 'Warthog9' Hawley (VMware)
580c75d22d9SSteven Rostedt (Red Hat)sub _logit {
581c75d22d9SSteven Rostedt (Red Hat)    if (defined($opt{"LOG_FILE"})) {
582d6bc29d9SSteven Rostedt (VMware)	print LOG @_;
583c75d22d9SSteven Rostedt (Red Hat)    }
584c75d22d9SSteven Rostedt (Red Hat)}
585c75d22d9SSteven Rostedt (Red Hat)
586c75d22d9SSteven Rostedt (Red Hat)sub logit {
587c75d22d9SSteven Rostedt (Red Hat)    if (defined($opt{"LOG_FILE"})) {
588c75d22d9SSteven Rostedt (Red Hat)	_logit @_;
589c75d22d9SSteven Rostedt (Red Hat)    } else {
590c75d22d9SSteven Rostedt (Red Hat)	print @_;
591c75d22d9SSteven Rostedt (Red Hat)    }
592c75d22d9SSteven Rostedt (Red Hat)}
593c75d22d9SSteven Rostedt (Red Hat)
594c75d22d9SSteven Rostedt (Red Hat)sub doprint {
595c75d22d9SSteven Rostedt (Red Hat)    print @_;
596c75d22d9SSteven Rostedt (Red Hat)    _logit @_;
597c75d22d9SSteven Rostedt (Red Hat)}
598c75d22d9SSteven Rostedt (Red Hat)
599dad98754SSteven Rostedtsub read_prompt {
600dad98754SSteven Rostedt    my ($cancel, $prompt) = @_;
60135ce5952SSteven Rostedt
60235ce5952SSteven Rostedt    my $ans;
60335ce5952SSteven Rostedt
60435ce5952SSteven Rostedt    for (;;) {
605dad98754SSteven Rostedt        if ($cancel) {
606dad98754SSteven Rostedt	    print "$prompt [y/n/C] ";
607dad98754SSteven Rostedt	} else {
60835ce5952SSteven Rostedt	    print "$prompt [Y/n] ";
609dad98754SSteven Rostedt	}
61035ce5952SSteven Rostedt	$ans = <STDIN>;
61135ce5952SSteven Rostedt	chomp $ans;
61235ce5952SSteven Rostedt	if ($ans =~ /^\s*$/) {
613dad98754SSteven Rostedt	    if ($cancel) {
614dad98754SSteven Rostedt		$ans = "c";
615dad98754SSteven Rostedt	    } else {
61635ce5952SSteven Rostedt		$ans = "y";
61735ce5952SSteven Rostedt	    }
618dad98754SSteven Rostedt	}
61935ce5952SSteven Rostedt	last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
620dad98754SSteven Rostedt	if ($cancel) {
621dad98754SSteven Rostedt	    last if ($ans =~ /^c$/i);
622dad98754SSteven Rostedt	    print "Please answer either 'y', 'n' or 'c'.\n";
623dad98754SSteven Rostedt	} else {
62435ce5952SSteven Rostedt	    print "Please answer either 'y' or 'n'.\n";
62535ce5952SSteven Rostedt	}
626dad98754SSteven Rostedt    }
627dad98754SSteven Rostedt    if ($ans =~ /^c/i) {
628dad98754SSteven Rostedt	exit;
629dad98754SSteven Rostedt    }
63035ce5952SSteven Rostedt    if ($ans !~ /^y$/i) {
63135ce5952SSteven Rostedt	return 0;
63235ce5952SSteven Rostedt    }
63335ce5952SSteven Rostedt    return 1;
63435ce5952SSteven Rostedt}
6358d1491baSSteven Rostedt
636dad98754SSteven Rostedtsub read_yn {
637dad98754SSteven Rostedt    my ($prompt) = @_;
638dad98754SSteven Rostedt
639dad98754SSteven Rostedt    return read_prompt 0, $prompt;
640dad98754SSteven Rostedt}
641dad98754SSteven Rostedt
642dad98754SSteven Rostedtsub read_ync {
643dad98754SSteven Rostedt    my ($prompt) = @_;
644dad98754SSteven Rostedt
645dad98754SSteven Rostedt    return read_prompt 1, $prompt;
646dad98754SSteven Rostedt}
647dad98754SSteven Rostedt
6485269faadSSatoru Takeuchisub get_mandatory_config {
6498d1491baSSteven Rostedt    my ($config) = @_;
650815e2bd7SSteven Rostedt    my $ans;
6518d1491baSSteven Rostedt
6528d1491baSSteven Rostedt    return if (defined($opt{$config}));
6538d1491baSSteven Rostedt
6548d1491baSSteven Rostedt    if (defined($config_help{$config})) {
6558d1491baSSteven Rostedt	print "\n";
6568d1491baSSteven Rostedt	print $config_help{$config};
6578d1491baSSteven Rostedt    }
6588d1491baSSteven Rostedt
6598d1491baSSteven Rostedt    for (;;) {
6608d1491baSSteven Rostedt	print "$config = ";
661dbd3783bSSteven Rostedt	if (defined($default{$config}) && length($default{$config})) {
6628d1491baSSteven Rostedt	    print "\[$default{$config}\] ";
6638d1491baSSteven Rostedt	}
664815e2bd7SSteven Rostedt	$ans = <STDIN>;
665815e2bd7SSteven Rostedt	$ans =~ s/^\s*(.*\S)\s*$/$1/;
666815e2bd7SSteven Rostedt	if ($ans =~ /^\s*$/) {
6678d1491baSSteven Rostedt	    if ($default{$config}) {
668815e2bd7SSteven Rostedt		$ans = $default{$config};
6698d1491baSSteven Rostedt	    } else {
6708d1491baSSteven Rostedt		print "Your answer can not be blank\n";
6718d1491baSSteven Rostedt		next;
6728d1491baSSteven Rostedt	    }
6738d1491baSSteven Rostedt	}
6740e7a22deSSteven Rostedt	$entered_configs{$config} = ${ans};
6758d1491baSSteven Rostedt	last;
6768d1491baSSteven Rostedt    }
6778d1491baSSteven Rostedt}
6788d1491baSSteven Rostedt
67938fa3dc1SSteven Rostedt (Red Hat)sub show_time {
68038fa3dc1SSteven Rostedt (Red Hat)    my ($time) = @_;
68138fa3dc1SSteven Rostedt (Red Hat)
68238fa3dc1SSteven Rostedt (Red Hat)    my $hours = 0;
68338fa3dc1SSteven Rostedt (Red Hat)    my $minutes = 0;
68438fa3dc1SSteven Rostedt (Red Hat)
68538fa3dc1SSteven Rostedt (Red Hat)    if ($time > 3600) {
68638fa3dc1SSteven Rostedt (Red Hat)	$hours = int($time / 3600);
68738fa3dc1SSteven Rostedt (Red Hat)	$time -= $hours * 3600;
68838fa3dc1SSteven Rostedt (Red Hat)    }
68938fa3dc1SSteven Rostedt (Red Hat)    if ($time > 60) {
69038fa3dc1SSteven Rostedt (Red Hat)	$minutes = int($time / 60);
69138fa3dc1SSteven Rostedt (Red Hat)	$time -= $minutes * 60;
69238fa3dc1SSteven Rostedt (Red Hat)    }
69338fa3dc1SSteven Rostedt (Red Hat)
69438fa3dc1SSteven Rostedt (Red Hat)    if ($hours > 0) {
69538fa3dc1SSteven Rostedt (Red Hat)	doprint "$hours hour";
69638fa3dc1SSteven Rostedt (Red Hat)	doprint "s" if ($hours > 1);
69738fa3dc1SSteven Rostedt (Red Hat)	doprint " ";
69838fa3dc1SSteven Rostedt (Red Hat)    }
69938fa3dc1SSteven Rostedt (Red Hat)
70038fa3dc1SSteven Rostedt (Red Hat)    if ($minutes > 0) {
70138fa3dc1SSteven Rostedt (Red Hat)	doprint "$minutes minute";
70238fa3dc1SSteven Rostedt (Red Hat)	doprint "s" if ($minutes > 1);
70338fa3dc1SSteven Rostedt (Red Hat)	doprint " ";
70438fa3dc1SSteven Rostedt (Red Hat)    }
70538fa3dc1SSteven Rostedt (Red Hat)
70638fa3dc1SSteven Rostedt (Red Hat)    doprint "$time second";
70738fa3dc1SSteven Rostedt (Red Hat)    doprint "s" if ($time != 1);
70838fa3dc1SSteven Rostedt (Red Hat)}
70938fa3dc1SSteven Rostedt (Red Hat)
71038fa3dc1SSteven Rostedt (Red Hat)sub print_times {
71138fa3dc1SSteven Rostedt (Red Hat)    doprint "\n";
71238fa3dc1SSteven Rostedt (Red Hat)    if ($build_time) {
71338fa3dc1SSteven Rostedt (Red Hat)	doprint "Build time:   ";
71438fa3dc1SSteven Rostedt (Red Hat)	show_time($build_time);
71538fa3dc1SSteven Rostedt (Red Hat)	doprint "\n";
71638fa3dc1SSteven Rostedt (Red Hat)    }
71738fa3dc1SSteven Rostedt (Red Hat)    if ($install_time) {
71838fa3dc1SSteven Rostedt (Red Hat)	doprint "Install time: ";
71938fa3dc1SSteven Rostedt (Red Hat)	show_time($install_time);
72038fa3dc1SSteven Rostedt (Red Hat)	doprint "\n";
72138fa3dc1SSteven Rostedt (Red Hat)    }
72238fa3dc1SSteven Rostedt (Red Hat)    if ($reboot_time) {
72338fa3dc1SSteven Rostedt (Red Hat)	doprint "Reboot time:  ";
72438fa3dc1SSteven Rostedt (Red Hat)	show_time($reboot_time);
72538fa3dc1SSteven Rostedt (Red Hat)	doprint "\n";
72638fa3dc1SSteven Rostedt (Red Hat)    }
72738fa3dc1SSteven Rostedt (Red Hat)    if ($test_time) {
72838fa3dc1SSteven Rostedt (Red Hat)	doprint "Test time:    ";
72938fa3dc1SSteven Rostedt (Red Hat)	show_time($test_time);
73038fa3dc1SSteven Rostedt (Red Hat)	doprint "\n";
73138fa3dc1SSteven Rostedt (Red Hat)    }
73238fa3dc1SSteven Rostedt (Red Hat)    # reset for iterations like bisect
73338fa3dc1SSteven Rostedt (Red Hat)    $build_time = 0;
73438fa3dc1SSteven Rostedt (Red Hat)    $install_time = 0;
73538fa3dc1SSteven Rostedt (Red Hat)    $reboot_time = 0;
73638fa3dc1SSteven Rostedt (Red Hat)    $test_time = 0;
73738fa3dc1SSteven Rostedt (Red Hat)}
73838fa3dc1SSteven Rostedt (Red Hat)
7395269faadSSatoru Takeuchisub get_mandatory_configs {
7405269faadSSatoru Takeuchi    get_mandatory_config("MACHINE");
7415269faadSSatoru Takeuchi    get_mandatory_config("BUILD_DIR");
7425269faadSSatoru Takeuchi    get_mandatory_config("OUTPUT_DIR");
743bb8474b1SSteven Rostedt
744dbd3783bSSteven Rostedt    if ($newconfig) {
7455269faadSSatoru Takeuchi	get_mandatory_config("BUILD_OPTIONS");
746dbd3783bSSteven Rostedt    }
747dbd3783bSSteven Rostedt
748bb8474b1SSteven Rostedt    # options required for other than just building a kernel
749bb8474b1SSteven Rostedt    if (!$buildonly) {
7505269faadSSatoru Takeuchi	get_mandatory_config("POWER_CYCLE");
7515269faadSSatoru Takeuchi	get_mandatory_config("CONSOLE");
752165708b2SSteven Rostedt    }
753165708b2SSteven Rostedt
754165708b2SSteven Rostedt    # options required for install and more
755165708b2SSteven Rostedt    if ($buildonly != 1) {
7565269faadSSatoru Takeuchi	get_mandatory_config("SSH_USER");
7575269faadSSatoru Takeuchi	get_mandatory_config("BUILD_TARGET");
7585269faadSSatoru Takeuchi	get_mandatory_config("TARGET_IMAGE");
759bb8474b1SSteven Rostedt    }
760bb8474b1SSteven Rostedt
7615269faadSSatoru Takeuchi    get_mandatory_config("LOCALVERSION");
7628d1491baSSteven Rostedt
763bb8474b1SSteven Rostedt    return if ($buildonly);
764bb8474b1SSteven Rostedt
7658d1491baSSteven Rostedt    my $rtype = $opt{"REBOOT_TYPE"};
7668d1491baSSteven Rostedt
7678d1491baSSteven Rostedt    if (!defined($rtype)) {
7688d1491baSSteven Rostedt	if (!defined($opt{"GRUB_MENU"})) {
7695269faadSSatoru Takeuchi	    get_mandatory_config("REBOOT_TYPE");
7708d1491baSSteven Rostedt	    $rtype = $entered_configs{"REBOOT_TYPE"};
7718d1491baSSteven Rostedt	} else {
7728d1491baSSteven Rostedt	    $rtype = "grub";
7738d1491baSSteven Rostedt	}
7748d1491baSSteven Rostedt    }
7758d1491baSSteven Rostedt
776ac246645SMasayoshi Mizuma    if (($rtype eq "grub") or ($rtype eq "grub2bls")) {
7775269faadSSatoru Takeuchi	get_mandatory_config("GRUB_MENU");
7788d1491baSSteven Rostedt    }
779a15ba913SSteven Rostedt
780a15ba913SSteven Rostedt    if ($rtype eq "grub2") {
7815269faadSSatoru Takeuchi	get_mandatory_config("GRUB_MENU");
7825269faadSSatoru Takeuchi	get_mandatory_config("GRUB_FILE");
783a15ba913SSteven Rostedt    }
7847786954cSSteven Rostedt
7857786954cSSteven Rostedt    if ($rtype eq "syslinux") {
7865269faadSSatoru Takeuchi	get_mandatory_config("SYSLINUX_LABEL");
7877786954cSSteven Rostedt    }
7888d1491baSSteven Rostedt}
7898d1491baSSteven Rostedt
79077d942ceSSteven Rostedtsub process_variables {
7918d735212SSteven Rostedt    my ($value, $remove_undef) = @_;
79277d942ceSSteven Rostedt    my $retval = "";
79377d942ceSSteven Rostedt
79477d942ceSSteven Rostedt    # We want to check for '\', and it is just easier
79577d942ceSSteven Rostedt    # to check the previous characet of '$' and not need
79677d942ceSSteven Rostedt    # to worry if '$' is the first character. By adding
79777d942ceSSteven Rostedt    # a space to $value, we can just check [^\\]\$ and
79877d942ceSSteven Rostedt    # it will still work.
79977d942ceSSteven Rostedt    $value = " $value";
80077d942ceSSteven Rostedt
80177d942ceSSteven Rostedt    while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
80277d942ceSSteven Rostedt	my $begin = $1;
80377d942ceSSteven Rostedt	my $var = $2;
80477d942ceSSteven Rostedt	my $end = $3;
80577d942ceSSteven Rostedt	# append beginning of value to retval
80677d942ceSSteven Rostedt	$retval = "$retval$begin";
80788a51b4fSSteven Rostedt (Google)	if ($var =~ s/^shell\s+//) {
80888a51b4fSSteven Rostedt (Google)	    $retval = `$var`;
80988a51b4fSSteven Rostedt (Google)	    if ($?) {
81088a51b4fSSteven Rostedt (Google)		doprint "WARNING: $var returned an error\n";
81188a51b4fSSteven Rostedt (Google)	    } else {
81288a51b4fSSteven Rostedt (Google)		chomp $retval;
81388a51b4fSSteven Rostedt (Google)	    }
81488a51b4fSSteven Rostedt (Google)	} elsif (defined($variable{$var})) {
81577d942ceSSteven Rostedt	    $retval = "$retval$variable{$var}";
8168d735212SSteven Rostedt	} elsif (defined($remove_undef) && $remove_undef) {
8178d735212SSteven Rostedt	    # for if statements, any variable that is not defined,
8188d735212SSteven Rostedt	    # we simple convert to 0
8198d735212SSteven Rostedt	    $retval = "${retval}0";
82077d942ceSSteven Rostedt	} else {
82177d942ceSSteven Rostedt	    # put back the origin piece.
82277d942ceSSteven Rostedt	    $retval = "$retval\$\{$var\}";
8239cc9e091SSteven Rostedt	    # This could be an option that is used later, save
8249cc9e091SSteven Rostedt	    # it so we don't warn if this option is not one of
8259cc9e091SSteven Rostedt	    # ktests options.
8269cc9e091SSteven Rostedt	    $used_options{$var} = 1;
82777d942ceSSteven Rostedt	}
82877d942ceSSteven Rostedt	$value = $end;
82977d942ceSSteven Rostedt    }
83077d942ceSSteven Rostedt    $retval = "$retval$value";
83177d942ceSSteven Rostedt
83277d942ceSSteven Rostedt    # remove the space added in the beginning
83377d942ceSSteven Rostedt    $retval =~ s/ //;
83477d942ceSSteven Rostedt
83512d4cdddSJohn 'Warthog9' Hawley (VMware)    return "$retval";
83677d942ceSSteven Rostedt}
83777d942ceSSteven Rostedt
838a57419b3SSteven Rostedtsub set_value {
8393d1cc414SSteven Rostedt    my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
8402545eb61SSteven Rostedt
841cad96669SSteven Rostedt    my $prvalue = process_variables($rvalue);
842cad96669SSteven Rostedt
84325bc70faSScott Wood    if ($lvalue =~ /^(TEST|BISECT|CONFIG_BISECT)_TYPE(\[.*\])?$/ &&
84425bc70faSScott Wood	$prvalue !~ /^(config_|)bisect$/ &&
84525bc70faSScott Wood	$prvalue !~ /^build$/ &&
846fd067c8bSRicardo B. Marliere	$prvalue !~ /^make_warnings_file$/ &&
84725bc70faSScott Wood	$buildonly) {
84825bc70faSScott Wood
849bb8474b1SSteven Rostedt	# Note if a test is something other than build, then we
85022722799SPavel Machek	# will need other mandatory options.
851cad96669SSteven Rostedt	if ($prvalue ne "install") {
852bb8474b1SSteven Rostedt	    $buildonly = 0;
853165708b2SSteven Rostedt	} else {
85422722799SPavel Machek	    # install still limits some mandatory options.
855165708b2SSteven Rostedt	    $buildonly = 2;
856165708b2SSteven Rostedt	}
857bb8474b1SSteven Rostedt    }
858bb8474b1SSteven Rostedt
859a75fececSSteven Rostedt    if (defined($opt{$lvalue})) {
8603d1cc414SSteven Rostedt	if (!$override || defined(${$overrides}{$lvalue})) {
8613d1cc414SSteven Rostedt	    my $extra = "";
8623d1cc414SSteven Rostedt	    if ($override) {
8633d1cc414SSteven Rostedt		$extra = "In the same override section!\n";
8643d1cc414SSteven Rostedt	    }
8653d1cc414SSteven Rostedt	    die "$name: $.: Option $lvalue defined more than once!\n$extra";
8663d1cc414SSteven Rostedt	}
867cad96669SSteven Rostedt	${$overrides}{$lvalue} = $prvalue;
868a75fececSSteven Rostedt    }
86922c37a9aSSteven Rostedt (Red Hat)
870cad96669SSteven Rostedt    $opt{$lvalue} = $prvalue;
87121a9679fSSteven Rostedt}
872a57419b3SSteven Rostedt
873c75d22d9SSteven Rostedt (Red Hat)sub set_eval {
874c75d22d9SSteven Rostedt (Red Hat)    my ($lvalue, $rvalue, $name) = @_;
875c75d22d9SSteven Rostedt (Red Hat)
876c75d22d9SSteven Rostedt (Red Hat)    my $prvalue = process_variables($rvalue);
877c75d22d9SSteven Rostedt (Red Hat)    my $arr;
878c75d22d9SSteven Rostedt (Red Hat)
879c75d22d9SSteven Rostedt (Red Hat)    if (defined($evals{$lvalue})) {
880c75d22d9SSteven Rostedt (Red Hat)	$arr = $evals{$lvalue};
881c75d22d9SSteven Rostedt (Red Hat)    } else {
882c75d22d9SSteven Rostedt (Red Hat)	$arr = [];
883c75d22d9SSteven Rostedt (Red Hat)	$evals{$lvalue} = $arr;
884c75d22d9SSteven Rostedt (Red Hat)    }
885c75d22d9SSteven Rostedt (Red Hat)
886c75d22d9SSteven Rostedt (Red Hat)    push @{$arr}, $rvalue;
887c75d22d9SSteven Rostedt (Red Hat)}
888c75d22d9SSteven Rostedt (Red Hat)
88977d942ceSSteven Rostedtsub set_variable {
89077d942ceSSteven Rostedt    my ($lvalue, $rvalue) = @_;
89177d942ceSSteven Rostedt
89277d942ceSSteven Rostedt    if ($rvalue =~ /^\s*$/) {
89377d942ceSSteven Rostedt	delete $variable{$lvalue};
89477d942ceSSteven Rostedt    } else {
89577d942ceSSteven Rostedt	$rvalue = process_variables($rvalue);
89677d942ceSSteven Rostedt	$variable{$lvalue} = $rvalue;
89777d942ceSSteven Rostedt    }
89877d942ceSSteven Rostedt}
89977d942ceSSteven Rostedt
900ab7a3f52SSteven Rostedtsub process_compare {
901ab7a3f52SSteven Rostedt    my ($lval, $cmp, $rval) = @_;
902ab7a3f52SSteven Rostedt
903ab7a3f52SSteven Rostedt    # remove whitespace
904ab7a3f52SSteven Rostedt
905ab7a3f52SSteven Rostedt    $lval =~ s/^\s*//;
906ab7a3f52SSteven Rostedt    $lval =~ s/\s*$//;
907ab7a3f52SSteven Rostedt
908ab7a3f52SSteven Rostedt    $rval =~ s/^\s*//;
909ab7a3f52SSteven Rostedt    $rval =~ s/\s*$//;
910ab7a3f52SSteven Rostedt
911ab7a3f52SSteven Rostedt    if ($cmp eq "==") {
912ab7a3f52SSteven Rostedt	return $lval eq $rval;
913ab7a3f52SSteven Rostedt    } elsif ($cmp eq "!=") {
914ab7a3f52SSteven Rostedt	return $lval ne $rval;
9158fddbe9bSSteven Rostedt    } elsif ($cmp eq "=~") {
9168fddbe9bSSteven Rostedt	return $lval =~ m/$rval/;
9178fddbe9bSSteven Rostedt    } elsif ($cmp eq "!~") {
9188fddbe9bSSteven Rostedt	return $lval !~ m/$rval/;
919ab7a3f52SSteven Rostedt    }
920ab7a3f52SSteven Rostedt
921ab7a3f52SSteven Rostedt    my $statement = "$lval $cmp $rval";
922ab7a3f52SSteven Rostedt    my $ret = eval $statement;
923ab7a3f52SSteven Rostedt
924ab7a3f52SSteven Rostedt    # $@ stores error of eval
925ab7a3f52SSteven Rostedt    if ($@) {
926ab7a3f52SSteven Rostedt	return -1;
927ab7a3f52SSteven Rostedt    }
928ab7a3f52SSteven Rostedt
929ab7a3f52SSteven Rostedt    return $ret;
930ab7a3f52SSteven Rostedt}
931ab7a3f52SSteven Rostedt
9329900b5dcSSteven Rostedtsub value_defined {
9339900b5dcSSteven Rostedt    my ($val) = @_;
9349900b5dcSSteven Rostedt
9359900b5dcSSteven Rostedt    return defined($variable{$2}) ||
9369900b5dcSSteven Rostedt	defined($opt{$2});
9379900b5dcSSteven Rostedt}
9389900b5dcSSteven Rostedt
9398d735212SSteven Rostedtsub process_expression {
9408d735212SSteven Rostedt    my ($name, $val) = @_;
94145d73a5dSSteven Rostedt
9428d735212SSteven Rostedt    my $c = $d++;
9438d735212SSteven Rostedt
9448d735212SSteven Rostedt    while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
9458d735212SSteven Rostedt	my $express = $1;
9468d735212SSteven Rostedt
9478d735212SSteven Rostedt	if (process_expression($name, $express)) {
9488d735212SSteven Rostedt	    $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
9498d735212SSteven Rostedt	} else {
9508d735212SSteven Rostedt	    $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
9518d735212SSteven Rostedt	}
9528d735212SSteven Rostedt    }
9538d735212SSteven Rostedt
9548d735212SSteven Rostedt    $d--;
9558d735212SSteven Rostedt    my $OR = "\\|\\|";
9568d735212SSteven Rostedt    my $AND = "\\&\\&";
9578d735212SSteven Rostedt
9588d735212SSteven Rostedt    while ($val =~ s/^(.*?)($OR|$AND)//) {
9598d735212SSteven Rostedt	my $express = $1;
9608d735212SSteven Rostedt	my $op = $2;
9618d735212SSteven Rostedt
9628d735212SSteven Rostedt	if (process_expression($name, $express)) {
9638d735212SSteven Rostedt	    if ($op eq "||") {
9648d735212SSteven Rostedt		return 1;
9658d735212SSteven Rostedt	    }
9668d735212SSteven Rostedt	} else {
9678d735212SSteven Rostedt	    if ($op eq "&&") {
9688d735212SSteven Rostedt		return 0;
9698d735212SSteven Rostedt	    }
9708d735212SSteven Rostedt	}
9718d735212SSteven Rostedt    }
97245d73a5dSSteven Rostedt
9738fddbe9bSSteven Rostedt    if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
974ab7a3f52SSteven Rostedt	my $ret = process_compare($1, $2, $3);
975ab7a3f52SSteven Rostedt	if ($ret < 0) {
976ab7a3f52SSteven Rostedt	    die "$name: $.: Unable to process comparison\n";
977ab7a3f52SSteven Rostedt	}
978ab7a3f52SSteven Rostedt	return $ret;
979ab7a3f52SSteven Rostedt    }
980ab7a3f52SSteven Rostedt
9819900b5dcSSteven Rostedt    if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
9829900b5dcSSteven Rostedt	if (defined $1) {
9839900b5dcSSteven Rostedt	    return !value_defined($2);
9849900b5dcSSteven Rostedt	} else {
9859900b5dcSSteven Rostedt	    return value_defined($2);
9869900b5dcSSteven Rostedt	}
9879900b5dcSSteven Rostedt    }
9889900b5dcSSteven Rostedt
98916723426SSteven Rostedt (VMware)    if ($val =~ s/^\s*NOT\s+(.*)//) {
99016723426SSteven Rostedt (VMware)	my $express = $1;
99116723426SSteven Rostedt (VMware)	my $ret = process_expression($name, $express);
99216723426SSteven Rostedt (VMware)	return !$ret;
99316723426SSteven Rostedt (VMware)    }
99416723426SSteven Rostedt (VMware)
99545d73a5dSSteven Rostedt    if ($val =~ /^\s*0\s*$/) {
99645d73a5dSSteven Rostedt	return 0;
99745d73a5dSSteven Rostedt    } elsif ($val =~ /^\s*\d+\s*$/) {
99845d73a5dSSteven Rostedt	return 1;
99945d73a5dSSteven Rostedt    }
100045d73a5dSSteven Rostedt
10019900b5dcSSteven Rostedt    die ("$name: $.: Undefined content $val in if statement\n");
10028d735212SSteven Rostedt}
10038d735212SSteven Rostedt
10048d735212SSteven Rostedtsub process_if {
10058d735212SSteven Rostedt    my ($name, $value) = @_;
10068d735212SSteven Rostedt
10078d735212SSteven Rostedt    # Convert variables and replace undefined ones with 0
10088d735212SSteven Rostedt    my $val = process_variables($value, 1);
10098d735212SSteven Rostedt    my $ret = process_expression $name, $val;
10108d735212SSteven Rostedt
10118d735212SSteven Rostedt    return $ret;
101245d73a5dSSteven Rostedt}
101345d73a5dSSteven Rostedt
10142ed3b161SSteven Rostedtsub __read_config {
10152ed3b161SSteven Rostedt    my ($config, $current_test_num) = @_;
1016a57419b3SSteven Rostedt
10172ed3b161SSteven Rostedt    my $in;
10182ed3b161SSteven Rostedt    open($in, $config) || die "can't read file $config";
1019a57419b3SSteven Rostedt
1020a57419b3SSteven Rostedt    my $name = $config;
1021a57419b3SSteven Rostedt    $name =~ s,.*/(.*),$1,;
1022a57419b3SSteven Rostedt
10232ed3b161SSteven Rostedt    my $test_num = $$current_test_num;
1024a57419b3SSteven Rostedt    my $default = 1;
1025a57419b3SSteven Rostedt    my $repeat = 1;
1026a57419b3SSteven Rostedt    my $num_tests_set = 0;
1027a57419b3SSteven Rostedt    my $skip = 0;
1028a57419b3SSteven Rostedt    my $rest;
1029a9f84424SSteven Rostedt    my $line;
10300df213caSSteven Rostedt    my $test_case = 0;
103145d73a5dSSteven Rostedt    my $if = 0;
103245d73a5dSSteven Rostedt    my $if_set = 0;
10333d1cc414SSteven Rostedt    my $override = 0;
10343d1cc414SSteven Rostedt
10353d1cc414SSteven Rostedt    my %overrides;
1036a57419b3SSteven Rostedt
10372ed3b161SSteven Rostedt    while (<$in>) {
1038a57419b3SSteven Rostedt
1039a57419b3SSteven Rostedt	# ignore blank lines and comments
1040a57419b3SSteven Rostedt	next if (/^\s*$/ || /\s*\#/);
1041a57419b3SSteven Rostedt
10420050b6bbSSteven Rostedt	if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
1043a57419b3SSteven Rostedt
10440050b6bbSSteven Rostedt	    my $type = $1;
10450050b6bbSSteven Rostedt	    $rest = $2;
1046a9f84424SSteven Rostedt	    $line = $2;
10470050b6bbSSteven Rostedt
10480050b6bbSSteven Rostedt	    my $old_test_num;
10490050b6bbSSteven Rostedt	    my $old_repeat;
10503d1cc414SSteven Rostedt	    $override = 0;
10510050b6bbSSteven Rostedt
10520050b6bbSSteven Rostedt	    if ($type eq "TEST_START") {
1053a57419b3SSteven Rostedt		if ($num_tests_set) {
1054a57419b3SSteven Rostedt		    die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
1055a57419b3SSteven Rostedt		}
1056a57419b3SSteven Rostedt
10570050b6bbSSteven Rostedt		$old_test_num = $test_num;
10580050b6bbSSteven Rostedt		$old_repeat = $repeat;
1059a57419b3SSteven Rostedt
1060a57419b3SSteven Rostedt		$test_num += $repeat;
1061a57419b3SSteven Rostedt		$default = 0;
1062a57419b3SSteven Rostedt		$repeat = 1;
10630050b6bbSSteven Rostedt	    } else {
10640050b6bbSSteven Rostedt		$default = 1;
10650050b6bbSSteven Rostedt	    }
1066a57419b3SSteven Rostedt
1067a9f84424SSteven Rostedt	    # If SKIP is anywhere in the line, the command will be skipped
1068a9f84424SSteven Rostedt	    if ($rest =~ s/\s+SKIP\b//) {
1069a57419b3SSteven Rostedt		$skip = 1;
1070a57419b3SSteven Rostedt	    } else {
10710df213caSSteven Rostedt		$test_case = 1;
1072a57419b3SSteven Rostedt		$skip = 0;
1073a57419b3SSteven Rostedt	    }
1074a57419b3SSteven Rostedt
1075a9f84424SSteven Rostedt	    if ($rest =~ s/\sELSE\b//) {
1076a9f84424SSteven Rostedt		if (!$if) {
1077a9f84424SSteven Rostedt		    die "$name: $.: ELSE found with out matching IF section\n$_";
1078a57419b3SSteven Rostedt		}
1079a9f84424SSteven Rostedt		$if = 0;
1080a9f84424SSteven Rostedt
1081a9f84424SSteven Rostedt		if ($if_set) {
1082a9f84424SSteven Rostedt		    $skip = 1;
1083a9f84424SSteven Rostedt		} else {
1084a9f84424SSteven Rostedt		    $skip = 0;
10853d1cc414SSteven Rostedt		}
10863d1cc414SSteven Rostedt	    }
1087a57419b3SSteven Rostedt
1088a9f84424SSteven Rostedt	    if ($rest =~ s/\sIF\s+(.*)//) {
108945d73a5dSSteven Rostedt		if (process_if($name, $1)) {
109045d73a5dSSteven Rostedt		    $if_set = 1;
109145d73a5dSSteven Rostedt		} else {
1092a57419b3SSteven Rostedt		    $skip = 1;
1093a57419b3SSteven Rostedt		}
109445d73a5dSSteven Rostedt		$if = 1;
109545d73a5dSSteven Rostedt	    } else {
109645d73a5dSSteven Rostedt		$if = 0;
1097a9f84424SSteven Rostedt		$if_set = 0;
109845d73a5dSSteven Rostedt	    }
1099a57419b3SSteven Rostedt
1100a9f84424SSteven Rostedt	    if (!$skip) {
1101a9f84424SSteven Rostedt		if ($type eq "TEST_START") {
1102a9f84424SSteven Rostedt		    if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
1103a9f84424SSteven Rostedt			$repeat = $1;
1104a9f84424SSteven Rostedt			$repeat_tests{"$test_num"} = $repeat;
1105a9f84424SSteven Rostedt		    }
1106a9f84424SSteven Rostedt		} elsif ($rest =~ s/\sOVERRIDE\b//) {
1107a9f84424SSteven Rostedt		    # DEFAULT only
1108a9f84424SSteven Rostedt		    $override = 1;
1109a9f84424SSteven Rostedt		    # Clear previous overrides
1110a9f84424SSteven Rostedt		    %overrides = ();
1111a9f84424SSteven Rostedt		}
1112a9f84424SSteven Rostedt	    }
1113a9f84424SSteven Rostedt
1114a9f84424SSteven Rostedt	    if (!$skip && $rest !~ /^\s*$/) {
11151091c8fcSMasanari Iida		die "$name: $.: Garbage found after $type\n$_";
1116a57419b3SSteven Rostedt	    }
1117a57419b3SSteven Rostedt
11180050b6bbSSteven Rostedt	    if ($skip && $type eq "TEST_START") {
1119a57419b3SSteven Rostedt		$test_num = $old_test_num;
1120e48c5293SSteven Rostedt		$repeat = $old_repeat;
1121a57419b3SSteven Rostedt	    }
1122ab7a3f52SSteven Rostedt	} elsif (/^\s*ELSE\b(.*)$/) {
112345d73a5dSSteven Rostedt	    if (!$if) {
112445d73a5dSSteven Rostedt		die "$name: $.: ELSE found with out matching IF section\n$_";
112545d73a5dSSteven Rostedt	    }
112645d73a5dSSteven Rostedt	    $rest = $1;
112745d73a5dSSteven Rostedt	    if ($if_set) {
112845d73a5dSSteven Rostedt		$skip = 1;
1129ab7a3f52SSteven Rostedt		$rest = "";
113045d73a5dSSteven Rostedt	    } else {
113145d73a5dSSteven Rostedt		$skip = 0;
113245d73a5dSSteven Rostedt
1133ab7a3f52SSteven Rostedt		if ($rest =~ /\sIF\s+(.*)/) {
113445d73a5dSSteven Rostedt		    # May be a ELSE IF section.
113595f57838SSteven Rostedt		    if (process_if($name, $1)) {
113695f57838SSteven Rostedt			$if_set = 1;
113795f57838SSteven Rostedt		    } else {
113845d73a5dSSteven Rostedt			$skip = 1;
113945d73a5dSSteven Rostedt		    }
1140ab7a3f52SSteven Rostedt		    $rest = "";
114145d73a5dSSteven Rostedt		} else {
114245d73a5dSSteven Rostedt		    $if = 0;
114345d73a5dSSteven Rostedt		}
114445d73a5dSSteven Rostedt	    }
114545d73a5dSSteven Rostedt
1146ab7a3f52SSteven Rostedt	    if ($rest !~ /^\s*$/) {
11471091c8fcSMasanari Iida		die "$name: $.: Garbage found after DEFAULTS\n$_";
1148ab7a3f52SSteven Rostedt	    }
1149ab7a3f52SSteven Rostedt
11502ed3b161SSteven Rostedt	} elsif (/^\s*INCLUDE\s+(\S+)/) {
11512ed3b161SSteven Rostedt
11522ed3b161SSteven Rostedt	    next if ($skip);
11532ed3b161SSteven Rostedt
11542ed3b161SSteven Rostedt	    if (!$default) {
11552ed3b161SSteven Rostedt		die "$name: $.: INCLUDE can only be done in default sections\n$_";
11562ed3b161SSteven Rostedt	    }
11572ed3b161SSteven Rostedt
11582ed3b161SSteven Rostedt	    my $file = process_variables($1);
11592ed3b161SSteven Rostedt
11602ed3b161SSteven Rostedt	    if ($file !~ m,^/,) {
11612ed3b161SSteven Rostedt		# check the path of the config file first
11622ed3b161SSteven Rostedt		if ($config =~ m,(.*)/,) {
11632ed3b161SSteven Rostedt		    if (-f "$1/$file") {
11642ed3b161SSteven Rostedt			$file = "$1/$file";
11652ed3b161SSteven Rostedt		    }
11662ed3b161SSteven Rostedt		}
11672ed3b161SSteven Rostedt	    }
11682ed3b161SSteven Rostedt
11692ed3b161SSteven Rostedt	    if ( ! -r $file ) {
11702ed3b161SSteven Rostedt		die "$name: $.: Can't read file $file\n$_";
11712ed3b161SSteven Rostedt	    }
11722ed3b161SSteven Rostedt
11732ed3b161SSteven Rostedt	    if (__read_config($file, \$test_num)) {
11742ed3b161SSteven Rostedt		$test_case = 1;
11752ed3b161SSteven Rostedt	    }
11762ed3b161SSteven Rostedt
1177c75d22d9SSteven Rostedt (Red Hat)	} elsif (/^\s*([A-Z_\[\]\d]+)\s*=~\s*(.*?)\s*$/) {
1178c75d22d9SSteven Rostedt (Red Hat)
1179c75d22d9SSteven Rostedt (Red Hat)	    next if ($skip);
1180c75d22d9SSteven Rostedt (Red Hat)
1181c75d22d9SSteven Rostedt (Red Hat)	    my $lvalue = $1;
1182c75d22d9SSteven Rostedt (Red Hat)	    my $rvalue = $2;
1183c75d22d9SSteven Rostedt (Red Hat)
1184c75d22d9SSteven Rostedt (Red Hat)	    if ($default || $lvalue =~ /\[\d+\]$/) {
1185c75d22d9SSteven Rostedt (Red Hat)		set_eval($lvalue, $rvalue, $name);
1186c75d22d9SSteven Rostedt (Red Hat)	    } else {
1187c75d22d9SSteven Rostedt (Red Hat)		my $val = "$lvalue\[$test_num\]";
1188c75d22d9SSteven Rostedt (Red Hat)		set_eval($val, $rvalue, $name);
1189c75d22d9SSteven Rostedt (Red Hat)	    }
1190c75d22d9SSteven Rostedt (Red Hat)
1191a57419b3SSteven Rostedt	} elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
1192a57419b3SSteven Rostedt
1193a57419b3SSteven Rostedt	    next if ($skip);
1194a57419b3SSteven Rostedt
1195a57419b3SSteven Rostedt	    my $lvalue = $1;
1196a57419b3SSteven Rostedt	    my $rvalue = $2;
1197a57419b3SSteven Rostedt
1198a57419b3SSteven Rostedt	    if (!$default &&
1199a57419b3SSteven Rostedt		($lvalue eq "NUM_TESTS" ||
1200a57419b3SSteven Rostedt		 $lvalue eq "LOG_FILE" ||
1201a57419b3SSteven Rostedt		 $lvalue eq "CLEAR_LOG")) {
1202a57419b3SSteven Rostedt		die "$name: $.: $lvalue must be set in DEFAULTS section\n";
1203a57419b3SSteven Rostedt	    }
1204a57419b3SSteven Rostedt
1205a57419b3SSteven Rostedt	    if ($lvalue eq "NUM_TESTS") {
1206a57419b3SSteven Rostedt		if ($test_num) {
1207a57419b3SSteven Rostedt		    die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
1208a57419b3SSteven Rostedt		}
1209a57419b3SSteven Rostedt		if (!$default) {
1210a57419b3SSteven Rostedt		    die "$name: $.: NUM_TESTS must be set in default section\n";
1211a57419b3SSteven Rostedt		}
1212a57419b3SSteven Rostedt		$num_tests_set = 1;
1213a57419b3SSteven Rostedt	    }
1214a57419b3SSteven Rostedt
1215a57419b3SSteven Rostedt	    if ($default || $lvalue =~ /\[\d+\]$/) {
12163d1cc414SSteven Rostedt		set_value($lvalue, $rvalue, $override, \%overrides, $name);
1217a57419b3SSteven Rostedt	    } else {
1218a57419b3SSteven Rostedt		my $val = "$lvalue\[$test_num\]";
12193d1cc414SSteven Rostedt		set_value($val, $rvalue, $override, \%overrides, $name);
1220a57419b3SSteven Rostedt
1221a57419b3SSteven Rostedt		if ($repeat > 1) {
1222a57419b3SSteven Rostedt		    $repeats{$val} = $repeat;
1223a57419b3SSteven Rostedt		}
1224a57419b3SSteven Rostedt	    }
122577d942ceSSteven Rostedt	} elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
122677d942ceSSteven Rostedt	    next if ($skip);
122777d942ceSSteven Rostedt
122877d942ceSSteven Rostedt	    my $lvalue = $1;
122977d942ceSSteven Rostedt	    my $rvalue = $2;
123077d942ceSSteven Rostedt
123177d942ceSSteven Rostedt	    # process config variables.
123277d942ceSSteven Rostedt	    # Config variables are only active while reading the
123377d942ceSSteven Rostedt	    # config and can be defined anywhere. They also ignore
123477d942ceSSteven Rostedt	    # TEST_START and DEFAULTS, but are skipped if they are in
123577d942ceSSteven Rostedt	    # on of these sections that have SKIP defined.
123677d942ceSSteven Rostedt	    # The save variable can be
123777d942ceSSteven Rostedt	    # defined multiple times and the new one simply overrides
12381091c8fcSMasanari Iida	    # the previous one.
123977d942ceSSteven Rostedt	    set_variable($lvalue, $rvalue);
124077d942ceSSteven Rostedt
1241a57419b3SSteven Rostedt	} else {
1242a57419b3SSteven Rostedt	    die "$name: $.: Garbage found in config\n$_";
1243a57419b3SSteven Rostedt	}
12442545eb61SSteven Rostedt    }
12452545eb61SSteven Rostedt
1246a57419b3SSteven Rostedt    if ($test_num) {
1247a57419b3SSteven Rostedt	$test_num += $repeat - 1;
1248a57419b3SSteven Rostedt	$opt{"NUM_TESTS"} = $test_num;
1249a57419b3SSteven Rostedt    }
1250a57419b3SSteven Rostedt
12512ed3b161SSteven Rostedt    close($in);
12522ed3b161SSteven Rostedt
12532ed3b161SSteven Rostedt    $$current_test_num = $test_num;
12542ed3b161SSteven Rostedt
12552ed3b161SSteven Rostedt    return $test_case;
12562ed3b161SSteven Rostedt}
12572ed3b161SSteven Rostedt
1258c4261d0fSSteven Rostedtsub get_test_case {
1259c4261d0fSSteven Rostedt    print "What test case would you like to run?\n";
1260c4261d0fSSteven Rostedt    print " (build, install or boot)\n";
1261edbd0edeSScott Wood    print " Other tests are available but require editing ktest.conf\n";
1262edbd0edeSScott Wood    print " (see tools/testing/ktest/sample.conf)\n";
1263c4261d0fSSteven Rostedt    my $ans = <STDIN>;
1264c4261d0fSSteven Rostedt    chomp $ans;
1265c4261d0fSSteven Rostedt    $default{"TEST_TYPE"} = $ans;
1266c4261d0fSSteven Rostedt}
1267c4261d0fSSteven Rostedt
12682ed3b161SSteven Rostedtsub read_config {
12692ed3b161SSteven Rostedt    my ($config) = @_;
12702ed3b161SSteven Rostedt
12712ed3b161SSteven Rostedt    my $test_case;
12722ed3b161SSteven Rostedt    my $test_num = 0;
12732ed3b161SSteven Rostedt
12742ed3b161SSteven Rostedt    $test_case = __read_config $config, \$test_num;
12752ed3b161SSteven Rostedt
12768d1491baSSteven Rostedt    # make sure we have all mandatory configs
12775269faadSSatoru Takeuchi    get_mandatory_configs;
12788d1491baSSteven Rostedt
12790df213caSSteven Rostedt    # was a test specified?
12800df213caSSteven Rostedt    if (!$test_case) {
12810df213caSSteven Rostedt	print "No test case specified.\n";
1282c4261d0fSSteven Rostedt	get_test_case;
12830df213caSSteven Rostedt    }
12840df213caSSteven Rostedt
1285a75fececSSteven Rostedt    # set any defaults
1286a75fececSSteven Rostedt
1287a75fececSSteven Rostedt    foreach my $default (keys %default) {
1288a75fececSSteven Rostedt	if (!defined($opt{$default})) {
1289a75fececSSteven Rostedt	    $opt{$default} = $default{$default};
1290a75fececSSteven Rostedt	}
1291a75fececSSteven Rostedt    }
12929cc9e091SSteven Rostedt
12939cc9e091SSteven Rostedt    if ($opt{"IGNORE_UNUSED"} == 1) {
12949cc9e091SSteven Rostedt	return;
12959cc9e091SSteven Rostedt    }
12969cc9e091SSteven Rostedt
12979cc9e091SSteven Rostedt    my %not_used;
12989cc9e091SSteven Rostedt
12999cc9e091SSteven Rostedt    # check if there are any stragglers (typos?)
13009cc9e091SSteven Rostedt    foreach my $option (keys %opt) {
13019cc9e091SSteven Rostedt	my $op = $option;
13029cc9e091SSteven Rostedt	# remove per test labels.
13039cc9e091SSteven Rostedt	$op =~ s/\[.*\]//;
13049cc9e091SSteven Rostedt	if (!exists($option_map{$op}) &&
13059cc9e091SSteven Rostedt	    !exists($default{$op}) &&
13069cc9e091SSteven Rostedt	    !exists($used_options{$op})) {
13079cc9e091SSteven Rostedt	    $not_used{$op} = 1;
13089cc9e091SSteven Rostedt	}
13099cc9e091SSteven Rostedt    }
13109cc9e091SSteven Rostedt
13119cc9e091SSteven Rostedt    if (%not_used) {
13129cc9e091SSteven Rostedt	my $s = "s are";
13139cc9e091SSteven Rostedt	$s = " is" if (keys %not_used == 1);
13149cc9e091SSteven Rostedt	print "The following option$s not used; could be a typo:\n";
13159cc9e091SSteven Rostedt	foreach my $option (keys %not_used) {
13169cc9e091SSteven Rostedt	    print "$option\n";
13179cc9e091SSteven Rostedt	}
13181091c8fcSMasanari Iida	print "Set IGNORE_UNUSED = 1 to have ktest ignore unused variables\n";
13199cc9e091SSteven Rostedt	if (!read_yn "Do you want to continue?") {
13209cc9e091SSteven Rostedt	    exit -1;
13219cc9e091SSteven Rostedt	}
13229cc9e091SSteven Rostedt    }
13232545eb61SSteven Rostedt}
13242545eb61SSteven Rostedt
132523715c3cSSteven Rostedtsub __eval_option {
132604262be3SSteven Rostedt (Red Hat)    my ($name, $option, $i) = @_;
132723715c3cSSteven Rostedt
132823715c3cSSteven Rostedt    # Add space to evaluate the character before $
132923715c3cSSteven Rostedt    $option = " $option";
133023715c3cSSteven Rostedt    my $retval = "";
1331f9dfb65bSRabin Vincent    my $repeated = 0;
1332f9dfb65bSRabin Vincent    my $parent = 0;
1333f9dfb65bSRabin Vincent
1334f9dfb65bSRabin Vincent    foreach my $test (keys %repeat_tests) {
1335f9dfb65bSRabin Vincent	if ($i >= $test &&
1336f9dfb65bSRabin Vincent	    $i < $test + $repeat_tests{$test}) {
1337f9dfb65bSRabin Vincent
1338f9dfb65bSRabin Vincent	    $repeated = 1;
1339f9dfb65bSRabin Vincent	    $parent = $test;
1340f9dfb65bSRabin Vincent	    last;
1341f9dfb65bSRabin Vincent	}
1342f9dfb65bSRabin Vincent    }
134323715c3cSSteven Rostedt
134423715c3cSSteven Rostedt    while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
134523715c3cSSteven Rostedt	my $start = $1;
134623715c3cSSteven Rostedt	my $var = $2;
134723715c3cSSteven Rostedt	my $end = $3;
134823715c3cSSteven Rostedt
134923715c3cSSteven Rostedt	# Append beginning of line
135023715c3cSSteven Rostedt	$retval = "$retval$start";
135123715c3cSSteven Rostedt
135223715c3cSSteven Rostedt	# If the iteration option OPT[$i] exists, then use that.
135323715c3cSSteven Rostedt	# otherwise see if the default OPT (without [$i]) exists.
135423715c3cSSteven Rostedt
135523715c3cSSteven Rostedt	my $o = "$var\[$i\]";
1356f9dfb65bSRabin Vincent	my $parento = "$var\[$parent\]";
135723715c3cSSteven Rostedt
135804262be3SSteven Rostedt (Red Hat)	# If a variable contains itself, use the default var
135904262be3SSteven Rostedt (Red Hat)	if (($var eq $name) && defined($opt{$var})) {
136004262be3SSteven Rostedt (Red Hat)	    $o = $opt{$var};
136104262be3SSteven Rostedt (Red Hat)	    $retval = "$retval$o";
136204262be3SSteven Rostedt (Red Hat)	} elsif (defined($opt{$o})) {
136323715c3cSSteven Rostedt	    $o = $opt{$o};
136423715c3cSSteven Rostedt	    $retval = "$retval$o";
1365f9dfb65bSRabin Vincent	} elsif ($repeated && defined($opt{$parento})) {
1366f9dfb65bSRabin Vincent	    $o = $opt{$parento};
1367f9dfb65bSRabin Vincent	    $retval = "$retval$o";
136823715c3cSSteven Rostedt	} elsif (defined($opt{$var})) {
136923715c3cSSteven Rostedt	    $o = $opt{$var};
137023715c3cSSteven Rostedt	    $retval = "$retval$o";
13718e80bf05SSteven Rostedt (Red Hat)	} elsif ($var eq "KERNEL_VERSION" && defined($make)) {
13728e80bf05SSteven Rostedt (Red Hat)	    # special option KERNEL_VERSION uses kernel version
13738e80bf05SSteven Rostedt (Red Hat)	    get_version();
13748e80bf05SSteven Rostedt (Red Hat)	    $retval = "$retval$version";
137523715c3cSSteven Rostedt	} else {
137623715c3cSSteven Rostedt	    $retval = "$retval\$\{$var\}";
137723715c3cSSteven Rostedt	}
137823715c3cSSteven Rostedt
137923715c3cSSteven Rostedt	$option = $end;
138023715c3cSSteven Rostedt    }
138123715c3cSSteven Rostedt
138223715c3cSSteven Rostedt    $retval = "$retval$option";
138323715c3cSSteven Rostedt
138423715c3cSSteven Rostedt    $retval =~ s/^ //;
138523715c3cSSteven Rostedt
138623715c3cSSteven Rostedt    return $retval;
138723715c3cSSteven Rostedt}
138823715c3cSSteven Rostedt
1389c75d22d9SSteven Rostedt (Red Hat)sub process_evals {
1390c75d22d9SSteven Rostedt (Red Hat)    my ($name, $option, $i) = @_;
1391c75d22d9SSteven Rostedt (Red Hat)
1392c75d22d9SSteven Rostedt (Red Hat)    my $option_name = "$name\[$i\]";
1393c75d22d9SSteven Rostedt (Red Hat)    my $ev;
1394c75d22d9SSteven Rostedt (Red Hat)
1395c75d22d9SSteven Rostedt (Red Hat)    my $old_option = $option;
1396c75d22d9SSteven Rostedt (Red Hat)
1397c75d22d9SSteven Rostedt (Red Hat)    if (defined($evals{$option_name})) {
1398c75d22d9SSteven Rostedt (Red Hat)	$ev = $evals{$option_name};
1399c75d22d9SSteven Rostedt (Red Hat)    } elsif (defined($evals{$name})) {
1400c75d22d9SSteven Rostedt (Red Hat)	$ev = $evals{$name};
1401c75d22d9SSteven Rostedt (Red Hat)    } else {
1402c75d22d9SSteven Rostedt (Red Hat)	return $option;
1403c75d22d9SSteven Rostedt (Red Hat)    }
1404c75d22d9SSteven Rostedt (Red Hat)
1405c75d22d9SSteven Rostedt (Red Hat)    for my $e (@{$ev}) {
1406c75d22d9SSteven Rostedt (Red Hat)	eval "\$option =~ $e";
1407c75d22d9SSteven Rostedt (Red Hat)    }
1408c75d22d9SSteven Rostedt (Red Hat)
1409c75d22d9SSteven Rostedt (Red Hat)    if ($option ne $old_option) {
1410c75d22d9SSteven Rostedt (Red Hat)	doprint("$name changed from '$old_option' to '$option'\n");
1411c75d22d9SSteven Rostedt (Red Hat)    }
1412c75d22d9SSteven Rostedt (Red Hat)
1413c75d22d9SSteven Rostedt (Red Hat)    return $option;
1414c75d22d9SSteven Rostedt (Red Hat)}
1415c75d22d9SSteven Rostedt (Red Hat)
141623715c3cSSteven Rostedtsub eval_option {
141704262be3SSteven Rostedt (Red Hat)    my ($name, $option, $i) = @_;
141823715c3cSSteven Rostedt
141923715c3cSSteven Rostedt    my $prev = "";
142023715c3cSSteven Rostedt
142123715c3cSSteven Rostedt    # Since an option can evaluate to another option,
142223715c3cSSteven Rostedt    # keep iterating until we do not evaluate any more
142323715c3cSSteven Rostedt    # options.
142423715c3cSSteven Rostedt    my $r = 0;
142523715c3cSSteven Rostedt    while ($prev ne $option) {
142623715c3cSSteven Rostedt	# Check for recursive evaluations.
142723715c3cSSteven Rostedt	# 100 deep should be more than enough.
142823715c3cSSteven Rostedt	if ($r++ > 100) {
14291091c8fcSMasanari Iida	    die "Over 100 evaluations occurred with $option\n" .
143023715c3cSSteven Rostedt		"Check for recursive variables\n";
143123715c3cSSteven Rostedt	}
143223715c3cSSteven Rostedt	$prev = $option;
143304262be3SSteven Rostedt (Red Hat)	$option = __eval_option($name, $option, $i);
143423715c3cSSteven Rostedt    }
143523715c3cSSteven Rostedt
1436c75d22d9SSteven Rostedt (Red Hat)    $option = process_evals($name, $option, $i);
1437c75d22d9SSteven Rostedt (Red Hat)
143823715c3cSSteven Rostedt    return $option;
143923715c3cSSteven Rostedt}
144023715c3cSSteven Rostedt
14417faafbd6SSteven Rostedtsub reboot {
14422728be41SAndrew Jones    my ($time) = @_;
14436474ace9SSteven Rostedt (VMware)    my $powercycle = 0;
14442728be41SAndrew Jones
14453e1d3678SSteven Rostedt (VMware)    # test if the machine can be connected to within a few seconds
14463e1d3678SSteven Rostedt (VMware)    my $stat = run_ssh("echo check machine status", $connect_timeout);
14476474ace9SSteven Rostedt (VMware)    if (!$stat) {
14486474ace9SSteven Rostedt (VMware)	doprint("power cycle\n");
14496474ace9SSteven Rostedt (VMware)	$powercycle = 1;
14506474ace9SSteven Rostedt (VMware)    }
14516474ace9SSteven Rostedt (VMware)
14526474ace9SSteven Rostedt (VMware)    if ($powercycle) {
14536474ace9SSteven Rostedt (VMware)	run_command "$power_cycle";
14546474ace9SSteven Rostedt (VMware)
14556474ace9SSteven Rostedt (VMware)	start_monitor;
14566474ace9SSteven Rostedt (VMware)	# flush out current monitor
14576474ace9SSteven Rostedt (VMware)	# May contain the reboot success line
14586474ace9SSteven Rostedt (VMware)	wait_for_monitor 1;
14596474ace9SSteven Rostedt (VMware)
14606474ace9SSteven Rostedt (VMware)    } else {
1461a4968722SSteven Rostedt	# Make sure everything has been written to disk
14624d00fc47SSteven Rostedt (VMware)	run_ssh("sync", 10);
1463a4968722SSteven Rostedt
14642b803365SSteven Rostedt	if (defined($time)) {
14652b803365SSteven Rostedt	    start_monitor;
14662b803365SSteven Rostedt	    # flush out current monitor
14672b803365SSteven Rostedt	    # May contain the reboot success line
14682b803365SSteven Rostedt	    wait_for_monitor 1;
14692b803365SSteven Rostedt	}
14702b803365SSteven Rostedt
14717faafbd6SSteven Rostedt	# try to reboot normally
1472e48c5293SSteven Rostedt	if (run_command $reboot) {
1473576f627cSSteven Rostedt	    if (defined($powercycle_after_reboot)) {
1474576f627cSSteven Rostedt		sleep $powercycle_after_reboot;
1475576f627cSSteven Rostedt		run_command "$power_cycle";
1476576f627cSSteven Rostedt	    }
1477576f627cSSteven Rostedt	} else {
14787faafbd6SSteven Rostedt	    # nope? power cycle it.
1479a75fececSSteven Rostedt	    run_command "$power_cycle";
14807faafbd6SSteven Rostedt	}
14816474ace9SSteven Rostedt (VMware)    }
14822728be41SAndrew Jones
14832728be41SAndrew Jones    if (defined($time)) {
14844c0b67a2SSteven Rostedt (Red Hat)
14854c0b67a2SSteven Rostedt (Red Hat)	# We only want to get to the new kernel, don't fail
14864c0b67a2SSteven Rostedt (Red Hat)	# if we stumble over a call trace.
14874c0b67a2SSteven Rostedt (Red Hat)	my $save_ignore_errors = $ignore_errors;
14884c0b67a2SSteven Rostedt (Red Hat)	$ignore_errors = 1;
14894c0b67a2SSteven Rostedt (Red Hat)
1490d6845536SSteven Rostedt (Red Hat)	# Look for the good kernel to boot
1491d6845536SSteven Rostedt (Red Hat)	if (wait_for_monitor($time, "Linux version")) {
1492407b95b7SSteven Rostedt	    # reboot got stuck?
14938a80c727SSteven Rostedt	    doprint "Reboot did not finish. Forcing power cycle\n";
1494407b95b7SSteven Rostedt	    run_command "$power_cycle";
1495407b95b7SSteven Rostedt	}
1496d6845536SSteven Rostedt (Red Hat)
14974c0b67a2SSteven Rostedt (Red Hat)	$ignore_errors = $save_ignore_errors;
14984c0b67a2SSteven Rostedt (Red Hat)
1499d6845536SSteven Rostedt (Red Hat)	# Still need to wait for the reboot to finish
1500d6845536SSteven Rostedt (Red Hat)	wait_for_monitor($time, $reboot_success_line);
1501e8bf9b98SSteven Rostedt    }
1502e8bf9b98SSteven Rostedt    if ($powercycle || $time) {
15032728be41SAndrew Jones	end_monitor;
15042728be41SAndrew Jones    }
15057faafbd6SSteven Rostedt}
15067faafbd6SSteven Rostedt
1507bc7c5803SSteven Rostedtsub reboot_to_good {
1508bc7c5803SSteven Rostedt    my ($time) = @_;
1509bc7c5803SSteven Rostedt
1510bc7c5803SSteven Rostedt    if (defined($switch_to_good)) {
1511bc7c5803SSteven Rostedt	run_command $switch_to_good;
1512bc7c5803SSteven Rostedt    }
1513bc7c5803SSteven Rostedt
1514bc7c5803SSteven Rostedt    reboot $time;
1515bc7c5803SSteven Rostedt}
1516bc7c5803SSteven Rostedt
1517576f627cSSteven Rostedtsub do_not_reboot {
1518576f627cSSteven Rostedt    my $i = $iteration;
1519576f627cSSteven Rostedt
15204ab1cce5SSteven Rostedt    return $test_type eq "build" || $no_reboot ||
1521576f627cSSteven Rostedt	($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1522649c0f12SScott Wood	($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build") ||
1523649c0f12SScott Wood	($test_type eq "config_bisect" && $opt{"CONFIG_BISECT_TYPE[$i]"} eq "build");
1524576f627cSSteven Rostedt}
1525576f627cSSteven Rostedt
1526fca797f1SSteven Rostedt (VMware)sub get_test_name() {
1527fca797f1SSteven Rostedt (VMware)    my $name;
1528fca797f1SSteven Rostedt (VMware)
1529fca797f1SSteven Rostedt (VMware)    if (defined($test_name)) {
1530fca797f1SSteven Rostedt (VMware)	$name = "$test_name:$test_type";
1531fca797f1SSteven Rostedt (VMware)    } else {
1532fca797f1SSteven Rostedt (VMware)	$name = $test_type;
1533fca797f1SSteven Rostedt (VMware)    }
1534fca797f1SSteven Rostedt (VMware)    return $name;
1535fca797f1SSteven Rostedt (VMware)}
1536fca797f1SSteven Rostedt (VMware)
15375c42fc5bSSteven Rostedtsub dodie {
15381091c8fcSMasanari Iida    # avoid recursion
1539255769a1SSteven Rostedt (VMware)    return if ($in_die);
1540255769a1SSteven Rostedt (VMware)    $in_die = 1;
1541255769a1SSteven Rostedt (VMware)
15427dc8e24fSMasami Hiramatsu (Google)    if ($monitor_cnt) {
15437dc8e24fSMasami Hiramatsu (Google)	# restore terminal settings
15447dc8e24fSMasami Hiramatsu (Google)	system("stty $stty_orig");
15457dc8e24fSMasami Hiramatsu (Google)    }
15467dc8e24fSMasami Hiramatsu (Google)
1547576f627cSSteven Rostedt    my $i = $iteration;
1548576f627cSSteven Rostedt
1549fca797f1SSteven Rostedt (VMware)    doprint "CRITICAL FAILURE... [TEST $i] ", @_, "\n";
1550fca797f1SSteven Rostedt (VMware)
1551576f627cSSteven Rostedt    if ($reboot_on_error && !do_not_reboot) {
155275c3fda7SSteven Rostedt	doprint "REBOOTING\n";
1553bc7c5803SSteven Rostedt	reboot_to_good;
1554a75fececSSteven Rostedt    } elsif ($poweroff_on_error && defined($power_off)) {
15555c42fc5bSSteven Rostedt	doprint "POWERING OFF\n";
1556a75fececSSteven Rostedt	`$power_off`;
15575c42fc5bSSteven Rostedt    }
155875c3fda7SSteven Rostedt
1559f80802cbSSteven Rostedt    if (defined($opt{"LOG_FILE"})) {
1560f80802cbSSteven Rostedt	print " See $opt{LOG_FILE} for more info.\n";
1561f80802cbSSteven Rostedt    }
1562f80802cbSSteven Rostedt
15632ceb2d85STim Tianyang Chen    if ($email_on_error) {
1564fca797f1SSteven Rostedt (VMware)	my $name = get_test_name;
156534148b13SSteven Rostedt (VMware)	my $log_file;
156634148b13SSteven Rostedt (VMware)
156734148b13SSteven Rostedt (VMware)	if (defined($opt{"LOG_FILE"})) {
1568170f4869SSteven Rostedt (VMware)	    my $whence = 2; # End of file
1569170f4869SSteven Rostedt (VMware)	    my $log_size = tell LOG;
1570170f4869SSteven Rostedt (VMware)	    my $size = $log_size - $test_log_start;
1571855d8abdSSteven Rostedt (VMware)
1572f9869002SSteven Rostedt (VMware)	    if (defined($mail_max_size)) {
1573170f4869SSteven Rostedt (VMware)		if ($size > $mail_max_size) {
1574170f4869SSteven Rostedt (VMware)		    $size = $mail_max_size;
1575f9869002SSteven Rostedt (VMware)		}
1576f9869002SSteven Rostedt (VMware)	    }
1577170f4869SSteven Rostedt (VMware)	    my $pos = - $size;
157834148b13SSteven Rostedt (VMware)	    $log_file = "$tmpdir/log";
157934148b13SSteven Rostedt (VMware)	    open (L, "$opt{LOG_FILE}") or die "Can't open $opt{LOG_FILE} to read)";
158034148b13SSteven Rostedt (VMware)	    open (O, "> $tmpdir/log") or die "Can't open $tmpdir/log\n";
1581855d8abdSSteven Rostedt (VMware)	    seek(L, $pos, $whence);
158234148b13SSteven Rostedt (VMware)	    while (<L>) {
158334148b13SSteven Rostedt (VMware)		print O;
158434148b13SSteven Rostedt (VMware)	    }
158534148b13SSteven Rostedt (VMware)	    close O;
158634148b13SSteven Rostedt (VMware)	    close L;
158734148b13SSteven Rostedt (VMware)	}
1588c043ccbfSJohn 'Warthog9' Hawley (VMware)
1589fca797f1SSteven Rostedt (VMware)	send_email("KTEST: critical failure for test $i [$name]",
159034148b13SSteven Rostedt (VMware)		"Your test started at $script_start_time has failed with:\n@_\n", $log_file);
15912ceb2d85STim Tianyang Chen    }
15922ceb2d85STim Tianyang Chen
15932e07c9f5SSteven Rostedt (VMware)    if (defined($post_test)) {
15942e07c9f5SSteven Rostedt (VMware)	run_command $post_test;
15952e07c9f5SSteven Rostedt (VMware)    }
15962e07c9f5SSteven Rostedt (VMware)
1597576f627cSSteven Rostedt    die @_, "\n";
15985c42fc5bSSteven Rostedt}
15995c42fc5bSSteven Rostedt
16009f2cdcbbSJosh Poimboeufsub create_pty {
16019f2cdcbbSJosh Poimboeuf    my ($ptm, $pts) = @_;
16029f2cdcbbSJosh Poimboeuf    my $tmp;
16039f2cdcbbSJosh Poimboeuf    my $TIOCSPTLCK = 0x40045431;
16049f2cdcbbSJosh Poimboeuf    my $TIOCGPTN = 0x80045430;
16057faafbd6SSteven Rostedt
16069f2cdcbbSJosh Poimboeuf    sysopen($ptm, "/dev/ptmx", O_RDWR | O_NONBLOCK) or
1607ff131effSColin Ian King	dodie "Can't open /dev/ptmx";
16089f2cdcbbSJosh Poimboeuf
16099f2cdcbbSJosh Poimboeuf    # unlockpt()
16109f2cdcbbSJosh Poimboeuf    $tmp = pack("i", 0);
16119f2cdcbbSJosh Poimboeuf    ioctl($ptm, $TIOCSPTLCK, $tmp) or
16129f2cdcbbSJosh Poimboeuf	dodie "ioctl TIOCSPTLCK for /dev/ptmx failed";
16139f2cdcbbSJosh Poimboeuf
16149f2cdcbbSJosh Poimboeuf    # ptsname()
16159f2cdcbbSJosh Poimboeuf    ioctl($ptm, $TIOCGPTN, $tmp) or
16169f2cdcbbSJosh Poimboeuf	dodie "ioctl TIOCGPTN for /dev/ptmx failed";
16179f2cdcbbSJosh Poimboeuf    $tmp = unpack("i", $tmp);
16189f2cdcbbSJosh Poimboeuf
16199f2cdcbbSJosh Poimboeuf    sysopen($pts, "/dev/pts/$tmp", O_RDWR | O_NONBLOCK) or
16209f2cdcbbSJosh Poimboeuf	dodie "Can't open /dev/pts/$tmp";
16219f2cdcbbSJosh Poimboeuf}
16229f2cdcbbSJosh Poimboeuf
16239f2cdcbbSJosh Poimboeufsub exec_console {
16249f2cdcbbSJosh Poimboeuf    my ($ptm, $pts) = @_;
16259f2cdcbbSJosh Poimboeuf
16269f2cdcbbSJosh Poimboeuf    close($ptm);
16279f2cdcbbSJosh Poimboeuf
16289f2cdcbbSJosh Poimboeuf    close(\*STDIN);
16299f2cdcbbSJosh Poimboeuf    close(\*STDOUT);
16309f2cdcbbSJosh Poimboeuf    close(\*STDERR);
16319f2cdcbbSJosh Poimboeuf
16329f2cdcbbSJosh Poimboeuf    open(\*STDIN, '<&', $pts);
16339f2cdcbbSJosh Poimboeuf    open(\*STDOUT, '>&', $pts);
16349f2cdcbbSJosh Poimboeuf    open(\*STDERR, '>&', $pts);
16359f2cdcbbSJosh Poimboeuf
16369f2cdcbbSJosh Poimboeuf    close($pts);
16379f2cdcbbSJosh Poimboeuf
16389f2cdcbbSJosh Poimboeuf    exec $console or
1639df46fce6STim Tianyang Chen	dodie "Can't open console $console";
16409f2cdcbbSJosh Poimboeuf}
16419f2cdcbbSJosh Poimboeuf
16429f2cdcbbSJosh Poimboeufsub open_console {
16439f2cdcbbSJosh Poimboeuf    my ($ptm) = @_;
16449f2cdcbbSJosh Poimboeuf    my $pts = \*PTSFD;
16459f2cdcbbSJosh Poimboeuf    my $pid;
16467faafbd6SSteven Rostedt
164798842782SJosh Poimboeuf    # save terminal settings
16489d2f7f05SJosh Poimboeuf    $stty_orig = `stty -g`;
16499d2f7f05SJosh Poimboeuf
16509d2f7f05SJosh Poimboeuf    # place terminal in cbreak mode so that stdin can be read one character at
16519d2f7f05SJosh Poimboeuf    # a time without having to wait for a newline
16529d2f7f05SJosh Poimboeuf    system("stty -icanon -echo -icrnl");
165398842782SJosh Poimboeuf
16549f2cdcbbSJosh Poimboeuf    create_pty($ptm, $pts);
16557faafbd6SSteven Rostedt
16569f2cdcbbSJosh Poimboeuf    $pid = fork;
16579f2cdcbbSJosh Poimboeuf
16589f2cdcbbSJosh Poimboeuf    if (!$pid) {
16599f2cdcbbSJosh Poimboeuf	# child
16609f2cdcbbSJosh Poimboeuf	exec_console($ptm, $pts)
16619f2cdcbbSJosh Poimboeuf    }
16629f2cdcbbSJosh Poimboeuf
16639f2cdcbbSJosh Poimboeuf    # parent
16649f2cdcbbSJosh Poimboeuf    close($pts);
16657faafbd6SSteven Rostedt
16667faafbd6SSteven Rostedt    return $pid;
16679f2cdcbbSJosh Poimboeuf
16689f2cdcbbSJosh Poimboeuf    open(PTSFD, "Stop perl from warning about single use of PTSFD");
16697faafbd6SSteven Rostedt}
16707faafbd6SSteven Rostedt
16717faafbd6SSteven Rostedtsub close_console {
16727faafbd6SSteven Rostedt    my ($fp, $pid) = @_;
16737faafbd6SSteven Rostedt
16747faafbd6SSteven Rostedt    doprint "kill child process $pid\n";
16755a5d8e48SSatoru Takeuchi    kill $close_console_signal, $pid;
16767faafbd6SSteven Rostedt
16779f23b129SSteven Rostedt (VMware)    doprint "wait for child process $pid to exit\n";
16789f23b129SSteven Rostedt (VMware)    waitpid($pid, 0);
16799f23b129SSteven Rostedt (VMware)
16807faafbd6SSteven Rostedt    print "closing!\n";
16817faafbd6SSteven Rostedt    close($fp);
168298842782SJosh Poimboeuf
168398842782SJosh Poimboeuf    # restore terminal settings
16849d2f7f05SJosh Poimboeuf    system("stty $stty_orig");
16857faafbd6SSteven Rostedt}
16867faafbd6SSteven Rostedt
16877faafbd6SSteven Rostedtsub start_monitor {
16887faafbd6SSteven Rostedt    if ($monitor_cnt++) {
16897faafbd6SSteven Rostedt	return;
16907faafbd6SSteven Rostedt    }
16917faafbd6SSteven Rostedt    $monitor_fp = \*MONFD;
16927faafbd6SSteven Rostedt    $monitor_pid = open_console $monitor_fp;
1693a75fececSSteven Rostedt
1694a75fececSSteven Rostedt    return;
1695a75fececSSteven Rostedt
1696a75fececSSteven Rostedt    open(MONFD, "Stop perl from warning about single use of MONFD");
16977faafbd6SSteven Rostedt}
16987faafbd6SSteven Rostedt
16997faafbd6SSteven Rostedtsub end_monitor {
1700319ab14fSSteven Rostedt (Red Hat)    return if (!defined $console);
17017faafbd6SSteven Rostedt    if (--$monitor_cnt) {
17027faafbd6SSteven Rostedt	return;
17037faafbd6SSteven Rostedt    }
17047faafbd6SSteven Rostedt    close_console($monitor_fp, $monitor_pid);
17057faafbd6SSteven Rostedt}
17067faafbd6SSteven Rostedt
17077faafbd6SSteven Rostedtsub wait_for_monitor {
17082b803365SSteven Rostedt    my ($time, $stop) = @_;
17092b803365SSteven Rostedt    my $full_line = "";
17107faafbd6SSteven Rostedt    my $line;
17112b803365SSteven Rostedt    my $booted = 0;
1712407b95b7SSteven Rostedt    my $start_time = time;
17138a80c727SSteven Rostedt    my $skip_call_trace = 0;
17148a80c727SSteven Rostedt    my $bug = 0;
17158a80c727SSteven Rostedt    my $bug_ignored = 0;
1716407b95b7SSteven Rostedt    my $now;
17177faafbd6SSteven Rostedt
1718a75fececSSteven Rostedt    doprint "** Wait for monitor to settle down **\n";
17197faafbd6SSteven Rostedt
17207faafbd6SSteven Rostedt    # read the monitor and wait for the system to calm down
17212b803365SSteven Rostedt    while (!$booted) {
17227faafbd6SSteven Rostedt	$line = wait_for_input($monitor_fp, $time);
17232b803365SSteven Rostedt	last if (!defined($line));
17242b803365SSteven Rostedt	print "$line";
17252b803365SSteven Rostedt	$full_line .= $line;
17262b803365SSteven Rostedt
17272b803365SSteven Rostedt	if (defined($stop) && $full_line =~ /$stop/) {
17282b803365SSteven Rostedt	    doprint "wait for monitor detected $stop\n";
17292b803365SSteven Rostedt	    $booted = 1;
17302b803365SSteven Rostedt	}
17312b803365SSteven Rostedt
17328a80c727SSteven Rostedt	if ($full_line =~ /\[ backtrace testing \]/) {
17338a80c727SSteven Rostedt	    $skip_call_trace = 1;
17348a80c727SSteven Rostedt	}
17358a80c727SSteven Rostedt
17368a80c727SSteven Rostedt	if ($full_line =~ /call trace:/i) {
17378a80c727SSteven Rostedt	    if (!$bug && !$skip_call_trace) {
17388a80c727SSteven Rostedt		if ($ignore_errors) {
17398a80c727SSteven Rostedt		    $bug_ignored = 1;
17408a80c727SSteven Rostedt		} else {
17418a80c727SSteven Rostedt		    $bug = 1;
17428a80c727SSteven Rostedt		}
17438a80c727SSteven Rostedt	    }
17448a80c727SSteven Rostedt	}
17458a80c727SSteven Rostedt
17468a80c727SSteven Rostedt	if ($full_line =~ /\[ end of backtrace testing \]/) {
17478a80c727SSteven Rostedt	    $skip_call_trace = 0;
17488a80c727SSteven Rostedt	}
17498a80c727SSteven Rostedt
17508a80c727SSteven Rostedt	if ($full_line =~ /Kernel panic -/) {
17518a80c727SSteven Rostedt	    $bug = 1;
17528a80c727SSteven Rostedt	}
17538a80c727SSteven Rostedt
17542b803365SSteven Rostedt	if ($line =~ /\n/) {
17552b803365SSteven Rostedt	    $full_line = "";
17562b803365SSteven Rostedt	}
1757407b95b7SSteven Rostedt	$now = time;
1758407b95b7SSteven Rostedt	if ($now - $start_time >= $max_monitor_wait) {
1759407b95b7SSteven Rostedt	    doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
1760407b95b7SSteven Rostedt	    return 1;
1761407b95b7SSteven Rostedt	}
17622b803365SSteven Rostedt    }
1763a75fececSSteven Rostedt    print "** Monitor flushed **\n";
1764995bc431SSteven Rostedt (Red Hat)
1765995bc431SSteven Rostedt (Red Hat)    # if stop is defined but wasn't hit, return error
1766995bc431SSteven Rostedt (Red Hat)    # used by reboot (which wants to see a reboot)
1767995bc431SSteven Rostedt (Red Hat)    if (defined($stop) && !$booted) {
1768995bc431SSteven Rostedt (Red Hat)	$bug = 1;
1769995bc431SSteven Rostedt (Red Hat)    }
17708a80c727SSteven Rostedt    return $bug;
17717faafbd6SSteven Rostedt}
17727faafbd6SSteven Rostedt
1773de5b6e3bSRabin Vincentsub save_logs {
1774de5b6e3bSRabin Vincent    my ($result, $basedir) = @_;
1775de5b6e3bSRabin Vincent    my @t = localtime;
1776de5b6e3bSRabin Vincent    my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1777de5b6e3bSRabin Vincent	1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1778de5b6e3bSRabin Vincent
1779de5b6e3bSRabin Vincent    my $type = $build_type;
1780de5b6e3bSRabin Vincent    if ($type =~ /useconfig/) {
1781de5b6e3bSRabin Vincent	$type = "useconfig";
1782de5b6e3bSRabin Vincent    }
1783de5b6e3bSRabin Vincent
1784de5b6e3bSRabin Vincent    my $dir = "$machine-$test_type-$type-$result-$date";
1785de5b6e3bSRabin Vincent
1786de5b6e3bSRabin Vincent    $dir = "$basedir/$dir";
1787de5b6e3bSRabin Vincent
1788de5b6e3bSRabin Vincent    if (!-d $dir) {
1789de5b6e3bSRabin Vincent	mkpath($dir) or
1790df46fce6STim Tianyang Chen	    dodie "can't create $dir";
1791de5b6e3bSRabin Vincent    }
1792de5b6e3bSRabin Vincent
1793de5b6e3bSRabin Vincent    my %files = (
1794de5b6e3bSRabin Vincent	"config" => $output_config,
1795de5b6e3bSRabin Vincent	"buildlog" => $buildlog,
1796de5b6e3bSRabin Vincent	"dmesg" => $dmesg,
1797de5b6e3bSRabin Vincent	"testlog" => $testlog,
1798de5b6e3bSRabin Vincent    );
1799de5b6e3bSRabin Vincent
1800de5b6e3bSRabin Vincent    while (my ($name, $source) = each(%files)) {
1801de5b6e3bSRabin Vincent	if (-f "$source") {
1802de5b6e3bSRabin Vincent	    cp "$source", "$dir/$name" or
1803df46fce6STim Tianyang Chen		dodie "failed to copy $source";
1804de5b6e3bSRabin Vincent	}
1805de5b6e3bSRabin Vincent    }
1806de5b6e3bSRabin Vincent
1807de5b6e3bSRabin Vincent    doprint "*** Saved info to $dir ***\n";
1808de5b6e3bSRabin Vincent}
1809de5b6e3bSRabin Vincent
18102b7d9b21SSteven Rostedtsub fail {
18112b7d9b21SSteven Rostedt
1812a75fececSSteven Rostedt    if ($die_on_failure) {
18132b7d9b21SSteven Rostedt	dodie @_;
18142b7d9b21SSteven Rostedt    }
18152b7d9b21SSteven Rostedt
1816a75fececSSteven Rostedt    doprint "FAILED\n";
18177faafbd6SSteven Rostedt
1818576f627cSSteven Rostedt    my $i = $iteration;
1819576f627cSSteven Rostedt
1820a75fececSSteven Rostedt    # no need to reboot for just building.
1821576f627cSSteven Rostedt    if (!do_not_reboot) {
18227faafbd6SSteven Rostedt	doprint "REBOOTING\n";
1823bc7c5803SSteven Rostedt	reboot_to_good $sleep_time;
1824a75fececSSteven Rostedt    }
18257faafbd6SSteven Rostedt
18269064af52SSteven Rostedt    my $name = "";
18279064af52SSteven Rostedt
18289064af52SSteven Rostedt    if (defined($test_name)) {
18299064af52SSteven Rostedt	$name = " ($test_name)";
18309064af52SSteven Rostedt    }
18319064af52SSteven Rostedt
18324bf6e1fcSSteven Rostedt (Red Hat)    print_times;
18334bf6e1fcSSteven Rostedt (Red Hat)
1834576f627cSSteven Rostedt    doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1835576f627cSSteven Rostedt    doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
18369064af52SSteven Rostedt    doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
1837576f627cSSteven Rostedt    doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1838576f627cSSteven Rostedt    doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1839a75fececSSteven Rostedt
1840de5b6e3bSRabin Vincent    if (defined($store_failures)) {
1841de5b6e3bSRabin Vincent	save_logs "fail", $store_failures;
1842cccae1a6SSteven Rostedt    }
1843cccae1a6SSteven Rostedt
18442e07c9f5SSteven Rostedt (VMware)    if (defined($post_test)) {
18452e07c9f5SSteven Rostedt (VMware)	run_command $post_test;
18462e07c9f5SSteven Rostedt (VMware)    }
18472e07c9f5SSteven Rostedt (VMware)
18482b7d9b21SSteven Rostedt    return 1;
18492b7d9b21SSteven Rostedt}
18502b7d9b21SSteven Rostedt
18512545eb61SSteven Rostedtsub run_command {
18526e98d1b4SSteven Rostedt (VMware)    my ($command, $redirect, $timeout) = @_;
1853b53486e0SSteven Rostedt (Red Hat)    my $start_time;
1854b53486e0SSteven Rostedt (Red Hat)    my $end_time;
1855d6ce2a0bSSteven Rostedt    my $dolog = 0;
1856d6ce2a0bSSteven Rostedt    my $dord = 0;
1857b337f979SSteven Rostedt (VMware)    my $dostdout = 0;
1858d6ce2a0bSSteven Rostedt    my $pid;
185937e16773SMasayoshi Mizuma    my $command_orig = $command;
1860d6ce2a0bSSteven Rostedt
1861e48c5293SSteven Rostedt    $command =~ s/\$SSH_USER/$ssh_user/g;
1862e48c5293SSteven Rostedt    $command =~ s/\$MACHINE/$machine/g;
1863e48c5293SSteven Rostedt
18644e7d2a8fSSteven Rostedt    if (!defined($timeout)) {
18654e7d2a8fSSteven Rostedt	$timeout = $run_timeout;
18664e7d2a8fSSteven Rostedt    }
18674e7d2a8fSSteven Rostedt
18684e7d2a8fSSteven Rostedt    if (!defined($timeout)) {
18694e7d2a8fSSteven Rostedt	$timeout = -1; # tell wait_for_input to wait indefinitely
18704e7d2a8fSSteven Rostedt    }
18714e7d2a8fSSteven Rostedt
1872d6ce2a0bSSteven Rostedt    doprint("$command ... ");
18736e98d1b4SSteven Rostedt (VMware)    $start_time = time;
1874d6ce2a0bSSteven Rostedt
1875d6ce2a0bSSteven Rostedt    $pid = open(CMD, "$command 2>&1 |") or
18762b7d9b21SSteven Rostedt	(fail "unable to exec $command" and return 0);
18772545eb61SSteven Rostedt
18782545eb61SSteven Rostedt    if (defined($opt{"LOG_FILE"})) {
1879d6ce2a0bSSteven Rostedt	$dolog = 1;
18806c5ee0beSSteven Rostedt    }
18816c5ee0beSSteven Rostedt
18826c5ee0beSSteven Rostedt    if (defined($redirect)) {
1883b337f979SSteven Rostedt (VMware)	if ($redirect eq 1) {
1884b337f979SSteven Rostedt (VMware)	    $dostdout = 1;
1885b337f979SSteven Rostedt (VMware)	    # Have the output of the command on its own line
1886b337f979SSteven Rostedt (VMware)	    doprint "\n";
1887b337f979SSteven Rostedt (VMware)	} else {
1888d6ce2a0bSSteven Rostedt	    open (RD, ">$redirect") or
1889d6ce2a0bSSteven Rostedt		dodie "failed to write to redirect $redirect";
1890d6ce2a0bSSteven Rostedt	    $dord = 1;
18912545eb61SSteven Rostedt	}
1892b337f979SSteven Rostedt (VMware)    }
18932545eb61SSteven Rostedt
18946e98d1b4SSteven Rostedt (VMware)    my $hit_timeout = 0;
18956e98d1b4SSteven Rostedt (VMware)
18966e98d1b4SSteven Rostedt (VMware)    while (1) {
18976e98d1b4SSteven Rostedt (VMware)	my $fp = \*CMD;
18986e98d1b4SSteven Rostedt (VMware)	my $line = wait_for_input($fp, $timeout);
18996e98d1b4SSteven Rostedt (VMware)	if (!defined($line)) {
19006e98d1b4SSteven Rostedt (VMware)	    my $now = time;
19014e7d2a8fSSteven Rostedt	    if ($timeout >= 0 && (($now - $start_time) >= $timeout)) {
19026e98d1b4SSteven Rostedt (VMware)		doprint "Hit timeout of $timeout, killing process\n";
19036e98d1b4SSteven Rostedt (VMware)		$hit_timeout = 1;
19046e98d1b4SSteven Rostedt (VMware)		kill 9, $pid;
19056e98d1b4SSteven Rostedt (VMware)	    }
19066e98d1b4SSteven Rostedt (VMware)	    last;
19076e98d1b4SSteven Rostedt (VMware)	}
19086e98d1b4SSteven Rostedt (VMware)	print LOG $line if ($dolog);
19096e98d1b4SSteven Rostedt (VMware)	print RD $line if ($dord);
1910b337f979SSteven Rostedt (VMware)	print $line if ($dostdout);
1911d6ce2a0bSSteven Rostedt    }
19122545eb61SSteven Rostedt
1913d6ce2a0bSSteven Rostedt    waitpid($pid, 0);
19145739438bSSteven Rostedt (VMware)    # shift 8 for real exit status
19155739438bSSteven Rostedt (VMware)    $run_command_status = $? >> 8;
19162545eb61SSteven Rostedt
191737e16773SMasayoshi Mizuma    if ($command_orig eq $default{REBOOT} &&
191837e16773SMasayoshi Mizuma	$run_command_status == $reboot_return_code) {
191937e16773SMasayoshi Mizuma	$run_command_status = 0;
192037e16773SMasayoshi Mizuma    }
192137e16773SMasayoshi Mizuma
1922d6ce2a0bSSteven Rostedt    close(CMD);
1923d6ce2a0bSSteven Rostedt    close(RD)  if ($dord);
1924d6ce2a0bSSteven Rostedt
1925b53486e0SSteven Rostedt (Red Hat)    $end_time = time;
1926b53486e0SSteven Rostedt (Red Hat)    my $delta = $end_time - $start_time;
1927b53486e0SSteven Rostedt (Red Hat)
1928b53486e0SSteven Rostedt (Red Hat)    if ($delta == 1) {
1929b53486e0SSteven Rostedt (Red Hat)	doprint "[1 second] ";
1930b53486e0SSteven Rostedt (Red Hat)    } else {
1931b53486e0SSteven Rostedt (Red Hat)	doprint "[$delta seconds] ";
1932b53486e0SSteven Rostedt (Red Hat)    }
1933b53486e0SSteven Rostedt (Red Hat)
19345739438bSSteven Rostedt (VMware)    if ($hit_timeout) {
19355739438bSSteven Rostedt (VMware)	$run_command_status = 1;
19365739438bSSteven Rostedt (VMware)    }
19375739438bSSteven Rostedt (VMware)
19385739438bSSteven Rostedt (VMware)    if ($run_command_status) {
19392545eb61SSteven Rostedt	doprint "FAILED!\n";
19402545eb61SSteven Rostedt    } else {
19412545eb61SSteven Rostedt	doprint "SUCCESS\n";
19422545eb61SSteven Rostedt    }
19432545eb61SSteven Rostedt
19445739438bSSteven Rostedt (VMware)    return !$run_command_status;
19455f9b6cedSSteven Rostedt}
19465f9b6cedSSteven Rostedt
1947e48c5293SSteven Rostedtsub run_ssh {
19486e98d1b4SSteven Rostedt (VMware)    my ($cmd, $timeout) = @_;
1949e48c5293SSteven Rostedt    my $cp_exec = $ssh_exec;
1950e48c5293SSteven Rostedt
1951e48c5293SSteven Rostedt    $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
19526e98d1b4SSteven Rostedt (VMware)    return run_command "$cp_exec", undef , $timeout;
1953e48c5293SSteven Rostedt}
1954e48c5293SSteven Rostedt
1955e48c5293SSteven Rostedtsub run_scp {
195602ad2617SSteven Rostedt    my ($src, $dst, $cp_scp) = @_;
1957e48c5293SSteven Rostedt
1958e48c5293SSteven Rostedt    $cp_scp =~ s/\$SRC_FILE/$src/g;
1959e48c5293SSteven Rostedt    $cp_scp =~ s/\$DST_FILE/$dst/g;
1960e48c5293SSteven Rostedt
1961e48c5293SSteven Rostedt    return run_command "$cp_scp";
1962e48c5293SSteven Rostedt}
1963e48c5293SSteven Rostedt
196402ad2617SSteven Rostedtsub run_scp_install {
196502ad2617SSteven Rostedt    my ($src, $dst) = @_;
196602ad2617SSteven Rostedt
196702ad2617SSteven Rostedt    my $cp_scp = $scp_to_target_install;
196802ad2617SSteven Rostedt
196902ad2617SSteven Rostedt    return run_scp($src, $dst, $cp_scp);
197002ad2617SSteven Rostedt}
197102ad2617SSteven Rostedt
197202ad2617SSteven Rostedtsub run_scp_mod {
197302ad2617SSteven Rostedt    my ($src, $dst) = @_;
197402ad2617SSteven Rostedt
197502ad2617SSteven Rostedt    my $cp_scp = $scp_to_target;
197602ad2617SSteven Rostedt
197702ad2617SSteven Rostedt    return run_scp($src, $dst, $cp_scp);
197802ad2617SSteven Rostedt}
197902ad2617SSteven Rostedt
1980f824b686SMasayoshi Mizumasub _get_grub_index {
1981f824b686SMasayoshi Mizuma
198226df05a8SSteven Rostedt    my ($command, $target, $skip, $submenu) = @_;
1983f824b686SMasayoshi Mizuma
1984f824b686SMasayoshi Mizuma    return if (defined($grub_number) && defined($last_grub_menu) &&
1985f824b686SMasayoshi Mizuma	$last_grub_menu eq $grub_menu && defined($last_machine) &&
1986f824b686SMasayoshi Mizuma	$last_machine eq $machine);
1987f824b686SMasayoshi Mizuma
1988f824b686SMasayoshi Mizuma    doprint "Find $reboot_type menu ... ";
1989f824b686SMasayoshi Mizuma    $grub_number = -1;
1990f824b686SMasayoshi Mizuma
1991f824b686SMasayoshi Mizuma    my $ssh_grub = $ssh_exec;
1992f824b686SMasayoshi Mizuma    $ssh_grub =~ s,\$SSH_COMMAND,$command,g;
1993f824b686SMasayoshi Mizuma
1994c043ccbfSJohn 'Warthog9' Hawley (VMware)    open(IN, "$ssh_grub |") or
1995c043ccbfSJohn 'Warthog9' Hawley (VMware)	dodie "unable to execute $command";
1996f824b686SMasayoshi Mizuma
1997f824b686SMasayoshi Mizuma    my $found = 0;
1998f824b686SMasayoshi Mizuma
199926df05a8SSteven Rostedt    my $submenu_number = 0;
200026df05a8SSteven Rostedt
2001f824b686SMasayoshi Mizuma    while (<IN>) {
2002f824b686SMasayoshi Mizuma	if (/$target/) {
2003f824b686SMasayoshi Mizuma	    $grub_number++;
2004f824b686SMasayoshi Mizuma	    $found = 1;
2005f824b686SMasayoshi Mizuma	    last;
200626df05a8SSteven Rostedt	} elsif (defined($submenu) && /$submenu/) {
200726df05a8SSteven Rostedt		$submenu_number++;
200826df05a8SSteven Rostedt		$grub_number = -1;
2009f824b686SMasayoshi Mizuma	} elsif (/$skip/) {
2010f824b686SMasayoshi Mizuma	    $grub_number++;
2011f824b686SMasayoshi Mizuma	}
2012f824b686SMasayoshi Mizuma    }
2013f824b686SMasayoshi Mizuma    close(IN);
2014f824b686SMasayoshi Mizuma
2015f824b686SMasayoshi Mizuma    dodie "Could not find '$grub_menu' through $command on $machine"
2016f824b686SMasayoshi Mizuma	if (!$found);
201726df05a8SSteven Rostedt    if ($submenu_number > 0) {
201826df05a8SSteven Rostedt	$grub_number = "$submenu_number>$grub_number";
201926df05a8SSteven Rostedt    }
2020f824b686SMasayoshi Mizuma    doprint "$grub_number\n";
2021f824b686SMasayoshi Mizuma    $last_grub_menu = $grub_menu;
2022f824b686SMasayoshi Mizuma    $last_machine = $machine;
2023f824b686SMasayoshi Mizuma}
2024f824b686SMasayoshi Mizuma
20255f9b6cedSSteven Rostedtsub get_grub_index {
20265f9b6cedSSteven Rostedt
202738891392SMasayoshi Mizuma    my $command;
202838891392SMasayoshi Mizuma    my $target;
202938891392SMasayoshi Mizuma    my $skip;
203026df05a8SSteven Rostedt    my $submenu;
203138891392SMasayoshi Mizuma    my $grub_menu_qt;
203238891392SMasayoshi Mizuma
203338891392SMasayoshi Mizuma    if ($reboot_type !~ /^grub/) {
2034a15ba913SSteven Rostedt	return;
2035a15ba913SSteven Rostedt    }
2036a15ba913SSteven Rostedt
203738891392SMasayoshi Mizuma    $grub_menu_qt = quotemeta($grub_menu);
203838891392SMasayoshi Mizuma
203938891392SMasayoshi Mizuma    if ($reboot_type eq "grub") {
204038891392SMasayoshi Mizuma	$command = "cat /boot/grub/menu.lst";
204138891392SMasayoshi Mizuma	$target = '^\s*title\s+' . $grub_menu_qt . '\s*$';
204238891392SMasayoshi Mizuma	$skip = '^\s*title\s';
204338891392SMasayoshi Mizuma    } elsif ($reboot_type eq "grub2") {
204438891392SMasayoshi Mizuma	$command = "cat $grub_file";
204526df05a8SSteven Rostedt	$target = '^\s*menuentry.*' . $grub_menu_qt;
2046*6ba8ecf9SDaniel Jordan	$skip = '^\s*menuentry\s';
204726df05a8SSteven Rostedt	$submenu = '^\s*submenu\s';
2048ac246645SMasayoshi Mizuma    } elsif ($reboot_type eq "grub2bls") {
2049ac246645SMasayoshi Mizuma	$command = $grub_bls_get;
2050ac246645SMasayoshi Mizuma	$target = '^title=.*' . $grub_menu_qt;
2051ac246645SMasayoshi Mizuma	$skip = '^title=';
205238891392SMasayoshi Mizuma    } else {
2053a75fececSSteven Rostedt	return;
2054a75fececSSteven Rostedt    }
20555f9b6cedSSteven Rostedt
205626df05a8SSteven Rostedt    _get_grub_index($command, $target, $skip, $submenu);
20572545eb61SSteven Rostedt}
20582545eb61SSteven Rostedt
2059c043ccbfSJohn 'Warthog9' Hawley (VMware)sub wait_for_input {
20602545eb61SSteven Rostedt    my ($fp, $time) = @_;
2061f7c6401fSSteven Rostedt (VMware)    my $start_time;
20622545eb61SSteven Rostedt    my $rin;
20639d2f7f05SJosh Poimboeuf    my $rout;
20649d2f7f05SJosh Poimboeuf    my $nr;
20659d2f7f05SJosh Poimboeuf    my $buf;
20662545eb61SSteven Rostedt    my $line;
20672545eb61SSteven Rostedt    my $ch;
20682545eb61SSteven Rostedt
20692545eb61SSteven Rostedt    if (!defined($time)) {
20702545eb61SSteven Rostedt	$time = $timeout;
20712545eb61SSteven Rostedt    }
20722545eb61SSteven Rostedt
20734e7d2a8fSSteven Rostedt    if ($time < 0) {
20744e7d2a8fSSteven Rostedt	# Negative number means wait indefinitely
20754e7d2a8fSSteven Rostedt	undef $time;
20764e7d2a8fSSteven Rostedt    }
20774e7d2a8fSSteven Rostedt
20782545eb61SSteven Rostedt    $rin = '';
20792545eb61SSteven Rostedt    vec($rin, fileno($fp), 1) = 1;
20809d2f7f05SJosh Poimboeuf    vec($rin, fileno(\*STDIN), 1) = 1;
20819d2f7f05SJosh Poimboeuf
2082f7c6401fSSteven Rostedt (VMware)    $start_time = time;
2083f7c6401fSSteven Rostedt (VMware)
20849d2f7f05SJosh Poimboeuf    while (1) {
20859d2f7f05SJosh Poimboeuf	$nr = select($rout=$rin, undef, undef, $time);
20869d2f7f05SJosh Poimboeuf
2087f7c6401fSSteven Rostedt (VMware)	last if ($nr <= 0);
20889d2f7f05SJosh Poimboeuf
20899d2f7f05SJosh Poimboeuf	# copy data from stdin to the console
20909d2f7f05SJosh Poimboeuf	if (vec($rout, fileno(\*STDIN), 1) == 1) {
209199c014a8SSteven Rostedt (VMware)	    $nr = sysread(\*STDIN, $buf, 1000);
209299c014a8SSteven Rostedt (VMware)	    syswrite($fp, $buf, $nr) if ($nr > 0);
20939d2f7f05SJosh Poimboeuf	}
20942545eb61SSteven Rostedt
2095f7c6401fSSteven Rostedt (VMware)	# The timeout is based on time waiting for the fp data
2096f7c6401fSSteven Rostedt (VMware)	if (vec($rout, fileno($fp), 1) != 1) {
2097f7c6401fSSteven Rostedt (VMware)	    last if (defined($time) && (time - $start_time > $time));
20982545eb61SSteven Rostedt	    next;
20992545eb61SSteven Rostedt	}
21002545eb61SSteven Rostedt
21012545eb61SSteven Rostedt	$line = "";
21022545eb61SSteven Rostedt
21032545eb61SSteven Rostedt	# try to read one char at a time
21042545eb61SSteven Rostedt	while (sysread $fp, $ch, 1) {
21052545eb61SSteven Rostedt	    $line .= $ch;
21062545eb61SSteven Rostedt	    last if ($ch eq "\n");
21072545eb61SSteven Rostedt	}
21082545eb61SSteven Rostedt
2109f7c6401fSSteven Rostedt (VMware)	last if (!length($line));
21102545eb61SSteven Rostedt
21112545eb61SSteven Rostedt	return $line;
21122545eb61SSteven Rostedt    }
2113f7c6401fSSteven Rostedt (VMware)    return undef;
21149d2f7f05SJosh Poimboeuf}
21152545eb61SSteven Rostedt
211675c3fda7SSteven Rostedtsub reboot_to {
2117bc7c5803SSteven Rostedt    if (defined($switch_to_test)) {
2118bc7c5803SSteven Rostedt	run_command $switch_to_test;
2119bc7c5803SSteven Rostedt    }
2120bc7c5803SSteven Rostedt
2121a75fececSSteven Rostedt    if ($reboot_type eq "grub") {
2122c54367f9SSteven Rostedt	run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
2123271e0c9dSLibo Chen    } elsif (($reboot_type eq "grub2") or ($reboot_type eq "grub2bls")) {
212426df05a8SSteven Rostedt	run_ssh "$grub_reboot \"'$grub_number'\"";
21257786954cSSteven Rostedt    } elsif ($reboot_type eq "syslinux") {
21267786954cSSteven Rostedt	run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
212796f6a0dfSSteven Rostedt    } elsif (defined $reboot_script) {
2128a75fececSSteven Rostedt	run_command "$reboot_script";
21292545eb61SSteven Rostedt    }
213096f6a0dfSSteven Rostedt    reboot;
213196f6a0dfSSteven Rostedt}
21322545eb61SSteven Rostedt
2133a57419b3SSteven Rostedtsub get_sha1 {
2134a57419b3SSteven Rostedt    my ($commit) = @_;
2135a57419b3SSteven Rostedt
2136a57419b3SSteven Rostedt    doprint "git rev-list --max-count=1 $commit ... ";
2137a57419b3SSteven Rostedt    my $sha1 = `git rev-list --max-count=1 $commit`;
2138a57419b3SSteven Rostedt    my $ret = $?;
2139a57419b3SSteven Rostedt
2140a57419b3SSteven Rostedt    logit $sha1;
2141a57419b3SSteven Rostedt
2142a57419b3SSteven Rostedt    if ($ret) {
2143a57419b3SSteven Rostedt	doprint "FAILED\n";
2144a57419b3SSteven Rostedt	dodie "Failed to get git $commit";
2145a57419b3SSteven Rostedt    }
2146a57419b3SSteven Rostedt
2147a57419b3SSteven Rostedt    print "SUCCESS\n";
2148a57419b3SSteven Rostedt
2149a57419b3SSteven Rostedt    chomp $sha1;
2150a57419b3SSteven Rostedt
2151a57419b3SSteven Rostedt    return $sha1;
2152a57419b3SSteven Rostedt}
2153a57419b3SSteven Rostedt
21545a391fbfSSteven Rostedtsub monitor {
21552545eb61SSteven Rostedt    my $booted = 0;
21562545eb61SSteven Rostedt    my $bug = 0;
21576ca996ccSSteven Rostedt    my $bug_ignored = 0;
21585c42fc5bSSteven Rostedt    my $skip_call_trace = 0;
21592b7d9b21SSteven Rostedt    my $loops;
21602545eb61SSteven Rostedt
216138fa3dc1SSteven Rostedt (Red Hat)    my $start_time = time;
216238fa3dc1SSteven Rostedt (Red Hat)
21637faafbd6SSteven Rostedt    wait_for_monitor 5;
21642545eb61SSteven Rostedt
21652545eb61SSteven Rostedt    my $line;
21662545eb61SSteven Rostedt    my $full_line = "";
21672545eb61SSteven Rostedt
21687faafbd6SSteven Rostedt    open(DMESG, "> $dmesg") or
2169df46fce6STim Tianyang Chen	dodie "unable to write to $dmesg";
21702545eb61SSteven Rostedt
217175c3fda7SSteven Rostedt    reboot_to;
21722545eb61SSteven Rostedt
21731c8a617aSSteven Rostedt    my $success_start;
21741c8a617aSSteven Rostedt    my $failure_start;
21752d01b26aSSteven Rostedt    my $monitor_start = time;
21762d01b26aSSteven Rostedt    my $done = 0;
2177f1a5b962SSteven Rostedt    my $version_found = 0;
21781c8a617aSSteven Rostedt
21792d01b26aSSteven Rostedt    while (!$done) {
2180ecaf8e52SSteven Rostedt	if ($bug && defined($stop_after_failure) &&
2181ecaf8e52SSteven Rostedt	    $stop_after_failure >= 0) {
2182ecaf8e52SSteven Rostedt	    my $time = $stop_after_failure - (time - $failure_start);
2183ecaf8e52SSteven Rostedt	    $line = wait_for_input($monitor_fp, $time);
2184ecaf8e52SSteven Rostedt	    if (!defined($line)) {
2185ecaf8e52SSteven Rostedt		doprint "bug timed out after $booted_timeout seconds\n";
2186ecaf8e52SSteven Rostedt		doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
2187ecaf8e52SSteven Rostedt		last;
2188ecaf8e52SSteven Rostedt	    }
2189ecaf8e52SSteven Rostedt	} elsif ($booted) {
2190a75fececSSteven Rostedt	    $line = wait_for_input($monitor_fp, $booted_timeout);
2191cd4f1d53SSteven Rostedt	    if (!defined($line)) {
2192cd4f1d53SSteven Rostedt		my $s = $booted_timeout == 1 ? "" : "s";
2193cd4f1d53SSteven Rostedt		doprint "Successful boot found: break after $booted_timeout second$s\n";
2194cd4f1d53SSteven Rostedt		last;
2195cd4f1d53SSteven Rostedt	    }
21962b7d9b21SSteven Rostedt	} else {
21977faafbd6SSteven Rostedt	    $line = wait_for_input($monitor_fp);
2198cd4f1d53SSteven Rostedt	    if (!defined($line)) {
2199cd4f1d53SSteven Rostedt		my $s = $timeout == 1 ? "" : "s";
2200cd4f1d53SSteven Rostedt		doprint "Timed out after $timeout second$s\n";
2201cd4f1d53SSteven Rostedt		last;
22022b7d9b21SSteven Rostedt	    }
2203cd4f1d53SSteven Rostedt	}
22042545eb61SSteven Rostedt
22052545eb61SSteven Rostedt	doprint $line;
22067faafbd6SSteven Rostedt	print DMESG $line;
22072545eb61SSteven Rostedt
22082545eb61SSteven Rostedt	# we are not guaranteed to get a full line
22092545eb61SSteven Rostedt	$full_line .= $line;
22102545eb61SSteven Rostedt
2211a75fececSSteven Rostedt	if ($full_line =~ /$success_line/) {
22122545eb61SSteven Rostedt	    $booted = 1;
22131c8a617aSSteven Rostedt	    $success_start = time;
22141c8a617aSSteven Rostedt	}
22151c8a617aSSteven Rostedt
22161c8a617aSSteven Rostedt	if ($booted && defined($stop_after_success) &&
22171c8a617aSSteven Rostedt	    $stop_after_success >= 0) {
22181c8a617aSSteven Rostedt	    my $now = time;
22191c8a617aSSteven Rostedt	    if ($now - $success_start >= $stop_after_success) {
22201c8a617aSSteven Rostedt		doprint "Test forced to stop after $stop_after_success seconds after success\n";
22211c8a617aSSteven Rostedt		last;
22221c8a617aSSteven Rostedt	    }
22232545eb61SSteven Rostedt	}
22242545eb61SSteven Rostedt
22255c42fc5bSSteven Rostedt	if ($full_line =~ /\[ backtrace testing \]/) {
22265c42fc5bSSteven Rostedt	    $skip_call_trace = 1;
22275c42fc5bSSteven Rostedt	}
22285c42fc5bSSteven Rostedt
22292545eb61SSteven Rostedt	if ($full_line =~ /call trace:/i) {
22306ca996ccSSteven Rostedt	    if (!$bug && !$skip_call_trace) {
22316ca996ccSSteven Rostedt		if ($ignore_errors) {
22326ca996ccSSteven Rostedt		    $bug_ignored = 1;
22336ca996ccSSteven Rostedt		} else {
22341c8a617aSSteven Rostedt		    $bug = 1;
22351c8a617aSSteven Rostedt		    $failure_start = time;
22361c8a617aSSteven Rostedt		}
22371c8a617aSSteven Rostedt	    }
22386ca996ccSSteven Rostedt	}
22391c8a617aSSteven Rostedt
22401c8a617aSSteven Rostedt	if ($bug && defined($stop_after_failure) &&
22411c8a617aSSteven Rostedt	    $stop_after_failure >= 0) {
22421c8a617aSSteven Rostedt	    my $now = time;
22431c8a617aSSteven Rostedt	    if ($now - $failure_start >= $stop_after_failure) {
22441c8a617aSSteven Rostedt		doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
22451c8a617aSSteven Rostedt		last;
22461c8a617aSSteven Rostedt	    }
22475c42fc5bSSteven Rostedt	}
22485c42fc5bSSteven Rostedt
22495c42fc5bSSteven Rostedt	if ($full_line =~ /\[ end of backtrace testing \]/) {
22505c42fc5bSSteven Rostedt	    $skip_call_trace = 0;
22515c42fc5bSSteven Rostedt	}
22525c42fc5bSSteven Rostedt
22535c42fc5bSSteven Rostedt	if ($full_line =~ /Kernel panic -/) {
225410abf118SSteven Rostedt	    $failure_start = time;
22552545eb61SSteven Rostedt	    $bug = 1;
22562545eb61SSteven Rostedt	}
22572545eb61SSteven Rostedt
2258f1a5b962SSteven Rostedt	# Detect triple faults by testing the banner
2259f1a5b962SSteven Rostedt	if ($full_line =~ /\bLinux version (\S+).*\n/) {
2260f1a5b962SSteven Rostedt	    if ($1 eq $version) {
2261f1a5b962SSteven Rostedt		$version_found = 1;
2262f1a5b962SSteven Rostedt	    } elsif ($version_found && $detect_triplefault) {
2263f1a5b962SSteven Rostedt		# We already booted into the kernel we are testing,
2264f1a5b962SSteven Rostedt		# but now we booted into another kernel?
2265f1a5b962SSteven Rostedt		# Consider this a triple fault.
22668b513d0cSMasanari Iida		doprint "Already booted in Linux kernel $version, but now\n";
2267f1a5b962SSteven Rostedt		doprint "we booted into Linux kernel $1.\n";
2268f1a5b962SSteven Rostedt		doprint "Assuming that this is a triple fault.\n";
2269f1a5b962SSteven Rostedt		doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
2270f1a5b962SSteven Rostedt		last;
2271f1a5b962SSteven Rostedt	    }
2272f1a5b962SSteven Rostedt	}
2273f1a5b962SSteven Rostedt
22742545eb61SSteven Rostedt	if ($line =~ /\n/) {
22752545eb61SSteven Rostedt	    $full_line = "";
22762545eb61SSteven Rostedt	}
22772d01b26aSSteven Rostedt
22782d01b26aSSteven Rostedt	if ($stop_test_after > 0 && !$booted && !$bug) {
22792d01b26aSSteven Rostedt	    if (time - $monitor_start > $stop_test_after) {
22804d62bf51SSteven Rostedt		doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
22812d01b26aSSteven Rostedt		$done = 1;
22822d01b26aSSteven Rostedt	    }
22832d01b26aSSteven Rostedt	}
22842545eb61SSteven Rostedt    }
22852545eb61SSteven Rostedt
228638fa3dc1SSteven Rostedt (Red Hat)    my $end_time = time;
228738fa3dc1SSteven Rostedt (Red Hat)    $reboot_time = $end_time - $start_time;
228838fa3dc1SSteven Rostedt (Red Hat)
22897faafbd6SSteven Rostedt    close(DMESG);
22902545eb61SSteven Rostedt
22912545eb61SSteven Rostedt    if ($bug) {
22922b7d9b21SSteven Rostedt	return 0 if ($in_bisect);
2293576f627cSSteven Rostedt	fail "failed - got a bug report" and return 0;
22942545eb61SSteven Rostedt    }
22955f9b6cedSSteven Rostedt
2296a75fececSSteven Rostedt    if (!$booted) {
2297a75fececSSteven Rostedt	return 0 if ($in_bisect);
2298576f627cSSteven Rostedt	fail "failed - never got a boot prompt." and return 0;
2299a75fececSSteven Rostedt    }
2300a75fececSSteven Rostedt
23016ca996ccSSteven Rostedt    if ($bug_ignored) {
23026ca996ccSSteven Rostedt	doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
23036ca996ccSSteven Rostedt    }
23046ca996ccSSteven Rostedt
23052b7d9b21SSteven Rostedt    return 1;
23062545eb61SSteven Rostedt}
23072545eb61SSteven Rostedt
23082b29b2f8SSteven Rostedtsub eval_kernel_version {
23092b29b2f8SSteven Rostedt    my ($option) = @_;
23102b29b2f8SSteven Rostedt
23112b29b2f8SSteven Rostedt    $option =~ s/\$KERNEL_VERSION/$version/g;
23122b29b2f8SSteven Rostedt
23132b29b2f8SSteven Rostedt    return $option;
23142b29b2f8SSteven Rostedt}
23152b29b2f8SSteven Rostedt
2316db05cfefSSteven Rostedtsub do_post_install {
2317db05cfefSSteven Rostedt
2318db05cfefSSteven Rostedt    return if (!defined($post_install));
2319db05cfefSSteven Rostedt
23202b29b2f8SSteven Rostedt    my $cp_post_install = eval_kernel_version $post_install;
2321db05cfefSSteven Rostedt    run_command "$cp_post_install" or
2322db05cfefSSteven Rostedt	dodie "Failed to run post install";
2323db05cfefSSteven Rostedt}
2324db05cfefSSteven Rostedt
2325e1a6c3d7SSteven Rostedt# Sometimes the reboot fails, and will hang. We try to ssh to the box
2326e1a6c3d7SSteven Rostedt# and if we fail, we force another reboot, that should powercycle it.
2327e1a6c3d7SSteven Rostedtsub test_booted {
2328e1a6c3d7SSteven Rostedt    if (!run_ssh "echo testing connection") {
2329e1a6c3d7SSteven Rostedt	reboot $sleep_time;
2330e1a6c3d7SSteven Rostedt    }
2331e1a6c3d7SSteven Rostedt}
2332e1a6c3d7SSteven Rostedt
23332545eb61SSteven Rostedtsub install {
23342545eb61SSteven Rostedt
2335e0a8742eSSteven Rostedt    return if ($no_install);
2336e0a8742eSSteven Rostedt
233738fa3dc1SSteven Rostedt (Red Hat)    my $start_time = time;
233838fa3dc1SSteven Rostedt (Red Hat)
2339e5c2ec11SSteven Rostedt    if (defined($pre_install)) {
2340e5c2ec11SSteven Rostedt	my $cp_pre_install = eval_kernel_version $pre_install;
2341e5c2ec11SSteven Rostedt	run_command "$cp_pre_install" or
2342e5c2ec11SSteven Rostedt	    dodie "Failed to run pre install";
2343e5c2ec11SSteven Rostedt    }
2344e5c2ec11SSteven Rostedt
23452b29b2f8SSteven Rostedt    my $cp_target = eval_kernel_version $target_image;
23462b29b2f8SSteven Rostedt
2347e1a6c3d7SSteven Rostedt    test_booted;
2348e1a6c3d7SSteven Rostedt
234902ad2617SSteven Rostedt    run_scp_install "$outputdir/$build_target", "$cp_target" or
23505c42fc5bSSteven Rostedt	dodie "failed to copy image";
23515f9b6cedSSteven Rostedt
23525f9b6cedSSteven Rostedt    my $install_mods = 0;
23535f9b6cedSSteven Rostedt
23545f9b6cedSSteven Rostedt    # should we process modules?
23555f9b6cedSSteven Rostedt    $install_mods = 0;
235651ad1dd1SSteven Rostedt    open(IN, "$output_config") or dodie("Can't read config file");
23575f9b6cedSSteven Rostedt    while (<IN>) {
23585f9b6cedSSteven Rostedt	if (/CONFIG_MODULES(=y)?/) {
23598bc5e4eaSSteven Rostedt	    if (defined($1)) {
23608bc5e4eaSSteven Rostedt		$install_mods = 1;
23615f9b6cedSSteven Rostedt		last;
23625f9b6cedSSteven Rostedt	    }
23635f9b6cedSSteven Rostedt	}
23648bc5e4eaSSteven Rostedt    }
23655f9b6cedSSteven Rostedt    close(IN);
23665f9b6cedSSteven Rostedt
23675f9b6cedSSteven Rostedt    if (!$install_mods) {
2368db05cfefSSteven Rostedt	do_post_install;
23695f9b6cedSSteven Rostedt	doprint "No modules needed\n";
237038fa3dc1SSteven Rostedt (Red Hat)	my $end_time = time;
237138fa3dc1SSteven Rostedt (Red Hat)	$install_time = $end_time - $start_time;
23725f9b6cedSSteven Rostedt	return;
23732545eb61SSteven Rostedt    }
23742545eb61SSteven Rostedt
2375627977d8SSteven Rostedt    run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
23765f9b6cedSSteven Rostedt	dodie "Failed to install modules";
23775f9b6cedSSteven Rostedt
23782545eb61SSteven Rostedt    my $modlib = "/lib/modules/$version";
2379a57419b3SSteven Rostedt    my $modtar = "ktest-mods.tar.bz2";
23802545eb61SSteven Rostedt
2381e48c5293SSteven Rostedt    run_ssh "rm -rf $modlib" or
23825c42fc5bSSteven Rostedt	dodie "failed to remove old mods: $modlib";
23832545eb61SSteven Rostedt
23845c42fc5bSSteven Rostedt    # would be nice if scp -r did not follow symbolic links
2385a75fececSSteven Rostedt    run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
23865c42fc5bSSteven Rostedt	dodie "making tarball";
23875c42fc5bSSteven Rostedt
238802ad2617SSteven Rostedt    run_scp_mod "$tmpdir/$modtar", "/tmp" or
23895c42fc5bSSteven Rostedt	dodie "failed to copy modules";
23905c42fc5bSSteven Rostedt
2391a75fececSSteven Rostedt    unlink "$tmpdir/$modtar";
23925c42fc5bSSteven Rostedt
2393e7b13441SSteven Rostedt    run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
23945c42fc5bSSteven Rostedt	dodie "failed to tar modules";
23955c42fc5bSSteven Rostedt
2396e48c5293SSteven Rostedt    run_ssh "rm -f /tmp/$modtar";
23978b37ca8cSSteven Rostedt
2398db05cfefSSteven Rostedt    do_post_install;
239938fa3dc1SSteven Rostedt (Red Hat)
240038fa3dc1SSteven Rostedt (Red Hat)    my $end_time = time;
240138fa3dc1SSteven Rostedt (Red Hat)    $install_time = $end_time - $start_time;
24022545eb61SSteven Rostedt}
24032545eb61SSteven Rostedt
2404ddf607e5SSteven Rostedtsub get_version {
2405ddf607e5SSteven Rostedt    # get the release name
2406683a3e64SSteven Rostedt    return if ($have_version);
2407ddf607e5SSteven Rostedt    doprint "$make kernelrelease ... ";
240817150fefSSteven Rostedt (Red Hat)    $version = `$make -s kernelrelease | tail -1`;
2409ddf607e5SSteven Rostedt    chomp($version);
2410ddf607e5SSteven Rostedt    doprint "$version\n";
2411683a3e64SSteven Rostedt    $have_version = 1;
2412ddf607e5SSteven Rostedt}
2413ddf607e5SSteven Rostedt
241464d98283SSteven Rostedt (Red Hat)sub start_monitor_and_install {
24159f7424ccSSteven Rostedt    # Make sure the stable kernel has finished booting
2416319ab14fSSteven Rostedt (Red Hat)
2417319ab14fSSteven Rostedt (Red Hat)    # Install bisects, don't need console
2418319ab14fSSteven Rostedt (Red Hat)    if (defined $console) {
24199f7424ccSSteven Rostedt	start_monitor;
24209f7424ccSSteven Rostedt	wait_for_monitor 5;
24219f7424ccSSteven Rostedt	end_monitor;
2422319ab14fSSteven Rostedt (Red Hat)    }
24239f7424ccSSteven Rostedt
2424ddf607e5SSteven Rostedt    get_grub_index;
2425ddf607e5SSteven Rostedt    get_version;
2426ddf607e5SSteven Rostedt    install;
2427ddf607e5SSteven Rostedt
2428319ab14fSSteven Rostedt (Red Hat)    start_monitor if (defined $console);
2429ddf607e5SSteven Rostedt    return monitor;
2430ddf607e5SSteven Rostedt}
2431ddf607e5SSteven Rostedt
24327328735cSSteven Rostedt (Red Hat)sub process_warning_line {
24337328735cSSteven Rostedt (Red Hat)    my ($line) = @_;
24347328735cSSteven Rostedt (Red Hat)
24357328735cSSteven Rostedt (Red Hat)    chomp $line;
24367328735cSSteven Rostedt (Red Hat)
24377328735cSSteven Rostedt (Red Hat)    # for distcc heterogeneous systems, some compilers
24387328735cSSteven Rostedt (Red Hat)    # do things differently causing warning lines
24397328735cSSteven Rostedt (Red Hat)    # to be slightly different. This makes an attempt
24407328735cSSteven Rostedt (Red Hat)    # to fixe those issues.
24417328735cSSteven Rostedt (Red Hat)
24427328735cSSteven Rostedt (Red Hat)    # chop off the index into the line
24437328735cSSteven Rostedt (Red Hat)    # using distcc, some compilers give different indexes
24447328735cSSteven Rostedt (Red Hat)    # depending on white space
24457328735cSSteven Rostedt (Red Hat)    $line =~ s/^(\s*\S+:\d+:)\d+/$1/;
24467328735cSSteven Rostedt (Red Hat)
24477328735cSSteven Rostedt (Red Hat)    # Some compilers use UTF-8 extended for quotes and some don't.
24487328735cSSteven Rostedt (Red Hat)    $line =~ s/$utf8_quote/'/g;
24497328735cSSteven Rostedt (Red Hat)
24507328735cSSteven Rostedt (Red Hat)    return $line;
24517328735cSSteven Rostedt (Red Hat)}
24527328735cSSteven Rostedt (Red Hat)
24534283b169SSteven Rostedt (Red Hat)# Read buildlog and check against warnings file for any
24544283b169SSteven Rostedt (Red Hat)# new warnings.
24554283b169SSteven Rostedt (Red Hat)#
24564283b169SSteven Rostedt (Red Hat)# Returns 1 if OK
24574283b169SSteven Rostedt (Red Hat)#         0 otherwise
24586c5ee0beSSteven Rostedtsub check_buildlog {
24594283b169SSteven Rostedt (Red Hat)    return 1 if (!defined $warnings_file);
24604283b169SSteven Rostedt (Red Hat)
24614283b169SSteven Rostedt (Red Hat)    my %warnings_list;
24624283b169SSteven Rostedt (Red Hat)
24634283b169SSteven Rostedt (Red Hat)    # Failed builds should not reboot the target
24644283b169SSteven Rostedt (Red Hat)    my $save_no_reboot = $no_reboot;
24654283b169SSteven Rostedt (Red Hat)    $no_reboot = 1;
24664283b169SSteven Rostedt (Red Hat)
24674283b169SSteven Rostedt (Red Hat)    if (-f $warnings_file) {
24684283b169SSteven Rostedt (Red Hat)	open(IN, $warnings_file) or
24694283b169SSteven Rostedt (Red Hat)	    dodie "Error opening $warnings_file";
24704283b169SSteven Rostedt (Red Hat)
24714283b169SSteven Rostedt (Red Hat)	while (<IN>) {
24724283b169SSteven Rostedt (Red Hat)	    if (/$check_build_re/) {
24737328735cSSteven Rostedt (Red Hat)		my $warning = process_warning_line $_;
24747328735cSSteven Rostedt (Red Hat)
24757328735cSSteven Rostedt (Red Hat)		$warnings_list{$warning} = 1;
24764283b169SSteven Rostedt (Red Hat)	    }
24774283b169SSteven Rostedt (Red Hat)	}
24784283b169SSteven Rostedt (Red Hat)	close(IN);
24794283b169SSteven Rostedt (Red Hat)    }
24804283b169SSteven Rostedt (Red Hat)
24814283b169SSteven Rostedt (Red Hat)    # If warnings file didn't exist, and WARNINGS_FILE exist,
24824283b169SSteven Rostedt (Red Hat)    # then we fail on any warning!
24834283b169SSteven Rostedt (Red Hat)
24844283b169SSteven Rostedt (Red Hat)    open(IN, $buildlog) or dodie "Can't open $buildlog";
24854283b169SSteven Rostedt (Red Hat)    while (<IN>) {
24864283b169SSteven Rostedt (Red Hat)	if (/$check_build_re/) {
24877328735cSSteven Rostedt (Red Hat)	    my $warning = process_warning_line $_;
24884283b169SSteven Rostedt (Red Hat)
24897328735cSSteven Rostedt (Red Hat)	    if (!defined $warnings_list{$warning}) {
24904283b169SSteven Rostedt (Red Hat)		fail "New warning found (not in $warnings_file)\n$_\n";
24914283b169SSteven Rostedt (Red Hat)		$no_reboot = $save_no_reboot;
24924283b169SSteven Rostedt (Red Hat)		return 0;
24934283b169SSteven Rostedt (Red Hat)	    }
24944283b169SSteven Rostedt (Red Hat)	}
24954283b169SSteven Rostedt (Red Hat)    }
24964283b169SSteven Rostedt (Red Hat)    $no_reboot = $save_no_reboot;
24974283b169SSteven Rostedt (Red Hat)    close(IN);
24984283b169SSteven Rostedt (Red Hat)}
24994283b169SSteven Rostedt (Red Hat)
25004283b169SSteven Rostedt (Red Hat)sub check_patch_buildlog {
25016c5ee0beSSteven Rostedt    my ($patch) = @_;
25026c5ee0beSSteven Rostedt
25036c5ee0beSSteven Rostedt    my @files = `git show $patch | diffstat -l`;
25046c5ee0beSSteven Rostedt
250535275685SSteven Rostedt (Red Hat)    foreach my $file (@files) {
250635275685SSteven Rostedt (Red Hat)	chomp $file;
250735275685SSteven Rostedt (Red Hat)    }
250835275685SSteven Rostedt (Red Hat)
25096c5ee0beSSteven Rostedt    open(IN, "git show $patch |") or
25106c5ee0beSSteven Rostedt	dodie "failed to show $patch";
25116c5ee0beSSteven Rostedt    while (<IN>) {
25126c5ee0beSSteven Rostedt	if (m,^--- a/(.*),) {
25136c5ee0beSSteven Rostedt	    chomp $1;
25146c5ee0beSSteven Rostedt	    $files[$#files] = $1;
25156c5ee0beSSteven Rostedt	}
25166c5ee0beSSteven Rostedt    }
25176c5ee0beSSteven Rostedt    close(IN);
25186c5ee0beSSteven Rostedt
25196c5ee0beSSteven Rostedt    open(IN, $buildlog) or dodie "Can't open $buildlog";
25206c5ee0beSSteven Rostedt    while (<IN>) {
25216c5ee0beSSteven Rostedt	if (/^\s*(.*?):.*(warning|error)/) {
25226c5ee0beSSteven Rostedt	    my $err = $1;
25236c5ee0beSSteven Rostedt	    foreach my $file (@files) {
2524a75fececSSteven Rostedt		my $fullpath = "$builddir/$file";
25256c5ee0beSSteven Rostedt		if ($file eq $err || $fullpath eq $err) {
25262b7d9b21SSteven Rostedt		    fail "$file built with warnings" and return 0;
25276c5ee0beSSteven Rostedt		}
25286c5ee0beSSteven Rostedt	    }
25296c5ee0beSSteven Rostedt	}
25306c5ee0beSSteven Rostedt    }
25316c5ee0beSSteven Rostedt    close(IN);
25322b7d9b21SSteven Rostedt
25332b7d9b21SSteven Rostedt    return 1;
25346c5ee0beSSteven Rostedt}
25356c5ee0beSSteven Rostedt
2536fcb3f16aSSteven Rostedtsub apply_min_config {
2537fcb3f16aSSteven Rostedt    my $outconfig = "$output_config.new";
2538612b9e9bSSteven Rostedt
2539fcb3f16aSSteven Rostedt    # Read the config file and remove anything that
2540fcb3f16aSSteven Rostedt    # is in the force_config hash (from minconfig and others)
2541fcb3f16aSSteven Rostedt    # then add the force config back.
2542fcb3f16aSSteven Rostedt
2543fcb3f16aSSteven Rostedt    doprint "Applying minimum configurations into $output_config.new\n";
2544fcb3f16aSSteven Rostedt
2545fcb3f16aSSteven Rostedt    open (OUT, ">$outconfig") or
2546fcb3f16aSSteven Rostedt	dodie "Can't create $outconfig";
2547fcb3f16aSSteven Rostedt
2548fcb3f16aSSteven Rostedt    if (-f $output_config) {
2549fcb3f16aSSteven Rostedt	open (IN, $output_config) or
2550fcb3f16aSSteven Rostedt	    dodie "Failed to open $output_config";
2551fcb3f16aSSteven Rostedt	while (<IN>) {
2552fcb3f16aSSteven Rostedt	    if (/^(# )?(CONFIG_[^\s=]*)/) {
2553fcb3f16aSSteven Rostedt		next if (defined($force_config{$2}));
2554fcb3f16aSSteven Rostedt	    }
2555fcb3f16aSSteven Rostedt	    print OUT;
2556fcb3f16aSSteven Rostedt	}
2557fcb3f16aSSteven Rostedt	close IN;
2558fcb3f16aSSteven Rostedt    }
2559fcb3f16aSSteven Rostedt    foreach my $config (keys %force_config) {
2560fcb3f16aSSteven Rostedt	print OUT "$force_config{$config}\n";
2561fcb3f16aSSteven Rostedt    }
2562fcb3f16aSSteven Rostedt    close OUT;
2563fcb3f16aSSteven Rostedt
2564fcb3f16aSSteven Rostedt    run_command "mv $outconfig $output_config";
2565fcb3f16aSSteven Rostedt}
2566fcb3f16aSSteven Rostedt
2567fcb3f16aSSteven Rostedtsub make_oldconfig {
2568fcb3f16aSSteven Rostedt
25694c4ab120SSteven Rostedt    my @force_list = keys %force_config;
25704c4ab120SSteven Rostedt
25714c4ab120SSteven Rostedt    if ($#force_list >= 0) {
2572fcb3f16aSSteven Rostedt	apply_min_config;
25734c4ab120SSteven Rostedt    }
2574fcb3f16aSSteven Rostedt
2575fb16d891SAdam Lee    if (!run_command "$make olddefconfig") {
2576fb16d891SAdam Lee	# Perhaps olddefconfig doesn't exist in this version of the kernel
257718925170SSteven Rostedt	# try oldnoconfig
257818925170SSteven Rostedt	doprint "olddefconfig failed, trying make oldnoconfig\n";
257918925170SSteven Rostedt	if (!run_command "$make oldnoconfig") {
258018925170SSteven Rostedt	    doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
2581612b9e9bSSteven Rostedt	    # try a yes '' | oldconfig
2582fcb3f16aSSteven Rostedt	    run_command "yes '' | $make oldconfig" or
2583612b9e9bSSteven Rostedt		dodie "failed make config oldconfig";
2584612b9e9bSSteven Rostedt	}
2585612b9e9bSSteven Rostedt    }
258618925170SSteven Rostedt}
2587612b9e9bSSteven Rostedt
2588fcb3f16aSSteven Rostedt# read a config file and use this to force new configs.
2589fcb3f16aSSteven Rostedtsub load_force_config {
2590fcb3f16aSSteven Rostedt    my ($config) = @_;
2591fcb3f16aSSteven Rostedt
2592cf79fab6SSteven Rostedt    doprint "Loading force configs from $config\n";
2593fcb3f16aSSteven Rostedt    open(IN, $config) or
2594fcb3f16aSSteven Rostedt	dodie "failed to read $config";
2595fcb3f16aSSteven Rostedt    while (<IN>) {
2596fcb3f16aSSteven Rostedt	chomp;
2597fcb3f16aSSteven Rostedt	if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
2598fcb3f16aSSteven Rostedt	    $force_config{$1} = $_;
2599fcb3f16aSSteven Rostedt	} elsif (/^# (CONFIG_\S*) is not set/) {
2600fcb3f16aSSteven Rostedt	    $force_config{$1} = $_;
2601fcb3f16aSSteven Rostedt	}
2602fcb3f16aSSteven Rostedt    }
2603fcb3f16aSSteven Rostedt    close IN;
2604fcb3f16aSSteven Rostedt}
2605fcb3f16aSSteven Rostedt
26062545eb61SSteven Rostedtsub build {
26072545eb61SSteven Rostedt    my ($type) = @_;
26082545eb61SSteven Rostedt
26097faafbd6SSteven Rostedt    unlink $buildlog;
26107faafbd6SSteven Rostedt
261138fa3dc1SSteven Rostedt (Red Hat)    my $start_time = time;
261238fa3dc1SSteven Rostedt (Red Hat)
26134ab1cce5SSteven Rostedt    # Failed builds should not reboot the target
26144ab1cce5SSteven Rostedt    my $save_no_reboot = $no_reboot;
26154ab1cce5SSteven Rostedt    $no_reboot = 1;
26164ab1cce5SSteven Rostedt
2617683a3e64SSteven Rostedt    # Calculate a new version from here.
2618683a3e64SSteven Rostedt    $have_version = 0;
2619683a3e64SSteven Rostedt
26200bd6c1a3SSteven Rostedt    if (defined($pre_build)) {
26210bd6c1a3SSteven Rostedt	my $ret = run_command $pre_build;
26220bd6c1a3SSteven Rostedt	if (!$ret && defined($pre_build_die) &&
26230bd6c1a3SSteven Rostedt	    $pre_build_die) {
26240bd6c1a3SSteven Rostedt	    dodie "failed to pre_build\n";
26250bd6c1a3SSteven Rostedt	}
26260bd6c1a3SSteven Rostedt    }
26270bd6c1a3SSteven Rostedt
262875c3fda7SSteven Rostedt    if ($type =~ /^useconfig:(.*)/) {
262951ad1dd1SSteven Rostedt	run_command "cp $1 $output_config" or
263075c3fda7SSteven Rostedt	    dodie "could not copy $1 to .config";
26315f9b6cedSSteven Rostedt
263275c3fda7SSteven Rostedt	$type = "oldconfig";
263375c3fda7SSteven Rostedt    }
263475c3fda7SSteven Rostedt
26355c42fc5bSSteven Rostedt    # old config can ask questions
26365c42fc5bSSteven Rostedt    if ($type eq "oldconfig") {
2637fb16d891SAdam Lee	$type = "olddefconfig";
263875c3fda7SSteven Rostedt
263975c3fda7SSteven Rostedt	# allow for empty configs
264051ad1dd1SSteven Rostedt	run_command "touch $output_config";
264175c3fda7SSteven Rostedt
264213488231SAndrew Jones	if (!$noclean) {
264351ad1dd1SSteven Rostedt	    run_command "mv $output_config $outputdir/config_temp" or
26445c42fc5bSSteven Rostedt		dodie "moving .config";
26455c42fc5bSSteven Rostedt
264613488231SAndrew Jones	    run_command "$make mrproper" or dodie "make mrproper";
26475c42fc5bSSteven Rostedt
264851ad1dd1SSteven Rostedt	    run_command "mv $outputdir/config_temp $output_config" or
26495c42fc5bSSteven Rostedt		dodie "moving config_temp";
265013488231SAndrew Jones	}
26515c42fc5bSSteven Rostedt    } elsif (!$noclean) {
265251ad1dd1SSteven Rostedt	unlink "$output_config";
26535f9b6cedSSteven Rostedt	run_command "$make mrproper" or
26545c42fc5bSSteven Rostedt	    dodie "make mrproper";
26555c42fc5bSSteven Rostedt    }
26562545eb61SSteven Rostedt
26572545eb61SSteven Rostedt    # add something to distinguish this build
2658a75fececSSteven Rostedt    open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
2659a75fececSSteven Rostedt    print OUT "$localversion\n";
26602545eb61SSteven Rostedt    close(OUT);
26612545eb61SSteven Rostedt
26625f9b6cedSSteven Rostedt    if (defined($minconfig)) {
2663fcb3f16aSSteven Rostedt	load_force_config($minconfig);
26642545eb61SSteven Rostedt    }
26652545eb61SSteven Rostedt
2666fb16d891SAdam Lee    if ($type ne "olddefconfig") {
2667fcb3f16aSSteven Rostedt	run_command "$make $type" or
26685c42fc5bSSteven Rostedt	    dodie "failed make config";
2669612b9e9bSSteven Rostedt    }
2670fcb3f16aSSteven Rostedt    # Run old config regardless, to enforce min configurations
2671fcb3f16aSSteven Rostedt    make_oldconfig;
26722545eb61SSteven Rostedt
2673da2e5663SJohn 'Warthog9' Hawley (VMware)    if (not defined($build_options)){
2674da2e5663SJohn 'Warthog9' Hawley (VMware)	$build_options = "";
2675da2e5663SJohn 'Warthog9' Hawley (VMware)    }
2676f983a2bcSSatoru Takeuchi    my $build_ret = run_command "$make $build_options", $buildlog;
26770bd6c1a3SSteven Rostedt
26780bd6c1a3SSteven Rostedt    if (defined($post_build)) {
2679683a3e64SSteven Rostedt	# Because a post build may change the kernel version
2680683a3e64SSteven Rostedt	# do it now.
2681683a3e64SSteven Rostedt	get_version;
26820bd6c1a3SSteven Rostedt	my $ret = run_command $post_build;
26830bd6c1a3SSteven Rostedt	if (!$ret && defined($post_build_die) &&
26840bd6c1a3SSteven Rostedt	    $post_build_die) {
26850bd6c1a3SSteven Rostedt	    dodie "failed to post_build\n";
26860bd6c1a3SSteven Rostedt	}
26870bd6c1a3SSteven Rostedt    }
26880bd6c1a3SSteven Rostedt
26890bd6c1a3SSteven Rostedt    if (!$build_ret) {
26905f9b6cedSSteven Rostedt	# bisect may need this to pass
26914ab1cce5SSteven Rostedt	if ($in_bisect) {
26924ab1cce5SSteven Rostedt	    $no_reboot = $save_no_reboot;
26934ab1cce5SSteven Rostedt	    return 0;
26944ab1cce5SSteven Rostedt	}
26952b7d9b21SSteven Rostedt	fail "failed build" and return 0;
26962545eb61SSteven Rostedt    }
26975f9b6cedSSteven Rostedt
26984ab1cce5SSteven Rostedt    $no_reboot = $save_no_reboot;
26994ab1cce5SSteven Rostedt
270038fa3dc1SSteven Rostedt (Red Hat)    my $end_time = time;
270138fa3dc1SSteven Rostedt (Red Hat)    $build_time = $end_time - $start_time;
270238fa3dc1SSteven Rostedt (Red Hat)
27032b7d9b21SSteven Rostedt    return 1;
27042545eb61SSteven Rostedt}
27052545eb61SSteven Rostedt
270675c3fda7SSteven Rostedtsub halt {
2707e48c5293SSteven Rostedt    if (!run_ssh "halt" or defined($power_off)) {
2708576f627cSSteven Rostedt	if (defined($poweroff_after_halt)) {
2709576f627cSSteven Rostedt	    sleep $poweroff_after_halt;
2710576f627cSSteven Rostedt	    run_command "$power_off";
2711576f627cSSteven Rostedt	}
2712576f627cSSteven Rostedt    } else {
271375c3fda7SSteven Rostedt	# nope? the zap it!
2714a75fececSSteven Rostedt	run_command "$power_off";
271575c3fda7SSteven Rostedt    }
271675c3fda7SSteven Rostedt}
271775c3fda7SSteven Rostedt
27185f9b6cedSSteven Rostedtsub success {
27195f9b6cedSSteven Rostedt    my ($i) = @_;
27205f9b6cedSSteven Rostedt
2721e48c5293SSteven Rostedt    $successes++;
2722e48c5293SSteven Rostedt
27239064af52SSteven Rostedt    my $name = "";
27249064af52SSteven Rostedt
27259064af52SSteven Rostedt    if (defined($test_name)) {
27269064af52SSteven Rostedt	$name = " ($test_name)";
27279064af52SSteven Rostedt    }
27289064af52SSteven Rostedt
27294bf6e1fcSSteven Rostedt (Red Hat)    print_times;
27304bf6e1fcSSteven Rostedt (Red Hat)
2731c043ccbfSJohn 'Warthog9' Hawley (VMware)    doprint "\n\n";
2732c043ccbfSJohn 'Warthog9' Hawley (VMware)    doprint "*******************************************\n";
27335f9b6cedSSteven Rostedt    doprint "*******************************************\n";
27349064af52SSteven Rostedt    doprint "KTEST RESULT: TEST $i$name SUCCESS!!!!   **\n";
27355f9b6cedSSteven Rostedt    doprint "*******************************************\n";
27365f9b6cedSSteven Rostedt    doprint "*******************************************\n";
27375f9b6cedSSteven Rostedt
2738de5b6e3bSRabin Vincent    if (defined($store_successes)) {
2739de5b6e3bSRabin Vincent	save_logs "success", $store_successes;
2740de5b6e3bSRabin Vincent    }
2741de5b6e3bSRabin Vincent
2742576f627cSSteven Rostedt    if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
2743a75fececSSteven Rostedt	doprint "Reboot and wait $sleep_time seconds\n";
2744bc7c5803SSteven Rostedt	reboot_to_good $sleep_time;
27455f9b6cedSSteven Rostedt    }
27462e07c9f5SSteven Rostedt (VMware)
27472e07c9f5SSteven Rostedt (VMware)    if (defined($post_test)) {
27482e07c9f5SSteven Rostedt (VMware)	run_command $post_test;
27492e07c9f5SSteven Rostedt (VMware)    }
27505f9b6cedSSteven Rostedt}
27515f9b6cedSSteven Rostedt
2752c960bb9fSSteven Rostedtsub answer_bisect {
2753c960bb9fSSteven Rostedt    for (;;) {
2754fee9d3e6SChris J Arges	doprint "Pass, fail, or skip? [p/f/s]";
2755c960bb9fSSteven Rostedt	my $ans = <STDIN>;
2756c960bb9fSSteven Rostedt	chomp $ans;
2757c960bb9fSSteven Rostedt	if ($ans eq "p" || $ans eq "P") {
2758c960bb9fSSteven Rostedt	    return 1;
2759c960bb9fSSteven Rostedt	} elsif ($ans eq "f" || $ans eq "F") {
2760c960bb9fSSteven Rostedt	    return 0;
2761fee9d3e6SChris J Arges	} elsif ($ans eq "s" || $ans eq "S") {
2762fee9d3e6SChris J Arges	    return -1;
2763c960bb9fSSteven Rostedt	} else {
2764fee9d3e6SChris J Arges	    print "Please answer 'p', 'f', or 's'\n";
2765c960bb9fSSteven Rostedt	}
2766c960bb9fSSteven Rostedt    }
2767c960bb9fSSteven Rostedt}
2768c960bb9fSSteven Rostedt
27695a391fbfSSteven Rostedtsub child_run_test {
27705a391fbfSSteven Rostedt
27717faafbd6SSteven Rostedt    # child should have no power
2772a75fececSSteven Rostedt    $reboot_on_error = 0;
2773a75fececSSteven Rostedt    $poweroff_on_error = 0;
2774a75fececSSteven Rostedt    $die_on_failure = 1;
27757faafbd6SSteven Rostedt
27765739438bSSteven Rostedt (VMware)    run_command $run_test, $testlog;
2777a9dd5d63SRabin Vincent
27785739438bSSteven Rostedt (VMware)    exit $run_command_status;
27795a391fbfSSteven Rostedt}
27805a391fbfSSteven Rostedt
27815a391fbfSSteven Rostedtsub child_finished {
27825a391fbfSSteven Rostedt    $child_done = 1;
27835a391fbfSSteven Rostedt}
27845a391fbfSSteven Rostedt
27855a391fbfSSteven Rostedtsub do_run_test {
27865a391fbfSSteven Rostedt    my $child_pid;
27875a391fbfSSteven Rostedt    my $child_exit;
27885a391fbfSSteven Rostedt    my $line;
27895a391fbfSSteven Rostedt    my $full_line;
27905a391fbfSSteven Rostedt    my $bug = 0;
27919b1d367dSSteven Rostedt    my $bug_ignored = 0;
27925a391fbfSSteven Rostedt
279338fa3dc1SSteven Rostedt (Red Hat)    my $start_time = time;
279438fa3dc1SSteven Rostedt (Red Hat)
27957faafbd6SSteven Rostedt    wait_for_monitor 1;
27965a391fbfSSteven Rostedt
27977faafbd6SSteven Rostedt    doprint "run test $run_test\n";
27985a391fbfSSteven Rostedt
27995a391fbfSSteven Rostedt    $child_done = 0;
28005a391fbfSSteven Rostedt
28015a391fbfSSteven Rostedt    $SIG{CHLD} = qw(child_finished);
28025a391fbfSSteven Rostedt
28035a391fbfSSteven Rostedt    $child_pid = fork;
28045a391fbfSSteven Rostedt
28055a391fbfSSteven Rostedt    child_run_test if (!$child_pid);
28065a391fbfSSteven Rostedt
28075a391fbfSSteven Rostedt    $full_line = "";
28085a391fbfSSteven Rostedt
28095a391fbfSSteven Rostedt    do {
28107faafbd6SSteven Rostedt	$line = wait_for_input($monitor_fp, 1);
28115a391fbfSSteven Rostedt	if (defined($line)) {
28125a391fbfSSteven Rostedt
28135a391fbfSSteven Rostedt	    # we are not guaranteed to get a full line
28145a391fbfSSteven Rostedt	    $full_line .= $line;
28158ea0e063SSteven Rostedt	    doprint $line;
28165a391fbfSSteven Rostedt
28175a391fbfSSteven Rostedt	    if ($full_line =~ /call trace:/i) {
28189b1d367dSSteven Rostedt		if ($ignore_errors) {
28199b1d367dSSteven Rostedt		    $bug_ignored = 1;
28209b1d367dSSteven Rostedt		} else {
28215a391fbfSSteven Rostedt		    $bug = 1;
28225a391fbfSSteven Rostedt		}
28239b1d367dSSteven Rostedt	    }
28245a391fbfSSteven Rostedt
28255a391fbfSSteven Rostedt	    if ($full_line =~ /Kernel panic -/) {
28265a391fbfSSteven Rostedt		$bug = 1;
28275a391fbfSSteven Rostedt	    }
28285a391fbfSSteven Rostedt
28295a391fbfSSteven Rostedt	    if ($line =~ /\n/) {
28305a391fbfSSteven Rostedt		$full_line = "";
28315a391fbfSSteven Rostedt	    }
28325a391fbfSSteven Rostedt	}
28335a391fbfSSteven Rostedt    } while (!$child_done && !$bug);
28345a391fbfSSteven Rostedt
28359b1d367dSSteven Rostedt    if (!$bug && $bug_ignored) {
28369b1d367dSSteven Rostedt	doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
28379b1d367dSSteven Rostedt    }
28389b1d367dSSteven Rostedt
28395a391fbfSSteven Rostedt    if ($bug) {
28408ea0e063SSteven Rostedt	my $failure_start = time;
28418ea0e063SSteven Rostedt	my $now;
28428ea0e063SSteven Rostedt	do {
28438ea0e063SSteven Rostedt	    $line = wait_for_input($monitor_fp, 1);
28448ea0e063SSteven Rostedt	    if (defined($line)) {
28458ea0e063SSteven Rostedt		doprint $line;
28468ea0e063SSteven Rostedt	    }
28478ea0e063SSteven Rostedt	    $now = time;
28488ea0e063SSteven Rostedt	    if ($now - $failure_start >= $stop_after_failure) {
28498ea0e063SSteven Rostedt		last;
28508ea0e063SSteven Rostedt	    }
28518ea0e063SSteven Rostedt	} while (defined($line));
28528ea0e063SSteven Rostedt
28535a391fbfSSteven Rostedt	doprint "Detected kernel crash!\n";
28545a391fbfSSteven Rostedt	# kill the child with extreme prejudice
28555a391fbfSSteven Rostedt	kill 9, $child_pid;
28565a391fbfSSteven Rostedt    }
28575a391fbfSSteven Rostedt
28585a391fbfSSteven Rostedt    waitpid $child_pid, 0;
285932677207SSteven Rostedt (VMware)    $child_exit = $? >> 8;
28605a391fbfSSteven Rostedt
286138fa3dc1SSteven Rostedt (Red Hat)    my $end_time = time;
286238fa3dc1SSteven Rostedt (Red Hat)    $test_time = $end_time - $start_time;
286338fa3dc1SSteven Rostedt (Red Hat)
2864c5dacb88SSteven Rostedt    if (!$bug && $in_bisect) {
2865c5dacb88SSteven Rostedt	if (defined($bisect_ret_good)) {
2866c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_good) {
2867c5dacb88SSteven Rostedt		return 1;
2868c5dacb88SSteven Rostedt	    }
2869c5dacb88SSteven Rostedt	}
2870c5dacb88SSteven Rostedt	if (defined($bisect_ret_skip)) {
2871c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_skip) {
2872c5dacb88SSteven Rostedt		return -1;
2873c5dacb88SSteven Rostedt	    }
2874c5dacb88SSteven Rostedt	}
2875c5dacb88SSteven Rostedt	if (defined($bisect_ret_abort)) {
2876c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_abort) {
2877c5dacb88SSteven Rostedt		fail "test abort" and return -2;
2878c5dacb88SSteven Rostedt	    }
2879c5dacb88SSteven Rostedt	}
2880c5dacb88SSteven Rostedt	if (defined($bisect_ret_bad)) {
2881c5dacb88SSteven Rostedt	    if ($child_exit == $bisect_ret_skip) {
2882c5dacb88SSteven Rostedt		return 0;
2883c5dacb88SSteven Rostedt	    }
2884c5dacb88SSteven Rostedt	}
2885c5dacb88SSteven Rostedt	if (defined($bisect_ret_default)) {
2886c5dacb88SSteven Rostedt	    if ($bisect_ret_default eq "good") {
2887c5dacb88SSteven Rostedt		return 1;
2888c5dacb88SSteven Rostedt	    } elsif ($bisect_ret_default eq "bad") {
2889c5dacb88SSteven Rostedt		return 0;
2890c5dacb88SSteven Rostedt	    } elsif ($bisect_ret_default eq "skip") {
2891c5dacb88SSteven Rostedt		return -1;
2892c5dacb88SSteven Rostedt	    } elsif ($bisect_ret_default eq "abort") {
2893c5dacb88SSteven Rostedt		return -2;
2894c5dacb88SSteven Rostedt	    } else {
2895c5dacb88SSteven Rostedt		fail "unknown default action: $bisect_ret_default"
2896c5dacb88SSteven Rostedt		    and return -2;
2897c5dacb88SSteven Rostedt	    }
2898c5dacb88SSteven Rostedt	}
2899c5dacb88SSteven Rostedt    }
2900c5dacb88SSteven Rostedt
29015a391fbfSSteven Rostedt    if ($bug || $child_exit) {
29022b7d9b21SSteven Rostedt	return 0 if $in_bisect;
29032b7d9b21SSteven Rostedt	fail "test failed" and return 0;
29045a391fbfSSteven Rostedt    }
29052b7d9b21SSteven Rostedt    return 1;
29065a391fbfSSteven Rostedt}
29075a391fbfSSteven Rostedt
2908a75fececSSteven Rostedtsub run_git_bisect {
2909a75fececSSteven Rostedt    my ($command) = @_;
2910a75fececSSteven Rostedt
2911a75fececSSteven Rostedt    doprint "$command ... ";
2912a75fececSSteven Rostedt
2913a75fececSSteven Rostedt    my $output = `$command 2>&1`;
2914a75fececSSteven Rostedt    my $ret = $?;
2915a75fececSSteven Rostedt
2916a75fececSSteven Rostedt    logit $output;
2917a75fececSSteven Rostedt
2918a75fececSSteven Rostedt    if ($ret) {
2919a75fececSSteven Rostedt	doprint "FAILED\n";
2920a75fececSSteven Rostedt	dodie "Failed to git bisect";
2921a75fececSSteven Rostedt    }
2922a75fececSSteven Rostedt
2923a75fececSSteven Rostedt    doprint "SUCCESS\n";
2924a75fececSSteven Rostedt    if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2925a75fececSSteven Rostedt	doprint "$1 [$2]\n";
2926a75fececSSteven Rostedt    } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
2927b5f4aea6SSteven Rostedt	$bisect_bad_commit = $1;
2928a75fececSSteven Rostedt	doprint "Found bad commit... $1\n";
2929a75fececSSteven Rostedt	return 0;
2930a75fececSSteven Rostedt    } else {
2931a75fececSSteven Rostedt	# we already logged it, just print it now.
2932a75fececSSteven Rostedt	print $output;
2933a75fececSSteven Rostedt    }
2934a75fececSSteven Rostedt
2935a75fececSSteven Rostedt    return 1;
2936a75fececSSteven Rostedt}
2937a75fececSSteven Rostedt
2938c23dca7cSSteven Rostedtsub bisect_reboot {
2939c23dca7cSSteven Rostedt    doprint "Reboot and sleep $bisect_sleep_time seconds\n";
2940bc7c5803SSteven Rostedt    reboot_to_good $bisect_sleep_time;
2941c23dca7cSSteven Rostedt}
2942c23dca7cSSteven Rostedt
2943c23dca7cSSteven Rostedt# returns 1 on success, 0 on failure, -1 on skip
29440a05c769SSteven Rostedtsub run_bisect_test {
29450a05c769SSteven Rostedt    my ($type, $buildtype) = @_;
29465f9b6cedSSteven Rostedt
29472b7d9b21SSteven Rostedt    my $failed = 0;
29485f9b6cedSSteven Rostedt    my $result;
29495f9b6cedSSteven Rostedt    my $output;
29505f9b6cedSSteven Rostedt    my $ret;
29515f9b6cedSSteven Rostedt
29520a05c769SSteven Rostedt    $in_bisect = 1;
29530a05c769SSteven Rostedt
29540a05c769SSteven Rostedt    build $buildtype or $failed = 1;
29555f9b6cedSSteven Rostedt
29565f9b6cedSSteven Rostedt    if ($type ne "build") {
2957c23dca7cSSteven Rostedt	if ($failed && $bisect_skip) {
2958c23dca7cSSteven Rostedt	    $in_bisect = 0;
2959c23dca7cSSteven Rostedt	    return -1;
2960c23dca7cSSteven Rostedt	}
29617faafbd6SSteven Rostedt	dodie "Failed on build" if $failed;
29625f9b6cedSSteven Rostedt
29635f9b6cedSSteven Rostedt	# Now boot the box
296464d98283SSteven Rostedt (Red Hat)	start_monitor_and_install or $failed = 1;
29655f9b6cedSSteven Rostedt
29665f9b6cedSSteven Rostedt	if ($type ne "boot") {
2967c23dca7cSSteven Rostedt	    if ($failed && $bisect_skip) {
2968c23dca7cSSteven Rostedt		end_monitor;
2969c23dca7cSSteven Rostedt		bisect_reboot;
2970c23dca7cSSteven Rostedt		$in_bisect = 0;
2971c23dca7cSSteven Rostedt		return -1;
2972c23dca7cSSteven Rostedt	    }
29737faafbd6SSteven Rostedt	    dodie "Failed on boot" if $failed;
29745a391fbfSSteven Rostedt
29752b7d9b21SSteven Rostedt	    do_run_test or $failed = 1;
29765f9b6cedSSteven Rostedt	}
29777faafbd6SSteven Rostedt	end_monitor;
29785f9b6cedSSteven Rostedt    }
29795f9b6cedSSteven Rostedt
29805f9b6cedSSteven Rostedt    if ($failed) {
29810a05c769SSteven Rostedt	$result = 0;
29825f9b6cedSSteven Rostedt    } else {
29830a05c769SSteven Rostedt	$result = 1;
29845f9b6cedSSteven Rostedt    }
29854025bc62SSteven Rostedt
29864025bc62SSteven Rostedt    # reboot the box to a kernel we can ssh to
29874025bc62SSteven Rostedt    if ($type ne "build") {
29884025bc62SSteven Rostedt	bisect_reboot;
29894025bc62SSteven Rostedt    }
29900a05c769SSteven Rostedt    $in_bisect = 0;
29910a05c769SSteven Rostedt
29920a05c769SSteven Rostedt    return $result;
29930a05c769SSteven Rostedt}
29940a05c769SSteven Rostedt
29950a05c769SSteven Rostedtsub run_bisect {
29960a05c769SSteven Rostedt    my ($type) = @_;
29970a05c769SSteven Rostedt    my $buildtype = "oldconfig";
29980a05c769SSteven Rostedt
29990a05c769SSteven Rostedt    # We should have a minconfig to use?
30000a05c769SSteven Rostedt    if (defined($minconfig)) {
30010a05c769SSteven Rostedt	$buildtype = "useconfig:$minconfig";
30020a05c769SSteven Rostedt    }
30030a05c769SSteven Rostedt
3004961d9cacSSteven Rostedt (Red Hat)    # If the user sets bisect_tries to less than 1, then no tries
3005961d9cacSSteven Rostedt (Red Hat)    # is a success.
3006961d9cacSSteven Rostedt (Red Hat)    my $ret = 1;
3007961d9cacSSteven Rostedt (Red Hat)
3008961d9cacSSteven Rostedt (Red Hat)    # Still let the user manually decide that though.
3009961d9cacSSteven Rostedt (Red Hat)    if ($bisect_tries < 1 && $bisect_manual) {
3010961d9cacSSteven Rostedt (Red Hat)	$ret = answer_bisect;
3011961d9cacSSteven Rostedt (Red Hat)    }
3012961d9cacSSteven Rostedt (Red Hat)
3013961d9cacSSteven Rostedt (Red Hat)    for (my $i = 0; $i < $bisect_tries; $i++) {
3014961d9cacSSteven Rostedt (Red Hat)	if ($bisect_tries > 1) {
3015961d9cacSSteven Rostedt (Red Hat)	    my $t = $i + 1;
3016961d9cacSSteven Rostedt (Red Hat)	    doprint("Running bisect trial $t of $bisect_tries:\n");
3017961d9cacSSteven Rostedt (Red Hat)	}
3018961d9cacSSteven Rostedt (Red Hat)	$ret = run_bisect_test $type, $buildtype;
30190a05c769SSteven Rostedt
3020c960bb9fSSteven Rostedt	if ($bisect_manual) {
3021c960bb9fSSteven Rostedt	    $ret = answer_bisect;
3022c960bb9fSSteven Rostedt	}
30235f9b6cedSSteven Rostedt
3024961d9cacSSteven Rostedt (Red Hat)	last if (!$ret);
3025961d9cacSSteven Rostedt (Red Hat)    }
3026961d9cacSSteven Rostedt (Red Hat)
3027d6ce2a0bSSteven Rostedt    # Are we looking for where it worked, not failed?
30285158ba3eSRuss Dill    if ($reverse_bisect && $ret >= 0) {
30290a05c769SSteven Rostedt	$ret = !$ret;
3030d6ce2a0bSSteven Rostedt    }
3031d6ce2a0bSSteven Rostedt
3032c23dca7cSSteven Rostedt    if ($ret > 0) {
30330a05c769SSteven Rostedt	return "good";
3034c23dca7cSSteven Rostedt    } elsif ($ret == 0) {
30350a05c769SSteven Rostedt	return  "bad";
3036c23dca7cSSteven Rostedt    } elsif ($bisect_skip) {
3037c23dca7cSSteven Rostedt	doprint "HIT A BAD COMMIT ... SKIPPING\n";
3038c23dca7cSSteven Rostedt	return "skip";
30390a05c769SSteven Rostedt    }
30405f9b6cedSSteven Rostedt}
30415f9b6cedSSteven Rostedt
3042dad98754SSteven Rostedtsub update_bisect_replay {
3043dad98754SSteven Rostedt    my $tmp_log = "$tmpdir/ktest_bisect_log";
3044dad98754SSteven Rostedt    run_command "git bisect log > $tmp_log" or
3045df46fce6STim Tianyang Chen	dodie "can't create bisect log";
3046dad98754SSteven Rostedt    return $tmp_log;
3047dad98754SSteven Rostedt}
3048dad98754SSteven Rostedt
30495f9b6cedSSteven Rostedtsub bisect {
30505f9b6cedSSteven Rostedt    my ($i) = @_;
30515f9b6cedSSteven Rostedt
30525f9b6cedSSteven Rostedt    my $result;
30535f9b6cedSSteven Rostedt
3054df46fce6STim Tianyang Chen    dodie "BISECT_GOOD[$i] not defined\n"	if (!defined($bisect_good));
3055df46fce6STim Tianyang Chen    dodie "BISECT_BAD[$i] not defined\n"	if (!defined($bisect_bad));
3056df46fce6STim Tianyang Chen    dodie "BISECT_TYPE[$i] not defined\n"	if (!defined($bisect_type));
30575f9b6cedSSteven Rostedt
3058b5f4aea6SSteven Rostedt    my $good = $bisect_good;
3059b5f4aea6SSteven Rostedt    my $bad = $bisect_bad;
3060b5f4aea6SSteven Rostedt    my $type = $bisect_type;
3061b5f4aea6SSteven Rostedt    my $start = $bisect_start;
3062b5f4aea6SSteven Rostedt    my $replay = $bisect_replay;
3063b5f4aea6SSteven Rostedt    my $start_files = $bisect_files;
30643410f6fdSSteven Rostedt
30653410f6fdSSteven Rostedt    if (defined($start_files)) {
30663410f6fdSSteven Rostedt	$start_files = " -- " . $start_files;
30673410f6fdSSteven Rostedt    } else {
30683410f6fdSSteven Rostedt	$start_files = "";
30693410f6fdSSteven Rostedt    }
30705f9b6cedSSteven Rostedt
3071a57419b3SSteven Rostedt    # convert to true sha1's
3072a57419b3SSteven Rostedt    $good = get_sha1($good);
3073a57419b3SSteven Rostedt    $bad = get_sha1($bad);
3074a57419b3SSteven Rostedt
3075b5f4aea6SSteven Rostedt    if (defined($bisect_reverse) && $bisect_reverse == 1) {
3076d6ce2a0bSSteven Rostedt	doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
3077d6ce2a0bSSteven Rostedt	$reverse_bisect = 1;
3078d6ce2a0bSSteven Rostedt    } else {
3079d6ce2a0bSSteven Rostedt	$reverse_bisect = 0;
3080d6ce2a0bSSteven Rostedt    }
3081d6ce2a0bSSteven Rostedt
30825a391fbfSSteven Rostedt    # Can't have a test without having a test to run
30835a391fbfSSteven Rostedt    if ($type eq "test" && !defined($run_test)) {
30845a391fbfSSteven Rostedt	$type = "boot";
30855a391fbfSSteven Rostedt    }
30865a391fbfSSteven Rostedt
3087dad98754SSteven Rostedt    # Check if a bisect was running
3088dad98754SSteven Rostedt    my $bisect_start_file = "$builddir/.git/BISECT_START";
3089dad98754SSteven Rostedt
3090b5f4aea6SSteven Rostedt    my $check = $bisect_check;
3091dad98754SSteven Rostedt    my $do_check = defined($check) && $check ne "0";
3092dad98754SSteven Rostedt
3093dad98754SSteven Rostedt    if ( -f $bisect_start_file ) {
3094dad98754SSteven Rostedt	print "Bisect in progress found\n";
3095dad98754SSteven Rostedt	if ($do_check) {
3096dad98754SSteven Rostedt	    print " If you say yes, then no checks of good or bad will be done\n";
3097dad98754SSteven Rostedt	}
3098dad98754SSteven Rostedt	if (defined($replay)) {
3099dad98754SSteven Rostedt	    print "** BISECT_REPLAY is defined in config file **";
3100dad98754SSteven Rostedt	    print " Ignore config option and perform new git bisect log?\n";
3101dad98754SSteven Rostedt	    if (read_ync " (yes, no, or cancel) ") {
3102dad98754SSteven Rostedt		$replay = update_bisect_replay;
3103dad98754SSteven Rostedt		$do_check = 0;
3104dad98754SSteven Rostedt	    }
3105dad98754SSteven Rostedt	} elsif (read_yn "read git log and continue?") {
3106dad98754SSteven Rostedt	    $replay = update_bisect_replay;
3107dad98754SSteven Rostedt	    $do_check = 0;
3108dad98754SSteven Rostedt	}
3109dad98754SSteven Rostedt    }
3110dad98754SSteven Rostedt
3111dad98754SSteven Rostedt    if ($do_check) {
3112a75fececSSteven Rostedt	# get current HEAD
3113a57419b3SSteven Rostedt	my $head = get_sha1("HEAD");
3114a75fececSSteven Rostedt
3115a75fececSSteven Rostedt	if ($check ne "good") {
3116a75fececSSteven Rostedt	    doprint "TESTING BISECT BAD [$bad]\n";
3117a75fececSSteven Rostedt	    run_command "git checkout $bad" or
3118df46fce6STim Tianyang Chen		dodie "Failed to checkout $bad";
3119a75fececSSteven Rostedt
3120a75fececSSteven Rostedt	    $result = run_bisect $type;
3121a75fececSSteven Rostedt
3122a75fececSSteven Rostedt	    if ($result ne "bad") {
3123a75fececSSteven Rostedt		fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
3124a75fececSSteven Rostedt	    }
3125a75fececSSteven Rostedt	}
3126a75fececSSteven Rostedt
3127a75fececSSteven Rostedt	if ($check ne "bad") {
3128a75fececSSteven Rostedt	    doprint "TESTING BISECT GOOD [$good]\n";
3129a75fececSSteven Rostedt	    run_command "git checkout $good" or
3130df46fce6STim Tianyang Chen		dodie "Failed to checkout $good";
3131a75fececSSteven Rostedt
3132a75fececSSteven Rostedt	    $result = run_bisect $type;
3133a75fececSSteven Rostedt
3134a75fececSSteven Rostedt	    if ($result ne "good") {
3135a75fececSSteven Rostedt		fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
3136a75fececSSteven Rostedt	    }
3137a75fececSSteven Rostedt	}
3138a75fececSSteven Rostedt
3139a75fececSSteven Rostedt	# checkout where we started
3140a75fececSSteven Rostedt	run_command "git checkout $head" or
3141df46fce6STim Tianyang Chen	    dodie "Failed to checkout $head";
3142a75fececSSteven Rostedt    }
3143a75fececSSteven Rostedt
31443410f6fdSSteven Rostedt    run_command "git bisect start$start_files" or
3145a75fececSSteven Rostedt	dodie "could not start bisect";
3146a75fececSSteven Rostedt
3147d832d743SSteven Rostedt (Red Hat)    if (defined($replay)) {
3148d832d743SSteven Rostedt (Red Hat)	run_command "git bisect replay $replay" or
3149d832d743SSteven Rostedt (Red Hat)	    dodie "failed to run replay";
3150d832d743SSteven Rostedt (Red Hat)    } else {
3151a75fececSSteven Rostedt	run_command "git bisect good $good" or
3152a75fececSSteven Rostedt	    dodie "could not set bisect good to $good";
3153a75fececSSteven Rostedt
3154a75fececSSteven Rostedt	run_git_bisect "git bisect bad $bad" or
3155a75fececSSteven Rostedt	    dodie "could not set bisect bad to $bad";
3156a75fececSSteven Rostedt    }
3157a75fececSSteven Rostedt
3158a75fececSSteven Rostedt    if (defined($start)) {
3159a75fececSSteven Rostedt	run_command "git checkout $start" or
3160a75fececSSteven Rostedt	    dodie "failed to checkout $start";
3161a75fececSSteven Rostedt    }
3162a75fececSSteven Rostedt
3163a75fececSSteven Rostedt    my $test;
31645f9b6cedSSteven Rostedt    do {
31655f9b6cedSSteven Rostedt	$result = run_bisect $type;
3166a75fececSSteven Rostedt	$test = run_git_bisect "git bisect $result";
316738fa3dc1SSteven Rostedt (Red Hat)	print_times;
3168a75fececSSteven Rostedt    } while ($test);
31695f9b6cedSSteven Rostedt
31705f9b6cedSSteven Rostedt    run_command "git bisect log" or
31715f9b6cedSSteven Rostedt	dodie "could not capture git bisect log";
31725f9b6cedSSteven Rostedt
31735f9b6cedSSteven Rostedt    run_command "git bisect reset" or
31745f9b6cedSSteven Rostedt	dodie "could not reset git bisect";
31755f9b6cedSSteven Rostedt
3176b5f4aea6SSteven Rostedt    doprint "Bad commit was [$bisect_bad_commit]\n";
31775f9b6cedSSteven Rostedt
31780a05c769SSteven Rostedt    success $i;
31790a05c769SSteven Rostedt}
31800a05c769SSteven Rostedt
31814c4ab120SSteven Rostedtsub assign_configs {
31824c4ab120SSteven Rostedt    my ($hash, $config) = @_;
31830a05c769SSteven Rostedt
31846071c22eSSteven Rostedt (Red Hat)    doprint "Reading configs from $config\n";
31856071c22eSSteven Rostedt (Red Hat)
3186c043ccbfSJohn 'Warthog9' Hawley (VMware)    open (IN, $config) or
3187c043ccbfSJohn 'Warthog9' Hawley (VMware)	dodie "Failed to read $config";
31880a05c769SSteven Rostedt
31890a05c769SSteven Rostedt    while (<IN>) {
31906071c22eSSteven Rostedt (Red Hat)	chomp;
31919bf71749SSteven Rostedt	if (/^((CONFIG\S*)=.*)/) {
31924c4ab120SSteven Rostedt	    ${$hash}{$2} = $1;
31936071c22eSSteven Rostedt (Red Hat)	} elsif (/^(# (CONFIG\S*) is not set)/) {
31946071c22eSSteven Rostedt (Red Hat)	    ${$hash}{$2} = $1;
31950a05c769SSteven Rostedt	}
31960a05c769SSteven Rostedt    }
31970a05c769SSteven Rostedt
31980a05c769SSteven Rostedt    close(IN);
31990a05c769SSteven Rostedt}
32000a05c769SSteven Rostedt
32014c4ab120SSteven Rostedtsub process_config_ignore {
32024c4ab120SSteven Rostedt    my ($config) = @_;
32034c4ab120SSteven Rostedt
32044c4ab120SSteven Rostedt    assign_configs \%config_ignore, $config;
32054c4ab120SSteven Rostedt}
32064c4ab120SSteven Rostedt
32070a05c769SSteven Rostedtsub get_dependencies {
32080a05c769SSteven Rostedt    my ($config) = @_;
32090a05c769SSteven Rostedt
32100a05c769SSteven Rostedt    my $arr = $dependency{$config};
32110a05c769SSteven Rostedt    if (!defined($arr)) {
32120a05c769SSteven Rostedt	return ();
32130a05c769SSteven Rostedt    }
32140a05c769SSteven Rostedt
32150a05c769SSteven Rostedt    my @deps = @{$arr};
32160a05c769SSteven Rostedt
32170a05c769SSteven Rostedt    foreach my $dep (@{$arr}) {
32180a05c769SSteven Rostedt	print "ADD DEP $dep\n";
32190a05c769SSteven Rostedt	@deps = (@deps, get_dependencies $dep);
32200a05c769SSteven Rostedt    }
32210a05c769SSteven Rostedt
32220a05c769SSteven Rostedt    return @deps;
32230a05c769SSteven Rostedt}
32240a05c769SSteven Rostedt
32256071c22eSSteven Rostedt (Red Hat)sub save_config {
32266071c22eSSteven Rostedt (Red Hat)    my ($pc, $file) = @_;
32270a05c769SSteven Rostedt
32286071c22eSSteven Rostedt (Red Hat)    my %configs = %{$pc};
32290a05c769SSteven Rostedt
32306071c22eSSteven Rostedt (Red Hat)    doprint "Saving configs into $file\n";
32310a05c769SSteven Rostedt
32326071c22eSSteven Rostedt (Red Hat)    open(OUT, ">$file") or dodie "Can not write to $file";
3233cf79fab6SSteven Rostedt
32346071c22eSSteven Rostedt (Red Hat)    foreach my $config (keys %configs) {
32356071c22eSSteven Rostedt (Red Hat)	print OUT "$configs{$config}\n";
32360a05c769SSteven Rostedt    }
32370a05c769SSteven Rostedt    close(OUT);
32386071c22eSSteven Rostedt (Red Hat)}
32396071c22eSSteven Rostedt (Red Hat)
32406071c22eSSteven Rostedt (Red Hat)sub create_config {
32416071c22eSSteven Rostedt (Red Hat)    my ($name, $pc) = @_;
32426071c22eSSteven Rostedt (Red Hat)
32436071c22eSSteven Rostedt (Red Hat)    doprint "Creating old config from $name configs\n";
32446071c22eSSteven Rostedt (Red Hat)
32456071c22eSSteven Rostedt (Red Hat)    save_config $pc, $output_config;
32460a05c769SSteven Rostedt
3247fcb3f16aSSteven Rostedt    make_oldconfig;
32480a05c769SSteven Rostedt}
32490a05c769SSteven Rostedt
32500a05c769SSteven Rostedtsub run_config_bisect_test {
32510a05c769SSteven Rostedt    my ($type) = @_;
32520a05c769SSteven Rostedt
32534cc559bdSSteven Rostedt (Red Hat)    my $ret = run_bisect_test $type, "oldconfig";
32544cc559bdSSteven Rostedt (Red Hat)
32554cc559bdSSteven Rostedt (Red Hat)    if ($bisect_manual) {
32564cc559bdSSteven Rostedt (Red Hat)	$ret = answer_bisect;
32574cc559bdSSteven Rostedt (Red Hat)    }
32584cc559bdSSteven Rostedt (Red Hat)
32594cc559bdSSteven Rostedt (Red Hat)    return $ret;
32600a05c769SSteven Rostedt}
32610a05c769SSteven Rostedt
3262a9adc261SScott Woodsub config_bisect_end {
3263a9adc261SScott Wood    my ($good, $bad) = @_;
3264b2b07ea2SSteven Rostedt (VMware)    my $diffexec = "diff -u";
32650a05c769SSteven Rostedt
3266b2b07ea2SSteven Rostedt (VMware)    if (-f "$builddir/scripts/diffconfig") {
3267b2b07ea2SSteven Rostedt (VMware)	$diffexec = "$builddir/scripts/diffconfig";
3268b2b07ea2SSteven Rostedt (VMware)    }
32690a05c769SSteven Rostedt    doprint "\n\n***************************************\n";
3270a9adc261SScott Wood    doprint "No more config bisecting possible.\n";
3271b2b07ea2SSteven Rostedt (VMware)    run_command "$diffexec $good $bad", 1;
32720a05c769SSteven Rostedt    doprint "***************************************\n\n";
32730a05c769SSteven Rostedt}
32740a05c769SSteven Rostedt
32756071c22eSSteven Rostedt (Red Hat)sub run_config_bisect {
3276a9adc261SScott Wood    my ($good, $bad, $last_result) = @_;
3277133087f0SSteven Rostedt (VMware)    my $reset = "";
3278a9adc261SScott Wood    my $cmd;
32796071c22eSSteven Rostedt (Red Hat)    my $ret;
32806071c22eSSteven Rostedt (Red Hat)
3281133087f0SSteven Rostedt (VMware)    if (!length($last_result)) {
3282133087f0SSteven Rostedt (VMware)	$reset = "-r";
3283133087f0SSteven Rostedt (VMware)    }
328440667fb5SSteven Rostedt (VMware)    run_command "$config_bisect_exec $reset -b $outputdir $good $bad $last_result", 1;
32856071c22eSSteven Rostedt (Red Hat)
3286a9adc261SScott Wood    # config-bisect returns:
3287a9adc261SScott Wood    #   0 if there is more to bisect
3288a9adc261SScott Wood    #   1 for finding a good config
3289a9adc261SScott Wood    #   2 if it can not find any more configs
3290a9adc261SScott Wood    #  -1 (255) on error
3291a9adc261SScott Wood    if ($run_command_status) {
3292a9adc261SScott Wood	return $run_command_status;
32936071c22eSSteven Rostedt (Red Hat)    }
32946071c22eSSteven Rostedt (Red Hat)
3295a9adc261SScott Wood    $ret = run_config_bisect_test $config_bisect_type;
32966071c22eSSteven Rostedt (Red Hat)    if ($ret) {
32979dce29e6SSteven Rostedt (VMware)	doprint "NEW GOOD CONFIG ($pass)\n";
32989dce29e6SSteven Rostedt (VMware)	system("cp $output_config $tmpdir/good_config.tmp.$pass");
32999dce29e6SSteven Rostedt (VMware)	$pass++;
3300a9adc261SScott Wood	# Return 3 for good config
3301a9adc261SScott Wood	return 3;
3302cf79fab6SSteven Rostedt    } else {
33039dce29e6SSteven Rostedt (VMware)	doprint "NEW BAD CONFIG ($pass)\n";
33049dce29e6SSteven Rostedt (VMware)	system("cp $output_config $tmpdir/bad_config.tmp.$pass");
33059dce29e6SSteven Rostedt (VMware)	$pass++;
3306a9adc261SScott Wood	# Return 4 for bad config
3307a9adc261SScott Wood	return 4;
33086071c22eSSteven Rostedt (Red Hat)    }
33090a05c769SSteven Rostedt}
33100a05c769SSteven Rostedt
33110a05c769SSteven Rostedtsub config_bisect {
33120a05c769SSteven Rostedt    my ($i) = @_;
33130a05c769SSteven Rostedt
3314a9adc261SScott Wood    my $good_config;
3315a9adc261SScott Wood    my $bad_config;
3316a9adc261SScott Wood
33176071c22eSSteven Rostedt (Red Hat)    my $type = $config_bisect_type;
3318c4d1d11fSSteven Rostedt (Red Hat)    my $ret;
33190a05c769SSteven Rostedt
33206071c22eSSteven Rostedt (Red Hat)    $bad_config = $config_bisect;
33210a05c769SSteven Rostedt
332230f75da5SSteven Rostedt    if (defined($config_bisect_good)) {
33236071c22eSSteven Rostedt (Red Hat)	$good_config = $config_bisect_good;
33246071c22eSSteven Rostedt (Red Hat)    } elsif (defined($minconfig)) {
33256071c22eSSteven Rostedt (Red Hat)	$good_config = $minconfig;
33260a05c769SSteven Rostedt    } else {
33276071c22eSSteven Rostedt (Red Hat)	doprint "No config specified, checking if defconfig works";
3328c4d1d11fSSteven Rostedt (Red Hat)	$ret = run_bisect_test $type, "defconfig";
33296071c22eSSteven Rostedt (Red Hat)	if (!$ret) {
33306071c22eSSteven Rostedt (Red Hat)	    fail "Have no good config to compare with, please set CONFIG_BISECT_GOOD";
33316071c22eSSteven Rostedt (Red Hat)	    return 1;
33326071c22eSSteven Rostedt (Red Hat)	}
33336071c22eSSteven Rostedt (Red Hat)	$good_config = $output_config;
33340a05c769SSteven Rostedt    }
33350a05c769SSteven Rostedt
333640667fb5SSteven Rostedt (VMware)    if (!defined($config_bisect_exec)) {
333740667fb5SSteven Rostedt (VMware)	# First check the location that ktest.pl ran
3338c043ccbfSJohn 'Warthog9' Hawley (VMware)	my @locations = (
3339c043ccbfSJohn 'Warthog9' Hawley (VMware)		"$pwd/config-bisect.pl",
334040667fb5SSteven Rostedt (VMware)		"$dirname/config-bisect.pl",
334140667fb5SSteven Rostedt (VMware)		"$builddir/tools/testing/ktest/config-bisect.pl",
334240667fb5SSteven Rostedt (VMware)		undef );
334340667fb5SSteven Rostedt (VMware)	foreach my $loc (@locations) {
334440667fb5SSteven Rostedt (VMware)	    doprint "loc = $loc\n";
334540667fb5SSteven Rostedt (VMware)	    $config_bisect_exec = $loc;
334640667fb5SSteven Rostedt (VMware)	    last if (defined($config_bisect_exec && -x $config_bisect_exec));
334740667fb5SSteven Rostedt (VMware)	}
334840667fb5SSteven Rostedt (VMware)	if (!defined($config_bisect_exec)) {
334940667fb5SSteven Rostedt (VMware)	    fail "Could not find an executable config-bisect.pl\n",
335040667fb5SSteven Rostedt (VMware)		"  Set CONFIG_BISECT_EXEC to point to config-bisect.pl";
335140667fb5SSteven Rostedt (VMware)	    return 1;
335240667fb5SSteven Rostedt (VMware)	}
335340667fb5SSteven Rostedt (VMware)    }
335440667fb5SSteven Rostedt (VMware)
33556071c22eSSteven Rostedt (Red Hat)    # we don't want min configs to cause issues here.
33566071c22eSSteven Rostedt (Red Hat)    doprint "Disabling 'MIN_CONFIG' for this test\n";
33576071c22eSSteven Rostedt (Red Hat)    undef $minconfig;
33580a05c769SSteven Rostedt
33596071c22eSSteven Rostedt (Red Hat)    my %good_configs;
33606071c22eSSteven Rostedt (Red Hat)    my %bad_configs;
33616071c22eSSteven Rostedt (Red Hat)    my %tmp_configs;
33620a05c769SSteven Rostedt
33635a57299aSSteven Rostedt (VMware)    if (-f "$tmpdir/good_config.tmp" || -f "$tmpdir/bad_config.tmp") {
33645a57299aSSteven Rostedt (VMware)	if (read_yn "Interrupted config-bisect. Continue (n - will start new)?") {
33655a57299aSSteven Rostedt (VMware)	    if (-f "$tmpdir/good_config.tmp") {
33665a57299aSSteven Rostedt (VMware)		$good_config = "$tmpdir/good_config.tmp";
33675a57299aSSteven Rostedt (VMware)	    } else {
33685a57299aSSteven Rostedt (VMware)		$good_config = "$tmpdir/good_config";
33695a57299aSSteven Rostedt (VMware)	    }
33705a57299aSSteven Rostedt (VMware)	    if (-f "$tmpdir/bad_config.tmp") {
33715a57299aSSteven Rostedt (VMware)		$bad_config = "$tmpdir/bad_config.tmp";
33725a57299aSSteven Rostedt (VMware)	    } else {
33735a57299aSSteven Rostedt (VMware)		$bad_config = "$tmpdir/bad_config";
33745a57299aSSteven Rostedt (VMware)	    }
33755a57299aSSteven Rostedt (VMware)	}
33765a57299aSSteven Rostedt (VMware)    }
33776071c22eSSteven Rostedt (Red Hat)    doprint "Run good configs through make oldconfig\n";
33786071c22eSSteven Rostedt (Red Hat)    assign_configs \%tmp_configs, $good_config;
33796071c22eSSteven Rostedt (Red Hat)    create_config "$good_config", \%tmp_configs;
3380a9adc261SScott Wood    $good_config = "$tmpdir/good_config";
3381a9adc261SScott Wood    system("cp $output_config $good_config") == 0 or dodie "cp good config";
33820a05c769SSteven Rostedt
33836071c22eSSteven Rostedt (Red Hat)    doprint "Run bad configs through make oldconfig\n";
33846071c22eSSteven Rostedt (Red Hat)    assign_configs \%tmp_configs, $bad_config;
33856071c22eSSteven Rostedt (Red Hat)    create_config "$bad_config", \%tmp_configs;
33866071c22eSSteven Rostedt (Red Hat)    $bad_config = "$tmpdir/bad_config";
3387a9adc261SScott Wood    system("cp $output_config $bad_config") == 0 or dodie "cp bad config";
33880a05c769SSteven Rostedt
3389c4d1d11fSSteven Rostedt (Red Hat)    if (defined($config_bisect_check) && $config_bisect_check ne "0") {
3390c4d1d11fSSteven Rostedt (Red Hat)	if ($config_bisect_check ne "good") {
3391c4d1d11fSSteven Rostedt (Red Hat)	    doprint "Testing bad config\n";
3392c4d1d11fSSteven Rostedt (Red Hat)
3393c4d1d11fSSteven Rostedt (Red Hat)	    $ret = run_bisect_test $type, "useconfig:$bad_config";
3394c4d1d11fSSteven Rostedt (Red Hat)	    if ($ret) {
3395c4d1d11fSSteven Rostedt (Red Hat)		fail "Bad config succeeded when expected to fail!";
3396c4d1d11fSSteven Rostedt (Red Hat)		return 0;
3397c4d1d11fSSteven Rostedt (Red Hat)	    }
3398c4d1d11fSSteven Rostedt (Red Hat)	}
3399c4d1d11fSSteven Rostedt (Red Hat)	if ($config_bisect_check ne "bad") {
3400c4d1d11fSSteven Rostedt (Red Hat)	    doprint "Testing good config\n";
3401c4d1d11fSSteven Rostedt (Red Hat)
3402c4d1d11fSSteven Rostedt (Red Hat)	    $ret = run_bisect_test $type, "useconfig:$good_config";
3403c4d1d11fSSteven Rostedt (Red Hat)	    if (!$ret) {
3404c4d1d11fSSteven Rostedt (Red Hat)		fail "Good config failed when expected to succeed!";
3405c4d1d11fSSteven Rostedt (Red Hat)		return 0;
3406c4d1d11fSSteven Rostedt (Red Hat)	    }
3407c4d1d11fSSteven Rostedt (Red Hat)	}
3408c4d1d11fSSteven Rostedt (Red Hat)    }
3409b0918612SSteven Rostedt
3410a9adc261SScott Wood    my $last_run = "";
3411a9adc261SScott Wood
34120a05c769SSteven Rostedt    do {
3413a9adc261SScott Wood	$ret = run_config_bisect $good_config, $bad_config, $last_run;
3414a9adc261SScott Wood	if ($ret == 3) {
3415a9adc261SScott Wood	    $last_run = "good";
3416a9adc261SScott Wood	} elsif ($ret == 4) {
3417a9adc261SScott Wood	    $last_run = "bad";
3418a9adc261SScott Wood	}
341938fa3dc1SSteven Rostedt (Red Hat)	print_times;
3420a9adc261SScott Wood    } while ($ret == 3 || $ret == 4);
3421a9adc261SScott Wood
3422a9adc261SScott Wood    if ($ret == 2) {
3423a9adc261SScott Wood	config_bisect_end "$good_config.tmp", "$bad_config.tmp";
3424a9adc261SScott Wood    }
34250a05c769SSteven Rostedt
34260a05c769SSteven Rostedt    return $ret if ($ret < 0);
34275f9b6cedSSteven Rostedt
34285f9b6cedSSteven Rostedt    success $i;
34295f9b6cedSSteven Rostedt}
34305f9b6cedSSteven Rostedt
343127d934b2SSteven Rostedtsub patchcheck_reboot {
343227d934b2SSteven Rostedt    doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
3433bc7c5803SSteven Rostedt    reboot_to_good $patchcheck_sleep_time;
343427d934b2SSteven Rostedt}
343527d934b2SSteven Rostedt
34366c5ee0beSSteven Rostedtsub patchcheck {
34376c5ee0beSSteven Rostedt    my ($i) = @_;
34386c5ee0beSSteven Rostedt
3439df46fce6STim Tianyang Chen    dodie "PATCHCHECK_START[$i] not defined\n"
3440b5f4aea6SSteven Rostedt	if (!defined($patchcheck_start));
3441df46fce6STim Tianyang Chen    dodie "PATCHCHECK_TYPE[$i] not defined\n"
3442b5f4aea6SSteven Rostedt	if (!defined($patchcheck_type));
34436c5ee0beSSteven Rostedt
3444b5f4aea6SSteven Rostedt    my $start = $patchcheck_start;
34456c5ee0beSSteven Rostedt
344623a0e161SSteven Rostedt (Red Hat)    my $cherry = $patchcheck_cherry;
344723a0e161SSteven Rostedt (Red Hat)    if (!defined($cherry)) {
344823a0e161SSteven Rostedt (Red Hat)	$cherry = 0;
344923a0e161SSteven Rostedt (Red Hat)    }
345023a0e161SSteven Rostedt (Red Hat)
34516c5ee0beSSteven Rostedt    my $end = "HEAD";
3452b5f4aea6SSteven Rostedt    if (defined($patchcheck_end)) {
3453b5f4aea6SSteven Rostedt	$end = $patchcheck_end;
345423a0e161SSteven Rostedt (Red Hat)    } elsif ($cherry) {
3455df46fce6STim Tianyang Chen	dodie "PATCHCHECK_END must be defined with PATCHCHECK_CHERRY\n";
34566c5ee0beSSteven Rostedt    }
34576c5ee0beSSteven Rostedt
3458a57419b3SSteven Rostedt    # Get the true sha1's since we can use things like HEAD~3
3459a57419b3SSteven Rostedt    $start = get_sha1($start);
3460a57419b3SSteven Rostedt    $end = get_sha1($end);
3461a57419b3SSteven Rostedt
3462b5f4aea6SSteven Rostedt    my $type = $patchcheck_type;
34636c5ee0beSSteven Rostedt
34646c5ee0beSSteven Rostedt    # Can't have a test without having a test to run
34656c5ee0beSSteven Rostedt    if ($type eq "test" && !defined($run_test)) {
34666c5ee0beSSteven Rostedt	$type = "boot";
34676c5ee0beSSteven Rostedt    }
34686c5ee0beSSteven Rostedt
346923a0e161SSteven Rostedt (Red Hat)    if ($cherry) {
347023a0e161SSteven Rostedt (Red Hat)	open (IN, "git cherry -v $start $end|") or
347123a0e161SSteven Rostedt (Red Hat)	    dodie "could not get git list";
347223a0e161SSteven Rostedt (Red Hat)    } else {
34736c5ee0beSSteven Rostedt	open (IN, "git log --pretty=oneline $end|") or
34746c5ee0beSSteven Rostedt	    dodie "could not get git list";
347523a0e161SSteven Rostedt (Red Hat)    }
34766c5ee0beSSteven Rostedt
34776c5ee0beSSteven Rostedt    my @list;
34786c5ee0beSSteven Rostedt
34796c5ee0beSSteven Rostedt    while (<IN>) {
34806c5ee0beSSteven Rostedt	chomp;
348123a0e161SSteven Rostedt (Red Hat)	# git cherry adds a '+' we want to remove
348223a0e161SSteven Rostedt (Red Hat)	s/^\+ //;
34836c5ee0beSSteven Rostedt	$list[$#list+1] = $_;
34846c5ee0beSSteven Rostedt	last if (/^$start/);
34856c5ee0beSSteven Rostedt    }
34866c5ee0beSSteven Rostedt    close(IN);
34876c5ee0beSSteven Rostedt
348823a0e161SSteven Rostedt (Red Hat)    if (!$cherry) {
34896c5ee0beSSteven Rostedt	if ($list[$#list] !~ /^$start/) {
34902b7d9b21SSteven Rostedt	    fail "SHA1 $start not found";
34916c5ee0beSSteven Rostedt	}
34926c5ee0beSSteven Rostedt
34936c5ee0beSSteven Rostedt	# go backwards in the list
34946c5ee0beSSteven Rostedt	@list = reverse @list;
349523a0e161SSteven Rostedt (Red Hat)    }
349623a0e161SSteven Rostedt (Red Hat)
349723a0e161SSteven Rostedt (Red Hat)    doprint("Going to test the following commits:\n");
349823a0e161SSteven Rostedt (Red Hat)    foreach my $l (@list) {
349923a0e161SSteven Rostedt (Red Hat)	doprint "$l\n";
350023a0e161SSteven Rostedt (Red Hat)    }
35016c5ee0beSSteven Rostedt
35026c5ee0beSSteven Rostedt    my $save_clean = $noclean;
35031990207dSSteven Rostedt    my %ignored_warnings;
35041990207dSSteven Rostedt
35051990207dSSteven Rostedt    if (defined($ignore_warnings)) {
35061990207dSSteven Rostedt	foreach my $sha1 (split /\s+/, $ignore_warnings) {
35071990207dSSteven Rostedt	    $ignored_warnings{$sha1} = 1;
35081990207dSSteven Rostedt	}
35091990207dSSteven Rostedt    }
35106c5ee0beSSteven Rostedt
35116c5ee0beSSteven Rostedt    $in_patchcheck = 1;
35126c5ee0beSSteven Rostedt    foreach my $item (@list) {
35136c5ee0beSSteven Rostedt	my $sha1 = $item;
35146c5ee0beSSteven Rostedt	$sha1 =~ s/^([[:xdigit:]]+).*/$1/;
35156c5ee0beSSteven Rostedt
35167c2c49ecSSteven Rostedt (Red Hat)	doprint "\nProcessing commit \"$item\"\n\n";
35176c5ee0beSSteven Rostedt
35186c5ee0beSSteven Rostedt	run_command "git checkout $sha1" or
3519df46fce6STim Tianyang Chen	    dodie "Failed to checkout $sha1";
35206c5ee0beSSteven Rostedt
35216c5ee0beSSteven Rostedt	# only clean on the first and last patch
35226c5ee0beSSteven Rostedt	if ($item eq $list[0] ||
35236c5ee0beSSteven Rostedt	    $item eq $list[$#list]) {
35246c5ee0beSSteven Rostedt	    $noclean = $save_clean;
35256c5ee0beSSteven Rostedt	} else {
35266c5ee0beSSteven Rostedt	    $noclean = 1;
35276c5ee0beSSteven Rostedt	}
35286c5ee0beSSteven Rostedt
35296c5ee0beSSteven Rostedt	if (defined($minconfig)) {
35302b7d9b21SSteven Rostedt	    build "useconfig:$minconfig" or return 0;
35316c5ee0beSSteven Rostedt	} else {
35326c5ee0beSSteven Rostedt	    # ?? no config to use?
35332b7d9b21SSteven Rostedt	    build "oldconfig" or return 0;
35346c5ee0beSSteven Rostedt	}
35356c5ee0beSSteven Rostedt
35364283b169SSteven Rostedt (Red Hat)	# No need to do per patch checking if warnings file exists
35374283b169SSteven Rostedt (Red Hat)	if (!defined($warnings_file) && !defined($ignored_warnings{$sha1})) {
35384283b169SSteven Rostedt (Red Hat)	    check_patch_buildlog $sha1 or return 0;
35391990207dSSteven Rostedt	}
35406c5ee0beSSteven Rostedt
35414283b169SSteven Rostedt (Red Hat)	check_buildlog or return 0;
35424283b169SSteven Rostedt (Red Hat)
35436c5ee0beSSteven Rostedt	next if ($type eq "build");
35446c5ee0beSSteven Rostedt
35457faafbd6SSteven Rostedt	my $failed = 0;
35467faafbd6SSteven Rostedt
354764d98283SSteven Rostedt (Red Hat)	start_monitor_and_install or $failed = 1;
35487faafbd6SSteven Rostedt
35497faafbd6SSteven Rostedt	if (!$failed && $type ne "boot"){
35507faafbd6SSteven Rostedt	    do_run_test or $failed = 1;
35517faafbd6SSteven Rostedt	}
35527faafbd6SSteven Rostedt	end_monitor;
355338fa3dc1SSteven Rostedt (Red Hat)	if ($failed) {
355438fa3dc1SSteven Rostedt (Red Hat)	    print_times;
355538fa3dc1SSteven Rostedt (Red Hat)	    return 0;
355638fa3dc1SSteven Rostedt (Red Hat)	}
355727d934b2SSteven Rostedt	patchcheck_reboot;
355838fa3dc1SSteven Rostedt (Red Hat)	print_times;
35596c5ee0beSSteven Rostedt    }
35606c5ee0beSSteven Rostedt    $in_patchcheck = 0;
35616c5ee0beSSteven Rostedt    success $i;
35622b7d9b21SSteven Rostedt
35632b7d9b21SSteven Rostedt    return 1;
35646c5ee0beSSteven Rostedt}
35656c5ee0beSSteven Rostedt
3566ac6974c7SSteven Rostedtsub add_dep {
3567ac6974c7SSteven Rostedt    # $config depends on $dep
3568ac6974c7SSteven Rostedt    my ($config, $dep) = @_;
3569ac6974c7SSteven Rostedt
3570ac6974c7SSteven Rostedt    if (defined($depends{$config})) {
3571ac6974c7SSteven Rostedt	$depends{$config} .= " " . $dep;
3572ac6974c7SSteven Rostedt    } else {
3573ac6974c7SSteven Rostedt	$depends{$config} = $dep;
3574ac6974c7SSteven Rostedt    }
3575ac6974c7SSteven Rostedt
3576ac6974c7SSteven Rostedt    # record the number of configs depending on $dep
3577ac6974c7SSteven Rostedt    if (defined $depcount{$dep}) {
3578ac6974c7SSteven Rostedt	$depcount{$dep}++;
3579ac6974c7SSteven Rostedt    } else {
3580ac6974c7SSteven Rostedt	$depcount{$dep} = 1;
3581ac6974c7SSteven Rostedt    }
3582ac6974c7SSteven Rostedt}
3583ac6974c7SSteven Rostedt
3584b9066f6cSSteven Rostedt# taken from streamline_config.pl
3585b9066f6cSSteven Rostedtsub read_kconfig {
3586b9066f6cSSteven Rostedt    my ($kconfig) = @_;
3587b9066f6cSSteven Rostedt
3588b9066f6cSSteven Rostedt    my $state = "NONE";
3589b9066f6cSSteven Rostedt    my $config;
3590b9066f6cSSteven Rostedt    my @kconfigs;
3591b9066f6cSSteven Rostedt
3592b9066f6cSSteven Rostedt    my $cont = 0;
3593b9066f6cSSteven Rostedt    my $line;
3594b9066f6cSSteven Rostedt
3595b9066f6cSSteven Rostedt    if (! -f $kconfig) {
3596b9066f6cSSteven Rostedt	doprint "file $kconfig does not exist, skipping\n";
3597b9066f6cSSteven Rostedt	return;
3598b9066f6cSSteven Rostedt    }
3599b9066f6cSSteven Rostedt
3600b9066f6cSSteven Rostedt    open(KIN, "$kconfig")
3601df46fce6STim Tianyang Chen	or dodie "Can't open $kconfig";
3602b9066f6cSSteven Rostedt    while (<KIN>) {
3603b9066f6cSSteven Rostedt	chomp;
3604b9066f6cSSteven Rostedt
3605b9066f6cSSteven Rostedt	# Make sure that lines ending with \ continue
3606b9066f6cSSteven Rostedt	if ($cont) {
3607b9066f6cSSteven Rostedt	    $_ = $line . " " . $_;
3608b9066f6cSSteven Rostedt	}
3609b9066f6cSSteven Rostedt
3610b9066f6cSSteven Rostedt	if (s/\\$//) {
3611b9066f6cSSteven Rostedt	    $cont = 1;
3612b9066f6cSSteven Rostedt	    $line = $_;
3613b9066f6cSSteven Rostedt	    next;
3614b9066f6cSSteven Rostedt	}
3615b9066f6cSSteven Rostedt
3616b9066f6cSSteven Rostedt	$cont = 0;
3617b9066f6cSSteven Rostedt
3618b9066f6cSSteven Rostedt	# collect any Kconfig sources
3619b9066f6cSSteven Rostedt	if (/^source\s*"(.*)"/) {
3620b9066f6cSSteven Rostedt	    $kconfigs[$#kconfigs+1] = $1;
3621b9066f6cSSteven Rostedt	}
3622b9066f6cSSteven Rostedt
3623b9066f6cSSteven Rostedt	# configs found
3624b9066f6cSSteven Rostedt	if (/^\s*(menu)?config\s+(\S+)\s*$/) {
3625b9066f6cSSteven Rostedt	    $state = "NEW";
3626b9066f6cSSteven Rostedt	    $config = $2;
3627b9066f6cSSteven Rostedt
3628b9066f6cSSteven Rostedt	    for (my $i = 0; $i < $iflevel; $i++) {
3629ac6974c7SSteven Rostedt		add_dep $config, $ifdeps[$i];
3630b9066f6cSSteven Rostedt	    }
3631b9066f6cSSteven Rostedt
3632b9066f6cSSteven Rostedt	# collect the depends for the config
3633b9066f6cSSteven Rostedt	} elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
3634b9066f6cSSteven Rostedt
3635ac6974c7SSteven Rostedt	    add_dep $config, $1;
3636b9066f6cSSteven Rostedt
3637b9066f6cSSteven Rostedt	# Get the configs that select this config
3638ac6974c7SSteven Rostedt	} elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
3639ac6974c7SSteven Rostedt
3640ac6974c7SSteven Rostedt	    # selected by depends on config
3641ac6974c7SSteven Rostedt	    add_dep $1, $config;
3642b9066f6cSSteven Rostedt
3643b9066f6cSSteven Rostedt	# Check for if statements
3644b9066f6cSSteven Rostedt	} elsif (/^if\s+(.*\S)\s*$/) {
3645b9066f6cSSteven Rostedt	    my $deps = $1;
3646b9066f6cSSteven Rostedt	    # remove beginning and ending non text
3647b9066f6cSSteven Rostedt	    $deps =~ s/^[^a-zA-Z0-9_]*//;
3648b9066f6cSSteven Rostedt	    $deps =~ s/[^a-zA-Z0-9_]*$//;
3649b9066f6cSSteven Rostedt
3650b9066f6cSSteven Rostedt	    my @deps = split /[^a-zA-Z0-9_]+/, $deps;
3651b9066f6cSSteven Rostedt
3652b9066f6cSSteven Rostedt	    $ifdeps[$iflevel++] = join ':', @deps;
3653b9066f6cSSteven Rostedt
3654b9066f6cSSteven Rostedt	} elsif (/^endif/) {
3655b9066f6cSSteven Rostedt
3656b9066f6cSSteven Rostedt	    $iflevel-- if ($iflevel);
3657b9066f6cSSteven Rostedt
3658b9066f6cSSteven Rostedt	# stop on "help"
3659b9066f6cSSteven Rostedt	} elsif (/^\s*help\s*$/) {
3660b9066f6cSSteven Rostedt	    $state = "NONE";
3661b9066f6cSSteven Rostedt	}
3662b9066f6cSSteven Rostedt    }
3663b9066f6cSSteven Rostedt    close(KIN);
3664b9066f6cSSteven Rostedt
3665b9066f6cSSteven Rostedt    # read in any configs that were found.
3666b9066f6cSSteven Rostedt    foreach $kconfig (@kconfigs) {
3667b9066f6cSSteven Rostedt	if (!defined($read_kconfigs{$kconfig})) {
3668b9066f6cSSteven Rostedt	    $read_kconfigs{$kconfig} = 1;
3669b9066f6cSSteven Rostedt	    read_kconfig("$builddir/$kconfig");
3670b9066f6cSSteven Rostedt	}
3671b9066f6cSSteven Rostedt    }
3672b9066f6cSSteven Rostedt}
3673b9066f6cSSteven Rostedt
3674b9066f6cSSteven Rostedtsub read_depends {
3675b9066f6cSSteven Rostedt    # find out which arch this is by the kconfig file
3676c043ccbfSJohn 'Warthog9' Hawley (VMware)    open (IN, $output_config) or
3677c043ccbfSJohn 'Warthog9' Hawley (VMware)	dodie "Failed to read $output_config";
3678b9066f6cSSteven Rostedt    my $arch;
3679b9066f6cSSteven Rostedt    while (<IN>) {
3680b9066f6cSSteven Rostedt	if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
3681b9066f6cSSteven Rostedt	    $arch = $1;
3682b9066f6cSSteven Rostedt	    last;
3683b9066f6cSSteven Rostedt	}
3684b9066f6cSSteven Rostedt    }
3685b9066f6cSSteven Rostedt    close IN;
3686b9066f6cSSteven Rostedt
3687b9066f6cSSteven Rostedt    if (!defined($arch)) {
3688b9066f6cSSteven Rostedt	doprint "Could not find arch from config file\n";
3689b9066f6cSSteven Rostedt	doprint "no dependencies used\n";
3690b9066f6cSSteven Rostedt	return;
3691b9066f6cSSteven Rostedt    }
3692b9066f6cSSteven Rostedt
3693b9066f6cSSteven Rostedt    # arch is really the subarch, we need to know
3694b9066f6cSSteven Rostedt    # what directory to look at.
3695b9066f6cSSteven Rostedt    if ($arch eq "i386" || $arch eq "x86_64") {
3696b9066f6cSSteven Rostedt	$arch = "x86";
3697b9066f6cSSteven Rostedt    }
3698b9066f6cSSteven Rostedt
3699b9066f6cSSteven Rostedt    my $kconfig = "$builddir/arch/$arch/Kconfig";
3700b9066f6cSSteven Rostedt
3701b9066f6cSSteven Rostedt    if (! -f $kconfig && $arch =~ /\d$/) {
3702b9066f6cSSteven Rostedt	my $orig = $arch;
3703b9066f6cSSteven Rostedt	# some subarchs have numbers, truncate them
3704b9066f6cSSteven Rostedt	$arch =~ s/\d*$//;
3705b9066f6cSSteven Rostedt	$kconfig = "$builddir/arch/$arch/Kconfig";
3706b9066f6cSSteven Rostedt	if (! -f $kconfig) {
3707b9066f6cSSteven Rostedt	    doprint "No idea what arch dir $orig is for\n";
3708b9066f6cSSteven Rostedt	    doprint "no dependencies used\n";
3709b9066f6cSSteven Rostedt	    return;
3710b9066f6cSSteven Rostedt	}
3711b9066f6cSSteven Rostedt    }
3712b9066f6cSSteven Rostedt
3713b9066f6cSSteven Rostedt    read_kconfig($kconfig);
3714b9066f6cSSteven Rostedt}
3715b9066f6cSSteven Rostedt
37164c4ab120SSteven Rostedtsub make_new_config {
37174c4ab120SSteven Rostedt    my @configs = @_;
37184c4ab120SSteven Rostedt
37194c4ab120SSteven Rostedt    open (OUT, ">$output_config")
37204c4ab120SSteven Rostedt	or dodie "Failed to write $output_config";
37214c4ab120SSteven Rostedt
37224c4ab120SSteven Rostedt    foreach my $config (@configs) {
37234c4ab120SSteven Rostedt	print OUT "$config\n";
37244c4ab120SSteven Rostedt    }
37254c4ab120SSteven Rostedt    close OUT;
37264c4ab120SSteven Rostedt}
37274c4ab120SSteven Rostedt
3728ac6974c7SSteven Rostedtsub chomp_config {
3729ac6974c7SSteven Rostedt    my ($config) = @_;
3730ac6974c7SSteven Rostedt
3731ac6974c7SSteven Rostedt    $config =~ s/CONFIG_//;
3732ac6974c7SSteven Rostedt
3733ac6974c7SSteven Rostedt    return $config;
3734ac6974c7SSteven Rostedt}
3735ac6974c7SSteven Rostedt
3736b9066f6cSSteven Rostedtsub get_depends {
3737b9066f6cSSteven Rostedt    my ($dep) = @_;
3738b9066f6cSSteven Rostedt
3739ac6974c7SSteven Rostedt    my $kconfig = chomp_config $dep;
3740b9066f6cSSteven Rostedt
3741b9066f6cSSteven Rostedt    $dep = $depends{"$kconfig"};
3742b9066f6cSSteven Rostedt
3743b9066f6cSSteven Rostedt    # the dep string we have saves the dependencies as they
3744b9066f6cSSteven Rostedt    # were found, including expressions like ! && ||. We
3745b9066f6cSSteven Rostedt    # want to split this out into just an array of configs.
3746b9066f6cSSteven Rostedt
3747b9066f6cSSteven Rostedt    my $valid = "A-Za-z_0-9";
3748b9066f6cSSteven Rostedt
3749b9066f6cSSteven Rostedt    my @configs;
3750b9066f6cSSteven Rostedt
3751b9066f6cSSteven Rostedt    while ($dep =~ /[$valid]/) {
3752b9066f6cSSteven Rostedt	if ($dep =~ /^[^$valid]*([$valid]+)/) {
3753b9066f6cSSteven Rostedt	    my $conf = "CONFIG_" . $1;
3754b9066f6cSSteven Rostedt
3755b9066f6cSSteven Rostedt	    $configs[$#configs + 1] = $conf;
3756b9066f6cSSteven Rostedt
3757b9066f6cSSteven Rostedt	    $dep =~ s/^[^$valid]*[$valid]+//;
3758b9066f6cSSteven Rostedt	} else {
3759df46fce6STim Tianyang Chen	    dodie "this should never happen";
3760b9066f6cSSteven Rostedt	}
3761b9066f6cSSteven Rostedt    }
3762b9066f6cSSteven Rostedt
3763b9066f6cSSteven Rostedt    return @configs;
3764b9066f6cSSteven Rostedt}
3765b9066f6cSSteven Rostedt
3766b9066f6cSSteven Rostedtsub test_this_config {
3767b9066f6cSSteven Rostedt    my ($config) = @_;
3768b9066f6cSSteven Rostedt
3769b9066f6cSSteven Rostedt    my $found;
3770b9066f6cSSteven Rostedt
3771b9066f6cSSteven Rostedt    # if we already processed this config, skip it
3772b9066f6cSSteven Rostedt    if (defined($processed_configs{$config})) {
3773b9066f6cSSteven Rostedt	return undef;
3774b9066f6cSSteven Rostedt    }
3775b9066f6cSSteven Rostedt    $processed_configs{$config} = 1;
3776b9066f6cSSteven Rostedt
3777b9066f6cSSteven Rostedt    # if this config failed during this round, skip it
3778b9066f6cSSteven Rostedt    if (defined($nochange_config{$config})) {
3779b9066f6cSSteven Rostedt	return undef;
3780b9066f6cSSteven Rostedt    }
3781b9066f6cSSteven Rostedt
3782ac6974c7SSteven Rostedt    my $kconfig = chomp_config $config;
3783b9066f6cSSteven Rostedt
3784b9066f6cSSteven Rostedt    # Test dependencies first
3785b9066f6cSSteven Rostedt    if (defined($depends{"$kconfig"})) {
3786b9066f6cSSteven Rostedt	my @parents = get_depends $config;
3787b9066f6cSSteven Rostedt	foreach my $parent (@parents) {
3788b9066f6cSSteven Rostedt	    # if the parent is in the min config, check it first
3789b9066f6cSSteven Rostedt	    next if (!defined($min_configs{$parent}));
3790b9066f6cSSteven Rostedt	    $found = test_this_config($parent);
3791b9066f6cSSteven Rostedt	    if (defined($found)) {
3792b9066f6cSSteven Rostedt		return $found;
3793b9066f6cSSteven Rostedt	    }
3794b9066f6cSSteven Rostedt	}
3795b9066f6cSSteven Rostedt    }
3796b9066f6cSSteven Rostedt
3797b9066f6cSSteven Rostedt    # Remove this config from the list of configs
3798fb16d891SAdam Lee    # do a make olddefconfig and then read the resulting
3799b9066f6cSSteven Rostedt    # .config to make sure it is missing the config that
3800b9066f6cSSteven Rostedt    # we had before
3801b9066f6cSSteven Rostedt    my %configs = %min_configs;
3802ef784eebSSteven Rostedt    $configs{$config} = "# $config is not set";
3803b9066f6cSSteven Rostedt    make_new_config ((values %configs), (values %keep_configs));
3804b9066f6cSSteven Rostedt    make_oldconfig;
3805ef784eebSSteven Rostedt    delete $configs{$config};
3806b9066f6cSSteven Rostedt    undef %configs;
3807b9066f6cSSteven Rostedt    assign_configs \%configs, $output_config;
3808b9066f6cSSteven Rostedt
38099972fc0bSSteven Rostedt (Red Hat)    if (!defined($configs{$config}) || $configs{$config} =~ /^#/) {
38109972fc0bSSteven Rostedt (Red Hat)	return $config;
38119972fc0bSSteven Rostedt (Red Hat)    }
3812b9066f6cSSteven Rostedt
3813b9066f6cSSteven Rostedt    doprint "disabling config $config did not change .config\n";
3814b9066f6cSSteven Rostedt
3815b9066f6cSSteven Rostedt    $nochange_config{$config} = 1;
3816b9066f6cSSteven Rostedt
3817b9066f6cSSteven Rostedt    return undef;
3818b9066f6cSSteven Rostedt}
3819b9066f6cSSteven Rostedt
38204c4ab120SSteven Rostedtsub make_min_config {
38214c4ab120SSteven Rostedt    my ($i) = @_;
38224c4ab120SSteven Rostedt
3823ccc513b6SSteven Rostedt    my $type = $minconfig_type;
3824ccc513b6SSteven Rostedt    if ($type ne "boot" && $type ne "test") {
3825ccc513b6SSteven Rostedt	fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3826ccc513b6SSteven Rostedt	    " make_min_config works only with 'boot' and 'test'\n" and return;
3827ccc513b6SSteven Rostedt    }
3828ccc513b6SSteven Rostedt
38294c4ab120SSteven Rostedt    if (!defined($output_minconfig)) {
38304c4ab120SSteven Rostedt	fail "OUTPUT_MIN_CONFIG not defined" and return;
38314c4ab120SSteven Rostedt    }
383235ce5952SSteven Rostedt
383335ce5952SSteven Rostedt    # If output_minconfig exists, and the start_minconfig
383435ce5952SSteven Rostedt    # came from min_config, than ask if we should use
383535ce5952SSteven Rostedt    # that instead.
383635ce5952SSteven Rostedt    if (-f $output_minconfig && !$start_minconfig_defined) {
383735ce5952SSteven Rostedt	print "$output_minconfig exists\n";
383843de3316SSteven Rostedt	if (!defined($use_output_minconfig)) {
383935ce5952SSteven Rostedt	    if (read_yn " Use it as minconfig?") {
384035ce5952SSteven Rostedt		$start_minconfig = $output_minconfig;
384135ce5952SSteven Rostedt	    }
384243de3316SSteven Rostedt	} elsif ($use_output_minconfig > 0) {
384343de3316SSteven Rostedt	    doprint "Using $output_minconfig as MIN_CONFIG\n";
384443de3316SSteven Rostedt	    $start_minconfig = $output_minconfig;
384543de3316SSteven Rostedt	} else {
384643de3316SSteven Rostedt	    doprint "Set to still use MIN_CONFIG as starting point\n";
384743de3316SSteven Rostedt	}
384835ce5952SSteven Rostedt    }
384935ce5952SSteven Rostedt
38504c4ab120SSteven Rostedt    if (!defined($start_minconfig)) {
38514c4ab120SSteven Rostedt	fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
38524c4ab120SSteven Rostedt    }
38534c4ab120SSteven Rostedt
385435ce5952SSteven Rostedt    my $temp_config = "$tmpdir/temp_config";
385535ce5952SSteven Rostedt
38564c4ab120SSteven Rostedt    # First things first. We build an allnoconfig to find
38574c4ab120SSteven Rostedt    # out what the defaults are that we can't touch.
38584c4ab120SSteven Rostedt    # Some are selections, but we really can't handle selections.
38594c4ab120SSteven Rostedt
38604c4ab120SSteven Rostedt    my $save_minconfig = $minconfig;
38614c4ab120SSteven Rostedt    undef $minconfig;
38624c4ab120SSteven Rostedt
38634c4ab120SSteven Rostedt    run_command "$make allnoconfig" or return 0;
38644c4ab120SSteven Rostedt
3865b9066f6cSSteven Rostedt    read_depends;
3866b9066f6cSSteven Rostedt
38674c4ab120SSteven Rostedt    process_config_ignore $output_config;
3868b9066f6cSSteven Rostedt
386943d1b651SSteven Rostedt    undef %save_configs;
3870b9066f6cSSteven Rostedt    undef %min_configs;
38714c4ab120SSteven Rostedt
38724c4ab120SSteven Rostedt    if (defined($ignore_config)) {
38734c4ab120SSteven Rostedt	# make sure the file exists
38744c4ab120SSteven Rostedt	`touch $ignore_config`;
387543d1b651SSteven Rostedt	assign_configs \%save_configs, $ignore_config;
38764c4ab120SSteven Rostedt    }
38774c4ab120SSteven Rostedt
387843d1b651SSteven Rostedt    %keep_configs = %save_configs;
387943d1b651SSteven Rostedt
38804c4ab120SSteven Rostedt    doprint "Load initial configs from $start_minconfig\n";
38814c4ab120SSteven Rostedt
38824c4ab120SSteven Rostedt    # Look at the current min configs, and save off all the
38834c4ab120SSteven Rostedt    # ones that were set via the allnoconfig
38844c4ab120SSteven Rostedt    assign_configs \%min_configs, $start_minconfig;
38854c4ab120SSteven Rostedt
38864c4ab120SSteven Rostedt    my @config_keys = keys %min_configs;
38874c4ab120SSteven Rostedt
3888ac6974c7SSteven Rostedt    # All configs need a depcount
3889ac6974c7SSteven Rostedt    foreach my $config (@config_keys) {
3890ac6974c7SSteven Rostedt	my $kconfig = chomp_config $config;
3891ac6974c7SSteven Rostedt	if (!defined $depcount{$kconfig}) {
3892ac6974c7SSteven Rostedt	    $depcount{$kconfig} = 0;
3893ac6974c7SSteven Rostedt	}
3894ac6974c7SSteven Rostedt    }
3895ac6974c7SSteven Rostedt
38964c4ab120SSteven Rostedt    # Remove anything that was set by the make allnoconfig
38974c4ab120SSteven Rostedt    # we shouldn't need them as they get set for us anyway.
38984c4ab120SSteven Rostedt    foreach my $config (@config_keys) {
38994c4ab120SSteven Rostedt	# Remove anything in the ignore_config
39004c4ab120SSteven Rostedt	if (defined($keep_configs{$config})) {
39014c4ab120SSteven Rostedt	    my $file = $ignore_config;
39024c4ab120SSteven Rostedt	    $file =~ s,.*/(.*?)$,$1,;
39034c4ab120SSteven Rostedt	    doprint "$config set by $file ... ignored\n";
39044c4ab120SSteven Rostedt	    delete $min_configs{$config};
39054c4ab120SSteven Rostedt	    next;
39064c4ab120SSteven Rostedt	}
39074c4ab120SSteven Rostedt	# But make sure the settings are the same. If a min config
39084c4ab120SSteven Rostedt	# sets a selection, we do not want to get rid of it if
39094c4ab120SSteven Rostedt	# it is not the same as what we have. Just move it into
39104c4ab120SSteven Rostedt	# the keep configs.
39114c4ab120SSteven Rostedt	if (defined($config_ignore{$config})) {
39124c4ab120SSteven Rostedt	    if ($config_ignore{$config} ne $min_configs{$config}) {
39134c4ab120SSteven Rostedt		doprint "$config is in allnoconfig as '$config_ignore{$config}'";
39144c4ab120SSteven Rostedt		doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
39154c4ab120SSteven Rostedt		$keep_configs{$config} = $min_configs{$config};
39164c4ab120SSteven Rostedt	    } else {
39174c4ab120SSteven Rostedt		doprint "$config set by allnoconfig ... ignored\n";
39184c4ab120SSteven Rostedt	    }
39194c4ab120SSteven Rostedt	    delete $min_configs{$config};
39204c4ab120SSteven Rostedt	}
39214c4ab120SSteven Rostedt    }
39224c4ab120SSteven Rostedt
39234c4ab120SSteven Rostedt    my $done = 0;
3924b9066f6cSSteven Rostedt    my $take_two = 0;
39254c4ab120SSteven Rostedt
39264c4ab120SSteven Rostedt    while (!$done) {
39274c4ab120SSteven Rostedt	my $config;
39284c4ab120SSteven Rostedt	my $found;
39294c4ab120SSteven Rostedt
39304c4ab120SSteven Rostedt	# Now disable each config one by one and do a make oldconfig
39314c4ab120SSteven Rostedt	# till we find a config that changes our list.
39324c4ab120SSteven Rostedt
39334c4ab120SSteven Rostedt	my @test_configs = keys %min_configs;
3934ac6974c7SSteven Rostedt
3935ac6974c7SSteven Rostedt	# Sort keys by who is most dependent on
3936ac6974c7SSteven Rostedt	@test_configs = sort  { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3937ac6974c7SSteven Rostedt	    @test_configs ;
3938ac6974c7SSteven Rostedt
3939ac6974c7SSteven Rostedt	# Put configs that did not modify the config at the end.
39404c4ab120SSteven Rostedt	my $reset = 1;
39414c4ab120SSteven Rostedt	for (my $i = 0; $i < $#test_configs; $i++) {
39424c4ab120SSteven Rostedt	    if (!defined($nochange_config{$test_configs[0]})) {
39434c4ab120SSteven Rostedt		$reset = 0;
39444c4ab120SSteven Rostedt		last;
39454c4ab120SSteven Rostedt	    }
39464c4ab120SSteven Rostedt	    # This config didn't change the .config last time.
39474c4ab120SSteven Rostedt	    # Place it at the end
39484c4ab120SSteven Rostedt	    my $config = shift @test_configs;
39494c4ab120SSteven Rostedt	    push @test_configs, $config;
39504c4ab120SSteven Rostedt	}
39514c4ab120SSteven Rostedt
39524c4ab120SSteven Rostedt	# if every test config has failed to modify the .config file
39534c4ab120SSteven Rostedt	# in the past, then reset and start over.
39544c4ab120SSteven Rostedt	if ($reset) {
39554c4ab120SSteven Rostedt	    undef %nochange_config;
39564c4ab120SSteven Rostedt	}
39574c4ab120SSteven Rostedt
3958b9066f6cSSteven Rostedt	undef %processed_configs;
3959b9066f6cSSteven Rostedt
39604c4ab120SSteven Rostedt	foreach my $config (@test_configs) {
39614c4ab120SSteven Rostedt
3962b9066f6cSSteven Rostedt	    $found = test_this_config $config;
39634c4ab120SSteven Rostedt
3964b9066f6cSSteven Rostedt	    last if (defined($found));
39654c4ab120SSteven Rostedt
39664c4ab120SSteven Rostedt	    # oh well, try another config
39674c4ab120SSteven Rostedt	}
39684c4ab120SSteven Rostedt
39694c4ab120SSteven Rostedt	if (!defined($found)) {
3970b9066f6cSSteven Rostedt	    # we could have failed due to the nochange_config hash
3971b9066f6cSSteven Rostedt	    # reset and try again
3972b9066f6cSSteven Rostedt	    if (!$take_two) {
3973b9066f6cSSteven Rostedt		undef %nochange_config;
3974b9066f6cSSteven Rostedt		$take_two = 1;
3975b9066f6cSSteven Rostedt		next;
3976b9066f6cSSteven Rostedt	    }
39774c4ab120SSteven Rostedt	    doprint "No more configs found that we can disable\n";
39784c4ab120SSteven Rostedt	    $done = 1;
39794c4ab120SSteven Rostedt	    last;
39804c4ab120SSteven Rostedt	}
3981b9066f6cSSteven Rostedt	$take_two = 0;
39824c4ab120SSteven Rostedt
39834c4ab120SSteven Rostedt	$config = $found;
39844c4ab120SSteven Rostedt
39854c4ab120SSteven Rostedt	doprint "Test with $config disabled\n";
39864c4ab120SSteven Rostedt
39874c4ab120SSteven Rostedt	# set in_bisect to keep build and monitor from dieing
39884c4ab120SSteven Rostedt	$in_bisect = 1;
39894c4ab120SSteven Rostedt
39904c4ab120SSteven Rostedt	my $failed = 0;
3991bf1c95abSSteven Rostedt	build "oldconfig" or $failed = 1;
3992bf1c95abSSteven Rostedt	if (!$failed) {
399364d98283SSteven Rostedt (Red Hat)	    start_monitor_and_install or $failed = 1;
3994ccc513b6SSteven Rostedt
3995ccc513b6SSteven Rostedt	    if ($type eq "test" && !$failed) {
3996ccc513b6SSteven Rostedt		do_run_test or $failed = 1;
3997ccc513b6SSteven Rostedt	    }
3998ccc513b6SSteven Rostedt
39994c4ab120SSteven Rostedt	    end_monitor;
4000bf1c95abSSteven Rostedt	}
40014c4ab120SSteven Rostedt
40024c4ab120SSteven Rostedt	$in_bisect = 0;
40034c4ab120SSteven Rostedt
40044c4ab120SSteven Rostedt	if ($failed) {
4005b9066f6cSSteven Rostedt	    doprint "$min_configs{$config} is needed to boot the box... keeping\n";
40064c4ab120SSteven Rostedt	    # this config is needed, add it to the ignore list.
40074c4ab120SSteven Rostedt	    $keep_configs{$config} = $min_configs{$config};
400843d1b651SSteven Rostedt	    $save_configs{$config} = $min_configs{$config};
40094c4ab120SSteven Rostedt	    delete $min_configs{$config};
401035ce5952SSteven Rostedt
401135ce5952SSteven Rostedt	    # update new ignore configs
401235ce5952SSteven Rostedt	    if (defined($ignore_config)) {
4013c043ccbfSJohn 'Warthog9' Hawley (VMware)		open (OUT, ">$temp_config") or
4014c043ccbfSJohn 'Warthog9' Hawley (VMware)		    dodie "Can't write to $temp_config";
401543d1b651SSteven Rostedt		foreach my $config (keys %save_configs) {
401643d1b651SSteven Rostedt		    print OUT "$save_configs{$config}\n";
401735ce5952SSteven Rostedt		}
401835ce5952SSteven Rostedt		close OUT;
401935ce5952SSteven Rostedt		run_command "mv $temp_config $ignore_config" or
402035ce5952SSteven Rostedt		    dodie "failed to copy update to $ignore_config";
402135ce5952SSteven Rostedt	    }
402235ce5952SSteven Rostedt
40234c4ab120SSteven Rostedt	} else {
40244c4ab120SSteven Rostedt	    # We booted without this config, remove it from the minconfigs.
40254c4ab120SSteven Rostedt	    doprint "$config is not needed, disabling\n";
40264c4ab120SSteven Rostedt
40274c4ab120SSteven Rostedt	    delete $min_configs{$config};
40284c4ab120SSteven Rostedt
40294c4ab120SSteven Rostedt	    # Also disable anything that is not enabled in this config
40304c4ab120SSteven Rostedt	    my %configs;
40314c4ab120SSteven Rostedt	    assign_configs \%configs, $output_config;
40324c4ab120SSteven Rostedt	    my @config_keys = keys %min_configs;
40334c4ab120SSteven Rostedt	    foreach my $config (@config_keys) {
40344c4ab120SSteven Rostedt		if (!defined($configs{$config})) {
40354c4ab120SSteven Rostedt		    doprint "$config is not set, disabling\n";
40364c4ab120SSteven Rostedt		    delete $min_configs{$config};
40374c4ab120SSteven Rostedt		}
40384c4ab120SSteven Rostedt	    }
40394c4ab120SSteven Rostedt
404022722799SPavel Machek	    # Save off all the current mandatory configs
4041c043ccbfSJohn 'Warthog9' Hawley (VMware)	    open (OUT, ">$temp_config") or
4042c043ccbfSJohn 'Warthog9' Hawley (VMware)		dodie "Can't write to $temp_config";
40434c4ab120SSteven Rostedt	    foreach my $config (keys %keep_configs) {
40444c4ab120SSteven Rostedt		print OUT "$keep_configs{$config}\n";
40454c4ab120SSteven Rostedt	    }
40464c4ab120SSteven Rostedt	    foreach my $config (keys %min_configs) {
40474c4ab120SSteven Rostedt		print OUT "$min_configs{$config}\n";
40484c4ab120SSteven Rostedt	    }
40494c4ab120SSteven Rostedt	    close OUT;
405035ce5952SSteven Rostedt
405135ce5952SSteven Rostedt	    run_command "mv $temp_config $output_minconfig" or
405235ce5952SSteven Rostedt		dodie "failed to copy update to $output_minconfig";
40534c4ab120SSteven Rostedt	}
40544c4ab120SSteven Rostedt
40554c4ab120SSteven Rostedt	doprint "Reboot and wait $sleep_time seconds\n";
4056bc7c5803SSteven Rostedt	reboot_to_good $sleep_time;
40574c4ab120SSteven Rostedt    }
40584c4ab120SSteven Rostedt
40594c4ab120SSteven Rostedt    success $i;
40604c4ab120SSteven Rostedt    return 1;
40614c4ab120SSteven Rostedt}
40624c4ab120SSteven Rostedt
40634283b169SSteven Rostedt (Red Hat)sub make_warnings_file {
40644283b169SSteven Rostedt (Red Hat)    my ($i) = @_;
40654283b169SSteven Rostedt (Red Hat)
40664283b169SSteven Rostedt (Red Hat)    if (!defined($warnings_file)) {
40674283b169SSteven Rostedt (Red Hat)	dodie "Must define WARNINGS_FILE for make_warnings_file test";
40684283b169SSteven Rostedt (Red Hat)    }
40694283b169SSteven Rostedt (Red Hat)
40704283b169SSteven Rostedt (Red Hat)    if ($build_type eq "nobuild") {
40714283b169SSteven Rostedt (Red Hat)	dodie "BUILD_TYPE can not be 'nobuild' for make_warnings_file test";
40724283b169SSteven Rostedt (Red Hat)    }
40734283b169SSteven Rostedt (Red Hat)
40744283b169SSteven Rostedt (Red Hat)    build $build_type or dodie "Failed to build";
40754283b169SSteven Rostedt (Red Hat)
40764283b169SSteven Rostedt (Red Hat)    open(OUT, ">$warnings_file") or dodie "Can't create $warnings_file";
40774283b169SSteven Rostedt (Red Hat)
40784283b169SSteven Rostedt (Red Hat)    open(IN, $buildlog) or dodie "Can't open $buildlog";
40794283b169SSteven Rostedt (Red Hat)    while (<IN>) {
40804283b169SSteven Rostedt (Red Hat)	# Some compilers use UTF-8 extended for quotes
40814283b169SSteven Rostedt (Red Hat)	# for distcc heterogeneous systems, this causes issues
40824283b169SSteven Rostedt (Red Hat)	s/$utf8_quote/'/g;
40834283b169SSteven Rostedt (Red Hat)
40844283b169SSteven Rostedt (Red Hat)	if (/$check_build_re/) {
40854283b169SSteven Rostedt (Red Hat)	    print OUT;
40864283b169SSteven Rostedt (Red Hat)	}
40874283b169SSteven Rostedt (Red Hat)    }
40884283b169SSteven Rostedt (Red Hat)    close(IN);
40894283b169SSteven Rostedt (Red Hat)
40904283b169SSteven Rostedt (Red Hat)    close(OUT);
40914283b169SSteven Rostedt (Red Hat)
40924283b169SSteven Rostedt (Red Hat)    success $i;
40934283b169SSteven Rostedt (Red Hat)}
40944283b169SSteven Rostedt (Red Hat)
409522c37a9aSSteven Rostedt (Red Hat)sub option_defined {
409622c37a9aSSteven Rostedt (Red Hat)    my ($option) = @_;
409722c37a9aSSteven Rostedt (Red Hat)
409822c37a9aSSteven Rostedt (Red Hat)    if (defined($opt{$option}) && $opt{$option} !~ /^\s*$/) {
409922c37a9aSSteven Rostedt (Red Hat)	return 1;
410022c37a9aSSteven Rostedt (Red Hat)    }
410122c37a9aSSteven Rostedt (Red Hat)
410222c37a9aSSteven Rostedt (Red Hat)    return 0;
410322c37a9aSSteven Rostedt (Red Hat)}
410422c37a9aSSteven Rostedt (Red Hat)
41052a62512bSSteven Rostedtsub __set_test_option {
41065a391fbfSSteven Rostedt    my ($name, $i) = @_;
41075a391fbfSSteven Rostedt
41085a391fbfSSteven Rostedt    my $option = "$name\[$i\]";
41095a391fbfSSteven Rostedt
411022c37a9aSSteven Rostedt (Red Hat)    if (option_defined($option)) {
41115a391fbfSSteven Rostedt	return $opt{$option};
41125a391fbfSSteven Rostedt    }
41135a391fbfSSteven Rostedt
4114a57419b3SSteven Rostedt    foreach my $test (keys %repeat_tests) {
4115a57419b3SSteven Rostedt	if ($i >= $test &&
4116a57419b3SSteven Rostedt	    $i < $test + $repeat_tests{$test}) {
4117a57419b3SSteven Rostedt	    $option = "$name\[$test\]";
411822c37a9aSSteven Rostedt (Red Hat)	    if (option_defined($option)) {
4119a57419b3SSteven Rostedt		return $opt{$option};
4120a57419b3SSteven Rostedt	    }
4121a57419b3SSteven Rostedt	}
4122a57419b3SSteven Rostedt    }
4123a57419b3SSteven Rostedt
412422c37a9aSSteven Rostedt (Red Hat)    if (option_defined($name)) {
41255a391fbfSSteven Rostedt	return $opt{$name};
41265a391fbfSSteven Rostedt    }
41275a391fbfSSteven Rostedt
41285a391fbfSSteven Rostedt    return undef;
41295a391fbfSSteven Rostedt}
41305a391fbfSSteven Rostedt
41312a62512bSSteven Rostedtsub set_test_option {
41322a62512bSSteven Rostedt    my ($name, $i) = @_;
41332a62512bSSteven Rostedt
41342a62512bSSteven Rostedt    my $option = __set_test_option($name, $i);
41352a62512bSSteven Rostedt    return $option if (!defined($option));
41362a62512bSSteven Rostedt
413704262be3SSteven Rostedt (Red Hat)    return eval_option($name, $option, $i);
41382a62512bSSteven Rostedt}
41392a62512bSSteven Rostedt
4140be1546b8SSteven Rostedt (VMware)sub find_mailer {
4141be1546b8SSteven Rostedt (VMware)    my ($mailer) = @_;
4142be1546b8SSteven Rostedt (VMware)
4143be1546b8SSteven Rostedt (VMware)    my @paths = split /:/, $ENV{PATH};
4144be1546b8SSteven Rostedt (VMware)
4145be1546b8SSteven Rostedt (VMware)    # sendmail is usually in /usr/sbin
4146be1546b8SSteven Rostedt (VMware)    $paths[$#paths + 1] = "/usr/sbin";
4147be1546b8SSteven Rostedt (VMware)
4148be1546b8SSteven Rostedt (VMware)    foreach my $path (@paths) {
4149be1546b8SSteven Rostedt (VMware)	if (-x "$path/$mailer") {
4150be1546b8SSteven Rostedt (VMware)	    return $path;
4151be1546b8SSteven Rostedt (VMware)	}
4152be1546b8SSteven Rostedt (VMware)    }
4153be1546b8SSteven Rostedt (VMware)
4154be1546b8SSteven Rostedt (VMware)    return undef;
41552ceb2d85STim Tianyang Chen}
41562ceb2d85STim Tianyang Chen
4157c2d84ddbSSteven Rostedt (VMware)sub do_send_mail {
415834148b13SSteven Rostedt (VMware)    my ($subject, $message, $file) = @_;
4159c2d84ddbSSteven Rostedt (VMware)
4160be1546b8SSteven Rostedt (VMware)    if (!defined($mail_path)) {
4161be1546b8SSteven Rostedt (VMware)	# find the mailer
4162be1546b8SSteven Rostedt (VMware)	$mail_path = find_mailer $mailer;
4163be1546b8SSteven Rostedt (VMware)	if (!defined($mail_path)) {
4164be1546b8SSteven Rostedt (VMware)	    die "\nCan not find $mailer in PATH\n";
4165be1546b8SSteven Rostedt (VMware)	}
4166be1546b8SSteven Rostedt (VMware)    }
4167c2d84ddbSSteven Rostedt (VMware)
416834148b13SSteven Rostedt (VMware)    my $header_file = "$tmpdir/header";
416934148b13SSteven Rostedt (VMware)    open (HEAD, ">$header_file") or die "Can not create $header_file\n";
417034148b13SSteven Rostedt (VMware)    print HEAD "To: $mailto\n";
417134148b13SSteven Rostedt (VMware)    print HEAD "Subject: $subject\n\n";
417234148b13SSteven Rostedt (VMware)    print HEAD "$message\n";
417334148b13SSteven Rostedt (VMware)    close HEAD;
417434148b13SSteven Rostedt (VMware)
4175c2d84ddbSSteven Rostedt (VMware)    if (!defined($mail_command)) {
4176c2d84ddbSSteven Rostedt (VMware)	if ($mailer eq "mail" || $mailer eq "mailx") {
417734148b13SSteven Rostedt (VMware)	    $mail_command = "cat \$HEADER_FILE \$BODY_FILE | \$MAIL_PATH/\$MAILER -s \'\$SUBJECT\' \$MAILTO";
4178c2d84ddbSSteven Rostedt (VMware)	} elsif ($mailer eq "sendmail" ) {
417934148b13SSteven Rostedt (VMware)	    $mail_command =  "cat \$HEADER_FILE \$BODY_FILE | \$MAIL_PATH/\$MAILER -t \$MAILTO";
4180c2d84ddbSSteven Rostedt (VMware)	} else {
4181c2d84ddbSSteven Rostedt (VMware)	    die "\nYour mailer: $mailer is not supported.\n";
4182c2d84ddbSSteven Rostedt (VMware)	}
4183c2d84ddbSSteven Rostedt (VMware)    }
4184c2d84ddbSSteven Rostedt (VMware)
418534148b13SSteven Rostedt (VMware)    if (defined($file)) {
418634148b13SSteven Rostedt (VMware)	$mail_command =~ s/\$BODY_FILE/$file/g;
418734148b13SSteven Rostedt (VMware)    } else {
418834148b13SSteven Rostedt (VMware)	$mail_command =~ s/\$BODY_FILE//g;
418934148b13SSteven Rostedt (VMware)    }
419034148b13SSteven Rostedt (VMware)
419134148b13SSteven Rostedt (VMware)    $mail_command =~ s/\$HEADER_FILE/$header_file/g;
4192c2d84ddbSSteven Rostedt (VMware)    $mail_command =~ s/\$MAILER/$mailer/g;
4193c2d84ddbSSteven Rostedt (VMware)    $mail_command =~ s/\$MAIL_PATH/$mail_path/g;
4194c2d84ddbSSteven Rostedt (VMware)    $mail_command =~ s/\$MAILTO/$mailto/g;
4195c2d84ddbSSteven Rostedt (VMware)    $mail_command =~ s/\$SUBJECT/$subject/g;
4196c2d84ddbSSteven Rostedt (VMware)    $mail_command =~ s/\$MESSAGE/$message/g;
4197c2d84ddbSSteven Rostedt (VMware)
41988cd6bc03SSteven Rostedt (VMware)    my $ret = run_command $mail_command;
41998cd6bc03SSteven Rostedt (VMware)    if (!$ret && defined($file)) {
42008cd6bc03SSteven Rostedt (VMware)	# try again without the file
42018cd6bc03SSteven Rostedt (VMware)	$message .= "\n\n*** FAILED TO SEND LOG ***\n\n";
42028cd6bc03SSteven Rostedt (VMware)	do_send_email($subject, $message);
42038cd6bc03SSteven Rostedt (VMware)    }
4204c2d84ddbSSteven Rostedt (VMware)}
4205c2d84ddbSSteven Rostedt (VMware)
4206c2d84ddbSSteven Rostedt (VMware)sub send_email {
4207c2d84ddbSSteven Rostedt (VMware)    if (defined($mailto)) {
4208c2d84ddbSSteven Rostedt (VMware)	if (!defined($mailer)) {
4209c2d84ddbSSteven Rostedt (VMware)	    doprint "No email sent: email or mailer not specified in config.\n";
4210c2d84ddbSSteven Rostedt (VMware)	    return;
4211c2d84ddbSSteven Rostedt (VMware)	}
4212c2d84ddbSSteven Rostedt (VMware)	do_send_mail @_;
42132ceb2d85STim Tianyang Chen    }
42142ceb2d85STim Tianyang Chen}
42152ceb2d85STim Tianyang Chen
421692db453eSTim Tianyang Chensub cancel_test {
421783d29d43SSteven Rostedt    if ($monitor_cnt) {
421883d29d43SSteven Rostedt	end_monitor;
421983d29d43SSteven Rostedt    }
422092db453eSTim Tianyang Chen    if ($email_when_canceled) {
4221fca797f1SSteven Rostedt (VMware)	my $name = get_test_name;
4222fca797f1SSteven Rostedt (VMware)	send_email("KTEST: Your [$name] test was cancelled",
422392db453eSTim Tianyang Chen	    "Your test started at $script_start_time was cancelled: sig int");
422492db453eSTim Tianyang Chen    }
422592db453eSTim Tianyang Chen    die "\nCaught Sig Int, test interrupted: $!\n"
422692db453eSTim Tianyang Chen}
422792db453eSTim Tianyang Chen
42286a0f3652SJohn 'Warthog9' Hawley (VMware)$#ARGV < 1 or die "ktest.pl version: $VERSION\n   usage: ktest.pl [config-file]\n";
42296a0f3652SJohn 'Warthog9' Hawley (VMware)
42306a0f3652SJohn 'Warthog9' Hawley (VMware)if ($#ARGV == 0) {
42316a0f3652SJohn 'Warthog9' Hawley (VMware)    $ktest_config = $ARGV[0];
42326a0f3652SJohn 'Warthog9' Hawley (VMware)    if (! -f $ktest_config) {
42336a0f3652SJohn 'Warthog9' Hawley (VMware)	print "$ktest_config does not exist.\n";
42346a0f3652SJohn 'Warthog9' Hawley (VMware)	if (!read_yn "Create it?") {
42356a0f3652SJohn 'Warthog9' Hawley (VMware)	    exit 0;
42366a0f3652SJohn 'Warthog9' Hawley (VMware)	}
42376a0f3652SJohn 'Warthog9' Hawley (VMware)    }
42386a0f3652SJohn 'Warthog9' Hawley (VMware)}
42396a0f3652SJohn 'Warthog9' Hawley (VMware)
42406a0f3652SJohn 'Warthog9' Hawley (VMware)if (! -f $ktest_config) {
42416a0f3652SJohn 'Warthog9' Hawley (VMware)    $newconfig = 1;
42426a0f3652SJohn 'Warthog9' Hawley (VMware)    get_test_case;
42436a0f3652SJohn 'Warthog9' Hawley (VMware)    open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
42446a0f3652SJohn 'Warthog9' Hawley (VMware)    print OUT << "EOF"
42456a0f3652SJohn 'Warthog9' Hawley (VMware)# Generated by ktest.pl
42466a0f3652SJohn 'Warthog9' Hawley (VMware)#
42476a0f3652SJohn 'Warthog9' Hawley (VMware)
42486a0f3652SJohn 'Warthog9' Hawley (VMware)# PWD is a ktest.pl variable that will result in the process working
42496a0f3652SJohn 'Warthog9' Hawley (VMware)# directory that ktest.pl is executed in.
42506a0f3652SJohn 'Warthog9' Hawley (VMware)
42516a0f3652SJohn 'Warthog9' Hawley (VMware)# THIS_DIR is automatically assigned the PWD of the path that generated
42526a0f3652SJohn 'Warthog9' Hawley (VMware)# the config file. It is best to use this variable when assigning other
42536a0f3652SJohn 'Warthog9' Hawley (VMware)# directory paths within this directory. This allows you to easily
42546a0f3652SJohn 'Warthog9' Hawley (VMware)# move the test cases to other locations or to other machines.
42556a0f3652SJohn 'Warthog9' Hawley (VMware)#
42566a0f3652SJohn 'Warthog9' Hawley (VMware)THIS_DIR := $variable{"PWD"}
42576a0f3652SJohn 'Warthog9' Hawley (VMware)
42586a0f3652SJohn 'Warthog9' Hawley (VMware)# Define each test with TEST_START
42596a0f3652SJohn 'Warthog9' Hawley (VMware)# The config options below it will override the defaults
42606a0f3652SJohn 'Warthog9' Hawley (VMware)TEST_START
42616a0f3652SJohn 'Warthog9' Hawley (VMware)TEST_TYPE = $default{"TEST_TYPE"}
42626a0f3652SJohn 'Warthog9' Hawley (VMware)
42636a0f3652SJohn 'Warthog9' Hawley (VMware)DEFAULTS
42646a0f3652SJohn 'Warthog9' Hawley (VMware)EOF
42656a0f3652SJohn 'Warthog9' Hawley (VMware);
42666a0f3652SJohn 'Warthog9' Hawley (VMware)    close(OUT);
42676a0f3652SJohn 'Warthog9' Hawley (VMware)}
42686a0f3652SJohn 'Warthog9' Hawley (VMware)read_config $ktest_config;
42696a0f3652SJohn 'Warthog9' Hawley (VMware)
42706a0f3652SJohn 'Warthog9' Hawley (VMware)if (defined($opt{"LOG_FILE"})) {
42716a0f3652SJohn 'Warthog9' Hawley (VMware)    $opt{"LOG_FILE"} = eval_option("LOG_FILE", $opt{"LOG_FILE"}, -1);
42726a0f3652SJohn 'Warthog9' Hawley (VMware)}
42736a0f3652SJohn 'Warthog9' Hawley (VMware)
42746a0f3652SJohn 'Warthog9' Hawley (VMware)# Append any configs entered in manually to the config file.
42756a0f3652SJohn 'Warthog9' Hawley (VMware)my @new_configs = keys %entered_configs;
42766a0f3652SJohn 'Warthog9' Hawley (VMware)if ($#new_configs >= 0) {
42776a0f3652SJohn 'Warthog9' Hawley (VMware)    print "\nAppending entered in configs to $ktest_config\n";
42786a0f3652SJohn 'Warthog9' Hawley (VMware)    open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
42796a0f3652SJohn 'Warthog9' Hawley (VMware)    foreach my $config (@new_configs) {
42806a0f3652SJohn 'Warthog9' Hawley (VMware)	print OUT "$config = $entered_configs{$config}\n";
42816a0f3652SJohn 'Warthog9' Hawley (VMware)	$opt{$config} = process_variables($entered_configs{$config});
42826a0f3652SJohn 'Warthog9' Hawley (VMware)    }
42836a0f3652SJohn 'Warthog9' Hawley (VMware)}
42846a0f3652SJohn 'Warthog9' Hawley (VMware)
42856a0f3652SJohn 'Warthog9' Hawley (VMware)if (defined($opt{"LOG_FILE"})) {
42866a0f3652SJohn 'Warthog9' Hawley (VMware)    if ($opt{"CLEAR_LOG"}) {
42876a0f3652SJohn 'Warthog9' Hawley (VMware)	unlink $opt{"LOG_FILE"};
42886a0f3652SJohn 'Warthog9' Hawley (VMware)    }
42896a0f3652SJohn 'Warthog9' Hawley (VMware)    open(LOG, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
42906a0f3652SJohn 'Warthog9' Hawley (VMware)    LOG->autoflush(1);
42916a0f3652SJohn 'Warthog9' Hawley (VMware)}
42926a0f3652SJohn 'Warthog9' Hawley (VMware)
42936a0f3652SJohn 'Warthog9' Hawley (VMware)doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
42946a0f3652SJohn 'Warthog9' Hawley (VMware)
42956a0f3652SJohn 'Warthog9' Hawley (VMware)for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
42966a0f3652SJohn 'Warthog9' Hawley (VMware)
42976a0f3652SJohn 'Warthog9' Hawley (VMware)    if (!$i) {
42986a0f3652SJohn 'Warthog9' Hawley (VMware)	doprint "DEFAULT OPTIONS:\n";
42996a0f3652SJohn 'Warthog9' Hawley (VMware)    } else {
43006a0f3652SJohn 'Warthog9' Hawley (VMware)	doprint "\nTEST $i OPTIONS";
43016a0f3652SJohn 'Warthog9' Hawley (VMware)	if (defined($repeat_tests{$i})) {
43026a0f3652SJohn 'Warthog9' Hawley (VMware)	    $repeat = $repeat_tests{$i};
43036a0f3652SJohn 'Warthog9' Hawley (VMware)	    doprint " ITERATE $repeat";
43046a0f3652SJohn 'Warthog9' Hawley (VMware)	}
43056a0f3652SJohn 'Warthog9' Hawley (VMware)	doprint "\n";
43066a0f3652SJohn 'Warthog9' Hawley (VMware)    }
43076a0f3652SJohn 'Warthog9' Hawley (VMware)
43086a0f3652SJohn 'Warthog9' Hawley (VMware)    foreach my $option (sort keys %opt) {
43096a0f3652SJohn 'Warthog9' Hawley (VMware)	if ($option =~ /\[(\d+)\]$/) {
43106a0f3652SJohn 'Warthog9' Hawley (VMware)	    next if ($i != $1);
43116a0f3652SJohn 'Warthog9' Hawley (VMware)	} else {
43126a0f3652SJohn 'Warthog9' Hawley (VMware)	    next if ($i);
43136a0f3652SJohn 'Warthog9' Hawley (VMware)	}
43146a0f3652SJohn 'Warthog9' Hawley (VMware)
43156a0f3652SJohn 'Warthog9' Hawley (VMware)	doprint "$option = $opt{$option}\n";
43166a0f3652SJohn 'Warthog9' Hawley (VMware)    }
43176a0f3652SJohn 'Warthog9' Hawley (VMware)}
43186a0f3652SJohn 'Warthog9' Hawley (VMware)
431992db453eSTim Tianyang Chen$SIG{INT} = qw(cancel_test);
432092db453eSTim Tianyang Chen
43212545eb61SSteven Rostedt# First we need to do is the builds
4322a75fececSSteven Rostedtfor (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
43232545eb61SSteven Rostedt
43244ab1cce5SSteven Rostedt    # Do not reboot on failing test options
43254ab1cce5SSteven Rostedt    $no_reboot = 1;
4326759a3cc6SSteven Rostedt    $reboot_success = 0;
43274ab1cce5SSteven Rostedt
4328683a3e64SSteven Rostedt    $have_version = 0;
4329683a3e64SSteven Rostedt
4330576f627cSSteven Rostedt    $iteration = $i;
4331576f627cSSteven Rostedt
433238fa3dc1SSteven Rostedt (Red Hat)    $build_time = 0;
433338fa3dc1SSteven Rostedt (Red Hat)    $install_time = 0;
433438fa3dc1SSteven Rostedt (Red Hat)    $reboot_time = 0;
433538fa3dc1SSteven Rostedt (Red Hat)    $test_time = 0;
433638fa3dc1SSteven Rostedt (Red Hat)
4337c1434dccSSteven Rostedt    undef %force_config;
4338c1434dccSSteven Rostedt
4339a75fececSSteven Rostedt    my $makecmd = set_test_option("MAKE_CMD", $i);
4340a75fececSSteven Rostedt
43418e80bf05SSteven Rostedt (Red Hat)    $outputdir = set_test_option("OUTPUT_DIR", $i);
43428e80bf05SSteven Rostedt (Red Hat)    $builddir = set_test_option("BUILD_DIR", $i);
43438e80bf05SSteven Rostedt (Red Hat)
4344df46fce6STim Tianyang Chen    chdir $builddir || dodie "can't change directory to $builddir";
43458e80bf05SSteven Rostedt (Red Hat)
43468e80bf05SSteven Rostedt (Red Hat)    if (!-d $outputdir) {
43478e80bf05SSteven Rostedt (Red Hat)	mkpath($outputdir) or
4348df46fce6STim Tianyang Chen	    dodie "can't create $outputdir";
43498e80bf05SSteven Rostedt (Red Hat)    }
43508e80bf05SSteven Rostedt (Red Hat)
43518e80bf05SSteven Rostedt (Red Hat)    $make = "$makecmd O=$outputdir";
43528e80bf05SSteven Rostedt (Red Hat)
43539cc9e091SSteven Rostedt    # Load all the options into their mapped variable names
43549cc9e091SSteven Rostedt    foreach my $opt (keys %option_map) {
43559cc9e091SSteven Rostedt	${$option_map{$opt}} = set_test_option($opt, $i);
43569cc9e091SSteven Rostedt    }
4357b5f4aea6SSteven Rostedt
435835ce5952SSteven Rostedt    $start_minconfig_defined = 1;
435935ce5952SSteven Rostedt
4360921ed4c7SSteven Rostedt    # The first test may override the PRE_KTEST option
43612ceb2d85STim Tianyang Chen    if ($i == 1) {
43622ceb2d85STim Tianyang Chen	if (defined($pre_ktest)) {
4363921ed4c7SSteven Rostedt	    doprint "\n";
4364921ed4c7SSteven Rostedt	    run_command $pre_ktest;
4365921ed4c7SSteven Rostedt	}
43662ceb2d85STim Tianyang Chen	if ($email_when_started) {
4367fca797f1SSteven Rostedt (VMware)	    my $name = get_test_name;
4368fca797f1SSteven Rostedt (VMware)	    send_email("KTEST: Your [$name] test was started",
43692ceb2d85STim Tianyang Chen		"Your test was started on $script_start_time");
43702ceb2d85STim Tianyang Chen	}
43712ceb2d85STim Tianyang Chen    }
4372921ed4c7SSteven Rostedt
4373921ed4c7SSteven Rostedt    # Any test can override the POST_KTEST option
4374921ed4c7SSteven Rostedt    # The last test takes precedence.
4375921ed4c7SSteven Rostedt    if (defined($post_ktest)) {
4376921ed4c7SSteven Rostedt	$final_post_ktest = $post_ktest;
4377921ed4c7SSteven Rostedt    }
4378921ed4c7SSteven Rostedt
43794c4ab120SSteven Rostedt    if (!defined($start_minconfig)) {
438035ce5952SSteven Rostedt	$start_minconfig_defined = 0;
43814c4ab120SSteven Rostedt	$start_minconfig = $minconfig;
43824c4ab120SSteven Rostedt    }
43834c4ab120SSteven Rostedt
43848e80bf05SSteven Rostedt (Red Hat)    if (!-d $tmpdir) {
43858e80bf05SSteven Rostedt (Red Hat)	mkpath($tmpdir) or
4386df46fce6STim Tianyang Chen	    dodie "can't create $tmpdir";
4387a75fececSSteven Rostedt    }
4388a75fececSSteven Rostedt
4389e48c5293SSteven Rostedt    $ENV{"SSH_USER"} = $ssh_user;
4390e48c5293SSteven Rostedt    $ENV{"MACHINE"} = $machine;
4391e48c5293SSteven Rostedt
4392a75fececSSteven Rostedt    $buildlog = "$tmpdir/buildlog-$machine";
4393a9dd5d63SRabin Vincent    $testlog = "$tmpdir/testlog-$machine";
4394a75fececSSteven Rostedt    $dmesg = "$tmpdir/dmesg-$machine";
439551ad1dd1SSteven Rostedt    $output_config = "$outputdir/.config";
4396a75fececSSteven Rostedt
4397bb8474b1SSteven Rostedt    if (!$buildonly) {
4398bb8474b1SSteven Rostedt	$target = "$ssh_user\@$machine";
4399ac246645SMasayoshi Mizuma	if (($reboot_type eq "grub") or ($reboot_type eq "grub2bls")) {
4400576f627cSSteven Rostedt	    dodie "GRUB_MENU not defined" if (!defined($grub_menu));
4401a15ba913SSteven Rostedt	} elsif ($reboot_type eq "grub2") {
4402a15ba913SSteven Rostedt	    dodie "GRUB_MENU not defined" if (!defined($grub_menu));
4403a15ba913SSteven Rostedt	    dodie "GRUB_FILE not defined" if (!defined($grub_file));
44047786954cSSteven Rostedt	} elsif ($reboot_type eq "syslinux") {
44057786954cSSteven Rostedt	    dodie "SYSLINUX_LABEL not defined" if (!defined($syslinux_label));
4406a75fececSSteven Rostedt	}
4407bb8474b1SSteven Rostedt    }
4408a75fececSSteven Rostedt
4409a75fececSSteven Rostedt    my $run_type = $build_type;
4410a75fececSSteven Rostedt    if ($test_type eq "patchcheck") {
4411b5f4aea6SSteven Rostedt	$run_type = $patchcheck_type;
4412a75fececSSteven Rostedt    } elsif ($test_type eq "bisect") {
4413b5f4aea6SSteven Rostedt	$run_type = $bisect_type;
44140a05c769SSteven Rostedt    } elsif ($test_type eq "config_bisect") {
4415b5f4aea6SSteven Rostedt	$run_type = $config_bisect_type;
44164283b169SSteven Rostedt (Red Hat)    } elsif ($test_type eq "make_min_config") {
44174283b169SSteven Rostedt (Red Hat)	$run_type = "";
44184283b169SSteven Rostedt (Red Hat)    } elsif ($test_type eq "make_warnings_file") {
44194c4ab120SSteven Rostedt	$run_type = "";
44204c4ab120SSteven Rostedt    }
44214c4ab120SSteven Rostedt
4422a75fececSSteven Rostedt    # mistake in config file?
4423a75fececSSteven Rostedt    if (!defined($run_type)) {
4424a75fececSSteven Rostedt	$run_type = "ERROR";
4425a75fececSSteven Rostedt    }
44262545eb61SSteven Rostedt
4427e0a8742eSSteven Rostedt    my $installme = "";
4428e0a8742eSSteven Rostedt    $installme = " no_install" if ($no_install);
4429e0a8742eSSteven Rostedt
443018656c70SSteven Rostedt (Red Hat)    my $name = "";
443118656c70SSteven Rostedt (Red Hat)
443218656c70SSteven Rostedt (Red Hat)    if (defined($test_name)) {
443318656c70SSteven Rostedt (Red Hat)	$name = " ($test_name)";
443418656c70SSteven Rostedt (Red Hat)    }
443518656c70SSteven Rostedt (Red Hat)
44362545eb61SSteven Rostedt    doprint "\n\n";
443734148b13SSteven Rostedt (VMware)
443834148b13SSteven Rostedt (VMware)    if (defined($opt{"LOG_FILE"})) {
443934148b13SSteven Rostedt (VMware)	$test_log_start = tell(LOG);
444034148b13SSteven Rostedt (VMware)    }
444134148b13SSteven Rostedt (VMware)
444218656c70SSteven Rostedt (Red Hat)    doprint "RUNNING TEST $i of $opt{NUM_TESTS}$name with option $test_type $run_type$installme\n\n";
44437faafbd6SSteven Rostedt
4444921ed4c7SSteven Rostedt    if (defined($pre_test)) {
4445d53cdda3SSteven Rostedt (VMware)	my $ret = run_command $pre_test;
4446d53cdda3SSteven Rostedt (VMware)	if (!$ret && defined($pre_test_die) &&
4447d53cdda3SSteven Rostedt (VMware)	    $pre_test_die) {
4448d53cdda3SSteven Rostedt (VMware)		dodie "failed to pre_test\n";
4449d53cdda3SSteven Rostedt (VMware)	}
4450921ed4c7SSteven Rostedt    }
4451921ed4c7SSteven Rostedt
44527faafbd6SSteven Rostedt    unlink $dmesg;
44537faafbd6SSteven Rostedt    unlink $buildlog;
4454a9dd5d63SRabin Vincent    unlink $testlog;
44552545eb61SSteven Rostedt
4456250bae8bSSteven Rostedt    if (defined($addconfig)) {
4457250bae8bSSteven Rostedt	my $min = $minconfig;
44582b7d9b21SSteven Rostedt	if (!defined($minconfig)) {
4459250bae8bSSteven Rostedt	    $min = "";
4460250bae8bSSteven Rostedt	}
4461250bae8bSSteven Rostedt	run_command "cat $addconfig $min > $tmpdir/add_config" or
44622b7d9b21SSteven Rostedt	    dodie "Failed to create temp config";
44639be2e6b5SSteven Rostedt	$minconfig = "$tmpdir/add_config";
44642b7d9b21SSteven Rostedt    }
44652b7d9b21SSteven Rostedt
44666c5ee0beSSteven Rostedt    if (defined($checkout)) {
44676c5ee0beSSteven Rostedt	run_command "git checkout $checkout" or
4468df46fce6STim Tianyang Chen	    dodie "failed to checkout $checkout";
44696c5ee0beSSteven Rostedt    }
44706c5ee0beSSteven Rostedt
4471759a3cc6SSteven Rostedt    $no_reboot = 0;
4472759a3cc6SSteven Rostedt
4473648a182cSSteven Rostedt    # A test may opt to not reboot the box
4474648a182cSSteven Rostedt    if ($reboot_on_success) {
4475759a3cc6SSteven Rostedt	$reboot_success = 1;
4476648a182cSSteven Rostedt    }
44774ab1cce5SSteven Rostedt
4478a75fececSSteven Rostedt    if ($test_type eq "bisect") {
44795f9b6cedSSteven Rostedt	bisect $i;
44805f9b6cedSSteven Rostedt	next;
44810a05c769SSteven Rostedt    } elsif ($test_type eq "config_bisect") {
44820a05c769SSteven Rostedt	config_bisect $i;
44830a05c769SSteven Rostedt	next;
4484a75fececSSteven Rostedt    } elsif ($test_type eq "patchcheck") {
44856c5ee0beSSteven Rostedt	patchcheck $i;
44866c5ee0beSSteven Rostedt	next;
44874c4ab120SSteven Rostedt    } elsif ($test_type eq "make_min_config") {
44884c4ab120SSteven Rostedt	make_min_config $i;
44894c4ab120SSteven Rostedt	next;
44904283b169SSteven Rostedt (Red Hat)    } elsif ($test_type eq "make_warnings_file") {
44914283b169SSteven Rostedt (Red Hat)	$no_reboot = 1;
44924283b169SSteven Rostedt (Red Hat)	make_warnings_file $i;
44934283b169SSteven Rostedt (Red Hat)	next;
44945f9b6cedSSteven Rostedt    }
44955f9b6cedSSteven Rostedt
44967faafbd6SSteven Rostedt    if ($build_type ne "nobuild") {
44977faafbd6SSteven Rostedt	build $build_type or next;
44984283b169SSteven Rostedt (Red Hat)	check_buildlog or next;
44992545eb61SSteven Rostedt    }
45002545eb61SSteven Rostedt
4501cd8e368fSSteven Rostedt    if ($test_type eq "install") {
4502cd8e368fSSteven Rostedt	get_version;
4503cd8e368fSSteven Rostedt	install;
4504cd8e368fSSteven Rostedt	success $i;
4505cd8e368fSSteven Rostedt	next;
4506cd8e368fSSteven Rostedt    }
4507cd8e368fSSteven Rostedt
4508a75fececSSteven Rostedt    if ($test_type ne "build") {
45097faafbd6SSteven Rostedt	my $failed = 0;
451064d98283SSteven Rostedt (Red Hat)	start_monitor_and_install or $failed = 1;
4511a75fececSSteven Rostedt
4512a75fececSSteven Rostedt	if (!$failed && $test_type ne "boot" && defined($run_test)) {
45137faafbd6SSteven Rostedt	    do_run_test or $failed = 1;
45145a391fbfSSteven Rostedt	}
45157faafbd6SSteven Rostedt	end_monitor;
451638fa3dc1SSteven Rostedt (Red Hat)	if ($failed) {
451738fa3dc1SSteven Rostedt (Red Hat)	    print_times;
451838fa3dc1SSteven Rostedt (Red Hat)	    next;
4519a75fececSSteven Rostedt	}
452038fa3dc1SSteven Rostedt (Red Hat)    }
452138fa3dc1SSteven Rostedt (Red Hat)
452238fa3dc1SSteven Rostedt (Red Hat)    print_times;
45235a391fbfSSteven Rostedt
45245f9b6cedSSteven Rostedt    success $i;
452575c3fda7SSteven Rostedt}
45262545eb61SSteven Rostedt
4527921ed4c7SSteven Rostedtif (defined($final_post_ktest)) {
4528cc2eb3a2SMasayoshi Mizuma
4529cc2eb3a2SMasayoshi Mizuma    my $cp_final_post_ktest = eval_kernel_version $final_post_ktest;
4530cc2eb3a2SMasayoshi Mizuma    run_command $cp_final_post_ktest;
4531921ed4c7SSteven Rostedt}
4532921ed4c7SSteven Rostedt
45335c42fc5bSSteven Rostedtif ($opt{"POWEROFF_ON_SUCCESS"}) {
453475c3fda7SSteven Rostedt    halt;
4535759a3cc6SSteven Rostedt} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
4536bc7c5803SSteven Rostedt    reboot_to_good;
4537648a182cSSteven Rostedt} elsif (defined($switch_to_good)) {
4538648a182cSSteven Rostedt    # still need to get to the good kernel
4539648a182cSSteven Rostedt    run_command $switch_to_good;
45405c42fc5bSSteven Rostedt}
454175c3fda7SSteven Rostedt
4542e48c5293SSteven Rostedtdoprint "\n    $successes of $opt{NUM_TESTS} tests were successful\n\n";
4543e48c5293SSteven Rostedt
45442ceb2d85STim Tianyang Chenif ($email_when_finished) {
4545fca797f1SSteven Rostedt (VMware)    send_email("KTEST: Your test has finished!",
45462ceb2d85STim Tianyang Chen	"$successes of $opt{NUM_TESTS} tests started at $script_start_time were successful!");
45472ceb2d85STim Tianyang Chen}
4548d6bc29d9SSteven Rostedt (VMware)
4549d6bc29d9SSteven Rostedt (VMware)if (defined($opt{"LOG_FILE"})) {
4550d6bc29d9SSteven Rostedt (VMware)    print "\n See $opt{LOG_FILE} for the record of results.\n\n";
4551d6bc29d9SSteven Rostedt (VMware)    close LOG;
4552d6bc29d9SSteven Rostedt (VMware)}
4553d6bc29d9SSteven Rostedt (VMware)
45542545eb61SSteven Rostedtexit 0;
4555becdd17bSJohn 'Warthog9' Hawley (VMware)
4556becdd17bSJohn 'Warthog9' Hawley (VMware)##
4557becdd17bSJohn 'Warthog9' Hawley (VMware)# The following are here to standardize tabs/spaces/etc across the most likely editors
4558becdd17bSJohn 'Warthog9' Hawley (VMware)###
4559becdd17bSJohn 'Warthog9' Hawley (VMware)
4560becdd17bSJohn 'Warthog9' Hawley (VMware)# Local Variables:
4561becdd17bSJohn 'Warthog9' Hawley (VMware)# mode: perl
4562becdd17bSJohn 'Warthog9' Hawley (VMware)# End:
4563becdd17bSJohn 'Warthog9' Hawley (VMware)# vim: softtabstop=4
4564