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