Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what if tail fails while reading from pipe

distinguish stdout from stderr on pipe

So, related to the link above, I have a child who is executing tail and parent is reading its out put via a pipe.

dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[1], STDERR_FILENO);

My question is, if somehow tail fails, what happens to the pipe from which I am reading? Do I get anything on stderr? Does tail terminate itself? or it may hang in there as defunct?

like image 593
hari Avatar asked Nov 22 '25 14:11

hari


2 Answers

The kernel will send a SIGPIPE signal to the other process on the pipe when tail has terminated. The default action for this signal (if a handler is not installed) is to terminate the process.

If you don't want to deal with signals, you can ignore SIGPIPE in the parent (so it doesn't terminate when tail has terminated), and instead check whether the value of errno is EPIPE after each read. Additionally, you'll have to call wait or waitpid from the parent to reap the zombie child.

like image 55
Blagovest Buyukliev Avatar answered Nov 25 '25 04:11

Blagovest Buyukliev


you don't get EPIPE when reading, only write will return EPIPE. You'll get EOF, indicated by read returning 0, and since you read stderr, you'll get the error message as well (before the EOF).

The process will become a zombie, and you can use wait/waitpid to get the exit status, which will be non-zero if there was an error.

like image 43
Per Johansson Avatar answered Nov 25 '25 02:11

Per Johansson