blob: 6b793cd6a56395a18b48690becae3fe9f14fbf6e [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 *
573 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
574 * returns the passed @mod with its refcount decremented.
575 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200576KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
577{
578 for (; list != NULL; list = kmod_list_remove(list))
579 kmod_module_unref(list->data);
580
581 return 0;
582}
583
Lucas De Marchi0d467432011-12-31 11:15:52 -0200584/**
585 * kmod_module_get_filtered_blacklist:
586 * @ctx: kmod library context
587 * @input: list of kmod_module to be filtered with blacklist
588 * @output: where to save the new list
589 *
Kay Sievers471a7d02012-04-14 20:47:47 +0200590 * This function should not be used. Use kmod_module_apply_filter instead.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500591 *
Lucas De Marchi0d467432011-12-31 11:15:52 -0200592 * Given a list @input, this function filter it out with config's blacklist
Dave Reisnerd80b1032012-02-24 10:05:11 -0500593 * and save it in @output.
Lucas De Marchi0d467432011-12-31 11:15:52 -0200594 *
595 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
596 * list.
597 */
598KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
599 const struct kmod_list *input,
600 struct kmod_list **output)
601{
Dave Reisnerd80b1032012-02-24 10:05:11 -0500602 return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
Lucas De Marchi0d467432011-12-31 11:15:52 -0200603}
604
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200605static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
606{
607 if (!mod->init.dep) {
608 /* lazy init */
609 char *line = kmod_search_moddep(mod->ctx, mod->name);
610
611 if (line == NULL)
612 return NULL;
613
614 kmod_module_parse_depline((struct kmod_module *)mod, line);
615 free(line);
616
617 if (!mod->init.dep)
618 return NULL;
619 }
620
621 return mod->dep;
622}
623
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200624/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200625 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200626 * @mod: kmod module
627 *
628 * Search the modules.dep index to find the dependencies of the given @mod.
629 * The result is cached in @mod, so subsequent calls to this function will
630 * return the already searched list of modules.
631 *
632 * Returns: NULL on failure or if there are any dependencies. Otherwise it
633 * returns a list of kmod modules that can be released by calling
634 * kmod_module_unref_list().
635 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200636KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200637{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200638 struct kmod_list *l, *l_new, *list_new = NULL;
639
640 if (mod == NULL)
641 return NULL;
642
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200643 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200644
645 kmod_list_foreach(l, mod->dep) {
646 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
647 if (l_new == NULL) {
648 kmod_module_unref(l->data);
649 goto fail;
650 }
651
652 list_new = l_new;
653 }
654
655 return list_new;
656
657fail:
658 ERR(mod->ctx, "out of memory\n");
659 kmod_module_unref_list(list_new);
660 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200661}
662
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200663/**
664 * kmod_module_get_module:
665 * @entry: an entry in a list of kmod modules.
666 *
667 * Get the kmod module of this @entry in the list, increasing its refcount.
668 * After it's used, unref it. Since the refcount is incremented upon return,
669 * you still have to call kmod_module_unref_list() to release the list of kmod
670 * modules.
671 *
672 * Returns: NULL on failure or the kmod_module contained in this list entry
673 * with its refcount incremented.
674 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200675KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200676{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200677 if (entry == NULL)
678 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200679
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200680 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200681}
682
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200683/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200684 * kmod_module_get_name:
685 * @mod: kmod module
686 *
687 * Get the name of this kmod module. Name is always available, independently
688 * if it was created by kmod_module_new_from_name() or another function and
689 * it's always normalized (dashes are replaced with underscores).
690 *
691 * Returns: the name of this kmod module.
692 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200693KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200694{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200695 if (mod == NULL)
696 return NULL;
697
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200698 return mod->name;
699}
700
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200701/**
702 * kmod_module_get_path:
703 * @mod: kmod module
704 *
705 * Get the path of this kmod module. If this kmod module was not created by
706 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200707 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200708 *
709 * Returns: the path of this kmod module or NULL if such information is not
710 * available.
711 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200712KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200713{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200714 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200715
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200716 if (mod == NULL)
717 return NULL;
718
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200719 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200720
Lucas De Marchie005fac2011-12-08 10:42:34 -0200721 if (mod->path != NULL)
722 return mod->path;
723 if (mod->init.dep)
724 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200725
Lucas De Marchie005fac2011-12-08 10:42:34 -0200726 /* lazy init */
727 line = kmod_search_moddep(mod->ctx, mod->name);
728 if (line == NULL)
729 return NULL;
730
731 kmod_module_parse_depline((struct kmod_module *) mod, line);
732 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200733
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200734 return mod->path;
735}
736
737
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200738extern long delete_module(const char *name, unsigned int flags);
739
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200740/**
741 * kmod_module_remove_module:
742 * @mod: kmod module
743 * @flags: flags to pass to Linux kernel when removing the module
744 *
745 * Remove a module from Linux kernel.
746 *
747 * Returns: 0 on success or < 0 on failure.
748 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200749KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
750 unsigned int flags)
751{
752 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200753
754 if (mod == NULL)
755 return -ENOENT;
756
757 /* Filter out other flags */
758 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
759
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200760 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200761 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200762 err = -errno;
763 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200764 }
765
Lucas De Marchiba998b92012-01-11 00:08:14 -0200766 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200767}
768
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200769extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200770
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200771/**
772 * kmod_module_insert_module:
773 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200774 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
775 * behavior of this function.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200776 * @options: module's options to pass to Linux Kernel.
777 *
778 * Insert a module in Linux kernel. It opens the file pointed by @mod,
779 * mmap'ing it and passing to kernel.
780 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200781 * Returns: 0 on success or < 0 on failure. If module is already loaded it
782 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200783 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200784KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200785 unsigned int flags,
786 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200787{
788 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200789 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200790 off_t size;
791 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200792 struct kmod_elf *elf = NULL;
793 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200794 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200795
796 if (mod == NULL)
797 return -ENOENT;
798
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200799 path = kmod_module_get_path(mod);
800 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500801 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200802 return -ENOSYS;
803 }
804
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200805 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200806 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200807 err = -errno;
808 return err;
809 }
810
Kees Cook144d1822013-02-18 12:02:32 -0800811 if (kmod_file_get_direct(file)) {
812 unsigned int kernel_flags = 0;
813
Kees Cook144d1822013-02-18 12:02:32 -0800814 if (flags & KMOD_INSERT_FORCE_VERMAGIC)
815 kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
816 if (flags & KMOD_INSERT_FORCE_MODVERSION)
817 kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
Kees Cook144d1822013-02-18 12:02:32 -0800818
819 err = finit_module(kmod_file_get_fd(file), args, kernel_flags);
820 if (err == 0 || errno != ENOSYS)
821 goto init_finished;
822 }
823
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200824 size = kmod_file_get_size(file);
825 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200826
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200827 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
828 elf = kmod_elf_new(mem, size);
829 if (elf == NULL) {
830 err = -errno;
831 goto elf_failed;
832 }
833
834 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
835 err = kmod_elf_strip_section(elf, "__versions");
836 if (err < 0)
837 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
838 }
839
840 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
841 err = kmod_elf_strip_vermagic(elf);
842 if (err < 0)
843 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
844 }
845
846 mem = kmod_elf_get_memory(elf);
847 }
848
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200849 err = init_module(mem, size, args);
Kees Cook144d1822013-02-18 12:02:32 -0800850init_finished:
Lucas De Marchibbf59322011-12-30 14:13:33 -0200851 if (err < 0) {
852 err = -errno;
853 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
854 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200855
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200856 if (elf != NULL)
857 kmod_elf_unref(elf);
858elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200859 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200860
861 return err;
862}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200863
Lucas De Marchiddbda022011-12-27 11:40:10 -0200864static bool module_is_blacklisted(struct kmod_module *mod)
865{
866 struct kmod_ctx *ctx = mod->ctx;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300867 const struct kmod_config *config = kmod_get_config(ctx);
868 const struct kmod_list *bl = config->blacklists;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200869 const struct kmod_list *l;
870
871 kmod_list_foreach(l, bl) {
872 const char *modname = kmod_blacklist_get_modname(l);
873
874 if (streq(modname, mod->name))
875 return true;
876 }
877
878 return false;
879}
880
Dave Reisnerd80b1032012-02-24 10:05:11 -0500881/**
882 * kmod_module_apply_filter
883 * @ctx: kmod library context
884 * @filter_type: bitmask to filter modules on
885 * @input: list of kmod_module to be filtered
886 * @output: where to save the new list
887 *
888 * Given a list @input, this function filter it out by the filter mask
889 * and save it in @output.
890 *
891 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
892 * list.
893 */
894KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
895 enum kmod_filter filter_type,
896 const struct kmod_list *input,
897 struct kmod_list **output)
898{
899 const struct kmod_list *li;
900
901 if (ctx == NULL || output == NULL)
902 return -ENOENT;
903
904 *output = NULL;
905 if (input == NULL)
906 return 0;
907
908 kmod_list_foreach(li, input) {
909 struct kmod_module *mod = li->data;
910 struct kmod_list *node;
911
912 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
913 module_is_blacklisted(mod))
914 continue;
915
Dave Reisnerbdda7e12012-02-24 23:02:06 -0500916 if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin)
Dave Reisnerd80b1032012-02-24 10:05:11 -0500917 continue;
918
919 node = kmod_list_append(*output, mod);
920 if (node == NULL)
921 goto fail;
922
923 *output = node;
924 kmod_module_ref(mod);
925 }
926
927 return 0;
928
929fail:
930 kmod_module_unref_list(*output);
931 *output = NULL;
932 return -ENOMEM;
933}
934
Lucas De Marchiddbda022011-12-27 11:40:10 -0200935static int command_do(struct kmod_module *mod, const char *type,
936 const char *cmd)
937{
938 const char *modname = kmod_module_get_name(mod);
939 int err;
940
941 DBG(mod->ctx, "%s %s\n", type, cmd);
942
943 setenv("MODPROBE_MODULE", modname, 1);
944 err = system(cmd);
945 unsetenv("MODPROBE_MODULE");
946
947 if (err == -1 || WEXITSTATUS(err)) {
948 ERR(mod->ctx, "Error running %s command for %s\n",
949 type, modname);
950 if (err != -1)
951 err = -WEXITSTATUS(err);
952 }
953
954 return err;
955}
956
Lucas De Marchib1a51252012-01-29 01:49:09 -0200957struct probe_insert_cb {
958 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
959 void *data;
960};
961
Lucas De Marchiddbda022011-12-27 11:40:10 -0200962static int module_do_install_commands(struct kmod_module *mod,
963 const char *options,
964 struct probe_insert_cb *cb)
965{
966 const char *command = kmod_module_get_install_commands(mod);
967 char *p, *cmd;
968 int err;
969 size_t cmdlen, options_len, varlen;
970
971 assert(command);
972
973 if (options == NULL)
974 options = "";
975
976 options_len = strlen(options);
977 cmdlen = strlen(command);
978 varlen = sizeof("$CMDLINE_OPTS") - 1;
979
980 cmd = memdup(command, cmdlen + 1);
981 if (cmd == NULL)
982 return -ENOMEM;
983
984 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
985 size_t prefixlen = p - cmd;
986 size_t suffixlen = cmdlen - prefixlen - varlen;
987 size_t slen = cmdlen - varlen + options_len;
988 char *suffix = p + varlen;
989 char *s = malloc(slen + 1);
990 if (s == NULL) {
991 free(cmd);
992 return -ENOMEM;
993 }
994 memcpy(s, cmd, p - cmd);
995 memcpy(s + prefixlen, options, options_len);
996 memcpy(s + prefixlen + options_len, suffix, suffixlen);
997 s[slen] = '\0';
998
999 free(cmd);
1000 cmd = s;
1001 cmdlen = slen;
1002 }
1003
1004 if (cb->run_install != NULL)
1005 err = cb->run_install(mod, cmd, cb->data);
1006 else
1007 err = command_do(mod, "install", cmd);
1008
1009 free(cmd);
1010
1011 return err;
1012}
1013
Lucas De Marchiddbda022011-12-27 11:40:10 -02001014static char *module_options_concat(const char *opt, const char *xopt)
1015{
1016 // TODO: we might need to check if xopt overrides options on opt
1017 size_t optlen = opt == NULL ? 0 : strlen(opt);
1018 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1019 char *r;
1020
1021 if (optlen == 0 && xoptlen == 0)
1022 return NULL;
1023
1024 r = malloc(optlen + xoptlen + 2);
1025
1026 if (opt != NULL) {
1027 memcpy(r, opt, optlen);
1028 r[optlen] = ' ';
1029 optlen++;
1030 }
1031
1032 if (xopt != NULL)
1033 memcpy(r + optlen, xopt, xoptlen);
1034
1035 r[optlen + xoptlen] = '\0';
1036
1037 return r;
1038}
1039
Lucas De Marchib1a51252012-01-29 01:49:09 -02001040static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001041 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001042 struct kmod_list **list);
1043
1044/* re-entrant */
1045static int __kmod_module_fill_softdep(struct kmod_module *mod,
1046 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001047{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001048 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001049 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001050
1051 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001052 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001053 ERR(mod->ctx, "could not get softdep: %s\n",
1054 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001055 goto fail;
1056 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001057
Lucas De Marchib1a51252012-01-29 01:49:09 -02001058 kmod_list_foreach(l, pre) {
1059 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001060 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001061 if (err < 0)
1062 goto fail;
1063 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001064
Lucas De Marchib1a51252012-01-29 01:49:09 -02001065 l = kmod_list_append(*list, kmod_module_ref(mod));
1066 if (l == NULL) {
1067 kmod_module_unref(mod);
1068 err = -ENOMEM;
1069 goto fail;
1070 }
1071 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001072 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001073
Lucas De Marchib1a51252012-01-29 01:49:09 -02001074 kmod_list_foreach(l, post) {
1075 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001076 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001077 if (err < 0)
1078 goto fail;
1079 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001080
Lucas De Marchib1a51252012-01-29 01:49:09 -02001081fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001082 kmod_module_unref_list(pre);
1083 kmod_module_unref_list(post);
1084
1085 return err;
1086}
1087
Lucas De Marchib1a51252012-01-29 01:49:09 -02001088/* re-entrant */
1089static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001090 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001091 struct kmod_list **list)
1092{
1093 struct kmod_list *dep, *l;
1094 int err = 0;
1095
1096 if (mod->visited) {
1097 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1098 mod->name);
1099 return 0;
1100 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001101 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001102
1103 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001104 kmod_list_foreach(l, dep) {
1105 struct kmod_module *m = l->data;
1106 err = __kmod_module_fill_softdep(m, list);
1107 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001108 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001109 }
1110
Lucas De Marchi89e92482012-01-29 02:35:46 -02001111 if (ignorecmd) {
1112 l = kmod_list_append(*list, kmod_module_ref(mod));
1113 if (l == NULL) {
1114 kmod_module_unref(mod);
1115 err = -ENOMEM;
1116 goto finish;
1117 }
1118 *list = l;
1119 mod->ignorecmd = true;
1120 } else
1121 err = __kmod_module_fill_softdep(mod, list);
1122
Lucas De Marchib1a51252012-01-29 01:49:09 -02001123finish:
1124 kmod_module_unref_list(dep);
1125 return err;
1126}
1127
1128static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001129 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001130 struct kmod_list **list)
1131{
1132 int err;
1133
1134 assert(mod != NULL);
1135 assert(list != NULL && *list == NULL);
1136
1137 /*
1138 * Make sure we don't get screwed by previous calls to this function
1139 */
1140 kmod_set_modules_visited(mod->ctx, false);
1141
Lucas De Marchi89e92482012-01-29 02:35:46 -02001142 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001143 if (err < 0) {
1144 kmod_module_unref_list(*list);
1145 *list = NULL;
1146 }
1147
1148 return err;
1149}
1150
Lucas De Marchiddbda022011-12-27 11:40:10 -02001151/**
1152 * kmod_module_probe_insert_module:
1153 * @mod: kmod module
1154 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1155 * behavior of this function.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001156 * @extra_options: module's options to pass to Linux Kernel. It applies only
1157 * to @mod, not to its dependencies.
1158 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001159 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001160 * @print_action: function to call with the action being taken (install or
1161 * insmod). It's useful for tools like modprobe when running with verbose
1162 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001163 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001164 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001165 * install commands and applying blacklist.
1166 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001167 * If @run_install is NULL, this function will fork and exec by calling
1168 * system(3). Don't pass a NULL argument in @run_install if your binary is
1169 * setuid/setgid (see warning in system(3)). If you need control over the
1170 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001171 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001172 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1173 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001174 */
1175KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1176 unsigned int flags, const char *extra_options,
1177 int (*run_install)(struct kmod_module *m,
1178 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001179 const void *data,
1180 void (*print_action)(struct kmod_module *m,
1181 bool install,
1182 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001183{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001184 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001185 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001186 int err;
1187
1188 if (mod == NULL)
1189 return -ENOENT;
1190
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001191 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1192 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001193 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001194 return -EEXIST;
1195 else
1196 return 0;
1197 }
1198
Lucas De Marchi68820172012-08-17 09:38:05 -03001199 /*
1200 * Ugly assignement + check. We need to check if we were told to check
1201 * blacklist and also return the reason why we failed.
1202 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1203 * module is an alias, so we also need to check it
1204 */
1205 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1206 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1207 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001208 if (module_is_blacklisted(mod))
1209 return err;
1210 }
1211
Lucas De Marchi89e92482012-01-29 02:35:46 -02001212 err = kmod_module_get_probe_list(mod,
1213 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001214 if (err < 0)
1215 return err;
1216
1217 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1218 struct kmod_list *filtered = NULL;
1219
Dave Reisnerd80b1032012-02-24 10:05:11 -05001220 err = kmod_module_apply_filter(mod->ctx,
1221 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001222 if (err < 0)
1223 return err;
1224
1225 kmod_module_unref_list(list);
1226 if (filtered == NULL)
1227 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1228
1229 list = filtered;
1230 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001231
1232 cb.run_install = run_install;
1233 cb.data = (void *) data;
1234
Lucas De Marchib1a51252012-01-29 01:49:09 -02001235 kmod_list_foreach(l, list) {
1236 struct kmod_module *m = l->data;
1237 const char *moptions = kmod_module_get_options(m);
1238 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001239 char *options;
1240
1241 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1242 && module_is_inkernel(m)) {
1243 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1244 m->name);
1245 err = -EEXIST;
1246 goto finish_module;
1247 }
1248
1249 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001250 m == mod ? extra_options : NULL);
1251
Lucas De Marchi89e92482012-01-29 02:35:46 -02001252 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001253 if (print_action != NULL)
1254 print_action(m, true, options ?: "");
1255
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001256 if (!(flags & KMOD_PROBE_DRY_RUN))
1257 err = module_do_install_commands(m, options,
1258 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001259 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001260 if (print_action != NULL)
1261 print_action(m, false, options ?: "");
1262
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001263 if (!(flags & KMOD_PROBE_DRY_RUN))
1264 err = kmod_module_insert_module(m, flags,
1265 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001266 }
1267
1268 free(options);
1269
Lucas De Marchiabd55572012-02-19 04:20:30 -02001270finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001271 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001272 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001273 * already loaded and the module being loaded is not a softdep
1274 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001275 *
1276 * We need to check here because of race conditions. We
1277 * checked first if module was already loaded but it may have
1278 * been loaded between the check and the moment we try to
1279 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001280 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001281 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001282 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001283 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001284
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001285 if (err == -EEXIST)
1286 err = 0;
1287 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001288 break;
1289 }
1290
1291 kmod_module_unref_list(list);
1292 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001293}
1294
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001295/**
1296 * kmod_module_get_options:
1297 * @mod: kmod module
1298 *
1299 * Get options of this kmod module. Options come from the configuration file
1300 * and are cached in @mod. The first call to this function will search for
1301 * this module in configuration and subsequent calls return the cached string.
1302 *
1303 * Returns: a string with all the options separated by spaces. This string is
1304 * owned by @mod, do not free it.
1305 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001306KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1307{
1308 if (mod == NULL)
1309 return NULL;
1310
1311 if (!mod->init.options) {
1312 /* lazy init */
1313 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001314 const struct kmod_list *l;
1315 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001316 char *opts = NULL;
1317 size_t optslen = 0;
1318
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001319 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001320
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001321 kmod_list_foreach(l, config->options) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001322 const char *modname = kmod_option_get_modname(l);
1323 const char *str;
1324 size_t len;
1325 void *tmp;
1326
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001327 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1328 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1329 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001330 continue;
1331
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001332 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 -02001333 str = kmod_option_get_options(l);
1334 len = strlen(str);
1335 if (len < 1)
1336 continue;
1337
1338 tmp = realloc(opts, optslen + len + 2);
1339 if (tmp == NULL) {
1340 free(opts);
1341 goto failed;
1342 }
1343
1344 opts = tmp;
1345
1346 if (optslen > 0) {
1347 opts[optslen] = ' ';
1348 optslen++;
1349 }
1350
1351 memcpy(opts + optslen, str, len);
1352 optslen += len;
1353 opts[optslen] = '\0';
1354 }
1355
1356 m->init.options = true;
1357 m->options = opts;
1358 }
1359
1360 return mod->options;
1361
1362failed:
1363 ERR(mod->ctx, "out of memory\n");
1364 return NULL;
1365}
1366
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001367/**
1368 * kmod_module_get_install_commands:
1369 * @mod: kmod module
1370 *
1371 * Get install commands for this kmod module. Install commands come from the
1372 * configuration file and are cached in @mod. The first call to this function
1373 * will search for this module in configuration and subsequent calls return
1374 * the cached string. The install commands are returned as they were in the
1375 * configuration, concatenated by ';'. No other processing is made in this
1376 * string.
1377 *
1378 * Returns: a string with all install commands separated by semicolons. This
1379 * string is owned by @mod, do not free it.
1380 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001381KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1382{
1383 if (mod == NULL)
1384 return NULL;
1385
1386 if (!mod->init.install_commands) {
1387 /* lazy init */
1388 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001389 const struct kmod_list *l;
1390 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001391
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001392 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001393
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001394 kmod_list_foreach(l, config->install_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001395 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001396
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001397 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001398 continue;
1399
Lucas De Marchi60f67602011-12-16 03:33:26 -02001400 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001401
Lucas De Marchi60f67602011-12-16 03:33:26 -02001402 /*
1403 * find only the first command, as modprobe from
1404 * module-init-tools does
1405 */
1406 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001407 }
1408
1409 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001410 }
1411
1412 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001413}
1414
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001415void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1416{
1417 mod->init.install_commands = true;
1418 mod->install_commands = cmd;
1419}
1420
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001421static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1422{
1423 struct kmod_list *ret = NULL;
1424 unsigned i;
1425
1426 for (i = 0; i < count; i++) {
1427 const char *depname = array[i];
1428 struct kmod_list *lst = NULL;
1429 int err;
1430
1431 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1432 if (err < 0) {
1433 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1434 continue;
1435 } else if (lst != NULL)
1436 ret = kmod_list_append_list(ret, lst);
1437 }
1438 return ret;
1439}
1440
1441/**
1442 * kmod_module_get_softdeps:
1443 * @mod: kmod module
1444 * @pre: where to save the list of preceding soft dependencies.
1445 * @post: where to save the list of post soft dependencies.
1446 *
1447 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001448 * from configuration file and are not cached in @mod because it may include
1449 * dependency cycles that would make we leak kmod_module. Any call
1450 * to this function will search for this module in configuration, allocate a
1451 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001452 *
1453 * Both @pre and @post are newly created list of kmod_module and
1454 * should be unreferenced with kmod_module_unref_list().
1455 *
1456 * Returns: 0 on success or < 0 otherwise.
1457 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001458KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1459 struct kmod_list **pre,
1460 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001461{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001462 const struct kmod_list *l;
1463 const struct kmod_config *config;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001464
1465 if (mod == NULL || pre == NULL || post == NULL)
1466 return -ENOENT;
1467
1468 assert(*pre == NULL);
1469 assert(*post == NULL);
1470
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001471 config = kmod_get_config(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001472
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001473 kmod_list_foreach(l, config->softdeps) {
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001474 const char *modname = kmod_softdep_get_name(l);
1475 const char * const *array;
1476 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001477
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001478 if (fnmatch(modname, mod->name, 0) != 0)
1479 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001480
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001481 array = kmod_softdep_get_pre(l, &count);
1482 *pre = lookup_softdep(mod->ctx, array, count);
1483 array = kmod_softdep_get_post(l, &count);
1484 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001485
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001486 /*
1487 * find only the first command, as modprobe from
1488 * module-init-tools does
1489 */
1490 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001491 }
1492
1493 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001494}
1495
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001496/**
1497 * kmod_module_get_remove_commands:
1498 * @mod: kmod module
1499 *
1500 * Get remove commands for this kmod module. Remove commands come from the
1501 * configuration file and are cached in @mod. The first call to this function
1502 * will search for this module in configuration and subsequent calls return
1503 * the cached string. The remove commands are returned as they were in the
1504 * configuration, concatenated by ';'. No other processing is made in this
1505 * string.
1506 *
1507 * Returns: a string with all remove commands separated by semicolons. This
1508 * string is owned by @mod, do not free it.
1509 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001510KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1511{
1512 if (mod == NULL)
1513 return NULL;
1514
1515 if (!mod->init.remove_commands) {
1516 /* lazy init */
1517 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001518 const struct kmod_list *l;
1519 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001520
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001521 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001522
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001523 kmod_list_foreach(l, config->remove_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001524 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001525
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001526 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001527 continue;
1528
Lucas De Marchi60f67602011-12-16 03:33:26 -02001529 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001530
Lucas De Marchi60f67602011-12-16 03:33:26 -02001531 /*
1532 * find only the first command, as modprobe from
1533 * module-init-tools does
1534 */
1535 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001536 }
1537
1538 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001539 }
1540
1541 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001542}
1543
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001544void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1545{
1546 mod->init.remove_commands = true;
1547 mod->remove_commands = cmd;
1548}
1549
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001550/**
1551 * SECTION:libkmod-loaded
1552 * @short_description: currently loaded modules
1553 *
1554 * Information about currently loaded modules, as reported by Linux kernel.
1555 * These information are not cached by libkmod and are always read from /sys
1556 * and /proc/modules.
1557 */
1558
1559/**
1560 * kmod_module_new_from_loaded:
1561 * @ctx: kmod library context
1562 * @list: where to save the list of loaded modules
1563 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001564 * Create a new list of kmod modules with all modules currently loaded in
1565 * kernel. It uses /proc/modules to get the names of loaded modules and to
1566 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1567 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001568 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001569 * The initial refcount is 1, and needs to be decremented to release the
1570 * resources of the kmod_module. The returned @list must be released by
1571 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1572 * kmod_modules created, they are all released upon @ctx destruction too. Do
1573 * not unref @ctx before all the desired operations with the returned list are
1574 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001575 *
1576 * Returns: 0 on success or < 0 on error.
1577 */
1578KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1579 struct kmod_list **list)
1580{
1581 struct kmod_list *l = NULL;
1582 FILE *fp;
1583 char line[4096];
1584
1585 if (ctx == NULL || list == NULL)
1586 return -ENOENT;
1587
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001588 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001589 if (fp == NULL) {
1590 int err = -errno;
1591 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1592 return err;
1593 }
1594
1595 while (fgets(line, sizeof(line), fp)) {
1596 struct kmod_module *m;
1597 struct kmod_list *node;
1598 int err;
1599 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1600
1601 err = kmod_module_new_from_name(ctx, name, &m);
1602 if (err < 0) {
1603 ERR(ctx, "could not get module from name '%s': %s\n",
1604 name, strerror(-err));
1605 continue;
1606 }
1607
1608 node = kmod_list_append(l, m);
1609 if (node)
1610 l = node;
1611 else {
1612 ERR(ctx, "out of memory\n");
1613 kmod_module_unref(m);
1614 }
1615 }
1616
1617 fclose(fp);
1618 *list = l;
1619
1620 return 0;
1621}
1622
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001623/**
1624 * kmod_module_initstate_str:
1625 * @state: the state as returned by kmod_module_get_initstate()
1626 *
1627 * Translate a initstate to a string.
1628 *
1629 * Returns: the string associated to the @state. This string is statically
1630 * allocated, do not free it.
1631 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001632KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1633{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001634 switch (state) {
1635 case KMOD_MODULE_BUILTIN:
1636 return "builtin";
1637 case KMOD_MODULE_LIVE:
1638 return "live";
1639 case KMOD_MODULE_COMING:
1640 return "coming";
1641 case KMOD_MODULE_GOING:
1642 return "going";
1643 default:
1644 return NULL;
1645 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001646}
1647
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001648/**
1649 * kmod_module_get_initstate:
1650 * @mod: kmod module
1651 *
1652 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1653 * /sys filesystem.
1654 *
1655 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1656 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001657KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1658{
1659 char path[PATH_MAX], buf[32];
1660 int fd, err, pathlen;
1661
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001662 if (mod == NULL)
1663 return -ENOENT;
1664
Lucas De Marchi38052742012-02-16 20:43:16 -02001665 if (mod->builtin)
1666 return KMOD_MODULE_BUILTIN;
1667
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001668 pathlen = snprintf(path, sizeof(path),
1669 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001670 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001671 if (fd < 0) {
1672 err = -errno;
1673
Lucas De Marchiddbda022011-12-27 11:40:10 -02001674 DBG(mod->ctx, "could not open '%s': %s\n",
1675 path, strerror(-err));
1676
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001677 if (pathlen > (int)sizeof("/initstate") - 1) {
1678 struct stat st;
1679 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1680 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1681 return KMOD_MODULE_BUILTIN;
1682 }
1683
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001684 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001685 path, strerror(-err));
1686 return err;
1687 }
1688
1689 err = read_str_safe(fd, buf, sizeof(buf));
1690 close(fd);
1691 if (err < 0) {
1692 ERR(mod->ctx, "could not read from '%s': %s\n",
1693 path, strerror(-err));
1694 return err;
1695 }
1696
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001697 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001698 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001699 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001700 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001701 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001702 return KMOD_MODULE_GOING;
1703
1704 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1705 return -EINVAL;
1706}
1707
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001708/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001709 * kmod_module_get_size:
1710 * @mod: kmod module
1711 *
Dave Reisner486f9012012-06-28 09:09:48 -04001712 * Get the size of this kmod module as returned by Linux kernel. If supported,
1713 * the size is read from the coresize attribute in /sys/module. For older
1714 * kernels, this falls back on /proc/modules and searches for the specified
1715 * module to get its size.
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001716 *
1717 * Returns: the size of this kmod module.
1718 */
1719KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1720{
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001721 FILE *fp;
1722 char line[4096];
1723 int lineno = 0;
1724 long size = -ENOENT;
Dave Reisner486f9012012-06-28 09:09:48 -04001725 int dfd, cfd;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001726
1727 if (mod == NULL)
1728 return -ENOENT;
1729
Dave Reisner486f9012012-06-28 09:09:48 -04001730 /* try to open the module dir in /sys. If this fails, don't
1731 * bother trying to find the size as we know the module isn't
1732 * loaded.
1733 */
1734 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
1735 dfd = open(line, O_RDONLY);
1736 if (dfd < 0)
1737 return -errno;
1738
1739 /* available as of linux 3.3.x */
1740 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1741 if (cfd >= 0) {
1742 if (read_str_long(cfd, &size, 10) < 0)
1743 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1744 close(cfd);
1745 goto done;
1746 }
1747
1748 /* fall back on parsing /proc/modules */
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001749 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001750 if (fp == NULL) {
1751 int err = -errno;
1752 ERR(mod->ctx,
1753 "could not open /proc/modules: %s\n", strerror(errno));
1754 return err;
1755 }
1756
1757 while (fgets(line, sizeof(line), fp)) {
1758 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1759 long value;
1760
1761 lineno++;
1762 if (tok == NULL || !streq(tok, mod->name))
1763 continue;
1764
1765 tok = strtok_r(NULL, " \t", &saveptr);
1766 if (tok == NULL) {
1767 ERR(mod->ctx,
1768 "invalid line format at /proc/modules:%d\n", lineno);
1769 break;
1770 }
1771
1772 value = strtol(tok, &endptr, 10);
1773 if (endptr == tok || *endptr != '\0') {
1774 ERR(mod->ctx,
1775 "invalid line format at /proc/modules:%d\n", lineno);
1776 break;
1777 }
1778
1779 size = value;
1780 break;
1781 }
1782 fclose(fp);
Dave Reisner486f9012012-06-28 09:09:48 -04001783
1784done:
1785 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001786 return size;
1787}
1788
1789/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001790 * kmod_module_get_refcnt:
1791 * @mod: kmod module
1792 *
1793 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1794 * /sys filesystem.
1795 *
1796 * Returns: 0 on success or < 0 on failure.
1797 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001798KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1799{
1800 char path[PATH_MAX];
1801 long refcnt;
1802 int fd, err;
1803
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001804 if (mod == NULL)
1805 return -ENOENT;
1806
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001807 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001808 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001809 if (fd < 0) {
1810 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001811 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001812 path, strerror(errno));
1813 return err;
1814 }
1815
1816 err = read_str_long(fd, &refcnt, 10);
1817 close(fd);
1818 if (err < 0) {
1819 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1820 path, strerror(-err));
1821 return err;
1822 }
1823
1824 return (int)refcnt;
1825}
1826
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001827/**
1828 * kmod_module_get_holders:
1829 * @mod: kmod module
1830 *
1831 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1832 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1833 *
1834 * Returns: a new list of kmod modules on success or NULL on failure.
1835 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001836KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1837{
1838 char dname[PATH_MAX];
1839 struct kmod_list *list = NULL;
1840 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001841
1842 if (mod == NULL)
1843 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001844
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001845 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1846
1847 d = opendir(dname);
1848 if (d == NULL) {
1849 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001850 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001851 return NULL;
1852 }
1853
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001854 for (;;) {
1855 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001856 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001857 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001858 int err;
1859
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001860 err = readdir_r(d, &de, &entp);
1861 if (err != 0) {
1862 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1863 mod->name, strerror(-err));
1864 goto fail;
1865 }
1866
1867 if (entp == NULL)
1868 break;
1869
1870 if (de.d_name[0] == '.') {
1871 if (de.d_name[1] == '\0' ||
1872 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001873 continue;
1874 }
1875
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001876 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001877 if (err < 0) {
1878 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001879 de.d_name, strerror(-err));
1880 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001881 }
1882
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001883 l = kmod_list_append(list, holder);
1884 if (l != NULL) {
1885 list = l;
1886 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001887 ERR(mod->ctx, "out of memory\n");
1888 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001889 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001890 }
1891 }
1892
1893 closedir(d);
1894 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001895
1896fail:
1897 closedir(d);
1898 kmod_module_unref_list(list);
1899 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001900}
1901
1902struct kmod_module_section {
1903 unsigned long address;
1904 char name[];
1905};
1906
1907static void kmod_module_section_free(struct kmod_module_section *section)
1908{
1909 free(section);
1910}
1911
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001912/**
1913 * kmod_module_get_sections:
1914 * @mod: kmod module
1915 *
1916 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1917 * structure contained in this list is internal to libkmod and their fields
1918 * can be obtained by calling kmod_module_section_get_name() and
1919 * kmod_module_section_get_address().
1920 *
1921 * After use, free the @list by calling kmod_module_section_free_list().
1922 *
1923 * Returns: a new list of kmod module sections on success or NULL on failure.
1924 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001925KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1926{
1927 char dname[PATH_MAX];
1928 struct kmod_list *list = NULL;
1929 DIR *d;
1930 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001931
1932 if (mod == NULL)
1933 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001934
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001935 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1936
1937 d = opendir(dname);
1938 if (d == NULL) {
1939 ERR(mod->ctx, "could not open '%s': %s\n",
1940 dname, strerror(errno));
1941 return NULL;
1942 }
1943
1944 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001945
1946 for (;;) {
1947 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001948 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001949 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001950 unsigned long address;
1951 size_t namesz;
1952 int fd, err;
1953
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001954 err = readdir_r(d, &de, &entp);
1955 if (err != 0) {
1956 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1957 mod->name, strerror(-err));
1958 goto fail;
1959 }
1960
1961 if (de.d_name[0] == '.') {
1962 if (de.d_name[1] == '\0' ||
1963 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001964 continue;
1965 }
1966
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001967 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001968 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001969 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1970 dname, de.d_name);
1971 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001972 }
1973
1974 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001975 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001976 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001977 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1978 dname, de.d_name);
1979 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001980 }
1981
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001982 namesz = strlen(de.d_name) + 1;
1983 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001984
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001985 if (section == NULL) {
1986 ERR(mod->ctx, "out of memory\n");
1987 goto fail;
1988 }
1989
1990 section->address = address;
1991 memcpy(section->name, de.d_name, namesz);
1992
1993 l = kmod_list_append(list, section);
1994 if (l != NULL) {
1995 list = l;
1996 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001997 ERR(mod->ctx, "out of memory\n");
1998 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001999 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002000 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002001 }
2002
2003 closedir(d);
2004 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002005
2006fail:
2007 closedir(d);
2008 kmod_module_unref_list(list);
2009 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002010}
2011
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002012/**
2013 * kmod_module_section_get_module_name:
2014 * @entry: a list entry representing a kmod module section
2015 *
2016 * Get the name of a kmod module section.
2017 *
2018 * After use, free the @list by calling kmod_module_section_free_list().
2019 *
2020 * Returns: the name of this kmod module section on success or NULL on
2021 * failure. The string is owned by the section, do not free it.
2022 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002023KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2024{
2025 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002026
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002027 if (entry == NULL)
2028 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002029
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002030 section = entry->data;
2031 return section->name;
2032}
2033
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002034/**
2035 * kmod_module_section_get_address:
2036 * @entry: a list entry representing a kmod module section
2037 *
2038 * Get the address of a kmod module section.
2039 *
2040 * After use, free the @list by calling kmod_module_section_free_list().
2041 *
2042 * Returns: the address of this kmod module section on success or ULONG_MAX
2043 * on failure.
2044 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002045KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2046{
2047 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002048
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002049 if (entry == NULL)
2050 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002051
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002052 section = entry->data;
2053 return section->address;
2054}
2055
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002056/**
2057 * kmod_module_section_free_list:
2058 * @list: kmod module section list
2059 *
2060 * Release the resources taken by @list
2061 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002062KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2063{
2064 while (list) {
2065 kmod_module_section_free(list->data);
2066 list = kmod_list_remove(list);
2067 }
2068}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002069
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002070static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2071{
2072 if (mod->file == NULL) {
2073 const char *path = kmod_module_get_path(mod);
2074
2075 if (path == NULL) {
2076 errno = ENOENT;
2077 return NULL;
2078 }
2079
2080 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2081 path);
2082 if (mod->file == NULL)
2083 return NULL;
2084 }
2085
2086 return kmod_file_get_elf(mod->file);
2087}
2088
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002089struct kmod_module_info {
2090 char *key;
2091 char value[];
2092};
2093
2094static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2095{
2096 struct kmod_module_info *info;
2097
2098 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2099 if (info == NULL)
2100 return NULL;
2101
2102 info->key = (char *)info + sizeof(struct kmod_module_info)
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002103 + valuelen + 1;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002104 memcpy(info->key, key, keylen);
2105 info->key[keylen] = '\0';
2106 memcpy(info->value, value, valuelen);
2107 info->value[valuelen] = '\0';
2108 return info;
2109}
2110
2111static void kmod_module_info_free(struct kmod_module_info *info)
2112{
2113 free(info);
2114}
2115
Michal Marekf64458c2013-01-16 10:18:17 +01002116static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2117{
2118 struct kmod_module_info *info;
2119 struct kmod_list *n;
2120
2121 info = kmod_module_info_new(key, keylen, value, valuelen);
Michal Marek63339342013-01-16 17:51:57 +01002122 if (info == NULL)
Michal Marekf64458c2013-01-16 10:18:17 +01002123 return NULL;
Michal Marekf64458c2013-01-16 10:18:17 +01002124 n = kmod_list_append(*list, info);
Michal Marek63339342013-01-16 17:51:57 +01002125 if (n != NULL)
2126 *list = n;
2127 else
Michal Marekf64458c2013-01-16 10:18:17 +01002128 kmod_module_info_free(info);
Michal Marekf64458c2013-01-16 10:18:17 +01002129 return n;
2130}
2131
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002132/**
2133 * kmod_module_get_info:
2134 * @mod: kmod module
2135 * @list: where to return list of module information. Use
2136 * kmod_module_info_get_key() and
2137 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002138 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002139 *
2140 * Get a list of entries in ELF section ".modinfo", these contain
2141 * alias, license, depends, vermagic and other keys with respective
Michal Marek8fe16812013-01-16 09:52:01 +01002142 * values. If the module is signed (CONFIG_MODULE_SIG), information
2143 * about the module signature is included as well: signer,
2144 * sig_key and sig_hashalgo.
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002145 *
2146 * After use, free the @list by calling kmod_module_info_free_list().
2147 *
2148 * Returns: 0 on success or < 0 otherwise.
2149 */
2150KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2151{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002152 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002153 char **strings;
Michal Marekf64458c2013-01-16 10:18:17 +01002154 int i, count, ret = -ENOMEM;
Michal Marek8fe16812013-01-16 09:52:01 +01002155 struct kmod_signature_info sig_info;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002156
2157 if (mod == NULL || list == NULL)
2158 return -ENOENT;
2159
2160 assert(*list == NULL);
2161
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002162 elf = kmod_module_get_elf(mod);
2163 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002164 return -errno;
2165
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002166 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002167 if (count < 0)
2168 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002169
2170 for (i = 0; i < count; i++) {
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002171 struct kmod_list *n;
2172 const char *key, *value;
2173 size_t keylen, valuelen;
2174
2175 key = strings[i];
2176 value = strchr(key, '=');
2177 if (value == NULL) {
2178 keylen = strlen(key);
2179 valuelen = 0;
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002180 value = key;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002181 } else {
2182 keylen = value - key;
2183 value++;
2184 valuelen = strlen(value);
2185 }
2186
Michal Marekf64458c2013-01-16 10:18:17 +01002187 n = kmod_module_info_append(list, key, keylen, value, valuelen);
2188 if (n == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002189 goto list_error;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002190 }
Michal Marek8fe16812013-01-16 09:52:01 +01002191
2192 if (kmod_module_signature_info(mod->file, &sig_info)) {
2193 struct kmod_list *n;
2194 char *key_hex;
2195
2196 n = kmod_module_info_append(list, "signer", strlen("signer"),
2197 sig_info.signer, sig_info.signer_len);
2198 if (n == NULL)
2199 goto list_error;
2200 count++;
2201
2202 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2203 key_hex = malloc(sig_info.key_id_len * 3);
2204 if (key_hex == NULL)
2205 goto list_error;
2206 for (i = 0; i < (int)sig_info.key_id_len; i++) {
2207 sprintf(key_hex + i * 3, "%02X",
2208 (unsigned char)sig_info.key_id[i]);
2209 if (i < (int)sig_info.key_id_len - 1)
2210 key_hex[i * 3 + 2] = ':';
2211 }
2212 n = kmod_module_info_append(list, "sig_key", strlen("sig_key"),
2213 key_hex, sig_info.key_id_len * 3 - 1);
2214 free(key_hex);
2215 if (n == NULL)
2216 goto list_error;
2217 count++;
2218
2219 n = kmod_module_info_append(list,
2220 "sig_hashalgo", strlen("sig_hashalgo"),
2221 sig_info.hash_algo, strlen(sig_info.hash_algo));
2222 if (n == NULL)
2223 goto list_error;
2224 count++;
2225
2226 /*
2227 * Omit sig_info.id_type and sig_info.algo for now, as these
2228 * are currently constant.
2229 */
2230 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002231 ret = count;
2232
2233list_error:
Michal Marek63339342013-01-16 17:51:57 +01002234 if (ret < 0) {
2235 kmod_module_info_free_list(*list);
2236 *list = NULL;
2237 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002238 free(strings);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002239 return ret;
2240}
2241
2242/**
2243 * kmod_module_info_get_key:
2244 * @entry: a list entry representing a kmod module info
2245 *
2246 * Get the key of a kmod module info.
2247 *
2248 * Returns: the key of this kmod module info on success or NULL on
2249 * failure. The string is owned by the info, do not free it.
2250 */
2251KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2252{
2253 struct kmod_module_info *info;
2254
2255 if (entry == NULL)
2256 return NULL;
2257
2258 info = entry->data;
2259 return info->key;
2260}
2261
2262/**
2263 * kmod_module_info_get_value:
2264 * @entry: a list entry representing a kmod module info
2265 *
2266 * Get the value of a kmod module info.
2267 *
2268 * Returns: the value of this kmod module info on success or NULL on
2269 * failure. The string is owned by the info, do not free it.
2270 */
2271KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2272{
2273 struct kmod_module_info *info;
2274
2275 if (entry == NULL)
2276 return NULL;
2277
2278 info = entry->data;
2279 return info->value;
2280}
2281
2282/**
2283 * kmod_module_info_free_list:
2284 * @list: kmod module info list
2285 *
2286 * Release the resources taken by @list
2287 */
2288KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2289{
2290 while (list) {
2291 kmod_module_info_free(list->data);
2292 list = kmod_list_remove(list);
2293 }
2294}
2295
2296struct kmod_module_version {
2297 uint64_t crc;
2298 char symbol[];
2299};
2300
2301static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2302{
2303 struct kmod_module_version *mv;
2304 size_t symbollen = strlen(symbol) + 1;
2305
2306 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2307 if (mv == NULL)
2308 return NULL;
2309
2310 mv->crc = crc;
2311 memcpy(mv->symbol, symbol, symbollen);
2312 return mv;
2313}
2314
2315static void kmod_module_version_free(struct kmod_module_version *version)
2316{
2317 free(version);
2318}
2319
2320/**
2321 * kmod_module_get_versions:
2322 * @mod: kmod module
2323 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002324 * kmod_module_version_get_symbol() and
2325 * kmod_module_version_get_crc(). Release this list with
2326 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002327 *
2328 * Get a list of entries in ELF section "__versions".
2329 *
2330 * After use, free the @list by calling kmod_module_versions_free_list().
2331 *
2332 * Returns: 0 on success or < 0 otherwise.
2333 */
2334KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2335{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002336 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002337 struct kmod_modversion *versions;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002338 int i, count, ret = 0;
2339
2340 if (mod == NULL || list == NULL)
2341 return -ENOENT;
2342
2343 assert(*list == NULL);
2344
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002345 elf = kmod_module_get_elf(mod);
2346 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002347 return -errno;
2348
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002349 count = kmod_elf_get_modversions(elf, &versions);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002350 if (count < 0)
2351 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002352
2353 for (i = 0; i < count; i++) {
2354 struct kmod_module_version *mv;
2355 struct kmod_list *n;
2356
2357 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2358 if (mv == NULL) {
2359 ret = -errno;
2360 kmod_module_versions_free_list(*list);
2361 *list = NULL;
2362 goto list_error;
2363 }
2364
2365 n = kmod_list_append(*list, mv);
2366 if (n != NULL)
2367 *list = n;
2368 else {
2369 kmod_module_version_free(mv);
2370 kmod_module_versions_free_list(*list);
2371 *list = NULL;
2372 ret = -ENOMEM;
2373 goto list_error;
2374 }
2375 }
2376 ret = count;
2377
2378list_error:
2379 free(versions);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002380 return ret;
2381}
2382
2383/**
2384 * kmod_module_versions_get_symbol:
2385 * @entry: a list entry representing a kmod module versions
2386 *
2387 * Get the symbol of a kmod module versions.
2388 *
2389 * Returns: the symbol of this kmod module versions on success or NULL
2390 * on failure. The string is owned by the versions, do not free it.
2391 */
2392KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2393{
2394 struct kmod_module_version *version;
2395
2396 if (entry == NULL)
2397 return NULL;
2398
2399 version = entry->data;
2400 return version->symbol;
2401}
2402
2403/**
2404 * kmod_module_version_get_crc:
2405 * @entry: a list entry representing a kmod module version
2406 *
2407 * Get the crc of a kmod module version.
2408 *
2409 * Returns: the crc of this kmod module version on success or NULL on
2410 * failure. The string is owned by the version, do not free it.
2411 */
2412KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2413{
2414 struct kmod_module_version *version;
2415
2416 if (entry == NULL)
2417 return 0;
2418
2419 version = entry->data;
2420 return version->crc;
2421}
2422
2423/**
2424 * kmod_module_versions_free_list:
2425 * @list: kmod module versions list
2426 *
2427 * Release the resources taken by @list
2428 */
2429KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2430{
2431 while (list) {
2432 kmod_module_version_free(list->data);
2433 list = kmod_list_remove(list);
2434 }
2435}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002436
2437struct kmod_module_symbol {
2438 uint64_t crc;
2439 char symbol[];
2440};
2441
2442static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2443{
2444 struct kmod_module_symbol *mv;
2445 size_t symbollen = strlen(symbol) + 1;
2446
2447 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2448 if (mv == NULL)
2449 return NULL;
2450
2451 mv->crc = crc;
2452 memcpy(mv->symbol, symbol, symbollen);
2453 return mv;
2454}
2455
2456static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2457{
2458 free(symbol);
2459}
2460
2461/**
2462 * kmod_module_get_symbols:
2463 * @mod: kmod module
2464 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002465 * kmod_module_symbol_get_symbol() and
2466 * kmod_module_symbol_get_crc(). Release this list with
2467 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002468 *
2469 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2470 *
2471 * After use, free the @list by calling kmod_module_symbols_free_list().
2472 *
2473 * Returns: 0 on success or < 0 otherwise.
2474 */
2475KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2476{
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002477 struct kmod_elf *elf;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002478 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002479 int i, count, ret = 0;
2480
2481 if (mod == NULL || list == NULL)
2482 return -ENOENT;
2483
2484 assert(*list == NULL);
2485
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002486 elf = kmod_module_get_elf(mod);
2487 if (elf == NULL)
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002488 return -errno;
2489
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002490 count = kmod_elf_get_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002491 if (count < 0)
2492 return count;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002493
2494 for (i = 0; i < count; i++) {
2495 struct kmod_module_symbol *mv;
2496 struct kmod_list *n;
2497
2498 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2499 if (mv == NULL) {
2500 ret = -errno;
2501 kmod_module_symbols_free_list(*list);
2502 *list = NULL;
2503 goto list_error;
2504 }
2505
2506 n = kmod_list_append(*list, mv);
2507 if (n != NULL)
2508 *list = n;
2509 else {
2510 kmod_module_symbol_free(mv);
2511 kmod_module_symbols_free_list(*list);
2512 *list = NULL;
2513 ret = -ENOMEM;
2514 goto list_error;
2515 }
2516 }
2517 ret = count;
2518
2519list_error:
2520 free(symbols);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002521 return ret;
2522}
2523
2524/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002525 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002526 * @entry: a list entry representing a kmod module symbols
2527 *
2528 * Get the symbol of a kmod module symbols.
2529 *
2530 * Returns: the symbol of this kmod module symbols on success or NULL
2531 * on failure. The string is owned by the symbols, do not free it.
2532 */
2533KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2534{
2535 struct kmod_module_symbol *symbol;
2536
2537 if (entry == NULL)
2538 return NULL;
2539
2540 symbol = entry->data;
2541 return symbol->symbol;
2542}
2543
2544/**
2545 * kmod_module_symbol_get_crc:
2546 * @entry: a list entry representing a kmod module symbol
2547 *
2548 * Get the crc of a kmod module symbol.
2549 *
2550 * Returns: the crc of this kmod module symbol on success or NULL on
2551 * failure. The string is owned by the symbol, do not free it.
2552 */
2553KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2554{
2555 struct kmod_module_symbol *symbol;
2556
2557 if (entry == NULL)
2558 return 0;
2559
2560 symbol = entry->data;
2561 return symbol->crc;
2562}
2563
2564/**
2565 * kmod_module_symbols_free_list:
2566 * @list: kmod module symbols list
2567 *
2568 * Release the resources taken by @list
2569 */
2570KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2571{
2572 while (list) {
2573 kmod_module_symbol_free(list->data);
2574 list = kmod_list_remove(list);
2575 }
2576}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002577
2578struct kmod_module_dependency_symbol {
2579 uint64_t crc;
2580 uint8_t bind;
2581 char symbol[];
2582};
2583
2584static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2585{
2586 struct kmod_module_dependency_symbol *mv;
2587 size_t symbollen = strlen(symbol) + 1;
2588
2589 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2590 if (mv == NULL)
2591 return NULL;
2592
2593 mv->crc = crc;
2594 mv->bind = bind;
2595 memcpy(mv->symbol, symbol, symbollen);
2596 return mv;
2597}
2598
2599static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2600{
2601 free(dependency_symbol);
2602}
2603
2604/**
2605 * kmod_module_get_dependency_symbols:
2606 * @mod: kmod module
2607 * @list: where to return list of module dependency_symbols. Use
2608 * kmod_module_dependency_symbol_get_symbol() and
2609 * kmod_module_dependency_symbol_get_crc(). Release this list with
2610 * kmod_module_dependency_symbols_free_list()
2611 *
2612 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2613 *
2614 * After use, free the @list by calling
2615 * kmod_module_dependency_symbols_free_list().
2616 *
2617 * Returns: 0 on success or < 0 otherwise.
2618 */
2619KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2620{
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002621 struct kmod_elf *elf;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002622 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002623 int i, count, ret = 0;
2624
2625 if (mod == NULL || list == NULL)
2626 return -ENOENT;
2627
2628 assert(*list == NULL);
2629
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002630 elf = kmod_module_get_elf(mod);
2631 if (elf == NULL)
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002632 return -errno;
2633
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002634 count = kmod_elf_get_dependency_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002635 if (count < 0)
2636 return count;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002637
2638 for (i = 0; i < count; i++) {
2639 struct kmod_module_dependency_symbol *mv;
2640 struct kmod_list *n;
2641
2642 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2643 symbols[i].bind,
2644 symbols[i].symbol);
2645 if (mv == NULL) {
2646 ret = -errno;
2647 kmod_module_dependency_symbols_free_list(*list);
2648 *list = NULL;
2649 goto list_error;
2650 }
2651
2652 n = kmod_list_append(*list, mv);
2653 if (n != NULL)
2654 *list = n;
2655 else {
2656 kmod_module_dependency_symbol_free(mv);
2657 kmod_module_dependency_symbols_free_list(*list);
2658 *list = NULL;
2659 ret = -ENOMEM;
2660 goto list_error;
2661 }
2662 }
2663 ret = count;
2664
2665list_error:
2666 free(symbols);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002667 return ret;
2668}
2669
2670/**
2671 * kmod_module_dependency_symbol_get_symbol:
2672 * @entry: a list entry representing a kmod module dependency_symbols
2673 *
2674 * Get the dependency symbol of a kmod module
2675 *
2676 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2677 * on failure. The string is owned by the dependency_symbols, do not free it.
2678 */
2679KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2680{
2681 struct kmod_module_dependency_symbol *dependency_symbol;
2682
2683 if (entry == NULL)
2684 return NULL;
2685
2686 dependency_symbol = entry->data;
2687 return dependency_symbol->symbol;
2688}
2689
2690/**
2691 * kmod_module_dependency_symbol_get_crc:
2692 * @entry: a list entry representing a kmod module dependency_symbol
2693 *
2694 * Get the crc of a kmod module dependency_symbol.
2695 *
2696 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2697 * failure. The string is owned by the dependency_symbol, do not free it.
2698 */
2699KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2700{
2701 struct kmod_module_dependency_symbol *dependency_symbol;
2702
2703 if (entry == NULL)
2704 return 0;
2705
2706 dependency_symbol = entry->data;
2707 return dependency_symbol->crc;
2708}
2709
2710/**
2711 * kmod_module_dependency_symbol_get_bind:
2712 * @entry: a list entry representing a kmod module dependency_symbol
2713 *
2714 * Get the bind type of a kmod module dependency_symbol.
2715 *
2716 * Returns: the bind of this kmod module dependency_symbol on success
2717 * or < 0 on failure.
2718 */
2719KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2720{
2721 struct kmod_module_dependency_symbol *dependency_symbol;
2722
2723 if (entry == NULL)
2724 return 0;
2725
2726 dependency_symbol = entry->data;
2727 return dependency_symbol->bind;
2728}
2729
2730/**
2731 * kmod_module_dependency_symbols_free_list:
2732 * @list: kmod module dependency_symbols list
2733 *
2734 * Release the resources taken by @list
2735 */
2736KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2737{
2738 while (list) {
2739 kmod_module_dependency_symbol_free(list->data);
2740 list = kmod_list_remove(list);
2741 }
2742}