blob: 2f92e16709ef35d2ac23ea3cae7d7de61d467dfa [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Lucas De Marchi8f788d52011-11-25 01:22:56 -02005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02008 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchi8f788d52011-11-25 01:22:56 -020010 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Lucas De Marchi7636e722011-12-01 17:56:03 -020021#include <assert.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020022#include <stdio.h>
23#include <stdlib.h>
24#include <stddef.h>
25#include <stdarg.h>
26#include <unistd.h>
27#include <errno.h>
28#include <string.h>
29#include <ctype.h>
30#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020031#include <limits.h>
32#include <dirent.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020033#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/mman.h>
Kees Cook144d1822013-02-18 12:02:32 -080036#include <sys/syscall.h>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020037#include <sys/wait.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020038#include <string.h>
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020039#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020040
Kees Cook144d1822013-02-18 12:02:32 -080041#ifdef HAVE_LINUX_MODULE_H
42#include <linux/module.h>
43#endif
44
Lucas De Marchi8f788d52011-11-25 01:22:56 -020045#include "libkmod.h"
Lucas De Marchi83b855a2013-07-04 16:13:11 -030046#include "libkmod-internal.h"
Lucas De Marchi8f788d52011-11-25 01:22:56 -020047
48/**
Lucas De Marchi66819512012-01-09 04:47:40 -020049 * SECTION:libkmod-module
50 * @short_description: operate on kernel modules
51 */
52
53/**
Lucas De Marchi8f788d52011-11-25 01:22:56 -020054 * kmod_module:
55 *
56 * Opaque object representing a module.
57 */
58struct kmod_module {
59 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020060 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020061 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020062 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020063 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020064 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020065 const char *install_commands; /* owned by kmod_config */
66 const char *remove_commands; /* owned by kmod_config */
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020067 char *alias; /* only set if this module was created from an alias */
Lucas De Marchi1eff9422012-10-18 01:36:33 -030068 struct kmod_file *file;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020069 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020070 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020071 struct {
72 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020073 bool options : 1;
74 bool install_commands : 1;
75 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020076 } init;
Lucas De Marchib1a51252012-01-29 01:49:09 -020077
78 /*
79 * private field used by kmod_module_get_probe_list() to detect
80 * dependency loops
81 */
Lucas De Marchiece09aa2012-01-18 01:26:44 -020082 bool visited : 1;
Lucas De Marchi89e92482012-01-29 02:35:46 -020083
84 /*
85 * set by kmod_module_get_probe_list: indicates for probe_insert()
86 * whether the module's command and softdep should be ignored
87 */
88 bool ignorecmd : 1;
Lucas De Marchi38052742012-02-16 20:43:16 -020089
90 /*
91 * if module was created by searching the modules.builtin file, this
92 * is set. There's nothing much useful one can do with such a
93 * "module", except knowing it's builtin.
94 */
95 bool builtin : 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020096};
97
Lucas De Marchic35347f2011-12-12 10:48:02 -020098static inline const char *path_join(const char *path, size_t prefixlen,
99 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200100{
101 size_t pathlen;
102
103 if (path[0] == '/')
104 return path;
105
106 pathlen = strlen(path);
107 if (prefixlen + pathlen + 1 >= PATH_MAX)
108 return NULL;
109
110 memcpy(buf + prefixlen, path, pathlen + 1);
111 return buf;
112}
113
Dave Reisneraf9572c2012-02-02 11:07:33 -0500114static inline bool module_is_inkernel(struct kmod_module *mod)
115{
116 int state = kmod_module_get_initstate(mod);
Lucas De Marchi38052742012-02-16 20:43:16 -0200117
Dave Reisneraf9572c2012-02-02 11:07:33 -0500118 if (state == KMOD_MODULE_LIVE ||
Dave Reisneraf9572c2012-02-02 11:07:33 -0500119 state == KMOD_MODULE_BUILTIN)
120 return true;
Lucas De Marchi38052742012-02-16 20:43:16 -0200121
122 return false;
Dave Reisneraf9572c2012-02-02 11:07:33 -0500123}
124
Lucas De Marchi671d4892011-12-05 20:23:05 -0200125int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200126{
127 struct kmod_ctx *ctx = mod->ctx;
128 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200129 const char *dirname;
130 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200131 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200132 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200133 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200134
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200135 if (mod->init.dep)
136 return mod->n_dep;
137 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200138 mod->init.dep = true;
139
140 p = strchr(line, ':');
141 if (p == NULL)
142 return 0;
143
Lucas De Marchi671d4892011-12-05 20:23:05 -0200144 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200145 dirname = kmod_get_dirname(mod->ctx);
146 dirnamelen = strlen(dirname);
147 if (dirnamelen + 2 >= PATH_MAX)
148 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200149
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200150 memcpy(buf, dirname, dirnamelen);
151 buf[dirnamelen] = '/';
152 dirnamelen++;
153 buf[dirnamelen] = '\0';
154
155 if (mod->path == NULL) {
156 const char *str = path_join(line, dirnamelen, buf);
157 if (str == NULL)
158 return 0;
159 mod->path = strdup(str);
160 if (mod->path == NULL)
161 return 0;
162 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200163
Lucas De Marchi7636e722011-12-01 17:56:03 -0200164 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200165 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
166 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200167 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200168 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200169
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200170 path = path_join(p, dirnamelen, buf);
171 if (path == NULL) {
172 ERR(ctx, "could not join path '%s' and '%s'.\n",
173 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200174 goto fail;
175 }
176
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200177 err = kmod_module_new_from_path(ctx, path, &depmod);
178 if (err < 0) {
179 ERR(ctx, "ctx=%p path=%s error=%s\n",
180 ctx, path, strerror(-err));
181 goto fail;
182 }
183
184 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200185
Lucas De Marchib94a7372011-12-26 20:10:49 -0200186 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200187 n++;
188 }
189
190 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
191
192 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200193 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200194 return n;
195
196fail:
197 kmod_module_unref_list(list);
198 mod->init.dep = false;
199 return err;
200}
201
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200202void kmod_module_set_visited(struct kmod_module *mod, bool visited)
203{
204 mod->visited = visited;
205}
206
Lucas De Marchi38052742012-02-16 20:43:16 -0200207void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
208{
209 mod->builtin = builtin;
210}
211
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200212/*
213 * Memory layout with alias:
214 *
215 * struct kmod_module {
216 * hashkey -----.
217 * alias -----. |
218 * name ----. | |
219 * } | | |
220 * name <----------' | |
221 * alias <-----------' |
222 * name\alias <--------'
223 *
224 * Memory layout without alias:
225 *
226 * struct kmod_module {
227 * hashkey ---.
228 * alias -----|----> NULL
229 * name ----. |
230 * } | |
231 * name <----------'-'
232 *
233 * @key is "name\alias" or "name" (in which case alias == NULL)
234 */
235static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
236 const char *name, size_t namelen,
237 const char *alias, size_t aliaslen,
238 struct kmod_module **mod)
239{
240 struct kmod_module *m;
241 size_t keylen;
242
243 m = kmod_pool_get_module(ctx, key);
244 if (m != NULL) {
245 *mod = kmod_module_ref(m);
246 return 0;
247 }
248
249 if (alias == NULL)
250 keylen = namelen;
251 else
252 keylen = namelen + aliaslen + 1;
253
254 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
Lucas De Marchi9f025612013-11-18 11:52:53 -0200255 if (m == NULL)
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200256 return -ENOMEM;
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200257
258 memset(m, 0, sizeof(*m));
259
260 m->ctx = kmod_ref(ctx);
261 m->name = (char *)m + sizeof(*m);
262 memcpy(m->name, key, keylen + 1);
263 if (alias == NULL) {
264 m->hashkey = m->name;
265 m->alias = NULL;
266 } else {
267 m->name[namelen] = '\0';
268 m->alias = m->name + namelen + 1;
269 m->hashkey = m->name + keylen + 1;
270 memcpy(m->hashkey, key, keylen + 1);
271 }
272
273 m->refcount = 1;
274 kmod_pool_add_module(ctx, m, m->hashkey);
275 *mod = m;
276
277 return 0;
278}
279
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200280/**
281 * kmod_module_new_from_name:
282 * @ctx: kmod library context
283 * @name: name of the module
284 * @mod: where to save the created struct kmod_module
285 *
286 * Create a new struct kmod_module using the module name. @name can not be an
287 * alias, file name or anything else; it must be a module name. There's no
Dan McGee9a252c22012-02-03 20:29:07 -0600288 * check if the module exists in the system.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200289 *
290 * This function is also used internally by many others that return a new
291 * struct kmod_module or a new list of modules.
292 *
293 * The initial refcount is 1, and needs to be decremented to release the
294 * resources of the kmod_module. Since libkmod keeps track of all
295 * kmod_modules created, they are all released upon @ctx destruction too. Do
296 * not unref @ctx before all the desired operations with the returned
297 * kmod_module are done.
298 *
299 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
300 * module name or if memory allocation failed.
301 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200302KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
303 const char *name,
304 struct kmod_module **mod)
305{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200306 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200307 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200308
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200309 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200310 return -ENOENT;
311
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200312 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200313
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200314 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200315}
316
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200317int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
318 const char *name, struct kmod_module **mod)
319{
320 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200321 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200322 size_t namelen = strlen(name);
323 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200324
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200325 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200326 return -ENAMETOOLONG;
327
328 memcpy(key, name, namelen);
329 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200330 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200331
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200332 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200333 if (err < 0)
334 return err;
335
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200336 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200337}
338
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200339/**
340 * kmod_module_new_from_path:
341 * @ctx: kmod library context
342 * @path: path where to find the given module
343 * @mod: where to save the created struct kmod_module
344 *
345 * Create a new struct kmod_module using the module path. @path must be an
346 * existent file with in the filesystem and must be accessible to libkmod.
347 *
348 * The initial refcount is 1, and needs to be decremented to release the
349 * resources of the kmod_module. Since libkmod keeps track of all
350 * kmod_modules created, they are all released upon @ctx destruction too. Do
351 * not unref @ctx before all the desired operations with the returned
352 * kmod_module are done.
353 *
354 * If @path is relative, it's treated as relative to the current working
355 * directory. Otherwise, give an absolute path.
356 *
357 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
358 * it's not a valid file for a kmod_module or if memory allocation failed.
359 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200360KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
361 const char *path,
362 struct kmod_module **mod)
363{
364 struct kmod_module *m;
365 int err;
366 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200367 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200368 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200369 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200370
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200371 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200372 return -ENOENT;
373
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200374 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200375 if (abspath == NULL) {
376 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200377 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200378 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200379
380 err = stat(abspath, &st);
381 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200382 err = -errno;
383 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200384 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200385 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200386 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200387
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200388 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200389 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200390 free(abspath);
391 return -ENOENT;
392 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200393
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200394 m = kmod_pool_get_module(ctx, name);
395 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200396 if (m->path == NULL)
397 m->path = abspath;
398 else if (streq(m->path, abspath))
399 free(abspath);
400 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200401 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 -0200402 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200403 free(abspath);
404 return -EEXIST;
405 }
406
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200407 *mod = kmod_module_ref(m);
408 return 0;
409 }
410
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200411 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
412 if (err < 0)
413 return err;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200414
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200415 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200416 *mod = m;
417
418 return 0;
419}
420
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200421/**
422 * kmod_module_unref:
423 * @mod: kmod module
424 *
425 * Drop a reference of the kmod module. If the refcount reaches zero, its
426 * resources are released.
427 *
428 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
429 * returns the passed @mod with its refcount decremented.
430 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200431KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
432{
433 if (mod == NULL)
434 return NULL;
435
436 if (--mod->refcount > 0)
437 return mod;
438
439 DBG(mod->ctx, "kmod_module %p released\n", mod);
440
Lucas De Marchi4084c172011-12-15 13:43:22 -0200441 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200442 kmod_module_unref_list(mod->dep);
Lucas De Marchi1eff9422012-10-18 01:36:33 -0300443
444 if (mod->file)
445 kmod_file_unref(mod->file);
446
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200447 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200448 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200449 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200450 free(mod);
451 return NULL;
452}
453
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200454/**
455 * kmod_module_ref:
456 * @mod: kmod module
457 *
458 * Take a reference of the kmod module, incrementing its refcount.
459 *
460 * Returns: the passed @module with its refcount incremented.
461 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200462KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
463{
464 if (mod == NULL)
465 return NULL;
466
467 mod->refcount++;
468
469 return mod;
470}
471
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200472#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
473 do { \
474 if ((_err) < 0) \
475 goto _label_err; \
476 if (*(_list) != NULL) \
477 goto finish; \
478 } while (0)
479
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200480/**
481 * kmod_module_new_from_lookup:
482 * @ctx: kmod library context
483 * @given_alias: alias to look for
484 * @list: an empty list where to save the list of modules matching
485 * @given_alias
486 *
487 * Create a new list of kmod modules using an alias or module name and lookup
488 * libkmod's configuration files and indexes in order to find the module.
489 * Once it's found in one of the places, it stops searching and create the
490 * list of modules that is saved in @list.
491 *
492 * The search order is: 1. aliases in configuration file; 2. module names in
493 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
494 * in modules.alias index.
495 *
496 * The initial refcount is 1, and needs to be decremented to release the
497 * resources of the kmod_module. The returned @list must be released by
498 * calling kmod_module_unref_list(). Since libkmod keeps track of all
499 * kmod_modules created, they are all released upon @ctx destruction too. Do
500 * not unref @ctx before all the desired operations with the returned list are
501 * completed.
502 *
503 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
504 * methods failed, which is basically due to memory allocation fail. If module
505 * is not found, it still returns 0, but @list is an empty list.
506 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200507KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200508 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200509 struct kmod_list **list)
510{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200511 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200512 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200513
Lucas De Marchi4308b172011-12-13 10:26:04 -0200514 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200515 return -ENOENT;
516
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200517 if (list == NULL || *list != NULL) {
518 ERR(ctx, "An empty list is needed to create lookup\n");
519 return -ENOSYS;
520 }
521
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200522 if (alias_normalize(given_alias, alias, NULL) < 0) {
523 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200524 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200525 }
526
527 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200528
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200529 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200530 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200531 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200532
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200533 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200534 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
535 CHECK_ERR_AND_FINISH(err, fail, list, finish);
536
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200537 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200538 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
539 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200540
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200541 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200542 err = kmod_lookup_alias_from_commands(ctx, alias, list);
543 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200544
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200545 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200546 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
547 CHECK_ERR_AND_FINISH(err, fail, list, finish);
548
Lucas De Marchi38052742012-02-16 20:43:16 -0200549 DBG(ctx, "lookup modules.builtin %s\n", alias);
550 err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
551 CHECK_ERR_AND_FINISH(err, fail, list, finish);
552
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200553finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200554 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200555 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200556fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200557 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200558 kmod_module_unref_list(*list);
559 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200560 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200561}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200562#undef CHECK_ERR_AND_FINISH
563
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200564/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200565 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200566 * @list: list of kmod modules
567 *
568 * Drop a reference of each kmod module in @list and releases the resources
569 * taken by the list itself.
570 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800571 * Returns: 0
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200572 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200573KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
574{
575 for (; list != NULL; list = kmod_list_remove(list))
576 kmod_module_unref(list->data);
577
578 return 0;
579}
580
Lucas De Marchi0d467432011-12-31 11:15:52 -0200581/**
582 * kmod_module_get_filtered_blacklist:
583 * @ctx: kmod library context
584 * @input: list of kmod_module to be filtered with blacklist
585 * @output: where to save the new list
586 *
Kay Sievers471a7d02012-04-14 20:47:47 +0200587 * This function should not be used. Use kmod_module_apply_filter instead.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500588 *
Lucas De Marchi0d467432011-12-31 11:15:52 -0200589 * Given a list @input, this function filter it out with config's blacklist
Dave Reisnerd80b1032012-02-24 10:05:11 -0500590 * and save it in @output.
Lucas De Marchi0d467432011-12-31 11:15:52 -0200591 *
592 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
593 * list.
594 */
595KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
596 const struct kmod_list *input,
597 struct kmod_list **output)
598{
Dave Reisnerd80b1032012-02-24 10:05:11 -0500599 return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
Lucas De Marchi0d467432011-12-31 11:15:52 -0200600}
601
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200602static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
603{
604 if (!mod->init.dep) {
605 /* lazy init */
606 char *line = kmod_search_moddep(mod->ctx, mod->name);
607
608 if (line == NULL)
609 return NULL;
610
611 kmod_module_parse_depline((struct kmod_module *)mod, line);
612 free(line);
613
614 if (!mod->init.dep)
615 return NULL;
616 }
617
618 return mod->dep;
619}
620
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200621/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200622 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200623 * @mod: kmod module
624 *
625 * Search the modules.dep index to find the dependencies of the given @mod.
626 * The result is cached in @mod, so subsequent calls to this function will
627 * return the already searched list of modules.
628 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800629 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
630 * that can be released by calling kmod_module_unref_list().
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200631 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200632KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200633{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200634 struct kmod_list *l, *l_new, *list_new = NULL;
635
636 if (mod == NULL)
637 return NULL;
638
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200639 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200640
641 kmod_list_foreach(l, mod->dep) {
642 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
643 if (l_new == NULL) {
644 kmod_module_unref(l->data);
645 goto fail;
646 }
647
648 list_new = l_new;
649 }
650
651 return list_new;
652
653fail:
654 ERR(mod->ctx, "out of memory\n");
655 kmod_module_unref_list(list_new);
656 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200657}
658
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200659/**
660 * kmod_module_get_module:
661 * @entry: an entry in a list of kmod modules.
662 *
663 * Get the kmod module of this @entry in the list, increasing its refcount.
664 * After it's used, unref it. Since the refcount is incremented upon return,
665 * you still have to call kmod_module_unref_list() to release the list of kmod
666 * modules.
667 *
668 * Returns: NULL on failure or the kmod_module contained in this list entry
669 * with its refcount incremented.
670 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200671KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200672{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200673 if (entry == NULL)
674 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200675
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200676 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200677}
678
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200679/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200680 * kmod_module_get_name:
681 * @mod: kmod module
682 *
683 * Get the name of this kmod module. Name is always available, independently
684 * if it was created by kmod_module_new_from_name() or another function and
685 * it's always normalized (dashes are replaced with underscores).
686 *
687 * Returns: the name of this kmod module.
688 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200689KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200690{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200691 if (mod == NULL)
692 return NULL;
693
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200694 return mod->name;
695}
696
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200697/**
698 * kmod_module_get_path:
699 * @mod: kmod module
700 *
701 * Get the path of this kmod module. If this kmod module was not created by
702 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200703 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200704 *
705 * Returns: the path of this kmod module or NULL if such information is not
706 * available.
707 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200708KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200709{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200710 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200711
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200712 if (mod == NULL)
713 return NULL;
714
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200715 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200716
Lucas De Marchie005fac2011-12-08 10:42:34 -0200717 if (mod->path != NULL)
718 return mod->path;
719 if (mod->init.dep)
720 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200721
Lucas De Marchie005fac2011-12-08 10:42:34 -0200722 /* lazy init */
723 line = kmod_search_moddep(mod->ctx, mod->name);
724 if (line == NULL)
725 return NULL;
726
727 kmod_module_parse_depline((struct kmod_module *) mod, line);
728 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200729
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200730 return mod->path;
731}
732
733
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200734extern long delete_module(const char *name, unsigned int flags);
735
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200736/**
737 * kmod_module_remove_module:
738 * @mod: kmod module
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500739 * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
Chengwei Yangd7152f62013-05-04 17:07:03 +0800740 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
741 * use by a kernel subsystem or other process;
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500742 * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
743 * delete_module(2).
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200744 *
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
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500757 /* Filter out other flags and force ONONBLOCK */
758 flags &= KMOD_REMOVE_FORCE;
759 flags |= KMOD_REMOVE_NOWAIT;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200760
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200761 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200762 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200763 err = -errno;
764 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200765 }
766
Lucas De Marchiba998b92012-01-11 00:08:14 -0200767 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200768}
769
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200770extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200771
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200772/**
773 * kmod_module_insert_module:
774 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200775 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +0800776 * behavior of this function, valid flags are
777 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
778 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200779 * @options: module's options to pass to Linux Kernel.
780 *
781 * Insert a module in Linux kernel. It opens the file pointed by @mod,
782 * mmap'ing it and passing to kernel.
783 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200784 * Returns: 0 on success or < 0 on failure. If module is already loaded it
785 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200786 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200787KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200788 unsigned int flags,
789 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200790{
791 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200792 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200793 off_t size;
794 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200795 struct kmod_elf *elf = NULL;
796 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200797 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200798
799 if (mod == NULL)
800 return -ENOENT;
801
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200802 path = kmod_module_get_path(mod);
803 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500804 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200805 return -ENOSYS;
806 }
807
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200808 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200809 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200810 err = -errno;
811 return err;
812 }
813
Kees Cook144d1822013-02-18 12:02:32 -0800814 if (kmod_file_get_direct(file)) {
815 unsigned int kernel_flags = 0;
816
Kees Cook144d1822013-02-18 12:02:32 -0800817 if (flags & KMOD_INSERT_FORCE_VERMAGIC)
818 kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
819 if (flags & KMOD_INSERT_FORCE_MODVERSION)
820 kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
Kees Cook144d1822013-02-18 12:02:32 -0800821
822 err = finit_module(kmod_file_get_fd(file), args, kernel_flags);
823 if (err == 0 || errno != ENOSYS)
824 goto init_finished;
825 }
826
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200827 size = kmod_file_get_size(file);
828 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200829
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200830 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
831 elf = kmod_elf_new(mem, size);
832 if (elf == NULL) {
833 err = -errno;
834 goto elf_failed;
835 }
836
837 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
838 err = kmod_elf_strip_section(elf, "__versions");
839 if (err < 0)
840 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
841 }
842
843 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
844 err = kmod_elf_strip_vermagic(elf);
845 if (err < 0)
846 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
847 }
848
849 mem = kmod_elf_get_memory(elf);
850 }
851
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200852 err = init_module(mem, size, args);
Kees Cook144d1822013-02-18 12:02:32 -0800853init_finished:
Lucas De Marchibbf59322011-12-30 14:13:33 -0200854 if (err < 0) {
855 err = -errno;
856 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
857 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200858
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200859 if (elf != NULL)
860 kmod_elf_unref(elf);
861elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200862 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200863
864 return err;
865}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200866
Lucas De Marchiddbda022011-12-27 11:40:10 -0200867static bool module_is_blacklisted(struct kmod_module *mod)
868{
869 struct kmod_ctx *ctx = mod->ctx;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300870 const struct kmod_config *config = kmod_get_config(ctx);
871 const struct kmod_list *bl = config->blacklists;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200872 const struct kmod_list *l;
873
874 kmod_list_foreach(l, bl) {
875 const char *modname = kmod_blacklist_get_modname(l);
876
877 if (streq(modname, mod->name))
878 return true;
879 }
880
881 return false;
882}
883
Dave Reisnerd80b1032012-02-24 10:05:11 -0500884/**
885 * kmod_module_apply_filter
886 * @ctx: kmod library context
Chengwei Yangd7152f62013-05-04 17:07:03 +0800887 * @filter_type: bitmask to filter modules out, valid types are
888 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
889 * KMOD_FILTER_BUILTIN: filter builtin modules out.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500890 * @input: list of kmod_module to be filtered
891 * @output: where to save the new list
892 *
893 * Given a list @input, this function filter it out by the filter mask
894 * and save it in @output.
895 *
896 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
897 * list.
898 */
899KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
900 enum kmod_filter filter_type,
901 const struct kmod_list *input,
902 struct kmod_list **output)
903{
904 const struct kmod_list *li;
905
906 if (ctx == NULL || output == NULL)
907 return -ENOENT;
908
909 *output = NULL;
910 if (input == NULL)
911 return 0;
912
913 kmod_list_foreach(li, input) {
914 struct kmod_module *mod = li->data;
915 struct kmod_list *node;
916
917 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
918 module_is_blacklisted(mod))
919 continue;
920
Dave Reisnerbdda7e12012-02-24 23:02:06 -0500921 if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin)
Dave Reisnerd80b1032012-02-24 10:05:11 -0500922 continue;
923
924 node = kmod_list_append(*output, mod);
925 if (node == NULL)
926 goto fail;
927
928 *output = node;
929 kmod_module_ref(mod);
930 }
931
932 return 0;
933
934fail:
935 kmod_module_unref_list(*output);
936 *output = NULL;
937 return -ENOMEM;
938}
939
Lucas De Marchiddbda022011-12-27 11:40:10 -0200940static int command_do(struct kmod_module *mod, const char *type,
941 const char *cmd)
942{
943 const char *modname = kmod_module_get_name(mod);
944 int err;
945
946 DBG(mod->ctx, "%s %s\n", type, cmd);
947
948 setenv("MODPROBE_MODULE", modname, 1);
949 err = system(cmd);
950 unsetenv("MODPROBE_MODULE");
951
952 if (err == -1 || WEXITSTATUS(err)) {
953 ERR(mod->ctx, "Error running %s command for %s\n",
954 type, modname);
955 if (err != -1)
956 err = -WEXITSTATUS(err);
957 }
958
959 return err;
960}
961
Lucas De Marchib1a51252012-01-29 01:49:09 -0200962struct probe_insert_cb {
963 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
964 void *data;
965};
966
Lucas De Marchiddbda022011-12-27 11:40:10 -0200967static int module_do_install_commands(struct kmod_module *mod,
968 const char *options,
969 struct probe_insert_cb *cb)
970{
971 const char *command = kmod_module_get_install_commands(mod);
Lucas De Marchi9f025612013-11-18 11:52:53 -0200972 char *p;
973 _cleanup_free_ char *cmd;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200974 int err;
975 size_t cmdlen, options_len, varlen;
976
977 assert(command);
978
979 if (options == NULL)
980 options = "";
981
982 options_len = strlen(options);
983 cmdlen = strlen(command);
984 varlen = sizeof("$CMDLINE_OPTS") - 1;
985
986 cmd = memdup(command, cmdlen + 1);
987 if (cmd == NULL)
988 return -ENOMEM;
989
990 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
991 size_t prefixlen = p - cmd;
992 size_t suffixlen = cmdlen - prefixlen - varlen;
993 size_t slen = cmdlen - varlen + options_len;
994 char *suffix = p + varlen;
995 char *s = malloc(slen + 1);
Lucas De Marchi9f025612013-11-18 11:52:53 -0200996 if (!s)
Lucas De Marchiddbda022011-12-27 11:40:10 -0200997 return -ENOMEM;
Lucas De Marchi9f025612013-11-18 11:52:53 -0200998
Lucas De Marchiddbda022011-12-27 11:40:10 -0200999 memcpy(s, cmd, p - cmd);
1000 memcpy(s + prefixlen, options, options_len);
1001 memcpy(s + prefixlen + options_len, suffix, suffixlen);
1002 s[slen] = '\0';
1003
1004 free(cmd);
1005 cmd = s;
1006 cmdlen = slen;
1007 }
1008
1009 if (cb->run_install != NULL)
1010 err = cb->run_install(mod, cmd, cb->data);
1011 else
1012 err = command_do(mod, "install", cmd);
1013
Lucas De Marchiddbda022011-12-27 11:40:10 -02001014 return err;
1015}
1016
Lucas De Marchiddbda022011-12-27 11:40:10 -02001017static char *module_options_concat(const char *opt, const char *xopt)
1018{
1019 // TODO: we might need to check if xopt overrides options on opt
1020 size_t optlen = opt == NULL ? 0 : strlen(opt);
1021 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1022 char *r;
1023
1024 if (optlen == 0 && xoptlen == 0)
1025 return NULL;
1026
1027 r = malloc(optlen + xoptlen + 2);
1028
1029 if (opt != NULL) {
1030 memcpy(r, opt, optlen);
1031 r[optlen] = ' ';
1032 optlen++;
1033 }
1034
1035 if (xopt != NULL)
1036 memcpy(r + optlen, xopt, xoptlen);
1037
1038 r[optlen + xoptlen] = '\0';
1039
1040 return r;
1041}
1042
Lucas De Marchib1a51252012-01-29 01:49:09 -02001043static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001044 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001045 struct kmod_list **list);
1046
1047/* re-entrant */
1048static int __kmod_module_fill_softdep(struct kmod_module *mod,
1049 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001050{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001051 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001052 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001053
1054 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001055 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001056 ERR(mod->ctx, "could not get softdep: %s\n",
1057 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001058 goto fail;
1059 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001060
Lucas De Marchib1a51252012-01-29 01:49:09 -02001061 kmod_list_foreach(l, pre) {
1062 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001063 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001064 if (err < 0)
1065 goto fail;
1066 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001067
Lucas De Marchib1a51252012-01-29 01:49:09 -02001068 l = kmod_list_append(*list, kmod_module_ref(mod));
1069 if (l == NULL) {
1070 kmod_module_unref(mod);
1071 err = -ENOMEM;
1072 goto fail;
1073 }
1074 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001075 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001076
Lucas De Marchib1a51252012-01-29 01:49:09 -02001077 kmod_list_foreach(l, post) {
1078 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001079 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001080 if (err < 0)
1081 goto fail;
1082 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001083
Lucas De Marchib1a51252012-01-29 01:49:09 -02001084fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001085 kmod_module_unref_list(pre);
1086 kmod_module_unref_list(post);
1087
1088 return err;
1089}
1090
Lucas De Marchib1a51252012-01-29 01:49:09 -02001091/* re-entrant */
1092static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001093 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001094 struct kmod_list **list)
1095{
1096 struct kmod_list *dep, *l;
1097 int err = 0;
1098
1099 if (mod->visited) {
1100 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1101 mod->name);
1102 return 0;
1103 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001104 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001105
1106 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001107 kmod_list_foreach(l, dep) {
1108 struct kmod_module *m = l->data;
1109 err = __kmod_module_fill_softdep(m, list);
1110 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001111 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001112 }
1113
Lucas De Marchi89e92482012-01-29 02:35:46 -02001114 if (ignorecmd) {
1115 l = kmod_list_append(*list, kmod_module_ref(mod));
1116 if (l == NULL) {
1117 kmod_module_unref(mod);
1118 err = -ENOMEM;
1119 goto finish;
1120 }
1121 *list = l;
1122 mod->ignorecmd = true;
1123 } else
1124 err = __kmod_module_fill_softdep(mod, list);
1125
Lucas De Marchib1a51252012-01-29 01:49:09 -02001126finish:
1127 kmod_module_unref_list(dep);
1128 return err;
1129}
1130
1131static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001132 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001133 struct kmod_list **list)
1134{
1135 int err;
1136
1137 assert(mod != NULL);
1138 assert(list != NULL && *list == NULL);
1139
1140 /*
1141 * Make sure we don't get screwed by previous calls to this function
1142 */
1143 kmod_set_modules_visited(mod->ctx, false);
1144
Lucas De Marchi89e92482012-01-29 02:35:46 -02001145 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001146 if (err < 0) {
1147 kmod_module_unref_list(*list);
1148 *list = NULL;
1149 }
1150
1151 return err;
1152}
1153
Lucas De Marchiddbda022011-12-27 11:40:10 -02001154/**
1155 * kmod_module_probe_insert_module:
1156 * @mod: kmod module
1157 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +08001158 * behavior of this function, valid flags are
1159 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1160 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1161 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1162 * commands and softdeps configured in the system;
1163 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1164 * live in kernel or not;
1165 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1166 * associated callback function;
1167 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1168 * and the module is already live in kernel, the function will fail if this
1169 * flag is specified;
1170 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1171 * filter to this module and its dependencies. If any of the dependencies (or
1172 * the module) is blacklisted, the probe will fail, unless the blacklisted
1173 * module is already live in kernel;
1174 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1175 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1176 * alias and is blacklisted.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001177 * @extra_options: module's options to pass to Linux Kernel. It applies only
1178 * to @mod, not to its dependencies.
1179 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001180 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001181 * @print_action: function to call with the action being taken (install or
1182 * insmod). It's useful for tools like modprobe when running with verbose
1183 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001184 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001185 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001186 * install commands and applying blacklist.
1187 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001188 * If @run_install is NULL, this function will fork and exec by calling
1189 * system(3). Don't pass a NULL argument in @run_install if your binary is
1190 * setuid/setgid (see warning in system(3)). If you need control over the
1191 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001192 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001193 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1194 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001195 */
1196KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1197 unsigned int flags, const char *extra_options,
1198 int (*run_install)(struct kmod_module *m,
1199 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001200 const void *data,
1201 void (*print_action)(struct kmod_module *m,
1202 bool install,
1203 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001204{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001205 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001206 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001207 int err;
1208
1209 if (mod == NULL)
1210 return -ENOENT;
1211
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001212 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1213 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001214 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001215 return -EEXIST;
1216 else
1217 return 0;
1218 }
1219
Lucas De Marchi68820172012-08-17 09:38:05 -03001220 /*
1221 * Ugly assignement + check. We need to check if we were told to check
1222 * blacklist and also return the reason why we failed.
1223 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1224 * module is an alias, so we also need to check it
1225 */
1226 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1227 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1228 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001229 if (module_is_blacklisted(mod))
1230 return err;
1231 }
1232
Lucas De Marchi89e92482012-01-29 02:35:46 -02001233 err = kmod_module_get_probe_list(mod,
1234 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001235 if (err < 0)
1236 return err;
1237
1238 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1239 struct kmod_list *filtered = NULL;
1240
Dave Reisnerd80b1032012-02-24 10:05:11 -05001241 err = kmod_module_apply_filter(mod->ctx,
1242 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001243 if (err < 0)
1244 return err;
1245
1246 kmod_module_unref_list(list);
1247 if (filtered == NULL)
1248 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1249
1250 list = filtered;
1251 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001252
1253 cb.run_install = run_install;
1254 cb.data = (void *) data;
1255
Lucas De Marchib1a51252012-01-29 01:49:09 -02001256 kmod_list_foreach(l, list) {
1257 struct kmod_module *m = l->data;
1258 const char *moptions = kmod_module_get_options(m);
1259 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001260 char *options;
1261
1262 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1263 && module_is_inkernel(m)) {
1264 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1265 m->name);
1266 err = -EEXIST;
1267 goto finish_module;
1268 }
1269
1270 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001271 m == mod ? extra_options : NULL);
1272
Lucas De Marchi89e92482012-01-29 02:35:46 -02001273 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001274 if (print_action != NULL)
1275 print_action(m, true, options ?: "");
1276
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001277 if (!(flags & KMOD_PROBE_DRY_RUN))
1278 err = module_do_install_commands(m, options,
1279 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001280 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001281 if (print_action != NULL)
1282 print_action(m, false, options ?: "");
1283
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001284 if (!(flags & KMOD_PROBE_DRY_RUN))
1285 err = kmod_module_insert_module(m, flags,
1286 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001287 }
1288
1289 free(options);
1290
Lucas De Marchiabd55572012-02-19 04:20:30 -02001291finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001292 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001293 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001294 * already loaded and the module being loaded is not a softdep
1295 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001296 *
1297 * We need to check here because of race conditions. We
1298 * checked first if module was already loaded but it may have
1299 * been loaded between the check and the moment we try to
1300 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001301 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001302 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001303 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001304 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001305
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001306 if (err == -EEXIST)
1307 err = 0;
1308 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001309 break;
1310 }
1311
1312 kmod_module_unref_list(list);
1313 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001314}
1315
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001316/**
1317 * kmod_module_get_options:
1318 * @mod: kmod module
1319 *
1320 * Get options of this kmod module. Options come from the configuration file
1321 * and are cached in @mod. The first call to this function will search for
1322 * this module in configuration and subsequent calls return the cached string.
1323 *
1324 * Returns: a string with all the options separated by spaces. This string is
1325 * owned by @mod, do not free it.
1326 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001327KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1328{
1329 if (mod == NULL)
1330 return NULL;
1331
1332 if (!mod->init.options) {
1333 /* lazy init */
1334 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001335 const struct kmod_list *l;
1336 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001337 char *opts = NULL;
1338 size_t optslen = 0;
1339
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001340 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001341
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001342 kmod_list_foreach(l, config->options) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001343 const char *modname = kmod_option_get_modname(l);
1344 const char *str;
1345 size_t len;
1346 void *tmp;
1347
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001348 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1349 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1350 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001351 continue;
1352
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001353 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 -02001354 str = kmod_option_get_options(l);
1355 len = strlen(str);
1356 if (len < 1)
1357 continue;
1358
1359 tmp = realloc(opts, optslen + len + 2);
1360 if (tmp == NULL) {
1361 free(opts);
1362 goto failed;
1363 }
1364
1365 opts = tmp;
1366
1367 if (optslen > 0) {
1368 opts[optslen] = ' ';
1369 optslen++;
1370 }
1371
1372 memcpy(opts + optslen, str, len);
1373 optslen += len;
1374 opts[optslen] = '\0';
1375 }
1376
1377 m->init.options = true;
1378 m->options = opts;
1379 }
1380
1381 return mod->options;
1382
1383failed:
1384 ERR(mod->ctx, "out of memory\n");
1385 return NULL;
1386}
1387
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001388/**
1389 * kmod_module_get_install_commands:
1390 * @mod: kmod module
1391 *
1392 * Get install commands for this kmod module. Install commands come from the
1393 * configuration file and are cached in @mod. The first call to this function
1394 * will search for this module in configuration and subsequent calls return
1395 * the cached string. The install commands are returned as they were in the
1396 * configuration, concatenated by ';'. No other processing is made in this
1397 * string.
1398 *
1399 * Returns: a string with all install commands separated by semicolons. This
1400 * string is owned by @mod, do not free it.
1401 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001402KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1403{
1404 if (mod == NULL)
1405 return NULL;
1406
1407 if (!mod->init.install_commands) {
1408 /* lazy init */
1409 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001410 const struct kmod_list *l;
1411 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001412
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001413 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001414
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001415 kmod_list_foreach(l, config->install_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001416 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001417
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001418 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001419 continue;
1420
Lucas De Marchi60f67602011-12-16 03:33:26 -02001421 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001422
Lucas De Marchi60f67602011-12-16 03:33:26 -02001423 /*
1424 * find only the first command, as modprobe from
1425 * module-init-tools does
1426 */
1427 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001428 }
1429
1430 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001431 }
1432
1433 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001434}
1435
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001436void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1437{
1438 mod->init.install_commands = true;
1439 mod->install_commands = cmd;
1440}
1441
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001442static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1443{
1444 struct kmod_list *ret = NULL;
1445 unsigned i;
1446
1447 for (i = 0; i < count; i++) {
1448 const char *depname = array[i];
1449 struct kmod_list *lst = NULL;
1450 int err;
1451
1452 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1453 if (err < 0) {
1454 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1455 continue;
1456 } else if (lst != NULL)
1457 ret = kmod_list_append_list(ret, lst);
1458 }
1459 return ret;
1460}
1461
1462/**
1463 * kmod_module_get_softdeps:
1464 * @mod: kmod module
1465 * @pre: where to save the list of preceding soft dependencies.
1466 * @post: where to save the list of post soft dependencies.
1467 *
1468 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001469 * from configuration file and are not cached in @mod because it may include
1470 * dependency cycles that would make we leak kmod_module. Any call
1471 * to this function will search for this module in configuration, allocate a
1472 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001473 *
1474 * Both @pre and @post are newly created list of kmod_module and
1475 * should be unreferenced with kmod_module_unref_list().
1476 *
1477 * Returns: 0 on success or < 0 otherwise.
1478 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001479KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1480 struct kmod_list **pre,
1481 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001482{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001483 const struct kmod_list *l;
1484 const struct kmod_config *config;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001485
1486 if (mod == NULL || pre == NULL || post == NULL)
1487 return -ENOENT;
1488
1489 assert(*pre == NULL);
1490 assert(*post == NULL);
1491
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001492 config = kmod_get_config(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001493
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001494 kmod_list_foreach(l, config->softdeps) {
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001495 const char *modname = kmod_softdep_get_name(l);
1496 const char * const *array;
1497 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001498
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001499 if (fnmatch(modname, mod->name, 0) != 0)
1500 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001501
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001502 array = kmod_softdep_get_pre(l, &count);
1503 *pre = lookup_softdep(mod->ctx, array, count);
1504 array = kmod_softdep_get_post(l, &count);
1505 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001506
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001507 /*
1508 * find only the first command, as modprobe from
1509 * module-init-tools does
1510 */
1511 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001512 }
1513
1514 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001515}
1516
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001517/**
1518 * kmod_module_get_remove_commands:
1519 * @mod: kmod module
1520 *
1521 * Get remove commands for this kmod module. Remove commands come from the
1522 * configuration file and are cached in @mod. The first call to this function
1523 * will search for this module in configuration and subsequent calls return
1524 * the cached string. The remove commands are returned as they were in the
1525 * configuration, concatenated by ';'. No other processing is made in this
1526 * string.
1527 *
1528 * Returns: a string with all remove commands separated by semicolons. This
1529 * string is owned by @mod, do not free it.
1530 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001531KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1532{
1533 if (mod == NULL)
1534 return NULL;
1535
1536 if (!mod->init.remove_commands) {
1537 /* lazy init */
1538 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001539 const struct kmod_list *l;
1540 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001541
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001542 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001543
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001544 kmod_list_foreach(l, config->remove_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001545 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001546
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001547 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001548 continue;
1549
Lucas De Marchi60f67602011-12-16 03:33:26 -02001550 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001551
Lucas De Marchi60f67602011-12-16 03:33:26 -02001552 /*
1553 * find only the first command, as modprobe from
1554 * module-init-tools does
1555 */
1556 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001557 }
1558
1559 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001560 }
1561
1562 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001563}
1564
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001565void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1566{
1567 mod->init.remove_commands = true;
1568 mod->remove_commands = cmd;
1569}
1570
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001571/**
1572 * SECTION:libkmod-loaded
1573 * @short_description: currently loaded modules
1574 *
1575 * Information about currently loaded modules, as reported by Linux kernel.
1576 * These information are not cached by libkmod and are always read from /sys
1577 * and /proc/modules.
1578 */
1579
1580/**
1581 * kmod_module_new_from_loaded:
1582 * @ctx: kmod library context
1583 * @list: where to save the list of loaded modules
1584 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001585 * Create a new list of kmod modules with all modules currently loaded in
1586 * kernel. It uses /proc/modules to get the names of loaded modules and to
1587 * create kmod modules by calling kmod_module_new_from_name() in each of them.
Chengwei Yang491c4902013-05-04 17:07:02 +08001588 * They are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001589 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001590 * The initial refcount is 1, and needs to be decremented to release the
1591 * resources of the kmod_module. The returned @list must be released by
1592 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1593 * kmod_modules created, they are all released upon @ctx destruction too. Do
1594 * not unref @ctx before all the desired operations with the returned list are
1595 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001596 *
1597 * Returns: 0 on success or < 0 on error.
1598 */
1599KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1600 struct kmod_list **list)
1601{
1602 struct kmod_list *l = NULL;
1603 FILE *fp;
1604 char line[4096];
1605
1606 if (ctx == NULL || list == NULL)
1607 return -ENOENT;
1608
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001609 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001610 if (fp == NULL) {
1611 int err = -errno;
1612 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1613 return err;
1614 }
1615
1616 while (fgets(line, sizeof(line), fp)) {
1617 struct kmod_module *m;
1618 struct kmod_list *node;
1619 int err;
1620 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1621
1622 err = kmod_module_new_from_name(ctx, name, &m);
1623 if (err < 0) {
1624 ERR(ctx, "could not get module from name '%s': %s\n",
1625 name, strerror(-err));
1626 continue;
1627 }
1628
1629 node = kmod_list_append(l, m);
1630 if (node)
1631 l = node;
1632 else {
1633 ERR(ctx, "out of memory\n");
1634 kmod_module_unref(m);
1635 }
1636 }
1637
1638 fclose(fp);
1639 *list = l;
1640
1641 return 0;
1642}
1643
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001644/**
1645 * kmod_module_initstate_str:
1646 * @state: the state as returned by kmod_module_get_initstate()
1647 *
1648 * Translate a initstate to a string.
1649 *
1650 * Returns: the string associated to the @state. This string is statically
1651 * allocated, do not free it.
1652 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001653KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1654{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001655 switch (state) {
1656 case KMOD_MODULE_BUILTIN:
1657 return "builtin";
1658 case KMOD_MODULE_LIVE:
1659 return "live";
1660 case KMOD_MODULE_COMING:
1661 return "coming";
1662 case KMOD_MODULE_GOING:
1663 return "going";
1664 default:
1665 return NULL;
1666 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001667}
1668
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001669/**
1670 * kmod_module_get_initstate:
1671 * @mod: kmod module
1672 *
1673 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1674 * /sys filesystem.
1675 *
Chengwei Yangd7152f62013-05-04 17:07:03 +08001676 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1677 * KMOD_MODULE_BUILTIN: module is builtin;
1678 * KMOD_MODULE_LIVE: module is live in kernel;
1679 * KMOD_MODULE_COMING: module is being loaded;
1680 * KMOD_MODULE_GOING: module is being unloaded.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001681 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001682KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1683{
1684 char path[PATH_MAX], buf[32];
1685 int fd, err, pathlen;
1686
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001687 if (mod == NULL)
1688 return -ENOENT;
1689
Lucas De Marchi38052742012-02-16 20:43:16 -02001690 if (mod->builtin)
1691 return KMOD_MODULE_BUILTIN;
1692
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001693 pathlen = snprintf(path, sizeof(path),
1694 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001695 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001696 if (fd < 0) {
1697 err = -errno;
1698
Lucas De Marchiddbda022011-12-27 11:40:10 -02001699 DBG(mod->ctx, "could not open '%s': %s\n",
1700 path, strerror(-err));
1701
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001702 if (pathlen > (int)sizeof("/initstate") - 1) {
1703 struct stat st;
1704 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1705 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1706 return KMOD_MODULE_BUILTIN;
1707 }
1708
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001709 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001710 path, strerror(-err));
1711 return err;
1712 }
1713
1714 err = read_str_safe(fd, buf, sizeof(buf));
1715 close(fd);
1716 if (err < 0) {
1717 ERR(mod->ctx, "could not read from '%s': %s\n",
1718 path, strerror(-err));
1719 return err;
1720 }
1721
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001722 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001723 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001724 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001725 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001726 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001727 return KMOD_MODULE_GOING;
1728
1729 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1730 return -EINVAL;
1731}
1732
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001733/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001734 * kmod_module_get_size:
1735 * @mod: kmod module
1736 *
Dave Reisner486f9012012-06-28 09:09:48 -04001737 * Get the size of this kmod module as returned by Linux kernel. If supported,
1738 * the size is read from the coresize attribute in /sys/module. For older
1739 * kernels, this falls back on /proc/modules and searches for the specified
1740 * module to get its size.
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001741 *
1742 * Returns: the size of this kmod module.
1743 */
1744KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1745{
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001746 FILE *fp;
1747 char line[4096];
1748 int lineno = 0;
1749 long size = -ENOENT;
Dave Reisner486f9012012-06-28 09:09:48 -04001750 int dfd, cfd;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001751
1752 if (mod == NULL)
1753 return -ENOENT;
1754
Dave Reisner486f9012012-06-28 09:09:48 -04001755 /* try to open the module dir in /sys. If this fails, don't
1756 * bother trying to find the size as we know the module isn't
1757 * loaded.
1758 */
1759 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
1760 dfd = open(line, O_RDONLY);
1761 if (dfd < 0)
1762 return -errno;
1763
1764 /* available as of linux 3.3.x */
1765 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1766 if (cfd >= 0) {
1767 if (read_str_long(cfd, &size, 10) < 0)
1768 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1769 close(cfd);
1770 goto done;
1771 }
1772
1773 /* fall back on parsing /proc/modules */
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001774 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001775 if (fp == NULL) {
1776 int err = -errno;
1777 ERR(mod->ctx,
1778 "could not open /proc/modules: %s\n", strerror(errno));
1779 return err;
1780 }
1781
1782 while (fgets(line, sizeof(line), fp)) {
1783 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1784 long value;
1785
1786 lineno++;
1787 if (tok == NULL || !streq(tok, mod->name))
1788 continue;
1789
1790 tok = strtok_r(NULL, " \t", &saveptr);
1791 if (tok == NULL) {
1792 ERR(mod->ctx,
1793 "invalid line format at /proc/modules:%d\n", lineno);
1794 break;
1795 }
1796
1797 value = strtol(tok, &endptr, 10);
1798 if (endptr == tok || *endptr != '\0') {
1799 ERR(mod->ctx,
1800 "invalid line format at /proc/modules:%d\n", lineno);
1801 break;
1802 }
1803
1804 size = value;
1805 break;
1806 }
1807 fclose(fp);
Dave Reisner486f9012012-06-28 09:09:48 -04001808
1809done:
1810 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001811 return size;
1812}
1813
1814/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001815 * kmod_module_get_refcnt:
1816 * @mod: kmod module
1817 *
1818 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1819 * /sys filesystem.
1820 *
1821 * Returns: 0 on success or < 0 on failure.
1822 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001823KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1824{
1825 char path[PATH_MAX];
1826 long refcnt;
1827 int fd, err;
1828
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001829 if (mod == NULL)
1830 return -ENOENT;
1831
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001832 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001833 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001834 if (fd < 0) {
1835 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001836 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001837 path, strerror(errno));
1838 return err;
1839 }
1840
1841 err = read_str_long(fd, &refcnt, 10);
1842 close(fd);
1843 if (err < 0) {
1844 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1845 path, strerror(-err));
1846 return err;
1847 }
1848
1849 return (int)refcnt;
1850}
1851
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001852/**
1853 * kmod_module_get_holders:
1854 * @mod: kmod module
1855 *
1856 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1857 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1858 *
1859 * Returns: a new list of kmod modules on success or NULL on failure.
1860 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001861KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1862{
1863 char dname[PATH_MAX];
1864 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001865 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001866 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001867
Lucas De Marchib9a7da32013-04-23 20:47:28 -03001868 if (mod == NULL || mod->ctx == NULL)
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001869 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001870
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001871 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1872
1873 d = opendir(dname);
1874 if (d == NULL) {
1875 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001876 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001877 return NULL;
1878 }
1879
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001880 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001881 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001882 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001883 int err;
1884
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001885 if (dent->d_name[0] == '.') {
1886 if (dent->d_name[1] == '\0' ||
1887 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001888 continue;
1889 }
1890
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001891 err = kmod_module_new_from_name(mod->ctx, dent->d_name,
1892 &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001893 if (err < 0) {
1894 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001895 dent->d_name, strerror(-err));
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001896 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001897 }
1898
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001899 l = kmod_list_append(list, holder);
1900 if (l != NULL) {
1901 list = l;
1902 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001903 ERR(mod->ctx, "out of memory\n");
1904 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001905 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001906 }
1907 }
1908
1909 closedir(d);
1910 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001911
1912fail:
1913 closedir(d);
1914 kmod_module_unref_list(list);
1915 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001916}
1917
1918struct kmod_module_section {
1919 unsigned long address;
1920 char name[];
1921};
1922
1923static void kmod_module_section_free(struct kmod_module_section *section)
1924{
1925 free(section);
1926}
1927
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001928/**
1929 * kmod_module_get_sections:
1930 * @mod: kmod module
1931 *
1932 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1933 * structure contained in this list is internal to libkmod and their fields
1934 * can be obtained by calling kmod_module_section_get_name() and
1935 * kmod_module_section_get_address().
1936 *
1937 * After use, free the @list by calling kmod_module_section_free_list().
1938 *
1939 * Returns: a new list of kmod module sections on success or NULL on failure.
1940 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001941KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1942{
1943 char dname[PATH_MAX];
1944 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001945 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001946 DIR *d;
1947 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001948
1949 if (mod == NULL)
1950 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001951
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001952 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1953
1954 d = opendir(dname);
1955 if (d == NULL) {
1956 ERR(mod->ctx, "could not open '%s': %s\n",
1957 dname, strerror(errno));
1958 return NULL;
1959 }
1960
1961 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001962
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001963 for (dent = readdir(d); dent; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001964 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001965 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001966 unsigned long address;
1967 size_t namesz;
1968 int fd, err;
1969
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001970 if (dent->d_name[0] == '.') {
1971 if (dent->d_name[1] == '\0' ||
1972 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001973 continue;
1974 }
1975
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001976 fd = openat(dfd, dent->d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001977 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001978 ERR(mod->ctx, "could not open '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001979 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001980 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001981 }
1982
1983 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001984 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001985 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001986 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001987 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001988 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001989 }
1990
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001991 namesz = strlen(dent->d_name) + 1;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001992 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001993
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001994 if (section == NULL) {
1995 ERR(mod->ctx, "out of memory\n");
1996 goto fail;
1997 }
1998
1999 section->address = address;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002000 memcpy(section->name, dent->d_name, namesz);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002001
2002 l = kmod_list_append(list, section);
2003 if (l != NULL) {
2004 list = l;
2005 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002006 ERR(mod->ctx, "out of memory\n");
2007 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002008 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002009 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002010 }
2011
2012 closedir(d);
2013 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002014
2015fail:
2016 closedir(d);
2017 kmod_module_unref_list(list);
2018 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002019}
2020
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002021/**
2022 * kmod_module_section_get_module_name:
2023 * @entry: a list entry representing a kmod module section
2024 *
2025 * Get the name of a kmod module section.
2026 *
2027 * After use, free the @list by calling kmod_module_section_free_list().
2028 *
2029 * Returns: the name of this kmod module section on success or NULL on
2030 * failure. The string is owned by the section, do not free it.
2031 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002032KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2033{
2034 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002035
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002036 if (entry == NULL)
2037 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002038
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002039 section = entry->data;
2040 return section->name;
2041}
2042
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002043/**
2044 * kmod_module_section_get_address:
2045 * @entry: a list entry representing a kmod module section
2046 *
2047 * Get the address of a kmod module section.
2048 *
2049 * After use, free the @list by calling kmod_module_section_free_list().
2050 *
2051 * Returns: the address of this kmod module section on success or ULONG_MAX
2052 * on failure.
2053 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002054KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2055{
2056 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002057
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002058 if (entry == NULL)
2059 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002060
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002061 section = entry->data;
2062 return section->address;
2063}
2064
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002065/**
2066 * kmod_module_section_free_list:
2067 * @list: kmod module section list
2068 *
2069 * Release the resources taken by @list
2070 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002071KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2072{
2073 while (list) {
2074 kmod_module_section_free(list->data);
2075 list = kmod_list_remove(list);
2076 }
2077}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002078
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002079static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2080{
2081 if (mod->file == NULL) {
2082 const char *path = kmod_module_get_path(mod);
2083
2084 if (path == NULL) {
2085 errno = ENOENT;
2086 return NULL;
2087 }
2088
2089 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2090 path);
2091 if (mod->file == NULL)
2092 return NULL;
2093 }
2094
2095 return kmod_file_get_elf(mod->file);
2096}
2097
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002098struct kmod_module_info {
2099 char *key;
2100 char value[];
2101};
2102
2103static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2104{
2105 struct kmod_module_info *info;
2106
2107 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2108 if (info == NULL)
2109 return NULL;
2110
2111 info->key = (char *)info + sizeof(struct kmod_module_info)
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002112 + valuelen + 1;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002113 memcpy(info->key, key, keylen);
2114 info->key[keylen] = '\0';
2115 memcpy(info->value, value, valuelen);
2116 info->value[valuelen] = '\0';
2117 return info;
2118}
2119
2120static void kmod_module_info_free(struct kmod_module_info *info)
2121{
2122 free(info);
2123}
2124
Michal Marekf64458c2013-01-16 10:18:17 +01002125static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2126{
2127 struct kmod_module_info *info;
2128 struct kmod_list *n;
2129
2130 info = kmod_module_info_new(key, keylen, value, valuelen);
Michal Marek63339342013-01-16 17:51:57 +01002131 if (info == NULL)
Michal Marekf64458c2013-01-16 10:18:17 +01002132 return NULL;
Michal Marekf64458c2013-01-16 10:18:17 +01002133 n = kmod_list_append(*list, info);
Michal Marek63339342013-01-16 17:51:57 +01002134 if (n != NULL)
2135 *list = n;
2136 else
Michal Marekf64458c2013-01-16 10:18:17 +01002137 kmod_module_info_free(info);
Michal Marekf64458c2013-01-16 10:18:17 +01002138 return n;
2139}
2140
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002141/**
2142 * kmod_module_get_info:
2143 * @mod: kmod module
2144 * @list: where to return list of module information. Use
2145 * kmod_module_info_get_key() and
2146 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002147 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002148 *
2149 * Get a list of entries in ELF section ".modinfo", these contain
2150 * alias, license, depends, vermagic and other keys with respective
Michal Marek8fe16812013-01-16 09:52:01 +01002151 * values. If the module is signed (CONFIG_MODULE_SIG), information
2152 * about the module signature is included as well: signer,
2153 * sig_key and sig_hashalgo.
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002154 *
2155 * After use, free the @list by calling kmod_module_info_free_list().
2156 *
2157 * Returns: 0 on success or < 0 otherwise.
2158 */
2159KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2160{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002161 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002162 char **strings;
Michal Marekf64458c2013-01-16 10:18:17 +01002163 int i, count, ret = -ENOMEM;
Michal Marek8fe16812013-01-16 09:52:01 +01002164 struct kmod_signature_info sig_info;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002165
2166 if (mod == NULL || list == NULL)
2167 return -ENOENT;
2168
2169 assert(*list == NULL);
2170
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002171 elf = kmod_module_get_elf(mod);
2172 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002173 return -errno;
2174
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002175 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002176 if (count < 0)
2177 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002178
2179 for (i = 0; i < count; i++) {
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002180 struct kmod_list *n;
2181 const char *key, *value;
2182 size_t keylen, valuelen;
2183
2184 key = strings[i];
2185 value = strchr(key, '=');
2186 if (value == NULL) {
2187 keylen = strlen(key);
2188 valuelen = 0;
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002189 value = key;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002190 } 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/**
Chengwei Yang491c4902013-05-04 17:07:02 +08002393 * kmod_module_version_get_symbol:
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002394 * @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}