mirror of
https://github.com/containers/podman.git
synced 2025-06-22 01:48:54 +08:00
Fix terminal attach
Signed-off-by: Matthew Heon <matthew.heon@gmail.com> Closes: #608 Approved by: baude
This commit is contained in:
@ -69,16 +69,12 @@ func attachCmd(c *cli.Context) error {
|
||||
return errors.Errorf("you can only attach to running containers")
|
||||
}
|
||||
|
||||
if c.BoolT("sig-proxy") {
|
||||
ProxySignals(ctr)
|
||||
}
|
||||
|
||||
inputStream := os.Stdin
|
||||
if c.Bool("no-stdin") {
|
||||
inputStream = nil
|
||||
}
|
||||
|
||||
if err := attachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys")); err != nil {
|
||||
if err := attachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy")); err != nil {
|
||||
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
|
||||
}
|
||||
|
||||
|
@ -179,8 +179,7 @@ func runCmd(c *cli.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
attachChan, err := startAttachCtr(ctr, outputStream, errorStream, inputStream, c.String("detach-keys"))
|
||||
if err != nil {
|
||||
if err := startAttachCtr(ctr, outputStream, errorStream, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy")); err != nil {
|
||||
// This means the command did not exist
|
||||
exitCode = 127
|
||||
if strings.Index(err.Error(), "permission denied") > -1 {
|
||||
@ -189,16 +188,6 @@ func runCmd(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.BoolT("sig-proxy") {
|
||||
ProxySignals(ctr)
|
||||
}
|
||||
|
||||
// Wait for attach to complete
|
||||
err = <-attachChan
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
|
||||
}
|
||||
|
||||
if ecode, err := ctr.ExitCode(); err != nil {
|
||||
logrus.Errorf("unable to get exit code of container %s: %q", ctr.ID(), err)
|
||||
} else {
|
||||
|
@ -110,21 +110,10 @@ func startCmd(c *cli.Context) error {
|
||||
inputStream = nil
|
||||
}
|
||||
|
||||
attachChan, err := startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"))
|
||||
if err != nil {
|
||||
if err := startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.Bool("sig-proxy")); err != nil {
|
||||
return errors.Wrapf(err, "unable to start container %s", ctr.ID())
|
||||
}
|
||||
|
||||
if c.Bool("sig-proxy") {
|
||||
ProxySignals(ctr)
|
||||
}
|
||||
|
||||
// Wait for attach to complete
|
||||
err = <-attachChan
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
|
||||
}
|
||||
|
||||
if ecode, err := ctr.ExitCode(); err != nil {
|
||||
logrus.Errorf("unable to get exit code of container %s: %q", ctr.ID(), err)
|
||||
} else {
|
||||
|
@ -65,8 +65,9 @@ func getRuntime(c *cli.Context) (*libpod.Runtime, error) {
|
||||
}
|
||||
|
||||
// Attach to a container
|
||||
func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string) error {
|
||||
func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool) error {
|
||||
resize := make(chan remotecommand.TerminalSize)
|
||||
defer close(resize)
|
||||
|
||||
haveTerminal := terminal.IsTerminal(int(os.Stdin.Fd()))
|
||||
|
||||
@ -87,12 +88,17 @@ func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys
|
||||
defer term.RestoreTerminal(os.Stdin.Fd(), oldTermState)
|
||||
}
|
||||
|
||||
if sigProxy {
|
||||
ProxySignals(ctr)
|
||||
}
|
||||
|
||||
return ctr.Attach(stdout, stderr, stdin, detachKeys, resize)
|
||||
}
|
||||
|
||||
// Start and attach to a container
|
||||
func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string) (<-chan error, error) {
|
||||
func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool) error {
|
||||
resize := make(chan remotecommand.TerminalSize)
|
||||
defer close(resize)
|
||||
|
||||
haveTerminal := terminal.IsTerminal(int(os.Stdin.Fd()))
|
||||
|
||||
@ -105,7 +111,7 @@ func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detac
|
||||
|
||||
oldTermState, err := term.SaveState(os.Stdin.Fd())
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to save terminal state")
|
||||
return errors.Wrapf(err, "unable to save terminal state")
|
||||
}
|
||||
|
||||
term.SetRawTerminal(os.Stdin.Fd())
|
||||
@ -113,7 +119,21 @@ func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detac
|
||||
defer term.RestoreTerminal(os.Stdin.Fd(), oldTermState)
|
||||
}
|
||||
|
||||
return ctr.StartAndAttach(stdout, stderr, stdin, detachKeys, resize)
|
||||
attachChan, err := ctr.StartAndAttach(stdout, stderr, stdin, detachKeys, resize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if sigProxy {
|
||||
ProxySignals(ctr)
|
||||
}
|
||||
|
||||
err = <-attachChan
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Helper for prepareAttach - set up a goroutine to generate terminal resize events
|
||||
|
Reference in New Issue
Block a user