Skip to content
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

x/sys/unix: add SignalNum() to convert signal name to a number #28027

Closed
kolyshkin opened this issue Oct 4, 2018 · 14 comments
Closed

x/sys/unix: add SignalNum() to convert signal name to a number #28027

kolyshkin opened this issue Oct 4, 2018 · 14 comments

Comments

@kolyshkin
Copy link
Contributor

kolyshkin commented Oct 4, 2018

A SignalName() method was recently (#25134) added to x/sys/unix to convert a numeric signal id to a name. It uses signalList 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.

// SignalNum returns the syscall.Signal for signal named s,
// or an error if a signal with such name is not found.
func SignalNum(s string) (syscall.Signal, error)

For example, SignalNum("SIGTERM") is to return 15, nil, and it should probably do the same for SignalNum("TERM") and SignalNum("term").

Update: s/SignalId/SignalNum/

@gopherbot gopherbot added this to the Unreleased milestone Oct 4, 2018
@ianlancetaylor
Copy link
Contributor

Now that we have SignalName, you can write this yourself. Is this really a common enough desire to put it in the package?

@kolyshkin
Copy link
Contributor Author

Currently quite a few packages have to implement it, here are a few examples:

@kolyshkin
Copy link
Contributor Author

An alternative would be to expose signalList, which I think is much less maintainable long term.

@tklauser
Copy link
Member

tklauser commented Oct 5, 2018

@ianlancetaylor I don't think we can use SignalName to implement that as it maps a signal number to a string. But what's proposed here is the reverse operation (string to signal number). Or am I missing something?

@kolyshkin I'd propose to name the proposed function SignalNum instead of SignalId, as the signal(2) and signal(7) use the term signal number to refer to the numeric signal id.

@ianlancetaylor
Copy link
Contributor

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

@tklauser
Copy link
Member

tklauser commented Oct 5, 2018

Are signal number guaranteed to be contiguous? At least on linux they seem to be. But looking at the recently added zerrors_aix_*.go I see:

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.

@tklauser
Copy link
Member

tklauser commented Oct 5, 2018

We could add unix.NSIG though and then do:

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
}

@ondrej-fabry
Copy link

ondrej-fabry commented Oct 5, 2018

@tklauser It also needs some check if s == "" otherwise it will return first undefined signal.

EDIT: by the way, why not loop over signalList slice?

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..

@tklauser
Copy link
Member

tklauser commented Oct 5, 2018

@ondrej-fabry oops, right. Thank you. The proposed SignalNum is supposed to be implemented outside of golang.org/x/sys/unix (see #28027 (comment)) thus we cannot use signalList directly.

@ondrej-fabry
Copy link

ondrej-fabry commented Oct 5, 2018

@tklauser right, got that

btw, loop could start from 0 to avoid need for extra check of s == "" and loop until 255 for maximum possible signal number to eliminate need to define unix.NSIG.

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

@kolyshkin
Copy link
Contributor Author

As previous comments demonstrate, it is slightly less than trivial to write SignalNum() implementation based on unix.SignalName(). Perhaps it is easier to add SignalNum() right into the package?

@kolyshkin kolyshkin changed the title x/sys/unix: add SignalId() to convert signal name to a number x/sys/unix: add SignalNum() to convert signal name to a number Oct 8, 2018
@kolyshkin
Copy link
Contributor Author

I have updated title and description to use SignalNum() rather than SignalId() as per @tklauser suggestion at #28027 (comment)

@tklauser
Copy link
Member

@kolyshkin I agree, it might be better to just add SignalNum to the package. Want to send a CL?

@gopherbot
Copy link

Change https://golang.org/cl/164778 mentions this issue: unix: add SignalNum to convert signal name to a number

@golang golang locked and limited conversation to collaborators Mar 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants