Hi,

I have a problem with signal handler algorithm in linux. My code is hanging ( It is continuously looping inside the signal handler) . I am pasting my code here...

any help is appreciated

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

struct sigaction oldHandler;

void myHandler(int sig,  siginfo_t *siginfo, void *context) {
// if i have not written any code inside this function, the program will give a feel of hang( this functions is getting continuously called
        if(siginfo->si_code == SEGV_MAPERR)
        {
                write(1,"address not mapped to object",strlen("address not mapped to object"));
        }
        else if (siginfo->si_code == SEGV_ACCERR)
        {
                write(1,"invalid permissions for mapped object",strlen("invalid permissions for mapped object"));
        }

       write(1,"\n",1);

 //        exit(0); if this exit(0) is not present, program will get continuous calls to this signal handler
        return;
}

int main(int argc, char *argv[]) {

    /* Install mySignalHandler for SIGSEGV */
    struct sigaction sigAct;
    int              status = 0;
    char *addr = NULL;

    sigAct.sa_handler   = 0;
    sigAct.sa_sigaction = myHandler;
    sigfillset(&sigAct.sa_mask);
    sigAct.sa_flags = SA_SIGINFO;

    status = sigaction(SIGSEGV, &sigAct, &oldHandler);
    if (status != 0) {
        perror("Failed to install handler for signal SIGSEGV");
        exit(1);
    }
#if 1
    /* This will invoke the signal handler */
// addr = malloc(strlen("Hello"));
     strcpy(addr,"Hello");
    printf("%s\n",addr);

#endif
        printf("Returning from main\n");
    return 0;
}

Hi Sree_ec,
That is how this particular signal works. Check out this link. After a "Segmentation fault", it is not possible for the program to continue and it will still continue to handle the signal. This step is causing the loop.
The text says that - "The handler should end by specifying the default action for the signal that happened and then reraising it; this will cause the program to terminate with that signal, as if it had not had a handler".
When you were putting exit(0), you were invariable doing the same thing.
However, the proper way to do is - (snippet of the code)

if(siginfo->si_code == SEGV_MAPERR)
        {
                write(1,"address not mapped to object",strlen("address not mapped to object"));
                 sigaction(SIGSEGV, &oldHandler, NULL);
        }

That should fix it !

Hi Sree_ec,
That is how this particular signal works. Check out this link. After a "Segmentation fault", it is not possible for the program to continue and it will still continue to handle the signal. This step is causing the loop.
The text says that - "The handler should end by specifying the default action for the signal that happened and then reraising it; this will cause the program to terminate with that signal, as if it had not had a handler".
When you were putting exit(0), you were invariable doing the same thing.
However, the proper way to do is - (snippet of the code)

if(siginfo->si_code == SEGV_MAPERR)
        {
                write(1,"address not mapped to object",strlen("address not mapped to object"));
                 sigaction(SIGSEGV, &oldHandler, NULL);
        }

That should fix it !

That was a good piece of information you shared... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.