Talk:C signal handling
This is the talk page for discussing improvements to the C signal handling article. This is not a forum for general discussion of the article's subject. |
Article policies
|
Find sources: Google (books · news · scholar · free images · WP refs) · FENS · JSTOR · TWL |
![]() | C/C++ Start‑class High‑importance | ||||||||||||
|
POSIX vs Standard C
Some POSIX functions are mentioned without it being clear that they are not part of the C standard library. kill
and sigaction
are part of POSIX (but POSIX prescribes that they be defined in signal.h
).
I'm not sure whether POSIX-specific C signal handling should be in scope for this article. I am not aware of an existing article which covers it. Maybe it would be more relevant in Signal (IPC)#POSIX Signals. If not, we should at least split POSIX-specific information into its own section within this article.
DpEpsilon ( talk | contribs ) 04:44, 29 December 2018 (UTC)
Correct code in "Example usage"
In the "Example usage" the following code is found:
#include <signal.h> #include <stdio.h> #include <stdlib.h> static void catch_function(int signo) { puts("Interactive attention signal caught."); }
However, this goes against security guidelines, as well as the C standard. For example, SEI's "SIG30-C. Call only asynchronous-safe functions within signal handlers" rule.
The C Standard, 7.14.1.1, paragraph 5 (ISO/IEC 9899:2011), states that if the signal occurs other than as the result of calling the abort() or raise() function, the behavior is undefined if:
"...the signal handler calls any function in the standard library other than the abort function, the _Exit function, the quick_exit function, or the signal function with the first argument equal to the signal number corresponding to the signal that caused the invocation of the handler."
The function `puts` is part of the C standard library and is not asynchronous safe. It is not safe to use within a signal handler.
According to the C Rationale, 7.14.1.1 [C99 Rationale 2003], "When a signal occurs, the normal flow of control of a program is interrupted. If a signal occurs that is being trapped by a signal handler, that handler is invoked. When it is finished, execution continues at the point at which the signal occurred. This arrangement can cause problems if the signal handler invokes a library function that was being executed at the time of the signal."
It is better for the software to set a volatile atomic flag when a signal is trapped, and part of the normal flow control of the software should check that flag and respond appropriately.
A compliant example from the SEI wikipage (see above):
#include <signal.h> #include <stdio.h> #include <stdlib.h> enum { MAXLINE = 1024 }; volatile sig_atomic_t eflag = 0; char *info = NULL; void log_message(void) { fputs(info, stderr); } void handler(int signum) { eflag = 1; } int main(void) { if (signal(SIGINT, handler) == SIG_ERR) { /* Handle error */ } info = (char *)malloc(MAXLINE); if (info == NULL) { /* Handle error */ } while (!eflag) { /* Main loop program code */ log_message(); /* More program code */ } log_message(); free(info); info = NULL; return 0; }