Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle signal with sigaction

Tags:

c

unix

signals

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.

like image 233
giozh Avatar asked May 12 '12 16:05

giozh


2 Answers

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.

like image 144
alk Avatar answered Oct 12 '22 14:10

alk


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.

like image 27
Jay Avatar answered Oct 12 '22 16:10

Jay