All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ktest: restore tty settings after closing console
@ 2015-01-27 18:10 Josh Poimboeuf
  2015-01-27 22:01 ` Steven Rostedt
  0 siblings, 1 reply; 5+ messages in thread
From: Josh Poimboeuf @ 2015-01-27 18:10 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: Satoru Takeuchi, linux-kernel

When ktest runs the console program as a child process, the parent and
child share the same tty for stdin and stderr.  This is problematic when
using a libvirt target.  The "virsh console" program makes a lot of
changes to the tty settings, making ktest's output hard to read
(carriage returns don't work).  After ktest exits, the terminal is
unusable (CRs broken, stdin isn't echoed).

I think the best way to fix this issue would be to create a
pseudoterminal (pty pair) so the child process would have a dedicated
tty, and then use pipes to connect the two ttys.  I'm not sure if that's
overkill, but it's far beyond my current Perl abilities.

This patch is a much easier way to (partially) fix this issue.  It saves
the tty settings before opening the console and restores them after
closing it.  There are still a few places where ktest prints mangled
output while the console is open, but the output is much more legible
overall, and the terminal works just fine after ktest exits.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
---
 tools/testing/ktest/ktest.pl | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
index b9cd036..ba20f89 100755
--- a/tools/testing/ktest/ktest.pl
+++ b/tools/testing/ktest/ktest.pl
@@ -178,6 +178,7 @@ my $checkout;
 my $localversion;
 my $iteration = 0;
 my $successes = 0;
+my $stty;
 
 my $bisect_good;
 my $bisect_bad;
@@ -1349,6 +1350,9 @@ sub open_console {
 
     my $flags;
 
+    # save terminal settings
+    $stty = `stty -g`;
+
     my $pid = open($fp, "$console|") or
 	dodie "Can't open console $console";
 
@@ -1368,6 +1372,9 @@ sub close_console {
 
     print "closing!\n";
     close($fp);
+
+    # restore terminal settings
+    system("stty $stty");
 }
 
 sub start_monitor {
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] ktest: restore tty settings after closing console
  2015-01-27 18:10 [PATCH] ktest: restore tty settings after closing console Josh Poimboeuf
@ 2015-01-27 22:01 ` Steven Rostedt
  2015-01-28 14:59   ` Josh Poimboeuf
  0 siblings, 1 reply; 5+ messages in thread
From: Steven Rostedt @ 2015-01-27 22:01 UTC (permalink / raw)
  To: Josh Poimboeuf; +Cc: Satoru Takeuchi, linux-kernel

On Tue, 27 Jan 2015 12:10:04 -0600
Josh Poimboeuf <jpoimboe@redhat.com> wrote:

> When ktest runs the console program as a child process, the parent and
> child share the same tty for stdin and stderr.  This is problematic when
> using a libvirt target.  The "virsh console" program makes a lot of
> changes to the tty settings, making ktest's output hard to read
> (carriage returns don't work).  After ktest exits, the terminal is
> unusable (CRs broken, stdin isn't echoed).
> 
> I think the best way to fix this issue would be to create a
> pseudoterminal (pty pair) so the child process would have a dedicated
> tty, and then use pipes to connect the two ttys.  I'm not sure if that's
> overkill, but it's far beyond my current Perl abilities.

And beyond mine too. I tried to get that right a few times, and it
never worked out. Maybe someone with more oyster skilz can fix this.

> 
> This patch is a much easier way to (partially) fix this issue.  It saves
> the tty settings before opening the console and restores them after
> closing it.  There are still a few places where ktest prints mangled
> output while the console is open, but the output is much more legible
> overall, and the terminal works just fine after ktest exits.

Thanks, I'll add this, and make sure that it doesn't break anything, as
I use ktest daily.

-- Steve

> 
> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
> ---
>  tools/testing/ktest/ktest.pl | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
> index b9cd036..ba20f89 100755
> --- a/tools/testing/ktest/ktest.pl
> +++ b/tools/testing/ktest/ktest.pl
> @@ -178,6 +178,7 @@ my $checkout;
>  my $localversion;
>  my $iteration = 0;
>  my $successes = 0;
> +my $stty;
>  
>  my $bisect_good;
>  my $bisect_bad;
> @@ -1349,6 +1350,9 @@ sub open_console {
>  
>      my $flags;
>  
> +    # save terminal settings
> +    $stty = `stty -g`;
> +
>      my $pid = open($fp, "$console|") or
>  	dodie "Can't open console $console";
>  
> @@ -1368,6 +1372,9 @@ sub close_console {
>  
>      print "closing!\n";
>      close($fp);
> +
> +    # restore terminal settings
> +    system("stty $stty");
>  }
>  
>  sub start_monitor {


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ktest: restore tty settings after closing console
  2015-01-27 22:01 ` Steven Rostedt
@ 2015-01-28 14:59   ` Josh Poimboeuf
  2015-01-28 15:08     ` Steven Rostedt
  0 siblings, 1 reply; 5+ messages in thread
From: Josh Poimboeuf @ 2015-01-28 14:59 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: Satoru Takeuchi, linux-kernel

On Tue, Jan 27, 2015 at 05:01:29PM -0500, Steven Rostedt wrote:
> On Tue, 27 Jan 2015 12:10:04 -0600
> Josh Poimboeuf <jpoimboe@redhat.com> wrote:
> 
> > When ktest runs the console program as a child process, the parent and
> > child share the same tty for stdin and stderr.  This is problematic when
> > using a libvirt target.  The "virsh console" program makes a lot of
> > changes to the tty settings, making ktest's output hard to read
> > (carriage returns don't work).  After ktest exits, the terminal is
> > unusable (CRs broken, stdin isn't echoed).
> > 
> > I think the best way to fix this issue would be to create a
> > pseudoterminal (pty pair) so the child process would have a dedicated
> > tty, and then use pipes to connect the two ttys.  I'm not sure if that's
> > overkill, but it's far beyond my current Perl abilities.
> 
> And beyond mine too. I tried to get that right a few times, and it
> never worked out. Maybe someone with more oyster skilz can fix this.

I decided to level up my oyster skilz.  I came up with this (to be
applied instead of the original patch).  It's working well for me.
Right now, stdin isn't hooked up, so the user can't mess with the
console.  Is stdin needed?

---8<---

Subject: [PATCH] ktest: give console process a dedicated tty

ktest's sharing of its tty with its child console monitoring process
creates problems when the console program changes tty settings.  In the
case of "virsh console", it makes ktest's output unreadable and makes
the post-ktest terminal unusable (carriage returns broken, stdin not
echoed).

Create a pseudoterminal (pty pair) to give the console a dedicated tty
so it doesn't mess up ktest's tty.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
---
 tools/testing/ktest/ktest.pl | 64 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 56 insertions(+), 8 deletions(-)

diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
index b9cd036..5886169 100755
--- a/tools/testing/ktest/ktest.pl
+++ b/tools/testing/ktest/ktest.pl
@@ -1344,20 +1344,68 @@ sub dodie {
     die @_, "\n";
 }
 
-sub open_console {
-    my ($fp) = @_;
+sub create_pty {
+    my ($ptm, $pts) = @_;
+    my $tmp;
+    my $TIOCSPTLCK = 0x40045431;
+    my $TIOCGPTN = 0x80045430;
+
+    sysopen($ptm, "/dev/ptmx", O_RDWR | O_NONBLOCK) or
+	dodie "Cant open /dev/ptmx";
+
+    # unlockpt()
+    $tmp = pack("i", 0);
+    ioctl($ptm, $TIOCSPTLCK, $tmp) or
+	dodie "ioctl TIOCSPTLCK for /dev/ptmx failed";
+
+    # ptsname()
+    ioctl($ptm, $TIOCGPTN, $tmp) or
+	dodie "ioctl TIOCGPTN for /dev/ptmx failed";
+    $tmp = unpack("i", $tmp);
+
+    sysopen($pts, "/dev/pts/$tmp", O_RDWR | O_NONBLOCK) or
+	dodie "Can't open /dev/pts/$tmp";
+}
+
+sub exec_console {
+    my ($ptm, $pts) = @_;
+
+    close($ptm);
 
-    my $flags;
+    close(\*STDIN);
+    close(\*STDOUT);
+    close(\*STDERR);
 
-    my $pid = open($fp, "$console|") or
+    open(\*STDIN, '<&', $pts);
+    open(\*STDOUT, '>&', $pts);
+    open(\*STDERR, '>&', $pts);
+
+    close($pts);
+
+    exec $console or
 	dodie "Can't open console $console";
+}
 
-    $flags = fcntl($fp, F_GETFL, 0) or
-	dodie "Can't get flags for the socket: $!";
-    $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
-	dodie "Can't set flags for the socket: $!";
+sub open_console {
+    my ($ptm) = @_;
+    my $pts = \*PTSFD;
+    my $pid;
+
+    create_pty($ptm, $pts);
+
+    $pid = fork;
+
+    if (!$pid) {
+	# child
+	exec_console($ptm, $pts)
+    }
+
+    # parent
+    close($pts);
 
     return $pid;
+
+    open(PTSFD, "Stop perl from warning about single use of PTSFD");
 }
 
 sub close_console {
-- 
2.1.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] ktest: restore tty settings after closing console
  2015-01-28 14:59   ` Josh Poimboeuf
@ 2015-01-28 15:08     ` Steven Rostedt
  2015-01-28 15:15       ` Josh Poimboeuf
  0 siblings, 1 reply; 5+ messages in thread
From: Steven Rostedt @ 2015-01-28 15:08 UTC (permalink / raw)
  To: Josh Poimboeuf; +Cc: Satoru Takeuchi, linux-kernel

On Wed, 28 Jan 2015 08:59:23 -0600
Josh Poimboeuf <jpoimboe@redhat.com> wrote:

> On Tue, Jan 27, 2015 at 05:01:29PM -0500, Steven Rostedt wrote:
> > On Tue, 27 Jan 2015 12:10:04 -0600
> > Josh Poimboeuf <jpoimboe@redhat.com> wrote:
> > 
> > > When ktest runs the console program as a child process, the parent and
> > > child share the same tty for stdin and stderr.  This is problematic when
> > > using a libvirt target.  The "virsh console" program makes a lot of
> > > changes to the tty settings, making ktest's output hard to read
> > > (carriage returns don't work).  After ktest exits, the terminal is
> > > unusable (CRs broken, stdin isn't echoed).
> > > 
> > > I think the best way to fix this issue would be to create a
> > > pseudoterminal (pty pair) so the child process would have a dedicated
> > > tty, and then use pipes to connect the two ttys.  I'm not sure if that's
> > > overkill, but it's far beyond my current Perl abilities.
> > 
> > And beyond mine too. I tried to get that right a few times, and it
> > never worked out. Maybe someone with more oyster skilz can fix this.
> 
> I decided to level up my oyster skilz.  I came up with this (to be
> applied instead of the original patch).  It's working well for me.

I already applied your other patch. If there's any other work, please
apply it on top. I in the process of testing other changes on top of
this too. I'll be pushing it soon to my for-next linux-ktest.git tree,
after I'm finished testing.


> Right now, stdin isn't hooked up, so the user can't mess with the
> console.  Is stdin needed?

Actually, I do. I found it's nice to move things along when the console
shows grub or syslinux options, and I can hit enter and it continues.

It's not critical, but it's a freebee for me.

-- Steve


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ktest: restore tty settings after closing console
  2015-01-28 15:08     ` Steven Rostedt
@ 2015-01-28 15:15       ` Josh Poimboeuf
  0 siblings, 0 replies; 5+ messages in thread
From: Josh Poimboeuf @ 2015-01-28 15:15 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: Satoru Takeuchi, linux-kernel

On Wed, Jan 28, 2015 at 10:08:31AM -0500, Steven Rostedt wrote:
> On Wed, 28 Jan 2015 08:59:23 -0600
> Josh Poimboeuf <jpoimboe@redhat.com> wrote:
> 
> > On Tue, Jan 27, 2015 at 05:01:29PM -0500, Steven Rostedt wrote:
> > > On Tue, 27 Jan 2015 12:10:04 -0600
> > > Josh Poimboeuf <jpoimboe@redhat.com> wrote:
> > > 
> > > > When ktest runs the console program as a child process, the parent and
> > > > child share the same tty for stdin and stderr.  This is problematic when
> > > > using a libvirt target.  The "virsh console" program makes a lot of
> > > > changes to the tty settings, making ktest's output hard to read
> > > > (carriage returns don't work).  After ktest exits, the terminal is
> > > > unusable (CRs broken, stdin isn't echoed).
> > > > 
> > > > I think the best way to fix this issue would be to create a
> > > > pseudoterminal (pty pair) so the child process would have a dedicated
> > > > tty, and then use pipes to connect the two ttys.  I'm not sure if that's
> > > > overkill, but it's far beyond my current Perl abilities.
> > > 
> > > And beyond mine too. I tried to get that right a few times, and it
> > > never worked out. Maybe someone with more oyster skilz can fix this.
> > 
> > I decided to level up my oyster skilz.  I came up with this (to be
> > applied instead of the original patch).  It's working well for me.
> 
> I already applied your other patch. If there's any other work, please
> apply it on top. I in the process of testing other changes on top of
> this too. I'll be pushing it soon to my for-next linux-ktest.git tree,
> after I'm finished testing.
> 
> 
> > Right now, stdin isn't hooked up, so the user can't mess with the
> > console.  Is stdin needed?
> 
> Actually, I do. I found it's nice to move things along when the console
> shows grub or syslinux options, and I can hit enter and it continues.
> 
> It's not critical, but it's a freebee for me.

Ok, I'll add stdin for v2.

-- 
Josh

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-01-29  3:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-27 18:10 [PATCH] ktest: restore tty settings after closing console Josh Poimboeuf
2015-01-27 22:01 ` Steven Rostedt
2015-01-28 14:59   ` Josh Poimboeuf
2015-01-28 15:08     ` Steven Rostedt
2015-01-28 15:15       ` Josh Poimboeuf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.