Disable SIGPIPE when recording (2) a call in the library

Question

Is it possible to disable signal boost ( SIGPIPE) when writing to pipe()FD, without installing my own signal handler or disabling / masking the signal globally?


Background

I am working on a small library that sometimes creates a pipe, and fork()a temporary child / dummy process, waiting for messages from the parent. When a child process receives a message from the parent, it dies (intentionally).


Problem

The child process, in circumstances beyond my control, runs the code from another (third party) library that is prone to failure, so I can not always be sure that the child process is alive before I go write()to the channel,

This leads to the fact that sometimes I try to execute write()for a channel with a child process, the end of which is already dead / closed, and it raises SIGPIPEin the parent process. I am in a library that other clients will use, so my library should be as autonomous and transparent as possible for the calling application. Installing a custom signal handler may break the client code.


Still working

I have a problem with sockets using setsockopt(..., MSG_NOSIGNAL), but I cannot find anything functionally equivalent for pipes. I looked at the temporary installation of the signal handler to catch SIGPIPE, but I see no way to limit its scope to the calling function in my library, and not the whole process (and this is not atomic).

SO, ,, , , poll()/select() , ( ) , select() write().


()

, , , , SIGPIPE? , , ? , , , "crashy" , , .

+4
3

, , :

  • socketpair(PF_LOCAL, SOCK_STREAM, 0, fd) .
  • "" fork(), , SIGPIPE .

socketpair. , , .

!

+1

(SIGPIPE) pipe() FD [...]?

. , , SIGPIPE .

- . , , , . , , . , , SIGPIPE.

, , , waitpid() WNOHANG. , waitpid() write.

, , , , , , .

+1

, : , .. . , . - , , . , , , . , , " ".

The first hack that comes to mind will be that you do not close the read side of the channel in the parent. This allows you to freely write into the pipe, without impairing the child's ability to read from it.

If this is not good, please clarify the problem.

0
source

Source: https://habr.com/ru/post/1663995/


All Articles