blob: 3adbb69d6a7b1074df3ca609b5ad465c88914618 [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -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 Marchi8f788d52011-11-25 01:22:56 -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 Marchi8f788d52011-11-25 01:22:56 -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
Lucas De Marchi7636e722011-12-01 17:56:03 -020021#include <assert.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020022#include <stdio.h>
23#include <stdlib.h>
24#include <stddef.h>
25#include <stdarg.h>
26#include <unistd.h>
27#include <errno.h>
28#include <string.h>
29#include <ctype.h>
30#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020031#include <limits.h>
32#include <dirent.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020033#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/mman.h>
Kees Cook144d1822013-02-18 12:02:32 -080036#include <sys/syscall.h>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020037#include <sys/wait.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020038#include <string.h>
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020039#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020040
Kees Cook144d1822013-02-18 12:02:32 -080041#ifdef HAVE_LINUX_MODULE_H
42#include <linux/module.h>
43#endif
44
Lucas De Marchi8f788d52011-11-25 01:22:56 -020045#include "libkmod.h"
Lucas De Marchi83b855a2013-07-04 16:13:11 -030046#include "libkmod-internal.h"
Lucas De Marchi8f788d52011-11-25 01:22:56 -020047
48/**
Lucas De Marchi66819512012-01-09 04:47:40 -020049 * SECTION:libkmod-module
50 * @short_description: operate on kernel modules
51 */
52
53/**
Lucas De Marchi8f788d52011-11-25 01:22:56 -020054 * kmod_module:
55 *
56 * Opaque object representing a module.
57 */
58struct kmod_module {
59 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020060 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020061 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020062 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020063 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020064 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020065 const char *install_commands; /* owned by kmod_config */
66 const char *remove_commands; /* owned by kmod_config */
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020067 char *alias; /* only set if this module was created from an alias */
Lucas De Marchi1eff9422012-10-18 01:36:33 -030068 struct kmod_file *file;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020069 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020070 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020071 struct {
72 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020073 bool options : 1;
74 bool install_commands : 1;
75 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020076 } init;
Lucas De Marchib1a51252012-01-29 01:49:09 -020077
78 /*
79 * private field used by kmod_module_get_probe_list() to detect
80 * dependency loops
81 */
Lucas De Marchiece09aa2012-01-18 01:26:44 -020082 bool visited : 1;
Lucas De Marchi89e92482012-01-29 02:35:46 -020083
84 /*
85 * set by kmod_module_get_probe_list: indicates for probe_insert()
86 * whether the module's command and softdep should be ignored
87 */
88 bool ignorecmd : 1;
Lucas De Marchi38052742012-02-16 20:43:16 -020089
90 /*
91 * if module was created by searching the modules.builtin file, this
92 * is set. There's nothing much useful one can do with such a
93 * "module", except knowing it's builtin.
94 */
95 bool builtin : 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020096};
97
Lucas De Marchic35347f2011-12-12 10:48:02 -020098static inline const char *path_join(const char *path, size_t prefixlen,
99 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200100{
101 size_t pathlen;
102
103 if (path[0] == '/')
104 return path;
105
106 pathlen = strlen(path);
107 if (prefixlen + pathlen + 1 >= PATH_MAX)
108 return NULL;
109
110 memcpy(buf + prefixlen, path, pathlen + 1);
111 return buf;
112}
113
Dave Reisneraf9572c2012-02-02 11:07:33 -0500114static inline bool module_is_inkernel(struct kmod_module *mod)
115{
116 int state = kmod_module_get_initstate(mod);
Lucas De Marchi38052742012-02-16 20:43:16 -0200117
Dave Reisneraf9572c2012-02-02 11:07:33 -0500118 if (state == KMOD_MODULE_LIVE ||
Dave Reisneraf9572c2012-02-02 11:07:33 -0500119 state == KMOD_MODULE_BUILTIN)
120 return true;
Lucas De Marchi38052742012-02-16 20:43:16 -0200121
122 return false;
Dave Reisneraf9572c2012-02-02 11:07:33 -0500123}
124
Lucas De Marchi671d4892011-12-05 20:23:05 -0200125int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200126{
127 struct kmod_ctx *ctx = mod->ctx;
128 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200129 const char *dirname;
130 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200131 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200132 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200133 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200134
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200135 if (mod->init.dep)
136 return mod->n_dep;
137 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200138 mod->init.dep = true;
139
140 p = strchr(line, ':');
141 if (p == NULL)
142 return 0;
143
Lucas De Marchi671d4892011-12-05 20:23:05 -0200144 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200145 dirname = kmod_get_dirname(mod->ctx);
146 dirnamelen = strlen(dirname);
147 if (dirnamelen + 2 >= PATH_MAX)
148 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200149
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200150 memcpy(buf, dirname, dirnamelen);
151 buf[dirnamelen] = '/';
152 dirnamelen++;
153 buf[dirnamelen] = '\0';
154
155 if (mod->path == NULL) {
156 const char *str = path_join(line, dirnamelen, buf);
157 if (str == NULL)
158 return 0;
159 mod->path = strdup(str);
160 if (mod->path == NULL)
161 return 0;
162 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200163
Lucas De Marchi7636e722011-12-01 17:56:03 -0200164 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200165 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
166 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200167 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200168 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200169
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200170 path = path_join(p, dirnamelen, buf);
171 if (path == NULL) {
172 ERR(ctx, "could not join path '%s' and '%s'.\n",
173 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200174 goto fail;
175 }
176
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200177 err = kmod_module_new_from_path(ctx, path, &depmod);
178 if (err < 0) {
179 ERR(ctx, "ctx=%p path=%s error=%s\n",
180 ctx, path, strerror(-err));
181 goto fail;
182 }
183
184 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200185
Lucas De Marchib94a7372011-12-26 20:10:49 -0200186 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200187 n++;
188 }
189
190 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
191
192 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200193 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200194 return n;
195
196fail:
197 kmod_module_unref_list(list);
198 mod->init.dep = false;
199 return err;
200}
201
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200202void kmod_module_set_visited(struct kmod_module *mod, bool visited)
203{
204 mod->visited = visited;
205}
206
Lucas De Marchi38052742012-02-16 20:43:16 -0200207void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
208{
209 mod->builtin = builtin;
210}
211
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200212/*
213 * Memory layout with alias:
214 *
215 * struct kmod_module {
216 * hashkey -----.
217 * alias -----. |
218 * name ----. | |
219 * } | | |
220 * name <----------' | |
221 * alias <-----------' |
222 * name\alias <--------'
223 *
224 * Memory layout without alias:
225 *
226 * struct kmod_module {
227 * hashkey ---.
228 * alias -----|----> NULL
229 * name ----. |
230 * } | |
231 * name <----------'-'
232 *
233 * @key is "name\alias" or "name" (in which case alias == NULL)
234 */
235static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
236 const char *name, size_t namelen,
237 const char *alias, size_t aliaslen,
238 struct kmod_module **mod)
239{
240 struct kmod_module *m;
241 size_t keylen;
242
243 m = kmod_pool_get_module(ctx, key);
244 if (m != NULL) {
245 *mod = kmod_module_ref(m);
246 return 0;
247 }
248
249 if (alias == NULL)
250 keylen = namelen;
251 else
252 keylen = namelen + aliaslen + 1;
253
254 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
255 if (m == NULL) {
256 free(m);
257 return -ENOMEM;
258 }
259
260 memset(m, 0, sizeof(*m));
261
262 m->ctx = kmod_ref(ctx);
263 m->name = (char *)m + sizeof(*m);
264 memcpy(m->name, key, keylen + 1);
265 if (alias == NULL) {
266 m->hashkey = m->name;
267 m->alias = NULL;
268 } else {
269 m->name[namelen] = '\0';
270 m->alias = m->name + namelen + 1;
271 m->hashkey = m->name + keylen + 1;
272 memcpy(m->hashkey, key, keylen + 1);
273 }
274
275 m->refcount = 1;
276 kmod_pool_add_module(ctx, m, m->hashkey);
277 *mod = m;
278
279 return 0;
280}
281
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200282/**
283 * kmod_module_new_from_name:
284 * @ctx: kmod library context
285 * @name: name of the module
286 * @mod: where to save the created struct kmod_module
287 *
288 * Create a new struct kmod_module using the module name. @name can not be an
289 * alias, file name or anything else; it must be a module name. There's no
Dan McGee9a252c22012-02-03 20:29:07 -0600290 * check if the module exists in the system.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200291 *
292 * This function is also used internally by many others that return a new
293 * struct kmod_module or a new list of modules.
294 *
295 * The initial refcount is 1, and needs to be decremented to release the
296 * resources of the kmod_module. Since libkmod keeps track of all
297 * kmod_modules created, they are all released upon @ctx destruction too. Do
298 * not unref @ctx before all the desired operations with the returned
299 * kmod_module are done.
300 *
301 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
302 * module name or if memory allocation failed.
303 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200304KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
305 const char *name,
306 struct kmod_module **mod)
307{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200308 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200309 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200310
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200311 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200312 return -ENOENT;
313
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200314 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200315
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200316 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200317}
318
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200319int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
320 const char *name, struct kmod_module **mod)
321{
322 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200323 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200324 size_t namelen = strlen(name);
325 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200326
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200327 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200328 return -ENAMETOOLONG;
329
330 memcpy(key, name, namelen);
331 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200332 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200333
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200334 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200335 if (err < 0)
336 return err;
337
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200338 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200339}
340
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200341/**
342 * kmod_module_new_from_path:
343 * @ctx: kmod library context
344 * @path: path where to find the given module
345 * @mod: where to save the created struct kmod_module
346 *
347 * Create a new struct kmod_module using the module path. @path must be an
348 * existent file with in the filesystem and must be accessible to libkmod.
349 *
350 * The initial refcount is 1, and needs to be decremented to release the
351 * resources of the kmod_module. Since libkmod keeps track of all
352 * kmod_modules created, they are all released upon @ctx destruction too. Do
353 * not unref @ctx before all the desired operations with the returned
354 * kmod_module are done.
355 *
356 * If @path is relative, it's treated as relative to the current working
357 * directory. Otherwise, give an absolute path.
358 *
359 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
360 * it's not a valid file for a kmod_module or if memory allocation failed.
361 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200362KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
363 const char *path,
364 struct kmod_module **mod)
365{
366 struct kmod_module *m;
367 int err;
368 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200369 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200370 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200371 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200372
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200373 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200374 return -ENOENT;
375
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200376 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200377 if (abspath == NULL) {
378 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200379 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200380 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200381
382 err = stat(abspath, &st);
383 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200384 err = -errno;
385 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200386 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200387 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200388 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200389
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200390 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200391 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200392 free(abspath);
393 return -ENOENT;
394 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200395
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200396 m = kmod_pool_get_module(ctx, name);
397 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200398 if (m->path == NULL)
399 m->path = abspath;
400 else if (streq(m->path, abspath))
401 free(abspath);
402 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200403 ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200404 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200405 free(abspath);
406 return -EEXIST;
407 }
408
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200409 *mod = kmod_module_ref(m);
410 return 0;
411 }
412
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200413 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
414 if (err < 0)
415 return err;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200416
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200417 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200418 *mod = m;
419
420 return 0;
421}
422
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200423/**
424 * kmod_module_unref:
425 * @mod: kmod module
426 *
427 * Drop a reference of the kmod module. If the refcount reaches zero, its
428 * resources are released.
429 *
430 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
431 * returns the passed @mod with its refcount decremented.
432 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200433KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
434{
435 if (mod == NULL)
436 return NULL;
437
438 if (--mod->refcount > 0)
439 return mod;
440
441 DBG(mod->ctx, "kmod_module %p released\n", mod);
442
Lucas De Marchi4084c172011-12-15 13:43:22 -0200443 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200444 kmod_module_unref_list(mod->dep);
Lucas De Marchi1eff9422012-10-18 01:36:33 -0300445
446 if (mod->file)
447 kmod_file_unref(mod->file);
448
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200449 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200450 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200451 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200452 free(mod);
453 return NULL;
454}
455
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200456/**
457 * kmod_module_ref:
458 * @mod: kmod module
459 *
460 * Take a reference of the kmod module, incrementing its refcount.
461 *
462 * Returns: the passed @module with its refcount incremented.
463 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200464KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
465{
466 if (mod == NULL)
467 return NULL;
468
469 mod->refcount++;
470
471 return mod;
472}
473
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200474#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
475 do { \
476 if ((_err) < 0) \
477 goto _label_err; \
478 if (*(_list) != NULL) \
479 goto finish; \
480 } while (0)
481
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200482/**
483 * kmod_module_new_from_lookup:
484 * @ctx: kmod library context
485 * @given_alias: alias to look for
486 * @list: an empty list where to save the list of modules matching
487 * @given_alias
488 *
489 * Create a new list of kmod modules using an alias or module name and lookup
490 * libkmod's configuration files and indexes in order to find the module.
491 * Once it's found in one of the places, it stops searching and create the
492 * list of modules that is saved in @list.
493 *
494 * The search order is: 1. aliases in configuration file; 2. module names in
495 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
496 * in modules.alias index.
497 *
498 * The initial refcount is 1, and needs to be decremented to release the
499 * resources of the kmod_module. The returned @list must be released by
500 * calling kmod_module_unref_list(). Since libkmod keeps track of all
501 * kmod_modules created, they are all released upon @ctx destruction too. Do
502 * not unref @ctx before all the desired operations with the returned list are
503 * completed.
504 *
505 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
506 * methods failed, which is basically due to memory allocation fail. If module
507 * is not found, it still returns 0, but @list is an empty list.
508 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200509KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200510 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200511 struct kmod_list **list)
512{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200513 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200514 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200515
Lucas De Marchi4308b172011-12-13 10:26:04 -0200516 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200517 return -ENOENT;
518
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200519 if (list == NULL || *list != NULL) {
520 ERR(ctx, "An empty list is needed to create lookup\n");
521 return -ENOSYS;
522 }
523
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200524 if (alias_normalize(given_alias, alias, NULL) < 0) {
525 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200526 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200527 }
528
529 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200530
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200531 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200532 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200533 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200534
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200535 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200536 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
537 CHECK_ERR_AND_FINISH(err, fail, list, finish);
538
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200539 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200540 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
541 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200542
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200543 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200544 err = kmod_lookup_alias_from_commands(ctx, alias, list);
545 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200546
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200547 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200548 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
549 CHECK_ERR_AND_FINISH(err, fail, list, finish);
550
Lucas De Marchi38052742012-02-16 20:43:16 -0200551 DBG(ctx, "lookup modules.builtin %s\n", alias);
552 err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
553 CHECK_ERR_AND_FINISH(err, fail, list, finish);
554
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200555finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200556 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200557 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200558fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200559 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200560 kmod_module_unref_list(*list);
561 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200562 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200563}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200564#undef CHECK_ERR_AND_FINISH
565
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200566/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200567 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200568 * @list: list of kmod modules
569 *
570 * Drop a reference of each kmod module in @list and releases the resources
571 * taken by the list itself.
572 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800573 * Returns: 0
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200574 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200575KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
576{
577 for (; list != NULL; list = kmod_list_remove(list))
578 kmod_module_unref(list->data);
579
580 return 0;
581}
582
Lucas De Marchi0d467432011-12-31 11:15:52 -0200583/**
584 * kmod_module_get_filtered_blacklist:
585 * @ctx: kmod library context
586 * @input: list of kmod_module to be filtered with blacklist
587 * @output: where to save the new list
588 *
Kay Sievers471a7d02012-04-14 20:47:47 +0200589 * This function should not be used. Use kmod_module_apply_filter instead.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500590 *
Lucas De Marchi0d467432011-12-31 11:15:52 -0200591 * Given a list @input, this function filter it out with config's blacklist
Dave Reisnerd80b1032012-02-24 10:05:11 -0500592 * and save it in @output.
Lucas De Marchi0d467432011-12-31 11:15:52 -0200593 *
594 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
595 * list.
596 */
597KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
598 const struct kmod_list *input,
599 struct kmod_list **output)
600{
Dave Reisnerd80b1032012-02-24 10:05:11 -0500601 return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
Lucas De Marchi0d467432011-12-31 11:15:52 -0200602}
603
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200604static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
605{
606 if (!mod->init.dep) {
607 /* lazy init */
608 char *line = kmod_search_moddep(mod->ctx, mod->name);
609
610 if (line == NULL)
611 return NULL;
612
613 kmod_module_parse_depline((struct kmod_module *)mod, line);
614 free(line);
615
616 if (!mod->init.dep)
617 return NULL;
618 }
619
620 return mod->dep;
621}
622
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200623/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200624 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200625 * @mod: kmod module
626 *
627 * Search the modules.dep index to find the dependencies of the given @mod.
628 * The result is cached in @mod, so subsequent calls to this function will
629 * return the already searched list of modules.
630 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800631 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
632 * that can be released by calling kmod_module_unref_list().
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200633 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200634KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200635{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200636 struct kmod_list *l, *l_new, *list_new = NULL;
637
638 if (mod == NULL)
639 return NULL;
640
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200641 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200642
643 kmod_list_foreach(l, mod->dep) {
644 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
645 if (l_new == NULL) {
646 kmod_module_unref(l->data);
647 goto fail;
648 }
649
650 list_new = l_new;
651 }
652
653 return list_new;
654
655fail:
656 ERR(mod->ctx, "out of memory\n");
657 kmod_module_unref_list(list_new);
658 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200659}
660
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200661/**
662 * kmod_module_get_module:
663 * @entry: an entry in a list of kmod modules.
664 *
665 * Get the kmod module of this @entry in the list, increasing its refcount.
666 * After it's used, unref it. Since the refcount is incremented upon return,
667 * you still have to call kmod_module_unref_list() to release the list of kmod
668 * modules.
669 *
670 * Returns: NULL on failure or the kmod_module contained in this list entry
671 * with its refcount incremented.
672 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200673KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200674{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200675 if (entry == NULL)
676 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200677
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200678 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200679}
680
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200681/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200682 * kmod_module_get_name:
683 * @mod: kmod module
684 *
685 * Get the name of this kmod module. Name is always available, independently
686 * if it was created by kmod_module_new_from_name() or another function and
687 * it's always normalized (dashes are replaced with underscores).
688 *
689 * Returns: the name of this kmod module.
690 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200691KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200692{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200693 if (mod == NULL)
694 return NULL;
695
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200696 return mod->name;
697}
698
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200699/**
700 * kmod_module_get_path:
701 * @mod: kmod module
702 *
703 * Get the path of this kmod module. If this kmod module was not created by
704 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200705 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200706 *
707 * Returns: the path of this kmod module or NULL if such information is not
708 * available.
709 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200710KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200711{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200712 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200713
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200714 if (mod == NULL)
715 return NULL;
716
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200717 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200718
Lucas De Marchie005fac2011-12-08 10:42:34 -0200719 if (mod->path != NULL)
720 return mod->path;
721 if (mod->init.dep)
722 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200723
Lucas De Marchie005fac2011-12-08 10:42:34 -0200724 /* lazy init */
725 line = kmod_search_moddep(mod->ctx, mod->name);
726 if (line == NULL)
727 return NULL;
728
729 kmod_module_parse_depline((struct kmod_module *) mod, line);
730 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200731
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200732 return mod->path;
733}
734
735
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200736extern long delete_module(const char *name, unsigned int flags);
737
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200738/**
739 * kmod_module_remove_module:
740 * @mod: kmod module
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500741 * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
Chengwei Yangd7152f62013-05-04 17:07:03 +0800742 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
743 * use by a kernel subsystem or other process;
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500744 * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
745 * delete_module(2).
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200746 *
747 * Remove a module from Linux kernel.
748 *
749 * Returns: 0 on success or < 0 on failure.
750 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200751KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
752 unsigned int flags)
753{
754 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200755
756 if (mod == NULL)
757 return -ENOENT;
758
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500759 /* Filter out other flags and force ONONBLOCK */
760 flags &= KMOD_REMOVE_FORCE;
761 flags |= KMOD_REMOVE_NOWAIT;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200762
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200763 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200764 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200765 err = -errno;
766 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200767 }
768
Lucas De Marchiba998b92012-01-11 00:08:14 -0200769 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200770}
771
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200772extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200773
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200774/**
775 * kmod_module_insert_module:
776 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200777 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +0800778 * behavior of this function, valid flags are
779 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
780 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200781 * @options: module's options to pass to Linux Kernel.
782 *
783 * Insert a module in Linux kernel. It opens the file pointed by @mod,
784 * mmap'ing it and passing to kernel.
785 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200786 * Returns: 0 on success or < 0 on failure. If module is already loaded it
787 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200788 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200789KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200790 unsigned int flags,
791 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200792{
793 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200794 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200795 off_t size;
796 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200797 struct kmod_elf *elf = NULL;
798 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200799 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200800
801 if (mod == NULL)
802 return -ENOENT;
803
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200804 path = kmod_module_get_path(mod);
805 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500806 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200807 return -ENOSYS;
808 }
809
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200810 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200811 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200812 err = -errno;
813 return err;
814 }
815
Kees Cook144d1822013-02-18 12:02:32 -0800816 if (kmod_file_get_direct(file)) {
817 unsigned int kernel_flags = 0;
818
Kees Cook144d1822013-02-18 12:02:32 -0800819 if (flags & KMOD_INSERT_FORCE_VERMAGIC)
820 kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
821 if (flags & KMOD_INSERT_FORCE_MODVERSION)
822 kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
Kees Cook144d1822013-02-18 12:02:32 -0800823
824 err = finit_module(kmod_file_get_fd(file), args, kernel_flags);
825 if (err == 0 || errno != ENOSYS)
826 goto init_finished;
827 }
828
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200829 size = kmod_file_get_size(file);
830 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200831
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200832 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
833 elf = kmod_elf_new(mem, size);
834 if (elf == NULL) {
835 err = -errno;
836 goto elf_failed;
837 }
838
839 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
840 err = kmod_elf_strip_section(elf, "__versions");
841 if (err < 0)
842 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
843 }
844
845 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
846 err = kmod_elf_strip_vermagic(elf);
847 if (err < 0)
848 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
849 }
850
851 mem = kmod_elf_get_memory(elf);
852 }
853
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200854 err = init_module(mem, size, args);
Kees Cook144d1822013-02-18 12:02:32 -0800855init_finished:
Lucas De Marchibbf59322011-12-30 14:13:33 -0200856 if (err < 0) {
857 err = -errno;
858 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
859 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200860
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200861 if (elf != NULL)
862 kmod_elf_unref(elf);
863elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200864 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200865
866 return err;
867}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200868
Lucas De Marchiddbda022011-12-27 11:40:10 -0200869static bool module_is_blacklisted(struct kmod_module *mod)
870{
871 struct kmod_ctx *ctx = mod->ctx;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300872 const struct kmod_config *config = kmod_get_config(ctx);
873 const struct kmod_list *bl = config->blacklists;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200874 const struct kmod_list *l;
875
876 kmod_list_foreach(l, bl) {
877 const char *modname = kmod_blacklist_get_modname(l);
878
879 if (streq(modname, mod->name))
880 return true;
881 }
882
883 return false;
884}
885
Dave Reisnerd80b1032012-02-24 10:05:11 -0500886/**
887 * kmod_module_apply_filter
888 * @ctx: kmod library context
Chengwei Yangd7152f62013-05-04 17:07:03 +0800889 * @filter_type: bitmask to filter modules out, valid types are
890 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
891 * KMOD_FILTER_BUILTIN: filter builtin modules out.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500892 * @input: list of kmod_module to be filtered
893 * @output: where to save the new list
894 *
895 * Given a list @input, this function filter it out by the filter mask
896 * and save it in @output.
897 *
898 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
899 * list.
900 */
901KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
902 enum kmod_filter filter_type,
903 const struct kmod_list *input,
904 struct kmod_list **output)
905{
906 const struct kmod_list *li;
907
908 if (ctx == NULL || output == NULL)
909 return -ENOENT;
910
911 *output = NULL;
912 if (input == NULL)
913 return 0;
914
915 kmod_list_foreach(li, input) {
916 struct kmod_module *mod = li->data;
917 struct kmod_list *node;
918
919 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
920 module_is_blacklisted(mod))
921 continue;
922
Dave Reisnerbdda7e12012-02-24 23:02:06 -0500923 if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin)
Dave Reisnerd80b1032012-02-24 10:05:11 -0500924 continue;
925
926 node = kmod_list_append(*output, mod);
927 if (node == NULL)
928 goto fail;
929
930 *output = node;
931 kmod_module_ref(mod);
932 }
933
934 return 0;
935
936fail:
937 kmod_module_unref_list(*output);
938 *output = NULL;
939 return -ENOMEM;
940}
941
Lucas De Marchiddbda022011-12-27 11:40:10 -0200942static int command_do(struct kmod_module *mod, const char *type,
943 const char *cmd)
944{
945 const char *modname = kmod_module_get_name(mod);
946 int err;
947
948 DBG(mod->ctx, "%s %s\n", type, cmd);
949
950 setenv("MODPROBE_MODULE", modname, 1);
951 err = system(cmd);
952 unsetenv("MODPROBE_MODULE");
953
954 if (err == -1 || WEXITSTATUS(err)) {
955 ERR(mod->ctx, "Error running %s command for %s\n",
956 type, modname);
957 if (err != -1)
958 err = -WEXITSTATUS(err);
959 }
960
961 return err;
962}
963
Lucas De Marchib1a51252012-01-29 01:49:09 -0200964struct probe_insert_cb {
965 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
966 void *data;
967};
968
Lucas De Marchiddbda022011-12-27 11:40:10 -0200969static int module_do_install_commands(struct kmod_module *mod,
970 const char *options,
971 struct probe_insert_cb *cb)
972{
973 const char *command = kmod_module_get_install_commands(mod);
974 char *p, *cmd;
975 int err;
976 size_t cmdlen, options_len, varlen;
977
978 assert(command);
979
980 if (options == NULL)
981 options = "";
982
983 options_len = strlen(options);
984 cmdlen = strlen(command);
985 varlen = sizeof("$CMDLINE_OPTS") - 1;
986
987 cmd = memdup(command, cmdlen + 1);
988 if (cmd == NULL)
989 return -ENOMEM;
990
991 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
992 size_t prefixlen = p - cmd;
993 size_t suffixlen = cmdlen - prefixlen - varlen;
994 size_t slen = cmdlen - varlen + options_len;
995 char *suffix = p + varlen;
996 char *s = malloc(slen + 1);
997 if (s == NULL) {
998 free(cmd);
999 return -ENOMEM;
1000 }
1001 memcpy(s, cmd, p - cmd);
1002 memcpy(s + prefixlen, options, options_len);
1003 memcpy(s + prefixlen + options_len, suffix, suffixlen);
1004 s[slen] = '\0';
1005
1006 free(cmd);
1007 cmd = s;
1008 cmdlen = slen;
1009 }
1010
1011 if (cb->run_install != NULL)
1012 err = cb->run_install(mod, cmd, cb->data);
1013 else
1014 err = command_do(mod, "install", cmd);
1015
1016 free(cmd);
1017
1018 return err;
1019}
1020
Lucas De Marchiddbda022011-12-27 11:40:10 -02001021static char *module_options_concat(const char *opt, const char *xopt)
1022{
1023 // TODO: we might need to check if xopt overrides options on opt
1024 size_t optlen = opt == NULL ? 0 : strlen(opt);
1025 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1026 char *r;
1027
1028 if (optlen == 0 && xoptlen == 0)
1029 return NULL;
1030
1031 r = malloc(optlen + xoptlen + 2);
1032
1033 if (opt != NULL) {
1034 memcpy(r, opt, optlen);
1035 r[optlen] = ' ';
1036 optlen++;
1037 }
1038
1039 if (xopt != NULL)
1040 memcpy(r + optlen, xopt, xoptlen);
1041
1042 r[optlen + xoptlen] = '\0';
1043
1044 return r;
1045}
1046
Lucas De Marchib1a51252012-01-29 01:49:09 -02001047static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001048 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001049 struct kmod_list **list);
1050
1051/* re-entrant */
1052static int __kmod_module_fill_softdep(struct kmod_module *mod,
1053 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001054{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001055 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001056 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001057
1058 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001059 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001060 ERR(mod->ctx, "could not get softdep: %s\n",
1061 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001062 goto fail;
1063 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001064
Lucas De Marchib1a51252012-01-29 01:49:09 -02001065 kmod_list_foreach(l, pre) {
1066 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001067 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001068 if (err < 0)
1069 goto fail;
1070 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001071
Lucas De Marchib1a51252012-01-29 01:49:09 -02001072 l = kmod_list_append(*list, kmod_module_ref(mod));
1073 if (l == NULL) {
1074 kmod_module_unref(mod);
1075 err = -ENOMEM;
1076 goto fail;
1077 }
1078 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001079 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001080
Lucas De Marchib1a51252012-01-29 01:49:09 -02001081 kmod_list_foreach(l, post) {
1082 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001083 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001084 if (err < 0)
1085 goto fail;
1086 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001087
Lucas De Marchib1a51252012-01-29 01:49:09 -02001088fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001089 kmod_module_unref_list(pre);
1090 kmod_module_unref_list(post);
1091
1092 return err;
1093}
1094
Lucas De Marchib1a51252012-01-29 01:49:09 -02001095/* re-entrant */
1096static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001097 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001098 struct kmod_list **list)
1099{
1100 struct kmod_list *dep, *l;
1101 int err = 0;
1102
1103 if (mod->visited) {
1104 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1105 mod->name);
1106 return 0;
1107 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001108 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001109
1110 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001111 kmod_list_foreach(l, dep) {
1112 struct kmod_module *m = l->data;
1113 err = __kmod_module_fill_softdep(m, list);
1114 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001115 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001116 }
1117
Lucas De Marchi89e92482012-01-29 02:35:46 -02001118 if (ignorecmd) {
1119 l = kmod_list_append(*list, kmod_module_ref(mod));
1120 if (l == NULL) {
1121 kmod_module_unref(mod);
1122 err = -ENOMEM;
1123 goto finish;
1124 }
1125 *list = l;
1126 mod->ignorecmd = true;
1127 } else
1128 err = __kmod_module_fill_softdep(mod, list);
1129
Lucas De Marchib1a51252012-01-29 01:49:09 -02001130finish:
1131 kmod_module_unref_list(dep);
1132 return err;
1133}
1134
1135static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001136 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001137 struct kmod_list **list)
1138{
1139 int err;
1140
1141 assert(mod != NULL);
1142 assert(list != NULL && *list == NULL);
1143
1144 /*
1145 * Make sure we don't get screwed by previous calls to this function
1146 */
1147 kmod_set_modules_visited(mod->ctx, false);
1148
Lucas De Marchi89e92482012-01-29 02:35:46 -02001149 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001150 if (err < 0) {
1151 kmod_module_unref_list(*list);
1152 *list = NULL;
1153 }
1154
1155 return err;
1156}
1157
Lucas De Marchiddbda022011-12-27 11:40:10 -02001158/**
1159 * kmod_module_probe_insert_module:
1160 * @mod: kmod module
1161 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +08001162 * behavior of this function, valid flags are
1163 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1164 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1165 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1166 * commands and softdeps configured in the system;
1167 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1168 * live in kernel or not;
1169 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1170 * associated callback function;
1171 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1172 * and the module is already live in kernel, the function will fail if this
1173 * flag is specified;
1174 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1175 * filter to this module and its dependencies. If any of the dependencies (or
1176 * the module) is blacklisted, the probe will fail, unless the blacklisted
1177 * module is already live in kernel;
1178 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1179 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1180 * alias and is blacklisted.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001181 * @extra_options: module's options to pass to Linux Kernel. It applies only
1182 * to @mod, not to its dependencies.
1183 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001184 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001185 * @print_action: function to call with the action being taken (install or
1186 * insmod). It's useful for tools like modprobe when running with verbose
1187 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001188 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001189 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001190 * install commands and applying blacklist.
1191 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001192 * If @run_install is NULL, this function will fork and exec by calling
1193 * system(3). Don't pass a NULL argument in @run_install if your binary is
1194 * setuid/setgid (see warning in system(3)). If you need control over the
1195 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001196 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001197 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1198 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001199 */
1200KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1201 unsigned int flags, const char *extra_options,
1202 int (*run_install)(struct kmod_module *m,
1203 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001204 const void *data,
1205 void (*print_action)(struct kmod_module *m,
1206 bool install,
1207 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001208{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001209 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001210 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001211 int err;
1212
1213 if (mod == NULL)
1214 return -ENOENT;
1215
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001216 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1217 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001218 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001219 return -EEXIST;
1220 else
1221 return 0;
1222 }
1223
Lucas De Marchi68820172012-08-17 09:38:05 -03001224 /*
1225 * Ugly assignement + check. We need to check if we were told to check
1226 * blacklist and also return the reason why we failed.
1227 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1228 * module is an alias, so we also need to check it
1229 */
1230 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1231 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1232 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001233 if (module_is_blacklisted(mod))
1234 return err;
1235 }
1236
Lucas De Marchi89e92482012-01-29 02:35:46 -02001237 err = kmod_module_get_probe_list(mod,
1238 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001239 if (err < 0)
1240 return err;
1241
1242 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1243 struct kmod_list *filtered = NULL;
1244
Dave Reisnerd80b1032012-02-24 10:05:11 -05001245 err = kmod_module_apply_filter(mod->ctx,
1246 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001247 if (err < 0)
1248 return err;
1249
1250 kmod_module_unref_list(list);
1251 if (filtered == NULL)
1252 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1253
1254 list = filtered;
1255 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001256
1257 cb.run_install = run_install;
1258 cb.data = (void *) data;
1259
Lucas De Marchib1a51252012-01-29 01:49:09 -02001260 kmod_list_foreach(l, list) {
1261 struct kmod_module *m = l->data;
1262 const char *moptions = kmod_module_get_options(m);
1263 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001264 char *options;
1265
1266 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1267 && module_is_inkernel(m)) {
1268 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1269 m->name);
1270 err = -EEXIST;
1271 goto finish_module;
1272 }
1273
1274 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001275 m == mod ? extra_options : NULL);
1276
Lucas De Marchi89e92482012-01-29 02:35:46 -02001277 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001278 if (print_action != NULL)
1279 print_action(m, true, options ?: "");
1280
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001281 if (!(flags & KMOD_PROBE_DRY_RUN))
1282 err = module_do_install_commands(m, options,
1283 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001284 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001285 if (print_action != NULL)
1286 print_action(m, false, options ?: "");
1287
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001288 if (!(flags & KMOD_PROBE_DRY_RUN))
1289 err = kmod_module_insert_module(m, flags,
1290 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001291 }
1292
1293 free(options);
1294
Lucas De Marchiabd55572012-02-19 04:20:30 -02001295finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001296 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001297 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001298 * already loaded and the module being loaded is not a softdep
1299 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001300 *
1301 * We need to check here because of race conditions. We
1302 * checked first if module was already loaded but it may have
1303 * been loaded between the check and the moment we try to
1304 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001305 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001306 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001307 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001308 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001309
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001310 if (err == -EEXIST)
1311 err = 0;
1312 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001313 break;
1314 }
1315
1316 kmod_module_unref_list(list);
1317 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001318}
1319
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001320/**
1321 * kmod_module_get_options:
1322 * @mod: kmod module
1323 *
1324 * Get options of this kmod module. Options come from the configuration file
1325 * and are cached in @mod. The first call to this function will search for
1326 * this module in configuration and subsequent calls return the cached string.
1327 *
1328 * Returns: a string with all the options separated by spaces. This string is
1329 * owned by @mod, do not free it.
1330 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001331KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1332{
1333 if (mod == NULL)
1334 return NULL;
1335
1336 if (!mod->init.options) {
1337 /* lazy init */
1338 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001339 const struct kmod_list *l;
1340 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001341 char *opts = NULL;
1342 size_t optslen = 0;
1343
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001344 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001345
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001346 kmod_list_foreach(l, config->options) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001347 const char *modname = kmod_option_get_modname(l);
1348 const char *str;
1349 size_t len;
1350 void *tmp;
1351
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001352 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1353 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1354 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001355 continue;
1356
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001357 DBG(mod->ctx, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001358 str = kmod_option_get_options(l);
1359 len = strlen(str);
1360 if (len < 1)
1361 continue;
1362
1363 tmp = realloc(opts, optslen + len + 2);
1364 if (tmp == NULL) {
1365 free(opts);
1366 goto failed;
1367 }
1368
1369 opts = tmp;
1370
1371 if (optslen > 0) {
1372 opts[optslen] = ' ';
1373 optslen++;
1374 }
1375
1376 memcpy(opts + optslen, str, len);
1377 optslen += len;
1378 opts[optslen] = '\0';
1379 }
1380
1381 m->init.options = true;
1382 m->options = opts;
1383 }
1384
1385 return mod->options;
1386
1387failed:
1388 ERR(mod->ctx, "out of memory\n");
1389 return NULL;
1390}
1391
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001392/**
1393 * kmod_module_get_install_commands:
1394 * @mod: kmod module
1395 *
1396 * Get install commands for this kmod module. Install commands come from the
1397 * configuration file and are cached in @mod. The first call to this function
1398 * will search for this module in configuration and subsequent calls return
1399 * the cached string. The install commands are returned as they were in the
1400 * configuration, concatenated by ';'. No other processing is made in this
1401 * string.
1402 *
1403 * Returns: a string with all install commands separated by semicolons. This
1404 * string is owned by @mod, do not free it.
1405 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001406KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1407{
1408 if (mod == NULL)
1409 return NULL;
1410
1411 if (!mod->init.install_commands) {
1412 /* lazy init */
1413 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001414 const struct kmod_list *l;
1415 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001416
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001417 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001418
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001419 kmod_list_foreach(l, config->install_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001420 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001421
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001422 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001423 continue;
1424
Lucas De Marchi60f67602011-12-16 03:33:26 -02001425 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001426
Lucas De Marchi60f67602011-12-16 03:33:26 -02001427 /*
1428 * find only the first command, as modprobe from
1429 * module-init-tools does
1430 */
1431 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001432 }
1433
1434 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001435 }
1436
1437 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001438}
1439
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001440void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1441{
1442 mod->init.install_commands = true;
1443 mod->install_commands = cmd;
1444}
1445
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001446static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1447{
1448 struct kmod_list *ret = NULL;
1449 unsigned i;
1450
1451 for (i = 0; i < count; i++) {
1452 const char *depname = array[i];
1453 struct kmod_list *lst = NULL;
1454 int err;
1455
1456 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1457 if (err < 0) {
1458 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1459 continue;
1460 } else if (lst != NULL)
1461 ret = kmod_list_append_list(ret, lst);
1462 }
1463 return ret;
1464}
1465
1466/**
1467 * kmod_module_get_softdeps:
1468 * @mod: kmod module
1469 * @pre: where to save the list of preceding soft dependencies.
1470 * @post: where to save the list of post soft dependencies.
1471 *
1472 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001473 * from configuration file and are not cached in @mod because it may include
1474 * dependency cycles that would make we leak kmod_module. Any call
1475 * to this function will search for this module in configuration, allocate a
1476 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001477 *
1478 * Both @pre and @post are newly created list of kmod_module and
1479 * should be unreferenced with kmod_module_unref_list().
1480 *
1481 * Returns: 0 on success or < 0 otherwise.
1482 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001483KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1484 struct kmod_list **pre,
1485 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001486{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001487 const struct kmod_list *l;
1488 const struct kmod_config *config;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001489
1490 if (mod == NULL || pre == NULL || post == NULL)
1491 return -ENOENT;
1492
1493 assert(*pre == NULL);
1494 assert(*post == NULL);
1495
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001496 config = kmod_get_config(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001497
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001498 kmod_list_foreach(l, config->softdeps) {
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001499 const char *modname = kmod_softdep_get_name(l);
1500 const char * const *array;
1501 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001502
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001503 if (fnmatch(modname, mod->name, 0) != 0)
1504 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001505
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001506 array = kmod_softdep_get_pre(l, &count);
1507 *pre = lookup_softdep(mod->ctx, array, count);
1508 array = kmod_softdep_get_post(l, &count);
1509 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001510
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001511 /*
1512 * find only the first command, as modprobe from
1513 * module-init-tools does
1514 */
1515 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001516 }
1517
1518 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001519}
1520
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001521/**
1522 * kmod_module_get_remove_commands:
1523 * @mod: kmod module
1524 *
1525 * Get remove commands for this kmod module. Remove commands come from the
1526 * configuration file and are cached in @mod. The first call to this function
1527 * will search for this module in configuration and subsequent calls return
1528 * the cached string. The remove commands are returned as they were in the
1529 * configuration, concatenated by ';'. No other processing is made in this
1530 * string.
1531 *
1532 * Returns: a string with all remove commands separated by semicolons. This
1533 * string is owned by @mod, do not free it.
1534 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001535KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1536{
1537 if (mod == NULL)
1538 return NULL;
1539
1540 if (!mod->init.remove_commands) {
1541 /* lazy init */
1542 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001543 const struct kmod_list *l;
1544 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001545
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001546 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001547
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001548 kmod_list_foreach(l, config->remove_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001549 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001550
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001551 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001552 continue;
1553
Lucas De Marchi60f67602011-12-16 03:33:26 -02001554 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001555
Lucas De Marchi60f67602011-12-16 03:33:26 -02001556 /*
1557 * find only the first command, as modprobe from
1558 * module-init-tools does
1559 */
1560 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001561 }
1562
1563 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001564 }
1565
1566 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001567}
1568
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001569void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1570{
1571 mod->init.remove_commands = true;
1572 mod->remove_commands = cmd;
1573}
1574
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001575/**
1576 * SECTION:libkmod-loaded
1577 * @short_description: currently loaded modules
1578 *
1579 * Information about currently loaded modules, as reported by Linux kernel.
1580 * These information are not cached by libkmod and are always read from /sys
1581 * and /proc/modules.
1582 */
1583
1584/**
1585 * kmod_module_new_from_loaded:
1586 * @ctx: kmod library context
1587 * @list: where to save the list of loaded modules
1588 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001589 * Create a new list of kmod modules with all modules currently loaded in
1590 * kernel. It uses /proc/modules to get the names of loaded modules and to
1591 * create kmod modules by calling kmod_module_new_from_name() in each of them.
Chengwei Yang491c4902013-05-04 17:07:02 +08001592 * They are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001593 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001594 * The initial refcount is 1, and needs to be decremented to release the
1595 * resources of the kmod_module. The returned @list must be released by
1596 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1597 * kmod_modules created, they are all released upon @ctx destruction too. Do
1598 * not unref @ctx before all the desired operations with the returned list are
1599 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001600 *
1601 * Returns: 0 on success or < 0 on error.
1602 */
1603KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1604 struct kmod_list **list)
1605{
1606 struct kmod_list *l = NULL;
1607 FILE *fp;
1608 char line[4096];
1609
1610 if (ctx == NULL || list == NULL)
1611 return -ENOENT;
1612
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001613 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001614 if (fp == NULL) {
1615 int err = -errno;
1616 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1617 return err;
1618 }
1619
1620 while (fgets(line, sizeof(line), fp)) {
1621 struct kmod_module *m;
1622 struct kmod_list *node;
1623 int err;
1624 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1625
1626 err = kmod_module_new_from_name(ctx, name, &m);
1627 if (err < 0) {
1628 ERR(ctx, "could not get module from name '%s': %s\n",
1629 name, strerror(-err));
1630 continue;
1631 }
1632
1633 node = kmod_list_append(l, m);
1634 if (node)
1635 l = node;
1636 else {
1637 ERR(ctx, "out of memory\n");
1638 kmod_module_unref(m);
1639 }
1640 }
1641
1642 fclose(fp);
1643 *list = l;
1644
1645 return 0;
1646}
1647
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001648/**
1649 * kmod_module_initstate_str:
1650 * @state: the state as returned by kmod_module_get_initstate()
1651 *
1652 * Translate a initstate to a string.
1653 *
1654 * Returns: the string associated to the @state. This string is statically
1655 * allocated, do not free it.
1656 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001657KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1658{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001659 switch (state) {
1660 case KMOD_MODULE_BUILTIN:
1661 return "builtin";
1662 case KMOD_MODULE_LIVE:
1663 return "live";
1664 case KMOD_MODULE_COMING:
1665 return "coming";
1666 case KMOD_MODULE_GOING:
1667 return "going";
1668 default:
1669 return NULL;
1670 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001671}
1672
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001673/**
1674 * kmod_module_get_initstate:
1675 * @mod: kmod module
1676 *
1677 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1678 * /sys filesystem.
1679 *
Chengwei Yangd7152f62013-05-04 17:07:03 +08001680 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1681 * KMOD_MODULE_BUILTIN: module is builtin;
1682 * KMOD_MODULE_LIVE: module is live in kernel;
1683 * KMOD_MODULE_COMING: module is being loaded;
1684 * KMOD_MODULE_GOING: module is being unloaded.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001685 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001686KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1687{
1688 char path[PATH_MAX], buf[32];
1689 int fd, err, pathlen;
1690
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001691 if (mod == NULL)
1692 return -ENOENT;
1693
Lucas De Marchi38052742012-02-16 20:43:16 -02001694 if (mod->builtin)
1695 return KMOD_MODULE_BUILTIN;
1696
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001697 pathlen = snprintf(path, sizeof(path),
1698 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001699 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001700 if (fd < 0) {
1701 err = -errno;
1702
Lucas De Marchiddbda022011-12-27 11:40:10 -02001703 DBG(mod->ctx, "could not open '%s': %s\n",
1704 path, strerror(-err));
1705
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001706 if (pathlen > (int)sizeof("/initstate") - 1) {
1707 struct stat st;
1708 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1709 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1710 return KMOD_MODULE_BUILTIN;
1711 }
1712
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001713 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001714 path, strerror(-err));
1715 return err;
1716 }
1717
1718 err = read_str_safe(fd, buf, sizeof(buf));
1719 close(fd);
1720 if (err < 0) {
1721 ERR(mod->ctx, "could not read from '%s': %s\n",
1722 path, strerror(-err));
1723 return err;
1724 }
1725
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001726 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001727 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001728 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001729 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001730 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001731 return KMOD_MODULE_GOING;
1732
1733 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1734 return -EINVAL;
1735}
1736
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001737/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001738 * kmod_module_get_size:
1739 * @mod: kmod module
1740 *
Dave Reisner486f9012012-06-28 09:09:48 -04001741 * Get the size of this kmod module as returned by Linux kernel. If supported,
1742 * the size is read from the coresize attribute in /sys/module. For older
1743 * kernels, this falls back on /proc/modules and searches for the specified
1744 * module to get its size.
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001745 *
1746 * Returns: the size of this kmod module.
1747 */
1748KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1749{
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001750 FILE *fp;
1751 char line[4096];
1752 int lineno = 0;
1753 long size = -ENOENT;
Dave Reisner486f9012012-06-28 09:09:48 -04001754 int dfd, cfd;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001755
1756 if (mod == NULL)
1757 return -ENOENT;
1758
Dave Reisner486f9012012-06-28 09:09:48 -04001759 /* try to open the module dir in /sys. If this fails, don't
1760 * bother trying to find the size as we know the module isn't
1761 * loaded.
1762 */
1763 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
1764 dfd = open(line, O_RDONLY);
1765 if (dfd < 0)
1766 return -errno;
1767
1768 /* available as of linux 3.3.x */
1769 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1770 if (cfd >= 0) {
1771 if (read_str_long(cfd, &size, 10) < 0)
1772 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1773 close(cfd);
1774 goto done;
1775 }
1776
1777 /* fall back on parsing /proc/modules */
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001778 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001779 if (fp == NULL) {
1780 int err = -errno;
1781 ERR(mod->ctx,
1782 "could not open /proc/modules: %s\n", strerror(errno));
1783 return err;
1784 }
1785
1786 while (fgets(line, sizeof(line), fp)) {
1787 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1788 long value;
1789
1790 lineno++;
1791 if (tok == NULL || !streq(tok, mod->name))
1792 continue;
1793
1794 tok = strtok_r(NULL, " \t", &saveptr);
1795 if (tok == NULL) {
1796 ERR(mod->ctx,
1797 "invalid line format at /proc/modules:%d\n", lineno);
1798 break;
1799 }
1800
1801 value = strtol(tok, &endptr, 10);
1802 if (endptr == tok || *endptr != '\0') {
1803 ERR(mod->ctx,
1804 "invalid line format at /proc/modules:%d\n", lineno);
1805 break;
1806 }
1807
1808 size = value;
1809 break;
1810 }
1811 fclose(fp);
Dave Reisner486f9012012-06-28 09:09:48 -04001812
1813done:
1814 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001815 return size;
1816}
1817
1818/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001819 * kmod_module_get_refcnt:
1820 * @mod: kmod module
1821 *
1822 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1823 * /sys filesystem.
1824 *
1825 * Returns: 0 on success or < 0 on failure.
1826 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001827KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1828{
1829 char path[PATH_MAX];
1830 long refcnt;
1831 int fd, err;
1832
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001833 if (mod == NULL)
1834 return -ENOENT;
1835
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001836 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001837 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001838 if (fd < 0) {
1839 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001840 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001841 path, strerror(errno));
1842 return err;
1843 }
1844
1845 err = read_str_long(fd, &refcnt, 10);
1846 close(fd);
1847 if (err < 0) {
1848 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1849 path, strerror(-err));
1850 return err;
1851 }
1852
1853 return (int)refcnt;
1854}
1855
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001856/**
1857 * kmod_module_get_holders:
1858 * @mod: kmod module
1859 *
1860 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1861 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1862 *
1863 * Returns: a new list of kmod modules on success or NULL on failure.
1864 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001865KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1866{
1867 char dname[PATH_MAX];
1868 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001869 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001870 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001871
Lucas De Marchib9a7da32013-04-23 20:47:28 -03001872 if (mod == NULL || mod->ctx == NULL)
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001873 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001874
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001875 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1876
1877 d = opendir(dname);
1878 if (d == NULL) {
1879 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001880 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001881 return NULL;
1882 }
1883
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001884 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001885 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001886 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001887 int err;
1888
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001889 if (dent->d_name[0] == '.') {
1890 if (dent->d_name[1] == '\0' ||
1891 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001892 continue;
1893 }
1894
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001895 err = kmod_module_new_from_name(mod->ctx, dent->d_name,
1896 &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001897 if (err < 0) {
1898 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001899 dent->d_name, strerror(-err));
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001900 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001901 }
1902
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001903 l = kmod_list_append(list, holder);
1904 if (l != NULL) {
1905 list = l;
1906 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001907 ERR(mod->ctx, "out of memory\n");
1908 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001909 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001910 }
1911 }
1912
1913 closedir(d);
1914 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001915
1916fail:
1917 closedir(d);
1918 kmod_module_unref_list(list);
1919 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001920}
1921
1922struct kmod_module_section {
1923 unsigned long address;
1924 char name[];
1925};
1926
1927static void kmod_module_section_free(struct kmod_module_section *section)
1928{
1929 free(section);
1930}
1931
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001932/**
1933 * kmod_module_get_sections:
1934 * @mod: kmod module
1935 *
1936 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1937 * structure contained in this list is internal to libkmod and their fields
1938 * can be obtained by calling kmod_module_section_get_name() and
1939 * kmod_module_section_get_address().
1940 *
1941 * After use, free the @list by calling kmod_module_section_free_list().
1942 *
1943 * Returns: a new list of kmod module sections on success or NULL on failure.
1944 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001945KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1946{
1947 char dname[PATH_MAX];
1948 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001949 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001950 DIR *d;
1951 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001952
1953 if (mod == NULL)
1954 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001955
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001956 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1957
1958 d = opendir(dname);
1959 if (d == NULL) {
1960 ERR(mod->ctx, "could not open '%s': %s\n",
1961 dname, strerror(errno));
1962 return NULL;
1963 }
1964
1965 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001966
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001967 for (dent = readdir(d); dent; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001968 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001969 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001970 unsigned long address;
1971 size_t namesz;
1972 int fd, err;
1973
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001974 if (dent->d_name[0] == '.') {
1975 if (dent->d_name[1] == '\0' ||
1976 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001977 continue;
1978 }
1979
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001980 fd = openat(dfd, dent->d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001981 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001982 ERR(mod->ctx, "could not open '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001983 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001984 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001985 }
1986
1987 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001988 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001989 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001990 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001991 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001992 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001993 }
1994
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001995 namesz = strlen(dent->d_name) + 1;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001996 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001997
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001998 if (section == NULL) {
1999 ERR(mod->ctx, "out of memory\n");
2000 goto fail;
2001 }
2002
2003 section->address = address;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002004 memcpy(section->name, dent->d_name, namesz);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002005
2006 l = kmod_list_append(list, section);
2007 if (l != NULL) {
2008 list = l;
2009 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002010 ERR(mod->ctx, "out of memory\n");
2011 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002012 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002013 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002014 }
2015
2016 closedir(d);
2017 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002018
2019fail:
2020 closedir(d);
2021 kmod_module_unref_list(list);
2022 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002023}
2024
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002025/**
2026 * kmod_module_section_get_module_name:
2027 * @entry: a list entry representing a kmod module section
2028 *
2029 * Get the name of a kmod module section.
2030 *
2031 * After use, free the @list by calling kmod_module_section_free_list().
2032 *
2033 * Returns: the name of this kmod module section on success or NULL on
2034 * failure. The string is owned by the section, do not free it.
2035 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002036KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2037{
2038 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002039
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002040 if (entry == NULL)
2041 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002042
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002043 section = entry->data;
2044 return section->name;
2045}
2046
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002047/**
2048 * kmod_module_section_get_address:
2049 * @entry: a list entry representing a kmod module section
2050 *
2051 * Get the address of a kmod module section.
2052 *
2053 * After use, free the @list by calling kmod_module_section_free_list().
2054 *
2055 * Returns: the address of this kmod module section on success or ULONG_MAX
2056 * on failure.
2057 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002058KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2059{
2060 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002061
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002062 if (entry == NULL)
2063 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002064
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002065 section = entry->data;
2066 return section->address;
2067}
2068
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002069/**
2070 * kmod_module_section_free_list:
2071 * @list: kmod module section list
2072 *
2073 * Release the resources taken by @list
2074 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002075KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2076{
2077 while (list) {
2078 kmod_module_section_free(list->data);
2079 list = kmod_list_remove(list);
2080 }
2081}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002082
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002083static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2084{
2085 if (mod->file == NULL) {
2086 const char *path = kmod_module_get_path(mod);
2087
2088 if (path == NULL) {
2089 errno = ENOENT;
2090 return NULL;
2091 }
2092
2093 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2094 path);
2095 if (mod->file == NULL)
2096 return NULL;
2097 }
2098
2099 return kmod_file_get_elf(mod->file);
2100}
2101
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002102struct kmod_module_info {
2103 char *key;
2104 char value[];
2105};
2106
2107static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2108{
2109 struct kmod_module_info *info;
2110
2111 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2112 if (info == NULL)
2113 return NULL;
2114
2115 info->key = (char *)info + sizeof(struct kmod_module_info)
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002116 + valuelen + 1;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002117 memcpy(info->key, key, keylen);
2118 info->key[keylen] = '\0';
2119 memcpy(info->value, value, valuelen);
2120 info->value[valuelen] = '\0';
2121 return info;
2122}
2123
2124static void kmod_module_info_free(struct kmod_module_info *info)
2125{
2126 free(info);
2127}
2128
Michal Marekf64458c2013-01-16 10:18:17 +01002129static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2130{
2131 struct kmod_module_info *info;
2132 struct kmod_list *n;
2133
2134 info = kmod_module_info_new(key, keylen, value, valuelen);
Michal Marek63339342013-01-16 17:51:57 +01002135 if (info == NULL)
Michal Marekf64458c2013-01-16 10:18:17 +01002136 return NULL;
Michal Marekf64458c2013-01-16 10:18:17 +01002137 n = kmod_list_append(*list, info);
Michal Marek63339342013-01-16 17:51:57 +01002138 if (n != NULL)
2139 *list = n;
2140 else
Michal Marekf64458c2013-01-16 10:18:17 +01002141 kmod_module_info_free(info);
Michal Marekf64458c2013-01-16 10:18:17 +01002142 return n;
2143}
2144
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002145/**
2146 * kmod_module_get_info:
2147 * @mod: kmod module
2148 * @list: where to return list of module information. Use
2149 * kmod_module_info_get_key() and
2150 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002151 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002152 *
2153 * Get a list of entries in ELF section ".modinfo", these contain
2154 * alias, license, depends, vermagic and other keys with respective
Michal Marek8fe16812013-01-16 09:52:01 +01002155 * values. If the module is signed (CONFIG_MODULE_SIG), information
2156 * about the module signature is included as well: signer,
2157 * sig_key and sig_hashalgo.
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002158 *
2159 * After use, free the @list by calling kmod_module_info_free_list().
2160 *
2161 * Returns: 0 on success or < 0 otherwise.
2162 */
2163KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2164{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002165 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002166 char **strings;
Michal Marekf64458c2013-01-16 10:18:17 +01002167 int i, count, ret = -ENOMEM;
Michal Marek8fe16812013-01-16 09:52:01 +01002168 struct kmod_signature_info sig_info;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002169
2170 if (mod == NULL || list == NULL)
2171 return -ENOENT;
2172
2173 assert(*list == NULL);
2174
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002175 elf = kmod_module_get_elf(mod);
2176 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002177 return -errno;
2178
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002179 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002180 if (count < 0)
2181 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002182
2183 for (i = 0; i < count; i++) {
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002184 struct kmod_list *n;
2185 const char *key, *value;
2186 size_t keylen, valuelen;
2187
2188 key = strings[i];
2189 value = strchr(key, '=');
2190 if (value == NULL) {
2191 keylen = strlen(key);
2192 valuelen = 0;
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002193 value = key;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002194 } else {
2195 keylen = value - key;
2196 value++;
2197 valuelen = strlen(value);
2198 }
2199
Michal Marekf64458c2013-01-16 10:18:17 +01002200 n = kmod_module_info_append(list, key, keylen, value, valuelen);
2201 if (n == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002202 goto list_error;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002203 }
Michal Marek8fe16812013-01-16 09:52:01 +01002204
2205 if (kmod_module_signature_info(mod->file, &sig_info)) {
2206 struct kmod_list *n;
2207 char *key_hex;
2208
2209 n = kmod_module_info_append(list, "signer", strlen("signer"),
2210 sig_info.signer, sig_info.signer_len);
2211 if (n == NULL)
2212 goto list_error;
2213 count++;
2214
2215 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2216 key_hex = malloc(sig_info.key_id_len * 3);
2217 if (key_hex == NULL)
2218 goto list_error;
2219 for (i = 0; i < (int)sig_info.key_id_len; i++) {
2220 sprintf(key_hex + i * 3, "%02X",
2221 (unsigned char)sig_info.key_id[i]);
2222 if (i < (int)sig_info.key_id_len - 1)
2223 key_hex[i * 3 + 2] = ':';
2224 }
2225 n = kmod_module_info_append(list, "sig_key", strlen("sig_key"),
2226 key_hex, sig_info.key_id_len * 3 - 1);
2227 free(key_hex);
2228 if (n == NULL)
2229 goto list_error;
2230 count++;
2231
2232 n = kmod_module_info_append(list,
2233 "sig_hashalgo", strlen("sig_hashalgo"),
2234 sig_info.hash_algo, strlen(sig_info.hash_algo));
2235 if (n == NULL)
2236 goto list_error;
2237 count++;
2238
2239 /*
2240 * Omit sig_info.id_type and sig_info.algo for now, as these
2241 * are currently constant.
2242 */
2243 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002244 ret = count;
2245
2246list_error:
Michal Marek63339342013-01-16 17:51:57 +01002247 if (ret < 0) {
2248 kmod_module_info_free_list(*list);
2249 *list = NULL;
2250 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002251 free(strings);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002252 return ret;
2253}
2254
2255/**
2256 * kmod_module_info_get_key:
2257 * @entry: a list entry representing a kmod module info
2258 *
2259 * Get the key of a kmod module info.
2260 *
2261 * Returns: the key of this kmod module info on success or NULL on
2262 * failure. The string is owned by the info, do not free it.
2263 */
2264KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2265{
2266 struct kmod_module_info *info;
2267
2268 if (entry == NULL)
2269 return NULL;
2270
2271 info = entry->data;
2272 return info->key;
2273}
2274
2275/**
2276 * kmod_module_info_get_value:
2277 * @entry: a list entry representing a kmod module info
2278 *
2279 * Get the value of a kmod module info.
2280 *
2281 * Returns: the value of this kmod module info on success or NULL on
2282 * failure. The string is owned by the info, do not free it.
2283 */
2284KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2285{
2286 struct kmod_module_info *info;
2287
2288 if (entry == NULL)
2289 return NULL;
2290
2291 info = entry->data;
2292 return info->value;
2293}
2294
2295/**
2296 * kmod_module_info_free_list:
2297 * @list: kmod module info list
2298 *
2299 * Release the resources taken by @list
2300 */
2301KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2302{
2303 while (list) {
2304 kmod_module_info_free(list->data);
2305 list = kmod_list_remove(list);
2306 }
2307}
2308
2309struct kmod_module_version {
2310 uint64_t crc;
2311 char symbol[];
2312};
2313
2314static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2315{
2316 struct kmod_module_version *mv;
2317 size_t symbollen = strlen(symbol) + 1;
2318
2319 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2320 if (mv == NULL)
2321 return NULL;
2322
2323 mv->crc = crc;
2324 memcpy(mv->symbol, symbol, symbollen);
2325 return mv;
2326}
2327
2328static void kmod_module_version_free(struct kmod_module_version *version)
2329{
2330 free(version);
2331}
2332
2333/**
2334 * kmod_module_get_versions:
2335 * @mod: kmod module
2336 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002337 * kmod_module_version_get_symbol() and
2338 * kmod_module_version_get_crc(). Release this list with
2339 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002340 *
2341 * Get a list of entries in ELF section "__versions".
2342 *
2343 * After use, free the @list by calling kmod_module_versions_free_list().
2344 *
2345 * Returns: 0 on success or < 0 otherwise.
2346 */
2347KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2348{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002349 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002350 struct kmod_modversion *versions;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002351 int i, count, ret = 0;
2352
2353 if (mod == NULL || list == NULL)
2354 return -ENOENT;
2355
2356 assert(*list == NULL);
2357
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002358 elf = kmod_module_get_elf(mod);
2359 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002360 return -errno;
2361
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002362 count = kmod_elf_get_modversions(elf, &versions);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002363 if (count < 0)
2364 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002365
2366 for (i = 0; i < count; i++) {
2367 struct kmod_module_version *mv;
2368 struct kmod_list *n;
2369
2370 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2371 if (mv == NULL) {
2372 ret = -errno;
2373 kmod_module_versions_free_list(*list);
2374 *list = NULL;
2375 goto list_error;
2376 }
2377
2378 n = kmod_list_append(*list, mv);
2379 if (n != NULL)
2380 *list = n;
2381 else {
2382 kmod_module_version_free(mv);
2383 kmod_module_versions_free_list(*list);
2384 *list = NULL;
2385 ret = -ENOMEM;
2386 goto list_error;
2387 }
2388 }
2389 ret = count;
2390
2391list_error:
2392 free(versions);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002393 return ret;
2394}
2395
2396/**
Chengwei Yang491c4902013-05-04 17:07:02 +08002397 * kmod_module_version_get_symbol:
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002398 * @entry: a list entry representing a kmod module versions
2399 *
2400 * Get the symbol of a kmod module versions.
2401 *
2402 * Returns: the symbol of this kmod module versions on success or NULL
2403 * on failure. The string is owned by the versions, do not free it.
2404 */
2405KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2406{
2407 struct kmod_module_version *version;
2408
2409 if (entry == NULL)
2410 return NULL;
2411
2412 version = entry->data;
2413 return version->symbol;
2414}
2415
2416/**
2417 * kmod_module_version_get_crc:
2418 * @entry: a list entry representing a kmod module version
2419 *
2420 * Get the crc of a kmod module version.
2421 *
2422 * Returns: the crc of this kmod module version on success or NULL on
2423 * failure. The string is owned by the version, do not free it.
2424 */
2425KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2426{
2427 struct kmod_module_version *version;
2428
2429 if (entry == NULL)
2430 return 0;
2431
2432 version = entry->data;
2433 return version->crc;
2434}
2435
2436/**
2437 * kmod_module_versions_free_list:
2438 * @list: kmod module versions list
2439 *
2440 * Release the resources taken by @list
2441 */
2442KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2443{
2444 while (list) {
2445 kmod_module_version_free(list->data);
2446 list = kmod_list_remove(list);
2447 }
2448}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002449
2450struct kmod_module_symbol {
2451 uint64_t crc;
2452 char symbol[];
2453};
2454
2455static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2456{
2457 struct kmod_module_symbol *mv;
2458 size_t symbollen = strlen(symbol) + 1;
2459
2460 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2461 if (mv == NULL)
2462 return NULL;
2463
2464 mv->crc = crc;
2465 memcpy(mv->symbol, symbol, symbollen);
2466 return mv;
2467}
2468
2469static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2470{
2471 free(symbol);
2472}
2473
2474/**
2475 * kmod_module_get_symbols:
2476 * @mod: kmod module
2477 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002478 * kmod_module_symbol_get_symbol() and
2479 * kmod_module_symbol_get_crc(). Release this list with
2480 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002481 *
2482 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2483 *
2484 * After use, free the @list by calling kmod_module_symbols_free_list().
2485 *
2486 * Returns: 0 on success or < 0 otherwise.
2487 */
2488KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2489{
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002490 struct kmod_elf *elf;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002491 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002492 int i, count, ret = 0;
2493
2494 if (mod == NULL || list == NULL)
2495 return -ENOENT;
2496
2497 assert(*list == NULL);
2498
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002499 elf = kmod_module_get_elf(mod);
2500 if (elf == NULL)
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002501 return -errno;
2502
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002503 count = kmod_elf_get_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002504 if (count < 0)
2505 return count;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002506
2507 for (i = 0; i < count; i++) {
2508 struct kmod_module_symbol *mv;
2509 struct kmod_list *n;
2510
2511 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2512 if (mv == NULL) {
2513 ret = -errno;
2514 kmod_module_symbols_free_list(*list);
2515 *list = NULL;
2516 goto list_error;
2517 }
2518
2519 n = kmod_list_append(*list, mv);
2520 if (n != NULL)
2521 *list = n;
2522 else {
2523 kmod_module_symbol_free(mv);
2524 kmod_module_symbols_free_list(*list);
2525 *list = NULL;
2526 ret = -ENOMEM;
2527 goto list_error;
2528 }
2529 }
2530 ret = count;
2531
2532list_error:
2533 free(symbols);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002534 return ret;
2535}
2536
2537/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002538 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002539 * @entry: a list entry representing a kmod module symbols
2540 *
2541 * Get the symbol of a kmod module symbols.
2542 *
2543 * Returns: the symbol of this kmod module symbols on success or NULL
2544 * on failure. The string is owned by the symbols, do not free it.
2545 */
2546KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2547{
2548 struct kmod_module_symbol *symbol;
2549
2550 if (entry == NULL)
2551 return NULL;
2552
2553 symbol = entry->data;
2554 return symbol->symbol;
2555}
2556
2557/**
2558 * kmod_module_symbol_get_crc:
2559 * @entry: a list entry representing a kmod module symbol
2560 *
2561 * Get the crc of a kmod module symbol.
2562 *
2563 * Returns: the crc of this kmod module symbol on success or NULL on
2564 * failure. The string is owned by the symbol, do not free it.
2565 */
2566KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2567{
2568 struct kmod_module_symbol *symbol;
2569
2570 if (entry == NULL)
2571 return 0;
2572
2573 symbol = entry->data;
2574 return symbol->crc;
2575}
2576
2577/**
2578 * kmod_module_symbols_free_list:
2579 * @list: kmod module symbols list
2580 *
2581 * Release the resources taken by @list
2582 */
2583KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2584{
2585 while (list) {
2586 kmod_module_symbol_free(list->data);
2587 list = kmod_list_remove(list);
2588 }
2589}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002590
2591struct kmod_module_dependency_symbol {
2592 uint64_t crc;
2593 uint8_t bind;
2594 char symbol[];
2595};
2596
2597static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2598{
2599 struct kmod_module_dependency_symbol *mv;
2600 size_t symbollen = strlen(symbol) + 1;
2601
2602 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2603 if (mv == NULL)
2604 return NULL;
2605
2606 mv->crc = crc;
2607 mv->bind = bind;
2608 memcpy(mv->symbol, symbol, symbollen);
2609 return mv;
2610}
2611
2612static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2613{
2614 free(dependency_symbol);
2615}
2616
2617/**
2618 * kmod_module_get_dependency_symbols:
2619 * @mod: kmod module
2620 * @list: where to return list of module dependency_symbols. Use
2621 * kmod_module_dependency_symbol_get_symbol() and
2622 * kmod_module_dependency_symbol_get_crc(). Release this list with
2623 * kmod_module_dependency_symbols_free_list()
2624 *
2625 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2626 *
2627 * After use, free the @list by calling
2628 * kmod_module_dependency_symbols_free_list().
2629 *
2630 * Returns: 0 on success or < 0 otherwise.
2631 */
2632KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2633{
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002634 struct kmod_elf *elf;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002635 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002636 int i, count, ret = 0;
2637
2638 if (mod == NULL || list == NULL)
2639 return -ENOENT;
2640
2641 assert(*list == NULL);
2642
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002643 elf = kmod_module_get_elf(mod);
2644 if (elf == NULL)
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002645 return -errno;
2646
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002647 count = kmod_elf_get_dependency_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002648 if (count < 0)
2649 return count;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002650
2651 for (i = 0; i < count; i++) {
2652 struct kmod_module_dependency_symbol *mv;
2653 struct kmod_list *n;
2654
2655 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2656 symbols[i].bind,
2657 symbols[i].symbol);
2658 if (mv == NULL) {
2659 ret = -errno;
2660 kmod_module_dependency_symbols_free_list(*list);
2661 *list = NULL;
2662 goto list_error;
2663 }
2664
2665 n = kmod_list_append(*list, mv);
2666 if (n != NULL)
2667 *list = n;
2668 else {
2669 kmod_module_dependency_symbol_free(mv);
2670 kmod_module_dependency_symbols_free_list(*list);
2671 *list = NULL;
2672 ret = -ENOMEM;
2673 goto list_error;
2674 }
2675 }
2676 ret = count;
2677
2678list_error:
2679 free(symbols);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002680 return ret;
2681}
2682
2683/**
2684 * kmod_module_dependency_symbol_get_symbol:
2685 * @entry: a list entry representing a kmod module dependency_symbols
2686 *
2687 * Get the dependency symbol of a kmod module
2688 *
2689 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2690 * on failure. The string is owned by the dependency_symbols, do not free it.
2691 */
2692KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2693{
2694 struct kmod_module_dependency_symbol *dependency_symbol;
2695
2696 if (entry == NULL)
2697 return NULL;
2698
2699 dependency_symbol = entry->data;
2700 return dependency_symbol->symbol;
2701}
2702
2703/**
2704 * kmod_module_dependency_symbol_get_crc:
2705 * @entry: a list entry representing a kmod module dependency_symbol
2706 *
2707 * Get the crc of a kmod module dependency_symbol.
2708 *
2709 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2710 * failure. The string is owned by the dependency_symbol, do not free it.
2711 */
2712KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2713{
2714 struct kmod_module_dependency_symbol *dependency_symbol;
2715
2716 if (entry == NULL)
2717 return 0;
2718
2719 dependency_symbol = entry->data;
2720 return dependency_symbol->crc;
2721}
2722
2723/**
2724 * kmod_module_dependency_symbol_get_bind:
2725 * @entry: a list entry representing a kmod module dependency_symbol
2726 *
2727 * Get the bind type of a kmod module dependency_symbol.
2728 *
2729 * Returns: the bind of this kmod module dependency_symbol on success
2730 * or < 0 on failure.
2731 */
2732KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2733{
2734 struct kmod_module_dependency_symbol *dependency_symbol;
2735
2736 if (entry == NULL)
2737 return 0;
2738
2739 dependency_symbol = entry->data;
2740 return dependency_symbol->bind;
2741}
2742
2743/**
2744 * kmod_module_dependency_symbols_free_list:
2745 * @list: kmod module dependency_symbols list
2746 *
2747 * Release the resources taken by @list
2748 */
2749KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2750{
2751 while (list) {
2752 kmod_module_dependency_symbol_free(list->data);
2753 list = kmod_list_remove(list);
2754 }
2755}