Skip to content

Commit

Permalink
libkmod/zstd: Allow to load libzstd.so on demand
Browse files Browse the repository at this point in the history
Use dlfcn helpers to load libzstd once it's needed.

Signed-off-by: Lucas De Marchi <[email protected]>
Reviewed-by: Emil Velikov <[email protected]>
Link: #262
  • Loading branch information
lucasdemarchi committed Dec 6, 2024
1 parent 9fd66be commit 9a3930e
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions libkmod/libkmod-file-zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* Copyright © 2024 Intel Corporation
*/

/* TODO: replace with build system define once supported */
#define DLSYM_LOCALLY_ENABLED 0

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -19,6 +22,24 @@
#include "libkmod-internal.h"
#include "libkmod-internal-file.h"

#define DL_SYMBOL_TABLE(M) \
M(ZSTD_decompress) \
M(ZSTD_getErrorName) \
M(ZSTD_getFrameContentSize) \
M(ZSTD_isError)

DL_SYMBOL_TABLE(DECLARE_SYM)

static int dlopen_zstd(void)
{
static void *dl = NULL;

if (!DLSYM_LOCALLY_ENABLED)
return 0;

return dlsym_many(&dl, "libzstd.so.1", DL_SYMBOL_TABLE(DLSYM_ARG) NULL);
}

int kmod_file_load_zstd(struct kmod_file *file)
{
void *src_buf = MAP_FAILED, *dst_buf = NULL;
Expand All @@ -27,6 +48,13 @@ int kmod_file_load_zstd(struct kmod_file *file)
struct stat st;
int ret;

ret = dlopen_zstd();
if (ret < 0) {
ERR(file->ctx, "zstd: can't load and resolve symbols (%s)",
strerror(-ret));
return -EINVAL;
}

if (fstat(file->fd, &st) < 0) {
ret = -errno;
ERR(file->ctx, "zstd: %m\n");
Expand All @@ -45,7 +73,7 @@ int kmod_file_load_zstd(struct kmod_file *file)
goto out;
}

frame_size = ZSTD_getFrameContentSize(src_buf, src_size);
frame_size = sym_ZSTD_getFrameContentSize(src_buf, src_size);
if (frame_size == 0 || frame_size == ZSTD_CONTENTSIZE_UNKNOWN ||
frame_size == ZSTD_CONTENTSIZE_ERROR) {
ret = -EINVAL;
Expand All @@ -65,9 +93,9 @@ int kmod_file_load_zstd(struct kmod_file *file)
goto out;
}

dst_size = ZSTD_decompress(dst_buf, dst_size, src_buf, src_size);
if (ZSTD_isError(dst_size)) {
ERR(file->ctx, "zstd: %s\n", ZSTD_getErrorName(dst_size));
dst_size = sym_ZSTD_decompress(dst_buf, dst_size, src_buf, src_size);
if (sym_ZSTD_isError(dst_size)) {
ERR(file->ctx, "zstd: %s\n", sym_ZSTD_getErrorName(dst_size));
ret = -EINVAL;
goto out;
}
Expand Down

0 comments on commit 9a3930e

Please sign in to comment.