-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Based on /~https://github.com/docker/cli/blob/ef2f64abbd37edfa148f745fa0013731b5074d1b/cli/command/container/tty.go#L99-L12 Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
- Loading branch information
1 parent
05d72a2
commit bbccf1e
Showing
7 changed files
with
295 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Package signal provides helper functions for dealing with signals across | ||
// various operating systems. | ||
// | ||
// Forked from /~https://github.com/moby/moby/tree/37defbfd9b968f38e8e15dfa5f06d9f878bd65ba/pkg/signal | ||
package signal | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
// CatchAll catches all signals and relays them to the specified channel. | ||
func CatchAll(sigc chan os.Signal) { | ||
var handledSigs []os.Signal | ||
for _, s := range SignalMap { | ||
handledSigs = append(handledSigs, s) | ||
} | ||
signal.Notify(sigc, handledSigs...) | ||
} | ||
|
||
// StopCatch stops catching the signals and closes the specified channel. | ||
func StopCatch(sigc chan os.Signal) { | ||
signal.Stop(sigc) | ||
close(sigc) | ||
} | ||
|
||
// ParseSignal translates a string to a valid syscall signal. | ||
// It returns an error if the signal map doesn't include the given signal. | ||
func ParseSignal(rawSignal string) (syscall.Signal, error) { | ||
s, err := strconv.Atoi(rawSignal) | ||
if err == nil { | ||
if s == 0 { | ||
return -1, fmt.Errorf("Invalid signal: %s", rawSignal) | ||
} | ||
return syscall.Signal(s), nil | ||
} | ||
signal, ok := SignalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")] | ||
if !ok { | ||
return -1, fmt.Errorf("Invalid signal: %s", rawSignal) | ||
} | ||
return signal, nil | ||
} | ||
|
||
// ValidSignalForPlatform returns true if a signal is valid on the platform | ||
func ValidSignalForPlatform(sig syscall.Signal) bool { | ||
for _, v := range SignalMap { | ||
if v == sig { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// +build !mips,!mipsle,!mips64,!mips64le | ||
|
||
package signal | ||
|
||
import ( | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
const ( | ||
sigrtmin = 34 | ||
sigrtmax = 64 | ||
) | ||
|
||
// SignalMap is a map of Linux signals. | ||
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, | ||
"URG": unix.SIGURG, | ||
"USR1": unix.SIGUSR1, | ||
"USR2": unix.SIGUSR2, | ||
"VTALRM": unix.SIGVTALRM, | ||
"WINCH": unix.SIGWINCH, | ||
"XCPU": unix.SIGXCPU, | ||
"XFSZ": unix.SIGXFSZ, | ||
"RTMIN": sigrtmin, | ||
"RTMIN+1": sigrtmin + 1, | ||
"RTMIN+2": sigrtmin + 2, | ||
"RTMIN+3": sigrtmin + 3, | ||
"RTMIN+4": sigrtmin + 4, | ||
"RTMIN+5": sigrtmin + 5, | ||
"RTMIN+6": sigrtmin + 6, | ||
"RTMIN+7": sigrtmin + 7, | ||
"RTMIN+8": sigrtmin + 8, | ||
"RTMIN+9": sigrtmin + 9, | ||
"RTMIN+10": sigrtmin + 10, | ||
"RTMIN+11": sigrtmin + 11, | ||
"RTMIN+12": sigrtmin + 12, | ||
"RTMIN+13": sigrtmin + 13, | ||
"RTMIN+14": sigrtmin + 14, | ||
"RTMIN+15": sigrtmin + 15, | ||
"RTMAX-14": sigrtmax - 14, | ||
"RTMAX-13": sigrtmax - 13, | ||
"RTMAX-12": sigrtmax - 12, | ||
"RTMAX-11": sigrtmax - 11, | ||
"RTMAX-10": sigrtmax - 10, | ||
"RTMAX-9": sigrtmax - 9, | ||
"RTMAX-8": sigrtmax - 8, | ||
"RTMAX-7": sigrtmax - 7, | ||
"RTMAX-6": sigrtmax - 6, | ||
"RTMAX-5": sigrtmax - 5, | ||
"RTMAX-4": sigrtmax - 4, | ||
"RTMAX-3": sigrtmax - 3, | ||
"RTMAX-2": sigrtmax - 2, | ||
"RTMAX-1": sigrtmax - 1, | ||
"RTMAX": sigrtmax, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// +build linux | ||
// +build mips mipsle mips64 mips64le | ||
|
||
package signal | ||
|
||
import ( | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
const ( | ||
sigrtmin = 34 | ||
sigrtmax = 127 | ||
) | ||
|
||
// SignalMap is a map of Linux signals. | ||
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, | ||
"EMT": unix.SIGEMT, | ||
"STOP": unix.SIGSTOP, | ||
"SYS": unix.SIGSYS, | ||
"TERM": unix.SIGTERM, | ||
"TRAP": unix.SIGTRAP, | ||
"TSTP": unix.SIGTSTP, | ||
"TTIN": unix.SIGTTIN, | ||
"TTOU": unix.SIGTTOU, | ||
"URG": unix.SIGURG, | ||
"USR1": unix.SIGUSR1, | ||
"USR2": unix.SIGUSR2, | ||
"VTALRM": unix.SIGVTALRM, | ||
"WINCH": unix.SIGWINCH, | ||
"XCPU": unix.SIGXCPU, | ||
"XFSZ": unix.SIGXFSZ, | ||
"RTMIN": sigrtmin, | ||
"RTMIN+1": sigrtmin + 1, | ||
"RTMIN+2": sigrtmin + 2, | ||
"RTMIN+3": sigrtmin + 3, | ||
"RTMIN+4": sigrtmin + 4, | ||
"RTMIN+5": sigrtmin + 5, | ||
"RTMIN+6": sigrtmin + 6, | ||
"RTMIN+7": sigrtmin + 7, | ||
"RTMIN+8": sigrtmin + 8, | ||
"RTMIN+9": sigrtmin + 9, | ||
"RTMIN+10": sigrtmin + 10, | ||
"RTMIN+11": sigrtmin + 11, | ||
"RTMIN+12": sigrtmin + 12, | ||
"RTMIN+13": sigrtmin + 13, | ||
"RTMIN+14": sigrtmin + 14, | ||
"RTMIN+15": sigrtmin + 15, | ||
"RTMAX-14": sigrtmax - 14, | ||
"RTMAX-13": sigrtmax - 13, | ||
"RTMAX-12": sigrtmax - 12, | ||
"RTMAX-11": sigrtmax - 11, | ||
"RTMAX-10": sigrtmax - 10, | ||
"RTMAX-9": sigrtmax - 9, | ||
"RTMAX-8": sigrtmax - 8, | ||
"RTMAX-7": sigrtmax - 7, | ||
"RTMAX-6": sigrtmax - 6, | ||
"RTMAX-5": sigrtmax - 5, | ||
"RTMAX-4": sigrtmax - 4, | ||
"RTMAX-3": sigrtmax - 3, | ||
"RTMAX-2": sigrtmax - 2, | ||
"RTMAX-1": sigrtmax - 1, | ||
"RTMAX": sigrtmax, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// +build !windows | ||
|
||
package signal | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
// Signals used in cli/command (no windows equivalent, use | ||
// invalid signals so they don't get handled) | ||
|
||
const ( | ||
// SIGCHLD is a signal sent to a process when a child process terminates, is interrupted, or resumes after being interrupted. | ||
SIGCHLD = syscall.SIGCHLD | ||
// SIGWINCH is a signal sent to a process when its controlling terminal changes its size | ||
SIGWINCH = syscall.SIGWINCH | ||
// SIGPIPE is a signal sent to a process when a pipe is written to before the other end is open for reading | ||
SIGPIPE = syscall.SIGPIPE | ||
// DefaultStopSignal is the syscall signal used to stop a container in unix systems. | ||
DefaultStopSignal = "SIGTERM" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package sigproxy | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/sirupsen/logrus" | ||
"golang.org/x/sys/unix" | ||
|
||
"github.com/rootless-containers/rootlesskit/pkg/sigproxy/signal" | ||
) | ||
|
||
// ForwardAllSignals forwards signals. | ||
// Based on /~https://github.com/docker/cli/blob/ef2f64abbd37edfa148f745fa0013731b5074d1b/cli/command/container/tty.go#L99-L126 | ||
func ForwardAllSignals(ctx context.Context, pid int) chan os.Signal { | ||
sigc := make(chan os.Signal, 128) | ||
signal.CatchAll(sigc) | ||
go func() { | ||
for s := range sigc { | ||
if s == unix.SIGCHLD || s == unix.SIGPIPE || s == unix.SIGURG { | ||
continue | ||
} | ||
us, ok := s.(unix.Signal) | ||
if !ok { | ||
logrus.Warnf("Unsupported signal %v", s) | ||
continue | ||
} | ||
if err := unix.Kill(pid, us); err != nil { | ||
logrus.WithError(err).Debugf("Error sending signal %v", s) | ||
} | ||
} | ||
}() | ||
return sigc | ||
} |