Sigaction returning -1 on declaration in a child process

Transferring the gist of my comments into an answer.

Study the POSIX specification of sigaction() carefully.

One part of your problem may be that you’ve not set the signal mask, sa.sa_mask, so it is random garbage, and presumably, the random garbage is not acceptable. Writing sigemptyset(&sa.sa_mask); before the call might fix the problem — it would fix the problem if my analysis is correct. Basically, you should make sure you’ve initialized the structure properly/thoroughly. Even if this is not the cause of the trouble, you should still always set the mask. It isn’t a good idea to block a random set of signals; you should know what you’re blocking, so either sigemptyset() or sigfillset() should be used to set the mask — or you can ensure the struct sigaction structure is zeroed when you create it.

(Similarly, you should ensure the that the sa_flags member is appropriately set too — part of the previous point about setting the structure properly. This was noted by Nate Eldridge in this answer. You should only set one of sa_handler and sa_sigaction, depending on whether you set SA_SIGINFO in the sa_flags.)

However, you have another more major problem. Take a good look at the if statement:

if (sigaction(SIGUSR1, &sa, NULL) == -1);

What’s the semicolon doing? Don’t put semicolons there; it means that the code block after it is executed unconditionally.

Note that errors should be written to stderr, and you could consider using perror(), or using errno and strerror() to report the error message (and possibly the error number). But the semicolon is the main problem. You should still make sure the structure passed to sigaction() is fully initialized.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top