From lennox@cs.columbia.edu  Tue Sep 30 11:19:41 2003
Return-Path: <lennox@cs.columbia.edu>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id A932916A4C0
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 30 Sep 2003 11:19:41 -0700 (PDT)
Received: from cs.columbia.edu (cs.columbia.edu [128.59.16.20])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 44B8743FDD
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 30 Sep 2003 11:19:39 -0700 (PDT)
	(envelope-from lennox@cs.columbia.edu)
Received: from cnr.cs.columbia.edu (cnr.cs.columbia.edu [128.59.19.133])
	by cs.columbia.edu (8.12.10/8.12.10) with ESMTP id h8UIJaLa012297
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT)
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 30 Sep 2003 14:19:37 -0400 (EDT)
Received: from cnr.cs.columbia.edu (localhost [127.0.0.1])
	by cnr.cs.columbia.edu (8.12.9p1/8.12.9) with ESMTP id h8UIJa6b022390
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 30 Sep 2003 14:19:36 -0400 (EDT)
	(envelope-from lennox@cnr.cs.columbia.edu)
Received: (from lennox@localhost)
	by cnr.cs.columbia.edu (8.12.9p1/8.12.9/Submit) id h8UIJZv5022389;
	Tue, 30 Sep 2003 14:19:35 -0400 (EDT)
Message-Id: <200309301819.h8UIJZv5022389@cnr.cs.columbia.edu>
Date: Tue, 30 Sep 2003 14:19:35 -0400 (EDT)
From: Jonathan Lennox <lennox@cs.columbia.edu>
Reply-To: Jonathan Lennox <lennox@cs.columbia.edu>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: /usr/bin/script fails silently if stdin is not a terminal
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         57414
>Category:       bin
>Synopsis:       /usr/bin/script fails silently if stdin is not a terminal
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    cperciva
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Sep 30 11:20:17 PDT 2003
>Closed-Date:    Sat Mar 13 01:25:23 PST 2004
>Last-Modified:  Sat Mar 13 01:25:23 PST 2004
>Originator:     Jonathan Lennox
>Release:        FreeBSD 5.1-RELEASE-p7 i386
>Organization:
Columbia University
>Environment:
System: FreeBSD cnr.cs.columbia.edu 5.1-RELEASE-p7 FreeBSD 5.1-RELEASE-p7 #7: Wed Sep 24 18:19:12 EDT 2003 lennox@cnr.cs.columbia.edu:/usr/obj/usr/src/sys/CNR i386


	
>Description:

The /usr/bin/script utility fails silently if its standard input is not a
terminal.  It does nothing, and does not execute its argument, but exits
with a successful output status.

This is not such a big deal if the user is using 'script' directly -- it's
pretty easy to figure out what happened.  However, this can be *extremely*
surprising -- and dangerous -- if 'script' is called by some other program
which doesn't document this limitation.

In particular, the 'portupgrade' utility wraps its calls to 'make' and 'make
install' of upgraded ports in 'script'.  As a result, if portupgrade is run
with its standard input redirected, out-of-date ports will be deleted and
then not reinstalled, with no warning or indication of any problem.

>How-To-Repeat:
$ script /var/tmp/foo cat /etc/passwd < /dev/null
outputs only
Script started, output file is /var/tmp/foo

Script done, output file is /var/tmp/foo
and /var/tmp/foo is empty.

# BATCH=yes portupgrade -arRv < /dev/null > /var/tmp/portupgrade.log 2>&1 &
(which seems like a perfectly reasonable way of upgrading all one's ports,
without having to stay logged in to watch it happen)
deletes all out-of-date ports from your system, without replacing them with
up-to-date versions.

>Fix:

The following patch causes /usr/bin/script to error quickly with a non-zero
exit status if tcgetattr or ioctl fail, as they will if stdin is not a tty.

--- usr.bin/script/script.1.orig	Tue Sep 30 14:02:29 2003
+++ usr.bin/script/script.1	Tue Sep 30 14:06:29 2003
@@ -155,3 +155,7 @@
 mode, echo cancelling is far from ideal.  The slave terminal mode is checked
 for ECHO mode to check when to avoid manual echo logging.  This does not
 work when in a raw mode where the program being run is doing manual echo.
+.Pp
+The
+.Nm
+utility fails if the standard input is not a terminal.
--- usr.bin/script/script.c.orig	Tue Sep 30 14:01:46 2003
+++ usr.bin/script/script.c	Tue Sep 30 14:04:48 2003
@@ -126,8 +126,10 @@
 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
 		err(1, "%s", fname);
 
-	(void)tcgetattr(STDIN_FILENO, &tt);
-	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
+	if (tcgetattr(STDIN_FILENO, &tt) == -1)
+		err(1, "tcgetattr");
+	if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
+		err(1, "ioctl");
 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
 		err(1, "openpty");




>Release-Note:
>Audit-Trail:

From: Colin Percival <colin.percival@wadham.ox.ac.uk>
To: freebsd-gnats-submit@FreeBSD.org, lennox@cs.columbia.edu
Cc:  
Subject: Re: bin/57414: /usr/bin/script fails silently if stdin is not
  a terminal
Date: Fri, 10 Oct 2003 20:09:49 +0100

    This is the same bug as PR bin/56166 -- the issue is not one of whether 
 stdin is a terminal, but rather relates to the behaviour of select(2) -- 
 /usr/bin/script exits if stdin is closed even if it is operating in 
 execute-a-script mode instead of log-this-shell mode.
 
 Colin Percival
 
State-Changed-From-To: open->patched 
State-Changed-By: cperciva 
State-Changed-When: Thu Jan 22 13:08:22 PST 2004 
State-Changed-Why:  
Fixed in -current, will MFC in 7 days. 
Claim ownership to remind myself to close this later. 



Responsible-Changed-From-To: freebsd-bugs->cperciva 
Responsible-Changed-By: cperciva 
Responsible-Changed-When: Thu Jan 22 13:08:22 PST 2004 
Responsible-Changed-Why:  
Fixed in -current, will MFC in 7 days. 
Claim ownership to remind myself to close this later. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=57414 
State-Changed-From-To: patched->closed 
State-Changed-By: cperciva 
State-Changed-When: Sat Mar 13 01:25:05 PST 2004 
State-Changed-Why:  
MFC to RELENG_4 is done. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=57414 
>Unformatted:
