3 ms·
You need a signal handler to eat it and do something about it, or you have to explicitly say that you don't care and set it to "ignored", but you can't just pre
by userbinator 6y ago
You need a signal handler to eat it and do something about it, or you have to explicitly say that you don't care and set it to "ignored", but you can't just pretend it won't happen... because it sure will.
The default action of SIGPIPE is to kill the process, which is exactly what you need for pipelines to work correctly. Related article: https://news.ycombinator.com/item?id=22647539 https://news.ycombinator.com/item?id=22647539
- sfoley 6y agoNot all programs are meant to be used in pipelines. It’s not even entirely accurate to say that that’s EXACTLY how pipelined programs should work; maybe they want to do some cleanup before they die. SIGPIPE was essentially created as a hack for naive programs that didn’t properly check the return value of write(). EPIPE should have been enough, it’s a perfectly fine solution.
- inetknght 6y ago> Not all programs are meant to be used in pipelines. I would argue that programs that aren't meant to be used in pipelines aren't well designed programs. > It’s not even entirely accurate to say that that’s EXACTLY how pipelined programs should work; maybe they want to do some cleanup before they die. If you need to do some cleanup before you die, then that's exactly what a signal handler is there for you to do. Nothing stops you from exiting immediately after a signal handler.
- jlokier 6y ago> If you need to do some cleanup before you die, then that's exactly what a signal handler is there for you to do. I challenge you to clean up a temporary directory, like doing "rm -fr $TMPDIR/tmp$SECRET/" except not by running another program, in an async-signal-safe manner inside a SIGPIPE handler which then exits. Hint: You're not allowed to call system(), readdir(), malloc() or any of exec*().
- zaarn 6y agoIf you'd check your errno when write returns -1 on standard in or standard out, SIGPIPE would not be necessary.
- userbinator 6y agoThat would mean duplicated logic across all applications and all places in them. SIGPIPE is like an exception. If you really need to, you can catch it (to do cleanup or similar, as a sibling comment notes), but otherwise the default behaviour is fine and likely what you want. I've made use of a similar technique when reading input: in code where an EOF is a rare case, but reads occur in multiple places, you can simplify the main logic considerably if you create a function that contains the read and a longjmp() upon EOF to a specific handler which then uses state variables to decide what else needs to be done; it gets rid of all the "if EOF" duplication and puts all the special-case EOF-handling logic in one place.
- zaarn 6y agoTo be honest, your solution doesn't sound more maintainable, because now I have to write a complicated longjmp handler and have global state variables that interact with all my read calls. That only maybe works if you write most of the application code yourself but it would be unrealistic in larger projects that have to do more than read from stdin and pop out data on the other end. SIGPIPE should not exist, no matter what hacky ways you found to abuse it for your advantage in some edge cases.