giozh
giozh

Reputation: 10068

Handle signal with sigaction

I need to handle some signal with sigaction, but when I compile my code I get this error:

warning: assignment from incompatible pointer type

the code is:

struct sigaction hup_act;
...
hup_act.sa_flags=SA_SIGINFO;
hup_act.sa_sigaction = sighupHandler;
sigaction(SIGHUP, &hup_act, NULL);

Warning is on hup_act.sa_sigaction assignment. This warning causes a segmentation fault when I send sighup to my application.

Upvotes: 6

Views: 4125

Answers (2)

alk
alk

Reputation: 70883

If SA_SIGINFOis set in struct sigaction's member sa_flags use a method of type

void signalhandler(int, siginfo_t *, void *);

as handler function to be assigned to struct sigaction's member sa_sigaction

else use a method of type

void signalhandler(int);

as handler function to be assigned to struct sigaction's member sa_handler.

Upvotes: 6

Jay
Jay

Reputation: 24895

Your sighupHandler must be one of the two below:

void sighupHandler(int) //for sa_handler

or

void sighupHandler(int, signinfo_t *, void *) //for sa_sigaction which is your case

For more info on this refer here and here

EDIT: Please refer to below and my above suggested links for better clarity. I think in your case because of common storage as suggested below, compiler is only generating a warning, else it would have generated an error.

The sigaction structure is defined as something like:

struct sigaction {
    void     (*sa_handler)(int);
    void     (*sa_sigaction)(int, siginfo_t *, void *);
    sigset_t   sa_mask;
    int        sa_flags;
    void     (*sa_restorer)(void);
};

On some architectures a union is involved: do not assign to both sa_handler and sa_sigaction.

Upvotes: 5

Related Questions