[lxc-devel] [lxd/master] lxc/exec: Cleanup terminal logic
stgraber on Github
lxc-bot at linuxcontainers.org
Tue Feb 26 10:38:20 UTC 2019
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190226/9824fb9c/attachment.bin>
-------------- next part --------------
From 1a22b1956d85c501573f7446a9203359bafae958 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 26 Feb 2019 09:12:04 +0100
Subject: [PATCH] lxc/exec: Cleanup terminal logic
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
lxc/exec.go | 23 ++++++++++++++++-------
1 file changed, 16 insertions(+), 7 deletions(-)
diff --git a/lxc/exec.go b/lxc/exec.go
index b130e81a9d..28610e71fd 100644
--- a/lxc/exec.go
+++ b/lxc/exec.go
@@ -154,8 +154,13 @@ func (c *cmdExec) Run(cmd *cobra.Command, args []string) error {
}
// Configure the terminal
- cfd := int(syscall.Stdin)
+ stdinFd := int(syscall.Stdin)
+ stdoutFd := int(syscall.Stdout)
+ stdinTerminal := termios.IsTerminal(stdinFd)
+ stdoutTerminal := termios.IsTerminal(stdoutFd)
+
+ // Determine interaction mode
var interactive bool
if c.flagDisableStdin {
interactive = false
@@ -164,25 +169,29 @@ func (c *cmdExec) Run(cmd *cobra.Command, args []string) error {
} else if c.flagMode == "non-interactive" || c.flagForceNonInteractive {
interactive = false
} else {
- interactive = termios.IsTerminal(cfd) && termios.IsTerminal(int(syscall.Stdout))
+ interactive = stdinTerminal && stdoutTerminal
}
+ // Record terminal state
var oldttystate *termios.State
- if interactive {
- oldttystate, err = termios.MakeRaw(cfd)
+ if interactive && stdinTerminal {
+ oldttystate, err = termios.MakeRaw(stdinFd)
if err != nil {
return err
}
- defer termios.Restore(cfd, oldttystate)
+
+ defer termios.Restore(stdinFd, oldttystate)
}
+ // Setup interactive console handler
handler := c.controlSocketHandler
if !interactive {
handler = nil
}
+ // Grab current terminal dimensions
var width, height int
- if interactive {
+ if stdoutTerminal {
width, height, err = termios.GetSize(int(syscall.Stdout))
if err != nil {
return err
@@ -239,7 +248,7 @@ func (c *cmdExec) Run(cmd *cobra.Command, args []string) error {
* Additionally, since os.Exit() exits without running deferred
* functions, we restore the terminal explicitly.
*/
- termios.Restore(cfd, oldttystate)
+ termios.Restore(stdinFd, oldttystate)
}
os.Exit(int(opAPI.Metadata["return"].(float64)))
More information about the lxc-devel
mailing list