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
20 changes: 13 additions & 7 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli"
netcontext "golang.org/x/net/context"
"golang.org/x/sys/unix"
)

var createCommand = cli.Command{
Expand Down Expand Up @@ -245,7 +246,7 @@ func createContainer(context *cli.Context, container, namespace string, config *
return err
}

return ociCreate(context, container, "init", func(stdin, stdout, stderr string) error {
return ociCreate(context, container, "init", namespace, func(stdin, stdout, stderr string) error {
r := &types.CreateContainerRequest{
Id: container,
Runtime: "runv-create",
Expand All @@ -269,7 +270,7 @@ func createContainer(context *cli.Context, container, namespace string, config *

}

func ociCreate(context *cli.Context, container, process string, createFunc func(stdin, stdout, stderr string) error) error {
func ociCreate(context *cli.Context, container, process, namespace string, createFunc func(stdin, stdout, stderr string) error) error {
path, err := osext.Executable()
if err != nil {
return fmt.Errorf("cannot find self executable path for %s: %v\n", os.Args[0], err)
Expand Down Expand Up @@ -298,10 +299,15 @@ func ociCreate(context *cli.Context, container, process string, createFunc func(
stdout = fmt.Sprintf("/proc/%d/fd/1", pid)
stderr = fmt.Sprintf("/proc/%d/fd/2", pid)
} else {
defer tty.Close()
stdin = tty.Name()
stdout = tty.Name()
stderr = tty.Name()
streamDir := filepath.Join(namespace, container)
if _, ex := os.Stat(streamDir); ex != nil && os.IsNotExist(ex) {
os.MkdirAll(streamDir, 0755)
}

stdin = fmt.Sprintf("%s/%s-0", streamDir, process)
unix.Mkfifo(stdin, 0)
stdout = fmt.Sprintf("%s/%s-1", streamDir, process)
unix.Mkfifo(stdout, 0)
}
err = createFunc(stdin, stdout, stderr)
if err != nil {
Expand All @@ -324,7 +330,7 @@ func ociCreate(context *cli.Context, container, process string, createFunc func(
args = append(args, "--proxy-exit-code", "--proxy-signal")
}
if tty != nil {
args = append(args, "--proxy-winsize")
args = append(args, "--proxy-winsize", "--input-pipe", stdin, "--output-pipe", stdout)
}
cmd = &exec.Cmd{
Path: path,
Expand Down
2 changes: 1 addition & 1 deletion exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func runProcess(context *cli.Context, container string, config *specs.Process) (
monitorTtySize(c, container, process)
}

err = ociCreate(context, container, process, func(stdin, stdout, stderr string) error {
err = ociCreate(context, container, process, filepath.Join(context.GlobalString("root"), container, "namespace"), func(stdin, stdout, stderr string) error {
p := &types.AddProcessRequest{
Id: container,
Pid: process,
Expand Down
36 changes: 36 additions & 0 deletions shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -33,11 +34,46 @@ var shimCommand = cli.Command{
cli.BoolFlag{
Name: "proxy-winsize",
},
cli.StringFlag{
Name: "input-pipe",
},
cli.StringFlag{
Name: "output-pipe",
},
},
Action: func(context *cli.Context) error {
root := context.GlobalString("root")
container := context.String("container")
process := context.String("process")

var stdinStream, stdoutStream io.ReadWriteCloser
var err error
stdinPath := context.String("input-pipe")
stdoutPath := context.String("output-pipe")
if context.Bool("proxy-winsize") {
stdinStream, err = os.OpenFile(stdinPath, syscall.O_WRONLY, 0)
if err != nil {
return err
}

stdoutStream, err = os.OpenFile(stdoutPath, syscall.O_RDONLY, 0)
if err != nil {
return err
}
}

if stdinStream != nil {
go func() {
io.Copy(stdinStream, os.Stdin)
}()
}

if stdoutStream != nil {
go func() {
io.Copy(os.Stdout, stdoutStream)
}()
}

c, err := getClient(filepath.Join(root, container, "namespace", "namespaced.sock"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to get client: %v", err), -1)
Expand Down