aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libbb.h4
-rw-r--r--util-linux/losetup.c37
2 files changed, 23 insertions, 18 deletions
diff --git a/include/libbb.h b/include/libbb.h
index b5d1156ae..65c6b9f39 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1803,7 +1803,7 @@ extern const char bb_default_login_shell[] ALIGN1;
1803# define VC_4 "/dev/vc/4" 1803# define VC_4 "/dev/vc/4"
1804# define VC_5 "/dev/vc/5" 1804# define VC_5 "/dev/vc/5"
1805# define VC_FORMAT "/dev/vc/%d" 1805# define VC_FORMAT "/dev/vc/%d"
1806# define LOOP_FORMAT "/dev/loop/%d" 1806# define LOOP_FORMAT "/dev/loop/%u"
1807# define LOOP_NAMESIZE (sizeof("/dev/loop/") + sizeof(int)*3 + 1) 1807# define LOOP_NAMESIZE (sizeof("/dev/loop/") + sizeof(int)*3 + 1)
1808# define LOOP_NAME "/dev/loop/" 1808# define LOOP_NAME "/dev/loop/"
1809# define FB_0 "/dev/fb/0" 1809# define FB_0 "/dev/fb/0"
@@ -1816,7 +1816,7 @@ extern const char bb_default_login_shell[] ALIGN1;
1816# define VC_4 "/dev/tty4" 1816# define VC_4 "/dev/tty4"
1817# define VC_5 "/dev/tty5" 1817# define VC_5 "/dev/tty5"
1818# define VC_FORMAT "/dev/tty%d" 1818# define VC_FORMAT "/dev/tty%d"
1819# define LOOP_FORMAT "/dev/loop%d" 1819# define LOOP_FORMAT "/dev/loop%u"
1820# define LOOP_NAMESIZE (sizeof("/dev/loop") + sizeof(int)*3 + 1) 1820# define LOOP_NAMESIZE (sizeof("/dev/loop") + sizeof(int)*3 + 1)
1821# define LOOP_NAME "/dev/loop" 1821# define LOOP_NAME "/dev/loop"
1822# define FB_0 "/dev/fb0" 1822# define FB_0 "/dev/fb0"
diff --git a/util-linux/losetup.c b/util-linux/losetup.c
index c69763335..d450b5a78 100644
--- a/util-linux/losetup.c
+++ b/util-linux/losetup.c
@@ -10,12 +10,12 @@
10//usage:#define losetup_trivial_usage 10//usage:#define losetup_trivial_usage
11//usage: "[-r] [-o OFS] {-f|LOOPDEV} FILE - associate loop devices\n" 11//usage: "[-r] [-o OFS] {-f|LOOPDEV} FILE - associate loop devices\n"
12//usage: " losetup -d LOOPDEV - disassociate\n" 12//usage: " losetup -d LOOPDEV - disassociate\n"
13//usage: " losetup -a - show status of all\n" 13//usage: " losetup -a - show status\n"
14//usage: " losetup -f - show next available" 14//usage: " losetup -f - show next free loop device"
15//usage:#define losetup_full_usage "\n\n" 15//usage:#define losetup_full_usage "\n\n"
16//usage: " -o OFS Start OFS bytes into FILE" 16//usage: " -o OFS Start OFS bytes into FILE"
17//usage: "\n -r Read-only" 17//usage: "\n -r Read-only"
18//usage: "\n -f Show/find first free loop device" 18//usage: "\n -f Show/use next free loop device"
19//usage: 19//usage:
20//usage:#define losetup_notes_usage 20//usage:#define losetup_notes_usage
21//usage: "One argument (losetup /dev/loop1) will display the current association\n" 21//usage: "One argument (losetup /dev/loop1) will display the current association\n"
@@ -27,6 +27,10 @@
27 27
28#include "libbb.h" 28#include "libbb.h"
29 29
30/* 1048575 is a max possible minor number in Linux circa 2010 */
31/* for now use something less extreme */
32#define MAX_LOOP_NUM 1023
33
30int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 34int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31int losetup_main(int argc UNUSED_PARAM, char **argv) 35int losetup_main(int argc UNUSED_PARAM, char **argv)
32{ 36{
@@ -59,7 +63,7 @@ int losetup_main(int argc UNUSED_PARAM, char **argv)
59 } 63 }
60 64
61 /* -d LOOPDEV */ 65 /* -d LOOPDEV */
62 if (opt == OPT_d) { 66 if (opt == OPT_d && argv[0]) {
63 if (del_loop(argv[0])) 67 if (del_loop(argv[0]))
64 bb_simple_perror_msg_and_die(argv[0]); 68 bb_simple_perror_msg_and_die(argv[0]);
65 return EXIT_SUCCESS; 69 return EXIT_SUCCESS;
@@ -68,15 +72,14 @@ int losetup_main(int argc UNUSED_PARAM, char **argv)
68 /* -a */ 72 /* -a */
69 if (opt == OPT_a) { 73 if (opt == OPT_a) {
70 int n; 74 int n;
71 for (n = 0; n < 10; n++) { 75 for (n = 0; n < MAX_LOOP_NUM; n++) {
72 char *s; 76 char *s;
73 77
74 sprintf(dev, LOOP_FORMAT, n); 78 sprintf(dev, LOOP_FORMAT, n);
75 s = query_loop(dev); 79 s = query_loop(dev);
76 if (s) { 80 if (s) {
77 printf("%s: %s\n", dev, s); 81 printf("%s: %s\n", dev, s);
78 if (ENABLE_FEATURE_CLEAN_UP) 82 free(s);
79 free(s);
80 } 83 }
81 } 84 }
82 return EXIT_SUCCESS; 85 return EXIT_SUCCESS;
@@ -88,11 +91,13 @@ int losetup_main(int argc UNUSED_PARAM, char **argv)
88 int n = 0; 91 int n = 0;
89 92
90 do { 93 do {
91 sprintf(dev, LOOP_FORMAT, n); 94 if (n > MAX_LOOP_NUM)
95 bb_error_msg_and_die("no free loop devices");
96 sprintf(dev, LOOP_FORMAT, n++);
92 s = query_loop(dev); 97 s = query_loop(dev);
93 if (s && ENABLE_FEATURE_CLEAN_UP) 98 free(s);
94 free(s);
95 } while (s); 99 } while (s);
100 /* now: dev is next free "/dev/loopN" */
96 if ((opt == OPT_f) && !argv[0]) { 101 if ((opt == OPT_f) && !argv[0]) {
97 puts(dev); 102 puts(dev);
98 return EXIT_SUCCESS; 103 return EXIT_SUCCESS;
@@ -104,18 +109,18 @@ int losetup_main(int argc UNUSED_PARAM, char **argv)
104 unsigned long long offset = 0; 109 unsigned long long offset = 0;
105 char *d = dev; 110 char *d = dev;
106 111
107 if (opt == OPT_o) 112 if (opt & OPT_o)
108 offset = xatoull(opt_o); 113 offset = xatoull(opt_o);
109 if (opt != OPT_f) 114 if (!(opt & OPT_f))
110 d = *(argv++); 115 d = *argv++;
111 116
112 if (argv[0]) { 117 if (argv[0]) {
113 if (set_loop(&d, argv[0], offset, (opt / OPT_r)) < 0) 118 if (set_loop(&d, argv[0], offset, (opt & OPT_r)) < 0)
114 bb_simple_perror_msg_and_die(argv[0]); 119 bb_simple_perror_msg_and_die(argv[0]);
115 return EXIT_SUCCESS; 120 return EXIT_SUCCESS;
116 } 121 }
117 } 122 }
118 123
119 bb_show_usage(); 124 bb_show_usage(); /* does not return */
120 return EXIT_FAILURE; 125 /*return EXIT_FAILURE;*/
121} 126}