Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/kr/pty"
"github.com/opencontainers/runtime-spec/specs-go"
netcontext "golang.org/x/net/context"
"golang.org/x/sys/unix"
)

func firstExistingFile(candidates []string) string {
Expand Down Expand Up @@ -334,10 +336,13 @@ func ociCreate(context *cli.Context, container string, createFunc func(stdin, st
ptymaster.Close()
}
if tty == nil {
pid := os.Getpid()
stdin = fmt.Sprintf("/proc/%d/fd/0", pid)
stdout = fmt.Sprintf("/proc/%d/fd/1", pid)
stderr = fmt.Sprintf("/proc/%d/fd/2", pid)
s, err := getStdio(context, container, true)
if err != nil {
return err
}
stdin = s.stdin
stdout = s.stdout
stderr = s.stderr
} else {
defer tty.Close()
stdin = tty.Name()
Expand Down Expand Up @@ -407,3 +412,63 @@ func createPidFile(path string, pid int) error {
}
return os.Rename(tmpName, path)
}

type stdio struct {
stdin string
stdout string
stderr string
}

// getStdio get or creates fifo files to act stdin, stdout and stderr
func getStdio(context *cli.Context, container string, create bool) (s *stdio, err error) {
stdioDir := getStdioDir(context, container)
s = &stdio{}
if _, err = os.Stat(stdioDir); os.IsNotExist(err) {
if !create {
return s, fmt.Errorf("try to get stdio failed: %v", err)
}
err = os.MkdirAll(stdioDir, 0755)
}
if err != nil {
return s, err
}
// create fifo's for the process
for name, fd := range map[string]*string{
"stdin": &s.stdin,
"stdout": &s.stdout,
"stderr": &s.stderr,
} {
path := filepath.Join(stdioDir, name)
if err := unix.Mkfifo(path, 0755); err != nil && !os.IsExist(err) {
return s, err
} else if !create && err == nil {
return s, fmt.Errorf("stdio file (%s/%s) not exist", stdioDir, name)
}
*fd = path
}
return s, nil
}

// attachStdio open fifo's created by createStdio(), and copy current
// process's stdout and stderr to stdout fifo and stderr fifo.
func attachStdio(s *stdio) (io.WriteCloser, error) {
stdinf, err := os.OpenFile(s.stdin, syscall.O_RDWR, 0)
if err != nil {
return nil, err
}
stdoutf, err := os.OpenFile(s.stdout, syscall.O_RDWR, 0)
if err != nil {
return stdinf, err
}
go io.Copy(os.Stdout, stdoutf)
stderrf, err := os.OpenFile(s.stderr, syscall.O_RDWR, 0)
if err != nil {
return stdinf, err
}
go io.Copy(os.Stderr, stderrf)
return stdinf, nil
}

func getStdioDir(context *cli.Context, container string) string {
return filepath.Join(filepath.Clean(context.GlobalString("root"))+"-stdio", container)
}
69 changes: 56 additions & 13 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -67,6 +68,10 @@ your host.`,
}

func startContainer(context *cli.Context, bundle, container, address string, config *specs.Spec, detach bool) int {
var err error

defer os.RemoveAll(getStdioDir(context, container))

r := &types.CreateContainerRequest{
Id: container,
Runtime: "runv-start",
Expand All @@ -75,18 +80,41 @@ func startContainer(context *cli.Context, bundle, container, address string, con

c := getClient(address)
evChan := containerEvents(c, container)
if _, err := c.CreateContainer(netcontext.Background(), r); err != nil {
if _, err = c.CreateContainer(netcontext.Background(), r); err != nil {
fmt.Printf("error %v\n", err)
return -1
}
if !detach && config.Process.Terminal {
s, err := term.SetRawTerminal(os.Stdin.Fd())
if err != nil {
var (
stdin io.WriteCloser
state *term.State
)
restoreAndCloseStdin := func() {
if state != nil {
term.RestoreTerminal(os.Stdin.Fd(), state)
}
if stdin != nil {
stdin.Close()
}
}
defer restoreAndCloseStdin()

if !detach {
if config.Process.Terminal {
state, err = term.SetRawTerminal(os.Stdin.Fd())
if err != nil {
fmt.Printf("error %v\n", err)
return -1
}
}
var s *stdio
if s, err = getStdio(context, container, false); err != nil {
fmt.Printf("error %v\n", err)
return -1
}
if stdin, err = attachStdio(s); err != nil {
fmt.Printf("error %v\n", err)
return -1
}
defer term.RestoreTerminal(os.Stdin.Fd(), s)
monitorTtySize(c, container, "init")
}
var started bool
for e := range evChan {
Expand All @@ -103,13 +131,28 @@ func startContainer(context *cli.Context, bundle, container, address string, con
fmt.Printf("failed to get the start event\n")
return -1
}
if !detach {
for e := range evChan {
if e.Type == "exit" && e.Pid == "init" {
return int(e.Status)
}
if detach {
return 0
}
go func() {
io.Copy(stdin, os.Stdin)
if _, err := c.UpdateProcess(netcontext.Background(), &types.UpdateProcessRequest{
Id: container,
Pid: "init",
CloseStdin: true,
}); err != nil {
fmt.Printf("error %v\n", err)
return
}
restoreAndCloseStdin()
}()
if config.Process.Terminal {
monitorTtySize(c, container, "init")
}
for e := range evChan {
if e.Type == "exit" && e.Pid == "init" {
return int(e.Status)
}
return -1
}
return 0
return -1
}