From owner-svn-src-all@FreeBSD.ORG Mon Mar 15 16:07:09 2010 Return-Path: Delivered-To: svn-src-all@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 16114106574B; Mon, 15 Mar 2010 16:07:09 +0000 (UTC) (envelope-from brde@optusnet.com.au) Received: from mail02.syd.optusnet.com.au (mail02.syd.optusnet.com.au [211.29.132.183]) by mx1.freebsd.org (Postfix) with ESMTP id A4B0D8FC0C; Mon, 15 Mar 2010 16:07:08 +0000 (UTC) Received: from c122-107-126-6.carlnfd1.nsw.optusnet.com.au (c122-107-126-6.carlnfd1.nsw.optusnet.com.au [122.107.126.6]) by mail02.syd.optusnet.com.au (8.13.1/8.13.1) with ESMTP id o2FG75BF025230 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 16 Mar 2010 03:07:06 +1100 Date: Tue, 16 Mar 2010 03:07:05 +1100 (EST) From: Bruce Evans X-X-Sender: bde@delplex.bde.org To: Bruce Evans In-Reply-To: <20100316021240.N24765@delplex.bde.org> Message-ID: <20100316024446.A24853@delplex.bde.org> References: <201003150858.o2F8wZid011308@svn.freebsd.org> <20100316021240.N24765@delplex.bde.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: svn-src-head@FreeBSD.org, svn-src-all@FreeBSD.org, src-committers@FreeBSD.org, Poul-Henning Kamp Subject: Re: svn commit: r205165 - head/lib/libc/gen X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Mar 2010 16:07:09 -0000 On Tue, 16 Mar 2010, Bruce Evans wrote: > ... > 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. 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. - unflushed input in stdin may cause problems, since although stdin is redirected, the input may be in stdio's buffers. - it is unclear if stdio can do something better than crash when its fd's for stdinout/err are redirected without telling it. - fd's above 2 are not touched, so the child inherits any open streams on these fd's. These streams are likely to get flushed on exit() if not explicitly. I think daeomon() should be doing an fflush(NULL) or fcloseall() and most other cleanups done by atexit processing. Before the fork() of course, so that the child doesn't inherit stuff. fcloseall() would be too destructive if the fork() fails, but fflush(NULL) is almost as clean. At least the fflush(NULL) is safe since it has no effect except for buggy callers that have unflushed streams. Bruce