diff options
author | Lucas De Marchi <lucas.de.marchi@gmail.com> | 2023-06-01 15:40:00 -0700 |
---|---|---|
committer | Lucas De Marchi <lucas.de.marchi@gmail.com> | 2023-06-09 10:45:55 -0700 |
commit | 30077bf1719eb9625b1c223c144b74285425d581 (patch) | |
tree | e7532cb1de1e9f47ec1ea76b55b61b403af8501b | |
parent | e539827635db486ecc70801250f9f73f652cd8d0 (diff) | |
download | kmod-30077bf1719eb9625b1c223c144b74285425d581.tar.gz kmod-30077bf1719eb9625b1c223c144b74285425d581.tar.bz2 kmod-30077bf1719eb9625b1c223c144b74285425d581.zip |
libkmod: Keep track of in-kernel compression support
When creating the context, read /sys/kernel/compression to check what's
the compression type supported by the kernel. This will later be used
when loading modules to check if the decompression step has to happen in
userspace or if it can be delegated to the kernel.
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
-rw-r--r-- | libkmod/libkmod.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 2670f9a..103469e 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -83,6 +83,7 @@ struct kmod_ctx { void *log_data; const void *userdata; char *dirname; + enum kmod_file_compression_type kernel_compression; struct kmod_config *config; struct hash *modules_by_name; struct index_mm *indexes[_KMOD_INDEX_MODULES_SIZE]; @@ -227,6 +228,40 @@ static char *get_kernel_release(const char *dirname) return p; } +static enum kmod_file_compression_type get_kernel_compression(struct kmod_ctx *ctx) +{ + const char *path = "/sys/module/compression"; + char buf[16]; + int fd; + int err; + + fd = open(path, O_RDONLY|O_CLOEXEC); + if (fd < 0) { + /* Not having the file is not an error: kernel may be too old */ + DBG(ctx, "could not open '%s' for reading: %m\n", path); + return KMOD_FILE_COMPRESSION_NONE; + } + + err = read_str_safe(fd, buf, sizeof(buf)); + close(fd); + if (err < 0) { + ERR(ctx, "could not read from '%s': %s\n", + path, strerror(-err)); + return KMOD_FILE_COMPRESSION_NONE; + } + + if (streq(buf, "zstd\n")) + return KMOD_FILE_COMPRESSION_ZSTD; + else if (streq(buf, "xz\n")) + return KMOD_FILE_COMPRESSION_XZ; + else if (streq(buf, "gzip\n")) + return KMOD_FILE_COMPRESSION_ZLIB; + + ERR(ctx, "unknown kernel compression %s", buf); + + return KMOD_FILE_COMPRESSION_NONE; +} + /** * kmod_new: * @dirname: what to consider as linux module's directory, if NULL @@ -272,6 +307,8 @@ KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname, if (env != NULL) kmod_set_log_priority(ctx, log_priority(env)); + ctx->kernel_compression = get_kernel_compression(ctx); + if (config_paths == NULL) config_paths = default_config_paths; err = kmod_config_new(ctx, &ctx->config, config_paths); |