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