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

2010-03-18 Thread Bruce Evans

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


Bruce Evans b...@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___
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to svn-src-all-unsubscr...@freebsd.org

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

2010-03-17 Thread Dag-Erling Smørgrav
Bruce Evans b...@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);

?

DES
-- 
Dag-Erling Smørgrav - d...@des.no
___
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to svn-src-all-unsubscr...@freebsd.org


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

2010-03-16 Thread Bruce Evans

On Mon, 15 Mar 2010, Poul-Henning Kamp wrote:


In message 20100316024446.a24...@delplex.bde.org, Bruce Evans writes:

On Tue, 16 Mar 2010, Bruce Evans wrote:



Due to the way that daemon() works, it is really an error to have any
open streams when it is called.  This is also undocumented, except
implicitly.  The errors are:
- unflushed output on stdout and stderr won't get flushed normally by

 

  the child.  stdout and stderr are redirected so it will go there if
  the child erroneously (?) uses these streams or simply calls exit()
  which will give the default flush.


The in-core FILE buffers are copied in the child process, so they
should most certainly not be flushed, but rather, as they correctly
are, discarded, so that when the child causes the flush, the content
is only output once.


Nope.

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).

As described in the unquoted part of my reply above, all streams should
be flushed by the parent before the fork(), so that they are flushed
normally.

daemon(3) has a lot to say about problems with open fd's 0-2, but it
says nothing about the extra problems with open streams on these
descriptors.  It documents as a feature that fd's above 2 are inherited
normally.  It also says too little about the implementation always
using fork() (it cross-references fork() but doesn't say that the
context is always a child context on successful completion).

Bruce
___
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to svn-src-all-unsubscr...@freebsd.org


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

2010-03-15 Thread Poul-Henning Kamp
Author: phk
Date: Mon Mar 15 08:58:35 2010
New Revision: 205165
URL: http://svn.freebsd.org/changeset/base/205165

Log:
  Comment a fine point, so it does not get lost when people borrow code
  from FreeBSD for other purposes.

Modified:
  head/lib/libc/gen/daemon.c

Modified: head/lib/libc/gen/daemon.c
==
--- head/lib/libc/gen/daemon.c  Mon Mar 15 00:29:15 2010(r205164)
+++ head/lib/libc/gen/daemon.c  Mon Mar 15 08:58:35 2010(r205165)
@@ -64,6 +64,10 @@ daemon(nochdir, noclose)
case 0:
break;
default:
+   /*
+* A fine point:  _exit(0), not exit(0), to avoid triggering
+* atexit(3) processing
+*/
_exit(0);
}
 
___
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to svn-src-all-unsubscr...@freebsd.org


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

2010-03-15 Thread Bruce Evans

On Mon, 15 Mar 2010, Poul-Henning Kamp wrote:


Log:
 Comment a fine point, so it does not get lost when people borrow code
 from FreeBSD for other purposes.

Modified:
 head/lib/libc/gen/daemon.c

Modified: head/lib/libc/gen/daemon.c
==
--- head/lib/libc/gen/daemon.c  Mon Mar 15 00:29:15 2010(r205164)
+++ head/lib/libc/gen/daemon.c  Mon Mar 15 08:58:35 2010(r205165)
@@ -64,6 +64,10 @@ daemon(nochdir, noclose)
case 0:
break;
default:
+   /*
+* A fine point:  _exit(0), not exit(0), to avoid triggering
+* atexit(3) processing
+*/
_exit(0);
}


This point is obvious -- the programmer used _exit() for a reason, and
reading _exit(3) gives the reason.  _exit(3) only mentions atexit(3)
cryptically as etc., but it makes it clear that there are more reasons
than to avoid triggering atexit(3) -- exit(3) also does things like
flushing open streams which are logically and possibly implementationally
independent of atexit(3).  They _should_ be implementationally independent
to avoid bloating all programs with dynamic allocation of the atexit()
table.


The unobvious points are:
- why avoid triggering atexit(), etc. processing in daemon()?
- why isn't this documented?  Some callers of daemon() need to know that
  it won't flush open streams, etc., so that they can flush and their
  open streams, etc., before calling daemon().  Callers should do this
  anyway before calling any function that can exit (especially exit()
  itself), so that they can actually check that the output went out,
  but most are sloppy.  Callers should know if they have any open
  streams but they would have a hard time telling what else they are
  missing from not calling exit().  There may be profiling cleanup
  (profiling won't work right for the child?) and other magic destructors.

Bruce
___
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to svn-src-all-unsubscr...@freebsd.org