Skip to content

Commit 30b6f46

Browse files
Merge pull request #3063 from zhsj/fix-mipsx
Use unix.SignalNum in ParseSignal on unix platform
2 parents 63328c1 + f0d5dd3 commit 30b6f46

46 files changed

Lines changed: 873 additions & 263 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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+
}

vendor.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ golang.org/x/net b3756b4b77d7b13260a0a2ec658753cf48922eac
2828
google.golang.org/grpc v1.12.0
2929
github.com/pkg/errors v0.8.0
3030
github.com/opencontainers/go-digest c9281466c8b2f606084ac71339773efd177436e7
31-
golang.org/x/sys 41f3e6584952bb034a481797859f6ab34b6803bd https://github.com/golang/sys
31+
golang.org/x/sys d455e41777fca6e8a5a79e34a14b8368bc11d9ba https://github.com/golang/sys
3232
github.com/opencontainers/image-spec v1.0.1
3333
golang.org/x/sync 42b317875d0fa942474b76e1b46a6060d720ae6e
3434
github.com/BurntSushi/toml a368813c5e648fee92e5f6c30e3944ff9d5e8895

vendor/golang.org/x/sys/cpu/byteorder.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/sys/cpu/cpu.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)