How can I know the current state of signals
Is there a way in C to know the following information about signals:
- Is certain signal is blocked now?
- Are we i开发者_如何学编程nside a signal handling function chanin (i.e, was the current code called from function which was called as signal handler for certain signal)? If so, can I know what is the current signal?
Thanks
You can know which signals are currently blocked by calling sigprocmask
with second argument null and third argument non-null (the first argument is ignored under these conditions, so pass zero). It'll fill in the sigset_t
you provide as the third argument.
I'm not aware of any way to know whether there is a signal handler frame on the stack. I suppose you might be able to use the _Unwind_*
family of functions somehow but it would be a horrible kludge.
Firstly, you can use sigprocmask with an empty set pointer.
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
a. how can be set to:
SIG_UNBLOCK
(the signal in set are removed from the current set of blocked signals. It is legal to attempt to unblock signal which is not blocked)
b. set
can be set to NULL
(as you don't want to change the blocked signals)
c. If oldset
is not NULL
, the previous value of the signal mask is stored in oldset. Ergo, you get the blocked signals in the location whose address is stored in oldset.
Secondly, for knowing if you are in a signal handling routine, when you write the signal handler definition, you can accept int signum as a parameter, as in:
void mySignalHandler(int signum);
If you want to know so that you can block some other signals at that point of time, you could just have a blocking function at the start & unblocking function at the end (using sigprocmask()
). You could even set said signals to SIG_IGN
status to ignore them while handling the current signal (using signal()
).
Lastly, please read the man pages!
Edit: Since the author says he does read them, I recommend using the apropos command to find such hard-to-find functions. For example,
$ apropos "blocked signals"
gives you around 5 hits, 1 of which is sigprocmask
Cheers!
精彩评论