]>
Commit | Line | Data |
---|---|---|
1 | #pragma once | |
2 | ||
3 | #include <stdbool.h> | |
4 | #include <stdio.h> | |
5 | #include <syslog.h> | |
6 | #include <limits.h> | |
7 | ||
8 | #include <shared/macro.h> | |
9 | #include <shared/missing.h> | |
10 | ||
11 | #include "libkmod.h" | |
12 | ||
13 | #define kmod_log_cond(ctx, prio, arg...) \ | |
14 | do { \ | |
15 | if (ENABLE_LOGGING == 1 && \ | |
16 | (ENABLE_DEBUG == 1 || (!ENABLE_DEBUG && prio != LOG_DEBUG)) && \ | |
17 | kmod_get_log_priority(ctx) >= prio) \ | |
18 | kmod_log(ctx, prio, __FILE__, __LINE__, __func__, ##arg); \ | |
19 | } while (0) | |
20 | ||
21 | #define DBG(ctx, arg...) kmod_log_cond(ctx, LOG_DEBUG, ##arg) | |
22 | #define NOTICE(ctx, arg...) kmod_log_cond(ctx, LOG_NOTICE, ##arg) | |
23 | #define INFO(ctx, arg...) kmod_log_cond(ctx, LOG_INFO, ##arg) | |
24 | #define ERR(ctx, arg...) kmod_log_cond(ctx, LOG_ERR, ##arg) | |
25 | ||
26 | #define KMOD_EXPORT __attribute__((visibility("default"))) | |
27 | ||
28 | #define KCMD_LINE_SIZE 4096 | |
29 | ||
30 | #if !HAVE_SECURE_GETENV | |
31 | #warning secure_getenv is not available | |
32 | #define secure_getenv getenv | |
33 | #endif | |
34 | ||
35 | _printf_format_(6, 7) _nonnull_(1, 3, 5) void kmod_log(const struct kmod_ctx *ctx, | |
36 | int priority, const char *file, | |
37 | int line, const char *fn, | |
38 | const char *format, ...); | |
39 | ||
40 | struct list_node { | |
41 | struct list_node *next, *prev; | |
42 | }; | |
43 | ||
44 | struct kmod_list { | |
45 | struct list_node node; | |
46 | void *data; | |
47 | }; | |
48 | ||
49 | enum kmod_file_compression_type { | |
50 | KMOD_FILE_COMPRESSION_NONE = 0, | |
51 | KMOD_FILE_COMPRESSION_ZSTD, | |
52 | KMOD_FILE_COMPRESSION_XZ, | |
53 | KMOD_FILE_COMPRESSION_ZLIB, | |
54 | }; | |
55 | ||
56 | // clang-format off | |
57 | _must_check_ _nonnull_(2) struct kmod_list *kmod_list_append(struct kmod_list *list, const void *data); | |
58 | _must_check_ _nonnull_(2) struct kmod_list *kmod_list_prepend(struct kmod_list *list, const void *data); | |
59 | _must_check_ struct kmod_list *kmod_list_remove(struct kmod_list *list); | |
60 | _must_check_ _nonnull_(2) struct kmod_list *kmod_list_remove_data(struct kmod_list *list, const void *data); | |
61 | _nonnull_(2) struct kmod_list *kmod_list_insert_after(struct kmod_list *list, const void *data); | |
62 | _nonnull_(2) struct kmod_list *kmod_list_insert_before(struct kmod_list *list, const void *data); | |
63 | _must_check_ struct kmod_list *kmod_list_append_list(struct kmod_list *list1, struct kmod_list *list2); | |
64 | #define kmod_list_release(list, free_data) \ | |
65 | while (list) { \ | |
66 | free_data((list)->data); \ | |
67 | list = kmod_list_remove(list); \ | |
68 | } | |
69 | ||
70 | /* libkmod.c */ | |
71 | _nonnull_all_ int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, struct kmod_list **list); | |
72 | _nonnull_all_ int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name, struct kmod_list **list); | |
73 | _nonnull_all_ int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name, struct kmod_list **list); | |
74 | _nonnull_all_ int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name, struct kmod_list **list); | |
75 | _nonnull_all_ int kmod_lookup_alias_from_kernel_builtin_file(struct kmod_ctx *ctx, const char *name, struct kmod_list **list); | |
76 | _nonnull_all_ int kmod_lookup_alias_from_builtin_file(struct kmod_ctx *ctx, const char *name, struct kmod_list **list); | |
77 | _nonnull_all_ bool kmod_lookup_alias_is_builtin(struct kmod_ctx *ctx, const char *name); | |
78 | _nonnull_all_ int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name, struct kmod_list **list); | |
79 | _nonnull_(1) void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited); | |
80 | _nonnull_(1) void kmod_set_modules_required(struct kmod_ctx *ctx, bool required); | |
81 | ||
82 | _nonnull_all_ char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name); | |
83 | ||
84 | _nonnull_all_ struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx, const char *key); | |
85 | _nonnull_all_ int kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, const char *key); | |
86 | _nonnull_all_ void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod, const char *key); | |
87 | ||
88 | _nonnull_all_ const struct kmod_config *kmod_get_config(const struct kmod_ctx *ctx); | |
89 | _nonnull_all_ enum kmod_file_compression_type kmod_get_kernel_compression(const struct kmod_ctx *ctx); | |
90 | ||
91 | /* libkmod-config.c */ | |
92 | struct kmod_config_path { | |
93 | unsigned long long stamp; | |
94 | char path[]; | |
95 | }; | |
96 | ||
97 | struct kmod_config { | |
98 | struct kmod_ctx *ctx; | |
99 | struct kmod_list *aliases; | |
100 | struct kmod_list *blacklists; | |
101 | struct kmod_list *options; | |
102 | struct kmod_list *remove_commands; | |
103 | struct kmod_list *install_commands; | |
104 | struct kmod_list *softdeps; | |
105 | struct kmod_list *weakdeps; | |
106 | ||
107 | struct kmod_list *paths; | |
108 | }; | |
109 | ||
110 | _nonnull_all_ int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **config, const char *const *config_paths); | |
111 | _nonnull_all_ void kmod_config_free(struct kmod_config *config); | |
112 | _nonnull_all_ const char *kmod_blacklist_get_modname(const struct kmod_list *l); | |
113 | _nonnull_all_ const char *kmod_alias_get_name(const struct kmod_list *l); | |
114 | _nonnull_all_ const char *kmod_alias_get_modname(const struct kmod_list *l); | |
115 | _nonnull_all_ const char *kmod_option_get_options(const struct kmod_list *l); | |
116 | _nonnull_all_ const char *kmod_option_get_modname(const struct kmod_list *l); | |
117 | _nonnull_all_ const char *kmod_command_get_command(const struct kmod_list *l); | |
118 | _nonnull_all_ const char *kmod_command_get_modname(const struct kmod_list *l); | |
119 | ||
120 | _nonnull_all_ const char *kmod_softdep_get_name(const struct kmod_list *l); | |
121 | _nonnull_all_ const char *const *kmod_softdep_get_pre(const struct kmod_list *l, unsigned int *count); | |
122 | const char *const *kmod_softdep_get_post(const struct kmod_list *l, unsigned int *count); | |
123 | ||
124 | _nonnull_all_ const char * kmod_weakdep_get_name(const struct kmod_list *l); | |
125 | _nonnull_all_ const char *const *kmod_weakdep_get_weak(const struct kmod_list *l, unsigned int *count); | |
126 | ||
127 | /* libkmod-module.c */ | |
128 | int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias, const char *name, struct kmod_module **mod); | |
129 | _nonnull_all_ void kmod_module_parse_depline(struct kmod_module *mod, char *line); | |
130 | _nonnull_(1) void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd); | |
131 | _nonnull_(1) void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd); | |
132 | _nonnull_(1)void kmod_module_set_visited(struct kmod_module *mod, bool visited); | |
133 | _nonnull_(1) void kmod_module_set_builtin(struct kmod_module *mod, bool builtin); | |
134 | _nonnull_(1) void kmod_module_set_required(struct kmod_module *mod, bool required); | |
135 | _nonnull_all_ bool kmod_module_is_builtin(struct kmod_module *mod); | |
136 | ||
137 | /* libkmod-file.c */ | |
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); | |
142 | _nonnull_all_ int kmod_file_load_contents(struct kmod_file *file); | |
143 | _must_check_ _nonnull_all_ const void *kmod_file_get_contents(const struct kmod_file *file); | |
144 | _must_check_ _nonnull_all_ off_t kmod_file_get_size(const struct kmod_file *file); | |
145 | _must_check_ _nonnull_all_ enum kmod_file_compression_type kmod_file_get_compression(const struct kmod_file *file); | |
146 | _must_check_ _nonnull_all_ int kmod_file_get_fd(const struct kmod_file *file); | |
147 | _nonnull_all_ void kmod_file_unref(struct kmod_file *file); | |
148 | ||
149 | /* libkmod-elf.c */ | |
150 | struct kmod_modversion { | |
151 | uint64_t crc; | |
152 | enum kmod_symbol_bind bind; | |
153 | const char *symbol; | |
154 | }; | |
155 | ||
156 | _must_check_ _nonnull_all_ int kmod_elf_new(const void *memory, off_t size, struct kmod_elf **elf); | |
157 | _nonnull_all_ void kmod_elf_unref(struct kmod_elf *elf); | |
158 | _must_check_ _nonnull_all_ const void *kmod_elf_get_memory(const struct kmod_elf *elf); | |
159 | _must_check_ _nonnull_all_ int kmod_elf_get_modinfo_strings(const struct kmod_elf *elf, char ***array); | |
160 | _must_check_ _nonnull_all_ int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion **array); | |
161 | _must_check_ _nonnull_all_ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **array); | |
162 | _must_check_ _nonnull_all_ int kmod_elf_get_dependency_symbols(const struct kmod_elf *elf, struct kmod_modversion **array); | |
163 | _must_check_ _nonnull_all_ int kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags, const void **stripped); | |
164 | ||
165 | /* | |
166 | * Debug mock lib need to find section ".gnu.linkonce.this_module" in order to | |
167 | * get modname | |
168 | */ | |
169 | _must_check_ _nonnull_all_ int kmod_elf_get_section(const struct kmod_elf *elf, const char *section, uint64_t *sec_off, uint64_t *sec_size); | |
170 | ||
171 | /* libkmod-signature.c */ | |
172 | struct kmod_signature_info { | |
173 | const char *signer; | |
174 | size_t signer_len; | |
175 | const char *key_id; | |
176 | size_t key_id_len; | |
177 | const char *algo, *hash_algo, *id_type; | |
178 | const char *sig; | |
179 | size_t sig_len; | |
180 | void (*free)(void *); | |
181 | void *private; | |
182 | }; | |
183 | _must_check_ _nonnull_all_ bool kmod_module_signature_info(const struct kmod_file *file, struct kmod_signature_info *sig_info); | |
184 | _nonnull_all_ void kmod_module_signature_info_free(struct kmod_signature_info *sig_info); | |
185 | ||
186 | /* libkmod-builtin.c */ | |
187 | _nonnull_all_ ssize_t kmod_builtin_get_modinfo(struct kmod_ctx *ctx, const char *modname, char ***modinfo); | |
188 | // clang-format on |