blob: cc2c076f27a0eb95311b0e81510153cacf800301 [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"
46#include "libkmod-private.h"
47
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
Chengwei Yangd7152f62013-05-04 17:07:03 +0800741 * @flags: flags to pass to Linux kernel when removing the module, valid flags are
742 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
743 * use by a kernel subsystem or other process;
744 * KMOD_REMOVE_NOWAIT: return immediately. It will fail if the module
745 * is in using and KMOD_REMOVE_FORCE is not specified.
746 * If this module is in use by any kernel subsystem or process, not using
747 * this flag will cause the call to block indefinitely, until the module
748 * is not in use anymore. Always use this flag, it's deprecated not using
749 * it and the default behavior might change in future to always set it.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200750 *
751 * Remove a module from Linux kernel.
752 *
753 * Returns: 0 on success or < 0 on failure.
754 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200755KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
756 unsigned int flags)
757{
758 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200759
760 if (mod == NULL)
761 return -ENOENT;
762
763 /* Filter out other flags */
764 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
765
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200766 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200767 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200768 err = -errno;
769 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200770 }
771
Lucas De Marchiba998b92012-01-11 00:08:14 -0200772 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200773}
774
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200775extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200776
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200777/**
778 * kmod_module_insert_module:
779 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200780 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +0800781 * behavior of this function, valid flags are
782 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
783 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200784 * @options: module's options to pass to Linux Kernel.
785 *
786 * Insert a module in Linux kernel. It opens the file pointed by @mod,
787 * mmap'ing it and passing to kernel.
788 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200789 * Returns: 0 on success or < 0 on failure. If module is already loaded it
790 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200791 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200792KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200793 unsigned int flags,
794 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200795{
796 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200797 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200798 off_t size;
799 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200800 struct kmod_elf *elf = NULL;
801 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200802 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200803
804 if (mod == NULL)
805 return -ENOENT;
806
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200807 path = kmod_module_get_path(mod);
808 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500809 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200810 return -ENOSYS;
811 }
812
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200813 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200814 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200815 err = -errno;
816 return err;
817 }
818
Kees Cook144d1822013-02-18 12:02:32 -0800819 if (kmod_file_get_direct(file)) {
820 unsigned int kernel_flags = 0;
821
Kees Cook144d1822013-02-18 12:02:32 -0800822 if (flags & KMOD_INSERT_FORCE_VERMAGIC)
823 kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
824 if (flags & KMOD_INSERT_FORCE_MODVERSION)
825 kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
Kees Cook144d1822013-02-18 12:02:32 -0800826
827 err = finit_module(kmod_file_get_fd(file), args, kernel_flags);
828 if (err == 0 || errno != ENOSYS)
829 goto init_finished;
830 }
831
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200832 size = kmod_file_get_size(file);
833 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200834
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200835 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
836 elf = kmod_elf_new(mem, size);
837 if (elf == NULL) {
838 err = -errno;
839 goto elf_failed;
840 }
841
842 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
843 err = kmod_elf_strip_section(elf, "__versions");
844 if (err < 0)
845 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
846 }
847
848 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
849 err = kmod_elf_strip_vermagic(elf);
850 if (err < 0)
851 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
852 }
853
854 mem = kmod_elf_get_memory(elf);
855 }
856
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200857 err = init_module(mem, size, args);
Kees Cook144d1822013-02-18 12:02:32 -0800858init_finished:
Lucas De Marchibbf59322011-12-30 14:13:33 -0200859 if (err < 0) {
860 err = -errno;
861 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
862 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200863
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200864 if (elf != NULL)
865 kmod_elf_unref(elf);
866elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200867 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200868
869 return err;
870}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200871
Lucas De Marchiddbda022011-12-27 11:40:10 -0200872static bool module_is_blacklisted(struct kmod_module *mod)
873{
874 struct kmod_ctx *ctx = mod->ctx;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300875 const struct kmod_config *config = kmod_get_config(ctx);
876 const struct kmod_list *bl = config->blacklists;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200877 const struct kmod_list *l;
878
879 kmod_list_foreach(l, bl) {
880 const char *modname = kmod_blacklist_get_modname(l);
881
882 if (streq(modname, mod->name))
883 return true;
884 }
885
886 return false;
887}
888
Dave Reisnerd80b1032012-02-24 10:05:11 -0500889/**
890 * kmod_module_apply_filter
891 * @ctx: kmod library context
Chengwei Yangd7152f62013-05-04 17:07:03 +0800892 * @filter_type: bitmask to filter modules out, valid types are
893 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
894 * KMOD_FILTER_BUILTIN: filter builtin modules out.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500895 * @input: list of kmod_module to be filtered
896 * @output: where to save the new list
897 *
898 * Given a list @input, this function filter it out by the filter mask
899 * and save it in @output.
900 *
901 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
902 * list.
903 */
904KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
905 enum kmod_filter filter_type,
906 const struct kmod_list *input,
907 struct kmod_list **output)
908{
909 const struct kmod_list *li;
910
911 if (ctx == NULL || output == NULL)
912 return -ENOENT;
913
914 *output = NULL;
915 if (input == NULL)
916 return 0;
917
918 kmod_list_foreach(li, input) {
919 struct kmod_module *mod = li->data;
920 struct kmod_list *node;
921
922 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
923 module_is_blacklisted(mod))
924 continue;
925
Dave Reisnerbdda7e12012-02-24 23:02:06 -0500926 if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin)
Dave Reisnerd80b1032012-02-24 10:05:11 -0500927 continue;
928
929 node = kmod_list_append(*output, mod);
930 if (node == NULL)
931 goto fail;
932
933 *output = node;
934 kmod_module_ref(mod);
935 }
936
937 return 0;
938
939fail:
940 kmod_module_unref_list(*output);
941 *output = NULL;
942 return -ENOMEM;
943}
944
Lucas De Marchiddbda022011-12-27 11:40:10 -0200945static int command_do(struct kmod_module *mod, const char *type,
946 const char *cmd)
947{
948 const char *modname = kmod_module_get_name(mod);
949 int err;
950
951 DBG(mod->ctx, "%s %s\n", type, cmd);
952
953 setenv("MODPROBE_MODULE", modname, 1);
954 err = system(cmd);
955 unsetenv("MODPROBE_MODULE");
956
957 if (err == -1 || WEXITSTATUS(err)) {
958 ERR(mod->ctx, "Error running %s command for %s\n",
959 type, modname);
960 if (err != -1)
961 err = -WEXITSTATUS(err);
962 }
963
964 return err;
965}
966
Lucas De Marchib1a51252012-01-29 01:49:09 -0200967struct probe_insert_cb {
968 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
969 void *data;
970};
971
Lucas De Marchiddbda022011-12-27 11:40:10 -0200972static int module_do_install_commands(struct kmod_module *mod,
973 const char *options,
974 struct probe_insert_cb *cb)
975{
976 const char *command = kmod_module_get_install_commands(mod);
977 char *p, *cmd;
978 int err;
979 size_t cmdlen, options_len, varlen;
980
981 assert(command);
982
983 if (options == NULL)
984 options = "";
985
986 options_len = strlen(options);
987 cmdlen = strlen(command);
988 varlen = sizeof("$CMDLINE_OPTS") - 1;
989
990 cmd = memdup(command, cmdlen + 1);
991 if (cmd == NULL)
992 return -ENOMEM;
993
994 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
995 size_t prefixlen = p - cmd;
996 size_t suffixlen = cmdlen - prefixlen - varlen;
997 size_t slen = cmdlen - varlen + options_len;
998 char *suffix = p + varlen;
999 char *s = malloc(slen + 1);
1000 if (s == NULL) {
1001 free(cmd);
1002 return -ENOMEM;
1003 }
1004 memcpy(s, cmd, p - cmd);
1005 memcpy(s + prefixlen, options, options_len);
1006 memcpy(s + prefixlen + options_len, suffix, suffixlen);
1007 s[slen] = '\0';
1008
1009 free(cmd);
1010 cmd = s;
1011 cmdlen = slen;
1012 }
1013
1014 if (cb->run_install != NULL)
1015 err = cb->run_install(mod, cmd, cb->data);
1016 else
1017 err = command_do(mod, "install", cmd);
1018
1019 free(cmd);
1020
1021 return err;
1022}
1023
Lucas De Marchiddbda022011-12-27 11:40:10 -02001024static char *module_options_concat(const char *opt, const char *xopt)
1025{
1026 // TODO: we might need to check if xopt overrides options on opt
1027 size_t optlen = opt == NULL ? 0 : strlen(opt);
1028 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1029 char *r;
1030
1031 if (optlen == 0 && xoptlen == 0)
1032 return NULL;
1033
1034 r = malloc(optlen + xoptlen + 2);
1035
1036 if (opt != NULL) {
1037 memcpy(r, opt, optlen);
1038 r[optlen] = ' ';
1039 optlen++;
1040 }
1041
1042 if (xopt != NULL)
1043 memcpy(r + optlen, xopt, xoptlen);
1044
1045 r[optlen + xoptlen] = '\0';
1046
1047 return r;
1048}
1049
Lucas De Marchib1a51252012-01-29 01:49:09 -02001050static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001051 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001052 struct kmod_list **list);
1053
1054/* re-entrant */
1055static int __kmod_module_fill_softdep(struct kmod_module *mod,
1056 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001057{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001058 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001059 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001060
1061 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001062 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001063 ERR(mod->ctx, "could not get softdep: %s\n",
1064 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001065 goto fail;
1066 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001067
Lucas De Marchib1a51252012-01-29 01:49:09 -02001068 kmod_list_foreach(l, pre) {
1069 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001070 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001071 if (err < 0)
1072 goto fail;
1073 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001074
Lucas De Marchib1a51252012-01-29 01:49:09 -02001075 l = kmod_list_append(*list, kmod_module_ref(mod));
1076 if (l == NULL) {
1077 kmod_module_unref(mod);
1078 err = -ENOMEM;
1079 goto fail;
1080 }
1081 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001082 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001083
Lucas De Marchib1a51252012-01-29 01:49:09 -02001084 kmod_list_foreach(l, post) {
1085 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001086 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001087 if (err < 0)
1088 goto fail;
1089 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001090
Lucas De Marchib1a51252012-01-29 01:49:09 -02001091fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001092 kmod_module_unref_list(pre);
1093 kmod_module_unref_list(post);
1094
1095 return err;
1096}
1097
Lucas De Marchib1a51252012-01-29 01:49:09 -02001098/* re-entrant */
1099static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001100 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001101 struct kmod_list **list)
1102{
1103 struct kmod_list *dep, *l;
1104 int err = 0;
1105
1106 if (mod->visited) {
1107 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1108 mod->name);
1109 return 0;
1110 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001111 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001112
1113 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001114 kmod_list_foreach(l, dep) {
1115 struct kmod_module *m = l->data;
1116 err = __kmod_module_fill_softdep(m, list);
1117 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001118 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001119 }
1120
Lucas De Marchi89e92482012-01-29 02:35:46 -02001121 if (ignorecmd) {
1122 l = kmod_list_append(*list, kmod_module_ref(mod));
1123 if (l == NULL) {
1124 kmod_module_unref(mod);
1125 err = -ENOMEM;
1126 goto finish;
1127 }
1128 *list = l;
1129 mod->ignorecmd = true;
1130 } else
1131 err = __kmod_module_fill_softdep(mod, list);
1132
Lucas De Marchib1a51252012-01-29 01:49:09 -02001133finish:
1134 kmod_module_unref_list(dep);
1135 return err;
1136}
1137
1138static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001139 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001140 struct kmod_list **list)
1141{
1142 int err;
1143
1144 assert(mod != NULL);
1145 assert(list != NULL && *list == NULL);
1146
1147 /*
1148 * Make sure we don't get screwed by previous calls to this function
1149 */
1150 kmod_set_modules_visited(mod->ctx, false);
1151
Lucas De Marchi89e92482012-01-29 02:35:46 -02001152 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001153 if (err < 0) {
1154 kmod_module_unref_list(*list);
1155 *list = NULL;
1156 }
1157
1158 return err;
1159}
1160
Lucas De Marchiddbda022011-12-27 11:40:10 -02001161/**
1162 * kmod_module_probe_insert_module:
1163 * @mod: kmod module
1164 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +08001165 * behavior of this function, valid flags are
1166 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1167 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1168 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1169 * commands and softdeps configured in the system;
1170 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1171 * live in kernel or not;
1172 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1173 * associated callback function;
1174 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1175 * and the module is already live in kernel, the function will fail if this
1176 * flag is specified;
1177 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1178 * filter to this module and its dependencies. If any of the dependencies (or
1179 * the module) is blacklisted, the probe will fail, unless the blacklisted
1180 * module is already live in kernel;
1181 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1182 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1183 * alias and is blacklisted.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001184 * @extra_options: module's options to pass to Linux Kernel. It applies only
1185 * to @mod, not to its dependencies.
1186 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001187 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001188 * @print_action: function to call with the action being taken (install or
1189 * insmod). It's useful for tools like modprobe when running with verbose
1190 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001191 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001192 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001193 * install commands and applying blacklist.
1194 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001195 * If @run_install is NULL, this function will fork and exec by calling
1196 * system(3). Don't pass a NULL argument in @run_install if your binary is
1197 * setuid/setgid (see warning in system(3)). If you need control over the
1198 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001199 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001200 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1201 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001202 */
1203KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1204 unsigned int flags, const char *extra_options,
1205 int (*run_install)(struct kmod_module *m,
1206 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001207 const void *data,
1208 void (*print_action)(struct kmod_module *m,
1209 bool install,
1210 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001211{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001212 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001213 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001214 int err;
1215
1216 if (mod == NULL)
1217 return -ENOENT;
1218
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001219 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1220 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001221 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001222 return -EEXIST;
1223 else
1224 return 0;
1225 }
1226
Lucas De Marchi68820172012-08-17 09:38:05 -03001227 /*
1228 * Ugly assignement + check. We need to check if we were told to check
1229 * blacklist and also return the reason why we failed.
1230 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1231 * module is an alias, so we also need to check it
1232 */
1233 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1234 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1235 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001236 if (module_is_blacklisted(mod))
1237 return err;
1238 }
1239
Lucas De Marchi89e92482012-01-29 02:35:46 -02001240 err = kmod_module_get_probe_list(mod,
1241 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001242 if (err < 0)
1243 return err;
1244
1245 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1246 struct kmod_list *filtered = NULL;
1247
Dave Reisnerd80b1032012-02-24 10:05:11 -05001248 err = kmod_module_apply_filter(mod->ctx,
1249 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001250 if (err < 0)
1251 return err;
1252
1253 kmod_module_unref_list(list);
1254 if (filtered == NULL)
1255 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1256
1257 list = filtered;
1258 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001259
1260 cb.run_install = run_install;
1261 cb.data = (void *) data;
1262
Lucas De Marchib1a51252012-01-29 01:49:09 -02001263 kmod_list_foreach(l, list) {
1264 struct kmod_module *m = l->data;
1265 const char *moptions = kmod_module_get_options(m);
1266 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001267 char *options;
1268
1269 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1270 && module_is_inkernel(m)) {
1271 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1272 m->name);
1273 err = -EEXIST;
1274 goto finish_module;
1275 }
1276
1277 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001278 m == mod ? extra_options : NULL);
1279
Lucas De Marchi89e92482012-01-29 02:35:46 -02001280 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001281 if (print_action != NULL)
1282 print_action(m, true, options ?: "");
1283
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001284 if (!(flags & KMOD_PROBE_DRY_RUN))
1285 err = module_do_install_commands(m, options,
1286 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001287 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001288 if (print_action != NULL)
1289 print_action(m, false, options ?: "");
1290
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001291 if (!(flags & KMOD_PROBE_DRY_RUN))
1292 err = kmod_module_insert_module(m, flags,
1293 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001294 }
1295
1296 free(options);
1297
Lucas De Marchiabd55572012-02-19 04:20:30 -02001298finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001299 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001300 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001301 * already loaded and the module being loaded is not a softdep
1302 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001303 *
1304 * We need to check here because of race conditions. We
1305 * checked first if module was already loaded but it may have
1306 * been loaded between the check and the moment we try to
1307 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001308 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001309 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001310 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001311 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001312
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001313 if (err == -EEXIST)
1314 err = 0;
1315 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001316 break;
1317 }
1318
1319 kmod_module_unref_list(list);
1320 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001321}
1322
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001323/**
1324 * kmod_module_get_options:
1325 * @mod: kmod module
1326 *
1327 * Get options of this kmod module. Options come from the configuration file
1328 * and are cached in @mod. The first call to this function will search for
1329 * this module in configuration and subsequent calls return the cached string.
1330 *
1331 * Returns: a string with all the options separated by spaces. This string is
1332 * owned by @mod, do not free it.
1333 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001334KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1335{
1336 if (mod == NULL)
1337 return NULL;
1338
1339 if (!mod->init.options) {
1340 /* lazy init */
1341 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001342 const struct kmod_list *l;
1343 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001344 char *opts = NULL;
1345 size_t optslen = 0;
1346
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001347 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001348
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001349 kmod_list_foreach(l, config->options) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001350 const char *modname = kmod_option_get_modname(l);
1351 const char *str;
1352 size_t len;
1353 void *tmp;
1354
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001355 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1356 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1357 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001358 continue;
1359
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001360 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 -02001361 str = kmod_option_get_options(l);
1362 len = strlen(str);
1363 if (len < 1)
1364 continue;
1365
1366 tmp = realloc(opts, optslen + len + 2);
1367 if (tmp == NULL) {
1368 free(opts);
1369 goto failed;
1370 }
1371
1372 opts = tmp;
1373
1374 if (optslen > 0) {
1375 opts[optslen] = ' ';
1376 optslen++;
1377 }
1378
1379 memcpy(opts + optslen, str, len);
1380 optslen += len;
1381 opts[optslen] = '\0';
1382 }
1383
1384 m->init.options = true;
1385 m->options = opts;
1386 }
1387
1388 return mod->options;
1389
1390failed:
1391 ERR(mod->ctx, "out of memory\n");
1392 return NULL;
1393}
1394
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001395/**
1396 * kmod_module_get_install_commands:
1397 * @mod: kmod module
1398 *
1399 * Get install commands for this kmod module. Install commands come from the
1400 * configuration file and are cached in @mod. The first call to this function
1401 * will search for this module in configuration and subsequent calls return
1402 * the cached string. The install commands are returned as they were in the
1403 * configuration, concatenated by ';'. No other processing is made in this
1404 * string.
1405 *
1406 * Returns: a string with all install commands separated by semicolons. This
1407 * string is owned by @mod, do not free it.
1408 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001409KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1410{
1411 if (mod == NULL)
1412 return NULL;
1413
1414 if (!mod->init.install_commands) {
1415 /* lazy init */
1416 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001417 const struct kmod_list *l;
1418 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001419
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001420 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001421
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001422 kmod_list_foreach(l, config->install_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001423 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001424
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001425 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001426 continue;
1427
Lucas De Marchi60f67602011-12-16 03:33:26 -02001428 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001429
Lucas De Marchi60f67602011-12-16 03:33:26 -02001430 /*
1431 * find only the first command, as modprobe from
1432 * module-init-tools does
1433 */
1434 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001435 }
1436
1437 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001438 }
1439
1440 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001441}
1442
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001443void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1444{
1445 mod->init.install_commands = true;
1446 mod->install_commands = cmd;
1447}
1448
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001449static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1450{
1451 struct kmod_list *ret = NULL;
1452 unsigned i;
1453
1454 for (i = 0; i < count; i++) {
1455 const char *depname = array[i];
1456 struct kmod_list *lst = NULL;
1457 int err;
1458
1459 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1460 if (err < 0) {
1461 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1462 continue;
1463 } else if (lst != NULL)
1464 ret = kmod_list_append_list(ret, lst);
1465 }
1466 return ret;
1467}
1468
1469/**
1470 * kmod_module_get_softdeps:
1471 * @mod: kmod module
1472 * @pre: where to save the list of preceding soft dependencies.
1473 * @post: where to save the list of post soft dependencies.
1474 *
1475 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001476 * from configuration file and are not cached in @mod because it may include
1477 * dependency cycles that would make we leak kmod_module. Any call
1478 * to this function will search for this module in configuration, allocate a
1479 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001480 *
1481 * Both @pre and @post are newly created list of kmod_module and
1482 * should be unreferenced with kmod_module_unref_list().
1483 *
1484 * Returns: 0 on success or < 0 otherwise.
1485 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001486KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1487 struct kmod_list **pre,
1488 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001489{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001490 const struct kmod_list *l;
1491 const struct kmod_config *config;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001492
1493 if (mod == NULL || pre == NULL || post == NULL)
1494 return -ENOENT;
1495
1496 assert(*pre == NULL);
1497 assert(*post == NULL);
1498
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001499 config = kmod_get_config(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001500
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001501 kmod_list_foreach(l, config->softdeps) {
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001502 const char *modname = kmod_softdep_get_name(l);
1503 const char * const *array;
1504 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001505
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001506 if (fnmatch(modname, mod->name, 0) != 0)
1507 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001508
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001509 array = kmod_softdep_get_pre(l, &count);
1510 *pre = lookup_softdep(mod->ctx, array, count);
1511 array = kmod_softdep_get_post(l, &count);
1512 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001513
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001514 /*
1515 * find only the first command, as modprobe from
1516 * module-init-tools does
1517 */
1518 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001519 }
1520
1521 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001522}
1523
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001524/**
1525 * kmod_module_get_remove_commands:
1526 * @mod: kmod module
1527 *
1528 * Get remove commands for this kmod module. Remove commands come from the
1529 * configuration file and are cached in @mod. The first call to this function
1530 * will search for this module in configuration and subsequent calls return
1531 * the cached string. The remove commands are returned as they were in the
1532 * configuration, concatenated by ';'. No other processing is made in this
1533 * string.
1534 *
1535 * Returns: a string with all remove commands separated by semicolons. This
1536 * string is owned by @mod, do not free it.
1537 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001538KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1539{
1540 if (mod == NULL)
1541 return NULL;
1542
1543 if (!mod->init.remove_commands) {
1544 /* lazy init */
1545 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001546 const struct kmod_list *l;
1547 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001548
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001549 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001550
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001551 kmod_list_foreach(l, config->remove_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001552 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001553
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001554 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001555 continue;
1556
Lucas De Marchi60f67602011-12-16 03:33:26 -02001557 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001558
Lucas De Marchi60f67602011-12-16 03:33:26 -02001559 /*
1560 * find only the first command, as modprobe from
1561 * module-init-tools does
1562 */
1563 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001564 }
1565
1566 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001567 }
1568
1569 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001570}
1571
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001572void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1573{
1574 mod->init.remove_commands = true;
1575 mod->remove_commands = cmd;
1576}
1577
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001578/**
1579 * SECTION:libkmod-loaded
1580 * @short_description: currently loaded modules
1581 *
1582 * Information about currently loaded modules, as reported by Linux kernel.
1583 * These information are not cached by libkmod and are always read from /sys
1584 * and /proc/modules.
1585 */
1586
1587/**
1588 * kmod_module_new_from_loaded:
1589 * @ctx: kmod library context
1590 * @list: where to save the list of loaded modules
1591 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001592 * Create a new list of kmod modules with all modules currently loaded in
1593 * kernel. It uses /proc/modules to get the names of loaded modules and to
1594 * create kmod modules by calling kmod_module_new_from_name() in each of them.
Chengwei Yang491c4902013-05-04 17:07:02 +08001595 * They are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001596 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001597 * The initial refcount is 1, and needs to be decremented to release the
1598 * resources of the kmod_module. The returned @list must be released by
1599 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1600 * kmod_modules created, they are all released upon @ctx destruction too. Do
1601 * not unref @ctx before all the desired operations with the returned list are
1602 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001603 *
1604 * Returns: 0 on success or < 0 on error.
1605 */
1606KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1607 struct kmod_list **list)
1608{
1609 struct kmod_list *l = NULL;
1610 FILE *fp;
1611 char line[4096];
1612
1613 if (ctx == NULL || list == NULL)
1614 return -ENOENT;
1615
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001616 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001617 if (fp == NULL) {
1618 int err = -errno;
1619 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1620 return err;
1621 }
1622
1623 while (fgets(line, sizeof(line), fp)) {
1624 struct kmod_module *m;
1625 struct kmod_list *node;
1626 int err;
1627 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1628
1629 err = kmod_module_new_from_name(ctx, name, &m);
1630 if (err < 0) {
1631 ERR(ctx, "could not get module from name '%s': %s\n",
1632 name, strerror(-err));
1633 continue;
1634 }
1635
1636 node = kmod_list_append(l, m);
1637 if (node)
1638 l = node;
1639 else {
1640 ERR(ctx, "out of memory\n");
1641 kmod_module_unref(m);
1642 }
1643 }
1644
1645 fclose(fp);
1646 *list = l;
1647
1648 return 0;
1649}
1650
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001651/**
1652 * kmod_module_initstate_str:
1653 * @state: the state as returned by kmod_module_get_initstate()
1654 *
1655 * Translate a initstate to a string.
1656 *
1657 * Returns: the string associated to the @state. This string is statically
1658 * allocated, do not free it.
1659 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001660KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1661{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001662 switch (state) {
1663 case KMOD_MODULE_BUILTIN:
1664 return "builtin";
1665 case KMOD_MODULE_LIVE:
1666 return "live";
1667 case KMOD_MODULE_COMING:
1668 return "coming";
1669 case KMOD_MODULE_GOING:
1670 return "going";
1671 default:
1672 return NULL;
1673 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001674}
1675
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001676/**
1677 * kmod_module_get_initstate:
1678 * @mod: kmod module
1679 *
1680 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1681 * /sys filesystem.
1682 *
Chengwei Yangd7152f62013-05-04 17:07:03 +08001683 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1684 * KMOD_MODULE_BUILTIN: module is builtin;
1685 * KMOD_MODULE_LIVE: module is live in kernel;
1686 * KMOD_MODULE_COMING: module is being loaded;
1687 * KMOD_MODULE_GOING: module is being unloaded.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001688 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001689KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1690{
1691 char path[PATH_MAX], buf[32];
1692 int fd, err, pathlen;
1693
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001694 if (mod == NULL)
1695 return -ENOENT;
1696
Lucas De Marchi38052742012-02-16 20:43:16 -02001697 if (mod->builtin)
1698 return KMOD_MODULE_BUILTIN;
1699
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001700 pathlen = snprintf(path, sizeof(path),
1701 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001702 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001703 if (fd < 0) {
1704 err = -errno;
1705
Lucas De Marchiddbda022011-12-27 11:40:10 -02001706 DBG(mod->ctx, "could not open '%s': %s\n",
1707 path, strerror(-err));
1708
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001709 if (pathlen > (int)sizeof("/initstate") - 1) {
1710 struct stat st;
1711 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1712 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1713 return KMOD_MODULE_BUILTIN;
1714 }
1715
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001716 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001717 path, strerror(-err));
1718 return err;
1719 }
1720
1721 err = read_str_safe(fd, buf, sizeof(buf));
1722 close(fd);
1723 if (err < 0) {
1724 ERR(mod->ctx, "could not read from '%s': %s\n",
1725 path, strerror(-err));
1726 return err;
1727 }
1728
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001729 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001730 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001731 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001732 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001733 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001734 return KMOD_MODULE_GOING;
1735
1736 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1737 return -EINVAL;
1738}
1739
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001740/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001741 * kmod_module_get_size:
1742 * @mod: kmod module
1743 *
Dave Reisner486f9012012-06-28 09:09:48 -04001744 * Get the size of this kmod module as returned by Linux kernel. If supported,
1745 * the size is read from the coresize attribute in /sys/module. For older
1746 * kernels, this falls back on /proc/modules and searches for the specified
1747 * module to get its size.
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001748 *
1749 * Returns: the size of this kmod module.
1750 */
1751KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1752{
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001753 FILE *fp;
1754 char line[4096];
1755 int lineno = 0;
1756 long size = -ENOENT;
Dave Reisner486f9012012-06-28 09:09:48 -04001757 int dfd, cfd;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001758
1759 if (mod == NULL)
1760 return -ENOENT;
1761
Dave Reisner486f9012012-06-28 09:09:48 -04001762 /* try to open the module dir in /sys. If this fails, don't
1763 * bother trying to find the size as we know the module isn't
1764 * loaded.
1765 */
1766 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
1767 dfd = open(line, O_RDONLY);
1768 if (dfd < 0)
1769 return -errno;
1770
1771 /* available as of linux 3.3.x */
1772 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1773 if (cfd >= 0) {
1774 if (read_str_long(cfd, &size, 10) < 0)
1775 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1776 close(cfd);
1777 goto done;
1778 }
1779
1780 /* fall back on parsing /proc/modules */
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001781 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001782 if (fp == NULL) {
1783 int err = -errno;
1784 ERR(mod->ctx,
1785 "could not open /proc/modules: %s\n", strerror(errno));
1786 return err;
1787 }
1788
1789 while (fgets(line, sizeof(line), fp)) {
1790 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1791 long value;
1792
1793 lineno++;
1794 if (tok == NULL || !streq(tok, mod->name))
1795 continue;
1796
1797 tok = strtok_r(NULL, " \t", &saveptr);
1798 if (tok == NULL) {
1799 ERR(mod->ctx,
1800 "invalid line format at /proc/modules:%d\n", lineno);
1801 break;
1802 }
1803
1804 value = strtol(tok, &endptr, 10);
1805 if (endptr == tok || *endptr != '\0') {
1806 ERR(mod->ctx,
1807 "invalid line format at /proc/modules:%d\n", lineno);
1808 break;
1809 }
1810
1811 size = value;
1812 break;
1813 }
1814 fclose(fp);
Dave Reisner486f9012012-06-28 09:09:48 -04001815
1816done:
1817 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001818 return size;
1819}
1820
1821/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001822 * kmod_module_get_refcnt:
1823 * @mod: kmod module
1824 *
1825 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1826 * /sys filesystem.
1827 *
1828 * Returns: 0 on success or < 0 on failure.
1829 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001830KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1831{
1832 char path[PATH_MAX];
1833 long refcnt;
1834 int fd, err;
1835
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001836 if (mod == NULL)
1837 return -ENOENT;
1838
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001839 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001840 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001841 if (fd < 0) {
1842 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001843 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001844 path, strerror(errno));
1845 return err;
1846 }
1847
1848 err = read_str_long(fd, &refcnt, 10);
1849 close(fd);
1850 if (err < 0) {
1851 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1852 path, strerror(-err));
1853 return err;
1854 }
1855
1856 return (int)refcnt;
1857}
1858
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001859/**
1860 * kmod_module_get_holders:
1861 * @mod: kmod module
1862 *
1863 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1864 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1865 *
1866 * Returns: a new list of kmod modules on success or NULL on failure.
1867 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001868KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1869{
1870 char dname[PATH_MAX];
1871 struct kmod_list *list = NULL;
1872 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001873
Lucas De Marchib9a7da32013-04-23 20:47:28 -03001874 if (mod == NULL || mod->ctx == NULL)
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001875 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001876
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001877 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1878
1879 d = opendir(dname);
1880 if (d == NULL) {
1881 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001882 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001883 return NULL;
1884 }
1885
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001886 for (;;) {
1887 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001888 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001889 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001890 int err;
1891
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001892 err = readdir_r(d, &de, &entp);
1893 if (err != 0) {
1894 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1895 mod->name, strerror(-err));
1896 goto fail;
1897 }
1898
1899 if (entp == NULL)
1900 break;
1901
1902 if (de.d_name[0] == '.') {
1903 if (de.d_name[1] == '\0' ||
1904 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001905 continue;
1906 }
1907
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001908 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001909 if (err < 0) {
1910 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001911 de.d_name, strerror(-err));
1912 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001913 }
1914
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001915 l = kmod_list_append(list, holder);
1916 if (l != NULL) {
1917 list = l;
1918 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001919 ERR(mod->ctx, "out of memory\n");
1920 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001921 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001922 }
1923 }
1924
1925 closedir(d);
1926 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001927
1928fail:
1929 closedir(d);
1930 kmod_module_unref_list(list);
1931 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001932}
1933
1934struct kmod_module_section {
1935 unsigned long address;
1936 char name[];
1937};
1938
1939static void kmod_module_section_free(struct kmod_module_section *section)
1940{
1941 free(section);
1942}
1943
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001944/**
1945 * kmod_module_get_sections:
1946 * @mod: kmod module
1947 *
1948 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1949 * structure contained in this list is internal to libkmod and their fields
1950 * can be obtained by calling kmod_module_section_get_name() and
1951 * kmod_module_section_get_address().
1952 *
1953 * After use, free the @list by calling kmod_module_section_free_list().
1954 *
1955 * Returns: a new list of kmod module sections on success or NULL on failure.
1956 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001957KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1958{
1959 char dname[PATH_MAX];
1960 struct kmod_list *list = NULL;
1961 DIR *d;
1962 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001963
1964 if (mod == NULL)
1965 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001966
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001967 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1968
1969 d = opendir(dname);
1970 if (d == NULL) {
1971 ERR(mod->ctx, "could not open '%s': %s\n",
1972 dname, strerror(errno));
1973 return NULL;
1974 }
1975
1976 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001977
1978 for (;;) {
1979 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001980 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001981 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001982 unsigned long address;
1983 size_t namesz;
1984 int fd, err;
1985
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001986 err = readdir_r(d, &de, &entp);
1987 if (err != 0) {
1988 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1989 mod->name, strerror(-err));
1990 goto fail;
1991 }
1992
1993 if (de.d_name[0] == '.') {
1994 if (de.d_name[1] == '\0' ||
1995 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001996 continue;
1997 }
1998
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001999 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002000 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002001 ERR(mod->ctx, "could not open '%s/%s': %m\n",
2002 dname, de.d_name);
2003 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002004 }
2005
2006 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002007 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002008 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002009 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
2010 dname, de.d_name);
2011 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002012 }
2013
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002014 namesz = strlen(de.d_name) + 1;
2015 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002016
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002017 if (section == NULL) {
2018 ERR(mod->ctx, "out of memory\n");
2019 goto fail;
2020 }
2021
2022 section->address = address;
2023 memcpy(section->name, de.d_name, namesz);
2024
2025 l = kmod_list_append(list, section);
2026 if (l != NULL) {
2027 list = l;
2028 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002029 ERR(mod->ctx, "out of memory\n");
2030 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002031 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002032 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002033 }
2034
2035 closedir(d);
2036 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002037
2038fail:
2039 closedir(d);
2040 kmod_module_unref_list(list);
2041 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002042}
2043
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002044/**
2045 * kmod_module_section_get_module_name:
2046 * @entry: a list entry representing a kmod module section
2047 *
2048 * Get the name of a kmod module section.
2049 *
2050 * After use, free the @list by calling kmod_module_section_free_list().
2051 *
2052 * Returns: the name of this kmod module section on success or NULL on
2053 * failure. The string is owned by the section, do not free it.
2054 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002055KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2056{
2057 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002058
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002059 if (entry == NULL)
2060 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002061
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002062 section = entry->data;
2063 return section->name;
2064}
2065
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002066/**
2067 * kmod_module_section_get_address:
2068 * @entry: a list entry representing a kmod module section
2069 *
2070 * Get the address of a kmod module section.
2071 *
2072 * After use, free the @list by calling kmod_module_section_free_list().
2073 *
2074 * Returns: the address of this kmod module section on success or ULONG_MAX
2075 * on failure.
2076 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002077KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2078{
2079 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002080
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002081 if (entry == NULL)
2082 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002083
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002084 section = entry->data;
2085 return section->address;
2086}
2087
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002088/**
2089 * kmod_module_section_free_list:
2090 * @list: kmod module section list
2091 *
2092 * Release the resources taken by @list
2093 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002094KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2095{
2096 while (list) {
2097 kmod_module_section_free(list->data);
2098 list = kmod_list_remove(list);
2099 }
2100}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002101
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002102static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2103{
2104 if (mod->file == NULL) {
2105 const char *path = kmod_module_get_path(mod);
2106
2107 if (path == NULL) {
2108 errno = ENOENT;
2109 return NULL;
2110 }
2111
2112 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2113 path);
2114 if (mod->file == NULL)
2115 return NULL;
2116 }
2117
2118 return kmod_file_get_elf(mod->file);
2119}
2120
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002121struct kmod_module_info {
2122 char *key;
2123 char value[];
2124};
2125
2126static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2127{
2128 struct kmod_module_info *info;
2129
2130 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2131 if (info == NULL)
2132 return NULL;
2133
2134 info->key = (char *)info + sizeof(struct kmod_module_info)
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002135 + valuelen + 1;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002136 memcpy(info->key, key, keylen);
2137 info->key[keylen] = '\0';
2138 memcpy(info->value, value, valuelen);
2139 info->value[valuelen] = '\0';
2140 return info;
2141}
2142
2143static void kmod_module_info_free(struct kmod_module_info *info)
2144{
2145 free(info);
2146}
2147
Michal Marekf64458c2013-01-16 10:18:17 +01002148static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2149{
2150 struct kmod_module_info *info;
2151 struct kmod_list *n;
2152
2153 info = kmod_module_info_new(key, keylen, value, valuelen);
Michal Marek63339342013-01-16 17:51:57 +01002154 if (info == NULL)
Michal Marekf64458c2013-01-16 10:18:17 +01002155 return NULL;
Michal Marekf64458c2013-01-16 10:18:17 +01002156 n = kmod_list_append(*list, info);
Michal Marek63339342013-01-16 17:51:57 +01002157 if (n != NULL)
2158 *list = n;
2159 else
Michal Marekf64458c2013-01-16 10:18:17 +01002160 kmod_module_info_free(info);
Michal Marekf64458c2013-01-16 10:18:17 +01002161 return n;
2162}
2163
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002164/**
2165 * kmod_module_get_info:
2166 * @mod: kmod module
2167 * @list: where to return list of module information. Use
2168 * kmod_module_info_get_key() and
2169 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002170 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002171 *
2172 * Get a list of entries in ELF section ".modinfo", these contain
2173 * alias, license, depends, vermagic and other keys with respective
Michal Marek8fe16812013-01-16 09:52:01 +01002174 * values. If the module is signed (CONFIG_MODULE_SIG), information
2175 * about the module signature is included as well: signer,
2176 * sig_key and sig_hashalgo.
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002177 *
2178 * After use, free the @list by calling kmod_module_info_free_list().
2179 *
2180 * Returns: 0 on success or < 0 otherwise.
2181 */
2182KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2183{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002184 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002185 char **strings;
Michal Marekf64458c2013-01-16 10:18:17 +01002186 int i, count, ret = -ENOMEM;
Michal Marek8fe16812013-01-16 09:52:01 +01002187 struct kmod_signature_info sig_info;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002188
2189 if (mod == NULL || list == NULL)
2190 return -ENOENT;
2191
2192 assert(*list == NULL);
2193
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002194 elf = kmod_module_get_elf(mod);
2195 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002196 return -errno;
2197
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002198 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002199 if (count < 0)
2200 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002201
2202 for (i = 0; i < count; i++) {
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002203 struct kmod_list *n;
2204 const char *key, *value;
2205 size_t keylen, valuelen;
2206
2207 key = strings[i];
2208 value = strchr(key, '=');
2209 if (value == NULL) {
2210 keylen = strlen(key);
2211 valuelen = 0;
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002212 value = key;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002213 } else {
2214 keylen = value - key;
2215 value++;
2216 valuelen = strlen(value);
2217 }
2218
Michal Marekf64458c2013-01-16 10:18:17 +01002219 n = kmod_module_info_append(list, key, keylen, value, valuelen);
2220 if (n == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002221 goto list_error;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002222 }
Michal Marek8fe16812013-01-16 09:52:01 +01002223
2224 if (kmod_module_signature_info(mod->file, &sig_info)) {
2225 struct kmod_list *n;
2226 char *key_hex;
2227
2228 n = kmod_module_info_append(list, "signer", strlen("signer"),
2229 sig_info.signer, sig_info.signer_len);
2230 if (n == NULL)
2231 goto list_error;
2232 count++;
2233
2234 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2235 key_hex = malloc(sig_info.key_id_len * 3);
2236 if (key_hex == NULL)
2237 goto list_error;
2238 for (i = 0; i < (int)sig_info.key_id_len; i++) {
2239 sprintf(key_hex + i * 3, "%02X",
2240 (unsigned char)sig_info.key_id[i]);
2241 if (i < (int)sig_info.key_id_len - 1)
2242 key_hex[i * 3 + 2] = ':';
2243 }
2244 n = kmod_module_info_append(list, "sig_key", strlen("sig_key"),
2245 key_hex, sig_info.key_id_len * 3 - 1);
2246 free(key_hex);
2247 if (n == NULL)
2248 goto list_error;
2249 count++;
2250
2251 n = kmod_module_info_append(list,
2252 "sig_hashalgo", strlen("sig_hashalgo"),
2253 sig_info.hash_algo, strlen(sig_info.hash_algo));
2254 if (n == NULL)
2255 goto list_error;
2256 count++;
2257
2258 /*
2259 * Omit sig_info.id_type and sig_info.algo for now, as these
2260 * are currently constant.
2261 */
2262 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002263 ret = count;
2264
2265list_error:
Michal Marek63339342013-01-16 17:51:57 +01002266 if (ret < 0) {
2267 kmod_module_info_free_list(*list);
2268 *list = NULL;
2269 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002270 free(strings);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002271 return ret;
2272}
2273
2274/**
2275 * kmod_module_info_get_key:
2276 * @entry: a list entry representing a kmod module info
2277 *
2278 * Get the key of a kmod module info.
2279 *
2280 * Returns: the key of this kmod module info on success or NULL on
2281 * failure. The string is owned by the info, do not free it.
2282 */
2283KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2284{
2285 struct kmod_module_info *info;
2286
2287 if (entry == NULL)
2288 return NULL;
2289
2290 info = entry->data;
2291 return info->key;
2292}
2293
2294/**
2295 * kmod_module_info_get_value:
2296 * @entry: a list entry representing a kmod module info
2297 *
2298 * Get the value of a kmod module info.
2299 *
2300 * Returns: the value of this kmod module info on success or NULL on
2301 * failure. The string is owned by the info, do not free it.
2302 */
2303KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2304{
2305 struct kmod_module_info *info;
2306
2307 if (entry == NULL)
2308 return NULL;
2309
2310 info = entry->data;
2311 return info->value;
2312}
2313
2314/**
2315 * kmod_module_info_free_list:
2316 * @list: kmod module info list
2317 *
2318 * Release the resources taken by @list
2319 */
2320KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2321{
2322 while (list) {
2323 kmod_module_info_free(list->data);
2324 list = kmod_list_remove(list);
2325 }
2326}
2327
2328struct kmod_module_version {
2329 uint64_t crc;
2330 char symbol[];
2331};
2332
2333static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2334{
2335 struct kmod_module_version *mv;
2336 size_t symbollen = strlen(symbol) + 1;
2337
2338 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2339 if (mv == NULL)
2340 return NULL;
2341
2342 mv->crc = crc;
2343 memcpy(mv->symbol, symbol, symbollen);
2344 return mv;
2345}
2346
2347static void kmod_module_version_free(struct kmod_module_version *version)
2348{
2349 free(version);
2350}
2351
2352/**
2353 * kmod_module_get_versions:
2354 * @mod: kmod module
2355 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002356 * kmod_module_version_get_symbol() and
2357 * kmod_module_version_get_crc(). Release this list with
2358 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002359 *
2360 * Get a list of entries in ELF section "__versions".
2361 *
2362 * After use, free the @list by calling kmod_module_versions_free_list().
2363 *
2364 * Returns: 0 on success or < 0 otherwise.
2365 */
2366KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2367{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002368 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002369 struct kmod_modversion *versions;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002370 int i, count, ret = 0;
2371
2372 if (mod == NULL || list == NULL)
2373 return -ENOENT;
2374
2375 assert(*list == NULL);
2376
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002377 elf = kmod_module_get_elf(mod);
2378 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002379 return -errno;
2380
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002381 count = kmod_elf_get_modversions(elf, &versions);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002382 if (count < 0)
2383 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002384
2385 for (i = 0; i < count; i++) {
2386 struct kmod_module_version *mv;
2387 struct kmod_list *n;
2388
2389 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2390 if (mv == NULL) {
2391 ret = -errno;
2392 kmod_module_versions_free_list(*list);
2393 *list = NULL;
2394 goto list_error;
2395 }
2396
2397 n = kmod_list_append(*list, mv);
2398 if (n != NULL)
2399 *list = n;
2400 else {
2401 kmod_module_version_free(mv);
2402 kmod_module_versions_free_list(*list);
2403 *list = NULL;
2404 ret = -ENOMEM;
2405 goto list_error;
2406 }
2407 }
2408 ret = count;
2409
2410list_error:
2411 free(versions);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002412 return ret;
2413}
2414
2415/**
Chengwei Yang491c4902013-05-04 17:07:02 +08002416 * kmod_module_version_get_symbol:
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002417 * @entry: a list entry representing a kmod module versions
2418 *
2419 * Get the symbol of a kmod module versions.
2420 *
2421 * Returns: the symbol of this kmod module versions on success or NULL
2422 * on failure. The string is owned by the versions, do not free it.
2423 */
2424KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2425{
2426 struct kmod_module_version *version;
2427
2428 if (entry == NULL)
2429 return NULL;
2430
2431 version = entry->data;
2432 return version->symbol;
2433}
2434
2435/**
2436 * kmod_module_version_get_crc:
2437 * @entry: a list entry representing a kmod module version
2438 *
2439 * Get the crc of a kmod module version.
2440 *
2441 * Returns: the crc of this kmod module version on success or NULL on
2442 * failure. The string is owned by the version, do not free it.
2443 */
2444KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2445{
2446 struct kmod_module_version *version;
2447
2448 if (entry == NULL)
2449 return 0;
2450
2451 version = entry->data;
2452 return version->crc;
2453}
2454
2455/**
2456 * kmod_module_versions_free_list:
2457 * @list: kmod module versions list
2458 *
2459 * Release the resources taken by @list
2460 */
2461KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2462{
2463 while (list) {
2464 kmod_module_version_free(list->data);
2465 list = kmod_list_remove(list);
2466 }
2467}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002468
2469struct kmod_module_symbol {
2470 uint64_t crc;
2471 char symbol[];
2472};
2473
2474static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2475{
2476 struct kmod_module_symbol *mv;
2477 size_t symbollen = strlen(symbol) + 1;
2478
2479 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2480 if (mv == NULL)
2481 return NULL;
2482
2483 mv->crc = crc;
2484 memcpy(mv->symbol, symbol, symbollen);
2485 return mv;
2486}
2487
2488static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2489{
2490 free(symbol);
2491}
2492
2493/**
2494 * kmod_module_get_symbols:
2495 * @mod: kmod module
2496 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002497 * kmod_module_symbol_get_symbol() and
2498 * kmod_module_symbol_get_crc(). Release this list with
2499 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002500 *
2501 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2502 *
2503 * After use, free the @list by calling kmod_module_symbols_free_list().
2504 *
2505 * Returns: 0 on success or < 0 otherwise.
2506 */
2507KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2508{
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002509 struct kmod_elf *elf;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002510 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002511 int i, count, ret = 0;
2512
2513 if (mod == NULL || list == NULL)
2514 return -ENOENT;
2515
2516 assert(*list == NULL);
2517
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002518 elf = kmod_module_get_elf(mod);
2519 if (elf == NULL)
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002520 return -errno;
2521
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002522 count = kmod_elf_get_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002523 if (count < 0)
2524 return count;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002525
2526 for (i = 0; i < count; i++) {
2527 struct kmod_module_symbol *mv;
2528 struct kmod_list *n;
2529
2530 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2531 if (mv == NULL) {
2532 ret = -errno;
2533 kmod_module_symbols_free_list(*list);
2534 *list = NULL;
2535 goto list_error;
2536 }
2537
2538 n = kmod_list_append(*list, mv);
2539 if (n != NULL)
2540 *list = n;
2541 else {
2542 kmod_module_symbol_free(mv);
2543 kmod_module_symbols_free_list(*list);
2544 *list = NULL;
2545 ret = -ENOMEM;
2546 goto list_error;
2547 }
2548 }
2549 ret = count;
2550
2551list_error:
2552 free(symbols);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002553 return ret;
2554}
2555
2556/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002557 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002558 * @entry: a list entry representing a kmod module symbols
2559 *
2560 * Get the symbol of a kmod module symbols.
2561 *
2562 * Returns: the symbol of this kmod module symbols on success or NULL
2563 * on failure. The string is owned by the symbols, do not free it.
2564 */
2565KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2566{
2567 struct kmod_module_symbol *symbol;
2568
2569 if (entry == NULL)
2570 return NULL;
2571
2572 symbol = entry->data;
2573 return symbol->symbol;
2574}
2575
2576/**
2577 * kmod_module_symbol_get_crc:
2578 * @entry: a list entry representing a kmod module symbol
2579 *
2580 * Get the crc of a kmod module symbol.
2581 *
2582 * Returns: the crc of this kmod module symbol on success or NULL on
2583 * failure. The string is owned by the symbol, do not free it.
2584 */
2585KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2586{
2587 struct kmod_module_symbol *symbol;
2588
2589 if (entry == NULL)
2590 return 0;
2591
2592 symbol = entry->data;
2593 return symbol->crc;
2594}
2595
2596/**
2597 * kmod_module_symbols_free_list:
2598 * @list: kmod module symbols list
2599 *
2600 * Release the resources taken by @list
2601 */
2602KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2603{
2604 while (list) {
2605 kmod_module_symbol_free(list->data);
2606 list = kmod_list_remove(list);
2607 }
2608}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002609
2610struct kmod_module_dependency_symbol {
2611 uint64_t crc;
2612 uint8_t bind;
2613 char symbol[];
2614};
2615
2616static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2617{
2618 struct kmod_module_dependency_symbol *mv;
2619 size_t symbollen = strlen(symbol) + 1;
2620
2621 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2622 if (mv == NULL)
2623 return NULL;
2624
2625 mv->crc = crc;
2626 mv->bind = bind;
2627 memcpy(mv->symbol, symbol, symbollen);
2628 return mv;
2629}
2630
2631static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2632{
2633 free(dependency_symbol);
2634}
2635
2636/**
2637 * kmod_module_get_dependency_symbols:
2638 * @mod: kmod module
2639 * @list: where to return list of module dependency_symbols. Use
2640 * kmod_module_dependency_symbol_get_symbol() and
2641 * kmod_module_dependency_symbol_get_crc(). Release this list with
2642 * kmod_module_dependency_symbols_free_list()
2643 *
2644 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2645 *
2646 * After use, free the @list by calling
2647 * kmod_module_dependency_symbols_free_list().
2648 *
2649 * Returns: 0 on success or < 0 otherwise.
2650 */
2651KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2652{
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002653 struct kmod_elf *elf;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002654 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002655 int i, count, ret = 0;
2656
2657 if (mod == NULL || list == NULL)
2658 return -ENOENT;
2659
2660 assert(*list == NULL);
2661
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002662 elf = kmod_module_get_elf(mod);
2663 if (elf == NULL)
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002664 return -errno;
2665
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002666 count = kmod_elf_get_dependency_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002667 if (count < 0)
2668 return count;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002669
2670 for (i = 0; i < count; i++) {
2671 struct kmod_module_dependency_symbol *mv;
2672 struct kmod_list *n;
2673
2674 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2675 symbols[i].bind,
2676 symbols[i].symbol);
2677 if (mv == NULL) {
2678 ret = -errno;
2679 kmod_module_dependency_symbols_free_list(*list);
2680 *list = NULL;
2681 goto list_error;
2682 }
2683
2684 n = kmod_list_append(*list, mv);
2685 if (n != NULL)
2686 *list = n;
2687 else {
2688 kmod_module_dependency_symbol_free(mv);
2689 kmod_module_dependency_symbols_free_list(*list);
2690 *list = NULL;
2691 ret = -ENOMEM;
2692 goto list_error;
2693 }
2694 }
2695 ret = count;
2696
2697list_error:
2698 free(symbols);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002699 return ret;
2700}
2701
2702/**
2703 * kmod_module_dependency_symbol_get_symbol:
2704 * @entry: a list entry representing a kmod module dependency_symbols
2705 *
2706 * Get the dependency symbol of a kmod module
2707 *
2708 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2709 * on failure. The string is owned by the dependency_symbols, do not free it.
2710 */
2711KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2712{
2713 struct kmod_module_dependency_symbol *dependency_symbol;
2714
2715 if (entry == NULL)
2716 return NULL;
2717
2718 dependency_symbol = entry->data;
2719 return dependency_symbol->symbol;
2720}
2721
2722/**
2723 * kmod_module_dependency_symbol_get_crc:
2724 * @entry: a list entry representing a kmod module dependency_symbol
2725 *
2726 * Get the crc of a kmod module dependency_symbol.
2727 *
2728 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2729 * failure. The string is owned by the dependency_symbol, do not free it.
2730 */
2731KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2732{
2733 struct kmod_module_dependency_symbol *dependency_symbol;
2734
2735 if (entry == NULL)
2736 return 0;
2737
2738 dependency_symbol = entry->data;
2739 return dependency_symbol->crc;
2740}
2741
2742/**
2743 * kmod_module_dependency_symbol_get_bind:
2744 * @entry: a list entry representing a kmod module dependency_symbol
2745 *
2746 * Get the bind type of a kmod module dependency_symbol.
2747 *
2748 * Returns: the bind of this kmod module dependency_symbol on success
2749 * or < 0 on failure.
2750 */
2751KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2752{
2753 struct kmod_module_dependency_symbol *dependency_symbol;
2754
2755 if (entry == NULL)
2756 return 0;
2757
2758 dependency_symbol = entry->data;
2759 return dependency_symbol->bind;
2760}
2761
2762/**
2763 * kmod_module_dependency_symbols_free_list:
2764 * @list: kmod module dependency_symbols list
2765 *
2766 * Release the resources taken by @list
2767 */
2768KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2769{
2770 while (list) {
2771 kmod_module_dependency_symbol_free(list->data);
2772 list = kmod_list_remove(list);
2773 }
2774}