From: Mike Yuan Date: Sun, 14 Sep 2025 18:58:11 +0000 (+0200) Subject: remount-fs: port to libmount parser X-Git-Tag: v259-rc1~501^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9d05015bb9e5e3d756d5abd0c95a657d487a0d62;p=thirdparty%2Fsystemd.git remount-fs: port to libmount parser --- diff --git a/src/remount-fs/meson.build b/src/remount-fs/meson.build index 8761d25418f..9ff7c9b8e48 100644 --- a/src/remount-fs/meson.build +++ b/src/remount-fs/meson.build @@ -4,5 +4,6 @@ executables += [ libexec_template + { 'name' : 'systemd-remount-fs', 'sources' : files('remount-fs.c'), + 'dependencies' : libmount, }, ] diff --git a/src/remount-fs/remount-fs.c b/src/remount-fs/remount-fs.c index 8dd65637b25..ceaa2676ca6 100644 --- a/src/remount-fs/remount-fs.c +++ b/src/remount-fs/remount-fs.c @@ -1,6 +1,5 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ -#include #include #include #include @@ -11,6 +10,7 @@ #include "format-util.h" #include "fstab-util.h" #include "hashmap.h" +#include "libmount-util.h" #include "log.h" #include "main-func.h" #include "mount-setup.h" @@ -70,10 +70,10 @@ static int do_remount(const char *path, bool force_rw, Hashmap **pids) { } static int remount_by_fstab(Hashmap **ret_pids) { + _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; + _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; _cleanup_hashmap_free_ Hashmap *pids = NULL; - _cleanup_endmntent_ FILE *f = NULL; bool has_root = false; - struct mntent* me; int r; assert(ret_pids); @@ -81,24 +81,33 @@ static int remount_by_fstab(Hashmap **ret_pids) { if (!fstab_enabled()) return 0; - f = setmntent(fstab_path(), "re"); - if (!f) { - if (errno != ENOENT) - return log_error_errno(errno, "Failed to open %s: %m", fstab_path()); - + r = libmount_parse_fstab(&table, &iter); + if (r == -ENOENT) return 0; - } + if (r < 0) + return log_error_errno(r, "Failed to parse fstab: %m"); + + for (;;) { + struct libmnt_fs *fs; - while ((me = getmntent(f))) { - /* Remount the root fs, /usr, and all API VFSs */ - if (!mount_point_is_api(me->mnt_dir) && - !PATH_IN_SET(me->mnt_dir, "/", "/usr")) + r = mnt_table_next_fs(table, iter, &fs); + if (r < 0) + return log_error_errno(r, "Failed to get next entry from fstab: %m"); + if (r > 0) /* EOF */ + break; + + const char *target = mnt_fs_get_target(fs); + if (!target) continue; - if (path_equal(me->mnt_dir, "/")) + /* Remount the root fs, /usr/, and all API VFSs */ + + if (path_equal(target, "/")) has_root = true; + else if (!path_equal(target, "/usr") && !mount_point_is_api(target)) + continue; - r = do_remount(me->mnt_dir, /* force_rw = */ false, &pids); + r = do_remount(target, /* force_rw = */ false, &pids); if (r < 0) return r; }