On Sat, Sep 08, 2018 at 08:47:28PM +0800, Nan Xiao wrote: > I don't think need "if (fd > STDERR_FILENO)" cause the fd is a new > opened file descriptor. If its value is less or equal than > `STDERR_FILENO', it means the stdin or stderr is already closed > intentionally. So it should be OK close fd again.
In general it is a bad idea to start programs without opened 0 1 2 file descriptors. Then the next open will fill the gap and a printf() or err() may write to random files. Some paranoid daemons like syslogd(8) dup2 /dev/null to them. And there is this check: if (nullfd > 2) close(nullfd); If course nohup(1) is not that paranoid. And the isatty() checks prevent that the stdio file decriptor gaps get filled. But before you plugged the fd leak, "nohup true 2>&-" would write to nohup.out. Perhaps we could make nohup(1) a bit more sane by checks like this. errno = 0; if (isatty(STDOUT_FILENO) || errno == EBADF) bluhm