]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
remount-fs: port to libmount parser
authorMike Yuan <me@yhndnzj.com>
Sun, 14 Sep 2025 18:58:11 +0000 (20:58 +0200)
committerMike Yuan <me@yhndnzj.com>
Thu, 18 Sep 2025 18:25:15 +0000 (20:25 +0200)
src/remount-fs/meson.build
src/remount-fs/remount-fs.c

index 8761d25418f0315bee7edc2a60ff62b238e78e29..9ff7c9b8e48e4da5faa5b083243930be814ffa24 100644 (file)
@@ -4,5 +4,6 @@ executables += [
         libexec_template + {
                 'name' : 'systemd-remount-fs',
                 'sources' : files('remount-fs.c'),
+                'dependencies' : libmount,
         },
 ]
index 8dd65637b25fb501dd783b87df060d297e7ea15d..ceaa2676ca6e43ae7fb3c55a254ee2bff572f626 100644 (file)
@@ -1,6 +1,5 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
-#include <mntent.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <unistd.h>
@@ -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;
         }