mirror of
https://github.com/rwinkhart/sys.git
synced 2026-08-31 22:36:44 -04:00
unix: add SignalNum to convert signal name to a number
Fixes golang/go#28027 Change-Id: Idf8b554e0fd102fd8b2f2f2ea0fa47bf139303da Reviewed-on: https://go-review.googlesource.com/c/164778 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
committed by
Tobias Klauser
parent
b6889370fb
commit
d455e41777
@@ -28,6 +28,11 @@ var (
|
|||||||
errENOENT error = syscall.ENOENT
|
errENOENT error = syscall.ENOENT
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
signalNameMapOnce sync.Once
|
||||||
|
signalNameMap map[string]syscall.Signal
|
||||||
|
)
|
||||||
|
|
||||||
// errnoErr returns common boxed Errno values, to prevent
|
// errnoErr returns common boxed Errno values, to prevent
|
||||||
// allocations at runtime.
|
// allocations at runtime.
|
||||||
func errnoErr(e syscall.Errno) error {
|
func errnoErr(e syscall.Errno) error {
|
||||||
@@ -66,6 +71,19 @@ func SignalName(s syscall.Signal) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SignalNum returns the syscall.Signal for signal named s,
|
||||||
|
// or 0 if a signal with such name is not found.
|
||||||
|
// The signal name should start with "SIG".
|
||||||
|
func SignalNum(s string) syscall.Signal {
|
||||||
|
signalNameMapOnce.Do(func() {
|
||||||
|
signalNameMap = make(map[string]syscall.Signal)
|
||||||
|
for _, signal := range signalList {
|
||||||
|
signalNameMap[signal.name] = signal.num
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return signalNameMap[s]
|
||||||
|
}
|
||||||
|
|
||||||
// clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
|
// clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
|
||||||
func clen(n []byte) int {
|
func clen(n []byte) int {
|
||||||
i := bytes.IndexByte(n, 0)
|
i := bytes.IndexByte(n, 0)
|
||||||
|
|||||||
@@ -98,6 +98,27 @@ func TestErrnoSignalName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSignalNum(t *testing.T) {
|
||||||
|
testSignals := []struct {
|
||||||
|
name string
|
||||||
|
want syscall.Signal
|
||||||
|
}{
|
||||||
|
{"SIGHUP", syscall.SIGHUP},
|
||||||
|
{"SIGPIPE", syscall.SIGPIPE},
|
||||||
|
{"SIGSEGV", syscall.SIGSEGV},
|
||||||
|
{"NONEXISTS", 0},
|
||||||
|
}
|
||||||
|
for _, ts := range testSignals {
|
||||||
|
t.Run(fmt.Sprintf("%s/%d", ts.name, ts.want), func(t *testing.T) {
|
||||||
|
got := unix.SignalNum(ts.name)
|
||||||
|
if got != ts.want {
|
||||||
|
t.Errorf("SignalNum(%s) returned %d, want %d", ts.name, got, ts.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFcntlInt(t *testing.T) {
|
func TestFcntlInt(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
file, err := ioutil.TempFile("", "TestFnctlInt")
|
file, err := ioutil.TempFile("", "TestFnctlInt")
|
||||||
|
|||||||
Reference in New Issue
Block a user