Subject: bin/25699: sh(1) hangs opening a named pipe as stdin for background process
To: None <gnats-bugs@gnats.netbsd.org>
From: None <dyoung@netbsd.org>
List: netbsd-bugs
Date: 05/25/2004 07:19:46
>Number:         25699
>Category:       bin
>Synopsis:       sh(1) hangs opening a named pipe as stdin for background process
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue May 25 07:20:00 UTC 2004
>Closed-Date:
>Last-Modified:
>Originator:     David Young
>Release:        NetBSD 2.0E
>Organization:
OJC Technologies
>Environment:
System: NetBSD cuw.ojctech.com 2.0E NetBSD 2.0E (GENERIC.merge) #38: Sun May  9 04:24:01 CDT 2004  dyoung@cuw.ojctech.com:/u1/dyoung/merge-nbsd/O/sys/arch/i386/compile/GENERIC.merge i386
Architecture: i386
Machine: i386
cuw:~> ident /bin/sh
/bin/sh:
     $NetBSD: crt0.c,v 1.13 2003/07/26 19:24:27 salo Exp $
     $NetBSD: alias.c,v 1.12 2003/08/07 09:05:29 agc Exp $
     $NetBSD: cd.c,v 1.34 2003/11/14 20:00:28 dsl Exp $
     $NetBSD: error.c,v 1.31 2003/08/07 09:05:30 agc Exp $
     $NetBSD: eval.c,v 1.76 2004/04/30 06:27:59 dsl Exp $
     $NetBSD: exec.c,v 1.37 2003/08/07 09:05:31 agc Exp $
     $NetBSD: expand.c,v 1.60 2003/12/21 08:32:39 jdolecek Exp $
     $NetBSD: histedit.c,v 1.34 2003/10/27 06:19:29 lukem Exp $
     $NetBSD: input.c,v 1.39 2003/08/07 09:05:32 agc Exp $
     $NetBSD: jobs.c,v 1.62 2003/12/18 00:56:05 christos Exp $
     $NetBSD: mail.c,v 1.16 2003/08/07 09:05:33 agc Exp $
     $NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv Exp $
     $NetBSD: memalloc.c,v 1.28 2003/08/07 09:05:34 agc Exp $
     $NetBSD: miscbltin.c,v 1.34 2004/04/19 01:36:32 lukem Exp $
     $NetBSD: mystring.c,v 1.16 2003/08/07 09:05:35 agc Exp $
     $NetBSD: options.c,v 1.36 2004/01/05 23:23:32 jmmv Exp $
     $NetBSD: parser.c,v 1.55 2003/08/07 09:05:37 agc Exp $
     $NetBSD: redir.c,v 1.28 2003/08/07 09:05:37 agc Exp $
     $NetBSD: show.c,v 1.26 2003/11/14 10:46:13 dsl Exp $
     $NetBSD: trap.c,v 1.30 2003/08/26 18:13:25 jmmv Exp $
     $NetBSD: output.c,v 1.28 2003/08/07 09:05:36 agc Exp $
     $NetBSD: var.c,v 1.34 2003/08/26 18:14:24 jmmv Exp $
     $NetBSD: test.c,v 1.25 2002/05/25 23:12:16 wiz Exp $
     $NetBSD: printf.c,v 1.29 2003/08/07 11:15:33 agc Exp $
     $NetBSD: kill.c,v 1.23 2003/08/07 09:05:13 agc Exp $
     $NetBSD: skeleton.c,v 1.25 2003/08/07 11:17:54 agc Exp $
     $NetBSD: arith.y,v 1.17 2003/09/17 17:33:36 jmmv Exp $
     $NetBSD: arith_lex.l,v 1.12 2003/08/07 09:05:30 agc Exp $

>Description:
sh(1) hangs if it runs a script that starts a background process
whose standard input is redirected from a named pipe that a second
process does not have open for writing.

After sh(1) vfork(2)'s the background process, it open(2)'s the named
pipe.  Because the pipe is not opened for writing by a second process,
open(2) blocks.  Since the child process is still running in the
context of its parent, both parent and child are blocked.  This is
different from the old sh(1) behavior, where background processes were
started with fork(2), not vfork(2). 

>How-To-Repeat:
Run this script:

	#!/bin/sh

	fifoname=./$(basename $0)-fifo
	mkfifo $fifoname

	cut -d ' ' -f 2 < $fifoname &

	# never gets here

	cat > $fifoname <<EOF
	donkey kong
	david young
	abraham lincoln
	EOF

	rm $fifoname

Observe the parent and child state:

> ps aux
USER       PID %CPU %MEM  VSZ    RSS TT STAT STARTED     TIME COMMAND
dyoung   10515  0.0  0.1  136    676 p4 IV+   1:58AM  0:00.00 /bin/sh ./sh-bug
dyoung   27874  0.0  0.1  136    676 p4 D+    1:58AM  0:00.00 /bin/sh ./sh-bug
 
>Fix:
Don't vfork a background process?

Do something more elaborate?
>Release-Note:
>Audit-Trail:
>Unformatted: