Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of sigaction()

Tags:

c

signals

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?

like image 517
Sicioldr Avatar asked Dec 20 '11 10:12

Sicioldr


People also ask

What does sigaction return in C?

sigaction(sig, act, oact) means “set the disposition for sig to act , and store the old disposition in oact ”. Its return value is 0 or -1, indicating whether the system call errored.

What is signal and sigaction?

The signal() function does not (necessarily) block other signals from arriving while the current handler is executing; sigaction() can block other signals until the current handler returns. The signal() function (usually) resets the signal action back to SIG_DFL (default) for almost all signals.

Which sigaction flag is used to prevent a system call from being interrupted?

You can set sa_mask in your sigaction call to block certain signals while a particular signal handler runs. This way, the signal handler can run without being interrupted itself by signals.

What is Sa_mask?

sa_mask. A signal mask of type sigset_t that specifies an additional set of signals to be blocked during processing by the signal handling function specified by the sa_handler field. sa_flags. Flags that affect the behavior of the signal.


1 Answers

See the sigaction(2) manual page. It's all described there.

Basically you set either sa_handler or sa_sigaction depending on whether you want the extra signal info.

If you set the later, you need to add SA_SIGINFO to the flags. Otherwise the flags should probably be 0 for your case. You probably want system calls to fail with errno EINTR when 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 (select and poll are never restarted), you can set the SA_RESTART flag.

The sa_mask is 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_restorer is obsolete and unused anyway.

like image 102
Jan Hudec Avatar answered Nov 10 '22 00:11

Jan Hudec