From: ZHOU Xiao-bo on
hi:

I am confused by the behavior of stdout/stderr in daemon process.

If a daemon doesn't close stdout/stderr and redirect them to
/dev/null. (that is, it inherits the terminal(/dev/ptsX) from it's
parent process) Then the I logout and the terminal is closed. What
will happen if the daemon process keep writing characters to its
stdout/stderr? And what file do stdout/stderr associated to?

thanks a lot

From: Ben Finney on
ZHOU Xiao-bo <zhxb.ustc(a)gmail.com> writes:

> If a daemon doesn't close stdout/stderr and redirect them to
> /dev/null. (that is, it inherits the terminal(/dev/ptsX) from it's
> parent process) Then the I logout and the terminal is closed.

This shouldn't be dependent on whether the process is a daemon. Your
question applies equally to any process whose controlling terminal is
closed.

> What will happen if the daemon process keep writing characters to its
> stdout/stderr? And what file do stdout/stderr associated to?

What happens when you try? Start by running any process, closing its
controlling terminal, then having that process write to stdout or
stderr.

--
\ “He who wonders discovers that this in itself is wonder.” |
`\ —Maurits Cornelis Escher |
_o__) |
Ben Finney
From: ZHOU Xiao-bo on
On 2010-1-29 12:37, Ben Finney wrote:
> ZHOU Xiao-bo<zhxb.ustc(a)gmail.com> writes:
>
>> If a daemon doesn't close stdout/stderr and redirect them to
>> /dev/null. (that is, it inherits the terminal(/dev/ptsX) from it's
>> parent process) Then the I logout and the terminal is closed.
>
> This shouldn't be dependent on whether the process is a daemon. Your
> question applies equally to any process whose controlling terminal is
> closed.
>
>> What will happen if the daemon process keep writing characters to its
>> stdout/stderr? And what file do stdout/stderr associated to?
>
> What happens when you try? Start by running any process, closing its
> controlling terminal, then having that process write to stdout or
> stderr.
>

Actually, I encountered a crash in C++'s ostream::sentry::sentry() which
is cout. It happens occasionally. I doubt there is something to do with
a closed terminal.
From: Klaus ter Fehn on
ZHOU Xiao-bo <zhxb.ustc(a)gmail.com> wrote:
> If a daemon doesn't close stdout/stderr and redirect them to
> /dev/null. (that is, it inherits the terminal(/dev/ptsX) from it's
> parent process) Then the I logout and the terminal is closed. What
> will happen if the daemon process keep writing characters to its
> stdout/stderr? And what file do stdout/stderr associated to?

In this case, the daemon process wasn't started properly. Beside of
closing stdin/stdout/stderr (or opening files instead), the process
should fork(2). Then the parent process terminates and the child process
should call setsid(2) to become a new session leader (normally the login
process is the session leader). This setsid(2) call will also have the
effect, that the process has no controlling tty anymore.:

Simple example:

pid_t pid; /* or int */

close(0); close(1); close(2);
/***
use fclose(stdin); fclose(stdout); fclose(stderr); if you're
using <stdio.h> functions

when using C++ <iostream> or similar libraries, the
corresponding streams should be closed
***/

if ((pid = fork()) < 0) {
/* some error occured ... */
exit(1);
} else if (pid > 0) {
/* parent process may terminate */
exit(0);
}

/* child process resumes */
setsid();

/* now do your daemon thing... */

BTW: Closing a terminal window will not cause associated processes to
automagically change their stdin/stdout/stderr devices. They resume
writing to wherever they were writing before and will most likely get an
error from the OS when the device permissions change.

SEE ALSO
setsid(2), fork(2)

--
Klaus ter Fehn Wagnerstr. 4 Mobile: +49-172-2529379
40212 Duesseldorf Phone: +49-211-356880
ktf(a)cflow.de FRG/Germany Fax: +49-211-356881

... to boldly code where no byte has gone before ...
From: Ersek, Laszlo on
In article <hjtmr0$9vm$1(a)www.shinco.com>, ZHOU Xiao-bo <zhxb.ustc(a)gmail.com> writes:

> If a daemon doesn't close stdout/stderr and redirect them to
> /dev/null. (that is, it inherits the terminal(/dev/ptsX) from it's
> parent process) Then the I logout and the terminal is closed. What
> will happen if the daemon process keep writing characters to its
> stdout/stderr? And what file do stdout/stderr associated to?

http://www.opengroup.org/onlinepubs/007908775/xbd/termios.html#tag_008_001_010
http://www.gnu.org/software/libc/manual/html_node/Orphaned-Process-Groups.html
http://www-theorie.physik.unizh.ch/~dpotter/howto/daemonize

Cheers,
lacos