From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Moritz Sanft <58110325+msanft@users.noreply.github.com>
Date: Fri, 20 Dec 2024 16:34:50 +0100
Subject: [PATCH] ldcache: don't use ldcache

This patch hinders libnvidia-container from using the loader cache, which doesn't get used on NixOS.
---
 src/ldcache.c     | 46 +++++++++++++++++-----------------------------
 src/ldcache.h     |  2 +-
 src/nvc_info.c    |  8 ++------
 src/nvc_ldcache.c |  4 ++--
 4 files changed, 22 insertions(+), 38 deletions(-)

diff --git a/src/ldcache.c b/src/ldcache.c
index 38bab0553208f66b2866ccea6cdb0faca4357f19..1c4acd52b622be4ca6accdc80da5a6fcf9ae67dd 100644
--- a/src/ldcache.c
+++ b/src/ldcache.c
@@ -108,40 +108,28 @@ ldcache_close(struct ldcache *ctx)
 
 int
 ldcache_resolve(struct ldcache *ctx, uint32_t arch, const char *root, const char * const libs[],
-    char *paths[], size_t size, ldcache_select_fn select, void *select_ctx)
+    char *paths[], size_t size, const char* version)
 {
         char path[PATH_MAX];
-        struct header_libc6 *h;
-        int override;
+        char dir[PATH_MAX];
+        char lib[PATH_MAX];
 
-        h = (struct header_libc6 *)ctx->ptr;
         memset(paths, 0, size * sizeof(*paths));
 
-        for (uint32_t i = 0; i < h->nlibs; ++i) {
-                int32_t flags = h->libs[i].flags;
-                char *key = (char *)ctx->ptr + h->libs[i].key;
-                char *value = (char *)ctx->ptr + h->libs[i].value;
-
-                if (!(flags & LD_ELF) || (flags & LD_ARCH_MASK) != arch)
-                        continue;
-
-                for (size_t j = 0; j < size; ++j) {
-                        if (!str_has_prefix(key, libs[j]))
-                                continue;
-                        if (path_resolve(ctx->err, path, root, value) < 0)
-                                return (-1);
-                        if (paths[j] != NULL && str_equal(paths[j], path))
-                                continue;
-                        if ((override = select(ctx->err, select_ctx, root, paths[j], path)) < 0)
-                                return (-1);
-                        if (override) {
-                                free(paths[j]);
-                                paths[j] = xstrdup(ctx->err, path);
-                                if (paths[j] == NULL)
-                                        return (-1);
-                        }
-                        break;
-                }
+        for (size_t j = 0; j < size; ++j) {
+                snprintf(dir, 100, "@driverLink@/lib");
+
+                if (!strncmp(libs[j], "libvdpau_nvidia.so", 100))
+                        strcat(dir, "/vdpau");
+                snprintf(lib, 100, "%s/%s.%s", dir, libs[j], version);
+                if (path_resolve_full(ctx->err, path, "/", lib) < 0)
+                        return (-1);
+                if (!file_exists(ctx->err, path))
+                         continue;
+
+                paths[j] = xstrdup(ctx->err, path);
+                if (paths[j] == NULL)
+                        return (-1);
         }
         return (0);
 }
diff --git a/src/ldcache.h b/src/ldcache.h
index 33d78dd7e21f65eb696535c115bbd2839a6c67ca..2b087dbca1a6a2946cd495e676a61e956212e3dc 100644
--- a/src/ldcache.h
+++ b/src/ldcache.h
@@ -50,6 +50,6 @@ void ldcache_init(struct ldcache *, struct error *, const char *);
 int  ldcache_open(struct ldcache *);
 int  ldcache_close(struct ldcache *);
 int  ldcache_resolve(struct ldcache *, uint32_t, const char *, const char * const [],
-    char *[], size_t, ldcache_select_fn, void *);
+    char *[], size_t, const char*);
 
 #endif /* HEADER_LDCACHE_H */
diff --git a/src/nvc_info.c b/src/nvc_info.c
index bcc887b2345bd42a098f9b85d9c66fae2775f736..5eaef61ada5e955ab11c6a4eb8429c50468e3370 100644
--- a/src/nvc_info.c
+++ b/src/nvc_info.c
@@ -217,15 +217,13 @@ find_library_paths(struct error *err, struct dxcore_context *dxcore, struct nvc_
         if (path_resolve_full(err, path, root, ldcache) < 0)
                 return (-1);
         ldcache_init(&ld, err, path);
-        if (ldcache_open(&ld) < 0)
-                return (-1);
 
         info->nlibs = size;
         info->libs = array_new(err, size);
         if (info->libs == NULL)
                 goto fail;
         if (ldcache_resolve(&ld, LIB_ARCH, root, libs,
-            info->libs, info->nlibs, select_libraries_fn, info) < 0)
+            info->libs, info->nlibs, info->nvrm_version) < 0)
                 goto fail;
 
         info->nlibs32 = size;
@@ -233,13 +231,11 @@ find_library_paths(struct error *err, struct dxcore_context *dxcore, struct nvc_
         if (info->libs32 == NULL)
                 goto fail;
         if (ldcache_resolve(&ld, LIB32_ARCH, root, libs,
-            info->libs32, info->nlibs32, select_libraries_fn, info) < 0)
+            info->libs32, info->nlibs32, info->nvrm_version) < 0)
                 goto fail;
         rv = 0;
 
  fail:
-        if (ldcache_close(&ld) < 0)
-                return (-1);
         return (rv);
 }
 
diff --git a/src/nvc_ldcache.c b/src/nvc_ldcache.c
index 0535090dafbae5a00acb707bbbb5a35dbcea4a7a..5de429f4c2ea62775403a5fc1ed0f23a6c88655c 100644
--- a/src/nvc_ldcache.c
+++ b/src/nvc_ldcache.c
@@ -482,8 +482,8 @@ nvc_ldcache_update(struct nvc_context *ctx, const struct nvc_container *cnt)
          * See https://github.com/NVIDIA/libnvidia-container/issues/316 for an
          * in-depth investigation.
          */
-        char *argv_default[] = {cnt->cfg.ldconfig, "-f", "/etc/ld.so.conf", "-C", "/etc/ld.so.cache", cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL};
-        char *argv_with_compat_dir[] = {cnt->cfg.ldconfig, "-f", "/etc/ld.so.conf", "-C", "/etc/ld.so.cache", cnt->cuda_compat_dir, cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL};
+        char *argv_default[] = {cnt->cfg.ldconfig, "-f", "/tmp/ld.so.conf.nvidia-host", "-C", "/tmp/ld.so.cache.nvidia-host", cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL};
+        char *argv_with_compat_dir[] = {cnt->cfg.ldconfig, "-f", "/tmp/ld.so.conf.nvidia-host", "-C", "/tmp/ld.so.cache.nvidia-host", cnt->cuda_compat_dir, cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL};
         if ((cnt->flags & OPT_CUDA_COMPAT_MODE_LDCONFIG) && (cnt->cuda_compat_dir != NULL)) {
                 /*
                  * We include the cuda_compat_dir directory on the ldconfig
