]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cryptsetup: port from setmntent() to libmount parser
authorMike Yuan <me@yhndnzj.com>
Sat, 6 Sep 2025 15:23:27 +0000 (17:23 +0200)
committerMike Yuan <me@yhndnzj.com>
Thu, 18 Sep 2025 18:25:14 +0000 (20:25 +0200)
src/cryptsetup/cryptsetup.c
src/cryptsetup/meson.build

index 357ee786954cef07eab31d00ec52c9a71be2ece7..c7897a2f208bdd54585f5923ec283ea241b2fc10 100644 (file)
@@ -1,7 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <getopt.h>
-#include <mntent.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -33,6 +32,7 @@
 #include "hexdecoct.h"
 #include "json-util.h"
 #include "libfido2-util.h"
+#include "libmount-util.h"
 #include "log.h"
 #include "main-func.h"
 #include "memory-util.h"
@@ -732,26 +732,37 @@ static char* disk_description(const char *path) {
         return NULL;
 }
 
-static char *disk_mount_point(const char *label) {
+static char* disk_mount_point(const char *label) {
+        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
+        _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
         _cleanup_free_ char *device = NULL;
-        _cleanup_endmntent_ FILE *f = NULL;
-        struct mntent *m;
+        int r;
 
         /* Yeah, we don't support native systemd unit files here for now */
 
+        assert(label);
+
         device = strjoin("/dev/mapper/", label);
         if (!device)
                 return NULL;
 
-        f = setmntent(fstab_path(), "re");
-        if (!f)
+        r = libmount_parse_fstab(&table, &iter);
+        if (r < 0)
                 return NULL;
 
-        while ((m = getmntent(f)))
-                if (path_equal(m->mnt_fsname, device))
-                        return strdup(m->mnt_dir);
+        for (;;) {
+                struct libmnt_fs *fs;
 
-        return NULL;
+                r = mnt_table_next_fs(table, iter, &fs);
+                if (r != 0)
+                        return NULL;
+
+                if (path_equal(mnt_fs_get_source(fs), device)) {
+                        const char *target = mnt_fs_get_target(fs);
+                        if (target)
+                                return strdup(target);
+                }
+        }
 }
 
 static char *friendly_disk_name(const char *src, const char *vol) {
index c2f4dc906534479eac044df06ac9736c81c3d43b..cb47fd94172866d02abb85bedbbe9c811a4034d1 100644 (file)
@@ -19,6 +19,7 @@ executables += [
                 'sources' : systemd_cryptsetup_sources,
                 'dependencies' : [
                         libcryptsetup,
+                        libmount,
                         libopenssl,
                         libp11kit_cflags,
                 ],