29 December 2011

Disable Control-C [ CTRL+C ] in linux

Signals are the notification send to a process on the occurrence of an event.

#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

struct sigaction act;

void sighandler(int signum, siginfo_t *info, void *ptr)
{
    printf("Received signal %d\n", signum);
    printf("Signal originates from process %lu\n",
        (unsigned long)info->si_pid);
}

int main()
{
    printf("I am %lu\n", (unsigned long)getpid());

    memset(&act, 0, sizeof(act));
#if 0
    act.sa_sigaction = sighandler;
    act.sa_flags = SA_SIGINFO;
    sigaction(SIGINT, &act, NULL);
#endif
#if 0
    act.sa_handler = SIG_IGN;
    sigaction(SIGINT, &act, NULL);
#endif

    signal(SIGINT, SIG_IGN);

    // Waiting for CTRL+C...
    sleep(100);

    return 0;
}

Reference:
http://nixcraft.com/shell-scripting/12605-shell-script-disable-ctrl-c-ctrl-z.html

No comments:

Post a Comment