github.com/runcom/containerd@v0.0.0-20160708090337-9bff9f934c0d/containerd-shim/console.go (about) 1 // +build !solaris 2 3 package main 4 5 import ( 6 "fmt" 7 "os" 8 "syscall" 9 "unsafe" 10 ) 11 12 // NewConsole returns an initialized console that can be used within a container by copying bytes 13 // from the master side to the slave that is attached as the tty for the container's init process. 14 func newConsole(uid, gid int) (*os.File, string, error) { 15 master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0) 16 if err != nil { 17 return nil, "", err 18 } 19 console, err := ptsname(master) 20 if err != nil { 21 return nil, "", err 22 } 23 if err := unlockpt(master); err != nil { 24 return nil, "", err 25 } 26 if err := os.Chmod(console, 0600); err != nil { 27 return nil, "", err 28 } 29 if err := os.Chown(console, uid, gid); err != nil { 30 return nil, "", err 31 } 32 return master, console, nil 33 } 34 35 func ioctl(fd uintptr, flag, data uintptr) error { 36 if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, flag, data); err != 0 { 37 return err 38 } 39 return nil 40 } 41 42 // unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f. 43 // unlockpt should be called before opening the slave side of a pty. 44 func unlockpt(f *os.File) error { 45 var u int32 46 return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u))) 47 } 48 49 // ptsname retrieves the name of the first available pts for the given master. 50 func ptsname(f *os.File) (string, error) { 51 var n int32 52 if err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))); err != nil { 53 return "", err 54 } 55 return fmt.Sprintf("/dev/pts/%d", n), nil 56 }