-/* These functions are here because the getmntent functions do not appear
- * to be re-entrant, which leads to all sorts of problems when we try to
- * use them recursively - randolph
- */
-void mtab_read(void)
-{
- struct _mtab_entry_t *entry = NULL;
- struct mntent *e;
- FILE *fp;
-
- if (mtab_cache != NULL) return;
-
- if ((fp = setmntent(mtab_file, "r")) == NULL) {
- fprintf(stderr, "Cannot open %s\n", mtab_file);
- return;
- }
- while ((e = getmntent(fp))) {
- entry = malloc(sizeof(struct _mtab_entry_t));
- entry->device = strdup(e->mnt_fsname);
- entry->mountpt = strdup(e->mnt_dir);
- entry->next = mtab_cache;
- mtab_cache = entry;
- }
- endmntent(fp);
-}
+ // 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);
+ }