ctr: move signals to commands package
Signed-off-by: Jess Valarezo <valarezo.jessica@gmail.com>
This commit is contained in:
parent
638f3a3380
commit
ffd0d2ef58
@ -55,8 +55,8 @@ var taskAttachCommand = cli.Command{
|
||||
logrus.WithError(err).Error("console resize")
|
||||
}
|
||||
} else {
|
||||
sigc := forwardAllSignals(ctx, task)
|
||||
defer stopCatch(sigc)
|
||||
sigc := commands.ForwardAllSignals(ctx, task)
|
||||
defer commands.StopCatch(sigc)
|
||||
}
|
||||
|
||||
ec := <-statusC
|
||||
|
59
cmd/ctr/commands/signals.go
Normal file
59
cmd/ctr/commands/signals.go
Normal file
@ -0,0 +1,59 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
gocontext "context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type killer interface {
|
||||
Kill(gocontext.Context, syscall.Signal, ...containerd.KillOpts) error
|
||||
}
|
||||
|
||||
// ForwardAllSignals forwards signals
|
||||
func ForwardAllSignals(ctx gocontext.Context, task killer) chan os.Signal {
|
||||
sigc := make(chan os.Signal, 128)
|
||||
signal.Notify(sigc)
|
||||
go func() {
|
||||
for s := range sigc {
|
||||
logrus.Debug("forwarding signal ", s)
|
||||
if err := task.Kill(ctx, s.(syscall.Signal)); err != nil {
|
||||
logrus.WithError(err).Errorf("forward signal %s", s)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return sigc
|
||||
}
|
||||
|
||||
// StopCatch stops and closes a channel
|
||||
func StopCatch(sigc chan os.Signal) {
|
||||
signal.Stop(sigc)
|
||||
close(sigc)
|
||||
}
|
||||
|
||||
// ParseSignal parses a given string into a syscall.Signal
|
||||
// it checks that the signal exists in the platform-appropriate signalMap
|
||||
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
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package commands
|
||||
|
||||
import (
|
||||
"syscall"
|
@ -1,6 +1,6 @@
|
||||
// +build darwin freebsd solaris
|
||||
|
||||
package main
|
||||
package commands
|
||||
|
||||
import (
|
||||
"syscall"
|
23
cmd/ctr/commands/signals_windows.go
Normal file
23
cmd/ctr/commands/signals_windows.go
Normal file
@ -0,0 +1,23 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
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),
|
||||
}
|
@ -10,6 +10,7 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
//TODO:(jessvalarezo) exec-id is optional here, update to required arg
|
||||
var taskExecCommand = cli.Command{
|
||||
Name: "exec",
|
||||
Usage: "execute additional processes in an existing container",
|
||||
@ -87,8 +88,8 @@ var taskExecCommand = cli.Command{
|
||||
logrus.WithError(err).Error("console resize")
|
||||
}
|
||||
} else {
|
||||
sigc := forwardAllSignals(ctx, process)
|
||||
defer stopCatch(sigc)
|
||||
sigc := commands.ForwardAllSignals(ctx, process)
|
||||
defer commands.StopCatch(sigc)
|
||||
}
|
||||
|
||||
if err := process.Start(ctx); err != nil {
|
||||
|
@ -6,6 +6,8 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// TODO:(jessvalarezo) the pid flag is not used here
|
||||
// update to be able to signal given pid
|
||||
var taskKillCommand = cli.Command{
|
||||
Name: "kill",
|
||||
Usage: "signal a container (default: SIGTERM)",
|
||||
@ -31,7 +33,7 @@ var taskKillCommand = cli.Command{
|
||||
if id == "" {
|
||||
return errors.New("container id must be provided")
|
||||
}
|
||||
signal, err := parseSignal(context.String("signal"))
|
||||
signal, err := commands.ParseSignal(context.String("signal"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
gocontext "context"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"syscall"
|
||||
|
||||
"github.com/containerd/console"
|
||||
"github.com/containerd/containerd"
|
||||
@ -20,10 +19,6 @@ type resizer interface {
|
||||
Resize(ctx gocontext.Context, w, h uint32) error
|
||||
}
|
||||
|
||||
type killer interface {
|
||||
Kill(gocontext.Context, syscall.Signal, ...containerd.KillOpts) error
|
||||
}
|
||||
|
||||
func withEnv(context *cli.Context) containerd.SpecOpts {
|
||||
return func(_ gocontext.Context, _ *containerd.Client, _ *containers.Container, s *specs.Spec) error {
|
||||
env := context.StringSlice("env")
|
||||
@ -160,8 +155,8 @@ var runCommand = cli.Command{
|
||||
logrus.WithError(err).Error("console resize")
|
||||
}
|
||||
} else {
|
||||
sigc := forwardAllSignals(ctx, task)
|
||||
defer stopCatch(sigc)
|
||||
sigc := commands.ForwardAllSignals(ctx, task)
|
||||
defer commands.StopCatch(sigc)
|
||||
}
|
||||
status := <-statusC
|
||||
code, _, err := status.Result()
|
||||
|
@ -70,8 +70,8 @@ var taskStartCommand = cli.Command{
|
||||
logrus.WithError(err).Error("console resize")
|
||||
}
|
||||
} else {
|
||||
sigc := forwardAllSignals(ctx, task)
|
||||
defer stopCatch(sigc)
|
||||
sigc := commands.ForwardAllSignals(ctx, task)
|
||||
defer commands.StopCatch(sigc)
|
||||
}
|
||||
|
||||
status := <-statusC
|
||||
|
@ -8,11 +8,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/containerd/console"
|
||||
@ -20,7 +16,6 @@ import (
|
||||
"github.com/containerd/containerd/remotes/docker"
|
||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -95,43 +90,6 @@ func getResolver(ctx gocontext.Context, clicontext *cli.Context) (remotes.Resolv
|
||||
return docker.NewResolver(options), nil
|
||||
}
|
||||
|
||||
func forwardAllSignals(ctx gocontext.Context, task killer) chan os.Signal {
|
||||
sigc := make(chan os.Signal, 128)
|
||||
signal.Notify(sigc)
|
||||
go func() {
|
||||
for s := range sigc {
|
||||
logrus.Debug("forwarding signal ", s)
|
||||
if err := task.Kill(ctx, s.(syscall.Signal)); err != nil {
|
||||
logrus.WithError(err).Errorf("forward signal %s", s)
|
||||
}
|
||||
}
|
||||
}()
|
||||
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)
|
||||
}
|
||||
|
||||
// parseMountFlag parses a mount string in the form "type=foo,source=/path,destination=/target,options=rbind:rw"
|
||||
func parseMountFlag(m string) (specs.Mount, error) {
|
||||
mount := specs.Mount{}
|
||||
|
@ -5,12 +5,10 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/Microsoft/go-winio"
|
||||
clog "github.com/containerd/containerd/log"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup, error) {
|
||||
@ -91,19 +89,3 @@ 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),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user