Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should one redirect STDIN, STDOUT, STDERR to /dev/null, during startup of daemon?

Tags:

c

unix

daemon

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 ?

like image 731
smRaj Avatar asked Oct 21 '13 10:10

smRaj


People also ask

Why do we redirect to dev Null?

By combining redirection with the /dev/null device, we can silence error output, normal output, or both.

What is the purpose of dev stderr?

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.

What is stdin stdout and stderr?

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.

How do I redirect stdout to dev Null?

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.


2 Answers

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.

like image 109
trojanfoe Avatar answered Sep 20 '22 10:09

trojanfoe


TL;DR

You shouldn't. It is not necessary for a proper startup of a unix/linux daemon. Always write your log messages to stderr.

Bonus

errno is set to EBADF if a closed file descriptor is used, and the operation will fail.

Summary

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.

Reference

  • Mistakes to avoid when designing Unix dæmon programs
  • http://cloud9.hedgee.com./scribbles/daemon#logging
like image 35
Yufan Lou Avatar answered Sep 19 '22 10:09

Yufan Lou