]> git.ipfire.org Git - thirdparty/kmod.git/blame - libkmod/libkmod.c
build-sys: Append -Werror when testing flags
[thirdparty/kmod.git] / libkmod / libkmod.c
CommitLineData
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 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
51static 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 61static const char *default_config_paths[] = {
a308abec 62 SYSCONFDIR "/modprobe.d",
436da1e9 63 "/run/modprobe.d",
a308abec 64 ROOTPREFIX "/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 73struct 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
88void 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 102static 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 146const 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 160KMOD_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 174KMOD_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
181static 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
a308abec 198static const char *dirname_default_prefix = ROOTPREFIX "/lib/modules";
904c63aa 199
1ce08a56 200static 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
2f47c7fa
LDM
220 * defaults to $rootprefix/lib/modules/`uname -r`. If it's relative,
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
2f47c7fa
LDM
227 * $rootprefix/lib/modprobe.d. Give an empty vector if
228 * configuration should not be read. This array must be null
229 * terminated.
cb8d4d3e 230 *
e1daa4f5
LDM
231 * Create kmod library context. This reads the kmod configuration
232 * and fills in the default values.
233 *
234 * The initial refcount is 1, and needs to be decremented to
235 * release the resources of the kmod library context.
236 *
586fc304 237 * Returns: a new kmod library context
54ba8b34 238 */
c35347f1
LDM
239KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname,
240 const char * const *config_paths)
ecd40ee4
LDM
241{
242 const char *env;
52a7704f 243 struct kmod_ctx *ctx;
d13e606f 244 int err;
ecd40ee4 245
52a7704f
LDM
246 ctx = calloc(1, sizeof(struct kmod_ctx));
247 if (!ctx)
248 return NULL;
ecd40ee4 249
52a7704f 250 ctx->refcount = 1;
1bdd951e
GSB
251 ctx->log_fn = log_filep;
252 ctx->log_data = stderr;
52a7704f 253 ctx->log_priority = LOG_ERR;
ecd40ee4 254
904c63aa 255 ctx->dirname = get_kernel_release(dirname);
221631d5 256
ecd40ee4 257 /* environment overwrites config */
586fc304 258 env = getenv("KMOD_LOG");
ecd40ee4 259 if (env != NULL)
52a7704f 260 kmod_set_log_priority(ctx, log_priority(env));
ecd40ee4 261
cb8d4d3e
GSB
262 if (config_paths == NULL)
263 config_paths = default_config_paths;
264 err = kmod_config_new(ctx, &ctx->config, config_paths);
d13e606f 265 if (err < 0) {
fd186ae9
LDM
266 ERR(ctx, "could not create config\n");
267 goto fail;
268 }
269
822913d7 270 ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL);
fd186ae9
LDM
271 if (ctx->modules_by_name == NULL) {
272 ERR(ctx, "could not create by-name hash\n");
273 goto fail;
d13e606f 274 }
7c2ab358 275
ae6df84a
LDM
276 INFO(ctx, "ctx %p created\n", ctx);
277 DBG(ctx, "log_priority=%d\n", ctx->log_priority);
52a7704f
LDM
278
279 return ctx;
fd186ae9
LDM
280
281fail:
282 free(ctx->modules_by_name);
283 free(ctx->dirname);
284 free(ctx);
285 return NULL;
ecd40ee4
LDM
286}
287
288/**
586fc304
LDM
289 * kmod_ref:
290 * @ctx: kmod library context
ecd40ee4 291 *
586fc304 292 * Take a reference of the kmod library context.
ecd40ee4 293 *
586fc304 294 * Returns: the passed kmod library context
54ba8b34 295 */
586fc304 296KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx)
ecd40ee4
LDM
297{
298 if (ctx == NULL)
299 return NULL;
300 ctx->refcount++;
301 return ctx;
302}
303
304/**
586fc304
LDM
305 * kmod_unref:
306 * @ctx: kmod library context
ecd40ee4 307 *
586fc304 308 * Drop a reference of the kmod library context. If the refcount
ecd40ee4 309 * reaches zero, the resources of the context will be released.
54ba8b34 310 */
586fc304 311KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
ecd40ee4
LDM
312{
313 if (ctx == NULL)
314 return NULL;
4d1e689a
LDM
315
316 if (--ctx->refcount > 0)
ecd40ee4 317 return ctx;
33bb69b9 318
ae6df84a 319 INFO(ctx, "context %p released\n", ctx);
33bb69b9
LDM
320
321 kmod_unload_resources(ctx);
822913d7 322 hash_free(ctx->modules_by_name);
1ce08a56 323 free(ctx->dirname);
d13e606f
GSB
324 if (ctx->config)
325 kmod_config_free(ctx->config);
33bb69b9 326
ecd40ee4
LDM
327 free(ctx);
328 return NULL;
329}
330
331/**
586fc304
LDM
332 * kmod_set_log_fn:
333 * @ctx: kmod library context
ecd40ee4 334 * @log_fn: function to be called for logging messages
b5b4d8e8 335 * @data: data to pass to log function
ecd40ee4
LDM
336 *
337 * The built-in logging writes to stderr. It can be
338 * overridden by a custom function, to plug log messages
339 * into the user's logging functionality.
54ba8b34 340 */
586fc304 341KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx,
1bdd951e 342 void (*log_fn)(void *data,
e4351b05
LDM
343 int priority, const char *file,
344 int line, const char *fn,
1bdd951e
GSB
345 const char *format, va_list args),
346 const void *data)
ecd40ee4 347{
e5c60f1c
GSB
348 if (ctx == NULL)
349 return;
ecd40ee4 350 ctx->log_fn = log_fn;
1bdd951e 351 ctx->log_data = (void *)data;
ae6df84a 352 INFO(ctx, "custom logging function %p registered\n", log_fn);
ecd40ee4
LDM
353}
354
355/**
586fc304
LDM
356 * kmod_get_log_priority:
357 * @ctx: kmod library context
ecd40ee4
LDM
358 *
359 * Returns: the current logging priority
54ba8b34 360 */
6d177553 361KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx)
ecd40ee4 362{
e5c60f1c
GSB
363 if (ctx == NULL)
364 return -1;
ecd40ee4
LDM
365 return ctx->log_priority;
366}
367
368/**
586fc304
LDM
369 * kmod_set_log_priority:
370 * @ctx: kmod library context
ecd40ee4
LDM
371 * @priority: the new logging priority
372 *
373 * Set the current logging priority. The value controls which messages
374 * are logged.
54ba8b34 375 */
586fc304 376KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority)
ecd40ee4 377{
e5c60f1c
GSB
378 if (ctx == NULL)
379 return;
ecd40ee4
LDM
380 ctx->log_priority = priority;
381}
7f3eb0cc 382
fd186ae9 383struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx,
8bdeca11 384 const char *key)
fd186ae9
LDM
385{
386 struct kmod_module *mod;
387
822913d7 388 mod = hash_find(ctx->modules_by_name, key);
fd186ae9 389
8bdeca11 390 DBG(ctx, "get module name='%s' found=%p\n", key, mod);
fd186ae9
LDM
391
392 return mod;
393}
394
8bdeca11
LDM
395void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod,
396 const char *key)
fd186ae9 397{
8bdeca11 398 DBG(ctx, "add %p key='%s'\n", mod, key);
fd186ae9 399
822913d7 400 hash_add(ctx->modules_by_name, key, mod);
fd186ae9
LDM
401}
402
8bdeca11
LDM
403void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod,
404 const char *key)
fd186ae9 405{
8bdeca11 406 DBG(ctx, "del %p key='%s'\n", mod, key);
fd186ae9 407
822913d7 408 hash_del(ctx->modules_by_name, key);
fd186ae9 409}
9ba6f57b 410
a009482c 411static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,
810803db 412 enum kmod_index index_number,
7b30f4f4 413 const char *name,
9ba6f57b
LDM
414 struct kmod_list **list)
415{
6f1bc6e3 416 int err, nmatch = 0;
0fbdfef3 417 struct index_file *idx;
9ba6f57b
LDM
418 struct index_value *realnames, *realname;
419
65a84f55 420 if (ctx->indexes[index_number] != NULL) {
3e245be1 421 DBG(ctx, "use mmaped index '%s' for name=%s\n",
63be91cb 422 index_files[index_number].fn, name);
65a84f55
LDM
423 realnames = index_mm_searchwild(ctx->indexes[index_number],
424 name);
9fd58f30 425 } else {
65a84f55
LDM
426 char fn[PATH_MAX];
427
3b209959 428 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
63be91cb 429 index_files[index_number].fn);
9ba6f57b 430
65a84f55 431 DBG(ctx, "file=%s name=%s\n", fn, name);
9ba6f57b 432
65a84f55
LDM
433 idx = index_file_open(fn);
434 if (idx == NULL)
435 return -ENOSYS;
9ba6f57b 436
65a84f55
LDM
437 realnames = index_searchwild(idx, name);
438 index_file_close(idx);
439 }
4272d087 440
a955f71f 441 for (realname = realnames; realname; realname = realname->next) {
9ba6f57b
LDM
442 struct kmod_module *mod;
443
ee3b3ff2 444 err = kmod_module_new_from_alias(ctx, name, realname->value, &mod);
9ba6f57b 445 if (err < 0) {
dfa96f15
GSB
446 ERR(ctx, "Could not create module for alias=%s realname=%s: %s\n",
447 name, realname->value, strerror(-err));
23fc91c6 448 goto fail;
9ba6f57b
LDM
449 }
450
451 *list = kmod_list_append(*list, mod);
23fc91c6 452 nmatch++;
9ba6f57b
LDM
453 }
454
9ba6f57b 455 index_values_free(realnames);
23fc91c6
LDM
456 return nmatch;
457
458fail:
459 *list = kmod_list_remove_n_latest(*list, nmatch);
9ba6f57b 460 return err;
7b30f4f4
LDM
461
462}
463
7b30f4f4
LDM
464int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name,
465 struct kmod_list **list)
466{
0c010fae 467 if (!strstartswith(name, "symbol:"))
7b30f4f4
LDM
468 return 0;
469
b08314f7
LDM
470 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_SYMBOL,
471 name, list);
9ba6f57b
LDM
472}
473
49e61ca3
LDM
474int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name,
475 struct kmod_list **list)
476{
b08314f7
LDM
477 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_ALIAS,
478 name, list);
49e61ca3
LDM
479}
480
3805274b
LDM
481int kmod_lookup_alias_from_builtin_file(struct kmod_ctx *ctx, const char *name,
482 struct kmod_list **list)
483{
ee1d188f
LDM
484 char *line = NULL;
485 int err = 0;
3805274b 486
ee1d188f 487 assert(*list == NULL);
3805274b 488
ee1d188f
LDM
489 if (ctx->indexes[KMOD_INDEX_MODULES_BUILTIN]) {
490 DBG(ctx, "use mmaped index '%s' modname=%s\n",
491 index_files[KMOD_INDEX_MODULES_BUILTIN].fn,
492 name);
493 line = index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_BUILTIN],
494 name);
495 } else {
496 struct index_file *idx;
497 char fn[PATH_MAX];
498
499 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
500 index_files[KMOD_INDEX_MODULES_BUILTIN].fn);
501 DBG(ctx, "file=%s modname=%s\n", fn, name);
502
503 idx = index_file_open(fn);
504 if (idx == NULL) {
505 DBG(ctx, "could not open builtin file '%s'\n", fn);
506 goto finish;
507 }
508
509 line = index_search(idx, name);
510 index_file_close(idx);
511 }
512
513 if (line != NULL) {
514 struct kmod_module *mod;
515
516 err = kmod_module_new_from_name(ctx, name, &mod);
517 if (err < 0) {
518 ERR(ctx, "Could not create module from name %s: %s\n",
519 name, strerror(-err));
520 goto finish;
521 }
522
523 kmod_module_set_builtin(mod, true);
524 *list = kmod_list_append(*list, mod);
525 if (*list == NULL)
526 err = -ENOMEM;
3805274b
LDM
527 }
528
ee1d188f
LDM
529finish:
530 free(line);
3805274b
LDM
531 return err;
532}
533
671d4894 534char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name)
1eb2ef69
LDM
535{
536 struct index_file *idx;
537 char fn[PATH_MAX];
538 char *line;
539
b08314f7 540 if (ctx->indexes[KMOD_INDEX_MODULES_DEP]) {
85132101 541 DBG(ctx, "use mmaped index '%s' modname=%s\n",
b08314f7
LDM
542 index_files[KMOD_INDEX_MODULES_DEP].fn, name);
543 return index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_DEP],
544 name);
85132101
GSB
545 }
546
a4a75029 547 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
b08314f7 548 index_files[KMOD_INDEX_MODULES_DEP].fn);
1eb2ef69
LDM
549
550 DBG(ctx, "file=%s modname=%s\n", fn, name);
551
552 idx = index_file_open(fn);
553 if (idx == NULL) {
adca3cd2 554 DBG(ctx, "could not open moddep file '%s'\n", fn);
1eb2ef69
LDM
555 return NULL;
556 }
557
558 line = index_search(idx, name);
559 index_file_close(idx);
560
561 return line;
562}
563
64700e47
LDM
564int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name,
565 struct kmod_list **list)
566{
1eb2ef69 567 char *line;
64700e47
LDM
568 int n = 0;
569
570 /*
571 * Module names do not contain ':'. Return early if we know it will
572 * not be found.
573 */
574 if (strchr(name, ':'))
575 return 0;
576
671d4894 577 line = kmod_search_moddep(ctx, name);
64700e47
LDM
578 if (line != NULL) {
579 struct kmod_module *mod;
580
581 n = kmod_module_new_from_name(ctx, name, &mod);
582 if (n < 0) {
dfa96f15
GSB
583 ERR(ctx, "Could not create module from name %s: %s\n",
584 name, strerror(-n));
64700e47
LDM
585 goto finish;
586 }
587
588 *list = kmod_list_append(*list, mod);
671d4894 589 kmod_module_parse_depline(mod, line);
64700e47
LDM
590 }
591
592finish:
593 free(line);
64700e47
LDM
594
595 return n;
596}
597
7f3eb0cc
LDM
598int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name,
599 struct kmod_list **list)
600{
d13e606f 601 struct kmod_config *config = ctx->config;
7f3eb0cc 602 struct kmod_list *l;
23fc91c6 603 int err, nmatch = 0;
7f3eb0cc
LDM
604
605 kmod_list_foreach(l, config->aliases) {
606 const char *aliasname = kmod_alias_get_name(l);
607 const char *modname = kmod_alias_get_modname(l);
608
609 if (fnmatch(aliasname, name, 0) == 0) {
610 struct kmod_module *mod;
611
ee3b3ff2
LDM
612 err = kmod_module_new_from_alias(ctx, aliasname,
613 modname, &mod);
7f3eb0cc 614 if (err < 0) {
dfa96f15
GSB
615 ERR(ctx, "Could not create module for alias=%s modname=%s: %s\n",
616 name, modname, strerror(-err));
23fc91c6 617 goto fail;
7f3eb0cc
LDM
618 }
619
620 *list = kmod_list_append(*list, mod);
23fc91c6 621 nmatch++;
7f3eb0cc
LDM
622 }
623 }
624
23fc91c6
LDM
625 return nmatch;
626
627fail:
628 *list = kmod_list_remove_n_latest(*list, nmatch);
629 return err;
7f3eb0cc 630}
1487a64f 631
f4fc5523
LDM
632int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name,
633 struct kmod_list **list)
634{
635 struct kmod_config *config = ctx->config;
636 struct kmod_list *l, *node;
637 int err, nmatch = 0;
638
639 kmod_list_foreach(l, config->install_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_install_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 if (nmatch)
673 return nmatch;
674
675 kmod_list_foreach(l, config->remove_commands) {
676 const char *modname = kmod_command_get_modname(l);
677
678 if (streq(modname, name)) {
679 const char *cmd = kmod_command_get_command(l);
680 struct kmod_module *mod;
681
682 err = kmod_module_new_from_name(ctx, modname, &mod);
683 if (err < 0) {
dfa96f15
GSB
684 ERR(ctx, "Could not create module from name %s: %s\n",
685 modname, strerror(-err));
f4fc5523
LDM
686 return err;
687 }
688
689 node = kmod_list_append(*list, mod);
690 if (node == NULL) {
691 ERR(ctx, "out of memory\n");
692 return -ENOMEM;
693 }
694
695 *list = node;
696 nmatch = 1;
697
698 kmod_module_set_remove_commands(mod, cmd);
699
700 /*
701 * match only the first one, like modprobe from
702 * module-init-tools does
703 */
704 break;
705 }
706 }
707
708 return nmatch;
709}
710
ece09aac
LDM
711void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited)
712{
713 struct hash_iter iter;
714 const void *v;
715
716 hash_iter_init(ctx->modules_by_name, &iter);
717 while (hash_iter_next(&iter, NULL, &v))
718 kmod_module_set_visited((struct kmod_module *)v, visited);
719}
720
c4dc3ca8
LDM
721static bool is_cache_invalid(const char *path, unsigned long long stamp)
722{
723 struct stat st;
724
725 if (stat(path, &st) < 0)
726 return true;
727
6068aaae 728 if (stamp != stat_mstamp(&st))
c4dc3ca8
LDM
729 return true;
730
731 return false;
732}
733
734/**
735 * kmod_validate_resources:
736 * @ctx: kmod library context
737 *
738 * Check if indexes and configuration files changed on disk and the current
739 * context is not valid anymore.
740 *
f4cc6ea5 741 * Returns: KMOD_RESOURCES_OK if resources are still valid,
c4dc3ca8
LDM
742 * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call
743 * kmod_unload_resources() and kmod_load_resources() or
744 * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created.
745 */
746KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx)
747{
748 struct kmod_list *l;
749 size_t i;
750
751 if (ctx == NULL || ctx->config == NULL)
752 return KMOD_RESOURCES_MUST_RECREATE;
753
754 kmod_list_foreach(l, ctx->config->paths) {
755 struct kmod_config_path *cf = l->data;
756
757 if (is_cache_invalid(cf->path, cf->stamp))
758 return KMOD_RESOURCES_MUST_RECREATE;
759 }
760
b08314f7 761 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
c4dc3ca8
LDM
762 char path[PATH_MAX];
763
764 if (ctx->indexes[i] == NULL)
765 continue;
766
767 snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
63be91cb 768 index_files[i].fn);
c4dc3ca8
LDM
769
770 if (is_cache_invalid(path, ctx->indexes_stamp[i]))
771 return KMOD_RESOURCES_MUST_RELOAD;
772 }
773
774 return KMOD_RESOURCES_OK;
775}
776
be5a6dea
LDM
777/**
778 * kmod_load_resources:
779 * @ctx: kmod library context
780 *
781 * Load indexes and keep them open in @ctx. This way it's faster to lookup
782 * information within the indexes. If this function is not called before a
783 * search, the necessary index is always opened and closed.
784 *
785 * If user will do more than one or two lookups, insertions, deletions, most
786 * likely it's good to call this function first. Particularly in a daemon like
787 * udev that on bootup issues hundreds of calls to lookup the index, calling
788 * this function will speedup the searches.
789 *
790 * Returns: 0 on success or < 0 otherwise.
791 */
33bb69b9
LDM
792KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
793{
794 size_t i;
795
796 if (ctx == NULL)
797 return -ENOENT;
798
b08314f7 799 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
6de8f6e9 800 char path[PATH_MAX];
3e676766 801
16ca3666 802 if (ctx->indexes[i] != NULL) {
63be91cb
LDM
803 INFO(ctx, "Index %s already loaded\n",
804 index_files[i].fn);
3e676766
LDM
805 continue;
806 }
79d57fcb 807
6de8f6e9 808 snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
63be91cb 809 index_files[i].fn);
2e2e252b 810 ctx->indexes[i] = index_mm_open(ctx, path,
9fd58f30 811 &ctx->indexes_stamp[i]);
3e676766
LDM
812 if (ctx->indexes[i] == NULL)
813 goto fail;
33bb69b9
LDM
814 }
815
816 return 0;
817
818fail:
819 kmod_unload_resources(ctx);
820 return -ENOMEM;
821}
822
be5a6dea
LDM
823/**
824 * kmod_unload_resources:
825 * @ctx: kmod library context
826 *
827 * Unload all the indexes. This will free the resources to maintain the index
828 * open and all subsequent searches will need to open and close the index.
829 *
830 * User is free to call kmod_load_resources() and kmod_unload_resources() as
831 * many times as wanted during the lifecycle of @ctx. For example, if a daemon
832 * knows that when starting up it will lookup a lot of modules, it could call
833 * kmod_load_resources() and after the first burst of searches is gone, it
834 * could free the resources by calling kmod_unload_resources().
835 *
836 * Returns: 0 on success or < 0 otherwise.
837 */
33bb69b9
LDM
838KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
839{
840 size_t i;
841
842 if (ctx == NULL)
843 return;
844
b08314f7 845 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
33bb69b9
LDM
846 if (ctx->indexes[i] != NULL) {
847 index_mm_close(ctx->indexes[i]);
848 ctx->indexes[i] = NULL;
9fd58f30 849 ctx->indexes_stamp[i] = 0;
33bb69b9
LDM
850 }
851 }
852}
bd3f5535 853
0224482e
LDM
854/**
855 * kmod_dump_index:
856 * @ctx: kmod library context
857 * @type: index to dump
858 * @fd: file descriptor to dump index to
859 *
09e9ae58
LDM
860 * Dump index to file descriptor. Note that this function doesn't use stdio.h
861 * so call fflush() before calling this function to be sure data is written in
862 * order.
0224482e
LDM
863 *
864 * Returns: 0 on success or < 0 otherwise.
865 */
758428a7
LDM
866KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,
867 int fd)
868{
869 if (ctx == NULL)
870 return -ENOSYS;
871
872 if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE)
873 return -ENOENT;
874
875 if (ctx->indexes[type] != NULL) {
876 DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn);
877 index_mm_dump(ctx->indexes[type], fd,
878 index_files[type].prefix);
879 } else {
880 char fn[PATH_MAX];
881 struct index_file *idx;
882
883 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
884 index_files[type].fn);
885
886 DBG(ctx, "file=%s\n", fn);
887
888 idx = index_file_open(fn);
889 if (idx == NULL)
890 return -ENOSYS;
891
892 index_dump(idx, fd, index_files[type].prefix);
893 index_file_close(idx);
894 }
895
896 return 0;
897}
898
e7fc2c86 899const struct kmod_config *kmod_get_config(const struct kmod_ctx *ctx)
c1c9c446 900{
e7fc2c86 901 return ctx->config;
8b5ee618 902}