From: Paolo Bonzini Date: Mon, 25 Aug 2008 11:49:38 GMT Subject: Re: [PATCH] Fix start_command() pipe bug when stdin is closed. Message-ID: <48B29C52.8040901@gnu.org> In-Reply-To: <48B28CF8.2060306@viscovery.net> > While I do see that there is a problem, it is only half of the story, and > your patch addresses only this half. > > What if stdout is closed, too? Then the ends of the first allocated pipe > would go to fds 0 and 1, and then the pipe end at 1 would be closed by a > subsequent dup2(xxx, 1). What about opening files (in start_command, protected by a loop that run only once, or on startup) until you get a descriptor that is > 2? Like this: static int low_fds_reserved; if (!low_fds_reserved) { int fd = open("/dev/null", O_RDWR); while (fd >= 0 && fd <= 2) fd = dup (fd); if (fd != -1) close (fd); else perror ("start_command"); low_fds_reserved = 1; } Paolo