Bug 6314 - losetup : when -o and -r are combined, -o is ignored
Summary: losetup : when -o and -r are combined, -o is ignored
Status: RESOLVED FIXED
Alias: None
Product: Busybox
Classification: Unclassified
Component: Other (show other bugs)
Version: unspecified
Hardware: PC Windows
: P5 minor
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-06-11 10:17 UTC by Vincent G
Modified: 2013-06-27 01:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent G 2013-06-11 10:17:20 UTC
Hi,

I'm using busybox 1.20.2.

I think this is a bug : using losetup, when -r and -o options are combined, -o option is ignored.

Also, combining -r and -d, behaves like no options.

The search of free loop devices was also limited to 8. This is set by kernel command line "max_loop=##" and I want more than 8 loop devices.

Most of the time, losetup was exiting without error.

Here is the patch :


--- util-linux/losetup.c.orig    2013-05-30 11:02:40.000000000 +0200
+++ util-linux/losetup.c    2013-05-30 12:03:54.000000000 +0200
@@ -38,7 +38,7 @@
         OPT_d = (1 << 0),
         OPT_o = (1 << 1),
         OPT_f = (1 << 2),
-        OPT_r = (1 << 3), /* must be last */
+        OPT_r = (1 << 3),
     };
 
     /* max 2 args, -d,-o,-f opts are mutually exclusive */
@@ -46,13 +46,15 @@
     opt = getopt32(argv, "do:fr", &opt_o);
     argv += optind;
 
-    if (opt == OPT_o)
+    if (opt & OPT_o)
         offset = xatoull(opt_o);
 
-    if (opt == OPT_d) {
+    if (opt & OPT_d) {
         /* -d BLOCKDEV */
-        if (!argv[0] || argv[1])
+        if (!argv[0] || argv[1]){
             bb_show_usage();
+            return EXIT_FAILURE;
+        }
         if (del_loop(argv[0]))
             bb_simple_perror_msg_and_die(argv[0]);
         return EXIT_SUCCESS;
@@ -61,12 +63,14 @@
     if (argv[0]) {
         char *s;
 
-        if (opt == OPT_f) /* -f should not have arguments */
+        if (opt & OPT_f){ /* -f should not have arguments */
             bb_show_usage();
+            return EXIT_FAILURE;
+        }
 
         if (argv[1]) {
             /* [-r] [-o OFS] BLOCKDEV FILE */
-            if (set_loop(&argv[0], argv[1], offset, (opt / OPT_r)) < 0)
+            if (set_loop(&argv[0], argv[1], offset, (opt & OPT_r)?1:0) < 0)
                 bb_simple_perror_msg_and_die(argv[0]);
             return EXIT_SUCCESS;
         }
@@ -88,20 +92,20 @@
 
         sprintf(dev, LOOP_FORMAT, n);
         s = query_loop(dev);
-        n++;
         if (!s) {
-            if (n > 9 && errno && errno != ENXIO)
-                return EXIT_SUCCESS;
-            if (opt == OPT_f) {
+            if (errno && errno != ENXIO)
+                return (opt & OPT_f) ? EXIT_FAILURE : EXIT_SUCCESS;
+            if (opt & OPT_f) {
                 puts(dev);
                 return EXIT_SUCCESS;
             }
         } else {
-            if (opt != OPT_f)
+            if (!(opt & OPT_f))
                 printf("%s: %s\n", dev, s);
             if (ENABLE_FEATURE_CLEAN_UP)
                 free(s);
         }
+        n++;
     }
     return EXIT_SUCCESS;
 }


----------------------------------------------------------------------


Note that I saw a strange way to determine the length of the name of a loop device in libbb.h :

# define LOOP_NAMESIZE (sizeof("/dev/loop") + sizeof(int)*3 + 1)
or
# define LOOP_NAMESIZE (sizeof("/dev/loop/") + sizeof(int)*3 + 1)

"sizeof(int)" is not the length of one digit character.... Anyway, this is larger, so it won't harm.


Thanks,
Comment 1 Vincent G 2013-06-22 15:01:28 UTC
Not corrected in 1.21.0
Comment 2 Denys Vlasenko 2013-06-27 01:49:18 UTC
Fixed:

commit 4928e9f7d0a17898300f20567a331417fafc16c1
Author: Denys Vlasenko <vda.linux@googlemail.com>
Date:   Thu Jun 27 03:45:16 2013 +0200

    losetup: assorted fixes. Closes 6314

    "losetup -d" was not complaining that LOOPDEV is missing.
    "losetup -a" was listing only up to /dev/loop9.
    "losetup -f" looped forever if llop0 was taken, and never tried
    anything after /dev/loop9.
    "-o" with other options (say, -r) had no effect.