Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 19 Mar 2010 00:19:36 +1100 (EST)
From:      Bruce Evans <brde@optusnet.com.au>
To:        =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= <des@des.no>
Cc:        svn-src-head@freebsd.org, Poul-Henning Kamp <phk@phk.freebsd.dk>, src-committers@freebsd.org, svn-src-all@freebsd.org, Bruce Evans <brde@optusnet.com.au>
Subject:   Re: svn commit: r205165 - head/lib/libc/gen
Message-ID:  <20100319000301.D27813@delplex.bde.org>
In-Reply-To: <86ljdq6h89.fsf@ds4.des.no>
References:  <44640.1268676905@critter.freebsd.dk> <20100317041500.V26092@delplex.bde.org> <86ljdq6h89.fsf@ds4.des.no>

next in thread | previous in thread | raw e-mail | index | archive | help
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--0-323523359-1268918376=:27813
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

On Wed, 17 Mar 2010, [utf-8] Dag-Erling Sm=C3=B8rgrav wrote:

> Bruce Evans <brde@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
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> --- gen/daemon.c=09(revision 204870)
> +++ gen/daemon.c=09(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 @@
> =09=09(void)chdir("/");
>
> =09if (!noclose && (fd =3D _open(_PATH_DEVNULL, O_RDWR, 0)) !=3D -1) {
> +=09=09fpurge(stdin);
> +=09=09fflush(stdout);
> +=09=09fflush(stderr);
> =09=09(void)_dup2(fd, STDIN_FILENO);
> =09=09(void)_dup2(fd, STDOUT_FILENO);
> =09=09(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
--0-323523359-1268918376=:27813--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20100319000301.D27813>