From: Emil Velikov Date: Wed, 7 May 2025 17:58:11 +0000 (+0100) Subject: libkmod: update remaining function to return the error code X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5cd2a2a4874154bab15de6ad8be85d0571dfc2b8;p=thirdparty%2Fkmod.git libkmod: update remaining function to return the error code Rework the function signature and return the error code instead of the stripped module. Thus we no longer explicitly set errno. v2: - kmod_file_open() - use _cleanup_free_, return errno instead of ENOMEM Reference: https://github.com/kmod-project/kmod/issues/244 Signed-off-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/346 Signed-off-by: Lucas De Marchi --- diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c index 23057fa6..d651fa2e 100644 --- a/libkmod/libkmod-file.c +++ b/libkmod/libkmod-file.c @@ -58,57 +58,53 @@ static const struct comp_type { // clang-format on }; -struct kmod_elf *kmod_file_get_elf(struct kmod_file *file) +int kmod_file_get_elf(struct kmod_file *file, struct kmod_elf **elf) { - int err; - - if (file->elf) - return file->elf; - - err = kmod_file_load_contents(file); - if (err) { - errno = -err; - return NULL; + if (!file->elf) { + int err = kmod_file_load_contents(file); + if (err) + return err; + + err = kmod_elf_new(file->memory, file->size, &file->elf); + if (err) + return err; } - err = kmod_elf_new(file->memory, file->size, &file->elf); - if (err) { - errno = -err; - return NULL; - } - return file->elf; + *elf = file->elf; + return 0; } -struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, const char *filename) +int kmod_file_open(const struct kmod_ctx *ctx, const char *filename, + struct kmod_file **out_file) { - struct kmod_file *file; + _cleanup_free_ struct kmod_file *file; char buf[7]; ssize_t sz; + int ret; assert_cc(sizeof(magic_zstd) < sizeof(buf)); assert_cc(sizeof(magic_xz) < sizeof(buf)); assert_cc(sizeof(magic_zlib) < sizeof(buf)); file = calloc(1, sizeof(struct kmod_file)); - if (file == NULL) - return NULL; + if (file == NULL) { + file = NULL; + return -ENOMEM; + } file->fd = open(filename, O_RDONLY | O_CLOEXEC); - if (file->fd < 0) { - free(file); - return NULL; - } + if (file->fd < 0) + return -errno; sz = pread_str_safe(file->fd, buf, sizeof(buf), 0); if (sz != (sizeof(buf) - 1)) { if (sz < 0) - errno = -sz; + ret = (int)sz; else - errno = EINVAL; + ret = -EINVAL; close(file->fd); - free(file); - return NULL; + return ret; } for (unsigned int i = 0; i < ARRAY_SIZE(comp_types); i++) { @@ -124,7 +120,9 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, const char *filenam file->ctx = ctx; - return file; + *out_file = file; + file = NULL; + return 0; } /* diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h index 17e6e5f5..c4c24e6f 100644 --- a/libkmod/libkmod-internal.h +++ b/libkmod/libkmod-internal.h @@ -135,8 +135,10 @@ _nonnull_(1) void kmod_module_set_required(struct kmod_module *mod, bool require _nonnull_all_ bool kmod_module_is_builtin(struct kmod_module *mod); /* libkmod-file.c */ -_must_check_ _nonnull_all_ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, const char *filename); -_nonnull_all_ struct kmod_elf *kmod_file_get_elf(struct kmod_file *file); +struct kmod_file; +struct kmod_elf; +_must_check_ _nonnull_all_ int kmod_file_open(const struct kmod_ctx *ctx, const char *filename, struct kmod_file **file); +_must_check_ _nonnull_all_ int kmod_file_get_elf(struct kmod_file *file, struct kmod_elf **elf); _nonnull_all_ int kmod_file_load_contents(struct kmod_file *file); _must_check_ _nonnull_all_ const void *kmod_file_get_contents(const struct kmod_file *file); _must_check_ _nonnull_all_ off_t kmod_file_get_size(const struct kmod_file *file); @@ -145,7 +147,6 @@ _must_check_ _nonnull_all_ int kmod_file_get_fd(const struct kmod_file *file); _nonnull_all_ void kmod_file_unref(struct kmod_file *file); /* libkmod-elf.c */ -struct kmod_elf; struct kmod_modversion { uint64_t crc; enum kmod_symbol_bind bind; diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 5fc40a40..796dafa1 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -656,11 +656,9 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags, const cha int err; if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) { - elf = kmod_file_get_elf(mod->file); - if (elf == NULL) { - err = -errno; + err = kmod_file_get_elf(mod->file, &elf); + if (err) return err; - } err = kmod_elf_strip(elf, flags, &stripped); if (err) { @@ -702,11 +700,9 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod, unsigned int } if (!mod->file) { - mod->file = kmod_file_open(mod->ctx, path); - if (mod->file == NULL) { - err = -errno; + err = kmod_file_open(mod->ctx, path, &mod->file); + if (err) return err; - } } err = do_finit_module(mod, flags, args); @@ -1736,22 +1732,21 @@ KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list) kmod_list_release(list, kmod_module_section_free); } -static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod) +static int kmod_module_get_elf(const struct kmod_module *mod, struct kmod_elf **elf) { if (mod->file == NULL) { const char *path = kmod_module_get_path(mod); + int ret; - if (path == NULL) { - errno = ENOENT; - return NULL; - } + if (path == NULL) + return -ENOENT; - ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx, path); - if (mod->file == NULL) - return NULL; + ret = kmod_file_open(mod->ctx, path, &((struct kmod_module *)mod)->file); + if (ret) + return ret; } - return kmod_file_get_elf(mod->file); + return kmod_file_get_elf(mod->file, elf); } struct kmod_module_info { @@ -1872,9 +1867,9 @@ KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, if (count < 0) return count; } else { - elf = kmod_module_get_elf(mod); - if (elf == NULL) - return -errno; + ret = kmod_module_get_elf(mod, &elf); + if (ret) + return ret; count = kmod_elf_get_modinfo_strings(elf, &strings); if (count < 0) @@ -2020,9 +2015,9 @@ KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, assert(*list == NULL); - elf = kmod_module_get_elf(mod); - if (elf == NULL) - return -errno; + ret = kmod_module_get_elf(mod, &elf); + if (ret) + return ret; count = kmod_elf_get_modversions(elf, &versions); if (count < 0) @@ -2121,9 +2116,9 @@ KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, assert(*list == NULL); - elf = kmod_module_get_elf(mod); - if (elf == NULL) - return -errno; + ret = kmod_module_get_elf(mod, &elf); + if (ret) + return ret; count = kmod_elf_get_symbols(elf, &symbols); if (count < 0) @@ -2227,9 +2222,9 @@ KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod assert(*list == NULL); - elf = kmod_module_get_elf(mod); - if (elf == NULL) - return -errno; + ret = kmod_module_get_elf(mod, &elf); + if (ret) + return ret; count = kmod_elf_get_dependency_symbols(elf, &symbols); if (count < 0)