Can't catch SIGINT signal when using select ()

I have defined my own signal handler for SIGINT. But my signal handler is not called The signal handler just exits the program. But when pressing ctrl + c the program does not quit. Please, help...

This is what the code looks like.

sa.sa_handler = handle_termsig;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, 0);
sigaction(SIGTERM, &sa, 0);
sigemptyset(&g_termsigs);
sigaddset(&g_termsigs, SIGINT);
sigaddset(&g_termsigs, SIGTERM);

sigprocmask(SIG_BLOCK, &g_termsigs, 0);
if (select (fdmax+1, &rd, &wr, NULL, &time) < 0)
{
  exit();
}
sigprocmask(SIG_UNBLOCK, &g_termsigs, &savemask);

      

+2


source to share


2 answers


Since SIGINT is blocked during the select call, it is not delivered or select()

aborted. If your handler is just calling exit()

and you want it even when your code calls are picking, the best solution would be to leave the signal mask alone. If for some reason you don't want your normal handler to be called during a call select()

, you can use pselect()

.

Edit:

When the signal is given

If the signal is on (i.e. not blocked) and the process is started, immediately.

If the signal is on and the process is asleep while the sleep is interrupted (meaning == S state in high / ps output), syscall is aborted (for example, returning EINTR to the caller) and the signal handler is executed.

If the signal is blocked, it waits for delivery as soon as it is unblocked. Syscalls are not interrupted by blocked signals.



In your example, this means it select()

expects input; when the input becomes available it returns, the signal handler is activated (in the second call to sigprocmask) and the handler is called.

This is how it looks in strace:

Process 22393 attached - interrupt to quit
19:31:05.844940 select(1, [0], NULL, NULL, NULL) = 1 (in [0])
19:31:12.131431 rt_sigprocmask(SIG_UNBLOCK, [INT TERM], [INT TERM], 8) = 0
19:31:12.131560 --- SIGINT (Interrupt) @ 0 (0) ---
19:31:12.131730 fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
19:31:12.131871 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7ffb000
19:31:12.132027 write(1, "signal..."..., 9) = 9
19:31:12.132148 exit_group(1)           = ?
Process 22393 detached

      

How to enable signals

To keep the signal handler on all the time, just remove the calls sigprocmask()

from your code.

0


source


You are explicitly blocking SIGINT

around the call select

, so of course it doesn't work.



What are you trying to do?

0


source







All Articles