aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/loop.c49
1 files changed, 33 insertions, 16 deletions
diff --git a/libbb/loop.c b/libbb/loop.c
index d30b378d7..f0d4296ae 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -78,22 +78,24 @@ int FAST_FUNC del_loop(const char *device)
78 return rc; 78 return rc;
79} 79}
80 80
81/* Returns 0 if mounted RW, 1 if mounted read-only, <0 for error. 81/* Returns opened fd to the loop device, <0 on error.
82 *device is loop device to use, or if *device==NULL finds a loop device to 82 * *device is loop device to use, or if *device==NULL finds a loop device to
83 mount it on and sets *device to a strdup of that loop device name. This 83 * mount it on and sets *device to a strdup of that loop device name. This
84 search will re-use an existing loop device already bound to that 84 * search will re-use an existing loop device already bound to that
85 file/offset if it finds one. 85 * file/offset if it finds one.
86 */ 86 */
87int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, int ro) 87int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, unsigned flags)
88{ 88{
89 char dev[LOOP_NAMESIZE]; 89 char dev[LOOP_NAMESIZE];
90 char *try; 90 char *try;
91 bb_loop_info loopinfo; 91 bb_loop_info loopinfo;
92 struct stat statbuf; 92 struct stat statbuf;
93 int i, dfd, ffd, mode, rc = -1; 93 int i, dfd, ffd, mode, rc;
94
95 rc = dfd = -1;
94 96
95 /* Open the file. Barf if this doesn't work. */ 97 /* Open the file. Barf if this doesn't work. */
96 mode = ro ? O_RDONLY : O_RDWR; 98 mode = (flags & BB_LO_FLAGS_READ_ONLY) ? O_RDONLY : O_RDWR;
97 open_ffd: 99 open_ffd:
98 ffd = open(file, mode); 100 ffd = open(file, mode);
99 if (ffd < 0) { 101 if (ffd < 0) {
@@ -144,20 +146,35 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
144 146
145 /* If device is free, claim it. */ 147 /* If device is free, claim it. */
146 if (rc && errno == ENXIO) { 148 if (rc && errno == ENXIO) {
147 memset(&loopinfo, 0, sizeof(loopinfo));
148 safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
149 loopinfo.lo_offset = offset;
150 /* Associate free loop device with file. */ 149 /* Associate free loop device with file. */
151 if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) { 150 if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) {
152 if (ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo) == 0) 151 memset(&loopinfo, 0, sizeof(loopinfo));
153 rc = 0; 152 safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
154 else 153 loopinfo.lo_offset = offset;
154 /*
155 * Used by mount to set LO_FLAGS_AUTOCLEAR.
156 * LO_FLAGS_READ_ONLY is not set because RO is controlled by open type of the file.
157 * Note that closing LO_FLAGS_AUTOCLEARed dfd before mount
158 * is wrong (would free the loop device!)
159 */
160 loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
161 rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
162 if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
163 /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
164 /* (this code path is not tested) */
165 loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
166 rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
167 }
168 if (rc != 0) {
155 ioctl(dfd, LOOP_CLR_FD, 0); 169 ioctl(dfd, LOOP_CLR_FD, 0);
170 }
156 } 171 }
157 } else { 172 } else {
158 rc = -1; 173 rc = -1;
159 } 174 }
160 close(dfd); 175 if (rc != 0) {
176 close(dfd);
177 }
161 try_again: 178 try_again:
162 if (*device) break; 179 if (*device) break;
163 } 180 }
@@ -165,7 +182,7 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
165 if (rc == 0) { 182 if (rc == 0) {
166 if (!*device) 183 if (!*device)
167 *device = xstrdup(dev); 184 *device = xstrdup(dev);
168 return (mode == O_RDONLY); /* 1:ro, 0:rw */ 185 return dfd;
169 } 186 }
170 return rc; 187 return rc;
171} 188}