Skip to content

Commit 9d2f7f0

Browse files
jpoimboerostedt
authored andcommitted
ktest: Enable user input to the console
Allow the user to send input to the console by putting the terminal in cbreak mode (to allow reading stdin one character at a time) and copying all stdin data to the console's pty. Link: http://lkml.kernel.org/r/bb1bbe7d202c95a3ce7894cfffdd8c725875978e.1422473610.git.jpoimboe@redhat.com Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 9f2cdcb commit 9d2f7f0

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

‎tools/testing/ktest/ktest.pl‎

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
my $localversion;
179179
my $iteration = 0;
180180
my $successes = 0;
181-
my $stty;
181+
my $stty_orig;
182182

183183
my $bisect_good;
184184
my $bisect_bad;
@@ -1458,7 +1458,11 @@ sub open_console {
14581458
my $pid;
14591459

14601460
# save terminal settings
1461-
$stty = `stty -g`;
1461+
$stty_orig = `stty -g`;
1462+
1463+
# place terminal in cbreak mode so that stdin can be read one character at
1464+
# a time without having to wait for a newline
1465+
system("stty -icanon -echo -icrnl");
14621466

14631467
create_pty($ptm, $pts);
14641468

@@ -1487,7 +1491,7 @@ sub close_console {
14871491
close($fp);
14881492

14891493
# restore terminal settings
1490-
system("stty $stty");
1494+
system("stty $stty_orig");
14911495
}
14921496

14931497
sub start_monitor {
@@ -1827,7 +1831,9 @@ sub wait_for_input
18271831
{
18281832
my ($fp, $time) = @_;
18291833
my $rin;
1830-
my $ready;
1834+
my $rout;
1835+
my $nr;
1836+
my $buf;
18311837
my $line;
18321838
my $ch;
18331839

@@ -1837,21 +1843,36 @@ sub wait_for_input
18371843

18381844
$rin = '';
18391845
vec($rin, fileno($fp), 1) = 1;
1840-
($ready, $time) = select($rin, undef, undef, $time);
1846+
vec($rin, fileno(\*STDIN), 1) = 1;
18411847

1842-
$line = "";
1848+
while (1) {
1849+
$nr = select($rout=$rin, undef, undef, $time);
18431850

1844-
# try to read one char at a time
1845-
while (sysread $fp, $ch, 1) {
1846-
$line .= $ch;
1847-
last if ($ch eq "\n");
1848-
}
1851+
if ($nr <= 0) {
1852+
return undef;
1853+
}
18491854

1850-
if (!length($line)) {
1851-
return undef;
1852-
}
1855+
# copy data from stdin to the console
1856+
if (vec($rout, fileno(\*STDIN), 1) == 1) {
1857+
sysread(\*STDIN, $buf, 1000);
1858+
syswrite($fp, $buf, 1000);
1859+
next;
1860+
}
18531861

1854-
return $line;
1862+
$line = "";
1863+
1864+
# try to read one char at a time
1865+
while (sysread $fp, $ch, 1) {
1866+
$line .= $ch;
1867+
last if ($ch eq "\n");
1868+
}
1869+
1870+
if (!length($line)) {
1871+
return undef;
1872+
}
1873+
1874+
return $line;
1875+
}
18551876
}
18561877

18571878
sub reboot_to {

0 commit comments

Comments
 (0)