blob: d5bcab1af8552dfcb71eb8a1cdecaf01ec07851b [file] [log] [blame]
Lucas De Marchi586fc302011-11-21 14:35:35 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchia66a6a92012-01-09 00:40:50 -02004 * Copyright (C) 2011-2012 ProFUSION embedded systems
Lucas De Marchi586fc302011-11-21 14:35:35 -02005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02008 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchi586fc302011-11-21 14:35:35 -020010 *
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 */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <stddef.h>
24#include <stdarg.h>
Lucas De Marchi1eb2ef62011-12-05 19:58:39 -020025#include <limits.h>
Lucas De Marchi586fc302011-11-21 14:35:35 -020026#include <unistd.h>
27#include <errno.h>
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -020028#include <fnmatch.h>
Lucas De Marchi586fc302011-11-21 14:35:35 -020029#include <string.h>
30#include <ctype.h>
Lucas De Marchi221631d2011-11-24 16:41:01 -020031#include <sys/utsname.h>
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -020032#include <sys/stat.h>
Lucas De Marchi586fc302011-11-21 14:35:35 -020033
34#include "libkmod.h"
35#include "libkmod-private.h"
Lucas De Marchi9ba6f572011-11-30 20:31:45 -020036#include "libkmod-index.h"
Lucas De Marchi586fc302011-11-21 14:35:35 -020037
Lucas De Marchifd186ae2011-12-06 03:38:37 -020038#define KMOD_HASH_SIZE (256)
39#define KMOD_LRU_MAX (128)
Lucas De Marchib08314f2012-01-16 12:01:48 -020040#define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_SYMBOL + 1
Lucas De Marchifd186ae2011-12-06 03:38:37 -020041
Lucas De Marchi586fc302011-11-21 14:35:35 -020042/**
43 * SECTION:libkmod
44 * @short_description: libkmod context
45 *
46 * The context contains the default values for the library user,
47 * and is passed to all library operations.
48 */
49
Lucas De Marchi63be91c2012-01-16 10:43:34 -020050static struct _index_files {
51 const char *fn;
52 const char *prefix;
53} index_files[] = {
Lucas De Marchib08314f2012-01-16 12:01:48 -020054 [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 "},
Lucas De Marchia4a75022011-12-08 14:56:48 -020057};
58
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -020059static const char *default_config_paths[] = {
60 "/run/modprobe.d",
Kay Sieversa308abe2011-12-20 17:58:05 +010061 SYSCONFDIR "/modprobe.d",
62 ROOTPREFIX "/lib/modprobe.d",
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -020063 NULL
64};
65
Lucas De Marchi586fc302011-11-21 14:35:35 -020066/**
67 * kmod_ctx:
68 *
69 * Opaque object representing the library context.
70 */
71struct kmod_ctx {
72 int refcount;
Gustavo Sverzut Barbieri8d3f3ef2011-12-02 21:10:24 -020073 int log_priority;
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -020074 void (*log_fn)(void *data,
Lucas De Marchie4351b02011-11-21 14:59:23 -020075 int priority, const char *file, int line,
76 const char *fn, const char *format, va_list args);
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -020077 void *log_data;
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -020078 const void *userdata;
79 char *dirname;
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -020080 struct kmod_config *config;
Lucas De Marchi822913d2011-12-27 12:13:54 -020081 struct hash *modules_by_name;
Lucas De Marchib08314f2012-01-16 12:01:48 -020082 struct index_mm *indexes[_KMOD_INDEX_MODULES_SIZE];
83 unsigned long long indexes_stamp[_KMOD_INDEX_MODULES_SIZE];
Lucas De Marchi586fc302011-11-21 14:35:35 -020084};
85
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -020086void kmod_log(const struct kmod_ctx *ctx,
Lucas De Marchie4351b02011-11-21 14:59:23 -020087 int priority, const char *file, int line, const char *fn,
88 const char *format, ...)
Lucas De Marchi586fc302011-11-21 14:35:35 -020089{
90 va_list args;
91
Gustavo Sverzut Barbierie5c60f12011-12-08 13:58:46 -020092 if (ctx->log_fn == NULL)
93 return;
94
Lucas De Marchi586fc302011-11-21 14:35:35 -020095 va_start(args, format);
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -020096 ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args);
Lucas De Marchi586fc302011-11-21 14:35:35 -020097 va_end(args);
98}
99
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200100static void log_filep(void *data,
Lucas De Marchie4351b02011-11-21 14:59:23 -0200101 int priority, const char *file, int line,
102 const char *fn, const char *format, va_list args)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200103{
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200104 FILE *fp = data;
105 fprintf(fp, "libkmod: %s: ", fn);
106 vfprintf(fp, format, args);
Lucas De Marchi586fc302011-11-21 14:35:35 -0200107}
108
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200109const char *kmod_get_dirname(const struct kmod_ctx *ctx)
Lucas De Marchi221631d2011-11-24 16:41:01 -0200110{
111 return ctx->dirname;
112}
113
Lucas De Marchi586fc302011-11-21 14:35:35 -0200114/**
115 * kmod_get_userdata:
116 * @ctx: kmod library context
117 *
118 * Retrieve stored data pointer from library context. This might be useful
Lucas De Marchibe5a6de2011-12-14 03:44:38 -0200119 * to access from callbacks.
Lucas De Marchi586fc302011-11-21 14:35:35 -0200120 *
121 * Returns: stored userdata
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200122 */
Lucas De Marchi6d177552011-11-23 11:52:30 -0200123KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200124{
125 if (ctx == NULL)
126 return NULL;
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200127 return (void *)ctx->userdata;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200128}
129
130/**
131 * kmod_set_userdata:
132 * @ctx: kmod library context
133 * @userdata: data pointer
134 *
135 * Store custom @userdata in the library context.
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200136 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200137KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200138{
139 if (ctx == NULL)
140 return;
141 ctx->userdata = userdata;
142}
143
144static int log_priority(const char *priority)
145{
146 char *endptr;
147 int prio;
148
149 prio = strtol(priority, &endptr, 10);
150 if (endptr[0] == '\0' || isspace(endptr[0]))
151 return prio;
152 if (strncmp(priority, "err", 3) == 0)
153 return LOG_ERR;
154 if (strncmp(priority, "info", 4) == 0)
155 return LOG_INFO;
156 if (strncmp(priority, "debug", 5) == 0)
157 return LOG_DEBUG;
158 return 0;
159}
160
Kay Sieversa308abe2011-12-20 17:58:05 +0100161static const char *dirname_default_prefix = ROOTPREFIX "/lib/modules";
Lucas De Marchi904c63a2011-11-30 20:27:50 -0200162
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200163static char *get_kernel_release(const char *dirname)
Lucas De Marchi221631d2011-11-24 16:41:01 -0200164{
165 struct utsname u;
Lucas De Marchi904c63a2011-11-30 20:27:50 -0200166 char *p;
167
168 if (dirname != NULL)
Lucas De Marchi2e092e12012-01-14 02:31:51 -0200169 return path_make_absolute_cwd(dirname);
Lucas De Marchi221631d2011-11-24 16:41:01 -0200170
171 if (uname(&u) < 0)
172 return NULL;
173
Lucas De Marchi904c63a2011-11-30 20:27:50 -0200174 if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0)
175 return NULL;
176
177 return p;
Lucas De Marchi221631d2011-11-24 16:41:01 -0200178}
179
Lucas De Marchi586fc302011-11-21 14:35:35 -0200180/**
181 * kmod_new:
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200182 * @dirname: what to consider as linux module's directory, if NULL
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -0200183 * defaults to $rootprefix/lib/modules/`uname -r`. If it's relative,
184 * it's treated as relative to current the current working
185 * directory. Otherwise, give an absolute dirname.
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200186 * @config_paths: ordered array of paths (directories or files) where
Lucas De Marchibe5a6de2011-12-14 03:44:38 -0200187 * to load from user-defined configuration parameters such as
188 * alias, blacklists, commands (install, remove). If
189 * NULL defaults to /run/modprobe.d, /etc/modprobe.d and
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -0200190 * $rootprefix/lib/modprobe.d. Give an empty vector if
191 * configuration should not be read. This array must be null
192 * terminated.
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200193 *
Lucas De Marchie1daa4f2012-01-09 03:09:49 -0200194 * Create kmod library context. This reads the kmod configuration
195 * and fills in the default values.
196 *
197 * The initial refcount is 1, and needs to be decremented to
198 * release the resources of the kmod library context.
199 *
Lucas De Marchi586fc302011-11-21 14:35:35 -0200200 * Returns: a new kmod library context
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200201 */
Lucas De Marchic35347f2011-12-12 10:48:02 -0200202KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname,
203 const char * const *config_paths)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200204{
205 const char *env;
Lucas De Marchi52a77042011-11-21 15:07:27 -0200206 struct kmod_ctx *ctx;
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200207 int err;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200208
Lucas De Marchi52a77042011-11-21 15:07:27 -0200209 ctx = calloc(1, sizeof(struct kmod_ctx));
210 if (!ctx)
211 return NULL;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200212
Lucas De Marchi52a77042011-11-21 15:07:27 -0200213 ctx->refcount = 1;
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200214 ctx->log_fn = log_filep;
215 ctx->log_data = stderr;
Lucas De Marchi52a77042011-11-21 15:07:27 -0200216 ctx->log_priority = LOG_ERR;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200217
Lucas De Marchi904c63a2011-11-30 20:27:50 -0200218 ctx->dirname = get_kernel_release(dirname);
Lucas De Marchi221631d2011-11-24 16:41:01 -0200219
Lucas De Marchi586fc302011-11-21 14:35:35 -0200220 /* environment overwrites config */
221 env = getenv("KMOD_LOG");
222 if (env != NULL)
Lucas De Marchi52a77042011-11-21 15:07:27 -0200223 kmod_set_log_priority(ctx, log_priority(env));
Lucas De Marchi586fc302011-11-21 14:35:35 -0200224
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200225 if (config_paths == NULL)
226 config_paths = default_config_paths;
227 err = kmod_config_new(ctx, &ctx->config, config_paths);
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200228 if (err < 0) {
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200229 ERR(ctx, "could not create config\n");
230 goto fail;
231 }
232
Lucas De Marchi822913d2011-12-27 12:13:54 -0200233 ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200234 if (ctx->modules_by_name == NULL) {
235 ERR(ctx, "could not create by-name hash\n");
236 goto fail;
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200237 }
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200238
Lucas De Marchiae6df842011-11-25 01:05:30 -0200239 INFO(ctx, "ctx %p created\n", ctx);
240 DBG(ctx, "log_priority=%d\n", ctx->log_priority);
Lucas De Marchi52a77042011-11-21 15:07:27 -0200241
242 return ctx;
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200243
244fail:
245 free(ctx->modules_by_name);
246 free(ctx->dirname);
247 free(ctx);
248 return NULL;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200249}
250
251/**
252 * kmod_ref:
253 * @ctx: kmod library context
254 *
255 * Take a reference of the kmod library context.
256 *
257 * Returns: the passed kmod library context
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200258 */
Lucas De Marchi586fc302011-11-21 14:35:35 -0200259KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx)
260{
261 if (ctx == NULL)
262 return NULL;
263 ctx->refcount++;
264 return ctx;
265}
266
267/**
268 * kmod_unref:
269 * @ctx: kmod library context
270 *
271 * Drop a reference of the kmod library context. If the refcount
272 * reaches zero, the resources of the context will be released.
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200273 */
Lucas De Marchi586fc302011-11-21 14:35:35 -0200274KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
275{
276 if (ctx == NULL)
277 return NULL;
Lucas De Marchi4d1e6892011-11-24 15:41:48 -0200278
279 if (--ctx->refcount > 0)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200280 return ctx;
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200281
Lucas De Marchiae6df842011-11-25 01:05:30 -0200282 INFO(ctx, "context %p released\n", ctx);
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200283
284 kmod_unload_resources(ctx);
Lucas De Marchi822913d2011-12-27 12:13:54 -0200285 hash_free(ctx->modules_by_name);
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200286 free(ctx->dirname);
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200287 if (ctx->config)
288 kmod_config_free(ctx->config);
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200289
Lucas De Marchi586fc302011-11-21 14:35:35 -0200290 free(ctx);
291 return NULL;
292}
293
294/**
295 * kmod_set_log_fn:
296 * @ctx: kmod library context
297 * @log_fn: function to be called for logging messages
Lucas De Marchib5b4d8e2012-01-04 21:07:59 -0200298 * @data: data to pass to log function
Lucas De Marchi586fc302011-11-21 14:35:35 -0200299 *
300 * The built-in logging writes to stderr. It can be
301 * overridden by a custom function, to plug log messages
302 * into the user's logging functionality.
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200303 */
Lucas De Marchi586fc302011-11-21 14:35:35 -0200304KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx,
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200305 void (*log_fn)(void *data,
Lucas De Marchie4351b02011-11-21 14:59:23 -0200306 int priority, const char *file,
307 int line, const char *fn,
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200308 const char *format, va_list args),
309 const void *data)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200310{
Gustavo Sverzut Barbierie5c60f12011-12-08 13:58:46 -0200311 if (ctx == NULL)
312 return;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200313 ctx->log_fn = log_fn;
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200314 ctx->log_data = (void *)data;
Lucas De Marchiae6df842011-11-25 01:05:30 -0200315 INFO(ctx, "custom logging function %p registered\n", log_fn);
Lucas De Marchi586fc302011-11-21 14:35:35 -0200316}
317
318/**
319 * kmod_get_log_priority:
320 * @ctx: kmod library context
321 *
322 * Returns: the current logging priority
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200323 */
Lucas De Marchi6d177552011-11-23 11:52:30 -0200324KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200325{
Gustavo Sverzut Barbierie5c60f12011-12-08 13:58:46 -0200326 if (ctx == NULL)
327 return -1;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200328 return ctx->log_priority;
329}
330
331/**
332 * kmod_set_log_priority:
333 * @ctx: kmod library context
334 * @priority: the new logging priority
335 *
336 * Set the current logging priority. The value controls which messages
337 * are logged.
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200338 */
Lucas De Marchi586fc302011-11-21 14:35:35 -0200339KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority)
340{
Gustavo Sverzut Barbierie5c60f12011-12-08 13:58:46 -0200341 if (ctx == NULL)
342 return;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200343 ctx->log_priority = priority;
344}
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200345
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200346struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx,
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200347 const char *key)
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200348{
349 struct kmod_module *mod;
350
Lucas De Marchi822913d2011-12-27 12:13:54 -0200351 mod = hash_find(ctx->modules_by_name, key);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200352
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200353 DBG(ctx, "get module name='%s' found=%p\n", key, mod);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200354
355 return mod;
356}
357
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200358void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod,
359 const char *key)
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200360{
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200361 DBG(ctx, "add %p key='%s'\n", mod, key);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200362
Lucas De Marchi822913d2011-12-27 12:13:54 -0200363 hash_add(ctx->modules_by_name, key, mod);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200364}
365
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200366void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod,
367 const char *key)
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200368{
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200369 DBG(ctx, "del %p key='%s'\n", mod, key);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200370
Lucas De Marchi822913d2011-12-27 12:13:54 -0200371 hash_del(ctx->modules_by_name, key);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200372}
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200373
Lucas De Marchia0094822011-12-02 09:53:31 -0200374static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,
Lucas De Marchi810803d2011-12-09 16:06:04 -0200375 enum kmod_index index_number,
Lucas De Marchi7b30f4f2011-12-01 16:25:37 -0200376 const char *name,
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200377 struct kmod_list **list)
378{
Lucas De Marchi6f1bc6e2011-12-02 10:02:05 -0200379 int err, nmatch = 0;
Lucas De Marchi0fbdfef2011-12-02 09:56:22 -0200380 struct index_file *idx;
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200381 struct index_value *realnames, *realname;
382
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200383 if (ctx->indexes[index_number] != NULL) {
Gustavo Sverzut Barbieri3e245be2011-12-10 09:28:42 -0200384 DBG(ctx, "use mmaped index '%s' for name=%s\n",
Lucas De Marchi63be91c2012-01-16 10:43:34 -0200385 index_files[index_number].fn, name);
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200386 realnames = index_mm_searchwild(ctx->indexes[index_number],
387 name);
Lucas De Marchi9fd58f32011-12-31 18:53:24 -0200388 } else {
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200389 char fn[PATH_MAX];
390
Gustavo Sverzut Barbieri3b209952011-12-10 09:21:03 -0200391 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
Lucas De Marchi63be91c2012-01-16 10:43:34 -0200392 index_files[index_number].fn);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200393
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200394 DBG(ctx, "file=%s name=%s\n", fn, name);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200395
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200396 idx = index_file_open(fn);
397 if (idx == NULL)
398 return -ENOSYS;
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200399
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200400 realnames = index_searchwild(idx, name);
401 index_file_close(idx);
402 }
Lucas De Marchi4272d082011-12-09 15:33:37 -0200403
Ulisses Furquima955f712011-12-15 19:17:49 -0200404 for (realname = realnames; realname; realname = realname->next) {
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200405 struct kmod_module *mod;
406
Lucas De Marchiee3b3ff2011-12-13 14:20:48 -0200407 err = kmod_module_new_from_alias(ctx, name, realname->value, &mod);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200408 if (err < 0) {
409 ERR(ctx, "%s\n", strerror(-err));
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200410 goto fail;
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200411 }
412
413 *list = kmod_list_append(*list, mod);
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200414 nmatch++;
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200415 }
416
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200417 index_values_free(realnames);
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200418 return nmatch;
419
420fail:
421 *list = kmod_list_remove_n_latest(*list, nmatch);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200422 return err;
Lucas De Marchi7b30f4f2011-12-01 16:25:37 -0200423
424}
425
Lucas De Marchi7b30f4f2011-12-01 16:25:37 -0200426int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name,
427 struct kmod_list **list)
428{
Lucas De Marchi0c010fa2011-12-28 13:33:26 -0200429 if (!strstartswith(name, "symbol:"))
Lucas De Marchi7b30f4f2011-12-01 16:25:37 -0200430 return 0;
431
Lucas De Marchib08314f2012-01-16 12:01:48 -0200432 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_SYMBOL,
433 name, list);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200434}
435
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200436int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name,
437 struct kmod_list **list)
438{
Lucas De Marchib08314f2012-01-16 12:01:48 -0200439 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_ALIAS,
440 name, list);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200441}
442
Lucas De Marchi671d4892011-12-05 20:23:05 -0200443char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name)
Lucas De Marchi1eb2ef62011-12-05 19:58:39 -0200444{
445 struct index_file *idx;
446 char fn[PATH_MAX];
447 char *line;
448
Lucas De Marchib08314f2012-01-16 12:01:48 -0200449 if (ctx->indexes[KMOD_INDEX_MODULES_DEP]) {
Gustavo Sverzut Barbieri85132102011-12-10 09:26:27 -0200450 DBG(ctx, "use mmaped index '%s' modname=%s\n",
Lucas De Marchib08314f2012-01-16 12:01:48 -0200451 index_files[KMOD_INDEX_MODULES_DEP].fn, name);
452 return index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_DEP],
453 name);
Gustavo Sverzut Barbieri85132102011-12-10 09:26:27 -0200454 }
455
Lucas De Marchia4a75022011-12-08 14:56:48 -0200456 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
Lucas De Marchib08314f2012-01-16 12:01:48 -0200457 index_files[KMOD_INDEX_MODULES_DEP].fn);
Lucas De Marchi1eb2ef62011-12-05 19:58:39 -0200458
459 DBG(ctx, "file=%s modname=%s\n", fn, name);
460
461 idx = index_file_open(fn);
462 if (idx == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500463 ERR(ctx, "could not open moddep file '%s'\n", fn);
Lucas De Marchi1eb2ef62011-12-05 19:58:39 -0200464 return NULL;
465 }
466
467 line = index_search(idx, name);
468 index_file_close(idx);
469
470 return line;
471}
472
Lucas De Marchi64700e42011-12-01 15:57:53 -0200473int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name,
474 struct kmod_list **list)
475{
Lucas De Marchi1eb2ef62011-12-05 19:58:39 -0200476 char *line;
Lucas De Marchi64700e42011-12-01 15:57:53 -0200477 int n = 0;
478
479 /*
480 * Module names do not contain ':'. Return early if we know it will
481 * not be found.
482 */
483 if (strchr(name, ':'))
484 return 0;
485
Lucas De Marchi671d4892011-12-05 20:23:05 -0200486 line = kmod_search_moddep(ctx, name);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200487 if (line != NULL) {
488 struct kmod_module *mod;
489
490 n = kmod_module_new_from_name(ctx, name, &mod);
491 if (n < 0) {
492 ERR(ctx, "%s\n", strerror(-n));
493 goto finish;
494 }
495
496 *list = kmod_list_append(*list, mod);
Lucas De Marchi671d4892011-12-05 20:23:05 -0200497 kmod_module_parse_depline(mod, line);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200498 }
499
500finish:
501 free(line);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200502
503 return n;
504}
505
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200506int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name,
507 struct kmod_list **list)
508{
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200509 struct kmod_config *config = ctx->config;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200510 struct kmod_list *l;
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200511 int err, nmatch = 0;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200512
513 kmod_list_foreach(l, config->aliases) {
514 const char *aliasname = kmod_alias_get_name(l);
515 const char *modname = kmod_alias_get_modname(l);
516
517 if (fnmatch(aliasname, name, 0) == 0) {
518 struct kmod_module *mod;
519
Lucas De Marchiee3b3ff2011-12-13 14:20:48 -0200520 err = kmod_module_new_from_alias(ctx, aliasname,
521 modname, &mod);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200522 if (err < 0) {
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200523 ERR(ctx, "%s\n", strerror(-err));
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200524 goto fail;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200525 }
526
527 *list = kmod_list_append(*list, mod);
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200528 nmatch++;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200529 }
530 }
531
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200532 return nmatch;
533
534fail:
535 *list = kmod_list_remove_n_latest(*list, nmatch);
536 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200537}
Gustavo Sverzut Barbieri1487a642011-12-08 05:17:43 -0200538
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200539int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name,
540 struct kmod_list **list)
541{
542 struct kmod_config *config = ctx->config;
543 struct kmod_list *l, *node;
544 int err, nmatch = 0;
545
546 kmod_list_foreach(l, config->install_commands) {
547 const char *modname = kmod_command_get_modname(l);
548
549 if (streq(modname, name)) {
550 const char *cmd = kmod_command_get_command(l);
551 struct kmod_module *mod;
552
553 err = kmod_module_new_from_name(ctx, modname, &mod);
554 if (err < 0) {
555 ERR(ctx, "%s\n", strerror(-err));
556 return err;
557 }
558
559 node = kmod_list_append(*list, mod);
560 if (node == NULL) {
561 ERR(ctx, "out of memory\n");
562 return -ENOMEM;
563 }
564
565 *list = node;
566 nmatch = 1;
567
568 kmod_module_set_install_commands(mod, cmd);
569
570 /*
571 * match only the first one, like modprobe from
572 * module-init-tools does
573 */
574 break;
575 }
576 }
577
578 if (nmatch)
579 return nmatch;
580
581 kmod_list_foreach(l, config->remove_commands) {
582 const char *modname = kmod_command_get_modname(l);
583
584 if (streq(modname, name)) {
585 const char *cmd = kmod_command_get_command(l);
586 struct kmod_module *mod;
587
588 err = kmod_module_new_from_name(ctx, modname, &mod);
589 if (err < 0) {
590 ERR(ctx, "%s\n", strerror(-err));
591 return err;
592 }
593
594 node = kmod_list_append(*list, mod);
595 if (node == NULL) {
596 ERR(ctx, "out of memory\n");
597 return -ENOMEM;
598 }
599
600 *list = node;
601 nmatch = 1;
602
603 kmod_module_set_remove_commands(mod, cmd);
604
605 /*
606 * match only the first one, like modprobe from
607 * module-init-tools does
608 */
609 break;
610 }
611 }
612
613 return nmatch;
614}
615
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200616void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited)
617{
618 struct hash_iter iter;
619 const void *v;
620
621 hash_iter_init(ctx->modules_by_name, &iter);
622 while (hash_iter_next(&iter, NULL, &v))
623 kmod_module_set_visited((struct kmod_module *)v, visited);
624}
625
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -0200626static bool is_cache_invalid(const char *path, unsigned long long stamp)
627{
628 struct stat st;
629
630 if (stat(path, &st) < 0)
631 return true;
632
Lucas De Marchi6068aaa2012-01-17 12:10:42 -0200633 if (stamp != stat_mstamp(&st))
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -0200634 return true;
635
636 return false;
637}
638
639/**
640 * kmod_validate_resources:
641 * @ctx: kmod library context
642 *
643 * Check if indexes and configuration files changed on disk and the current
644 * context is not valid anymore.
645 *
Lucas De Marchif4cc6ea2012-01-09 02:35:41 -0200646 * Returns: KMOD_RESOURCES_OK if resources are still valid,
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -0200647 * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call
648 * kmod_unload_resources() and kmod_load_resources() or
649 * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created.
650 */
651KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx)
652{
653 struct kmod_list *l;
654 size_t i;
655
656 if (ctx == NULL || ctx->config == NULL)
657 return KMOD_RESOURCES_MUST_RECREATE;
658
659 kmod_list_foreach(l, ctx->config->paths) {
660 struct kmod_config_path *cf = l->data;
661
662 if (is_cache_invalid(cf->path, cf->stamp))
663 return KMOD_RESOURCES_MUST_RECREATE;
664 }
665
Lucas De Marchib08314f2012-01-16 12:01:48 -0200666 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -0200667 char path[PATH_MAX];
668
669 if (ctx->indexes[i] == NULL)
670 continue;
671
672 snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
Lucas De Marchi63be91c2012-01-16 10:43:34 -0200673 index_files[i].fn);
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -0200674
675 if (is_cache_invalid(path, ctx->indexes_stamp[i]))
676 return KMOD_RESOURCES_MUST_RELOAD;
677 }
678
679 return KMOD_RESOURCES_OK;
680}
681
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200682/**
Lucas De Marchibe5a6de2011-12-14 03:44:38 -0200683 * kmod_load_resources:
684 * @ctx: kmod library context
685 *
686 * Load indexes and keep them open in @ctx. This way it's faster to lookup
687 * information within the indexes. If this function is not called before a
688 * search, the necessary index is always opened and closed.
689 *
690 * If user will do more than one or two lookups, insertions, deletions, most
691 * likely it's good to call this function first. Particularly in a daemon like
692 * udev that on bootup issues hundreds of calls to lookup the index, calling
693 * this function will speedup the searches.
694 *
695 * Returns: 0 on success or < 0 otherwise.
696 */
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200697KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
698{
699 size_t i;
700
701 if (ctx == NULL)
702 return -ENOENT;
703
Lucas De Marchib08314f2012-01-16 12:01:48 -0200704 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
Lucas De Marchi6de8f6e2011-12-14 03:58:31 -0200705 char path[PATH_MAX];
Lucas De Marchi3e676762011-12-14 03:53:43 -0200706
Lucas De Marchi16ca3662011-12-20 12:29:13 -0200707 if (ctx->indexes[i] != NULL) {
Lucas De Marchi63be91c2012-01-16 10:43:34 -0200708 INFO(ctx, "Index %s already loaded\n",
709 index_files[i].fn);
Lucas De Marchi3e676762011-12-14 03:53:43 -0200710 continue;
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200711 }
Lucas De Marchi3e676762011-12-14 03:53:43 -0200712
Lucas De Marchi6de8f6e2011-12-14 03:58:31 -0200713 snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
Lucas De Marchi63be91c2012-01-16 10:43:34 -0200714 index_files[i].fn);
Lucas De Marchi9fd58f32011-12-31 18:53:24 -0200715 ctx->indexes[i] = index_mm_open(ctx, path, true,
716 &ctx->indexes_stamp[i]);
Lucas De Marchi3e676762011-12-14 03:53:43 -0200717 if (ctx->indexes[i] == NULL)
718 goto fail;
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200719 }
720
721 return 0;
722
723fail:
724 kmod_unload_resources(ctx);
725 return -ENOMEM;
726}
727
Lucas De Marchibe5a6de2011-12-14 03:44:38 -0200728/**
729 * kmod_unload_resources:
730 * @ctx: kmod library context
731 *
732 * Unload all the indexes. This will free the resources to maintain the index
733 * open and all subsequent searches will need to open and close the index.
734 *
735 * User is free to call kmod_load_resources() and kmod_unload_resources() as
736 * many times as wanted during the lifecycle of @ctx. For example, if a daemon
737 * knows that when starting up it will lookup a lot of modules, it could call
738 * kmod_load_resources() and after the first burst of searches is gone, it
739 * could free the resources by calling kmod_unload_resources().
740 *
741 * Returns: 0 on success or < 0 otherwise.
742 */
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200743KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
744{
745 size_t i;
746
747 if (ctx == NULL)
748 return;
749
Lucas De Marchib08314f2012-01-16 12:01:48 -0200750 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200751 if (ctx->indexes[i] != NULL) {
752 index_mm_close(ctx->indexes[i]);
753 ctx->indexes[i] = NULL;
Lucas De Marchi9fd58f32011-12-31 18:53:24 -0200754 ctx->indexes_stamp[i] = 0;
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200755 }
756 }
757}
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200758
Lucas De Marchi02244822012-01-16 16:43:47 -0200759/**
760 * kmod_dump_index:
761 * @ctx: kmod library context
762 * @type: index to dump
763 * @fd: file descriptor to dump index to
764 *
Lucas De Marchi09e9ae52012-01-17 10:05:02 -0200765 * Dump index to file descriptor. Note that this function doesn't use stdio.h
766 * so call fflush() before calling this function to be sure data is written in
767 * order.
Lucas De Marchi02244822012-01-16 16:43:47 -0200768 *
769 * Returns: 0 on success or < 0 otherwise.
770 */
Lucas De Marchi758428a2012-01-16 15:56:17 -0200771KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,
772 int fd)
773{
774 if (ctx == NULL)
775 return -ENOSYS;
776
777 if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE)
778 return -ENOENT;
779
780 if (ctx->indexes[type] != NULL) {
781 DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn);
782 index_mm_dump(ctx->indexes[type], fd,
783 index_files[type].prefix);
784 } else {
785 char fn[PATH_MAX];
786 struct index_file *idx;
787
788 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
789 index_files[type].fn);
790
791 DBG(ctx, "file=%s\n", fn);
792
793 idx = index_file_open(fn);
794 if (idx == NULL)
795 return -ENOSYS;
796
797 index_dump(idx, fd, index_files[type].prefix);
798 index_file_close(idx);
799 }
800
801 return 0;
802}
803
Lucas De Marchic1c9c442011-12-24 10:50:47 -0200804const struct kmod_list *kmod_get_blacklists(const struct kmod_ctx *ctx)
805{
806 return ctx->config->blacklists;
807}
808
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200809const struct kmod_list *kmod_get_options(const struct kmod_ctx *ctx)
810{
811 return ctx->config->options;
812}
813
814const struct kmod_list *kmod_get_install_commands(const struct kmod_ctx *ctx)
815{
816 return ctx->config->install_commands;
817}
818
819const struct kmod_list *kmod_get_remove_commands(const struct kmod_ctx *ctx)
820{
821 return ctx->config->remove_commands;
822}
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200823
824const struct kmod_list *kmod_get_softdeps(const struct kmod_ctx *ctx)
825{
826 return ctx->config->softdeps;
827}
Lucas De Marchi8b5ee612012-01-13 01:14:46 -0200828
829const struct kmod_list *kmod_get_aliases(const struct kmod_ctx *ctx)
830{
831 return ctx->config->aliases;
832}