141 lines
3.0 KiB
Go
141 lines
3.0 KiB
Go
// +build !windows
|
|
|
|
package main
|
|
|
|
import (
|
|
gocontext "context"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/tonistiigi/fifo"
|
|
"github.com/urfave/cli"
|
|
"golang.org/x/sys/unix"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/grpclog"
|
|
)
|
|
|
|
func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup, error) {
|
|
var wg sync.WaitGroup
|
|
ctx := gocontext.Background()
|
|
|
|
f, err := fifo.OpenFifo(ctx, stdin, syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func(c io.Closer) {
|
|
if err != nil {
|
|
c.Close()
|
|
}
|
|
}(f)
|
|
go func(w io.WriteCloser) {
|
|
io.Copy(w, os.Stdin)
|
|
w.Close()
|
|
}(f)
|
|
|
|
f, err = fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func(c io.Closer) {
|
|
if err != nil {
|
|
c.Close()
|
|
}
|
|
}(f)
|
|
wg.Add(1)
|
|
go func(r io.ReadCloser) {
|
|
io.Copy(os.Stdout, r)
|
|
r.Close()
|
|
wg.Done()
|
|
}(f)
|
|
|
|
f, err = fifo.OpenFifo(ctx, stderr, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func(c io.Closer) {
|
|
if err != nil {
|
|
c.Close()
|
|
}
|
|
}(f)
|
|
if !console {
|
|
wg.Add(1)
|
|
go func(r io.ReadCloser) {
|
|
io.Copy(os.Stderr, r)
|
|
r.Close()
|
|
wg.Done()
|
|
}(f)
|
|
}
|
|
|
|
return &wg, nil
|
|
}
|
|
|
|
func getGRPCConnection(context *cli.Context) (*grpc.ClientConn, error) {
|
|
if grpcConn != nil {
|
|
return grpcConn, nil
|
|
}
|
|
|
|
bindSocket := context.GlobalString("address")
|
|
// reset the logger for grpc to log to dev/null so that it does not mess with our stdio
|
|
grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
|
|
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(100 * time.Second)}
|
|
dialOpts = append(dialOpts,
|
|
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
|
return net.DialTimeout("unix", bindSocket, timeout)
|
|
},
|
|
))
|
|
|
|
conn, err := grpc.Dial(fmt.Sprintf("unix://%s", bindSocket), dialOpts...)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to dial %q", bindSocket)
|
|
}
|
|
|
|
grpcConn = conn
|
|
return grpcConn, nil
|
|
}
|
|
|
|
var signalMap = map[string]syscall.Signal{
|
|
"ABRT": unix.SIGABRT,
|
|
"ALRM": unix.SIGALRM,
|
|
"BUS": unix.SIGBUS,
|
|
"CHLD": unix.SIGCHLD,
|
|
"CLD": unix.SIGCLD,
|
|
"CONT": unix.SIGCONT,
|
|
"FPE": unix.SIGFPE,
|
|
"HUP": unix.SIGHUP,
|
|
"ILL": unix.SIGILL,
|
|
"INT": unix.SIGINT,
|
|
"IO": unix.SIGIO,
|
|
"IOT": unix.SIGIOT,
|
|
"KILL": unix.SIGKILL,
|
|
"PIPE": unix.SIGPIPE,
|
|
"POLL": unix.SIGPOLL,
|
|
"PROF": unix.SIGPROF,
|
|
"PWR": unix.SIGPWR,
|
|
"QUIT": unix.SIGQUIT,
|
|
"SEGV": unix.SIGSEGV,
|
|
"STKFLT": unix.SIGSTKFLT,
|
|
"STOP": unix.SIGSTOP,
|
|
"SYS": unix.SIGSYS,
|
|
"TERM": unix.SIGTERM,
|
|
"TRAP": unix.SIGTRAP,
|
|
"TSTP": unix.SIGTSTP,
|
|
"TTIN": unix.SIGTTIN,
|
|
"TTOU": unix.SIGTTOU,
|
|
"UNUSED": unix.SIGUNUSED,
|
|
"URG": unix.SIGURG,
|
|
"USR1": unix.SIGUSR1,
|
|
"USR2": unix.SIGUSR2,
|
|
"VTALRM": unix.SIGVTALRM,
|
|
"WINCH": unix.SIGWINCH,
|
|
"XCPU": unix.SIGXCPU,
|
|
"XFSZ": unix.SIGXFSZ,
|
|
}
|