/*
 * Copyright (C) 2015 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "mount-support-nvidia.h"
#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
/* POSIX version of basename() and dirname() */
#include <libgen.h>

#include "../libsnap-confine-private/classic.h"
#include "../libsnap-confine-private/cleanup-funcs.h"
#include "../libsnap-confine-private/mount-opt.h"
#include "../libsnap-confine-private/string-utils.h"
#include "../libsnap-confine-private/utils.h"
#include "mount-support.h"

#define SC_LIBGL_DIR SC_EXTRA_LIB_DIR "/gl"
#define SC_LIBGL32_DIR SC_EXTRA_LIB_DIR "/gl32"
#define SC_VULKAN_DIR SC_EXTRA_LIB_DIR "/vulkan"
#define SC_GLVND_DIR SC_EXTRA_LIB_DIR "/glvnd"
#define SC_VULKAN_ICD_DIR SC_VULKAN_DIR "/icd.d"
#define SC_GLVND_EGL_DIR SC_GLVND_DIR "/egl_vendor.d"

#define SC_VULKAN_SOURCE_DIR "/usr/share/vulkan"
#define SC_EGL_VENDOR_SOURCE_DIR "/usr/share/glvnd"

#define SC_SNAPD_EXPORT "/var/lib/snapd/export"
#define SC_SNAP_DIR "/snap"
#define SC_LIBGPU_DIR SC_EXTRA_LIB_DIR "/system/gpu"

// Globs for files to copy when exporting configuration from snaps
#define SC_ETC_GLVND_EGL_SOURCE_DIR "/etc/glvnd/egl_vendor.d"
#define SC_SNAP_EGL_VENDOR_GLOB SC_ETC_GLVND_EGL_SOURCE_DIR "/*_snap_*_*_*.json"
#define SC_ETC_VULKAN_ICD_SOURCE_DIR "/etc/vulkan/icd.d"
#define SC_SNAP_VULKAN_ICD_GLOB SC_ETC_VULKAN_ICD_SOURCE_DIR "/snap_*_*_*.json"

// Location for NVIDIA vulkan files (including _wayland)
static const char *vulkan_globs[] = {
    "icd.d/*nvidia*.json",
};

static const size_t vulkan_globs_len = SC_ARRAY_SIZE(vulkan_globs);

// Location of EGL vendor files
static const char *egl_vendor_globs[] = {
    "egl_vendor.d/*nvidia*.json",
};

static const size_t egl_vendor_globs_len = SC_ARRAY_SIZE(egl_vendor_globs);

static const char *nvidia_driver_version_file(void) {
    const char *path = getenv("SNAPD_TESTING_NVIDIA_DRIVER_VERSION_FILE");
    if (path != NULL) {
        return path;
    }
    return "/sys/module/nvidia/version";
}

#if defined(NVIDIA_BIARCH) || defined(NVIDIA_MULTIARCH)

// List of globs that describe nvidia userspace libraries.
// This list was compiled from the following packages.
//
// https://www.archlinux.org/packages/extra/x86_64/nvidia-304xx-libgl/files/
// https://www.archlinux.org/packages/extra/x86_64/nvidia-304xx-utils/files/
// https://www.archlinux.org/packages/extra/x86_64/nvidia-340xx-libgl/files/
// https://www.archlinux.org/packages/extra/x86_64/nvidia-340xx-utils/files/
// https://www.archlinux.org/packages/extra/x86_64/nvidia-libgl/files/
// https://www.archlinux.org/packages/extra/x86_64/nvidia-utils/files/
//
// FIXME: this doesn't yet work with libGLX and libglvnd redirector
// FIXME: this still doesn't work with the 361 driver
static const char *nvidia_globs[] = {
    "gbm/nvidia-drm_gbm.so*",
    "libnvidia-allocator.so*",
    "libnvidia-api.so*",
    "libnvidia-cbl.so*",
    "libnvidia-cfg.so*",
    "libnvidia-compiler-next.so*",
    "libnvidia-egl-gbm.so*",
    "libnvidia-ngx.so*",
    "libnvidia-nscq.so*",
    "libnvidia-nvvm.so*",
    "libnvidia-pkcs11-openssl3.so*",
    "libnvidia-pkcs11.so*",
    "libnvidia-vulkan-producer.so*",
    "libnvidia-vksc-core.so.*",

    "libEGL_nvidia.so*",
    "libGLESv1_CM_nvidia.so*",
    "libGLESv2_nvidia.so*",
    "libGLX_nvidia.so*",
    "libXvMCNVIDIA.so*",
    "libXvMCNVIDIA_dynamic.so*",
    "libnvidia-cfg.so*",
    "libnvidia-compiler.so*",
    "libnvidia-eglcore.so*",
    "libnvidia-egl-wayland*",
    "libnvidia-encode.so*",
    "libnvidia-fatbinaryloader.so*",
    "libnvidia-fbc.so*",
    "libnvidia-glcore.so*",
    "libnvidia-glsi.so*",
    "libnvidia-glvkspirv.so*",
    "libnvidia-gpucomp.so*",
    "libnvidia-ifr.so*",
    "libnvidia-ml.so*",
    "libnvidia-opencl.so*",
    "libnvidia-opticalflow.so*",
    "libnvidia-ptxjitcompiler.so*",
    "libnvidia-rtcore.so*",
    "libnvidia-tls.so*",
    "libnvoptix.so*",
    "tls/libnvidia-tls.so*",
    "vdpau/libvdpau_nvidia.so*",

    // additional libraries for Tegra
    // https://docs.nvidia.com/jetson/l4t/index.html#page/Tegra%20Linux%20Driver%20Package%20Development%20Guide/manifest_tx2_tx2i.html
    "libnvdc.so*",
    "libnvos.so*",
    "libnvrm_gpu.so*",
    "libnvimp.so*",
    "libnvrm.so*",
    "libnvrm_graphics.so*",
    // CUDA
    // https://docs.nvidia.com/cuda/#cuda-api-references
    "libcuda.so*",
    "libcudart.so*",
    "libnvcuvid.so*",
    "libcufft.so*",
    "libcublas.so*",
    "libcublasLt.so*",
    "libcusolver.so*",
    "libcuparse.so*",
    "libcurand.so*",
    "libnppc.so*",
    "libnppig.so*",
    "libnppial.so*",
    "libnppicc.so*",
    "libnppidei.so*",
    "libnppist.so*",
    "libnppcif.so*",
    "libnppim.so*",
    "libnppitc.so*",
    "libnvrtc*",
    "libnvrtc-builtins*",
    "libnvToolsExt.so*",
    // libraries for CUDA DNN
    // https://docs.nvidia.com/deeplearning/cudnn/api/index.html
    // https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html
    "libcudnn.so*",
    "libcudnn_adv_infer*",
    "libcudnn_adv_train*",
    "libcudnn_cnn_infer*",
    "libcudnn_cnn_train*",
    "libcudnn_ops_infer*",
    "libcudnn_ops_train*",
};

static const size_t nvidia_globs_len = SC_ARRAY_SIZE(nvidia_globs);

static const char *glvnd_globs[] = {
    "libEGL.so*",          "libGL.so*",  "libOpenGL.so*",     "libGLESv1_CM.so*", "libGLESv2.so*",
    "libGLX_indirect.so*", "libGLX.so*", "libGLdispatch.so*", "libGLU.so*",
};

static const size_t glvnd_globs_len = SC_ARRAY_SIZE(glvnd_globs);

#endif  // defined(NVIDIA_BIARCH) || defined(NVIDIA_MULTIARCH)

// Populate libgl_dir with a symlink farm to files matching glob_list.
//
// The symbolic links are made in one of two ways. If the library found is a
// file a regular symlink "$libname" -> "/path/to/hostfs/$libname" is created.
// If the library is a symbolic link then relative links are kept as-is but
// absolute links are translated to have "/path/to/hostfs" up front so that
// they work after the pivot_root elsewhere.
//
// The glob list passed to us is produced with paths relative to source dir,
// to simplify the various tie-in points with this function.
static void sc_populate_libgl_with_hostfs_symlinks(const char *libgl_dir, const char *source_dir,
                                                   const char *glob_list[], size_t glob_list_len) {
    size_t source_dir_len = strlen(source_dir);
    glob_t glob_res SC_CLEANUP(globfree) = {.gl_pathv = NULL};
    // Find all the entries matching the list of globs
    for (size_t i = 0; i < glob_list_len; ++i) {
        const char *glob_pattern = glob_list[i];
        char glob_pattern_full[512] = {0};
        sc_must_snprintf(glob_pattern_full, sizeof glob_pattern_full, "%s/%s", source_dir, glob_pattern);

        int err = glob(glob_pattern_full, i ? GLOB_APPEND : 0, NULL, &glob_res);
        // Not all of the files have to be there (they differ depending on the
        // driver version used). Ignore all errors that are not GLOB_NOMATCH.
        if (err != 0 && err != GLOB_NOMATCH) {
            die("cannot search using glob pattern %s: %d", glob_pattern_full, err);
        }
    }
    // Symlink each file found
    for (size_t i = 0; i < glob_res.gl_pathc; ++i) {
        char symlink_name[512] = {0};
        char symlink_target[512] = {0};
        char prefix_dir[512] = {0};
        const char *pathname = glob_res.gl_pathv[i];
        char *pathname_copy1 SC_CLEANUP(sc_cleanup_string) = sc_strdup(pathname);
        char *pathname_copy2 SC_CLEANUP(sc_cleanup_string) = sc_strdup(pathname);
        // POSIX dirname() and basename() may modify their input arguments
        char *filename = basename(pathname_copy1);
        char *directory_name = dirname(pathname_copy2);
        sc_must_snprintf(prefix_dir, sizeof prefix_dir, "%s", libgl_dir);

        if (strlen(directory_name) > source_dir_len) {
            // Additional path elements between source_dir and dirname, meaning the
            // actual file is not placed directly under source_dir but under one or
            // more directories below source_dir. Make sure to recreate the whole
            // prefix
            sc_must_snprintf(prefix_dir, sizeof prefix_dir, "%s%s", libgl_dir, &directory_name[source_dir_len]);
            if (sc_nonfatal_mkpath(prefix_dir, 0755, 0, 0) != 0) {
                die("failed to create prefix path: %s", prefix_dir);
            }
        }

        struct stat stat_buf;
        int err = lstat(pathname, &stat_buf);
        if (err != 0) {
            die("cannot stat file %s", pathname);
        }
        switch (stat_buf.st_mode & S_IFMT) {
            case S_IFLNK:;
                // Read the target of the symbolic link
                char hostfs_symlink_target[512] = {0};
                ssize_t num_read;
                hostfs_symlink_target[0] = 0;
                num_read = readlink(pathname, hostfs_symlink_target, sizeof hostfs_symlink_target - 1);
                if (num_read == -1) {
                    die("cannot read symbolic link %s", pathname);
                }
                hostfs_symlink_target[num_read] = 0;
                if (hostfs_symlink_target[0] == '/') {
                    sc_must_snprintf(symlink_target, sizeof symlink_target, "/var/lib/snapd/hostfs%s",
                                     hostfs_symlink_target);
                } else {
                    // Keep relative symlinks as-is, so that they point to -> libfoo.so.0.123
                    sc_must_snprintf(symlink_target, sizeof symlink_target, "%s", hostfs_symlink_target);
                }
                break;
            case S_IFREG:
                sc_must_snprintf(symlink_target, sizeof symlink_target, "/var/lib/snapd/hostfs%s", pathname);
                break;
            default:
                debug("ignoring unsupported entry: %s", pathname);
                continue;
        }
        sc_must_snprintf(symlink_name, sizeof symlink_name, "%s/%s", prefix_dir, filename);
        debug("creating symbolic link %s -> %s", symlink_name, symlink_target);

        // Make sure we don't have some link already (merged GLVND systems)
        if (lstat(symlink_name, &stat_buf) == 0) {
            if (unlink(symlink_name) != 0) {
                die("cannot remove symbolic link target %s", symlink_name);
            }
        }

        if (symlink(symlink_target, symlink_name) != 0) {
            die("cannot create symbolic link %s -> %s", symlink_name, symlink_target);
        }
    }
}

static void sc_mkdir_and_mount_tpmfs(const char *dir) {
    if (sc_ensure_mkdir(dir, 0755, 0, 0) != 0) {
        die("cannot create tmpfs target %s", dir);
    }

    debug("mounting tmpfs at %s", dir);
    sc_do_mount("none", dir, "tmpfs", MS_NODEV | MS_NOEXEC, NULL);
}

static void sc_remount_ro(const char *mount_point) {
    debug("remounting as read-only %s", mount_point);
    sc_do_mount(NULL, mount_point, NULL, MS_REMOUNT | MS_BIND | MS_RDONLY, NULL);
}

static void sc_mkdir_and_mount_and_glob_files(const char *rootfs_dir, const char *source_dir[], size_t source_dir_len,
                                              const char *tgt_dir, const char *glob_list[], size_t glob_list_len) {
    // Bind mount a tmpfs on $rootfs_dir/$tgt_dir (i.e. /var/lib/snapd/lib/gl)
    char buf[512] = {0};
    sc_must_snprintf(buf, sizeof(buf), "%s%s", rootfs_dir, tgt_dir);
    const char *libgl_dir = buf;

    sc_mkdir_and_mount_tpmfs(libgl_dir);

    for (size_t i = 0; i < source_dir_len; i++) {
        // Populate libgl_dir with symlinks to libraries from hostfs
        sc_populate_libgl_with_hostfs_symlinks(libgl_dir, source_dir[i], glob_list, glob_list_len);
    }

    // Remount $tgt_dir (i.e. .../lib/gl) read only
    sc_remount_ro(libgl_dir);
}

#ifdef NVIDIA_BIARCH

// Expose host NVIDIA drivers to the snap on biarch systems.
//
// Order is absolutely imperative here. We'll attempt to find the
// primary files for the architecture in the main directory, and end
// up copying any files across. However it is possible we're using a
// GLVND enabled host, in which case we copied libGL* to the farm.
// The next step in the list is to look within the private nvidia
// directory, exposed using ld.so.conf tricks within the host OS.
// In some distros (i.e. Solus) only the private libGL/libEGL files
// may be found here, and they'll clobber the existing GLVND files from
// the previous run.
// In other distros (like Fedora) all NVIDIA libraries are contained
// within the private directory, so we clobber the GLVND files and we
// also grab all the private NVIDIA libraries.
//
// In non GLVND cases we just copy across the exposed libGLs and NVIDIA
// libraries from wherever we find, and clobbering is also harmless.
static void sc_mount_nvidia_driver_biarch(const char *rootfs_dir, const char **globs, size_t globs_len) {
    static const char *native_sources[] = {
        NATIVE_LIBDIR,
        NATIVE_LIBDIR "/nvidia*",
    };
    const size_t native_sources_len = SC_ARRAY_SIZE(native_sources);

#if UINTPTR_MAX == 0xffffffffffffffff
    // Alternative 32-bit support
    static const char *lib32_sources[] = {
        LIB32_DIR,
        LIB32_DIR "/nvidia*",
    };
    const size_t lib32_sources_len = SC_ARRAY_SIZE(lib32_sources);
#endif

    // Primary arch
    sc_mkdir_and_mount_and_glob_files(rootfs_dir, native_sources, native_sources_len, SC_LIBGL_DIR, globs, globs_len);

#if UINTPTR_MAX == 0xffffffffffffffff
    // Alternative 32-bit support
    sc_mkdir_and_mount_and_glob_files(rootfs_dir, lib32_sources, lib32_sources_len, SC_LIBGL32_DIR, globs, globs_len);
#endif
}

#endif  // ifdef NVIDIA_BIARCH

#ifdef NVIDIA_MULTIARCH

typedef struct {
    int major;
    // Driver version format is MAJOR.MINOR[.MICRO] but we only care about the
    // major version and the full version string. The micro component has been
    // seen with relevant leading zeros (e.g. "440.48.02").
    char raw[128];  // The size was picked as "big enough" for version strings.
} sc_nv_version;

static void sc_probe_nvidia_driver(sc_nv_version *version) {
    memset(version, 0, sizeof *version);

    FILE *file SC_CLEANUP(sc_cleanup_file) = NULL;
    debug("opening file describing nvidia driver version");
    file = fopen(nvidia_driver_version_file(), "rt");
    if (file == NULL) {
        if (errno == ENOENT) {
            debug("nvidia driver version file doesn't exist");
            return;
        }
        die("cannot open file describing nvidia driver version");
    }
    int nread = fread(version->raw, 1, sizeof version->raw - 1, file);
    if (nread < 0) {
        die("cannot read nvidia driver version string");
    }
    if (nread == sizeof version->raw - 1 && !feof(file)) {
        die("cannot fit entire nvidia driver version string");
    }
    version->raw[nread] = '\0';
    if (nread > 0 && version->raw[nread - 1] == '\n') {
        version->raw[nread - 1] = '\0';
    }
    if (sscanf(version->raw, "%d.", &version->major) != 1) {
        die("cannot parse major version from nvidia driver version string");
    }
}

static void sc_mkdir_and_mount_and_bind(const char *rootfs_dir, const char *src_dir, const char *tgt_dir) {
    sc_nv_version version;

    // Probe sysfs to get the version of the driver that is currently inserted.
    sc_probe_nvidia_driver(&version);

    // If there's driver in the kernel then don't mount userspace.
    if (version.major == 0) {
        return;
    }
    // Construct the paths for the driver userspace libraries
    // and for the gl directory.
    char src[PATH_MAX] = {0};
    char dst[PATH_MAX] = {0};
    sc_must_snprintf(src, sizeof src, "%s-%d", src_dir, version.major);
    sc_must_snprintf(dst, sizeof dst, "%s%s", rootfs_dir, tgt_dir);

    // If there is no userspace driver available then don't try to mount it.
    // This can happen for any number of reasons but one interesting one is
    // that that snapd runs in a lxd container on a host that uses nvidia. In
    // that case the container may not have the userspace library installed but
    // the kernel will still have the module around.
    if (access(src, F_OK) != 0) {
        return;
    }
    if (sc_ensure_mkdir(dst, 0755, 0, 0) != 0) {
        die("cannot create directory %s", dst);
    }
    // Bind mount the binary nvidia driver into $tgt_dir (i.e. /var/lib/snapd/lib/gl).
    debug("bind mounting nvidia driver %s -> %s", src, dst);
    sc_do_mount(src, dst, NULL, MS_BIND, NULL);
}

static int sc_mount_nvidia_is_driver_in_dir(const char *dir) {
    char driver_path[512] = {0};

    sc_nv_version version;

    // Probe sysfs to get the version of the driver that is currently inserted.
    sc_probe_nvidia_driver(&version);

    // If there's no driver then we should not bother ourselves with finding the
    // matching library
    if (version.major == 0) {
        return 0;
    }

    // Probe if a well known library is found in directory dir. We must use the
    // raw version because it may contain more than just major.minor. In
    // practice the micro version may have leading zeros that are relevant.
    sc_must_snprintf(driver_path, sizeof driver_path, "%s/libnvidia-glcore.so.%s", dir, version.raw);

    debug("looking for nvidia canary file %s", driver_path);
    if (access(driver_path, F_OK) == 0) {
        debug("nvidia library detected at path %s", driver_path);
        return 1;
    }
    return 0;
}

static void sc_mount_nvidia_driver_multiarch(const char *rootfs_dir, const char **globs, size_t globs_len) {
    const char *native_libdir = NATIVE_LIBDIR "/" HOST_ARCH_TRIPLET;
    const char *lib32_libdir = NATIVE_LIBDIR "/" HOST_ARCH32_TRIPLET;

    if ((strlen(HOST_ARCH_TRIPLET) > 0) && (sc_mount_nvidia_is_driver_in_dir(native_libdir) == 1)) {
        // sc_mkdir_and_mount_and_glob_files() takes an array of strings, so
        // initialize native_sources accordingly, but calculate the array length
        // dynamically to make adjustments to native_sources easier.
        const char *native_sources[] = {native_libdir};
        const size_t native_sources_len = SC_ARRAY_SIZE(native_sources);
        // Primary arch
        sc_mkdir_and_mount_and_glob_files(rootfs_dir, native_sources, native_sources_len, SC_LIBGL_DIR, globs,
                                          globs_len);

        // Alternative 32-bit support
        if ((strlen(HOST_ARCH32_TRIPLET) > 0) && (sc_mount_nvidia_is_driver_in_dir(lib32_libdir) == 1)) {
            // sc_mkdir_and_mount_and_glob_files() takes an array of strings, so
            // initialize lib32_sources accordingly, but calculate the array length
            // dynamically to make adjustments to lib32_sources easier.
            const char *lib32_sources[] = {lib32_libdir};
            const size_t lib32_sources_len = SC_ARRAY_SIZE(lib32_sources);
            sc_mkdir_and_mount_and_glob_files(rootfs_dir, lib32_sources, lib32_sources_len, SC_LIBGL32_DIR, globs,
                                              globs_len);
        }
    } else {
        // Attempt mount of both the native and 32-bit variants of the driver if they exist
        sc_mkdir_and_mount_and_bind(rootfs_dir, "/usr/lib/nvidia", SC_LIBGL_DIR);
        // Alternative 32-bit support
        sc_mkdir_and_mount_and_bind(rootfs_dir, "/usr/lib32/nvidia", SC_LIBGL32_DIR);
    }
}

static int sc_mount_exported_paths(const char *rootfs_dir) {
    // We are interested only in exports from GPU related interfaces to the
    // system, so we check the interface name in the files.
    // TODO we need to think what would happen if at some point these
    // interfaces are used by some GPU that is not Nvidia.
    static const char *export_globs[] = {
        SC_SNAPD_EXPORT "/system_*_egl-driver-libs.library-source",
        SC_SNAPD_EXPORT "/system_*_gbm-driver-libs.library-source",
        SC_SNAPD_EXPORT "/system_*_cuda-driver-libs.library-source",
        SC_SNAPD_EXPORT "/system_*_nvidia-video-driver-libs.library-source",
        SC_SNAPD_EXPORT "/system_*_opengl-driver-libs.library-source",
        SC_SNAPD_EXPORT "/system_*_opengles-driver-libs.library-source",
        SC_SNAPD_EXPORT "/system_*_vulkan-driver-libs.library-source",
    };
    size_t export_globs_len = SC_ARRAY_SIZE(export_globs);
    glob_t glob_res SC_CLEANUP(globfree) = {.gl_pathv = NULL};

    for (size_t i = 0; i < export_globs_len; ++i) {
        const char *glob_pattern = export_globs[i];
        int err = glob(glob_pattern, i ? GLOB_APPEND : 0, NULL, &glob_res);
        if (err != 0 && err != GLOB_NOMATCH) {
            die("cannot search using glob pattern %s: %d", glob_pattern, err);
        }
    }
    if (glob_res.gl_pathc == 0) {
        debug("no relevant sources in snaps export folder");
        return 0;
    }

    debug("sources in export folder, nvidia libraries from snaps");

    // Create tmpfs - here we will create subdirectories and mount the paths
    // exported by other snaps.
    char tmpfs_path[PATH_MAX] = {0};
    sc_must_snprintf(tmpfs_path, sizeof tmpfs_path, "%s%s", rootfs_dir, SC_LIBGPU_DIR);
    // There are 2 new dirs here, so we have to create before sc_mkdir_and_mount_tpmfs
    if (sc_nonfatal_mkpath(tmpfs_path, 0755, 0, 0) != 0) {
        die("cannot create %s", tmpfs_path);
    }
    sc_mkdir_and_mount_tpmfs(tmpfs_path);

    // Libs exported by snaps, bind mount them
    char path[PATH_MAX] = {0};
    char dest_path[PATH_MAX] = {0};
    for (size_t i = 0; i < glob_res.gl_pathc; ++i) {
        debug("reading sources from %s", glob_res.gl_pathv[i]);
        FILE *file SC_CLEANUP(sc_cleanup_file) = NULL;
        file = fopen(glob_res.gl_pathv[i], "r");
        if (file == NULL) {
            die("cannot open file %s", glob_res.gl_pathv[i]);
        }

        while (fgets(path, sizeof path, file) != NULL) {
            // Remove trailing \n if present
            sc_str_chomp(path);
            if (!sc_startswith(path, SC_SNAP_DIR)) {
                debug("WARNING: unexpectedly %s does not start with %s, not mounting", path, SC_SNAP_DIR);
                continue;
            }

            // Skip if source does not exist or is not a directory
            struct stat stat_buf;
            int res = lstat(path, &stat_buf);
            if (res == -1) {
                debug("%s does not exist, skipping", path);
                continue;
            }
            if ((stat_buf.st_mode & S_IFMT) != S_IFDIR) {
                debug("%s is not a directory, skipping", path);
                continue;
            }

            size_t path_start = 0;
            path_start = sizeof SC_SNAP_DIR - 1;
            sc_must_snprintf(dest_path, sizeof dest_path, "%s" SC_LIBGPU_DIR "%s", rootfs_dir, &path[path_start]);

            // If the path exists that implies that it has been mounted already
            // and that this is a repeated entry.
            res = stat(dest_path, &stat_buf);
            if (res == 0) {
                debug("%s is already mounted, skipping", dest_path);
                continue;
            }

            debug("mounting %s at %s", path, dest_path);
            if (sc_nonfatal_mkpath(dest_path, 0755, 0, 0) != 0) {
                die("failed to create mount path: %s", dest_path);
            }
            sc_do_mount(path, dest_path, NULL, MS_BIND | MS_RDONLY, NULL);
        }
    }

    // Make the tmpfs read-only
    sc_remount_ro(tmpfs_path);

    return 1;
}

// TODO move to utils.c and add some unit tests
static void sc_copy_file(const char *src, const char *dest) {
    int fd_in SC_CLEANUP(sc_cleanup_close) = -1;
    int fd_out SC_CLEANUP(sc_cleanup_close) = -1;
    if ((fd_in = open(src, O_RDONLY)) == -1) {
        die("cannot open %s", src);
    }
    struct stat f_stat = {0};
    if (fstat(fd_in, &f_stat) == -1) {
        die("cannot stat %s", src);
    }
    if ((fd_out = creat(dest, f_stat.st_mode)) == -1) {
        die("cannot create %s", dest);
    }

    off_t copied = 0;
    while (copied < f_stat.st_size) {
        ssize_t written = sendfile(fd_out, fd_in, &copied, SSIZE_MAX);
        if (written == -1) {
            die("while copying %s to %s", src, dest);
        }
        copied += written;
    }
}

// Copy files matching path_glob to the target directory.
static void sc_copy_glob_files(const char *rootfs_dir, const char *mnt_dir, const char *path_glob,
                               const char *target_dir) {
    char glob_pattern[PATH_MAX] = {0};
    sc_must_snprintf(glob_pattern, sizeof glob_pattern, "%s%s", rootfs_dir, path_glob);
    debug("copying files defined by glob %s", glob_pattern);

    // Find matching files
    glob_t glob_res SC_CLEANUP(globfree) = {.gl_pathv = NULL};
    int err = glob(glob_pattern, 0, NULL, &glob_res);
    if (err != 0 && err != GLOB_NOMATCH) {
        die("while searching using glob pattern %s: %d", glob_pattern, err);
    }
    if (glob_res.gl_pathc == 0) {
        return;
    }

    // Create tmpfs on the directory where we will copy the files
    char tmpfs_path[PATH_MAX] = {0};
    sc_must_snprintf(tmpfs_path, sizeof tmpfs_path, "%s%s", rootfs_dir, mnt_dir);
    sc_mkdir_and_mount_tpmfs(tmpfs_path);

    // Ensure target, which is a subdirectory of mnt_dir, exists
    char dir[PATH_MAX] = {0};
    sc_must_snprintf(dir, sizeof(dir), "%s%s", rootfs_dir, target_dir);
    if (sc_ensure_mkdir(dir, 0755, 0, 0) != 0) {
        die("cannot create vendor directory %s", dir);
    }

    // Copy over the configuration files created by snapd
    for (size_t i = 0; i < glob_res.gl_pathc; ++i) {
        // Copy needed as basename() might modify its argument
        char *src_dup SC_CLEANUP(sc_cleanup_string) = sc_strdup(glob_res.gl_pathv[i]);
        const char *filename = basename(src_dup);

        char target[PATH_MAX] = {0};
        sc_must_snprintf(target, sizeof target, "%s%s/%s", rootfs_dir, target_dir, filename);

        debug("copying %s to %s", glob_res.gl_pathv[i], target);
        sc_copy_file(glob_res.gl_pathv[i], target);
    }

    // Make the tmpfs read-only
    sc_remount_ro(tmpfs_path);
}

#endif  // ifdef NVIDIA_MULTIARCH

static void sc_mount_vulkan(const char *rootfs_dir) {
    const char *vulkan_sources[] = {
        SC_VULKAN_SOURCE_DIR,
    };
    const size_t vulkan_sources_len = SC_ARRAY_SIZE(vulkan_sources);

    sc_mkdir_and_mount_and_glob_files(rootfs_dir, vulkan_sources, vulkan_sources_len, SC_VULKAN_DIR, vulkan_globs,
                                      vulkan_globs_len);
}

static void sc_mount_egl(const char *rootfs_dir) {
    const char *egl_vendor_sources[] = {SC_EGL_VENDOR_SOURCE_DIR};
    const size_t egl_vendor_sources_len = SC_ARRAY_SIZE(egl_vendor_sources);

    sc_mkdir_and_mount_and_glob_files(rootfs_dir, egl_vendor_sources, egl_vendor_sources_len, SC_GLVND_DIR,
                                      egl_vendor_globs, egl_vendor_globs_len);
}

void sc_mount_nvidia_driver(const char *rootfs_dir, const char *base_snap_name) {
    /* If NVIDIA module isn't loaded, don't attempt to mount the drivers */
    if (access(nvidia_driver_version_file(), F_OK) != 0) {
        return;
    }

    if (sc_nonfatal_mkpath(SC_EXTRA_LIB_DIR, 0755, 0, 0) != 0) {
        die("cannot create " SC_EXTRA_LIB_DIR);
    }

#if defined(NVIDIA_BIARCH) || defined(NVIDIA_MULTIARCH)
    /* We include the globs for the glvnd libraries for old snaps
     * based on core, Ubuntu 16.04 did not include glvnd itself.
     *
     * While there is no guarantee that the host system's glvnd
     * libGL will be compatible (as it is built with the host
     * system's glibc), the Mesa libGL included with the snap will
     * definitely not be compatible (as it expects to find the Mesa
     * implementation of the GLX extension)..
     */
    const char **globs = nvidia_globs;
    size_t globs_len = nvidia_globs_len;
    const char **full_globs SC_CLEANUP(sc_cleanup_shallow_strv) = NULL;
    if (sc_streq(base_snap_name, "core")) {
        full_globs = malloc(sizeof nvidia_globs + sizeof glvnd_globs);
        if (full_globs == NULL) {
            die("cannot allocate globs array");
        }
        memcpy(full_globs, nvidia_globs, sizeof nvidia_globs);
        memcpy(&full_globs[nvidia_globs_len], glvnd_globs, sizeof glvnd_globs);
        globs = full_globs;
        globs_len = nvidia_globs_len + glvnd_globs_len;
    }
#endif

    int exported_paths = 0;
#ifdef NVIDIA_MULTIARCH
    // For hybrid we might have exported paths from snaps, otherwise do the
    // regular multiarch.
    exported_paths = sc_mount_exported_paths(rootfs_dir);
    if (exported_paths > 0) {
        // Copy configuration files created by {egl,vulkan}-driver-libs. The
        // sources are symlinks to files shipped by snaps, so we cannot just
        // symlink from the export folder, instead we copy in the tmpfs. The
        // files are actually very small so this is ok.
        sc_copy_glob_files(rootfs_dir, SC_GLVND_DIR, SC_SNAP_EGL_VENDOR_GLOB, SC_GLVND_EGL_DIR);
        sc_copy_glob_files(rootfs_dir, SC_VULKAN_DIR, SC_SNAP_VULKAN_ICD_GLOB, SC_VULKAN_ICD_DIR);
    } else {
        sc_mount_nvidia_driver_multiarch(rootfs_dir, globs, globs_len);
    }
#endif  // ifdef NVIDIA_MULTIARCH
#ifdef NVIDIA_BIARCH
    sc_mount_nvidia_driver_biarch(rootfs_dir, globs, globs_len);
#endif  // ifdef NVIDIA_BIARCH

    if (exported_paths == 0) {
        // Common for both driver mechanisms
        sc_mount_vulkan(rootfs_dir);
        sc_mount_egl(rootfs_dir);
    }
}
