A simple way to check would be to do a 0 byte write(2) to the pipe and check the return. If you're catching SIGPIPE or checking for EPIPE, you get the error. But that's just the same as if you go ahead and do your real write, checking for the error return. So, just do the write and handle an error either in a signal handler (SIGPIPE) or, if the signal is ignored, by checking the error return from write.
The send() function shall fail if:
[EPIPE] The socket is shut down for writing, or the socket is connection-mode and is no longer connected. In the latter case, and if the socket is of type SOCK_STREAM or SOCK_SEQPACKET and the MSG_NOSIGNAL flag is not set, the SIGPIPE signal is generated to
the calling thread.
You can avoid this by using the MSG_NOSIGNAL
flag on the send()
call, or by ignoring the
SIGPIPE
signal with signal(SIGPIPE, SIG_IGN)
at the beginning of your program. Then the
send()
function will return -1 and set errno
to EPIPE
in this situation.
|