Subject: bin/2769: Final fix for badsect(8)
To: None <gnats-bugs@gnats.netbsd.org>
From: Curt Sampson <curt@cynic.net>
List: netbsd-bugs
Date: 09/19/1996 13:05:50
>Number:         2769
>Category:       bin
>Synopsis:       Badsect should use raw device rather than block device.
>Confidential:   no
>Severity:       critical
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Sep 19 13:20:01 1996
>Last-Modified:
>Originator:     Curt Sampson
>Organization:
Curt Sampson    curt@portal.ca		Info at http://www.portal.ca/
Internet Portal Services, Inc.	
Vancouver, BC   (604) 257-9400		De gustibus, aut bene aut nihil.
>Release:        NetBSD 1.2_BETA (DIDACTIC) #1: Wed Jul 10 00:40:51 PDT 1996
>Environment:
System: NetBSD didactic.cynic.net 1.2_BETA NetBSD 1.2_BETA (DIDACTIC) #1: Wed Jul 10 00:40:51 PDT 1996 curt@didactic.cynic.net:/usr/src/sys/arch/sparc/compile/DIDACTIC sparc


>Description:
Badsect attempts to write to the block device rather than the raw device,
and thus fails.
>How-To-Repeat:
Run badsect on any drive, given any valid non-inode sector.
>Fix:

Here is a patch. I have tested this, and it works. This replaces
PRs misc/897, bin/994 and bin/2719.

*** /usr2/src/sbin/badsect/badsect.c	Fri Oct 13 18:08:15 1995
--- badsect.c	Fri Sep 13 08:44:27 1996
***************
*** 101,106 ****
--- 102,108 ----
  	register struct direct *dp;
  	DIR *dirp;
  	char name[BUFSIZ];
+ 	int len;
  
  	if (argc < 3) {
  		fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n");
***************
*** 111,122 ****
  		exit(2);
  	}
  	strcpy(name, _PATH_DEV);
  	if ((dirp = opendir(name)) == NULL) {
  		perror(name);
  		exit(3);
  	}
  	while ((dp = readdir(dirp)) != NULL) {
! 		strcpy(&name[5], dp->d_name);
  		if (stat(name, &devstat) < 0) {
  			perror(name);
  			exit(4);
--- 113,125 ----
  		exit(2);
  	}
  	strcpy(name, _PATH_DEV);
+ 	len = strlen(name);
  	if ((dirp = opendir(name)) == NULL) {
  		perror(name);
  		exit(3);
  	}
  	while ((dp = readdir(dirp)) != NULL) {
! 		strcpy(&name[len], dp->d_name);
  		if (stat(name, &devstat) < 0) {
  			perror(name);
  			exit(4);
***************
*** 125,130 ****
--- 128,143 ----
  		    S_ISBLK(devstat.st_mode))
  			break;
  	}
+ 
+ 	/*
+ 	 * XXX We've found the block device, but since the filesystem 
+ 	 * is mounted, we must write to the raw (character) device
+ 	 * instead. We assume the raw device is the block device with
+	 * `r' prepended to the filename.
+ 	 */
+ 	name[len] = 'r';
+ 	strcpy(&name[len+1], dp->d_name);
  	closedir(dirp);
  	if (dp == NULL) {
  		printf("Cannot find dev 0%o corresponding to %s\n",
>Audit-Trail:
>Unformatted: