35b2031
--- util-linux-2.13-pre2/mount/lomount.c.all	2005-08-29 16:59:06.000000000 +0200
35b2031
+++ util-linux-2.13-pre2/mount/lomount.c	2005-08-29 17:17:49.000000000 +0200
35b2031
@@ -28,6 +28,8 @@
35b2031
 extern char *xstrdup (const char *s);	/* not: #include "sundries.h" */
35b2031
 extern void error (const char *fmt, ...);	/* idem */
35b2031
 
35b2031
+#define SIZE(a) (sizeof(a)/sizeof(a[0]))
35b2031
+
35b2031
 #ifdef LOOP_SET_FD
35b2031
 
35b2031
 static int
35b2031
@@ -128,6 +130,42 @@
35b2031
 	close (fd);
35b2031
 	return 1;
35b2031
 }
35b2031
+
35b2031
+static int
35b2031
+show_used_loop_devices (void) {
35b2031
+	char dev[20];
35b2031
+	char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
35b2031
+	int i, j, fd, permission = 0, somedev = 0;
35b2031
+	struct stat statbuf;
35b2031
+	struct loop_info loopinfo;
35b2031
+
35b2031
+	for (j = 0; j < SIZE(loop_formats); j++) {
35b2031
+	    for(i = 0; i < 256; i++) {
35b2031
+		sprintf(dev, loop_formats[j], i);
35b2031
+		if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
35b2031
+			somedev++;
35b2031
+			fd = open (dev, O_RDONLY);
35b2031
+			if (fd >= 0) {
35b2031
+				if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
35b2031
+					show_loop(dev);
35b2031
+				close (fd);
35b2031
+				somedev++;
35b2031
+			} else if (errno == EACCES)
35b2031
+				permission++;
35b2031
+			continue; /* continue trying as long as devices exist */
35b2031
+		}
35b2031
+		break;
35b2031
+	    }
35b2031
+	}
35b2031
+
35b2031
+	if (somedev==0 && permission) {
35b2031
+		error(_("%s: no permission to look at /dev/loop#"), progname);
35b2031
+		return 1;
35b2031
+	}
35b2031
+	return 0;
35b2031
+}
35b2031
+
35b2031
+
35b2031
 #endif
35b2031
 
35b2031
 int
35b2031
@@ -139,8 +177,6 @@
35b2031
 		major(statbuf.st_rdev) == LOOPMAJOR);
35b2031
 }
35b2031
 
35b2031
-#define SIZE(a) (sizeof(a)/sizeof(a[0]))
35b2031
-
35b2031
 char *
35b2031
 find_unused_loop_device (void) {
35b2031
 	/* Just creating a device, say in /tmp, is probably a bad idea -
35b2031
@@ -403,12 +439,13 @@
35b2031
 
35b2031
 static void
35b2031
 usage(void) {
35b2031
-	fprintf(stderr, _("usage:\n\
35b2031
-  %s loop_device                                       # give info\n\
35b2031
-  %s -d loop_device                                    # delete\n\
35b2031
-  %s -f                                                # find unused\n\
35b2031
-  %s [-e encryption] [-o offset] {-f|loop_device} file # setup\n"),
35b2031
-		progname, progname, progname, progname);
35b2031
+	fprintf(stderr, _("usage:\n"
35b2031
+  "  %1$s loop_device                                       # give info\n"
35b2031
+  "  %1$s -d loop_device                                    # delete\n"
35b2031
+  "  %1$s -f                                                # find unused\n"
35b2031
+  "  %1$s -a                                                # list all used\n"
35b2031
+  "  %1$s [-e encryption] [-o offset] {-f|loop_device} file # setup\n"),
35b2031
+		progname);
35b2031
 	exit(1);
35b2031
 }
35b2031
 
35b2031
@@ -442,7 +479,7 @@
35b2031
 int
35b2031
 main(int argc, char **argv) {
35b2031
 	char *p, *offset, *encryption, *passfd, *device, *file;
35b2031
-	int delete, find, c;
35b2031
+	int delete, find, c, all;
35b2031
 	int res = 0;
35b2031
 	int ro = 0;
35b2031
 	int pfd = -1;
35b2031
@@ -452,7 +489,7 @@
35b2031
 	bindtextdomain(PACKAGE, LOCALEDIR);
35b2031
 	textdomain(PACKAGE);
35b2031
 
35b2031
-	delete = find = 0;
35b2031
+	delete = find = all = 0;
35b2031
 	off = 0;
35b2031
 	offset = encryption = passfd = NULL;
35b2031
 
35b2031
@@ -460,8 +497,11 @@
35b2031
 	if ((p = strrchr(progname, '/')) != NULL)
35b2031
 		progname = p+1;
35b2031
 
35b2031
-	while ((c = getopt(argc, argv, "de:E:fo:p:v")) != -1) {
35b2031
+	while ((c = getopt(argc, argv, "ade:E:fo:p:v")) != -1) {
35b2031
 		switch (c) {
35b2031
+		case 'a':
35b2031
+			all = 1;
35b2031
+			break;
35b2031
 		case 'd':
35b2031
 			delete = 1;
35b2031
 			break;
35b2031
@@ -489,17 +529,22 @@
35b2031
 	if (argc == 1) {
35b2031
 		usage();
35b2031
 	} else if (delete) {
35b2031
-		if (argc != optind+1 || encryption || offset || find)
35b2031
+		if (argc != optind+1 || encryption || offset || find || all)
35b2031
 			usage();
35b2031
 	} else if (find) {
35b2031
-		if (argc < optind || argc > optind+1)
35b2031
+		if (all || argc < optind || argc > optind+1)
35b2031
+			usage();
35b2031
+	} else if (all) {
35b2031
+		if (argc > 2)
35b2031
 			usage();
35b2031
 	} else {
35b2031
 		if (argc < optind+1 || argc > optind+2)
35b2031
 			usage();
35b2031
 	}
35b2031
 
35b2031
-	if (find) {
35b2031
+	if (all)
35b2031
+		return show_used_loop_devices();
35b2031
+	else if (find) {
35b2031
 		device = find_unused_loop_device();
35b2031
 		if (device == NULL)
35b2031
 			return -1;