Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 1 | /* |
| 2 | * libkmod - interface to kernel module operations |
| 3 | * |
| 4 | * Copyright (C) 2011 ProFUSION embedded systems |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stddef.h> |
| 24 | #include <stdarg.h> |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 25 | #include <limits.h> |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <errno.h> |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 28 | #include <fnmatch.h> |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include <ctype.h> |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 31 | #include <sys/utsname.h> |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 32 | |
| 33 | #include "libkmod.h" |
| 34 | #include "libkmod-private.h" |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 35 | #include "libkmod-index.h" |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 36 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 37 | #define KMOD_HASH_SIZE (256) |
| 38 | #define KMOD_LRU_MAX (128) |
| 39 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 40 | /** |
| 41 | * SECTION:libkmod |
| 42 | * @short_description: libkmod context |
| 43 | * |
| 44 | * The context contains the default values for the library user, |
| 45 | * and is passed to all library operations. |
| 46 | */ |
| 47 | |
Lucas De Marchi | a4a7502 | 2011-12-08 14:56:48 -0200 | [diff] [blame] | 48 | enum kmod_index { |
| 49 | KMOD_INDEX_DEP = 0, |
| 50 | KMOD_INDEX_ALIAS, |
| 51 | KMOD_INDEX_SYMBOL, |
| 52 | _KMOD_INDEX_LAST, |
| 53 | }; |
| 54 | |
| 55 | static const char* index_files[] = { |
| 56 | [KMOD_INDEX_DEP] = "modules.dep", |
| 57 | [KMOD_INDEX_ALIAS] = "modules.alias", |
| 58 | [KMOD_INDEX_SYMBOL] = "modules.symbols", |
| 59 | }; |
| 60 | |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 61 | static const char *default_config_paths[] = { |
| 62 | "/run/modprobe.d", |
| 63 | "/etc/modprobe.d", |
| 64 | "/lib/modprobe.d", |
| 65 | NULL |
| 66 | }; |
| 67 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 68 | /** |
| 69 | * kmod_ctx: |
| 70 | * |
| 71 | * Opaque object representing the library context. |
| 72 | */ |
| 73 | struct kmod_ctx { |
| 74 | int refcount; |
Gustavo Sverzut Barbieri | 8d3f3ef | 2011-12-02 21:10:24 -0200 | [diff] [blame] | 75 | int log_priority; |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 76 | void (*log_fn)(void *data, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 77 | int priority, const char *file, int line, |
| 78 | const char *fn, const char *format, va_list args); |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 79 | void *log_data; |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 80 | const void *userdata; |
| 81 | char *dirname; |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 82 | struct kmod_config *config; |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 83 | struct kmod_hash *modules_by_name; |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 84 | struct index_mm *indexes[_KMOD_INDEX_LAST]; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 85 | }; |
| 86 | |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 87 | void kmod_log(const struct kmod_ctx *ctx, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 88 | int priority, const char *file, int line, const char *fn, |
| 89 | const char *format, ...) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 90 | { |
| 91 | va_list args; |
| 92 | |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 93 | if (ctx->log_fn == NULL) |
| 94 | return; |
| 95 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 96 | va_start(args, format); |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 97 | ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 98 | va_end(args); |
| 99 | } |
| 100 | |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 101 | static void log_filep(void *data, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 102 | int priority, const char *file, int line, |
| 103 | const char *fn, const char *format, va_list args) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 104 | { |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 105 | FILE *fp = data; |
| 106 | fprintf(fp, "libkmod: %s: ", fn); |
| 107 | vfprintf(fp, format, args); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 108 | } |
| 109 | |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 110 | const char *kmod_get_dirname(const struct kmod_ctx *ctx) |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 111 | { |
| 112 | return ctx->dirname; |
| 113 | } |
| 114 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 115 | /** |
| 116 | * kmod_get_userdata: |
| 117 | * @ctx: kmod library context |
| 118 | * |
| 119 | * Retrieve stored data pointer from library context. This might be useful |
Lucas De Marchi | be5a6de | 2011-12-14 03:44:38 -0200 | [diff] [blame] | 120 | * to access from callbacks. |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 121 | * |
| 122 | * Returns: stored userdata |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 123 | */ |
Lucas De Marchi | 6d17755 | 2011-11-23 11:52:30 -0200 | [diff] [blame] | 124 | KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 125 | { |
| 126 | if (ctx == NULL) |
| 127 | return NULL; |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 128 | return (void *)ctx->userdata; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * kmod_set_userdata: |
| 133 | * @ctx: kmod library context |
| 134 | * @userdata: data pointer |
| 135 | * |
| 136 | * Store custom @userdata in the library context. |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 137 | */ |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 138 | KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 139 | { |
| 140 | if (ctx == NULL) |
| 141 | return; |
| 142 | ctx->userdata = userdata; |
| 143 | } |
| 144 | |
| 145 | static int log_priority(const char *priority) |
| 146 | { |
| 147 | char *endptr; |
| 148 | int prio; |
| 149 | |
| 150 | prio = strtol(priority, &endptr, 10); |
| 151 | if (endptr[0] == '\0' || isspace(endptr[0])) |
| 152 | return prio; |
| 153 | if (strncmp(priority, "err", 3) == 0) |
| 154 | return LOG_ERR; |
| 155 | if (strncmp(priority, "info", 4) == 0) |
| 156 | return LOG_INFO; |
| 157 | if (strncmp(priority, "debug", 5) == 0) |
| 158 | return LOG_DEBUG; |
| 159 | return 0; |
| 160 | } |
| 161 | |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 162 | static const char *dirname_default_prefix = "/lib/modules"; |
| 163 | |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 164 | static char *get_kernel_release(const char *dirname) |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 165 | { |
| 166 | struct utsname u; |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 167 | char *p; |
| 168 | |
| 169 | if (dirname != NULL) |
| 170 | return strdup(dirname); |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 171 | |
| 172 | if (uname(&u) < 0) |
| 173 | return NULL; |
| 174 | |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 175 | if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0) |
| 176 | return NULL; |
| 177 | |
| 178 | return p; |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 179 | } |
| 180 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 181 | /** |
| 182 | * kmod_new: |
| 183 | * |
| 184 | * Create kmod library context. This reads the kmod configuration |
| 185 | * and fills in the default values. |
| 186 | * |
| 187 | * The initial refcount is 1, and needs to be decremented to |
| 188 | * release the resources of the kmod library context. |
| 189 | * |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 190 | * @dirname: what to consider as linux module's directory, if NULL |
Lucas De Marchi | be5a6de | 2011-12-14 03:44:38 -0200 | [diff] [blame] | 191 | * defaults to /lib/modules/`uname -r`. |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 192 | * @config_paths: ordered array of paths (directories or files) where |
Lucas De Marchi | be5a6de | 2011-12-14 03:44:38 -0200 | [diff] [blame] | 193 | * to load from user-defined configuration parameters such as |
| 194 | * alias, blacklists, commands (install, remove). If |
| 195 | * NULL defaults to /run/modprobe.d, /etc/modprobe.d and |
| 196 | * /lib/modprobe.d. Give an empty vector if configuration should |
| 197 | * not be read. This array must be null terminated. |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 198 | * |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 199 | * Returns: a new kmod library context |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 200 | */ |
Lucas De Marchi | c35347f | 2011-12-12 10:48:02 -0200 | [diff] [blame] | 201 | KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname, |
| 202 | const char * const *config_paths) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 203 | { |
| 204 | const char *env; |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 205 | struct kmod_ctx *ctx; |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 206 | int err; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 207 | |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 208 | ctx = calloc(1, sizeof(struct kmod_ctx)); |
| 209 | if (!ctx) |
| 210 | return NULL; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 211 | |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 212 | ctx->refcount = 1; |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 213 | ctx->log_fn = log_filep; |
| 214 | ctx->log_data = stderr; |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 215 | ctx->log_priority = LOG_ERR; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 216 | |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 217 | ctx->dirname = get_kernel_release(dirname); |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 218 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 219 | /* environment overwrites config */ |
| 220 | env = getenv("KMOD_LOG"); |
| 221 | if (env != NULL) |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 222 | kmod_set_log_priority(ctx, log_priority(env)); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 223 | |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 224 | if (config_paths == NULL) |
| 225 | config_paths = default_config_paths; |
| 226 | err = kmod_config_new(ctx, &ctx->config, config_paths); |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 227 | if (err < 0) { |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 228 | ERR(ctx, "could not create config\n"); |
| 229 | goto fail; |
| 230 | } |
| 231 | |
| 232 | ctx->modules_by_name = kmod_hash_new(KMOD_HASH_SIZE, NULL); |
| 233 | if (ctx->modules_by_name == NULL) { |
| 234 | ERR(ctx, "could not create by-name hash\n"); |
| 235 | goto fail; |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 236 | } |
Lucas De Marchi | 7c2ab35 | 2011-11-29 18:07:43 -0200 | [diff] [blame] | 237 | |
Lucas De Marchi | ae6df84 | 2011-11-25 01:05:30 -0200 | [diff] [blame] | 238 | INFO(ctx, "ctx %p created\n", ctx); |
| 239 | DBG(ctx, "log_priority=%d\n", ctx->log_priority); |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 240 | |
| 241 | return ctx; |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 242 | |
| 243 | fail: |
| 244 | free(ctx->modules_by_name); |
| 245 | free(ctx->dirname); |
| 246 | free(ctx); |
| 247 | return NULL; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /** |
| 251 | * kmod_ref: |
| 252 | * @ctx: kmod library context |
| 253 | * |
| 254 | * Take a reference of the kmod library context. |
| 255 | * |
| 256 | * Returns: the passed kmod library context |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 257 | */ |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 258 | KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx) |
| 259 | { |
| 260 | if (ctx == NULL) |
| 261 | return NULL; |
| 262 | ctx->refcount++; |
| 263 | return ctx; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * kmod_unref: |
| 268 | * @ctx: kmod library context |
| 269 | * |
| 270 | * Drop a reference of the kmod library context. If the refcount |
| 271 | * reaches zero, the resources of the context will be released. |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 272 | */ |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 273 | KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx) |
| 274 | { |
| 275 | if (ctx == NULL) |
| 276 | return NULL; |
Lucas De Marchi | 4d1e689 | 2011-11-24 15:41:48 -0200 | [diff] [blame] | 277 | |
| 278 | if (--ctx->refcount > 0) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 279 | return ctx; |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 280 | |
Lucas De Marchi | ae6df84 | 2011-11-25 01:05:30 -0200 | [diff] [blame] | 281 | INFO(ctx, "context %p released\n", ctx); |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 282 | |
| 283 | kmod_unload_resources(ctx); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 284 | kmod_hash_free(ctx->modules_by_name); |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 285 | free(ctx->dirname); |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 286 | if (ctx->config) |
| 287 | kmod_config_free(ctx->config); |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 288 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 289 | free(ctx); |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * kmod_set_log_fn: |
| 295 | * @ctx: kmod library context |
| 296 | * @log_fn: function to be called for logging messages |
| 297 | * |
| 298 | * The built-in logging writes to stderr. It can be |
| 299 | * overridden by a custom function, to plug log messages |
| 300 | * into the user's logging functionality. |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 301 | */ |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 302 | KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx, |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 303 | void (*log_fn)(void *data, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 304 | int priority, const char *file, |
| 305 | int line, const char *fn, |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 306 | const char *format, va_list args), |
| 307 | const void *data) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 308 | { |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 309 | if (ctx == NULL) |
| 310 | return; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 311 | ctx->log_fn = log_fn; |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 312 | ctx->log_data = (void *)data; |
Lucas De Marchi | ae6df84 | 2011-11-25 01:05:30 -0200 | [diff] [blame] | 313 | INFO(ctx, "custom logging function %p registered\n", log_fn); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
| 317 | * kmod_get_log_priority: |
| 318 | * @ctx: kmod library context |
| 319 | * |
| 320 | * Returns: the current logging priority |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 321 | */ |
Lucas De Marchi | 6d17755 | 2011-11-23 11:52:30 -0200 | [diff] [blame] | 322 | KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 323 | { |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 324 | if (ctx == NULL) |
| 325 | return -1; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 326 | return ctx->log_priority; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * kmod_set_log_priority: |
| 331 | * @ctx: kmod library context |
| 332 | * @priority: the new logging priority |
| 333 | * |
| 334 | * Set the current logging priority. The value controls which messages |
| 335 | * are logged. |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 336 | */ |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 337 | KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority) |
| 338 | { |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 339 | if (ctx == NULL) |
| 340 | return; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 341 | ctx->log_priority = priority; |
| 342 | } |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 343 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 344 | struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx, |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 345 | const char *key) |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 346 | { |
| 347 | struct kmod_module *mod; |
| 348 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 349 | mod = kmod_hash_find(ctx->modules_by_name, key); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 350 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 351 | DBG(ctx, "get module name='%s' found=%p\n", key, mod); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 352 | |
| 353 | return mod; |
| 354 | } |
| 355 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 356 | void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, |
| 357 | const char *key) |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 358 | { |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 359 | DBG(ctx, "add %p key='%s'\n", mod, key); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 360 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 361 | kmod_hash_add(ctx->modules_by_name, key, mod); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 362 | } |
| 363 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 364 | void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod, |
| 365 | const char *key) |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 366 | { |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 367 | DBG(ctx, "del %p key='%s'\n", mod, key); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 368 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 369 | kmod_hash_del(ctx->modules_by_name, key); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 370 | } |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 371 | |
Lucas De Marchi | a009482 | 2011-12-02 09:53:31 -0200 | [diff] [blame] | 372 | static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx, |
Lucas De Marchi | 810803d | 2011-12-09 16:06:04 -0200 | [diff] [blame] | 373 | enum kmod_index index_number, |
Lucas De Marchi | 7b30f4f | 2011-12-01 16:25:37 -0200 | [diff] [blame] | 374 | const char *name, |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 375 | struct kmod_list **list) |
| 376 | { |
Lucas De Marchi | 6f1bc6e | 2011-12-02 10:02:05 -0200 | [diff] [blame] | 377 | int err, nmatch = 0; |
Lucas De Marchi | 0fbdfef | 2011-12-02 09:56:22 -0200 | [diff] [blame] | 378 | struct index_file *idx; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 379 | struct index_value *realnames, *realname; |
| 380 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 381 | if (ctx->indexes[index_number] != NULL) { |
Gustavo Sverzut Barbieri | 3e245be | 2011-12-10 09:28:42 -0200 | [diff] [blame] | 382 | DBG(ctx, "use mmaped index '%s' for name=%s\n", |
| 383 | index_files[index_number], name); |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 384 | realnames = index_mm_searchwild(ctx->indexes[index_number], |
| 385 | name); |
| 386 | } else{ |
| 387 | char fn[PATH_MAX]; |
| 388 | |
Gustavo Sverzut Barbieri | 3b20995 | 2011-12-10 09:21:03 -0200 | [diff] [blame] | 389 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
Lucas De Marchi | 810803d | 2011-12-09 16:06:04 -0200 | [diff] [blame] | 390 | index_files[index_number]); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 391 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 392 | DBG(ctx, "file=%s name=%s\n", fn, name); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 393 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 394 | idx = index_file_open(fn); |
| 395 | if (idx == NULL) |
| 396 | return -ENOSYS; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 397 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 398 | realnames = index_searchwild(idx, name); |
| 399 | index_file_close(idx); |
| 400 | } |
Lucas De Marchi | 4272d08 | 2011-12-09 15:33:37 -0200 | [diff] [blame] | 401 | |
Ulisses Furquim | a955f71 | 2011-12-15 19:17:49 -0200 | [diff] [blame] | 402 | for (realname = realnames; realname; realname = realname->next) { |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 403 | struct kmod_module *mod; |
| 404 | |
Lucas De Marchi | ee3b3ff | 2011-12-13 14:20:48 -0200 | [diff] [blame] | 405 | err = kmod_module_new_from_alias(ctx, name, realname->value, &mod); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 406 | if (err < 0) { |
| 407 | ERR(ctx, "%s\n", strerror(-err)); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 408 | goto fail; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | *list = kmod_list_append(*list, mod); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 412 | nmatch++; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 413 | } |
| 414 | |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 415 | index_values_free(realnames); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 416 | return nmatch; |
| 417 | |
| 418 | fail: |
| 419 | *list = kmod_list_remove_n_latest(*list, nmatch); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 420 | return err; |
Lucas De Marchi | 7b30f4f | 2011-12-01 16:25:37 -0200 | [diff] [blame] | 421 | |
| 422 | } |
| 423 | |
Lucas De Marchi | 7b30f4f | 2011-12-01 16:25:37 -0200 | [diff] [blame] | 424 | int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name, |
| 425 | struct kmod_list **list) |
| 426 | { |
| 427 | if (!startswith(name, "symbol:")) |
| 428 | return 0; |
| 429 | |
Lucas De Marchi | 810803d | 2011-12-09 16:06:04 -0200 | [diff] [blame] | 430 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_SYMBOL, name, |
| 431 | list); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 432 | } |
| 433 | |
Lucas De Marchi | 49e61ca | 2011-12-01 16:27:04 -0200 | [diff] [blame] | 434 | int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name, |
| 435 | struct kmod_list **list) |
| 436 | { |
Lucas De Marchi | 810803d | 2011-12-09 16:06:04 -0200 | [diff] [blame] | 437 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_ALIAS, name, |
| 438 | list); |
Lucas De Marchi | 49e61ca | 2011-12-01 16:27:04 -0200 | [diff] [blame] | 439 | } |
| 440 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 441 | char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name) |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 442 | { |
| 443 | struct index_file *idx; |
| 444 | char fn[PATH_MAX]; |
| 445 | char *line; |
| 446 | |
Gustavo Sverzut Barbieri | 8513210 | 2011-12-10 09:26:27 -0200 | [diff] [blame] | 447 | if (ctx->indexes[KMOD_INDEX_DEP]) { |
| 448 | DBG(ctx, "use mmaped index '%s' modname=%s\n", |
| 449 | index_files[KMOD_INDEX_DEP], name); |
| 450 | return index_mm_search(ctx->indexes[KMOD_INDEX_DEP], name); |
| 451 | } |
| 452 | |
Lucas De Marchi | a4a7502 | 2011-12-08 14:56:48 -0200 | [diff] [blame] | 453 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
| 454 | index_files[KMOD_INDEX_DEP]); |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 455 | |
| 456 | DBG(ctx, "file=%s modname=%s\n", fn, name); |
| 457 | |
| 458 | idx = index_file_open(fn); |
| 459 | if (idx == NULL) { |
Gustavo Sverzut Barbieri | d01c67e | 2011-12-11 19:42:02 -0200 | [diff] [blame] | 460 | ERR(ctx, "Could not open moddep file '%s'\n", fn); |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 461 | return NULL; |
| 462 | } |
| 463 | |
| 464 | line = index_search(idx, name); |
| 465 | index_file_close(idx); |
| 466 | |
| 467 | return line; |
| 468 | } |
| 469 | |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 470 | int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name, |
| 471 | struct kmod_list **list) |
| 472 | { |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 473 | char *line; |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 474 | int n = 0; |
| 475 | |
| 476 | /* |
| 477 | * Module names do not contain ':'. Return early if we know it will |
| 478 | * not be found. |
| 479 | */ |
| 480 | if (strchr(name, ':')) |
| 481 | return 0; |
| 482 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 483 | line = kmod_search_moddep(ctx, name); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 484 | if (line != NULL) { |
| 485 | struct kmod_module *mod; |
| 486 | |
| 487 | n = kmod_module_new_from_name(ctx, name, &mod); |
| 488 | if (n < 0) { |
| 489 | ERR(ctx, "%s\n", strerror(-n)); |
| 490 | goto finish; |
| 491 | } |
| 492 | |
| 493 | *list = kmod_list_append(*list, mod); |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 494 | kmod_module_parse_depline(mod, line); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | finish: |
| 498 | free(line); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 499 | |
| 500 | return n; |
| 501 | } |
| 502 | |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 503 | int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, |
| 504 | struct kmod_list **list) |
| 505 | { |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 506 | struct kmod_config *config = ctx->config; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 507 | struct kmod_list *l; |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 508 | int err, nmatch = 0; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 509 | |
| 510 | kmod_list_foreach(l, config->aliases) { |
| 511 | const char *aliasname = kmod_alias_get_name(l); |
| 512 | const char *modname = kmod_alias_get_modname(l); |
| 513 | |
| 514 | if (fnmatch(aliasname, name, 0) == 0) { |
| 515 | struct kmod_module *mod; |
| 516 | |
Lucas De Marchi | ee3b3ff | 2011-12-13 14:20:48 -0200 | [diff] [blame] | 517 | err = kmod_module_new_from_alias(ctx, aliasname, |
| 518 | modname, &mod); |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 519 | if (err < 0) { |
Gustavo Sverzut Barbieri | d01c67e | 2011-12-11 19:42:02 -0200 | [diff] [blame] | 520 | ERR(ctx, "%s\n", strerror(-err)); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 521 | goto fail; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | *list = kmod_list_append(*list, mod); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 525 | nmatch++; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 529 | return nmatch; |
| 530 | |
| 531 | fail: |
| 532 | *list = kmod_list_remove_n_latest(*list, nmatch); |
| 533 | return err; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 534 | } |
Gustavo Sverzut Barbieri | 1487a64 | 2011-12-08 05:17:43 -0200 | [diff] [blame] | 535 | |
Lucas De Marchi | f4fc552 | 2011-12-16 03:57:12 -0200 | [diff] [blame] | 536 | int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name, |
| 537 | struct kmod_list **list) |
| 538 | { |
| 539 | struct kmod_config *config = ctx->config; |
| 540 | struct kmod_list *l, *node; |
| 541 | int err, nmatch = 0; |
| 542 | |
| 543 | kmod_list_foreach(l, config->install_commands) { |
| 544 | const char *modname = kmod_command_get_modname(l); |
| 545 | |
| 546 | if (streq(modname, name)) { |
| 547 | const char *cmd = kmod_command_get_command(l); |
| 548 | struct kmod_module *mod; |
| 549 | |
| 550 | err = kmod_module_new_from_name(ctx, modname, &mod); |
| 551 | if (err < 0) { |
| 552 | ERR(ctx, "%s\n", strerror(-err)); |
| 553 | return err; |
| 554 | } |
| 555 | |
| 556 | node = kmod_list_append(*list, mod); |
| 557 | if (node == NULL) { |
| 558 | ERR(ctx, "out of memory\n"); |
| 559 | return -ENOMEM; |
| 560 | } |
| 561 | |
| 562 | *list = node; |
| 563 | nmatch = 1; |
| 564 | |
| 565 | kmod_module_set_install_commands(mod, cmd); |
| 566 | |
| 567 | /* |
| 568 | * match only the first one, like modprobe from |
| 569 | * module-init-tools does |
| 570 | */ |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (nmatch) |
| 576 | return nmatch; |
| 577 | |
| 578 | kmod_list_foreach(l, config->remove_commands) { |
| 579 | const char *modname = kmod_command_get_modname(l); |
| 580 | |
| 581 | if (streq(modname, name)) { |
| 582 | const char *cmd = kmod_command_get_command(l); |
| 583 | struct kmod_module *mod; |
| 584 | |
| 585 | err = kmod_module_new_from_name(ctx, modname, &mod); |
| 586 | if (err < 0) { |
| 587 | ERR(ctx, "%s\n", strerror(-err)); |
| 588 | return err; |
| 589 | } |
| 590 | |
| 591 | node = kmod_list_append(*list, mod); |
| 592 | if (node == NULL) { |
| 593 | ERR(ctx, "out of memory\n"); |
| 594 | return -ENOMEM; |
| 595 | } |
| 596 | |
| 597 | *list = node; |
| 598 | nmatch = 1; |
| 599 | |
| 600 | kmod_module_set_remove_commands(mod, cmd); |
| 601 | |
| 602 | /* |
| 603 | * match only the first one, like modprobe from |
| 604 | * module-init-tools does |
| 605 | */ |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | return nmatch; |
| 611 | } |
| 612 | |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 613 | /** |
| 614 | * kmod_module_get_filtered_blacklist: |
| 615 | * @ctx: kmod library context |
Lucas De Marchi | be5a6de | 2011-12-14 03:44:38 -0200 | [diff] [blame] | 616 | * @input: list of kmod_module to be filtered with blacklist |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 617 | * @output: where to save the new list |
| 618 | * |
| 619 | * Given a list @input, this function filter it out with config's blacklist |
| 620 | * ans save it in @output. |
| 621 | * |
| 622 | * Returns: 0 on success or < 0 otherwise. @output is saved with the updated |
| 623 | * list. |
| 624 | */ |
Lucas De Marchi | c35347f | 2011-12-12 10:48:02 -0200 | [diff] [blame] | 625 | KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx, |
| 626 | const struct kmod_list *input, |
| 627 | struct kmod_list **output) |
Gustavo Sverzut Barbieri | 1487a64 | 2011-12-08 05:17:43 -0200 | [diff] [blame] | 628 | { |
| 629 | const struct kmod_config *config; |
| 630 | const struct kmod_list *li; |
| 631 | |
| 632 | if (ctx == NULL || output == NULL) |
| 633 | return -ENOENT; |
| 634 | |
| 635 | *output = NULL; |
| 636 | if (input == NULL) |
| 637 | return 0; |
| 638 | |
| 639 | config = ctx->config; |
| 640 | kmod_list_foreach(li, input) { |
| 641 | struct kmod_module *mod = li->data; |
| 642 | const struct kmod_list *lb; |
| 643 | struct kmod_list *node; |
| 644 | bool filtered = false; |
| 645 | kmod_list_foreach(lb, config->blacklists) { |
| 646 | const char *name = lb->data; |
| 647 | if (streq(name, kmod_module_get_name(mod))) { |
| 648 | filtered = true; |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | if (filtered) |
| 653 | continue; |
| 654 | |
| 655 | node = kmod_list_append(*output, mod); |
| 656 | if (node == NULL) |
| 657 | goto fail; |
| 658 | *output = node; |
| 659 | kmod_module_ref(mod); |
| 660 | } |
| 661 | return 0; |
| 662 | |
| 663 | fail: |
| 664 | kmod_module_unref_list(*output); |
| 665 | *output = NULL; |
| 666 | return -ENOMEM; |
| 667 | } |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 668 | |
Lucas De Marchi | be5a6de | 2011-12-14 03:44:38 -0200 | [diff] [blame] | 669 | /** |
| 670 | * kmod_load_resources: |
| 671 | * @ctx: kmod library context |
| 672 | * |
| 673 | * Load indexes and keep them open in @ctx. This way it's faster to lookup |
| 674 | * information within the indexes. If this function is not called before a |
| 675 | * search, the necessary index is always opened and closed. |
| 676 | * |
| 677 | * If user will do more than one or two lookups, insertions, deletions, most |
| 678 | * likely it's good to call this function first. Particularly in a daemon like |
| 679 | * udev that on bootup issues hundreds of calls to lookup the index, calling |
| 680 | * this function will speedup the searches. |
| 681 | * |
| 682 | * Returns: 0 on success or < 0 otherwise. |
| 683 | */ |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 684 | KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx) |
| 685 | { |
| 686 | size_t i; |
| 687 | |
| 688 | if (ctx == NULL) |
| 689 | return -ENOENT; |
| 690 | |
| 691 | for (i = 0; i < ARRAY_SIZE(index_files); i++) { |
Lucas De Marchi | 6de8f6e | 2011-12-14 03:58:31 -0200 | [diff] [blame] | 692 | char path[PATH_MAX]; |
Lucas De Marchi | 3e67676 | 2011-12-14 03:53:43 -0200 | [diff] [blame] | 693 | |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 694 | if (ctx->indexes[i] == NULL) { |
Lucas De Marchi | 6de8f6e | 2011-12-14 03:58:31 -0200 | [diff] [blame] | 695 | INFO(ctx, "Index %s already loaded\n", index_files[i]); |
Lucas De Marchi | 3e67676 | 2011-12-14 03:53:43 -0200 | [diff] [blame] | 696 | continue; |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 697 | } |
Lucas De Marchi | 3e67676 | 2011-12-14 03:53:43 -0200 | [diff] [blame] | 698 | |
Lucas De Marchi | 6de8f6e | 2011-12-14 03:58:31 -0200 | [diff] [blame] | 699 | snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, |
| 700 | index_files[i]); |
| 701 | ctx->indexes[i] = index_mm_open(ctx, path, true); |
Lucas De Marchi | 3e67676 | 2011-12-14 03:53:43 -0200 | [diff] [blame] | 702 | if (ctx->indexes[i] == NULL) |
| 703 | goto fail; |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | return 0; |
| 707 | |
| 708 | fail: |
| 709 | kmod_unload_resources(ctx); |
| 710 | return -ENOMEM; |
| 711 | } |
| 712 | |
Lucas De Marchi | be5a6de | 2011-12-14 03:44:38 -0200 | [diff] [blame] | 713 | /** |
| 714 | * kmod_unload_resources: |
| 715 | * @ctx: kmod library context |
| 716 | * |
| 717 | * Unload all the indexes. This will free the resources to maintain the index |
| 718 | * open and all subsequent searches will need to open and close the index. |
| 719 | * |
| 720 | * User is free to call kmod_load_resources() and kmod_unload_resources() as |
| 721 | * many times as wanted during the lifecycle of @ctx. For example, if a daemon |
| 722 | * knows that when starting up it will lookup a lot of modules, it could call |
| 723 | * kmod_load_resources() and after the first burst of searches is gone, it |
| 724 | * could free the resources by calling kmod_unload_resources(). |
| 725 | * |
| 726 | * Returns: 0 on success or < 0 otherwise. |
| 727 | */ |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 728 | KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx) |
| 729 | { |
| 730 | size_t i; |
| 731 | |
| 732 | if (ctx == NULL) |
| 733 | return; |
| 734 | |
| 735 | for (i = 0; i < ARRAY_SIZE(index_files); i++) { |
| 736 | if (ctx->indexes[i] != NULL) { |
| 737 | index_mm_close(ctx->indexes[i]); |
| 738 | ctx->indexes[i] = NULL; |
| 739 | } |
| 740 | } |
| 741 | } |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 742 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 743 | const struct kmod_list *kmod_get_options(const struct kmod_ctx *ctx) |
| 744 | { |
| 745 | return ctx->config->options; |
| 746 | } |
| 747 | |
| 748 | const struct kmod_list *kmod_get_install_commands(const struct kmod_ctx *ctx) |
| 749 | { |
| 750 | return ctx->config->install_commands; |
| 751 | } |
| 752 | |
| 753 | const struct kmod_list *kmod_get_remove_commands(const struct kmod_ctx *ctx) |
| 754 | { |
| 755 | return ctx->config->remove_commands; |
| 756 | } |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame^] | 757 | |
| 758 | const struct kmod_list *kmod_get_softdeps(const struct kmod_ctx *ctx) |
| 759 | { |
| 760 | return ctx->config->softdeps; |
| 761 | } |