Skip to content

Commit

Permalink
Merge pull request moby#7069 from crosbymichael/update-libcontainer-j…
Browse files Browse the repository at this point in the history
…uly3

Update libcontainer to be85764f109c3f0f62cd2a5c8be
  • Loading branch information
unclejack committed Jul 18, 2014
2 parents 615f211 + 7a8ea91 commit 1646e5d
Show file tree
Hide file tree
Showing 39 changed files with 606 additions and 397 deletions.
6 changes: 3 additions & 3 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/docker/libcontainer/label"
"github.com/docker/libcontainer/selinux"
"github.com/dotcloud/docker/archive"
"github.com/dotcloud/docker/daemon/execdriver"
"github.com/dotcloud/docker/daemon/execdriver/execdrivers"
Expand Down Expand Up @@ -300,7 +299,8 @@ func (daemon *Daemon) Destroy(container *Container) error {
if err := os.RemoveAll(container.root); err != nil {
return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err)
}
selinux.FreeLxcContexts(container.ProcessLabel)

selinuxFreeLxcContexts(container.ProcessLabel)

return nil
}
Expand Down Expand Up @@ -761,7 +761,7 @@ func NewDaemon(config *daemonconfig.Config, eng *engine.Engine) (*Daemon, error)

func NewDaemonFromDirectory(config *daemonconfig.Config, eng *engine.Engine) (*Daemon, error) {
if !config.EnableSelinuxSupport {
selinux.SetDisabled()
selinuxSetDisabled()
}

// Create the root directory if it doesn't exists
Expand Down
88 changes: 86 additions & 2 deletions daemon/execdriver/lxc/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lxc
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
Expand All @@ -19,7 +20,9 @@ import (
"github.com/docker/libcontainer/label"
"github.com/docker/libcontainer/mount/nodes"
"github.com/dotcloud/docker/daemon/execdriver"
"github.com/dotcloud/docker/pkg/term"
"github.com/dotcloud/docker/utils"
"github.com/kr/pty"
)

const DriverName = "lxc"
Expand Down Expand Up @@ -78,10 +81,20 @@ func (d *driver) Name() string {
}

func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
if err := execdriver.SetTerminal(c, pipes); err != nil {
return -1, err
var (
term execdriver.Terminal
err error
)

if c.Tty {
term, err = NewTtyConsole(c, pipes)
} else {
term, err = execdriver.NewStdConsole(c, pipes)
}
c.Terminal = term

c.Mounts = append(c.Mounts, execdriver.Mount{d.initPath, c.InitPath, false, true})

if err := d.generateEnvConfig(c); err != nil {
return -1, err
}
Expand Down Expand Up @@ -462,3 +475,74 @@ func (d *driver) generateEnvConfig(c *execdriver.Command) error {

return ioutil.WriteFile(p, data, 0600)
}

type TtyConsole struct {
MasterPty *os.File
SlavePty *os.File
}

func NewTtyConsole(command *execdriver.Command, pipes *execdriver.Pipes) (*TtyConsole, error) {
// lxc is special in that we cannot create the master outside of the container without
// opening the slave because we have nothing to provide to the cmd. We have to open both then do
// the crazy setup on command right now instead of passing the console path to lxc and telling it
// to open up that console. we save a couple of openfiles in the native driver because we can do
// this.
ptyMaster, ptySlave, err := pty.Open()
if err != nil {
return nil, err
}

tty := &TtyConsole{
MasterPty: ptyMaster,
SlavePty: ptySlave,
}

if err := tty.AttachPipes(&command.Cmd, pipes); err != nil {
tty.Close()
return nil, err
}

command.Console = tty.SlavePty.Name()

return tty, nil
}

func (t *TtyConsole) Master() *os.File {
return t.MasterPty
}

func (t *TtyConsole) Resize(h, w int) error {
return term.SetWinsize(t.MasterPty.Fd(), &term.Winsize{Height: uint16(h), Width: uint16(w)})
}

func (t *TtyConsole) AttachPipes(command *exec.Cmd, pipes *execdriver.Pipes) error {
command.Stdout = t.SlavePty
command.Stderr = t.SlavePty

go func() {
if wb, ok := pipes.Stdout.(interface {
CloseWriters() error
}); ok {
defer wb.CloseWriters()
}

io.Copy(pipes.Stdout, t.MasterPty)
}()

if pipes.Stdin != nil {
command.Stdin = t.SlavePty
command.SysProcAttr.Setctty = true

go func() {
io.Copy(t.MasterPty, pipes.Stdin)

pipes.Stdin.Close()
}()
}
return nil
}

func (t *TtyConsole) Close() error {
t.SlavePty.Close()
return t.MasterPty.Close()
}
2 changes: 1 addition & 1 deletion daemon/execdriver/native/create.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux
// +build linux,cgo

package native

Expand Down
86 changes: 71 additions & 15 deletions daemon/execdriver/native/driver.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// +build linux
// +build linux,cgo

package native

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/docker/libcontainer/syncpipe"
"github.com/dotcloud/docker/daemon/execdriver"
"github.com/dotcloud/docker/pkg/system"
"github.com/dotcloud/docker/pkg/term"
)

const (
Expand Down Expand Up @@ -95,6 +97,16 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba
if err != nil {
return -1, err
}

var term execdriver.Terminal

if c.Tty {
term, err = NewTtyConsole(c, pipes)
} else {
term, err = execdriver.NewStdConsole(c, pipes)
}
c.Terminal = term

d.Lock()
d.activeContainers[c.ID] = &activeContainer{
container: container,
Expand All @@ -106,6 +118,7 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba
dataPath = filepath.Join(d.root, c.ID)
args = append([]string{c.Entrypoint}, c.Arguments...)
)

if err := d.createContainerRoot(c.ID); err != nil {
return -1, err
}
Expand All @@ -115,9 +128,7 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba
return -1, err
}

term := getTerminal(c, pipes)

return namespaces.Exec(container, term, c.Rootfs, dataPath, args, func(container *libcontainer.Config, console, rootfs, dataPath, init string, child *os.File, args []string) *exec.Cmd {
return namespaces.Exec(container, c.Stdin, c.Stdout, c.Stderr, c.Console, c.Rootfs, dataPath, args, func(container *libcontainer.Config, console, rootfs, dataPath, init string, child *os.File, args []string) *exec.Cmd {
// we need to join the rootfs because namespaces will setup the rootfs and chroot
initPath := filepath.Join(c.Rootfs, c.InitPath)

Expand Down Expand Up @@ -201,11 +212,13 @@ func (d *driver) Terminate(p *execdriver.Command) error {
if err != nil {
return err
}

if state.InitStartTime == currentStartTime {
err = syscall.Kill(p.Process.Pid, 9)
syscall.Wait4(p.Process.Pid, nil, 0, nil)
}
d.removeContainerRoot(p.ID)

return err

}
Expand Down Expand Up @@ -267,17 +280,60 @@ func getEnv(key string, env []string) string {
return ""
}

func getTerminal(c *execdriver.Command, pipes *execdriver.Pipes) namespaces.Terminal {
var term namespaces.Terminal
if c.Tty {
term = &dockerTtyTerm{
pipes: pipes,
}
} else {
term = &dockerStdTerm{
pipes: pipes,
type TtyConsole struct {
MasterPty *os.File
}

func NewTtyConsole(command *execdriver.Command, pipes *execdriver.Pipes) (*TtyConsole, error) {
ptyMaster, console, err := system.CreateMasterAndConsole()
if err != nil {
return nil, err
}

tty := &TtyConsole{
MasterPty: ptyMaster,
}

if err := tty.AttachPipes(&command.Cmd, pipes); err != nil {
tty.Close()
return nil, err
}

command.Console = console

return tty, nil
}

func (t *TtyConsole) Master() *os.File {
return t.MasterPty
}

func (t *TtyConsole) Resize(h, w int) error {
return term.SetWinsize(t.MasterPty.Fd(), &term.Winsize{Height: uint16(h), Width: uint16(w)})
}

func (t *TtyConsole) AttachPipes(command *exec.Cmd, pipes *execdriver.Pipes) error {
go func() {
if wb, ok := pipes.Stdout.(interface {
CloseWriters() error
}); ok {
defer wb.CloseWriters()
}

io.Copy(pipes.Stdout, t.MasterPty)
}()

if pipes.Stdin != nil {
go func() {
io.Copy(t.MasterPty, pipes.Stdin)

pipes.Stdin.Close()
}()
}
c.Terminal = term
return term

return nil
}

func (t *TtyConsole) Close() error {
return t.MasterPty.Close()
}
13 changes: 13 additions & 0 deletions daemon/execdriver/native/driver_unsupported_nocgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build linux,!cgo

package native

import (
"fmt"

"github.com/dotcloud/docker/daemon/execdriver"
)

func NewDriver(root, initPath string) (execdriver.Driver, error) {
return nil, fmt.Errorf("native driver not supported on non-linux")
}
2 changes: 1 addition & 1 deletion daemon/execdriver/native/info.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux
// +build linux,cgo

package native

Expand Down
42 changes: 0 additions & 42 deletions daemon/execdriver/native/term.go

This file was deleted.

Loading

0 comments on commit 1646e5d

Please sign in to comment.