-
Notifications
You must be signed in to change notification settings - Fork 18k
x/sys/unix: add SignalNum() to convert signal name to a number #28027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Now that we have |
Currently quite a few packages have to implement it, here are a few examples: |
An alternative would be to expose |
@ianlancetaylor I don't think we can use @kolyshkin I'd propose to name the proposed function |
func SignalNum(s string) syscall.Signal {
for i := syscall.Signal(1); ; i++ {
sn := unix.SignalName(i)
if sn == s {
return i
}
if sn == "" {
return 0
}
}
} |
Are signal number guaranteed to be contiguous? At least on linux they seem to be. But looking at the recently added var signalList = [...]struct {
num syscall.Signal
name string
desc string
}{
// ...
{39, "SIGWAITING", "signal 39"},
{48, "SIGSYSERROR", "signal 48"},
{49, "SIGCAPI", "signal 49"},
{58, "SIGRECONFIG", "signal 58"},
{59, "SIGCPUFAIL", "CPU Failure Predicted"},
{60, "SIGGRANT", "monitor mode granted"},
{61, "SIGRETRACT", "monitor mode retracted"},
{62, "SIGSOUND", "sound completed"},
{63, "SIGMAX32", "secure attention"},
{255, "SIGMAX", "signal 255"},
} so on aix something like num := SignalNum("SIGSYSERROR") wouldn't work. |
We could add func SignalNum(s string) syscall.Signal {
for i := syscall.Signal(1); i < syscall.Signal(unix.NSIG); i++ {
sn := unix.SignalName(i)
if sn == s {
return i
}
}
return 0
} |
@tklauser It also needs some check if EDIT: func SignalNum(name string) syscall.Signal {
for _, s := range signalList {
if s.name == name {
return s.num
}
}
return 0
} oh I guess you were actually talking about implementation outside proposed package.. |
@ondrej-fabry oops, right. Thank you. The proposed |
@tklauser right, got that btw, loop could start from 0 to avoid need for extra check of func SignalNum(s string) syscall.Signal {
for i := syscall.Signal(0); i < syscall.Signal(255); i++ {
sn := unix.SignalName(i)
if sn == s {
return i
}
}
return 0
}
SignalNum("") // will return 0 |
As previous comments demonstrate, it is slightly less than trivial to write |
I have updated title and description to use |
@kolyshkin I agree, it might be better to just add |
Change https://golang.org/cl/164778 mentions this issue: |
A
SignalName()
method was recently (#25134) added to x/sys/unix to convert a numeric signal id to a name. It usessignalList
array which contains names and numeric values for all known signals on every platform.This is a proposal to add a complementary method to convert a string representation to a number.
For example,
SignalNum("SIGTERM")
is to return15, nil
, and it should probably do the same forSignalNum("TERM")
andSignalNum("term")
.Update:
s/SignalId/SignalNum/
The text was updated successfully, but these errors were encountered: