HackX123
HackX123

Reputation: 127

Ignoring/blocking SIGPIPE signals in multi-threaded linux program

I have the following situation: Thread 1: Forks a child and the child, say A in turn forks again and executes a process. B

Thread 2: Listens for commands over a Unix Domain Socket and kills the process, B that has been forked by child, A in Thread 1 Respond to caller that it has killed the child

I want to ignore SIGPIPE for Thread 2 as I do not want the program to crash when client has closed the socket. So i tried doing this using

sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGPIPE);
pthread_sigmask(SIG_BLOCK, &set, NULL);

Doing this helps block SIGPIPE but it also blocks the ability of the thread 1 to send SIGKILL to the child.

I also tried using the below in main function before creating threads

signal(SIG_IGN, SIGPIPE);

and send with MSG_NOSIGNAL flag in socket.

This doesnt help my scenario with SIGKILL as well. Any idea how to ignore safely the SIGPIPE in a multi-threaded condition like above with forks and execs and SIGKILLs sent?

Upvotes: 2

Views: 1279

Answers (1)

HackX123
HackX123

Reputation: 127

After some investigation, I found out the solution to it. I had to do
pthread_sigmask(SIG_UNBLOCK, &set, NULL);
after the each fork() calls and before exec() in the grand child. This caused SIGKILL to not get blocked.

Upvotes: 2

Related Questions