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 |
| 8 | * License as published by the Free Software Foundation version 2.1. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stddef.h> |
| 23 | #include <stdarg.h> |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 24 | #include <limits.h> |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | #include <errno.h> |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 27 | #include <fnmatch.h> |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <ctype.h> |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 30 | #include <sys/utsname.h> |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 31 | |
| 32 | #include "libkmod.h" |
| 33 | #include "libkmod-private.h" |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 34 | #include "libkmod-index.h" |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 35 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 36 | #define KMOD_HASH_SIZE (256) |
| 37 | #define KMOD_LRU_MAX (128) |
| 38 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 39 | /** |
| 40 | * SECTION:libkmod |
| 41 | * @short_description: libkmod context |
| 42 | * |
| 43 | * The context contains the default values for the library user, |
| 44 | * and is passed to all library operations. |
| 45 | */ |
| 46 | |
Lucas De Marchi | a4a7502 | 2011-12-08 14:56:48 -0200 | [diff] [blame] | 47 | enum kmod_index { |
| 48 | KMOD_INDEX_DEP = 0, |
| 49 | KMOD_INDEX_ALIAS, |
| 50 | KMOD_INDEX_SYMBOL, |
| 51 | _KMOD_INDEX_LAST, |
| 52 | }; |
| 53 | |
| 54 | static const char* index_files[] = { |
| 55 | [KMOD_INDEX_DEP] = "modules.dep", |
| 56 | [KMOD_INDEX_ALIAS] = "modules.alias", |
| 57 | [KMOD_INDEX_SYMBOL] = "modules.symbols", |
| 58 | }; |
| 59 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 60 | /** |
| 61 | * kmod_ctx: |
| 62 | * |
| 63 | * Opaque object representing the library context. |
| 64 | */ |
| 65 | struct kmod_ctx { |
| 66 | int refcount; |
Gustavo Sverzut Barbieri | 8d3f3ef | 2011-12-02 21:10:24 -0200 | [diff] [blame] | 67 | int log_priority; |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 68 | void (*log_fn)(void *data, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 69 | int priority, const char *file, int line, |
| 70 | const char *fn, const char *format, va_list args); |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 71 | void *log_data; |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 72 | const void *userdata; |
| 73 | char *dirname; |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 74 | struct kmod_config *config; |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 75 | struct kmod_hash *modules_by_name; |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 76 | struct index_mm *indexes[_KMOD_INDEX_LAST]; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 77 | }; |
| 78 | |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 79 | void kmod_log(const struct kmod_ctx *ctx, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 80 | int priority, const char *file, int line, const char *fn, |
| 81 | const char *format, ...) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 82 | { |
| 83 | va_list args; |
| 84 | |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 85 | if (ctx->log_fn == NULL) |
| 86 | return; |
| 87 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 88 | va_start(args, format); |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 89 | 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] | 90 | va_end(args); |
| 91 | } |
| 92 | |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 93 | static void log_filep(void *data, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 94 | int priority, const char *file, int line, |
| 95 | const char *fn, const char *format, va_list args) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 96 | { |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 97 | FILE *fp = data; |
| 98 | fprintf(fp, "libkmod: %s: ", fn); |
| 99 | vfprintf(fp, format, args); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 100 | } |
| 101 | |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 102 | const char *kmod_get_dirname(const struct kmod_ctx *ctx) |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 103 | { |
| 104 | return ctx->dirname; |
| 105 | } |
| 106 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 107 | /** |
| 108 | * kmod_get_userdata: |
| 109 | * @ctx: kmod library context |
| 110 | * |
| 111 | * Retrieve stored data pointer from library context. This might be useful |
| 112 | * to access from callbacks like a custom logging function. |
| 113 | * |
| 114 | * Returns: stored userdata |
| 115 | **/ |
Lucas De Marchi | 6d17755 | 2011-11-23 11:52:30 -0200 | [diff] [blame] | 116 | KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 117 | { |
| 118 | if (ctx == NULL) |
| 119 | return NULL; |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 120 | return (void *)ctx->userdata; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * kmod_set_userdata: |
| 125 | * @ctx: kmod library context |
| 126 | * @userdata: data pointer |
| 127 | * |
| 128 | * Store custom @userdata in the library context. |
| 129 | **/ |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 130 | 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] | 131 | { |
| 132 | if (ctx == NULL) |
| 133 | return; |
| 134 | ctx->userdata = userdata; |
| 135 | } |
| 136 | |
| 137 | static int log_priority(const char *priority) |
| 138 | { |
| 139 | char *endptr; |
| 140 | int prio; |
| 141 | |
| 142 | prio = strtol(priority, &endptr, 10); |
| 143 | if (endptr[0] == '\0' || isspace(endptr[0])) |
| 144 | return prio; |
| 145 | if (strncmp(priority, "err", 3) == 0) |
| 146 | return LOG_ERR; |
| 147 | if (strncmp(priority, "info", 4) == 0) |
| 148 | return LOG_INFO; |
| 149 | if (strncmp(priority, "debug", 5) == 0) |
| 150 | return LOG_DEBUG; |
| 151 | return 0; |
| 152 | } |
| 153 | |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 154 | static const char *dirname_default_prefix = "/lib/modules"; |
| 155 | |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 156 | static char *get_kernel_release(const char *dirname) |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 157 | { |
| 158 | struct utsname u; |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 159 | char *p; |
| 160 | |
| 161 | if (dirname != NULL) |
| 162 | return strdup(dirname); |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 163 | |
| 164 | if (uname(&u) < 0) |
| 165 | return NULL; |
| 166 | |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 167 | if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0) |
| 168 | return NULL; |
| 169 | |
| 170 | return p; |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 171 | } |
| 172 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 173 | /** |
| 174 | * kmod_new: |
| 175 | * |
| 176 | * Create kmod library context. This reads the kmod configuration |
| 177 | * and fills in the default values. |
| 178 | * |
| 179 | * The initial refcount is 1, and needs to be decremented to |
| 180 | * release the resources of the kmod library context. |
| 181 | * |
| 182 | * Returns: a new kmod library context |
| 183 | **/ |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 184 | KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 185 | { |
| 186 | const char *env; |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 187 | struct kmod_ctx *ctx; |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 188 | int err; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 189 | |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 190 | ctx = calloc(1, sizeof(struct kmod_ctx)); |
| 191 | if (!ctx) |
| 192 | return NULL; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 193 | |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 194 | ctx->refcount = 1; |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 195 | ctx->log_fn = log_filep; |
| 196 | ctx->log_data = stderr; |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 197 | ctx->log_priority = LOG_ERR; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 198 | |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 199 | ctx->dirname = get_kernel_release(dirname); |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 200 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 201 | /* environment overwrites config */ |
| 202 | env = getenv("KMOD_LOG"); |
| 203 | if (env != NULL) |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 204 | kmod_set_log_priority(ctx, log_priority(env)); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 205 | |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 206 | err = kmod_config_new(ctx, &ctx->config); |
| 207 | if (err < 0) { |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 208 | ERR(ctx, "could not create config\n"); |
| 209 | goto fail; |
| 210 | } |
| 211 | |
| 212 | ctx->modules_by_name = kmod_hash_new(KMOD_HASH_SIZE, NULL); |
| 213 | if (ctx->modules_by_name == NULL) { |
| 214 | ERR(ctx, "could not create by-name hash\n"); |
| 215 | goto fail; |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 216 | } |
Lucas De Marchi | 7c2ab35 | 2011-11-29 18:07:43 -0200 | [diff] [blame] | 217 | |
Lucas De Marchi | ae6df84 | 2011-11-25 01:05:30 -0200 | [diff] [blame] | 218 | INFO(ctx, "ctx %p created\n", ctx); |
| 219 | DBG(ctx, "log_priority=%d\n", ctx->log_priority); |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 220 | |
| 221 | return ctx; |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 222 | |
| 223 | fail: |
| 224 | free(ctx->modules_by_name); |
| 225 | free(ctx->dirname); |
| 226 | free(ctx); |
| 227 | return NULL; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
| 231 | * kmod_ref: |
| 232 | * @ctx: kmod library context |
| 233 | * |
| 234 | * Take a reference of the kmod library context. |
| 235 | * |
| 236 | * Returns: the passed kmod library context |
| 237 | **/ |
| 238 | KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx) |
| 239 | { |
| 240 | if (ctx == NULL) |
| 241 | return NULL; |
| 242 | ctx->refcount++; |
| 243 | return ctx; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * kmod_unref: |
| 248 | * @ctx: kmod library context |
| 249 | * |
| 250 | * Drop a reference of the kmod library context. If the refcount |
| 251 | * reaches zero, the resources of the context will be released. |
| 252 | * |
| 253 | **/ |
| 254 | KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx) |
| 255 | { |
| 256 | if (ctx == NULL) |
| 257 | return NULL; |
Lucas De Marchi | 4d1e689 | 2011-11-24 15:41:48 -0200 | [diff] [blame] | 258 | |
| 259 | if (--ctx->refcount > 0) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 260 | return ctx; |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 261 | |
Lucas De Marchi | ae6df84 | 2011-11-25 01:05:30 -0200 | [diff] [blame] | 262 | INFO(ctx, "context %p released\n", ctx); |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 263 | |
| 264 | kmod_unload_resources(ctx); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 265 | kmod_hash_free(ctx->modules_by_name); |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 266 | free(ctx->dirname); |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 267 | if (ctx->config) |
| 268 | kmod_config_free(ctx->config); |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 269 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 270 | free(ctx); |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * kmod_set_log_fn: |
| 276 | * @ctx: kmod library context |
| 277 | * @log_fn: function to be called for logging messages |
| 278 | * |
| 279 | * The built-in logging writes to stderr. It can be |
| 280 | * overridden by a custom function, to plug log messages |
| 281 | * into the user's logging functionality. |
| 282 | * |
| 283 | **/ |
| 284 | KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx, |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 285 | void (*log_fn)(void *data, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 286 | int priority, const char *file, |
| 287 | int line, const char *fn, |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 288 | const char *format, va_list args), |
| 289 | const void *data) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 290 | { |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 291 | if (ctx == NULL) |
| 292 | return; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 293 | ctx->log_fn = log_fn; |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 294 | ctx->log_data = (void *)data; |
Lucas De Marchi | ae6df84 | 2011-11-25 01:05:30 -0200 | [diff] [blame] | 295 | INFO(ctx, "custom logging function %p registered\n", log_fn); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
| 299 | * kmod_get_log_priority: |
| 300 | * @ctx: kmod library context |
| 301 | * |
| 302 | * Returns: the current logging priority |
| 303 | **/ |
Lucas De Marchi | 6d17755 | 2011-11-23 11:52:30 -0200 | [diff] [blame] | 304 | 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] | 305 | { |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 306 | if (ctx == NULL) |
| 307 | return -1; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 308 | return ctx->log_priority; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * kmod_set_log_priority: |
| 313 | * @ctx: kmod library context |
| 314 | * @priority: the new logging priority |
| 315 | * |
| 316 | * Set the current logging priority. The value controls which messages |
| 317 | * are logged. |
| 318 | **/ |
| 319 | KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority) |
| 320 | { |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 321 | if (ctx == NULL) |
| 322 | return; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 323 | ctx->log_priority = priority; |
| 324 | } |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 325 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 326 | struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx, |
| 327 | const char *name) |
| 328 | { |
| 329 | struct kmod_module *mod; |
| 330 | |
| 331 | mod = kmod_hash_find(ctx->modules_by_name, name); |
| 332 | |
| 333 | DBG(ctx, "get module name='%s' found=%p\n", name, mod); |
| 334 | |
| 335 | return mod; |
| 336 | } |
| 337 | |
| 338 | void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod) |
| 339 | { |
| 340 | const char *name = kmod_module_get_name(mod); |
| 341 | |
| 342 | DBG(ctx, "add %p name='%s'\n", mod, name); |
| 343 | |
| 344 | kmod_hash_add(ctx->modules_by_name, name, mod); |
| 345 | } |
| 346 | |
| 347 | void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod) |
| 348 | { |
| 349 | const char *name = kmod_module_get_name(mod); |
| 350 | |
| 351 | DBG(ctx, "del %p name='%s'\n", mod, name); |
| 352 | |
| 353 | kmod_hash_del(ctx->modules_by_name, name); |
| 354 | } |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 355 | |
Lucas De Marchi | a009482 | 2011-12-02 09:53:31 -0200 | [diff] [blame] | 356 | 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] | 357 | enum kmod_index index_number, |
Lucas De Marchi | 7b30f4f | 2011-12-01 16:25:37 -0200 | [diff] [blame] | 358 | const char *name, |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 359 | struct kmod_list **list) |
| 360 | { |
Lucas De Marchi | 6f1bc6e | 2011-12-02 10:02:05 -0200 | [diff] [blame] | 361 | int err, nmatch = 0; |
Lucas De Marchi | 0fbdfef | 2011-12-02 09:56:22 -0200 | [diff] [blame] | 362 | struct index_file *idx; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 363 | struct index_value *realnames, *realname; |
| 364 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 365 | if (ctx->indexes[index_number] != NULL) { |
| 366 | realnames = index_mm_searchwild(ctx->indexes[index_number], |
| 367 | name); |
| 368 | } else{ |
| 369 | char fn[PATH_MAX]; |
| 370 | |
Gustavo Sverzut Barbieri | 3b20995 | 2011-12-10 09:21:03 -0200 | [diff] [blame] | 371 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
Lucas De Marchi | 810803d | 2011-12-09 16:06:04 -0200 | [diff] [blame] | 372 | index_files[index_number]); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 373 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 374 | DBG(ctx, "file=%s name=%s\n", fn, name); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 375 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 376 | idx = index_file_open(fn); |
| 377 | if (idx == NULL) |
| 378 | return -ENOSYS; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 379 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 380 | realnames = index_searchwild(idx, name); |
| 381 | index_file_close(idx); |
| 382 | } |
Lucas De Marchi | 4272d08 | 2011-12-09 15:33:37 -0200 | [diff] [blame] | 383 | |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 384 | for (realname = realnames; realname; realname = realnames->next) { |
| 385 | struct kmod_module *mod; |
| 386 | |
| 387 | err = kmod_module_new_from_name(ctx, realname->value, &mod); |
| 388 | if (err < 0) { |
| 389 | ERR(ctx, "%s\n", strerror(-err)); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 390 | goto fail; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | *list = kmod_list_append(*list, mod); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 394 | nmatch++; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 395 | } |
| 396 | |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 397 | index_values_free(realnames); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 398 | return nmatch; |
| 399 | |
| 400 | fail: |
| 401 | *list = kmod_list_remove_n_latest(*list, nmatch); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 402 | return err; |
Lucas De Marchi | 7b30f4f | 2011-12-01 16:25:37 -0200 | [diff] [blame] | 403 | |
| 404 | } |
| 405 | |
Lucas De Marchi | 7b30f4f | 2011-12-01 16:25:37 -0200 | [diff] [blame] | 406 | int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name, |
| 407 | struct kmod_list **list) |
| 408 | { |
| 409 | if (!startswith(name, "symbol:")) |
| 410 | return 0; |
| 411 | |
Lucas De Marchi | 810803d | 2011-12-09 16:06:04 -0200 | [diff] [blame] | 412 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_SYMBOL, name, |
| 413 | list); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 414 | } |
| 415 | |
Lucas De Marchi | 49e61ca | 2011-12-01 16:27:04 -0200 | [diff] [blame] | 416 | int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name, |
| 417 | struct kmod_list **list) |
| 418 | { |
Lucas De Marchi | 810803d | 2011-12-09 16:06:04 -0200 | [diff] [blame] | 419 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_ALIAS, name, |
| 420 | list); |
Lucas De Marchi | 49e61ca | 2011-12-01 16:27:04 -0200 | [diff] [blame] | 421 | } |
| 422 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 423 | char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name) |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 424 | { |
| 425 | struct index_file *idx; |
| 426 | char fn[PATH_MAX]; |
| 427 | char *line; |
| 428 | |
Gustavo Sverzut Barbieri | 8513210 | 2011-12-10 09:26:27 -0200 | [diff] [blame^] | 429 | if (ctx->indexes[KMOD_INDEX_DEP]) { |
| 430 | DBG(ctx, "use mmaped index '%s' modname=%s\n", |
| 431 | index_files[KMOD_INDEX_DEP], name); |
| 432 | return index_mm_search(ctx->indexes[KMOD_INDEX_DEP], name); |
| 433 | } |
| 434 | |
Lucas De Marchi | a4a7502 | 2011-12-08 14:56:48 -0200 | [diff] [blame] | 435 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
| 436 | index_files[KMOD_INDEX_DEP]); |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 437 | |
| 438 | DBG(ctx, "file=%s modname=%s\n", fn, name); |
| 439 | |
| 440 | idx = index_file_open(fn); |
| 441 | if (idx == NULL) { |
| 442 | ERR(ctx, "Could not open moddep file '%s'", fn); |
| 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | line = index_search(idx, name); |
| 447 | index_file_close(idx); |
| 448 | |
| 449 | return line; |
| 450 | } |
| 451 | |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 452 | int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name, |
| 453 | struct kmod_list **list) |
| 454 | { |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 455 | char *line; |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 456 | int n = 0; |
| 457 | |
| 458 | /* |
| 459 | * Module names do not contain ':'. Return early if we know it will |
| 460 | * not be found. |
| 461 | */ |
| 462 | if (strchr(name, ':')) |
| 463 | return 0; |
| 464 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 465 | line = kmod_search_moddep(ctx, name); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 466 | if (line != NULL) { |
| 467 | struct kmod_module *mod; |
| 468 | |
| 469 | n = kmod_module_new_from_name(ctx, name, &mod); |
| 470 | if (n < 0) { |
| 471 | ERR(ctx, "%s\n", strerror(-n)); |
| 472 | goto finish; |
| 473 | } |
| 474 | |
| 475 | *list = kmod_list_append(*list, mod); |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 476 | kmod_module_parse_depline(mod, line); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | finish: |
| 480 | free(line); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 481 | |
| 482 | return n; |
| 483 | } |
| 484 | |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 485 | int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, |
| 486 | struct kmod_list **list) |
| 487 | { |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 488 | struct kmod_config *config = ctx->config; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 489 | struct kmod_list *l; |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 490 | int err, nmatch = 0; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 491 | |
| 492 | kmod_list_foreach(l, config->aliases) { |
| 493 | const char *aliasname = kmod_alias_get_name(l); |
| 494 | const char *modname = kmod_alias_get_modname(l); |
| 495 | |
| 496 | if (fnmatch(aliasname, name, 0) == 0) { |
| 497 | struct kmod_module *mod; |
| 498 | |
| 499 | err = kmod_module_new_from_name(ctx, modname, &mod); |
| 500 | if (err < 0) { |
| 501 | ERR(ctx, "%s", strerror(-err)); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 502 | goto fail; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | *list = kmod_list_append(*list, mod); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 506 | nmatch++; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 510 | return nmatch; |
| 511 | |
| 512 | fail: |
| 513 | *list = kmod_list_remove_n_latest(*list, nmatch); |
| 514 | return err; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 515 | } |
Gustavo Sverzut Barbieri | 1487a64 | 2011-12-08 05:17:43 -0200 | [diff] [blame] | 516 | |
Gustavo Sverzut Barbieri | 1487a64 | 2011-12-08 05:17:43 -0200 | [diff] [blame] | 517 | KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx, const struct kmod_list *input, struct kmod_list **output) |
| 518 | { |
| 519 | const struct kmod_config *config; |
| 520 | const struct kmod_list *li; |
| 521 | |
| 522 | if (ctx == NULL || output == NULL) |
| 523 | return -ENOENT; |
| 524 | |
| 525 | *output = NULL; |
| 526 | if (input == NULL) |
| 527 | return 0; |
| 528 | |
| 529 | config = ctx->config; |
| 530 | kmod_list_foreach(li, input) { |
| 531 | struct kmod_module *mod = li->data; |
| 532 | const struct kmod_list *lb; |
| 533 | struct kmod_list *node; |
| 534 | bool filtered = false; |
| 535 | kmod_list_foreach(lb, config->blacklists) { |
| 536 | const char *name = lb->data; |
| 537 | if (streq(name, kmod_module_get_name(mod))) { |
| 538 | filtered = true; |
| 539 | break; |
| 540 | } |
| 541 | } |
| 542 | if (filtered) |
| 543 | continue; |
| 544 | |
| 545 | node = kmod_list_append(*output, mod); |
| 546 | if (node == NULL) |
| 547 | goto fail; |
| 548 | *output = node; |
| 549 | kmod_module_ref(mod); |
| 550 | } |
| 551 | return 0; |
| 552 | |
| 553 | fail: |
| 554 | kmod_module_unref_list(*output); |
| 555 | *output = NULL; |
| 556 | return -ENOMEM; |
| 557 | } |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 558 | |
| 559 | KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx) |
| 560 | { |
| 561 | size_t i; |
| 562 | |
| 563 | if (ctx == NULL) |
| 564 | return -ENOENT; |
| 565 | |
| 566 | for (i = 0; i < ARRAY_SIZE(index_files); i++) { |
| 567 | if (ctx->indexes[i] == NULL) { |
| 568 | const char *fn = index_files[i]; |
| 569 | |
| 570 | ctx->indexes[i] = index_mm_open(ctx, fn, true); |
| 571 | if (ctx->indexes[i] == NULL) |
| 572 | goto fail; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | return 0; |
| 577 | |
| 578 | fail: |
| 579 | kmod_unload_resources(ctx); |
| 580 | return -ENOMEM; |
| 581 | } |
| 582 | |
| 583 | KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx) |
| 584 | { |
| 585 | size_t i; |
| 586 | |
| 587 | if (ctx == NULL) |
| 588 | return; |
| 589 | |
| 590 | for (i = 0; i < ARRAY_SIZE(index_files); i++) { |
| 591 | if (ctx->indexes[i] != NULL) { |
| 592 | index_mm_close(ctx->indexes[i]); |
| 593 | ctx->indexes[i] = NULL; |
| 594 | } |
| 595 | } |
| 596 | } |