]>
Commit | Line | Data |
---|---|---|
ecd40ee4 | 1 | /* |
586fc304 LDM |
2 | * libkmod - interface to kernel module operations |
3 | * | |
e6b0e49b | 4 | * Copyright (C) 2011-2013 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 | |
dea2dfee | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
586fc304 | 18 | */ |
ecd40ee4 | 19 | |
ee1d188f | 20 | #include <assert.h> |
c2e4286b | 21 | #include <ctype.h> |
ecd40ee4 | 22 | #include <errno.h> |
7f3eb0cc | 23 | #include <fnmatch.h> |
c2e4286b LDM |
24 | #include <limits.h> |
25 | #include <stdarg.h> | |
26 | #include <stddef.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
ecd40ee4 | 29 | #include <string.h> |
c2e4286b | 30 | #include <unistd.h> |
c4dc3ca8 | 31 | #include <sys/stat.h> |
c2e4286b | 32 | #include <sys/utsname.h> |
ecd40ee4 | 33 | |
0db718ed | 34 | #include <shared/hash.h> |
96573a02 LDM |
35 | #include <shared/util.h> |
36 | ||
586fc304 | 37 | #include "libkmod.h" |
83b855a6 | 38 | #include "libkmod-internal.h" |
9ba6f57b | 39 | #include "libkmod-index.h" |
ecd40ee4 | 40 | |
fd186ae9 LDM |
41 | #define KMOD_HASH_SIZE (256) |
42 | #define KMOD_LRU_MAX (128) | |
3805274b | 43 | #define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_BUILTIN + 1 |
fd186ae9 | 44 | |
ecd40ee4 | 45 | /** |
586fc304 LDM |
46 | * SECTION:libkmod |
47 | * @short_description: libkmod context | |
ecd40ee4 LDM |
48 | * |
49 | * The context contains the default values for the library user, | |
50 | * and is passed to all library operations. | |
51 | */ | |
52 | ||
63be91cb LDM |
53 | static struct _index_files { |
54 | const char *fn; | |
55 | const char *prefix; | |
56 | } index_files[] = { | |
b08314f7 LDM |
57 | [KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep", .prefix = "" }, |
58 | [KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .prefix = "alias " }, | |
59 | [KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .prefix = "alias "}, | |
3805274b | 60 | [KMOD_INDEX_MODULES_BUILTIN] = { .fn = "modules.builtin", .prefix = ""}, |
a4a75029 LDM |
61 | }; |
62 | ||
cb8d4d3e | 63 | static const char *default_config_paths[] = { |
a308abec | 64 | SYSCONFDIR "/modprobe.d", |
436da1e9 | 65 | "/run/modprobe.d", |
c5b37dba | 66 | "/lib/modprobe.d", |
cb8d4d3e GSB |
67 | NULL |
68 | }; | |
69 | ||
ecd40ee4 | 70 | /** |
586fc304 | 71 | * kmod_ctx: |
ecd40ee4 LDM |
72 | * |
73 | * Opaque object representing the library context. | |
74 | */ | |
586fc304 | 75 | struct kmod_ctx { |
ecd40ee4 | 76 | int refcount; |
8d3f3ef8 | 77 | int log_priority; |
1bdd951e | 78 | void (*log_fn)(void *data, |
e4351b05 LDM |
79 | int priority, const char *file, int line, |
80 | const char *fn, const char *format, va_list args); | |
1bdd951e | 81 | void *log_data; |
1ce08a56 GSB |
82 | const void *userdata; |
83 | char *dirname; | |
d13e606f | 84 | struct kmod_config *config; |
822913d7 | 85 | struct hash *modules_by_name; |
b08314f7 LDM |
86 | struct index_mm *indexes[_KMOD_INDEX_MODULES_SIZE]; |
87 | unsigned long long indexes_stamp[_KMOD_INDEX_MODULES_SIZE]; | |
ecd40ee4 LDM |
88 | }; |
89 | ||
71928288 LDM |
90 | void kmod_log(const struct kmod_ctx *ctx, |
91 | int priority, const char *file, int line, const char *fn, | |
92 | const char *format, ...) | |
ecd40ee4 LDM |
93 | { |
94 | va_list args; | |
95 | ||
e5c60f1c GSB |
96 | if (ctx->log_fn == NULL) |
97 | return; | |
98 | ||
ecd40ee4 | 99 | va_start(args, format); |
1bdd951e | 100 | ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args); |
ecd40ee4 LDM |
101 | va_end(args); |
102 | } | |
103 | ||
1958af88 | 104 | _printf_format_(6, 0) |
1bdd951e | 105 | static void log_filep(void *data, |
e4351b05 LDM |
106 | int priority, const char *file, int line, |
107 | const char *fn, const char *format, va_list args) | |
ecd40ee4 | 108 | { |
1bdd951e | 109 | FILE *fp = data; |
e3cb0900 GSB |
110 | #ifdef ENABLE_DEBUG |
111 | char buf[16]; | |
112 | const char *priname; | |
113 | switch (priority) { | |
114 | case LOG_EMERG: | |
115 | priname = "EMERGENCY"; | |
116 | break; | |
117 | case LOG_ALERT: | |
118 | priname = "ALERT"; | |
119 | break; | |
120 | case LOG_CRIT: | |
121 | priname = "CRITICAL"; | |
122 | break; | |
123 | case LOG_ERR: | |
124 | priname = "ERROR"; | |
125 | break; | |
126 | case LOG_WARNING: | |
127 | priname = "WARNING"; | |
128 | break; | |
129 | case LOG_NOTICE: | |
130 | priname = "NOTICE"; | |
131 | break; | |
132 | case LOG_INFO: | |
133 | priname = "INFO"; | |
134 | break; | |
135 | case LOG_DEBUG: | |
136 | priname = "DEBUG"; | |
137 | break; | |
138 | default: | |
139 | snprintf(buf, sizeof(buf), "L:%d", priority); | |
140 | priname = buf; | |
141 | } | |
142 | fprintf(fp, "libkmod: %s %s:%d %s: ", priname, file, line, fn); | |
143 | #else | |
1bdd951e | 144 | fprintf(fp, "libkmod: %s: ", fn); |
e3cb0900 | 145 | #endif |
1bdd951e | 146 | vfprintf(fp, format, args); |
ecd40ee4 LDM |
147 | } |
148 | ||
1ce08a56 | 149 | const char *kmod_get_dirname(const struct kmod_ctx *ctx) |
221631d5 LDM |
150 | { |
151 | return ctx->dirname; | |
152 | } | |
153 | ||
ecd40ee4 | 154 | /** |
586fc304 LDM |
155 | * kmod_get_userdata: |
156 | * @ctx: kmod library context | |
ecd40ee4 LDM |
157 | * |
158 | * Retrieve stored data pointer from library context. This might be useful | |
be5a6dea | 159 | * to access from callbacks. |
ecd40ee4 LDM |
160 | * |
161 | * Returns: stored userdata | |
54ba8b34 | 162 | */ |
6d177553 | 163 | KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx) |
ecd40ee4 LDM |
164 | { |
165 | if (ctx == NULL) | |
166 | return NULL; | |
1ce08a56 | 167 | return (void *)ctx->userdata; |
ecd40ee4 LDM |
168 | } |
169 | ||
170 | /** | |
586fc304 LDM |
171 | * kmod_set_userdata: |
172 | * @ctx: kmod library context | |
ecd40ee4 LDM |
173 | * @userdata: data pointer |
174 | * | |
175 | * Store custom @userdata in the library context. | |
54ba8b34 | 176 | */ |
1ce08a56 | 177 | KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata) |
ecd40ee4 LDM |
178 | { |
179 | if (ctx == NULL) | |
180 | return; | |
181 | ctx->userdata = userdata; | |
182 | } | |
183 | ||
184 | static int log_priority(const char *priority) | |
185 | { | |
186 | char *endptr; | |
187 | int prio; | |
188 | ||
189 | prio = strtol(priority, &endptr, 10); | |
190 | if (endptr[0] == '\0' || isspace(endptr[0])) | |
191 | return prio; | |
192 | if (strncmp(priority, "err", 3) == 0) | |
193 | return LOG_ERR; | |
194 | if (strncmp(priority, "info", 4) == 0) | |
195 | return LOG_INFO; | |
196 | if (strncmp(priority, "debug", 5) == 0) | |
197 | return LOG_DEBUG; | |
198 | return 0; | |
199 | } | |
200 | ||
c5b37dba | 201 | static const char *dirname_default_prefix = "/lib/modules"; |
904c63aa | 202 | |
1ce08a56 | 203 | static char *get_kernel_release(const char *dirname) |
221631d5 LDM |
204 | { |
205 | struct utsname u; | |
904c63aa LDM |
206 | char *p; |
207 | ||
208 | if (dirname != NULL) | |
2e092e19 | 209 | return path_make_absolute_cwd(dirname); |
221631d5 LDM |
210 | |
211 | if (uname(&u) < 0) | |
212 | return NULL; | |
213 | ||
904c63aa LDM |
214 | if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0) |
215 | return NULL; | |
216 | ||
217 | return p; | |
221631d5 LDM |
218 | } |
219 | ||
ecd40ee4 | 220 | /** |
586fc304 | 221 | * kmod_new: |
cb8d4d3e | 222 | * @dirname: what to consider as linux module's directory, if NULL |
c5b37dba | 223 | * defaults to /lib/modules/`uname -r`. If it's relative, |
491c4902 CY |
224 | * it's treated as relative to the current working directory. |
225 | * Otherwise, give an absolute dirname. | |
cb8d4d3e | 226 | * @config_paths: ordered array of paths (directories or files) where |
be5a6dea LDM |
227 | * to load from user-defined configuration parameters such as |
228 | * alias, blacklists, commands (install, remove). If | |
229 | * NULL defaults to /run/modprobe.d, /etc/modprobe.d and | |
c5b37dba DR |
230 | * /lib/modprobe.d. Give an empty vector if configuration should |
231 | * not be read. This array must be null terminated. | |
cb8d4d3e | 232 | * |
e1daa4f5 LDM |
233 | * Create kmod library context. This reads the kmod configuration |
234 | * and fills in the default values. | |
235 | * | |
236 | * The initial refcount is 1, and needs to be decremented to | |
237 | * release the resources of the kmod library context. | |
238 | * | |
586fc304 | 239 | * Returns: a new kmod library context |
54ba8b34 | 240 | */ |
c35347f1 LDM |
241 | KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname, |
242 | const char * const *config_paths) | |
ecd40ee4 LDM |
243 | { |
244 | const char *env; | |
52a7704f | 245 | struct kmod_ctx *ctx; |
d13e606f | 246 | int err; |
ecd40ee4 | 247 | |
52a7704f LDM |
248 | ctx = calloc(1, sizeof(struct kmod_ctx)); |
249 | if (!ctx) | |
250 | return NULL; | |
ecd40ee4 | 251 | |
52a7704f | 252 | ctx->refcount = 1; |
1bdd951e GSB |
253 | ctx->log_fn = log_filep; |
254 | ctx->log_data = stderr; | |
52a7704f | 255 | ctx->log_priority = LOG_ERR; |
ecd40ee4 | 256 | |
904c63aa | 257 | ctx->dirname = get_kernel_release(dirname); |
221631d5 | 258 | |
ecd40ee4 | 259 | /* environment overwrites config */ |
41a51c2a | 260 | env = secure_getenv("KMOD_LOG"); |
ecd40ee4 | 261 | if (env != NULL) |
52a7704f | 262 | kmod_set_log_priority(ctx, log_priority(env)); |
ecd40ee4 | 263 | |
cb8d4d3e GSB |
264 | if (config_paths == NULL) |
265 | config_paths = default_config_paths; | |
266 | err = kmod_config_new(ctx, &ctx->config, config_paths); | |
d13e606f | 267 | if (err < 0) { |
fd186ae9 LDM |
268 | ERR(ctx, "could not create config\n"); |
269 | goto fail; | |
270 | } | |
271 | ||
822913d7 | 272 | ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL); |
fd186ae9 LDM |
273 | if (ctx->modules_by_name == NULL) { |
274 | ERR(ctx, "could not create by-name hash\n"); | |
275 | goto fail; | |
d13e606f | 276 | } |
7c2ab358 | 277 | |
ae6df84a LDM |
278 | INFO(ctx, "ctx %p created\n", ctx); |
279 | DBG(ctx, "log_priority=%d\n", ctx->log_priority); | |
52a7704f LDM |
280 | |
281 | return ctx; | |
fd186ae9 LDM |
282 | |
283 | fail: | |
284 | free(ctx->modules_by_name); | |
285 | free(ctx->dirname); | |
286 | free(ctx); | |
287 | return NULL; | |
ecd40ee4 LDM |
288 | } |
289 | ||
290 | /** | |
586fc304 LDM |
291 | * kmod_ref: |
292 | * @ctx: kmod library context | |
ecd40ee4 | 293 | * |
586fc304 | 294 | * Take a reference of the kmod library context. |
ecd40ee4 | 295 | * |
586fc304 | 296 | * Returns: the passed kmod library context |
54ba8b34 | 297 | */ |
586fc304 | 298 | KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx) |
ecd40ee4 LDM |
299 | { |
300 | if (ctx == NULL) | |
301 | return NULL; | |
302 | ctx->refcount++; | |
303 | return ctx; | |
304 | } | |
305 | ||
306 | /** | |
586fc304 LDM |
307 | * kmod_unref: |
308 | * @ctx: kmod library context | |
ecd40ee4 | 309 | * |
586fc304 | 310 | * Drop a reference of the kmod library context. If the refcount |
ecd40ee4 | 311 | * reaches zero, the resources of the context will be released. |
491c4902 CY |
312 | * |
313 | * Returns: the passed kmod library context or NULL if it's freed | |
54ba8b34 | 314 | */ |
586fc304 | 315 | KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx) |
ecd40ee4 LDM |
316 | { |
317 | if (ctx == NULL) | |
318 | return NULL; | |
4d1e689a LDM |
319 | |
320 | if (--ctx->refcount > 0) | |
ecd40ee4 | 321 | return ctx; |
33bb69b9 | 322 | |
ae6df84a | 323 | INFO(ctx, "context %p released\n", ctx); |
33bb69b9 LDM |
324 | |
325 | kmod_unload_resources(ctx); | |
822913d7 | 326 | hash_free(ctx->modules_by_name); |
1ce08a56 | 327 | free(ctx->dirname); |
d13e606f GSB |
328 | if (ctx->config) |
329 | kmod_config_free(ctx->config); | |
33bb69b9 | 330 | |
ecd40ee4 LDM |
331 | free(ctx); |
332 | return NULL; | |
333 | } | |
334 | ||
335 | /** | |
586fc304 LDM |
336 | * kmod_set_log_fn: |
337 | * @ctx: kmod library context | |
ecd40ee4 | 338 | * @log_fn: function to be called for logging messages |
b5b4d8e8 | 339 | * @data: data to pass to log function |
ecd40ee4 LDM |
340 | * |
341 | * The built-in logging writes to stderr. It can be | |
342 | * overridden by a custom function, to plug log messages | |
343 | * into the user's logging functionality. | |
54ba8b34 | 344 | */ |
586fc304 | 345 | KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx, |
1bdd951e | 346 | void (*log_fn)(void *data, |
e4351b05 LDM |
347 | int priority, const char *file, |
348 | int line, const char *fn, | |
1bdd951e GSB |
349 | const char *format, va_list args), |
350 | const void *data) | |
ecd40ee4 | 351 | { |
e5c60f1c GSB |
352 | if (ctx == NULL) |
353 | return; | |
ecd40ee4 | 354 | ctx->log_fn = log_fn; |
1bdd951e | 355 | ctx->log_data = (void *)data; |
ae6df84a | 356 | INFO(ctx, "custom logging function %p registered\n", log_fn); |
ecd40ee4 LDM |
357 | } |
358 | ||
359 | /** | |
586fc304 LDM |
360 | * kmod_get_log_priority: |
361 | * @ctx: kmod library context | |
ecd40ee4 LDM |
362 | * |
363 | * Returns: the current logging priority | |
54ba8b34 | 364 | */ |
6d177553 | 365 | KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx) |
ecd40ee4 | 366 | { |
e5c60f1c GSB |
367 | if (ctx == NULL) |
368 | return -1; | |
ecd40ee4 LDM |
369 | return ctx->log_priority; |
370 | } | |
371 | ||
372 | /** | |
586fc304 LDM |
373 | * kmod_set_log_priority: |
374 | * @ctx: kmod library context | |
ecd40ee4 LDM |
375 | * @priority: the new logging priority |
376 | * | |
377 | * Set the current logging priority. The value controls which messages | |
378 | * are logged. | |
54ba8b34 | 379 | */ |
586fc304 | 380 | KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority) |
ecd40ee4 | 381 | { |
e5c60f1c GSB |
382 | if (ctx == NULL) |
383 | return; | |
ecd40ee4 LDM |
384 | ctx->log_priority = priority; |
385 | } | |
7f3eb0cc | 386 | |
fd186ae9 | 387 | struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx, |
8bdeca11 | 388 | const char *key) |
fd186ae9 LDM |
389 | { |
390 | struct kmod_module *mod; | |
391 | ||
822913d7 | 392 | mod = hash_find(ctx->modules_by_name, key); |
fd186ae9 | 393 | |
8bdeca11 | 394 | DBG(ctx, "get module name='%s' found=%p\n", key, mod); |
fd186ae9 LDM |
395 | |
396 | return mod; | |
397 | } | |
398 | ||
8bdeca11 LDM |
399 | void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, |
400 | const char *key) | |
fd186ae9 | 401 | { |
8bdeca11 | 402 | DBG(ctx, "add %p key='%s'\n", mod, key); |
fd186ae9 | 403 | |
822913d7 | 404 | hash_add(ctx->modules_by_name, key, mod); |
fd186ae9 LDM |
405 | } |
406 | ||
8bdeca11 LDM |
407 | void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod, |
408 | const char *key) | |
fd186ae9 | 409 | { |
8bdeca11 | 410 | DBG(ctx, "del %p key='%s'\n", mod, key); |
fd186ae9 | 411 | |
822913d7 | 412 | hash_del(ctx->modules_by_name, key); |
fd186ae9 | 413 | } |
9ba6f57b | 414 | |
a009482c | 415 | static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx, |
810803db | 416 | enum kmod_index index_number, |
7b30f4f4 | 417 | const char *name, |
9ba6f57b LDM |
418 | struct kmod_list **list) |
419 | { | |
6f1bc6e3 | 420 | int err, nmatch = 0; |
0fbdfef3 | 421 | struct index_file *idx; |
9ba6f57b LDM |
422 | struct index_value *realnames, *realname; |
423 | ||
65a84f55 | 424 | if (ctx->indexes[index_number] != NULL) { |
3e245be1 | 425 | DBG(ctx, "use mmaped index '%s' for name=%s\n", |
63be91cb | 426 | index_files[index_number].fn, name); |
65a84f55 LDM |
427 | realnames = index_mm_searchwild(ctx->indexes[index_number], |
428 | name); | |
9fd58f30 | 429 | } else { |
65a84f55 LDM |
430 | char fn[PATH_MAX]; |
431 | ||
3b209959 | 432 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
63be91cb | 433 | index_files[index_number].fn); |
9ba6f57b | 434 | |
65a84f55 | 435 | DBG(ctx, "file=%s name=%s\n", fn, name); |
9ba6f57b | 436 | |
65a84f55 LDM |
437 | idx = index_file_open(fn); |
438 | if (idx == NULL) | |
439 | return -ENOSYS; | |
9ba6f57b | 440 | |
65a84f55 LDM |
441 | realnames = index_searchwild(idx, name); |
442 | index_file_close(idx); | |
443 | } | |
4272d087 | 444 | |
a955f71f | 445 | for (realname = realnames; realname; realname = realname->next) { |
9ba6f57b LDM |
446 | struct kmod_module *mod; |
447 | ||
ee3b3ff2 | 448 | err = kmod_module_new_from_alias(ctx, name, realname->value, &mod); |
9ba6f57b | 449 | if (err < 0) { |
dfa96f15 GSB |
450 | ERR(ctx, "Could not create module for alias=%s realname=%s: %s\n", |
451 | name, realname->value, strerror(-err)); | |
23fc91c6 | 452 | goto fail; |
9ba6f57b LDM |
453 | } |
454 | ||
455 | *list = kmod_list_append(*list, mod); | |
23fc91c6 | 456 | nmatch++; |
9ba6f57b LDM |
457 | } |
458 | ||
9ba6f57b | 459 | index_values_free(realnames); |
23fc91c6 LDM |
460 | return nmatch; |
461 | ||
462 | fail: | |
463 | *list = kmod_list_remove_n_latest(*list, nmatch); | |
e84d912b | 464 | index_values_free(realnames); |
9ba6f57b | 465 | return err; |
7b30f4f4 LDM |
466 | |
467 | } | |
468 | ||
7b30f4f4 LDM |
469 | int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name, |
470 | struct kmod_list **list) | |
471 | { | |
0c010fae | 472 | if (!strstartswith(name, "symbol:")) |
7b30f4f4 LDM |
473 | return 0; |
474 | ||
b08314f7 LDM |
475 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_SYMBOL, |
476 | name, list); | |
9ba6f57b LDM |
477 | } |
478 | ||
49e61ca3 LDM |
479 | int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name, |
480 | struct kmod_list **list) | |
481 | { | |
b08314f7 LDM |
482 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_ALIAS, |
483 | name, list); | |
49e61ca3 LDM |
484 | } |
485 | ||
3805274b LDM |
486 | int kmod_lookup_alias_from_builtin_file(struct kmod_ctx *ctx, const char *name, |
487 | struct kmod_list **list) | |
488 | { | |
ee1d188f LDM |
489 | char *line = NULL; |
490 | int err = 0; | |
3805274b | 491 | |
ee1d188f | 492 | assert(*list == NULL); |
3805274b | 493 | |
ee1d188f LDM |
494 | if (ctx->indexes[KMOD_INDEX_MODULES_BUILTIN]) { |
495 | DBG(ctx, "use mmaped index '%s' modname=%s\n", | |
496 | index_files[KMOD_INDEX_MODULES_BUILTIN].fn, | |
497 | name); | |
498 | line = index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_BUILTIN], | |
499 | name); | |
500 | } else { | |
501 | struct index_file *idx; | |
502 | char fn[PATH_MAX]; | |
503 | ||
504 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, | |
505 | index_files[KMOD_INDEX_MODULES_BUILTIN].fn); | |
506 | DBG(ctx, "file=%s modname=%s\n", fn, name); | |
507 | ||
508 | idx = index_file_open(fn); | |
509 | if (idx == NULL) { | |
510 | DBG(ctx, "could not open builtin file '%s'\n", fn); | |
511 | goto finish; | |
512 | } | |
513 | ||
514 | line = index_search(idx, name); | |
515 | index_file_close(idx); | |
516 | } | |
517 | ||
518 | if (line != NULL) { | |
519 | struct kmod_module *mod; | |
520 | ||
521 | err = kmod_module_new_from_name(ctx, name, &mod); | |
522 | if (err < 0) { | |
523 | ERR(ctx, "Could not create module from name %s: %s\n", | |
524 | name, strerror(-err)); | |
525 | goto finish; | |
526 | } | |
527 | ||
528 | kmod_module_set_builtin(mod, true); | |
529 | *list = kmod_list_append(*list, mod); | |
530 | if (*list == NULL) | |
531 | err = -ENOMEM; | |
3805274b LDM |
532 | } |
533 | ||
ee1d188f LDM |
534 | finish: |
535 | free(line); | |
3805274b LDM |
536 | return err; |
537 | } | |
538 | ||
671d4894 | 539 | char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name) |
1eb2ef69 LDM |
540 | { |
541 | struct index_file *idx; | |
542 | char fn[PATH_MAX]; | |
543 | char *line; | |
544 | ||
b08314f7 | 545 | if (ctx->indexes[KMOD_INDEX_MODULES_DEP]) { |
85132101 | 546 | DBG(ctx, "use mmaped index '%s' modname=%s\n", |
b08314f7 LDM |
547 | index_files[KMOD_INDEX_MODULES_DEP].fn, name); |
548 | return index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_DEP], | |
549 | name); | |
85132101 GSB |
550 | } |
551 | ||
a4a75029 | 552 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
b08314f7 | 553 | index_files[KMOD_INDEX_MODULES_DEP].fn); |
1eb2ef69 LDM |
554 | |
555 | DBG(ctx, "file=%s modname=%s\n", fn, name); | |
556 | ||
557 | idx = index_file_open(fn); | |
558 | if (idx == NULL) { | |
adca3cd2 | 559 | DBG(ctx, "could not open moddep file '%s'\n", fn); |
1eb2ef69 LDM |
560 | return NULL; |
561 | } | |
562 | ||
563 | line = index_search(idx, name); | |
564 | index_file_close(idx); | |
565 | ||
566 | return line; | |
567 | } | |
568 | ||
64700e47 LDM |
569 | int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name, |
570 | struct kmod_list **list) | |
571 | { | |
1eb2ef69 | 572 | char *line; |
64700e47 LDM |
573 | int n = 0; |
574 | ||
575 | /* | |
576 | * Module names do not contain ':'. Return early if we know it will | |
577 | * not be found. | |
578 | */ | |
579 | if (strchr(name, ':')) | |
580 | return 0; | |
581 | ||
671d4894 | 582 | line = kmod_search_moddep(ctx, name); |
64700e47 LDM |
583 | if (line != NULL) { |
584 | struct kmod_module *mod; | |
585 | ||
586 | n = kmod_module_new_from_name(ctx, name, &mod); | |
587 | if (n < 0) { | |
dfa96f15 GSB |
588 | ERR(ctx, "Could not create module from name %s: %s\n", |
589 | name, strerror(-n)); | |
64700e47 LDM |
590 | goto finish; |
591 | } | |
592 | ||
593 | *list = kmod_list_append(*list, mod); | |
671d4894 | 594 | kmod_module_parse_depline(mod, line); |
64700e47 LDM |
595 | } |
596 | ||
597 | finish: | |
598 | free(line); | |
64700e47 LDM |
599 | |
600 | return n; | |
601 | } | |
602 | ||
7f3eb0cc LDM |
603 | int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, |
604 | struct kmod_list **list) | |
605 | { | |
d13e606f | 606 | struct kmod_config *config = ctx->config; |
7f3eb0cc | 607 | struct kmod_list *l; |
23fc91c6 | 608 | int err, nmatch = 0; |
7f3eb0cc LDM |
609 | |
610 | kmod_list_foreach(l, config->aliases) { | |
611 | const char *aliasname = kmod_alias_get_name(l); | |
612 | const char *modname = kmod_alias_get_modname(l); | |
613 | ||
614 | if (fnmatch(aliasname, name, 0) == 0) { | |
615 | struct kmod_module *mod; | |
616 | ||
ee3b3ff2 LDM |
617 | err = kmod_module_new_from_alias(ctx, aliasname, |
618 | modname, &mod); | |
7f3eb0cc | 619 | if (err < 0) { |
dfa96f15 GSB |
620 | ERR(ctx, "Could not create module for alias=%s modname=%s: %s\n", |
621 | name, modname, strerror(-err)); | |
23fc91c6 | 622 | goto fail; |
7f3eb0cc LDM |
623 | } |
624 | ||
625 | *list = kmod_list_append(*list, mod); | |
23fc91c6 | 626 | nmatch++; |
7f3eb0cc LDM |
627 | } |
628 | } | |
629 | ||
23fc91c6 LDM |
630 | return nmatch; |
631 | ||
632 | fail: | |
633 | *list = kmod_list_remove_n_latest(*list, nmatch); | |
634 | return err; | |
7f3eb0cc | 635 | } |
1487a64f | 636 | |
f4fc5523 LDM |
637 | int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name, |
638 | struct kmod_list **list) | |
639 | { | |
640 | struct kmod_config *config = ctx->config; | |
641 | struct kmod_list *l, *node; | |
642 | int err, nmatch = 0; | |
643 | ||
644 | kmod_list_foreach(l, config->install_commands) { | |
645 | const char *modname = kmod_command_get_modname(l); | |
646 | ||
647 | if (streq(modname, name)) { | |
648 | const char *cmd = kmod_command_get_command(l); | |
649 | struct kmod_module *mod; | |
650 | ||
651 | err = kmod_module_new_from_name(ctx, modname, &mod); | |
652 | if (err < 0) { | |
dfa96f15 GSB |
653 | ERR(ctx, "Could not create module from name %s: %s\n", |
654 | modname, strerror(-err)); | |
f4fc5523 LDM |
655 | return err; |
656 | } | |
657 | ||
658 | node = kmod_list_append(*list, mod); | |
659 | if (node == NULL) { | |
660 | ERR(ctx, "out of memory\n"); | |
661 | return -ENOMEM; | |
662 | } | |
663 | ||
664 | *list = node; | |
665 | nmatch = 1; | |
666 | ||
667 | kmod_module_set_install_commands(mod, cmd); | |
668 | ||
669 | /* | |
670 | * match only the first one, like modprobe from | |
671 | * module-init-tools does | |
672 | */ | |
673 | break; | |
674 | } | |
675 | } | |
676 | ||
677 | if (nmatch) | |
678 | return nmatch; | |
679 | ||
680 | kmod_list_foreach(l, config->remove_commands) { | |
681 | const char *modname = kmod_command_get_modname(l); | |
682 | ||
683 | if (streq(modname, name)) { | |
684 | const char *cmd = kmod_command_get_command(l); | |
685 | struct kmod_module *mod; | |
686 | ||
687 | err = kmod_module_new_from_name(ctx, modname, &mod); | |
688 | if (err < 0) { | |
dfa96f15 GSB |
689 | ERR(ctx, "Could not create module from name %s: %s\n", |
690 | modname, strerror(-err)); | |
f4fc5523 LDM |
691 | return err; |
692 | } | |
693 | ||
694 | node = kmod_list_append(*list, mod); | |
695 | if (node == NULL) { | |
696 | ERR(ctx, "out of memory\n"); | |
697 | return -ENOMEM; | |
698 | } | |
699 | ||
700 | *list = node; | |
701 | nmatch = 1; | |
702 | ||
703 | kmod_module_set_remove_commands(mod, cmd); | |
704 | ||
705 | /* | |
706 | * match only the first one, like modprobe from | |
707 | * module-init-tools does | |
708 | */ | |
709 | break; | |
710 | } | |
711 | } | |
712 | ||
713 | return nmatch; | |
714 | } | |
715 | ||
ece09aac LDM |
716 | void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited) |
717 | { | |
718 | struct hash_iter iter; | |
719 | const void *v; | |
720 | ||
721 | hash_iter_init(ctx->modules_by_name, &iter); | |
722 | while (hash_iter_next(&iter, NULL, &v)) | |
723 | kmod_module_set_visited((struct kmod_module *)v, visited); | |
724 | } | |
725 | ||
450bd1b4 MM |
726 | void kmod_set_modules_required(struct kmod_ctx *ctx, bool required) |
727 | { | |
728 | struct hash_iter iter; | |
729 | const void *v; | |
730 | ||
731 | hash_iter_init(ctx->modules_by_name, &iter); | |
732 | while (hash_iter_next(&iter, NULL, &v)) | |
733 | kmod_module_set_required((struct kmod_module *)v, required); | |
734 | } | |
735 | ||
c4dc3ca8 LDM |
736 | static bool is_cache_invalid(const char *path, unsigned long long stamp) |
737 | { | |
738 | struct stat st; | |
739 | ||
740 | if (stat(path, &st) < 0) | |
741 | return true; | |
742 | ||
6068aaae | 743 | if (stamp != stat_mstamp(&st)) |
c4dc3ca8 LDM |
744 | return true; |
745 | ||
746 | return false; | |
747 | } | |
748 | ||
749 | /** | |
750 | * kmod_validate_resources: | |
751 | * @ctx: kmod library context | |
752 | * | |
753 | * Check if indexes and configuration files changed on disk and the current | |
754 | * context is not valid anymore. | |
755 | * | |
f4cc6ea5 | 756 | * Returns: KMOD_RESOURCES_OK if resources are still valid, |
c4dc3ca8 LDM |
757 | * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call |
758 | * kmod_unload_resources() and kmod_load_resources() or | |
759 | * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created. | |
760 | */ | |
761 | KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx) | |
762 | { | |
763 | struct kmod_list *l; | |
764 | size_t i; | |
765 | ||
766 | if (ctx == NULL || ctx->config == NULL) | |
767 | return KMOD_RESOURCES_MUST_RECREATE; | |
768 | ||
769 | kmod_list_foreach(l, ctx->config->paths) { | |
770 | struct kmod_config_path *cf = l->data; | |
771 | ||
772 | if (is_cache_invalid(cf->path, cf->stamp)) | |
773 | return KMOD_RESOURCES_MUST_RECREATE; | |
774 | } | |
775 | ||
b08314f7 | 776 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
c4dc3ca8 LDM |
777 | char path[PATH_MAX]; |
778 | ||
779 | if (ctx->indexes[i] == NULL) | |
780 | continue; | |
781 | ||
782 | snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, | |
63be91cb | 783 | index_files[i].fn); |
c4dc3ca8 LDM |
784 | |
785 | if (is_cache_invalid(path, ctx->indexes_stamp[i])) | |
786 | return KMOD_RESOURCES_MUST_RELOAD; | |
787 | } | |
788 | ||
789 | return KMOD_RESOURCES_OK; | |
790 | } | |
791 | ||
be5a6dea LDM |
792 | /** |
793 | * kmod_load_resources: | |
794 | * @ctx: kmod library context | |
795 | * | |
796 | * Load indexes and keep them open in @ctx. This way it's faster to lookup | |
797 | * information within the indexes. If this function is not called before a | |
798 | * search, the necessary index is always opened and closed. | |
799 | * | |
800 | * If user will do more than one or two lookups, insertions, deletions, most | |
801 | * likely it's good to call this function first. Particularly in a daemon like | |
802 | * udev that on bootup issues hundreds of calls to lookup the index, calling | |
803 | * this function will speedup the searches. | |
804 | * | |
805 | * Returns: 0 on success or < 0 otherwise. | |
806 | */ | |
33bb69b9 LDM |
807 | KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx) |
808 | { | |
809 | size_t i; | |
810 | ||
811 | if (ctx == NULL) | |
812 | return -ENOENT; | |
813 | ||
b08314f7 | 814 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
6de8f6e9 | 815 | char path[PATH_MAX]; |
3e676766 | 816 | |
16ca3666 | 817 | if (ctx->indexes[i] != NULL) { |
63be91cb LDM |
818 | INFO(ctx, "Index %s already loaded\n", |
819 | index_files[i].fn); | |
3e676766 LDM |
820 | continue; |
821 | } | |
79d57fcb | 822 | |
6de8f6e9 | 823 | snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, |
63be91cb | 824 | index_files[i].fn); |
2e2e252b | 825 | ctx->indexes[i] = index_mm_open(ctx, path, |
9fd58f30 | 826 | &ctx->indexes_stamp[i]); |
3e676766 LDM |
827 | if (ctx->indexes[i] == NULL) |
828 | goto fail; | |
33bb69b9 LDM |
829 | } |
830 | ||
831 | return 0; | |
832 | ||
833 | fail: | |
834 | kmod_unload_resources(ctx); | |
835 | return -ENOMEM; | |
836 | } | |
837 | ||
be5a6dea LDM |
838 | /** |
839 | * kmod_unload_resources: | |
840 | * @ctx: kmod library context | |
841 | * | |
842 | * Unload all the indexes. This will free the resources to maintain the index | |
843 | * open and all subsequent searches will need to open and close the index. | |
844 | * | |
845 | * User is free to call kmod_load_resources() and kmod_unload_resources() as | |
846 | * many times as wanted during the lifecycle of @ctx. For example, if a daemon | |
847 | * knows that when starting up it will lookup a lot of modules, it could call | |
848 | * kmod_load_resources() and after the first burst of searches is gone, it | |
849 | * could free the resources by calling kmod_unload_resources(). | |
850 | * | |
851 | * Returns: 0 on success or < 0 otherwise. | |
852 | */ | |
33bb69b9 LDM |
853 | KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx) |
854 | { | |
855 | size_t i; | |
856 | ||
857 | if (ctx == NULL) | |
858 | return; | |
859 | ||
b08314f7 | 860 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
33bb69b9 LDM |
861 | if (ctx->indexes[i] != NULL) { |
862 | index_mm_close(ctx->indexes[i]); | |
863 | ctx->indexes[i] = NULL; | |
9fd58f30 | 864 | ctx->indexes_stamp[i] = 0; |
33bb69b9 LDM |
865 | } |
866 | } | |
867 | } | |
bd3f5535 | 868 | |
0224482e LDM |
869 | /** |
870 | * kmod_dump_index: | |
871 | * @ctx: kmod library context | |
d7152f62 CY |
872 | * @type: index to dump, valid indexes are |
873 | * KMOD_INDEX_MODULES_DEP: index of module dependencies; | |
874 | * KMOD_INDEX_MODULES_ALIAS: index of module aliases; | |
875 | * KMOD_INDEX_MODULES_SYMBOL: index of symbol aliases; | |
876 | * KMOD_INDEX_MODULES_BUILTIN: index of builtin module. | |
0224482e LDM |
877 | * @fd: file descriptor to dump index to |
878 | * | |
09e9ae58 LDM |
879 | * Dump index to file descriptor. Note that this function doesn't use stdio.h |
880 | * so call fflush() before calling this function to be sure data is written in | |
881 | * order. | |
0224482e LDM |
882 | * |
883 | * Returns: 0 on success or < 0 otherwise. | |
884 | */ | |
758428a7 LDM |
885 | KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type, |
886 | int fd) | |
887 | { | |
888 | if (ctx == NULL) | |
889 | return -ENOSYS; | |
890 | ||
891 | if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE) | |
892 | return -ENOENT; | |
893 | ||
894 | if (ctx->indexes[type] != NULL) { | |
895 | DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn); | |
896 | index_mm_dump(ctx->indexes[type], fd, | |
897 | index_files[type].prefix); | |
898 | } else { | |
899 | char fn[PATH_MAX]; | |
900 | struct index_file *idx; | |
901 | ||
902 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, | |
903 | index_files[type].fn); | |
904 | ||
905 | DBG(ctx, "file=%s\n", fn); | |
906 | ||
907 | idx = index_file_open(fn); | |
908 | if (idx == NULL) | |
909 | return -ENOSYS; | |
910 | ||
911 | index_dump(idx, fd, index_files[type].prefix); | |
912 | index_file_close(idx); | |
913 | } | |
914 | ||
915 | return 0; | |
916 | } | |
917 | ||
e7fc2c86 | 918 | const struct kmod_config *kmod_get_config(const struct kmod_ctx *ctx) |
c1c9c446 | 919 | { |
e7fc2c86 | 920 | return ctx->config; |
8b5ee618 | 921 | } |