blob: 7b38e648891de3e261cca3f5e59a1ab695a24b7c [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
Kees Cook144d1822013-02-18 12:02:32 -0800771#ifndef __NR_finit_module
772# define __NR_finit_module -1
773#endif
774static inline int finit_module(int fd, const char *uargs, int flags)
775{
776 return syscall(__NR_finit_module, fd, uargs, flags);
777}
778
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200779/**
780 * kmod_module_insert_module:
781 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200782 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
783 * behavior of this function.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200784 * @options: module's options to pass to Linux Kernel.
785 *
786 * Insert a module in Linux kernel. It opens the file pointed by @mod,
787 * mmap'ing it and passing to kernel.
788 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200789 * Returns: 0 on success or < 0 on failure. If module is already loaded it
790 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200791 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200792KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200793 unsigned int flags,
794 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200795{
796 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200797 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200798 off_t size;
799 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200800 struct kmod_elf *elf = NULL;
801 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200802 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200803
804 if (mod == NULL)
805 return -ENOENT;
806
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200807 path = kmod_module_get_path(mod);
808 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500809 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200810 return -ENOSYS;
811 }
812
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200813 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200814 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200815 err = -errno;
816 return err;
817 }
818
Kees Cook144d1822013-02-18 12:02:32 -0800819 if (kmod_file_get_direct(file)) {
820 unsigned int kernel_flags = 0;
821
822#ifdef HAVE_LINUX_MODULE_H
823 if (flags & KMOD_INSERT_FORCE_VERMAGIC)
824 kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
825 if (flags & KMOD_INSERT_FORCE_MODVERSION)
826 kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
827#endif
828
829 err = finit_module(kmod_file_get_fd(file), args, kernel_flags);
830 if (err == 0 || errno != ENOSYS)
831 goto init_finished;
832 }
833
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200834 size = kmod_file_get_size(file);
835 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200836
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200837 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
838 elf = kmod_elf_new(mem, size);
839 if (elf == NULL) {
840 err = -errno;
841 goto elf_failed;
842 }
843
844 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
845 err = kmod_elf_strip_section(elf, "__versions");
846 if (err < 0)
847 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
848 }
849
850 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
851 err = kmod_elf_strip_vermagic(elf);
852 if (err < 0)
853 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
854 }
855
856 mem = kmod_elf_get_memory(elf);
857 }
858
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200859 err = init_module(mem, size, args);
Kees Cook144d1822013-02-18 12:02:32 -0800860init_finished:
Lucas De Marchibbf59322011-12-30 14:13:33 -0200861 if (err < 0) {
862 err = -errno;
863 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
864 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200865
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200866 if (elf != NULL)
867 kmod_elf_unref(elf);
868elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200869 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200870
871 return err;
872}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200873
Lucas De Marchiddbda022011-12-27 11:40:10 -0200874static bool module_is_blacklisted(struct kmod_module *mod)
875{
876 struct kmod_ctx *ctx = mod->ctx;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300877 const struct kmod_config *config = kmod_get_config(ctx);
878 const struct kmod_list *bl = config->blacklists;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200879 const struct kmod_list *l;
880
881 kmod_list_foreach(l, bl) {
882 const char *modname = kmod_blacklist_get_modname(l);
883
884 if (streq(modname, mod->name))
885 return true;
886 }
887
888 return false;
889}
890
Dave Reisnerd80b1032012-02-24 10:05:11 -0500891/**
892 * kmod_module_apply_filter
893 * @ctx: kmod library context
894 * @filter_type: bitmask to filter modules on
895 * @input: list of kmod_module to be filtered
896 * @output: where to save the new list
897 *
898 * Given a list @input, this function filter it out by the filter mask
899 * and save it in @output.
900 *
901 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
902 * list.
903 */
904KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
905 enum kmod_filter filter_type,
906 const struct kmod_list *input,
907 struct kmod_list **output)
908{
909 const struct kmod_list *li;
910
911 if (ctx == NULL || output == NULL)
912 return -ENOENT;
913
914 *output = NULL;
915 if (input == NULL)
916 return 0;
917
918 kmod_list_foreach(li, input) {
919 struct kmod_module *mod = li->data;
920 struct kmod_list *node;
921
922 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
923 module_is_blacklisted(mod))
924 continue;
925
Dave Reisnerbdda7e12012-02-24 23:02:06 -0500926 if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin)
Dave Reisnerd80b1032012-02-24 10:05:11 -0500927 continue;
928
929 node = kmod_list_append(*output, mod);
930 if (node == NULL)
931 goto fail;
932
933 *output = node;
934 kmod_module_ref(mod);
935 }
936
937 return 0;
938
939fail:
940 kmod_module_unref_list(*output);
941 *output = NULL;
942 return -ENOMEM;
943}
944
Lucas De Marchiddbda022011-12-27 11:40:10 -0200945static int command_do(struct kmod_module *mod, const char *type,
946 const char *cmd)
947{
948 const char *modname = kmod_module_get_name(mod);
949 int err;
950
951 DBG(mod->ctx, "%s %s\n", type, cmd);
952
953 setenv("MODPROBE_MODULE", modname, 1);
954 err = system(cmd);
955 unsetenv("MODPROBE_MODULE");
956
957 if (err == -1 || WEXITSTATUS(err)) {
958 ERR(mod->ctx, "Error running %s command for %s\n",
959 type, modname);
960 if (err != -1)
961 err = -WEXITSTATUS(err);
962 }
963
964 return err;
965}
966
Lucas De Marchib1a51252012-01-29 01:49:09 -0200967struct probe_insert_cb {
968 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
969 void *data;
970};
971
Lucas De Marchiddbda022011-12-27 11:40:10 -0200972static int module_do_install_commands(struct kmod_module *mod,
973 const char *options,
974 struct probe_insert_cb *cb)
975{
976 const char *command = kmod_module_get_install_commands(mod);
977 char *p, *cmd;
978 int err;
979 size_t cmdlen, options_len, varlen;
980
981 assert(command);
982
983 if (options == NULL)
984 options = "";
985
986 options_len = strlen(options);
987 cmdlen = strlen(command);
988 varlen = sizeof("$CMDLINE_OPTS") - 1;
989
990 cmd = memdup(command, cmdlen + 1);
991 if (cmd == NULL)
992 return -ENOMEM;
993
994 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
995 size_t prefixlen = p - cmd;
996 size_t suffixlen = cmdlen - prefixlen - varlen;
997 size_t slen = cmdlen - varlen + options_len;
998 char *suffix = p + varlen;
999 char *s = malloc(slen + 1);
1000 if (s == NULL) {
1001 free(cmd);
1002 return -ENOMEM;
1003 }
1004 memcpy(s, cmd, p - cmd);
1005 memcpy(s + prefixlen, options, options_len);
1006 memcpy(s + prefixlen + options_len, suffix, suffixlen);
1007 s[slen] = '\0';
1008
1009 free(cmd);
1010 cmd = s;
1011 cmdlen = slen;
1012 }
1013
1014 if (cb->run_install != NULL)
1015 err = cb->run_install(mod, cmd, cb->data);
1016 else
1017 err = command_do(mod, "install", cmd);
1018
1019 free(cmd);
1020
1021 return err;
1022}
1023
Lucas De Marchiddbda022011-12-27 11:40:10 -02001024static char *module_options_concat(const char *opt, const char *xopt)
1025{
1026 // TODO: we might need to check if xopt overrides options on opt
1027 size_t optlen = opt == NULL ? 0 : strlen(opt);
1028 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1029 char *r;
1030
1031 if (optlen == 0 && xoptlen == 0)
1032 return NULL;
1033
1034 r = malloc(optlen + xoptlen + 2);
1035
1036 if (opt != NULL) {
1037 memcpy(r, opt, optlen);
1038 r[optlen] = ' ';
1039 optlen++;
1040 }
1041
1042 if (xopt != NULL)
1043 memcpy(r + optlen, xopt, xoptlen);
1044
1045 r[optlen + xoptlen] = '\0';
1046
1047 return r;
1048}
1049
Lucas De Marchib1a51252012-01-29 01:49:09 -02001050static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001051 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001052 struct kmod_list **list);
1053
1054/* re-entrant */
1055static int __kmod_module_fill_softdep(struct kmod_module *mod,
1056 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001057{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001058 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001059 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001060
1061 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001062 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001063 ERR(mod->ctx, "could not get softdep: %s\n",
1064 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001065 goto fail;
1066 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001067
Lucas De Marchib1a51252012-01-29 01:49:09 -02001068 kmod_list_foreach(l, pre) {
1069 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001070 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001071 if (err < 0)
1072 goto fail;
1073 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001074
Lucas De Marchib1a51252012-01-29 01:49:09 -02001075 l = kmod_list_append(*list, kmod_module_ref(mod));
1076 if (l == NULL) {
1077 kmod_module_unref(mod);
1078 err = -ENOMEM;
1079 goto fail;
1080 }
1081 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001082 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001083
Lucas De Marchib1a51252012-01-29 01:49:09 -02001084 kmod_list_foreach(l, post) {
1085 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001086 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001087 if (err < 0)
1088 goto fail;
1089 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001090
Lucas De Marchib1a51252012-01-29 01:49:09 -02001091fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001092 kmod_module_unref_list(pre);
1093 kmod_module_unref_list(post);
1094
1095 return err;
1096}
1097
Lucas De Marchib1a51252012-01-29 01:49:09 -02001098/* re-entrant */
1099static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001100 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001101 struct kmod_list **list)
1102{
1103 struct kmod_list *dep, *l;
1104 int err = 0;
1105
1106 if (mod->visited) {
1107 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1108 mod->name);
1109 return 0;
1110 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001111 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001112
1113 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001114 kmod_list_foreach(l, dep) {
1115 struct kmod_module *m = l->data;
1116 err = __kmod_module_fill_softdep(m, list);
1117 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001118 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001119 }
1120
Lucas De Marchi89e92482012-01-29 02:35:46 -02001121 if (ignorecmd) {
1122 l = kmod_list_append(*list, kmod_module_ref(mod));
1123 if (l == NULL) {
1124 kmod_module_unref(mod);
1125 err = -ENOMEM;
1126 goto finish;
1127 }
1128 *list = l;
1129 mod->ignorecmd = true;
1130 } else
1131 err = __kmod_module_fill_softdep(mod, list);
1132
Lucas De Marchib1a51252012-01-29 01:49:09 -02001133finish:
1134 kmod_module_unref_list(dep);
1135 return err;
1136}
1137
1138static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001139 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001140 struct kmod_list **list)
1141{
1142 int err;
1143
1144 assert(mod != NULL);
1145 assert(list != NULL && *list == NULL);
1146
1147 /*
1148 * Make sure we don't get screwed by previous calls to this function
1149 */
1150 kmod_set_modules_visited(mod->ctx, false);
1151
Lucas De Marchi89e92482012-01-29 02:35:46 -02001152 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001153 if (err < 0) {
1154 kmod_module_unref_list(*list);
1155 *list = NULL;
1156 }
1157
1158 return err;
1159}
1160
Lucas De Marchiddbda022011-12-27 11:40:10 -02001161/**
1162 * kmod_module_probe_insert_module:
1163 * @mod: kmod module
1164 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1165 * behavior of this function.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001166 * @extra_options: module's options to pass to Linux Kernel. It applies only
1167 * to @mod, not to its dependencies.
1168 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001169 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001170 * @print_action: function to call with the action being taken (install or
1171 * insmod). It's useful for tools like modprobe when running with verbose
1172 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001173 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001174 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001175 * install commands and applying blacklist.
1176 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001177 * If @run_install is NULL, this function will fork and exec by calling
1178 * system(3). Don't pass a NULL argument in @run_install if your binary is
1179 * setuid/setgid (see warning in system(3)). If you need control over the
1180 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001181 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001182 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1183 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001184 */
1185KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1186 unsigned int flags, const char *extra_options,
1187 int (*run_install)(struct kmod_module *m,
1188 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001189 const void *data,
1190 void (*print_action)(struct kmod_module *m,
1191 bool install,
1192 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001193{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001194 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001195 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001196 int err;
1197
1198 if (mod == NULL)
1199 return -ENOENT;
1200
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001201 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1202 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001203 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001204 return -EEXIST;
1205 else
1206 return 0;
1207 }
1208
Lucas De Marchi68820172012-08-17 09:38:05 -03001209 /*
1210 * Ugly assignement + check. We need to check if we were told to check
1211 * blacklist and also return the reason why we failed.
1212 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1213 * module is an alias, so we also need to check it
1214 */
1215 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1216 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1217 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001218 if (module_is_blacklisted(mod))
1219 return err;
1220 }
1221
Lucas De Marchi89e92482012-01-29 02:35:46 -02001222 err = kmod_module_get_probe_list(mod,
1223 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001224 if (err < 0)
1225 return err;
1226
1227 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1228 struct kmod_list *filtered = NULL;
1229
Dave Reisnerd80b1032012-02-24 10:05:11 -05001230 err = kmod_module_apply_filter(mod->ctx,
1231 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001232 if (err < 0)
1233 return err;
1234
1235 kmod_module_unref_list(list);
1236 if (filtered == NULL)
1237 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1238
1239 list = filtered;
1240 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001241
1242 cb.run_install = run_install;
1243 cb.data = (void *) data;
1244
Lucas De Marchib1a51252012-01-29 01:49:09 -02001245 kmod_list_foreach(l, list) {
1246 struct kmod_module *m = l->data;
1247 const char *moptions = kmod_module_get_options(m);
1248 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001249 char *options;
1250
1251 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1252 && module_is_inkernel(m)) {
1253 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1254 m->name);
1255 err = -EEXIST;
1256 goto finish_module;
1257 }
1258
1259 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001260 m == mod ? extra_options : NULL);
1261
Lucas De Marchi89e92482012-01-29 02:35:46 -02001262 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001263 if (print_action != NULL)
1264 print_action(m, true, options ?: "");
1265
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001266 if (!(flags & KMOD_PROBE_DRY_RUN))
1267 err = module_do_install_commands(m, options,
1268 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001269 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001270 if (print_action != NULL)
1271 print_action(m, false, options ?: "");
1272
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001273 if (!(flags & KMOD_PROBE_DRY_RUN))
1274 err = kmod_module_insert_module(m, flags,
1275 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001276 }
1277
1278 free(options);
1279
Lucas De Marchiabd55572012-02-19 04:20:30 -02001280finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001281 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001282 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001283 * already loaded and the module being loaded is not a softdep
1284 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001285 *
1286 * We need to check here because of race conditions. We
1287 * checked first if module was already loaded but it may have
1288 * been loaded between the check and the moment we try to
1289 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001290 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001291 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001292 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001293 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001294
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001295 if (err == -EEXIST)
1296 err = 0;
1297 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001298 break;
1299 }
1300
1301 kmod_module_unref_list(list);
1302 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001303}
1304
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001305/**
1306 * kmod_module_get_options:
1307 * @mod: kmod module
1308 *
1309 * Get options of this kmod module. Options come from the configuration file
1310 * and are cached in @mod. The first call to this function will search for
1311 * this module in configuration and subsequent calls return the cached string.
1312 *
1313 * Returns: a string with all the options separated by spaces. This string is
1314 * owned by @mod, do not free it.
1315 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001316KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1317{
1318 if (mod == NULL)
1319 return NULL;
1320
1321 if (!mod->init.options) {
1322 /* lazy init */
1323 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001324 const struct kmod_list *l;
1325 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001326 char *opts = NULL;
1327 size_t optslen = 0;
1328
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001329 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001330
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001331 kmod_list_foreach(l, config->options) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001332 const char *modname = kmod_option_get_modname(l);
1333 const char *str;
1334 size_t len;
1335 void *tmp;
1336
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001337 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1338 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1339 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001340 continue;
1341
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001342 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 -02001343 str = kmod_option_get_options(l);
1344 len = strlen(str);
1345 if (len < 1)
1346 continue;
1347
1348 tmp = realloc(opts, optslen + len + 2);
1349 if (tmp == NULL) {
1350 free(opts);
1351 goto failed;
1352 }
1353
1354 opts = tmp;
1355
1356 if (optslen > 0) {
1357 opts[optslen] = ' ';
1358 optslen++;
1359 }
1360
1361 memcpy(opts + optslen, str, len);
1362 optslen += len;
1363 opts[optslen] = '\0';
1364 }
1365
1366 m->init.options = true;
1367 m->options = opts;
1368 }
1369
1370 return mod->options;
1371
1372failed:
1373 ERR(mod->ctx, "out of memory\n");
1374 return NULL;
1375}
1376
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001377/**
1378 * kmod_module_get_install_commands:
1379 * @mod: kmod module
1380 *
1381 * Get install commands for this kmod module. Install commands come from the
1382 * configuration file and are cached in @mod. The first call to this function
1383 * will search for this module in configuration and subsequent calls return
1384 * the cached string. The install commands are returned as they were in the
1385 * configuration, concatenated by ';'. No other processing is made in this
1386 * string.
1387 *
1388 * Returns: a string with all install commands separated by semicolons. This
1389 * string is owned by @mod, do not free it.
1390 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001391KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1392{
1393 if (mod == NULL)
1394 return NULL;
1395
1396 if (!mod->init.install_commands) {
1397 /* lazy init */
1398 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001399 const struct kmod_list *l;
1400 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001401
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001402 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001403
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001404 kmod_list_foreach(l, config->install_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001405 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001406
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001407 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001408 continue;
1409
Lucas De Marchi60f67602011-12-16 03:33:26 -02001410 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001411
Lucas De Marchi60f67602011-12-16 03:33:26 -02001412 /*
1413 * find only the first command, as modprobe from
1414 * module-init-tools does
1415 */
1416 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001417 }
1418
1419 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001420 }
1421
1422 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001423}
1424
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001425void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1426{
1427 mod->init.install_commands = true;
1428 mod->install_commands = cmd;
1429}
1430
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001431static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1432{
1433 struct kmod_list *ret = NULL;
1434 unsigned i;
1435
1436 for (i = 0; i < count; i++) {
1437 const char *depname = array[i];
1438 struct kmod_list *lst = NULL;
1439 int err;
1440
1441 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1442 if (err < 0) {
1443 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1444 continue;
1445 } else if (lst != NULL)
1446 ret = kmod_list_append_list(ret, lst);
1447 }
1448 return ret;
1449}
1450
1451/**
1452 * kmod_module_get_softdeps:
1453 * @mod: kmod module
1454 * @pre: where to save the list of preceding soft dependencies.
1455 * @post: where to save the list of post soft dependencies.
1456 *
1457 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001458 * from configuration file and are not cached in @mod because it may include
1459 * dependency cycles that would make we leak kmod_module. Any call
1460 * to this function will search for this module in configuration, allocate a
1461 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001462 *
1463 * Both @pre and @post are newly created list of kmod_module and
1464 * should be unreferenced with kmod_module_unref_list().
1465 *
1466 * Returns: 0 on success or < 0 otherwise.
1467 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001468KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1469 struct kmod_list **pre,
1470 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001471{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001472 const struct kmod_list *l;
1473 const struct kmod_config *config;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001474
1475 if (mod == NULL || pre == NULL || post == NULL)
1476 return -ENOENT;
1477
1478 assert(*pre == NULL);
1479 assert(*post == NULL);
1480
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001481 config = kmod_get_config(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001482
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001483 kmod_list_foreach(l, config->softdeps) {
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001484 const char *modname = kmod_softdep_get_name(l);
1485 const char * const *array;
1486 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001487
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001488 if (fnmatch(modname, mod->name, 0) != 0)
1489 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001490
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001491 array = kmod_softdep_get_pre(l, &count);
1492 *pre = lookup_softdep(mod->ctx, array, count);
1493 array = kmod_softdep_get_post(l, &count);
1494 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001495
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001496 /*
1497 * find only the first command, as modprobe from
1498 * module-init-tools does
1499 */
1500 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001501 }
1502
1503 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001504}
1505
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001506/**
1507 * kmod_module_get_remove_commands:
1508 * @mod: kmod module
1509 *
1510 * Get remove commands for this kmod module. Remove commands come from the
1511 * configuration file and are cached in @mod. The first call to this function
1512 * will search for this module in configuration and subsequent calls return
1513 * the cached string. The remove commands are returned as they were in the
1514 * configuration, concatenated by ';'. No other processing is made in this
1515 * string.
1516 *
1517 * Returns: a string with all remove commands separated by semicolons. This
1518 * string is owned by @mod, do not free it.
1519 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001520KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1521{
1522 if (mod == NULL)
1523 return NULL;
1524
1525 if (!mod->init.remove_commands) {
1526 /* lazy init */
1527 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001528 const struct kmod_list *l;
1529 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001530
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001531 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001532
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001533 kmod_list_foreach(l, config->remove_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001534 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001535
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001536 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001537 continue;
1538
Lucas De Marchi60f67602011-12-16 03:33:26 -02001539 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001540
Lucas De Marchi60f67602011-12-16 03:33:26 -02001541 /*
1542 * find only the first command, as modprobe from
1543 * module-init-tools does
1544 */
1545 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001546 }
1547
1548 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001549 }
1550
1551 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001552}
1553
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001554void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1555{
1556 mod->init.remove_commands = true;
1557 mod->remove_commands = cmd;
1558}
1559
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001560/**
1561 * SECTION:libkmod-loaded
1562 * @short_description: currently loaded modules
1563 *
1564 * Information about currently loaded modules, as reported by Linux kernel.
1565 * These information are not cached by libkmod and are always read from /sys
1566 * and /proc/modules.
1567 */
1568
1569/**
1570 * kmod_module_new_from_loaded:
1571 * @ctx: kmod library context
1572 * @list: where to save the list of loaded modules
1573 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001574 * Create a new list of kmod modules with all modules currently loaded in
1575 * kernel. It uses /proc/modules to get the names of loaded modules and to
1576 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1577 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001578 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001579 * The initial refcount is 1, and needs to be decremented to release the
1580 * resources of the kmod_module. The returned @list must be released by
1581 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1582 * kmod_modules created, they are all released upon @ctx destruction too. Do
1583 * not unref @ctx before all the desired operations with the returned list are
1584 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001585 *
1586 * Returns: 0 on success or < 0 on error.
1587 */
1588KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1589 struct kmod_list **list)
1590{
1591 struct kmod_list *l = NULL;
1592 FILE *fp;
1593 char line[4096];
1594
1595 if (ctx == NULL || list == NULL)
1596 return -ENOENT;
1597
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001598 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001599 if (fp == NULL) {
1600 int err = -errno;
1601 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1602 return err;
1603 }
1604
1605 while (fgets(line, sizeof(line), fp)) {
1606 struct kmod_module *m;
1607 struct kmod_list *node;
1608 int err;
1609 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1610
1611 err = kmod_module_new_from_name(ctx, name, &m);
1612 if (err < 0) {
1613 ERR(ctx, "could not get module from name '%s': %s\n",
1614 name, strerror(-err));
1615 continue;
1616 }
1617
1618 node = kmod_list_append(l, m);
1619 if (node)
1620 l = node;
1621 else {
1622 ERR(ctx, "out of memory\n");
1623 kmod_module_unref(m);
1624 }
1625 }
1626
1627 fclose(fp);
1628 *list = l;
1629
1630 return 0;
1631}
1632
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001633/**
1634 * kmod_module_initstate_str:
1635 * @state: the state as returned by kmod_module_get_initstate()
1636 *
1637 * Translate a initstate to a string.
1638 *
1639 * Returns: the string associated to the @state. This string is statically
1640 * allocated, do not free it.
1641 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001642KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1643{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001644 switch (state) {
1645 case KMOD_MODULE_BUILTIN:
1646 return "builtin";
1647 case KMOD_MODULE_LIVE:
1648 return "live";
1649 case KMOD_MODULE_COMING:
1650 return "coming";
1651 case KMOD_MODULE_GOING:
1652 return "going";
1653 default:
1654 return NULL;
1655 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001656}
1657
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001658/**
1659 * kmod_module_get_initstate:
1660 * @mod: kmod module
1661 *
1662 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1663 * /sys filesystem.
1664 *
1665 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1666 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001667KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1668{
1669 char path[PATH_MAX], buf[32];
1670 int fd, err, pathlen;
1671
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001672 if (mod == NULL)
1673 return -ENOENT;
1674
Lucas De Marchi38052742012-02-16 20:43:16 -02001675 if (mod->builtin)
1676 return KMOD_MODULE_BUILTIN;
1677
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001678 pathlen = snprintf(path, sizeof(path),
1679 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001680 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001681 if (fd < 0) {
1682 err = -errno;
1683
Lucas De Marchiddbda022011-12-27 11:40:10 -02001684 DBG(mod->ctx, "could not open '%s': %s\n",
1685 path, strerror(-err));
1686
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001687 if (pathlen > (int)sizeof("/initstate") - 1) {
1688 struct stat st;
1689 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1690 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1691 return KMOD_MODULE_BUILTIN;
1692 }
1693
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001694 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001695 path, strerror(-err));
1696 return err;
1697 }
1698
1699 err = read_str_safe(fd, buf, sizeof(buf));
1700 close(fd);
1701 if (err < 0) {
1702 ERR(mod->ctx, "could not read from '%s': %s\n",
1703 path, strerror(-err));
1704 return err;
1705 }
1706
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001707 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001708 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001709 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001710 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001711 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001712 return KMOD_MODULE_GOING;
1713
1714 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1715 return -EINVAL;
1716}
1717
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001718/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001719 * kmod_module_get_size:
1720 * @mod: kmod module
1721 *
Dave Reisner486f9012012-06-28 09:09:48 -04001722 * Get the size of this kmod module as returned by Linux kernel. If supported,
1723 * the size is read from the coresize attribute in /sys/module. For older
1724 * kernels, this falls back on /proc/modules and searches for the specified
1725 * module to get its size.
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001726 *
1727 * Returns: the size of this kmod module.
1728 */
1729KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1730{
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001731 FILE *fp;
1732 char line[4096];
1733 int lineno = 0;
1734 long size = -ENOENT;
Dave Reisner486f9012012-06-28 09:09:48 -04001735 int dfd, cfd;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001736
1737 if (mod == NULL)
1738 return -ENOENT;
1739
Dave Reisner486f9012012-06-28 09:09:48 -04001740 /* try to open the module dir in /sys. If this fails, don't
1741 * bother trying to find the size as we know the module isn't
1742 * loaded.
1743 */
1744 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
1745 dfd = open(line, O_RDONLY);
1746 if (dfd < 0)
1747 return -errno;
1748
1749 /* available as of linux 3.3.x */
1750 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1751 if (cfd >= 0) {
1752 if (read_str_long(cfd, &size, 10) < 0)
1753 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1754 close(cfd);
1755 goto done;
1756 }
1757
1758 /* fall back on parsing /proc/modules */
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001759 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001760 if (fp == NULL) {
1761 int err = -errno;
1762 ERR(mod->ctx,
1763 "could not open /proc/modules: %s\n", strerror(errno));
1764 return err;
1765 }
1766
1767 while (fgets(line, sizeof(line), fp)) {
1768 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1769 long value;
1770
1771 lineno++;
1772 if (tok == NULL || !streq(tok, mod->name))
1773 continue;
1774
1775 tok = strtok_r(NULL, " \t", &saveptr);
1776 if (tok == NULL) {
1777 ERR(mod->ctx,
1778 "invalid line format at /proc/modules:%d\n", lineno);
1779 break;
1780 }
1781
1782 value = strtol(tok, &endptr, 10);
1783 if (endptr == tok || *endptr != '\0') {
1784 ERR(mod->ctx,
1785 "invalid line format at /proc/modules:%d\n", lineno);
1786 break;
1787 }
1788
1789 size = value;
1790 break;
1791 }
1792 fclose(fp);
Dave Reisner486f9012012-06-28 09:09:48 -04001793
1794done:
1795 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001796 return size;
1797}
1798
1799/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001800 * kmod_module_get_refcnt:
1801 * @mod: kmod module
1802 *
1803 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1804 * /sys filesystem.
1805 *
1806 * Returns: 0 on success or < 0 on failure.
1807 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001808KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1809{
1810 char path[PATH_MAX];
1811 long refcnt;
1812 int fd, err;
1813
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001814 if (mod == NULL)
1815 return -ENOENT;
1816
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001817 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001818 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001819 if (fd < 0) {
1820 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001821 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001822 path, strerror(errno));
1823 return err;
1824 }
1825
1826 err = read_str_long(fd, &refcnt, 10);
1827 close(fd);
1828 if (err < 0) {
1829 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1830 path, strerror(-err));
1831 return err;
1832 }
1833
1834 return (int)refcnt;
1835}
1836
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001837/**
1838 * kmod_module_get_holders:
1839 * @mod: kmod module
1840 *
1841 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1842 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1843 *
1844 * Returns: a new list of kmod modules on success or NULL on failure.
1845 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001846KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1847{
1848 char dname[PATH_MAX];
1849 struct kmod_list *list = NULL;
1850 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001851
1852 if (mod == NULL)
1853 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001854
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001855 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1856
1857 d = opendir(dname);
1858 if (d == NULL) {
1859 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001860 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001861 return NULL;
1862 }
1863
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001864 for (;;) {
1865 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001866 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001867 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001868 int err;
1869
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001870 err = readdir_r(d, &de, &entp);
1871 if (err != 0) {
1872 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1873 mod->name, strerror(-err));
1874 goto fail;
1875 }
1876
1877 if (entp == NULL)
1878 break;
1879
1880 if (de.d_name[0] == '.') {
1881 if (de.d_name[1] == '\0' ||
1882 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001883 continue;
1884 }
1885
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001886 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001887 if (err < 0) {
1888 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001889 de.d_name, strerror(-err));
1890 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001891 }
1892
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001893 l = kmod_list_append(list, holder);
1894 if (l != NULL) {
1895 list = l;
1896 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001897 ERR(mod->ctx, "out of memory\n");
1898 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001899 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001900 }
1901 }
1902
1903 closedir(d);
1904 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001905
1906fail:
1907 closedir(d);
1908 kmod_module_unref_list(list);
1909 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001910}
1911
1912struct kmod_module_section {
1913 unsigned long address;
1914 char name[];
1915};
1916
1917static void kmod_module_section_free(struct kmod_module_section *section)
1918{
1919 free(section);
1920}
1921
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001922/**
1923 * kmod_module_get_sections:
1924 * @mod: kmod module
1925 *
1926 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1927 * structure contained in this list is internal to libkmod and their fields
1928 * can be obtained by calling kmod_module_section_get_name() and
1929 * kmod_module_section_get_address().
1930 *
1931 * After use, free the @list by calling kmod_module_section_free_list().
1932 *
1933 * Returns: a new list of kmod module sections on success or NULL on failure.
1934 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001935KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1936{
1937 char dname[PATH_MAX];
1938 struct kmod_list *list = NULL;
1939 DIR *d;
1940 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001941
1942 if (mod == NULL)
1943 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001944
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001945 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1946
1947 d = opendir(dname);
1948 if (d == NULL) {
1949 ERR(mod->ctx, "could not open '%s': %s\n",
1950 dname, strerror(errno));
1951 return NULL;
1952 }
1953
1954 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001955
1956 for (;;) {
1957 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001958 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001959 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001960 unsigned long address;
1961 size_t namesz;
1962 int fd, err;
1963
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001964 err = readdir_r(d, &de, &entp);
1965 if (err != 0) {
1966 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1967 mod->name, strerror(-err));
1968 goto fail;
1969 }
1970
1971 if (de.d_name[0] == '.') {
1972 if (de.d_name[1] == '\0' ||
1973 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001974 continue;
1975 }
1976
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001977 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001978 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001979 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1980 dname, de.d_name);
1981 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001982 }
1983
1984 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001985 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001986 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001987 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1988 dname, de.d_name);
1989 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001990 }
1991
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001992 namesz = strlen(de.d_name) + 1;
1993 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001994
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001995 if (section == NULL) {
1996 ERR(mod->ctx, "out of memory\n");
1997 goto fail;
1998 }
1999
2000 section->address = address;
2001 memcpy(section->name, de.d_name, namesz);
2002
2003 l = kmod_list_append(list, section);
2004 if (l != NULL) {
2005 list = l;
2006 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002007 ERR(mod->ctx, "out of memory\n");
2008 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002009 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002010 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002011 }
2012
2013 closedir(d);
2014 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002015
2016fail:
2017 closedir(d);
2018 kmod_module_unref_list(list);
2019 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002020}
2021
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002022/**
2023 * kmod_module_section_get_module_name:
2024 * @entry: a list entry representing a kmod module section
2025 *
2026 * Get the name of a kmod module section.
2027 *
2028 * After use, free the @list by calling kmod_module_section_free_list().
2029 *
2030 * Returns: the name of this kmod module section on success or NULL on
2031 * failure. The string is owned by the section, do not free it.
2032 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002033KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2034{
2035 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002036
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002037 if (entry == NULL)
2038 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002039
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002040 section = entry->data;
2041 return section->name;
2042}
2043
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002044/**
2045 * kmod_module_section_get_address:
2046 * @entry: a list entry representing a kmod module section
2047 *
2048 * Get the address of a kmod module section.
2049 *
2050 * After use, free the @list by calling kmod_module_section_free_list().
2051 *
2052 * Returns: the address of this kmod module section on success or ULONG_MAX
2053 * on failure.
2054 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002055KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2056{
2057 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002058
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002059 if (entry == NULL)
2060 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002061
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002062 section = entry->data;
2063 return section->address;
2064}
2065
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002066/**
2067 * kmod_module_section_free_list:
2068 * @list: kmod module section list
2069 *
2070 * Release the resources taken by @list
2071 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002072KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2073{
2074 while (list) {
2075 kmod_module_section_free(list->data);
2076 list = kmod_list_remove(list);
2077 }
2078}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002079
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002080static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2081{
2082 if (mod->file == NULL) {
2083 const char *path = kmod_module_get_path(mod);
2084
2085 if (path == NULL) {
2086 errno = ENOENT;
2087 return NULL;
2088 }
2089
2090 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2091 path);
2092 if (mod->file == NULL)
2093 return NULL;
2094 }
2095
2096 return kmod_file_get_elf(mod->file);
2097}
2098
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002099struct kmod_module_info {
2100 char *key;
2101 char value[];
2102};
2103
2104static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2105{
2106 struct kmod_module_info *info;
2107
2108 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2109 if (info == NULL)
2110 return NULL;
2111
2112 info->key = (char *)info + sizeof(struct kmod_module_info)
2113 + valuelen + 1;
2114 memcpy(info->key, key, keylen);
2115 info->key[keylen] = '\0';
2116 memcpy(info->value, value, valuelen);
2117 info->value[valuelen] = '\0';
2118 return info;
2119}
2120
2121static void kmod_module_info_free(struct kmod_module_info *info)
2122{
2123 free(info);
2124}
2125
Michal Marekf64458c2013-01-16 10:18:17 +01002126static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2127{
2128 struct kmod_module_info *info;
2129 struct kmod_list *n;
2130
2131 info = kmod_module_info_new(key, keylen, value, valuelen);
Michal Marek63339342013-01-16 17:51:57 +01002132 if (info == NULL)
Michal Marekf64458c2013-01-16 10:18:17 +01002133 return NULL;
Michal Marekf64458c2013-01-16 10:18:17 +01002134 n = kmod_list_append(*list, info);
Michal Marek63339342013-01-16 17:51:57 +01002135 if (n != NULL)
2136 *list = n;
2137 else
Michal Marekf64458c2013-01-16 10:18:17 +01002138 kmod_module_info_free(info);
Michal Marekf64458c2013-01-16 10:18:17 +01002139 return n;
2140}
2141
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002142/**
2143 * kmod_module_get_info:
2144 * @mod: kmod module
2145 * @list: where to return list of module information. Use
2146 * kmod_module_info_get_key() and
2147 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002148 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002149 *
2150 * Get a list of entries in ELF section ".modinfo", these contain
2151 * alias, license, depends, vermagic and other keys with respective
Michal Marek8fe16812013-01-16 09:52:01 +01002152 * values. If the module is signed (CONFIG_MODULE_SIG), information
2153 * about the module signature is included as well: signer,
2154 * sig_key and sig_hashalgo.
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002155 *
2156 * After use, free the @list by calling kmod_module_info_free_list().
2157 *
2158 * Returns: 0 on success or < 0 otherwise.
2159 */
2160KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2161{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002162 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002163 char **strings;
Michal Marekf64458c2013-01-16 10:18:17 +01002164 int i, count, ret = -ENOMEM;
Michal Marek8fe16812013-01-16 09:52:01 +01002165 struct kmod_signature_info sig_info;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002166
2167 if (mod == NULL || list == NULL)
2168 return -ENOENT;
2169
2170 assert(*list == NULL);
2171
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002172 elf = kmod_module_get_elf(mod);
2173 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002174 return -errno;
2175
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002176 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002177 if (count < 0)
2178 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002179
2180 for (i = 0; i < count; i++) {
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002181 struct kmod_list *n;
2182 const char *key, *value;
2183 size_t keylen, valuelen;
2184
2185 key = strings[i];
2186 value = strchr(key, '=');
2187 if (value == NULL) {
2188 keylen = strlen(key);
2189 valuelen = 0;
2190 } else {
2191 keylen = value - key;
2192 value++;
2193 valuelen = strlen(value);
2194 }
2195
Michal Marekf64458c2013-01-16 10:18:17 +01002196 n = kmod_module_info_append(list, key, keylen, value, valuelen);
2197 if (n == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002198 goto list_error;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002199 }
Michal Marek8fe16812013-01-16 09:52:01 +01002200
2201 if (kmod_module_signature_info(mod->file, &sig_info)) {
2202 struct kmod_list *n;
2203 char *key_hex;
2204
2205 n = kmod_module_info_append(list, "signer", strlen("signer"),
2206 sig_info.signer, sig_info.signer_len);
2207 if (n == NULL)
2208 goto list_error;
2209 count++;
2210
2211 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2212 key_hex = malloc(sig_info.key_id_len * 3);
2213 if (key_hex == NULL)
2214 goto list_error;
2215 for (i = 0; i < (int)sig_info.key_id_len; i++) {
2216 sprintf(key_hex + i * 3, "%02X",
2217 (unsigned char)sig_info.key_id[i]);
2218 if (i < (int)sig_info.key_id_len - 1)
2219 key_hex[i * 3 + 2] = ':';
2220 }
2221 n = kmod_module_info_append(list, "sig_key", strlen("sig_key"),
2222 key_hex, sig_info.key_id_len * 3 - 1);
2223 free(key_hex);
2224 if (n == NULL)
2225 goto list_error;
2226 count++;
2227
2228 n = kmod_module_info_append(list,
2229 "sig_hashalgo", strlen("sig_hashalgo"),
2230 sig_info.hash_algo, strlen(sig_info.hash_algo));
2231 if (n == NULL)
2232 goto list_error;
2233 count++;
2234
2235 /*
2236 * Omit sig_info.id_type and sig_info.algo for now, as these
2237 * are currently constant.
2238 */
2239 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002240 ret = count;
2241
2242list_error:
Michal Marek63339342013-01-16 17:51:57 +01002243 if (ret < 0) {
2244 kmod_module_info_free_list(*list);
2245 *list = NULL;
2246 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002247 free(strings);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002248 return ret;
2249}
2250
2251/**
2252 * kmod_module_info_get_key:
2253 * @entry: a list entry representing a kmod module info
2254 *
2255 * Get the key of a kmod module info.
2256 *
2257 * Returns: the key of this kmod module info on success or NULL on
2258 * failure. The string is owned by the info, do not free it.
2259 */
2260KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2261{
2262 struct kmod_module_info *info;
2263
2264 if (entry == NULL)
2265 return NULL;
2266
2267 info = entry->data;
2268 return info->key;
2269}
2270
2271/**
2272 * kmod_module_info_get_value:
2273 * @entry: a list entry representing a kmod module info
2274 *
2275 * Get the value of a kmod module info.
2276 *
2277 * Returns: the value of this kmod module info on success or NULL on
2278 * failure. The string is owned by the info, do not free it.
2279 */
2280KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2281{
2282 struct kmod_module_info *info;
2283
2284 if (entry == NULL)
2285 return NULL;
2286
2287 info = entry->data;
2288 return info->value;
2289}
2290
2291/**
2292 * kmod_module_info_free_list:
2293 * @list: kmod module info list
2294 *
2295 * Release the resources taken by @list
2296 */
2297KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2298{
2299 while (list) {
2300 kmod_module_info_free(list->data);
2301 list = kmod_list_remove(list);
2302 }
2303}
2304
2305struct kmod_module_version {
2306 uint64_t crc;
2307 char symbol[];
2308};
2309
2310static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2311{
2312 struct kmod_module_version *mv;
2313 size_t symbollen = strlen(symbol) + 1;
2314
2315 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2316 if (mv == NULL)
2317 return NULL;
2318
2319 mv->crc = crc;
2320 memcpy(mv->symbol, symbol, symbollen);
2321 return mv;
2322}
2323
2324static void kmod_module_version_free(struct kmod_module_version *version)
2325{
2326 free(version);
2327}
2328
2329/**
2330 * kmod_module_get_versions:
2331 * @mod: kmod module
2332 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002333 * kmod_module_version_get_symbol() and
2334 * kmod_module_version_get_crc(). Release this list with
2335 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002336 *
2337 * Get a list of entries in ELF section "__versions".
2338 *
2339 * After use, free the @list by calling kmod_module_versions_free_list().
2340 *
2341 * Returns: 0 on success or < 0 otherwise.
2342 */
2343KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2344{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002345 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002346 struct kmod_modversion *versions;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002347 int i, count, ret = 0;
2348
2349 if (mod == NULL || list == NULL)
2350 return -ENOENT;
2351
2352 assert(*list == NULL);
2353
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002354 elf = kmod_module_get_elf(mod);
2355 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002356 return -errno;
2357
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002358 count = kmod_elf_get_modversions(elf, &versions);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002359 if (count < 0)
2360 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002361
2362 for (i = 0; i < count; i++) {
2363 struct kmod_module_version *mv;
2364 struct kmod_list *n;
2365
2366 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2367 if (mv == NULL) {
2368 ret = -errno;
2369 kmod_module_versions_free_list(*list);
2370 *list = NULL;
2371 goto list_error;
2372 }
2373
2374 n = kmod_list_append(*list, mv);
2375 if (n != NULL)
2376 *list = n;
2377 else {
2378 kmod_module_version_free(mv);
2379 kmod_module_versions_free_list(*list);
2380 *list = NULL;
2381 ret = -ENOMEM;
2382 goto list_error;
2383 }
2384 }
2385 ret = count;
2386
2387list_error:
2388 free(versions);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002389 return ret;
2390}
2391
2392/**
2393 * kmod_module_versions_get_symbol:
2394 * @entry: a list entry representing a kmod module versions
2395 *
2396 * Get the symbol of a kmod module versions.
2397 *
2398 * Returns: the symbol of this kmod module versions on success or NULL
2399 * on failure. The string is owned by the versions, do not free it.
2400 */
2401KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2402{
2403 struct kmod_module_version *version;
2404
2405 if (entry == NULL)
2406 return NULL;
2407
2408 version = entry->data;
2409 return version->symbol;
2410}
2411
2412/**
2413 * kmod_module_version_get_crc:
2414 * @entry: a list entry representing a kmod module version
2415 *
2416 * Get the crc of a kmod module version.
2417 *
2418 * Returns: the crc of this kmod module version on success or NULL on
2419 * failure. The string is owned by the version, do not free it.
2420 */
2421KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2422{
2423 struct kmod_module_version *version;
2424
2425 if (entry == NULL)
2426 return 0;
2427
2428 version = entry->data;
2429 return version->crc;
2430}
2431
2432/**
2433 * kmod_module_versions_free_list:
2434 * @list: kmod module versions list
2435 *
2436 * Release the resources taken by @list
2437 */
2438KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2439{
2440 while (list) {
2441 kmod_module_version_free(list->data);
2442 list = kmod_list_remove(list);
2443 }
2444}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002445
2446struct kmod_module_symbol {
2447 uint64_t crc;
2448 char symbol[];
2449};
2450
2451static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2452{
2453 struct kmod_module_symbol *mv;
2454 size_t symbollen = strlen(symbol) + 1;
2455
2456 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2457 if (mv == NULL)
2458 return NULL;
2459
2460 mv->crc = crc;
2461 memcpy(mv->symbol, symbol, symbollen);
2462 return mv;
2463}
2464
2465static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2466{
2467 free(symbol);
2468}
2469
2470/**
2471 * kmod_module_get_symbols:
2472 * @mod: kmod module
2473 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002474 * kmod_module_symbol_get_symbol() and
2475 * kmod_module_symbol_get_crc(). Release this list with
2476 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002477 *
2478 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2479 *
2480 * After use, free the @list by calling kmod_module_symbols_free_list().
2481 *
2482 * Returns: 0 on success or < 0 otherwise.
2483 */
2484KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2485{
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002486 struct kmod_elf *elf;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002487 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002488 int i, count, ret = 0;
2489
2490 if (mod == NULL || list == NULL)
2491 return -ENOENT;
2492
2493 assert(*list == NULL);
2494
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002495 elf = kmod_module_get_elf(mod);
2496 if (elf == NULL)
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002497 return -errno;
2498
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002499 count = kmod_elf_get_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002500 if (count < 0)
2501 return count;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002502
2503 for (i = 0; i < count; i++) {
2504 struct kmod_module_symbol *mv;
2505 struct kmod_list *n;
2506
2507 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2508 if (mv == NULL) {
2509 ret = -errno;
2510 kmod_module_symbols_free_list(*list);
2511 *list = NULL;
2512 goto list_error;
2513 }
2514
2515 n = kmod_list_append(*list, mv);
2516 if (n != NULL)
2517 *list = n;
2518 else {
2519 kmod_module_symbol_free(mv);
2520 kmod_module_symbols_free_list(*list);
2521 *list = NULL;
2522 ret = -ENOMEM;
2523 goto list_error;
2524 }
2525 }
2526 ret = count;
2527
2528list_error:
2529 free(symbols);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002530 return ret;
2531}
2532
2533/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002534 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002535 * @entry: a list entry representing a kmod module symbols
2536 *
2537 * Get the symbol of a kmod module symbols.
2538 *
2539 * Returns: the symbol of this kmod module symbols on success or NULL
2540 * on failure. The string is owned by the symbols, do not free it.
2541 */
2542KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2543{
2544 struct kmod_module_symbol *symbol;
2545
2546 if (entry == NULL)
2547 return NULL;
2548
2549 symbol = entry->data;
2550 return symbol->symbol;
2551}
2552
2553/**
2554 * kmod_module_symbol_get_crc:
2555 * @entry: a list entry representing a kmod module symbol
2556 *
2557 * Get the crc of a kmod module symbol.
2558 *
2559 * Returns: the crc of this kmod module symbol on success or NULL on
2560 * failure. The string is owned by the symbol, do not free it.
2561 */
2562KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2563{
2564 struct kmod_module_symbol *symbol;
2565
2566 if (entry == NULL)
2567 return 0;
2568
2569 symbol = entry->data;
2570 return symbol->crc;
2571}
2572
2573/**
2574 * kmod_module_symbols_free_list:
2575 * @list: kmod module symbols list
2576 *
2577 * Release the resources taken by @list
2578 */
2579KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2580{
2581 while (list) {
2582 kmod_module_symbol_free(list->data);
2583 list = kmod_list_remove(list);
2584 }
2585}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002586
2587struct kmod_module_dependency_symbol {
2588 uint64_t crc;
2589 uint8_t bind;
2590 char symbol[];
2591};
2592
2593static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2594{
2595 struct kmod_module_dependency_symbol *mv;
2596 size_t symbollen = strlen(symbol) + 1;
2597
2598 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2599 if (mv == NULL)
2600 return NULL;
2601
2602 mv->crc = crc;
2603 mv->bind = bind;
2604 memcpy(mv->symbol, symbol, symbollen);
2605 return mv;
2606}
2607
2608static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2609{
2610 free(dependency_symbol);
2611}
2612
2613/**
2614 * kmod_module_get_dependency_symbols:
2615 * @mod: kmod module
2616 * @list: where to return list of module dependency_symbols. Use
2617 * kmod_module_dependency_symbol_get_symbol() and
2618 * kmod_module_dependency_symbol_get_crc(). Release this list with
2619 * kmod_module_dependency_symbols_free_list()
2620 *
2621 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2622 *
2623 * After use, free the @list by calling
2624 * kmod_module_dependency_symbols_free_list().
2625 *
2626 * Returns: 0 on success or < 0 otherwise.
2627 */
2628KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2629{
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002630 struct kmod_elf *elf;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002631 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002632 int i, count, ret = 0;
2633
2634 if (mod == NULL || list == NULL)
2635 return -ENOENT;
2636
2637 assert(*list == NULL);
2638
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002639 elf = kmod_module_get_elf(mod);
2640 if (elf == NULL)
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002641 return -errno;
2642
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002643 count = kmod_elf_get_dependency_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002644 if (count < 0)
2645 return count;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002646
2647 for (i = 0; i < count; i++) {
2648 struct kmod_module_dependency_symbol *mv;
2649 struct kmod_list *n;
2650
2651 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2652 symbols[i].bind,
2653 symbols[i].symbol);
2654 if (mv == NULL) {
2655 ret = -errno;
2656 kmod_module_dependency_symbols_free_list(*list);
2657 *list = NULL;
2658 goto list_error;
2659 }
2660
2661 n = kmod_list_append(*list, mv);
2662 if (n != NULL)
2663 *list = n;
2664 else {
2665 kmod_module_dependency_symbol_free(mv);
2666 kmod_module_dependency_symbols_free_list(*list);
2667 *list = NULL;
2668 ret = -ENOMEM;
2669 goto list_error;
2670 }
2671 }
2672 ret = count;
2673
2674list_error:
2675 free(symbols);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002676 return ret;
2677}
2678
2679/**
2680 * kmod_module_dependency_symbol_get_symbol:
2681 * @entry: a list entry representing a kmod module dependency_symbols
2682 *
2683 * Get the dependency symbol of a kmod module
2684 *
2685 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2686 * on failure. The string is owned by the dependency_symbols, do not free it.
2687 */
2688KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2689{
2690 struct kmod_module_dependency_symbol *dependency_symbol;
2691
2692 if (entry == NULL)
2693 return NULL;
2694
2695 dependency_symbol = entry->data;
2696 return dependency_symbol->symbol;
2697}
2698
2699/**
2700 * kmod_module_dependency_symbol_get_crc:
2701 * @entry: a list entry representing a kmod module dependency_symbol
2702 *
2703 * Get the crc of a kmod module dependency_symbol.
2704 *
2705 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2706 * failure. The string is owned by the dependency_symbol, do not free it.
2707 */
2708KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2709{
2710 struct kmod_module_dependency_symbol *dependency_symbol;
2711
2712 if (entry == NULL)
2713 return 0;
2714
2715 dependency_symbol = entry->data;
2716 return dependency_symbol->crc;
2717}
2718
2719/**
2720 * kmod_module_dependency_symbol_get_bind:
2721 * @entry: a list entry representing a kmod module dependency_symbol
2722 *
2723 * Get the bind type of a kmod module dependency_symbol.
2724 *
2725 * Returns: the bind of this kmod module dependency_symbol on success
2726 * or < 0 on failure.
2727 */
2728KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2729{
2730 struct kmod_module_dependency_symbol *dependency_symbol;
2731
2732 if (entry == NULL)
2733 return 0;
2734
2735 dependency_symbol = entry->data;
2736 return dependency_symbol->bind;
2737}
2738
2739/**
2740 * kmod_module_dependency_symbols_free_list:
2741 * @list: kmod module dependency_symbols list
2742 *
2743 * Release the resources taken by @list
2744 */
2745KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2746{
2747 while (list) {
2748 kmod_module_dependency_symbol_free(list->data);
2749 list = kmod_list_remove(list);
2750 }
2751}