Jump to content

Talk:C signal handling

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 2a02:ab88:c80:c080:e5d2:4890:f705:a0c5 (talk) at 06:58, 8 April 2022 (Correct code in "Example usage": new section). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
WikiProject iconC/C++ Start‑class High‑importance
WikiProject iconThis article is within the scope of WikiProject C/C++, a collaborative effort to improve the coverage of C and C++ topics on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
StartThis article has been rated as Start-class on Wikipedia's content assessment scale.
HighThis article has been rated as High-importance on the importance scale.
Taskforce icon
This article falls within the scope of C.

Template:IEP assignment

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)[reply]

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;
}