]>
Commit | Line | Data |
---|---|---|
ecd40ee4 | 1 | /* |
586fc304 LDM |
2 | * libkmod - interface to kernel module operations |
3 | * | |
4 | * Copyright (C) 2011 ProFUSION embedded systems | |
586fc304 LDM |
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 | */ | |
ecd40ee4 LDM |
19 | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <stddef.h> | |
23 | #include <stdarg.h> | |
1eb2ef69 | 24 | #include <limits.h> |
ecd40ee4 LDM |
25 | #include <unistd.h> |
26 | #include <errno.h> | |
7f3eb0cc | 27 | #include <fnmatch.h> |
ecd40ee4 LDM |
28 | #include <string.h> |
29 | #include <ctype.h> | |
221631d5 | 30 | #include <sys/utsname.h> |
ecd40ee4 | 31 | |
586fc304 LDM |
32 | #include "libkmod.h" |
33 | #include "libkmod-private.h" | |
9ba6f57b | 34 | #include "libkmod-index.h" |
ecd40ee4 | 35 | |
fd186ae9 LDM |
36 | #define KMOD_HASH_SIZE (256) |
37 | #define KMOD_LRU_MAX (128) | |
38 | ||
ecd40ee4 | 39 | /** |
586fc304 LDM |
40 | * SECTION:libkmod |
41 | * @short_description: libkmod context | |
ecd40ee4 LDM |
42 | * |
43 | * The context contains the default values for the library user, | |
44 | * and is passed to all library operations. | |
45 | */ | |
46 | ||
a4a75029 LDM |
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 | ||
ecd40ee4 | 60 | /** |
586fc304 | 61 | * kmod_ctx: |
ecd40ee4 LDM |
62 | * |
63 | * Opaque object representing the library context. | |
64 | */ | |
586fc304 | 65 | struct kmod_ctx { |
ecd40ee4 | 66 | int refcount; |
8d3f3ef8 | 67 | int log_priority; |
1bdd951e | 68 | void (*log_fn)(void *data, |
e4351b05 LDM |
69 | int priority, const char *file, int line, |
70 | const char *fn, const char *format, va_list args); | |
1bdd951e | 71 | void *log_data; |
1ce08a56 GSB |
72 | const void *userdata; |
73 | char *dirname; | |
d13e606f | 74 | struct kmod_config *config; |
fd186ae9 | 75 | struct kmod_hash *modules_by_name; |
33bb69b9 | 76 | struct index_mm *indexes[_KMOD_INDEX_LAST]; |
ecd40ee4 LDM |
77 | }; |
78 | ||
1bdd951e | 79 | void kmod_log(const struct kmod_ctx *ctx, |
e4351b05 LDM |
80 | int priority, const char *file, int line, const char *fn, |
81 | const char *format, ...) | |
ecd40ee4 LDM |
82 | { |
83 | va_list args; | |
84 | ||
e5c60f1c GSB |
85 | if (ctx->log_fn == NULL) |
86 | return; | |
87 | ||
ecd40ee4 | 88 | va_start(args, format); |
1bdd951e | 89 | ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args); |
ecd40ee4 LDM |
90 | va_end(args); |
91 | } | |
92 | ||
1bdd951e | 93 | static void log_filep(void *data, |
e4351b05 LDM |
94 | int priority, const char *file, int line, |
95 | const char *fn, const char *format, va_list args) | |
ecd40ee4 | 96 | { |
1bdd951e GSB |
97 | FILE *fp = data; |
98 | fprintf(fp, "libkmod: %s: ", fn); | |
99 | vfprintf(fp, format, args); | |
ecd40ee4 LDM |
100 | } |
101 | ||
1ce08a56 | 102 | const char *kmod_get_dirname(const struct kmod_ctx *ctx) |
221631d5 LDM |
103 | { |
104 | return ctx->dirname; | |
105 | } | |
106 | ||
ecd40ee4 | 107 | /** |
586fc304 LDM |
108 | * kmod_get_userdata: |
109 | * @ctx: kmod library context | |
ecd40ee4 LDM |
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 | **/ | |
6d177553 | 116 | KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx) |
ecd40ee4 LDM |
117 | { |
118 | if (ctx == NULL) | |
119 | return NULL; | |
1ce08a56 | 120 | return (void *)ctx->userdata; |
ecd40ee4 LDM |
121 | } |
122 | ||
123 | /** | |
586fc304 LDM |
124 | * kmod_set_userdata: |
125 | * @ctx: kmod library context | |
ecd40ee4 LDM |
126 | * @userdata: data pointer |
127 | * | |
128 | * Store custom @userdata in the library context. | |
129 | **/ | |
1ce08a56 | 130 | KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata) |
ecd40ee4 LDM |
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 | ||
904c63aa LDM |
154 | static const char *dirname_default_prefix = "/lib/modules"; |
155 | ||
1ce08a56 | 156 | static char *get_kernel_release(const char *dirname) |
221631d5 LDM |
157 | { |
158 | struct utsname u; | |
904c63aa LDM |
159 | char *p; |
160 | ||
161 | if (dirname != NULL) | |
162 | return strdup(dirname); | |
221631d5 LDM |
163 | |
164 | if (uname(&u) < 0) | |
165 | return NULL; | |
166 | ||
904c63aa LDM |
167 | if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0) |
168 | return NULL; | |
169 | ||
170 | return p; | |
221631d5 LDM |
171 | } |
172 | ||
ecd40ee4 | 173 | /** |
586fc304 | 174 | * kmod_new: |
ecd40ee4 | 175 | * |
586fc304 | 176 | * Create kmod library context. This reads the kmod configuration |
ecd40ee4 LDM |
177 | * and fills in the default values. |
178 | * | |
179 | * The initial refcount is 1, and needs to be decremented to | |
586fc304 | 180 | * release the resources of the kmod library context. |
ecd40ee4 | 181 | * |
586fc304 | 182 | * Returns: a new kmod library context |
ecd40ee4 | 183 | **/ |
221631d5 | 184 | KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname) |
ecd40ee4 LDM |
185 | { |
186 | const char *env; | |
52a7704f | 187 | struct kmod_ctx *ctx; |
d13e606f | 188 | int err; |
ecd40ee4 | 189 | |
52a7704f LDM |
190 | ctx = calloc(1, sizeof(struct kmod_ctx)); |
191 | if (!ctx) | |
192 | return NULL; | |
ecd40ee4 | 193 | |
52a7704f | 194 | ctx->refcount = 1; |
1bdd951e GSB |
195 | ctx->log_fn = log_filep; |
196 | ctx->log_data = stderr; | |
52a7704f | 197 | ctx->log_priority = LOG_ERR; |
ecd40ee4 | 198 | |
904c63aa | 199 | ctx->dirname = get_kernel_release(dirname); |
221631d5 | 200 | |
ecd40ee4 | 201 | /* environment overwrites config */ |
586fc304 | 202 | env = getenv("KMOD_LOG"); |
ecd40ee4 | 203 | if (env != NULL) |
52a7704f | 204 | kmod_set_log_priority(ctx, log_priority(env)); |
ecd40ee4 | 205 | |
d13e606f GSB |
206 | err = kmod_config_new(ctx, &ctx->config); |
207 | if (err < 0) { | |
fd186ae9 LDM |
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; | |
d13e606f | 216 | } |
7c2ab358 | 217 | |
ae6df84a LDM |
218 | INFO(ctx, "ctx %p created\n", ctx); |
219 | DBG(ctx, "log_priority=%d\n", ctx->log_priority); | |
52a7704f LDM |
220 | |
221 | return ctx; | |
fd186ae9 LDM |
222 | |
223 | fail: | |
224 | free(ctx->modules_by_name); | |
225 | free(ctx->dirname); | |
226 | free(ctx); | |
227 | return NULL; | |
ecd40ee4 LDM |
228 | } |
229 | ||
230 | /** | |
586fc304 LDM |
231 | * kmod_ref: |
232 | * @ctx: kmod library context | |
ecd40ee4 | 233 | * |
586fc304 | 234 | * Take a reference of the kmod library context. |
ecd40ee4 | 235 | * |
586fc304 | 236 | * Returns: the passed kmod library context |
ecd40ee4 | 237 | **/ |
586fc304 | 238 | KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx) |
ecd40ee4 LDM |
239 | { |
240 | if (ctx == NULL) | |
241 | return NULL; | |
242 | ctx->refcount++; | |
243 | return ctx; | |
244 | } | |
245 | ||
246 | /** | |
586fc304 LDM |
247 | * kmod_unref: |
248 | * @ctx: kmod library context | |
ecd40ee4 | 249 | * |
586fc304 | 250 | * Drop a reference of the kmod library context. If the refcount |
ecd40ee4 LDM |
251 | * reaches zero, the resources of the context will be released. |
252 | * | |
253 | **/ | |
586fc304 | 254 | KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx) |
ecd40ee4 LDM |
255 | { |
256 | if (ctx == NULL) | |
257 | return NULL; | |
4d1e689a LDM |
258 | |
259 | if (--ctx->refcount > 0) | |
ecd40ee4 | 260 | return ctx; |
33bb69b9 | 261 | |
ae6df84a | 262 | INFO(ctx, "context %p released\n", ctx); |
33bb69b9 LDM |
263 | |
264 | kmod_unload_resources(ctx); | |
fd186ae9 | 265 | kmod_hash_free(ctx->modules_by_name); |
1ce08a56 | 266 | free(ctx->dirname); |
d13e606f GSB |
267 | if (ctx->config) |
268 | kmod_config_free(ctx->config); | |
33bb69b9 | 269 | |
ecd40ee4 LDM |
270 | free(ctx); |
271 | return NULL; | |
272 | } | |
273 | ||
274 | /** | |
586fc304 LDM |
275 | * kmod_set_log_fn: |
276 | * @ctx: kmod library context | |
ecd40ee4 LDM |
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 | **/ | |
586fc304 | 284 | KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx, |
1bdd951e | 285 | void (*log_fn)(void *data, |
e4351b05 LDM |
286 | int priority, const char *file, |
287 | int line, const char *fn, | |
1bdd951e GSB |
288 | const char *format, va_list args), |
289 | const void *data) | |
ecd40ee4 | 290 | { |
e5c60f1c GSB |
291 | if (ctx == NULL) |
292 | return; | |
ecd40ee4 | 293 | ctx->log_fn = log_fn; |
1bdd951e | 294 | ctx->log_data = (void *)data; |
ae6df84a | 295 | INFO(ctx, "custom logging function %p registered\n", log_fn); |
ecd40ee4 LDM |
296 | } |
297 | ||
298 | /** | |
586fc304 LDM |
299 | * kmod_get_log_priority: |
300 | * @ctx: kmod library context | |
ecd40ee4 LDM |
301 | * |
302 | * Returns: the current logging priority | |
303 | **/ | |
6d177553 | 304 | KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx) |
ecd40ee4 | 305 | { |
e5c60f1c GSB |
306 | if (ctx == NULL) |
307 | return -1; | |
ecd40ee4 LDM |
308 | return ctx->log_priority; |
309 | } | |
310 | ||
311 | /** | |
586fc304 LDM |
312 | * kmod_set_log_priority: |
313 | * @ctx: kmod library context | |
ecd40ee4 LDM |
314 | * @priority: the new logging priority |
315 | * | |
316 | * Set the current logging priority. The value controls which messages | |
317 | * are logged. | |
318 | **/ | |
586fc304 | 319 | KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority) |
ecd40ee4 | 320 | { |
e5c60f1c GSB |
321 | if (ctx == NULL) |
322 | return; | |
ecd40ee4 LDM |
323 | ctx->log_priority = priority; |
324 | } | |
7f3eb0cc | 325 | |
fd186ae9 LDM |
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 | } | |
9ba6f57b | 355 | |
a009482c LDM |
356 | static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx, |
357 | const char *file, | |
7b30f4f4 | 358 | const char *name, |
9ba6f57b LDM |
359 | struct kmod_list **list) |
360 | { | |
4272d087 | 361 | char fn[PATH_MAX]; |
6f1bc6e3 | 362 | int err, nmatch = 0; |
0fbdfef3 | 363 | struct index_file *idx; |
9ba6f57b LDM |
364 | struct index_value *realnames, *realname; |
365 | ||
4272d087 LDM |
366 | fn[PATH_MAX - 1] = '\0'; |
367 | snprintf(fn, sizeof(fn) - 1, "%s/%s.bin", ctx->dirname, file); | |
9ba6f57b | 368 | |
e915f92a | 369 | DBG(ctx, "file=%s name=%s\n", fn, name); |
9ba6f57b | 370 | |
0fbdfef3 | 371 | idx = index_file_open(fn); |
4272d087 | 372 | if (idx == NULL) |
9ba6f57b | 373 | return -ENOSYS; |
9ba6f57b | 374 | |
0fbdfef3 | 375 | realnames = index_searchwild(idx, name); |
4272d087 LDM |
376 | index_file_close(idx); |
377 | ||
9ba6f57b LDM |
378 | for (realname = realnames; realname; realname = realnames->next) { |
379 | struct kmod_module *mod; | |
380 | ||
381 | err = kmod_module_new_from_name(ctx, realname->value, &mod); | |
382 | if (err < 0) { | |
383 | ERR(ctx, "%s\n", strerror(-err)); | |
23fc91c6 | 384 | goto fail; |
9ba6f57b LDM |
385 | } |
386 | ||
387 | *list = kmod_list_append(*list, mod); | |
23fc91c6 | 388 | nmatch++; |
9ba6f57b LDM |
389 | } |
390 | ||
9ba6f57b | 391 | index_values_free(realnames); |
9ba6f57b | 392 | |
23fc91c6 LDM |
393 | return nmatch; |
394 | ||
395 | fail: | |
396 | *list = kmod_list_remove_n_latest(*list, nmatch); | |
9ba6f57b | 397 | return err; |
7b30f4f4 LDM |
398 | |
399 | } | |
400 | ||
7b30f4f4 LDM |
401 | int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name, |
402 | struct kmod_list **list) | |
403 | { | |
404 | if (!startswith(name, "symbol:")) | |
405 | return 0; | |
406 | ||
a4a75029 LDM |
407 | return kmod_lookup_alias_from_alias_bin(ctx, |
408 | index_files[KMOD_INDEX_SYMBOL], name, list); | |
9ba6f57b LDM |
409 | } |
410 | ||
49e61ca3 LDM |
411 | int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name, |
412 | struct kmod_list **list) | |
413 | { | |
a4a75029 LDM |
414 | return kmod_lookup_alias_from_alias_bin(ctx, |
415 | index_files[KMOD_INDEX_ALIAS], name, list); | |
49e61ca3 LDM |
416 | } |
417 | ||
671d4894 | 418 | char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name) |
1eb2ef69 LDM |
419 | { |
420 | struct index_file *idx; | |
421 | char fn[PATH_MAX]; | |
422 | char *line; | |
423 | ||
a4a75029 LDM |
424 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
425 | index_files[KMOD_INDEX_DEP]); | |
1eb2ef69 LDM |
426 | |
427 | DBG(ctx, "file=%s modname=%s\n", fn, name); | |
428 | ||
d65d71ce LDM |
429 | if (ctx->indexes[KMOD_INDEX_DEP]) |
430 | return index_mm_search(ctx->indexes[KMOD_INDEX_DEP], name); | |
431 | ||
1eb2ef69 LDM |
432 | idx = index_file_open(fn); |
433 | if (idx == NULL) { | |
434 | ERR(ctx, "Could not open moddep file '%s'", fn); | |
435 | return NULL; | |
436 | } | |
437 | ||
438 | line = index_search(idx, name); | |
439 | index_file_close(idx); | |
440 | ||
441 | return line; | |
442 | } | |
443 | ||
64700e47 LDM |
444 | int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name, |
445 | struct kmod_list **list) | |
446 | { | |
1eb2ef69 | 447 | char *line; |
64700e47 LDM |
448 | int n = 0; |
449 | ||
450 | /* | |
451 | * Module names do not contain ':'. Return early if we know it will | |
452 | * not be found. | |
453 | */ | |
454 | if (strchr(name, ':')) | |
455 | return 0; | |
456 | ||
671d4894 | 457 | line = kmod_search_moddep(ctx, name); |
64700e47 LDM |
458 | if (line != NULL) { |
459 | struct kmod_module *mod; | |
460 | ||
461 | n = kmod_module_new_from_name(ctx, name, &mod); | |
462 | if (n < 0) { | |
463 | ERR(ctx, "%s\n", strerror(-n)); | |
464 | goto finish; | |
465 | } | |
466 | ||
467 | *list = kmod_list_append(*list, mod); | |
671d4894 | 468 | kmod_module_parse_depline(mod, line); |
64700e47 LDM |
469 | } |
470 | ||
471 | finish: | |
472 | free(line); | |
64700e47 LDM |
473 | |
474 | return n; | |
475 | } | |
476 | ||
7f3eb0cc LDM |
477 | int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, |
478 | struct kmod_list **list) | |
479 | { | |
d13e606f | 480 | struct kmod_config *config = ctx->config; |
7f3eb0cc | 481 | struct kmod_list *l; |
23fc91c6 | 482 | int err, nmatch = 0; |
7f3eb0cc LDM |
483 | |
484 | kmod_list_foreach(l, config->aliases) { | |
485 | const char *aliasname = kmod_alias_get_name(l); | |
486 | const char *modname = kmod_alias_get_modname(l); | |
487 | ||
488 | if (fnmatch(aliasname, name, 0) == 0) { | |
489 | struct kmod_module *mod; | |
490 | ||
491 | err = kmod_module_new_from_name(ctx, modname, &mod); | |
492 | if (err < 0) { | |
493 | ERR(ctx, "%s", strerror(-err)); | |
23fc91c6 | 494 | goto fail; |
7f3eb0cc LDM |
495 | } |
496 | ||
497 | *list = kmod_list_append(*list, mod); | |
23fc91c6 | 498 | nmatch++; |
7f3eb0cc LDM |
499 | } |
500 | } | |
501 | ||
23fc91c6 LDM |
502 | return nmatch; |
503 | ||
504 | fail: | |
505 | *list = kmod_list_remove_n_latest(*list, nmatch); | |
506 | return err; | |
7f3eb0cc | 507 | } |
1487a64f | 508 | |
1487a64f GSB |
509 | KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx, const struct kmod_list *input, struct kmod_list **output) |
510 | { | |
511 | const struct kmod_config *config; | |
512 | const struct kmod_list *li; | |
513 | ||
514 | if (ctx == NULL || output == NULL) | |
515 | return -ENOENT; | |
516 | ||
517 | *output = NULL; | |
518 | if (input == NULL) | |
519 | return 0; | |
520 | ||
521 | config = ctx->config; | |
522 | kmod_list_foreach(li, input) { | |
523 | struct kmod_module *mod = li->data; | |
524 | const struct kmod_list *lb; | |
525 | struct kmod_list *node; | |
526 | bool filtered = false; | |
527 | kmod_list_foreach(lb, config->blacklists) { | |
528 | const char *name = lb->data; | |
529 | if (streq(name, kmod_module_get_name(mod))) { | |
530 | filtered = true; | |
531 | break; | |
532 | } | |
533 | } | |
534 | if (filtered) | |
535 | continue; | |
536 | ||
537 | node = kmod_list_append(*output, mod); | |
538 | if (node == NULL) | |
539 | goto fail; | |
540 | *output = node; | |
541 | kmod_module_ref(mod); | |
542 | } | |
543 | return 0; | |
544 | ||
545 | fail: | |
546 | kmod_module_unref_list(*output); | |
547 | *output = NULL; | |
548 | return -ENOMEM; | |
549 | } | |
33bb69b9 LDM |
550 | |
551 | KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx) | |
552 | { | |
553 | size_t i; | |
554 | ||
555 | if (ctx == NULL) | |
556 | return -ENOENT; | |
557 | ||
558 | for (i = 0; i < ARRAY_SIZE(index_files); i++) { | |
559 | if (ctx->indexes[i] == NULL) { | |
560 | const char *fn = index_files[i]; | |
561 | ||
562 | ctx->indexes[i] = index_mm_open(ctx, fn, true); | |
563 | if (ctx->indexes[i] == NULL) | |
564 | goto fail; | |
565 | } | |
566 | } | |
567 | ||
568 | return 0; | |
569 | ||
570 | fail: | |
571 | kmod_unload_resources(ctx); | |
572 | return -ENOMEM; | |
573 | } | |
574 | ||
575 | KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx) | |
576 | { | |
577 | size_t i; | |
578 | ||
579 | if (ctx == NULL) | |
580 | return; | |
581 | ||
582 | for (i = 0; i < ARRAY_SIZE(index_files); i++) { | |
583 | if (ctx->indexes[i] != NULL) { | |
584 | index_mm_close(ctx->indexes[i]); | |
585 | ctx->indexes[i] = NULL; | |
586 | } | |
587 | } | |
588 | } |