svn commit: r205165 - head/lib/libc/gen

Bruce Evans brde at optusnet.com.au
Thu Mar 18 13:19:45 UTC 2010


On Wed, 17 Mar 2010, [utf-8] Dag-Erling Smørgrav wrote:

> Bruce Evans <brde at optusnet.com.au> writes:
>> Even if the child causes the flush, the content of stdout and stderr
>> is not flushed normally since it is redirected to /dev/null.  Unflushed
>> input in stdin is handled more brokenly: although stdin is redirected
>> to /dev/null, input on it can still be read via stdin's buffer.
>> Inheriting unflushed input on other streams is a feature (unless these
>> streams are open on fd's 0-2; then these streams will have the same
>> corruption as std* streams open on their normal fd's 0-2).
>
> how about
>
> Index: gen/daemon.c
> ===================================================================
> --- gen/daemon.c	(revision 204870)
> +++ gen/daemon.c	(working copy)
> @@ -37,6 +37,7 @@
> #include <errno.h>
> #include <fcntl.h>
> #include <paths.h>
> +#include <stdio.h>
> #include <stdlib.h>
> #include <signal.h>
> #include <unistd.h>
> @@ -81,6 +82,9 @@
> 		(void)chdir("/");
>
> 	if (!noclose && (fd = _open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
> +		fpurge(stdin);
> +		fflush(stdout);
> +		fflush(stderr);
> 		(void)_dup2(fd, STDIN_FILENO);
> 		(void)_dup2(fd, STDOUT_FILENO);
> 		(void)_dup2(fd, STDERR_FILENO);
>
> ?

That seems like a reasonable start.  I wanted to flush/purge all streams
but that is more dangerous of course.

I misread the man page about fflush(NULL) purging anything.  It is
only fpurge() that purges, but I didn't notice that fflush()'s man
page is also about fpurge().  fpurge() hasn't caught up with the 1980's
developments that made fflush(NULL) operate on all streams, so there
is no way to purge all streams without using stdio internals (_fwalk()).

The above is missing purging of stdout and stderr.  Someone may have
freopen()ed these in "rw" mode.  Callers of daemon() shouldn't do this,
but if fpurge(NULL) worked right then it would be easy to purge stdout
and stderr when purging stdin and all other streams (if this is what
we want to do).

Bruce


More information about the svn-src-head mailing list