git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/5] daemon: if one of the standard fds is missing open it to /dev/null

From
MLMatthias Lederhofer <matled@gmx.net>
Date
Jul 13, 2006, 14:04 UTC
Message-ID
<E1G11nq-00076g-Aa@moooo.ath.cx>
In-Reply-To
<20060713152725.7a5081df.froese@gmx.de>
Edgar Toernig <froese@gmx.de> wrote:
Show 36 quoted lines
> Matthias Lederhofer wrote:
> >
> > +/* if any standard file descriptor is missing open it to /dev/null */
> > +static void sanitize_stdfds(void)
> > +{
> > +	int devnull = -1, i;
> > +	struct stat buf;
> > +	for (i = 0; i < 3; ++i) {
> > +		if (fstat(i, &buf) != -1)
> > +			continue;
> > +		if (devnull == -1 &&
> > +			(devnull = open("/dev/null", O_RDWR, 0)) == -1)
> > +			die("open /dev/null failed: %s", strerror(errno));
> > +		if (dup2(devnull, i) != i)
> > +			die("dup2 failed: %s", strerror(errno));
> > +	}
> > +	if (devnull != -1)
> > +		close(devnull);
> > +}
> 
> This looks broken.  The open will return i as this is
> the lowest free fd.  I don't know what POSIX says
> about dup2(i,i) but anyway, you close it at the end
> which completely defeats the intent of the function.
> 
> How's this?
> 
> 	devnull = open("/dev/null", O_RDWR, 0);
> 	if (devnull == 0)
> 		devnull = dup(devnull);
> 	if (devnull == 1)
> 		devnull = dup(devnull);
> 	if (devnull == -1)
> 		die("open/dup /dev/null failed: %s", strerror(errno));
> 	if (devnull > 2)
> 		close(devnull);

You're right (also for the daemonize function to use sanitize_stdfds). The code looks good to me, this could also be done using a while-loop (making it a little bit shorter, I don't know what is easier to read):

    devnull = open("/dev/null", O_RDWR, 0);
    while (devnull != -1 && devnull < 2)
        dup(devnull);
    if (devnull == -1)
        die("..");
    close(devnull);
(This is similar to what Andre Noll posted.)
I'll correct and resend those patches later.
Previous: Edgar ToernigNext: Uwe Zeisberger
Message 13 of 23 in “git-daemon problem”
  1. Matthias LederhoferJul 11, 2006
  2. Junio C HamanoJul 11, 2006
  3. Junio C HamanoJul 11, 2006
  4. Junio C HamanoJul 12, 2006
  5. Matthias LederhoferJul 12, 2006
  6. Junio C HamanoJul 13, 2006
  7. Matthias LederhoferJul 13, 2006
  8. Andre NollJul 13, 2006
  9. Matthias LederhoferJul 13, 2006
  10. 1/5 daemon: use a custom die routine with syslogMatthias Lederhofer, Jul 13, 2006
  11. 2/5 daemon: if one of the standard fds is missing open it to /dev/nullMatthias Lederhofer, Jul 13, 2006
  12. Edgar ToernigJul 13, 2006
  13. Matthias LederhoferJul 13, 2006
  14. Uwe ZeisbergerJul 13, 2006
  15. Morten WelinderJul 13, 2006
  16. Matthias LederhoferJul 13, 2006
  17. 1/5 daemon: if one of the standard fds is missing open it to /dev/nullMatthias Lederhofer, Jul 13, 2006
  18. 4/5 daemon: new option --pid-file=<path> to store the pidMatthias Lederhofer, Jul 13, 2006
  19. 5/5 daemon: new option --detach to run git-daemon in backgroundMatthias Lederhofer, Jul 13, 2006
  20. Edgar ToernigJul 13, 2006
  21. Matthias LederhoferJul 13, 2006
  22. 3/5 upload-pack: ignore write errors to stderrMatthias Lederhofer, Jul 13, 2006
  23. daemon: documentation for --reuseaddr, --detach and --pid-fileMatthias Lederhofer, Jul 14, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.