X-Spam-Check-By: sourceware.org
Message-ID: <44EB57F0.7020706@byu.net>
Date: Tue, 22 Aug 2006 13:16:00 -0600
From: Eric Blake <ebb9@byu.net>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Thunderbird/1.5.0.5 Mnenhy/0.7.4.666
MIME-Version: 1.0
To: newlib@sources.redhat.com, cygwin@cygwin.com
Subject: Re: popen bug [patch]
Content-Type: multipart/mixed;  boundary="------------010600070205070502020601"
X-IsSubscribed: yes
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com

--------------010600070205070502020601
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> Cygwin popen does not match Linux popen when stdout is closed.
...
> Oops - when stdout is closed, and the pipe is output on child, cygwin
> popen mistakenly lost the pipe in both processes.
>
>
> The real world case that found this:
> cygwin$ echo 'errprint(esyscmd(echo hi))dnl' | m4 >&-
> sh: line 0: echo: write error: Bad file descriptor
>
> linux$ echo 'errprint(esyscmd(echo hi))dnl' | m4 >&-
> hi

Fixed as follows, along with a fix to obey POSIX "The popen() function
shall ensure that any streams from previous popen() calls that remain open
in the parent process are closed in the new child process."

2006-08-22  Eric Blake  <ebb9@byu.net>

	* libc/posix/popen.c (popen): Don't close output end of pipe in
	child if stdout was closed on entry.
	[HAVE_FCNTL]: In parent, mark file as close-on-exec, per POSIX.

- --
Life is short - so eat dessert first!

Eric Blake             ebb9@byu.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.1 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFE61fw84KuGfSFAYARArN1AJ0ePKBbffbvvJaJOyW2XlsXUBFixwCffAvM
xu28ESQ/Md1uD58Bh77N+V4=
=oy2k
-----END PGP SIGNATURE-----

--------------010600070205070502020601
Content-Type: text/plain;
 name="newlib.patch3"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="newlib.patch3"

Index: libc/posix/popen.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/posix/popen.c,v
retrieving revision 1.5
diff -u -p -r1.5 popen.c
--- libc/posix/popen.c	6 Jun 2003 19:57:51 -0000	1.5
+++ libc/posix/popen.c	22 Aug 2006 19:13:23 -0000
@@ -1,7 +1,7 @@
 /*	$NetBSD: popen.c,v 1.11 1995/06/16 07:05:33 jtc Exp $	*/
 
 /*
- * Copyright (c) 1988, 1993
+ * Copyright (c) 1988, 1993, 2006
  *	The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software written by Ken Arnold and
@@ -55,6 +55,7 @@ static char rcsid[] = "$NetBSD: popen.c,
 #include <stdlib.h>
 #include <string.h>
 #include <paths.h>
+#include <fcntl.h>
 
 static struct pid {
 	struct pid *next;
@@ -102,7 +103,9 @@ _DEFUN(popen, (program, type),
 				(void)dup2(pdes[1], STDOUT_FILENO);
 				(void)close(pdes[1]);
 			}
-			(void) close(pdes[0]);
+			if (pdes[0] != STDOUT_FILENO) {
+				(void) close(pdes[0]);
+			}
 		} else {
 			if (pdes[0] != STDIN_FILENO) {
 				(void)dup2(pdes[0], STDIN_FILENO);
@@ -129,6 +132,12 @@ _DEFUN(popen, (program, type),
 		(void)close(pdes[0]);
 	}
 
+#ifdef HAVE_FCNTL
+	/* Hide pipe from future popens; assume fcntl can't fail.  */
+	fcntl (fileno (iop), F_SETFD,
+	       fcntl (fileno (iop), F_GETFD, 0) | FD_CLOEXEC);
+#endif /* HAVE_FCNTL */
+
 	/* Link into list of file descriptors. */
 	cur->fp = iop;
 	cur->pid =  pid;


--------------010600070205070502020601
Content-Type: text/plain; charset=us-ascii

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/
--------------010600070205070502020601--
