summaryrefslogtreecommitdiff
path: root/pkg/signal/signal_common.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-05-29 06:00:48 -0400
committerGitHub <noreply@github.com>2020-05-29 06:00:48 -0400
commit6e3aec3c6d36e1e344a315e3a5a1600bac154904 (patch)
treee1cf83f982d9141e39de172e6e360556ccf03122 /pkg/signal/signal_common.go
parentf9217939489ce693db2b358fc8eab68bedac2180 (diff)
parente0eb6022b32f4261dbd5b78d958afc15ec2af297 (diff)
downloadpodman-6e3aec3c6d36e1e344a315e3a5a1600bac154904.tar.gz
podman-6e3aec3c6d36e1e344a315e3a5a1600bac154904.tar.bz2
podman-6e3aec3c6d36e1e344a315e3a5a1600bac154904.zip
Merge pull request #6419 from mheon/signal_parse_windows
Ensure that signal names can be parsed on Windows
Diffstat (limited to 'pkg/signal/signal_common.go')
-rw-r--r--pkg/signal/signal_common.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/pkg/signal/signal_common.go b/pkg/signal/signal_common.go
new file mode 100644
index 000000000..8ff4b4dbf
--- /dev/null
+++ b/pkg/signal/signal_common.go
@@ -0,0 +1,41 @@
+package signal
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+ "syscall"
+)
+
+// 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
+ }
+ sig, ok := signalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
+ if !ok {
+ return -1, fmt.Errorf("invalid signal: %s", rawSignal)
+ }
+ return sig, nil
+}
+
+// ParseSignalNameOrNumber translates a string to a valid syscall signal. Input
+// can be a name or number representation i.e. "KILL" "9"
+func ParseSignalNameOrNumber(rawSignal string) (syscall.Signal, error) {
+ basename := strings.TrimPrefix(rawSignal, "-")
+ s, err := ParseSignal(basename)
+ if err == nil {
+ return s, nil
+ }
+ for k, v := range signalMap {
+ if k == strings.ToUpper(basename) {
+ return v, nil
+ }
+ }
+ return -1, fmt.Errorf("invalid signal: %s", basename)
+}