From c2403c32dbf8a67870ab2ba7524c117fc0652256 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 12 Feb 2015 10:38:43 -0800 Subject: [PATCH] Add GetPath on namespace config Signed-off-by: Michael Crosby --- configs/namespaces.go | 27 +++++++++++++++++++++++++++ console.go | 2 +- linux_container.go | 21 +-------------------- nsinit/config.go | 2 +- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/configs/namespaces.go b/configs/namespaces.go index fbb56e1d..9078e6ab 100644 --- a/configs/namespaces.go +++ b/configs/namespaces.go @@ -1,6 +1,7 @@ package configs import ( + "fmt" "syscall" ) @@ -26,6 +27,32 @@ func (n *Namespace) Syscall() int { return namespaceInfo[n.Type] } +func (n *Namespace) GetPath(pid int) string { + if n.Path != "" { + return n.Path + } + return fmt.Sprintf("/proc/%d/ns/%s", pid, n.file()) +} + +func (n *Namespace) file() string { + file := "" + switch n.Type { + case NEWNET: + file = "net" + case NEWNS: + file = "mnt" + case NEWPID: + file = "pid" + case NEWIPC: + file = "ipc" + case NEWUSER: + file = "user" + case NEWUTS: + file = "uts" + } + return file +} + type Namespaces []Namespace func (n *Namespaces) Remove(t NamespaceType) bool { diff --git a/console.go b/console.go index 1998086f..042a2a2e 100644 --- a/console.go +++ b/console.go @@ -2,7 +2,7 @@ package libcontainer import "io" -// Console represents a psuedo TTY. +// Console represents a pseudo TTY. type Console interface { io.ReadWriter io.Closer diff --git a/linux_container.go b/linux_container.go index 0cb6749f..492936ea 100644 --- a/linux_container.go +++ b/linux_container.go @@ -72,26 +72,7 @@ func (c *linuxContainer) State() (*State, error) { NamespacePaths: make(map[string]string), } for _, ns := range c.config.Namespaces { - if ns.Path != "" { - state.NamespacePaths[string(ns.Type)] = ns.Path - continue - } - file := "" - switch ns.Type { - case configs.NEWNET: - file = "net" - case configs.NEWNS: - file = "mnt" - case configs.NEWPID: - file = "pid" - case configs.NEWIPC: - file = "ipc" - case configs.NEWUSER: - file = "user" - case configs.NEWUTS: - file = "uts" - } - state.NamespacePaths[string(ns.Type)] = fmt.Sprintf("/proc/%d/ns/%s", c.initProcess.pid(), file) + state.NamespacePaths[string(ns.Type)] = ns.GetPath(c.initProcess.pid()) } return state, nil } diff --git a/nsinit/config.go b/nsinit/config.go index f06804f2..145fe59a 100644 --- a/nsinit/config.go +++ b/nsinit/config.go @@ -13,7 +13,7 @@ import ( ) var createFlags = []cli.Flag{ - cli.IntFlag{Name: "parent-death-signal", Usage: "set the signal that will be delivered to the process incase the parent dies"}, + cli.IntFlag{Name: "parent-death-signal", Usage: "set the signal that will be delivered to the process in case the parent dies"}, cli.BoolFlag{Name: "read-only", Usage: "set the container's rootfs as read-only"}, cli.StringSliceFlag{Name: "bind", Value: &cli.StringSlice{}, Usage: "add bind mounts to the container"}, cli.StringSliceFlag{Name: "tmpfs", Value: &cli.StringSlice{}, Usage: "add tmpfs mounts to the container"},