+ // Loop through everything we're supposed to umount, and do so.
+ for (;;) {
+ int curstat;
+ char *zapit = *argv;
+
+ // Do we already know what to umount this time through the loop?
+ if (m)
+ safe_strncpy(path, m->dir, PATH_MAX);
+ // For umount -a, end of mtab means time to exit.
+ else if (opt & OPT_ALL)
+ break;
+ // Use command line argument (and look it up in mtab list)
+ else {
+ if (!zapit)
+ break;
+ argv++;
+ realpath(zapit, path);
+ for (m = mtl; m; m = m->next)
+ if (!strcmp(path, m->dir) || !strcmp(path, m->device))
+ break;
+ }
+ // If we couldn't find this sucker in /etc/mtab, punt by passing our
+ // command line argument straight to the umount syscall. Otherwise,
+ // umount the directory even if we were given the block device.
+ if (m) zapit = m->dir;
+
+ // Let's ask the thing nicely to unmount.
+ curstat = umount(zapit);
+
+ // Force the unmount, if necessary.
+ if (curstat && doForce)
+ curstat = umount2(zapit, doForce);
+
+ // If still can't umount, maybe remount read-only?
+ if (curstat) {
+ if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
+ // Note! Even if we succeed here, later we should not
+ // free loop device or erase mtab entry!
+ const char *msg = "%s busy - remounted read-only";
+ curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
+ if (curstat) {
+ msg = "can't remount %s read-only";
+ status = EXIT_FAILURE;
+ }
+ bb_error_msg(msg, m->device);
+ } else {
+ status = EXIT_FAILURE;
+ bb_perror_msg("can't %sumount %s", (doForce ? "forcibly " : ""), zapit);
+ }
+ } else {
+ // De-allocate the loop device. This ioctl should be ignored on
+ // any non-loop block devices.
+ if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m)
+ del_loop(m->device);
+ if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
+ erase_mtab(m->dir);
+ }
+
+ // Find next matching mtab entry for -a or umount /dev
+ // Note this means that "umount /dev/blah" will unmount all instances
+ // of /dev/blah, not just the most recent.
+ if (m) while ((m = m->next) != NULL)
+ if ((opt & OPT_ALL) || !strcmp(path, m->device))
+ break;
+ }
+
+ // Free mtab list if necessary
+ if (ENABLE_FEATURE_CLEAN_UP) {
+ while (mtl) {
+ m = mtl->next;
+ free(mtl->device);
+ free(mtl->dir);
+ free(mtl);
+ mtl = m;
+ }
+ free(path);
+ }
+
+ return status;
+}