From asami@silvia.HIP.Berkeley.EDU  Wed Oct 11 03:03:05 1995
Received: from silvia.HIP.Berkeley.EDU (silvia.HIP.Berkeley.EDU [136.152.64.181])
          by freefall.freebsd.org (8.6.12/8.6.6) with ESMTP id DAA02852
          for <FreeBSD-gnats-submit@freebsd.org>; Wed, 11 Oct 1995 03:02:58 -0700
Received: (from asami@localhost) by silvia.HIP.Berkeley.EDU (8.6.12/8.6.9) id DAA20395; Wed, 11 Oct 1995 03:02:54 -0700
Message-Id: <199510111002.DAA20395@silvia.HIP.Berkeley.EDU>
Date: Wed, 11 Oct 1995 03:02:54 -0700
From: asami@cs.berkeley.edu
To: FreeBSD-gnats-submit@freebsd.org
Subject: patch doesn't realize stdin is closed
X-Send-Pr-Version: 3.2

>Number:         777
>Category:       bin
>Synopsis:       patch doesn't realize stdin is closed and asks questions forever
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 11 03:10:03 PDT 1995
>Closed-Date:    Thu Feb 13 13:17:29 PST 1997
>Last-Modified:  Thu Feb 13 13:21:57 PST 1997
>Originator:     Satoshi Asami
>Release:        FreeBSD 2.2-CURRENT i386
>Organization:
The Awesome FreeBSD Ports Team
>Environment:

	I saw this on a very recent stable (thud) as well.

>Description:

	When you logout while a patch is running, and it has to
	ask questions, it will try to do that anyway -- and goes
	into an infinite loop.

>How-To-Repeat:

	% rlogin localhost
	% cat t
	#!/bin/sh
	sleep 5
	patch < /usr/ports/net/zircon/patches/patch-aa > log 2>&1
	% sh ./t & logout
	% ls -s log
	% ls -s log
	% ls -s log
	% ls -s log
	
	and watch it grow forever as it fills up with these two lines:

	File to patch: 
	No file found--skip this patch? [n] 

>Fix:
	
	No idea, I took a look at the source but patch does too many
	funny things with stdio. ;)
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: scrappy 
State-Changed-When: Mon Oct 21 23:12:09 PDT 1996 
State-Changed-Why:  

Dust off the cobwebs - Confirm Status 
State-Changed-From-To: feedback->open 
State-Changed-By: scrappy 
State-Changed-When: Tue Oct 22 01:43:50 PDT 1996 
State-Changed-Why:  

Problem Still Exists 

--- 

*         When you logout while a patch is running, and it has to 
*         ask questions, it will try to do that anyway -- and goes 
*         into an infinite loop. 

Yes, it still exists on -current. 

Satoshi 

Date: Thu, 02 Jan 1997 22:30:59 +1100
From: Giles Lean <giles@nemeton.com.au>
Organization: Nemeton Pty. Ltd.
X-Mailer: Mozilla 3.01 (X11; I; BSD/386 uname failed)
MIME-Version: 1.0
To: freebsd-gnats-submit@freebsd.org, asami@cs.berkeley.edu
CC: giles@nemeton.com.au
Subject: Re: bin/777: patch doesn't realize stdin is closed and asks questions forever
Content-Type: multipart/mixed; boundary="------------446B9B3D2781E494167EB0E7"

This is a multi-part message in MIME format.

--------------446B9B3D2781E494167EB0E7
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

The following patch fixes this problem, although not very
elegantly.  The structure of the patch code's user interaction
doesn't lend itself to returning error codes. :-(

-- 

Giles Lean             <giles@nemeton.com.au>           +61 3 9480 2118
Nemeton Pty. Ltd.                                   fax +61 3 9480 1771
PGP Key fingerprint =  9F FB 28 37 81 F2 AC F3  8A B0 37 E5 73 CF 39 E7

--------------446B9B3D2781E494167EB0E7
Content-Type: text/plain; charset=us-ascii; name="patch_diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="patch_diff"

Index: pch.c
===================================================================
RCS file: /a/FreeBSD/CVS/src/gnu/usr.bin/patch/pch.c,v
retrieving revision 1.8
diff -c -r1.8 pch.c
*** pch.c	1996/04/12 11:37:32	1.8
--- pch.c	1997/01/02 11:24:53
***************
*** 188,193 ****
--- 188,195 ----
  bool
  there_is_another_patch()
  {
+     int try_default_name = 0;
+ 
      if (p_base != 0L && p_base >= p_filesize) {
  	if (verbose)
  	    say1("done\n");
***************
*** 230,239 ****
  	    bestguess = savestr(buf);
  	    filearg[0] = fetchname(buf, 0, FALSE);
  	}
  	if (filearg[0] == Nullch) {
! 	    ask1("No file found--skip this patch? [n] ");
! 	    if (*buf != 'y') {
! 		continue;
  	    }
  	    if (verbose)
  		say1("Skipping patch...\n");
--- 232,247 ----
  	    bestguess = savestr(buf);
  	    filearg[0] = fetchname(buf, 0, FALSE);
  	}
+ 	else {
+ 	    /* keep count -- may be looping after losing /dev/tty */
+ 	    try_default_name++;
+ 	}
  	if (filearg[0] == Nullch) {
! 	    if (try_default_name < 3) {
! 	        ask1("No file found--skip this patch? [n] ");
! 	        if (*buf != 'y') {
! 		    continue;
! 	        }
  	    }
  	    if (verbose)
  		say1("Skipping patch...\n");

--------------446B9B3D2781E494167EB0E7--



From: John-Mark Gurney <jmg@nike.efn.org>
To: asami@cs.berkeley.edu, freebsd-gnats-submit@freefall.FreeBSD.org,
        FreeBSD Current <freebsd-current@freebsd.org>
Cc:  Subject: bin/777 patch had problems with closed tty...
Date: Tue, 11 Feb 1997 00:30:02 -0800 (PST)

 well... I finally found the problem with this bug... it seems that the ask
 routine will default to a cr if there is an error on the read (i.e.
 eof)... so it defaults to "not skip this patch"...  because you didn't
 skip the stdin it tries it again and again...
 
 so the only way to EASILY resolve this bug it to make the default for the
 question to be yes, skip this patch....
 
 here is the patch:
 Index: pch.c
 ===================================================================
 RCS file: /usr/cvs/src/gnu/usr.bin/patch/pch.c,v
 retrieving revision 1.8
 diff -c -r1.8 pch.c
 *** pch.c       1996/04/12 11:37:32     1.8
 --- pch.c       1997/02/11 08:24:31
 ***************
 *** 231,238 ****
             filearg[0] = fetchname(buf, 0, FALSE);
         }
         if (filearg[0] == Nullch) {
 !           ask1("No file found--skip this patch? [n] ");
 !           if (*buf != 'y') {
                 continue;
             }
             if (verbose)
 --- 231,238 ----
             filearg[0] = fetchname(buf, 0, FALSE);
         }
         if (filearg[0] == Nullch) {
 !           ask1("No file found--skip this patch? [y] ");
 !           if (*buf == 'n') {
                 continue;
             }
             if (verbose)
 
 comments?  is it ok if we change the default??  
 
 John-Mark
 
 gurney_j@efn.org
 http://resnet.uoregon.edu/~gurney_j/
 Modem/FAX: (541) 683-6954   (FreeBSD Box)
 
 Live in Peace, destroy Micro$oft, support free software, run FreeBSD (unix)
 

From: Poul-Henning Kamp <phk@critter.dk.tfs.com>
To: John-Mark Gurney <gurney_j@resnet.uoregon.edu>
Cc: asami@cs.berkeley.edu, freebsd-gnats-submit@freefall.FreeBSD.org,
        FreeBSD Current <freebsd-current@freebsd.org>
Subject: Re: bin/777 patch had problems with closed tty... 
Date: Tue, 11 Feb 1997 10:07:47 +0100

 In message <Pine.BSF.3.95q.970211002604.24299g-100000@hydrogen.nike.efn.org>, J
 ohn-Mark Gurney writes:
 >well... I finally found the problem with this bug... it seems that the ask
 >routine will default to a cr if there is an error on the read (i.e.
 >eof)... so it defaults to "not skip this patch"...  because you didn't
 >skip the stdin it tries it again and again...
 >
 >so the only way to EASILY resolve this bug it to make the default for the
 >question to be yes, skip this patch....
 >
 
 >comments?  is it ok if we change the default??  
 
 Yes, it's consistent with POLA.
 
 --
 Poul-Henning Kamp           | phk@FreeBSD.ORG       FreeBSD Core-team.
 http://www.freebsd.org/~phk | phk@login.dknet.dk    Private mailbox.
 whois: [PHK]                | phk@tfs.com           TRW Financial Systems, Inc.
 Power and ignorance is a disgusting cocktail.

From: Giles Lean <giles@nemeton.com.au>
To: John-Mark Gurney <gurney_j@resnet.uoregon.edu>
Cc: asami@cs.berkeley.edu, freebsd-gnats-submit@freefall.freebsd.org,
        FreeBSD Current <freebsd-current@freebsd.org>
Subject: Re: bin/777 patch had problems with closed tty... 
Date: Tue, 11 Feb 1997 22:36:45 +1100

 On Tue, 11 Feb 1997 00:30:02 -0800 (PST)  John-Mark Gurney wrote:
 
 > comments?  is it ok if we change the default??  
 > 
 
 I don't like changing the default.  I'd rather see no fix than do
 this.
 
 I sent in an alternative patch that is ugly but does solve the problem
 without much code change.
 
 Giles
 

From: John-Mark Gurney <jmg@nike.efn.org>
To: Giles Lean <giles@nemeton.com.au>
Cc: freebsd-gnats-submit@freefall.freebsd.org,
        FreeBSD Current <freebsd-current@freebsd.org>
Subject: Re: bin/777 patch had problems with closed tty... 
Date: Tue, 11 Feb 1997 07:12:27 -0800 (PST)

 On Tue, 11 Feb 1997, Giles Lean wrote:
 
 > 
 > On Tue, 11 Feb 1997 00:30:02 -0800 (PST)  John-Mark Gurney wrote:
 > 
 > > comments?  is it ok if we change the default??  
 > > 
 > 
 > I don't like changing the default.  I'd rather see no fix than do
 > this.
 > 
 > I sent in an alternative patch that is ugly but does solve the problem
 > without much code change.
 
 heh...  I see what you mean...
 
 well... I think I have a patch that will please every one :)...
 
 basicly what this does is adds a return value to ask which you can
 check...  ask will now return 0 if there was an "error" when reading... 
 then you can check this return value and if it's 0 know that you should do
 something that will continue processesing... else it will return 1 meaning
 success... 
 
 I've also modified the 'default' fall back case to return 0 for an error
 also.. that way it actually fixes the problem else it would still exist...
 
 anybody have problems with this patch?
 
 here's the patch:
 Index: pch.c
 ===================================================================
 RCS file: /usr/cvs/src/gnu/usr.bin/patch/pch.c,v
 retrieving revision 1.8
 diff -c -r1.8 pch.c
 *** pch.c	1996/04/12 11:37:32	1.8
 --- pch.c	1997/02/11 15:00:09
 ***************
 *** 231,239 ****
   	    filearg[0] = fetchname(buf, 0, FALSE);
   	}
   	if (filearg[0] == Nullch) {
 ! 	    ask1("No file found--skip this patch? [n] ");
 ! 	    if (*buf != 'y') {
 ! 		continue;
   	    }
   	    if (verbose)
   		say1("Skipping patch...\n");
 --- 231,240 ----
   	    filearg[0] = fetchname(buf, 0, FALSE);
   	}
   	if (filearg[0] == Nullch) {
 ! 	    if (ask1("No file found--skip this patch? [n] ")) {
 ! 	    	if (*buf != 'y') {
 ! 		    continue;
 ! 	    	}
   	    }
   	    if (verbose)
   		say1("Skipping patch...\n");
 Index: util.c
 ===================================================================
 RCS file: /usr/cvs/src/gnu/usr.bin/patch/util.c,v
 retrieving revision 1.2
 diff -c -r1.2 util.c
 *** util.c	1995/05/30 05:02:37	1.2
 --- util.c	1997/02/11 15:08:12
 ***************
 *** 227,233 ****
   
   /* Get a response from the user, somehow or other. */
   
 ! void
   ask(pat,arg1,arg2,arg3)
   char *pat;
   long arg1,arg2,arg3;
 --- 227,233 ----
   
   /* Get a response from the user, somehow or other. */
   
 ! int
   ask(pat,arg1,arg2,arg3)
   char *pat;
   long arg1,arg2,arg3;
 ***************
 *** 260,266 ****
       }
       else {				/* no terminal at all--default it */
   	buf[0] = '\n';
 ! 	r = 1;
       }
       if (r <= 0)
   	buf[0] = 0;
 --- 260,268 ----
       }
       else {				/* no terminal at all--default it */
   	buf[0] = '\n';
 ! 	buf[1] = 0;
 ! 	say1(buf);
 ! 	return 0;			/* signal possible error */
       }
       if (r <= 0)
   	buf[0] = 0;
 ***************
 *** 268,273 ****
 --- 270,280 ----
   	buf[r] = '\0';
       if (!tty2)
   	say1(buf);
 + 
 +     if (r <= 0)
 + 	return 0;			/* if there was an error, return it */
 +     else
 + 	return 1;
   }
   #endif /* lint */
   
 Index: util.h
 ===================================================================
 RCS file: /usr/cvs/src/gnu/usr.bin/patch/util.h,v
 retrieving revision 1.2
 diff -c -r1.2 util.h
 *** util.h	1995/05/30 05:02:38	1.2
 --- util.h	1997/02/11 14:49:19
 ***************
 *** 86,92 ****
   void say();
   void fatal();
   void pfatal();
 ! void ask();
   char *savestr();
   void set_signals();
   void ignore_signals();
 --- 86,92 ----
   void say();
   void fatal();
   void pfatal();
 ! int ask();
   char *savestr();
   void set_signals();
   void ignore_signals();
 
 
 John-Mark
 
 gurney_j@efn.org
 http://resnet.uoregon.edu/~gurney_j/
 Modem/FAX: (541) 683-6954   (FreeBSD Box)
 
 Live in Peace, destroy Micro$oft, support free software, run FreeBSD (unix)
 
State-Changed-From-To: open->closed 
State-Changed-By: jmg 
State-Changed-When: Thu Feb 13 13:17:29 PST 1997 
State-Changed-Why:  
fixed patch to be aware of when there is a read error, and act appropriately. 
>Unformatted:
