int
set_loop(const char *device, const char *file, unsigned long long offset,
- const char *encryption, int pfd, int *loopro) {
+ const char *encryption, int pfd, int *options) {
struct loop_info64 loopinfo64;
int fd, ffd, mode, i;
char *pass;
}
}
- mode = (*loopro ? O_RDONLY : O_RDWR);
+ mode = (*options & SETLOOP_RDONLY) ? O_RDONLY : O_RDWR;
if ((ffd = open(file, mode)) < 0) {
- if (!*loopro && errno == EROFS)
+ if (!(*options & SETLOOP_RDONLY) && errno == EROFS)
ffd = open(file, mode = O_RDONLY);
if (ffd < 0) {
perror(file);
return 1;
}
+ *options |= SETLOOP_RDONLY;
}
if ((fd = open(device, mode)) < 0) {
perror (device);
close(ffd);
return 1;
}
- *loopro = (mode == O_RDONLY);
-
memset(&loopinfo64, 0, sizeof(loopinfo64));
if (!(filename = canonicalize(file)))
}
close (ffd);
+ if (*options & SETLOOP_AUTOCLEAR)
+ loopinfo64.lo_flags = LO_FLAGS_AUTOCLEAR;
+
i = ioctl(fd, LOOP_SET_STATUS64, &loopinfo64);
if (i) {
struct loop_info loopinfo;
i = loop_info64_to_old(&loopinfo64, &loopinfo);
if (i) {
errno = errsv;
+ *options &= ~SETLOOP_AUTOCLEAR;
perror("ioctl: LOOP_SET_STATUS64");
} else {
i = ioctl(fd, LOOP_SET_STATUS, &loopinfo);
if (i)
perror("ioctl: LOOP_SET_STATUS");
+ else if (*options & SETLOOP_AUTOCLEAR)
+ {
+ i = ioctl(fd, LOOP_GET_STATUS, &loopinfo);
+ if (i || !(loopinfo.lo_flags & LO_FLAGS_AUTOCLEAR))
+ *options &= ~SETLOOP_AUTOCLEAR;
+ }
}
memset(&loopinfo, 0, sizeof(loopinfo));
}
+ else if (*options & SETLOOP_AUTOCLEAR)
+ {
+ i = ioctl(fd, LOOP_GET_STATUS64, &loopinfo64);
+ if (i || !(loopinfo64.lo_flags & LO_FLAGS_AUTOCLEAR))
+ *options &= ~SETLOOP_AUTOCLEAR;
+ }
memset(&loopinfo64, 0, sizeof(loopinfo64));
+
if (i) {
ioctl (fd, LOOP_CLR_FD, 0);
close (fd);
free(filename);
return 1;
}
- close (fd);
+
+ /*
+ * HACK: here we're leeking a file descriptor,
+ * but mount is a short-lived process anyway.
+ */
+ if (!(*options & SETLOOP_AUTOCLEAR))
+ close (fd);
if (verbose > 1)
printf(_("set_loop(%s,%s,%llu): success\n"),
if (verbose)
printf(_("mount: skipping the setup of a loop device\n"));
} else {
- int loopro = (*flags & MS_RDONLY);
+ int loop_opts = SETLOOP_AUTOCLEAR; /* always attempt autoclear */
int res;
+ if (*flags & MS_RDONLY)
+ loop_opts |= SETLOOP_RDONLY;
+
offset = opt_offset ? strtoull(opt_offset, NULL, 0) : 0;
if (is_mounted_same_loopfile(node, *loopfile, offset)) {
printf(_("mount: going to use the loop device %s\n"), *loopdev);
if ((res = set_loop(*loopdev, *loopfile, offset,
- opt_encryption, pfd, &loopro))) {
+ opt_encryption, pfd, &loop_opts))) {
if (res == 2) {
/* loop dev has been grabbed by some other process,
try again, if not given explicitly */
if (verbose > 1)
printf(_("mount: setup loop device successfully\n"));
*spec = *loopdev;
- if (loopro)
- *flags |= MS_RDONLY;
+
+ if (loop_opts & SETLOOP_RDONLY)
+ *flags |= MS_RDONLY;
+
+ if (loop_opts & SETLOOP_AUTOCLEAR)
+ /* Prevent recording loop dev in mtab for cleanup on umount */
+ *loop = 0;
}
}