From: Lennart Poettering Date: Tue, 16 May 2023 13:52:33 +0000 (+0200) Subject: mount-util: keep fd to /proc/self/mountinfo continously open in umount_recursive() X-Git-Tag: v254-rc1~441^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=890c14e343e028d55d96716baae14aaf1e74a467;p=thirdparty%2Fsystemd.git mount-util: keep fd to /proc/self/mountinfo continously open in umount_recursive() That way, if we end up unmounting /proc/ in our loop we can still operate correctly, since we don't have to go through /proc/ again to open the mount table again. --- diff --git a/src/shared/mount-util.c b/src/shared/mount-util.c index b41672eb922..b7f3616a1db 100644 --- a/src/shared/mount-util.c +++ b/src/shared/mount-util.c @@ -44,19 +44,22 @@ #include "user-util.h" int umount_recursive(const char *prefix, int flags) { + _cleanup_fclose_ FILE *f = NULL; int n = 0, r; - bool again; /* Try to umount everything recursively below a directory. Also, take care of stacked mounts, and * keep unmounting them until they are gone. */ - do { + f = fopen("/proc/self/mountinfo", "re"); /* Pin the file, in case we unmount /proc/ as part of the logic here */ + if (!f) + return log_debug_errno(errno, "Failed to open /proc/self/mountinfo: %m"); + + for (;;) { _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; + bool again = false; - again = false; - - r = libmount_parse("/proc/self/mountinfo", NULL, &table, &iter); + r = libmount_parse("/proc/self/mountinfo", f, &table, &iter); if (r < 0) return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m"); @@ -89,7 +92,12 @@ int umount_recursive(const char *prefix, int flags) { break; } - } while (again); + + if (!again) + break; + + rewind(f); + } return n; }