]>
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 | |
cb451f35 LDM |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or (at your option) any later version. | |
586fc304 LDM |
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 | */ | |
ecd40ee4 LDM |
20 | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <stddef.h> | |
24 | #include <stdarg.h> | |
1eb2ef69 | 25 | #include <limits.h> |
ecd40ee4 LDM |
26 | #include <unistd.h> |
27 | #include <errno.h> | |
7f3eb0cc | 28 | #include <fnmatch.h> |
ecd40ee4 LDM |
29 | #include <string.h> |
30 | #include <ctype.h> | |
221631d5 | 31 | #include <sys/utsname.h> |
ecd40ee4 | 32 | |
586fc304 LDM |
33 | #include "libkmod.h" |
34 | #include "libkmod-private.h" | |
9ba6f57b | 35 | #include "libkmod-index.h" |
ecd40ee4 | 36 | |
fd186ae9 LDM |
37 | #define KMOD_HASH_SIZE (256) |
38 | #define KMOD_LRU_MAX (128) | |
39 | ||
ecd40ee4 | 40 | /** |
586fc304 LDM |
41 | * SECTION:libkmod |
42 | * @short_description: libkmod context | |
ecd40ee4 LDM |
43 | * |
44 | * The context contains the default values for the library user, | |
45 | * and is passed to all library operations. | |
46 | */ | |
47 | ||
a4a75029 LDM |
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 | ||
cb8d4d3e GSB |
61 | static const char *default_config_paths[] = { |
62 | "/run/modprobe.d", | |
a308abec KS |
63 | SYSCONFDIR "/modprobe.d", |
64 | ROOTPREFIX "/lib/modprobe.d", | |
cb8d4d3e GSB |
65 | NULL |
66 | }; | |
67 | ||
ecd40ee4 | 68 | /** |
586fc304 | 69 | * kmod_ctx: |
ecd40ee4 LDM |
70 | * |
71 | * Opaque object representing the library context. | |
72 | */ | |
586fc304 | 73 | struct kmod_ctx { |
ecd40ee4 | 74 | int refcount; |
8d3f3ef8 | 75 | int log_priority; |
1bdd951e | 76 | void (*log_fn)(void *data, |
e4351b05 LDM |
77 | int priority, const char *file, int line, |
78 | const char *fn, const char *format, va_list args); | |
1bdd951e | 79 | void *log_data; |
1ce08a56 GSB |
80 | const void *userdata; |
81 | char *dirname; | |
d13e606f | 82 | struct kmod_config *config; |
822913d7 | 83 | struct hash *modules_by_name; |
33bb69b9 | 84 | struct index_mm *indexes[_KMOD_INDEX_LAST]; |
9fd58f30 | 85 | unsigned long long indexes_stamp[_KMOD_INDEX_LAST]; |
ecd40ee4 LDM |
86 | }; |
87 | ||
1bdd951e | 88 | void kmod_log(const struct kmod_ctx *ctx, |
e4351b05 LDM |
89 | int priority, const char *file, int line, const char *fn, |
90 | const char *format, ...) | |
ecd40ee4 LDM |
91 | { |
92 | va_list args; | |
93 | ||
e5c60f1c GSB |
94 | if (ctx->log_fn == NULL) |
95 | return; | |
96 | ||
ecd40ee4 | 97 | va_start(args, format); |
1bdd951e | 98 | ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args); |
ecd40ee4 LDM |
99 | va_end(args); |
100 | } | |
101 | ||
1bdd951e | 102 | static void log_filep(void *data, |
e4351b05 LDM |
103 | int priority, const char *file, int line, |
104 | const char *fn, const char *format, va_list args) | |
ecd40ee4 | 105 | { |
1bdd951e GSB |
106 | FILE *fp = data; |
107 | fprintf(fp, "libkmod: %s: ", fn); | |
108 | vfprintf(fp, format, args); | |
ecd40ee4 LDM |
109 | } |
110 | ||
1ce08a56 | 111 | const char *kmod_get_dirname(const struct kmod_ctx *ctx) |
221631d5 LDM |
112 | { |
113 | return ctx->dirname; | |
114 | } | |
115 | ||
ecd40ee4 | 116 | /** |
586fc304 LDM |
117 | * kmod_get_userdata: |
118 | * @ctx: kmod library context | |
ecd40ee4 LDM |
119 | * |
120 | * Retrieve stored data pointer from library context. This might be useful | |
be5a6dea | 121 | * to access from callbacks. |
ecd40ee4 LDM |
122 | * |
123 | * Returns: stored userdata | |
54ba8b34 | 124 | */ |
6d177553 | 125 | KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx) |
ecd40ee4 LDM |
126 | { |
127 | if (ctx == NULL) | |
128 | return NULL; | |
1ce08a56 | 129 | return (void *)ctx->userdata; |
ecd40ee4 LDM |
130 | } |
131 | ||
132 | /** | |
586fc304 LDM |
133 | * kmod_set_userdata: |
134 | * @ctx: kmod library context | |
ecd40ee4 LDM |
135 | * @userdata: data pointer |
136 | * | |
137 | * Store custom @userdata in the library context. | |
54ba8b34 | 138 | */ |
1ce08a56 | 139 | KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata) |
ecd40ee4 LDM |
140 | { |
141 | if (ctx == NULL) | |
142 | return; | |
143 | ctx->userdata = userdata; | |
144 | } | |
145 | ||
146 | static int log_priority(const char *priority) | |
147 | { | |
148 | char *endptr; | |
149 | int prio; | |
150 | ||
151 | prio = strtol(priority, &endptr, 10); | |
152 | if (endptr[0] == '\0' || isspace(endptr[0])) | |
153 | return prio; | |
154 | if (strncmp(priority, "err", 3) == 0) | |
155 | return LOG_ERR; | |
156 | if (strncmp(priority, "info", 4) == 0) | |
157 | return LOG_INFO; | |
158 | if (strncmp(priority, "debug", 5) == 0) | |
159 | return LOG_DEBUG; | |
160 | return 0; | |
161 | } | |
162 | ||
a308abec | 163 | static const char *dirname_default_prefix = ROOTPREFIX "/lib/modules"; |
904c63aa | 164 | |
1ce08a56 | 165 | static char *get_kernel_release(const char *dirname) |
221631d5 LDM |
166 | { |
167 | struct utsname u; | |
904c63aa LDM |
168 | char *p; |
169 | ||
170 | if (dirname != NULL) | |
171 | return strdup(dirname); | |
221631d5 LDM |
172 | |
173 | if (uname(&u) < 0) | |
174 | return NULL; | |
175 | ||
904c63aa LDM |
176 | if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0) |
177 | return NULL; | |
178 | ||
179 | return p; | |
221631d5 LDM |
180 | } |
181 | ||
ecd40ee4 | 182 | /** |
586fc304 | 183 | * kmod_new: |
ecd40ee4 | 184 | * |
586fc304 | 185 | * Create kmod library context. This reads the kmod configuration |
ecd40ee4 LDM |
186 | * and fills in the default values. |
187 | * | |
188 | * The initial refcount is 1, and needs to be decremented to | |
586fc304 | 189 | * release the resources of the kmod library context. |
ecd40ee4 | 190 | * |
cb8d4d3e | 191 | * @dirname: what to consider as linux module's directory, if NULL |
a308abec | 192 | * defaults to $rootprefix/lib/modules/`uname -r`. |
cb8d4d3e | 193 | * @config_paths: ordered array of paths (directories or files) where |
be5a6dea LDM |
194 | * to load from user-defined configuration parameters such as |
195 | * alias, blacklists, commands (install, remove). If | |
196 | * NULL defaults to /run/modprobe.d, /etc/modprobe.d and | |
a308abec | 197 | * $rootprefix/lib/modprobe.d. Give an empty vector if configuration should |
be5a6dea | 198 | * not be read. This array must be null terminated. |
cb8d4d3e | 199 | * |
586fc304 | 200 | * Returns: a new kmod library context |
54ba8b34 | 201 | */ |
c35347f1 LDM |
202 | KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname, |
203 | const char * const *config_paths) | |
ecd40ee4 LDM |
204 | { |
205 | const char *env; | |
52a7704f | 206 | struct kmod_ctx *ctx; |
d13e606f | 207 | int err; |
ecd40ee4 | 208 | |
52a7704f LDM |
209 | ctx = calloc(1, sizeof(struct kmod_ctx)); |
210 | if (!ctx) | |
211 | return NULL; | |
ecd40ee4 | 212 | |
52a7704f | 213 | ctx->refcount = 1; |
1bdd951e GSB |
214 | ctx->log_fn = log_filep; |
215 | ctx->log_data = stderr; | |
52a7704f | 216 | ctx->log_priority = LOG_ERR; |
ecd40ee4 | 217 | |
904c63aa | 218 | ctx->dirname = get_kernel_release(dirname); |
221631d5 | 219 | |
ecd40ee4 | 220 | /* environment overwrites config */ |
586fc304 | 221 | env = getenv("KMOD_LOG"); |
ecd40ee4 | 222 | if (env != NULL) |
52a7704f | 223 | kmod_set_log_priority(ctx, log_priority(env)); |
ecd40ee4 | 224 | |
cb8d4d3e GSB |
225 | if (config_paths == NULL) |
226 | config_paths = default_config_paths; | |
227 | err = kmod_config_new(ctx, &ctx->config, config_paths); | |
d13e606f | 228 | if (err < 0) { |
fd186ae9 LDM |
229 | ERR(ctx, "could not create config\n"); |
230 | goto fail; | |
231 | } | |
232 | ||
822913d7 | 233 | ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL); |
fd186ae9 LDM |
234 | if (ctx->modules_by_name == NULL) { |
235 | ERR(ctx, "could not create by-name hash\n"); | |
236 | goto fail; | |
d13e606f | 237 | } |
7c2ab358 | 238 | |
ae6df84a LDM |
239 | INFO(ctx, "ctx %p created\n", ctx); |
240 | DBG(ctx, "log_priority=%d\n", ctx->log_priority); | |
52a7704f LDM |
241 | |
242 | return ctx; | |
fd186ae9 LDM |
243 | |
244 | fail: | |
245 | free(ctx->modules_by_name); | |
246 | free(ctx->dirname); | |
247 | free(ctx); | |
248 | return NULL; | |
ecd40ee4 LDM |
249 | } |
250 | ||
251 | /** | |
586fc304 LDM |
252 | * kmod_ref: |
253 | * @ctx: kmod library context | |
ecd40ee4 | 254 | * |
586fc304 | 255 | * Take a reference of the kmod library context. |
ecd40ee4 | 256 | * |
586fc304 | 257 | * Returns: the passed kmod library context |
54ba8b34 | 258 | */ |
586fc304 | 259 | KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx) |
ecd40ee4 LDM |
260 | { |
261 | if (ctx == NULL) | |
262 | return NULL; | |
263 | ctx->refcount++; | |
264 | return ctx; | |
265 | } | |
266 | ||
267 | /** | |
586fc304 LDM |
268 | * kmod_unref: |
269 | * @ctx: kmod library context | |
ecd40ee4 | 270 | * |
586fc304 | 271 | * Drop a reference of the kmod library context. If the refcount |
ecd40ee4 | 272 | * reaches zero, the resources of the context will be released. |
54ba8b34 | 273 | */ |
586fc304 | 274 | KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx) |
ecd40ee4 LDM |
275 | { |
276 | if (ctx == NULL) | |
277 | return NULL; | |
4d1e689a LDM |
278 | |
279 | if (--ctx->refcount > 0) | |
ecd40ee4 | 280 | return ctx; |
33bb69b9 | 281 | |
ae6df84a | 282 | INFO(ctx, "context %p released\n", ctx); |
33bb69b9 LDM |
283 | |
284 | kmod_unload_resources(ctx); | |
822913d7 | 285 | hash_free(ctx->modules_by_name); |
1ce08a56 | 286 | free(ctx->dirname); |
d13e606f GSB |
287 | if (ctx->config) |
288 | kmod_config_free(ctx->config); | |
33bb69b9 | 289 | |
ecd40ee4 LDM |
290 | free(ctx); |
291 | return NULL; | |
292 | } | |
293 | ||
294 | /** | |
586fc304 LDM |
295 | * kmod_set_log_fn: |
296 | * @ctx: kmod library context | |
ecd40ee4 LDM |
297 | * @log_fn: function to be called for logging messages |
298 | * | |
299 | * The built-in logging writes to stderr. It can be | |
300 | * overridden by a custom function, to plug log messages | |
301 | * into the user's logging functionality. | |
54ba8b34 | 302 | */ |
586fc304 | 303 | KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx, |
1bdd951e | 304 | void (*log_fn)(void *data, |
e4351b05 LDM |
305 | int priority, const char *file, |
306 | int line, const char *fn, | |
1bdd951e GSB |
307 | const char *format, va_list args), |
308 | const void *data) | |
ecd40ee4 | 309 | { |
e5c60f1c GSB |
310 | if (ctx == NULL) |
311 | return; | |
ecd40ee4 | 312 | ctx->log_fn = log_fn; |
1bdd951e | 313 | ctx->log_data = (void *)data; |
ae6df84a | 314 | INFO(ctx, "custom logging function %p registered\n", log_fn); |
ecd40ee4 LDM |
315 | } |
316 | ||
317 | /** | |
586fc304 LDM |
318 | * kmod_get_log_priority: |
319 | * @ctx: kmod library context | |
ecd40ee4 LDM |
320 | * |
321 | * Returns: the current logging priority | |
54ba8b34 | 322 | */ |
6d177553 | 323 | KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx) |
ecd40ee4 | 324 | { |
e5c60f1c GSB |
325 | if (ctx == NULL) |
326 | return -1; | |
ecd40ee4 LDM |
327 | return ctx->log_priority; |
328 | } | |
329 | ||
330 | /** | |
586fc304 LDM |
331 | * kmod_set_log_priority: |
332 | * @ctx: kmod library context | |
ecd40ee4 LDM |
333 | * @priority: the new logging priority |
334 | * | |
335 | * Set the current logging priority. The value controls which messages | |
336 | * are logged. | |
54ba8b34 | 337 | */ |
586fc304 | 338 | KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority) |
ecd40ee4 | 339 | { |
e5c60f1c GSB |
340 | if (ctx == NULL) |
341 | return; | |
ecd40ee4 LDM |
342 | ctx->log_priority = priority; |
343 | } | |
7f3eb0cc | 344 | |
fd186ae9 | 345 | struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx, |
8bdeca11 | 346 | const char *key) |
fd186ae9 LDM |
347 | { |
348 | struct kmod_module *mod; | |
349 | ||
822913d7 | 350 | mod = hash_find(ctx->modules_by_name, key); |
fd186ae9 | 351 | |
8bdeca11 | 352 | DBG(ctx, "get module name='%s' found=%p\n", key, mod); |
fd186ae9 LDM |
353 | |
354 | return mod; | |
355 | } | |
356 | ||
8bdeca11 LDM |
357 | void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, |
358 | const char *key) | |
fd186ae9 | 359 | { |
8bdeca11 | 360 | DBG(ctx, "add %p key='%s'\n", mod, key); |
fd186ae9 | 361 | |
822913d7 | 362 | hash_add(ctx->modules_by_name, key, mod); |
fd186ae9 LDM |
363 | } |
364 | ||
8bdeca11 LDM |
365 | void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod, |
366 | const char *key) | |
fd186ae9 | 367 | { |
8bdeca11 | 368 | DBG(ctx, "del %p key='%s'\n", mod, key); |
fd186ae9 | 369 | |
822913d7 | 370 | hash_del(ctx->modules_by_name, key); |
fd186ae9 | 371 | } |
9ba6f57b | 372 | |
a009482c | 373 | static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx, |
810803db | 374 | enum kmod_index index_number, |
7b30f4f4 | 375 | const char *name, |
9ba6f57b LDM |
376 | struct kmod_list **list) |
377 | { | |
6f1bc6e3 | 378 | int err, nmatch = 0; |
0fbdfef3 | 379 | struct index_file *idx; |
9ba6f57b LDM |
380 | struct index_value *realnames, *realname; |
381 | ||
65a84f55 | 382 | if (ctx->indexes[index_number] != NULL) { |
3e245be1 GSB |
383 | DBG(ctx, "use mmaped index '%s' for name=%s\n", |
384 | index_files[index_number], name); | |
65a84f55 LDM |
385 | realnames = index_mm_searchwild(ctx->indexes[index_number], |
386 | name); | |
9fd58f30 | 387 | } else { |
65a84f55 LDM |
388 | char fn[PATH_MAX]; |
389 | ||
3b209959 | 390 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
810803db | 391 | index_files[index_number]); |
9ba6f57b | 392 | |
65a84f55 | 393 | DBG(ctx, "file=%s name=%s\n", fn, name); |
9ba6f57b | 394 | |
65a84f55 LDM |
395 | idx = index_file_open(fn); |
396 | if (idx == NULL) | |
397 | return -ENOSYS; | |
9ba6f57b | 398 | |
65a84f55 LDM |
399 | realnames = index_searchwild(idx, name); |
400 | index_file_close(idx); | |
401 | } | |
4272d087 | 402 | |
a955f71f | 403 | for (realname = realnames; realname; realname = realname->next) { |
9ba6f57b LDM |
404 | struct kmod_module *mod; |
405 | ||
ee3b3ff2 | 406 | err = kmod_module_new_from_alias(ctx, name, realname->value, &mod); |
9ba6f57b LDM |
407 | if (err < 0) { |
408 | ERR(ctx, "%s\n", strerror(-err)); | |
23fc91c6 | 409 | goto fail; |
9ba6f57b LDM |
410 | } |
411 | ||
412 | *list = kmod_list_append(*list, mod); | |
23fc91c6 | 413 | nmatch++; |
9ba6f57b LDM |
414 | } |
415 | ||
9ba6f57b | 416 | index_values_free(realnames); |
23fc91c6 LDM |
417 | return nmatch; |
418 | ||
419 | fail: | |
420 | *list = kmod_list_remove_n_latest(*list, nmatch); | |
9ba6f57b | 421 | return err; |
7b30f4f4 LDM |
422 | |
423 | } | |
424 | ||
7b30f4f4 LDM |
425 | int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name, |
426 | struct kmod_list **list) | |
427 | { | |
0c010fae | 428 | if (!strstartswith(name, "symbol:")) |
7b30f4f4 LDM |
429 | return 0; |
430 | ||
810803db LDM |
431 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_SYMBOL, name, |
432 | list); | |
9ba6f57b LDM |
433 | } |
434 | ||
49e61ca3 LDM |
435 | int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name, |
436 | struct kmod_list **list) | |
437 | { | |
810803db LDM |
438 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_ALIAS, name, |
439 | list); | |
49e61ca3 LDM |
440 | } |
441 | ||
671d4894 | 442 | char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name) |
1eb2ef69 LDM |
443 | { |
444 | struct index_file *idx; | |
445 | char fn[PATH_MAX]; | |
446 | char *line; | |
447 | ||
85132101 GSB |
448 | if (ctx->indexes[KMOD_INDEX_DEP]) { |
449 | DBG(ctx, "use mmaped index '%s' modname=%s\n", | |
450 | index_files[KMOD_INDEX_DEP], name); | |
451 | return index_mm_search(ctx->indexes[KMOD_INDEX_DEP], name); | |
452 | } | |
453 | ||
a4a75029 LDM |
454 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
455 | index_files[KMOD_INDEX_DEP]); | |
1eb2ef69 LDM |
456 | |
457 | DBG(ctx, "file=%s modname=%s\n", fn, name); | |
458 | ||
459 | idx = index_file_open(fn); | |
460 | if (idx == NULL) { | |
d01c67e3 | 461 | ERR(ctx, "Could not open moddep file '%s'\n", fn); |
1eb2ef69 LDM |
462 | return NULL; |
463 | } | |
464 | ||
465 | line = index_search(idx, name); | |
466 | index_file_close(idx); | |
467 | ||
468 | return line; | |
469 | } | |
470 | ||
64700e47 LDM |
471 | int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name, |
472 | struct kmod_list **list) | |
473 | { | |
1eb2ef69 | 474 | char *line; |
64700e47 LDM |
475 | int n = 0; |
476 | ||
477 | /* | |
478 | * Module names do not contain ':'. Return early if we know it will | |
479 | * not be found. | |
480 | */ | |
481 | if (strchr(name, ':')) | |
482 | return 0; | |
483 | ||
671d4894 | 484 | line = kmod_search_moddep(ctx, name); |
64700e47 LDM |
485 | if (line != NULL) { |
486 | struct kmod_module *mod; | |
487 | ||
488 | n = kmod_module_new_from_name(ctx, name, &mod); | |
489 | if (n < 0) { | |
490 | ERR(ctx, "%s\n", strerror(-n)); | |
491 | goto finish; | |
492 | } | |
493 | ||
494 | *list = kmod_list_append(*list, mod); | |
671d4894 | 495 | kmod_module_parse_depline(mod, line); |
64700e47 LDM |
496 | } |
497 | ||
498 | finish: | |
499 | free(line); | |
64700e47 LDM |
500 | |
501 | return n; | |
502 | } | |
503 | ||
7f3eb0cc LDM |
504 | int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, |
505 | struct kmod_list **list) | |
506 | { | |
d13e606f | 507 | struct kmod_config *config = ctx->config; |
7f3eb0cc | 508 | struct kmod_list *l; |
23fc91c6 | 509 | int err, nmatch = 0; |
7f3eb0cc LDM |
510 | |
511 | kmod_list_foreach(l, config->aliases) { | |
512 | const char *aliasname = kmod_alias_get_name(l); | |
513 | const char *modname = kmod_alias_get_modname(l); | |
514 | ||
515 | if (fnmatch(aliasname, name, 0) == 0) { | |
516 | struct kmod_module *mod; | |
517 | ||
ee3b3ff2 LDM |
518 | err = kmod_module_new_from_alias(ctx, aliasname, |
519 | modname, &mod); | |
7f3eb0cc | 520 | if (err < 0) { |
d01c67e3 | 521 | ERR(ctx, "%s\n", strerror(-err)); |
23fc91c6 | 522 | goto fail; |
7f3eb0cc LDM |
523 | } |
524 | ||
525 | *list = kmod_list_append(*list, mod); | |
23fc91c6 | 526 | nmatch++; |
7f3eb0cc LDM |
527 | } |
528 | } | |
529 | ||
23fc91c6 LDM |
530 | return nmatch; |
531 | ||
532 | fail: | |
533 | *list = kmod_list_remove_n_latest(*list, nmatch); | |
534 | return err; | |
7f3eb0cc | 535 | } |
1487a64f | 536 | |
f4fc5523 LDM |
537 | int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name, |
538 | struct kmod_list **list) | |
539 | { | |
540 | struct kmod_config *config = ctx->config; | |
541 | struct kmod_list *l, *node; | |
542 | int err, nmatch = 0; | |
543 | ||
544 | kmod_list_foreach(l, config->install_commands) { | |
545 | const char *modname = kmod_command_get_modname(l); | |
546 | ||
547 | if (streq(modname, name)) { | |
548 | const char *cmd = kmod_command_get_command(l); | |
549 | struct kmod_module *mod; | |
550 | ||
551 | err = kmod_module_new_from_name(ctx, modname, &mod); | |
552 | if (err < 0) { | |
553 | ERR(ctx, "%s\n", strerror(-err)); | |
554 | return err; | |
555 | } | |
556 | ||
557 | node = kmod_list_append(*list, mod); | |
558 | if (node == NULL) { | |
559 | ERR(ctx, "out of memory\n"); | |
560 | return -ENOMEM; | |
561 | } | |
562 | ||
563 | *list = node; | |
564 | nmatch = 1; | |
565 | ||
566 | kmod_module_set_install_commands(mod, cmd); | |
567 | ||
568 | /* | |
569 | * match only the first one, like modprobe from | |
570 | * module-init-tools does | |
571 | */ | |
572 | break; | |
573 | } | |
574 | } | |
575 | ||
576 | if (nmatch) | |
577 | return nmatch; | |
578 | ||
579 | kmod_list_foreach(l, config->remove_commands) { | |
580 | const char *modname = kmod_command_get_modname(l); | |
581 | ||
582 | if (streq(modname, name)) { | |
583 | const char *cmd = kmod_command_get_command(l); | |
584 | struct kmod_module *mod; | |
585 | ||
586 | err = kmod_module_new_from_name(ctx, modname, &mod); | |
587 | if (err < 0) { | |
588 | ERR(ctx, "%s\n", strerror(-err)); | |
589 | return err; | |
590 | } | |
591 | ||
592 | node = kmod_list_append(*list, mod); | |
593 | if (node == NULL) { | |
594 | ERR(ctx, "out of memory\n"); | |
595 | return -ENOMEM; | |
596 | } | |
597 | ||
598 | *list = node; | |
599 | nmatch = 1; | |
600 | ||
601 | kmod_module_set_remove_commands(mod, cmd); | |
602 | ||
603 | /* | |
604 | * match only the first one, like modprobe from | |
605 | * module-init-tools does | |
606 | */ | |
607 | break; | |
608 | } | |
609 | } | |
610 | ||
611 | return nmatch; | |
612 | } | |
613 | ||
be5a6dea LDM |
614 | /** |
615 | * kmod_load_resources: | |
616 | * @ctx: kmod library context | |
617 | * | |
618 | * Load indexes and keep them open in @ctx. This way it's faster to lookup | |
619 | * information within the indexes. If this function is not called before a | |
620 | * search, the necessary index is always opened and closed. | |
621 | * | |
622 | * If user will do more than one or two lookups, insertions, deletions, most | |
623 | * likely it's good to call this function first. Particularly in a daemon like | |
624 | * udev that on bootup issues hundreds of calls to lookup the index, calling | |
625 | * this function will speedup the searches. | |
626 | * | |
627 | * Returns: 0 on success or < 0 otherwise. | |
628 | */ | |
33bb69b9 LDM |
629 | KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx) |
630 | { | |
631 | size_t i; | |
632 | ||
633 | if (ctx == NULL) | |
634 | return -ENOENT; | |
635 | ||
2f76fda2 | 636 | for (i = 0; i < _KMOD_INDEX_LAST; i++) { |
6de8f6e9 | 637 | char path[PATH_MAX]; |
3e676766 | 638 | |
16ca3666 | 639 | if (ctx->indexes[i] != NULL) { |
6de8f6e9 | 640 | INFO(ctx, "Index %s already loaded\n", index_files[i]); |
3e676766 LDM |
641 | continue; |
642 | } | |
79d57fcb | 643 | |
6de8f6e9 LDM |
644 | snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, |
645 | index_files[i]); | |
9fd58f30 LDM |
646 | ctx->indexes[i] = index_mm_open(ctx, path, true, |
647 | &ctx->indexes_stamp[i]); | |
3e676766 LDM |
648 | if (ctx->indexes[i] == NULL) |
649 | goto fail; | |
33bb69b9 LDM |
650 | } |
651 | ||
652 | return 0; | |
653 | ||
654 | fail: | |
655 | kmod_unload_resources(ctx); | |
656 | return -ENOMEM; | |
657 | } | |
658 | ||
be5a6dea LDM |
659 | /** |
660 | * kmod_unload_resources: | |
661 | * @ctx: kmod library context | |
662 | * | |
663 | * Unload all the indexes. This will free the resources to maintain the index | |
664 | * open and all subsequent searches will need to open and close the index. | |
665 | * | |
666 | * User is free to call kmod_load_resources() and kmod_unload_resources() as | |
667 | * many times as wanted during the lifecycle of @ctx. For example, if a daemon | |
668 | * knows that when starting up it will lookup a lot of modules, it could call | |
669 | * kmod_load_resources() and after the first burst of searches is gone, it | |
670 | * could free the resources by calling kmod_unload_resources(). | |
671 | * | |
672 | * Returns: 0 on success or < 0 otherwise. | |
673 | */ | |
33bb69b9 LDM |
674 | KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx) |
675 | { | |
676 | size_t i; | |
677 | ||
678 | if (ctx == NULL) | |
679 | return; | |
680 | ||
2f76fda2 | 681 | for (i = 0; i < _KMOD_INDEX_LAST; i++) { |
33bb69b9 LDM |
682 | if (ctx->indexes[i] != NULL) { |
683 | index_mm_close(ctx->indexes[i]); | |
684 | ctx->indexes[i] = NULL; | |
9fd58f30 | 685 | ctx->indexes_stamp[i] = 0; |
33bb69b9 LDM |
686 | } |
687 | } | |
688 | } | |
bd3f5535 | 689 | |
c1c9c446 LDM |
690 | const struct kmod_list *kmod_get_blacklists(const struct kmod_ctx *ctx) |
691 | { | |
692 | return ctx->config->blacklists; | |
693 | } | |
694 | ||
bd3f5535 GSB |
695 | const struct kmod_list *kmod_get_options(const struct kmod_ctx *ctx) |
696 | { | |
697 | return ctx->config->options; | |
698 | } | |
699 | ||
700 | const struct kmod_list *kmod_get_install_commands(const struct kmod_ctx *ctx) | |
701 | { | |
702 | return ctx->config->install_commands; | |
703 | } | |
704 | ||
705 | const struct kmod_list *kmod_get_remove_commands(const struct kmod_ctx *ctx) | |
706 | { | |
707 | return ctx->config->remove_commands; | |
708 | } | |
1c522600 GSB |
709 | |
710 | const struct kmod_list *kmod_get_softdeps(const struct kmod_ctx *ctx) | |
711 | { | |
712 | return ctx->config->softdeps; | |
713 | } |