Skip to content
Merged
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
66 changes: 0 additions & 66 deletions cmd/ctr/kill_unix.go

This file was deleted.

7 changes: 0 additions & 7 deletions cmd/ctr/kill_windows.go

This file was deleted.

3 changes: 3 additions & 0 deletions cmd/ctr/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ var runCommand = cli.Command{
if err := handleConsoleResize(ctx, containers, response.ID, response.Pid, con); err != nil {
logrus.WithError(err).Error("console resize")
}
} else {
sigc := forwardAllSignals(containers, id)
defer stopCatch(sigc)
}
if _, err := containers.Start(ctx, &execution.StartRequest{
ID: response.ID,
Expand Down
50 changes: 50 additions & 0 deletions cmd/ctr/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package main

import (
gocontext "context"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"

"github.com/Sirupsen/logrus"
contentapi "github.com/containerd/containerd/api/services/content"
"github.com/containerd/containerd/api/services/execution"
imagesapi "github.com/containerd/containerd/api/services/images"
Expand Down Expand Up @@ -79,3 +85,47 @@ func waitContainer(events execution.ContainerService_EventsClient, id string, pi
}
}
}

func forwardAllSignals(containers execution.ContainerServiceClient, id string) chan os.Signal {
sigc := make(chan os.Signal, 128)
signal.Notify(sigc)

go func() {
for s := range sigc {
logrus.Debug("Forwarding signal ", s)
killRequest := &execution.KillRequest{
ID: id,
Signal: uint32(s.(syscall.Signal)),
All: false,
}
_, err := containers.Kill(gocontext.Background(), killRequest)
if err != nil {
logrus.WithError(err).Error("grpc event from kill")
}
}
}()
return sigc
}

func parseSignal(rawSignal string) (syscall.Signal, error) {
s, err := strconv.Atoi(rawSignal)
if err == nil {
sig := syscall.Signal(s)
for _, msig := range signalMap {
if sig == msig {
return sig, nil
}
}
return -1, fmt.Errorf("unknown signal %q", rawSignal)
}
signal, ok := signalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
if !ok {
return -1, fmt.Errorf("unknown signal %q", rawSignal)
}
return signal, nil
}

func stopCatch(sigc chan os.Signal) {
signal.Stop(sigc)
close(sigc)
}
42 changes: 40 additions & 2 deletions cmd/ctr/utils_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
gocontext "context"
"fmt"
"io"
"io/ioutil"
Expand All @@ -13,11 +14,10 @@ import (
"syscall"
"time"

gocontext "context"

"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"
)
Expand Down Expand Up @@ -100,3 +100,41 @@ func getGRPCConnection(context *cli.Context) (*grpc.ClientConn, error) {
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,
}
18 changes: 18 additions & 0 deletions cmd/ctr/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"net"
"os"
"sync"
"syscall"
"time"

"github.com/Microsoft/go-winio"
clog "github.com/containerd/containerd/log"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/sys/windows"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
)
Expand Down Expand Up @@ -119,3 +121,19 @@ func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup,

return &wg, nil
}

var signalMap = map[string]syscall.Signal{
"HUP": syscall.Signal(windows.SIGHUP),
"INT": syscall.Signal(windows.SIGINT),
"QUIT": syscall.Signal(windows.SIGQUIT),
"SIGILL": syscall.Signal(windows.SIGILL),
"TRAP": syscall.Signal(windows.SIGTRAP),
"ABRT": syscall.Signal(windows.SIGABRT),
"BUS": syscall.Signal(windows.SIGBUS),
"FPE": syscall.Signal(windows.SIGFPE),
"KILL": syscall.Signal(windows.SIGKILL),
"SEGV": syscall.Signal(windows.SIGSEGV),
"PIPE": syscall.Signal(windows.SIGPIPE),
"ALRM": syscall.Signal(windows.SIGALRM),
"TERM": syscall.Signal(windows.SIGTERM),
}