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