Skip to content

Commit 5cd2a2a

Browse files
evelikovlucasdemarchi
authored andcommitted
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: #244 Signed-off-by: Emil Velikov <[email protected]> Link: #346 Signed-off-by: Lucas De Marchi <[email protected]>
1 parent a0fb309 commit 5cd2a2a

File tree

3 files changed

+55
-61
lines changed

3 files changed

+55
-61
lines changed

libkmod/libkmod-file.c

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,57 +58,53 @@ static const struct comp_type {
5858
// clang-format on
5959
};
6060

61-
struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
61+
int kmod_file_get_elf(struct kmod_file *file, struct kmod_elf **elf)
6262
{
63-
int err;
64-
65-
if (file->elf)
66-
return file->elf;
67-
68-
err = kmod_file_load_contents(file);
69-
if (err) {
70-
errno = -err;
71-
return NULL;
63+
if (!file->elf) {
64+
int err = kmod_file_load_contents(file);
65+
if (err)
66+
return err;
67+
68+
err = kmod_elf_new(file->memory, file->size, &file->elf);
69+
if (err)
70+
return err;
7271
}
7372

74-
err = kmod_elf_new(file->memory, file->size, &file->elf);
75-
if (err) {
76-
errno = -err;
77-
return NULL;
78-
}
79-
return file->elf;
73+
*elf = file->elf;
74+
return 0;
8075
}
8176

82-
struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, const char *filename)
77+
int kmod_file_open(const struct kmod_ctx *ctx, const char *filename,
78+
struct kmod_file **out_file)
8379
{
84-
struct kmod_file *file;
80+
_cleanup_free_ struct kmod_file *file;
8581
char buf[7];
8682
ssize_t sz;
83+
int ret;
8784

8885
assert_cc(sizeof(magic_zstd) < sizeof(buf));
8986
assert_cc(sizeof(magic_xz) < sizeof(buf));
9087
assert_cc(sizeof(magic_zlib) < sizeof(buf));
9188

9289
file = calloc(1, sizeof(struct kmod_file));
93-
if (file == NULL)
94-
return NULL;
90+
if (file == NULL) {
91+
file = NULL;
92+
return -ENOMEM;
93+
}
9594

9695
file->fd = open(filename, O_RDONLY | O_CLOEXEC);
97-
if (file->fd < 0) {
98-
free(file);
99-
return NULL;
100-
}
96+
if (file->fd < 0)
97+
return -errno;
10198

10299
sz = pread_str_safe(file->fd, buf, sizeof(buf), 0);
103100
if (sz != (sizeof(buf) - 1)) {
104101
if (sz < 0)
105-
errno = -sz;
102+
ret = (int)sz;
106103
else
107-
errno = EINVAL;
104+
ret = -EINVAL;
108105

109106
close(file->fd);
110-
free(file);
111-
return NULL;
107+
return ret;
112108
}
113109

114110
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
124120

125121
file->ctx = ctx;
126122

127-
return file;
123+
*out_file = file;
124+
file = NULL;
125+
return 0;
128126
}
129127

130128
/*

libkmod/libkmod-internal.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ _nonnull_(1) void kmod_module_set_required(struct kmod_module *mod, bool require
135135
_nonnull_all_ bool kmod_module_is_builtin(struct kmod_module *mod);
136136

137137
/* libkmod-file.c */
138-
_must_check_ _nonnull_all_ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, const char *filename);
139-
_nonnull_all_ struct kmod_elf *kmod_file_get_elf(struct kmod_file *file);
138+
struct kmod_file;
139+
struct kmod_elf;
140+
_must_check_ _nonnull_all_ int kmod_file_open(const struct kmod_ctx *ctx, const char *filename, struct kmod_file **file);
141+
_must_check_ _nonnull_all_ int kmod_file_get_elf(struct kmod_file *file, struct kmod_elf **elf);
140142
_nonnull_all_ int kmod_file_load_contents(struct kmod_file *file);
141143
_must_check_ _nonnull_all_ const void *kmod_file_get_contents(const struct kmod_file *file);
142144
_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);
145147
_nonnull_all_ void kmod_file_unref(struct kmod_file *file);
146148

147149
/* libkmod-elf.c */
148-
struct kmod_elf;
149150
struct kmod_modversion {
150151
uint64_t crc;
151152
enum kmod_symbol_bind bind;

libkmod/libkmod-module.c

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,9 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags, const cha
656656
int err;
657657

658658
if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
659-
elf = kmod_file_get_elf(mod->file);
660-
if (elf == NULL) {
661-
err = -errno;
659+
err = kmod_file_get_elf(mod->file, &elf);
660+
if (err)
662661
return err;
663-
}
664662

665663
err = kmod_elf_strip(elf, flags, &stripped);
666664
if (err) {
@@ -702,11 +700,9 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod, unsigned int
702700
}
703701

704702
if (!mod->file) {
705-
mod->file = kmod_file_open(mod->ctx, path);
706-
if (mod->file == NULL) {
707-
err = -errno;
703+
err = kmod_file_open(mod->ctx, path, &mod->file);
704+
if (err)
708705
return err;
709-
}
710706
}
711707

712708
err = do_finit_module(mod, flags, args);
@@ -1736,22 +1732,21 @@ KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
17361732
kmod_list_release(list, kmod_module_section_free);
17371733
}
17381734

1739-
static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
1735+
static int kmod_module_get_elf(const struct kmod_module *mod, struct kmod_elf **elf)
17401736
{
17411737
if (mod->file == NULL) {
17421738
const char *path = kmod_module_get_path(mod);
1739+
int ret;
17431740

1744-
if (path == NULL) {
1745-
errno = ENOENT;
1746-
return NULL;
1747-
}
1741+
if (path == NULL)
1742+
return -ENOENT;
17481743

1749-
((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx, path);
1750-
if (mod->file == NULL)
1751-
return NULL;
1744+
ret = kmod_file_open(mod->ctx, path, &((struct kmod_module *)mod)->file);
1745+
if (ret)
1746+
return ret;
17521747
}
17531748

1754-
return kmod_file_get_elf(mod->file);
1749+
return kmod_file_get_elf(mod->file, elf);
17551750
}
17561751

17571752
struct kmod_module_info {
@@ -1872,9 +1867,9 @@ KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod,
18721867
if (count < 0)
18731868
return count;
18741869
} else {
1875-
elf = kmod_module_get_elf(mod);
1876-
if (elf == NULL)
1877-
return -errno;
1870+
ret = kmod_module_get_elf(mod, &elf);
1871+
if (ret)
1872+
return ret;
18781873

18791874
count = kmod_elf_get_modinfo_strings(elf, &strings);
18801875
if (count < 0)
@@ -2020,9 +2015,9 @@ KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod,
20202015

20212016
assert(*list == NULL);
20222017

2023-
elf = kmod_module_get_elf(mod);
2024-
if (elf == NULL)
2025-
return -errno;
2018+
ret = kmod_module_get_elf(mod, &elf);
2019+
if (ret)
2020+
return ret;
20262021

20272022
count = kmod_elf_get_modversions(elf, &versions);
20282023
if (count < 0)
@@ -2121,9 +2116,9 @@ KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod,
21212116

21222117
assert(*list == NULL);
21232118

2124-
elf = kmod_module_get_elf(mod);
2125-
if (elf == NULL)
2126-
return -errno;
2119+
ret = kmod_module_get_elf(mod, &elf);
2120+
if (ret)
2121+
return ret;
21272122

21282123
count = kmod_elf_get_symbols(elf, &symbols);
21292124
if (count < 0)
@@ -2227,9 +2222,9 @@ KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod
22272222

22282223
assert(*list == NULL);
22292224

2230-
elf = kmod_module_get_elf(mod);
2231-
if (elf == NULL)
2232-
return -errno;
2225+
ret = kmod_module_get_elf(mod, &elf);
2226+
if (ret)
2227+
return ret;
22332228

22342229
count = kmod_elf_get_dependency_symbols(elf, &symbols);
22352230
if (count < 0)

0 commit comments

Comments
 (0)