From 595e2b4e810c8263ecb4e57991c7f940afe3c8f4 Mon Sep 17 00:00:00 2001 From: Robert Andersson Date: Wed, 9 Sep 2020 14:38:14 +0200 Subject: [PATCH 1/2] Ensure all filesystems listed in /proc/mounts are unmounted The iterator function getmntent() is not stable. It may skip entries if the contents of the iterated file changes, which is the case with /proc/mounts when filesystems are unmounted. As a result, some filesystems were never unmounted. To fix this, iteratation is now restarted after every sucessful unmount. Signed-off-by: Robert Andersson Signed-off-by: Ming Liu --- src/mount.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/mount.c b/src/mount.c index 19fd9514..b8f5bd44 100644 --- a/src/mount.c +++ b/src/mount.c @@ -59,46 +59,54 @@ static int is_protected(char *dir) return 0; } -static struct mntent *iterator(char *fstab) +static void iterator_end(FILE **fp) +{ + endmntent(*fp); + *fp = NULL; +} + +static struct mntent *iterator(char *fstab, FILE **fp) { - static FILE *fp = NULL; static struct mntent *mnt; - if (!fp && fstab) { - fp = setmntent(fstab, "r"); - if (!fp) + if (!*fp && fstab) { + *fp = setmntent(fstab, "r"); + if (!*fp) return NULL; } - while ((mnt = getmntent(fp))) { + while ((mnt = getmntent(*fp))) { if (is_protected(mnt->mnt_dir)) continue; return mnt; } - endmntent(fp); - fp = NULL; - + iterator_end(fp); return NULL; } void unmount_tmpfs(void) { struct mntent *mnt; + FILE *fp = NULL; - while ((mnt = iterator("/proc/mounts"))) { - if (!strcmp("tmpfs", mnt->mnt_fsname)) - umount(mnt->mnt_dir); + while ((mnt = iterator("/proc/mounts", &fp))) { + if (!strcmp("tmpfs", mnt->mnt_fsname) && !umount(mnt->mnt_dir)) + iterator_end(&fp); // Restart iteration } } void unmount_regular(void) { struct mntent *mnt; + FILE *fp = NULL; - while ((mnt = iterator("/proc/mounts"))) { - umount(mnt->mnt_dir); + while ((mnt = iterator("/proc/mounts", &fp))) { + if (!umount(mnt->mnt_dir)) { + print(0, "Unmounted %s", mnt->mnt_dir); + iterator_end(&fp); // Restart iteration + } } } From 9b9158a765acb84519a43b946875b6cd7e8daefa Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 9 Sep 2021 08:51:26 +0200 Subject: [PATCH 2/2] Follow-up, coding style and drop extra print() Signed-off-by: Joachim Wiberg --- src/mount.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/mount.c b/src/mount.c index b8f5bd44..75e5a204 100644 --- a/src/mount.c +++ b/src/mount.c @@ -93,7 +93,7 @@ void unmount_tmpfs(void) while ((mnt = iterator("/proc/mounts", &fp))) { if (!strcmp("tmpfs", mnt->mnt_fsname) && !umount(mnt->mnt_dir)) - iterator_end(&fp); // Restart iteration + iterator_end(&fp); /* Restart iteration */ } } @@ -103,10 +103,8 @@ void unmount_regular(void) FILE *fp = NULL; while ((mnt = iterator("/proc/mounts", &fp))) { - if (!umount(mnt->mnt_dir)) { - print(0, "Unmounted %s", mnt->mnt_dir); - iterator_end(&fp); // Restart iteration - } + if (!umount(mnt->mnt_dir)) + iterator_end(&fp); /* Restart iteration */ } }