Search code examples
clinuxsignalsbsd

SIG_DFL throws warning at compiling, is ignored by OS when executing


I have a little problem which looks quite odd to me. I wrote this piece of code for handling Signals.

void sig_install(wlist *arg)
{
    struct sigaction sigstruct;
    //Doing stuff
    sigstruct.sa_handler = &handlerfunc;
    sigstruct.sa_flags = SA_RESTART;
    sigaction(sig, &sigstruct, NULL)
}

And this one works really fine. I had to define

#define _POSIX_C_SOURCE 200809L

to resolve the SA_RESTART-flag (My Professor told me to use it). My Problem is the following: I read everywhere that i can use SIG_DFL and sigaction to install the defaulthandler for my signal like:

void sig_uninstall(wlist *arg)
{
    //Do stuff
    if (sigaction(sig, SIG_DFL, NULL) == 0)
        printf("Signalhandler deleted!\n");
    else
        printf("Signalhandler could not be deleted!\n");
}

but when i compile it i get a warning for the SIG_DFL argument that says:

»const struct sigaction * __restrict__« expected, but Argument is type »void (*)(int)«

And when i execute the program the sigaction() returns 0 for success, but my signalhandler ist still not default when i send a signal.

The definitions in my headers are:

#define SIG_DFL ((__sighandler_t) 0)        /* Default action.  */
# define SA_RESTART   0x10000000 /* Restart syscall on signal return.  */
/* Get and/or set the action for signal SIG.  */
extern int sigaction (int __sig, __const struct sigaction *__restrict __act,
          struct sigaction *__restrict __oact) __THROW;

I read in the mandb that the SA_RESTART is just used on BSD. Is this maybe the problem? Is there an alternative flag for linux?(In university we have to use BSD but at home i use linux). Thank you very much for your time.


Solution

  • You need to set the sa_handler (pseudo) member of struct sigaction to SIG_DFL, not the struct sigaction * itself.

    Or, just use signal() instead.