umount -a should unmount in the reverse order

This commit is contained in:
sin 2014-03-15 18:44:16 +00:00
parent 98f7fcad94
commit 6ef8c718e1
1 changed files with 14 additions and 4 deletions

View File

@ -61,6 +61,8 @@ umountall(int flags)
FILE *fp; FILE *fp;
struct mntent *me; struct mntent *me;
int ret; int ret;
char **mntdirs = NULL;
int len = 0;
fp = setmntent("/etc/mtab", "r"); fp = setmntent("/etc/mtab", "r");
if (!fp) if (!fp)
@ -68,11 +70,19 @@ umountall(int flags)
while ((me = getmntent(fp))) { while ((me = getmntent(fp))) {
if (strcmp(me->mnt_type, "proc") == 0) if (strcmp(me->mnt_type, "proc") == 0)
continue; continue;
if (umount2(me->mnt_dir, flags) < 0) { mntdirs = realloc(mntdirs, ++len * sizeof(*mntdirs));
weprintf("umount2 %s:", me->mnt_dir); if (!mntdirs)
ret = EXIT_FAILURE; eprintf("realloc:");
} mntdirs[len - 1] = strdup(me->mnt_dir);
} }
endmntent(fp); endmntent(fp);
while (--len >= 0) {
if (umount2(mntdirs[len], flags) < 0) {
weprintf("umount2 %s:", mntdirs[len]);
ret = EXIT_FAILURE;
}
free(mntdirs[len]);
}
free(mntdirs);
return ret; return ret;
} }