Skip to content

Commit f0d5dd3

Browse files
committed
Use unix.SignalNum in ParseSignal on unix platform
This removes the signalMap on unix platform, since the signalMap on different architectures is not same, especially it's wrong on mipsx. golang.org/x/sys/unix now has a SignalNum func to convert signal name to a number, thus there's no need to keep this redundant map. Windows platform still needs to have a signalMap, since golang.org/x/sys/windows doesn't have corresponding functions. Address: #3061 Signed-off-by: Shengjing Zhu <[email protected]>
1 parent 1745951 commit f0d5dd3

6 files changed

Lines changed: 119 additions & 141 deletions

File tree

signal_map_linux.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

signal_map_unix.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

signals.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23-
"strconv"
24-
"strings"
2523
"syscall"
2624

2725
"github.com/containerd/containerd/content"
2826
"github.com/containerd/containerd/images"
29-
"github.com/opencontainers/image-spec/specs-go/v1"
27+
v1 "github.com/opencontainers/image-spec/specs-go/v1"
3028
)
3129

3230
// StopSignalLabel is a well-known containerd label for storing the stop
@@ -83,23 +81,3 @@ func GetOCIStopSignal(ctx context.Context, image Image, defaultSignal string) (s
8381

8482
return config.StopSignal, nil
8583
}
86-
87-
// ParseSignal parses a given string into a syscall.Signal
88-
// it checks that the signal exists in the platform-appropriate signalMap
89-
func ParseSignal(rawSignal string) (syscall.Signal, error) {
90-
s, err := strconv.Atoi(rawSignal)
91-
if err == nil {
92-
sig := syscall.Signal(s)
93-
for _, msig := range signalMap {
94-
if sig == msig {
95-
return sig, nil
96-
}
97-
}
98-
return -1, fmt.Errorf("unknown signal %q", rawSignal)
99-
}
100-
signal, ok := signalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
101-
if !ok {
102-
return -1, fmt.Errorf("unknown signal %q", rawSignal)
103-
}
104-
return signal, nil
105-
}

signals_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package containerd
18+
19+
import (
20+
"fmt"
21+
"syscall"
22+
"testing"
23+
)
24+
25+
func TestParseSignal(t *testing.T) {
26+
testSignals := []struct {
27+
raw string
28+
want syscall.Signal
29+
err bool
30+
}{
31+
{"1", syscall.Signal(1), false},
32+
{"SIGKILL", syscall.SIGKILL, false},
33+
{"NONEXIST", 0, true},
34+
{"65536", 0, true},
35+
}
36+
for _, ts := range testSignals {
37+
t.Run(fmt.Sprintf("%s/%d/%t", ts.raw, ts.want, ts.err), func(t *testing.T) {
38+
got, err := ParseSignal(ts.raw)
39+
if ts.err && err == nil {
40+
t.Errorf("ParseSignal(%s) should return error", ts.raw)
41+
}
42+
if !ts.err && got != ts.want {
43+
t.Errorf("ParseSignal(%s) return %d, want %d", ts.raw, got, ts.want)
44+
}
45+
})
46+
}
47+
}

signals_unix.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package containerd
20+
21+
import (
22+
"fmt"
23+
"strconv"
24+
"strings"
25+
"syscall"
26+
27+
"golang.org/x/sys/unix"
28+
)
29+
30+
// ParseSignal parses a given string into a syscall.Signal
31+
// the rawSignal can be a string with "SIG" prefix,
32+
// or a signal number in string format.
33+
func ParseSignal(rawSignal string) (syscall.Signal, error) {
34+
s, err := strconv.Atoi(rawSignal)
35+
if err == nil {
36+
signal := syscall.Signal(s)
37+
if unix.SignalName(signal) != "" {
38+
return signal, nil
39+
}
40+
return -1, fmt.Errorf("unknown signal %q", rawSignal)
41+
}
42+
signal := unix.SignalNum(strings.ToUpper(rawSignal))
43+
if signal == 0 {
44+
return -1, fmt.Errorf("unknown signal %q", rawSignal)
45+
}
46+
return signal, nil
47+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package containerd
1818

1919
import (
20+
"fmt"
21+
"strconv"
22+
"strings"
2023
"syscall"
2124

2225
"golang.org/x/sys/windows"
@@ -37,3 +40,24 @@ var signalMap = map[string]syscall.Signal{
3740
"ALRM": syscall.Signal(windows.SIGALRM),
3841
"TERM": syscall.Signal(windows.SIGTERM),
3942
}
43+
44+
// ParseSignal parses a given string into a syscall.Signal
45+
// the rawSignal can be a string with "SIG" prefix,
46+
// or a signal number in string format.
47+
func ParseSignal(rawSignal string) (syscall.Signal, error) {
48+
s, err := strconv.Atoi(rawSignal)
49+
if err == nil {
50+
sig := syscall.Signal(s)
51+
for _, msig := range signalMap {
52+
if sig == msig {
53+
return sig, nil
54+
}
55+
}
56+
return -1, fmt.Errorf("unknown signal %q", rawSignal)
57+
}
58+
signal, ok := signalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
59+
if !ok {
60+
return -1, fmt.Errorf("unknown signal %q", rawSignal)
61+
}
62+
return signal, nil
63+
}

0 commit comments

Comments
 (0)