Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
Merge pull request #258 from amshinde/controlling-terminal
Browse files Browse the repository at this point in the history
tty: Set the console as the controlling terminal for shim
  • Loading branch information
Samuel Ortiz authored May 18, 2017
2 parents 967d0fb + a43b84a commit a3fd661
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
10 changes: 10 additions & 0 deletions cc_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"os/exec"
"syscall"
)

type ccShim struct{}
Expand Down Expand Up @@ -75,6 +76,15 @@ func (s *ccShim) start(pod Pod, params ShimParams) (int, error) {
cmd.Stdin = f
cmd.Stdout = f
cmd.Stderr = f
cmd.SysProcAttr = &syscall.SysProcAttr{
// Create Session
Setsid: true,

// Set Controlling terminal to Ctty
Setctty: true,
Ctty: int(f.Fd()),
}

}
defer func() {
if f != nil {
Expand Down
61 changes: 56 additions & 5 deletions cc_shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package virtcontainers

import (
"fmt"
"os"
"path/filepath"
"syscall"
"testing"
"unsafe"

. "github.com/containers/virtcontainers/pkg/mock"
)
Expand Down Expand Up @@ -149,15 +151,63 @@ func TestCCShimStartWithConsoleNonExistingFailure(t *testing.T) {
testCCShimStart(t, pod, params, true)
}

func ioctl(fd uintptr, flag, data uintptr) error {
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, flag, data); err != 0 {
return err
}

return nil
}

// unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f.
func unlockpt(f *os.File) error {
var u int32

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) {
var n int32

if err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))); err != nil {
return "", err
}

return fmt.Sprintf("/dev/pts/%d", n), nil
}

func newConsole() (*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
}

console, err := ptsname(master)
if err != nil {
return nil, "", err
}

if err := unlockpt(master); err != nil {
return nil, "", err
}

if err := os.Chmod(console, 0600); err != nil {
return nil, "", err
}

return master, console, nil
}

func TestCCShimStartWithConsoleSuccessful(t *testing.T) {
cleanUp()

consolePath := filepath.Join(testDir, testConsolePath)
f, err := os.Create(consolePath)
master, console, err := newConsole()
t.Logf("Console created for tests:%s\n", console)

if err != nil {
t.Fatal(err)
}
f.Close()

pod := Pod{
config: &PodConfig{
Expand All @@ -171,8 +221,9 @@ func TestCCShimStartWithConsoleSuccessful(t *testing.T) {
params := ShimParams{
Token: "testToken",
URL: testProxyURL,
Console: consolePath,
Console: console,
}

testCCShimStart(t, pod, params, false)
master.Close()
}

0 comments on commit a3fd661

Please sign in to comment.