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