Module Name:    src
Committed By:   christos
Date:           Sun Jun  9 13:25:41 UTC 2013

Modified Files:
        src/usr.sbin/vnconfig: vnconfig.c

Log Message:
Now that we grow vnd's dynamically we cannot depend on the kernel returning
ENXIO when we exceed the number of configured vnds, so in the -l case, print
info for all vnds we can find device nodes for in /dev.


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/usr.sbin/vnconfig/vnconfig.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/vnconfig/vnconfig.c
diff -u src/usr.sbin/vnconfig/vnconfig.c:1.40 src/usr.sbin/vnconfig/vnconfig.c:1.41
--- src/usr.sbin/vnconfig/vnconfig.c:1.40	Tue Aug 30 16:54:18 2011
+++ src/usr.sbin/vnconfig/vnconfig.c	Sun Jun  9 09:25:40 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: vnconfig.c,v 1.40 2011/08/30 20:54:18 joerg Exp $	*/
+/*	$NetBSD: vnconfig.c,v 1.41 2013/06/09 13:25:40 christos Exp $	*/
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -73,11 +73,13 @@
 #include <sys/buf.h>
 #include <sys/disklabel.h>
 #include <sys/disk.h>
+#include <sys/bitops.h>
 
 #include <dev/vndvar.h>
 
 #include <disktab.h>
 #include <err.h>
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stddef.h>
@@ -86,6 +88,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <util.h>
+#include <paths.h>
 
 #define VND_CONFIG	1
 #define VND_UNCONFIG	2
@@ -97,6 +100,7 @@ static int	force = 0;
 static int	compressed = 0;
 static char	*tabname;
 
+static void	show(int, int);
 static int	config(char *, char *, char *, int);
 static int	getgeom(struct vndgeom *, char *);
 __dead static void	usage(void);
@@ -157,10 +161,9 @@ main(int argc, char *argv[])
 			usage();
 		rv = config(argv[0], NULL, NULL, action);
 	} else { /* VND_GET */
+		int n, v;
 		const char *vn;
 		char path[64];
-		struct vnd_user vnu;
-		int v, n;
 
 		if (argc != 0 && argc != 1)
 			usage();
@@ -171,66 +174,83 @@ main(int argc, char *argv[])
 		if (v == -1)
 			err(1, "open: %s", vn);
 
-		for (n = 0; ; n++) {
-			vnu.vnu_unit = argc ? -1 : n;
-			rv = ioctl(v, VNDIOCGET, &vnu);
-			if (rv == -1) {
-				if (errno == ENXIO)
-					break;
-				err(1, "VNDIOCGET");
+		if (argc)
+			show(v, -1);
+		else {
+			DIR *dirp;
+			struct dirent *dp;
+			__BITMAP_TYPE(, uint32_t, 65536) bm;
+
+			__BITMAP_ZERO(&bm);
+
+			if ((dirp = opendir(_PATH_DEV)) == NULL)
+				err(1, "opendir: %s", _PATH_DEV);
+
+			while ((dp = readdir(dirp)) != NULL) {
+				if (strncmp(dp->d_name, "rvnd", 4) != 0)
+					continue;
+				n = atoi(dp->d_name + 4);
+				if (__BITMAP_ISSET(n, &bm))
+					continue;
+				__BITMAP_SET(n, &bm);
+				show(v, n);
 			}
 
-			if (vnu.vnu_ino == 0)
-				printf("vnd%d: not in use\n",
-				    vnu.vnu_unit);
-			else {
-				char *dev;
-				struct statvfs *mnt = NULL;
-				int i, nmount;
-
-				nmount = 0;	/* XXXGCC -Wuninitialized */
-
-				printf("vnd%d: ", vnu.vnu_unit);
-
-				dev = devname(vnu.vnu_dev, S_IFBLK);
-				if (dev != NULL)
-					nmount = getmntinfo(&mnt, MNT_NOWAIT);
-				else
-					mnt = NULL;
-				if (mnt != NULL) {
-					for (i = 0; i < nmount; i++) {
-						if (strncmp(
-						    mnt[i].f_mntfromname,
-						    "/dev/", 5) == 0 &&
-						    strcmp(
-						    mnt[i].f_mntfromname + 5,
-						    dev) == 0)
-							break;
-					}
-					if (i < nmount)
-						printf("%s (%s) ",
-						    mnt[i].f_mntonname,
-						    mnt[i].f_mntfromname);
-					else
-						printf("%s ", dev);
-				}
-				else if (dev != NULL)
-					printf("%s ", dev);
-				else
-					printf("dev %llu,%llu ",
-					    (unsigned long long)major(vnu.vnu_dev),
-					    (unsigned long long)minor(vnu.vnu_dev));
+			closedir(dirp);
+		}
+		close(v);
+		rv = 0;
+	}
+	return rv;
+}
 
-				printf("inode %llu\n",
-				    (unsigned long long)vnu.vnu_ino);
-			}
+static void
+show(int v, int n)
+{
+	struct vnd_user vnu;
+	char *dev;
+	struct statvfs *mnt;
+	int i, nmount;
+
+	vnu.vnu_unit = n;
+	if (ioctl(v, VNDIOCGET, &vnu) == -1)
+		err(1, "VNDIOCGET");
+
+	if (vnu.vnu_ino == 0) {
+		printf("vnd%d: not in use\n", vnu.vnu_unit);
+		return;
+	}
 
-			if (argc)
+	printf("vnd%d: ", vnu.vnu_unit);
+
+	dev = devname(vnu.vnu_dev, S_IFBLK);
+	if (dev != NULL)
+		nmount = getmntinfo(&mnt, MNT_NOWAIT);
+	else {
+		mnt = NULL;
+		nmount = 0;
+	}
+
+	if (mnt != NULL) {
+		for (i = 0; i < nmount; i++) {
+			if (strncmp(mnt[i].f_mntfromname, "/dev/", 5) == 0 &&
+			    strcmp(mnt[i].f_mntfromname + 5, dev) == 0)
 				break;
 		}
-		close(v);
+		if (i < nmount)
+			printf("%s (%s) ", mnt[i].f_mntonname,
+			    mnt[i].f_mntfromname);
+		else
+			printf("%s ", dev);
 	}
-	exit(rv);
+	else if (dev != NULL)
+		printf("%s ", dev);
+	else
+		printf("dev %llu,%llu ",
+		    (unsigned long long)major(vnu.vnu_dev),
+		    (unsigned long long)minor(vnu.vnu_dev));
+
+	printf("inode %llu\n", (unsigned long long)vnu.vnu_ino);
 }
 
 static int

Reply via email to