I have seen in SO how to redirect STDIN, STDOUT, STDERR to /dev/null in C . This is done during startup of a daemon. But, why is this necessary for a proper startup of a unix/linux daemon?
Bonus Question :
What happens if STDOUT
is closed and file descriptor is used without re-opening ?
By combining redirection with the /dev/null device, we can silence error output, normal output, or both.
Stderr, also known as standard error, is the default file descriptor where a process can write error messages. In Unix-like operating systems, such as Linux, macOS X, and BSD, stderr is defined by the POSIX standard. Its default file descriptor number is 2. In the terminal, standard error defaults to the user's screen.
stdin − It stands for standard input, and is used for taking text as an input. stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream. stderr − It stands for standard error.
In Unix, how do I redirect error messages to /dev/null? You can send output to /dev/null, by using command >/dev/null syntax. However, this will not work when command will use the standard error (FD # 2). So you need to modify >/dev/null as follows to redirect both output and errors to /dev/null.
stdin
, stdout
and stderr
are closed so that the daemon can detach successfully from the tty it was started from and also so that the daemon (or its child processes) won't write to the tty when its running.
If you attempt to read/write from a closed file descriptor, the operation will fail and errno
will be set to EBADF
("fildes is not a valid file or socket descriptor open for reading"). Other than that, nothing untoward will happen.
You shouldn't. It is not necessary for a proper startup of a unix/linux daemon. Always write your log messages to stderr
.
errno
is set to EBADF
if a closed file descriptor is used, and the operation will fail.
Don't daemonize your program inside itself. Use a daemon manager to daemonize it. (Remember? Do one thing and do it right.)
Whatever program you write, always log to stderr
. Doing so has the following advantages, and I quote Jonathan de Boyne Pollard:
- The logging output from your dæmon will as a consequence automatically be entirely separate from the logging output of other dæmons.
- You won't need any extra files in the chroot() jail at all.
- Log data will not be lost and will be recorded in the same order that they were generated.
- Other programs/people will not be able to insert spoof messages into the output.
Redirecting stderr
to /dev/null
will make it a lot harder for sysadmin to aggregate your log into their systems. You may enrage them by doing the following, I quote Chris Jester Yang:
- Insist on using syslog, and don't provide any options to log to standard error or standard output.
- Just to be sure, explicitly close file descriptors 1 and 2, or redirect them to /dev/null.
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