From fenner@parc.xerox.com  Fri Feb 28 11:15:13 1997
Received: from alpha.xerox.com (alpha.Xerox.COM [13.1.64.93])
          by freefall.freebsd.org (8.8.5/8.8.5) with SMTP id LAA12385
          for <FreeBSD-gnats-submit@freebsd.org>; Fri, 28 Feb 1997 11:15:13 -0800 (PST)
Received: from fenestro.parc.xerox.com ([13.0.208.199]) by alpha.xerox.com with SMTP id <16973(4)>; Fri, 28 Feb 1997 11:14:38 PST
Received: (from fenner@localhost)
          by fenestro.parc.xerox.com (8.8.4/8.8.4)
	  id LAA10869; Fri, 28 Feb 1997 11:14:52 -0800 (PST)
Message-Id: <199702281914.LAA10869@fenestro.parc.xerox.com>
Date: Fri, 28 Feb 1997 11:14:52 PST
From: Bill Fenner <fenner@parc.xerox.com>
Reply-To: fenner@parc.xerox.com
To: FreeBSD-gnats-submit@freebsd.org
Subject: w treats corrupted utmp as fatal error
X-Send-Pr-Version: 3.2

>Number:         2832
>Category:       bin
>Synopsis:       w treats corrupted utmp as fatal error
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Feb 28 11:20:01 PST 1997
>Closed-Date:    Thu May 14 03:22:39 PDT 1998
>Last-Modified:  Thu May 14 03:25:27 PDT 1998
>Originator:     Bill Fenner
>Release:        FreeBSD 2.2-BETA_A i386
>Organization:
Xerox
>Environment:

	
FreeBSD fenestro.parc.xerox.com 2.2-BETA_A FreeBSD 2.2-BETA_A #1: Sat Jan 18 18:30:51 PST 1997     fenner@fenestro.parc.xerox.com:/usr/src/sys/compile/FENESTRO  i386

2.2-BETA installed via ftp from ftp.freebsd.org in mid-January.
X11R6 installed in that installation.

>Description:

	
My utmp file got corrupted (I don't know how).

% who | cat -v
root     ttyv0   Feb 22 20:08
fenner   ttyv1   Feb 22 21:30
fenner   ttyp5   Feb 27 17:26	(crevenia)
4        {q^P3ttypDec 31 16:00	(er)

w fails to run:

% w
w: /dev//{q^P3ttyp4: No such file or directory

while it could at least manage to print out as much as who does.

>How-To-Repeat:

	
Corrupt your utmp file so that a tty field references a nonexistent
device, and then run "w".


>Fix:
	
	

--- w.c.orig	Fri Feb 28 11:10:06 1997
+++ w.c	Fri Feb 28 11:11:28 1997
@@ -438,7 +438,7 @@
 
 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
 	if (stat(ttybuf, &sb))
-		err(1, "%s", ttybuf);
+		warn("%s", ttybuf);
 	return (&sb);
 }
 
% ./w |& cat -v
w: /dev//{q^P3ttyp4: No such file or directory
11:12AM  up 5 days, 15:06, 5 users, load averages: 0.04, 0.03, 0.00
USER     TTY FROM              LOGIN@  IDLE WHAT
root     v0  -                Sat08PM 13:54 -csh (csh)
fenner   v1  -                Sat09PM 5days xinit
ttyp0        fenner           13Aug95  9:12 -
fenner   p5  crevenia          5:26PM     - ./w
4        {q^P er               31Dec69     - -

>Release-Note:
>Audit-Trail:

From: Steve Price <sprice@hiwaay.net>
To: freebsd-gnats-submit@freebsd.org, fenner@parc.xerox.com
Cc:  Subject: Re: bin/2832: w treats corrupted utmp as fatal error
Date: Sun, 11 Jan 1998 18:44:48 -0600

 --- w.c.orig    Fri Feb 28 11:10:06 1997
 +++ w.c Fri Feb 28 11:11:28 1997
 @@ -438,7 +438,7 @@
        
  (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
  if (stat(ttybuf, &sb))
 -		err(1, "%s", ttybuf);
 +		warn("%s", ttybuf);
  	return (&sb);
 }
 
 After the stat(2) fails the contents of 'sb' should be considered
 indeterminate.  Since the code goes on to use the sb.st_rdev member,
 I would say this is a fatal error and not just a warning if the
 stat(2) fails.
 
 Would you care to rework w(1) to not use the return value from
 ttystat() if the stat(2) fails?
 
 Steve

From: A Joseph Koshy <koshy@india.hp.com>
To: freebsd-gnats-submit@freebsd.org, fenner@parc.xerox.com
Cc: phk@freebsd.org, steve@freebsd.org
Subject: Re: bin/2832: w treats corrupted utmp as fatal error
Date: Wed, 06 May 1998 14:37:50 +0530

 Here is a patch that fixes the problem pointed out by Steve P.
 This was generated against r1.25 of "w.c" i.e --current.  
 -stable can use the same fix.
 
 Regards,
 Koshy
 
 -------- -------- -------- -------- -------- -------- -------- --------
 
 # diff -u w.c-- w.c
 --- w.c--       Mon Sep  1 11:17:26 1997
 +++ w.c Wed Sep 17 11:13:19 1997
 @@ -209,6 +209,8 @@
         for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
                 if (utmp.ut_name[0] == '\0')
                         continue;
 +               if (!(stp = ttystat(utmp.ut_line)))
 +                       continue;
                 ++nusers;
                 if (wcmd == 0 || (sel_user &&
                     strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
 @@ -218,7 +220,6 @@
                 *nextp = ep;
                 nextp = &(ep->next);
                 memmove(&(ep->utmp), &utmp, sizeof(struct utmp));
 -               stp = ttystat(ep->utmp.ut_line);
                 ep->tdev = stp->st_rdev;
  #ifdef CPU_CONSDEV
                 /*
 @@ -476,8 +477,10 @@
         char ttybuf[MAXPATHLEN];
 
         (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
 -       if (stat(ttybuf, &sb))
 -               err(1, "%s", ttybuf);
 +       if (stat(ttybuf, &sb)) {
 +               warn("%s", ttybuf);
 +               return NULL;
 +       }
         return (&sb);
  }
 
 
 -------- -------- -------- -------- -------- -------- -------- --------
State-Changed-From-To: open->closed 
State-Changed-By: jkoshy 
State-Changed-When: Thu May 14 03:22:39 PDT 1998 
State-Changed-Why:  
Fixed in rev 1.26 of w.c 
>Unformatted:
