...
Code Block | ||
---|---|---|
| ||
void handler(int signum) { if (signal(signum, handler); == SIG_ERR) { /* handle error */ } /* handle signal */ } /* ... */ if (signal(SIGUSR1, handler);) == SIG_ERR) { /* handle error */ } |
On non-persistent platforms, this solution contains a race window, starting when the host environment resets the signal and ending when the handler calls signal()
. During that time, a second signal sent to the program will trigger the default signal behavior, consequently defeating the persistent behavior implied by the call to signal()
from within the handler to reassert the binding.
...
Code Block | ||
---|---|---|
| ||
void handler(int signum) { /* handle signal */ } /* ... */ if (signal(SIGUSR1, handler);) == SIG_ERR) { /* handle error */ } |
Compliant Solution (POSIX)
...
Code Block | ||
---|---|---|
| ||
void handler(int signum) { #ifndef WINDOWS if (signal(signum, SIG_DFL); == SIG_ERR) { /* handle error */ } #endif /* handle signal */ } /* ... */ if (signal(SIGUSR1, handler); == SIG_ERR) { /* handle error */ } |
Risk Assessment
Two signals in quick succession can trigger the race condition on non-persistent platforms, causing the signal's default behavior despite a handler's attempt to override it.
...