Why is it necessary to close standard input/output/error when writing a (unix) daemon?
Not only stdin, stdout and stderr should be closed, but all open files.
From "Advanced Programming in the UNIX Environment", W. Richard Stevens, Addison-Weseley, 18th Printing, 1999, page 417.
Unneeded file descriptors should be closed. This prevents the daemon from holding open any descriptors that is may have inherited from its parent (which could be a shell or some other process).
Mr. Stevens proposal is to get the maximum file descriptor and close all files up to that value.
The cited chapter is about 'Daemon Processes'. Please note that closing file descriptors is only one point from five when writing daemons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With