Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 22 Dec 2001 17:10:03 -0800 (PST)
From:      The Anarcat <anarcat@anarcat.dyndns.org>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: bin/33070: libdisk::Disk_Names() returns empty strings
Message-ID:  <200112230110.fBN1A3a29390@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/33070; it has been noted by GNATS.

From: The Anarcat <anarcat@anarcat.dyndns.org>
To: gnats-admin@FreeBSD.org, freebsd-bugs@FreeBSD.org
Cc: freebsd-gnats-submit@freebsd.org, arch@freebsd.org
Subject: Re: bin/33070: libdisk::Disk_Names() returns empty strings
Date: Sat, 22 Dec 2001 20:02:03 -0500

 This is because of a commit to libdisk which disables the use of
 kern.disks because it doesn't sort its output.
 
 The alternate method of getting the disk list is to stat devices and try
 to open them which fails if you're a regular user.
 
 Here is a proper fix that sort the output of kern.disks in Disk_Names()
 instead.
 
 Index: Makefile
 ===================================================================
 RCS file: /home/ncvs/src/lib/libdisk/Makefile,v
 retrieving revision 1.27.2.5
 diff -u -r1.27.2.5 Makefile
 --- Makefile	18 Sep 2001 06:47:30 -0000	1.27.2.5
 +++ Makefile	22 Dec 2001 23:58:22 -0000
 @@ -6,7 +6,7 @@
  INCS=	libdisk.h
  
 -# Remove KERN_DISKS_BROKEN when kern.disks sysctl returns disks in sorted order
 -CFLAGS+= 	-Wall -DKERN_DISKS_BROKEN
 +CFLAGS+= 	-Wall
  .if ${MACHINE} == "pc98"
  CFLAGS+=	-DPC98
  .endif
 Index: disk.c
 ===================================================================
 RCS file: /home/ncvs/src/lib/libdisk/disk.c,v
 retrieving revision 1.50.2.14
 diff -u -r1.50.2.14 disk.c
 --- disk.c	18 Sep 2001 06:47:30 -0000	1.50.2.14
 +++ disk.c	23 Dec 2001 00:43:01 -0000
 @@ -483,10 +483,18 @@
  static char * device_list[] = {"aacd", "ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "ar", "fd", 0};
  #endif
  
 +int qstrcmp(const void* a, const void* b) {
 +
 +	char *str1 = *(char**)a;
 +	char *str2 = *(char**)b;
 +	return strcmp(str1, str2);
 +
 +}
 +
  char **
  Disk_Names()
  {
 -    int i,j,k;
 +    int i,j,disk_cnt;
      char disk[25];
      char diskname[25];
      struct stat st;
 @@ -506,14 +514,16 @@
  	    error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
  	    if (error) 
  		    return NULL;
 -	    k = 0;
 -	    for (dp = disks; ((*dp = strsep(&disklist, " ")) != NULL) && k < MAX_NO_DISKS; k++, dp++);
 -	    return disks;
 -    }
 +	    disk_cnt = 0;
 +	    for (dp = disks; ((*dp = strsep(&disklist, " ")) != NULL) && 
 +			 disk_cnt < MAX_NO_DISKS; disk_cnt++, dp++);
 +    } else {
      warn("kern.disks sysctl not available");
  #endif
 -    k = 0;
 +    disk_cnt = 0;
  	for (j = 0; device_list[j]; j++) {
 +		if(disk_cnt >= MAX_NO_DISKS)
 +			break;
  		for (i = 0; i < MAX_NO_DISKS; i++) {
  			sprintf(diskname, "%s%d", device_list[j], i);
  			sprintf(disk, _PATH_DEV"%s", diskname);
 @@ -529,12 +539,17 @@
  				continue;
  			}
  			close(fd);
 -			disks[k++] = strdup(diskname);
 -			if(k == MAX_NO_DISKS)
 -				return disks;
 +			disks[disk_cnt++] = strdup(diskname);
 +			if(disk_cnt >= MAX_NO_DISKS)
 +				break;
  		}
  	}
 -	return disks;
 +#if !defined(PC98) && !defined(KERN_DISKS_BROKEN)
 +    }
 +#endif
 +    qsort(disks, disk_cnt, sizeof(char*), qstrcmp);
 +    
 +    return disks;
  }
  
  #ifdef PC98

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200112230110.fBN1A3a29390>