...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <signal.h> void log_msg(int signum) { /* Log error message in some asynchronous-safe manner */ } void handler(int signum) { /* Do some handling specific to SIGINT */ log_msg(SIGUSR1); } int main(void) { if (signal(SIGUSR1, log_msg) == SIG_ERR) { /* Handle error */ } if (signal(SIGINT, handler) == SIG_ERR) { /* handle error */ } /* program code */ if (raise(SIGINT) != 0) { /* Handle error */ } /* More code */ return 0; } |
...
Noncompliant Code Example (POSIX)
The POSIX standard is contradictory regarding raise()
in signal handlers. The POSIX standard [Open Group 2004] prohibits signal handlers installed using signal()
from calling the raise()
function if the signal occurs as the result of calling the raise()
, kill()
, pthread_kill()
, or sigqueue()
functions. However, it also requires that the raise()
function may be safely called within any signal handler. Consequently, it is not clear whether it is safe for POSIX applications to call raise()
in signal handlers installed using signal()
, but it is safe to call raise()
in signal handlers installed using sigaction()
.
In this non-compliant solutioncode example, the signal handlers are installed using signal()
and raise()
is called inside the signal handler.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <signal.h> void log_msg(int signum) { /* Log error message */ } void handler(int signum) { /* Do some handling specific to SIGINT */ if (raise(SIGUSR1) != 0) { /* violation */ /* Handle error */ } } int main(void) { signal(SIGUSR1, log_msg); signal(SIGINT, handler); /* program code */ if (raise(SIGINT) != 0) { /* Handle error */ } /* More code */ return 0; } |
...