delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/08/18/13:56:53

X-Spam-Check-By: sourceware.org
To: cygwin AT cygwin DOT com
From: Eric Blake <ebb9 AT byu DOT net>
Subject: popen bug
Date: Fri, 18 Aug 2006 17:55:59 +0000 (UTC)
Lines: 64
Message-ID: <loom.20060818T193208-233@post.gmane.org>
Mime-Version: 1.0
User-Agent: Loom/3.14 (http://gmane.org/)
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
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

Cygwin popen does not match Linux popen when stdout is closed.

In cygwin window 1:
$ cat foo.c
#include <stdio.h>
#include <stdlib.h>

int main()
{
   FILE *f;
   f = popen("sleep 30", "r");
   if (!f)
      return 1;
   getc(f);
   if (pclose(f))
      return 2;
   return 0;
}
$ ./foo >&-

In cygwin window 2, during the 30-second window:
$ pstree -p # determine pid of foo, sleep
...
$ ll /proc/<foo>/fd /proc/<sleep>/fd
/proc/5096/fd:
total 0
lrwxrwxrwx 1 eblake Domain Users 0 Aug 18 11:37 0 -> /dev/tty5
lrwxrwxrwx 1 eblake Domain Users 0 Aug 18  2006 2 -> /dev/tty5

/proc/9692/fd:
total 0
lrwxrwxrwx 1 eblake Domain Users 0 Aug 18 11:37 0 -> /dev/tty5
lrwxrwxrwx 1 eblake Domain Users 0 Aug 18 11:37 2 -> /dev/tty5


Repeat, but on Linux:

$ ll /proc/17837/fd /proc/17838/fd
/proc/17837/fd:
total 3
lrwx------  1 ericb mauve 64 Aug 18 17:36 0 -> /dev/pts/0
lr-x------  1 ericb mauve 64 Aug 18 17:36 1 -> pipe:[234602911]
lrwx------  1 ericb mauve 64 Aug 18 17:36 2 -> /dev/pts/0

/proc/17838/fd:
total 3
lrwx------  1 ericb mauve 64 Aug 18 17:36 0 -> /dev/pts/0
l-wx------  1 ericb mauve 64 Aug 18 17:36 1 -> pipe:[234602911]
lrwx------  1 ericb mauve 64 Aug 18 17:36 2 -> /dev/pts/0

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

-- 
Eric Blake



--
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/

- Raw text -


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