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