delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2014/01/17/15:39:08

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:message-id:date:from:mime-version:to:subject
:references:in-reply-to:content-type; q=dns; s=default; b=NlsLXU
DRFA9k3ZnDtwReyHLHVlv2mAXM1rZbxFXkl2ZLgmdwNZBH8o75hpv5jOrs7kqLSG
Em1HVyvgRLc9cpfCvDhP19si+ApRW/Vz8bVI7hT3mwmEzZrRAfbN+XDy5iAnBJOS
Lmq5TXnMI8MxKehdg79JZQzgL23wBjxsp2YZw=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:message-id:date:from:mime-version:to:subject
:references:in-reply-to:content-type; s=default; bh=dG3MWypXBiD+
ZCBbp8R2HnbS11w=; b=MoocPopEYwiXfUl2HKnYC+G5+XCjKvleMY5VkNKyfkCv
18bJ0ONqp3DgmjSg5BaLalBYBTBWE1luZ3vOfnhbZnLk2LFMmePUi/j4jkvAitE9
TU7GOM9olvzVcdiSrFDsfRC5xor+ZAs3LteTfLs+ntxo+xbW+vT/YgcQj+gS+iI=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2
X-HELO: mx1.redhat.com
Message-ID: <52D98E1D.8010907@redhat.com>
Date: Fri, 17 Jan 2014 13:10:05 -0700
From: Eric Blake <eblake AT redhat DOT com>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: fork() + file descriptor bug in 1.7.27(0.271/5/3) 2013-12-09 11:54
References: <831845 DOT 98759 DOT bm AT smtp116 DOT sbc DOT mail DOT ne1 DOT yahoo DOT com> <52D55D96 DOT 8070407 AT redhat DOT com> <946338 DOT 89157 DOT bm AT smtp116 DOT sbc DOT mail DOT ne1 DOT yahoo DOT com>
In-Reply-To: <946338.89157.bm@smtp116.sbc.mail.ne1.yahoo.com>
OpenPGP: url=http://people.redhat.com/eblake/eblake.gpg
X-IsSubscribed: yes

--SKoUmSLRLvoGcmRfkfPqjmshMl6iSDoKS
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On 01/14/2014 08:50 AM, tednolan AT bellsouth DOT net wrote:
> In message <52D55D96 DOT 8070407 AT redhat DOT com>you write:
>>
>> Your program may be violating POSIX, which would trigger undefined behav=
ior.
>>
>> Quoting POSIX:
>> pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15=
_05
>>
>=20
> [long quote elided]
>=20
> Yikes!  That's pretty impenatrable.  And if it says what I think it says,
> it seems to violate the way I've understood Unix fork() and how fds=20
> (and stdio buffers) are inherited since forever.

It says that, intuitively,

putc('a');
fork();
exit();

may or may not print 'a' twice, because both parent and child are
operating on the same stream buffer when they call exit() (which implies
a fflush()).  The fix is to:

putc('a');
fflush(NULL);
fork();
exit();

and now you are guaranteed 'a' is only printed once.

You have the same problem, but in the read direction.  You have both
parent and child with a stream buffer that hasn't yet been flushed back
to the fd underlying the stream.  If you add an fflush(NULL) before the
fork(), your bug should go away (if it doesn't, then _that's_ a cygwin
bug).  Yes, it's annoying that BSD and glibc don't comply with POSIX
behavior, and thereby mask the effects of your bug.  And we are patching
cygwin to mirror glibc's bug, so that your bug will also be masked on
cygwin.  But that's no excuse to not fix your program to not trigger the
bug in the first place.

>=20
> However..
>=20
> Do I understand that to say that if the first thing my child does is
>=20
> 	fclose(fp);
>=20
> everything should be hunky-dory?

No.  You have to fix things _in the parent, before the fork()_ for
everything to be hunky-dory.  The easiest way to do that is to
fflush(NULL) before fork()ing.

--=20
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


--SKoUmSLRLvoGcmRfkfPqjmshMl6iSDoKS
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: Public key at http://people.redhat.com/eblake/eblake.gpg
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBCAAGBQJS2Y4dAAoJEKeha0olJ0Nq6U0H/0mYdkHTWlygTIRU2BHrVdeV
Ir8mmc4le9Aw9PX/ZzEu6FJhtGqjfQ9bGRckiYTTyb7MjbH05TmjP2AeEMKdMQzP
Z5E9UwbJTOZ7UGTNNZbcA2UAxq8ggCyAwYxZe5rEccUCKnOI4NaPsU1QMKSUaq3c
2cENoph920n4eaSY50kqwISACe9b3aiwRknYGoTqtvcINMzgkxxjlNvI0KTu95EO
4Ob3WE7QEzVvVHHSYIa21lDj/wjDJXOevbTFG1cDCR487+xErza3GPt+DOx/gKH4
ImYuhY5oCglDmRrODAbQMt2EvD4/JRVLR5W0UbF+hYkbW54ADSvGO5S16uz3PBk=
=PdoG
-----END PGP SIGNATURE-----

--SKoUmSLRLvoGcmRfkfPqjmshMl6iSDoKS--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019