Is there a way to make process-internal-only conditionally-interrupting signals?
I'm looking for a way to, from within a signal handler, conditionally interrupt a syscall that way taking place at the time the signal was handled. To make this concrete, suppose a call to read
is in process, and SIGRT0
is received. This signal handler uses SA_RESTART
because it does not want to unconditionally interrupt syscalls, but depending on a condition, I want to cause read
to return EINTR
immediately once the signal handler returns.
One way I could do this is by setting up another signal handler for SIGRT1
, putting SIGRT1
in the signal mask for SIGRT0
's handler, and omitting SA_RESTART
from the SIGRT1
handler. Then the handler for SIGRT0
can raise
SIGRT1
, and when the first, non-interrupting signal handler returns, the second one will fire and read
gets interrupted.
The problem 开发者_开发百科with this solution is that other processes could send SIGRT1
, causing unwanted EINTR
occurrences.
Is there any way to achieve the result I'm looking for?
If you want to set a particular process to send that signal then you can any IPC techniques (e.g. pipe) to share its pid id and flags to make sure that signal was sent by that process. If signal wasn't sent by the process then just ignore it.
What I wanted was impossible for multiple reasons. Perhaps most importantly, the secondary signal that was intended to do the interrupting would potentially (and in reality on most systems) fire as soon as the first signal handler returned but before the interrupted syscall got restarted. Then the syscall would restart and continue to block.
Perhaps more importantly, any attempt to interrupt blocking syscalls with EINTR
intentionally is subject to race conditions where the signal arrives just before the blocking syscall, but after whatever check would have prevented making the syscall due to having received the signal. The only time this might be acceptable is when you're prepared to fire off multiple signals with increasing delays between them until the "interupt request" is honored, but that's getting into the realm of flaky hacks...
精彩评论