aboutsummaryrefslogtreecommitdiffstats
path: root/signal-reset.c
blob: d16b942af034471241b7096abad2ec899bb1c8cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// this fixes signals processing for idea
// see https://youtrack.jetbrains.com/issue/IDEA-157989
// signal-reset implemented by Thomas Lee https://github.com/thomaslee/signal-reset
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>

int
main (int argc, char **argv)
{
    sigset_t sigs;
    struct sigaction sa;

    sigemptyset(&sigs);
    sigaddset(&sigs, SIGINT);
    sigaddset(&sigs, SIGTERM);
    if (sigprocmask(SIG_UNBLOCK, &sigs, 0) != 0) {
        fprintf(stderr, "sigprocmask failed\n");
        return 1;
    }

    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = SIG_DFL;
    if (sigaction(SIGINT, &sa, 0) != 0) {
        fprintf(stderr, "sigaction failed for SIGINT\n");
        return 1;
    }

    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = SIG_DFL;
    if (sigaction(SIGTERM, &sa, 0) != 0) {
        fprintf(stderr, "sigaction failed for SIGTERM\n");
        return 1;
    }

    if (argc <= 1) {
        fprintf(stderr, "usage: %s <command> [args...]\n", argv[0]);
        return 1;
    }

    execvp(argv[1], argv + 1);
    fprintf(stderr, "execvp(...) failed\n");
    return 1;
}