Skip to content

Commit

Permalink
Merge pull request openshift#99 from crosbymichael/improve-term-handling
Browse files Browse the repository at this point in the history
Remove terminal handling in libcontainer
  • Loading branch information
vmarmol committed Jul 16, 2014
2 parents fb3d909 + b56aa06 commit b30e0e1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 157 deletions.
3 changes: 3 additions & 0 deletions console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,16 @@ func CreateMasterAndConsole() (*os.File, string, error) {
if err != nil {
return nil, "", err
}

console, err := Ptsname(master)
if err != nil {
return nil, "", err
}

if err := Unlockpt(master); err != nil {
return nil, "", err
}

return master, console, nil
}

Expand Down
27 changes: 9 additions & 18 deletions namespaces/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package namespaces

import (
"io"
"os"
"os/exec"
"syscall"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/docker/libcontainer/cgroups"
"github.com/docker/libcontainer/cgroups/fs"
"github.com/docker/libcontainer/cgroups/systemd"
consolePkg "github.com/docker/libcontainer/console"
"github.com/docker/libcontainer/network"
"github.com/docker/libcontainer/syncpipe"
"github.com/docker/libcontainer/system"
Expand All @@ -21,11 +21,9 @@ import (
// Move this to libcontainer package.
// Exec performs setup outside of a namespace so that a container can be
// executed. Exec is a high level function for working with container namespaces.
func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) {
func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Writer, console string, rootfs, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) {
var (
master *os.File
console string
err error
err error
)

// create a pipe so that we can syncronize with the namespaced process and
Expand All @@ -36,20 +34,13 @@ func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string
}
defer syncPipe.Close()

if container.Tty {
master, console, err = consolePkg.CreateMasterAndConsole()
if err != nil {
return -1, err
}
term.SetMaster(master)
}

command := createCommand(container, console, rootfs, dataPath, os.Args[0], syncPipe.Child(), args)

if err := term.Attach(command); err != nil {
return -1, err
}
defer term.Close()
// Note: these are only used in non-tty mode
// if there is a tty for the container it will be opened within the namespace and the
// fds will be duped to stdin, stdiout, and stderr
command.Stdin = stdin
command.Stdout = stdout
command.Stderr = stderr

if err := command.Start(); err != nil {
return -1, err
Expand Down
49 changes: 0 additions & 49 deletions namespaces/std_term.go

This file was deleted.

29 changes: 0 additions & 29 deletions namespaces/term.go

This file was deleted.

56 changes: 0 additions & 56 deletions namespaces/tty_term.go

This file was deleted.

66 changes: 61 additions & 5 deletions nsinit/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package nsinit

import (
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"syscall"

"github.com/codegangsta/cli"
"github.com/docker/libcontainer"
consolepkg "github.com/docker/libcontainer/console"
"github.com/docker/libcontainer/namespaces"
"github.com/dotcloud/docker/pkg/term"
)

var execCommand = cli.Command{
Expand All @@ -34,8 +38,7 @@ func execAction(context *cli.Context) {
if state != nil {
err = namespaces.ExecIn(container, state, []string(context.Args()))
} else {
term := namespaces.NewTerminal(os.Stdin, os.Stdout, os.Stderr, container.Tty)
exitCode, err = startContainer(container, term, dataPath, []string(context.Args()))
exitCode, err = startContainer(container, dataPath, []string(context.Args()))
}

if err != nil {
Expand All @@ -49,7 +52,7 @@ func execAction(context *cli.Context) {
// error.
//
// Signals sent to the current process will be forwarded to container.
func startContainer(container *libcontainer.Config, term namespaces.Terminal, dataPath string, args []string) (int, error) {
func startContainer(container *libcontainer.Config, dataPath string, args []string) (int, error) {
var (
cmd *exec.Cmd
sigc = make(chan os.Signal, 10)
Expand All @@ -65,13 +68,66 @@ func startContainer(container *libcontainer.Config, term namespaces.Terminal, da
return cmd
}

var (
master *os.File
console string
err error

stdin = os.Stdin
stdout = os.Stdout
stderr = os.Stderr
)

if container.Tty {
stdin = nil
stdout = nil
stderr = nil

master, console, err = consolepkg.CreateMasterAndConsole()
if err != nil {
return -1, err
}

go io.Copy(master, os.Stdin)
go io.Copy(os.Stdout, master)

state, err := term.SetRawTerminal(os.Stdin.Fd())
if err != nil {
return -1, err
}

defer term.RestoreTerminal(os.Stdin.Fd(), state)
}

startCallback := func() {
go func() {
resizeTty(master)

for sig := range sigc {
cmd.Process.Signal(sig)
switch sig {
case syscall.SIGWINCH:
resizeTty(master)
default:
cmd.Process.Signal(sig)
}
}
}()
}

return namespaces.Exec(container, term, "", dataPath, args, createCommand, startCallback)
return namespaces.Exec(container, stdin, stdout, stderr, console, "", dataPath, args, createCommand, startCallback)
}

func resizeTty(master *os.File) {
if master == nil {
return
}

ws, err := term.GetWinsize(os.Stdin.Fd())
if err != nil {
return
}

if err := term.SetWinsize(master.Fd(), ws); err != nil {
return
}
}

0 comments on commit b30e0e1

Please sign in to comment.