I have to send two signals to a process, SIGUSR1 and SIGUSR2, in order to modify a particular boolean variable in the program (SIGUSR1 sets it to true, SIGUSR2 sets it to false). So I wrote a signalHandler() function in order to control the behavior of SIGUSR1 or SIGUSR2. The problem is: how to set sigaction() to handle this particular task? I spent a lot of time on Google, I read everywhere that I should use sigaction() instead of the obsolete signal(). In the man page i found this
int sigaction(int signum, const struct sigaction *act,struct sigaction *oldact);
in signum I have to put the type of signal I want to handle, then I have a struct sigaction parameter:
struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
};
in the first field i thought i should set the name of my signal handler, but I don’t know how can I set the other fields.
Finally, what is the use of: struct sigaction *oldact?
See the sigaction(2) manual page. It’s all described there.
Basically you set either
sa_handlerorsa_sigactiondepending on whether you want the extra signal info.If you set the later, you need to add
SA_SIGINFOto the flags. Otherwise the flags should probably be 0 for your case. You probably want system calls to fail with errnoEINTRwhen interrupted with the signal (default behaviour), so you can consider the new value of the variable before restarting them, but if you ended up wanting to restart them automatically (selectandpollare never restarted), you can set theSA_RESTARTflag.The
sa_maskis set of signals that should be defered while this singal handler is running. You should set at least the two signals, so they don’t get mixed up if they come in quick succession.And the last,
sa_restoreris obsolete and unused anyway.