blob: 69fe431aa4c80ec2ae782625c1d2c80d00b11605 [file] [log] [blame]
Lucas De Marchi586fc302011-11-21 14:35:35 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 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
Lucas De Marchidea2dfe2014-12-25 23:32:03 -020017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Lucas De Marchi586fc302011-11-21 14:35:35 -020018 */
19
Lucas De Marchiee1d1882012-02-27 18:48:02 -030020#include <assert.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030021#include <ctype.h>
Lucas De Marchi586fc302011-11-21 14:35:35 -020022#include <errno.h>
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -020023#include <fnmatch.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030024#include <limits.h>
25#include <stdarg.h>
26#include <stddef.h>
27#include <stdio.h>
28#include <stdlib.h>
Lucas De Marchi586fc302011-11-21 14:35:35 -020029#include <string.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030030#include <unistd.h>
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -020031#include <sys/stat.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030032#include <sys/utsname.h>
Lucas De Marchi586fc302011-11-21 14:35:35 -020033
Lucas De Marchi0db718e2014-10-03 00:29:18 -030034#include <shared/hash.h>
Lucas De Marchi96573a02014-10-03 00:01:35 -030035#include <shared/util.h>
36
Lucas De Marchi586fc302011-11-21 14:35:35 -020037#include "libkmod.h"
Lucas De Marchi83b855a2013-07-04 16:13:11 -030038#include "libkmod-internal.h"
Lucas De Marchi9ba6f572011-11-30 20:31:45 -020039#include "libkmod-index.h"
Lucas De Marchi586fc302011-11-21 14:35:35 -020040
Lucas De Marchifd186ae2011-12-06 03:38:37 -020041#define KMOD_HASH_SIZE (256)
42#define KMOD_LRU_MAX (128)
Lucas De Marchi38052742012-02-16 20:43:16 -020043#define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_BUILTIN + 1
Lucas De Marchifd186ae2011-12-06 03:38:37 -020044
Lucas De Marchi586fc302011-11-21 14:35:35 -020045/**
46 * SECTION:libkmod
47 * @short_description: libkmod context
48 *
49 * The context contains the default values for the library user,
50 * and is passed to all library operations.
51 */
52
Lucas De Marchi63be91c2012-01-16 10:43:34 -020053static struct _index_files {
54 const char *fn;
55 const char *prefix;
56} index_files[] = {
Lucas De Marchib08314f2012-01-16 12:01:48 -020057 [KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep", .prefix = "" },
58 [KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .prefix = "alias " },
59 [KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .prefix = "alias "},
Lucas De Marchi38052742012-02-16 20:43:16 -020060 [KMOD_INDEX_MODULES_BUILTIN] = { .fn = "modules.builtin", .prefix = ""},
Lucas De Marchia4a75022011-12-08 14:56:48 -020061};
62
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -020063static const char *default_config_paths[] = {
Kay Sieversa308abe2011-12-20 17:58:05 +010064 SYSCONFDIR "/modprobe.d",
Lucas De Marchi436da1e2012-03-15 09:19:34 -030065 "/run/modprobe.d",
Dave Reisnerc5b37db2012-09-27 11:00:42 -040066 "/lib/modprobe.d",
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -020067 NULL
68};
69
Lucas De Marchi586fc302011-11-21 14:35:35 -020070/**
71 * kmod_ctx:
72 *
73 * Opaque object representing the library context.
74 */
75struct kmod_ctx {
76 int refcount;
Gustavo Sverzut Barbieri8d3f3ef2011-12-02 21:10:24 -020077 int log_priority;
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -020078 void (*log_fn)(void *data,
Lucas De Marchie4351b02011-11-21 14:59:23 -020079 int priority, const char *file, int line,
80 const char *fn, const char *format, va_list args);
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -020081 void *log_data;
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -020082 const void *userdata;
83 char *dirname;
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -020084 struct kmod_config *config;
Lucas De Marchi822913d2011-12-27 12:13:54 -020085 struct hash *modules_by_name;
Lucas De Marchib08314f2012-01-16 12:01:48 -020086 struct index_mm *indexes[_KMOD_INDEX_MODULES_SIZE];
87 unsigned long long indexes_stamp[_KMOD_INDEX_MODULES_SIZE];
Lucas De Marchi586fc302011-11-21 14:35:35 -020088};
89
Lucas De Marchi71928282012-05-10 20:58:46 -030090void kmod_log(const struct kmod_ctx *ctx,
91 int priority, const char *file, int line, const char *fn,
92 const char *format, ...)
Lucas De Marchi586fc302011-11-21 14:35:35 -020093{
94 va_list args;
95
Gustavo Sverzut Barbierie5c60f12011-12-08 13:58:46 -020096 if (ctx->log_fn == NULL)
97 return;
98
Lucas De Marchi586fc302011-11-21 14:35:35 -020099 va_start(args, format);
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200100 ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args);
Lucas De Marchi586fc302011-11-21 14:35:35 -0200101 va_end(args);
102}
103
Lucas De Marchi1958af82013-04-21 16:16:18 -0300104_printf_format_(6, 0)
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200105static void log_filep(void *data,
Lucas De Marchie4351b02011-11-21 14:59:23 -0200106 int priority, const char *file, int line,
107 const char *fn, const char *format, va_list args)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200108{
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200109 FILE *fp = data;
Gustavo Sverzut Barbierie3cb0902012-01-07 19:25:03 -0200110#ifdef ENABLE_DEBUG
111 char buf[16];
112 const char *priname;
113 switch (priority) {
114 case LOG_EMERG:
115 priname = "EMERGENCY";
116 break;
117 case LOG_ALERT:
118 priname = "ALERT";
119 break;
120 case LOG_CRIT:
121 priname = "CRITICAL";
122 break;
123 case LOG_ERR:
124 priname = "ERROR";
125 break;
126 case LOG_WARNING:
127 priname = "WARNING";
128 break;
129 case LOG_NOTICE:
130 priname = "NOTICE";
131 break;
132 case LOG_INFO:
133 priname = "INFO";
134 break;
135 case LOG_DEBUG:
136 priname = "DEBUG";
137 break;
138 default:
139 snprintf(buf, sizeof(buf), "L:%d", priority);
140 priname = buf;
141 }
142 fprintf(fp, "libkmod: %s %s:%d %s: ", priname, file, line, fn);
143#else
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200144 fprintf(fp, "libkmod: %s: ", fn);
Gustavo Sverzut Barbierie3cb0902012-01-07 19:25:03 -0200145#endif
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200146 vfprintf(fp, format, args);
Lucas De Marchi586fc302011-11-21 14:35:35 -0200147}
148
Lucas De Marchif7f28512015-09-30 15:28:24 -0300149
150/**
151 * kmod_get_dirname:
152 * @ctx: kmod library context
153 *
154 * Retrieve the absolute path used for linux modules in this context. The path
155 * is computed from the arguments to kmod_new().
156 */
157KMOD_EXPORT const char *kmod_get_dirname(const struct kmod_ctx *ctx)
Lucas De Marchi221631d2011-11-24 16:41:01 -0200158{
159 return ctx->dirname;
160}
161
Lucas De Marchi586fc302011-11-21 14:35:35 -0200162/**
163 * kmod_get_userdata:
164 * @ctx: kmod library context
165 *
166 * Retrieve stored data pointer from library context. This might be useful
Lucas De Marchibe5a6de2011-12-14 03:44:38 -0200167 * to access from callbacks.
Lucas De Marchi586fc302011-11-21 14:35:35 -0200168 *
169 * Returns: stored userdata
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200170 */
Lucas De Marchi6d177552011-11-23 11:52:30 -0200171KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200172{
173 if (ctx == NULL)
174 return NULL;
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200175 return (void *)ctx->userdata;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200176}
177
178/**
179 * kmod_set_userdata:
180 * @ctx: kmod library context
181 * @userdata: data pointer
182 *
183 * Store custom @userdata in the library context.
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200184 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200185KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200186{
187 if (ctx == NULL)
188 return;
189 ctx->userdata = userdata;
190}
191
192static int log_priority(const char *priority)
193{
194 char *endptr;
195 int prio;
196
197 prio = strtol(priority, &endptr, 10);
198 if (endptr[0] == '\0' || isspace(endptr[0]))
199 return prio;
200 if (strncmp(priority, "err", 3) == 0)
201 return LOG_ERR;
202 if (strncmp(priority, "info", 4) == 0)
203 return LOG_INFO;
204 if (strncmp(priority, "debug", 5) == 0)
205 return LOG_DEBUG;
206 return 0;
207}
208
Dave Reisnerc5b37db2012-09-27 11:00:42 -0400209static const char *dirname_default_prefix = "/lib/modules";
Lucas De Marchi904c63a2011-11-30 20:27:50 -0200210
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200211static char *get_kernel_release(const char *dirname)
Lucas De Marchi221631d2011-11-24 16:41:01 -0200212{
213 struct utsname u;
Lucas De Marchi904c63a2011-11-30 20:27:50 -0200214 char *p;
215
216 if (dirname != NULL)
Lucas De Marchi2e092e12012-01-14 02:31:51 -0200217 return path_make_absolute_cwd(dirname);
Lucas De Marchi221631d2011-11-24 16:41:01 -0200218
219 if (uname(&u) < 0)
220 return NULL;
221
Lucas De Marchi904c63a2011-11-30 20:27:50 -0200222 if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0)
223 return NULL;
224
225 return p;
Lucas De Marchi221631d2011-11-24 16:41:01 -0200226}
227
Lucas De Marchi586fc302011-11-21 14:35:35 -0200228/**
229 * kmod_new:
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200230 * @dirname: what to consider as linux module's directory, if NULL
Dave Reisnerc5b37db2012-09-27 11:00:42 -0400231 * defaults to /lib/modules/`uname -r`. If it's relative,
Chengwei Yang491c4902013-05-04 17:07:02 +0800232 * it's treated as relative to the current working directory.
233 * Otherwise, give an absolute dirname.
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200234 * @config_paths: ordered array of paths (directories or files) where
Lucas De Marchibe5a6de2011-12-14 03:44:38 -0200235 * to load from user-defined configuration parameters such as
236 * alias, blacklists, commands (install, remove). If
237 * NULL defaults to /run/modprobe.d, /etc/modprobe.d and
Dave Reisnerc5b37db2012-09-27 11:00:42 -0400238 * /lib/modprobe.d. Give an empty vector if configuration should
239 * not be read. This array must be null terminated.
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200240 *
Lucas De Marchie1daa4f2012-01-09 03:09:49 -0200241 * Create kmod library context. This reads the kmod configuration
242 * and fills in the default values.
243 *
244 * The initial refcount is 1, and needs to be decremented to
245 * release the resources of the kmod library context.
246 *
Lucas De Marchi586fc302011-11-21 14:35:35 -0200247 * Returns: a new kmod library context
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200248 */
Lucas De Marchic35347f2011-12-12 10:48:02 -0200249KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname,
250 const char * const *config_paths)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200251{
252 const char *env;
Lucas De Marchi52a77042011-11-21 15:07:27 -0200253 struct kmod_ctx *ctx;
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200254 int err;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200255
Lucas De Marchi52a77042011-11-21 15:07:27 -0200256 ctx = calloc(1, sizeof(struct kmod_ctx));
257 if (!ctx)
258 return NULL;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200259
Lucas De Marchi52a77042011-11-21 15:07:27 -0200260 ctx->refcount = 1;
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200261 ctx->log_fn = log_filep;
262 ctx->log_data = stderr;
Lucas De Marchi52a77042011-11-21 15:07:27 -0200263 ctx->log_priority = LOG_ERR;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200264
Lucas De Marchi904c63a2011-11-30 20:27:50 -0200265 ctx->dirname = get_kernel_release(dirname);
Lucas De Marchi221631d2011-11-24 16:41:01 -0200266
Lucas De Marchi586fc302011-11-21 14:35:35 -0200267 /* environment overwrites config */
Cristian Rodríguez41a51c22013-02-11 15:07:52 -0300268 env = secure_getenv("KMOD_LOG");
Lucas De Marchi586fc302011-11-21 14:35:35 -0200269 if (env != NULL)
Lucas De Marchi52a77042011-11-21 15:07:27 -0200270 kmod_set_log_priority(ctx, log_priority(env));
Lucas De Marchi586fc302011-11-21 14:35:35 -0200271
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200272 if (config_paths == NULL)
273 config_paths = default_config_paths;
274 err = kmod_config_new(ctx, &ctx->config, config_paths);
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200275 if (err < 0) {
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200276 ERR(ctx, "could not create config\n");
277 goto fail;
278 }
279
Lucas De Marchi822913d2011-12-27 12:13:54 -0200280 ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200281 if (ctx->modules_by_name == NULL) {
282 ERR(ctx, "could not create by-name hash\n");
283 goto fail;
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200284 }
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200285
Lucas De Marchiae6df842011-11-25 01:05:30 -0200286 INFO(ctx, "ctx %p created\n", ctx);
287 DBG(ctx, "log_priority=%d\n", ctx->log_priority);
Lucas De Marchi52a77042011-11-21 15:07:27 -0200288
289 return ctx;
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200290
291fail:
292 free(ctx->modules_by_name);
293 free(ctx->dirname);
294 free(ctx);
295 return NULL;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200296}
297
298/**
299 * kmod_ref:
300 * @ctx: kmod library context
301 *
302 * Take a reference of the kmod library context.
303 *
304 * Returns: the passed kmod library context
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200305 */
Lucas De Marchi586fc302011-11-21 14:35:35 -0200306KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx)
307{
308 if (ctx == NULL)
309 return NULL;
310 ctx->refcount++;
311 return ctx;
312}
313
314/**
315 * kmod_unref:
316 * @ctx: kmod library context
317 *
318 * Drop a reference of the kmod library context. If the refcount
319 * reaches zero, the resources of the context will be released.
Chengwei Yang491c4902013-05-04 17:07:02 +0800320 *
321 * Returns: the passed kmod library context or NULL if it's freed
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200322 */
Lucas De Marchi586fc302011-11-21 14:35:35 -0200323KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
324{
325 if (ctx == NULL)
326 return NULL;
Lucas De Marchi4d1e6892011-11-24 15:41:48 -0200327
328 if (--ctx->refcount > 0)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200329 return ctx;
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200330
Lucas De Marchiae6df842011-11-25 01:05:30 -0200331 INFO(ctx, "context %p released\n", ctx);
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200332
333 kmod_unload_resources(ctx);
Lucas De Marchi822913d2011-12-27 12:13:54 -0200334 hash_free(ctx->modules_by_name);
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200335 free(ctx->dirname);
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200336 if (ctx->config)
337 kmod_config_free(ctx->config);
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200338
Lucas De Marchi586fc302011-11-21 14:35:35 -0200339 free(ctx);
340 return NULL;
341}
342
343/**
344 * kmod_set_log_fn:
345 * @ctx: kmod library context
346 * @log_fn: function to be called for logging messages
Lucas De Marchib5b4d8e2012-01-04 21:07:59 -0200347 * @data: data to pass to log function
Lucas De Marchi586fc302011-11-21 14:35:35 -0200348 *
349 * The built-in logging writes to stderr. It can be
350 * overridden by a custom function, to plug log messages
351 * into the user's logging functionality.
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200352 */
Lucas De Marchi586fc302011-11-21 14:35:35 -0200353KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx,
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200354 void (*log_fn)(void *data,
Lucas De Marchie4351b02011-11-21 14:59:23 -0200355 int priority, const char *file,
356 int line, const char *fn,
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200357 const char *format, va_list args),
358 const void *data)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200359{
Gustavo Sverzut Barbierie5c60f12011-12-08 13:58:46 -0200360 if (ctx == NULL)
361 return;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200362 ctx->log_fn = log_fn;
Gustavo Sverzut Barbieri1bdd9512011-12-08 13:47:55 -0200363 ctx->log_data = (void *)data;
Lucas De Marchiae6df842011-11-25 01:05:30 -0200364 INFO(ctx, "custom logging function %p registered\n", log_fn);
Lucas De Marchi586fc302011-11-21 14:35:35 -0200365}
366
367/**
368 * kmod_get_log_priority:
369 * @ctx: kmod library context
370 *
371 * Returns: the current logging priority
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200372 */
Lucas De Marchi6d177552011-11-23 11:52:30 -0200373KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx)
Lucas De Marchi586fc302011-11-21 14:35:35 -0200374{
Gustavo Sverzut Barbierie5c60f12011-12-08 13:58:46 -0200375 if (ctx == NULL)
376 return -1;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200377 return ctx->log_priority;
378}
379
380/**
381 * kmod_set_log_priority:
382 * @ctx: kmod library context
383 * @priority: the new logging priority
384 *
385 * Set the current logging priority. The value controls which messages
386 * are logged.
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200387 */
Lucas De Marchi586fc302011-11-21 14:35:35 -0200388KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority)
389{
Gustavo Sverzut Barbierie5c60f12011-12-08 13:58:46 -0200390 if (ctx == NULL)
391 return;
Lucas De Marchi586fc302011-11-21 14:35:35 -0200392 ctx->log_priority = priority;
393}
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200394
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200395struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx,
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200396 const char *key)
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200397{
398 struct kmod_module *mod;
399
Lucas De Marchi822913d2011-12-27 12:13:54 -0200400 mod = hash_find(ctx->modules_by_name, key);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200401
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200402 DBG(ctx, "get module name='%s' found=%p\n", key, mod);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200403
404 return mod;
405}
406
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200407void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod,
408 const char *key)
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200409{
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200410 DBG(ctx, "add %p key='%s'\n", mod, key);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200411
Lucas De Marchi822913d2011-12-27 12:13:54 -0200412 hash_add(ctx->modules_by_name, key, mod);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200413}
414
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200415void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod,
416 const char *key)
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200417{
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200418 DBG(ctx, "del %p key='%s'\n", mod, key);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200419
Lucas De Marchi822913d2011-12-27 12:13:54 -0200420 hash_del(ctx->modules_by_name, key);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200421}
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200422
Lucas De Marchia0094822011-12-02 09:53:31 -0200423static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,
Lucas De Marchi810803d2011-12-09 16:06:04 -0200424 enum kmod_index index_number,
Lucas De Marchi7b30f4f2011-12-01 16:25:37 -0200425 const char *name,
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200426 struct kmod_list **list)
427{
Lucas De Marchi6f1bc6e2011-12-02 10:02:05 -0200428 int err, nmatch = 0;
Lucas De Marchi0fbdfef2011-12-02 09:56:22 -0200429 struct index_file *idx;
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200430 struct index_value *realnames, *realname;
431
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200432 if (ctx->indexes[index_number] != NULL) {
Gustavo Sverzut Barbieri3e245be2011-12-10 09:28:42 -0200433 DBG(ctx, "use mmaped index '%s' for name=%s\n",
Lucas De Marchi63be91c2012-01-16 10:43:34 -0200434 index_files[index_number].fn, name);
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200435 realnames = index_mm_searchwild(ctx->indexes[index_number],
436 name);
Lucas De Marchi9fd58f32011-12-31 18:53:24 -0200437 } else {
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200438 char fn[PATH_MAX];
439
Gustavo Sverzut Barbieri3b209952011-12-10 09:21:03 -0200440 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
Lucas De Marchi63be91c2012-01-16 10:43:34 -0200441 index_files[index_number].fn);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200442
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200443 DBG(ctx, "file=%s name=%s\n", fn, name);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200444
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200445 idx = index_file_open(fn);
446 if (idx == NULL)
447 return -ENOSYS;
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200448
Lucas De Marchi65a84f52011-12-09 16:11:42 -0200449 realnames = index_searchwild(idx, name);
450 index_file_close(idx);
451 }
Lucas De Marchi4272d082011-12-09 15:33:37 -0200452
Ulisses Furquima955f712011-12-15 19:17:49 -0200453 for (realname = realnames; realname; realname = realname->next) {
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200454 struct kmod_module *mod;
455
Lucas De Marchiee3b3ff2011-12-13 14:20:48 -0200456 err = kmod_module_new_from_alias(ctx, name, realname->value, &mod);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200457 if (err < 0) {
Gustavo Sverzut Barbieridfa96f12012-01-07 19:37:37 -0200458 ERR(ctx, "Could not create module for alias=%s realname=%s: %s\n",
459 name, realname->value, strerror(-err));
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200460 goto fail;
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200461 }
462
463 *list = kmod_list_append(*list, mod);
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200464 nmatch++;
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200465 }
466
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200467 index_values_free(realnames);
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200468 return nmatch;
469
470fail:
471 *list = kmod_list_remove_n_latest(*list, nmatch);
Leandro Pereirae84d9122014-04-28 21:01:48 -0300472 index_values_free(realnames);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200473 return err;
Lucas De Marchi7b30f4f2011-12-01 16:25:37 -0200474
475}
476
Lucas De Marchi7b30f4f2011-12-01 16:25:37 -0200477int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name,
478 struct kmod_list **list)
479{
Lucas De Marchi0c010fa2011-12-28 13:33:26 -0200480 if (!strstartswith(name, "symbol:"))
Lucas De Marchi7b30f4f2011-12-01 16:25:37 -0200481 return 0;
482
Lucas De Marchib08314f2012-01-16 12:01:48 -0200483 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_SYMBOL,
484 name, list);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200485}
486
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200487int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name,
488 struct kmod_list **list)
489{
Lucas De Marchib08314f2012-01-16 12:01:48 -0200490 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_ALIAS,
491 name, list);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200492}
493
Harish Jenny K Nfd44a982015-02-22 15:41:07 -0300494static char *lookup_builtin_file(struct kmod_ctx *ctx, const char *name)
Lucas De Marchi38052742012-02-16 20:43:16 -0200495{
Harish Jenny K Nfd44a982015-02-22 15:41:07 -0300496 char *line;
Lucas De Marchi38052742012-02-16 20:43:16 -0200497
Lucas De Marchiee1d1882012-02-27 18:48:02 -0300498 if (ctx->indexes[KMOD_INDEX_MODULES_BUILTIN]) {
499 DBG(ctx, "use mmaped index '%s' modname=%s\n",
500 index_files[KMOD_INDEX_MODULES_BUILTIN].fn,
501 name);
502 line = index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_BUILTIN],
503 name);
504 } else {
505 struct index_file *idx;
506 char fn[PATH_MAX];
507
508 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
509 index_files[KMOD_INDEX_MODULES_BUILTIN].fn);
510 DBG(ctx, "file=%s modname=%s\n", fn, name);
511
512 idx = index_file_open(fn);
513 if (idx == NULL) {
514 DBG(ctx, "could not open builtin file '%s'\n", fn);
Harish Jenny K Nfd44a982015-02-22 15:41:07 -0300515 return NULL;
Lucas De Marchiee1d1882012-02-27 18:48:02 -0300516 }
517
518 line = index_search(idx, name);
519 index_file_close(idx);
Lucas De Marchi38052742012-02-16 20:43:16 -0200520 }
521
Harish Jenny K Nfd44a982015-02-22 15:41:07 -0300522 return line;
523}
524
525int kmod_lookup_alias_from_builtin_file(struct kmod_ctx *ctx, const char *name,
526 struct kmod_list **list)
527{
528 char *line;
529 int err = 0;
530
531 assert(*list == NULL);
532
533 line = lookup_builtin_file(ctx, name);
Lucas De Marchiee1d1882012-02-27 18:48:02 -0300534 if (line != NULL) {
535 struct kmod_module *mod;
536
537 err = kmod_module_new_from_name(ctx, name, &mod);
538 if (err < 0) {
539 ERR(ctx, "Could not create module from name %s: %s\n",
540 name, strerror(-err));
541 goto finish;
542 }
543
Harish Jenny K Nfd44a982015-02-22 15:41:07 -0300544 /* already mark it as builtin since it's being created from
545 * this index */
Lucas De Marchiee1d1882012-02-27 18:48:02 -0300546 kmod_module_set_builtin(mod, true);
547 *list = kmod_list_append(*list, mod);
548 if (*list == NULL)
549 err = -ENOMEM;
550 }
551
552finish:
553 free(line);
Lucas De Marchi38052742012-02-16 20:43:16 -0200554 return err;
555}
556
Harish Jenny K Nfd44a982015-02-22 15:41:07 -0300557bool kmod_lookup_alias_is_builtin(struct kmod_ctx *ctx, const char *name)
558{
559 _cleanup_free_ char *line;
560
561 line = lookup_builtin_file(ctx, name);
562
563 return line != NULL;
564}
565
Lucas De Marchi671d4892011-12-05 20:23:05 -0200566char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name)
Lucas De Marchi1eb2ef62011-12-05 19:58:39 -0200567{
568 struct index_file *idx;
569 char fn[PATH_MAX];
570 char *line;
571
Lucas De Marchib08314f2012-01-16 12:01:48 -0200572 if (ctx->indexes[KMOD_INDEX_MODULES_DEP]) {
Gustavo Sverzut Barbieri85132102011-12-10 09:26:27 -0200573 DBG(ctx, "use mmaped index '%s' modname=%s\n",
Lucas De Marchib08314f2012-01-16 12:01:48 -0200574 index_files[KMOD_INDEX_MODULES_DEP].fn, name);
575 return index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_DEP],
576 name);
Gustavo Sverzut Barbieri85132102011-12-10 09:26:27 -0200577 }
578
Lucas De Marchia4a75022011-12-08 14:56:48 -0200579 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
Lucas De Marchib08314f2012-01-16 12:01:48 -0200580 index_files[KMOD_INDEX_MODULES_DEP].fn);
Lucas De Marchi1eb2ef62011-12-05 19:58:39 -0200581
582 DBG(ctx, "file=%s modname=%s\n", fn, name);
583
584 idx = index_file_open(fn);
585 if (idx == NULL) {
Lucas De Marchiadca3cd2012-02-17 05:00:09 -0200586 DBG(ctx, "could not open moddep file '%s'\n", fn);
Lucas De Marchi1eb2ef62011-12-05 19:58:39 -0200587 return NULL;
588 }
589
590 line = index_search(idx, name);
591 index_file_close(idx);
592
593 return line;
594}
595
Lucas De Marchi64700e42011-12-01 15:57:53 -0200596int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name,
597 struct kmod_list **list)
598{
Lucas De Marchi1eb2ef62011-12-05 19:58:39 -0200599 char *line;
Lucas De Marchi64700e42011-12-01 15:57:53 -0200600 int n = 0;
601
602 /*
603 * Module names do not contain ':'. Return early if we know it will
604 * not be found.
605 */
606 if (strchr(name, ':'))
607 return 0;
608
Lucas De Marchi671d4892011-12-05 20:23:05 -0200609 line = kmod_search_moddep(ctx, name);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200610 if (line != NULL) {
611 struct kmod_module *mod;
612
613 n = kmod_module_new_from_name(ctx, name, &mod);
614 if (n < 0) {
Gustavo Sverzut Barbieridfa96f12012-01-07 19:37:37 -0200615 ERR(ctx, "Could not create module from name %s: %s\n",
616 name, strerror(-n));
Lucas De Marchi64700e42011-12-01 15:57:53 -0200617 goto finish;
618 }
619
620 *list = kmod_list_append(*list, mod);
Lucas De Marchi671d4892011-12-05 20:23:05 -0200621 kmod_module_parse_depline(mod, line);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200622 }
623
624finish:
625 free(line);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200626
627 return n;
628}
629
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200630int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name,
631 struct kmod_list **list)
632{
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200633 struct kmod_config *config = ctx->config;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200634 struct kmod_list *l;
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200635 int err, nmatch = 0;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200636
637 kmod_list_foreach(l, config->aliases) {
638 const char *aliasname = kmod_alias_get_name(l);
639 const char *modname = kmod_alias_get_modname(l);
640
641 if (fnmatch(aliasname, name, 0) == 0) {
642 struct kmod_module *mod;
643
Lucas De Marchiee3b3ff2011-12-13 14:20:48 -0200644 err = kmod_module_new_from_alias(ctx, aliasname,
645 modname, &mod);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200646 if (err < 0) {
Gustavo Sverzut Barbieridfa96f12012-01-07 19:37:37 -0200647 ERR(ctx, "Could not create module for alias=%s modname=%s: %s\n",
648 name, modname, strerror(-err));
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200649 goto fail;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200650 }
651
652 *list = kmod_list_append(*list, mod);
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200653 nmatch++;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200654 }
655 }
656
Lucas De Marchi23fc91c2011-12-01 15:35:31 -0200657 return nmatch;
658
659fail:
660 *list = kmod_list_remove_n_latest(*list, nmatch);
661 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200662}
Gustavo Sverzut Barbieri1487a642011-12-08 05:17:43 -0200663
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200664int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name,
665 struct kmod_list **list)
666{
667 struct kmod_config *config = ctx->config;
668 struct kmod_list *l, *node;
669 int err, nmatch = 0;
670
671 kmod_list_foreach(l, config->install_commands) {
672 const char *modname = kmod_command_get_modname(l);
673
674 if (streq(modname, name)) {
675 const char *cmd = kmod_command_get_command(l);
676 struct kmod_module *mod;
677
678 err = kmod_module_new_from_name(ctx, modname, &mod);
679 if (err < 0) {
Gustavo Sverzut Barbieridfa96f12012-01-07 19:37:37 -0200680 ERR(ctx, "Could not create module from name %s: %s\n",
681 modname, strerror(-err));
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200682 return err;
683 }
684
685 node = kmod_list_append(*list, mod);
686 if (node == NULL) {
687 ERR(ctx, "out of memory\n");
688 return -ENOMEM;
689 }
690
691 *list = node;
692 nmatch = 1;
693
694 kmod_module_set_install_commands(mod, cmd);
695
696 /*
697 * match only the first one, like modprobe from
698 * module-init-tools does
699 */
700 break;
701 }
702 }
703
704 if (nmatch)
705 return nmatch;
706
707 kmod_list_foreach(l, config->remove_commands) {
708 const char *modname = kmod_command_get_modname(l);
709
710 if (streq(modname, name)) {
711 const char *cmd = kmod_command_get_command(l);
712 struct kmod_module *mod;
713
714 err = kmod_module_new_from_name(ctx, modname, &mod);
715 if (err < 0) {
Gustavo Sverzut Barbieridfa96f12012-01-07 19:37:37 -0200716 ERR(ctx, "Could not create module from name %s: %s\n",
717 modname, strerror(-err));
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200718 return err;
719 }
720
721 node = kmod_list_append(*list, mod);
722 if (node == NULL) {
723 ERR(ctx, "out of memory\n");
724 return -ENOMEM;
725 }
726
727 *list = node;
728 nmatch = 1;
729
730 kmod_module_set_remove_commands(mod, cmd);
731
732 /*
733 * match only the first one, like modprobe from
734 * module-init-tools does
735 */
736 break;
737 }
738 }
739
740 return nmatch;
741}
742
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200743void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited)
744{
745 struct hash_iter iter;
746 const void *v;
747
748 hash_iter_init(ctx->modules_by_name, &iter);
749 while (hash_iter_next(&iter, NULL, &v))
750 kmod_module_set_visited((struct kmod_module *)v, visited);
751}
752
Michal Marek450bd1b2014-03-31 15:18:50 +0200753void kmod_set_modules_required(struct kmod_ctx *ctx, bool required)
754{
755 struct hash_iter iter;
756 const void *v;
757
758 hash_iter_init(ctx->modules_by_name, &iter);
759 while (hash_iter_next(&iter, NULL, &v))
760 kmod_module_set_required((struct kmod_module *)v, required);
761}
762
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -0200763static bool is_cache_invalid(const char *path, unsigned long long stamp)
764{
765 struct stat st;
766
767 if (stat(path, &st) < 0)
768 return true;
769
Lucas De Marchi6068aaa2012-01-17 12:10:42 -0200770 if (stamp != stat_mstamp(&st))
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -0200771 return true;
772
773 return false;
774}
775
776/**
777 * kmod_validate_resources:
778 * @ctx: kmod library context
779 *
780 * Check if indexes and configuration files changed on disk and the current
781 * context is not valid anymore.
782 *
Lucas De Marchif4cc6ea2012-01-09 02:35:41 -0200783 * Returns: KMOD_RESOURCES_OK if resources are still valid,
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -0200784 * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call
785 * kmod_unload_resources() and kmod_load_resources() or
786 * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created.
787 */
788KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx)
789{
790 struct kmod_list *l;
791 size_t i;
792
793 if (ctx == NULL || ctx->config == NULL)
794 return KMOD_RESOURCES_MUST_RECREATE;
795
796 kmod_list_foreach(l, ctx->config->paths) {
797 struct kmod_config_path *cf = l->data;
798
799 if (is_cache_invalid(cf->path, cf->stamp))
800 return KMOD_RESOURCES_MUST_RECREATE;
801 }
802
Lucas De Marchib08314f2012-01-16 12:01:48 -0200803 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -0200804 char path[PATH_MAX];
805
806 if (ctx->indexes[i] == NULL)
807 continue;
808
809 snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
Lucas De Marchi63be91c2012-01-16 10:43:34 -0200810 index_files[i].fn);
Lucas De Marchic4dc3ca2011-12-31 19:28:31 -0200811
812 if (is_cache_invalid(path, ctx->indexes_stamp[i]))
813 return KMOD_RESOURCES_MUST_RELOAD;
814 }
815
816 return KMOD_RESOURCES_OK;
817}
818
Lucas De Marchi54ba8b32011-12-09 16:42:14 -0200819/**
Lucas De Marchibe5a6de2011-12-14 03:44:38 -0200820 * kmod_load_resources:
821 * @ctx: kmod library context
822 *
823 * Load indexes and keep them open in @ctx. This way it's faster to lookup
824 * information within the indexes. If this function is not called before a
825 * search, the necessary index is always opened and closed.
826 *
827 * If user will do more than one or two lookups, insertions, deletions, most
828 * likely it's good to call this function first. Particularly in a daemon like
829 * udev that on bootup issues hundreds of calls to lookup the index, calling
830 * this function will speedup the searches.
831 *
832 * Returns: 0 on success or < 0 otherwise.
833 */
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200834KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
835{
836 size_t i;
837
838 if (ctx == NULL)
839 return -ENOENT;
840
Lucas De Marchib08314f2012-01-16 12:01:48 -0200841 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
Lucas De Marchi6de8f6e2011-12-14 03:58:31 -0200842 char path[PATH_MAX];
Lucas De Marchi3e676762011-12-14 03:53:43 -0200843
Lucas De Marchi16ca3662011-12-20 12:29:13 -0200844 if (ctx->indexes[i] != NULL) {
Lucas De Marchi63be91c2012-01-16 10:43:34 -0200845 INFO(ctx, "Index %s already loaded\n",
846 index_files[i].fn);
Lucas De Marchi3e676762011-12-14 03:53:43 -0200847 continue;
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200848 }
Lucas De Marchi3e676762011-12-14 03:53:43 -0200849
Lucas De Marchi6de8f6e2011-12-14 03:58:31 -0200850 snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
Lucas De Marchi63be91c2012-01-16 10:43:34 -0200851 index_files[i].fn);
Lucas De Marchi2e2e2522012-03-02 20:33:26 -0300852 ctx->indexes[i] = index_mm_open(ctx, path,
Lucas De Marchi9fd58f32011-12-31 18:53:24 -0200853 &ctx->indexes_stamp[i]);
Lucas De Marchi3e676762011-12-14 03:53:43 -0200854 if (ctx->indexes[i] == NULL)
855 goto fail;
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200856 }
857
858 return 0;
859
860fail:
861 kmod_unload_resources(ctx);
862 return -ENOMEM;
863}
864
Lucas De Marchibe5a6de2011-12-14 03:44:38 -0200865/**
866 * kmod_unload_resources:
867 * @ctx: kmod library context
868 *
869 * Unload all the indexes. This will free the resources to maintain the index
870 * open and all subsequent searches will need to open and close the index.
871 *
872 * User is free to call kmod_load_resources() and kmod_unload_resources() as
873 * many times as wanted during the lifecycle of @ctx. For example, if a daemon
874 * knows that when starting up it will lookup a lot of modules, it could call
875 * kmod_load_resources() and after the first burst of searches is gone, it
876 * could free the resources by calling kmod_unload_resources().
877 *
878 * Returns: 0 on success or < 0 otherwise.
879 */
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200880KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
881{
882 size_t i;
883
884 if (ctx == NULL)
885 return;
886
Lucas De Marchib08314f2012-01-16 12:01:48 -0200887 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200888 if (ctx->indexes[i] != NULL) {
889 index_mm_close(ctx->indexes[i]);
890 ctx->indexes[i] = NULL;
Lucas De Marchi9fd58f32011-12-31 18:53:24 -0200891 ctx->indexes_stamp[i] = 0;
Lucas De Marchi33bb69b2011-12-08 14:59:51 -0200892 }
893 }
894}
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200895
Lucas De Marchi02244822012-01-16 16:43:47 -0200896/**
897 * kmod_dump_index:
898 * @ctx: kmod library context
Chengwei Yangd7152f62013-05-04 17:07:03 +0800899 * @type: index to dump, valid indexes are
900 * KMOD_INDEX_MODULES_DEP: index of module dependencies;
901 * KMOD_INDEX_MODULES_ALIAS: index of module aliases;
902 * KMOD_INDEX_MODULES_SYMBOL: index of symbol aliases;
903 * KMOD_INDEX_MODULES_BUILTIN: index of builtin module.
Lucas De Marchi02244822012-01-16 16:43:47 -0200904 * @fd: file descriptor to dump index to
905 *
Lucas De Marchi09e9ae52012-01-17 10:05:02 -0200906 * Dump index to file descriptor. Note that this function doesn't use stdio.h
907 * so call fflush() before calling this function to be sure data is written in
908 * order.
Lucas De Marchi02244822012-01-16 16:43:47 -0200909 *
910 * Returns: 0 on success or < 0 otherwise.
911 */
Lucas De Marchi758428a2012-01-16 15:56:17 -0200912KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,
913 int fd)
914{
915 if (ctx == NULL)
916 return -ENOSYS;
917
918 if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE)
919 return -ENOENT;
920
921 if (ctx->indexes[type] != NULL) {
922 DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn);
923 index_mm_dump(ctx->indexes[type], fd,
924 index_files[type].prefix);
925 } else {
926 char fn[PATH_MAX];
927 struct index_file *idx;
928
929 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
930 index_files[type].fn);
931
932 DBG(ctx, "file=%s\n", fn);
933
934 idx = index_file_open(fn);
935 if (idx == NULL)
936 return -ENOSYS;
937
938 index_dump(idx, fd, index_files[type].prefix);
939 index_file_close(idx);
940 }
941
942 return 0;
943}
944
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300945const struct kmod_config *kmod_get_config(const struct kmod_ctx *ctx)
Lucas De Marchic1c9c442011-12-24 10:50:47 -0200946{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300947 return ctx->config;
Lucas De Marchi8b5ee612012-01-13 01:14:46 -0200948}