ktest: Add INCLUDE keyword to include other config files
[deliverable/linux.git] / tools / testing / ktest / ktest.pl
CommitLineData
2545eb61 1#!/usr/bin/perl -w
d6ce2a0b 2#
cce1dac8 3# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
d6ce2a0b
SR
4# Licensed under the terms of the GNU GPL License version 2
5#
2545eb61
SR
6
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
7faafbd6
SR
10use File::Path qw(mkpath);
11use File::Copy qw(cp);
2545eb61
SR
12use FileHandle;
13
e48c5293
SR
14my $VERSION = "0.2";
15
2545eb61
SR
16$| = 1;
17
18my %opt;
a57419b3
SR
19my %repeat_tests;
20my %repeats;
a75fecec 21my %default;
2545eb61
SR
22
23#default opts
a57419b3 24$default{"NUM_TESTS"} = 1;
a75fecec
SR
25$default{"REBOOT_TYPE"} = "grub";
26$default{"TEST_TYPE"} = "test";
27$default{"BUILD_TYPE"} = "randconfig";
28$default{"MAKE_CMD"} = "make";
29$default{"TIMEOUT"} = 120;
48920630 30$default{"TMP_DIR"} = "/tmp/ktest/\${MACHINE}";
a75fecec
SR
31$default{"SLEEP_TIME"} = 60; # sleep time between tests
32$default{"BUILD_NOCLEAN"} = 0;
33$default{"REBOOT_ON_ERROR"} = 0;
34$default{"POWEROFF_ON_ERROR"} = 0;
35$default{"REBOOT_ON_SUCCESS"} = 1;
36$default{"POWEROFF_ON_SUCCESS"} = 0;
37$default{"BUILD_OPTIONS"} = "";
38$default{"BISECT_SLEEP_TIME"} = 60; # sleep time between bisects
27d934b2 39$default{"PATCHCHECK_SLEEP_TIME"} = 60; # sleep time between patch checks
a75fecec 40$default{"CLEAR_LOG"} = 0;
c960bb9f 41$default{"BISECT_MANUAL"} = 0;
c23dca7c 42$default{"BISECT_SKIP"} = 1;
a75fecec 43$default{"SUCCESS_LINE"} = "login:";
f1a5b962 44$default{"DETECT_TRIPLE_FAULT"} = 1;
e0a8742e 45$default{"NO_INSTALL"} = 0;
a75fecec
SR
46$default{"BOOTED_TIMEOUT"} = 1;
47$default{"DIE_ON_FAILURE"} = 1;
e48c5293
SR
48$default{"SSH_EXEC"} = "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND";
49$default{"SCP_TO_TARGET"} = "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE";
50$default{"REBOOT"} = "ssh \$SSH_USER\@\$MACHINE reboot";
1c8a617a
SR
51$default{"STOP_AFTER_SUCCESS"} = 10;
52$default{"STOP_AFTER_FAILURE"} = 60;
2d01b26a 53$default{"STOP_TEST_AFTER"} = 600;
8d1491ba 54$default{"LOCALVERSION"} = "-test";
2545eb61 55
8d1491ba 56my $ktest_config;
2545eb61 57my $version;
a75fecec 58my $machine;
e48c5293 59my $ssh_user;
a75fecec
SR
60my $tmpdir;
61my $builddir;
62my $outputdir;
51ad1dd1 63my $output_config;
a75fecec 64my $test_type;
7faafbd6 65my $build_type;
a75fecec 66my $build_options;
0bd6c1a3
SR
67my $pre_build;
68my $post_build;
69my $pre_build_die;
70my $post_build_die;
a75fecec
SR
71my $reboot_type;
72my $reboot_script;
73my $power_cycle;
e48c5293 74my $reboot;
a75fecec
SR
75my $reboot_on_error;
76my $poweroff_on_error;
77my $die_on_failure;
576f627c
SR
78my $powercycle_after_reboot;
79my $poweroff_after_halt;
e48c5293
SR
80my $ssh_exec;
81my $scp_to_target;
a75fecec
SR
82my $power_off;
83my $grub_menu;
2545eb61
SR
84my $grub_number;
85my $target;
86my $make;
8b37ca8c 87my $post_install;
e0a8742e 88my $no_install;
5c42fc5b 89my $noclean;
5f9b6ced 90my $minconfig;
4c4ab120 91my $start_minconfig;
35ce5952 92my $start_minconfig_defined;
4c4ab120
SR
93my $output_minconfig;
94my $ignore_config;
2b7d9b21 95my $addconfig;
5f9b6ced
SR
96my $in_bisect = 0;
97my $bisect_bad = "";
d6ce2a0b 98my $reverse_bisect;
c960bb9f 99my $bisect_manual;
c23dca7c 100my $bisect_skip;
30f75da5 101my $config_bisect_good;
6c5ee0be 102my $in_patchcheck = 0;
5a391fbf 103my $run_test;
6c5ee0be 104my $redirect;
7faafbd6
SR
105my $buildlog;
106my $dmesg;
107my $monitor_fp;
108my $monitor_pid;
109my $monitor_cnt = 0;
a75fecec
SR
110my $sleep_time;
111my $bisect_sleep_time;
27d934b2 112my $patchcheck_sleep_time;
1990207d 113my $ignore_warnings;
a75fecec 114my $store_failures;
9064af52 115my $test_name;
a75fecec
SR
116my $timeout;
117my $booted_timeout;
f1a5b962 118my $detect_triplefault;
a75fecec 119my $console;
2b803365 120my $reboot_success_line;
a75fecec 121my $success_line;
1c8a617a
SR
122my $stop_after_success;
123my $stop_after_failure;
2d01b26a 124my $stop_test_after;
a75fecec
SR
125my $build_target;
126my $target_image;
127my $localversion;
576f627c 128my $iteration = 0;
e48c5293 129my $successes = 0;
2545eb61 130
8d1491ba
SR
131my %entered_configs;
132my %config_help;
77d942ce 133my %variable;
fcb3f16a 134my %force_config;
8d1491ba 135
4ab1cce5
SR
136# do not force reboots on config problems
137my $no_reboot = 1;
138
8d1491ba
SR
139$config_help{"MACHINE"} = << "EOF"
140 The machine hostname that you will test.
141EOF
142 ;
143$config_help{"SSH_USER"} = << "EOF"
144 The box is expected to have ssh on normal bootup, provide the user
145 (most likely root, since you need privileged operations)
146EOF
147 ;
148$config_help{"BUILD_DIR"} = << "EOF"
149 The directory that contains the Linux source code (full path).
150EOF
151 ;
152$config_help{"OUTPUT_DIR"} = << "EOF"
153 The directory that the objects will be built (full path).
154 (can not be same as BUILD_DIR)
155EOF
156 ;
157$config_help{"BUILD_TARGET"} = << "EOF"
158 The location of the compiled file to copy to the target.
159 (relative to OUTPUT_DIR)
160EOF
161 ;
162$config_help{"TARGET_IMAGE"} = << "EOF"
163 The place to put your image on the test machine.
164EOF
165 ;
166$config_help{"POWER_CYCLE"} = << "EOF"
167 A script or command to reboot the box.
168
169 Here is a digital loggers power switch example
170 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
171
172 Here is an example to reboot a virtual box on the current host
173 with the name "Guest".
174 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
175EOF
176 ;
177$config_help{"CONSOLE"} = << "EOF"
178 The script or command that reads the console
179
180 If you use ttywatch server, something like the following would work.
181CONSOLE = nc -d localhost 3001
182
183 For a virtual machine with guest name "Guest".
184CONSOLE = virsh console Guest
185EOF
186 ;
187$config_help{"LOCALVERSION"} = << "EOF"
188 Required version ending to differentiate the test
189 from other linux builds on the system.
190EOF
191 ;
192$config_help{"REBOOT_TYPE"} = << "EOF"
193 Way to reboot the box to the test kernel.
194 Only valid options so far are "grub" and "script".
195
196 If you specify grub, it will assume grub version 1
197 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
198 and select that target to reboot to the kernel. If this is not
199 your setup, then specify "script" and have a command or script
200 specified in REBOOT_SCRIPT to boot to the target.
201
202 The entry in /boot/grub/menu.lst must be entered in manually.
203 The test will not modify that file.
204EOF
205 ;
206$config_help{"GRUB_MENU"} = << "EOF"
207 The grub title name for the test kernel to boot
208 (Only mandatory if REBOOT_TYPE = grub)
209
210 Note, ktest.pl will not update the grub menu.lst, you need to
211 manually add an option for the test. ktest.pl will search
212 the grub menu.lst for this option to find what kernel to
213 reboot into.
214
215 For example, if in the /boot/grub/menu.lst the test kernel title has:
216 title Test Kernel
217 kernel vmlinuz-test
218 GRUB_MENU = Test Kernel
219EOF
220 ;
221$config_help{"REBOOT_SCRIPT"} = << "EOF"
222 A script to reboot the target into the test kernel
223 (Only mandatory if REBOOT_TYPE = script)
224EOF
225 ;
226
35ce5952
SR
227sub read_yn {
228 my ($prompt) = @_;
229
230 my $ans;
231
232 for (;;) {
233 print "$prompt [Y/n] ";
234 $ans = <STDIN>;
235 chomp $ans;
236 if ($ans =~ /^\s*$/) {
237 $ans = "y";
238 }
239 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
240 print "Please answer either 'y' or 'n'.\n";
241 }
242 if ($ans !~ /^y$/i) {
243 return 0;
244 }
245 return 1;
246}
8d1491ba
SR
247
248sub get_ktest_config {
249 my ($config) = @_;
250
251 return if (defined($opt{$config}));
252
253 if (defined($config_help{$config})) {
254 print "\n";
255 print $config_help{$config};
256 }
257
258 for (;;) {
259 print "$config = ";
260 if (defined($default{$config})) {
261 print "\[$default{$config}\] ";
262 }
263 $entered_configs{$config} = <STDIN>;
264 $entered_configs{$config} =~ s/^\s*(.*\S)\s*$/$1/;
265 if ($entered_configs{$config} =~ /^\s*$/) {
266 if ($default{$config}) {
267 $entered_configs{$config} = $default{$config};
268 } else {
269 print "Your answer can not be blank\n";
270 next;
271 }
272 }
273 last;
274 }
275}
276
277sub get_ktest_configs {
278 get_ktest_config("MACHINE");
279 get_ktest_config("SSH_USER");
280 get_ktest_config("BUILD_DIR");
281 get_ktest_config("OUTPUT_DIR");
282 get_ktest_config("BUILD_TARGET");
283 get_ktest_config("TARGET_IMAGE");
284 get_ktest_config("POWER_CYCLE");
285 get_ktest_config("CONSOLE");
286 get_ktest_config("LOCALVERSION");
287
288 my $rtype = $opt{"REBOOT_TYPE"};
289
290 if (!defined($rtype)) {
291 if (!defined($opt{"GRUB_MENU"})) {
292 get_ktest_config("REBOOT_TYPE");
293 $rtype = $entered_configs{"REBOOT_TYPE"};
294 } else {
295 $rtype = "grub";
296 }
297 }
298
299 if ($rtype eq "grub") {
300 get_ktest_config("GRUB_MENU");
301 } else {
302 get_ktest_config("REBOOT_SCRIPT");
303 }
304}
305
77d942ce
SR
306sub process_variables {
307 my ($value) = @_;
308 my $retval = "";
309
310 # We want to check for '\', and it is just easier
311 # to check the previous characet of '$' and not need
312 # to worry if '$' is the first character. By adding
313 # a space to $value, we can just check [^\\]\$ and
314 # it will still work.
315 $value = " $value";
316
317 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
318 my $begin = $1;
319 my $var = $2;
320 my $end = $3;
321 # append beginning of value to retval
322 $retval = "$retval$begin";
323 if (defined($variable{$var})) {
324 $retval = "$retval$variable{$var}";
325 } else {
326 # put back the origin piece.
327 $retval = "$retval\$\{$var\}";
328 }
329 $value = $end;
330 }
331 $retval = "$retval$value";
332
333 # remove the space added in the beginning
334 $retval =~ s/ //;
335
336 return "$retval"
337}
338
a57419b3
SR
339sub set_value {
340 my ($lvalue, $rvalue) = @_;
341
342 if (defined($opt{$lvalue})) {
343 die "Error: Option $lvalue defined more than once!\n";
344 }
21a9679f
SR
345 if ($rvalue =~ /^\s*$/) {
346 delete $opt{$lvalue};
347 } else {
77d942ce 348 $rvalue = process_variables($rvalue);
21a9679f
SR
349 $opt{$lvalue} = $rvalue;
350 }
a57419b3
SR
351}
352
77d942ce
SR
353sub set_variable {
354 my ($lvalue, $rvalue) = @_;
355
356 if ($rvalue =~ /^\s*$/) {
357 delete $variable{$lvalue};
358 } else {
359 $rvalue = process_variables($rvalue);
360 $variable{$lvalue} = $rvalue;
361 }
362}
363
ab7a3f52
SR
364sub process_compare {
365 my ($lval, $cmp, $rval) = @_;
366
367 # remove whitespace
368
369 $lval =~ s/^\s*//;
370 $lval =~ s/\s*$//;
371
372 $rval =~ s/^\s*//;
373 $rval =~ s/\s*$//;
374
375 if ($cmp eq "==") {
376 return $lval eq $rval;
377 } elsif ($cmp eq "!=") {
378 return $lval ne $rval;
379 }
380
381 my $statement = "$lval $cmp $rval";
382 my $ret = eval $statement;
383
384 # $@ stores error of eval
385 if ($@) {
386 return -1;
387 }
388
389 return $ret;
390}
391
45d73a5d
SR
392sub process_if {
393 my ($name, $value) = @_;
394
395 my $val = process_variables($value);
396
ab7a3f52
SR
397 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
398 my $ret = process_compare($1, $2, $3);
399 if ($ret < 0) {
400 die "$name: $.: Unable to process comparison\n";
401 }
402 return $ret;
403 }
404
45d73a5d
SR
405 if ($val =~ /^\s*0\s*$/) {
406 return 0;
407 } elsif ($val =~ /^\s*\d+\s*$/) {
408 return 1;
409 }
410
411 die ("$name: $.: Undefined variable $val in if statement\n");
412 return 1;
413}
414
2ed3b161
SR
415sub __read_config {
416 my ($config, $current_test_num) = @_;
2545eb61 417
2ed3b161
SR
418 my $in;
419 open($in, $config) || die "can't read file $config";
2545eb61 420
a57419b3
SR
421 my $name = $config;
422 $name =~ s,.*/(.*),$1,;
423
2ed3b161 424 my $test_num = $$current_test_num;
a57419b3
SR
425 my $default = 1;
426 my $repeat = 1;
427 my $num_tests_set = 0;
428 my $skip = 0;
429 my $rest;
0df213ca 430 my $test_case = 0;
45d73a5d
SR
431 my $if = 0;
432 my $if_set = 0;
a57419b3 433
2ed3b161 434 while (<$in>) {
2545eb61
SR
435
436 # ignore blank lines and comments
437 next if (/^\s*$/ || /\s*\#/);
438
a57419b3
SR
439 if (/^\s*TEST_START(.*)/) {
440
441 $rest = $1;
442
443 if ($num_tests_set) {
444 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
445 }
446
447 my $old_test_num = $test_num;
e48c5293 448 my $old_repeat = $repeat;
a57419b3
SR
449
450 $test_num += $repeat;
451 $default = 0;
452 $repeat = 1;
453
45d73a5d 454 if ($rest =~ /\s+SKIP\b(.*)/) {
a57419b3
SR
455 $rest = $1;
456 $skip = 1;
457 } else {
0df213ca 458 $test_case = 1;
a57419b3
SR
459 $skip = 0;
460 }
461
462 if ($rest =~ /\s+ITERATE\s+(\d+)(.*)$/) {
463 $repeat = $1;
464 $rest = $2;
465 $repeat_tests{"$test_num"} = $repeat;
466 }
467
ab7a3f52
SR
468 if ($rest =~ /\sIF\s+(.*)/) {
469 $rest = "";
45d73a5d
SR
470 if (process_if($name, $1)) {
471 $if_set = 1;
472 } else {
473 $skip = 1;
474 }
475 $if = 1;
476 } else {
477 $if = 0;
a57419b3
SR
478 }
479
480 if ($rest !~ /^\s*$/) {
481 die "$name: $.: Gargbage found after TEST_START\n$_";
482 }
483
484 if ($skip) {
485 $test_num = $old_test_num;
e48c5293 486 $repeat = $old_repeat;
a57419b3
SR
487 }
488
489 } elsif (/^\s*DEFAULTS(.*)$/) {
490 $default = 1;
491
492 $rest = $1;
493
494 if ($rest =~ /\s+SKIP(.*)/) {
495 $rest = $1;
496 $skip = 1;
497 } else {
498 $skip = 0;
499 }
500
ab7a3f52 501 if ($rest =~ /\sIF\s+(.*)/) {
45d73a5d 502 $if = 1;
45d73a5d
SR
503 if (process_if($name, $1)) {
504 $if_set = 1;
505 } else {
506 $skip = 1;
507 }
ab7a3f52 508 $rest = "";
45d73a5d
SR
509 } else {
510 $if = 0;
511 }
512
a57419b3
SR
513 if ($rest !~ /^\s*$/) {
514 die "$name: $.: Gargbage found after DEFAULTS\n$_";
515 }
516
ab7a3f52 517 } elsif (/^\s*ELSE\b(.*)$/) {
45d73a5d
SR
518 if (!$if) {
519 die "$name: $.: ELSE found with out matching IF section\n$_";
520 }
521 $rest = $1;
522 if ($if_set) {
523 $skip = 1;
ab7a3f52 524 $rest = "";
45d73a5d
SR
525 } else {
526 $skip = 0;
527
ab7a3f52 528 if ($rest =~ /\sIF\s+(.*)/) {
45d73a5d
SR
529 # May be a ELSE IF section.
530 if (!process_if($name, $1)) {
531 $skip = 1;
532 }
ab7a3f52 533 $rest = "";
45d73a5d
SR
534 } else {
535 $if = 0;
536 }
537 }
538
ab7a3f52
SR
539 if ($rest !~ /^\s*$/) {
540 die "$name: $.: Gargbage found after DEFAULTS\n$_";
541 }
542
2ed3b161
SR
543 } elsif (/^\s*INCLUDE\s+(\S+)/) {
544
545 next if ($skip);
546
547 if (!$default) {
548 die "$name: $.: INCLUDE can only be done in default sections\n$_";
549 }
550
551 my $file = process_variables($1);
552
553 if ($file !~ m,^/,) {
554 # check the path of the config file first
555 if ($config =~ m,(.*)/,) {
556 if (-f "$1/$file") {
557 $file = "$1/$file";
558 }
559 }
560 }
561
562 if ( ! -r $file ) {
563 die "$name: $.: Can't read file $file\n$_";
564 }
565
566 if (__read_config($file, \$test_num)) {
567 $test_case = 1;
568 }
569
a57419b3
SR
570 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
571
572 next if ($skip);
573
2545eb61
SR
574 my $lvalue = $1;
575 my $rvalue = $2;
576
a57419b3
SR
577 if (!$default &&
578 ($lvalue eq "NUM_TESTS" ||
579 $lvalue eq "LOG_FILE" ||
580 $lvalue eq "CLEAR_LOG")) {
581 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
582 }
583
584 if ($lvalue eq "NUM_TESTS") {
585 if ($test_num) {
586 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
587 }
588 if (!$default) {
589 die "$name: $.: NUM_TESTS must be set in default section\n";
590 }
591 $num_tests_set = 1;
592 }
593
594 if ($default || $lvalue =~ /\[\d+\]$/) {
595 set_value($lvalue, $rvalue);
596 } else {
597 my $val = "$lvalue\[$test_num\]";
598 set_value($val, $rvalue);
599
600 if ($repeat > 1) {
601 $repeats{$val} = $repeat;
602 }
a75fecec 603 }
77d942ce
SR
604 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
605 next if ($skip);
606
607 my $lvalue = $1;
608 my $rvalue = $2;
609
610 # process config variables.
611 # Config variables are only active while reading the
612 # config and can be defined anywhere. They also ignore
613 # TEST_START and DEFAULTS, but are skipped if they are in
614 # on of these sections that have SKIP defined.
615 # The save variable can be
616 # defined multiple times and the new one simply overrides
617 # the prevous one.
618 set_variable($lvalue, $rvalue);
619
a57419b3
SR
620 } else {
621 die "$name: $.: Garbage found in config\n$_";
2545eb61
SR
622 }
623 }
624
a57419b3
SR
625 if ($test_num) {
626 $test_num += $repeat - 1;
627 $opt{"NUM_TESTS"} = $test_num;
628 }
629
2ed3b161
SR
630 close($in);
631
632 $$current_test_num = $test_num;
633
634 return $test_case;
635}
636
637sub read_config {
638 my ($config) = @_;
639
640 my $test_case;
641 my $test_num = 0;
642
643 $test_case = __read_config $config, \$test_num;
644
8d1491ba
SR
645 # make sure we have all mandatory configs
646 get_ktest_configs;
647
0df213ca
SR
648 # was a test specified?
649 if (!$test_case) {
650 print "No test case specified.\n";
651 print "What test case would you like to run?\n";
652 my $ans = <STDIN>;
653 chomp $ans;
654 $default{"TEST_TYPE"} = $ans;
655 }
656
a75fecec
SR
657 # set any defaults
658
659 foreach my $default (keys %default) {
660 if (!defined($opt{$default})) {
661 $opt{$default} = $default{$default};
662 }
663 }
2545eb61
SR
664}
665
23715c3c
SR
666sub __eval_option {
667 my ($option, $i) = @_;
668
669 # Add space to evaluate the character before $
670 $option = " $option";
671 my $retval = "";
672
673 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
674 my $start = $1;
675 my $var = $2;
676 my $end = $3;
677
678 # Append beginning of line
679 $retval = "$retval$start";
680
681 # If the iteration option OPT[$i] exists, then use that.
682 # otherwise see if the default OPT (without [$i]) exists.
683
684 my $o = "$var\[$i\]";
685
686 if (defined($opt{$o})) {
687 $o = $opt{$o};
688 $retval = "$retval$o";
689 } elsif (defined($opt{$var})) {
690 $o = $opt{$var};
691 $retval = "$retval$o";
692 } else {
693 $retval = "$retval\$\{$var\}";
694 }
695
696 $option = $end;
697 }
698
699 $retval = "$retval$option";
700
701 $retval =~ s/^ //;
702
703 return $retval;
704}
705
706sub eval_option {
707 my ($option, $i) = @_;
708
709 my $prev = "";
710
711 # Since an option can evaluate to another option,
712 # keep iterating until we do not evaluate any more
713 # options.
714 my $r = 0;
715 while ($prev ne $option) {
716 # Check for recursive evaluations.
717 # 100 deep should be more than enough.
718 if ($r++ > 100) {
719 die "Over 100 evaluations accurred with $option\n" .
720 "Check for recursive variables\n";
721 }
722 $prev = $option;
723 $option = __eval_option($option, $i);
724 }
725
726 return $option;
727}
728
d1e2f22a 729sub _logit {
2545eb61
SR
730 if (defined($opt{"LOG_FILE"})) {
731 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
732 print OUT @_;
733 close(OUT);
734 }
735}
736
d1e2f22a
SR
737sub logit {
738 if (defined($opt{"LOG_FILE"})) {
739 _logit @_;
740 } else {
741 print @_;
742 }
743}
744
5f9b6ced
SR
745sub doprint {
746 print @_;
d1e2f22a 747 _logit @_;
5f9b6ced
SR
748}
749
7faafbd6 750sub run_command;
2728be41
AJ
751sub start_monitor;
752sub end_monitor;
753sub wait_for_monitor;
7faafbd6
SR
754
755sub reboot {
2728be41
AJ
756 my ($time) = @_;
757
2b803365
SR
758 if (defined($time)) {
759 start_monitor;
760 # flush out current monitor
761 # May contain the reboot success line
762 wait_for_monitor 1;
763 }
764
7faafbd6 765 # try to reboot normally
e48c5293 766 if (run_command $reboot) {
576f627c
SR
767 if (defined($powercycle_after_reboot)) {
768 sleep $powercycle_after_reboot;
769 run_command "$power_cycle";
770 }
771 } else {
7faafbd6 772 # nope? power cycle it.
a75fecec 773 run_command "$power_cycle";
7faafbd6 774 }
2728be41
AJ
775
776 if (defined($time)) {
2b803365 777 wait_for_monitor($time, $reboot_success_line);
2728be41
AJ
778 end_monitor;
779 }
7faafbd6
SR
780}
781
576f627c
SR
782sub do_not_reboot {
783 my $i = $iteration;
784
4ab1cce5 785 return $test_type eq "build" || $no_reboot ||
576f627c
SR
786 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
787 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
788}
789
5c42fc5b 790sub dodie {
5a391fbf 791 doprint "CRITICAL FAILURE... ", @_, "\n";
5c42fc5b 792
576f627c
SR
793 my $i = $iteration;
794
795 if ($reboot_on_error && !do_not_reboot) {
796
75c3fda7 797 doprint "REBOOTING\n";
7faafbd6 798 reboot;
75c3fda7 799
a75fecec 800 } elsif ($poweroff_on_error && defined($power_off)) {
5c42fc5b 801 doprint "POWERING OFF\n";
a75fecec 802 `$power_off`;
5c42fc5b 803 }
75c3fda7 804
f80802cb
SR
805 if (defined($opt{"LOG_FILE"})) {
806 print " See $opt{LOG_FILE} for more info.\n";
807 }
808
576f627c 809 die @_, "\n";
5c42fc5b
SR
810}
811
7faafbd6
SR
812sub open_console {
813 my ($fp) = @_;
814
815 my $flags;
816
a75fecec
SR
817 my $pid = open($fp, "$console|") or
818 dodie "Can't open console $console";
7faafbd6
SR
819
820 $flags = fcntl($fp, F_GETFL, 0) or
576f627c 821 dodie "Can't get flags for the socket: $!";
7faafbd6 822 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
576f627c 823 dodie "Can't set flags for the socket: $!";
7faafbd6
SR
824
825 return $pid;
826}
827
828sub close_console {
829 my ($fp, $pid) = @_;
830
831 doprint "kill child process $pid\n";
832 kill 2, $pid;
833
834 print "closing!\n";
835 close($fp);
836}
837
838sub start_monitor {
839 if ($monitor_cnt++) {
840 return;
841 }
842 $monitor_fp = \*MONFD;
843 $monitor_pid = open_console $monitor_fp;
a75fecec
SR
844
845 return;
846
847 open(MONFD, "Stop perl from warning about single use of MONFD");
7faafbd6
SR
848}
849
850sub end_monitor {
851 if (--$monitor_cnt) {
852 return;
853 }
854 close_console($monitor_fp, $monitor_pid);
855}
856
857sub wait_for_monitor {
2b803365
SR
858 my ($time, $stop) = @_;
859 my $full_line = "";
7faafbd6 860 my $line;
2b803365 861 my $booted = 0;
7faafbd6 862
a75fecec 863 doprint "** Wait for monitor to settle down **\n";
7faafbd6
SR
864
865 # read the monitor and wait for the system to calm down
2b803365 866 while (!$booted) {
7faafbd6 867 $line = wait_for_input($monitor_fp, $time);
2b803365
SR
868 last if (!defined($line));
869 print "$line";
870 $full_line .= $line;
871
872 if (defined($stop) && $full_line =~ /$stop/) {
873 doprint "wait for monitor detected $stop\n";
874 $booted = 1;
875 }
876
877 if ($line =~ /\n/) {
878 $full_line = "";
879 }
880 }
a75fecec 881 print "** Monitor flushed **\n";
7faafbd6
SR
882}
883
2b7d9b21
SR
884sub fail {
885
a75fecec 886 if ($die_on_failure) {
2b7d9b21
SR
887 dodie @_;
888 }
889
a75fecec 890 doprint "FAILED\n";
7faafbd6 891
576f627c
SR
892 my $i = $iteration;
893
a75fecec 894 # no need to reboot for just building.
576f627c 895 if (!do_not_reboot) {
a75fecec 896 doprint "REBOOTING\n";
2728be41 897 reboot $sleep_time;
a75fecec 898 }
7faafbd6 899
9064af52
SR
900 my $name = "";
901
902 if (defined($test_name)) {
903 $name = " ($test_name)";
904 }
905
576f627c
SR
906 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
907 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
9064af52 908 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
576f627c
SR
909 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
910 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
a75fecec
SR
911
912 return 1 if (!defined($store_failures));
7faafbd6
SR
913
914 my @t = localtime;
915 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
916 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
917
cccae1a6
SR
918 my $type = $build_type;
919 if ($type =~ /useconfig/) {
920 $type = "useconfig";
921 }
922
923 my $dir = "$machine-$test_type-$type-fail-$date";
a75fecec 924 my $faildir = "$store_failures/$dir";
7faafbd6
SR
925
926 if (!-d $faildir) {
927 mkpath($faildir) or
a75fecec 928 die "can't create $faildir";
7faafbd6 929 }
51ad1dd1
SR
930 if (-f "$output_config") {
931 cp "$output_config", "$faildir/config" or
7faafbd6
SR
932 die "failed to copy .config";
933 }
934 if (-f $buildlog) {
935 cp $buildlog, "$faildir/buildlog" or
936 die "failed to move $buildlog";
937 }
938 if (-f $dmesg) {
939 cp $dmesg, "$faildir/dmesg" or
940 die "failed to move $dmesg";
941 }
942
943 doprint "*** Saved info to $faildir ***\n";
944
2b7d9b21
SR
945 return 1;
946}
947
2545eb61
SR
948sub run_command {
949 my ($command) = @_;
d6ce2a0b
SR
950 my $dolog = 0;
951 my $dord = 0;
952 my $pid;
953
e48c5293
SR
954 $command =~ s/\$SSH_USER/$ssh_user/g;
955 $command =~ s/\$MACHINE/$machine/g;
956
d6ce2a0b
SR
957 doprint("$command ... ");
958
959 $pid = open(CMD, "$command 2>&1 |") or
2b7d9b21 960 (fail "unable to exec $command" and return 0);
2545eb61
SR
961
962 if (defined($opt{"LOG_FILE"})) {
d6ce2a0b
SR
963 open(LOG, ">>$opt{LOG_FILE}") or
964 dodie "failed to write to log";
965 $dolog = 1;
6c5ee0be
SR
966 }
967
968 if (defined($redirect)) {
d6ce2a0b
SR
969 open (RD, ">$redirect") or
970 dodie "failed to write to redirect $redirect";
971 $dord = 1;
2545eb61
SR
972 }
973
d6ce2a0b
SR
974 while (<CMD>) {
975 print LOG if ($dolog);
976 print RD if ($dord);
977 }
2545eb61 978
d6ce2a0b 979 waitpid($pid, 0);
2545eb61
SR
980 my $failed = $?;
981
d6ce2a0b
SR
982 close(CMD);
983 close(LOG) if ($dolog);
984 close(RD) if ($dord);
985
2545eb61
SR
986 if ($failed) {
987 doprint "FAILED!\n";
988 } else {
989 doprint "SUCCESS\n";
990 }
991
5f9b6ced
SR
992 return !$failed;
993}
994
e48c5293
SR
995sub run_ssh {
996 my ($cmd) = @_;
997 my $cp_exec = $ssh_exec;
998
999 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1000 return run_command "$cp_exec";
1001}
1002
1003sub run_scp {
1004 my ($src, $dst) = @_;
1005 my $cp_scp = $scp_to_target;
1006
1007 $cp_scp =~ s/\$SRC_FILE/$src/g;
1008 $cp_scp =~ s/\$DST_FILE/$dst/g;
1009
1010 return run_command "$cp_scp";
1011}
1012
5f9b6ced
SR
1013sub get_grub_index {
1014
a75fecec
SR
1015 if ($reboot_type ne "grub") {
1016 return;
1017 }
5a391fbf 1018 return if (defined($grub_number));
5f9b6ced
SR
1019
1020 doprint "Find grub menu ... ";
1021 $grub_number = -1;
e48c5293
SR
1022
1023 my $ssh_grub = $ssh_exec;
1024 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1025
1026 open(IN, "$ssh_grub |")
5f9b6ced 1027 or die "unable to get menu.lst";
e48c5293 1028
eaa1fe25
SR
1029 my $found = 0;
1030
5f9b6ced 1031 while (<IN>) {
a75fecec 1032 if (/^\s*title\s+$grub_menu\s*$/) {
5f9b6ced 1033 $grub_number++;
eaa1fe25 1034 $found = 1;
5f9b6ced
SR
1035 last;
1036 } elsif (/^\s*title\s/) {
1037 $grub_number++;
1038 }
1039 }
1040 close(IN);
1041
a75fecec 1042 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
eaa1fe25 1043 if (!$found);
5f9b6ced 1044 doprint "$grub_number\n";
2545eb61
SR
1045}
1046
2545eb61
SR
1047sub wait_for_input
1048{
1049 my ($fp, $time) = @_;
1050 my $rin;
1051 my $ready;
1052 my $line;
1053 my $ch;
1054
1055 if (!defined($time)) {
1056 $time = $timeout;
1057 }
1058
1059 $rin = '';
1060 vec($rin, fileno($fp), 1) = 1;
1061 $ready = select($rin, undef, undef, $time);
1062
1063 $line = "";
1064
1065 # try to read one char at a time
1066 while (sysread $fp, $ch, 1) {
1067 $line .= $ch;
1068 last if ($ch eq "\n");
1069 }
1070
1071 if (!length($line)) {
1072 return undef;
1073 }
1074
1075 return $line;
1076}
1077
75c3fda7 1078sub reboot_to {
a75fecec 1079 if ($reboot_type eq "grub") {
4da46da2 1080 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch && reboot)'";
a75fecec
SR
1081 return;
1082 }
1083
1084 run_command "$reboot_script";
2545eb61
SR
1085}
1086
a57419b3
SR
1087sub get_sha1 {
1088 my ($commit) = @_;
1089
1090 doprint "git rev-list --max-count=1 $commit ... ";
1091 my $sha1 = `git rev-list --max-count=1 $commit`;
1092 my $ret = $?;
1093
1094 logit $sha1;
1095
1096 if ($ret) {
1097 doprint "FAILED\n";
1098 dodie "Failed to get git $commit";
1099 }
1100
1101 print "SUCCESS\n";
1102
1103 chomp $sha1;
1104
1105 return $sha1;
1106}
1107
5a391fbf 1108sub monitor {
2545eb61
SR
1109 my $booted = 0;
1110 my $bug = 0;
5c42fc5b 1111 my $skip_call_trace = 0;
2b7d9b21 1112 my $loops;
2545eb61 1113
7faafbd6 1114 wait_for_monitor 5;
2545eb61
SR
1115
1116 my $line;
1117 my $full_line = "";
1118
7faafbd6
SR
1119 open(DMESG, "> $dmesg") or
1120 die "unable to write to $dmesg";
2545eb61 1121
75c3fda7 1122 reboot_to;
2545eb61 1123
1c8a617a
SR
1124 my $success_start;
1125 my $failure_start;
2d01b26a
SR
1126 my $monitor_start = time;
1127 my $done = 0;
f1a5b962 1128 my $version_found = 0;
1c8a617a 1129
2d01b26a 1130 while (!$done) {
2545eb61 1131
ecaf8e52
SR
1132 if ($bug && defined($stop_after_failure) &&
1133 $stop_after_failure >= 0) {
1134 my $time = $stop_after_failure - (time - $failure_start);
1135 $line = wait_for_input($monitor_fp, $time);
1136 if (!defined($line)) {
1137 doprint "bug timed out after $booted_timeout seconds\n";
1138 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1139 last;
1140 }
1141 } elsif ($booted) {
a75fecec 1142 $line = wait_for_input($monitor_fp, $booted_timeout);
cd4f1d53
SR
1143 if (!defined($line)) {
1144 my $s = $booted_timeout == 1 ? "" : "s";
1145 doprint "Successful boot found: break after $booted_timeout second$s\n";
1146 last;
1147 }
2b7d9b21 1148 } else {
7faafbd6 1149 $line = wait_for_input($monitor_fp);
cd4f1d53
SR
1150 if (!defined($line)) {
1151 my $s = $timeout == 1 ? "" : "s";
1152 doprint "Timed out after $timeout second$s\n";
1153 last;
1154 }
2b7d9b21 1155 }
2545eb61 1156
2545eb61 1157 doprint $line;
7faafbd6 1158 print DMESG $line;
2545eb61
SR
1159
1160 # we are not guaranteed to get a full line
1161 $full_line .= $line;
1162
a75fecec 1163 if ($full_line =~ /$success_line/) {
2545eb61 1164 $booted = 1;
1c8a617a
SR
1165 $success_start = time;
1166 }
1167
1168 if ($booted && defined($stop_after_success) &&
1169 $stop_after_success >= 0) {
1170 my $now = time;
1171 if ($now - $success_start >= $stop_after_success) {
1172 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1173 last;
1174 }
2545eb61
SR
1175 }
1176
5c42fc5b
SR
1177 if ($full_line =~ /\[ backtrace testing \]/) {
1178 $skip_call_trace = 1;
1179 }
1180
2545eb61 1181 if ($full_line =~ /call trace:/i) {
4651920e 1182 if (!$bug && !$skip_call_trace) {
1c8a617a
SR
1183 $bug = 1;
1184 $failure_start = time;
1185 }
1186 }
1187
1188 if ($bug && defined($stop_after_failure) &&
1189 $stop_after_failure >= 0) {
1190 my $now = time;
1191 if ($now - $failure_start >= $stop_after_failure) {
1192 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1193 last;
1194 }
5c42fc5b
SR
1195 }
1196
1197 if ($full_line =~ /\[ end of backtrace testing \]/) {
1198 $skip_call_trace = 0;
1199 }
1200
1201 if ($full_line =~ /Kernel panic -/) {
10abf118 1202 $failure_start = time;
2545eb61
SR
1203 $bug = 1;
1204 }
1205
f1a5b962
SR
1206 # Detect triple faults by testing the banner
1207 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1208 if ($1 eq $version) {
1209 $version_found = 1;
1210 } elsif ($version_found && $detect_triplefault) {
1211 # We already booted into the kernel we are testing,
1212 # but now we booted into another kernel?
1213 # Consider this a triple fault.
1214 doprint "Aleady booted in Linux kernel $version, but now\n";
1215 doprint "we booted into Linux kernel $1.\n";
1216 doprint "Assuming that this is a triple fault.\n";
1217 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1218 last;
1219 }
1220 }
1221
2545eb61
SR
1222 if ($line =~ /\n/) {
1223 $full_line = "";
1224 }
2d01b26a
SR
1225
1226 if ($stop_test_after > 0 && !$booted && !$bug) {
1227 if (time - $monitor_start > $stop_test_after) {
4d62bf51 1228 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
2d01b26a
SR
1229 $done = 1;
1230 }
1231 }
2545eb61
SR
1232 }
1233
7faafbd6 1234 close(DMESG);
2545eb61 1235
a75fecec 1236 if ($bug) {
2b7d9b21 1237 return 0 if ($in_bisect);
576f627c 1238 fail "failed - got a bug report" and return 0;
2545eb61
SR
1239 }
1240
a75fecec 1241 if (!$booted) {
2b7d9b21 1242 return 0 if ($in_bisect);
576f627c 1243 fail "failed - never got a boot prompt." and return 0;
2545eb61 1244 }
5f9b6ced 1245
2b7d9b21 1246 return 1;
2545eb61
SR
1247}
1248
db05cfef
SR
1249sub do_post_install {
1250
1251 return if (!defined($post_install));
1252
1253 my $cp_post_install = $post_install;
1254 $cp_post_install =~ s/\$KERNEL_VERSION/$version/g;
1255 run_command "$cp_post_install" or
1256 dodie "Failed to run post install";
1257}
1258
2545eb61
SR
1259sub install {
1260
e0a8742e
SR
1261 return if ($no_install);
1262
e48c5293 1263 run_scp "$outputdir/$build_target", "$target_image" or
5c42fc5b 1264 dodie "failed to copy image";
2545eb61 1265
5f9b6ced 1266 my $install_mods = 0;
2545eb61 1267
5f9b6ced
SR
1268 # should we process modules?
1269 $install_mods = 0;
51ad1dd1 1270 open(IN, "$output_config") or dodie("Can't read config file");
5f9b6ced
SR
1271 while (<IN>) {
1272 if (/CONFIG_MODULES(=y)?/) {
1273 $install_mods = 1 if (defined($1));
1274 last;
5c42fc5b 1275 }
5f9b6ced
SR
1276 }
1277 close(IN);
5c42fc5b 1278
5f9b6ced 1279 if (!$install_mods) {
db05cfef 1280 do_post_install;
5f9b6ced
SR
1281 doprint "No modules needed\n";
1282 return;
1283 }
2545eb61 1284
a75fecec 1285 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
5f9b6ced 1286 dodie "Failed to install modules";
5c42fc5b 1287
5f9b6ced 1288 my $modlib = "/lib/modules/$version";
a57419b3 1289 my $modtar = "ktest-mods.tar.bz2";
5c42fc5b 1290
e48c5293 1291 run_ssh "rm -rf $modlib" or
5f9b6ced 1292 dodie "failed to remove old mods: $modlib";
5c42fc5b 1293
5f9b6ced 1294 # would be nice if scp -r did not follow symbolic links
a75fecec 1295 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
5f9b6ced
SR
1296 dodie "making tarball";
1297
e48c5293 1298 run_scp "$tmpdir/$modtar", "/tmp" or
5f9b6ced
SR
1299 dodie "failed to copy modules";
1300
a75fecec 1301 unlink "$tmpdir/$modtar";
5f9b6ced 1302
e7b13441 1303 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
5f9b6ced 1304 dodie "failed to tar modules";
2545eb61 1305
e48c5293 1306 run_ssh "rm -f /tmp/$modtar";
8b37ca8c 1307
db05cfef 1308 do_post_install;
2545eb61
SR
1309}
1310
ddf607e5
SR
1311sub get_version {
1312 # get the release name
1313 doprint "$make kernelrelease ... ";
1314 $version = `$make kernelrelease | tail -1`;
1315 chomp($version);
1316 doprint "$version\n";
1317}
1318
1319sub start_monitor_and_boot {
1320 get_grub_index;
1321 get_version;
1322 install;
1323
1324 start_monitor;
1325 return monitor;
1326}
1327
6c5ee0be
SR
1328sub check_buildlog {
1329 my ($patch) = @_;
1330
6c5ee0be
SR
1331 my @files = `git show $patch | diffstat -l`;
1332
1333 open(IN, "git show $patch |") or
1334 dodie "failed to show $patch";
1335 while (<IN>) {
1336 if (m,^--- a/(.*),) {
1337 chomp $1;
1338 $files[$#files] = $1;
1339 }
1340 }
1341 close(IN);
1342
1343 open(IN, $buildlog) or dodie "Can't open $buildlog";
1344 while (<IN>) {
1345 if (/^\s*(.*?):.*(warning|error)/) {
1346 my $err = $1;
1347 foreach my $file (@files) {
a75fecec 1348 my $fullpath = "$builddir/$file";
6c5ee0be 1349 if ($file eq $err || $fullpath eq $err) {
2b7d9b21 1350 fail "$file built with warnings" and return 0;
6c5ee0be
SR
1351 }
1352 }
1353 }
1354 }
1355 close(IN);
2b7d9b21
SR
1356
1357 return 1;
6c5ee0be
SR
1358}
1359
fcb3f16a
SR
1360sub apply_min_config {
1361 my $outconfig = "$output_config.new";
1362
1363 # Read the config file and remove anything that
1364 # is in the force_config hash (from minconfig and others)
1365 # then add the force config back.
1366
1367 doprint "Applying minimum configurations into $output_config.new\n";
1368
1369 open (OUT, ">$outconfig") or
1370 dodie "Can't create $outconfig";
1371
1372 if (-f $output_config) {
1373 open (IN, $output_config) or
1374 dodie "Failed to open $output_config";
1375 while (<IN>) {
1376 if (/^(# )?(CONFIG_[^\s=]*)/) {
1377 next if (defined($force_config{$2}));
1378 }
1379 print OUT;
1380 }
1381 close IN;
1382 }
1383 foreach my $config (keys %force_config) {
1384 print OUT "$force_config{$config}\n";
1385 }
1386 close OUT;
1387
1388 run_command "mv $outconfig $output_config";
1389}
1390
612b9e9b 1391sub make_oldconfig {
612b9e9b 1392
4c4ab120
SR
1393 my @force_list = keys %force_config;
1394
1395 if ($#force_list >= 0) {
1396 apply_min_config;
1397 }
fcb3f16a
SR
1398
1399 if (!run_command "$make oldnoconfig") {
612b9e9b
SR
1400 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1401 # try a yes '' | oldconfig
1402 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
fcb3f16a 1403 run_command "yes '' | $make oldconfig" or
612b9e9b
SR
1404 dodie "failed make config oldconfig";
1405 }
1406}
1407
fcb3f16a
SR
1408# read a config file and use this to force new configs.
1409sub load_force_config {
1410 my ($config) = @_;
1411
1412 open(IN, $config) or
1413 dodie "failed to read $config";
1414 while (<IN>) {
1415 chomp;
1416 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1417 $force_config{$1} = $_;
1418 } elsif (/^# (CONFIG_\S*) is not set/) {
1419 $force_config{$1} = $_;
1420 }
1421 }
1422 close IN;
1423}
1424
2545eb61
SR
1425sub build {
1426 my ($type) = @_;
5c42fc5b 1427
7faafbd6
SR
1428 unlink $buildlog;
1429
4ab1cce5
SR
1430 # Failed builds should not reboot the target
1431 my $save_no_reboot = $no_reboot;
1432 $no_reboot = 1;
1433
0bd6c1a3
SR
1434 if (defined($pre_build)) {
1435 my $ret = run_command $pre_build;
1436 if (!$ret && defined($pre_build_die) &&
1437 $pre_build_die) {
1438 dodie "failed to pre_build\n";
1439 }
1440 }
1441
75c3fda7 1442 if ($type =~ /^useconfig:(.*)/) {
51ad1dd1 1443 run_command "cp $1 $output_config" or
75c3fda7 1444 dodie "could not copy $1 to .config";
5f9b6ced 1445
75c3fda7
SR
1446 $type = "oldconfig";
1447 }
1448
5c42fc5b
SR
1449 # old config can ask questions
1450 if ($type eq "oldconfig") {
9386c6ab 1451 $type = "oldnoconfig";
75c3fda7
SR
1452
1453 # allow for empty configs
51ad1dd1 1454 run_command "touch $output_config";
75c3fda7 1455
13488231
AJ
1456 if (!$noclean) {
1457 run_command "mv $output_config $outputdir/config_temp" or
1458 dodie "moving .config";
2545eb61 1459
13488231 1460 run_command "$make mrproper" or dodie "make mrproper";
2545eb61 1461
13488231
AJ
1462 run_command "mv $outputdir/config_temp $output_config" or
1463 dodie "moving config_temp";
1464 }
5c42fc5b
SR
1465
1466 } elsif (!$noclean) {
51ad1dd1 1467 unlink "$output_config";
5f9b6ced 1468 run_command "$make mrproper" or
5c42fc5b 1469 dodie "make mrproper";
5c42fc5b 1470 }
2545eb61
SR
1471
1472 # add something to distinguish this build
a75fecec
SR
1473 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1474 print OUT "$localversion\n";
2545eb61
SR
1475 close(OUT);
1476
5f9b6ced 1477 if (defined($minconfig)) {
fcb3f16a 1478 load_force_config($minconfig);
2545eb61
SR
1479 }
1480
fcb3f16a
SR
1481 if ($type ne "oldnoconfig") {
1482 run_command "$make $type" or
612b9e9b
SR
1483 dodie "failed make config";
1484 }
fcb3f16a
SR
1485 # Run old config regardless, to enforce min configurations
1486 make_oldconfig;
2545eb61 1487
a75fecec 1488 $redirect = "$buildlog";
0bd6c1a3
SR
1489 my $build_ret = run_command "$make $build_options";
1490 undef $redirect;
1491
1492 if (defined($post_build)) {
1493 my $ret = run_command $post_build;
1494 if (!$ret && defined($post_build_die) &&
1495 $post_build_die) {
1496 dodie "failed to post_build\n";
1497 }
1498 }
1499
1500 if (!$build_ret) {
5f9b6ced 1501 # bisect may need this to pass
4ab1cce5
SR
1502 if ($in_bisect) {
1503 $no_reboot = $save_no_reboot;
1504 return 0;
1505 }
2b7d9b21 1506 fail "failed build" and return 0;
2545eb61 1507 }
5f9b6ced 1508
4ab1cce5
SR
1509 $no_reboot = $save_no_reboot;
1510
2b7d9b21 1511 return 1;
2545eb61
SR
1512}
1513
75c3fda7 1514sub halt {
e48c5293 1515 if (!run_ssh "halt" or defined($power_off)) {
576f627c
SR
1516 if (defined($poweroff_after_halt)) {
1517 sleep $poweroff_after_halt;
1518 run_command "$power_off";
1519 }
1520 } else {
75c3fda7 1521 # nope? the zap it!
a75fecec 1522 run_command "$power_off";
75c3fda7
SR
1523 }
1524}
1525
5f9b6ced
SR
1526sub success {
1527 my ($i) = @_;
1528
e48c5293
SR
1529 $successes++;
1530
9064af52
SR
1531 my $name = "";
1532
1533 if (defined($test_name)) {
1534 $name = " ($test_name)";
1535 }
1536
5f9b6ced
SR
1537 doprint "\n\n*******************************************\n";
1538 doprint "*******************************************\n";
9064af52 1539 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
5f9b6ced
SR
1540 doprint "*******************************************\n";
1541 doprint "*******************************************\n";
1542
576f627c 1543 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
a75fecec 1544 doprint "Reboot and wait $sleep_time seconds\n";
2728be41 1545 reboot $sleep_time;
5f9b6ced
SR
1546 }
1547}
1548
c960bb9f
SR
1549sub answer_bisect {
1550 for (;;) {
1551 doprint "Pass or fail? [p/f]";
1552 my $ans = <STDIN>;
1553 chomp $ans;
1554 if ($ans eq "p" || $ans eq "P") {
1555 return 1;
1556 } elsif ($ans eq "f" || $ans eq "F") {
1557 return 0;
1558 } else {
1559 print "Please answer 'P' or 'F'\n";
1560 }
1561 }
1562}
1563
5a391fbf 1564sub child_run_test {
7faafbd6 1565 my $failed = 0;
5a391fbf 1566
7faafbd6 1567 # child should have no power
a75fecec
SR
1568 $reboot_on_error = 0;
1569 $poweroff_on_error = 0;
1570 $die_on_failure = 1;
7faafbd6
SR
1571
1572 run_command $run_test or $failed = 1;
5a391fbf
SR
1573 exit $failed;
1574}
1575
1576my $child_done;
1577
1578sub child_finished {
1579 $child_done = 1;
1580}
1581
1582sub do_run_test {
1583 my $child_pid;
1584 my $child_exit;
5a391fbf
SR
1585 my $line;
1586 my $full_line;
1587 my $bug = 0;
5a391fbf 1588
7faafbd6 1589 wait_for_monitor 1;
5a391fbf 1590
7faafbd6 1591 doprint "run test $run_test\n";
5a391fbf
SR
1592
1593 $child_done = 0;
1594
1595 $SIG{CHLD} = qw(child_finished);
1596
1597 $child_pid = fork;
1598
1599 child_run_test if (!$child_pid);
1600
1601 $full_line = "";
1602
1603 do {
7faafbd6 1604 $line = wait_for_input($monitor_fp, 1);
5a391fbf
SR
1605 if (defined($line)) {
1606
1607 # we are not guaranteed to get a full line
1608 $full_line .= $line;
8ea0e063 1609 doprint $line;
5a391fbf
SR
1610
1611 if ($full_line =~ /call trace:/i) {
1612 $bug = 1;
1613 }
1614
1615 if ($full_line =~ /Kernel panic -/) {
1616 $bug = 1;
1617 }
1618
1619 if ($line =~ /\n/) {
1620 $full_line = "";
1621 }
1622 }
1623 } while (!$child_done && !$bug);
1624
1625 if ($bug) {
8ea0e063
SR
1626 my $failure_start = time;
1627 my $now;
1628 do {
1629 $line = wait_for_input($monitor_fp, 1);
1630 if (defined($line)) {
1631 doprint $line;
1632 }
1633 $now = time;
1634 if ($now - $failure_start >= $stop_after_failure) {
1635 last;
1636 }
1637 } while (defined($line));
1638
5a391fbf
SR
1639 doprint "Detected kernel crash!\n";
1640 # kill the child with extreme prejudice
1641 kill 9, $child_pid;
1642 }
1643
1644 waitpid $child_pid, 0;
1645 $child_exit = $?;
1646
5a391fbf 1647 if ($bug || $child_exit) {
2b7d9b21
SR
1648 return 0 if $in_bisect;
1649 fail "test failed" and return 0;
5a391fbf 1650 }
2b7d9b21 1651 return 1;
5a391fbf
SR
1652}
1653
a75fecec
SR
1654sub run_git_bisect {
1655 my ($command) = @_;
1656
1657 doprint "$command ... ";
1658
1659 my $output = `$command 2>&1`;
1660 my $ret = $?;
1661
1662 logit $output;
1663
1664 if ($ret) {
1665 doprint "FAILED\n";
1666 dodie "Failed to git bisect";
1667 }
1668
1669 doprint "SUCCESS\n";
1670 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1671 doprint "$1 [$2]\n";
1672 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1673 $bisect_bad = $1;
1674 doprint "Found bad commit... $1\n";
1675 return 0;
1676 } else {
1677 # we already logged it, just print it now.
1678 print $output;
1679 }
1680
1681 return 1;
1682}
1683
c23dca7c
SR
1684sub bisect_reboot {
1685 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
2728be41 1686 reboot $bisect_sleep_time;
c23dca7c
SR
1687}
1688
1689# returns 1 on success, 0 on failure, -1 on skip
0a05c769
SR
1690sub run_bisect_test {
1691 my ($type, $buildtype) = @_;
5f9b6ced 1692
2b7d9b21 1693 my $failed = 0;
5f9b6ced
SR
1694 my $result;
1695 my $output;
1696 my $ret;
1697
0a05c769
SR
1698 $in_bisect = 1;
1699
1700 build $buildtype or $failed = 1;
5f9b6ced
SR
1701
1702 if ($type ne "build") {
c23dca7c
SR
1703 if ($failed && $bisect_skip) {
1704 $in_bisect = 0;
1705 return -1;
1706 }
7faafbd6 1707 dodie "Failed on build" if $failed;
5f9b6ced
SR
1708
1709 # Now boot the box
ddf607e5 1710 start_monitor_and_boot or $failed = 1;
5f9b6ced
SR
1711
1712 if ($type ne "boot") {
c23dca7c
SR
1713 if ($failed && $bisect_skip) {
1714 end_monitor;
1715 bisect_reboot;
1716 $in_bisect = 0;
1717 return -1;
1718 }
7faafbd6 1719 dodie "Failed on boot" if $failed;
5a391fbf 1720
2b7d9b21 1721 do_run_test or $failed = 1;
5f9b6ced 1722 }
7faafbd6 1723 end_monitor;
5f9b6ced
SR
1724 }
1725
1726 if ($failed) {
0a05c769 1727 $result = 0;
5f9b6ced 1728 } else {
0a05c769
SR
1729 $result = 1;
1730 }
4025bc62
SR
1731
1732 # reboot the box to a kernel we can ssh to
1733 if ($type ne "build") {
1734 bisect_reboot;
1735 }
0a05c769
SR
1736 $in_bisect = 0;
1737
1738 return $result;
1739}
1740
1741sub run_bisect {
1742 my ($type) = @_;
1743 my $buildtype = "oldconfig";
1744
1745 # We should have a minconfig to use?
1746 if (defined($minconfig)) {
1747 $buildtype = "useconfig:$minconfig";
5f9b6ced
SR
1748 }
1749
0a05c769
SR
1750 my $ret = run_bisect_test $type, $buildtype;
1751
c960bb9f
SR
1752 if ($bisect_manual) {
1753 $ret = answer_bisect;
1754 }
0a05c769 1755
d6ce2a0b
SR
1756 # Are we looking for where it worked, not failed?
1757 if ($reverse_bisect) {
0a05c769 1758 $ret = !$ret;
d6ce2a0b
SR
1759 }
1760
c23dca7c 1761 if ($ret > 0) {
0a05c769 1762 return "good";
c23dca7c 1763 } elsif ($ret == 0) {
0a05c769 1764 return "bad";
c23dca7c
SR
1765 } elsif ($bisect_skip) {
1766 doprint "HIT A BAD COMMIT ... SKIPPING\n";
1767 return "skip";
0a05c769 1768 }
5f9b6ced
SR
1769}
1770
1771sub bisect {
1772 my ($i) = @_;
1773
1774 my $result;
1775
1776 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1777 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
1778 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1779
1780 my $good = $opt{"BISECT_GOOD[$i]"};
1781 my $bad = $opt{"BISECT_BAD[$i]"};
1782 my $type = $opt{"BISECT_TYPE[$i]"};
a75fecec
SR
1783 my $start = $opt{"BISECT_START[$i]"};
1784 my $replay = $opt{"BISECT_REPLAY[$i]"};
3410f6fd
SR
1785 my $start_files = $opt{"BISECT_FILES[$i]"};
1786
1787 if (defined($start_files)) {
1788 $start_files = " -- " . $start_files;
1789 } else {
1790 $start_files = "";
1791 }
5f9b6ced 1792
a57419b3
SR
1793 # convert to true sha1's
1794 $good = get_sha1($good);
1795 $bad = get_sha1($bad);
1796
d6ce2a0b
SR
1797 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1798 $opt{"BISECT_REVERSE[$i]"} == 1) {
1799 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
1800 $reverse_bisect = 1;
1801 } else {
1802 $reverse_bisect = 0;
1803 }
1804
a75fecec
SR
1805 # Can't have a test without having a test to run
1806 if ($type eq "test" && !defined($run_test)) {
1807 $type = "boot";
1808 }
1809
1810 my $check = $opt{"BISECT_CHECK[$i]"};
1811 if (defined($check) && $check ne "0") {
1812
1813 # get current HEAD
a57419b3 1814 my $head = get_sha1("HEAD");
a75fecec
SR
1815
1816 if ($check ne "good") {
1817 doprint "TESTING BISECT BAD [$bad]\n";
1818 run_command "git checkout $bad" or
1819 die "Failed to checkout $bad";
1820
1821 $result = run_bisect $type;
1822
1823 if ($result ne "bad") {
1824 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1825 }
1826 }
1827
1828 if ($check ne "bad") {
1829 doprint "TESTING BISECT GOOD [$good]\n";
1830 run_command "git checkout $good" or
1831 die "Failed to checkout $good";
1832
1833 $result = run_bisect $type;
1834
1835 if ($result ne "good") {
1836 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1837 }
1838 }
1839
1840 # checkout where we started
1841 run_command "git checkout $head" or
1842 die "Failed to checkout $head";
1843 }
1844
3410f6fd 1845 run_command "git bisect start$start_files" or
a75fecec 1846 dodie "could not start bisect";
5f9b6ced
SR
1847
1848 run_command "git bisect good $good" or
a75fecec 1849 dodie "could not set bisect good to $good";
5f9b6ced 1850
a75fecec
SR
1851 run_git_bisect "git bisect bad $bad" or
1852 dodie "could not set bisect bad to $bad";
5f9b6ced 1853
a75fecec
SR
1854 if (defined($replay)) {
1855 run_command "git bisect replay $replay" or
1856 dodie "failed to run replay";
5a391fbf
SR
1857 }
1858
a75fecec
SR
1859 if (defined($start)) {
1860 run_command "git checkout $start" or
1861 dodie "failed to checkout $start";
1862 }
1863
1864 my $test;
5f9b6ced
SR
1865 do {
1866 $result = run_bisect $type;
a75fecec
SR
1867 $test = run_git_bisect "git bisect $result";
1868 } while ($test);
5f9b6ced
SR
1869
1870 run_command "git bisect log" or
1871 dodie "could not capture git bisect log";
1872
1873 run_command "git bisect reset" or
1874 dodie "could not reset git bisect";
1875
1876 doprint "Bad commit was [$bisect_bad]\n";
1877
0a05c769
SR
1878 success $i;
1879}
1880
1881my %config_ignore;
1882my %config_set;
1883
1884my %config_list;
1885my %null_config;
1886
1887my %dependency;
1888
4c4ab120
SR
1889sub assign_configs {
1890 my ($hash, $config) = @_;
0a05c769
SR
1891
1892 open (IN, $config)
1893 or dodie "Failed to read $config";
1894
1895 while (<IN>) {
9bf71749 1896 if (/^((CONFIG\S*)=.*)/) {
4c4ab120 1897 ${$hash}{$2} = $1;
0a05c769
SR
1898 }
1899 }
1900
1901 close(IN);
1902}
1903
4c4ab120
SR
1904sub process_config_ignore {
1905 my ($config) = @_;
1906
1907 assign_configs \%config_ignore, $config;
1908}
1909
0a05c769
SR
1910sub read_current_config {
1911 my ($config_ref) = @_;
1912
1913 %{$config_ref} = ();
1914 undef %{$config_ref};
1915
1916 my @key = keys %{$config_ref};
1917 if ($#key >= 0) {
1918 print "did not delete!\n";
1919 exit;
1920 }
1921 open (IN, "$output_config");
1922
1923 while (<IN>) {
1924 if (/^(CONFIG\S+)=(.*)/) {
1925 ${$config_ref}{$1} = $2;
1926 }
1927 }
1928 close(IN);
1929}
1930
1931sub get_dependencies {
1932 my ($config) = @_;
1933
1934 my $arr = $dependency{$config};
1935 if (!defined($arr)) {
1936 return ();
1937 }
1938
1939 my @deps = @{$arr};
1940
1941 foreach my $dep (@{$arr}) {
1942 print "ADD DEP $dep\n";
1943 @deps = (@deps, get_dependencies $dep);
1944 }
1945
1946 return @deps;
1947}
1948
1949sub create_config {
1950 my @configs = @_;
1951
1952 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
1953
1954 foreach my $config (@configs) {
1955 print OUT "$config_set{$config}\n";
1956 my @deps = get_dependencies $config;
1957 foreach my $dep (@deps) {
1958 print OUT "$config_set{$dep}\n";
1959 }
1960 }
1961
1962 foreach my $config (keys %config_ignore) {
1963 print OUT "$config_ignore{$config}\n";
1964 }
1965 close(OUT);
1966
1967# exit;
fcb3f16a 1968 make_oldconfig;
0a05c769
SR
1969}
1970
1971sub compare_configs {
1972 my (%a, %b) = @_;
1973
1974 foreach my $item (keys %a) {
1975 if (!defined($b{$item})) {
1976 print "diff $item\n";
1977 return 1;
1978 }
1979 delete $b{$item};
1980 }
1981
1982 my @keys = keys %b;
1983 if ($#keys) {
1984 print "diff2 $keys[0]\n";
1985 }
1986 return -1 if ($#keys >= 0);
1987
1988 return 0;
1989}
1990
1991sub run_config_bisect_test {
1992 my ($type) = @_;
1993
1994 return run_bisect_test $type, "oldconfig";
1995}
1996
1997sub process_passed {
1998 my (%configs) = @_;
1999
2000 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2001 # Passed! All these configs are part of a good compile.
2002 # Add them to the min options.
2003 foreach my $config (keys %configs) {
2004 if (defined($config_list{$config})) {
2005 doprint " removing $config\n";
2006 $config_ignore{$config} = $config_list{$config};
2007 delete $config_list{$config};
2008 }
2009 }
f1a27850
SR
2010 doprint "config copied to $outputdir/config_good\n";
2011 run_command "cp -f $output_config $outputdir/config_good";
0a05c769
SR
2012}
2013
2014sub process_failed {
2015 my ($config) = @_;
2016
2017 doprint "\n\n***************************************\n";
2018 doprint "Found bad config: $config\n";
2019 doprint "***************************************\n\n";
2020}
2021
2022sub run_config_bisect {
2023
2024 my @start_list = keys %config_list;
2025
2026 if ($#start_list < 0) {
2027 doprint "No more configs to test!!!\n";
2028 return -1;
2029 }
2030
2031 doprint "***** RUN TEST ***\n";
2032 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2033 my $ret;
2034 my %current_config;
2035
2036 my $count = $#start_list + 1;
2037 doprint " $count configs to test\n";
2038
2039 my $half = int($#start_list / 2);
2040
2041 do {
2042 my @tophalf = @start_list[0 .. $half];
2043
2044 create_config @tophalf;
2045 read_current_config \%current_config;
2046
2047 $count = $#tophalf + 1;
2048 doprint "Testing $count configs\n";
2049 my $found = 0;
2050 # make sure we test something
2051 foreach my $config (@tophalf) {
2052 if (defined($current_config{$config})) {
2053 logit " $config\n";
2054 $found = 1;
2055 }
2056 }
2057 if (!$found) {
2058 # try the other half
2059 doprint "Top half produced no set configs, trying bottom half\n";
4c8cc55b 2060 @tophalf = @start_list[$half + 1 .. $#start_list];
0a05c769
SR
2061 create_config @tophalf;
2062 read_current_config \%current_config;
2063 foreach my $config (@tophalf) {
2064 if (defined($current_config{$config})) {
2065 logit " $config\n";
2066 $found = 1;
2067 }
2068 }
2069 if (!$found) {
2070 doprint "Failed: Can't make new config with current configs\n";
2071 foreach my $config (@start_list) {
2072 doprint " CONFIG: $config\n";
2073 }
2074 return -1;
2075 }
2076 $count = $#tophalf + 1;
2077 doprint "Testing $count configs\n";
2078 }
2079
2080 $ret = run_config_bisect_test $type;
c960bb9f
SR
2081 if ($bisect_manual) {
2082 $ret = answer_bisect;
2083 }
0a05c769
SR
2084 if ($ret) {
2085 process_passed %current_config;
2086 return 0;
2087 }
2088
2089 doprint "This config had a failure.\n";
2090 doprint "Removing these configs that were not set in this config:\n";
f1a27850
SR
2091 doprint "config copied to $outputdir/config_bad\n";
2092 run_command "cp -f $output_config $outputdir/config_bad";
0a05c769
SR
2093
2094 # A config exists in this group that was bad.
2095 foreach my $config (keys %config_list) {
2096 if (!defined($current_config{$config})) {
2097 doprint " removing $config\n";
2098 delete $config_list{$config};
2099 }
2100 }
2101
2102 @start_list = @tophalf;
2103
2104 if ($#start_list == 0) {
2105 process_failed $start_list[0];
2106 return 1;
2107 }
2108
2109 # remove half the configs we are looking at and see if
2110 # they are good.
2111 $half = int($#start_list / 2);
4c8cc55b 2112 } while ($#start_list > 0);
0a05c769 2113
c960bb9f
SR
2114 # we found a single config, try it again unless we are running manually
2115
2116 if ($bisect_manual) {
2117 process_failed $start_list[0];
2118 return 1;
2119 }
2120
0a05c769
SR
2121 my @tophalf = @start_list[0 .. 0];
2122
2123 $ret = run_config_bisect_test $type;
2124 if ($ret) {
2125 process_passed %current_config;
2126 return 0;
2127 }
2128
2129 process_failed $start_list[0];
2130 return 1;
2131}
2132
2133sub config_bisect {
2134 my ($i) = @_;
2135
2136 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2137
2138 my $tmpconfig = "$tmpdir/use_config";
2139
30f75da5
SR
2140 if (defined($config_bisect_good)) {
2141 process_config_ignore $config_bisect_good;
2142 }
2143
0a05c769
SR
2144 # Make the file with the bad config and the min config
2145 if (defined($minconfig)) {
2146 # read the min config for things to ignore
2147 run_command "cp $minconfig $tmpconfig" or
2148 dodie "failed to copy $minconfig to $tmpconfig";
2149 } else {
2150 unlink $tmpconfig;
2151 }
2152
0a05c769 2153 if (-f $tmpconfig) {
fcb3f16a 2154 load_force_config($tmpconfig);
0a05c769
SR
2155 process_config_ignore $tmpconfig;
2156 }
2157
2158 # now process the start config
2159 run_command "cp $start_config $output_config" or
2160 dodie "failed to copy $start_config to $output_config";
2161
2162 # read directly what we want to check
2163 my %config_check;
2164 open (IN, $output_config)
2165 or dodie "faied to open $output_config";
2166
2167 while (<IN>) {
2168 if (/^((CONFIG\S*)=.*)/) {
2169 $config_check{$2} = $1;
2170 }
2171 }
2172 close(IN);
2173
250bae8b 2174 # Now run oldconfig with the minconfig
fcb3f16a 2175 make_oldconfig;
0a05c769
SR
2176
2177 # check to see what we lost (or gained)
2178 open (IN, $output_config)
2179 or dodie "Failed to read $start_config";
2180
2181 my %removed_configs;
2182 my %added_configs;
2183
2184 while (<IN>) {
2185 if (/^((CONFIG\S*)=.*)/) {
2186 # save off all options
2187 $config_set{$2} = $1;
2188 if (defined($config_check{$2})) {
2189 if (defined($config_ignore{$2})) {
2190 $removed_configs{$2} = $1;
2191 } else {
2192 $config_list{$2} = $1;
2193 }
2194 } elsif (!defined($config_ignore{$2})) {
2195 $added_configs{$2} = $1;
2196 $config_list{$2} = $1;
2197 }
2198 }
2199 }
2200 close(IN);
2201
2202 my @confs = keys %removed_configs;
2203 if ($#confs >= 0) {
2204 doprint "Configs overridden by default configs and removed from check:\n";
2205 foreach my $config (@confs) {
2206 doprint " $config\n";
2207 }
2208 }
2209 @confs = keys %added_configs;
2210 if ($#confs >= 0) {
2211 doprint "Configs appearing in make oldconfig and added:\n";
2212 foreach my $config (@confs) {
2213 doprint " $config\n";
2214 }
2215 }
2216
2217 my %config_test;
2218 my $once = 0;
2219
2220 # Sometimes kconfig does weird things. We must make sure
2221 # that the config we autocreate has everything we need
2222 # to test, otherwise we may miss testing configs, or
2223 # may not be able to create a new config.
2224 # Here we create a config with everything set.
2225 create_config (keys %config_list);
2226 read_current_config \%config_test;
2227 foreach my $config (keys %config_list) {
2228 if (!defined($config_test{$config})) {
2229 if (!$once) {
2230 $once = 1;
2231 doprint "Configs not produced by kconfig (will not be checked):\n";
2232 }
2233 doprint " $config\n";
2234 delete $config_list{$config};
2235 }
2236 }
2237 my $ret;
2238 do {
2239 $ret = run_config_bisect;
2240 } while (!$ret);
2241
2242 return $ret if ($ret < 0);
5f9b6ced
SR
2243
2244 success $i;
2245}
2246
27d934b2
SR
2247sub patchcheck_reboot {
2248 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
2728be41 2249 reboot $patchcheck_sleep_time;
27d934b2
SR
2250}
2251
6c5ee0be
SR
2252sub patchcheck {
2253 my ($i) = @_;
2254
2255 die "PATCHCHECK_START[$i] not defined\n"
2256 if (!defined($opt{"PATCHCHECK_START[$i]"}));
2257 die "PATCHCHECK_TYPE[$i] not defined\n"
2258 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2259
2260 my $start = $opt{"PATCHCHECK_START[$i]"};
2261
2262 my $end = "HEAD";
2263 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2264 $end = $opt{"PATCHCHECK_END[$i]"};
2265 }
2266
a57419b3
SR
2267 # Get the true sha1's since we can use things like HEAD~3
2268 $start = get_sha1($start);
2269 $end = get_sha1($end);
2270
6c5ee0be
SR
2271 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2272
2273 # Can't have a test without having a test to run
2274 if ($type eq "test" && !defined($run_test)) {
2275 $type = "boot";
2276 }
2277
2278 open (IN, "git log --pretty=oneline $end|") or
2279 dodie "could not get git list";
2280
2281 my @list;
2282
2283 while (<IN>) {
2284 chomp;
2285 $list[$#list+1] = $_;
2286 last if (/^$start/);
2287 }
2288 close(IN);
2289
2290 if ($list[$#list] !~ /^$start/) {
2b7d9b21 2291 fail "SHA1 $start not found";
6c5ee0be
SR
2292 }
2293
2294 # go backwards in the list
2295 @list = reverse @list;
2296
2297 my $save_clean = $noclean;
1990207d
SR
2298 my %ignored_warnings;
2299
2300 if (defined($ignore_warnings)) {
2301 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2302 $ignored_warnings{$sha1} = 1;
2303 }
2304 }
6c5ee0be
SR
2305
2306 $in_patchcheck = 1;
2307 foreach my $item (@list) {
2308 my $sha1 = $item;
2309 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2310
2311 doprint "\nProcessing commit $item\n\n";
2312
2313 run_command "git checkout $sha1" or
2314 die "Failed to checkout $sha1";
2315
2316 # only clean on the first and last patch
2317 if ($item eq $list[0] ||
2318 $item eq $list[$#list]) {
2319 $noclean = $save_clean;
2320 } else {
2321 $noclean = 1;
2322 }
2323
2324 if (defined($minconfig)) {
2b7d9b21 2325 build "useconfig:$minconfig" or return 0;
6c5ee0be
SR
2326 } else {
2327 # ?? no config to use?
2b7d9b21 2328 build "oldconfig" or return 0;
6c5ee0be
SR
2329 }
2330
1990207d
SR
2331
2332 if (!defined($ignored_warnings{$sha1})) {
2333 check_buildlog $sha1 or return 0;
2334 }
6c5ee0be
SR
2335
2336 next if ($type eq "build");
2337
7faafbd6
SR
2338 my $failed = 0;
2339
ddf607e5 2340 start_monitor_and_boot or $failed = 1;
7faafbd6
SR
2341
2342 if (!$failed && $type ne "boot"){
2343 do_run_test or $failed = 1;
2344 }
2345 end_monitor;
2346 return 0 if ($failed);
2347
27d934b2
SR
2348 patchcheck_reboot;
2349
6c5ee0be
SR
2350 }
2351 $in_patchcheck = 0;
2352 success $i;
2b7d9b21
SR
2353
2354 return 1;
6c5ee0be
SR
2355}
2356
b9066f6c
SR
2357my %depends;
2358my $iflevel = 0;
2359my @ifdeps;
2360
2361# prevent recursion
2362my %read_kconfigs;
2363
2364# taken from streamline_config.pl
2365sub read_kconfig {
2366 my ($kconfig) = @_;
2367
2368 my $state = "NONE";
2369 my $config;
2370 my @kconfigs;
2371
2372 my $cont = 0;
2373 my $line;
2374
2375
2376 if (! -f $kconfig) {
2377 doprint "file $kconfig does not exist, skipping\n";
2378 return;
2379 }
2380
2381 open(KIN, "$kconfig")
2382 or die "Can't open $kconfig";
2383 while (<KIN>) {
2384 chomp;
2385
2386 # Make sure that lines ending with \ continue
2387 if ($cont) {
2388 $_ = $line . " " . $_;
2389 }
2390
2391 if (s/\\$//) {
2392 $cont = 1;
2393 $line = $_;
2394 next;
2395 }
2396
2397 $cont = 0;
2398
2399 # collect any Kconfig sources
2400 if (/^source\s*"(.*)"/) {
2401 $kconfigs[$#kconfigs+1] = $1;
2402 }
2403
2404 # configs found
2405 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2406 $state = "NEW";
2407 $config = $2;
2408
2409 for (my $i = 0; $i < $iflevel; $i++) {
2410 if ($i) {
2411 $depends{$config} .= " " . $ifdeps[$i];
2412 } else {
2413 $depends{$config} = $ifdeps[$i];
2414 }
2415 $state = "DEP";
2416 }
2417
2418 # collect the depends for the config
2419 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2420
2421 if (defined($depends{$1})) {
2422 $depends{$config} .= " " . $1;
2423 } else {
2424 $depends{$config} = $1;
2425 }
2426
2427 # Get the configs that select this config
2428 } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
2429 if (defined($depends{$1})) {
2430 $depends{$1} .= " " . $config;
2431 } else {
2432 $depends{$1} = $config;
2433 }
2434
2435 # Check for if statements
2436 } elsif (/^if\s+(.*\S)\s*$/) {
2437 my $deps = $1;
2438 # remove beginning and ending non text
2439 $deps =~ s/^[^a-zA-Z0-9_]*//;
2440 $deps =~ s/[^a-zA-Z0-9_]*$//;
2441
2442 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2443
2444 $ifdeps[$iflevel++] = join ':', @deps;
2445
2446 } elsif (/^endif/) {
2447
2448 $iflevel-- if ($iflevel);
2449
2450 # stop on "help"
2451 } elsif (/^\s*help\s*$/) {
2452 $state = "NONE";
2453 }
2454 }
2455 close(KIN);
2456
2457 # read in any configs that were found.
2458 foreach $kconfig (@kconfigs) {
2459 if (!defined($read_kconfigs{$kconfig})) {
2460 $read_kconfigs{$kconfig} = 1;
2461 read_kconfig("$builddir/$kconfig");
2462 }
2463 }
2464}
2465
2466sub read_depends {
2467 # find out which arch this is by the kconfig file
2468 open (IN, $output_config)
2469 or dodie "Failed to read $output_config";
2470 my $arch;
2471 while (<IN>) {
2472 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2473 $arch = $1;
2474 last;
2475 }
2476 }
2477 close IN;
2478
2479 if (!defined($arch)) {
2480 doprint "Could not find arch from config file\n";
2481 doprint "no dependencies used\n";
2482 return;
2483 }
2484
2485 # arch is really the subarch, we need to know
2486 # what directory to look at.
2487 if ($arch eq "i386" || $arch eq "x86_64") {
2488 $arch = "x86";
2489 } elsif ($arch =~ /^tile/) {
2490 $arch = "tile";
2491 }
2492
2493 my $kconfig = "$builddir/arch/$arch/Kconfig";
2494
2495 if (! -f $kconfig && $arch =~ /\d$/) {
2496 my $orig = $arch;
2497 # some subarchs have numbers, truncate them
2498 $arch =~ s/\d*$//;
2499 $kconfig = "$builddir/arch/$arch/Kconfig";
2500 if (! -f $kconfig) {
2501 doprint "No idea what arch dir $orig is for\n";
2502 doprint "no dependencies used\n";
2503 return;
2504 }
2505 }
2506
2507 read_kconfig($kconfig);
2508}
2509
4c4ab120
SR
2510sub read_config_list {
2511 my ($config) = @_;
2512
2513 open (IN, $config)
2514 or dodie "Failed to read $config";
2515
2516 while (<IN>) {
2517 if (/^((CONFIG\S*)=.*)/) {
2518 if (!defined($config_ignore{$2})) {
2519 $config_list{$2} = $1;
2520 }
2521 }
2522 }
2523
2524 close(IN);
2525}
2526
2527sub read_output_config {
2528 my ($config) = @_;
2529
2530 assign_configs \%config_ignore, $config;
2531}
2532
2533sub make_new_config {
2534 my @configs = @_;
2535
2536 open (OUT, ">$output_config")
2537 or dodie "Failed to write $output_config";
2538
2539 foreach my $config (@configs) {
2540 print OUT "$config\n";
2541 }
2542 close OUT;
2543}
2544
b9066f6c
SR
2545sub get_depends {
2546 my ($dep) = @_;
2547
2548 my $kconfig = $dep;
2549 $kconfig =~ s/CONFIG_//;
2550
2551 $dep = $depends{"$kconfig"};
2552
2553 # the dep string we have saves the dependencies as they
2554 # were found, including expressions like ! && ||. We
2555 # want to split this out into just an array of configs.
2556
2557 my $valid = "A-Za-z_0-9";
2558
2559 my @configs;
2560
2561 while ($dep =~ /[$valid]/) {
2562
2563 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2564 my $conf = "CONFIG_" . $1;
2565
2566 $configs[$#configs + 1] = $conf;
2567
2568 $dep =~ s/^[^$valid]*[$valid]+//;
2569 } else {
2570 die "this should never happen";
2571 }
2572 }
2573
2574 return @configs;
2575}
2576
2577my %min_configs;
2578my %keep_configs;
43d1b651 2579my %save_configs;
b9066f6c
SR
2580my %processed_configs;
2581my %nochange_config;
2582
2583sub test_this_config {
2584 my ($config) = @_;
2585
2586 my $found;
2587
2588 # if we already processed this config, skip it
2589 if (defined($processed_configs{$config})) {
2590 return undef;
2591 }
2592 $processed_configs{$config} = 1;
2593
2594 # if this config failed during this round, skip it
2595 if (defined($nochange_config{$config})) {
2596 return undef;
2597 }
2598
2599 my $kconfig = $config;
2600 $kconfig =~ s/CONFIG_//;
2601
2602 # Test dependencies first
2603 if (defined($depends{"$kconfig"})) {
2604 my @parents = get_depends $config;
2605 foreach my $parent (@parents) {
2606 # if the parent is in the min config, check it first
2607 next if (!defined($min_configs{$parent}));
2608 $found = test_this_config($parent);
2609 if (defined($found)) {
2610 return $found;
2611 }
2612 }
2613 }
2614
2615 # Remove this config from the list of configs
2616 # do a make oldnoconfig and then read the resulting
2617 # .config to make sure it is missing the config that
2618 # we had before
2619 my %configs = %min_configs;
2620 delete $configs{$config};
2621 make_new_config ((values %configs), (values %keep_configs));
2622 make_oldconfig;
2623 undef %configs;
2624 assign_configs \%configs, $output_config;
2625
2626 return $config if (!defined($configs{$config}));
2627
2628 doprint "disabling config $config did not change .config\n";
2629
2630 $nochange_config{$config} = 1;
2631
2632 return undef;
2633}
2634
4c4ab120
SR
2635sub make_min_config {
2636 my ($i) = @_;
2637
2638 if (!defined($output_minconfig)) {
2639 fail "OUTPUT_MIN_CONFIG not defined" and return;
2640 }
35ce5952
SR
2641
2642 # If output_minconfig exists, and the start_minconfig
2643 # came from min_config, than ask if we should use
2644 # that instead.
2645 if (-f $output_minconfig && !$start_minconfig_defined) {
2646 print "$output_minconfig exists\n";
2647 if (read_yn " Use it as minconfig?") {
2648 $start_minconfig = $output_minconfig;
2649 }
2650 }
2651
4c4ab120
SR
2652 if (!defined($start_minconfig)) {
2653 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2654 }
2655
35ce5952
SR
2656 my $temp_config = "$tmpdir/temp_config";
2657
4c4ab120
SR
2658 # First things first. We build an allnoconfig to find
2659 # out what the defaults are that we can't touch.
2660 # Some are selections, but we really can't handle selections.
2661
2662 my $save_minconfig = $minconfig;
2663 undef $minconfig;
2664
2665 run_command "$make allnoconfig" or return 0;
2666
b9066f6c
SR
2667 read_depends;
2668
4c4ab120 2669 process_config_ignore $output_config;
b9066f6c 2670
43d1b651 2671 undef %save_configs;
b9066f6c 2672 undef %min_configs;
4c4ab120
SR
2673
2674 if (defined($ignore_config)) {
2675 # make sure the file exists
2676 `touch $ignore_config`;
43d1b651 2677 assign_configs \%save_configs, $ignore_config;
4c4ab120
SR
2678 }
2679
43d1b651
SR
2680 %keep_configs = %save_configs;
2681
4c4ab120
SR
2682 doprint "Load initial configs from $start_minconfig\n";
2683
2684 # Look at the current min configs, and save off all the
2685 # ones that were set via the allnoconfig
4c4ab120
SR
2686 assign_configs \%min_configs, $start_minconfig;
2687
2688 my @config_keys = keys %min_configs;
2689
2690 # Remove anything that was set by the make allnoconfig
2691 # we shouldn't need them as they get set for us anyway.
2692 foreach my $config (@config_keys) {
2693 # Remove anything in the ignore_config
2694 if (defined($keep_configs{$config})) {
2695 my $file = $ignore_config;
2696 $file =~ s,.*/(.*?)$,$1,;
2697 doprint "$config set by $file ... ignored\n";
2698 delete $min_configs{$config};
2699 next;
2700 }
2701 # But make sure the settings are the same. If a min config
2702 # sets a selection, we do not want to get rid of it if
2703 # it is not the same as what we have. Just move it into
2704 # the keep configs.
2705 if (defined($config_ignore{$config})) {
2706 if ($config_ignore{$config} ne $min_configs{$config}) {
2707 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
2708 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
2709 $keep_configs{$config} = $min_configs{$config};
2710 } else {
2711 doprint "$config set by allnoconfig ... ignored\n";
2712 }
2713 delete $min_configs{$config};
2714 }
2715 }
2716
4c4ab120 2717 my $done = 0;
b9066f6c 2718 my $take_two = 0;
4c4ab120
SR
2719
2720 while (!$done) {
2721
2722 my $config;
2723 my $found;
2724
2725 # Now disable each config one by one and do a make oldconfig
2726 # till we find a config that changes our list.
2727
2728 # Put configs that did not modify the config at the end.
2729 my @test_configs = keys %min_configs;
2730 my $reset = 1;
2731 for (my $i = 0; $i < $#test_configs; $i++) {
2732 if (!defined($nochange_config{$test_configs[0]})) {
2733 $reset = 0;
2734 last;
2735 }
2736 # This config didn't change the .config last time.
2737 # Place it at the end
2738 my $config = shift @test_configs;
2739 push @test_configs, $config;
2740 }
2741
2742 # if every test config has failed to modify the .config file
2743 # in the past, then reset and start over.
2744 if ($reset) {
2745 undef %nochange_config;
2746 }
2747
b9066f6c
SR
2748 undef %processed_configs;
2749
4c4ab120
SR
2750 foreach my $config (@test_configs) {
2751
b9066f6c 2752 $found = test_this_config $config;
4c4ab120 2753
b9066f6c 2754 last if (defined($found));
4c4ab120
SR
2755
2756 # oh well, try another config
4c4ab120
SR
2757 }
2758
2759 if (!defined($found)) {
b9066f6c
SR
2760 # we could have failed due to the nochange_config hash
2761 # reset and try again
2762 if (!$take_two) {
2763 undef %nochange_config;
2764 $take_two = 1;
2765 next;
2766 }
4c4ab120
SR
2767 doprint "No more configs found that we can disable\n";
2768 $done = 1;
2769 last;
2770 }
b9066f6c 2771 $take_two = 0;
4c4ab120
SR
2772
2773 $config = $found;
2774
2775 doprint "Test with $config disabled\n";
2776
2777 # set in_bisect to keep build and monitor from dieing
2778 $in_bisect = 1;
2779
2780 my $failed = 0;
2781 build "oldconfig";
2782 start_monitor_and_boot or $failed = 1;
2783 end_monitor;
2784
2785 $in_bisect = 0;
2786
2787 if ($failed) {
b9066f6c 2788 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
4c4ab120
SR
2789 # this config is needed, add it to the ignore list.
2790 $keep_configs{$config} = $min_configs{$config};
43d1b651 2791 $save_configs{$config} = $min_configs{$config};
4c4ab120 2792 delete $min_configs{$config};
35ce5952
SR
2793
2794 # update new ignore configs
2795 if (defined($ignore_config)) {
2796 open (OUT, ">$temp_config")
2797 or die "Can't write to $temp_config";
43d1b651
SR
2798 foreach my $config (keys %save_configs) {
2799 print OUT "$save_configs{$config}\n";
35ce5952
SR
2800 }
2801 close OUT;
2802 run_command "mv $temp_config $ignore_config" or
2803 dodie "failed to copy update to $ignore_config";
2804 }
2805
4c4ab120
SR
2806 } else {
2807 # We booted without this config, remove it from the minconfigs.
2808 doprint "$config is not needed, disabling\n";
2809
2810 delete $min_configs{$config};
2811
2812 # Also disable anything that is not enabled in this config
2813 my %configs;
2814 assign_configs \%configs, $output_config;
2815 my @config_keys = keys %min_configs;
2816 foreach my $config (@config_keys) {
2817 if (!defined($configs{$config})) {
2818 doprint "$config is not set, disabling\n";
2819 delete $min_configs{$config};
2820 }
2821 }
2822
2823 # Save off all the current mandidory configs
35ce5952
SR
2824 open (OUT, ">$temp_config")
2825 or die "Can't write to $temp_config";
4c4ab120
SR
2826 foreach my $config (keys %keep_configs) {
2827 print OUT "$keep_configs{$config}\n";
2828 }
2829 foreach my $config (keys %min_configs) {
2830 print OUT "$min_configs{$config}\n";
2831 }
2832 close OUT;
35ce5952
SR
2833
2834 run_command "mv $temp_config $output_minconfig" or
2835 dodie "failed to copy update to $output_minconfig";
4c4ab120
SR
2836 }
2837
2838 doprint "Reboot and wait $sleep_time seconds\n";
2728be41 2839 reboot $sleep_time;
4c4ab120
SR
2840 }
2841
2842 success $i;
2843 return 1;
2844}
2845
8d1491ba
SR
2846$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
2847
2848if ($#ARGV == 0) {
2849 $ktest_config = $ARGV[0];
2850 if (! -f $ktest_config) {
2851 print "$ktest_config does not exist.\n";
35ce5952 2852 if (!read_yn "Create it?") {
8d1491ba
SR
2853 exit 0;
2854 }
2855 }
2856} else {
2857 $ktest_config = "ktest.conf";
2858}
2859
2860if (! -f $ktest_config) {
2861 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
2862 print OUT << "EOF"
2863# Generated by ktest.pl
2864#
2865# Define each test with TEST_START
2866# The config options below it will override the defaults
2867TEST_START
2868
2869DEFAULTS
2870EOF
2871;
2872 close(OUT);
2873}
2874read_config $ktest_config;
2875
23715c3c
SR
2876if (defined($opt{"LOG_FILE"})) {
2877 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
2878}
2879
8d1491ba
SR
2880# Append any configs entered in manually to the config file.
2881my @new_configs = keys %entered_configs;
2882if ($#new_configs >= 0) {
2883 print "\nAppending entered in configs to $ktest_config\n";
2884 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
2885 foreach my $config (@new_configs) {
2886 print OUT "$config = $entered_configs{$config}\n";
2887 $opt{$config} = $entered_configs{$config};
2888 }
2889}
2545eb61 2890
2b7d9b21
SR
2891if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
2892 unlink $opt{"LOG_FILE"};
2893}
2545eb61 2894
2b7d9b21
SR
2895doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
2896
a57419b3
SR
2897for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
2898
2899 if (!$i) {
2900 doprint "DEFAULT OPTIONS:\n";
2901 } else {
2902 doprint "\nTEST $i OPTIONS";
2903 if (defined($repeat_tests{$i})) {
2904 $repeat = $repeat_tests{$i};
2905 doprint " ITERATE $repeat";
2906 }
2907 doprint "\n";
2908 }
2909
2910 foreach my $option (sort keys %opt) {
2911
2912 if ($option =~ /\[(\d+)\]$/) {
2913 next if ($i != $1);
2914 } else {
2915 next if ($i);
2916 }
2917
2918 doprint "$option = $opt{$option}\n";
2919 }
2b7d9b21 2920}
2545eb61 2921
2a62512b 2922sub __set_test_option {
5a391fbf 2923 my ($name, $i) = @_;
2545eb61 2924
5a391fbf 2925 my $option = "$name\[$i\]";
5c42fc5b 2926
5a391fbf
SR
2927 if (defined($opt{$option})) {
2928 return $opt{$option};
5f9b6ced
SR
2929 }
2930
a57419b3
SR
2931 foreach my $test (keys %repeat_tests) {
2932 if ($i >= $test &&
2933 $i < $test + $repeat_tests{$test}) {
2934 $option = "$name\[$test\]";
2935 if (defined($opt{$option})) {
2936 return $opt{$option};
2937 }
2938 }
2939 }
2940
5a391fbf
SR
2941 if (defined($opt{$name})) {
2942 return $opt{$name};
2545eb61
SR
2943 }
2944
5a391fbf
SR
2945 return undef;
2946}
2947
2a62512b
SR
2948sub set_test_option {
2949 my ($name, $i) = @_;
2950
2951 my $option = __set_test_option($name, $i);
2952 return $option if (!defined($option));
2953
23715c3c 2954 return eval_option($option, $i);
2a62512b
SR
2955}
2956
5a391fbf 2957# First we need to do is the builds
a75fecec
SR
2958for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
2959
4ab1cce5
SR
2960 # Do not reboot on failing test options
2961 $no_reboot = 1;
2962
576f627c
SR
2963 $iteration = $i;
2964
a75fecec
SR
2965 my $makecmd = set_test_option("MAKE_CMD", $i);
2966
2967 $machine = set_test_option("MACHINE", $i);
e48c5293 2968 $ssh_user = set_test_option("SSH_USER", $i);
a75fecec
SR
2969 $tmpdir = set_test_option("TMP_DIR", $i);
2970 $outputdir = set_test_option("OUTPUT_DIR", $i);
2971 $builddir = set_test_option("BUILD_DIR", $i);
2972 $test_type = set_test_option("TEST_TYPE", $i);
2973 $build_type = set_test_option("BUILD_TYPE", $i);
2974 $build_options = set_test_option("BUILD_OPTIONS", $i);
0bd6c1a3
SR
2975 $pre_build = set_test_option("PRE_BUILD", $i);
2976 $post_build = set_test_option("POST_BUILD", $i);
2977 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
2978 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
a75fecec 2979 $power_cycle = set_test_option("POWER_CYCLE", $i);
e48c5293 2980 $reboot = set_test_option("REBOOT", $i);
a75fecec
SR
2981 $noclean = set_test_option("BUILD_NOCLEAN", $i);
2982 $minconfig = set_test_option("MIN_CONFIG", $i);
4c4ab120
SR
2983 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
2984 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
2985 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
a75fecec
SR
2986 $run_test = set_test_option("TEST", $i);
2987 $addconfig = set_test_option("ADD_CONFIG", $i);
2988 $reboot_type = set_test_option("REBOOT_TYPE", $i);
2989 $grub_menu = set_test_option("GRUB_MENU", $i);
8b37ca8c 2990 $post_install = set_test_option("POST_INSTALL", $i);
e0a8742e 2991 $no_install = set_test_option("NO_INSTALL", $i);
a75fecec
SR
2992 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
2993 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
2994 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
2995 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
2996 $power_off = set_test_option("POWER_OFF", $i);
576f627c
SR
2997 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
2998 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
a75fecec
SR
2999 $sleep_time = set_test_option("SLEEP_TIME", $i);
3000 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
27d934b2 3001 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
1990207d 3002 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
c960bb9f 3003 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
c23dca7c 3004 $bisect_skip = set_test_option("BISECT_SKIP", $i);
30f75da5 3005 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
a75fecec 3006 $store_failures = set_test_option("STORE_FAILURES", $i);
9064af52 3007 $test_name = set_test_option("TEST_NAME", $i);
a75fecec
SR
3008 $timeout = set_test_option("TIMEOUT", $i);
3009 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3010 $console = set_test_option("CONSOLE", $i);
f1a5b962 3011 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
a75fecec 3012 $success_line = set_test_option("SUCCESS_LINE", $i);
2b803365 3013 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
1c8a617a
SR
3014 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3015 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
2d01b26a 3016 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
a75fecec 3017 $build_target = set_test_option("BUILD_TARGET", $i);
e48c5293
SR
3018 $ssh_exec = set_test_option("SSH_EXEC", $i);
3019 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
a75fecec
SR
3020 $target_image = set_test_option("TARGET_IMAGE", $i);
3021 $localversion = set_test_option("LOCALVERSION", $i);
3022
35ce5952
SR
3023 $start_minconfig_defined = 1;
3024
4c4ab120 3025 if (!defined($start_minconfig)) {
35ce5952 3026 $start_minconfig_defined = 0;
4c4ab120
SR
3027 $start_minconfig = $minconfig;
3028 }
3029
a75fecec
SR
3030 chdir $builddir || die "can't change directory to $builddir";
3031
a908a665
AJ
3032 foreach my $dir ($tmpdir, $outputdir) {
3033 if (!-d $dir) {
3034 mkpath($dir) or
3035 die "can't create $dir";
3036 }
a75fecec 3037 }
1a5cfce3 3038
e48c5293
SR
3039 $ENV{"SSH_USER"} = $ssh_user;
3040 $ENV{"MACHINE"} = $machine;
3041
a75fecec
SR
3042 $target = "$ssh_user\@$machine";
3043
3044 $buildlog = "$tmpdir/buildlog-$machine";
3045 $dmesg = "$tmpdir/dmesg-$machine";
3046 $make = "$makecmd O=$outputdir";
51ad1dd1 3047 $output_config = "$outputdir/.config";
a75fecec
SR
3048
3049 if ($reboot_type eq "grub") {
576f627c 3050 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
a75fecec 3051 } elsif (!defined($reboot_script)) {
576f627c 3052 dodie "REBOOT_SCRIPT not defined"
a75fecec
SR
3053 }
3054
3055 my $run_type = $build_type;
3056 if ($test_type eq "patchcheck") {
3057 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3058 } elsif ($test_type eq "bisect") {
3059 $run_type = $opt{"BISECT_TYPE[$i]"};
0a05c769
SR
3060 } elsif ($test_type eq "config_bisect") {
3061 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
a75fecec
SR
3062 }
3063
4c4ab120
SR
3064 if ($test_type eq "make_min_config") {
3065 $run_type = "";
3066 }
3067
a75fecec
SR
3068 # mistake in config file?
3069 if (!defined($run_type)) {
3070 $run_type = "ERROR";
3071 }
5a391fbf 3072
e0a8742e
SR
3073 my $installme = "";
3074 $installme = " no_install" if ($no_install);
3075
2545eb61 3076 doprint "\n\n";
e0a8742e 3077 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
7faafbd6
SR
3078
3079 unlink $dmesg;
3080 unlink $buildlog;
2545eb61 3081
250bae8b
SR
3082 if (defined($addconfig)) {
3083 my $min = $minconfig;
3084 if (!defined($minconfig)) {
3085 $min = "";
3086 }
3087 run_command "cat $addconfig $min > $tmpdir/add_config" or
2b7d9b21 3088 dodie "Failed to create temp config";
9be2e6b5 3089 $minconfig = "$tmpdir/add_config";
2b7d9b21
SR
3090 }
3091
6c5ee0be
SR
3092 my $checkout = $opt{"CHECKOUT[$i]"};
3093 if (defined($checkout)) {
3094 run_command "git checkout $checkout" or
3095 die "failed to checkout $checkout";
3096 }
3097
4ab1cce5
SR
3098 $no_reboot = 0;
3099
3100
a75fecec 3101 if ($test_type eq "bisect") {
5f9b6ced
SR
3102 bisect $i;
3103 next;
0a05c769
SR
3104 } elsif ($test_type eq "config_bisect") {
3105 config_bisect $i;
3106 next;
a75fecec 3107 } elsif ($test_type eq "patchcheck") {
6c5ee0be
SR
3108 patchcheck $i;
3109 next;
4c4ab120
SR
3110 } elsif ($test_type eq "make_min_config") {
3111 make_min_config $i;
3112 next;
2545eb61 3113 }
2545eb61 3114
7faafbd6
SR
3115 if ($build_type ne "nobuild") {
3116 build $build_type or next;
2545eb61
SR
3117 }
3118
cd8e368f
SR
3119 if ($test_type eq "install") {
3120 get_version;
3121 install;
3122 success $i;
3123 next;
3124 }
3125
a75fecec 3126 if ($test_type ne "build") {
a75fecec 3127 my $failed = 0;
ddf607e5 3128 start_monitor_and_boot or $failed = 1;
a75fecec
SR
3129
3130 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3131 do_run_test or $failed = 1;
3132 }
3133 end_monitor;
3134 next if ($failed);
5a391fbf
SR
3135 }
3136
5f9b6ced 3137 success $i;
2545eb61
SR
3138}
3139
5c42fc5b 3140if ($opt{"POWEROFF_ON_SUCCESS"}) {
75c3fda7 3141 halt;
576f627c 3142} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
75c3fda7 3143 reboot;
5c42fc5b 3144}
75c3fda7 3145
e48c5293
SR
3146doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3147
2545eb61 3148exit 0;
This page took 0.229165 seconds and 5 git commands to generate.