Skip to content
Advertisement

Sending payload with sigaction

how can I use sigaction such that a process can send a payload with the signal that can be retrieved by the receiving process?

Advertisement

Answer

You use a realtime signal (SIGRTMIN+0 to SIGRTMAX-0), so that the signals are queued, and you have a much lesser chance of missing one. (Standard signals are not queued, so if two signals are sent at nearly the same time, only one of them is actually delivered.)

When installing the handler using sigaction(), you use the SA_SIGINFO flag, so that the signature of the handler is

void signal_handler(int signum, siginfo_t *info, void *context)

When a signal is sent using sigqueue(), the third parameter is a pointer or an int, stored in an union of type union sigval. (For union sigval value, the int is value.sival_int, and the pointer is value.sival_ptr. In Linux, you can send a full long or unsigned long using value.sival_ptr = (void *)(unsigned long)yourval;.)

(In Linux, sizeof (long) == sizeof (void *), so you can actually send CHAR_BIT*sizeof (long) bits of information; this is 32 in 32-bit architectures, and 64 in 64-bit architectures.)

The handler can determine if a signal was sent using sigqueue() by checking if info->si_code == SI_QUEUE. The payload is then available in info->si_value union. Note that you cannot know whether the sender sent a pointer or an int, you need to know that beforehand. The sigaction() man page describes all the fields, including si_code.

Don’t be confused about ->si_int, ->si_ptr, and ->si_value. The siginfo_t structure is defined so that the members si_int and si_value.sival_int refer to the exact same int, and si_ptr and si_value.sival_ptr refer to the exact same ptr. There are some extra “names” for these fields for backwards compatibility, that’s all.

Remember, at times of high load, even a realtime signal can be lost. However, we routinely use signals like HUP or TERM to tell service daemons to reload their configuration or exit; so, for similar management purposes, and for informational (as opposed to crucial) messages, using signals and their payload is okay.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement