From 9c9aac53859a2516c350aeb677819881c9ea8d27 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 9 Dec 2015 11:43:11 -0800 Subject: [PATCH] Export console New func Signed-off-by: Michael Crosby --- libcontainer/console_freebsd.go | 4 ++-- libcontainer/console_linux.go | 4 ++-- libcontainer/console_windows.go | 4 ++-- libcontainer/error.go | 3 +++ libcontainer/process.go | 11 ++++++++++- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/libcontainer/console_freebsd.go b/libcontainer/console_freebsd.go index 4d20b8da..3c89eda0 100644 --- a/libcontainer/console_freebsd.go +++ b/libcontainer/console_freebsd.go @@ -6,8 +6,8 @@ import ( "errors" ) -// newConsole returns an initalized console that can be used within a container by copying bytes +// NewConsole returns an initalized console that can be used within a container by copying bytes // from the master side to the slave that is attached as the tty for the container's init process. -func newConsole(uid, gid int) (Console, error) { +func NewConsole(uid, gid int) (Console, error) { return nil, errors.New("libcontainer console is not supported on FreeBSD") } diff --git a/libcontainer/console_linux.go b/libcontainer/console_linux.go index f345f572..7af771b6 100644 --- a/libcontainer/console_linux.go +++ b/libcontainer/console_linux.go @@ -10,9 +10,9 @@ import ( "github.com/opencontainers/runc/libcontainer/label" ) -// newConsole returns an initalized console that can be used within a container by copying bytes +// NewConsole returns an initalized console that can be used within a container by copying bytes // from the master side to the slave that is attached as the tty for the container's init process. -func newConsole(uid, gid int) (Console, error) { +func NewConsole(uid, gid int) (Console, error) { master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0) if err != nil { return nil, err diff --git a/libcontainer/console_windows.go b/libcontainer/console_windows.go index 80c7463b..a68c02f6 100644 --- a/libcontainer/console_windows.go +++ b/libcontainer/console_windows.go @@ -1,7 +1,7 @@ package libcontainer -// newConsole returns an initalized console that can be used within a container -func newConsole(uid, gid int) (Console, error) { +// NewConsole returns an initalized console that can be used within a container +func NewConsole(uid, gid int) (Console, error) { return &windowsConsole{}, nil } diff --git a/libcontainer/error.go b/libcontainer/error.go index 6c266620..c51a3648 100644 --- a/libcontainer/error.go +++ b/libcontainer/error.go @@ -22,6 +22,7 @@ const ( // Common errors ConfigInvalid + ConsoleExists SystemError ) @@ -43,6 +44,8 @@ func (c ErrorCode) String() string { return "Container is not stopped" case ContainerNotRunning: return "Container is not running" + case ConsoleExists: + return "Console exist for process" default: return "Unknown error" } diff --git a/libcontainer/process.go b/libcontainer/process.go index 7902d08c..e96dc0d3 100644 --- a/libcontainer/process.go +++ b/libcontainer/process.go @@ -80,10 +80,19 @@ func (p Process) Signal(sig os.Signal) error { // NewConsole creates new console for process and returns it func (p *Process) NewConsole(rootuid int) (Console, error) { - console, err := newConsole(rootuid, rootuid) + console, err := NewConsole(rootuid, rootuid) if err != nil { return nil, err } p.consolePath = console.Path() return console, nil } + +// ConsoleFromPath sets the process's console with the path provided +func (p *Process) ConsoleFromPath(path string) error { + if p.consolePath != "" { + return newGenericError(fmt.Errorf("console path already exists for process"), ConsoleExists) + } + p.consolePath = path + return nil +}