Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Sep 1998 11:25:32 +0100
From:      Brian Somers <brian@Awfulhak.org>
To:        Mark Ovens <marko@uk.radan.com>
Cc:        Brian Somers <brian@Awfulhak.org>, questions@FreeBSD.ORG, freebsd-users@freebsd-uk.eu.org
Subject:   Re: Help needed with fork(), pipe() & dup2() 
Message-ID:  <199809121025.LAA28513@woof.lan.awfulhak.org>
In-Reply-To: Your message of "Fri, 11 Sep 1998 08:16:27 BST." <35F8CE4B.ED9E33F3@uk.radan.com> 

next in thread | previous in thread | raw e-mail | index | archive | help
> Brian Somers wrote:
> > 
> > 
> > The parent should not be mucking around with STDIN and gets() at all.
> > In my experience, way to many library calls use unexpected routines
> > like err() and abort().  Besides, gets() produces warnings for a
> > reason.
> > 
> > Instead, change the parent if() block to
[.....]
> The major change here seems to be using fdopen() instead of dup2() and
> reading the pipe with fgets() rather than gets(), but only for the
> parent. Can I take it that dup2() can still be used for the child?

The major change is that the parent doesn't muck around with 
STDIN_FILENO - other things may break when this is dup'd or reopened.

The child is correct to dup2 to STDOUT_FILENO as the intention is to 
redirect the exec'd program output.  Nothing can get screwed up as 
the exec will throw away the current process.  Of course an exit() 
should be done after the exec() in case the exec fails.

> Leo has tried your mods and it works fine. I'll try it tonight. He
> also tried the original code and got the same results - works under
> Linux but not FreeBSD.

Maybe the linux stuff depends less on having STDIN_FILENO intact.  As 
I suggested before, far to many of the library routines in FreeBSD 
use stdin/stdout/stderr.  A library routine should never make 
assumptions about these descriptors unless it's documented as doing 
so, for example, the ppp code closes these descriptors near the start 
of main()..... and can end up reusing them for other purposes.

> -- 
>   When everything's coming your way, you're in the wrong lane.
> 
> Mark Ovens, CNC Applications Engineer, Radan Computational Ltd
> Sheet Metal CAD/CAM Solutions
> mailto:marko@uk.radan.com    http://www.radan.com

-- 
Brian <brian@Awfulhak.org>, <brian@FreeBSD.org>, <brian@OpenBSD.org>
      <http://www.Awfulhak.org>;
Don't _EVER_ lose your sense of humour....



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



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