]>
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 | |
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 | 20 | |
ee1d188f | 21 | #include <assert.h> |
ecd40ee4 LDM |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
24 | #include <stddef.h> | |
25 | #include <stdarg.h> | |
1eb2ef69 | 26 | #include <limits.h> |
ecd40ee4 LDM |
27 | #include <unistd.h> |
28 | #include <errno.h> | |
7f3eb0cc | 29 | #include <fnmatch.h> |
ecd40ee4 LDM |
30 | #include <string.h> |
31 | #include <ctype.h> | |
221631d5 | 32 | #include <sys/utsname.h> |
c4dc3ca8 | 33 | #include <sys/stat.h> |
ecd40ee4 | 34 | |
586fc304 LDM |
35 | #include "libkmod.h" |
36 | #include "libkmod-private.h" | |
9ba6f57b | 37 | #include "libkmod-index.h" |
ecd40ee4 | 38 | |
fd186ae9 LDM |
39 | #define KMOD_HASH_SIZE (256) |
40 | #define KMOD_LRU_MAX (128) | |
3805274b | 41 | #define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_BUILTIN + 1 |
fd186ae9 | 42 | |
ecd40ee4 | 43 | /** |
586fc304 LDM |
44 | * SECTION:libkmod |
45 | * @short_description: libkmod context | |
ecd40ee4 LDM |
46 | * |
47 | * The context contains the default values for the library user, | |
48 | * and is passed to all library operations. | |
49 | */ | |
50 | ||
63be91cb LDM |
51 | static struct _index_files { |
52 | const char *fn; | |
53 | const char *prefix; | |
54 | } index_files[] = { | |
b08314f7 LDM |
55 | [KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep", .prefix = "" }, |
56 | [KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .prefix = "alias " }, | |
57 | [KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .prefix = "alias "}, | |
3805274b | 58 | [KMOD_INDEX_MODULES_BUILTIN] = { .fn = "modules.builtin", .prefix = ""}, |
a4a75029 LDM |
59 | }; |
60 | ||
cb8d4d3e | 61 | static const char *default_config_paths[] = { |
a308abec | 62 | SYSCONFDIR "/modprobe.d", |
436da1e9 | 63 | "/run/modprobe.d", |
c5b37dba | 64 | "/lib/modprobe.d", |
cb8d4d3e GSB |
65 | NULL |
66 | }; | |
67 | ||
ecd40ee4 | 68 | /** |
586fc304 | 69 | * kmod_ctx: |
ecd40ee4 LDM |
70 | * |
71 | * Opaque object representing the library context. | |
72 | */ | |
586fc304 | 73 | struct kmod_ctx { |
ecd40ee4 | 74 | int refcount; |
8d3f3ef8 | 75 | int log_priority; |
1bdd951e | 76 | void (*log_fn)(void *data, |
e4351b05 LDM |
77 | int priority, const char *file, int line, |
78 | const char *fn, const char *format, va_list args); | |
1bdd951e | 79 | void *log_data; |
1ce08a56 GSB |
80 | const void *userdata; |
81 | char *dirname; | |
d13e606f | 82 | struct kmod_config *config; |
822913d7 | 83 | struct hash *modules_by_name; |
b08314f7 LDM |
84 | struct index_mm *indexes[_KMOD_INDEX_MODULES_SIZE]; |
85 | unsigned long long indexes_stamp[_KMOD_INDEX_MODULES_SIZE]; | |
ecd40ee4 LDM |
86 | }; |
87 | ||
71928288 LDM |
88 | void kmod_log(const struct kmod_ctx *ctx, |
89 | int priority, const char *file, int line, const char *fn, | |
90 | const char *format, ...) | |
ecd40ee4 LDM |
91 | { |
92 | va_list args; | |
93 | ||
e5c60f1c GSB |
94 | if (ctx->log_fn == NULL) |
95 | return; | |
96 | ||
ecd40ee4 | 97 | va_start(args, format); |
1bdd951e | 98 | ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args); |
ecd40ee4 LDM |
99 | va_end(args); |
100 | } | |
101 | ||
1bdd951e | 102 | static void log_filep(void *data, |
e4351b05 LDM |
103 | int priority, const char *file, int line, |
104 | const char *fn, const char *format, va_list args) | |
ecd40ee4 | 105 | { |
1bdd951e | 106 | FILE *fp = data; |
e3cb0900 GSB |
107 | #ifdef ENABLE_DEBUG |
108 | char buf[16]; | |
109 | const char *priname; | |
110 | switch (priority) { | |
111 | case LOG_EMERG: | |
112 | priname = "EMERGENCY"; | |
113 | break; | |
114 | case LOG_ALERT: | |
115 | priname = "ALERT"; | |
116 | break; | |
117 | case LOG_CRIT: | |
118 | priname = "CRITICAL"; | |
119 | break; | |
120 | case LOG_ERR: | |
121 | priname = "ERROR"; | |
122 | break; | |
123 | case LOG_WARNING: | |
124 | priname = "WARNING"; | |
125 | break; | |
126 | case LOG_NOTICE: | |
127 | priname = "NOTICE"; | |
128 | break; | |
129 | case LOG_INFO: | |
130 | priname = "INFO"; | |
131 | break; | |
132 | case LOG_DEBUG: | |
133 | priname = "DEBUG"; | |
134 | break; | |
135 | default: | |
136 | snprintf(buf, sizeof(buf), "L:%d", priority); | |
137 | priname = buf; | |
138 | } | |
139 | fprintf(fp, "libkmod: %s %s:%d %s: ", priname, file, line, fn); | |
140 | #else | |
1bdd951e | 141 | fprintf(fp, "libkmod: %s: ", fn); |
e3cb0900 | 142 | #endif |
1bdd951e | 143 | vfprintf(fp, format, args); |
ecd40ee4 LDM |
144 | } |
145 | ||
1ce08a56 | 146 | const char *kmod_get_dirname(const struct kmod_ctx *ctx) |
221631d5 LDM |
147 | { |
148 | return ctx->dirname; | |
149 | } | |
150 | ||
ecd40ee4 | 151 | /** |
586fc304 LDM |
152 | * kmod_get_userdata: |
153 | * @ctx: kmod library context | |
ecd40ee4 LDM |
154 | * |
155 | * Retrieve stored data pointer from library context. This might be useful | |
be5a6dea | 156 | * to access from callbacks. |
ecd40ee4 LDM |
157 | * |
158 | * Returns: stored userdata | |
54ba8b34 | 159 | */ |
6d177553 | 160 | KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx) |
ecd40ee4 LDM |
161 | { |
162 | if (ctx == NULL) | |
163 | return NULL; | |
1ce08a56 | 164 | return (void *)ctx->userdata; |
ecd40ee4 LDM |
165 | } |
166 | ||
167 | /** | |
586fc304 LDM |
168 | * kmod_set_userdata: |
169 | * @ctx: kmod library context | |
ecd40ee4 LDM |
170 | * @userdata: data pointer |
171 | * | |
172 | * Store custom @userdata in the library context. | |
54ba8b34 | 173 | */ |
1ce08a56 | 174 | KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata) |
ecd40ee4 LDM |
175 | { |
176 | if (ctx == NULL) | |
177 | return; | |
178 | ctx->userdata = userdata; | |
179 | } | |
180 | ||
181 | static int log_priority(const char *priority) | |
182 | { | |
183 | char *endptr; | |
184 | int prio; | |
185 | ||
186 | prio = strtol(priority, &endptr, 10); | |
187 | if (endptr[0] == '\0' || isspace(endptr[0])) | |
188 | return prio; | |
189 | if (strncmp(priority, "err", 3) == 0) | |
190 | return LOG_ERR; | |
191 | if (strncmp(priority, "info", 4) == 0) | |
192 | return LOG_INFO; | |
193 | if (strncmp(priority, "debug", 5) == 0) | |
194 | return LOG_DEBUG; | |
195 | return 0; | |
196 | } | |
197 | ||
c5b37dba | 198 | static const char *dirname_default_prefix = "/lib/modules"; |
904c63aa | 199 | |
1ce08a56 | 200 | static char *get_kernel_release(const char *dirname) |
221631d5 LDM |
201 | { |
202 | struct utsname u; | |
904c63aa LDM |
203 | char *p; |
204 | ||
205 | if (dirname != NULL) | |
2e092e19 | 206 | return path_make_absolute_cwd(dirname); |
221631d5 LDM |
207 | |
208 | if (uname(&u) < 0) | |
209 | return NULL; | |
210 | ||
904c63aa LDM |
211 | if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0) |
212 | return NULL; | |
213 | ||
214 | return p; | |
221631d5 LDM |
215 | } |
216 | ||
ecd40ee4 | 217 | /** |
586fc304 | 218 | * kmod_new: |
cb8d4d3e | 219 | * @dirname: what to consider as linux module's directory, if NULL |
c5b37dba | 220 | * defaults to /lib/modules/`uname -r`. If it's relative, |
2f47c7fa LDM |
221 | * it's treated as relative to current the current working |
222 | * directory. Otherwise, give an absolute dirname. | |
cb8d4d3e | 223 | * @config_paths: ordered array of paths (directories or files) where |
be5a6dea LDM |
224 | * to load from user-defined configuration parameters such as |
225 | * alias, blacklists, commands (install, remove). If | |
226 | * NULL defaults to /run/modprobe.d, /etc/modprobe.d and | |
c5b37dba DR |
227 | * /lib/modprobe.d. Give an empty vector if configuration should |
228 | * not be read. This array must be null 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 | { | |
ee1d188f LDM |
483 | char *line = NULL; |
484 | int err = 0; | |
3805274b | 485 | |
ee1d188f | 486 | assert(*list == NULL); |
3805274b | 487 | |
ee1d188f LDM |
488 | if (ctx->indexes[KMOD_INDEX_MODULES_BUILTIN]) { |
489 | DBG(ctx, "use mmaped index '%s' modname=%s\n", | |
490 | index_files[KMOD_INDEX_MODULES_BUILTIN].fn, | |
491 | name); | |
492 | line = index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_BUILTIN], | |
493 | name); | |
494 | } else { | |
495 | struct index_file *idx; | |
496 | char fn[PATH_MAX]; | |
497 | ||
498 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, | |
499 | index_files[KMOD_INDEX_MODULES_BUILTIN].fn); | |
500 | DBG(ctx, "file=%s modname=%s\n", fn, name); | |
501 | ||
502 | idx = index_file_open(fn); | |
503 | if (idx == NULL) { | |
504 | DBG(ctx, "could not open builtin file '%s'\n", fn); | |
505 | goto finish; | |
506 | } | |
507 | ||
508 | line = index_search(idx, name); | |
509 | index_file_close(idx); | |
510 | } | |
511 | ||
512 | if (line != NULL) { | |
513 | struct kmod_module *mod; | |
514 | ||
515 | err = kmod_module_new_from_name(ctx, name, &mod); | |
516 | if (err < 0) { | |
517 | ERR(ctx, "Could not create module from name %s: %s\n", | |
518 | name, strerror(-err)); | |
519 | goto finish; | |
520 | } | |
521 | ||
522 | kmod_module_set_builtin(mod, true); | |
523 | *list = kmod_list_append(*list, mod); | |
524 | if (*list == NULL) | |
525 | err = -ENOMEM; | |
3805274b LDM |
526 | } |
527 | ||
ee1d188f LDM |
528 | finish: |
529 | free(line); | |
3805274b LDM |
530 | return err; |
531 | } | |
532 | ||
671d4894 | 533 | char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name) |
1eb2ef69 LDM |
534 | { |
535 | struct index_file *idx; | |
536 | char fn[PATH_MAX]; | |
537 | char *line; | |
538 | ||
b08314f7 | 539 | if (ctx->indexes[KMOD_INDEX_MODULES_DEP]) { |
85132101 | 540 | DBG(ctx, "use mmaped index '%s' modname=%s\n", |
b08314f7 LDM |
541 | index_files[KMOD_INDEX_MODULES_DEP].fn, name); |
542 | return index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_DEP], | |
543 | name); | |
85132101 GSB |
544 | } |
545 | ||
a4a75029 | 546 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
b08314f7 | 547 | index_files[KMOD_INDEX_MODULES_DEP].fn); |
1eb2ef69 LDM |
548 | |
549 | DBG(ctx, "file=%s modname=%s\n", fn, name); | |
550 | ||
551 | idx = index_file_open(fn); | |
552 | if (idx == NULL) { | |
adca3cd2 | 553 | DBG(ctx, "could not open moddep file '%s'\n", fn); |
1eb2ef69 LDM |
554 | return NULL; |
555 | } | |
556 | ||
557 | line = index_search(idx, name); | |
558 | index_file_close(idx); | |
559 | ||
560 | return line; | |
561 | } | |
562 | ||
64700e47 LDM |
563 | int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name, |
564 | struct kmod_list **list) | |
565 | { | |
1eb2ef69 | 566 | char *line; |
64700e47 LDM |
567 | int n = 0; |
568 | ||
569 | /* | |
570 | * Module names do not contain ':'. Return early if we know it will | |
571 | * not be found. | |
572 | */ | |
573 | if (strchr(name, ':')) | |
574 | return 0; | |
575 | ||
671d4894 | 576 | line = kmod_search_moddep(ctx, name); |
64700e47 LDM |
577 | if (line != NULL) { |
578 | struct kmod_module *mod; | |
579 | ||
580 | n = kmod_module_new_from_name(ctx, name, &mod); | |
581 | if (n < 0) { | |
dfa96f15 GSB |
582 | ERR(ctx, "Could not create module from name %s: %s\n", |
583 | name, strerror(-n)); | |
64700e47 LDM |
584 | goto finish; |
585 | } | |
586 | ||
587 | *list = kmod_list_append(*list, mod); | |
671d4894 | 588 | kmod_module_parse_depline(mod, line); |
64700e47 LDM |
589 | } |
590 | ||
591 | finish: | |
592 | free(line); | |
64700e47 LDM |
593 | |
594 | return n; | |
595 | } | |
596 | ||
7f3eb0cc LDM |
597 | int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, |
598 | struct kmod_list **list) | |
599 | { | |
d13e606f | 600 | struct kmod_config *config = ctx->config; |
7f3eb0cc | 601 | struct kmod_list *l; |
23fc91c6 | 602 | int err, nmatch = 0; |
7f3eb0cc LDM |
603 | |
604 | kmod_list_foreach(l, config->aliases) { | |
605 | const char *aliasname = kmod_alias_get_name(l); | |
606 | const char *modname = kmod_alias_get_modname(l); | |
607 | ||
608 | if (fnmatch(aliasname, name, 0) == 0) { | |
609 | struct kmod_module *mod; | |
610 | ||
ee3b3ff2 LDM |
611 | err = kmod_module_new_from_alias(ctx, aliasname, |
612 | modname, &mod); | |
7f3eb0cc | 613 | if (err < 0) { |
dfa96f15 GSB |
614 | ERR(ctx, "Could not create module for alias=%s modname=%s: %s\n", |
615 | name, modname, strerror(-err)); | |
23fc91c6 | 616 | goto fail; |
7f3eb0cc LDM |
617 | } |
618 | ||
619 | *list = kmod_list_append(*list, mod); | |
23fc91c6 | 620 | nmatch++; |
7f3eb0cc LDM |
621 | } |
622 | } | |
623 | ||
23fc91c6 LDM |
624 | return nmatch; |
625 | ||
626 | fail: | |
627 | *list = kmod_list_remove_n_latest(*list, nmatch); | |
628 | return err; | |
7f3eb0cc | 629 | } |
1487a64f | 630 | |
f4fc5523 LDM |
631 | int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name, |
632 | struct kmod_list **list) | |
633 | { | |
634 | struct kmod_config *config = ctx->config; | |
635 | struct kmod_list *l, *node; | |
636 | int err, nmatch = 0; | |
637 | ||
638 | kmod_list_foreach(l, config->install_commands) { | |
639 | const char *modname = kmod_command_get_modname(l); | |
640 | ||
641 | if (streq(modname, name)) { | |
642 | const char *cmd = kmod_command_get_command(l); | |
643 | struct kmod_module *mod; | |
644 | ||
645 | err = kmod_module_new_from_name(ctx, modname, &mod); | |
646 | if (err < 0) { | |
dfa96f15 GSB |
647 | ERR(ctx, "Could not create module from name %s: %s\n", |
648 | modname, strerror(-err)); | |
f4fc5523 LDM |
649 | return err; |
650 | } | |
651 | ||
652 | node = kmod_list_append(*list, mod); | |
653 | if (node == NULL) { | |
654 | ERR(ctx, "out of memory\n"); | |
655 | return -ENOMEM; | |
656 | } | |
657 | ||
658 | *list = node; | |
659 | nmatch = 1; | |
660 | ||
661 | kmod_module_set_install_commands(mod, cmd); | |
662 | ||
663 | /* | |
664 | * match only the first one, like modprobe from | |
665 | * module-init-tools does | |
666 | */ | |
667 | break; | |
668 | } | |
669 | } | |
670 | ||
671 | if (nmatch) | |
672 | return nmatch; | |
673 | ||
674 | kmod_list_foreach(l, config->remove_commands) { | |
675 | const char *modname = kmod_command_get_modname(l); | |
676 | ||
677 | if (streq(modname, name)) { | |
678 | const char *cmd = kmod_command_get_command(l); | |
679 | struct kmod_module *mod; | |
680 | ||
681 | err = kmod_module_new_from_name(ctx, modname, &mod); | |
682 | if (err < 0) { | |
dfa96f15 GSB |
683 | ERR(ctx, "Could not create module from name %s: %s\n", |
684 | modname, strerror(-err)); | |
f4fc5523 LDM |
685 | return err; |
686 | } | |
687 | ||
688 | node = kmod_list_append(*list, mod); | |
689 | if (node == NULL) { | |
690 | ERR(ctx, "out of memory\n"); | |
691 | return -ENOMEM; | |
692 | } | |
693 | ||
694 | *list = node; | |
695 | nmatch = 1; | |
696 | ||
697 | kmod_module_set_remove_commands(mod, cmd); | |
698 | ||
699 | /* | |
700 | * match only the first one, like modprobe from | |
701 | * module-init-tools does | |
702 | */ | |
703 | break; | |
704 | } | |
705 | } | |
706 | ||
707 | return nmatch; | |
708 | } | |
709 | ||
ece09aac LDM |
710 | void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited) |
711 | { | |
712 | struct hash_iter iter; | |
713 | const void *v; | |
714 | ||
715 | hash_iter_init(ctx->modules_by_name, &iter); | |
716 | while (hash_iter_next(&iter, NULL, &v)) | |
717 | kmod_module_set_visited((struct kmod_module *)v, visited); | |
718 | } | |
719 | ||
c4dc3ca8 LDM |
720 | static bool is_cache_invalid(const char *path, unsigned long long stamp) |
721 | { | |
722 | struct stat st; | |
723 | ||
724 | if (stat(path, &st) < 0) | |
725 | return true; | |
726 | ||
6068aaae | 727 | if (stamp != stat_mstamp(&st)) |
c4dc3ca8 LDM |
728 | return true; |
729 | ||
730 | return false; | |
731 | } | |
732 | ||
733 | /** | |
734 | * kmod_validate_resources: | |
735 | * @ctx: kmod library context | |
736 | * | |
737 | * Check if indexes and configuration files changed on disk and the current | |
738 | * context is not valid anymore. | |
739 | * | |
f4cc6ea5 | 740 | * Returns: KMOD_RESOURCES_OK if resources are still valid, |
c4dc3ca8 LDM |
741 | * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call |
742 | * kmod_unload_resources() and kmod_load_resources() or | |
743 | * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created. | |
744 | */ | |
745 | KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx) | |
746 | { | |
747 | struct kmod_list *l; | |
748 | size_t i; | |
749 | ||
750 | if (ctx == NULL || ctx->config == NULL) | |
751 | return KMOD_RESOURCES_MUST_RECREATE; | |
752 | ||
753 | kmod_list_foreach(l, ctx->config->paths) { | |
754 | struct kmod_config_path *cf = l->data; | |
755 | ||
756 | if (is_cache_invalid(cf->path, cf->stamp)) | |
757 | return KMOD_RESOURCES_MUST_RECREATE; | |
758 | } | |
759 | ||
b08314f7 | 760 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
c4dc3ca8 LDM |
761 | char path[PATH_MAX]; |
762 | ||
763 | if (ctx->indexes[i] == NULL) | |
764 | continue; | |
765 | ||
766 | snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, | |
63be91cb | 767 | index_files[i].fn); |
c4dc3ca8 LDM |
768 | |
769 | if (is_cache_invalid(path, ctx->indexes_stamp[i])) | |
770 | return KMOD_RESOURCES_MUST_RELOAD; | |
771 | } | |
772 | ||
773 | return KMOD_RESOURCES_OK; | |
774 | } | |
775 | ||
be5a6dea LDM |
776 | /** |
777 | * kmod_load_resources: | |
778 | * @ctx: kmod library context | |
779 | * | |
780 | * Load indexes and keep them open in @ctx. This way it's faster to lookup | |
781 | * information within the indexes. If this function is not called before a | |
782 | * search, the necessary index is always opened and closed. | |
783 | * | |
784 | * If user will do more than one or two lookups, insertions, deletions, most | |
785 | * likely it's good to call this function first. Particularly in a daemon like | |
786 | * udev that on bootup issues hundreds of calls to lookup the index, calling | |
787 | * this function will speedup the searches. | |
788 | * | |
789 | * Returns: 0 on success or < 0 otherwise. | |
790 | */ | |
33bb69b9 LDM |
791 | KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx) |
792 | { | |
793 | size_t i; | |
794 | ||
795 | if (ctx == NULL) | |
796 | return -ENOENT; | |
797 | ||
b08314f7 | 798 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
6de8f6e9 | 799 | char path[PATH_MAX]; |
3e676766 | 800 | |
16ca3666 | 801 | if (ctx->indexes[i] != NULL) { |
63be91cb LDM |
802 | INFO(ctx, "Index %s already loaded\n", |
803 | index_files[i].fn); | |
3e676766 LDM |
804 | continue; |
805 | } | |
79d57fcb | 806 | |
6de8f6e9 | 807 | snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, |
63be91cb | 808 | index_files[i].fn); |
2e2e252b | 809 | ctx->indexes[i] = index_mm_open(ctx, path, |
9fd58f30 | 810 | &ctx->indexes_stamp[i]); |
3e676766 LDM |
811 | if (ctx->indexes[i] == NULL) |
812 | goto fail; | |
33bb69b9 LDM |
813 | } |
814 | ||
815 | return 0; | |
816 | ||
817 | fail: | |
818 | kmod_unload_resources(ctx); | |
819 | return -ENOMEM; | |
820 | } | |
821 | ||
be5a6dea LDM |
822 | /** |
823 | * kmod_unload_resources: | |
824 | * @ctx: kmod library context | |
825 | * | |
826 | * Unload all the indexes. This will free the resources to maintain the index | |
827 | * open and all subsequent searches will need to open and close the index. | |
828 | * | |
829 | * User is free to call kmod_load_resources() and kmod_unload_resources() as | |
830 | * many times as wanted during the lifecycle of @ctx. For example, if a daemon | |
831 | * knows that when starting up it will lookup a lot of modules, it could call | |
832 | * kmod_load_resources() and after the first burst of searches is gone, it | |
833 | * could free the resources by calling kmod_unload_resources(). | |
834 | * | |
835 | * Returns: 0 on success or < 0 otherwise. | |
836 | */ | |
33bb69b9 LDM |
837 | KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx) |
838 | { | |
839 | size_t i; | |
840 | ||
841 | if (ctx == NULL) | |
842 | return; | |
843 | ||
b08314f7 | 844 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
33bb69b9 LDM |
845 | if (ctx->indexes[i] != NULL) { |
846 | index_mm_close(ctx->indexes[i]); | |
847 | ctx->indexes[i] = NULL; | |
9fd58f30 | 848 | ctx->indexes_stamp[i] = 0; |
33bb69b9 LDM |
849 | } |
850 | } | |
851 | } | |
bd3f5535 | 852 | |
0224482e LDM |
853 | /** |
854 | * kmod_dump_index: | |
855 | * @ctx: kmod library context | |
856 | * @type: index to dump | |
857 | * @fd: file descriptor to dump index to | |
858 | * | |
09e9ae58 LDM |
859 | * Dump index to file descriptor. Note that this function doesn't use stdio.h |
860 | * so call fflush() before calling this function to be sure data is written in | |
861 | * order. | |
0224482e LDM |
862 | * |
863 | * Returns: 0 on success or < 0 otherwise. | |
864 | */ | |
758428a7 LDM |
865 | KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type, |
866 | int fd) | |
867 | { | |
868 | if (ctx == NULL) | |
869 | return -ENOSYS; | |
870 | ||
871 | if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE) | |
872 | return -ENOENT; | |
873 | ||
874 | if (ctx->indexes[type] != NULL) { | |
875 | DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn); | |
876 | index_mm_dump(ctx->indexes[type], fd, | |
877 | index_files[type].prefix); | |
878 | } else { | |
879 | char fn[PATH_MAX]; | |
880 | struct index_file *idx; | |
881 | ||
882 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, | |
883 | index_files[type].fn); | |
884 | ||
885 | DBG(ctx, "file=%s\n", fn); | |
886 | ||
887 | idx = index_file_open(fn); | |
888 | if (idx == NULL) | |
889 | return -ENOSYS; | |
890 | ||
891 | index_dump(idx, fd, index_files[type].prefix); | |
892 | index_file_close(idx); | |
893 | } | |
894 | ||
895 | return 0; | |
896 | } | |
897 | ||
e7fc2c86 | 898 | const struct kmod_config *kmod_get_config(const struct kmod_ctx *ctx) |
c1c9c446 | 899 | { |
e7fc2c86 | 900 | return ctx->config; |
8b5ee618 | 901 | } |