Subject: bin/8107: badsect fails after transition to ELF (i386)
To: None <gnats-bugs@gnats.netbsd.org>
From: None <cpg@aladdin.de>
List: netbsd-bugs
Date: 07/28/1999 15:25:07
>Number:         8107
>Category:       bin
>Synopsis:       badsect fails after transition to ELF (i386)
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Jul 28 15:05:00 1999
>Last-Modified:
>Originator:     Christian Groessler
>Organization:
	
>Release:        current-19990723
>Environment:
	
System: NetBSD tann 1.4G NetBSD 1.4G (TANN) #0: Sat Jul 24 05:28:57 CEST 1999 chris@tann:/usr/oth/netbsd/sys/arch/i386/compile/TANN i386


>Description:
	after transition of the system to ELF, badsect is unable to create
	bad sector entries.
	E.g.:
	  [tann:/mnt]# badsect BAD 1040
	  badsect: Cannot open `BAD': No such file or directory
	  [tann:/mnt]# ls -l
	  total 1
	  drwxr-xr-x  2 root  wheel  512 Jul 27 00:55 BAD
	  [tann:/mnt]# 

>How-To-Repeat:
	Try to set a bad sector (e.g. on a floppy)

>Fix:
	I think I found the reason:
	When determining the device of the filesystem where to mark the
	bad sector, badsect makes an opendir call to the /dev directory,
	scans this directory and if it find the correct device, it
	closes the directory. The it uses the dp->d_name entry of the
	device to access it. But the data buffer seems to be already
	cleared by the closedir call, so the name is empty.

	The attached patch simply delays the closedir call to after
	using the dp->d_name.

--- badsect.c.org	Mon Feb 22 01:13:31 1999
+++ badsect.c	Tue Jul 27 00:54:23 1999
@@ -132,10 +132,11 @@
 		    S_ISBLK(devstat.st_mode))
 			break;
 	}
-	closedir(dirp);
-	if (dp == NULL)
+	if (dp == NULL) {
+		closedir(dirp);
 		errx(1, "Cannot find dev 0%o corresponding to %s", 
 		    stbuf.st_rdev, argv[1]);
+	}
 
 	/*
 	 * The filesystem is mounted; use the character device instead.
@@ -143,8 +144,10 @@
 	 * the character device.
 	 */
 	(void) snprintf(name, sizeof(name), "%sr%s", _PATH_DEV, dp->d_name);
-	if ((fsi = open(name, O_RDONLY)) == -1)
+	if ((fsi = open(name, O_RDONLY)) == -1) {
+		closedir(dirp);
 		err(1, "Cannot open `%s'", argv[1]);
+	}
 
 	fs = &sblock;
 	rdfs(SBOFF, SBSIZE, (char *)fs);


>Audit-Trail:
>Unformatted: