Skip to content

Commit f7c6401

Browse files
committed
ktest: Make sure wait_for_input does honor the timeout
The function wait_for_input takes in a timeout, and even has a default timeout. But if for some reason the STDIN descriptor keeps sending in data, the function will never time out. The timout is to wait for the data from the passed in file descriptor, not for STDIN. Adding a test in the case where there's no data from the passed in file descriptor that checks to see if the timeout passed, will ensure that it will timeout properly even if there's input in STDIN. Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent 99c014a commit f7c6401

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

tools/testing/ktest/ktest.pl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,7 @@ sub get_grub_index {
18801880
sub wait_for_input
18811881
{
18821882
my ($fp, $time) = @_;
1883+
my $start_time;
18831884
my $rin;
18841885
my $rout;
18851886
my $nr;
@@ -1895,20 +1896,24 @@ sub wait_for_input
18951896
vec($rin, fileno($fp), 1) = 1;
18961897
vec($rin, fileno(\*STDIN), 1) = 1;
18971898

1899+
$start_time = time;
1900+
18981901
while (1) {
18991902
$nr = select($rout=$rin, undef, undef, $time);
19001903

1901-
if ($nr <= 0) {
1902-
return undef;
1903-
}
1904+
last if ($nr <= 0);
19041905

19051906
# copy data from stdin to the console
19061907
if (vec($rout, fileno(\*STDIN), 1) == 1) {
19071908
$nr = sysread(\*STDIN, $buf, 1000);
19081909
syswrite($fp, $buf, $nr) if ($nr > 0);
19091910
}
19101911

1911-
next if (vec($rout, fileno($fp), 1) != 1);
1912+
# The timeout is based on time waiting for the fp data
1913+
if (vec($rout, fileno($fp), 1) != 1) {
1914+
last if (defined($time) && (time - $start_time > $time));
1915+
next;
1916+
}
19121917

19131918
$line = "";
19141919

@@ -1918,12 +1923,11 @@ sub wait_for_input
19181923
last if ($ch eq "\n");
19191924
}
19201925

1921-
if (!length($line)) {
1922-
return undef;
1923-
}
1926+
last if (!length($line));
19241927

19251928
return $line;
19261929
}
1930+
return undef;
19271931
}
19281932

19291933
sub reboot_to {

0 commit comments

Comments
 (0)