2016-06-07 07:45:21 +00:00
|
|
|
// +build linux solaris
|
|
|
|
|
2016-03-18 18:50:19 +00:00
|
|
|
package libcontainerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-10-14 23:31:27 +00:00
|
|
|
"io/ioutil"
|
2016-03-18 18:50:19 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-06-07 07:45:21 +00:00
|
|
|
goruntime "runtime"
|
2016-11-30 01:41:39 +00:00
|
|
|
"strings"
|
2016-03-18 18:50:19 +00:00
|
|
|
|
2017-05-09 14:39:23 +00:00
|
|
|
containerd "github.com/containerd/containerd/api/grpc/types"
|
2016-10-14 23:31:27 +00:00
|
|
|
"github.com/tonistiigi/fifo"
|
2016-03-18 18:50:19 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-06-07 07:45:21 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2016-03-18 18:50:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var fdNames = map[int]string{
|
2016-06-07 07:45:21 +00:00
|
|
|
unix.Stdin: "stdin",
|
|
|
|
unix.Stdout: "stdout",
|
|
|
|
unix.Stderr: "stderr",
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// process keeps the state for both main container process and exec process.
|
|
|
|
type process struct {
|
|
|
|
processCommon
|
|
|
|
|
|
|
|
// Platform specific fields are below here.
|
|
|
|
dir string
|
|
|
|
}
|
|
|
|
|
2017-01-13 19:06:51 +00:00
|
|
|
func (p *process) openFifos(ctx context.Context, terminal bool) (pipe *IOPipe, err error) {
|
2016-10-14 23:31:27 +00:00
|
|
|
if err := os.MkdirAll(p.dir, 0700); err != nil {
|
2016-03-18 18:50:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
io := &IOPipe{}
|
2016-10-14 23:31:27 +00:00
|
|
|
|
2016-06-07 07:45:21 +00:00
|
|
|
io.Stdin, err = fifo.OpenFifo(ctx, p.fifo(unix.Stdin), unix.O_WRONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
|
2016-10-14 23:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-10-18 18:18:19 +00:00
|
|
|
io.Stdin.Close()
|
2016-10-14 23:31:27 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-06-07 07:45:21 +00:00
|
|
|
io.Stdout, err = fifo.OpenFifo(ctx, p.fifo(unix.Stdout), unix.O_RDONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
|
2016-03-18 18:50:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-14 23:31:27 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
io.Stdout.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-06-07 07:45:21 +00:00
|
|
|
if goruntime.GOOS == "solaris" || !terminal {
|
|
|
|
// For Solaris terminal handling is done exclusively by the runtime therefore we make no distinction
|
|
|
|
// in the processing for terminal and !terminal cases.
|
|
|
|
io.Stderr, err = fifo.OpenFifo(ctx, p.fifo(unix.Stderr), unix.O_RDONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
|
2016-10-14 23:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
io.Stderr.Close()
|
|
|
|
}
|
|
|
|
}()
|
2016-03-18 18:50:19 +00:00
|
|
|
} else {
|
2016-10-14 23:31:27 +00:00
|
|
|
io.Stderr = ioutil.NopCloser(emptyReader{})
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return io, nil
|
|
|
|
}
|
|
|
|
|
2016-10-18 18:18:19 +00:00
|
|
|
func (p *process) sendCloseStdin() error {
|
|
|
|
_, err := p.client.remote.apiClient.UpdateProcess(context.Background(), &containerd.UpdateProcessRequest{
|
|
|
|
Id: p.containerID,
|
|
|
|
Pid: p.friendlyName,
|
|
|
|
CloseStdin: true,
|
|
|
|
})
|
2016-11-30 01:41:39 +00:00
|
|
|
if err != nil && (strings.Contains(err.Error(), "container not found") || strings.Contains(err.Error(), "process not found")) {
|
|
|
|
return nil
|
|
|
|
}
|
2016-10-18 18:18:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-18 18:50:19 +00:00
|
|
|
func (p *process) closeFifos(io *IOPipe) {
|
|
|
|
io.Stdin.Close()
|
2016-10-14 23:31:27 +00:00
|
|
|
io.Stdout.Close()
|
|
|
|
io.Stderr.Close()
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type emptyReader struct{}
|
|
|
|
|
|
|
|
func (r emptyReader) Read(b []byte) (int, error) {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) fifo(index int) string {
|
|
|
|
return filepath.Join(p.dir, p.friendlyName+"-"+fdNames[index])
|
|
|
|
}
|