0

I'm trying to implement a signal handler and was wondering if I need to explicitly empty sa_mask field of the struct sigaction or if by initializing it with default value is sufficient.

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
  • are you refereeing to the `sa_mask` field from the posix [`sigaction`](https://www.man7.org/linux/man-pages/man2/sigaction.2.html)? You need to add a little bit of context to your question. – bolov Dec 13 '20 at 14:35
  • Yes, I'm trying to initialise struct sigaction and and setting sa_flags and sa_sigaction but I don't know if I need to empty the sa_mask – Michel Ruge Dec 13 '20 at 14:40
  • You need to ensure it is set to a known state — all bits zero is OK. If the structure is allocated on the stack, you must explicitly zero it. Ditto if it is allocated by `malloc()`, but not if allocated by `calloc()`. If the structure is allocated at file scope (static or global) it will be initialized to all bytes zero be default. All these assume you don’t provide an initializer. That will set bits you don’t explicitly initialize to zero too. – Jonathan Leffler Dec 13 '20 at 15:09

1 Answers1

2

Practically, you can initialize the structure to 0 with something like memset() and then initialize the signal handler field:

struct sigaction action;

memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = handler_func;

Theoretically, a typed object like "sigset_t" comes with its proper initialization method. And so, you can't suppose that setting it to 0 through memset() will work. Hence, for this field, you are supposed to use the signal set related routines to manipulate it:

struct sigaction action;

memset(&action, 0, sizeof(struct sigaction));
sigemptyset(&(action.sa_mask));
action.sa_handler = handler_func;
Rachid K.
  • 4,490
  • 3
  • 11
  • 30