From anarcat@anarcat.dyndns.org  Fri Dec 21 14:06:06 2001
Return-Path: <anarcat@anarcat.dyndns.org>
Received: from tomts10-srv.bellnexxia.net (tomts10.bellnexxia.net [209.226.175.54])
	by hub.freebsd.org (Postfix) with ESMTP id 14ECB37B419
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 21 Dec 2001 14:06:05 -0800 (PST)
Received: from khan.anarcat.dyndns.org ([65.94.189.35])
          by tomts10-srv.bellnexxia.net
          (InterMail vM.4.01.03.16 201-229-121-116-20010115) with ESMTP
          id <20011221220604.DGIA24174.tomts10-srv.bellnexxia.net@khan.anarcat.dyndns.org>
          for <FreeBSD-gnats-submit@freebsd.org>;
          Fri, 21 Dec 2001 17:06:04 -0500
Received: from shall.anarcat.dyndns.org (shall.anarcat.dyndns.org [192.168.0.1])
	by khan.anarcat.dyndns.org (Postfix) with ESMTP
	id 20FC6190B; Fri, 21 Dec 2001 17:06:00 -0500 (EST)
Received: by shall.anarcat.dyndns.org (Postfix, from userid 1000)
	id 8DEB020ACD; Fri, 21 Dec 2001 17:05:58 -0500 (EST)
Message-Id: <20011221220558.8DEB020ACD@shall.anarcat.dyndns.org>
Date: Fri, 21 Dec 2001 17:05:58 -0500 (EST)
From: The Anarcat <anarcat@anarcat.dyndns.org>
Reply-To: The Anarcat <anarcat@anarcat.dyndns.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc: anarcat@anarcat.dyndns.org
Subject: libdisk::Disk_Names() returns empty strings
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         33070
>Category:       bin
>Synopsis:       libdisk::Disk_Names() returns empty strings
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    phk
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Dec 21 14:10:01 PST 2001
>Closed-Date:    Tue Jan 8 08:41:16 PST 2002
>Last-Modified:  Tue Jan 08 08:41:56 PST 2002
>Originator:     The Anarcat
>Release:        FreeBSD 4.4-STABLE i386
>Organization:
Nada, Inc.
>Environment:
System: FreeBSD shall.anarcat.dyndns.org 4.4-STABLE FreeBSD 4.4-STABLE #0: Fri Nov 16 12:57:38 EST 2001 anarcat@shall.anarcat.dyndns.org:/usr/obj/usr/src/sys/SHALL i386

>Description:

Some recent MFC broke Disk_Names() on -stable:

revision 1.50.2.13.2.1
date: 2001/09/18 15:16:21;  author: murray;  state: Exp;  lines: +2 -2
MFS: Don't use kern.disks sysctl.

     Makefile (rev 1.27.2.5)
     disk.c (rev 1.50.2.14)

The diff is:

ndex: disk.c
===================================================================
RCS file: /home/ncvs/src/lib/libdisk/disk.c,v
retrieving revision 1.50.2.13
retrieving revision 1.50.2.13.2.1
diff -u -r1.50.2.13 -r1.50.2.13.2.1
--- disk.c      28 Aug 2001 03:45:47 -0000      1.50.2.13
+++ disk.c      18 Sep 2001 15:16:21 -0000      1.50.2.13.2.1
@@ -6,7 +6,7 @@
  * this stuff is worth it, you can buy me a beer in return.
  * Poul-Henning Kamp
  * ----------------------------------------------------------------------------
  *
- * $FreeBSD: src/lib/libdisk/disk.c,v 1.50.2.13 2001/08/28 03:45:47
  nyan Exp $
+ * $FreeBSD: src/lib/libdisk/disk.c,v 1.50.2.13.2.1 2001/09/18 15:16:21
murray Exp $
  *
  */
 
@@ -499,7 +499,7 @@
 
     disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
     memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
-#ifndef PC98
+#if !defined(PC98) && !defined(KERN_DISKS_BROKEN)
     error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0);
     if (!error) {
            disklist = (char *)malloc(listsize);

I do not understand why, but Disk_Names() returns a pointer to a list of
NULL strings. This is weird. 

It definitly breaks libh's libdisk.

>How-To-Repeat:

#include <iostream>
extern "C" {
#include <sys/types.h>
#include <libdisk.h>
}

int main() {

	char** names = ::Disk_Names();
	cerr << "n: " << names << endl;
	for (char **n = names; *n != NULL; n++) {
		cerr << "disk" << *n << endl;
	}
	
	return 1;

}

Simply returns with "n: 0x..."

gdb also gives us the same sad stuff.

>Fix:

Back out the change.
>Release-Note:
>Audit-Trail:

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

From: The Anarcat <anarcat@anarcat.dyndns.org>
To: gnats-admin@FreeBSD.org, freebsd-bugs@FreeBSD.org
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/33070: libdisk::Disk_Names() returns empty strings
Date: Sun, 23 Dec 2001 17:08:48 -0500

 --QnBU6tTI9sljzm9u
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 Note that this behaviors shows up only as non-root users since the
 problem is that the:
 
 if ((fd =3D open(disk, O_RDWR)) =3D=3D -1)
 
 fails...=20
 
 Also note that the above patch is for -stable only. I'm not sure current
 suffers from these problems.
 
 a.
 
 --QnBU6tTI9sljzm9u
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.0.6 (FreeBSD)
 Comment: For info see http://www.gnupg.org
 
 iEYEARECAAYFAjwmVdsACgkQttcWHAnWiGfUbgCgljOa03tBdpu5oXVm7BHR5SCZ
 wkUAni1JnGyIUZaZ1x3QgSCy6eYVr3B1
 =7ADy
 -----END PGP SIGNATURE-----
 
 --QnBU6tTI9sljzm9u--
State-Changed-From-To: open->closed 
State-Changed-By: sheldonh 
State-Changed-When: Tue Jan 8 08:41:16 PST 2002 
State-Changed-Why:  
Committed and merged onto the RELENG_4 branch in time for 
4.5-RELEASE, thanks! 


Responsible-Changed-From-To: freebsd-bugs->phk 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Tue Jan 8 08:41:16 PST 2002 
Responsible-Changed-Why:  
Poul-Henning committed the patch., 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=33070 
>Unformatted:
