2014-04-11 22:42:53 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
2014-07-15 07:46:51 +08:00
|
|
|
"unsafe"
|
2014-05-02 10:09:12 +08:00
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/label"
|
2014-04-11 22:42:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Setup initializes the proper /dev/console inside the rootfs path
|
2015-01-27 20:54:19 +08:00
|
|
|
func Setup(rootfs, consolePath, mountLabel string, hostRootUid, hostRootGid int) error {
|
2014-07-15 07:46:51 +08:00
|
|
|
oldMask := syscall.Umask(0000)
|
|
|
|
defer syscall.Umask(oldMask)
|
2014-04-11 22:42:53 +08:00
|
|
|
|
|
|
|
if err := os.Chmod(consolePath, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2015-01-27 20:54:19 +08:00
|
|
|
if err := os.Chown(consolePath, hostRootUid, hostRootGid); err != nil {
|
2014-04-11 22:42:53 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-04-11 22:42:53 +08:00
|
|
|
if err := label.SetFileLabel(consolePath, mountLabel); err != nil {
|
2014-05-29 02:44:24 +08:00
|
|
|
return fmt.Errorf("set file label %s %s", consolePath, err)
|
2014-04-11 22:42:53 +08:00
|
|
|
}
|
2014-05-29 02:44:24 +08:00
|
|
|
|
|
|
|
dest := filepath.Join(rootfs, "dev/console")
|
|
|
|
|
|
|
|
f, err := os.Create(dest)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
return fmt.Errorf("create %s %s", dest, err)
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-05-29 02:44:24 +08:00
|
|
|
if f != nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
2014-07-15 07:46:51 +08:00
|
|
|
if err := syscall.Mount(consolePath, dest, "bind", syscall.MS_BIND, ""); err != nil {
|
2014-05-29 02:27:03 +08:00
|
|
|
return fmt.Errorf("bind %s to %s %s", consolePath, dest, err)
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-04-11 22:42:53 +08:00
|
|
|
return nil
|
|
|
|
}
|
2014-04-11 23:15:28 +08:00
|
|
|
|
|
|
|
func OpenAndDup(consolePath string) error {
|
2014-07-15 07:46:51 +08:00
|
|
|
slave, err := OpenTerminal(consolePath, syscall.O_RDWR)
|
2014-04-11 23:15:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("open terminal %s", err)
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
|
|
|
if err := syscall.Dup2(int(slave.Fd()), 0); err != nil {
|
2014-04-11 23:15:28 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
|
|
|
if err := syscall.Dup2(int(slave.Fd()), 1); err != nil {
|
2014-04-11 23:15:28 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
|
|
|
return syscall.Dup2(int(slave.Fd()), 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f.
|
|
|
|
// Unlockpt should be called before opening the slave side of a pseudoterminal.
|
|
|
|
func Unlockpt(f *os.File) error {
|
2014-10-23 06:06:09 +08:00
|
|
|
var u int32
|
2014-07-15 07:46:51 +08:00
|
|
|
|
|
|
|
return Ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ptsname retrieves the name of the first available pts for the given master.
|
|
|
|
func Ptsname(f *os.File) (string, error) {
|
2014-10-23 06:06:09 +08:00
|
|
|
var n int32
|
2014-07-15 07:46:51 +08:00
|
|
|
|
|
|
|
if err := Ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("/dev/pts/%d", n), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateMasterAndConsole will open /dev/ptmx on the host and retreive the
|
|
|
|
// pts name for use as the pty slave inside the container
|
|
|
|
func CreateMasterAndConsole() (*os.File, string, error) {
|
|
|
|
master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2014-07-16 07:26:28 +08:00
|
|
|
|
2014-07-15 07:46:51 +08:00
|
|
|
console, err := Ptsname(master)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2014-07-16 07:26:28 +08:00
|
|
|
|
2014-07-15 07:46:51 +08:00
|
|
|
if err := Unlockpt(master); err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2014-07-16 07:26:28 +08:00
|
|
|
|
2014-07-15 07:46:51 +08:00
|
|
|
return master, console, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenPtmx opens /dev/ptmx, i.e. the PTY master.
|
|
|
|
func OpenPtmx() (*os.File, error) {
|
|
|
|
// O_NOCTTY and O_CLOEXEC are not present in os package so we use the syscall's one for all.
|
|
|
|
return os.OpenFile("/dev/ptmx", syscall.O_RDONLY|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenTerminal is a clone of os.OpenFile without the O_CLOEXEC
|
|
|
|
// used to open the pty slave inside the container namespace
|
|
|
|
func OpenTerminal(name string, flag int) (*os.File, error) {
|
|
|
|
r, e := syscall.Open(name, flag, 0)
|
|
|
|
if e != nil {
|
2014-08-13 18:38:20 +08:00
|
|
|
return nil, &os.PathError{Op: "open", Path: name, Err: e}
|
2014-07-15 07:46:51 +08:00
|
|
|
}
|
|
|
|
return os.NewFile(uintptr(r), name), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Ioctl(fd uintptr, flag, data uintptr) error {
|
|
|
|
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, flag, data); err != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-04-11 23:15:28 +08:00
|
|
|
}
|